Skip to content

Commit

Permalink
Add manual Node types
Browse files Browse the repository at this point in the history
  • Loading branch information
GregBrimble committed Jan 5, 2023
1 parent aa91875 commit b4037ca
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 31 deletions.
3 changes: 1 addition & 2 deletions fixtures/external-durable-objects-app/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"lib": ["DOM"],
"types": ["node", "vitest"]
},
"include": ["**/*.ts"]
"include": ["**/*.ts", "../../../node-types.d.ts"]
}
3 changes: 1 addition & 2 deletions fixtures/node-app-pages/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"lib": ["DOM"],
"types": ["node", "vitest"]
},
"include": ["**/*.ts"]
"include": ["**/*.ts", "../../../node-types.d.ts"]
}
4 changes: 1 addition & 3 deletions fixtures/pages-d1-shim/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"lib": ["DOM"],
"types": ["node", "vitest"]
},
"include": ["**/*.ts"],
"exclude": []
"include": ["**/*.ts", "../../../node-types.d.ts"]
}
4 changes: 1 addition & 3 deletions fixtures/pages-functions-app/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"lib": ["DOM"],
"types": ["node", "vitest"]
},
"include": ["**/*.ts"],
"exclude": []
"include": ["**/*.ts", "../../../node-types.d.ts"]
}
3 changes: 1 addition & 2 deletions fixtures/pages-functions-with-routes-app/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"lib": ["DOM"],
"types": ["node", "vitest"]
},
"include": ["**/*.ts"]
"include": ["**/*.ts", "../../../node-types.d.ts"]
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"lib": ["DOM"],
"types": ["node", "vitest"]
},
"include": ["**/*.ts"],
"exclude": []
"include": ["**/*.ts", "../../../node-types.d.ts"]
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"lib": ["DOM"],
"types": ["node", "vitest"]
},
"include": ["**/*.ts"]
"include": ["**/*.ts", "../../../node-types.d.ts"]
}
4 changes: 2 additions & 2 deletions fixtures/pages-workerjs-app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
4 changes: 2 additions & 2 deletions fixtures/pages-workerjs-with-routes-app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
3 changes: 1 addition & 2 deletions fixtures/pages-ws-app/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"lib": ["DOM"],
"types": ["node", "vitest"]
},
"include": ["**/*.ts"]
"include": ["**/*.ts", "../../../node-types.d.ts"]
}
4 changes: 1 addition & 3 deletions fixtures/remix-pages-app/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"lib": ["ES2019", "DOM"],
"types": ["node", "vitest"]
},
"include": ["**/*.ts"],
"exclude": []
"include": ["**/*.ts", "../../../node-types.d.ts"]
}
4 changes: 1 addition & 3 deletions fixtures/service-bindings-app/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"lib": ["DOM"],
"types": ["node", "vitest"]
},
"include": ["**/*.ts"],
"exclude": []
"include": ["**/*.ts", "../../../node-types.d.ts"]
}
4 changes: 2 additions & 2 deletions fixtures/worker-app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
91 changes: 91 additions & 0 deletions node-types.d.ts
Original file line number Diff line number Diff line change
@@ -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<Module>;
function instantiate(
bytes: BufferSource,
importObject?: Imports
): Promise<WebAssemblyInstantiatedSource>;
function instantiate(
moduleObject: Module,
importObject?: Imports
): Promise<Instance>;
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 {}
}

0 comments on commit b4037ca

Please sign in to comment.