Skip to content

Commit

Permalink
refactor(node/util/types): make util/types a nodejs_compat module (#329)
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig authored Oct 15, 2024
1 parent 2186bfc commit 4685643
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 117 deletions.
2 changes: 1 addition & 1 deletion src/presets/cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const cloudflareNodeCompatModules = [
"stream/promises",
"stream/web",
"string_decoder",
"util/types",
"zlib",
];

Expand All @@ -33,7 +34,6 @@ const hybridNodeCompatModules = [
"process",
"timers",
"util",
"util/types",
];

const cloudflarePreset: Preset = {
Expand Down
8 changes: 1 addition & 7 deletions src/runtime/node/util/$cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import {
isUndefined,
parseEnv,
styleText,
types as unenvUtilTypes,
} from "./index";

const workerdUtil = process.getBuiltinModule("node:util");
Expand Down Expand Up @@ -81,12 +80,7 @@ export const {
transferableAbortSignal,
} = workerdUtil;

// TODO(cloudflare): we should just implement this in workerd and drop this special case.
export const types = {
...workerdUtil.types,
isExternal: unenvUtilTypes.isExternal,
isAnyArrayBuffer: workerdUtil.types.isAnyArrayBuffer,
} satisfies typeof nodeUtil.types;
export const types = workerdUtil.types;

export default {
/**
Expand Down
4 changes: 1 addition & 3 deletions src/runtime/node/util/internal/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import type utilTypes from "node:util/types";
import { notImplemented } from "../../../_internal/utils";

export const isExternal = notImplemented<typeof utilTypes.isExternal>(
"util.types.isExternal",
);
export const isExternal: typeof utilTypes.isExternal = (_obj) => false;

export const isDate: typeof utilTypes.isDate = (val): val is Date =>
val instanceof Date;
Expand Down
106 changes: 0 additions & 106 deletions src/runtime/node/util/types/$cloudflare.ts

This file was deleted.

10 changes: 10 additions & 0 deletions test/workerd/tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,13 @@ export const buffer_implements = {
assert.ok(new buffer.Blob([]));
},
};

// --- node:util

export const util_implements = {
async test() {
const { types } = await import("unenv/runtime/node/util");
assert.strictEqual(types.isExternal("hello world"), false);
assert.strictEqual(types.isAnyArrayBuffer(new ArrayBuffer(0)), true);
},
};

0 comments on commit 4685643

Please sign in to comment.