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

Node.js + Web Compatibility

Node.js + Web Compatibility

TechFeed Summit#6 - v16リリース記念!Node.jsの最新動向を語り尽くす! - connpass
https://techfeed.connpass.com/event/213218/

Masashi Hirano

May 26, 2021
Tweet

More Decks by Masashi Hirano

Other Decks in Programming

Transcript

  1. Node.js v14 -> v16 Web APIͷҰྫ • Web Crypto API:

    ௥Ճ • AbortController: ҆ఆ൛΁ • EventTarget & Event: ҆ఆ൛΁ • Blob: ௥Ճ • BroadcastChannel: ௥Ճ &YQFSJNFOUBM &YQFSJNFOUBM ʢશ෦v15͔Β࢖͑Δ͚Ͳ…v15͸6݄ͰEOLʣ …etc &YQFSJNFOUBM
  2. AbortController & AbortSignal • DOMϦΫΤετΛதஅͰ͖Δ • Promise • Event •

    ReadableStream • Web Locks API • Ճ͑ͯNode.jsͰ͸ඇಉظͳॲཧͷதஅʹར༻Մೳ https://dom.spec.whatwg.org/#interface-abortcontroller const controller = new AbortController(); const signal = controller.signal; abortButton.addEventListener('click', function() { controller.abort(); }); try { await fetch('https://site.example', { signal }); } catch(err) { console.error(err.name); }
  3. AbortControllerʹରԠ͢ΔNode.js Core API • Child Process • Events • File

    System • HTTP • HTTP/2 • Net • Readline • Stream • Timers • UDP/datagram sockets const controller = new AbortController(); const { signal } = controller; const promise = fs.readFile(fileName, { signal }); setTimeout(() => { controller.abort(); }, 10000); await promise; import { setTimeout, } from 'timers/promises'; const ac = new AbortController(); const signal = ac.signal; await setTimeout(10000, 'timeout', {signal}); ac.abort();
  4. EventTarget • ΠϕϯτॲཧΛ͢ΔͨΊͷΠϯλϑΣʔε • addEventListenerΛඋ͍͑ͯΔ • Node.js ʹੲ͔ΒEventEmitter͸͋Δ͕ɺϒϥ΢βͱͷޓ׵ੑ͸ͳ͍ https://dom.spec.whatwg.org/#interface-eventtarget const

    target = new EventTarget(); target.addEventListener("foo", (event) => { console.log("foo is called"); }); target.dispatchEvent(new Event("foo"));
  5. EventTarget + AbortController • AbortControllerͰEventTargetʹొ࿥ͨ͠ΠϕϯτϦεφʔΛ࡟আͰ ͖Δ const ac = new

    AbortController(); const { signal } = ac; et.addEventListener(‘click', handler, { signal }); // ΠϕϯτϦεφʔͷ࡟আɻremoveEventListener͸ෆཁ controller.abort(); et.addEventListener('event1', handler, { signal }); et.addEventListener(‘event2', handler, { signal }); // Ұׅ࡟আ΋Ͱ͖Δ controller.abort();
  6. Node.jsʹ࣮૷͢Δ͔ٞ࿦தͷWeb API • Fetch API https://github.com/nodejs/node/issues/19393 • WHATWG Stream Full

    Support https://github.com/nodejs/node/issues/36566 • HTTPS Imports https://github.com/nodejs/node/pull/36328 • Web Locks API https://github.com/nodejs/node/pull/36502 • MIME Type https://github.com/nodejs/node/pull/21128