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

NodeFest2018/Node v10-v11

NodeFest2018/Node v10-v11

Yosuke Furukawa

October 30, 2018
Tweet

More Decks by Yosuke Furukawa

Other Decks in Programming

Transcript

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

    Error Code ʹΑΔϋϯυϦϯά • fs ʹ promises ͕௥Ճ͞Εͨ • Stream͕վળ͞Εͨ • Worker͕վળ͞Εͨ
  2. Node.js v11 • V8 version 7.0ରԠ (wasm վળ) • url

    module ͕ deprecated ʹͳΓɺ WHATWG URL Λར༻͢ΔΑ͏ʹ಺෦վળ͕ߦΘΕͨ • TextEncoder/TextDecoder͕globalʹͳͬͨ • queueMicrotask API ͕࣮ݧతʹ௥Ճ͞Εͨ
  3. Stream/Promiseͷ਌࿨ੑվળ • for await of Ͱ Stream Λஞ࣍ॲཧͰ͖ΔΑ͏ ʹͳͬͨ •

    Node v10 ͔Β Stream ʹ௥Ճ͞Εͨ finished, pipeline API͕ Promiseͱͷ਌࿨ੑߴ͍
  4. 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);
  5. 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();
  6. 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();
  7. 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) });
  8. 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();
  9. 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();
  10. process.on('multipleResolves ') process.on('multipleResolves', (type, promise, reason) => { console.error(type, promise,

    reason); setImmediate(() => process.exit(1)); }); async function main() { try { return await new Promise((resolve, reject) => { resolve('First call'); resolve('Swallowed resolve'); reject(new Error('Swallowed reject')); }); } catch { throw new Error('Failed'); } }
  11. queueMicrotask DataHandler.prototype.load = async function load(key) { const hit =

    this._cache.get(url); if (hit !== undefined) { queueMicrotask(() => { this.emit('load', hit); }); return; } const data = await fetchData(key); this._cache.set(url, data); this.emit('load', data); };
  12. 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