cf-workers-native 0.1.4 → 0.1.6
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/README.md +2 -2
- 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/headers/index.d.ts +2 -0
- package/dist/browser/headers/index.js +5 -0
- package/dist/browser/isolated/transport.js +3 -2
- package/dist/browser/request/index.js +3 -2
- package/dist/browser/response/index.d.ts +2 -0
- package/dist/browser/response/index.js +11 -3
- package/dist/browser/structured-clone/index.d.ts +3 -0
- package/dist/browser/structured-clone/index.js +75 -7
- package/dist/browser/websocket/attachment-size.d.ts +3 -0
- package/dist/browser/websocket/attachment-size.js +336 -0
- package/dist/browser/websocket/brand.d.ts +4 -0
- package/dist/browser/websocket/brand.js +16 -0
- package/dist/browser/websocket/close-event.d.ts +7 -0
- package/dist/browser/websocket/close-event.js +30 -0
- package/dist/browser/websocket/compression.d.ts +5 -0
- package/dist/browser/websocket/compression.js +124 -0
- package/dist/browser/websocket/conversion.d.ts +2 -0
- package/dist/browser/websocket/conversion.js +13 -0
- package/dist/browser/websocket/endpoint.d.ts +39 -0
- package/dist/browser/websocket/endpoint.js +379 -0
- package/dist/browser/websocket/frames.d.ts +21 -0
- package/dist/browser/websocket/frames.js +121 -0
- package/dist/browser/websocket/native.d.ts +14 -0
- package/dist/browser/websocket/native.js +226 -0
- package/dist/cloudflare/apis/cloudflare-node/index.js +11 -23
- 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 +39 -0
- package/dist/compat/crypto/LICENSE-APACHE +177 -0
- package/dist/compat/crypto/LICENSE.boringssl +238 -0
- package/dist/compat/crypto/LICENSE.emscripten +102 -0
- package/dist/compat/crypto/LICENSE.musl +193 -0
- package/dist/compat/crypto/LICENSE.ncrypto +21 -0
- package/dist/compat/crypto/LICENSE.openssl +177 -0
- package/dist/compat/crypto/asymmetric.d.ts +13 -0
- package/dist/compat/crypto/asymmetric.js +312 -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/cipher-handle.d.ts +27 -0
- package/dist/compat/crypto/cipher-handle.js +239 -0
- package/dist/compat/crypto/cipher-info.d.ts +12 -0
- package/dist/compat/crypto/cipher-info.js +324 -0
- package/dist/compat/crypto/constants.d.ts +62 -0
- package/dist/compat/crypto/constants.js +68 -0
- package/dist/compat/crypto/dh-handle.d.ts +17 -0
- package/dist/compat/crypto/dh-handle.js +180 -0
- package/dist/compat/crypto/dh.d.ts +53 -0
- package/dist/compat/crypto/dh.js +138 -0
- package/dist/compat/crypto/digest-stream.d.ts +15 -0
- package/dist/compat/crypto/digest-stream.js +213 -0
- package/dist/compat/crypto/digests.d.ts +3 -0
- package/dist/compat/crypto/digests.js +32 -0
- package/dist/compat/crypto/ecdh.d.ts +25 -0
- package/dist/compat/crypto/ecdh.js +109 -0
- package/dist/compat/crypto/hash-handle.d.ts +23 -0
- package/dist/compat/crypto/hash-handle.js +70 -0
- package/dist/compat/crypto/hash.d.ts +37 -0
- package/dist/compat/crypto/hash.js +218 -0
- package/dist/compat/crypto/hkdf.d.ts +2 -0
- package/dist/compat/crypto/hkdf.js +78 -0
- package/dist/compat/crypto/index.cjs +2 -0
- package/dist/compat/crypto/index.d.cts +169 -0
- package/dist/compat/crypto/index.d.ts +213 -0
- package/dist/compat/crypto/index.js +96 -0
- package/dist/compat/crypto/key-errors.d.ts +10 -0
- package/dist/compat/crypto/key-errors.js +17 -0
- package/dist/compat/crypto/key-export.d.ts +8 -0
- package/dist/compat/crypto/key-export.js +49 -0
- package/dist/compat/crypto/key-formats.d.ts +7 -0
- package/dist/compat/crypto/key-formats.js +251 -0
- package/dist/compat/crypto/key-options.d.ts +1 -0
- package/dist/compat/crypto/key-options.js +74 -0
- package/dist/compat/crypto/key-state.d.ts +10 -0
- package/dist/compat/crypto/key-state.js +4 -0
- package/dist/compat/crypto/keys.d.ts +58 -0
- package/dist/compat/crypto/keys.js +278 -0
- package/dist/compat/crypto/metadata.d.ts +13 -0
- package/dist/compat/crypto/metadata.js +109 -0
- package/dist/compat/crypto/native-cipher.d.ts +17 -0
- package/dist/compat/crypto/native-cipher.js +192 -0
- package/dist/compat/crypto/native-crypto.d.ts +81 -0
- package/dist/compat/crypto/native-crypto.js +681 -0
- package/dist/compat/crypto/pbkdf2-handle.d.ts +4 -0
- package/dist/compat/crypto/pbkdf2-handle.js +26 -0
- package/dist/compat/crypto/pbkdf2.d.ts +5 -0
- package/dist/compat/crypto/pbkdf2.js +60 -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 +18 -0
- package/dist/compat/crypto/random.js +203 -0
- package/dist/compat/crypto/rsa.d.ts +21 -0
- package/dist/compat/crypto/rsa.js +177 -0
- package/dist/compat/crypto/scrypt.d.ts +25 -0
- package/dist/compat/crypto/scrypt.js +152 -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 +149 -0
- package/dist/compat/crypto/signatures.d.ts +34 -0
- package/dist/compat/crypto/signatures.js +252 -0
- package/dist/compat/crypto/subtle-digest.d.ts +1 -0
- package/dist/compat/crypto/subtle-digest.js +56 -0
- package/dist/compat/crypto/symmetric.d.ts +45 -0
- package/dist/compat/crypto/symmetric.js +239 -0
- package/dist/compat/crypto/validation.d.ts +7 -0
- package/dist/compat/crypto/validation.js +46 -0
- package/dist/compat/crypto/webcrypto.d.ts +13 -0
- package/dist/compat/crypto/webcrypto.js +51 -0
- package/dist/compat/crypto/x509.d.ts +30 -0
- package/dist/compat/crypto/x509.js +407 -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/LICENSE-3RD-PARTY.md +11 -0
- package/dist/compat/fs/LICENSE-APACHE +177 -0
- package/dist/compat/fs/directory-backend.d.ts +6 -0
- package/dist/compat/fs/directory-backend.js +51 -0
- package/dist/compat/fs/directory.d.ts +37 -0
- package/dist/compat/fs/directory.js +137 -0
- package/dist/compat/fs/embedded.d.ts +85 -0
- package/dist/compat/fs/embedded.js +260 -0
- package/dist/compat/fs/file-handle.d.ts +75 -0
- package/dist/compat/fs/file-handle.js +226 -0
- package/dist/compat/fs/glob-internal.d.ts +15 -0
- package/dist/compat/fs/glob-internal.js +417 -0
- package/dist/compat/fs/glob.d.ts +10 -0
- package/dist/compat/fs/glob.js +71 -0
- package/dist/compat/fs/index.d.ts +8 -82
- package/dist/compat/fs/index.js +736 -238
- package/dist/compat/fs/io-validation.d.ts +14 -0
- package/dist/compat/fs/io-validation.js +130 -0
- package/dist/compat/fs/io.d.ts +1 -0
- package/dist/compat/fs/io.js +282 -0
- 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 +3 -1
- package/dist/compat/fs/promises/index.js +1 -1
- package/dist/compat/fs/public-types.d.ts +120 -0
- package/dist/compat/fs/public-types.js +1 -0
- package/dist/compat/fs/stream-errors.d.ts +6 -0
- package/dist/compat/fs/stream-errors.js +8 -0
- package/dist/compat/fs/stream-utils.d.ts +16 -0
- package/dist/compat/fs/stream-utils.js +29 -0
- package/dist/compat/fs/streams.d.ts +104 -0
- package/dist/compat/fs/streams.js +794 -0
- package/dist/compat/fs/writable.d.ts +14 -0
- package/dist/compat/fs/writable.js +532 -0
- package/dist/compat/http/index.d.ts +11 -28
- package/dist/compat/http/index.js +81 -125
- package/dist/compat/http/server-options.d.ts +1 -0
- package/dist/compat/http/server-options.js +87 -0
- package/dist/compat/http/symbols.d.ts +5 -0
- package/dist/compat/http/symbols.js +9 -0
- package/dist/compat/http/unique-headers.d.ts +1 -0
- package/dist/compat/http/unique-headers.js +21 -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 +14 -0
- package/dist/compat/http-outgoing/index.js +17 -0
- package/dist/compat/http-server/index.d.ts +18 -0
- package/dist/compat/http-server/index.js +26 -0
- package/dist/compat/https/index.d.ts +4 -2
- 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 +153 -14
- package/dist/compat/net/validators.d.ts +1 -0
- package/dist/compat/net/validators.js +8 -0
- package/dist/compat/process/configuration.d.ts +113 -0
- package/dist/compat/process/configuration.js +115 -0
- package/dist/compat/process/directory.d.ts +4 -0
- package/dist/compat/process/directory.js +28 -0
- package/dist/compat/process/files.d.ts +3 -0
- package/dist/compat/process/files.js +10 -0
- package/dist/compat/process/index.cjs +2 -0
- package/dist/compat/process/index.d.cts +4 -0
- package/dist/compat/process/index.d.ts +20 -0
- package/dist/compat/process/index.js +113 -0
- package/dist/compat/process/metadata.d.ts +135 -0
- package/dist/compat/process/metadata.js +251 -0
- package/dist/compat/process/scheduling.d.ts +2 -0
- package/dist/compat/process/scheduling.js +60 -0
- package/dist/compat/process/state.d.ts +4 -0
- package/dist/compat/process/state.js +5 -0
- package/dist/compat/process/stdio.d.ts +18 -0
- package/dist/compat/process/stdio.js +66 -0
- package/dist/compat/stream/destroy.d.ts +1 -0
- package/dist/compat/stream/destroy.js +21 -1
- package/dist/compat/stream/readable.js +10 -2
- 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 +196 -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 +541 -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/inspect.js +5 -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-3RD-PARTY.md +11 -0
- package/dist/compat/zlib/LICENSE-APACHE +177 -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.zlib +22 -0
- package/dist/compat/zlib/LICENSE.zstd +30 -0
- package/dist/compat/zlib/classic-base.cjs +600 -0
- package/dist/compat/zlib/classic-base.d.cts +39 -0
- package/dist/compat/zlib/classic-binding.d.ts +28 -0
- package/dist/compat/zlib/classic-binding.js +105 -0
- package/dist/compat/zlib/classic.d.ts +44 -0
- package/dist/compat/zlib/classic.js +199 -0
- package/dist/compat/zlib/codec-binding.d.ts +33 -0
- package/dist/compat/zlib/codec-binding.js +141 -0
- package/dist/compat/zlib/compression.d.ts +26 -0
- package/dist/compat/zlib/compression.js +170 -0
- package/dist/compat/zlib/constants.d.ts +172 -0
- package/dist/compat/zlib/constants.js +173 -0
- package/dist/compat/zlib/errors.d.ts +21 -0
- package/dist/compat/zlib/errors.js +32 -0
- package/dist/compat/zlib/index.d.ts +544 -0
- package/dist/compat/zlib/index.js +194 -0
- package/dist/compat/zlib/input.d.ts +16 -0
- package/dist/compat/zlib/input.js +132 -0
- package/dist/compat/zlib/native-codecs.d.ts +24 -0
- package/dist/compat/zlib/native-codecs.js +180 -0
- package/dist/compat/zlib/native-zlib.d.ts +5 -0
- package/dist/compat/zlib/native-zlib.js +52 -0
- package/dist/compat/zlib/public-types.d.ts +48 -0
- package/dist/compat/zlib/public-types.js +1 -0
- package/dist/compat/zlib/validators.d.ts +3 -0
- package/dist/compat/zlib/validators.js +18 -0
- package/dist/shared/buffer-source.d.ts +5 -0
- package/dist/shared/buffer-source.js +70 -0
- package/dist/shared/globals/inject.d.ts +2 -1
- package/dist/shared/globals/inject.js +2 -1
- package/dist/shared/globals/platform.d.ts +8 -0
- package/dist/shared/globals/platform.js +1 -0
- package/dist/shared/globals/runtime.d.ts +1 -2
- package/dist/shared/globals/runtime.js +1 -2
- package/dist/shared/globals/web.js +5 -0
- package/dist/shared/request-context.d.ts +2 -0
- package/dist/shared/request-context.js +11 -0
- package/dist/shared/request-scope.d.ts +4 -0
- package/dist/shared/request-scope.js +14 -0
- package/dist/tooling/async.js +36 -14
- package/dist/tooling/shared.d.ts +1 -0
- package/dist/tooling/shared.js +26 -1
- package/dist/tooling/vite.js +8 -3
- package/native/android/src/main/java/jp/rdlabo/workerscapacitor/WorkerSockets.java +92 -13
- 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 +4 -2
|
@@ -0,0 +1,794 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
// Ported from workerd@5358e3d337336eaf696a48bc1563d808b10feba5 internal_fs_streams.ts.
|
|
3
|
+
// Copyright (c) 2026 Cloudflare, Inc.
|
|
4
|
+
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
|
|
5
|
+
// https://opensource.org/licenses/Apache-2.0
|
|
6
|
+
import { AsyncLocalStorage } from "../async-hooks/index.js";
|
|
7
|
+
function bindFs(operations) {
|
|
8
|
+
const run = AsyncLocalStorage.snapshot();
|
|
9
|
+
return Object.fromEntries(Object.entries(operations).map(([name, fn]) => [
|
|
10
|
+
name,
|
|
11
|
+
(...args) => run(() => Reflect.apply(fn, operations, args)),
|
|
12
|
+
]));
|
|
13
|
+
}
|
|
14
|
+
import { Readable } from "../stream/readable.js";
|
|
15
|
+
import { Writable } from "../stream/writable.js";
|
|
16
|
+
import { Buffer } from "node:buffer";
|
|
17
|
+
import { normalizePath, getValidatedFd, isFileHandle, } from "./stream-utils.js";
|
|
18
|
+
import { toPathIfFileURL } from "./stream-utils.js";
|
|
19
|
+
import * as fs from "./index.js";
|
|
20
|
+
import { parseFileMode, validateAbortSignal, validateBoolean, validateFunction, validateObject, validateString, validateUint32, validateThisInternalField, } from "./stream-utils.js";
|
|
21
|
+
import { errorOrDestroy } from "../stream/destroy.js";
|
|
22
|
+
import { eos } from "../stream/end-of-stream.js";
|
|
23
|
+
import { ERR_INVALID_ARG_VALUE, ERR_OUT_OF_RANGE, ERR_MISSING_ARGS, ERR_METHOD_NOT_IMPLEMENTED, ERR_STREAM_DESTROYED, ERR_SYSTEM_ERROR, } from "./stream-errors.js";
|
|
24
|
+
const reportError = (error) => {
|
|
25
|
+
if (typeof globalThis.reportError === "function")
|
|
26
|
+
globalThis.reportError(error);
|
|
27
|
+
else
|
|
28
|
+
queueMicrotask(() => {
|
|
29
|
+
throw error;
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
// Temporary while developing...
|
|
33
|
+
/* eslint-disable */
|
|
34
|
+
let lazyFs;
|
|
35
|
+
async function getLazyFs() {
|
|
36
|
+
if (lazyFs == undefined) {
|
|
37
|
+
lazyFs = {
|
|
38
|
+
open: (...args) => {
|
|
39
|
+
fs.open(...args);
|
|
40
|
+
},
|
|
41
|
+
close: (...args) => {
|
|
42
|
+
fs.close(...args);
|
|
43
|
+
},
|
|
44
|
+
fsync: (...args) => {
|
|
45
|
+
fs.fsync(...args);
|
|
46
|
+
},
|
|
47
|
+
read: (...args) => {
|
|
48
|
+
fs.read(...args);
|
|
49
|
+
},
|
|
50
|
+
write: (...args) => {
|
|
51
|
+
fs.write(...args);
|
|
52
|
+
},
|
|
53
|
+
writev: (...args) => {
|
|
54
|
+
fs.writev(...args);
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
return lazyFs;
|
|
59
|
+
}
|
|
60
|
+
const kDefaultFsOperations = {
|
|
61
|
+
open(path, flags = "r", mode = 0o666, cb) {
|
|
62
|
+
let callback;
|
|
63
|
+
if (typeof flags === "function") {
|
|
64
|
+
callback = flags;
|
|
65
|
+
}
|
|
66
|
+
else if (typeof mode === "function") {
|
|
67
|
+
callback = mode;
|
|
68
|
+
}
|
|
69
|
+
else if (typeof cb === "function") {
|
|
70
|
+
callback = cb;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
throw new ERR_MISSING_ARGS("fs.open callback");
|
|
74
|
+
}
|
|
75
|
+
validateFunction(callback, "fs.open callback");
|
|
76
|
+
const openFlags = typeof flags === "function" ? "r" : flags;
|
|
77
|
+
const openMode = typeof mode === "function" ? 0o666 : mode;
|
|
78
|
+
const done = (err, fd) => {
|
|
79
|
+
try {
|
|
80
|
+
if (err)
|
|
81
|
+
callback(err);
|
|
82
|
+
else
|
|
83
|
+
callback(null, fd);
|
|
84
|
+
}
|
|
85
|
+
catch (e) {
|
|
86
|
+
reportError(e);
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
getLazyFs().then((fs) => {
|
|
90
|
+
// fs.open validates flags synchronously.
|
|
91
|
+
try {
|
|
92
|
+
fs.open(path, openFlags, openMode, done);
|
|
93
|
+
}
|
|
94
|
+
catch (err) {
|
|
95
|
+
done(err);
|
|
96
|
+
}
|
|
97
|
+
}, done);
|
|
98
|
+
},
|
|
99
|
+
close(fd, cb = () => { }) {
|
|
100
|
+
getLazyFs().then((fs) => {
|
|
101
|
+
fs.close(fd, (err) => {
|
|
102
|
+
if (err) {
|
|
103
|
+
try {
|
|
104
|
+
cb(err);
|
|
105
|
+
}
|
|
106
|
+
catch (e) {
|
|
107
|
+
reportError(e);
|
|
108
|
+
}
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
try {
|
|
112
|
+
cb(null);
|
|
113
|
+
}
|
|
114
|
+
catch (e) {
|
|
115
|
+
reportError(e);
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}, (err) => {
|
|
119
|
+
try {
|
|
120
|
+
cb(err);
|
|
121
|
+
}
|
|
122
|
+
catch (e) {
|
|
123
|
+
reportError(e);
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
},
|
|
127
|
+
fsync(fd, cb = () => { }) {
|
|
128
|
+
getLazyFs().then((fs) => {
|
|
129
|
+
fs.fsync(fd, (err) => {
|
|
130
|
+
if (err) {
|
|
131
|
+
try {
|
|
132
|
+
cb(err);
|
|
133
|
+
}
|
|
134
|
+
catch (e) {
|
|
135
|
+
reportError(e);
|
|
136
|
+
}
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
try {
|
|
140
|
+
cb(null);
|
|
141
|
+
}
|
|
142
|
+
catch (e) {
|
|
143
|
+
reportError(e);
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
}, (err) => {
|
|
147
|
+
try {
|
|
148
|
+
cb(err);
|
|
149
|
+
}
|
|
150
|
+
catch (e) {
|
|
151
|
+
reportError(e);
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
},
|
|
155
|
+
read(fd, buffer, offset, length, position, cb) {
|
|
156
|
+
getLazyFs().then((fs) => {
|
|
157
|
+
fs.read(fd, buffer, offset, length, position, (err, bytesRead, buffer) => {
|
|
158
|
+
if (err) {
|
|
159
|
+
try {
|
|
160
|
+
cb?.(err);
|
|
161
|
+
}
|
|
162
|
+
catch (e) {
|
|
163
|
+
reportError(e);
|
|
164
|
+
}
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
try {
|
|
168
|
+
cb?.(null, bytesRead, buffer);
|
|
169
|
+
}
|
|
170
|
+
catch (e) {
|
|
171
|
+
reportError(e);
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
}, (err) => {
|
|
175
|
+
try {
|
|
176
|
+
cb?.(err);
|
|
177
|
+
}
|
|
178
|
+
catch (e) {
|
|
179
|
+
reportError(e);
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
},
|
|
183
|
+
write(fd, buffer, offset, length, position, cb) {
|
|
184
|
+
getLazyFs().then((fs) => {
|
|
185
|
+
fs.write(fd, buffer, offset, length, position, (err, bytesWritten, buffer) => {
|
|
186
|
+
if (err) {
|
|
187
|
+
try {
|
|
188
|
+
cb?.(err);
|
|
189
|
+
}
|
|
190
|
+
catch (e) {
|
|
191
|
+
reportError(e);
|
|
192
|
+
}
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
try {
|
|
196
|
+
cb?.(null, bytesWritten, buffer);
|
|
197
|
+
}
|
|
198
|
+
catch (e) {
|
|
199
|
+
reportError(e);
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
}, (err) => {
|
|
203
|
+
try {
|
|
204
|
+
cb?.(err);
|
|
205
|
+
}
|
|
206
|
+
catch (e) {
|
|
207
|
+
reportError(e);
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
},
|
|
211
|
+
writev(fd, buffers, position, cb) {
|
|
212
|
+
getLazyFs().then((fs) => {
|
|
213
|
+
fs.writev(fd, buffers, position, (err, bytesWritten, buffers) => {
|
|
214
|
+
if (err) {
|
|
215
|
+
try {
|
|
216
|
+
cb?.(err);
|
|
217
|
+
}
|
|
218
|
+
catch (e) {
|
|
219
|
+
reportError(e);
|
|
220
|
+
}
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
try {
|
|
224
|
+
cb?.(null, bytesWritten, buffers);
|
|
225
|
+
}
|
|
226
|
+
catch (e) {
|
|
227
|
+
reportError(e);
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
}, (err) => {
|
|
231
|
+
try {
|
|
232
|
+
cb?.(err);
|
|
233
|
+
}
|
|
234
|
+
catch (e) {
|
|
235
|
+
reportError(e);
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
},
|
|
239
|
+
};
|
|
240
|
+
const kFs = Symbol("kFs");
|
|
241
|
+
const kIsPerformingIO = Symbol("kIsPerformingIO");
|
|
242
|
+
const kIoDone = Symbol("kIoDone");
|
|
243
|
+
const kHandle = Symbol("kHandle");
|
|
244
|
+
function construct(callback) {
|
|
245
|
+
const stream = this;
|
|
246
|
+
if (typeof stream.fd === "number") {
|
|
247
|
+
callback(null);
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
const ee = stream;
|
|
251
|
+
if (typeof stream.open === "function") {
|
|
252
|
+
// Backwards compat for monkey patching open().
|
|
253
|
+
const orgEmit = ee.emit;
|
|
254
|
+
ee.emit = function (...args) {
|
|
255
|
+
if (args[0] === "open") {
|
|
256
|
+
this.emit = orgEmit;
|
|
257
|
+
callback(null);
|
|
258
|
+
Reflect.apply(orgEmit, this, args);
|
|
259
|
+
}
|
|
260
|
+
else if (args[0] === "error") {
|
|
261
|
+
this.emit = orgEmit;
|
|
262
|
+
callback(args[1]);
|
|
263
|
+
}
|
|
264
|
+
else {
|
|
265
|
+
Reflect.apply(orgEmit, this, args);
|
|
266
|
+
}
|
|
267
|
+
return true;
|
|
268
|
+
};
|
|
269
|
+
stream.open();
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
stream[kFs].open(stream.path, stream.flags, stream.mode, (er, fd) => {
|
|
273
|
+
if (er) {
|
|
274
|
+
callback(er);
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
if (fd === undefined) {
|
|
278
|
+
callback(new ERR_INVALID_ARG_VALUE("fd", "undefined"));
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
stream.fd = fd;
|
|
282
|
+
callback(null);
|
|
283
|
+
ee.emit("open", stream.fd);
|
|
284
|
+
ee.emit("ready");
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
function getValidatedFsOptions(fs) {
|
|
288
|
+
validateObject(fs, "options.fs");
|
|
289
|
+
if (isFileHandle(fs)) {
|
|
290
|
+
const handle = fs;
|
|
291
|
+
const open = function (..._args) {
|
|
292
|
+
throw new ERR_METHOD_NOT_IMPLEMENTED("open()");
|
|
293
|
+
};
|
|
294
|
+
const close = function (...args) {
|
|
295
|
+
const cb = args[args.length - 1];
|
|
296
|
+
handle.close().then(() => cb(null), (err) => cb(err));
|
|
297
|
+
};
|
|
298
|
+
const fsync = function (...args) {
|
|
299
|
+
const cb = args[args.length - 1];
|
|
300
|
+
handle.sync().then(() => cb(null), (err) => cb(err));
|
|
301
|
+
};
|
|
302
|
+
const read = function (...args) {
|
|
303
|
+
const cb = args[args.length - 1];
|
|
304
|
+
// @ts-expect-error TS2345
|
|
305
|
+
handle.read(...args.slice(1, -1)).then((result) => {
|
|
306
|
+
cb(null, result.bytesRead, result.buffer);
|
|
307
|
+
}, (err) => cb(err));
|
|
308
|
+
};
|
|
309
|
+
const write = function (...args) {
|
|
310
|
+
const cb = args[args.length - 1];
|
|
311
|
+
// @ts-expect-error TS2556
|
|
312
|
+
handle.write(...args.slice(1, -1)).then((result) => {
|
|
313
|
+
cb(null, result.bytesWritten, result.buffer);
|
|
314
|
+
}, (err) => cb(err));
|
|
315
|
+
};
|
|
316
|
+
const writev = function (...args) {
|
|
317
|
+
const cb = args[args.length - 1];
|
|
318
|
+
// @ts-expect-error TS2556
|
|
319
|
+
handle.writev(...args.slice(1, -1)).then((result) => {
|
|
320
|
+
cb(null, result.bytesWritten, result.buffers);
|
|
321
|
+
}, (err) => cb(err));
|
|
322
|
+
};
|
|
323
|
+
return {
|
|
324
|
+
open,
|
|
325
|
+
close,
|
|
326
|
+
fsync,
|
|
327
|
+
read,
|
|
328
|
+
write,
|
|
329
|
+
writev,
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
let { open = kDefaultFsOperations.open, close = kDefaultFsOperations.close, fsync = kDefaultFsOperations.fsync, read = kDefaultFsOperations.read, write = kDefaultFsOperations.write, writev = kDefaultFsOperations.writev, } = fs;
|
|
333
|
+
validateFunction(open, "options.fs.open");
|
|
334
|
+
validateFunction(read, "options.fs.read");
|
|
335
|
+
validateFunction(close, "options.fs.close");
|
|
336
|
+
validateFunction(fsync, "options.fs.fsync");
|
|
337
|
+
validateFunction(write, "options.fs.write");
|
|
338
|
+
validateFunction(writev, "options.fs.writev");
|
|
339
|
+
return { open, close, fsync, read, write, writev };
|
|
340
|
+
}
|
|
341
|
+
function readImpl(n) {
|
|
342
|
+
n =
|
|
343
|
+
this.pos !== undefined
|
|
344
|
+
? Math.min(this.end - this.pos + 1, n)
|
|
345
|
+
: Math.min(this.end - this.bytesRead + 1, n);
|
|
346
|
+
if (n <= 0) {
|
|
347
|
+
this.push(null);
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
const buf = Buffer.allocUnsafeSlow(n);
|
|
351
|
+
const ee = this;
|
|
352
|
+
this[kIsPerformingIO] = true;
|
|
353
|
+
if (this.fd == null) {
|
|
354
|
+
this.push(null);
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
this[kFs].read(this.fd, buf, 0, n, this.pos, (er, bytesRead, buf) => {
|
|
358
|
+
this[kIsPerformingIO] = false;
|
|
359
|
+
// Tell ._destroy() that it's safe to close the fd now.
|
|
360
|
+
if (this.destroyed) {
|
|
361
|
+
ee.emit(kIoDone, er);
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
if (er) {
|
|
365
|
+
errorOrDestroy(this, er);
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
if (buf == null) {
|
|
369
|
+
errorOrDestroy(this, new ERR_INVALID_ARG_VALUE("buf", "null"));
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
if (bytesRead != null && bytesRead > 0) {
|
|
373
|
+
if (this.pos !== undefined) {
|
|
374
|
+
this.pos += bytesRead;
|
|
375
|
+
}
|
|
376
|
+
this.bytesRead += bytesRead;
|
|
377
|
+
if (bytesRead !== buf.byteLength) {
|
|
378
|
+
// Slow path. Shrink to fit.
|
|
379
|
+
// Copy instead of slice so that we don't retain
|
|
380
|
+
// large backing buffer for small reads.
|
|
381
|
+
const dst = Buffer.allocUnsafeSlow(bytesRead);
|
|
382
|
+
if (Buffer.isBuffer(buf)) {
|
|
383
|
+
buf.copy(dst, 0, 0, bytesRead);
|
|
384
|
+
}
|
|
385
|
+
else {
|
|
386
|
+
const buffer = Buffer.from(buf.buffer, buf.byteOffset, bytesRead);
|
|
387
|
+
buffer.copy(dst, 0, 0, bytesRead);
|
|
388
|
+
}
|
|
389
|
+
buf = dst;
|
|
390
|
+
}
|
|
391
|
+
this.push(buf);
|
|
392
|
+
}
|
|
393
|
+
else {
|
|
394
|
+
this.push(null);
|
|
395
|
+
}
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
function actualCloseImpl(stream, err, cb) {
|
|
399
|
+
if (stream.fd == null)
|
|
400
|
+
return;
|
|
401
|
+
stream[kFs].close(stream.fd, (er) => {
|
|
402
|
+
cb(er || err);
|
|
403
|
+
});
|
|
404
|
+
stream.fd = null;
|
|
405
|
+
}
|
|
406
|
+
function closeImpl(stream, err, cb) {
|
|
407
|
+
if (stream.fd == null) {
|
|
408
|
+
cb(err);
|
|
409
|
+
}
|
|
410
|
+
else if (stream.flush) {
|
|
411
|
+
stream[kFs].fsync(stream.fd, (flushErr) => {
|
|
412
|
+
actualCloseImpl(stream, err || flushErr, cb);
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
else {
|
|
416
|
+
actualCloseImpl(stream, err, cb);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
function destroyImpl(err, cb) {
|
|
420
|
+
// Usually for async IO it is safe to close a file descriptor
|
|
421
|
+
// even when there are pending operations. However, due to platform
|
|
422
|
+
// differences file IO is implemented using synchronous operations
|
|
423
|
+
// running in a thread pool. Therefore, file descriptors are not safe
|
|
424
|
+
// to close while used in a pending read or write operation. Wait for
|
|
425
|
+
// any pending IO (kIsPerformingIO) to complete (kIoDone).
|
|
426
|
+
const ee = this;
|
|
427
|
+
if (this[kIsPerformingIO]) {
|
|
428
|
+
ee.once(kIoDone, (er) => {
|
|
429
|
+
closeImpl(this, err || er, cb);
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
else {
|
|
433
|
+
closeImpl(this, err, cb);
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
export const ReadStream = function ReadStream(path, options = {}) {
|
|
437
|
+
if (!(this instanceof ReadStream)) {
|
|
438
|
+
return new ReadStream(path, options);
|
|
439
|
+
}
|
|
440
|
+
if (options === null) {
|
|
441
|
+
options = {};
|
|
442
|
+
}
|
|
443
|
+
else if (typeof options === "string") {
|
|
444
|
+
options = { encoding: options };
|
|
445
|
+
}
|
|
446
|
+
validateObject(options, "options");
|
|
447
|
+
const { encoding = null, autoClose = true, emitClose = true, start = 0, end = Infinity, highWaterMark = 64 * 1024, signal = null, flags = "r", fd = null, mode = 0o666, fs = kDefaultFsOperations, } = options;
|
|
448
|
+
const autoDestroy = autoClose;
|
|
449
|
+
if (encoding !== "buffer" &&
|
|
450
|
+
encoding !== null &&
|
|
451
|
+
!Buffer.isEncoding(encoding)) {
|
|
452
|
+
throw new ERR_INVALID_ARG_VALUE("options.encoding", encoding);
|
|
453
|
+
}
|
|
454
|
+
validateBoolean(autoClose, "options.autoClose");
|
|
455
|
+
validateBoolean(emitClose, "options.emitClose");
|
|
456
|
+
validateUint32(start, "options.start");
|
|
457
|
+
if (end !== Infinity) {
|
|
458
|
+
validateUint32(end, "options.end");
|
|
459
|
+
if (start > end) {
|
|
460
|
+
throw new ERR_OUT_OF_RANGE("start", `<= "end" (here: ${end})`, start);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
validateUint32(highWaterMark, "options.highWaterMark");
|
|
464
|
+
if (signal != null) {
|
|
465
|
+
validateAbortSignal(signal, "options.signal");
|
|
466
|
+
}
|
|
467
|
+
validateString(flags, "options.flags");
|
|
468
|
+
// We don't actually use the mode in our implementation. Parsing it is
|
|
469
|
+
// just to ensure that it is validated.
|
|
470
|
+
parseFileMode(mode, "options.mode", 0o666);
|
|
471
|
+
this[kFs] = bindFs(getValidatedFsOptions(fs));
|
|
472
|
+
if (fd == null) {
|
|
473
|
+
this.fd = null;
|
|
474
|
+
// Path will be ignored when fd is specified, so it can be falsy
|
|
475
|
+
this.path = toPathIfFileURL(normalizePath(path));
|
|
476
|
+
this.flags = options.flags === undefined ? "r" : options.flags;
|
|
477
|
+
this.mode = options.mode === undefined ? 0o666 : options.mode;
|
|
478
|
+
}
|
|
479
|
+
else {
|
|
480
|
+
if (isFileHandle(fd)) {
|
|
481
|
+
if (fs !== kDefaultFsOperations) {
|
|
482
|
+
throw new ERR_METHOD_NOT_IMPLEMENTED("FileHandle with fs");
|
|
483
|
+
}
|
|
484
|
+
this[kHandle] = fd;
|
|
485
|
+
this[kFs] = bindFs(getValidatedFsOptions(fd));
|
|
486
|
+
const ee = fd;
|
|
487
|
+
ee.on("close", () => this.close());
|
|
488
|
+
this.fd = fd.fd || null;
|
|
489
|
+
}
|
|
490
|
+
else {
|
|
491
|
+
this.fd = getValidatedFd(fd);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
this.start = start;
|
|
495
|
+
this.end = end;
|
|
496
|
+
this.pos = start;
|
|
497
|
+
this.bytesRead = 0;
|
|
498
|
+
this[kIsPerformingIO] = false;
|
|
499
|
+
Reflect.apply(Readable, this, [
|
|
500
|
+
{
|
|
501
|
+
highWaterMark,
|
|
502
|
+
encoding,
|
|
503
|
+
emitClose,
|
|
504
|
+
autoDestroy,
|
|
505
|
+
signal,
|
|
506
|
+
construct: construct.bind(this),
|
|
507
|
+
read: readImpl.bind(this),
|
|
508
|
+
destroy: destroyImpl.bind(this),
|
|
509
|
+
},
|
|
510
|
+
]);
|
|
511
|
+
return this;
|
|
512
|
+
};
|
|
513
|
+
Object.setPrototypeOf(ReadStream.prototype, Readable.prototype);
|
|
514
|
+
Object.setPrototypeOf(ReadStream, Readable);
|
|
515
|
+
Object.defineProperty(ReadStream.prototype, "autoClose", {
|
|
516
|
+
get() {
|
|
517
|
+
validateThisInternalField(this, kFs, "ReadStream");
|
|
518
|
+
return this._readableState?.autoDestroy || false;
|
|
519
|
+
},
|
|
520
|
+
set(_val) {
|
|
521
|
+
validateThisInternalField(this, kFs, "ReadStream");
|
|
522
|
+
if (this._readableState !== undefined) {
|
|
523
|
+
this._readableState.autoDestroy = _val;
|
|
524
|
+
}
|
|
525
|
+
},
|
|
526
|
+
});
|
|
527
|
+
ReadStream.prototype.close = function (cb = (_err) => { }) {
|
|
528
|
+
if (typeof cb === "function")
|
|
529
|
+
eos(this, cb);
|
|
530
|
+
this.destroy();
|
|
531
|
+
};
|
|
532
|
+
Object.defineProperty(ReadStream.prototype, "pending", {
|
|
533
|
+
get() {
|
|
534
|
+
return this.fd === null;
|
|
535
|
+
},
|
|
536
|
+
configurable: true,
|
|
537
|
+
});
|
|
538
|
+
function writeAll(data, size, pos, cb, retries = 0) {
|
|
539
|
+
if (this.fd == null) {
|
|
540
|
+
return cb(new ERR_INVALID_ARG_VALUE("fd", "null"));
|
|
541
|
+
}
|
|
542
|
+
this[kFs].write(this.fd, data, 0, size, pos, (er, bytesWritten, buffer) => {
|
|
543
|
+
// No data currently available and operation should be retried later.
|
|
544
|
+
if (er?.code === "EAGAIN") {
|
|
545
|
+
er = null;
|
|
546
|
+
bytesWritten = 0;
|
|
547
|
+
}
|
|
548
|
+
if (this.destroyed || er) {
|
|
549
|
+
return cb(er || new ERR_STREAM_DESTROYED("write"));
|
|
550
|
+
}
|
|
551
|
+
// The value should be set but let's suppress the possible
|
|
552
|
+
// typescript error here.
|
|
553
|
+
bytesWritten = bytesWritten ?? 0;
|
|
554
|
+
this.bytesWritten += bytesWritten;
|
|
555
|
+
retries = bytesWritten ? 0 : retries + 1;
|
|
556
|
+
size -= bytesWritten;
|
|
557
|
+
pos += bytesWritten;
|
|
558
|
+
// Try writing non-zero number of bytes up to 5 times.
|
|
559
|
+
if (retries > 5) {
|
|
560
|
+
cb(new ERR_SYSTEM_ERROR("write failed"));
|
|
561
|
+
}
|
|
562
|
+
else if (size) {
|
|
563
|
+
if (buffer == null) {
|
|
564
|
+
cb(null);
|
|
565
|
+
return;
|
|
566
|
+
}
|
|
567
|
+
const buf = typeof buffer === "string"
|
|
568
|
+
? Buffer.from(buffer)
|
|
569
|
+
: Buffer.from(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
570
|
+
writeAll.call(this, buf.slice(bytesWritten), size, pos, cb, retries);
|
|
571
|
+
}
|
|
572
|
+
else {
|
|
573
|
+
cb(null);
|
|
574
|
+
}
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
function writevAll(chunks, size, pos, cb, retries = 0) {
|
|
578
|
+
if (this.fd == null) {
|
|
579
|
+
return cb(new ERR_INVALID_ARG_VALUE("fd", "null"));
|
|
580
|
+
}
|
|
581
|
+
this[kFs].writev(this.fd, chunks, this.pos, (er, bytesWritten, buffers) => {
|
|
582
|
+
// No data currently available and operation should be retried later.
|
|
583
|
+
if (er?.code === "EAGAIN") {
|
|
584
|
+
er = null;
|
|
585
|
+
bytesWritten = 0;
|
|
586
|
+
}
|
|
587
|
+
if (this.destroyed || er) {
|
|
588
|
+
return cb(er || new ERR_STREAM_DESTROYED("writev"));
|
|
589
|
+
}
|
|
590
|
+
bytesWritten = bytesWritten ?? 0;
|
|
591
|
+
this.bytesWritten += bytesWritten;
|
|
592
|
+
retries = bytesWritten ? 0 : retries + 1;
|
|
593
|
+
size -= bytesWritten;
|
|
594
|
+
pos += bytesWritten;
|
|
595
|
+
// Try writing non-zero number of bytes up to 5 times.
|
|
596
|
+
if (retries > 5) {
|
|
597
|
+
cb(new ERR_SYSTEM_ERROR("writev failed"));
|
|
598
|
+
}
|
|
599
|
+
else if (size) {
|
|
600
|
+
buffers ??= [];
|
|
601
|
+
const bufs = buffers.map((b) => {
|
|
602
|
+
return Buffer.from(b.buffer, b.byteOffset, b.byteLength);
|
|
603
|
+
});
|
|
604
|
+
if (buffers.length === 0) {
|
|
605
|
+
cb(null);
|
|
606
|
+
return;
|
|
607
|
+
}
|
|
608
|
+
writevAll.call(this, [Buffer.concat(bufs).slice(bytesWritten)], size, pos, cb, retries);
|
|
609
|
+
}
|
|
610
|
+
else {
|
|
611
|
+
cb(null);
|
|
612
|
+
}
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
function writeImpl(data, encodingOrCallback, cb) {
|
|
616
|
+
let callback;
|
|
617
|
+
let encoding = null;
|
|
618
|
+
if (typeof encodingOrCallback === "function") {
|
|
619
|
+
callback = encodingOrCallback;
|
|
620
|
+
}
|
|
621
|
+
else if (encodingOrCallback != null) {
|
|
622
|
+
encoding = encodingOrCallback;
|
|
623
|
+
if (cb !== undefined)
|
|
624
|
+
callback = cb;
|
|
625
|
+
}
|
|
626
|
+
// @ts-expect-error TS2345
|
|
627
|
+
validateFunction(callback, "write callback");
|
|
628
|
+
if (typeof data === "string") {
|
|
629
|
+
data = Buffer.from(data, encoding);
|
|
630
|
+
}
|
|
631
|
+
this[kIsPerformingIO] = true;
|
|
632
|
+
const ee = this;
|
|
633
|
+
writeAll.call(this, data, data.byteLength, this.pos, (er) => {
|
|
634
|
+
this[kIsPerformingIO] = false;
|
|
635
|
+
if (this.destroyed) {
|
|
636
|
+
// Tell ._destroy() that it's safe to close the fd now.
|
|
637
|
+
callback(er);
|
|
638
|
+
ee.emit(kIoDone, er);
|
|
639
|
+
return;
|
|
640
|
+
}
|
|
641
|
+
callback(er);
|
|
642
|
+
});
|
|
643
|
+
if (this.pos !== undefined)
|
|
644
|
+
this.pos += data.byteLength;
|
|
645
|
+
}
|
|
646
|
+
function writevImpl(data, callback) {
|
|
647
|
+
let size = 0;
|
|
648
|
+
const chunks = data.map((d) => {
|
|
649
|
+
const chunk = d.chunk;
|
|
650
|
+
size += chunk.length;
|
|
651
|
+
return chunk;
|
|
652
|
+
});
|
|
653
|
+
this[kIsPerformingIO] = true;
|
|
654
|
+
writevAll.call(this, chunks, size, this.pos, (er) => {
|
|
655
|
+
this[kIsPerformingIO] = false;
|
|
656
|
+
const ee = this;
|
|
657
|
+
if (this.destroyed) {
|
|
658
|
+
// Tell ._destroy() that it's safe to close the fd now.
|
|
659
|
+
callback(er);
|
|
660
|
+
ee.emit(kIoDone, er);
|
|
661
|
+
return;
|
|
662
|
+
}
|
|
663
|
+
callback(er);
|
|
664
|
+
});
|
|
665
|
+
if (this.pos !== undefined)
|
|
666
|
+
this.pos += size;
|
|
667
|
+
}
|
|
668
|
+
export const WriteStream = function WriteStream(path, options = {}) {
|
|
669
|
+
if (!(this instanceof WriteStream)) {
|
|
670
|
+
return new WriteStream(path, options);
|
|
671
|
+
}
|
|
672
|
+
if (options === null) {
|
|
673
|
+
options = {};
|
|
674
|
+
}
|
|
675
|
+
else if (typeof options === "string") {
|
|
676
|
+
options = { encoding: options };
|
|
677
|
+
}
|
|
678
|
+
validateObject(options, "options");
|
|
679
|
+
const { encoding = null, autoClose = true, emitClose = true, start = 0, highWaterMark = 64 * 1024, signal = null, flags = "w", fd = null, mode = 0o666, fs = kDefaultFsOperations, } = options;
|
|
680
|
+
let { flush = false } = options;
|
|
681
|
+
const autoDestroy = autoClose;
|
|
682
|
+
if (encoding !== "buffer" &&
|
|
683
|
+
encoding !== null &&
|
|
684
|
+
!Buffer.isEncoding(encoding)) {
|
|
685
|
+
throw new ERR_INVALID_ARG_VALUE("options.encoding", encoding);
|
|
686
|
+
}
|
|
687
|
+
validateBoolean(autoClose, "options.autoClose");
|
|
688
|
+
validateBoolean(emitClose, "options.emitClose");
|
|
689
|
+
if (flush === null)
|
|
690
|
+
flush = false;
|
|
691
|
+
validateBoolean(flush, "options.flush");
|
|
692
|
+
validateUint32(start, "options.start");
|
|
693
|
+
validateUint32(highWaterMark, "options.highWaterMark");
|
|
694
|
+
if (signal != null) {
|
|
695
|
+
validateAbortSignal(signal, "options.signal");
|
|
696
|
+
}
|
|
697
|
+
validateString(flags, "options.flags");
|
|
698
|
+
// We don't actually use the mode in our implementation. Parsing it is
|
|
699
|
+
// just to ensure that it is validated.
|
|
700
|
+
parseFileMode(mode, "options.mode", 0o666);
|
|
701
|
+
this[kFs] = bindFs(getValidatedFsOptions(fs));
|
|
702
|
+
this.flush = flush;
|
|
703
|
+
if (fd == null) {
|
|
704
|
+
this.fd = null;
|
|
705
|
+
// Path will be ignored when fd is specified, so it can be falsy
|
|
706
|
+
this.path = toPathIfFileURL(normalizePath(path));
|
|
707
|
+
this.flags = options.flags === undefined ? "w" : options.flags;
|
|
708
|
+
this.mode = options.mode === undefined ? 0o666 : options.mode;
|
|
709
|
+
}
|
|
710
|
+
else {
|
|
711
|
+
if (isFileHandle(fd)) {
|
|
712
|
+
if (fs !== kDefaultFsOperations) {
|
|
713
|
+
throw new ERR_METHOD_NOT_IMPLEMENTED("FileHandle with fs");
|
|
714
|
+
}
|
|
715
|
+
this[kHandle] = fd;
|
|
716
|
+
this[kFs] = bindFs(getValidatedFsOptions(fd));
|
|
717
|
+
const ee = fd;
|
|
718
|
+
ee.on("close", () => this.close());
|
|
719
|
+
this.fd = fd.fd || null;
|
|
720
|
+
}
|
|
721
|
+
else {
|
|
722
|
+
this.fd = getValidatedFd(fd);
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
this.start = start;
|
|
726
|
+
this.pos = start;
|
|
727
|
+
this.bytesRead = 0;
|
|
728
|
+
this.bytesWritten = 0;
|
|
729
|
+
this[kIsPerformingIO] = false;
|
|
730
|
+
Reflect.apply(Writable, this, [
|
|
731
|
+
{
|
|
732
|
+
highWaterMark,
|
|
733
|
+
encoding,
|
|
734
|
+
emitClose,
|
|
735
|
+
autoDestroy,
|
|
736
|
+
signal,
|
|
737
|
+
construct: construct.bind(this),
|
|
738
|
+
write: writeImpl.bind(this),
|
|
739
|
+
writev: writevImpl.bind(this),
|
|
740
|
+
destroy: destroyImpl.bind(this),
|
|
741
|
+
},
|
|
742
|
+
]);
|
|
743
|
+
if (encoding != null && encoding !== "buffer") {
|
|
744
|
+
this.setDefaultEncoding(encoding);
|
|
745
|
+
}
|
|
746
|
+
return this;
|
|
747
|
+
};
|
|
748
|
+
Object.setPrototypeOf(WriteStream.prototype, Writable.prototype);
|
|
749
|
+
Object.setPrototypeOf(WriteStream, Writable);
|
|
750
|
+
Object.defineProperty(WriteStream.prototype, "autoClose", {
|
|
751
|
+
get() {
|
|
752
|
+
validateThisInternalField(this, kFs, "WriteStream");
|
|
753
|
+
return this._writableState?.autoDestroy || false;
|
|
754
|
+
},
|
|
755
|
+
set(val) {
|
|
756
|
+
validateThisInternalField(this, kFs, "WriteStream");
|
|
757
|
+
if (this._writableState !== undefined) {
|
|
758
|
+
this._writableState.autoDestroy = val;
|
|
759
|
+
}
|
|
760
|
+
},
|
|
761
|
+
});
|
|
762
|
+
WriteStream.prototype.close = function (cb = (_err) => { }) {
|
|
763
|
+
const writable = this;
|
|
764
|
+
const ee = this;
|
|
765
|
+
if (cb) {
|
|
766
|
+
if (writable.closed) {
|
|
767
|
+
queueMicrotask(() => cb(null));
|
|
768
|
+
return;
|
|
769
|
+
}
|
|
770
|
+
ee.on("close", cb);
|
|
771
|
+
}
|
|
772
|
+
// If we are not autoClosing, we should call
|
|
773
|
+
// destroy on 'finish'.
|
|
774
|
+
if (!this.autoClose) {
|
|
775
|
+
ee.on("finish", () => writable.destroy());
|
|
776
|
+
}
|
|
777
|
+
// We use end() instead of destroy() because of
|
|
778
|
+
// https://github.com/nodejs/node/issues/2006
|
|
779
|
+
writable.end();
|
|
780
|
+
};
|
|
781
|
+
// There is no shutdown() for files.
|
|
782
|
+
WriteStream.prototype.destroySoon = WriteStream.prototype.end;
|
|
783
|
+
Object.defineProperty(WriteStream.prototype, "pending", {
|
|
784
|
+
get() {
|
|
785
|
+
return this.fd === null;
|
|
786
|
+
},
|
|
787
|
+
configurable: true,
|
|
788
|
+
});
|
|
789
|
+
export function createReadStream(path, options = {}) {
|
|
790
|
+
return new ReadStream(path, options);
|
|
791
|
+
}
|
|
792
|
+
export function createWriteStream(path, options = {}) {
|
|
793
|
+
return new WriteStream(path, options);
|
|
794
|
+
}
|