Skip to content

Commit ce39095

Browse files
authored
Start to making some incremental improvements to node compat docs (#23773)
* Start to making some incremental improvements to node compat docs * Update src/content/docs/workers/runtime-apis/nodejs/EventEmitter.mdx * Update src/content/docs/workers/runtime-apis/nodejs/buffer.mdx
1 parent a9101d5 commit ce39095

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

src/content/docs/workers/runtime-apis/nodejs/EventEmitter.mdx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,22 @@ import { Render } from "~/components"
88

99
<Render file="nodejs-compat-howto" />
1010

11-
An `EventEmitter` is an object that emits named events that cause listeners to be called.
11+
An [`EventEmitter`](https://nodejs.org/docs/latest/api/events.html#class-eventemitter)
12+
is an object that emits named events that cause listeners to be called.
1213

1314
```js
1415
import { EventEmitter } from 'node:events';
1516

1617
const emitter = new EventEmitter();
1718
emitter.on('hello', (...args) => {
18-
console.log(...args);
19+
console.log(...args); // 1 2 3
1920
});
2021

2122
emitter.emit('hello', 1, 2, 3);
2223
```
2324

24-
The implementation in the Workers runtime fully supports the entire Node.js `EventEmitter` API. This includes the `captureRejections` option that allows improved handling of async functions as event handlers:
25+
The implementation in the Workers runtime supports the entire Node.js `EventEmitter` API. This includes the [`captureRejections`](https://nodejs.org/docs/latest/api/events.html#capture-rejections-of-promises)
26+
option that allows improved handling of async functions as event handlers:
2527

2628
```js
2729
const emitter = new EventEmitter({ captureRejections: true });
@@ -33,4 +35,12 @@ emitter.on('error', (err) => {
3335
});
3436
```
3537

38+
Like Node.js, when an `'error'` event is emitted on an `EventEmitter` and there
39+
is no listener for it, the error will be immediately thrown. However, in Node.js
40+
it is possible to add a handler on the `process` object for the
41+
`'uncaughtException'` event to catch globally uncaught exceptions. The
42+
`'uncaughtException'` event, however, is currently not implemented in the
43+
Workers runtime. It is strongly recommended to always add an `'error'` listener
44+
to any `EventEmitter` instance.
45+
3646
Refer to the [Node.js documentation for `EventEmitter`](https://nodejs.org/api/events.html#class-eventemitter) for more information.

src/content/docs/workers/runtime-apis/nodejs/assert.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Render } from "~/components";
77

88
<Render file="nodejs-compat-howto" />
99

10-
The `assert` module in Node.js provides a number of useful assertions that are useful when building tests.
10+
The [`node:assert`](https://nodejs.org/docs/latest/api/assert.html) module in Node.js provides a number of useful assertions that are useful when building tests.
1111

1212
```js
1313
import { strictEqual, deepStrictEqual, ok, doesNotReject } from "node:assert";

src/content/docs/workers/runtime-apis/nodejs/buffer.mdx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Render } from "~/components"
88

99
<Render file="nodejs-compat-howto" />
1010

11-
The `Buffer` API in Node.js is one of the most commonly used Node.js APIs for manipulating binary data. Every `Buffer` instance extends from the standard [`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) class, but adds a range of unique capabilities such as built-in base64 and hex encoding/decoding, byte-order manipulation, and encoding-aware substring searching.
11+
The [`Buffer`](https://nodejs.org/docs/latest/api/buffer.html) API in Node.js is one of the most commonly used Node.js APIs for manipulating binary data. Every `Buffer` instance extends from the standard [`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) class, but adds a range of unique capabilities such as built-in base64 and hex encoding/decoding, byte-order manipulation, and encoding-aware substring searching.
1212

1313
```js
1414
import { Buffer } from 'node:buffer';
@@ -35,4 +35,14 @@ const writer = writable.getWriter();
3535
writer.write(Buffer.from("hello world"));
3636
```
3737

38+
One key difference between the Workers implementation of `Buffer` and the Node.js
39+
implementation is that some methods of creating a `Buffer` in Node.js will allocate
40+
those from a global memory pool as a performance optimization. The Workers implementation
41+
does not use a memory pool and all `Buffer` instances are allocated independently.
42+
43+
Further, in Node.js it is possible to allocate a `Buffer` with uninitialized memory
44+
using the `Buffer.allocUnsafe()` method. This is not supported in Workers and `Buffer`
45+
instances are always initialized so that the `Buffer` is always filled
46+
with null bytes (`0x00`) when allocated.
47+
3848
Refer to the [Node.js documentation for `Buffer`](https://nodejs.org/dist/latest-v19.x/docs/api/buffer.html) for more information.

src/content/docs/workers/runtime-apis/nodejs/crypto.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import { Render } from "~/components";
77

88
<Render file="nodejs-compat-howto" />
99

10-
The `node:crypto` module provides cryptographic functionality that includes a set of wrappers for OpenSSL's hash, HMAC, cipher, decipher, sign, and verify functions.
10+
The [`node:crypto`](https://nodejs.org/docs/latest/api/crypto.html) module provides cryptographic functionality that includes a set of wrappers for OpenSSL's hash, HMAC, cipher, decipher, sign, and verify functions.
1111

1212
All `node:crypto` APIs are fully supported in Workers with the following exceptions:
1313

1414
- The functions [generateKeyPair](https://nodejs.org/api/crypto.html#cryptogeneratekeypairtype-options-callback) and [generateKeyPairSync](https://nodejs.org/api/crypto.html#cryptogeneratekeypairsynctype-options)
1515
do not support DSA or DH key pairs.
1616
- `ed448` and `x448` curves are not supported.
17+
- It is not possible to manually enable or disable [FIPS mode](https://nodejs.org/docs/latest/api/crypto.html#fips-mode).
1718

1819
The full `node:crypto` API is documented in the [Node.js documentation for `node:crypto`](https://nodejs.org/api/crypto.html).
1920

0 commit comments

Comments
 (0)