From b4037ca4b83b152bc600d03f993af914de6beb88 Mon Sep 17 00:00:00 2001 From: Greg Brimble Date: Thu, 5 Jan 2023 14:30:09 +0000 Subject: [PATCH] Add manual Node types --- .../tests/tsconfig.json | 3 +- fixtures/node-app-pages/tests/tsconfig.json | 3 +- fixtures/pages-d1-shim/tests/tsconfig.json | 4 +- .../pages-functions-app/tests/tsconfig.json | 4 +- .../tests/tsconfig.json | 3 +- .../tests/tsconfig.json | 4 +- .../tests/tsconfig.json | 3 +- fixtures/pages-workerjs-app/tsconfig.json | 4 +- .../tsconfig.json | 4 +- fixtures/pages-ws-app/tests/tsconfig.json | 3 +- fixtures/remix-pages-app/tests/tsconfig.json | 4 +- .../service-bindings-app/tests/tsconfig.json | 4 +- fixtures/worker-app/tsconfig.json | 4 +- node-types.d.ts | 91 +++++++++++++++++++ 14 files changed, 107 insertions(+), 31 deletions(-) create mode 100644 node-types.d.ts diff --git a/fixtures/external-durable-objects-app/tests/tsconfig.json b/fixtures/external-durable-objects-app/tests/tsconfig.json index f4bcc248cfa7..b1a3500dfd1e 100644 --- a/fixtures/external-durable-objects-app/tests/tsconfig.json +++ b/fixtures/external-durable-objects-app/tests/tsconfig.json @@ -1,8 +1,7 @@ { "extends": "../tsconfig.json", "compilerOptions": { - "lib": ["DOM"], "types": ["node", "vitest"] }, - "include": ["**/*.ts"] + "include": ["**/*.ts", "../../../node-types.d.ts"] } diff --git a/fixtures/node-app-pages/tests/tsconfig.json b/fixtures/node-app-pages/tests/tsconfig.json index f4bcc248cfa7..b1a3500dfd1e 100644 --- a/fixtures/node-app-pages/tests/tsconfig.json +++ b/fixtures/node-app-pages/tests/tsconfig.json @@ -1,8 +1,7 @@ { "extends": "../tsconfig.json", "compilerOptions": { - "lib": ["DOM"], "types": ["node", "vitest"] }, - "include": ["**/*.ts"] + "include": ["**/*.ts", "../../../node-types.d.ts"] } diff --git a/fixtures/pages-d1-shim/tests/tsconfig.json b/fixtures/pages-d1-shim/tests/tsconfig.json index fb1aeb9e2190..b1a3500dfd1e 100644 --- a/fixtures/pages-d1-shim/tests/tsconfig.json +++ b/fixtures/pages-d1-shim/tests/tsconfig.json @@ -1,9 +1,7 @@ { "extends": "../tsconfig.json", "compilerOptions": { - "lib": ["DOM"], "types": ["node", "vitest"] }, - "include": ["**/*.ts"], - "exclude": [] + "include": ["**/*.ts", "../../../node-types.d.ts"] } diff --git a/fixtures/pages-functions-app/tests/tsconfig.json b/fixtures/pages-functions-app/tests/tsconfig.json index fb1aeb9e2190..b1a3500dfd1e 100644 --- a/fixtures/pages-functions-app/tests/tsconfig.json +++ b/fixtures/pages-functions-app/tests/tsconfig.json @@ -1,9 +1,7 @@ { "extends": "../tsconfig.json", "compilerOptions": { - "lib": ["DOM"], "types": ["node", "vitest"] }, - "include": ["**/*.ts"], - "exclude": [] + "include": ["**/*.ts", "../../../node-types.d.ts"] } diff --git a/fixtures/pages-functions-with-routes-app/tests/tsconfig.json b/fixtures/pages-functions-with-routes-app/tests/tsconfig.json index f4bcc248cfa7..b1a3500dfd1e 100644 --- a/fixtures/pages-functions-with-routes-app/tests/tsconfig.json +++ b/fixtures/pages-functions-with-routes-app/tests/tsconfig.json @@ -1,8 +1,7 @@ { "extends": "../tsconfig.json", "compilerOptions": { - "lib": ["DOM"], "types": ["node", "vitest"] }, - "include": ["**/*.ts"] + "include": ["**/*.ts", "../../../node-types.d.ts"] } diff --git a/fixtures/pages-plugin-mounted-on-root-app/tests/tsconfig.json b/fixtures/pages-plugin-mounted-on-root-app/tests/tsconfig.json index fb1aeb9e2190..b1a3500dfd1e 100644 --- a/fixtures/pages-plugin-mounted-on-root-app/tests/tsconfig.json +++ b/fixtures/pages-plugin-mounted-on-root-app/tests/tsconfig.json @@ -1,9 +1,7 @@ { "extends": "../tsconfig.json", "compilerOptions": { - "lib": ["DOM"], "types": ["node", "vitest"] }, - "include": ["**/*.ts"], - "exclude": [] + "include": ["**/*.ts", "../../../node-types.d.ts"] } diff --git a/fixtures/pages-workerjs-and-functions-app/tests/tsconfig.json b/fixtures/pages-workerjs-and-functions-app/tests/tsconfig.json index f4bcc248cfa7..b1a3500dfd1e 100644 --- a/fixtures/pages-workerjs-and-functions-app/tests/tsconfig.json +++ b/fixtures/pages-workerjs-and-functions-app/tests/tsconfig.json @@ -1,8 +1,7 @@ { "extends": "../tsconfig.json", "compilerOptions": { - "lib": ["DOM"], "types": ["node", "vitest"] }, - "include": ["**/*.ts"] + "include": ["**/*.ts", "../../../node-types.d.ts"] } diff --git a/fixtures/pages-workerjs-app/tsconfig.json b/fixtures/pages-workerjs-app/tsconfig.json index b9a88756052b..0763751e16e5 100644 --- a/fixtures/pages-workerjs-app/tsconfig.json +++ b/fixtures/pages-workerjs-app/tsconfig.json @@ -3,10 +3,10 @@ "target": "ES2020", "esModuleInterop": true, "module": "CommonJS", - "lib": ["ES2020", "DOM"], + "lib": ["ES2020"], "types": ["vitest", "node"], "moduleResolution": "node", "noEmit": true }, - "include": ["tests"] + "include": ["tests", "../../node-types.d.ts"] } diff --git a/fixtures/pages-workerjs-with-routes-app/tsconfig.json b/fixtures/pages-workerjs-with-routes-app/tsconfig.json index fc8ace64e043..c6e4bc617056 100644 --- a/fixtures/pages-workerjs-with-routes-app/tsconfig.json +++ b/fixtures/pages-workerjs-with-routes-app/tsconfig.json @@ -3,10 +3,10 @@ "target": "ES2020", "esModuleInterop": true, "module": "CommonJS", - "lib": ["ES2020", "DOM"], + "lib": ["ES2020"], "types": ["node", "vitest"], "moduleResolution": "node", "noEmit": true }, - "include": ["tests"] + "include": ["tests", "../../node-types.d.ts"] } diff --git a/fixtures/pages-ws-app/tests/tsconfig.json b/fixtures/pages-ws-app/tests/tsconfig.json index f4bcc248cfa7..b1a3500dfd1e 100644 --- a/fixtures/pages-ws-app/tests/tsconfig.json +++ b/fixtures/pages-ws-app/tests/tsconfig.json @@ -1,8 +1,7 @@ { "extends": "../tsconfig.json", "compilerOptions": { - "lib": ["DOM"], "types": ["node", "vitest"] }, - "include": ["**/*.ts"] + "include": ["**/*.ts", "../../../node-types.d.ts"] } diff --git a/fixtures/remix-pages-app/tests/tsconfig.json b/fixtures/remix-pages-app/tests/tsconfig.json index 1a7a22d802b0..b1a3500dfd1e 100644 --- a/fixtures/remix-pages-app/tests/tsconfig.json +++ b/fixtures/remix-pages-app/tests/tsconfig.json @@ -1,9 +1,7 @@ { "extends": "../tsconfig.json", "compilerOptions": { - "lib": ["ES2019", "DOM"], "types": ["node", "vitest"] }, - "include": ["**/*.ts"], - "exclude": [] + "include": ["**/*.ts", "../../../node-types.d.ts"] } diff --git a/fixtures/service-bindings-app/tests/tsconfig.json b/fixtures/service-bindings-app/tests/tsconfig.json index fb1aeb9e2190..b1a3500dfd1e 100644 --- a/fixtures/service-bindings-app/tests/tsconfig.json +++ b/fixtures/service-bindings-app/tests/tsconfig.json @@ -1,9 +1,7 @@ { "extends": "../tsconfig.json", "compilerOptions": { - "lib": ["DOM"], "types": ["node", "vitest"] }, - "include": ["**/*.ts"], - "exclude": [] + "include": ["**/*.ts", "../../../node-types.d.ts"] } diff --git a/fixtures/worker-app/tsconfig.json b/fixtures/worker-app/tsconfig.json index b9a88756052b..0763751e16e5 100644 --- a/fixtures/worker-app/tsconfig.json +++ b/fixtures/worker-app/tsconfig.json @@ -3,10 +3,10 @@ "target": "ES2020", "esModuleInterop": true, "module": "CommonJS", - "lib": ["ES2020", "DOM"], + "lib": ["ES2020"], "types": ["vitest", "node"], "moduleResolution": "node", "noEmit": true }, - "include": ["tests"] + "include": ["tests", "../../node-types.d.ts"] } diff --git a/node-types.d.ts b/node-types.d.ts new file mode 100644 index 000000000000..3c755dbf4ecd --- /dev/null +++ b/node-types.d.ts @@ -0,0 +1,91 @@ +// https://github.com/cloudflare/wrangler2/pull/2496#discussion_r1062516883 + +import { + Event as WorkerEvent, + WebAssembly as WorkerWebAssembly, +} from "@cloudflare/workers-types"; +import type { + EventListenerOrEventListenerObject, + EventTargetAddEventListenerOptions, + EventTargetEventListenerOptions, +} from "@cloudflare/workers-types"; + +declare global { + // `Event` and `EventTarget` have been global since Node 15, but aren't + // included in `@types/node`. + class Event extends WorkerEvent {} + type EventListenerOptions = EventTargetEventListenerOptions; + type AddEventListenerOptions = EventTargetAddEventListenerOptions; + // (can't use EventTarget from "@cloudflare/workers-types" as it's event map + // type parameters are incompatible with `tinybench`, a `vitest` dependency) + class EventTarget { + addEventListener( + type: string, + callback: EventListenerOrEventListenerObject | null, + options?: EventTargetAddEventListenerOptions | boolean + ): void; + dispatchEvent(event: Event): boolean; + removeEventListener( + type: string, + callback: EventListenerOrEventListenerObject | null, + options?: EventTargetEventListenerOptions | boolean + ): void; + } + + // `WebAssembly` has been global since Node 8, but isn't included in + // `@types/node`. + type BufferSource = ArrayBufferView | ArrayBuffer; + namespace WebAssembly { + class CompileError extends WorkerWebAssembly.CompileError {} + class RuntimeError extends WorkerWebAssembly.RuntimeError {} + + type ValueType = WorkerWebAssembly.ValueType; + type GlobalDescriptor = WorkerWebAssembly.GlobalDescriptor; + class Global extends WorkerWebAssembly.Global {} + + type ImportValue = WorkerWebAssembly.ImportValue; + type ModuleImports = WorkerWebAssembly.ModuleImports; + type Imports = WorkerWebAssembly.Imports; + + type ExportValue = WorkerWebAssembly.ExportValue; + type Exports = WorkerWebAssembly.Exports; + + class Instance extends WorkerWebAssembly.Instance {} + + type MemoryDescriptor = WorkerWebAssembly.MemoryDescriptor; + class Memory extends WorkerWebAssembly.Memory {} + + type ImportExportKind = WorkerWebAssembly.ImportExportKind; + type ModuleExportDescriptor = WorkerWebAssembly.ModuleExportDescriptor; + type ModuleImportDescriptor = WorkerWebAssembly.ModuleImportDescriptor; + class Module extends WorkerWebAssembly.Module { + // Node.js allows dynamic compilation of WebAssembly unlike Workers + constructor(bytes: BufferSource); + } + + type TableKind = WorkerWebAssembly.TableKind; + type TableDescriptor = WorkerWebAssembly.TableDescriptor; + class Table extends WorkerWebAssembly.Table {} + + // Node.js allows dynamic compilation of WebAssembly unlike Workers + interface WebAssemblyInstantiatedSource { + instance: Instance; + module: Module; + } + function compile(bytes: BufferSource): Promise; + function instantiate( + bytes: BufferSource, + importObject?: Imports + ): Promise; + function instantiate( + moduleObject: Module, + importObject?: Imports + ): Promise; + function validate(bytes: BufferSource): boolean; + } + + // `Worker` isn't defined on the global scope in Node.js, but it's required + // by `vite`. Therefore, define it as an empty interface. + // eslint-disable-next-line @typescript-eslint/no-empty-interface + interface Worker {} +}