@react-router/node 0.0.0-experimental-a26b992a1 → 0.0.0-experimental-312bddb22
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/dist/globals.js +1 -1
- package/dist/globals.mjs +1 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.js +1 -4
- package/dist/index.mjs +1 -2
- package/dist/install.js +1 -1
- package/dist/install.mjs +1 -1
- package/dist/sessions/fileStorage.js +1 -1
- package/dist/sessions/fileStorage.mjs +1 -1
- package/dist/stream.js +1 -1
- package/dist/stream.mjs +1 -1
- package/package.json +3 -3
- package/dist/upload/fileUploadHandler.d.ts +0 -68
- package/dist/upload/fileUploadHandler.js +0 -207
- package/dist/upload/fileUploadHandler.mjs +0 -174
package/dist/globals.js
CHANGED
package/dist/globals.mjs
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
1
|
export { installGlobals } from "./globals";
|
|
2
2
|
export { createFileSessionStorage } from "./sessions/fileStorage";
|
|
3
|
-
export { createFileUploadHandler as unstable_createFileUploadHandler, NodeOnDiskFile, } from "./upload/fileUploadHandler";
|
|
4
3
|
export { createReadableStreamFromReadable, readableStreamToString, writeAsyncIterableToWritable, writeReadableStreamToWritable, } from "./stream";
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @react-router/node v0.0.0-experimental-
|
|
2
|
+
* @react-router/node v0.0.0-experimental-312bddb22
|
|
3
3
|
*
|
|
4
4
|
* Copyright (c) Remix Software Inc.
|
|
5
5
|
*
|
|
@@ -14,15 +14,12 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
14
14
|
|
|
15
15
|
var globals = require('./globals.js');
|
|
16
16
|
var fileStorage = require('./sessions/fileStorage.js');
|
|
17
|
-
var fileUploadHandler = require('./upload/fileUploadHandler.js');
|
|
18
17
|
var stream = require('./stream.js');
|
|
19
18
|
|
|
20
19
|
|
|
21
20
|
|
|
22
21
|
exports.installGlobals = globals.installGlobals;
|
|
23
22
|
exports.createFileSessionStorage = fileStorage.createFileSessionStorage;
|
|
24
|
-
exports.NodeOnDiskFile = fileUploadHandler.NodeOnDiskFile;
|
|
25
|
-
exports.unstable_createFileUploadHandler = fileUploadHandler.createFileUploadHandler;
|
|
26
23
|
exports.createReadableStreamFromReadable = stream.createReadableStreamFromReadable;
|
|
27
24
|
exports.readableStreamToString = stream.readableStreamToString;
|
|
28
25
|
exports.writeAsyncIterableToWritable = stream.writeAsyncIterableToWritable;
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @react-router/node v0.0.0-experimental-
|
|
2
|
+
* @react-router/node v0.0.0-experimental-312bddb22
|
|
3
3
|
*
|
|
4
4
|
* Copyright (c) Remix Software Inc.
|
|
5
5
|
*
|
|
@@ -10,5 +10,4 @@
|
|
|
10
10
|
*/
|
|
11
11
|
export { installGlobals } from './globals.mjs';
|
|
12
12
|
export { createFileSessionStorage } from './sessions/fileStorage.mjs';
|
|
13
|
-
export { NodeOnDiskFile, createFileUploadHandler as unstable_createFileUploadHandler } from './upload/fileUploadHandler.mjs';
|
|
14
13
|
export { createReadableStreamFromReadable, readableStreamToString, writeAsyncIterableToWritable, writeReadableStreamToWritable } from './stream.mjs';
|
package/dist/install.js
CHANGED
package/dist/install.mjs
CHANGED
package/dist/stream.js
CHANGED
package/dist/stream.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-router/node",
|
|
3
|
-
"version": "0.0.0-experimental-
|
|
3
|
+
"version": "0.0.0-experimental-312bddb22",
|
|
4
4
|
"description": "Node.js platform abstractions for React Router",
|
|
5
5
|
"bugs": {
|
|
6
6
|
"url": "https://github.com/remix-run/react-router/issues"
|
|
@@ -39,11 +39,11 @@
|
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@types/source-map-support": "^0.5.4",
|
|
41
41
|
"typescript": "^5.1.6",
|
|
42
|
-
"react-router": "0.0.0-experimental-
|
|
42
|
+
"react-router": "0.0.0-experimental-312bddb22"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
45
|
"typescript": "^5.1.0",
|
|
46
|
-
"react-router": "0.0.0-experimental-
|
|
46
|
+
"react-router": "0.0.0-experimental-312bddb22"
|
|
47
47
|
},
|
|
48
48
|
"peerDependenciesMeta": {
|
|
49
49
|
"typescript": {
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import type { UploadHandler } from "react-router";
|
|
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
|
-
}
|
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @react-router/node v0.0.0-experimental-a26b992a1
|
|
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_crypto = require('node:crypto');
|
|
16
|
-
var node_fs = require('node:fs');
|
|
17
|
-
var promises = require('node:fs/promises');
|
|
18
|
-
var node_os = require('node:os');
|
|
19
|
-
var path = require('node:path');
|
|
20
|
-
var node_stream = require('node:stream');
|
|
21
|
-
var node_util = require('node:util');
|
|
22
|
-
var reactRouter = require('react-router');
|
|
23
|
-
var streamSlice = require('stream-slice');
|
|
24
|
-
var stream = require('../stream.js');
|
|
25
|
-
|
|
26
|
-
function _interopNamespace(e) {
|
|
27
|
-
if (e && e.__esModule) return e;
|
|
28
|
-
var n = Object.create(null);
|
|
29
|
-
if (e) {
|
|
30
|
-
Object.keys(e).forEach(function (k) {
|
|
31
|
-
if (k !== 'default') {
|
|
32
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
33
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
34
|
-
enumerable: true,
|
|
35
|
-
get: function () { return e[k]; }
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
n["default"] = e;
|
|
41
|
-
return Object.freeze(n);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
var streamSlice__namespace = /*#__PURE__*/_interopNamespace(streamSlice);
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Chooses the path of the file to be uploaded. If a string is not
|
|
48
|
-
* returned the file will not be written.
|
|
49
|
-
*/
|
|
50
|
-
|
|
51
|
-
let defaultFilePathResolver = ({
|
|
52
|
-
filename
|
|
53
|
-
}) => {
|
|
54
|
-
let ext = filename ? path.extname(filename) : "";
|
|
55
|
-
return "upload_" + node_crypto.randomBytes(4).readUInt32LE(0) + ext;
|
|
56
|
-
};
|
|
57
|
-
async function uniqueFile(filepath) {
|
|
58
|
-
let ext = path.extname(filepath);
|
|
59
|
-
let uniqueFilepath = filepath;
|
|
60
|
-
for (let i = 1; await promises.stat(uniqueFilepath).then(() => true).catch(() => false); i++) {
|
|
61
|
-
uniqueFilepath = (ext ? filepath.slice(0, -ext.length) : filepath) + `-${new Date().getTime()}${ext}`;
|
|
62
|
-
}
|
|
63
|
-
return uniqueFilepath;
|
|
64
|
-
}
|
|
65
|
-
function createFileUploadHandler({
|
|
66
|
-
directory = node_os.tmpdir(),
|
|
67
|
-
avoidFileConflicts = true,
|
|
68
|
-
file = defaultFilePathResolver,
|
|
69
|
-
filter,
|
|
70
|
-
maxPartSize = 3000000
|
|
71
|
-
} = {}) {
|
|
72
|
-
return async ({
|
|
73
|
-
name,
|
|
74
|
-
filename,
|
|
75
|
-
contentType,
|
|
76
|
-
data
|
|
77
|
-
}) => {
|
|
78
|
-
if (!filename || filter && !(await filter({
|
|
79
|
-
name,
|
|
80
|
-
filename,
|
|
81
|
-
contentType
|
|
82
|
-
}))) {
|
|
83
|
-
return undefined;
|
|
84
|
-
}
|
|
85
|
-
let dir = typeof directory === "string" ? directory : directory({
|
|
86
|
-
name,
|
|
87
|
-
filename,
|
|
88
|
-
contentType
|
|
89
|
-
});
|
|
90
|
-
if (!dir) {
|
|
91
|
-
return undefined;
|
|
92
|
-
}
|
|
93
|
-
let filedir = path.resolve(dir);
|
|
94
|
-
let path$1 = typeof file === "string" ? file : file({
|
|
95
|
-
name,
|
|
96
|
-
filename,
|
|
97
|
-
contentType
|
|
98
|
-
});
|
|
99
|
-
if (!path$1) {
|
|
100
|
-
return undefined;
|
|
101
|
-
}
|
|
102
|
-
let filepath = path.resolve(filedir, path$1);
|
|
103
|
-
if (avoidFileConflicts) {
|
|
104
|
-
filepath = await uniqueFile(filepath);
|
|
105
|
-
}
|
|
106
|
-
await promises.mkdir(path.dirname(filepath), {
|
|
107
|
-
recursive: true
|
|
108
|
-
}).catch(() => {});
|
|
109
|
-
let writeFileStream = node_fs.createWriteStream(filepath);
|
|
110
|
-
let size = 0;
|
|
111
|
-
let deleteFile = false;
|
|
112
|
-
try {
|
|
113
|
-
for await (let chunk of data) {
|
|
114
|
-
size += chunk.byteLength;
|
|
115
|
-
if (size > maxPartSize) {
|
|
116
|
-
deleteFile = true;
|
|
117
|
-
throw new reactRouter.MaxPartSizeExceededError(name, maxPartSize);
|
|
118
|
-
}
|
|
119
|
-
writeFileStream.write(chunk);
|
|
120
|
-
}
|
|
121
|
-
} finally {
|
|
122
|
-
writeFileStream.end();
|
|
123
|
-
await node_util.promisify(node_stream.finished)(writeFileStream);
|
|
124
|
-
if (deleteFile) {
|
|
125
|
-
await promises.rm(filepath).catch(() => {});
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
// TODO: remove this typecast once TS fixed File class regression
|
|
130
|
-
// https://github.com/microsoft/TypeScript/issues/52166
|
|
131
|
-
return new NodeOnDiskFile(filepath, contentType);
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// TODO: remove this `Omit` usage once TS fixed File class regression
|
|
136
|
-
// https://github.com/microsoft/TypeScript/issues/52166
|
|
137
|
-
class NodeOnDiskFile {
|
|
138
|
-
lastModified = 0;
|
|
139
|
-
webkitRelativePath = "";
|
|
140
|
-
|
|
141
|
-
// TODO: remove this property once TS fixed File class regression
|
|
142
|
-
// https://github.com/microsoft/TypeScript/issues/52166
|
|
143
|
-
prototype = File.prototype;
|
|
144
|
-
constructor(filepath, type, slicer) {
|
|
145
|
-
this.filepath = filepath;
|
|
146
|
-
this.type = type;
|
|
147
|
-
this.slicer = slicer;
|
|
148
|
-
this.name = path.basename(filepath);
|
|
149
|
-
}
|
|
150
|
-
get size() {
|
|
151
|
-
let stats = node_fs.statSync(this.filepath);
|
|
152
|
-
if (this.slicer) {
|
|
153
|
-
let slice = this.slicer.end - this.slicer.start;
|
|
154
|
-
return slice < 0 ? 0 : slice > stats.size ? stats.size : slice;
|
|
155
|
-
}
|
|
156
|
-
return stats.size;
|
|
157
|
-
}
|
|
158
|
-
slice(start, end, type) {
|
|
159
|
-
var _this$slicer;
|
|
160
|
-
if (typeof start === "number" && start < 0) start = this.size + start;
|
|
161
|
-
if (typeof end === "number" && end < 0) end = this.size + end;
|
|
162
|
-
let startOffset = ((_this$slicer = this.slicer) === null || _this$slicer === void 0 ? void 0 : _this$slicer.start) || 0;
|
|
163
|
-
start = startOffset + (start || 0);
|
|
164
|
-
end = startOffset + (end || this.size);
|
|
165
|
-
return new NodeOnDiskFile(this.filepath, typeof type === "string" ? type : this.type, {
|
|
166
|
-
start,
|
|
167
|
-
end
|
|
168
|
-
}
|
|
169
|
-
// TODO: remove this typecast once TS fixed File class regression
|
|
170
|
-
// https://github.com/microsoft/TypeScript/issues/52166
|
|
171
|
-
);
|
|
172
|
-
}
|
|
173
|
-
async arrayBuffer() {
|
|
174
|
-
let stream = node_fs.createReadStream(this.filepath);
|
|
175
|
-
if (this.slicer) {
|
|
176
|
-
stream = stream.pipe(streamSlice__namespace.slice(this.slicer.start, this.slicer.end));
|
|
177
|
-
}
|
|
178
|
-
return new Promise((resolve, reject) => {
|
|
179
|
-
let buf = [];
|
|
180
|
-
stream.on("data", chunk => buf.push(chunk));
|
|
181
|
-
stream.on("end", () => resolve(Buffer.concat(buf)));
|
|
182
|
-
stream.on("error", err => reject(err));
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
stream() {
|
|
186
|
-
let stream$1 = node_fs.createReadStream(this.filepath);
|
|
187
|
-
if (this.slicer) {
|
|
188
|
-
stream$1 = stream$1.pipe(streamSlice__namespace.slice(this.slicer.start, this.slicer.end));
|
|
189
|
-
}
|
|
190
|
-
return stream.createReadableStreamFromReadable(stream$1);
|
|
191
|
-
}
|
|
192
|
-
async text() {
|
|
193
|
-
return stream.readableStreamToString(this.stream());
|
|
194
|
-
}
|
|
195
|
-
get [Symbol.toStringTag]() {
|
|
196
|
-
return "File";
|
|
197
|
-
}
|
|
198
|
-
remove() {
|
|
199
|
-
return promises.unlink(this.filepath);
|
|
200
|
-
}
|
|
201
|
-
getFilePath() {
|
|
202
|
-
return this.filepath;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
exports.NodeOnDiskFile = NodeOnDiskFile;
|
|
207
|
-
exports.createFileUploadHandler = createFileUploadHandler;
|
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @react-router/node v0.0.0-experimental-a26b992a1
|
|
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
|
-
import { randomBytes } from 'node:crypto';
|
|
12
|
-
import { createWriteStream, statSync, createReadStream } from 'node:fs';
|
|
13
|
-
import { mkdir, rm, unlink, stat } from 'node:fs/promises';
|
|
14
|
-
import { tmpdir } from 'node:os';
|
|
15
|
-
import { resolve, dirname, basename, extname } from 'node:path';
|
|
16
|
-
import { finished } from 'node:stream';
|
|
17
|
-
import { promisify } from 'node:util';
|
|
18
|
-
import { MaxPartSizeExceededError } from 'react-router';
|
|
19
|
-
import * as streamSlice from 'stream-slice';
|
|
20
|
-
import { createReadableStreamFromReadable, readableStreamToString } from '../stream.mjs';
|
|
21
|
-
|
|
22
|
-
let defaultFilePathResolver = ({
|
|
23
|
-
filename
|
|
24
|
-
}) => {
|
|
25
|
-
let ext = filename ? extname(filename) : "";
|
|
26
|
-
return "upload_" + randomBytes(4).readUInt32LE(0) + ext;
|
|
27
|
-
};
|
|
28
|
-
async function uniqueFile(filepath) {
|
|
29
|
-
let ext = extname(filepath);
|
|
30
|
-
let uniqueFilepath = filepath;
|
|
31
|
-
for (let i = 1; await stat(uniqueFilepath).then(() => true).catch(() => false); i++) {
|
|
32
|
-
uniqueFilepath = (ext ? filepath.slice(0, -ext.length) : filepath) + `-${new Date().getTime()}${ext}`;
|
|
33
|
-
}
|
|
34
|
-
return uniqueFilepath;
|
|
35
|
-
}
|
|
36
|
-
function createFileUploadHandler({
|
|
37
|
-
directory = tmpdir(),
|
|
38
|
-
avoidFileConflicts = true,
|
|
39
|
-
file = defaultFilePathResolver,
|
|
40
|
-
filter,
|
|
41
|
-
maxPartSize = 3000000
|
|
42
|
-
} = {}) {
|
|
43
|
-
return async ({
|
|
44
|
-
name,
|
|
45
|
-
filename,
|
|
46
|
-
contentType,
|
|
47
|
-
data
|
|
48
|
-
}) => {
|
|
49
|
-
if (!filename || filter && !(await filter({
|
|
50
|
-
name,
|
|
51
|
-
filename,
|
|
52
|
-
contentType
|
|
53
|
-
}))) {
|
|
54
|
-
return undefined;
|
|
55
|
-
}
|
|
56
|
-
let dir = typeof directory === "string" ? directory : directory({
|
|
57
|
-
name,
|
|
58
|
-
filename,
|
|
59
|
-
contentType
|
|
60
|
-
});
|
|
61
|
-
if (!dir) {
|
|
62
|
-
return undefined;
|
|
63
|
-
}
|
|
64
|
-
let filedir = resolve(dir);
|
|
65
|
-
let path = typeof file === "string" ? file : file({
|
|
66
|
-
name,
|
|
67
|
-
filename,
|
|
68
|
-
contentType
|
|
69
|
-
});
|
|
70
|
-
if (!path) {
|
|
71
|
-
return undefined;
|
|
72
|
-
}
|
|
73
|
-
let filepath = resolve(filedir, path);
|
|
74
|
-
if (avoidFileConflicts) {
|
|
75
|
-
filepath = await uniqueFile(filepath);
|
|
76
|
-
}
|
|
77
|
-
await mkdir(dirname(filepath), {
|
|
78
|
-
recursive: true
|
|
79
|
-
}).catch(() => {});
|
|
80
|
-
let writeFileStream = createWriteStream(filepath);
|
|
81
|
-
let size = 0;
|
|
82
|
-
let deleteFile = false;
|
|
83
|
-
try {
|
|
84
|
-
for await (let chunk of data) {
|
|
85
|
-
size += chunk.byteLength;
|
|
86
|
-
if (size > maxPartSize) {
|
|
87
|
-
deleteFile = true;
|
|
88
|
-
throw new MaxPartSizeExceededError(name, maxPartSize);
|
|
89
|
-
}
|
|
90
|
-
writeFileStream.write(chunk);
|
|
91
|
-
}
|
|
92
|
-
} finally {
|
|
93
|
-
writeFileStream.end();
|
|
94
|
-
await promisify(finished)(writeFileStream);
|
|
95
|
-
if (deleteFile) {
|
|
96
|
-
await rm(filepath).catch(() => {});
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
// TODO: remove this typecast once TS fixed File class regression
|
|
100
|
-
// https://github.com/microsoft/TypeScript/issues/52166
|
|
101
|
-
return new NodeOnDiskFile(filepath, contentType);
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
// TODO: remove this `Omit` usage once TS fixed File class regression
|
|
105
|
-
// https://github.com/microsoft/TypeScript/issues/52166
|
|
106
|
-
class NodeOnDiskFile {
|
|
107
|
-
lastModified = 0;
|
|
108
|
-
webkitRelativePath = "";
|
|
109
|
-
// TODO: remove this property once TS fixed File class regression
|
|
110
|
-
// https://github.com/microsoft/TypeScript/issues/52166
|
|
111
|
-
prototype = File.prototype;
|
|
112
|
-
constructor(filepath, type, slicer) {
|
|
113
|
-
this.filepath = filepath;
|
|
114
|
-
this.type = type;
|
|
115
|
-
this.slicer = slicer;
|
|
116
|
-
this.name = basename(filepath);
|
|
117
|
-
}
|
|
118
|
-
get size() {
|
|
119
|
-
let stats = statSync(this.filepath);
|
|
120
|
-
if (this.slicer) {
|
|
121
|
-
let slice = this.slicer.end - this.slicer.start;
|
|
122
|
-
return slice < 0 ? 0 : slice > stats.size ? stats.size : slice;
|
|
123
|
-
}
|
|
124
|
-
return stats.size;
|
|
125
|
-
}
|
|
126
|
-
slice(start, end, type) {
|
|
127
|
-
var _this$slicer;
|
|
128
|
-
if (typeof start === "number" && start < 0) start = this.size + start;
|
|
129
|
-
if (typeof end === "number" && end < 0) end = this.size + end;
|
|
130
|
-
let startOffset = ((_this$slicer = this.slicer) === null || _this$slicer === void 0 ? void 0 : _this$slicer.start) || 0;
|
|
131
|
-
start = startOffset + (start || 0);
|
|
132
|
-
end = startOffset + (end || this.size);
|
|
133
|
-
return new NodeOnDiskFile(this.filepath, typeof type === "string" ? type : this.type, {
|
|
134
|
-
start,
|
|
135
|
-
end
|
|
136
|
-
}
|
|
137
|
-
// TODO: remove this typecast once TS fixed File class regression
|
|
138
|
-
// https://github.com/microsoft/TypeScript/issues/52166
|
|
139
|
-
);
|
|
140
|
-
}
|
|
141
|
-
async arrayBuffer() {
|
|
142
|
-
let stream = createReadStream(this.filepath);
|
|
143
|
-
if (this.slicer) {
|
|
144
|
-
stream = stream.pipe(streamSlice.slice(this.slicer.start, this.slicer.end));
|
|
145
|
-
}
|
|
146
|
-
return new Promise((resolve, reject) => {
|
|
147
|
-
let buf = [];
|
|
148
|
-
stream.on("data", chunk => buf.push(chunk));
|
|
149
|
-
stream.on("end", () => resolve(Buffer.concat(buf)));
|
|
150
|
-
stream.on("error", err => reject(err));
|
|
151
|
-
});
|
|
152
|
-
}
|
|
153
|
-
stream() {
|
|
154
|
-
let stream = createReadStream(this.filepath);
|
|
155
|
-
if (this.slicer) {
|
|
156
|
-
stream = stream.pipe(streamSlice.slice(this.slicer.start, this.slicer.end));
|
|
157
|
-
}
|
|
158
|
-
return createReadableStreamFromReadable(stream);
|
|
159
|
-
}
|
|
160
|
-
async text() {
|
|
161
|
-
return readableStreamToString(this.stream());
|
|
162
|
-
}
|
|
163
|
-
get [Symbol.toStringTag]() {
|
|
164
|
-
return "File";
|
|
165
|
-
}
|
|
166
|
-
remove() {
|
|
167
|
-
return unlink(this.filepath);
|
|
168
|
-
}
|
|
169
|
-
getFilePath() {
|
|
170
|
-
return this.filepath;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
export { NodeOnDiskFile, createFileUploadHandler };
|