SlideShare a Scribd company logo
How To Node Core
NodeConf 2013
Pre-test: Raise
your hand if…
•You do not have Node checked
out on your computer
•You do not have Node built
•Node isn't working on your
computer right now
•If so, `make -j4` now.
Who should
contribute?
•Super star C++ haxx0rz?
•Low-level OS people?
•Close to metal?
•JavaScript ninja rockstar
gurus?
NO!* You!
•You will hit bugs.
•You are a Node programmer.
•You can fix those bugs.
•You can achieve immortality
in code.
*well, yes, those people. but ALSO you.
OSS
•Open Source Software
•Ownership of Software Stack
•If you use it, know it.
•If it's busted, fix it.
•Be involved.
TODO
•Writing good tests
•Structure (brief 50¢ tour)
•Let's Feature!
•Homework
tests
tests
•Node's tests are simple
JavaScript programs that
either throw, or don't.
•Not throwing = test pass
•Tests (mostly) go in
tests/simple/test-*.js
•No test = Rejected pull req
Running tests
•`make test` or `vcbuild test`
Runs simple tests, and jslint
•`make test-all`
`vcbuild test-all`
Runs ALL the tests,
even slow pummel tests
•./node test/simple/test-x.js
To run a single test
Bad Test Names
•test-GH4313.js
•test-rfc3523.js
•test-breaks-on-linux.js
•test-thisisnotveryreadable.js
Good Test Names
•test-https-keepalive.js
•test-url-double-encode.js
•test-tls-large-request.js
•test-http-simple.js
•test-fs-writefile.js
Good Test Output
•Should be almost nothing.
•I personally like doing
`console.log('ok')` at the
very end, just so I know it
ran fully.
•More than that is too much!
Good Test Speed
•Each test should take less
than 100ms to run in the
normal case.
•Rule of thumb:
If you can read the name
while `make test` runs, it's
taking too long.
Good Test Code
•Short and simple
•Entirely self-contained
•Do not use tests/fixtures/
unless ABSOLUTELY necessary
•assert something in
process.on('exit')
Always include
the test header
//
// copyright mumbo jumbo
//
var common = require('../common');
var assert = require('assert');
Be parallel-
friendly
•Servers listen on common.PORT
•Hard-coded ports don't play
nicely with CI servers!
Exit Gracefully
•Close servers, unref timers
•Don't process.exit()
explicitly.
•Exiting early can hide
serious problems!
Be Relevant!
•Tests FAIL without your patch
•Tests PASS with your patch
•otherwise… what's the point?
Be Relevant!
•Test the failure cases as
well as success cases.
•If something SHOULD fail,
make sure it DOES fail.
questions?
Exercise
•Write test-net-hello-world.js
•Assert that
require('net').hello()
returns the string 'world'
•Verify that test fails
structure
src/*.{cc,h}
•The C++ code, a lot
like .node addons.
•This code is unforgiving.
assert(0) on any weird stuff!
process.binding
•Private API function for
loading binding layer
•NODE_MODULE(node_foo, fn)
results in:
process.binding('foo')
lib/*.js
•JavaScript Node modules that
are bundled with the node
binary.
•"Native modules"
•Public API
•Just like normal Node modules
lib/*.js
•Wrap process.binding() APIs
in a friendly JavaScript
coating
•Validate input
•Throw on bad args,
emit('error') or cb(er) for
run-time failures
src/node.js
•Boot-straps the module system
•Figures out what main module
to load
•Sets up process object
questions?
tcp
tcp
•src/tcp_wrap.cc:
process.binding('tcp_wrap')
•lib/net.js: require('net')
•Today, that's as deep as
we'll go.
•(We'll cover pipes and ttys
in the level 2 class.)
net.Socket
•JavaScript abstraction of a
TCP connection
•Duplex stream, with methods
to do TCP type stuff
•Defined in lib/net.js
net.Server
•TCP server abstraction
•connection cb => gets a
connected net.Socket object
•call listen(cb) to bind to a
port/IP or fs socket path
•listen cb => actually
listening (async)
net.connect()
•TCP client abstraction
•Returns a net.Socket object
•Not yet connected, but in the
process of connecting
Exercise
•Add a "hello" method to
process.binding('tcp_wrap')
•Export this method from
require('net')
•Verify test passing
test-net-hello-world
questions?
http
How HTTP Works
•TCP server waits for
connections
•TCP client connects, writes
HTTP formatted request
•Server replies with HTTP
formatted response
http_parser
•process.binding('http_parser')
•C utility for parsing HTTP
•interprets a stream of bytes as
HTTP data, headers, chunked
encoding, etc.
http.Server
•Inherits from net.Server
•'request' event gets request/
response objects
•Pipes incoming Sockets
through http_parser
•wraps up response headers/
body/trailers for transport
http.request
•HTTP client abstraction
•Returns a request object,
emits 'response' with a
response
HTTP Client
•Request = ClientRequest
•Response = IncomingMessage
HTTP Server
•Request = IncomingMessage
•Response = ServerResponse
IncomingMessage
•Incoming messages are
messages that the OTHER party
is sending to YOU.
(They're COMING IN.)
•Server Request object
•Client Response object
Outgoing Message
•Outgoing messages are
messages that the YOU are
sending to the OTHER party.
(They're GOING OUT.)
•ServerResponse
•ClientRequest
questions?
let's feature!
Protips:
•Say hi to your neighbors.
•This is your team.
•Write the test first
(or don’t, it’s not like this
will land in core anyway ;)
.json(obj)
•Client:
request.json({some:'obj'})
•Server:
response.json({some:'obj'})
.json(obj)
•JSON.stringify the object
•Set headers:
content-type:application/json
content-length:<number>
•Call this.end()
•emit('error') if can't be
JSON encoded
Extra Credit
•Take optional arguments for
JSON.stringify pretty-
printing
•.json(obj, [indent])
pop quiz
What prototype?
A) IncomingMessage
B) OutgoingMessage
C) ServerResponse
D) ClientRequest
E) None of the above
What prototype?
A) IncomingMessage
B) OutgoingMessage
C) ServerResponse
D) ClientRequest
E) None of the above
ok for reals
do it now
•We'll give you about 15
minutes to add this function
•Raise your hand if you get
stuck.
•Protip: lib/_http_outgoing.js
.json(obj)
•JSON.stringify the object
•Set headers:
content-type:application/json
content-length:<number>
•Call this.end()
•emit('error') if can't be JSON
encoded
•Add optional arguments for
JSON.stringify pretty-printing
•.json(obj, [indent])
homework
Homework
Assignment #1
•Required reading:
CONTRIBUTING.md
•Sign the CLA
http://nodejs.org/cla.html
•Write good commit messages
•Be friendly on GitHub
Of course…
•It's silly to add a
response.json() method to
node-core
•This is an example only, it
clearly belongs in userland
•"Real" bug fixes are usually
much trickier
Homework
Assignment #2
•Go to
https://github.com/joyent/
node/issues
•Find a bug
•Write a test to confirm it
•Send a patch to fix it
Homework
Assignment #3
•Write this as a userland
module, with a package.json
•(optional)

More Related Content

What's hot

Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)
Ran Mizrahi
 
Fighting Ruby code smell
Fighting Ruby code smellFighting Ruby code smell
Fighting Ruby code smell
olegshpynov
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
Richard Lee
 
Fast, concurrent ruby web applications with EventMachine and EM::Synchrony
Fast, concurrent ruby web applications with EventMachine and EM::SynchronyFast, concurrent ruby web applications with EventMachine and EM::Synchrony
Fast, concurrent ruby web applications with EventMachine and EM::Synchrony
Kyle Drake
 

What's hot (20)

Intro to node and non blocking io
Intro to node and non blocking ioIntro to node and non blocking io
Intro to node and non blocking io
 
Node.js: A Guided Tour
Node.js: A Guided TourNode.js: A Guided Tour
Node.js: A Guided Tour
 
Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)
 
Scalable Web Apps
Scalable Web AppsScalable Web Apps
Scalable Web Apps
 
Intro to Node.js (v1)
Intro to Node.js (v1)Intro to Node.js (v1)
Intro to Node.js (v1)
 
Node.js, for architects - OpenSlava 2013
Node.js, for architects - OpenSlava 2013Node.js, for architects - OpenSlava 2013
Node.js, for architects - OpenSlava 2013
 
I18nize Scala programs à la gettext
I18nize Scala programs à la gettextI18nize Scala programs à la gettext
I18nize Scala programs à la gettext
 
Fighting Ruby code smell
Fighting Ruby code smellFighting Ruby code smell
Fighting Ruby code smell
 
Node.js Workshop - Sela SDP 2015
Node.js Workshop  - Sela SDP 2015Node.js Workshop  - Sela SDP 2015
Node.js Workshop - Sela SDP 2015
 
Basic Understanding and Implement of Node.js
Basic Understanding and Implement of Node.jsBasic Understanding and Implement of Node.js
Basic Understanding and Implement of Node.js
 
Introduction Node.js
Introduction Node.jsIntroduction Node.js
Introduction Node.js
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
Automating Your Daily Tasks with Scripting - RubyConf 2015 Taiwan
Automating Your Daily Tasks with Scripting - RubyConf 2015 TaiwanAutomating Your Daily Tasks with Scripting - RubyConf 2015 Taiwan
Automating Your Daily Tasks with Scripting - RubyConf 2015 Taiwan
 
RubyMotion #jbday
RubyMotion #jbdayRubyMotion #jbday
RubyMotion #jbday
 
OHHttpStubs
OHHttpStubsOHHttpStubs
OHHttpStubs
 
Fast, concurrent ruby web applications with EventMachine and EM::Synchrony
Fast, concurrent ruby web applications with EventMachine and EM::SynchronyFast, concurrent ruby web applications with EventMachine and EM::Synchrony
Fast, concurrent ruby web applications with EventMachine and EM::Synchrony
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
Node ppt
Node pptNode ppt
Node ppt
 
Dcjq node.js presentation
Dcjq node.js presentationDcjq node.js presentation
Dcjq node.js presentation
 
Understanding the Single Thread Event Loop
Understanding the Single Thread Event LoopUnderstanding the Single Thread Event Loop
Understanding the Single Thread Event Loop
 

Viewers also liked

Viewers also liked (8)

Where Node.JS Meets iOS
Where Node.JS Meets iOSWhere Node.JS Meets iOS
Where Node.JS Meets iOS
 
Ultimate Guide to 30+ API Documentation Solutions
Ultimate Guide to 30+ API Documentation SolutionsUltimate Guide to 30+ API Documentation Solutions
Ultimate Guide to 30+ API Documentation Solutions
 
Introduction to Nodejs
Introduction to NodejsIntroduction to Nodejs
Introduction to Nodejs
 
Nodejs intro
Nodejs introNodejs intro
Nodejs intro
 
Nodejs Explained with Examples
Nodejs Explained with ExamplesNodejs Explained with Examples
Nodejs Explained with Examples
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
Node Foundation Membership Overview 20160907
Node Foundation Membership Overview 20160907Node Foundation Membership Overview 20160907
Node Foundation Membership Overview 20160907
 
The Enterprise Case for Node.js
The Enterprise Case for Node.jsThe Enterprise Case for Node.js
The Enterprise Case for Node.js
 

Similar to How to-node-core

Speed geeking-lotusscript
Speed geeking-lotusscriptSpeed geeking-lotusscript
Speed geeking-lotusscript
Bill Buchan
 
T4T Training day - NodeJS
T4T Training day - NodeJST4T Training day - NodeJS
T4T Training day - NodeJS
Tim Sommer
 

Similar to How to-node-core (20)

My Little Webap - DevOpsSec is Magic
My Little Webap - DevOpsSec is MagicMy Little Webap - DevOpsSec is Magic
My Little Webap - DevOpsSec is Magic
 
Web a Quebec - JS Debugging
Web a Quebec - JS DebuggingWeb a Quebec - JS Debugging
Web a Quebec - JS Debugging
 
The Cowardly Test-o-Phobe's Guide To Testing
The Cowardly Test-o-Phobe's Guide To TestingThe Cowardly Test-o-Phobe's Guide To Testing
The Cowardly Test-o-Phobe's Guide To Testing
 
"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)
"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)
"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)
 
Testing sync engine
Testing sync engineTesting sync engine
Testing sync engine
 
Why ruby and rails
Why ruby and railsWhy ruby and rails
Why ruby and rails
 
The Transparent Web: Bridging the Chasm in Web Development
The Transparent Web: Bridging the Chasm in Web DevelopmentThe Transparent Web: Bridging the Chasm in Web Development
The Transparent Web: Bridging the Chasm in Web Development
 
Voldemort Nosql
Voldemort NosqlVoldemort Nosql
Voldemort Nosql
 
12 Step Guide to Lotuscript
12 Step Guide to Lotuscript12 Step Guide to Lotuscript
12 Step Guide to Lotuscript
 
WTF is Twisted?
WTF is Twisted?WTF is Twisted?
WTF is Twisted?
 
Speed geeking-lotusscript
Speed geeking-lotusscriptSpeed geeking-lotusscript
Speed geeking-lotusscript
 
Effective C++
Effective C++Effective C++
Effective C++
 
CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!CBDW2014 - MockBox, get ready to mock your socks off!
CBDW2014 - MockBox, get ready to mock your socks off!
 
CBDW2014 - Behavior Driven Development with TestBox
CBDW2014 - Behavior Driven Development with TestBoxCBDW2014 - Behavior Driven Development with TestBox
CBDW2014 - Behavior Driven Development with TestBox
 
Node azure
Node azureNode azure
Node azure
 
What is Node.js
What is Node.jsWhat is Node.js
What is Node.js
 
T4T Training day - NodeJS
T4T Training day - NodeJST4T Training day - NodeJS
T4T Training day - NodeJS
 
Micro Talk - Test Your S#!? !
Micro Talk - Test Your S#!? !Micro Talk - Test Your S#!? !
Micro Talk - Test Your S#!? !
 
Java script unit testing
Java script unit testingJava script unit testing
Java script unit testing
 
A tale of 3 databases
A tale of 3 databasesA tale of 3 databases
A tale of 3 databases
 

Recently uploaded

Recently uploaded (20)

JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and Planning
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
Designing for Hardware Accessibility at Comcast
Designing for Hardware Accessibility at ComcastDesigning for Hardware Accessibility at Comcast
Designing for Hardware Accessibility at Comcast
 
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeFree and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří Karpíšek
 
In-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsIn-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT Professionals
 
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
 
AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101
 
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
 
Motion for AI: Creating Empathy in Technology
Motion for AI: Creating Empathy in TechnologyMotion for AI: Creating Empathy in Technology
Motion for AI: Creating Empathy in Technology
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
Enterprise Security Monitoring, And Log Management.
Enterprise Security Monitoring, And Log Management.Enterprise Security Monitoring, And Log Management.
Enterprise Security Monitoring, And Log Management.
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 
Introduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG EvaluationIntroduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG Evaluation
 
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomSalesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
 
What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024
 
PLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. StartupsPLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. Startups
 
Connecting the Dots in Product Design at KAYAK
Connecting the Dots in Product Design at KAYAKConnecting the Dots in Product Design at KAYAK
Connecting the Dots in Product Design at KAYAK
 

How to-node-core