cf-workers-native 0.1.4 → 0.1.5
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/browser/documents/page-source.js +1 -1
- package/dist/browser/fetch/host-call.js +6 -1
- package/dist/browser/fetch/native-network.d.ts +1 -1
- package/dist/browser/fetch/relay.js +2 -0
- package/dist/browser/isolated/transport.js +3 -2
- package/dist/browser/request/index.js +3 -2
- package/dist/cloudflare/workers/worker-runtime.js +2 -1
- package/dist/compat/console/index.d.ts +35 -0
- package/dist/compat/console/index.js +92 -0
- package/dist/compat/crypto/LICENSE-3RD-PARTY.md +19 -0
- package/dist/compat/crypto/LICENSE-APACHE +177 -0
- package/dist/compat/crypto/LICENSE.emscripten +102 -0
- package/dist/compat/crypto/LICENSE.musl +193 -0
- package/dist/compat/crypto/LICENSE.openssl +177 -0
- package/dist/compat/crypto/asymmetric.d.ts +13 -0
- package/dist/compat/crypto/asymmetric.js +239 -0
- package/dist/compat/crypto/brorand.cjs +74 -0
- package/dist/compat/crypto/brorand.d.cts +6 -0
- package/dist/compat/crypto/certificate.d.ts +14 -0
- package/dist/compat/crypto/certificate.js +66 -0
- package/dist/compat/crypto/ecdh.d.ts +15 -0
- package/dist/compat/crypto/ecdh.js +109 -0
- package/dist/compat/crypto/hkdf.d.ts +2 -0
- package/dist/compat/crypto/hkdf.js +52 -0
- package/dist/compat/crypto/index.cjs +2 -0
- package/dist/compat/crypto/index.d.cts +2 -0
- package/dist/compat/crypto/index.d.ts +30 -0
- package/dist/compat/crypto/index.js +83 -0
- package/dist/compat/crypto/key-formats.d.ts +6 -0
- package/dist/compat/crypto/key-formats.js +120 -0
- package/dist/compat/crypto/key-state.d.ts +9 -0
- package/dist/compat/crypto/key-state.js +4 -0
- package/dist/compat/crypto/keys.d.ts +50 -0
- package/dist/compat/crypto/keys.js +209 -0
- package/dist/compat/crypto/metadata.d.ts +4 -0
- package/dist/compat/crypto/metadata.js +22 -0
- package/dist/compat/crypto/native-crypto.d.ts +68 -0
- package/dist/compat/crypto/native-crypto.js +569 -0
- package/dist/compat/crypto/prime.d.ts +19 -0
- package/dist/compat/crypto/prime.js +146 -0
- package/dist/compat/crypto/random.d.ts +7 -0
- package/dist/compat/crypto/random.js +123 -0
- package/dist/compat/crypto/rsa.d.ts +5 -0
- package/dist/compat/crypto/rsa.js +173 -0
- package/dist/compat/crypto/scrypt.d.ts +25 -0
- package/dist/compat/crypto/scrypt.js +150 -0
- package/dist/compat/crypto/sign-errors.d.ts +8 -0
- package/dist/compat/crypto/sign-errors.js +13 -0
- package/dist/compat/crypto/sign-handle.d.ts +22 -0
- package/dist/compat/crypto/sign-handle.js +105 -0
- package/dist/compat/crypto/signatures.d.ts +28 -0
- package/dist/compat/crypto/signatures.js +252 -0
- package/dist/compat/crypto/symmetric.d.ts +29 -0
- package/dist/compat/crypto/symmetric.js +104 -0
- package/dist/compat/crypto/validation.d.ts +3 -0
- package/dist/compat/crypto/validation.js +19 -0
- package/dist/compat/crypto/x509.d.ts +30 -0
- package/dist/compat/crypto/x509.js +406 -0
- package/dist/compat/events/errors.d.ts +8 -0
- package/dist/compat/events/errors.js +39 -0
- package/dist/compat/events/index.cjs +2 -0
- package/dist/compat/events/index.d.cts +2 -0
- package/dist/compat/events/index.d.ts +64 -0
- package/dist/compat/events/index.js +792 -0
- package/dist/compat/events/intrinsics.d.ts +1 -0
- package/dist/compat/events/intrinsics.js +2 -0
- package/dist/compat/fs/embedded.d.ts +85 -0
- package/dist/compat/fs/embedded.js +255 -0
- package/dist/compat/fs/index.d.ts +7 -84
- package/dist/compat/fs/index.js +218 -252
- package/dist/compat/fs/mode.d.ts +1 -0
- package/dist/compat/fs/mode.js +13 -0
- package/dist/compat/fs/promises/index.d.ts +1 -1
- package/dist/compat/fs/promises/index.js +1 -1
- package/dist/compat/fs/writable.d.ts +6 -0
- package/dist/compat/fs/writable.js +142 -0
- package/dist/compat/http/symbols.d.ts +5 -0
- package/dist/compat/http/symbols.js +9 -0
- package/dist/compat/http-common/index.d.ts +37 -0
- package/dist/compat/http-common/index.js +43 -0
- package/dist/compat/http-outgoing/index.d.ts +13 -0
- package/dist/compat/http-outgoing/index.js +32 -0
- package/dist/compat/http-server/index.d.ts +18 -0
- package/dist/compat/http-server/index.js +107 -0
- package/dist/compat/module/builtins.d.ts +1 -0
- package/dist/compat/module/builtins.js +101 -0
- package/dist/compat/module/index.d.ts +4 -2
- package/dist/compat/module/index.js +22 -17
- package/dist/compat/module/metadata.d.ts +44 -0
- package/dist/compat/module/metadata.js +127 -0
- package/dist/compat/net/internal-net.js +4 -0
- package/dist/compat/net/sockets.d.ts +5 -0
- package/dist/compat/net/sockets.js +74 -13
- package/dist/compat/process/index.cjs +2 -0
- package/dist/compat/process/index.d.cts +4 -0
- package/dist/compat/process/index.d.ts +18 -0
- package/dist/compat/process/index.js +140 -0
- package/dist/compat/process/metadata.d.ts +135 -0
- package/dist/compat/process/metadata.js +251 -0
- package/dist/compat/process/state.d.ts +4 -0
- package/dist/compat/process/state.js +4 -0
- package/dist/compat/stream-web/LICENSE +22 -0
- package/dist/compat/stream-web/ponyfill.d.ts +1 -1
- package/dist/compat/stream-web/ponyfill.js +2369 -1775
- package/dist/compat/stream-web/runtime.d.ts +1 -1
- package/dist/compat/stream-web/runtime.js +3 -2
- package/dist/compat/stream-wrap/LICENSE-3RD-PARTY.md +11 -0
- package/dist/compat/stream-wrap/LICENSE-APACHE +177 -0
- package/dist/compat/stream-wrap/index.d.ts +1 -0
- package/dist/compat/stream-wrap/index.js +1 -0
- package/dist/compat/tls/LICENSE-3RD-PARTY.md +11 -0
- package/dist/compat/tls/LICENSE-APACHE +177 -0
- package/dist/compat/tls/constants.d.ts +7 -0
- package/dist/compat/tls/constants.js +14 -0
- package/dist/compat/tls/errors.d.ts +35 -0
- package/dist/compat/tls/errors.js +68 -0
- package/dist/compat/tls/helpers.d.ts +9 -0
- package/dist/compat/tls/helpers.js +207 -0
- package/dist/compat/tls/index.d.ts +26 -0
- package/dist/compat/tls/index.js +50 -0
- package/dist/compat/tls/jsstream.d.ts +19 -0
- package/dist/compat/tls/jsstream.js +186 -0
- package/dist/compat/tls/native-session.d.ts +3 -0
- package/dist/compat/tls/native-session.js +187 -0
- package/dist/compat/tls/wrap.d.ts +65 -0
- package/dist/compat/tls/wrap.js +544 -0
- package/dist/compat/tls-wrap/LICENSE-3RD-PARTY.md +11 -0
- package/dist/compat/tls-wrap/LICENSE-APACHE +177 -0
- package/dist/compat/tls-wrap/index.d.ts +7 -0
- package/dist/compat/tls-wrap/index.js +3 -0
- package/dist/compat/util/index.js +43 -1
- package/dist/compat/util/types/index.d.ts +3 -0
- package/dist/compat/util/types/index.js +5 -0
- package/dist/compat/zlib/LICENSE.brotli +19 -0
- package/dist/compat/zlib/LICENSE.emscripten +102 -0
- package/dist/compat/zlib/LICENSE.musl +193 -0
- package/dist/compat/zlib/LICENSE.zstd +30 -0
- package/dist/compat/zlib/compression.d.ts +55 -0
- package/dist/compat/zlib/compression.js +259 -0
- package/dist/compat/zlib/constants.d.ts +172 -0
- package/dist/compat/zlib/constants.js +173 -0
- package/dist/compat/zlib/index.d.ts +180 -0
- package/dist/compat/zlib/index.js +177 -0
- package/dist/compat/zlib/native-codecs.d.ts +17 -0
- package/dist/compat/zlib/native-codecs.js +168 -0
- package/dist/shared/globals/inject.d.ts +2 -1
- package/dist/shared/globals/inject.js +2 -1
- package/dist/shared/globals/runtime.d.ts +1 -2
- package/dist/shared/globals/runtime.js +1 -2
- package/dist/shared/request-scope.d.ts +4 -0
- package/dist/shared/request-scope.js +11 -0
- package/dist/tooling/async.js +36 -14
- package/dist/tooling/shared.d.ts +1 -0
- package/dist/tooling/shared.js +20 -1
- package/dist/tooling/vite.js +4 -3
- package/native/android/src/main/java/jp/rdlabo/workerscapacitor/WorkerSockets.java +56 -8
- package/native/android/src/main/java/jp/rdlabo/workerscapacitor/WorkersCapacitorPlugin.java +4 -2
- package/native/ios/NetworkBridge.swift +11 -1
- package/native/ios/SocketBridge.swift +37 -2
- package/native/ios/WorkersCapacitorPlugin.swift +4 -0
- package/package.json +3 -1
package/dist/compat/fs/index.js
CHANGED
|
@@ -1,255 +1,221 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
function normalize(path) {
|
|
9
|
-
if (typeof path !== "string")
|
|
10
|
-
throw new TypeError("path must be a string");
|
|
11
|
-
const raw = path.startsWith("file://") ? new URL(path).pathname : path;
|
|
12
|
-
const parts = [];
|
|
13
|
-
for (const segment of raw.split("/")) {
|
|
14
|
-
if (segment === "" || segment === ".")
|
|
15
|
-
continue;
|
|
16
|
-
if (segment === "..")
|
|
17
|
-
parts.pop();
|
|
18
|
-
else
|
|
19
|
-
parts.push(segment);
|
|
20
|
-
}
|
|
21
|
-
return "/" + parts.join("/");
|
|
22
|
-
}
|
|
23
|
-
/** Called by generated bundle code; consumers do not invoke this directly. */
|
|
24
|
-
export function registerVirtualFiles(entries) {
|
|
25
|
-
for (const [path, contents] of Object.entries(entries)) {
|
|
26
|
-
const key = normalize(path);
|
|
27
|
-
files.set(key, typeof contents === "string"
|
|
28
|
-
? encoder.encode(contents)
|
|
29
|
-
: contents instanceof Uint8Array
|
|
30
|
-
? contents
|
|
31
|
-
: Uint8Array.from(atob(contents.base64), (c) => c.charCodeAt(0)));
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
/** Test/reset helper. */
|
|
1
|
+
import { fileMode } from "./mode.js";
|
|
2
|
+
import { validateInteger } from "../crypto/validation.js";
|
|
3
|
+
import * as embedded from "./embedded.js";
|
|
4
|
+
import { temporary, temporaryPath, clearTemporaryFiles, writablePath, pathOf, } from "./writable.js";
|
|
5
|
+
import { Buffer } from "node:buffer";
|
|
6
|
+
import { validateFunction, validateObject, validateBoolean, } from "../net/validators.js";
|
|
7
|
+
export const registerVirtualFiles = embedded.registerVirtualFiles;
|
|
35
8
|
export function clearVirtualFiles() {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
return
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
return 0;
|
|
146
|
-
}
|
|
147
|
-
get rdev() {
|
|
148
|
-
return 0;
|
|
149
|
-
}
|
|
150
|
-
get blksize() {
|
|
151
|
-
return 4096;
|
|
152
|
-
}
|
|
153
|
-
get blocks() {
|
|
154
|
-
return Math.ceil(this.#size / 512);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
function statFor(path, syscall) {
|
|
158
|
-
const data = fileEntry(path);
|
|
159
|
-
if (data !== undefined)
|
|
160
|
-
return new Stats(data.length, false, new Date(0));
|
|
161
|
-
if (dirEntries(path).length > 0 || path === "/")
|
|
162
|
-
return new Stats(0, true, new Date(0));
|
|
163
|
-
throw enoent(syscall, path);
|
|
164
|
-
}
|
|
165
|
-
function toPath(input) {
|
|
166
|
-
if (input instanceof URL)
|
|
167
|
-
return normalize(input.href);
|
|
168
|
-
if (typeof input !== "string" && !(input instanceof String))
|
|
169
|
-
throw new TypeError("path must be a string or URL");
|
|
170
|
-
return normalize(String(input));
|
|
171
|
-
}
|
|
172
|
-
function decode(data, encoding) {
|
|
173
|
-
if (encoding === "utf8" || encoding === "utf-8" || encoding == null)
|
|
174
|
-
return new TextDecoder().decode(data);
|
|
175
|
-
throw new Error(`Unsupported encoding '${encoding}'`);
|
|
176
|
-
}
|
|
177
|
-
export function readFileSync(path, options) {
|
|
178
|
-
const p = toPath(path);
|
|
179
|
-
const data = fileEntry(p);
|
|
180
|
-
if (data === undefined)
|
|
181
|
-
throw enoent("open", p);
|
|
182
|
-
const encoding = typeof options === "string"
|
|
183
|
-
? options
|
|
184
|
-
: options?.encoding;
|
|
185
|
-
return encoding ? decode(data, encoding) : data;
|
|
186
|
-
}
|
|
187
|
-
export function readFile(path, options, callback) {
|
|
188
|
-
const cb = (typeof options === "function" ? options : callback);
|
|
189
|
-
const promise = Promise.resolve().then(() => readFileSync(path, options));
|
|
190
|
-
if (cb)
|
|
191
|
-
promise.then((data) => cb(null, data), (error) => cb(error));
|
|
192
|
-
}
|
|
193
|
-
export function readdirSync(path) {
|
|
194
|
-
const p = toPath(path);
|
|
195
|
-
const entries = dirEntries(p);
|
|
196
|
-
if (entries.length === 0 && fileEntry(p) === undefined && p !== "/")
|
|
197
|
-
throw enoent("scandir", p);
|
|
198
|
-
return entries;
|
|
199
|
-
}
|
|
200
|
-
export function readdir(path, callback) {
|
|
201
|
-
Promise.resolve()
|
|
202
|
-
.then(() => readdirSync(path))
|
|
203
|
-
.then((entries) => callback(null, entries), (error) => callback(error));
|
|
204
|
-
}
|
|
205
|
-
export function existsSync(path) {
|
|
206
|
-
return exists(toPath(path));
|
|
207
|
-
}
|
|
208
|
-
export function statSync(path) {
|
|
209
|
-
return statFor(toPath(path), "stat");
|
|
210
|
-
}
|
|
211
|
-
export function lstatSync(path) {
|
|
212
|
-
return statSync(path);
|
|
213
|
-
}
|
|
214
|
-
export function stat(path, callback) {
|
|
215
|
-
Promise.resolve()
|
|
216
|
-
.then(() => statSync(path))
|
|
217
|
-
.then((stats) => callback(null, stats), (error) => callback(error));
|
|
218
|
-
}
|
|
219
|
-
export function realpathSync(path) {
|
|
220
|
-
const p = toPath(path);
|
|
221
|
-
if (!exists(p))
|
|
222
|
-
throw enoent("realpath", p);
|
|
223
|
-
return p;
|
|
224
|
-
}
|
|
225
|
-
export const promises = {
|
|
226
|
-
readFile: (path, options) => Promise.resolve().then(() => readFileSync(path, options)),
|
|
227
|
-
readdir: (path) => Promise.resolve().then(() => readdirSync(path)),
|
|
228
|
-
stat: (path) => Promise.resolve().then(() => statSync(path)),
|
|
229
|
-
lstat: (path) => Promise.resolve().then(() => lstatSync(path)),
|
|
230
|
-
realpath: (path) => Promise.resolve().then(() => realpathSync(path)),
|
|
231
|
-
access: (path) => Promise.resolve().then(() => {
|
|
232
|
-
if (!exists(toPath(path)))
|
|
233
|
-
throw enoent("access", toPath(path));
|
|
234
|
-
}),
|
|
9
|
+
embedded.clearVirtualFiles();
|
|
10
|
+
clearTemporaryFiles();
|
|
11
|
+
}
|
|
12
|
+
const reads = new Set([
|
|
13
|
+
"readFileSync",
|
|
14
|
+
"readFile",
|
|
15
|
+
"readdirSync",
|
|
16
|
+
"readdir",
|
|
17
|
+
"statSync",
|
|
18
|
+
"stat",
|
|
19
|
+
"lstatSync",
|
|
20
|
+
"realpathSync",
|
|
21
|
+
"existsSync",
|
|
22
|
+
]);
|
|
23
|
+
const api = { ...temporary };
|
|
24
|
+
for (const name of Object.keys(api))
|
|
25
|
+
if (typeof api[name] === "function" && /^[a-z]/.test(name))
|
|
26
|
+
api[name] = (...args) => temporary[name](...args);
|
|
27
|
+
for (const name of [
|
|
28
|
+
"ReadStream",
|
|
29
|
+
"WriteStream",
|
|
30
|
+
"FileReadStream",
|
|
31
|
+
"FileWriteStream",
|
|
32
|
+
]) {
|
|
33
|
+
api[name] = new Proxy(api[name], {
|
|
34
|
+
construct(_target, args) {
|
|
35
|
+
args[0] = writablePath(args[0], name);
|
|
36
|
+
return Reflect.construct(temporary[name], args);
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
for (const name of reads) {
|
|
41
|
+
const old = embedded[name];
|
|
42
|
+
api[name] = (...args) => temporaryPath(args[0]) ? temporary[name](...args) : old(...args);
|
|
43
|
+
}
|
|
44
|
+
for (const name of ["watch", "watchFile", "unwatchFile"])
|
|
45
|
+
api[name] = () => {
|
|
46
|
+
throw Object.assign(new Error("The operation is not supported"), {
|
|
47
|
+
code: "ERR_UNSUPPORTED_OPERATION",
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
// Workers reports the byte count from these synchronous operations.
|
|
51
|
+
for (const name of ["writeFileSync", "appendFileSync"])
|
|
52
|
+
api[name] = (...args) => {
|
|
53
|
+
temporary[name](...args);
|
|
54
|
+
const encoding = typeof args[2] === "string" ? args[2] : args[2]?.encoding;
|
|
55
|
+
return typeof args[1] === "string"
|
|
56
|
+
? Buffer.byteLength(args[1], encoding)
|
|
57
|
+
: args[1].byteLength;
|
|
58
|
+
};
|
|
59
|
+
const promises = { ...temporary.promises };
|
|
60
|
+
for (const name of Object.keys(promises))
|
|
61
|
+
if (typeof promises[name] === "function" && /^[a-z]/.test(name))
|
|
62
|
+
promises[name] = (...args) => temporary.promises[name](...args);
|
|
63
|
+
for (const name of [
|
|
64
|
+
"readFile",
|
|
65
|
+
"readdir",
|
|
66
|
+
"stat",
|
|
67
|
+
"lstat",
|
|
68
|
+
"realpath",
|
|
69
|
+
"access",
|
|
70
|
+
]) {
|
|
71
|
+
const old = embedded.promises[name];
|
|
72
|
+
promises[name] = (...args) => temporaryPath(args[0]) ? temporary.promises[name](...args) : old(...args);
|
|
73
|
+
}
|
|
74
|
+
// Keep all writes inside the memory volume, including FileHandle operations.
|
|
75
|
+
for (const name of [
|
|
76
|
+
"appendFile",
|
|
77
|
+
"writeFile",
|
|
78
|
+
"mkdir",
|
|
79
|
+
"mkdtemp",
|
|
80
|
+
"rm",
|
|
81
|
+
"rmdir",
|
|
82
|
+
"unlink",
|
|
83
|
+
"truncate",
|
|
84
|
+
"utimes",
|
|
85
|
+
"lutimes",
|
|
86
|
+
"chmod",
|
|
87
|
+
"chown",
|
|
88
|
+
"lchmod",
|
|
89
|
+
"lchown",
|
|
90
|
+
"open",
|
|
91
|
+
"rename",
|
|
92
|
+
"copyFile",
|
|
93
|
+
"cp",
|
|
94
|
+
"link",
|
|
95
|
+
"symlink",
|
|
96
|
+
]) {
|
|
97
|
+
promises[name] = async (...args) => {
|
|
98
|
+
if (name === "symlink") {
|
|
99
|
+
return new Promise((resolve, reject) => temporary.symlink(...args, (error) => error ? reject(error) : resolve(undefined)));
|
|
100
|
+
}
|
|
101
|
+
const indexes = ["rename", "copyFile", "cp", "link"].includes(name)
|
|
102
|
+
? [0, 1]
|
|
103
|
+
: [0];
|
|
104
|
+
for (const i of indexes)
|
|
105
|
+
if (typeof args[i] !== "object" ||
|
|
106
|
+
args[i] instanceof URL ||
|
|
107
|
+
Buffer.isBuffer(args[i])) {
|
|
108
|
+
if (!temporaryPath(args[i]))
|
|
109
|
+
throw Object.assign(new Error("EROFS: read-only file system"), {
|
|
110
|
+
code: "EROFS",
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
return temporary.promises[name](...args);
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
promises.watch = async function* () {
|
|
117
|
+
api.watch();
|
|
235
118
|
};
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
119
|
+
// Workers validates ownership/permission requests but has no mutable Unix
|
|
120
|
+
// ownership or permissions. These operations must not change the memory VFS.
|
|
121
|
+
function installOperation(name, handler, promise = true) {
|
|
122
|
+
api[name + "Sync"] = handler;
|
|
123
|
+
api[name] = (...args) => {
|
|
124
|
+
const callback = args.pop();
|
|
125
|
+
validateFunction(callback, "callback");
|
|
126
|
+
Promise.resolve()
|
|
127
|
+
.then(() => handler(...args))
|
|
128
|
+
.then((value) => callback(null, value), (error) => callback(error));
|
|
129
|
+
};
|
|
130
|
+
if (promise)
|
|
131
|
+
promises[name] = async (...args) => handler(...args);
|
|
132
|
+
}
|
|
133
|
+
function temporaryStats(stat) {
|
|
134
|
+
if (stat == null)
|
|
135
|
+
return stat;
|
|
136
|
+
const bigint = typeof stat.mode === "bigint";
|
|
137
|
+
const mode = (Number(stat.mode) & ~0o777) | 0o666;
|
|
138
|
+
stat.mode = bigint ? BigInt(mode) : mode;
|
|
139
|
+
stat.uid = stat.gid = bigint ? 0n : 0;
|
|
140
|
+
return stat;
|
|
141
|
+
}
|
|
142
|
+
for (const name of ["stat", "lstat"])
|
|
143
|
+
installOperation(name, (path, options) => temporaryPath(path)
|
|
144
|
+
? temporaryStats(temporary[name + "Sync"](path, options))
|
|
145
|
+
: embedded.statSync(path));
|
|
146
|
+
installOperation("fstat", (...args) => temporaryStats(temporary.fstatSync(...args)), false);
|
|
147
|
+
for (const name of ["chmod", "lchmod", "fchmod", "chown", "lchown", "fchown"]) {
|
|
148
|
+
installOperation(name, (path, value, gid) => {
|
|
149
|
+
if (name.endsWith("chmod"))
|
|
150
|
+
fileMode(value);
|
|
151
|
+
else {
|
|
152
|
+
validateInteger(value, "uid", -1, 0xffffffff);
|
|
153
|
+
validateInteger(gid, "gid", -1, 0xffffffff);
|
|
154
|
+
}
|
|
155
|
+
if (name.startsWith("f")) {
|
|
156
|
+
validateInteger(path, "fd", 0, 2147483647);
|
|
157
|
+
temporary.fstatSync(path);
|
|
158
|
+
}
|
|
159
|
+
else if (name.startsWith("l") && temporaryPath(path))
|
|
160
|
+
temporary.lstatSync(path);
|
|
161
|
+
else
|
|
162
|
+
api.statSync(path);
|
|
163
|
+
}, !name.startsWith("f"));
|
|
164
|
+
}
|
|
165
|
+
installOperation("statfs", (path, options = {}) => {
|
|
166
|
+
pathOf(path);
|
|
167
|
+
validateObject(options, "options");
|
|
168
|
+
const bigint = options.bigint ?? false;
|
|
169
|
+
validateBoolean(bigint, "options.bigint");
|
|
170
|
+
const value = bigint ? 0n : 0;
|
|
171
|
+
return {
|
|
172
|
+
type: value,
|
|
173
|
+
bsize: value,
|
|
174
|
+
blocks: value,
|
|
175
|
+
bfree: value,
|
|
176
|
+
bavail: value,
|
|
177
|
+
files: value,
|
|
178
|
+
ffree: value,
|
|
179
|
+
};
|
|
180
|
+
});
|
|
181
|
+
const openPromise = promises.open;
|
|
182
|
+
promises.open = async (...args) => {
|
|
183
|
+
fileMode(args[2], 0o666);
|
|
184
|
+
args[2] = 0o666;
|
|
185
|
+
const handle = await openPromise(...args);
|
|
186
|
+
const stat = handle.stat.bind(handle);
|
|
187
|
+
handle.stat = async (...options) => temporaryStats(await stat(...options));
|
|
188
|
+
handle.chmod = async (mode) => {
|
|
189
|
+
fileMode(mode);
|
|
190
|
+
await stat();
|
|
191
|
+
};
|
|
192
|
+
handle.chown = async (uid, gid) => {
|
|
193
|
+
validateInteger(uid, "uid", -1, 0xffffffff);
|
|
194
|
+
validateInteger(gid, "gid", -1, 0xffffffff);
|
|
195
|
+
await stat();
|
|
196
|
+
};
|
|
197
|
+
return handle;
|
|
255
198
|
};
|
|
199
|
+
// Copy an embedded bundle file into request-local /tmp without granting any
|
|
200
|
+
// write access to its source volume.
|
|
201
|
+
const temporaryCopyFile = api.copyFileSync;
|
|
202
|
+
installOperation("copyFile", (source, destination, mode = 0) => {
|
|
203
|
+
if (![0, 1, 2, 4].includes(mode))
|
|
204
|
+
throw Object.assign(new TypeError("Invalid copy mode"), {
|
|
205
|
+
code: "ERR_INVALID_ARG_VALUE",
|
|
206
|
+
});
|
|
207
|
+
if (mode === 4)
|
|
208
|
+
throw Object.assign(new Error("The operation is not supported"), {
|
|
209
|
+
code: "ERR_UNSUPPORTED_OPERATION",
|
|
210
|
+
});
|
|
211
|
+
if (temporaryPath(source))
|
|
212
|
+
return temporaryCopyFile(source, destination, mode);
|
|
213
|
+
writablePath(destination, "copyFile");
|
|
214
|
+
if (mode === 1 && api.existsSync(destination))
|
|
215
|
+
throw Object.assign(new Error("File already exists"), { code: "EEXIST" });
|
|
216
|
+
temporary.writeFileSync(destination, embedded.readFileSync(pathOf(source)));
|
|
217
|
+
});
|
|
218
|
+
api.promises = promises;
|
|
219
|
+
export { promises };
|
|
220
|
+
export const { Dir, Dirent, F_OK, FileReadStream, FileWriteStream, R_OK, ReadStream, Stats, W_OK, WriteStream, X_OK, access, accessSync, appendFile, appendFileSync, chmod, chmodSync, chown, chownSync, close, closeSync, constants, copyFile, copyFileSync, cp, cpSync, createReadStream, createWriteStream, exists, existsSync, fchmod, fchmodSync, fchown, fchownSync, fdatasync, fdatasyncSync, fstat, fstatSync, fsync, fsyncSync, ftruncate, ftruncateSync, futimes, futimesSync, glob, globSync, lchmod, lchmodSync, lchown, lchownSync, link, linkSync, lstat, lstatSync, lutimes, lutimesSync, mkdir, mkdirSync, mkdtemp, mkdtempSync, open, openAsBlob, openSync, opendir, opendirSync, read, readFile, readFileSync, readSync, readdir, readdirSync, readlink, readlinkSync, readv, readvSync, realpath, realpathSync, rename, renameSync, rm, rmSync, rmdir, rmdirSync, stat, statSync, statfs, statfsSync, symlink, symlinkSync, truncate, truncateSync, unlink, unlinkSync, unwatchFile, utimes, utimesSync, watch, watchFile, write, writeFile, writeFileSync, writeSync, writev, writevSync, } = api;
|
|
221
|
+
export default api;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function fileMode(value: unknown, fallback?: number): number;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Pinned Workers mode validation (node/internal/validators.ts).
|
|
2
|
+
import { validateUint32 } from "../net/validators.js";
|
|
3
|
+
import { ERR_INVALID_ARG_VALUE } from "../net/errors.js";
|
|
4
|
+
export function fileMode(value, fallback) {
|
|
5
|
+
value ??= fallback;
|
|
6
|
+
if (typeof value === "string") {
|
|
7
|
+
if (!/^[0-7]+$/.test(value))
|
|
8
|
+
throw new ERR_INVALID_ARG_VALUE("mode", value, "must be a 32-bit unsigned integer or an octal string");
|
|
9
|
+
value = Number.parseInt(value, 8);
|
|
10
|
+
}
|
|
11
|
+
validateUint32(value, "mode");
|
|
12
|
+
return value;
|
|
13
|
+
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { promises } from "../index.js";
|
|
2
|
-
export declare const
|
|
2
|
+
export declare const FileHandle: any, access: any, appendFile: any, chmod: any, chown: any, constants: any, copyFile: any, cp: any, glob: any, lchmod: any, lchown: any, link: any, lstat: any, lutimes: any, mkdir: any, mkdtemp: any, open: any, opendir: any, readFile: any, readdir: any, readlink: any, realpath: any, rename: any, rm: any, rmdir: any, stat: any, statfs: any, symlink: any, truncate: any, unlink: any, utimes: any, watch: any, writeFile: any;
|
|
3
3
|
export default promises;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { promises } from "../index.js";
|
|
2
|
-
export const { readFile, readdir, stat,
|
|
2
|
+
export const { FileHandle, access, appendFile, chmod, chown, constants, copyFile, cp, glob, lchmod, lchown, link, lstat, lutimes, mkdir, mkdtemp, open, opendir, readFile, readdir, readlink, realpath, rename, rm, rmdir, stat, statfs, symlink, truncate, unlink, utimes, watch, writeFile, } = promises;
|
|
3
3
|
export default promises;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare function pathOf(value: any): string;
|
|
2
|
+
export declare function temporaryPath(value: any): boolean;
|
|
3
|
+
export declare function writablePath(value: any, syscall: string): string | number;
|
|
4
|
+
export declare const temporary: any;
|
|
5
|
+
export declare function clearTemporaryFiles(): void;
|
|
6
|
+
export declare function runWithTemporaryFiles<T>(callback: () => T): T;
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { fileMode } from "./mode.js";
|
|
2
|
+
import { registerRequestScope } from "../../shared/request-scope.js";
|
|
3
|
+
// Writable, memory-only /tmp. Embedded bundle files remain read-only.
|
|
4
|
+
// This adapter does not expose the device filesystem.
|
|
5
|
+
import { Volume, createFsFromVolume } from "memfs";
|
|
6
|
+
import { Buffer } from "node:buffer";
|
|
7
|
+
import { posix } from "node:path";
|
|
8
|
+
import { ERR_INVALID_ARG_TYPE } from "../net/errors.js";
|
|
9
|
+
import { AsyncLocalStorage } from "../async-hooks/index.js";
|
|
10
|
+
export function pathOf(value) {
|
|
11
|
+
if (value instanceof URL)
|
|
12
|
+
value = decodeURIComponent(value.pathname);
|
|
13
|
+
if (Buffer.isBuffer(value))
|
|
14
|
+
value = value.toString();
|
|
15
|
+
if (typeof value !== "string")
|
|
16
|
+
throw new ERR_INVALID_ARG_TYPE("path", ["string", "Buffer", "URL"], value);
|
|
17
|
+
return posix.resolve("/bundle", value);
|
|
18
|
+
}
|
|
19
|
+
export function temporaryPath(value) {
|
|
20
|
+
if (typeof value === "number")
|
|
21
|
+
return true;
|
|
22
|
+
const path = pathOf(value);
|
|
23
|
+
return path === "/tmp" || path.startsWith("/tmp/");
|
|
24
|
+
}
|
|
25
|
+
export function writablePath(value, syscall) {
|
|
26
|
+
if (typeof value === "number")
|
|
27
|
+
return value;
|
|
28
|
+
const path = pathOf(value);
|
|
29
|
+
if (!(path === "/tmp" || path.startsWith("/tmp/")))
|
|
30
|
+
throw Object.assign(new Error(`EROFS: read-only file system, ${syscall} '${path}'`), { code: "EROFS", errno: -30, syscall, path });
|
|
31
|
+
return path;
|
|
32
|
+
}
|
|
33
|
+
function createTemporary() {
|
|
34
|
+
const volume = new Volume();
|
|
35
|
+
volume.mkdirSync("/tmp");
|
|
36
|
+
const fs = createFsFromVolume(volume);
|
|
37
|
+
const mutating = new Set([
|
|
38
|
+
"appendFile",
|
|
39
|
+
"writeFile",
|
|
40
|
+
"mkdir",
|
|
41
|
+
"mkdtemp",
|
|
42
|
+
"rm",
|
|
43
|
+
"rmdir",
|
|
44
|
+
"unlink",
|
|
45
|
+
"truncate",
|
|
46
|
+
"utimes",
|
|
47
|
+
"lutimes",
|
|
48
|
+
"chmod",
|
|
49
|
+
"chown",
|
|
50
|
+
"lchmod",
|
|
51
|
+
"lchown",
|
|
52
|
+
]);
|
|
53
|
+
const api = {};
|
|
54
|
+
for (const name of Object.keys(fs)) {
|
|
55
|
+
if (typeof fs[name] !== "function") {
|
|
56
|
+
api[name] = fs[name];
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
if (/^[A-Z]/.test(name)) {
|
|
60
|
+
api[name] = fs[name];
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
const base = name.replace(/Sync$/, "");
|
|
64
|
+
api[name] = (...args) => {
|
|
65
|
+
try {
|
|
66
|
+
if (["writeFile", "appendFile"].includes(base) &&
|
|
67
|
+
typeof args[0] !== "number") {
|
|
68
|
+
const opts = args[2];
|
|
69
|
+
if (typeof opts === "object" && opts !== null) {
|
|
70
|
+
fileMode(opts.mode, 0o666);
|
|
71
|
+
args[2] = { ...opts, mode: 0o666 };
|
|
72
|
+
}
|
|
73
|
+
else if (typeof opts === "function")
|
|
74
|
+
args.splice(2, 0, { mode: 0o666 });
|
|
75
|
+
else if (opts === undefined || typeof opts === "string")
|
|
76
|
+
args[2] = {
|
|
77
|
+
mode: 0o666,
|
|
78
|
+
...(typeof opts === "string" ? { encoding: opts } : {}),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
if (base === "symlink") {
|
|
82
|
+
args[1] = writablePath(args[1], base);
|
|
83
|
+
const target = Buffer.isBuffer(args[0])
|
|
84
|
+
? args[0].toString()
|
|
85
|
+
: args[0];
|
|
86
|
+
if (typeof target !== "string")
|
|
87
|
+
throw new ERR_INVALID_ARG_TYPE("target", ["string", "Buffer"], target);
|
|
88
|
+
writablePath(posix.resolve(posix.dirname(args[1]), target), base);
|
|
89
|
+
}
|
|
90
|
+
else if (["rename", "copyFile", "cp", "link"].includes(base)) {
|
|
91
|
+
args[0] = writablePath(args[0], base);
|
|
92
|
+
args[1] = writablePath(args[1], base);
|
|
93
|
+
}
|
|
94
|
+
else if (mutating.has(base) ||
|
|
95
|
+
base === "open" ||
|
|
96
|
+
base === "createWriteStream")
|
|
97
|
+
args[0] = writablePath(args[0], base);
|
|
98
|
+
else if (typeof args[0] === "string" ||
|
|
99
|
+
args[0] instanceof URL ||
|
|
100
|
+
Buffer.isBuffer(args[0]))
|
|
101
|
+
args[0] = pathOf(args[0]);
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
const callback = args[args.length - 1];
|
|
105
|
+
if (!name.endsWith("Sync") && typeof callback === "function") {
|
|
106
|
+
queueMicrotask(() => callback(error));
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
throw error;
|
|
110
|
+
}
|
|
111
|
+
return fs[name](...args);
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
api,
|
|
116
|
+
clear() {
|
|
117
|
+
for (const name of fs.readdirSync("/tmp"))
|
|
118
|
+
fs.rmSync(`/tmp/${name}`, { recursive: true, force: true });
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
const contexts = new AsyncLocalStorage();
|
|
123
|
+
const fallback = createTemporary();
|
|
124
|
+
const active = () => contexts.getStore() ?? fallback;
|
|
125
|
+
export const temporary = new Proxy({}, {
|
|
126
|
+
get(_target, name) {
|
|
127
|
+
return active().api[name];
|
|
128
|
+
},
|
|
129
|
+
ownKeys() {
|
|
130
|
+
return Reflect.ownKeys(fallback.api);
|
|
131
|
+
},
|
|
132
|
+
getOwnPropertyDescriptor() {
|
|
133
|
+
return { enumerable: true, configurable: true };
|
|
134
|
+
},
|
|
135
|
+
});
|
|
136
|
+
export function clearTemporaryFiles() {
|
|
137
|
+
active().clear();
|
|
138
|
+
}
|
|
139
|
+
export function runWithTemporaryFiles(callback) {
|
|
140
|
+
return contexts.run(createTemporary(), callback);
|
|
141
|
+
}
|
|
142
|
+
registerRequestScope(runWithTemporaryFiles);
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const kIncomingMessage: unique symbol;
|
|
2
|
+
export declare const kServerResponse: unique symbol;
|
|
3
|
+
export declare const kUniqueHeaders: unique symbol;
|
|
4
|
+
export declare const kHighWaterMark: unique symbol;
|
|
5
|
+
export declare const kConnectionsCheckingInterval: unique symbol;
|