Node.js 2018

Node.js 2018

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

D76231a2114896dfcc7b79ac69558b79?s=128

Yosuke Furukawa

November 25, 2018
Tweet

Transcript

  1. 6.
  2. 10.
  3. 12.

  4. 18.
  5. 20.
  6. 21.
  7. 29.
  8. 31.
  9. 32.
  10. 35.

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

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

    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
  13. 48.

    Node.js v10.0.0 ~ v10.12.0 • HTTP/2 • ES Modules •

    Promise in node core • Stream/Promisesͷ਌࿨ੑվળ
  14. 49.

    Node.js v11 • url module ͕ deprecated ʹͳΓɺ WHATWG URL

    Λར༻͢ΔΑ͏ʹ಺෦վળ͕ߦΘΕͨ • WHATWG TextEncoder/TextDecoder͕global ʹͳͬͨ • queueMicrotask API ͕࣮ݧతʹ௥Ճ͞Εͨ
  15. 50.
  16. 53.
  17. 54.
  18. 56.

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

    source) => { if (err) { console.error(err); } else { console.log(source); } });
  19. 57.

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

    source) => { if (err) { console.error(err); } else { console.log(source); } }); JNQPSUͰಡΈࠐΊΔ
  20. 58.

    ES Modules • ·ͩ Experimental ͕ͩɺ commonjs ΋ es modules

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

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

    • Promise • Stream • WebͰ͸Promise͕΄΅ඪ४తͳ஍Ґɺ͜Εʹ߹Θͤͯ Promiseͱ૬ޓӡ༻Ͱ͖ΔΑ͏ʹ͍ͯ͘͠ྲྀΕɻ
  22. 61.

    util.promisify const util = require('util'); const dns = require('dns'); //

    Promise Խ͞Εͨؔ਺ΛऔΕΔɻ const lookup = util.promisify(dns.lookup);
  23. 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();
  24. 63.

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

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

    Node v10 ͔Β Stream ʹ௥Ճ͞Εͨ finished, pipeline API͕ Promiseͱͷ਌࿨ੑߴ͍
  26. 66.

    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);
  27. 67.

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

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

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

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

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

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

    to create joint organization. Node.js Foundation ͱ JS Foundation ͸౷߹ ͞ΕͯɺҰͭʹͳΔͷΛ໨ࢦ͢ɻ
  33. 77.

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

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

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

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

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

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