Skip to content

Commit

Permalink
Forgive me for my TS sins
Browse files Browse the repository at this point in the history
  • Loading branch information
GregBrimble committed Sep 8, 2022
1 parent b93ae95 commit d244090
Show file tree
Hide file tree
Showing 10 changed files with 224 additions and 47 deletions.
98 changes: 96 additions & 2 deletions package-lock.json

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

6 changes: 5 additions & 1 deletion packages/pages-shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@
"node_modules/(?!find-up|locate-path|p-locate|p-limit|p-timeout|p-queue|yocto-queue|path-exists|execa|strip-final-newline|npm-run-path|path-key|onetime|mimic-fn|human-signals|is-stream|get-port|supports-color|pretty-bytes)"
]
},
"dependencies": {
"@miniflare/core": "2.7.1"
},
"devDependencies": {
"@cloudflare/workers-types": "^3.16.0",
"@miniflare/cache": "^2.8.1",
"@miniflare/html-rewriter": "^2.8.1",
"@types/service-worker-mock": "^2.0.1",
"concurrently": "^7.3.0",
"glob": "^8.0.3",
Expand Down
17 changes: 8 additions & 9 deletions packages/pages-shared/src/asset-server/handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
import "../environment-polyfills/types";

import {
FoundResponse,
Expand Down Expand Up @@ -91,7 +90,7 @@ type FullHandlerContext<AssetEntry, ContentNegotiation, Asset> = {
assetEntry: AssetEntry
) => void;
caches: CacheStorage;
waitUntil: ExecutionContext["waitUntil"];
waitUntil: (promise: Promise<unknown>) => void;
HTMLRewriter: typeof HTMLRewriter;
};

Expand Down Expand Up @@ -125,14 +124,14 @@ export async function generateHandler<
negotiateContent,
fetchAsset,
generateNotFoundResponse = async (
_notFoundRequest,
findNotFoundAssetEntryForPath,
serveNotFoundAsset
notFoundRequest,
notFoundFindAssetEntryForPath,
notFoundServeAsset
) => {
let assetEntry: AssetEntry | null;
// No custom 404 page, so try serving as a single-page app
if ((assetEntry = await findNotFoundAssetEntryForPath("/index.html"))) {
return serveNotFoundAsset(assetEntry, { preserve: false });
if ((assetEntry = await notFoundFindAssetEntryForPath("/index.html"))) {
return notFoundServeAsset(assetEntry, { preserve: false });
}

return new NotFoundResponse();
Expand Down Expand Up @@ -441,7 +440,7 @@ export async function generateHandler<

try {
const asset = await fetchAsset(assetKey);
const headers: HeadersInit = {
const headers: Record<string, string> = {
etag,
"content-type": asset.contentType,
};
Expand Down
3 changes: 1 addition & 2 deletions packages/pages-shared/src/asset-server/responses.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
import "../environment-polyfills/types";

type HeadersInit = ConstructorParameters<typeof Headers>[0];

Expand Down
3 changes: 1 addition & 2 deletions packages/pages-shared/src/asset-server/rulesEngine.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
import "../environment-polyfills/types";

// Taken from https://stackoverflow.com/a/3561711
// which is everything from the tc39 proposal, plus the following two characters: ^/
Expand Down
14 changes: 14 additions & 0 deletions packages/pages-shared/src/environment-polyfills/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { PolyfilledRuntimeEnvironment } from "./types";

export const polyfill = (
environment: Record<keyof PolyfilledRuntimeEnvironment, unknown>
) => {
Object.entries(environment).map(([name, value]) => {
Object.defineProperty(globalThis, name, {
value,
configurable: true,
enumerable: true,
writable: true,
});
});
};
14 changes: 14 additions & 0 deletions packages/pages-shared/src/environment-polyfills/miniflare.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {
fetch as miniflareFetch,
Headers as MiniflareHeaders,
Request as MiniflareRequest,
Response as MiniflareResponse,
} from "@miniflare/core";
import { polyfill } from ".";

polyfill({
fetch: miniflareFetch,
Headers: MiniflareHeaders,
Request: MiniflareRequest,
Response: MiniflareResponse,
});
44 changes: 44 additions & 0 deletions packages/pages-shared/src/environment-polyfills/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {
Headers as MiniflareHeaders,
Request as MiniflareRequest,
Response as MiniflareResponse,
} from "@miniflare/core";
import { HTMLRewriter as MiniflareHTMLRewriter } from "@miniflare/html-rewriter";
import type { CacheInterface as MiniflareCacheInterface } from "@miniflare/cache";
import type { fetch as miniflareFetch } from "@miniflare/core";
import type { ReadableStream as SimilarReadableStream } from "stream/web";

declare global {
const fetch: typeof miniflareFetch;
class Headers extends MiniflareHeaders {}
class Request extends MiniflareRequest {}
class Response extends MiniflareResponse {}

type CacheInterface = Omit<MiniflareCacheInterface, "match"> & {
match(
...args: Parameters<MiniflareCacheInterface["match"]>
): Promise<Response | undefined>;
};

class CacheStorage {
get default(): CacheInterface;
open(cacheName: string): Promise<CacheInterface>;
}

class HTMLRewriter extends MiniflareHTMLRewriter {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
transform(response: Response): Response;
}

type ReadableStream = SimilarReadableStream;
}

export type PolyfilledRuntimeEnvironment = {
fetch: typeof fetch;
Headers: typeof Headers;
Request: typeof Request;
Response: typeof Response;
};

export { fetch, Headers, Request, Response };
2 changes: 1 addition & 1 deletion packages/pages-shared/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"module": "CommonJS",
"types": ["@cloudflare/workers-types", "jest"]
"types": ["jest"]
}
}

0 comments on commit d244090

Please sign in to comment.