@react-router/node 0.0.0-experimental-c0856287f

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.
@@ -0,0 +1,4 @@
1
+ export declare const createCookie: import("@react-router/server-runtime").CreateCookieFunction;
2
+ export declare const createCookieSessionStorage: import("@react-router/server-runtime").CreateCookieSessionStorageFunction;
3
+ export declare const createSessionStorage: import("@react-router/server-runtime").CreateSessionStorageFunction;
4
+ export declare const createMemorySessionStorage: import("@react-router/server-runtime").CreateMemorySessionStorageFunction;
@@ -0,0 +1,29 @@
1
+ /**
2
+ * @react-router/node v0.0.0-experimental-c0856287f
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 serverRuntime = require('@react-router/server-runtime');
16
+ var crypto = require('./crypto.js');
17
+
18
+ const createCookie = serverRuntime.createCookieFactory({
19
+ sign: crypto.sign,
20
+ unsign: crypto.unsign
21
+ });
22
+ const createCookieSessionStorage = serverRuntime.createCookieSessionStorageFactory(createCookie);
23
+ const createSessionStorage = serverRuntime.createSessionStorageFactory(createCookie);
24
+ const createMemorySessionStorage = serverRuntime.createMemorySessionStorageFactory(createSessionStorage);
25
+
26
+ exports.createCookie = createCookie;
27
+ exports.createCookieSessionStorage = createCookieSessionStorage;
28
+ exports.createMemorySessionStorage = createMemorySessionStorage;
29
+ exports.createSessionStorage = createSessionStorage;
@@ -0,0 +1,7 @@
1
+ export { installGlobals } from "./globals";
2
+ export { createFileSessionStorage } from "./sessions/fileStorage";
3
+ export { createFileUploadHandler as unstable_createFileUploadHandler, NodeOnDiskFile, } from "./upload/fileUploadHandler";
4
+ export { createCookie, createCookieSessionStorage, createMemorySessionStorage, createSessionStorage, } from "./implementations";
5
+ export { createReadableStreamFromReadable, readableStreamToString, writeAsyncIterableToWritable, writeReadableStreamToWritable, } from "./stream";
6
+ export { createRequestHandler, createSession, defer, isCookie, isSession, json, MaxPartSizeExceededError, redirect, redirectDocument, unstable_composeUploadHandlers, unstable_createMemoryUploadHandler, unstable_parseMultipartFormData, } from "@react-router/server-runtime";
7
+ export type { ActionFunction, ActionFunctionArgs, AppLoadContext, Cookie, CookieOptions, CookieParseOptions, CookieSerializeOptions, CookieSignatureOptions, DataFunctionArgs, EntryContext, ErrorResponse, HandleDataRequestFunction, HandleDocumentRequestFunction, HeadersArgs, HeadersFunction, HtmlLinkDescriptor, JsonFunction, LinkDescriptor, LinksFunction, LoaderFunction, LoaderFunctionArgs, MemoryUploadHandlerFilterArgs, MemoryUploadHandlerOptions, HandleErrorFunction, PageLinkDescriptor, RequestHandler, SerializeFrom, ServerBuild, ServerEntryModule, ServerRuntimeMetaArgs as MetaArgs, ServerRuntimeMetaDescriptor as MetaDescriptor, ServerRuntimeMetaFunction as MetaFunction, Session, SessionData, SessionIdStorageStrategy, SessionStorage, SignFunction, TypedDeferredData, TypedResponse, UnsignFunction, UploadHandler, UploadHandlerPart, } from "@react-router/server-runtime";
package/dist/index.js ADDED
@@ -0,0 +1,83 @@
1
+ /**
2
+ * @react-router/node v0.0.0-experimental-c0856287f
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 globals = require('./globals.js');
16
+ var fileStorage = require('./sessions/fileStorage.js');
17
+ var fileUploadHandler = require('./upload/fileUploadHandler.js');
18
+ var implementations = require('./implementations.js');
19
+ var stream = require('./stream.js');
20
+ var serverRuntime = require('@react-router/server-runtime');
21
+
22
+
23
+
24
+ exports.installGlobals = globals.installGlobals;
25
+ exports.createFileSessionStorage = fileStorage.createFileSessionStorage;
26
+ exports.NodeOnDiskFile = fileUploadHandler.NodeOnDiskFile;
27
+ exports.unstable_createFileUploadHandler = fileUploadHandler.createFileUploadHandler;
28
+ exports.createCookie = implementations.createCookie;
29
+ exports.createCookieSessionStorage = implementations.createCookieSessionStorage;
30
+ exports.createMemorySessionStorage = implementations.createMemorySessionStorage;
31
+ exports.createSessionStorage = implementations.createSessionStorage;
32
+ exports.createReadableStreamFromReadable = stream.createReadableStreamFromReadable;
33
+ exports.readableStreamToString = stream.readableStreamToString;
34
+ exports.writeAsyncIterableToWritable = stream.writeAsyncIterableToWritable;
35
+ exports.writeReadableStreamToWritable = stream.writeReadableStreamToWritable;
36
+ Object.defineProperty(exports, 'MaxPartSizeExceededError', {
37
+ enumerable: true,
38
+ get: function () { return serverRuntime.MaxPartSizeExceededError; }
39
+ });
40
+ Object.defineProperty(exports, 'createRequestHandler', {
41
+ enumerable: true,
42
+ get: function () { return serverRuntime.createRequestHandler; }
43
+ });
44
+ Object.defineProperty(exports, 'createSession', {
45
+ enumerable: true,
46
+ get: function () { return serverRuntime.createSession; }
47
+ });
48
+ Object.defineProperty(exports, 'defer', {
49
+ enumerable: true,
50
+ get: function () { return serverRuntime.defer; }
51
+ });
52
+ Object.defineProperty(exports, 'isCookie', {
53
+ enumerable: true,
54
+ get: function () { return serverRuntime.isCookie; }
55
+ });
56
+ Object.defineProperty(exports, 'isSession', {
57
+ enumerable: true,
58
+ get: function () { return serverRuntime.isSession; }
59
+ });
60
+ Object.defineProperty(exports, 'json', {
61
+ enumerable: true,
62
+ get: function () { return serverRuntime.json; }
63
+ });
64
+ Object.defineProperty(exports, 'redirect', {
65
+ enumerable: true,
66
+ get: function () { return serverRuntime.redirect; }
67
+ });
68
+ Object.defineProperty(exports, 'redirectDocument', {
69
+ enumerable: true,
70
+ get: function () { return serverRuntime.redirectDocument; }
71
+ });
72
+ Object.defineProperty(exports, 'unstable_composeUploadHandlers', {
73
+ enumerable: true,
74
+ get: function () { return serverRuntime.unstable_composeUploadHandlers; }
75
+ });
76
+ Object.defineProperty(exports, 'unstable_createMemoryUploadHandler', {
77
+ enumerable: true,
78
+ get: function () { return serverRuntime.unstable_createMemoryUploadHandler; }
79
+ });
80
+ Object.defineProperty(exports, 'unstable_parseMultipartFormData', {
81
+ enumerable: true,
82
+ get: function () { return serverRuntime.unstable_parseMultipartFormData; }
83
+ });
@@ -0,0 +1,23 @@
1
+ import type { SessionStorage, SessionIdStorageStrategy, SessionData } from "@react-router/server-runtime";
2
+ interface FileSessionStorageOptions {
3
+ /**
4
+ * The Cookie used to store the session id on the client, or options used
5
+ * to automatically create one.
6
+ */
7
+ cookie?: SessionIdStorageStrategy["cookie"];
8
+ /**
9
+ * The directory to use to store session files.
10
+ */
11
+ dir: string;
12
+ }
13
+ /**
14
+ * Creates a SessionStorage that stores session data on a filesystem.
15
+ *
16
+ * The advantage of using this instead of cookie session storage is that
17
+ * files may contain much more data than cookies.
18
+ *
19
+ * @see https://remix.run/utils/sessions#createfilesessionstorage-node
20
+ */
21
+ export declare function createFileSessionStorage<Data = SessionData, FlashData = Data>({ cookie, dir, }: FileSessionStorageOptions): SessionStorage<Data, FlashData>;
22
+ export declare function getFile(dir: string, id: string): string;
23
+ export {};
@@ -0,0 +1,135 @@
1
+ /**
2
+ * @react-router/node v0.0.0-experimental-c0856287f
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 crypto = require('node:crypto');
16
+ var node_fs = require('node:fs');
17
+ var path = require('node:path');
18
+ var implementations = require('../implementations.js');
19
+
20
+ function _interopNamespace(e) {
21
+ if (e && e.__esModule) return e;
22
+ var n = Object.create(null);
23
+ if (e) {
24
+ Object.keys(e).forEach(function (k) {
25
+ if (k !== 'default') {
26
+ var d = Object.getOwnPropertyDescriptor(e, k);
27
+ Object.defineProperty(n, k, d.get ? d : {
28
+ enumerable: true,
29
+ get: function () { return e[k]; }
30
+ });
31
+ }
32
+ });
33
+ }
34
+ n["default"] = e;
35
+ return Object.freeze(n);
36
+ }
37
+
38
+ var crypto__namespace = /*#__PURE__*/_interopNamespace(crypto);
39
+ var path__namespace = /*#__PURE__*/_interopNamespace(path);
40
+
41
+ /**
42
+ * Creates a SessionStorage that stores session data on a filesystem.
43
+ *
44
+ * The advantage of using this instead of cookie session storage is that
45
+ * files may contain much more data than cookies.
46
+ *
47
+ * @see https://remix.run/utils/sessions#createfilesessionstorage-node
48
+ */
49
+ function createFileSessionStorage({
50
+ cookie,
51
+ dir
52
+ }) {
53
+ return implementations.createSessionStorage({
54
+ cookie,
55
+ async createData(data, expires) {
56
+ let content = JSON.stringify({
57
+ data,
58
+ expires
59
+ });
60
+ while (true) {
61
+ // TODO: Once Node v19 is supported we should use the globally provided
62
+ // Web Crypto API's crypto.getRandomValues() function here instead.
63
+ let randomBytes = crypto__namespace.webcrypto.getRandomValues(new Uint8Array(8));
64
+ // This storage manages an id space of 2^64 ids, which is far greater
65
+ // than the maximum number of files allowed on an NTFS or ext4 volume
66
+ // (2^32). However, the larger id space should help to avoid collisions
67
+ // with existing ids when creating new sessions, which speeds things up.
68
+ let id = Buffer.from(randomBytes).toString("hex");
69
+ try {
70
+ let file = getFile(dir, id);
71
+ await node_fs.promises.mkdir(path__namespace.dirname(file), {
72
+ recursive: true
73
+ });
74
+ await node_fs.promises.writeFile(file, content, {
75
+ encoding: "utf-8",
76
+ flag: "wx"
77
+ });
78
+ return id;
79
+ } catch (error) {
80
+ if (error.code !== "EEXIST") throw error;
81
+ }
82
+ }
83
+ },
84
+ async readData(id) {
85
+ try {
86
+ let file = getFile(dir, id);
87
+ let content = JSON.parse(await node_fs.promises.readFile(file, "utf-8"));
88
+ let data = content.data;
89
+ let expires = typeof content.expires === "string" ? new Date(content.expires) : null;
90
+ if (!expires || expires > new Date()) {
91
+ return data;
92
+ }
93
+ // Remove expired session data.
94
+ if (expires) await node_fs.promises.unlink(file);
95
+ return null;
96
+ } catch (error) {
97
+ if (error.code !== "ENOENT") throw error;
98
+ return null;
99
+ }
100
+ },
101
+ async updateData(id, data, expires) {
102
+ let content = JSON.stringify({
103
+ data,
104
+ expires
105
+ });
106
+ let file = getFile(dir, id);
107
+ await node_fs.promises.mkdir(path__namespace.dirname(file), {
108
+ recursive: true
109
+ });
110
+ await node_fs.promises.writeFile(file, content, "utf-8");
111
+ },
112
+ async deleteData(id) {
113
+ // Return early if the id is empty, otherwise we'll end up trying to
114
+ // unlink the dir, which will cause the EPERM error.
115
+ if (!id) {
116
+ return;
117
+ }
118
+ try {
119
+ await node_fs.promises.unlink(getFile(dir, id));
120
+ } catch (error) {
121
+ if (error.code !== "ENOENT") throw error;
122
+ }
123
+ }
124
+ });
125
+ }
126
+ function getFile(dir, id) {
127
+ // Divide the session id up into a directory (first 2 bytes) and filename
128
+ // (remaining 6 bytes) to reduce the chance of having very large directories,
129
+ // which should speed up file access. This is a maximum of 2^16 directories,
130
+ // each with 2^48 files.
131
+ return path__namespace.join(dir, id.slice(0, 4), id.slice(4));
132
+ }
133
+
134
+ exports.createFileSessionStorage = createFileSessionStorage;
135
+ exports.getFile = getFile;
@@ -0,0 +1,9 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import type { Readable, Writable } from "node:stream";
4
+ export declare function writeReadableStreamToWritable(stream: ReadableStream, writable: Writable): Promise<void>;
5
+ export declare function writeAsyncIterableToWritable(iterable: AsyncIterable<Uint8Array>, writable: Writable): Promise<void>;
6
+ export declare function readableStreamToString(stream: ReadableStream<Uint8Array>, encoding?: BufferEncoding): Promise<string>;
7
+ export declare const createReadableStreamFromReadable: (source: Readable & {
8
+ readableHighWaterMark?: number;
9
+ }) => ReadableStream<Uint8Array>;
package/dist/stream.js ADDED
@@ -0,0 +1,146 @@
1
+ /**
2
+ * @react-router/node v0.0.0-experimental-c0856287f
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 node_stream = require('node:stream');
16
+
17
+ async function writeReadableStreamToWritable(stream, writable) {
18
+ let reader = stream.getReader();
19
+ let flushable = writable;
20
+ try {
21
+ while (true) {
22
+ let {
23
+ done,
24
+ value
25
+ } = await reader.read();
26
+ if (done) {
27
+ writable.end();
28
+ break;
29
+ }
30
+ writable.write(value);
31
+ if (typeof flushable.flush === "function") {
32
+ flushable.flush();
33
+ }
34
+ }
35
+ } catch (error) {
36
+ writable.destroy(error);
37
+ throw error;
38
+ }
39
+ }
40
+ async function writeAsyncIterableToWritable(iterable, writable) {
41
+ try {
42
+ for await (let chunk of iterable) {
43
+ writable.write(chunk);
44
+ }
45
+ writable.end();
46
+ } catch (error) {
47
+ writable.destroy(error);
48
+ throw error;
49
+ }
50
+ }
51
+ async function readableStreamToString(stream, encoding) {
52
+ let reader = stream.getReader();
53
+ let chunks = [];
54
+ while (true) {
55
+ let {
56
+ done,
57
+ value
58
+ } = await reader.read();
59
+ if (done) {
60
+ break;
61
+ }
62
+ if (value) {
63
+ chunks.push(value);
64
+ }
65
+ }
66
+ return Buffer.concat(chunks).toString(encoding);
67
+ }
68
+ const createReadableStreamFromReadable = source => {
69
+ let pump = new StreamPump(source);
70
+ let stream = new ReadableStream(pump, pump);
71
+ return stream;
72
+ };
73
+ class StreamPump {
74
+ constructor(stream) {
75
+ this.highWaterMark = stream.readableHighWaterMark || new node_stream.Stream.Readable().readableHighWaterMark;
76
+ this.accumalatedSize = 0;
77
+ this.stream = stream;
78
+ this.enqueue = this.enqueue.bind(this);
79
+ this.error = this.error.bind(this);
80
+ this.close = this.close.bind(this);
81
+ }
82
+ size(chunk) {
83
+ return (chunk === null || chunk === void 0 ? void 0 : chunk.byteLength) || 0;
84
+ }
85
+ start(controller) {
86
+ this.controller = controller;
87
+ this.stream.on("data", this.enqueue);
88
+ this.stream.once("error", this.error);
89
+ this.stream.once("end", this.close);
90
+ this.stream.once("close", this.close);
91
+ }
92
+ pull() {
93
+ this.resume();
94
+ }
95
+ cancel(reason) {
96
+ if (this.stream.destroy) {
97
+ this.stream.destroy(reason);
98
+ }
99
+ this.stream.off("data", this.enqueue);
100
+ this.stream.off("error", this.error);
101
+ this.stream.off("end", this.close);
102
+ this.stream.off("close", this.close);
103
+ }
104
+ enqueue(chunk) {
105
+ if (this.controller) {
106
+ try {
107
+ let bytes = chunk instanceof Uint8Array ? chunk : Buffer.from(chunk);
108
+ let available = (this.controller.desiredSize || 0) - bytes.byteLength;
109
+ this.controller.enqueue(bytes);
110
+ if (available <= 0) {
111
+ this.pause();
112
+ }
113
+ } catch (error) {
114
+ this.controller.error(new Error("Could not create Buffer, chunk must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object"));
115
+ this.cancel();
116
+ }
117
+ }
118
+ }
119
+ pause() {
120
+ if (this.stream.pause) {
121
+ this.stream.pause();
122
+ }
123
+ }
124
+ resume() {
125
+ if (this.stream.readable && this.stream.resume) {
126
+ this.stream.resume();
127
+ }
128
+ }
129
+ close() {
130
+ if (this.controller) {
131
+ this.controller.close();
132
+ delete this.controller;
133
+ }
134
+ }
135
+ error(error) {
136
+ if (this.controller) {
137
+ this.controller.error(error);
138
+ delete this.controller;
139
+ }
140
+ }
141
+ }
142
+
143
+ exports.createReadableStreamFromReadable = createReadableStreamFromReadable;
144
+ exports.readableStreamToString = readableStreamToString;
145
+ exports.writeAsyncIterableToWritable = writeAsyncIterableToWritable;
146
+ exports.writeReadableStreamToWritable = writeReadableStreamToWritable;
@@ -0,0 +1,68 @@
1
+ /// <reference types="node" />
2
+ import type { UploadHandler } from "@react-router/server-runtime";
3
+ export type FileUploadHandlerFilterArgs = {
4
+ filename: string;
5
+ contentType: string;
6
+ name: string;
7
+ };
8
+ export type FileUploadHandlerPathResolverArgs = {
9
+ filename: string;
10
+ contentType: string;
11
+ name: string;
12
+ };
13
+ /**
14
+ * Chooses the path of the file to be uploaded. If a string is not
15
+ * returned the file will not be written.
16
+ */
17
+ export type FileUploadHandlerPathResolver = (args: FileUploadHandlerPathResolverArgs) => string | undefined;
18
+ export type FileUploadHandlerOptions = {
19
+ /**
20
+ * Avoid file conflicts by appending a count on the end of the filename
21
+ * if it already exists on disk. Defaults to `true`.
22
+ */
23
+ avoidFileConflicts?: boolean;
24
+ /**
25
+ * The directory to write the upload.
26
+ */
27
+ directory?: string | FileUploadHandlerPathResolver;
28
+ /**
29
+ * The name of the file in the directory. Can be a relative path, the directory
30
+ * structure will be created if it does not exist.
31
+ */
32
+ file?: FileUploadHandlerPathResolver;
33
+ /**
34
+ * The maximum upload size allowed. If the size is exceeded an error will be thrown.
35
+ * Defaults to 3000000B (3MB).
36
+ */
37
+ maxPartSize?: number;
38
+ /**
39
+ *
40
+ * @param filename
41
+ * @param contentType
42
+ * @param name
43
+ */
44
+ filter?(args: FileUploadHandlerFilterArgs): boolean | Promise<boolean>;
45
+ };
46
+ export declare function createFileUploadHandler({ directory, avoidFileConflicts, file, filter, maxPartSize, }?: FileUploadHandlerOptions): UploadHandler;
47
+ export declare class NodeOnDiskFile implements Omit<File, "constructor"> {
48
+ private filepath;
49
+ type: string;
50
+ private slicer?;
51
+ name: string;
52
+ lastModified: number;
53
+ webkitRelativePath: string;
54
+ prototype: File;
55
+ constructor(filepath: string, type: string, slicer?: {
56
+ start: number;
57
+ end: number;
58
+ } | undefined);
59
+ get size(): number;
60
+ slice(start?: number, end?: number, type?: string): Blob;
61
+ arrayBuffer(): Promise<ArrayBuffer>;
62
+ stream(): ReadableStream<any>;
63
+ stream(): NodeJS.ReadableStream;
64
+ text(): Promise<string>;
65
+ get [Symbol.toStringTag](): string;
66
+ remove(): Promise<void>;
67
+ getFilePath(): string;
68
+ }