Skip to content

Commit

Permalink
refactor(node/buffer): update buffer to be cloudflare compat module
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Oct 15, 2024
1 parent 03257ee commit b0a2b47
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 54 deletions.
8 changes: 3 additions & 5 deletions src/presets/cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import type { Preset } from "../types";
// Built-in APIs provided by workerd.
// https://developers.cloudflare.com/workers/runtime-apis/nodejs/
// https://github.com/cloudflare/workerd/tree/main/src/node
// Last checked: 2024-05-11
// Last checked: 2024-10-15
const cloudflareNodeCompatModules = [
"_stream_duplex",
"_stream_passthrough",
"_stream_readable",
"_stream_transform",
"_stream_writable",
"assert",
"buffer",
"diagnostics_channel",
"events",
"path",
Expand All @@ -19,6 +20,7 @@ const cloudflareNodeCompatModules = [
"stream/promises",
"stream/web",
"string_decoder",
"sys",
"util/types",
"zlib",
];
Expand All @@ -28,7 +30,6 @@ const cloudflareNodeCompatModules = [
const hybridNodeCompatModules = [
"async_hooks",
"console",
"buffer",
"crypto",
"module",
"process",
Expand All @@ -47,9 +48,6 @@ const cloudflarePreset: Preset = {
// The `node:assert` implementation of workerd uses strict semantics by default
"assert/strict": "node:assert",
"node:assert/strict": "node:assert",
// The `node:sys` module is just a deprecated alias for `node:util` which we implemented using a hybrid polyfill
sys: "unenv/runtime/node/util/$cloudflare",
"node:sys": "unenv/runtime/node/util/$cloudflare",

// define aliases for hybrid modules
...Object.fromEntries(
Expand Down
41 changes: 0 additions & 41 deletions src/runtime/node/buffer/$cloudflare.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/runtime/node/buffer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export const transcode = notImplemented("buffer.transcode");
export const isUtf8 = notImplemented("buffer.isUtf8");
export const isAscii = notImplemented("buffer.isAscii");

export const btoa = global.btoa;
export const atob = globalThis.atob;
export const btoa = globalThis.btoa.bind(globalThis);
export const atob = globalThis.atob.bind(globalThis);

export const kStringMaxLength = 0; // TODO
export const constants = {
Expand Down
42 changes: 36 additions & 6 deletions test/workerd/tests.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assert from "node:assert";
import { createRequire } from "node:module";
import process from "node:process";

globalThis.process = process;
Expand Down Expand Up @@ -33,17 +34,32 @@ export const url_parse = {

// --- node:buffer

export const buffer_implements = {
export const unenv_polyfills_buffer = {
async test() {
const encoder = new TextEncoder();
const buffer = await import("unenv/runtime/node/buffer");
const Buffer = buffer.Buffer;
assert.strictEqual(typeof buffer.isAscii, "function");
assert.strictEqual(typeof buffer.isUtf8, "function");
assert.strictEqual(buffer.btoa("hello"), "aGVsbG8=");
assert.strictEqual(buffer.atob("aGVsbG8="), "hello");
assert.strictEqual(typeof buffer.transcode, "function");
assert.strictEqual(typeof buffer.File, "function");
assert.strictEqual(typeof buffer.Blob, "function");
assert.strictEqual(typeof buffer.INSPECT_MAX_BYTES, "number");
assert.strictEqual(typeof buffer.resolveObjectURL, "function");
assert.strictEqual(typeof Buffer.from, "function");
},
};

export const workerd_implements_buffer = {
async test() {
const encoder = new TextEncoder();
const buffer = await import("node:buffer");
const Buffer = buffer.Buffer;
assert.strictEqual(buffer.isAscii(encoder.encode("hello world")), true);
assert.strictEqual(buffer.isUtf8(encoder.encode("Yağız")), true);
// TODO: enable this once atob and btoa invalid this bug is resolved.
// Ref: https://github.com/cloudflare/workerd/pull/2907
// assert.strictEqual(buffer.btoa("hello"), "aGVsbG8=");
// assert.strictEqual(buffer.atob("aGVsbG8="), "hello");
assert.strictEqual(buffer.btoa("hello"), "aGVsbG8=");
assert.strictEqual(buffer.atob("aGVsbG8="), "hello");
{
const dest = buffer.transcode(
Buffer.from([
Expand All @@ -60,6 +76,20 @@ export const buffer_implements = {
}
assert.ok(new buffer.File([], "file"));
assert.ok(new buffer.Blob([]));
assert.strictEqual(typeof buffer.INSPECT_MAX_BYTES, "number");
assert.strictEqual(typeof buffer.resolveObjectURL, "function");
},
};

// --- workerd modules

export const workerd_modules = {
async test() {
const require = createRequire("/");
const modules = ["node:buffer", "node:sys"];
for (const m of modules) {
assert.strictEqual(await import(m), require(m));
}
},
};

Expand Down

0 comments on commit b0a2b47

Please sign in to comment.