@react-router/cloudflare 7.15.1 → 8.0.0-pre.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.
- package/CHANGELOG.md +7 -0
- package/dist/index.d.ts +45 -31
- package/dist/index.js +81 -145
- package/package.json +12 -17
- package/dist/index.d.mts +0 -53
- package/dist/index.mjs +0 -129
package/CHANGELOG.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
|
-
import { SessionData, SessionIdStorageStrategy, SessionStorage, ServerBuild, UNSAFE_MiddlewareEnabled, RouterContextProvider, AppLoadContext } from 'react-router';
|
|
2
|
-
import { CacheStorage } from '@cloudflare/workers-types';
|
|
3
1
|
|
|
2
|
+
import { RouterContextProvider, ServerBuild, SessionData, SessionIdStorageStrategy, SessionStorage } from "react-router";
|
|
3
|
+
import { CacheStorage } from "@cloudflare/workers-types";
|
|
4
|
+
|
|
5
|
+
//#region sessions/workersKVStorage.d.ts
|
|
4
6
|
interface WorkersKVSessionStorageOptions {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
7
|
+
/**
|
|
8
|
+
* The Cookie used to store the session id on the client, or options used
|
|
9
|
+
* to automatically create one.
|
|
10
|
+
*/
|
|
11
|
+
cookie?: SessionIdStorageStrategy["cookie"];
|
|
12
|
+
/**
|
|
13
|
+
* The KVNamespace used to store the sessions.
|
|
14
|
+
*/
|
|
15
|
+
kv: KVNamespace;
|
|
14
16
|
}
|
|
15
17
|
/**
|
|
16
18
|
* Creates a SessionStorage that stores session data in the Clouldflare KV Store.
|
|
@@ -18,8 +20,12 @@ interface WorkersKVSessionStorageOptions {
|
|
|
18
20
|
* The advantage of using this instead of cookie session storage is that
|
|
19
21
|
* KV Store may contain much more data than cookies.
|
|
20
22
|
*/
|
|
21
|
-
declare function createWorkersKVSessionStorage<Data = SessionData, FlashData = Data>({
|
|
22
|
-
|
|
23
|
+
declare function createWorkersKVSessionStorage<Data = SessionData, FlashData = Data>({
|
|
24
|
+
cookie,
|
|
25
|
+
kv
|
|
26
|
+
}: WorkersKVSessionStorageOptions): SessionStorage<Data, FlashData>;
|
|
27
|
+
//#endregion
|
|
28
|
+
//#region worker.d.ts
|
|
23
29
|
type MaybePromise<T> = T | Promise<T>;
|
|
24
30
|
/**
|
|
25
31
|
* A function that returns the value to use as `context` in route `loader` and
|
|
@@ -29,25 +35,33 @@ type MaybePromise<T> = T | Promise<T>;
|
|
|
29
35
|
* environment/platform-specific values through to your loader/action.
|
|
30
36
|
*/
|
|
31
37
|
type GetLoadContextFunction<Env = unknown, Params extends string = any, Data extends Record<string, unknown> = Record<string, unknown>> = (args: {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
};
|
|
38
|
+
request: Request;
|
|
39
|
+
context: {
|
|
40
|
+
cloudflare: EventContext<Env, Params, Data> & {
|
|
41
|
+
cf: EventContext<Env, Params, Data>["request"]["cf"];
|
|
42
|
+
ctx: {
|
|
43
|
+
waitUntil: EventContext<Env, Params, Data>["waitUntil"];
|
|
44
|
+
passThroughOnException: EventContext<Env, Params, Data>["passThroughOnException"];
|
|
45
|
+
};
|
|
46
|
+
caches: CacheStorage;
|
|
42
47
|
};
|
|
43
|
-
}
|
|
48
|
+
};
|
|
49
|
+
}) => MaybePromise<RouterContextProvider>;
|
|
44
50
|
type RequestHandler<Env = any> = PagesFunction<Env>;
|
|
45
51
|
interface createPagesFunctionHandlerParams<Env = any> {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
52
|
+
build: ServerBuild | (() => ServerBuild | Promise<ServerBuild>);
|
|
53
|
+
getLoadContext: GetLoadContextFunction<Env>;
|
|
54
|
+
mode?: string;
|
|
49
55
|
}
|
|
50
|
-
declare function createRequestHandler<Env = any>({
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
56
|
+
declare function createRequestHandler<Env = any>({
|
|
57
|
+
build,
|
|
58
|
+
mode,
|
|
59
|
+
getLoadContext
|
|
60
|
+
}: createPagesFunctionHandlerParams<Env>): RequestHandler<Env>;
|
|
61
|
+
declare function createPagesFunctionHandler<Env = any>({
|
|
62
|
+
build,
|
|
63
|
+
getLoadContext,
|
|
64
|
+
mode
|
|
65
|
+
}: createPagesFunctionHandlerParams<Env>): (context: EventContext<Env, any, any>) => Promise<Response>;
|
|
66
|
+
//#endregion
|
|
67
|
+
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
|
|
2
|
+
* @react-router/cloudflare v8.0.0-pre.0
|
|
3
3
|
*
|
|
4
4
|
* Copyright (c) Remix Software Inc.
|
|
5
5
|
*
|
|
@@ -8,150 +8,86 @@
|
|
|
8
8
|
*
|
|
9
9
|
* @license MIT
|
|
10
10
|
*/
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
});
|
|
11
|
+
import { createRequestHandler as createRequestHandler$1, createSessionStorage } from "react-router";
|
|
12
|
+
import "@cloudflare/workers-types";
|
|
13
|
+
//#region sessions/workersKVStorage.ts
|
|
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
|
+
function createWorkersKVSessionStorage({ cookie, kv }) {
|
|
21
|
+
return createSessionStorage({
|
|
22
|
+
cookie,
|
|
23
|
+
async createData(data, expires) {
|
|
24
|
+
while (true) {
|
|
25
|
+
let id = [...crypto.getRandomValues(new Uint8Array(8))].map((x) => x.toString(16).padStart(2, "0")).join("");
|
|
26
|
+
if (await kv.get(id, "json")) continue;
|
|
27
|
+
await kv.put(id, JSON.stringify(data), { expiration: expires ? Math.round(expires.getTime() / 1e3) : void 0 });
|
|
28
|
+
return id;
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
async readData(id) {
|
|
32
|
+
let session = await kv.get(id);
|
|
33
|
+
if (!session) return null;
|
|
34
|
+
return JSON.parse(session);
|
|
35
|
+
},
|
|
36
|
+
async updateData(id, data, expires) {
|
|
37
|
+
await kv.put(id, JSON.stringify(data), { expiration: expires ? Math.round(expires.getTime() / 1e3) : void 0 });
|
|
38
|
+
},
|
|
39
|
+
async deleteData(id) {
|
|
40
|
+
await kv.delete(id);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
76
43
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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
|
-
};
|
|
44
|
+
//#endregion
|
|
45
|
+
//#region worker.ts
|
|
46
|
+
function createRequestHandler({ build, mode, getLoadContext }) {
|
|
47
|
+
let handleRequest = createRequestHandler$1(build, mode);
|
|
48
|
+
return async (cloudflare) => {
|
|
49
|
+
let loadContext = await getLoadContext({
|
|
50
|
+
request: cloudflare.request,
|
|
51
|
+
context: { cloudflare: {
|
|
52
|
+
...cloudflare,
|
|
53
|
+
cf: cloudflare.request.cf,
|
|
54
|
+
ctx: {
|
|
55
|
+
waitUntil: cloudflare.waitUntil.bind(cloudflare),
|
|
56
|
+
passThroughOnException: cloudflare.passThroughOnException.bind(cloudflare)
|
|
57
|
+
},
|
|
58
|
+
caches
|
|
59
|
+
} }
|
|
60
|
+
});
|
|
61
|
+
return handleRequest(cloudflare.request, loadContext);
|
|
62
|
+
};
|
|
109
63
|
}
|
|
110
|
-
function createPagesFunctionHandler({
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
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
|
-
};
|
|
64
|
+
function createPagesFunctionHandler({ build, getLoadContext, mode }) {
|
|
65
|
+
let handleRequest = createRequestHandler({
|
|
66
|
+
build,
|
|
67
|
+
getLoadContext,
|
|
68
|
+
mode
|
|
69
|
+
});
|
|
70
|
+
let handleFetch = async (context) => {
|
|
71
|
+
let response;
|
|
72
|
+
context.request.headers.delete("if-none-match");
|
|
73
|
+
try {
|
|
74
|
+
response = await context.env.ASSETS.fetch(context.request.url, context.request.clone());
|
|
75
|
+
response = response && response.status >= 200 && response.status < 400 ? new Response(response.body, response) : void 0;
|
|
76
|
+
} catch {}
|
|
77
|
+
if (!response) response = await handleRequest(context);
|
|
78
|
+
return response;
|
|
79
|
+
};
|
|
80
|
+
return async (context) => {
|
|
81
|
+
try {
|
|
82
|
+
return await handleFetch(context);
|
|
83
|
+
} catch (error) {
|
|
84
|
+
if (process.env.NODE_ENV === "development" && error instanceof Error) {
|
|
85
|
+
console.error(error);
|
|
86
|
+
return new Response(error.message || error.toString(), { status: 500 });
|
|
87
|
+
}
|
|
88
|
+
return new Response("Internal Error", { status: 500 });
|
|
89
|
+
}
|
|
90
|
+
};
|
|
151
91
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
createPagesFunctionHandler,
|
|
155
|
-
createRequestHandler,
|
|
156
|
-
createWorkersKVSessionStorage
|
|
157
|
-
});
|
|
92
|
+
//#endregion
|
|
93
|
+
export { createPagesFunctionHandler, createRequestHandler, createWorkersKVSessionStorage };
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-router/cloudflare",
|
|
3
|
-
"
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "8.0.0-pre.0",
|
|
4
5
|
"description": "Cloudflare platform abstractions for React Router",
|
|
5
6
|
"bugs": {
|
|
6
7
|
"url": "https://github.com/remix-run/react-router/issues"
|
|
@@ -15,20 +16,14 @@
|
|
|
15
16
|
"typings": "dist/index.d.ts",
|
|
16
17
|
"exports": {
|
|
17
18
|
".": {
|
|
18
|
-
"
|
|
19
|
-
|
|
20
|
-
"default": "./dist/index.mjs"
|
|
21
|
-
},
|
|
22
|
-
"default": {
|
|
23
|
-
"types": "./dist/index.d.ts",
|
|
24
|
-
"default": "./dist/index.js"
|
|
25
|
-
}
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"default": "./dist/index.js"
|
|
26
21
|
},
|
|
27
22
|
"./package.json": "./package.json"
|
|
28
23
|
},
|
|
29
24
|
"wireit": {
|
|
30
25
|
"build": {
|
|
31
|
-
"command": "
|
|
26
|
+
"command": "tsdown",
|
|
32
27
|
"files": [
|
|
33
28
|
"../../pnpm-workspace.yaml",
|
|
34
29
|
"sessions/**",
|
|
@@ -42,16 +37,16 @@
|
|
|
42
37
|
}
|
|
43
38
|
},
|
|
44
39
|
"devDependencies": {
|
|
45
|
-
"@cloudflare/workers-types": "^4.
|
|
46
|
-
"
|
|
47
|
-
"typescript": "^
|
|
48
|
-
"wireit": "0.14.
|
|
49
|
-
"react-router": "
|
|
40
|
+
"@cloudflare/workers-types": "^4.20260527.1",
|
|
41
|
+
"tsdown": "^0.22.0",
|
|
42
|
+
"typescript": "^6.0.3",
|
|
43
|
+
"wireit": "0.14.12",
|
|
44
|
+
"react-router": "8.0.0-pre.0"
|
|
50
45
|
},
|
|
51
46
|
"peerDependencies": {
|
|
52
47
|
"@cloudflare/workers-types": "^4.0.0",
|
|
53
48
|
"typescript": "^5.1.0 || ^6.0.0",
|
|
54
|
-
"react-router": "^
|
|
49
|
+
"react-router": "^8.0.0-pre.0"
|
|
55
50
|
},
|
|
56
51
|
"peerDependenciesMeta": {
|
|
57
52
|
"typescript": {
|
|
@@ -59,7 +54,7 @@
|
|
|
59
54
|
}
|
|
60
55
|
},
|
|
61
56
|
"engines": {
|
|
62
|
-
"node": ">=
|
|
57
|
+
"node": ">=22.12.0"
|
|
63
58
|
},
|
|
64
59
|
"files": [
|
|
65
60
|
"dist/",
|
package/dist/index.d.mts
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { SessionData, SessionIdStorageStrategy, SessionStorage, ServerBuild, UNSAFE_MiddlewareEnabled, RouterContextProvider, AppLoadContext } 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
|
-
type MaybePromise<T> = T | Promise<T>;
|
|
24
|
-
/**
|
|
25
|
-
* A function that returns the value to use as `context` in route `loader` and
|
|
26
|
-
* `action` functions.
|
|
27
|
-
*
|
|
28
|
-
* You can think of this as an escape hatch that allows you to pass
|
|
29
|
-
* environment/platform-specific values through to your loader/action.
|
|
30
|
-
*/
|
|
31
|
-
type GetLoadContextFunction<Env = unknown, Params extends string = any, Data extends Record<string, unknown> = Record<string, unknown>> = (args: {
|
|
32
|
-
request: Request;
|
|
33
|
-
context: {
|
|
34
|
-
cloudflare: EventContext<Env, Params, Data> & {
|
|
35
|
-
cf: EventContext<Env, Params, Data>["request"]["cf"];
|
|
36
|
-
ctx: {
|
|
37
|
-
waitUntil: EventContext<Env, Params, Data>["waitUntil"];
|
|
38
|
-
passThroughOnException: EventContext<Env, Params, Data>["passThroughOnException"];
|
|
39
|
-
};
|
|
40
|
-
caches: CacheStorage;
|
|
41
|
-
};
|
|
42
|
-
};
|
|
43
|
-
}) => UNSAFE_MiddlewareEnabled extends true ? MaybePromise<RouterContextProvider> : MaybePromise<AppLoadContext>;
|
|
44
|
-
type RequestHandler<Env = any> = PagesFunction<Env>;
|
|
45
|
-
interface createPagesFunctionHandlerParams<Env = any> {
|
|
46
|
-
build: ServerBuild | (() => ServerBuild | Promise<ServerBuild>);
|
|
47
|
-
getLoadContext?: GetLoadContextFunction<Env>;
|
|
48
|
-
mode?: string;
|
|
49
|
-
}
|
|
50
|
-
declare function createRequestHandler<Env = any>({ build, mode, getLoadContext, }: createPagesFunctionHandlerParams<Env>): RequestHandler<Env>;
|
|
51
|
-
declare function createPagesFunctionHandler<Env = any>({ build, getLoadContext, mode, }: createPagesFunctionHandlerParams<Env>): (context: EventContext<Env, any, any>) => Promise<Response>;
|
|
52
|
-
|
|
53
|
-
export { type GetLoadContextFunction, type RequestHandler, createPagesFunctionHandler, type createPagesFunctionHandlerParams, createRequestHandler, createWorkersKVSessionStorage };
|
package/dist/index.mjs
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @react-router/cloudflare v7.15.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
|
-
|
|
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
|
-
};
|