Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
name: Code checks

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
test:
timeout-minutes: 60
checks:
name: ${{ matrix.script }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
script: ["prettier:check", "lint:check", "ts:check", "test"]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
- name: Check out code
uses: actions/checkout@v4

- name: Setup pnpm
uses: pnpm/action-setup@v4
with:
node-version: lts/*
version: 9

- name: Setup Node.js environment
uses: actions/setup-node@v4
with:
node-version: 20
cache: "pnpm"

- name: Install dependencies
run: npm install -g pnpm && pnpm install
- name: Run code checks
run: pnpm code:checks
run: pnpm install --frozen-lockfile

- name: ${{ matrix.script }}
run: pnpm run ${{ matrix.script }}
31 changes: 23 additions & 8 deletions .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
name: Playwright Tests

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
test:
timeout-minutes: 60
timeout-minutes: 30
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
- name: Check out code
uses: actions/checkout@v4

- name: Setup pnpm
uses: pnpm/action-setup@v4
with:
node-version: lts/*
version: 9

- name: Setup Node.js environment
uses: actions/setup-node@v4
with:
node-version: 20
cache: "pnpm"

- name: Install dependencies
run: npm install -g pnpm && pnpm install
- name: build all workers
run: |
pnpm install --frozen-lockfile
pnpm run install-playwright

- name: Build all workers
run: pnpm -r build:worker
- name: Install Playwright browsers
run: pnpm run install-playwright

- name: Run playwright tests
run: pnpm e2e

- name: Run playwright dev tests
run: pnpm e2e:dev
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"prettier:fix": "prettier --write .",
"lint:check": "pnpm -r lint:check",
"lint:fix": "pnpm -r lint:fix",
"code:checks": "pnpm lint:check && pnpm lint:check",
"ts:check": "pnpm -r ts:check",
"test": "pnpm -r test",
"code:checks": "pnpm prettier:check && pnpm lint:check && pnpm ts:check",
"code:fixes": "pnpm prettier:fix && pnpm lint:fix",
"postinstall": "pnpm --filter cloudflare build",
"install-playwright": "playwright install --with-deps",
Expand Down
12 changes: 12 additions & 0 deletions packages/cloudflare/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
declare global {
namespace NodeJS {
interface ProcessEnv {
ASSETS: Fetcher;
__NEXT_PRIVATE_STANDALONE_CONFIG?: string;
SKIP_NEXT_APP_BUILD?: string;
[key: string]: string;
}
}
}

export {};
2 changes: 2 additions & 0 deletions packages/cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"build:watch": "tsup --watch src",
"lint:check": "eslint",
"lint:fix": "eslint --fix",
"ts:check": "tsc --noEmit",
"test": "vitest --run",
"test:watch": "vitest"
},
Expand Down Expand Up @@ -41,6 +42,7 @@
"devDependencies": {
"@cloudflare/workers-types": "catalog:",
"@eslint/js": "catalog:",
"@tsconfig/strictest": "catalog:",
"@types/node": "catalog:",
"esbuild": "catalog:",
"eslint": "catalog:",
Expand Down
2 changes: 1 addition & 1 deletion packages/cloudflare/src/api/get-cloudflare-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type CloudflareContext<
/**
* the request's [cf properties](https://developers.cloudflare.com/workers/runtime-apis/request/#the-cf-property-requestinitcfproperties)
*/
cf: CfProperties;
cf: CfProperties | undefined;
/**
* the current [execution context](https://developers.cloudflare.com/workers/runtime-apis/context)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function getInstallChunkDeclaration(sourceFile: ts.SourceFile): ts.VariableDecla

// the function we're looking for accesses its parameter three times, and it
// accesses its `modules`, `ids` and `runtime` properties (in this order)
const parameterName = functionParameters[0].getText();
const parameterName = functionParameters[0]!.getText();
const functionParameterAccessedProperties = arrowFunctionBodyBlock
.getDescendantsOfKind(ts.SyntaxKind.PropertyAccessExpression)
.filter(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs, { writeFileSync } from "node:fs";
import { readFileSync, statSync, writeFileSync } from "node:fs";
import { Config } from "../../../config";
import path from "node:path";

Expand All @@ -15,11 +15,12 @@ export function patchWranglerDeps(config: Config) {
// "critters" = "./.next/standalone/node_modules/cf/templates/shims/empty.ts"
const pagesRuntimeFile = path.join(distPath, "compiled", "next-server", "pages.runtime.prod.js");

const patchedPagesRuntime = fs
.readFileSync(pagesRuntimeFile, "utf-8")
.replace(`e.exports=require("critters")`, `e.exports={}`);
const patchedPagesRuntime = readFileSync(pagesRuntimeFile, "utf-8").replace(
`e.exports=require("critters")`,
`e.exports={}`
);

fs.writeFileSync(pagesRuntimeFile, patchedPagesRuntime);
writeFileSync(pagesRuntimeFile, patchedPagesRuntime);

// Patch .next/standalone/node_modules/next/dist/server/lib/trace/tracer.js
//
Expand All @@ -33,11 +34,12 @@ export function patchWranglerDeps(config: Config) {
// #"@opentelemetry/api" = "./.next/standalone/node_modules/cf/templates/shims/throw.ts"
const tracerFile = path.join(distPath, "server", "lib", "trace", "tracer.js");

const pacthedTracer = fs
.readFileSync(tracerFile, "utf-8")
.replaceAll(/\w+\s*=\s*require\([^/]*opentelemetry.*\)/g, `throw new Error("@opentelemetry/api")`);
const patchedTracer = readFileSync(tracerFile, "utf-8").replaceAll(
/\w+\s*=\s*require\([^/]*opentelemetry.*\)/g,
`throw new Error("@opentelemetry/api")`
);

writeFileSync(tracerFile, pacthedTracer);
writeFileSync(tracerFile, patchedTracer);
}

/**
Expand All @@ -56,7 +58,7 @@ function getDistPath(config: Config): string {
for (const root of [config.paths.standaloneApp, config.paths.standaloneRoot]) {
try {
const distPath = path.join(root, "node_modules", "next", "dist");
if (fs.statSync(distPath).isDirectory()) return distPath;
if (statSync(distPath).isDirectory()) return distPath;
} catch {
/* empty */
}
Expand Down
17 changes: 8 additions & 9 deletions packages/cloudflare/src/cli/templates/worker.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { ExportedHandler, Fetcher } from "@cloudflare/workers-types";
import { NodeNextRequest, NodeNextResponse } from "next/dist/server/base-http/node";
import { AsyncLocalStorage } from "node:async_hooks";
import { type CloudflareContext } from "../../api";
import type { CloudflareContext } from "../../api";
import type { IncomingMessage } from "node:http";
import { MockedResponse } from "next/dist/server/lib/mock-request";
import type { NextConfig } from "next";
import { type NodeRequestHandler } from "next/dist/server/next-server";
import type { NodeRequestHandler } from "next/dist/server/next-server";
import Stream from "node:stream";

const NON_BODY_RESPONSES = new Set([101, 204, 205, 304]);
Expand All @@ -30,8 +31,7 @@ const nextConfig: NextConfig = JSON.parse(process.env.__NEXT_PRIVATE_STANDALONE_
let requestHandler: NodeRequestHandler | null = null;

export default {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async fetch(request: Request & { cf: IncomingRequestCfProperties }, env: any, ctx: any) {
async fetch(request, env, ctx) {
return cloudflareContextALS.run({ env, ctx, cf: request.cf }, async () => {
if (requestHandler == null) {
globalThis.process.env = { ...globalThis.process.env, ...env };
Expand All @@ -42,7 +42,7 @@ export default {
.default as typeof import("next/dist/server/next-server").default;

requestHandler = new NextNodeServer({
conf: { ...nextConfig, env },
conf: nextConfig,
customServer: false,
dev: false,
dir: "",
Expand All @@ -58,18 +58,17 @@ export default {
if (imageUrl.startsWith("/")) {
return env.ASSETS.fetch(new URL(imageUrl, request.url));
}
// @ts-ignore
return fetch(imageUrl, { cf: { cacheEverything: true } } as unknown);
return fetch(imageUrl, { cf: { cacheEverything: true } });
}

const { req, res, webResponse } = getWrappedStreams(request, ctx);

ctx.waitUntil(requestHandler!(new NodeNextRequest(req), new NodeNextResponse(res)));
ctx.waitUntil(Promise.resolve(requestHandler(new NodeNextRequest(req), new NodeNextResponse(res))));

return await webResponse();
});
},
};
} as ExportedHandler<{ ASSETS: Fetcher }>;

function getWrappedStreams(request: Request, ctx: ExecutionContext) {
const url = new URL(request.url);
Expand Down
16 changes: 11 additions & 5 deletions packages/cloudflare/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "@tsconfig/strictest/tsconfig.json",
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"lib": ["ESNext"],
"moduleResolution": "Bundler",
"esModuleInterop": true,

"types": ["@cloudflare/workers-types"],

"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"types": ["@cloudflare/workers-types"]
}
"noImplicitReturns": false,
"exactOptionalPropertyTypes": false
},
"include": ["**/*.ts"],
"exclude": ["dist"]
}
18 changes: 15 additions & 3 deletions pnpm-lock.yaml

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

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ catalog:
"@cloudflare/workers-types": ^4.20240925.0
"@eslint/js": ^9.11.1
"@playwright/test": 1.47.0
"@tsconfig/strictest": "^2.0.5"
"@types/node": ^22.2.0
"@types/react": ^18
"@types/react-dom": ^18
Expand Down
Loading