@opennextjs/cloudflare 0.0.0-af15fd1 → 0.0.0-cf5113b

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,7 +17,7 @@ function existsSync(path: string) {
17
17
  return FILES.has(path);
18
18
  }
19
19
 
20
- async function readFile(path: string, options: unknown): Promise<any> {
20
+ async function readFile(path: string, options: unknown): Promise<unknown> {
21
21
  console.log(
22
22
  "readFile",
23
23
  { path, options }
@@ -1,17 +1,18 @@
1
- import { AsyncLocalStorage } from "node:async_hooks";
2
- import Stream from "node:stream";
3
- import type { NextConfig } from "next";
4
- import { NodeNextRequest, NodeNextResponse } from "next/dist/server/base-http/node";
5
- import { MockedResponse } from "next/dist/server/lib/mock-request";
6
1
  import NextNodeServer, { NodeRequestHandler } from "next/dist/server/next-server";
7
- import type { IncomingMessage } from "node:http";
2
+ import { NodeNextRequest, NodeNextResponse } from "next/dist/server/base-http/node";
3
+ import { AsyncLocalStorage } from "node:async_hooks";
8
4
  import { type CloudflareContext } from "../../api";
5
+ import type { IncomingMessage } from "node:http";
6
+ import { MockedResponse } from "next/dist/server/lib/mock-request";
7
+ import type { NextConfig } from "next";
8
+ import Stream from "node:stream";
9
9
 
10
10
  const NON_BODY_RESPONSES = new Set([101, 204, 205, 304]);
11
11
 
12
12
  const cloudflareContextALS = new AsyncLocalStorage<CloudflareContext>();
13
13
 
14
14
  // Note: this symbol needs to be kept in sync with the one defined in `src/api/get-cloudflare-context.ts`
15
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
15
16
  (globalThis as any)[Symbol.for("__cloudflare-context__")] = new Proxy(
16
17
  {},
17
18
  {
@@ -29,6 +30,7 @@ const nextConfig: NextConfig = JSON.parse(process.env.__NEXT_PRIVATE_STANDALONE_
29
30
  let requestHandler: NodeRequestHandler | null = null;
30
31
 
31
32
  export default {
33
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
32
34
  async fetch(request: Request & { cf: IncomingRequestCfProperties }, env: any, ctx: any) {
33
35
  return cloudflareContextALS.run({ env, ctx, cf: request.cf }, async () => {
34
36
  if (requestHandler == null) {
@@ -45,12 +47,13 @@ export default {
45
47
  const url = new URL(request.url);
46
48
 
47
49
  if (url.pathname === "/_next/image") {
48
- let imageUrl =
50
+ const imageUrl =
49
51
  url.searchParams.get("url") ?? "https://developers.cloudflare.com/_astro/logo.BU9hiExz.svg";
50
52
  if (imageUrl.startsWith("/")) {
51
53
  return env.ASSETS.fetch(new URL(imageUrl, request.url));
52
54
  }
53
- return fetch(imageUrl, { cf: { cacheEverything: true } } as any);
55
+ // @ts-ignore
56
+ return fetch(imageUrl, { cf: { cacheEverything: true } } as unknown);
54
57
  }
55
58
 
56
59
  const { req, res, webResponse } = getWrappedStreams(request, ctx);
@@ -62,12 +65,12 @@ export default {
62
65
  },
63
66
  };
64
67
 
65
- function getWrappedStreams(request: Request, ctx: any) {
68
+ function getWrappedStreams(request: Request, ctx: ExecutionContext) {
66
69
  const url = new URL(request.url);
67
70
 
68
- const req = (
69
- request.body ? Stream.Readable.fromWeb(request.body as any) : Stream.Readable.from([])
70
- ) as IncomingMessage;
71
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
72
+ const reqBody = request.body && Stream.Readable.fromWeb(request.body as any);
73
+ const req = (reqBody ?? Stream.Readable.from([])) as IncomingMessage;
71
74
  req.httpVersion = "1.0";
72
75
  req.httpVersionMajor = 1;
73
76
  req.httpVersionMinor = 0;
@@ -94,7 +97,7 @@ function getWrappedStreams(request: Request, ctx: any) {
94
97
 
95
98
  const res = new MockedResponse({
96
99
  resWriter: (chunk) => {
97
- resBodyWriter.write(typeof chunk === "string" ? Buffer.from(chunk) : chunk).catch((err: any) => {
100
+ resBodyWriter.write(typeof chunk === "string" ? Buffer.from(chunk) : chunk).catch((err) => {
98
101
  if (
99
102
  err.message.includes("WritableStream has been closed") ||
100
103
  err.message.includes("Network connection lost")
@@ -110,6 +113,7 @@ function getWrappedStreams(request: Request, ctx: any) {
110
113
  });
111
114
 
112
115
  // It's implemented as a no-op, but really it should mark the headers as done
116
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
113
117
  res.flushHeaders = () => (res as any).headPromiseResolve();
114
118
 
115
119
  // Only allow statusCode to be modified if not sent
@@ -127,6 +131,7 @@ function getWrappedStreams(request: Request, ctx: any) {
127
131
  });
128
132
 
129
133
  // Make sure the writer is eventually closed
134
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
130
135
  ctx.waitUntil((res as any).hasStreamed.finally(() => resBodyWriter.close().catch(() => {})));
131
136
 
132
137
  return {
@@ -138,6 +143,7 @@ function getWrappedStreams(request: Request, ctx: any) {
138
143
  res.setHeader("content-encoding", "identity");
139
144
  return new Response(NON_BODY_RESPONSES.has(res.statusCode) ? null : readable, {
140
145
  status: res.statusCode,
146
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
141
147
  headers: (res as any).headers,
142
148
  });
143
149
  },
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@opennextjs/cloudflare",
3
3
  "description": "Cloudflare builder for next apps",
4
- "version": "0.0.0-af15fd1",
4
+ "version": "0.0.0-cf5113b",
5
5
  "bin": "dist/cli/index.mjs",
6
6
  "main": "./dist/api/index.mjs",
7
7
  "types": "./dist/api/index.d.mts",
@@ -31,21 +31,32 @@
31
31
  },
32
32
  "homepage": "https://github.com/opennextjs/opennextjs-cloudflare",
33
33
  "devDependencies": {
34
- "@cloudflare/workers-types": "^4.20240919.0",
34
+ "@cloudflare/workers-types": "^4.20240925.0",
35
+ "@eslint/js": "^9.11.1",
35
36
  "@types/node": "^22.2.0",
36
37
  "esbuild": "^0.23.0",
38
+ "eslint": "^9.11.1",
39
+ "eslint-plugin-unicorn": "^55.0.0",
37
40
  "glob": "^11.0.0",
41
+ "globals": "^15.9.0",
38
42
  "next": "14.2.11",
43
+ "package-manager-detector": "^0.2.0",
39
44
  "tsup": "^8.2.4",
40
45
  "typescript": "^5.5.4",
46
+ "typescript-eslint": "^8.7.0",
41
47
  "vitest": "^2.1.1"
42
48
  },
43
49
  "dependencies": {
44
50
  "ts-morph": "^23.0.0"
45
51
  },
52
+ "peerDependencies": {
53
+ "wrangler": "^3.78.10"
54
+ },
46
55
  "scripts": {
47
56
  "build": "tsup",
48
57
  "build:watch": "tsup --watch src",
58
+ "lint:check": "eslint",
59
+ "lint:fix": "eslint --fix",
49
60
  "test": "vitest --run",
50
61
  "test:watch": "vitest"
51
62
  }
@@ -1,14 +0,0 @@
1
- // src/api/get-cloudflare-context.ts
2
- import "server-only";
3
- var cloudflareContextSymbol = Symbol.for("__cloudflare-context__");
4
- async function getCloudflareContext() {
5
- const cloudflareContext = globalThis[cloudflareContextSymbol];
6
- if (!cloudflareContext) {
7
- throw new Error("Cloudflare context is not defined!");
8
- }
9
- return cloudflareContext;
10
- }
11
-
12
- export {
13
- getCloudflareContext
14
- };