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
55 changes: 19 additions & 36 deletions src/runtime/node/buffer/$cloudflare.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,41 @@
// https://nodejs.org/api/buffer.html
import type nodeBuffer from "node:buffer";

export {
Blob,
File,
INSPECT_MAX_BYTES,
atob,
btoa,
Comment thread
anonrig marked this conversation as resolved.
isAscii,
isUtf8,
resolveObjectURL,
transcode,
} from "./index";
export { INSPECT_MAX_BYTES, resolveObjectURL } from "./index";

import {
Blob,
File,
INSPECT_MAX_BYTES,
atob,
btoa,
isAscii,
isUtf8,
resolveObjectURL,
transcode,
} from "./index";
import { INSPECT_MAX_BYTES, resolveObjectURL } from "./index";

// @ts-ignore typings are not up to date, but this API exists, see: https://github.com/cloudflare/workerd/pull/2147
const workerdBuffer = process.getBuiltinModule("node:buffer");

// TODO: Ideally this list is not hardcoded but instead is generated when the preset is being generated in the `env()` call
// This generation should use information from https://github.com/cloudflare/workerd/issues/2097
export const { Buffer, SlowBuffer, constants, kMaxLength, kStringMaxLength } =
workerdBuffer;

export default {
/**
* manually unroll unenv-polyfilled-symbols to make it tree-shakeable
*/
export const {
Blob,
Buffer,
Comment thread
anonrig marked this conversation as resolved.
File,
INSPECT_MAX_BYTES,
atob,
btoa,
SlowBuffer,
constants,
isAscii,
isUtf8,
resolveObjectURL,
kMaxLength,
kStringMaxLength,
transcode,
} = workerdBuffer;

/**
* manually unroll workerd-polyfilled-symbols to make it tree-shakeable
*/
export default {
INSPECT_MAX_BYTES,
Blob,
Buffer,
File,
SlowBuffer,
atob,
btoa,
constants,
isAscii,
isUtf8,
kMaxLength,
kStringMaxLength,
resolveObjectURL,
transcode,
} satisfies typeof nodeBuffer;
28 changes: 28 additions & 0 deletions test/workerd/tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,31 @@ export const url_parse = {
);
},
};

// --- node:buffer

export const buffer_implements = {
async test() {
const Buffer = await import("unenv/runtime/node/buffer");
assert.ok(Buffer.isAscii("hello world"));
assert.ok(Buffer.isUtf8("Yağız"));
assert.strictEqual(Buffer.btoa("hello"), "aGVsbG8=");
assert.strictEqual(Buffer.atob("aGVsbG8="), "hello");
{
const dest = Buffer.transcode(
Buffer.from([
0x74, 0x00, 0x1b, 0x01, 0x73, 0x00, 0x74, 0x00, 0x20, 0x00, 0x15,
0x26,
]),
"ucs2",
"utf8",
);
assert.strictEqual(
dest.toString(),
Buffer.from("těst ☕", "utf8").toString(),
);
}
assert.ok(new Buffer.File([], "file"));
assert.ok(new Buffer.Blob([]));
},
};