@react-router/cloudflare 0.0.0-nightly-a5f191b5e-20240820

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/LICENSE.md ADDED
@@ -0,0 +1,23 @@
1
+ MIT License
2
+
3
+ Copyright (c) React Training LLC 2015-2019
4
+ Copyright (c) Remix Software Inc. 2020-2021
5
+ Copyright (c) Shopify Inc. 2022-2023
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in all
15
+ copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # React Router Cloudflare
2
+
3
+ Cloudflare platform abstractions for [React Router.](https://reactrouter.com)
4
+
5
+ ```bash
6
+ npm install @react-router/cloudflare @cloudflare/workers-types
7
+ ```
@@ -0,0 +1,3 @@
1
+ export { createWorkersKVSessionStorage } from "./sessions/workersKVStorage";
2
+ export type { createPagesFunctionHandlerParams, GetLoadContextFunction, RequestHandler, } from "./worker";
3
+ export { createPagesFunctionHandler, createRequestHandler } from "./worker";
package/dist/index.js ADDED
@@ -0,0 +1,22 @@
1
+ /**
2
+ * @react-router/cloudflare v0.0.0-nightly-a5f191b5e-20240820
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 workersKVStorage = require('./sessions/workersKVStorage.js');
16
+ var worker = require('./worker.js');
17
+
18
+
19
+
20
+ exports.createWorkersKVSessionStorage = workersKVStorage.createWorkersKVSessionStorage;
21
+ exports.createPagesFunctionHandler = worker.createPagesFunctionHandler;
22
+ exports.createRequestHandler = worker.createRequestHandler;
@@ -0,0 +1,21 @@
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 {};
@@ -0,0 +1,64 @@
1
+ /**
2
+ * @react-router/cloudflare v0.0.0-nightly-a5f191b5e-20240820
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;
@@ -0,0 +1,30 @@
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 ADDED
@@ -0,0 +1,90 @@
1
+ /**
2
+ * @react-router/cloudflare v0.0.0-nightly-a5f191b5e-20240820
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;
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@react-router/cloudflare",
3
+ "version": "0.0.0-nightly-a5f191b5e-20240820",
4
+ "description": "Cloudflare platform abstractions for React Router",
5
+ "bugs": {
6
+ "url": "https://github.com/remix-run/react-router/issues"
7
+ },
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/remix-run/react-router",
11
+ "directory": "packages/react-router-cloudflare"
12
+ },
13
+ "license": "MIT",
14
+ "main": "dist/index.js",
15
+ "typings": "dist/index.d.ts",
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "default": "./dist/index.js"
20
+ },
21
+ "./package.json": "./package.json"
22
+ },
23
+ "devDependencies": {
24
+ "@cloudflare/workers-types": "^4.20230518.0",
25
+ "typescript": "^5.1.0",
26
+ "react-router": "0.0.0-nightly-a5f191b5e-20240820"
27
+ },
28
+ "peerDependencies": {
29
+ "@cloudflare/workers-types": "^4.0.0",
30
+ "typescript": "^5.1.0",
31
+ "react-router": "^0.0.0-nightly-a5f191b5e-20240820"
32
+ },
33
+ "peerDependenciesMeta": {
34
+ "typescript": {
35
+ "optional": true
36
+ }
37
+ },
38
+ "engines": {
39
+ "node": ">=18.0.0"
40
+ },
41
+ "files": [
42
+ "dist/",
43
+ "CHANGELOG.md",
44
+ "LICENSE.md",
45
+ "README.md"
46
+ ],
47
+ "scripts": {
48
+ "build": "rollup -c",
49
+ "tsc": "tsc"
50
+ }
51
+ }