SlideShare a Scribd company logo
1 of 60
Download to read offline
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

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Recently uploaded (20)

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 

How to-node-core