Pro Yearly is on sale from $80 to $50! »

Node.js 2018

Node.js 2018

HTML5 Conference で発表した Node.js 2018 の話です。 #html5j

D76231a2114896dfcc7b79ac69558b79?s=128

Yosuke Furukawa

November 25, 2018
Tweet

Transcript

  1. Node 2018 2018/11/23 @ HTML5j

  2. Twitter: @yosuke_furukawa Github: yosuke-furukawa

  3. Node.js v11 released!!!

  4. Node.js Core Policy

  5. Node.js Core Policy small core, less is more

  6. None
  7. Less is More Focusing on Stability, Security, Performance

  8. Stability

  9. Stability (Node.js Core)

  10. None
  11. Long Term Support Ͱෆ۩߹͸2೥ؒमਖ਼͞ΕΔ ·ͨɺηΩϡϦςΟ໰୊͸͞Βʹ3೥ؒमਖ਼͞ΕΔ

  12. — V8 ͸ Node.js ඇޓ׵ͷमਖ਼Λ͢Δͱ͖͸ࣄલʹ ௨஌͢Δɻ

  13. V8ࣗ਎͕NodeΛforkͯ͠࠷৽ΞοϓσʔτΛͯ͠΋ ςετյΕͳ͍͜ͱΛอূͯ͠Δɻ

  14. ຖPRຖʹCIͰςετ͢Δͷ͸΋ͪΖΜɺશCPUɾϓ ϥοτϑΥʔϜͰϏϧυͯ͠͏·͍͘͘͜ͱΛ֬ೝ͢Δɻ

  15. ͨ·ʹյΕΔςετ (flaky test) ʹؔͯ͠͸Ͳͷϓϥο τϑΥʔϜͰى͖Δ͔ௐࠪ͠ɺमਖ਼͍ͯ͘͠ྲྀΕ

  16. Stability (Node.js Application)

  17. None
  18. CPUɾϝϞϦ౳ͷϓϩϑΝΠϥʔ͕͋Γɺ͜ΕΛ࢖͏ͱ ϘτϧωοΫΛௐࠪͰ͖Δɻ

  19. None
  20. None
  21. Performance

  22. Performance priority is high

  23. ඞͣఆظతʹbenchmarkΛऔಘ͠ɺ஗͘ͳͬͯͳ͍͜ ͱΛௐࠪ͢Δɻ

  24. ϚΠΫϩϕϯνϚʔΫ͚ͩͰ͸ͳ͘ɺExpressΛ࢖ͬͨ ͋Δఔ౓ͷن໛ͷΞϓϦͰ΋ςετ͢Δɻ

  25. keep v8 fresh

  26. v8 ͷόʔδϣϯ͸ϝδϟʔ όʔδϣϯϦϦʔε࣌·Ͱͷ Stable Chrome ʹར༻͞Εͯ Δv8Λར༻ɻ

  27. ʢ༨ஊʣͳͷͰɺ࣮͸ES unshippedͷػೳͱ͔࢖͑Δ ʢv11ͩͱprivate fieldͱ͔ BigIntͱ͔ʣ

  28. Worker

  29. Worker • Thread Λ࢖͑ΔΑ͏ʹ͢ΔͨΊͷ΋ͷɻ • Node.js Ͱ multi thread ϓϩάϥϛϯά͕Ͱ

    ͖Δʢ͍ͭʹʣ
  30. Worker

  31. Worker

  32. ͪͳΈʹ࠷ۙೖͬͨ llhttpͱ͍͏ύʔα͕΍͹͍ (·ͩexperimental)

  33. llhttp ͸ Node.js v11.2͔Βೖͬͨ ৽͍͠ύʔα $ node --experimental- http-parser test.js

    # Ͱ࣮ߦՄೳɻ
  34. ͕͜͜΍͹͍Αllhttp • TypeScript ͔Β LLVM bitcodeੜ੒͢Δɻϥούʔ Λॻ͍ͯC͔Β΋ݺ΂ΔΑ͏ʹ͍ͯ͠Δɻ • (ී௨(?) C͔ΒLLVMʹͯ͠WASMʹͯ͠JSͰݺ΂Δ

    Α͏ʹ͍ͬͯ͏ٯͷΞϓϩʔνΛऔΓͦ͏͕ͩɺ TypeScript(JS)͔ΒLLVM bitcodeੜ੒ͯ͠ɺCͷϥ ΠϒϥϦͱͯ͠ݺ΂ΔΑ͏ʹͨ͠ͱ͍͏ॴ͕΍͹͍ Μ͚ͩͲɺҰ୴΍͹͞͸ॻ͖͖Εͳ͍ɻɻɻ)
  35. llhttp performance ഒ͘Β͍ૣ ͍ʢͳ͔ͥʣ

  36. Security

  37. Security (Node Core)

  38. Node.jsࣗ਎ͷηΩϡϦςΟ੬ ऑੑ͸TSCͰඞͣ࿩͞ΕͯΔɻ

  39. ͍ͭ࠷ۙ͋ͬͨϦϦʔεɺOpenSSLͷ੬ऑੑ͕ൃݟ͞ ΕͨͷͰύονόʔδϣϯͰશମʹల։ɻ

  40. Security Release͸੬ऑੑ͕ใ ࠂ͞ΕΔ౓ʹमਖ਼ύον͕࡞Β Εͯใࠂ͞ΕΔΑ͏ʹͳͬͯΔ

  41. Security (Ecosystem)

  42. npm audit ͸੬ऑੑ͕ใࠂ͞ΕͯΔϥΠϒϥϦ͕ ࣗ෼ͷύοέʔδ಺ʹଘࡏ͢Δ͔Λௐࠪͯ͘͠ΕΔػೳ

  43. ͪͳΈʹ yarn ʹ΋ audit ͸௥Ճ͞ΕͯΔʢsnyk.io ͱ ͍͏σʔλϕʔεͰnpmͷ੬ऑੑσʔλϕʔεͱҧ͏ ͕ʣ

  44. Less is More Ͱػೳ௥Ճ͸͠ ͳ͍ͱݴͬͯ΋ྫ֎͸͋Δɻ

  45. Web Standards

  46. 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
  47. Node.js v10.0.0 ~ v10.12.0 • HTTP/2 • ES Modules •

    Promise in node core • Stream/Promisesͷ਌࿨ੑվળ
  48. Node.js v11 • url module ͕ deprecated ʹͳΓɺ WHATWG URL

    Λར༻͢ΔΑ͏ʹ಺෦վળ͕ߦΘΕͨ • WHATWG TextEncoder/TextDecoder͕global ʹͳͬͨ • queueMicrotask API ͕࣮ݧతʹ௥Ճ͞Εͨ
  49. http2

  50. Multiplexing requests on 1 TCP connection 4FSWFS 5$1$POOFDUJPOCVUNVMUJ SFRVFTUT

  51. Multiplexing requests on 1 TCP connection 4FSWFS *GTPNFSFRVFTUTBSFTMPX #65 XIPMFSFRVFTUTBSFOPUB⒎FDUFE

  52. )551

  53. )551

  54. ES Modules

  55. ES Modules import { readFile } from 'fs'; readFile('./foo.txt', (err,

    source) => { if (err) { console.error(err); } else { console.log(source); } });
  56. ES Modules import { readFile } from 'fs'; readFile('./foo.txt', (err,

    source) => { if (err) { console.error(err); } else { console.log(source); } }); JNQPSUͰಡΈࠐΊΔ
  57. ES Modules • ·ͩ Experimental ͕ͩɺ commonjs ΋ es modules

    ΋૬ޓӡ༻Ͱ͖ΔܗͰ࣮૷͞ΕͯΔɻ • ͨͩ `.mjs` ͱ͍͏֦ுࢠ͕ඞཁʹͳΔʢࢼݧతͳ΋ ͷʣ • ࠓ͸ nodejs/modules Ͱٞ࿦த • https://github.com/nodejs/modules
  58. Promise in Node core

  59. Promise in Node Core • ͦ΋ͦ΋ Node.js ʹ͸ඇಉظॲཧͷ΍Γํ͕ෳ਺ଘࡏ͢Δɻ • Callback

    • Promise • Stream • WebͰ͸Promise͕΄΅ඪ४తͳ஍Ґɺ͜Εʹ߹Θͤͯ Promiseͱ૬ޓӡ༻Ͱ͖ΔΑ͏ʹ͍ͯ͘͠ྲྀΕɻ
  60. util.promisify const util = require('util'); const dns = require('dns'); //

    Promise Խ͞Εͨؔ਺ΛऔΕΔɻ const lookup = util.promisify(dns.lookup);
  61. fs.promises const fs = require('fs'); async function main() { //

    fs.promises͔ΒॲཧΛ࣮ߦ const content = await fs.promises.readFile("./foo.txt"); console.log(content.toString()); } main();
  62. fs.promises const fs = require('fs'); async function main() { //

    fs.promises͔ΒॲཧΛ࣮ߦ const content = await fs.promises.readFile("./foo.txt"); console.log(content.toString()); } main(); GTQSPNJTFTʹSFBE'JMFͰ$BMMCBDLͰ͸ͳ ͘1SPNJTFΛฦ͢Α͏ʹͳΔɻ
  63. fs.promises • ͜Ε΋·ͩExperimental • Promise͕Web։ൃऀʹͱͬͯ͸਌࿨ੑ͕ߴ ͘ɺχʔζ΋ڧ͍ͷͰ௥Ճ͞Εͨɻ • httpͳͲͷଞͷAPI͸·ͩ

  64. Stream/Promiseͷ਌࿨ੑվળ • for await of Ͱ Stream Λஞ࣍ॲཧͰ͖ΔΑ͏ ʹͳͬͨ •

    Node v10 ͔Β Stream ʹ௥Ճ͞Εͨ finished, pipeline API͕ Promiseͱͷ਌࿨ੑߴ͍
  65. for await of ͷվળ const fs = require('fs'); async function

    print(readable) { readable.setEncoding('utf8'); let data = ''; // for await ۟Ͱiterateͭͭ͠ɺchunkΛऔΓग़͢ for await (const k of readable) { data += k; } console.log(data); } print(fs.createReadStream('file')).catch(console.log);
  66. Stream finished API const fs = require('fs'); const { finished

    } = require('stream'); const rs = fs.createReadStream('./stream.js'); //finished API ͰΤϥʔऴྃɺਖ਼ৗऴྃʹ͔͔ΘΒͣऴྃͨ͠ΒίʔϧόοΫΛݺ΂Δɻ finished(rs, (err) => { if (err) { console.error('Stream failed', err); return; } console.log('Stream is done reading'); }); rs.resume();
  67. Stream finished API const fs = require('fs'); const util =

    require('util'); const stream = require('stream'); // Promisify Λ࢖ͬͯcallbackΛpromiseʹ͢Δ const finished = util.promisify(stream.finished); const rs = fs.createReadStream('./stream.js'); // async await ͕࢖͑Δ async function run() { await finished(rs); console.log('Stream is done reading'); } run().catch((err) => console.error('Stream failed', err)); rs.resume();
  68. Stream Pipeline API const fs = require('fs'); const rs =

    fs.createReadStream('error.txt'); const ws = fs.createWriteStream('output.txt'); rs.pipe(ws).on("error", (e) => { // ͜͜Ͱ౷ҰతʹΤϥʔΛϋϯυϦϯά͍͕ͨ͠ɺ // readstreamͰΤϥʔʹͳΔͱ͜͜ʹདྷͳ͍ɻ // ͜͏΍ͬͯॻ͔ͳͯ͘͸͍͚ͳ͍ // rs.on("error", errorHandler).pipe(ws).on("error", errorHandler) });
  69. Stream Pipeline API const { pipeline, Transform } = require('stream');

    const rs = fs.createReadStream('./stream.js'); const ts = new Transform({ transform(chunk, encoding, callback) { callback(null, chunk.toString().toUpperCase()) } }); const ws = fs.createWriteStream('./stream_cap.js'); pipeline(rs, ts, ws, (err) => { if (err) { console.error(err); return; } console.log('Stream is done reading'); }); rs.resume();
  70. Stream Pipeline API const { pipeline, Transform } = require('stream');

    const p = util.promisify(pipeline); const rs = fs.createReadStream('./stream.js'); const ts = new Transform({ transform(chunk, encoding, callback) { callback(null, chunk.toString().toUpperCase()) } }); const ws = fs.createWriteStream('./stream_cap.js'); // promisify ͔ͯ͠Β async-awaitͰ͖Δ async function run() { await p(rs, ts, ws); console.log('Stream is done reading'); } run().catch(console.error); rs.resume();
  71. Stream/Promiseͷ਌࿨ੑվળ • Stream => Promise • Promise => Stream •

    ͷͲͪΒͰ΋ӡ༻Ͱ͖ΔΑ͏ʹͳͬͨɻ
  72. Node.js Future ~~~

  73. Node.js Big News!!! • Node.js Foundation and JS Foundation intent

    to create joint organization.
  74. Node.js Big News!!! • Node.js Foundation and JS Foundation intent

    to create joint organization. Node.js Foundation ͱ JS Foundation ͸౷߹ ͞ΕͯɺҰͭʹͳΔͷΛ໨ࢦ͢ɻ
  75. Unified JavaScript Platform W3C / WHATWG Node.js ECMA

  76. Unified JavaScript Platform W3C / WHATWG Node.js ECMA W3C/WHATWG ͷ

    Web API ͱ Node.js API ͸ঃʑʹد͍ͤͯ͘ɻ͞ΒʹECMAScript͕ ͦͷඪ४ΛதԝͰݻΊ͍ͯ͘ɻ
  77. Example

  78. Unified JavaScript Platform • Web API ΋ Node.js API ΋

    ECMAScript ΋ٻΊͯ ͍Δͷ͸ "Ϣʔεέʔε" • ͞ΒʹWeb API ΋ Node API ΋ ECMAScript΋શ ෦ؙͬͱ஌͍ͬͯΔͷ͸࢓༷ࡦఆऀΑΓ΋։ൃऀ • ։ൃऀଆɺͭ·Γ๻Β͕ϢʔεέʔεΛ࡞͍͖ͬͯɺ ࢓༷ࡦఆऀଆʹϑΟʔυόοΫ͍ͯ͘͠ඞཁ͕͋Δɻ
  79. Unified JavaScript Platform • Web API ΋ Node.js API ΋

    ECMAScript ΋ٻΊͯ ͍Δͷ͸ "Ϣʔεέʔε" • ͞ΒʹWeb API ΋ Node API ΋ ECMAScript΋શ ෦ؙͬͱ஌͍ͬͯΔͷ͸࢓༷ࡦఆऀΑΓ΋։ൃऀ • ։ൃऀଆɺͭ·Γ๻Β͕ϢʔεέʔεΛ࡞͍͖ͬͯɺ ࢓༷ࡦఆऀଆʹϑΟʔυόοΫ͍ͯ͘͠ඞཁ͕͋Δɻ Ϧʔυ͍ͯ͘͠ͷ͸࢓༷ࡦఆऀ͚ͩͰ͸ͳ ͘ɺզʑͰ͢ɻ
  80. Thank you