67 Matching Annotations
  1. Nov 2023
    1. ```js import path from 'path'; import { fileURLToPath } from 'url';

      const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); ```

  2. Jul 2023
  3. Mar 2023
    1. Streaming across worker threads

      ```js import { ReadableStream } from 'node:stream/web'; import { Worker } from 'node:worker_threads';

      const readable = new ReadableStream(getSomeSource());

      const worker = new Worker('/path/to/worker.js', { workerData: readable, transferList: [readable], }); ```

      ```js const { workerData: stream } = require('worker_threads');

      const reader = stream.getReader(); reader.read().then(console.log); ```

    2. Consuming web streams

      ```js import { arrayBuffer, blob, buffer, json, text, } from 'node:stream/consumers';

      const data1 = await arrayBuffer(getReadableStreamSomehow());

      const data2 = await blob(getReadableStreamSomehow());

      const data3 = await buffer(getReadableStreamSomehow());

      const data4 = await json(getReadableStreamSomehow());

      const data5 = await text(getReadableStreamSomehow()); ```

    3. Adapting to the Node.js Streams API

      ```js /* * For instance, given a ReadableStream object, the stream.Readable.fromWeb() method * will create an return a Node.js stream.Readable object that can be used to consume * the ReadableStream's data: / import { Readable } from 'node:stream';

      const readable = new ReadableStream(getSomeSource());

      const nodeReadable = Readable.fromWeb(readable);

      nodeReadable.on('data', console.log); ```

      ```js /* * The adaptation can also work the other way -- starting with a Node.js * stream.Readable and acquiring a web streams ReadableStream: / import { Readable } from 'node:stream';

      const readable = new Readable({ read(size) { reader.push(Buffer.from('hello')); } });

      const readableStream = Readable.toWeb(readable);

      await readableStream.read();

      ```

  4. Feb 2023
    1. Node.js

      js import { renderToPipeableStream } from "react-dom/server.node"; import React from "react"; import http from "http"; const App = () => ( <html> <body> <h1>Hello World</h1> <p>This is an example.</p> </body> </html> ); var didError = false; http .createServer(function (req, res) { const stream = renderToPipeableStream(<App />, { onShellReady() { res.statusCode = didError ? 500 : 200; res.setHeader("Content-type", "text/html"); res.setHeader("Cache-Control", "no-transform"); stream.pipe(res); }, onShellError(error) { res.statusCode = 500; res.send( '<!doctype html><p>Loading...</p><script src="clientrender.js"></script>', ); }, onAllReady() { }, onError(err) { didError = true; console.error(err); }, }); }) .listen(3000);

      Deno

      ```js import { renderToReadableStream } from "https://esm.run/react-dom/server"; import * as React from "https://esm.run/react";

      const App = () => ( <html> <body>

      Hello World

      This is an example.

      </body> </html> );

      const headers = { headers: { "Content-Type": "text/html", "Cache-Control": "no-transform", }, };

      Deno.serve( async (req) => { return new Response(await renderToReadableStream(<App />), headers); }, { port: 3000 }, ); ```

      Bun

      ```js import { renderToReadableStream } from "react-dom/server"; const headers = { headers: { "Content-Type": "text/html", }, };

      const App = () => ( <html> <body>

      Hello World

      This is an example.

      </body> </html> );

      Bun.serve({ port: 3000, async fetch(req) { return new Response(await renderToReadableStream(<App />), headers); }, }); ```

  5. Dec 2022
  6. Sep 2022
  7. Aug 2022
  8. Jun 2022
  9. May 2022
    1. I develop in Node and Sveltekit regularly and the chances that on any given day my flow might be crushed by random madness is unacceptably high.
    1. Not only this. Try to change the app two years later. Dependencies gone, wrong NPM version, Webpack config depricated and what not.That's why I like to use vanilla JS as much as possible. It will be maintainable years later.
    1. To address this issue, and to make it easier for non-web browser environments to implement fetch in a consistent way, WinterCG is working on documenting a subset of the fetch standard that deals specifically with those different requirements and constraints.
  10. Jan 2022
  11. Dec 2021
    1. npm init -y \
      && npm i --save-dev node@16 \
      && npm config set prefix=$(pwd)/node_modules/node \
      && export PATH=$(pwd)/node_modules/node/bin:$PATH
      
  12. Nov 2021
    1. import { createRequire } from "module"; const require = createRequire(import.meta.url); const data = require("./data.json");
  13. Oct 2021
  14. Jan 2021
    1. Buffers and TypedArrays

      Mozilla TypedArray document

      Uint8Array is a type of TypedArray

      and Buffer is an instance of Uint8Array

      Creating TypedArrays from Buffer with or without sharing same memory.

      Creating a Buffer from the TypedArrays with or without sharing the same memory

      In order to share memory we use

      1. Buffer.buffer, Buffer.byteOffset, Buffer.length / TypedArray.BYTES_PER_ELEMENT
      2. TypedArray.buffer, offset, length
  15. Apr 2020
  16. Dec 2019
    1. You have to create duplicate of the stream by piping it to two streams. You can create a simple stream with a PassThrough stream, it simply passes the input to the output.
  17. Sep 2019
  18. Jun 2019
  19. Nov 2018
  20. Oct 2018
  21. Sep 2018
    1. module.exports属性表示当前模块对外输出的接口,其他文件加载该模块,实际上就是读取module.exports变量。

      也就是说:

      1. require 获得的是 module.export 对象;
      2. export === modue.export 指向同一块内存;export 是一个快捷方式,覆盖就没有意义;
      3. module.export 可以覆盖,这取决与需要暴露什么对象或方法;覆盖后 export 无效,因为 第 1 条;
  22. Jun 2018
    1. The next set of functions call app.use() to add the middleware libraries into the request handling chain

      This is how you'd add middlewares!

    2. Then we require() modules from our routes directory

      Routes are also a module. This 'module' is nothing more than a way to 'include' other file, so to speak in PHP terms. Its the ability to split code/functionality across multiple manageable files

  23. Apr 2018
  24. Nov 2017
  25. Jan 2016
  26. Jun 2015
    1. stream-adventure

      Some overlap with learnyounode and fewer instructions, I think this is one of the first ones that was created. Still nice to really dig into streams which are an important concept to understand.

    2. learnyounode

      This one's really well done, I recommend starting here if you already have some JS experience.

    3. Right now there's no good way (that I know of) to keep track of new workshoppers (the little interactive workshops that you can run on your own), so I occasionally check https://github.com/nodeschool/nodeschool.github.io (the code of the website) for commits.

  27. May 2015
  28. Jan 2014
    1. What are the some of the challenges that we are going to face in the next 5 years?

      Interesting analysis of Node.js's possible strengths.