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

    Α͏ʹ͍ͬͯ͏ٯͷΞϓϩʔνΛऔΓͦ͏͕ͩɺ TypeScript(JS)͔ΒLLVM bitcodeੜ੒ͯ͠ɺCͷϥ ΠϒϥϦͱͯ͠ݺ΂ΔΑ͏ʹͨ͠ͱ͍͏ॴ͕΍͹͍ Μ͚ͩͲɺҰ୴΍͹͞͸ॻ͖͖Εͳ͍ɻɻɻ)
  2. 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
  3. Node.js v10.0.0 ~ v10.12.0 • HTTP/2 • ES Modules •

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

    Λར༻͢ΔΑ͏ʹ಺෦վળ͕ߦΘΕͨ • WHATWG TextEncoder/TextDecoder͕global ʹͳͬͨ • queueMicrotask API ͕࣮ݧతʹ௥Ճ͞Εͨ
  5. ES Modules import { readFile } from 'fs'; readFile('./foo.txt', (err,

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

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

    ΋૬ޓӡ༻Ͱ͖ΔܗͰ࣮૷͞ΕͯΔɻ • ͨͩ `.mjs` ͱ͍͏֦ுࢠ͕ඞཁʹͳΔʢࢼݧతͳ΋ ͷʣ • ࠓ͸ nodejs/modules Ͱٞ࿦த • https://github.com/nodejs/modules
  8. Promise in Node Core • ͦ΋ͦ΋ Node.js ʹ͸ඇಉظॲཧͷ΍Γํ͕ෳ਺ଘࡏ͢Δɻ • Callback

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

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

    fs.promises͔ΒॲཧΛ࣮ߦ const content = await fs.promises.readFile("./foo.txt"); console.log(content.toString()); } main();
  11. 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Λฦ͢Α͏ʹͳΔɻ
  12. Stream/Promiseͷ਌࿨ੑվળ • for await of Ͱ Stream Λஞ࣍ॲཧͰ͖ΔΑ͏ ʹͳͬͨ •

    Node v10 ͔Β Stream ʹ௥Ճ͞Εͨ finished, pipeline API͕ Promiseͱͷ਌࿨ੑߴ͍
  13. 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);
  14. 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();
  15. 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();
  16. 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) });
  17. 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();
  18. 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();
  19. Node.js Big News!!! • Node.js Foundation and JS Foundation intent

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

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

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

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