From 256af56c4fb34a6c0efaa36f2765a966928816de Mon Sep 17 00:00:00 2001 From: MrBBot Date: Fri, 21 Jul 2023 21:56:45 +0100 Subject: [PATCH] Bump to TypeScript 5 and fix bundled TypeScript output (#635) * Bump to `typescript@5.0.4` and `@microsoft/api-extractor@7.63.3` TypeScript 5 includes lots of goodies, and allows us to remove a bunch of `@ts-expect-error`s. * Set `"moduleResolution": "bundler"` in `tsconfig.json` This loosens some resolution rules in accordance with how bundlers like `esbuild` resolve modules. This also allows us to remove a bunch of `@ts-expect-error`s. * Remove import cycle in `src/plugins/r2` This probably wasn't a problem, but generally it's not great to have cyclic dependencies. For future reference, this was found with `npx madge --circular --extensions ts packages/miniflare/src`. --- packages/miniflare/src/plugins/kv/sites.ts | 1 - .../miniflare/src/plugins/queues/gateway.ts | 2 -- .../miniflare/src/plugins/queues/index.ts | 2 -- .../miniflare/src/plugins/queues/router.ts | 2 -- packages/miniflare/src/plugins/r2/gateway.ts | 20 +------------------ packages/miniflare/src/plugins/r2/r2Object.ts | 19 +++++++++++++++++- packages/miniflare/src/plugins/r2/router.ts | 4 ++-- packages/miniflare/src/storage/blob/store.ts | 2 -- .../src/workers/core/entry.worker.ts | 4 ---- packages/miniflare/src/workers/tsconfig.json | 2 +- 10 files changed, 22 insertions(+), 36 deletions(-) diff --git a/packages/miniflare/src/plugins/kv/sites.ts b/packages/miniflare/src/plugins/kv/sites.ts index ae3ff57d83f5..8dcc419a628f 100644 --- a/packages/miniflare/src/plugins/kv/sites.ts +++ b/packages/miniflare/src/plugins/kv/sites.ts @@ -279,7 +279,6 @@ export async function sitesGatewayGet( typeof e === "object" && e !== null && "code" in e && - // @ts-expect-error `e.code` should be `unknown`, fixed in TypeScript 4.9 e.code === "ENOENT" ) { return; diff --git a/packages/miniflare/src/plugins/queues/gateway.ts b/packages/miniflare/src/plugins/queues/gateway.ts index b0c721359a71..7b2783d65952 100644 --- a/packages/miniflare/src/plugins/queues/gateway.ts +++ b/packages/miniflare/src/plugins/queues/gateway.ts @@ -1,8 +1,6 @@ import assert from "assert"; import crypto from "crypto"; import v8 from "v8"; -// @ts-expect-error "devalue" is ESM-only, but we're bundling for CommonJS here. -// That doesn't matter to `esbuild`, which will apply format conversion. import { stringify } from "devalue"; import { Colorize, bold, green, grey, red, reset, yellow } from "kleur/colors"; import { z } from "zod"; diff --git a/packages/miniflare/src/plugins/queues/index.ts b/packages/miniflare/src/plugins/queues/index.ts index 218bf95f2408..794dffc1ff1c 100644 --- a/packages/miniflare/src/plugins/queues/index.ts +++ b/packages/miniflare/src/plugins/queues/index.ts @@ -1,5 +1,3 @@ -// @ts-expect-error "devalue" is ESM-only, but we're bundling for CommonJS here. -// That doesn't matter to `esbuild`, which will apply format conversion. import { stringify } from "devalue"; import semiver from "semiver"; import { z } from "zod"; diff --git a/packages/miniflare/src/plugins/queues/router.ts b/packages/miniflare/src/plugins/queues/router.ts index dc2dcdb5e31e..cb53c38efefe 100644 --- a/packages/miniflare/src/plugins/queues/router.ts +++ b/packages/miniflare/src/plugins/queues/router.ts @@ -1,5 +1,3 @@ -// @ts-expect-error "devalue" is ESM-only, but we're bundling for CommonJS here. -// That doesn't matter to `esbuild`, which will apply format conversion. import { parse } from "devalue"; import { z } from "zod"; import { Headers, Response } from "../../http"; diff --git a/packages/miniflare/src/plugins/r2/gateway.ts b/packages/miniflare/src/plugins/r2/gateway.ts index 31ea67670379..c4aa5af72eae 100644 --- a/packages/miniflare/src/plugins/r2/gateway.ts +++ b/packages/miniflare/src/plugins/r2/gateway.ts @@ -27,7 +27,7 @@ import { NoSuchUpload, PreconditionFailed, } from "./errors"; -import { R2Object, R2ObjectBody } from "./r2Object"; +import { R2Object, R2ObjectBody, R2Objects } from "./r2Object"; import { MultipartPartRow, MultipartUploadRow, @@ -111,24 +111,6 @@ class DigestingStream< } } -export interface R2Objects { - // An array of objects matching the list request. - objects: R2Object[]; - // If true, indicates there are more results to be retrieved for the current - // list request. - truncated: boolean; - // A token that can be passed to future list calls to resume listing from that - // point. - // Only present if truncated is true. - cursor?: string; - // If a delimiter has been specified, contains all prefixes between the - // specified prefix and the next occurrence of the delimiter. For example, if - // no prefix is provided and the delimiter is "/", "foo/bar/baz" would return - // "foo" as a delimited prefix. If "foo/" was passed as a prefix with the same - // structure and delimiter, "foo/bar" would be returned as a delimited prefix. - delimitedPrefixes: string[]; -} - const validate = new Validator(); function generateVersion() { diff --git a/packages/miniflare/src/plugins/r2/r2Object.ts b/packages/miniflare/src/plugins/r2/r2Object.ts index 02ec0dc25e63..9236fb404167 100644 --- a/packages/miniflare/src/plugins/r2/r2Object.ts +++ b/packages/miniflare/src/plugins/r2/r2Object.ts @@ -2,7 +2,6 @@ import { Blob } from "buffer"; import { ReadableStream, TransformStream } from "stream/web"; import type { R2StringChecksums } from "@cloudflare/workers-types/experimental"; import { HEX_REGEXP } from "../../shared"; -import { R2Objects } from "./gateway"; import { ObjectRow, R2HeadResponse, R2HttpFields, R2Range } from "./schemas"; export interface EncodedMetadata { @@ -101,3 +100,21 @@ export class R2ObjectBody extends R2Object { }; } } + +export interface R2Objects { + // An array of objects matching the list request. + objects: R2Object[]; + // If true, indicates there are more results to be retrieved for the current + // list request. + truncated: boolean; + // A token that can be passed to future list calls to resume listing from that + // point. + // Only present if truncated is true. + cursor?: string; + // If a delimiter has been specified, contains all prefixes between the + // specified prefix and the next occurrence of the delimiter. For example, if + // no prefix is provided and the delimiter is "/", "foo/bar/baz" would return + // "foo" as a delimited prefix. If "foo/" was passed as a prefix with the same + // structure and delimiter, "foo/bar" would be returned as a delimited prefix. + delimitedPrefixes: string[]; +} diff --git a/packages/miniflare/src/plugins/r2/router.ts b/packages/miniflare/src/plugins/r2/router.ts index e352aa129c8e..390e726c0aa1 100644 --- a/packages/miniflare/src/plugins/r2/router.ts +++ b/packages/miniflare/src/plugins/r2/router.ts @@ -10,8 +10,8 @@ import { decodePersist, } from "../shared"; import { InternalError, InvalidMetadata } from "./errors"; -import { R2Gateway, R2Objects } from "./gateway"; -import { EncodedMetadata, R2Object, R2ObjectBody } from "./r2Object"; +import { R2Gateway } from "./gateway"; +import { EncodedMetadata, R2Object, R2ObjectBody, R2Objects } from "./r2Object"; import { R2BindingRequestSchema } from "./schemas"; async function decodeMetadata(req: Request) { diff --git a/packages/miniflare/src/storage/blob/store.ts b/packages/miniflare/src/storage/blob/store.ts index 036490ffacd8..0c61d4e0d129 100644 --- a/packages/miniflare/src/storage/blob/store.ts +++ b/packages/miniflare/src/storage/blob/store.ts @@ -144,7 +144,6 @@ export class FileBlobStore implements BlobStore { typeof e === "object" && e !== null && "code" in e && - // @ts-expect-error `e.code` should be `unknown`, fixed in TypeScript 4.9 e.code === "ENOENT" ) { return null; @@ -181,7 +180,6 @@ export class FileBlobStore implements BlobStore { typeof e === "object" && e !== null && "code" in e && - // @ts-expect-error `e.code` should be `unknown`, fixed in TypeScript 4.9 e.code === "ENOENT" ) { return; diff --git a/packages/miniflare/src/workers/core/entry.worker.ts b/packages/miniflare/src/workers/core/entry.worker.ts index 3c4785e5f469..085aaf1f4844 100644 --- a/packages/miniflare/src/workers/core/entry.worker.ts +++ b/packages/miniflare/src/workers/core/entry.worker.ts @@ -1,7 +1,3 @@ -// @ts-expect-error "devalue" is ESM-only, so TypeScript requires us to use the -// `*.mts` extension, or set `"type": "module"` in our `package.json`. We can't -// do the first as ambient types from `@cloudflare/workers-types` don't seem to -// work, and the second would affect all consumers of `miniflare`. import { unflatten } from "devalue"; import { CoreBindings, CoreHeaders, LogLevel } from "./constants"; import { structuredSerializableRevivers } from "./devalue"; diff --git a/packages/miniflare/src/workers/tsconfig.json b/packages/miniflare/src/workers/tsconfig.json index 304e2bc28f61..162ccbec491a 100644 --- a/packages/miniflare/src/workers/tsconfig.json +++ b/packages/miniflare/src/workers/tsconfig.json @@ -4,7 +4,7 @@ "target": "esnext", "lib": ["esnext"], "strict": true, - "moduleResolution": "node16", + "moduleResolution": "bundler", "isolatedModules": true, "noEmit": true, "types": ["@cloudflare/workers-types/experimental"]