@opennextjs/cloudflare 1.0.4 → 1.2.0

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.
Files changed (51) hide show
  1. package/dist/api/cloudflare-context.d.ts +5 -0
  2. package/dist/api/config.d.ts +12 -2
  3. package/dist/api/config.js +9 -2
  4. package/dist/api/durable-objects/bucket-cache-purge.d.ts +7 -0
  5. package/dist/api/durable-objects/bucket-cache-purge.js +75 -0
  6. package/dist/api/durable-objects/bucket-cache-purge.spec.js +121 -0
  7. package/dist/api/overrides/cache-purge/index.d.ts +12 -0
  8. package/dist/api/overrides/cache-purge/index.js +26 -0
  9. package/dist/api/overrides/internal.d.ts +2 -0
  10. package/dist/api/overrides/internal.js +52 -0
  11. package/dist/api/overrides/queue/do-queue.js +1 -1
  12. package/dist/api/overrides/queue/queue-cache.d.ts +36 -0
  13. package/dist/api/overrides/queue/queue-cache.js +93 -0
  14. package/dist/api/overrides/queue/queue-cache.spec.d.ts +1 -0
  15. package/dist/api/overrides/queue/queue-cache.spec.js +92 -0
  16. package/dist/api/overrides/tag-cache/d1-next-tag-cache.js +2 -1
  17. package/dist/api/overrides/tag-cache/do-sharded-tag-cache.d.ts +20 -0
  18. package/dist/api/overrides/tag-cache/do-sharded-tag-cache.js +70 -7
  19. package/dist/api/overrides/tag-cache/do-sharded-tag-cache.spec.js +81 -1
  20. package/dist/cli/build/bundle-server.d.ts +1 -1
  21. package/dist/cli/build/bundle-server.js +16 -38
  22. package/dist/cli/build/open-next/compileDurableObjects.js +1 -0
  23. package/dist/cli/build/open-next/createServerBundle.js +9 -10
  24. package/dist/cli/build/patches/index.d.ts +0 -1
  25. package/dist/cli/build/patches/index.js +0 -1
  26. package/dist/cli/build/patches/investigated/index.d.ts +0 -1
  27. package/dist/cli/build/patches/investigated/index.js +0 -1
  28. package/dist/cli/build/patches/plugins/load-manifest.d.ts +3 -1
  29. package/dist/cli/build/patches/plugins/load-manifest.js +49 -7
  30. package/dist/cli/build/patches/plugins/next-server.d.ts +25 -0
  31. package/dist/cli/build/patches/plugins/next-server.js +110 -0
  32. package/dist/cli/build/patches/plugins/next-server.spec.d.ts +1 -0
  33. package/dist/cli/build/patches/plugins/next-server.spec.js +429 -0
  34. package/dist/cli/build/patches/plugins/open-next.d.ts +8 -0
  35. package/dist/cli/build/patches/plugins/open-next.js +39 -0
  36. package/dist/cli/templates/init.js +5 -0
  37. package/dist/cli/templates/shims/throw.d.ts +2 -0
  38. package/dist/cli/templates/shims/throw.js +2 -0
  39. package/dist/cli/templates/worker.d.ts +1 -0
  40. package/dist/cli/templates/worker.js +2 -0
  41. package/package.json +3 -3
  42. package/dist/cli/build/patches/investigated/patch-cache.d.ts +0 -14
  43. package/dist/cli/build/patches/investigated/patch-cache.js +0 -40
  44. package/dist/cli/build/patches/plugins/build-id.d.ts +0 -6
  45. package/dist/cli/build/patches/plugins/build-id.js +0 -29
  46. package/dist/cli/build/patches/plugins/build-id.spec.js +0 -82
  47. package/dist/cli/build/patches/plugins/eval-manifest.d.ts +0 -7
  48. package/dist/cli/build/patches/plugins/eval-manifest.js +0 -61
  49. package/dist/cli/build/patches/to-investigate/inline-middleware-manifest.d.ts +0 -6
  50. package/dist/cli/build/patches/to-investigate/inline-middleware-manifest.js +0 -15
  51. /package/dist/{cli/build/patches/plugins/build-id.spec.d.ts → api/durable-objects/bucket-cache-purge.spec.d.ts} +0 -0
@@ -1,40 +0,0 @@
1
- import path from "node:path";
2
- import { getPackagePath } from "@opennextjs/aws/build/helper.js";
3
- import { normalizePath } from "../../utils/index.js";
4
- /**
5
- * Sets up the OpenNext cache handler in a Next.js build.
6
- *
7
- * The cache handler used by Next.js is normally defined in the config file as a path. At runtime,
8
- * Next.js would then do a dynamic require on a transformed version of the path to retrieve the
9
- * cache handler and create a new instance of it.
10
- *
11
- * This is problematic in workerd due to the dynamic import of the file that is not known from
12
- * build-time. Therefore, we have to manually override the default way that the cache handler is
13
- * instantiated with a dynamic require that uses a string literal for the path.
14
- */
15
- export async function patchCache(code, buildOpts) {
16
- const { outputDir } = buildOpts;
17
- // TODO: switch to cache.mjs
18
- const outputPath = path.join(outputDir, "server-functions/default");
19
- const cacheFile = path.join(outputPath, getPackagePath(buildOpts), "cache.cjs");
20
- return code.replace("const { cacheHandler } = this.nextConfig;", `
21
- const cacheHandler = null;
22
- CacheHandler = require('${normalizePath(cacheFile)}').default;
23
- `);
24
- }
25
- export async function patchComposableCache(code, buildOpts) {
26
- const { outputDir } = buildOpts;
27
- // TODO: switch to mjs
28
- const outputPath = path.join(outputDir, "server-functions/default");
29
- const cacheFile = path.join(outputPath, getPackagePath(buildOpts), "composable-cache.cjs");
30
- //TODO: Do we want to move this to the new CodePatcher ?
31
- return code.replace("const { cacheHandlers } = this.nextConfig.experimental", `
32
- const cacheHandlers = null;
33
- const handlersSymbol = Symbol.for('@next/cache-handlers');
34
- const handlersMapSymbol = Symbol.for('@next/cache-handlers-map');
35
- const handlersSetSymbol = Symbol.for('@next/cache-handlers-set');
36
- globalThis[handlersMapSymbol] = new Map();
37
- globalThis[handlersMapSymbol].set("default", require('${normalizePath(cacheFile)}').default);
38
- globalThis[handlersSetSymbol] = new Set(globalThis[handlersMapSymbol].values());
39
- `);
40
- }
@@ -1,6 +0,0 @@
1
- /**
2
- * Inline `getBuildId` as it relies on `readFileSync` that is not supported by workerd.
3
- */
4
- import type { ContentUpdater, Plugin } from "@opennextjs/aws/plugins/content-updater.js";
5
- export declare function inlineBuildId(updater: ContentUpdater): Plugin;
6
- export declare const rule = "\nrule:\n kind: method_definition\n has:\n field: name\n regex: ^getBuildId$\nfix: |-\n getBuildId() {\n return process.env.NEXT_BUILD_ID;\n }\n";
@@ -1,29 +0,0 @@
1
- /**
2
- * Inline `getBuildId` as it relies on `readFileSync` that is not supported by workerd.
3
- */
4
- import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js";
5
- import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js";
6
- export function inlineBuildId(updater) {
7
- return updater.updateContent("inline-build-id", [
8
- {
9
- field: {
10
- filter: getCrossPlatformPathRegex(String.raw `/next/dist/server/next-server\.js$`, {
11
- escape: false,
12
- }),
13
- contentFilter: /getBuildId\(/,
14
- callback: ({ contents }) => patchCode(contents, rule),
15
- },
16
- },
17
- ]);
18
- }
19
- export const rule = `
20
- rule:
21
- kind: method_definition
22
- has:
23
- field: name
24
- regex: ^getBuildId$
25
- fix: |-
26
- getBuildId() {
27
- return process.env.NEXT_BUILD_ID;
28
- }
29
- `;
@@ -1,82 +0,0 @@
1
- import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js";
2
- import { describe, expect, test } from "vitest";
3
- import { rule } from "./build-id.js";
4
- describe("getBuildId", () => {
5
- test("patch", () => {
6
- const code = `
7
- class NextNodeServer extends _baseserver.default {
8
- constructor(options){
9
- // Initialize super class
10
- super(options);
11
- this.handleNextImageRequest = async (req, res, parsedUrl) => { /* ... */ };
12
- }
13
- async handleUpgrade() {
14
- // The web server does not support web sockets, it's only used for HMR in
15
- // development.
16
- }
17
- loadEnvConfig({ dev, forceReload, silent }) {
18
- (0, _env.loadEnvConfig)(this.dir, dev, silent ? {
19
- info: ()=>{},
20
- error: ()=>{}
21
- } : _log, forceReload);
22
- }
23
- async hasPage(pathname) {
24
- var _this_nextConfig_i18n;
25
- return !!(0, _require.getMaybePagePath)(pathname, this.distDir, (_this_nextConfig_i18n = this.nextConfig.i18n) == null ? void 0 : _this_nextConfig_i18n.locales, this.enabledDirectories.app);
26
- }
27
- getBuildId() {
28
- const buildIdFile = (0, _path.join)(this.distDir, _constants.BUILD_ID_FILE);
29
- try {
30
- return _fs.default.readFileSync(buildIdFile, "utf8").trim();
31
- } catch (err) {
32
- if (err.code === "ENOENT") {
33
- throw new Error(\`Could not find a production build in the '\${this.distDir}' directory. Try building your app with 'next build' before starting the production server. https://nextjs.org/docs/messages/production-start-no-build-id\`);
34
- }
35
- throw err;
36
- }
37
- }
38
- getEnabledDirectories(dev) {
39
- const dir = dev ? this.dir : this.serverDistDir;
40
- return {
41
- app: (0, _findpagesdir.findDir)(dir, "app") ? true : false,
42
- pages: (0, _findpagesdir.findDir)(dir, "pages") ? true : false
43
- };
44
- }
45
- // ...
46
- }`;
47
- expect(patchCode(code, rule)).toMatchInlineSnapshot(`
48
- "class NextNodeServer extends _baseserver.default {
49
- constructor(options){
50
- // Initialize super class
51
- super(options);
52
- this.handleNextImageRequest = async (req, res, parsedUrl) => { /* ... */ };
53
- }
54
- async handleUpgrade() {
55
- // The web server does not support web sockets, it's only used for HMR in
56
- // development.
57
- }
58
- loadEnvConfig({ dev, forceReload, silent }) {
59
- (0, _env.loadEnvConfig)(this.dir, dev, silent ? {
60
- info: ()=>{},
61
- error: ()=>{}
62
- } : _log, forceReload);
63
- }
64
- async hasPage(pathname) {
65
- var _this_nextConfig_i18n;
66
- return !!(0, _require.getMaybePagePath)(pathname, this.distDir, (_this_nextConfig_i18n = this.nextConfig.i18n) == null ? void 0 : _this_nextConfig_i18n.locales, this.enabledDirectories.app);
67
- }
68
- getBuildId() {
69
- return process.env.NEXT_BUILD_ID;
70
- }
71
- getEnabledDirectories(dev) {
72
- const dir = dev ? this.dir : this.serverDistDir;
73
- return {
74
- app: (0, _findpagesdir.findDir)(dir, "app") ? true : false,
75
- pages: (0, _findpagesdir.findDir)(dir, "pages") ? true : false
76
- };
77
- }
78
- // ...
79
- }"
80
- `);
81
- });
82
- });
@@ -1,7 +0,0 @@
1
- /**
2
- * Inline `evalManifest` as it relies on `readFileSync` and `runInNewContext`
3
- * that are not supported by workerd.
4
- */
5
- import { type BuildOptions } from "@opennextjs/aws/build/helper.js";
6
- import type { ContentUpdater, Plugin } from "@opennextjs/aws/plugins/content-updater.js";
7
- export declare function inlineEvalManifest(updater: ContentUpdater, buildOpts: BuildOptions): Plugin;
@@ -1,61 +0,0 @@
1
- /**
2
- * Inline `evalManifest` as it relies on `readFileSync` and `runInNewContext`
3
- * that are not supported by workerd.
4
- */
5
- import { join, posix, relative, sep } from "node:path";
6
- import { getPackagePath } from "@opennextjs/aws/build/helper.js";
7
- import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js";
8
- import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js";
9
- import { glob } from "glob";
10
- import { normalizePath } from "../../utils/normalize-path.js";
11
- export function inlineEvalManifest(updater, buildOpts) {
12
- return updater.updateContent("inline-eval-manifest", [
13
- {
14
- field: {
15
- filter: getCrossPlatformPathRegex(String.raw `/next/dist/server/load-manifest(\.external)?\.js$`, {
16
- escape: false,
17
- }),
18
- contentFilter: /function evalManifest\(/,
19
- callback: async ({ contents }) => patchCode(contents, await getRule(buildOpts)),
20
- },
21
- },
22
- ]);
23
- }
24
- async function getRule(buildOpts) {
25
- const { outputDir } = buildOpts;
26
- const baseDir = join(outputDir, "server-functions/default", getPackagePath(buildOpts), ".next");
27
- const appDir = join(baseDir, "server/app");
28
- const manifests = await glob(join(baseDir, "**/*_client-reference-manifest.js"), {
29
- windowsPathsNoEscape: true,
30
- });
31
- const returnManifests = manifests
32
- .map((manifest) => {
33
- const endsWith = normalizePath(relative(baseDir, manifest));
34
- const key = normalizePath("/" + relative(appDir, manifest)).replace("_client-reference-manifest.js", "");
35
- return `
36
- if ($PATH.endsWith("${endsWith}")) {
37
- require(${JSON.stringify(manifest)});
38
- return {
39
- __RSC_MANIFEST: {
40
- "${key}": globalThis.__RSC_MANIFEST["${key}"],
41
- },
42
- };
43
- }
44
- `;
45
- })
46
- .join("\n");
47
- return {
48
- rule: {
49
- pattern: `
50
- function evalManifest($PATH, $$$ARGS) {
51
- $$$_
52
- }`,
53
- },
54
- fix: `
55
- function evalManifest($PATH, $$$ARGS) {
56
- $PATH = $PATH.replaceAll(${JSON.stringify(sep)}, ${JSON.stringify(posix.sep)});
57
- ${returnManifests}
58
- throw new Error(\`Unexpected evalManifest(\${$PATH}) call!\`);
59
- }`,
60
- };
61
- }
@@ -1,6 +0,0 @@
1
- import { type BuildOptions } from "@opennextjs/aws/build/helper.js";
2
- /**
3
- * Inlines the middleware manifest from the build output to prevent a dynamic require statement
4
- * as they result in runtime failures.
5
- */
6
- export declare function inlineMiddlewareManifestRequire(code: string, buildOpts: BuildOptions): string;
@@ -1,15 +0,0 @@
1
- import { existsSync, readFileSync } from "node:fs";
2
- import { join } from "node:path";
3
- import { getPackagePath } from "@opennextjs/aws/build/helper.js";
4
- /**
5
- * Inlines the middleware manifest from the build output to prevent a dynamic require statement
6
- * as they result in runtime failures.
7
- */
8
- export function inlineMiddlewareManifestRequire(code, buildOpts) {
9
- const { outputDir } = buildOpts;
10
- const middlewareManifestPath = join(outputDir, "server-functions/default", getPackagePath(buildOpts), ".next/server/middleware-manifest.json");
11
- const middlewareManifest = existsSync(middlewareManifestPath)
12
- ? JSON.parse(readFileSync(middlewareManifestPath, "utf-8"))
13
- : {};
14
- return code.replace("require(this.middlewareManifestPath)", JSON.stringify(middlewareManifest));
15
- }