@opennextjs/cloudflare 0.0.0-88fe982 → 0.0.0-9a03245

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.
@@ -1,46 +1,64 @@
1
+ import { AsyncLocalStorage } from "node:async_hooks";
1
2
  import Stream from "node:stream";
2
3
  import type { NextConfig } from "next";
3
4
  import { NodeNextRequest, NodeNextResponse } from "next/dist/server/base-http/node";
4
5
  import { MockedResponse } from "next/dist/server/lib/mock-request";
5
6
  import NextNodeServer, { NodeRequestHandler } from "next/dist/server/next-server";
6
7
  import type { IncomingMessage } from "node:http";
8
+ import { type CloudflareContext } from "../../api";
7
9
 
8
10
  const NON_BODY_RESPONSES = new Set([101, 204, 205, 304]);
9
11
 
12
+ const cloudflareContextALS = new AsyncLocalStorage<CloudflareContext>();
13
+
14
+ // Note: this symbol needs to be kept in sync with the one defined in `src/api/get-cloudflare-context.ts`
15
+ (globalThis as any)[Symbol.for("__cloudflare-context__")] = new Proxy(
16
+ {},
17
+ {
18
+ ownKeys: () => Reflect.ownKeys(cloudflareContextALS.getStore()!),
19
+ getOwnPropertyDescriptor: (_, ...args) =>
20
+ Reflect.getOwnPropertyDescriptor(cloudflareContextALS.getStore()!, ...args),
21
+ get: (_, property) => Reflect.get(cloudflareContextALS.getStore()!, property),
22
+ set: (_, property, value) => Reflect.set(cloudflareContextALS.getStore()!, property, value),
23
+ }
24
+ );
25
+
10
26
  // Injected at build time
11
27
  const nextConfig: NextConfig = JSON.parse(process.env.__NEXT_PRIVATE_STANDALONE_CONFIG ?? "{}");
12
28
 
13
29
  let requestHandler: NodeRequestHandler | null = null;
14
30
 
15
31
  export default {
16
- async fetch(request: Request, env: any, ctx: any) {
17
- if (requestHandler == null) {
18
- globalThis.process.env = { ...globalThis.process.env, ...env };
19
- requestHandler = new NextNodeServer({
20
- conf: { ...nextConfig, env },
21
- customServer: false,
22
- dev: false,
23
- dir: "",
24
- minimalMode: false,
25
- }).getRequestHandler();
26
- }
27
-
28
- const url = new URL(request.url);
29
-
30
- if (url.pathname === "/_next/image") {
31
- let imageUrl =
32
- url.searchParams.get("url") ?? "https://developers.cloudflare.com/_astro/logo.BU9hiExz.svg";
33
- if (imageUrl.startsWith("/")) {
34
- return env.ASSETS.fetch(new URL(imageUrl, request.url));
32
+ async fetch(request: Request & { cf: IncomingRequestCfProperties }, env: any, ctx: any) {
33
+ return cloudflareContextALS.run({ env, ctx, cf: request.cf }, async () => {
34
+ if (requestHandler == null) {
35
+ globalThis.process.env = { ...globalThis.process.env, ...env };
36
+ requestHandler = new NextNodeServer({
37
+ conf: { ...nextConfig, env },
38
+ customServer: false,
39
+ dev: false,
40
+ dir: "",
41
+ minimalMode: false,
42
+ }).getRequestHandler();
43
+ }
44
+
45
+ const url = new URL(request.url);
46
+
47
+ if (url.pathname === "/_next/image") {
48
+ let imageUrl =
49
+ url.searchParams.get("url") ?? "https://developers.cloudflare.com/_astro/logo.BU9hiExz.svg";
50
+ if (imageUrl.startsWith("/")) {
51
+ return env.ASSETS.fetch(new URL(imageUrl, request.url));
52
+ }
53
+ return fetch(imageUrl, { cf: { cacheEverything: true } } as any);
35
54
  }
36
- return fetch(imageUrl, { cf: { cacheEverything: true } } as any);
37
- }
38
55
 
39
- const { req, res, webResponse } = getWrappedStreams(request, ctx);
56
+ const { req, res, webResponse } = getWrappedStreams(request, ctx);
40
57
 
41
- ctx.waitUntil(requestHandler(new NodeNextRequest(req), new NodeNextResponse(res)));
58
+ ctx.waitUntil(requestHandler(new NodeNextRequest(req), new NodeNextResponse(res)));
42
59
 
43
- return await webResponse();
60
+ return await webResponse();
61
+ });
44
62
  },
45
63
  };
46
64
 
@@ -102,7 +120,6 @@ function getWrappedStreams(request: Request, ctx: any) {
102
120
  },
103
121
  set: function (val) {
104
122
  if (this.finished || this.headersSent) {
105
- console.error("headers already sent");
106
123
  return;
107
124
  }
108
125
  statusCode = val;
package/package.json CHANGED
@@ -1,16 +1,24 @@
1
1
  {
2
2
  "name": "@opennextjs/cloudflare",
3
3
  "description": "Cloudflare builder for next apps",
4
- "version": "0.0.0-88fe982",
5
- "bin": "dist/index.mjs",
4
+ "version": "0.0.0-9a03245",
5
+ "bin": "dist/cli/index.mjs",
6
+ "main": "./dist/api/index.mjs",
7
+ "types": "./dist/api/index.d.mts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/api/index.mjs",
11
+ "types": "./dist/api/index.d.mts"
12
+ }
13
+ },
6
14
  "files": [
7
15
  "README.md",
8
16
  "dist"
9
17
  ],
10
18
  "repository": {
11
19
  "type": "git",
12
- "url": "https://github.com/flarelabs-net/poc-next.git",
13
- "directory": "builder"
20
+ "url": "https://github.com/opennextjs/opennextjs-cloudflare.git",
21
+ "directory": "packages/cloudflare"
14
22
  },
15
23
  "keywords": [
16
24
  "cloudflare",
@@ -19,13 +27,15 @@
19
27
  ],
20
28
  "license": "MIT",
21
29
  "bugs": {
22
- "url": "https://github.com/flarelabs-net/poc-next/issues"
30
+ "url": "https://github.com/opennextjs/opennextjs-cloudflare/issues"
23
31
  },
24
- "homepage": "https://github.com/flarelabs-net/poc-next",
32
+ "homepage": "https://github.com/opennextjs/opennextjs-cloudflare",
25
33
  "devDependencies": {
34
+ "@cloudflare/workers-types": "^4.20240919.0",
26
35
  "@types/node": "^22.2.0",
27
36
  "esbuild": "^0.23.0",
28
37
  "glob": "^11.0.0",
38
+ "next": "14.2.11",
29
39
  "tsup": "^8.2.4",
30
40
  "typescript": "^5.5.4",
31
41
  "vitest": "^2.1.1"
@@ -33,6 +43,9 @@
33
43
  "dependencies": {
34
44
  "ts-morph": "^23.0.0"
35
45
  },
46
+ "peerDependencies": {
47
+ "wrangler": "^3.78.6"
48
+ },
36
49
  "scripts": {
37
50
  "build": "tsup",
38
51
  "build:watch": "tsup --watch src",
File without changes