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

Node.js 2018

Node.js 2018

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

Yosuke Furukawa

November 25, 2018
Tweet

More Decks by Yosuke Furukawa

Other Decks in Programming

Transcript

  1. Node 2018
    2018/11/23 @ HTML5j

    View full-size slide

  2. Twitter: @yosuke_furukawa
    Github: yosuke-furukawa

    View full-size slide

  3. Node.js v11 released!!!

    View full-size slide

  4. Node.js Core Policy

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  7. Stability (Node.js Core)

    View full-size slide

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

    View full-size slide


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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  13. Stability (Node.js
    Application)

    View full-size slide

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

    View full-size slide

  15. Performance priority is high

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  18. keep v8 fresh

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  23. llhttp ͸ Node.js v11.2͔Βೖͬͨ
    ৽͍͠ύʔα
    $ node --experimental-
    http-parser test.js
    # Ͱ࣮ߦՄೳɻ

    View full-size slide

  24. ͕͜͜΍͹͍Αllhttp
    • TypeScript ͔Β LLVM bitcodeੜ੒͢Δɻϥούʔ
    Λॻ͍ͯC͔Β΋ݺ΂ΔΑ͏ʹ͍ͯ͠Δɻ
    • (ී௨(?) C͔ΒLLVMʹͯ͠WASMʹͯ͠JSͰݺ΂Δ
    Α͏ʹ͍ͬͯ͏ٯͷΞϓϩʔνΛऔΓͦ͏͕ͩɺ
    TypeScript(JS)͔ΒLLVM bitcodeੜ੒ͯ͠ɺCͷϥ
    ΠϒϥϦͱͯ͠ݺ΂ΔΑ͏ʹͨ͠ͱ͍͏ॴ͕΍͹͍
    Μ͚ͩͲɺҰ୴΍͹͞͸ॻ͖͖Εͳ͍ɻɻɻ)

    View full-size slide

  25. llhttp performance
    ഒ͘Β͍ૣ
    ͍ʢͳ͔ͥʣ

    View full-size slide

  26. Security (Node Core)

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  30. Security (Ecosystem)

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  34. Web Standards

    View full-size slide

  35. 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

    View full-size slide

  36. Node.js v10.0.0 ~ v10.12.0
    • HTTP/2
    • ES Modules
    • Promise in node core
    • Stream/Promisesͷ਌࿨ੑվળ

    View full-size slide

  37. Node.js v11
    • url module ͕ deprecated ʹͳΓɺ WHATWG
    URL Λར༻͢ΔΑ͏ʹ಺෦վળ͕ߦΘΕͨ
    • WHATWG TextEncoder/TextDecoder͕global
    ʹͳͬͨ
    • queueMicrotask API ͕࣮ݧతʹ௥Ճ͞Εͨ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  41. ES Modules
    import { readFile } from 'fs';
    readFile('./foo.txt', (err, source) => {
    if (err) {
    console.error(err);
    } else {
    console.log(source);
    }
    });
    JNQPSUͰಡΈࠐΊΔ

    View full-size slide

  42. ES Modules
    • ·ͩ Experimental ͕ͩɺ commonjs ΋ es modules
    ΋૬ޓӡ༻Ͱ͖ΔܗͰ࣮૷͞ΕͯΔɻ
    • ͨͩ `.mjs` ͱ͍͏֦ுࢠ͕ඞཁʹͳΔʢࢼݧతͳ΋
    ͷʣ
    • ࠓ͸ nodejs/modules Ͱٞ࿦த
    • https://github.com/nodejs/modules

    View full-size slide

  43. Promise in Node core

    View full-size slide

  44. Promise in Node Core
    • ͦ΋ͦ΋ Node.js ʹ͸ඇಉظॲཧͷ΍Γํ͕ෳ਺ଘࡏ͢Δɻ
    • Callback
    • Promise
    • Stream
    • WebͰ͸Promise͕΄΅ඪ४తͳ஍Ґɺ͜Εʹ߹Θͤͯ
    Promiseͱ૬ޓӡ༻Ͱ͖ΔΑ͏ʹ͍ͯ͘͠ྲྀΕɻ

    View full-size slide

  45. util.promisify
    const util = require('util');
    const dns = require('dns');
    // Promise Խ͞Εͨؔ਺ΛऔΕΔɻ
    const lookup =
    util.promisify(dns.lookup);

    View full-size slide

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

    View full-size slide

  47. 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Λฦ͢Α͏ʹͳΔɻ

    View full-size slide

  48. fs.promises
    • ͜Ε΋·ͩExperimental
    • Promise͕Web։ൃऀʹͱͬͯ͸਌࿨ੑ͕ߴ
    ͘ɺχʔζ΋ڧ͍ͷͰ௥Ճ͞Εͨɻ
    • httpͳͲͷଞͷAPI͸·ͩ

    View full-size slide

  49. Stream/Promiseͷ਌࿨ੑվળ
    • for await of Ͱ Stream Λஞ࣍ॲཧͰ͖ΔΑ͏
    ʹͳͬͨ
    • Node v10 ͔Β Stream ʹ௥Ճ͞Εͨ finished,
    pipeline API͕ Promiseͱͷ਌࿨ੑߴ͍

    View full-size slide

  50. 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);

    View full-size slide

  51. 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();

    View full-size slide

  52. 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();

    View full-size slide

  53. 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)
    });

    View full-size slide

  54. 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();

    View full-size slide

  55. 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();

    View full-size slide

  56. Stream/Promiseͷ਌࿨ੑվળ
    • Stream => Promise
    • Promise => Stream
    • ͷͲͪΒͰ΋ӡ༻Ͱ͖ΔΑ͏ʹͳͬͨɻ

    View full-size slide

  57. Node.js Future ~~~

    View full-size slide

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

    View full-size slide

  59. Node.js Big News!!!
    • Node.js Foundation and JS Foundation intent
    to create joint organization.
    Node.js Foundation ͱ JS Foundation ͸౷߹
    ͞ΕͯɺҰͭʹͳΔͷΛ໨ࢦ͢ɻ

    View full-size slide

  60. Unified JavaScript Platform
    W3C / WHATWG Node.js
    ECMA

    View full-size slide

  61. Unified JavaScript Platform
    W3C / WHATWG Node.js
    ECMA
    W3C/WHATWG ͷ Web API ͱ Node.js API
    ͸ঃʑʹد͍ͤͯ͘ɻ͞ΒʹECMAScript͕
    ͦͷඪ४ΛதԝͰݻΊ͍ͯ͘ɻ

    View full-size slide

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

    View full-size slide

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

    View full-size slide