@react-router/cloudflare 7.0.0-pre.1 → 7.0.0-pre.3

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # @react-router/cloudflare
2
2
 
3
+ ## 7.0.0-pre.3
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies:
8
+ - `react-router@7.0.0-pre.3`
9
+
10
+ ## 7.0.0-pre.2
11
+
12
+ ### Patch Changes
13
+
14
+ - Updated dependencies:
15
+ - `react-router@7.0.0-pre.2`
16
+
3
17
  ## 7.0.0-pre.1
4
18
 
5
19
  ### Patch Changes
@@ -0,0 +1,52 @@
1
+ import { SessionData, SessionStorage, SessionIdStorageStrategy, AppLoadContext, ServerBuild } from 'react-router';
2
+ import { CacheStorage } from '@cloudflare/workers-types';
3
+
4
+ interface WorkersKVSessionStorageOptions {
5
+ /**
6
+ * The Cookie used to store the session id on the client, or options used
7
+ * to automatically create one.
8
+ */
9
+ cookie?: SessionIdStorageStrategy["cookie"];
10
+ /**
11
+ * The KVNamespace used to store the sessions.
12
+ */
13
+ kv: KVNamespace;
14
+ }
15
+ /**
16
+ * Creates a SessionStorage that stores session data in the Clouldflare KV Store.
17
+ *
18
+ * The advantage of using this instead of cookie session storage is that
19
+ * KV Store may contain much more data than cookies.
20
+ */
21
+ declare function createWorkersKVSessionStorage<Data = SessionData, FlashData = Data>({ cookie, kv, }: WorkersKVSessionStorageOptions): SessionStorage<Data, FlashData>;
22
+
23
+ /**
24
+ * A function that returns the value to use as `context` in route `loader` and
25
+ * `action` functions.
26
+ *
27
+ * You can think of this as an escape hatch that allows you to pass
28
+ * environment/platform-specific values through to your loader/action.
29
+ */
30
+ type GetLoadContextFunction<Env = unknown, Params extends string = any, Data extends Record<string, unknown> = Record<string, unknown>> = (args: {
31
+ request: Request;
32
+ context: {
33
+ cloudflare: EventContext<Env, Params, Data> & {
34
+ cf: EventContext<Env, Params, Data>["request"]["cf"];
35
+ ctx: {
36
+ waitUntil: EventContext<Env, Params, Data>["waitUntil"];
37
+ passThroughOnException: EventContext<Env, Params, Data>["passThroughOnException"];
38
+ };
39
+ caches: CacheStorage;
40
+ };
41
+ };
42
+ }) => AppLoadContext | Promise<AppLoadContext>;
43
+ type RequestHandler<Env = any> = PagesFunction<Env>;
44
+ interface createPagesFunctionHandlerParams<Env = any> {
45
+ build: ServerBuild | (() => ServerBuild | Promise<ServerBuild>);
46
+ getLoadContext?: GetLoadContextFunction<Env>;
47
+ mode?: string;
48
+ }
49
+ declare function createRequestHandler<Env = any>({ build, mode, getLoadContext, }: createPagesFunctionHandlerParams<Env>): RequestHandler<Env>;
50
+ declare function createPagesFunctionHandler<Env = any>({ build, getLoadContext, mode, }: createPagesFunctionHandlerParams<Env>): (context: EventContext<Env, any, any>) => Promise<Response>;
51
+
52
+ export { type GetLoadContextFunction, type RequestHandler, createPagesFunctionHandler, type createPagesFunctionHandlerParams, createRequestHandler, createWorkersKVSessionStorage };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,52 @@
1
- export { createWorkersKVSessionStorage } from "./sessions/workersKVStorage";
2
- export type { createPagesFunctionHandlerParams, GetLoadContextFunction, RequestHandler, } from "./worker";
3
- export { createPagesFunctionHandler, createRequestHandler } from "./worker";
1
+ import { SessionData, SessionStorage, SessionIdStorageStrategy, AppLoadContext, ServerBuild } from 'react-router';
2
+ import { CacheStorage } from '@cloudflare/workers-types';
3
+
4
+ interface WorkersKVSessionStorageOptions {
5
+ /**
6
+ * The Cookie used to store the session id on the client, or options used
7
+ * to automatically create one.
8
+ */
9
+ cookie?: SessionIdStorageStrategy["cookie"];
10
+ /**
11
+ * The KVNamespace used to store the sessions.
12
+ */
13
+ kv: KVNamespace;
14
+ }
15
+ /**
16
+ * Creates a SessionStorage that stores session data in the Clouldflare KV Store.
17
+ *
18
+ * The advantage of using this instead of cookie session storage is that
19
+ * KV Store may contain much more data than cookies.
20
+ */
21
+ declare function createWorkersKVSessionStorage<Data = SessionData, FlashData = Data>({ cookie, kv, }: WorkersKVSessionStorageOptions): SessionStorage<Data, FlashData>;
22
+
23
+ /**
24
+ * A function that returns the value to use as `context` in route `loader` and
25
+ * `action` functions.
26
+ *
27
+ * You can think of this as an escape hatch that allows you to pass
28
+ * environment/platform-specific values through to your loader/action.
29
+ */
30
+ type GetLoadContextFunction<Env = unknown, Params extends string = any, Data extends Record<string, unknown> = Record<string, unknown>> = (args: {
31
+ request: Request;
32
+ context: {
33
+ cloudflare: EventContext<Env, Params, Data> & {
34
+ cf: EventContext<Env, Params, Data>["request"]["cf"];
35
+ ctx: {
36
+ waitUntil: EventContext<Env, Params, Data>["waitUntil"];
37
+ passThroughOnException: EventContext<Env, Params, Data>["passThroughOnException"];
38
+ };
39
+ caches: CacheStorage;
40
+ };
41
+ };
42
+ }) => AppLoadContext | Promise<AppLoadContext>;
43
+ type RequestHandler<Env = any> = PagesFunction<Env>;
44
+ interface createPagesFunctionHandlerParams<Env = any> {
45
+ build: ServerBuild | (() => ServerBuild | Promise<ServerBuild>);
46
+ getLoadContext?: GetLoadContextFunction<Env>;
47
+ mode?: string;
48
+ }
49
+ declare function createRequestHandler<Env = any>({ build, mode, getLoadContext, }: createPagesFunctionHandlerParams<Env>): RequestHandler<Env>;
50
+ declare function createPagesFunctionHandler<Env = any>({ build, getLoadContext, mode, }: createPagesFunctionHandlerParams<Env>): (context: EventContext<Env, any, any>) => Promise<Response>;
51
+
52
+ export { type GetLoadContextFunction, type RequestHandler, createPagesFunctionHandler, type createPagesFunctionHandlerParams, createRequestHandler, createWorkersKVSessionStorage };
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @react-router/cloudflare v7.0.0-pre.1
2
+ * @react-router/cloudflare v7.0.0-pre.3
3
3
  *
4
4
  * Copyright (c) Remix Software Inc.
5
5
  *
@@ -8,15 +8,150 @@
8
8
  *
9
9
  * @license MIT
10
10
  */
11
- 'use strict';
11
+ "use strict";
12
+ var __defProp = Object.defineProperty;
13
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
14
+ var __getOwnPropNames = Object.getOwnPropertyNames;
15
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
16
+ var __export = (target, all) => {
17
+ for (var name in all)
18
+ __defProp(target, name, { get: all[name], enumerable: true });
19
+ };
20
+ var __copyProps = (to, from, except, desc) => {
21
+ if (from && typeof from === "object" || typeof from === "function") {
22
+ for (let key of __getOwnPropNames(from))
23
+ if (!__hasOwnProp.call(to, key) && key !== except)
24
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
25
+ }
26
+ return to;
27
+ };
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
12
29
 
13
- Object.defineProperty(exports, '__esModule', { value: true });
30
+ // index.ts
31
+ var react_router_cloudflare_exports = {};
32
+ __export(react_router_cloudflare_exports, {
33
+ createPagesFunctionHandler: () => createPagesFunctionHandler,
34
+ createRequestHandler: () => createRequestHandler,
35
+ createWorkersKVSessionStorage: () => createWorkersKVSessionStorage
36
+ });
37
+ module.exports = __toCommonJS(react_router_cloudflare_exports);
14
38
 
15
- var workersKVStorage = require('./sessions/workersKVStorage.js');
16
- var worker = require('./worker.js');
39
+ // sessions/workersKVStorage.ts
40
+ var import_react_router = require("react-router");
41
+ function createWorkersKVSessionStorage({
42
+ cookie,
43
+ kv
44
+ }) {
45
+ return (0, import_react_router.createSessionStorage)({
46
+ cookie,
47
+ async createData(data, expires) {
48
+ while (true) {
49
+ let randomBytes = crypto.getRandomValues(new Uint8Array(8));
50
+ let id = [...randomBytes].map((x) => x.toString(16).padStart(2, "0")).join("");
51
+ if (await kv.get(id, "json")) {
52
+ continue;
53
+ }
54
+ await kv.put(id, JSON.stringify(data), {
55
+ expiration: expires ? Math.round(expires.getTime() / 1e3) : void 0
56
+ });
57
+ return id;
58
+ }
59
+ },
60
+ async readData(id) {
61
+ let session = await kv.get(id);
62
+ if (!session) {
63
+ return null;
64
+ }
65
+ return JSON.parse(session);
66
+ },
67
+ async updateData(id, data, expires) {
68
+ await kv.put(id, JSON.stringify(data), {
69
+ expiration: expires ? Math.round(expires.getTime() / 1e3) : void 0
70
+ });
71
+ },
72
+ async deleteData(id) {
73
+ await kv.delete(id);
74
+ }
75
+ });
76
+ }
17
77
 
18
-
19
-
20
- exports.createWorkersKVSessionStorage = workersKVStorage.createWorkersKVSessionStorage;
21
- exports.createPagesFunctionHandler = worker.createPagesFunctionHandler;
22
- exports.createRequestHandler = worker.createRequestHandler;
78
+ // worker.ts
79
+ var import_react_router2 = require("react-router");
80
+ function createRequestHandler({
81
+ build,
82
+ mode,
83
+ getLoadContext = ({ context }) => ({
84
+ ...context,
85
+ cloudflare: {
86
+ ...context.cloudflare,
87
+ cf: context.cloudflare.request.cf
88
+ }
89
+ })
90
+ }) {
91
+ let handleRequest = (0, import_react_router2.createRequestHandler)(build, mode);
92
+ return async (cloudflare) => {
93
+ let loadContext = await getLoadContext({
94
+ request: cloudflare.request,
95
+ context: {
96
+ cloudflare: {
97
+ ...cloudflare,
98
+ cf: cloudflare.request.cf,
99
+ ctx: {
100
+ waitUntil: cloudflare.waitUntil.bind(cloudflare),
101
+ passThroughOnException: cloudflare.passThroughOnException.bind(cloudflare)
102
+ },
103
+ caches
104
+ }
105
+ }
106
+ });
107
+ return handleRequest(cloudflare.request, loadContext);
108
+ };
109
+ }
110
+ function createPagesFunctionHandler({
111
+ build,
112
+ getLoadContext,
113
+ mode
114
+ }) {
115
+ let handleRequest = createRequestHandler({
116
+ build,
117
+ getLoadContext,
118
+ mode
119
+ });
120
+ let handleFetch = async (context) => {
121
+ let response;
122
+ context.request.headers.delete("if-none-match");
123
+ try {
124
+ response = await context.env.ASSETS.fetch(
125
+ context.request.url,
126
+ context.request.clone()
127
+ );
128
+ response = response && response.status >= 200 && response.status < 400 ? new Response(response.body, response) : void 0;
129
+ } catch {
130
+ }
131
+ if (!response) {
132
+ response = await handleRequest(context);
133
+ }
134
+ return response;
135
+ };
136
+ return async (context) => {
137
+ try {
138
+ return await handleFetch(context);
139
+ } catch (error) {
140
+ if (process.env.NODE_ENV === "development" && error instanceof Error) {
141
+ console.error(error);
142
+ return new Response(error.message || error.toString(), {
143
+ status: 500
144
+ });
145
+ }
146
+ return new Response("Internal Error", {
147
+ status: 500
148
+ });
149
+ }
150
+ };
151
+ }
152
+ // Annotate the CommonJS export names for ESM import in node:
153
+ 0 && (module.exports = {
154
+ createPagesFunctionHandler,
155
+ createRequestHandler,
156
+ createWorkersKVSessionStorage
157
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,129 @@
1
+ /**
2
+ * @react-router/cloudflare v7.0.0-pre.3
3
+ *
4
+ * Copyright (c) Remix Software Inc.
5
+ *
6
+ * This source code is licensed under the MIT license found in the
7
+ * LICENSE.md file in the root directory of this source tree.
8
+ *
9
+ * @license MIT
10
+ */
11
+
12
+ // sessions/workersKVStorage.ts
13
+ import { createSessionStorage } from "react-router";
14
+ function createWorkersKVSessionStorage({
15
+ cookie,
16
+ kv
17
+ }) {
18
+ return createSessionStorage({
19
+ cookie,
20
+ async createData(data, expires) {
21
+ while (true) {
22
+ let randomBytes = crypto.getRandomValues(new Uint8Array(8));
23
+ let id = [...randomBytes].map((x) => x.toString(16).padStart(2, "0")).join("");
24
+ if (await kv.get(id, "json")) {
25
+ continue;
26
+ }
27
+ await kv.put(id, JSON.stringify(data), {
28
+ expiration: expires ? Math.round(expires.getTime() / 1e3) : void 0
29
+ });
30
+ return id;
31
+ }
32
+ },
33
+ async readData(id) {
34
+ let session = await kv.get(id);
35
+ if (!session) {
36
+ return null;
37
+ }
38
+ return JSON.parse(session);
39
+ },
40
+ async updateData(id, data, expires) {
41
+ await kv.put(id, JSON.stringify(data), {
42
+ expiration: expires ? Math.round(expires.getTime() / 1e3) : void 0
43
+ });
44
+ },
45
+ async deleteData(id) {
46
+ await kv.delete(id);
47
+ }
48
+ });
49
+ }
50
+
51
+ // worker.ts
52
+ import { createRequestHandler as createReactRouterRequestHandler } from "react-router";
53
+ function createRequestHandler({
54
+ build,
55
+ mode,
56
+ getLoadContext = ({ context }) => ({
57
+ ...context,
58
+ cloudflare: {
59
+ ...context.cloudflare,
60
+ cf: context.cloudflare.request.cf
61
+ }
62
+ })
63
+ }) {
64
+ let handleRequest = createReactRouterRequestHandler(build, mode);
65
+ return async (cloudflare) => {
66
+ let loadContext = await getLoadContext({
67
+ request: cloudflare.request,
68
+ context: {
69
+ cloudflare: {
70
+ ...cloudflare,
71
+ cf: cloudflare.request.cf,
72
+ ctx: {
73
+ waitUntil: cloudflare.waitUntil.bind(cloudflare),
74
+ passThroughOnException: cloudflare.passThroughOnException.bind(cloudflare)
75
+ },
76
+ caches
77
+ }
78
+ }
79
+ });
80
+ return handleRequest(cloudflare.request, loadContext);
81
+ };
82
+ }
83
+ function createPagesFunctionHandler({
84
+ build,
85
+ getLoadContext,
86
+ mode
87
+ }) {
88
+ let handleRequest = createRequestHandler({
89
+ build,
90
+ getLoadContext,
91
+ mode
92
+ });
93
+ let handleFetch = async (context) => {
94
+ let response;
95
+ context.request.headers.delete("if-none-match");
96
+ try {
97
+ response = await context.env.ASSETS.fetch(
98
+ context.request.url,
99
+ context.request.clone()
100
+ );
101
+ response = response && response.status >= 200 && response.status < 400 ? new Response(response.body, response) : void 0;
102
+ } catch {
103
+ }
104
+ if (!response) {
105
+ response = await handleRequest(context);
106
+ }
107
+ return response;
108
+ };
109
+ return async (context) => {
110
+ try {
111
+ return await handleFetch(context);
112
+ } catch (error) {
113
+ if (process.env.NODE_ENV === "development" && error instanceof Error) {
114
+ console.error(error);
115
+ return new Response(error.message || error.toString(), {
116
+ status: 500
117
+ });
118
+ }
119
+ return new Response("Internal Error", {
120
+ status: 500
121
+ });
122
+ }
123
+ };
124
+ }
125
+ export {
126
+ createPagesFunctionHandler,
127
+ createRequestHandler,
128
+ createWorkersKVSessionStorage
129
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-router/cloudflare",
3
- "version": "7.0.0-pre.1",
3
+ "version": "7.0.0-pre.3",
4
4
  "description": "Cloudflare platform abstractions for React Router",
5
5
  "bugs": {
6
6
  "url": "https://github.com/remix-run/react-router/issues"
@@ -15,20 +15,43 @@
15
15
  "typings": "dist/index.d.ts",
16
16
  "exports": {
17
17
  ".": {
18
- "types": "./dist/index.d.ts",
19
- "default": "./dist/index.js"
18
+ "import": {
19
+ "types": "./dist/index.d.mts",
20
+ "default": "./dist/index.mjs"
21
+ },
22
+ "default": {
23
+ "types": "./dist/index.d.ts",
24
+ "default": "./dist/index.js"
25
+ }
20
26
  },
21
27
  "./package.json": "./package.json"
22
28
  },
29
+ "wireit": {
30
+ "build": {
31
+ "command": "tsup",
32
+ "files": [
33
+ "sessions/**",
34
+ "*.ts",
35
+ "tsconfig.json",
36
+ "package.json"
37
+ ],
38
+ "output": [
39
+ "dist/**"
40
+ ]
41
+ }
42
+ },
23
43
  "devDependencies": {
24
44
  "@cloudflare/workers-types": "^4.20230518.0",
25
- "typescript": "^5.1.0",
26
- "react-router": "7.0.0-pre.1"
45
+ "tsup": "^8.3.0",
46
+ "typescript": "^5.1.6",
47
+ "wireit": "0.14.9",
48
+ "react-router": "7.0.0-pre.3"
27
49
  },
28
50
  "peerDependencies": {
29
51
  "@cloudflare/workers-types": "^4.0.0",
52
+ "tsup": "^8.3.0",
30
53
  "typescript": "^5.1.0",
31
- "react-router": "^7.0.0-pre.1"
54
+ "react-router": "^7.0.0-pre.3"
32
55
  },
33
56
  "peerDependenciesMeta": {
34
57
  "typescript": {
@@ -36,7 +59,7 @@
36
59
  }
37
60
  },
38
61
  "engines": {
39
- "node": ">=18.0.0"
62
+ "node": ">=20.0.0"
40
63
  },
41
64
  "files": [
42
65
  "dist/",
@@ -45,7 +68,6 @@
45
68
  "README.md"
46
69
  ],
47
70
  "scripts": {
48
- "build": "rollup -c",
49
- "tsc": "tsc"
71
+ "build": "wireit"
50
72
  }
51
73
  }
@@ -1,21 +0,0 @@
1
- /// <reference types="@cloudflare/workers-types" />
2
- import type { SessionStorage, SessionIdStorageStrategy, SessionData } from "react-router";
3
- interface WorkersKVSessionStorageOptions {
4
- /**
5
- * The Cookie used to store the session id on the client, or options used
6
- * to automatically create one.
7
- */
8
- cookie?: SessionIdStorageStrategy["cookie"];
9
- /**
10
- * The KVNamespace used to store the sessions.
11
- */
12
- kv: KVNamespace;
13
- }
14
- /**
15
- * Creates a SessionStorage that stores session data in the Clouldflare KV Store.
16
- *
17
- * The advantage of using this instead of cookie session storage is that
18
- * KV Store may contain much more data than cookies.
19
- */
20
- export declare function createWorkersKVSessionStorage<Data = SessionData, FlashData = Data>({ cookie, kv, }: WorkersKVSessionStorageOptions): SessionStorage<Data, FlashData>;
21
- export {};
@@ -1,64 +0,0 @@
1
- /**
2
- * @react-router/cloudflare v7.0.0-pre.1
3
- *
4
- * Copyright (c) Remix Software Inc.
5
- *
6
- * This source code is licensed under the MIT license found in the
7
- * LICENSE.md file in the root directory of this source tree.
8
- *
9
- * @license MIT
10
- */
11
- 'use strict';
12
-
13
- Object.defineProperty(exports, '__esModule', { value: true });
14
-
15
- var reactRouter = require('react-router');
16
-
17
- /**
18
- * Creates a SessionStorage that stores session data in the Clouldflare KV Store.
19
- *
20
- * The advantage of using this instead of cookie session storage is that
21
- * KV Store may contain much more data than cookies.
22
- */
23
- function createWorkersKVSessionStorage({
24
- cookie,
25
- kv
26
- }) {
27
- return reactRouter.createSessionStorage({
28
- cookie,
29
- async createData(data, expires) {
30
- while (true) {
31
- let randomBytes = crypto.getRandomValues(new Uint8Array(8));
32
- // This storage manages an id space of 2^64 ids, which is far greater
33
- // than the maximum number of files allowed on an NTFS or ext4 volume
34
- // (2^32). However, the larger id space should help to avoid collisions
35
- // with existing ids when creating new sessions, which speeds things up.
36
- let id = [...randomBytes].map(x => x.toString(16).padStart(2, "0")).join("");
37
- if (await kv.get(id, "json")) {
38
- continue;
39
- }
40
- await kv.put(id, JSON.stringify(data), {
41
- expiration: expires ? Math.round(expires.getTime() / 1000) : undefined
42
- });
43
- return id;
44
- }
45
- },
46
- async readData(id) {
47
- let session = await kv.get(id);
48
- if (!session) {
49
- return null;
50
- }
51
- return JSON.parse(session);
52
- },
53
- async updateData(id, data, expires) {
54
- await kv.put(id, JSON.stringify(data), {
55
- expiration: expires ? Math.round(expires.getTime() / 1000) : undefined
56
- });
57
- },
58
- async deleteData(id) {
59
- await kv.delete(id);
60
- }
61
- });
62
- }
63
-
64
- exports.createWorkersKVSessionStorage = createWorkersKVSessionStorage;
package/dist/worker.d.ts DELETED
@@ -1,30 +0,0 @@
1
- import type { AppLoadContext, ServerBuild } from "react-router";
2
- import { type CacheStorage } from "@cloudflare/workers-types";
3
- /**
4
- * A function that returns the value to use as `context` in route `loader` and
5
- * `action` functions.
6
- *
7
- * You can think of this as an escape hatch that allows you to pass
8
- * environment/platform-specific values through to your loader/action.
9
- */
10
- export type GetLoadContextFunction<Env = unknown, Params extends string = any, Data extends Record<string, unknown> = Record<string, unknown>> = (args: {
11
- request: Request;
12
- context: {
13
- cloudflare: EventContext<Env, Params, Data> & {
14
- cf: EventContext<Env, Params, Data>["request"]["cf"];
15
- ctx: {
16
- waitUntil: EventContext<Env, Params, Data>["waitUntil"];
17
- passThroughOnException: EventContext<Env, Params, Data>["passThroughOnException"];
18
- };
19
- caches: CacheStorage;
20
- };
21
- };
22
- }) => AppLoadContext | Promise<AppLoadContext>;
23
- export type RequestHandler<Env = any> = PagesFunction<Env>;
24
- export interface createPagesFunctionHandlerParams<Env = any> {
25
- build: ServerBuild | (() => ServerBuild | Promise<ServerBuild>);
26
- getLoadContext?: GetLoadContextFunction<Env>;
27
- mode?: string;
28
- }
29
- export declare function createRequestHandler<Env = any>({ build, mode, getLoadContext, }: createPagesFunctionHandlerParams<Env>): RequestHandler<Env>;
30
- export declare function createPagesFunctionHandler<Env = any>({ build, getLoadContext, mode, }: createPagesFunctionHandlerParams<Env>): (context: EventContext<Env, any, any>) => Promise<Response>;
package/dist/worker.js DELETED
@@ -1,90 +0,0 @@
1
- /**
2
- * @react-router/cloudflare v7.0.0-pre.1
3
- *
4
- * Copyright (c) Remix Software Inc.
5
- *
6
- * This source code is licensed under the MIT license found in the
7
- * LICENSE.md file in the root directory of this source tree.
8
- *
9
- * @license MIT
10
- */
11
- 'use strict';
12
-
13
- Object.defineProperty(exports, '__esModule', { value: true });
14
-
15
- var reactRouter = require('react-router');
16
-
17
- function createRequestHandler({
18
- build,
19
- mode,
20
- getLoadContext = ({
21
- context
22
- }) => ({
23
- ...context,
24
- cloudflare: {
25
- ...context.cloudflare,
26
- cf: context.cloudflare.request.cf
27
- }
28
- })
29
- }) {
30
- let handleRequest = reactRouter.createRequestHandler(build, mode);
31
- return async cloudflare => {
32
- let loadContext = await getLoadContext({
33
- request: cloudflare.request,
34
- context: {
35
- cloudflare: {
36
- ...cloudflare,
37
- cf: cloudflare.request.cf,
38
- ctx: {
39
- waitUntil: cloudflare.waitUntil.bind(cloudflare),
40
- passThroughOnException: cloudflare.passThroughOnException.bind(cloudflare)
41
- },
42
- caches
43
- }
44
- }
45
- });
46
- return handleRequest(cloudflare.request, loadContext);
47
- };
48
- }
49
- function createPagesFunctionHandler({
50
- build,
51
- getLoadContext,
52
- mode
53
- }) {
54
- let handleRequest = createRequestHandler({
55
- build,
56
- getLoadContext,
57
- mode
58
- });
59
- let handleFetch = async context => {
60
- let response;
61
- // https://github.com/cloudflare/wrangler2/issues/117
62
- context.request.headers.delete("if-none-match");
63
- try {
64
- response = await context.env.ASSETS.fetch(context.request.url, context.request.clone());
65
- response = response && response.status >= 200 && response.status < 400 ? new Response(response.body, response) : undefined;
66
- } catch {}
67
- if (!response) {
68
- response = await handleRequest(context);
69
- }
70
- return response;
71
- };
72
- return async context => {
73
- try {
74
- return await handleFetch(context);
75
- } catch (error) {
76
- if (process.env.NODE_ENV === "development" && error instanceof Error) {
77
- console.error(error);
78
- return new Response(error.message || error.toString(), {
79
- status: 500
80
- });
81
- }
82
- return new Response("Internal Error", {
83
- status: 500
84
- });
85
- }
86
- };
87
- }
88
-
89
- exports.createPagesFunctionHandler = createPagesFunctionHandler;
90
- exports.createRequestHandler = createRequestHandler;