Skip to content

Commit

Permalink
refactor: mark notImplemented constructors as side-effect free (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Feb 6, 2025
1 parent 041d4f6 commit d62cf31
Show file tree
Hide file tree
Showing 36 changed files with 679 additions and 503 deletions.
7 changes: 7 additions & 0 deletions src/runtime/_internal/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { HeadersObject } from "./types";

/*@__NO_SIDE_EFFECTS__*/
export function rawHeaders(headers: HeadersObject) {
const rawHeaders = [];
for (const key in headers) {
Expand All @@ -15,6 +16,8 @@ export function rawHeaders(headers: HeadersObject) {
}

type Fn = (...args: any[]) => any;

/*@__NO_SIDE_EFFECTS__*/
export function mergeFns(...functions: Fn[]) {
return function (...args: any[]) {
for (const fn of functions) {
Expand All @@ -23,10 +26,12 @@ export function mergeFns(...functions: Fn[]) {
};
}

/*@__NO_SIDE_EFFECTS__*/
export function createNotImplementedError(name: string) {
return new Error(`[unenv] ${name} is not implemented yet!`);
}

/*@__NO_SIDE_EFFECTS__*/
export function notImplemented<Fn extends (...args: any) => any>(
name: string,
): Fn {
Expand All @@ -42,13 +47,15 @@ export interface Promisifiable {
__promisify__: () => Promise<any>;
}

/*@__NO_SIDE_EFFECTS__*/
export function notImplementedAsync(name: string): Promisifiable {
const fn = notImplemented(name) as any;
fn.__promisify__ = () => notImplemented(name + ".__promisify__");
fn.native = fn;
return fn;
}

/*@__NO_SIDE_EFFECTS__*/
export function notImplementedClass<T = unknown>(name: string): T {
return class {
readonly __unenv__ = true;
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/node/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ function expectsNoError(
const assert = Object.assign(ok, {}) as typeof nodeAssert;

// deprecated
export const CallTracker = notImplementedClass(
export const CallTracker = /*@__PURE__*/ notImplementedClass(
"asset.CallTracker",
) as typeof nodeAssert.CallTracker;

Expand Down
10 changes: 6 additions & 4 deletions src/runtime/node/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ export { File } from "./internal/buffer/file";

// @ts-expect-eerror https://github.com/unjs/unenv/issues/64
export const Blob = globalThis.Blob as unknown as typeof buffer.Blob;
export const resolveObjectURL = notImplemented("buffer.resolveObjectURL");
export const transcode = notImplemented("buffer.transcode");
export const isUtf8 = notImplemented("buffer.isUtf8");
export const isAscii = notImplemented("buffer.isAscii");
export const resolveObjectURL = /*@__PURE__*/ notImplemented(
"buffer.resolveObjectURL",
);
export const transcode = /*@__PURE__*/ notImplemented("buffer.transcode");
export const isUtf8 = /*@__PURE__*/ notImplemented("buffer.isUtf8");
export const isAscii = /*@__PURE__*/ notImplemented("buffer.isAscii");

export const btoa = globalThis.btoa.bind(globalThis);
export const atob = globalThis.atob.bind(globalThis);
Expand Down
32 changes: 15 additions & 17 deletions src/runtime/node/child_process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,27 @@ import { notImplemented, notImplementedClass } from "../_internal/utils";
import type child_process from "node:child_process";

export const ChildProcess: typeof child_process.ChildProcess =
notImplementedClass("child_process.ChildProcess");
/*@__PURE__*/ notImplementedClass("child_process.ChildProcess");

export const _forkChild = notImplemented("child_process.ChildProcess");
export const _forkChild = /*@__PURE__*/ notImplemented(
"child_process.ChildProcess",
);

export const exec: typeof child_process.exec =
notImplemented("child_process.exec");
export const execFile: typeof child_process.execFile = notImplemented(
"child_process.execFile",
);
export const execFileSync: typeof child_process.execFileSync = notImplemented(
"child_process.execFileSync",
);
export const execSync: typeof child_process.execSync = notImplemented(
"child_process.execSyn",
);
/*@__PURE__*/ notImplemented("child_process.exec");
export const execFile: typeof child_process.execFile =
/*@__PURE__*/ notImplemented("child_process.execFile");
export const execFileSync: typeof child_process.execFileSync =
/*@__PURE__*/ notImplemented("child_process.execFileSync");
export const execSync: typeof child_process.execSync =
/*@__PURE__*/ notImplemented("child_process.execSyn");
export const fork: typeof child_process.fork =
notImplemented("child_process.fork");
export const spawn: typeof child_process.spawn = notImplemented(
/*@__PURE__*/ notImplemented("child_process.fork");
export const spawn: typeof child_process.spawn = /*@__PURE__*/ notImplemented(
"child_process.spawn",
);
export const spawnSync: typeof child_process.spawnSync = notImplemented(
"child_process.spawnSync",
);
export const spawnSync: typeof child_process.spawnSync =
/*@__PURE__*/ notImplemented("child_process.spawnSync");

export default <typeof child_process>{
ChildProcess,
Expand Down
15 changes: 7 additions & 8 deletions src/runtime/node/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@ export const schedulingPolicy: typeof nodeCluster.schedulingPolicy = SCHED_RR;
export const settings: typeof nodeCluster.settings = {};
export const workers: typeof nodeCluster.workers = {};

export const fork: typeof nodeCluster.fork = notImplemented("cluster.fork");
export const fork: typeof nodeCluster.fork =
/*@__PURE__*/ notImplemented("cluster.fork");

export const disconnect: typeof nodeCluster.disconnect =
notImplemented("cluster.disconnect");
/*@__PURE__*/ notImplemented("cluster.disconnect");

export const setupPrimary: typeof nodeCluster.setupPrimary = notImplemented(
"cluster.setupPrimary",
);
export const setupPrimary: typeof nodeCluster.setupPrimary =
/*@__PURE__*/ notImplemented("cluster.setupPrimary");

export const setupMaster: typeof nodeCluster.setupMaster = notImplemented(
"cluster.setupMaster",
);
export const setupMaster: typeof nodeCluster.setupMaster =
/*@__PURE__*/ notImplemented("cluster.setupMaster");

// Make ESM coverage happy
export const _events = [];
Expand Down
5 changes: 3 additions & 2 deletions src/runtime/node/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ export const warn: typeof console.warn = _console?.warn ?? error;

// https://developer.chrome.com/docs/devtools/console/api#createtask
export const createTask =
(_console as any)?.createTask ?? notImplemented("console.createTask");
(_console as any)?.createTask ??
/*@__PURE__*/ notImplemented("console.createTask");

export const assert: typeof console.assert =
notImplemented<typeof console.assert>("console.assert");
/*@__PURE__*/ notImplemented<typeof console.assert>("console.assert");

// noop
export const clear: typeof console.clear = _console?.clear ?? noop;
Expand Down
35 changes: 19 additions & 16 deletions src/runtime/node/dns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,39 @@ export const Resolver: typeof dns.Resolver =
export const getDefaultResultOrder: typeof dns.getDefaultResultOrder = () =>
"verbatim";
export const getServers: typeof dns.getServers = () => [];
export const lookup: typeof dns.lookup = notImplementedAsync("dns.lookup");
export const lookup: typeof dns.lookup =
/*@__PURE__*/ notImplementedAsync("dns.lookup");
export const lookupService: typeof dns.lookupService =
notImplementedAsync("dns.lookupService");
export const resolve: typeof dns.resolve = notImplementedAsync("dns.resolve");
/*@__PURE__*/ notImplementedAsync("dns.lookupService");
export const resolve: typeof dns.resolve =
/*@__PURE__*/ notImplementedAsync("dns.resolve");
export const resolve4: typeof dns.resolve4 =
notImplementedAsync("dns.resolve4");
/*@__PURE__*/ notImplementedAsync("dns.resolve4");
export const resolve6: typeof dns.resolve6 =
notImplementedAsync("dns.resolve6");
/*@__PURE__*/ notImplementedAsync("dns.resolve6");
export const resolveAny: typeof dns.resolveAny =
notImplementedAsync("dns.resolveAny");
/*@__PURE__*/ notImplementedAsync("dns.resolveAny");
export const resolveCaa: typeof dns.resolveCaa =
notImplementedAsync("dns.resolveCaa");
/*@__PURE__*/ notImplementedAsync("dns.resolveCaa");
export const resolveCname: typeof dns.resolveCname =
notImplementedAsync("dns.resolveCname");
/*@__PURE__*/ notImplementedAsync("dns.resolveCname");
export const resolveMx: typeof dns.resolveMx =
notImplementedAsync("dns.resolveMx");
/*@__PURE__*/ notImplementedAsync("dns.resolveMx");
export const resolveNaptr: typeof dns.resolveNaptr =
notImplementedAsync("dns.resolveNaptr");
/*@__PURE__*/ notImplementedAsync("dns.resolveNaptr");
export const resolveNs: typeof dns.resolveNs =
notImplementedAsync("dns.resolveNs");
/*@__PURE__*/ notImplementedAsync("dns.resolveNs");
export const resolvePtr: typeof dns.resolvePtr =
notImplementedAsync("dns.resolvePtr");
/*@__PURE__*/ notImplementedAsync("dns.resolvePtr");
export const resolveSoa: typeof dns.resolveSoa =
notImplementedAsync("dns.resolveSoa");
/*@__PURE__*/ notImplementedAsync("dns.resolveSoa");
export const resolveSrv: typeof dns.resolveSrv =
notImplementedAsync("dns.resolveSrv");
/*@__PURE__*/ notImplementedAsync("dns.resolveSrv");
export const resolveTxt: typeof dns.resolveTxt =
notImplementedAsync("dns.resolveTxt");
/*@__PURE__*/ notImplementedAsync("dns.resolveTxt");

export const reverse: typeof dns.reverse = notImplemented("dns.reverse");
export const reverse: typeof dns.reverse =
/*@__PURE__*/ notImplemented("dns.reverse");
export const setDefaultResultOrder: typeof dns.setDefaultResultOrder = noop;
export const setServers: typeof dns.setServers = noop;

Expand Down
35 changes: 19 additions & 16 deletions src/runtime/node/dns/promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,39 @@ export const Resolver: typeof dns.Resolver =
export const getDefaultResultOrder: typeof dns.getDefaultResultOrder = () =>
"verbatim";
export const getServers: typeof dns.getServers = () => [];
export const lookup: typeof dns.lookup = notImplementedAsync("dns.lookup");
export const lookup: typeof dns.lookup =
/*@__PURE__*/ notImplementedAsync("dns.lookup");
export const lookupService: typeof dns.lookupService =
notImplementedAsync("dns.lookupService");
export const resolve: typeof dns.resolve = notImplementedAsync("dns.resolve");
/*@__PURE__*/ notImplementedAsync("dns.lookupService");
export const resolve: typeof dns.resolve =
/*@__PURE__*/ notImplementedAsync("dns.resolve");
export const resolve4: typeof dns.resolve4 =
notImplementedAsync("dns.resolve4");
/*@__PURE__*/ notImplementedAsync("dns.resolve4");
export const resolve6: typeof dns.resolve6 =
notImplementedAsync("dns.resolve6");
/*@__PURE__*/ notImplementedAsync("dns.resolve6");
export const resolveAny: typeof dns.resolveAny =
notImplementedAsync("dns.resolveAny");
/*@__PURE__*/ notImplementedAsync("dns.resolveAny");
export const resolveCaa: typeof dns.resolveCaa =
notImplementedAsync("dns.resolveCaa");
/*@__PURE__*/ notImplementedAsync("dns.resolveCaa");
export const resolveCname: typeof dns.resolveCname =
notImplementedAsync("dns.resolveCname");
/*@__PURE__*/ notImplementedAsync("dns.resolveCname");
export const resolveMx: typeof dns.resolveMx =
notImplementedAsync("dns.resolveMx");
/*@__PURE__*/ notImplementedAsync("dns.resolveMx");
export const resolveNaptr: typeof dns.resolveNaptr =
notImplementedAsync("dns.resolveNaptr");
/*@__PURE__*/ notImplementedAsync("dns.resolveNaptr");
export const resolveNs: typeof dns.resolveNs =
notImplementedAsync("dns.resolveNs");
/*@__PURE__*/ notImplementedAsync("dns.resolveNs");
export const resolvePtr: typeof dns.resolvePtr =
notImplementedAsync("dns.resolvePtr");
/*@__PURE__*/ notImplementedAsync("dns.resolvePtr");
export const resolveSoa: typeof dns.resolveSoa =
notImplementedAsync("dns.resolveSoa");
/*@__PURE__*/ notImplementedAsync("dns.resolveSoa");
export const resolveSrv: typeof dns.resolveSrv =
notImplementedAsync("dns.resolveSrv");
/*@__PURE__*/ notImplementedAsync("dns.resolveSrv");
export const resolveTxt: typeof dns.resolveTxt =
notImplementedAsync("dns.resolveTxt");
/*@__PURE__*/ notImplementedAsync("dns.resolveTxt");

export const reverse: typeof dns.reverse = notImplemented("dns.reverse");
export const reverse: typeof dns.reverse =
/*@__PURE__*/ notImplemented("dns.reverse");
export const setDefaultResultOrder: typeof dns.setDefaultResultOrder = noop;
export const setServers: typeof dns.setServers = noop;

Expand Down
26 changes: 16 additions & 10 deletions src/runtime/node/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ export * from "./internal/http/request";
export * from "./internal/http/response";

export const createServer =
notImplemented<typeof http.createServer>("http.createServer");
export const request = notImplemented<typeof http.request>("http.request");
export const get = notImplemented<typeof http.get>("http.get");
/*@__PURE__*/ notImplemented<typeof http.createServer>("http.createServer");
export const request =
/*@__PURE__*/ notImplemented<typeof http.request>("http.request");
export const get = /*@__PURE__*/ notImplemented<typeof http.get>("http.get");

export const Server: typeof http.Server = mock.__createMock__("http.Server");

Expand All @@ -28,28 +29,33 @@ export const Agent: typeof http.Agent = mock.__createMock__("http.Agent");

export const globalAgent: typeof http.globalAgent = new Agent();

export const validateHeaderName = notImplemented<
export const validateHeaderName = /*@__PURE__*/ notImplemented<
typeof http.validateHeaderName
>("http.validateHeaderName");

export const validateHeaderValue = notImplemented<
export const validateHeaderValue = /*@__PURE__*/ notImplemented<
typeof http.validateHeaderValue
>("http.validateHeaderValue");

export const setMaxIdleHTTPParsers = notImplemented<
export const setMaxIdleHTTPParsers = /*@__PURE__*/ notImplemented<
typeof http.setMaxIdleHTTPParsers
>("http.setMaxIdleHTTPParsers");

export const _connectionListener = notImplemented("http._connectionListener");
export const _connectionListener = /*@__PURE__*/ notImplemented(
"http._connectionListener",
);

export const WebSocket =
globalThis.WebSocket || notImplementedClass<WebSocket>("WebSocket");
globalThis.WebSocket ||
/*@__PURE__*/ notImplementedClass<WebSocket>("WebSocket");

export const CloseEvent =
globalThis.CloseEvent || notImplementedClass<CloseEvent>("CloseEvent");
globalThis.CloseEvent ||
/*@__PURE__*/ notImplementedClass<CloseEvent>("CloseEvent");

export const MessageEvent =
globalThis.MessageEvent || notImplementedClass<MessageEvent>("MessageEvent");
globalThis.MessageEvent ||
/*@__PURE__*/ notImplementedClass<MessageEvent>("MessageEvent");

export default <typeof http>{
...consts,
Expand Down
9 changes: 5 additions & 4 deletions src/runtime/node/http2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import { constants } from "./internal/http2/constants";

export { constants } from "./internal/http2/constants";

export const createSecureServer = notImplemented<
export const createSecureServer = /*@__PURE__*/ notImplemented<
typeof http2.createSecureServer
>("http2.createSecureServer");
export const createServer =
notImplemented<typeof http2.createServer>("http2.createServer");
export const connect: typeof http2.connect = notImplemented("http2.connect");
/*@__PURE__*/ notImplemented<typeof http2.createServer>("http2.createServer");
export const connect: typeof http2.connect =
/*@__PURE__*/ notImplemented("http2.connect");
export const performServerHandshake: typeof http2.performServerHandshake =
notImplemented("http2.performServerHandshake ");
/*@__PURE__*/ notImplemented("http2.performServerHandshake ");

export const Http2ServerRequest: typeof http2.Http2ServerRequest =
mock.__createMock__("http2.Http2ServerRequest");
Expand Down
11 changes: 7 additions & 4 deletions src/runtime/node/https.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ import { notImplemented, notImplementedClass } from "../_internal/utils";
import mock from "../mock/proxy";

export const Server: typeof nodeHttps.Server =
notImplementedClass("https.Server");
/*@__PURE__*/ notImplementedClass("https.Server");

export const Agent: typeof nodeHttps.Agent = mock.__createMock__("https.Agent");
export const globalAgent: typeof nodeHttps.globalAgent = new Agent();

export const get = notImplemented<typeof nodeHttps.get>("https.get");
export const get =
/*@__PURE__*/ notImplemented<typeof nodeHttps.get>("https.get");

export const createServer =
notImplemented<typeof nodeHttps.createServer>("https.createServer");
/*@__PURE__*/ notImplemented<typeof nodeHttps.createServer>(
"https.createServer",
);

export const request =
notImplemented<typeof nodeHttps.request>("https.request");
/*@__PURE__*/ notImplemented<typeof nodeHttps.request>("https.request");

export default <typeof nodeHttps>{
Server,
Expand Down
Loading

0 comments on commit d62cf31

Please sign in to comment.