Skip to content

Commit

Permalink
feat: updates for node v22.9 compatibility (#350)
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb authored Nov 27, 2024
1 parent 5c42af4 commit 7a9f230
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
},
"devDependencies": {
"@parcel/watcher": "^2.4.1",
"@types/node": "^22.7.4",
"@types/node": "^22.9.3",
"automd": "^0.3.12",
"changelogen": "^0.5.7",
"consola": "^3.2.3",
Expand Down
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions src/runtime/node/http/internal/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,19 @@ export class ServerResponse extends Writable implements http.ServerResponse {
return this;
}

setHeader(name: string, value: number | string | string[]): this {
this._headers[name.toLowerCase()] = value;
setHeader(name: string, value: number | string | readonly string[]): this {
this._headers[name.toLowerCase()] = Array.isArray(value)
? ([...value] as string[])
: (value as number | string);
return this;
}

setHeaders(
headers: Headers | Map<string, number | string | readonly string[]>,
): this {
for (const [key, value] of headers.entries()) {
this.setHeader(key, value);
}
return this;
}

Expand Down
9 changes: 9 additions & 0 deletions src/runtime/node/module/$cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ export {
_resolveFilename,
_resolveLookupPaths,
builtinModules,
constants,
enableCompileCache,
findSourceMap,
getCompileCacheDir,
globalPaths,
isBuiltin,
register,
Expand All @@ -40,7 +43,10 @@ import {
_resolveFilename,
_resolveLookupPaths,
builtinModules,
constants,
enableCompileCache,
findSourceMap,
getCompileCacheDir,
globalPaths,
isBuiltin,
register,
Expand Down Expand Up @@ -80,8 +86,11 @@ export default {
_resolveFilename,
_resolveLookupPaths,
builtinModules,
enableCompileCache,
constants,
createRequire,
findSourceMap,
getCompileCacheDir,
globalPaths,
isBuiltin,
register,
Expand Down
30 changes: 26 additions & 4 deletions src/runtime/node/module/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@ export const createRequire = function (filename: string) {
});
};

export const getCompileCacheDir: typeof nodeModule.getCompileCacheDir =
function () {
return undefined;
};

export const enableCompileCache: typeof nodeModule.enableCompileCache =
function (caheDir: string) {
return {
status: 0 /* compileCacheStatus.FAILED */,
message: "not implemented",
};
};

export const constants: typeof nodeModule.constants = Object.freeze({
compileCacheStatus: Object.freeze({
FAILED: 0,
ENABLED: 1,
ALREADY_ENABLED: 2,
DISABLED: 3,
}),
});

// prettier-ignore
export const builtinModules: typeof nodeModule.builtinModules = [
'_http_agent', '_http_client', '_http_common',
Expand Down Expand Up @@ -67,10 +89,7 @@ export const findSourceMap: typeof nodeModule.findSourceMap = function (
path: string,
error?: Error,
) {
// The cast is necessary because Node types wrongly set the return type to `SourceMap`.
// Comments on Node types say "Returns `module.SourceMap` if a source map is found, `undefined` otherwise."
// Returning `undefined` is the verified behavior.
return undefined as unknown as nodeModule.SourceMap;
return undefined;
};

export const wrap: typeof nodeModule.wrap = function (source) {
Expand Down Expand Up @@ -130,8 +149,11 @@ export const Module = {
_resolveFilename,
_resolveLookupPaths,
builtinModules,
constants,
createRequire,
enableCompileCache,
findSourceMap,
getCompileCacheDir,
globalPaths,
isBuiltin,
register,
Expand Down
19 changes: 9 additions & 10 deletions src/runtime/node/perf_hooks/internal/performance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,19 @@ export {
PerformanceObserverEntryList,
} from "../../../web/performance/index";

// grabbed from Node.js v22.3.0 using:
// performance.nodeTiming
const nodeTiming = {
name: "node",
entryType: "node",
startTime: 0,
duration: 305_963.045_666,
nodeStart: 1.662_124_991_416_931_2,
v8Start: 44.762_125_015_258_79,
bootstrapComplete: 49.992_666_006_088_26,
environment: 46.754_665_970_802_31,
loopStart: 63.262_040_972_709_656,
loopExit: -1,
idleTime: 305_360.555_328,
duration: 0,
nodeStart: 0,
v8Start: 0,
bootstrapComplete: 0,
environment: 0,
loopStart: 0,
loopExit: 0,
idleTime: 0,
uvMetricsInfo: { loopCount: 0, events: 0, eventsWaiting: 0 },
// only present in Node.js 18.x
detail: undefined,
} satisfies Omit<perf_hooks.PerformanceNodeTiming, "toJSON">;
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/node/readline/internal/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class Interface extends EventEmitter implements readline.Interface {
return this;
}

async *[Symbol.asyncIterator](): AsyncIterableIterator<string> {
async *[Symbol.asyncIterator](): NodeJS.AsyncIterator<string> {
yield "";
}
}
4 changes: 2 additions & 2 deletions src/runtime/node/stream/internal/readable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ export class _Readable extends EventEmitter implements stream.Readable {
}

// eslint-disable-next-line require-yield
async *[Symbol.asyncIterator](): AsyncIterableIterator<any> {
async *[Symbol.asyncIterator](): NodeJS.AsyncIterator<any> {
throw createNotImplementedError("Readable.asyncIterator");
}

iterator(
options?: { destroyOnReturn?: boolean | undefined } | undefined,
): AsyncIterableIterator<any> {
): NodeJS.AsyncIterator<any> {
throw createNotImplementedError("Readable.iterator");
}

Expand Down
2 changes: 2 additions & 0 deletions src/runtime/node/util/$cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export const {
deprecate,
format,
formatWithOptions,
getCallSite,
inherits,
inspect,
log,
Expand Down Expand Up @@ -125,6 +126,7 @@ export default {
deprecate,
format,
formatWithOptions,
getCallSite,
inherits,
inspect,
log,
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/node/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,17 @@ export const parseEnv = notImplemented<typeof util.parseEnv>("util.parseEnv");
export const styleText =
notImplemented<typeof util.styleText>("util.styleText");

export const getCallSite =
notImplemented<typeof util.getCallSite>("util.getCallSites");

export default <typeof util>{
_errnoException,
_exceptionWithHostPort,
_extend,
aborted,
callbackify,
deprecate,
getCallSite,
getSystemErrorMap,
getSystemErrorName,
inherits,
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/node/vm/internal/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import type vm from "node:vm";

// Actual values in Node are Symbols
// See https://github.com/nodejs/node/blob/92c7dde/lib/vm.js#L66
export const USE_MAIN_CONTEXT_DEFAULT_LOADER: typeof vm.constants.USE_MAIN_CONTEXT_DEFAULT_LOADER = 0;
export const DONT_CONTEXTIFY: typeof vm.constants.DONT_CONTEXTIFY = 1;

0 comments on commit 7a9f230

Please sign in to comment.