Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Exploring Future Node

Exploring Future Node

Expoloring Future Node @ jsconf.asia 2016

Yosuke Furukawa

November 26, 2016
Tweet

More Decks by Yosuke Furukawa

Other Decks in Programming

Transcript

  1. My favorite features (v4.x ~ v6.x) • inspect mode with

    Chrome • Intl built-in Object support • stable and performance
  2. Intl built-in Object > new Intl.NumberFormat('en').format(10000000) ’10,000,000' > new Intl.NumberFormat('en',

    { style: 'currency', currency: 'USD' }).format(10000000) '$10,000,000' > new Intl.DateTimeFormat('ja-JP-u-ca- japanese').format(new Date()); 'ฏ੒28೥11݄24೔' > new Intl.DateTimeFormat('en').format(new Date()); '11/24/2016'
  3. Before async-await: callback const fs = require('fs'); const copy =

    (from, to, done) => { fs.readFile(from, (err, data) => { if (err) return done(err); fs.writeFile(to, data, (err) => { if (err) return done(err); done(null); }); }); }; copy('./foo.txt', './bar.txt', (err) => err ? console.error(err) : console.log('done'))
  4. Before async-await: Promise const fs = require('fs'); const promisify =

    require('./promisify'); const readFile = promisify(fs.readFile); const writeFile = promisify(fs.writeFile); const copy = (from, to) => { return readFile(from).then( (data) => writeFile(to, data)); }; copy('./foo.txt', './bar.txt').then(() => console.log('done')).catch(console.error);
  5. async-await // --harmony_async_await const fs = require('fs'); const promisify =

    require('./promisify'); const readFile = promisify(fs.readFile); const writeFile = promisify(fs.writeFile); const copy = async function(from, to) { const data = await readFile(from); await writeFile(to, data); }; copy('./foo.txt', './bar.txt').then(() => console.log('done')).catch(console.error);
  6. New URL Parser • current URL parser const url =

    require('url'); // Create URL Object const u = url.parse('https://www.example.com/path/ foo/bar'); // Stringify URL Object url.format(u);
  7. New URL Parser • new URL Parser is same to

    Browser URL API const URL = require('url').URL; // Create URL Object const u = new URL('https://www.example.com/path/ foo/bar'); // Stringify URL Object u.toString();
  8. New URL Parser • new URL Parser is same to

    Browser URL API const URL = require('url').URL; // Create URL Object const u = new URL('https://www.example.com/path/ foo/bar'); // Stringify URL Object u.toString(); 8)"58(63-
  9. HTTP/2 Support • HTTP/2 is new version of HTTP •

    RFC7540 is already published by IETF • Release Target: Node v8
  10. HTTP/2 Goal • Decrease Latency • Data Compression of HTTP

    Header (hpack) • Fixing Head-of-Line Blocking • Server Push • Prioritized Request
  11. Request Header becoming huge NFUIPE (&5 TDIFNF IUUQT IPTU FYBNQMFDPN

    QBUI GPPCBSCB[ VTFSBHFOU .P[JMMBʜ DPPLJF TFTTJPOJEYYYY
  12. Request Header becoming huge .P[JMMB DPNQBUJCMF.4*& 8JOEPXT/547 .P[JMMB 8JOEPXT/58JOY 

    "QQMF8FC,JU ,)5.- MJLF(FDLP  $ISPNF4BGBSJ&EHF 
  13. Data Compression of HTTP Header 3FRVFTU)FBEFST NFUIPE (&5 TDIFNF IUUQT

    IPTU FYBNQMFDPN QBUI GPPCBSCB[ VTFSBHFOU .P[JMMB $PNQSFTTJPO%JDUJPOBSZ  BVUIPSJUZ  NFUIPE (&5 ʜ  TDIFNF IUUQT ʜ  IPTU FYBNQMFDPN  VTFSBHFOU .P[JMMBʜ $PNQSFTTFE)FBEFS     )V⒎NBO GPPCBSCB[ 
  14. HTTP/2 on Node inside MJCVW IUUQQBSTFS 7 DBSFT 0QFO44- [MJC

    OHIUUQ OFXMJCSBSZUPDSFBUF )551DMJFOUTFSWFS
  15. HTTP/2 Support // plain http support const http2 = require('http').HTTP2;

    const server = http2.createServer((req, res) => { res.end('hello plain text http2'); }); server.listen(3000);
  16. HTTP/2 Support const http2 = require('http').HTTP2; const fs = require('fs');

    const path = require('path'); const options = { key: fs.readFileSync( path.resolve('.', 'keys/agent2-key.pem')), cert: fs.readFileSync( path.resolve('.', 'keys/agent2-cert.pem')) }; const server = http2.createSecureServer(options, (req, res) => { res.end('hello http2'); }); server.listen(3010);
  17. HTTP/2 Implementation Status • Strongly Work In Progress • Some

    missing APIs • We have not supported HTTP/2 feature like Server Push/Prioritization yet • We need help!!! • https://github.com/nodejs/http2
  18. ES Modules interoperability is…? • Node.js uses own CommonJS-like module

    load system. • However JavaScript defines new module system in ES2015. import/export • We need to show users to options to choose both CommonJS and ES Modules style.
  19. ES Modules Goal • new Declarative Grammar • Tools friendly,

    easy to detect errors • Engines friendly, easy to optimize • new Strict Rules • Module is new Script.
  20. Tools Friendly, Easy to detect errors export default function ()

    { return 'foo'; } export default function () => { return 'foo'; } // Syntax Error, export default is duplicated
  21. Tools Friendly, Easy to detect errors // export.js module.exports.foo =

    () => { return 'foo' } // import.js const bar = require('./export.js').bar; // bar is undefined
  22. Tools Friendly, Easy to detect errors // export.js export function

    foo() {} // import.js import { bar } from './export.js'; // Syntax Error, bar is not found.
  23. Engines Friendly, Easy to optimize • MicroSoft Edge experimentally implements

    ES Modules. • The Browser Engine (Chakra) is optimizing to lookup ES Modules.
  24. ES Modules: new Script • Module is new Script •

    Module has some differences with current Script • implicit Strict Mode • Different Scope (top level this : undefined) • reserved keywords ( await )
  25. ES Modules // Syntax Error under strict mode, you cannot

    use with. with (obj) {} // undefined, top level this is undefined not window console.log(this); // it is not window.foo, just module local scope var foo = 1; // Syntax Error, await is reserved in ES Modules var await = 'foo';
  26. Discussion, Discusssion, Discussion… • Loader Spec is under discussion •

    ES Modules new spec is also under discussion • Node Interop is also under discussion…
  27. new extension?: .mjs • Current Node Proposals looks at file

    extension • `.js` is Script • `.mjs` is Module • this is still discussing on Node-EPS repository. • https://github.com/nodejs/node-eps/
  28. Note: Babel Behavior is not standard. • if you are

    using import/export using transpilers like babel, you may change your code to use native ES Modules in the future. The Journey to ES Modules
  29. Features are based on Standard • Past Node.js • Intl

    built-in Object • Present Node.js • ES 2016+ support • new URL Support • Future Node.js • HTTP/2 • ES Modules &$." &$." 8)"58( *&5' &$."
  30. Why Node needs web standard? James Snell @ NodeFest 2016


    Node.js is, and has been, primarily a platform for Web Application Development While Node.js presumes a "small core" philosophy for most things, it includes support for the most basic and critical internet standards
  31. Why Node needs standard? • We are focusing on keep

    node ecosystems better. • ECMAScript and other internet Standard will be next community standard. • Node.js would be better to follow those standardization
  32. References • You Don’t Know ES Modules by Teppei Sato

    • Node And Web Standard by James Snell • The Journey to ES Modules by Bradley Ferias • High Performance Browser Networking by Ilya Grigorik • https://github.com/nodejs/node-eps