Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/content/docs/workers/runtime-apis/nodejs/EventEmitter.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@ import { Render } from "~/components"

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

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

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

const emitter = new EventEmitter();
emitter.on('hello', (...args) => {
console.log(...args);
console.log(...args); // 1 2 3
});

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

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:
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)
option that allows improved handling of async functions as event handlers:

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

Like Node.js, when an `'error'` event is emitted on an `EventEmitter` and there
is no listener for it, the error will be immediately thrown. However, in Node.js
it is possible to add a handler on the `process` object for the
`'uncaughtException'` event to catch globally uncaught exceptions. The
`'uncaughtException'` event, however, is currently not implemented in the
Workers runtime. It is strongly recommended to always add an `'error'` listener
to any `EventEmitter` instance.

Refer to the [Node.js documentation for `EventEmitter`](https://nodejs.org/api/events.html#class-eventemitter) for more information.
2 changes: 1 addition & 1 deletion src/content/docs/workers/runtime-apis/nodejs/assert.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Render } from "~/components";

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

The `assert` module in Node.js provides a number of useful assertions that are useful when building tests.
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.

```js
import { strictEqual, deepStrictEqual, ok, doesNotReject } from "node:assert";
Expand Down
12 changes: 11 additions & 1 deletion src/content/docs/workers/runtime-apis/nodejs/buffer.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Render } from "~/components"

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

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.
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.

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

One key difference between the Workers implementation of `Buffer` and the Node.js
implementation is that some methods of creating a `Buffer` in Node.js will allocate
those from a global memory pool as a performance optimization. The Workers implementation
does not use a memory pool and all `Buffer` instances are allocated independently.

Further, in Node.js it is possible to allocate a `Buffer` with uninitialized memory
using the `Buffer.allocUnsafe()` method. This is not supported in Workers and `Buffer`
instances are always initialized so that the `Buffer` is always filled
with null bytes (`0x00`) when allocated.

Refer to the [Node.js documentation for `Buffer`](https://nodejs.org/dist/latest-v19.x/docs/api/buffer.html) for more information.
3 changes: 2 additions & 1 deletion src/content/docs/workers/runtime-apis/nodejs/crypto.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import { Render } from "~/components";

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

The `node:crypto` module provides cryptographic functionality that includes a set of wrappers for OpenSSL's hash, HMAC, cipher, decipher, sign, and verify functions.
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.

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

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

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

Expand Down
Loading