1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
|
var { direct, isPromise, isCallable } = import.meta.primordials;
var fs = Bun.fs();
var debug = process.env.DEBUG ? console.log : () => {};
export var access = function access(...args) {
callbackify(fs.accessSync, args);
};
export var appendFile = function appendFile(...args) {
callbackify(fs.appendFileSync, args);
};
export var close = function close(...args) {
callbackify(fs.closeSync, args);
};
export var rm = function rm(...args) {
callbackify(fs.rmSync, args);
};
export var copyFile = function copyFile(...args) {
callbackify(fs.copyFileSync, args);
};
export var exists = function exists(...args) {
callbackify(fs.existsSync, args);
};
export var chown = function chown(...args) {
callbackify(fs.chownSync, args);
};
export var chmod = function chmod(...args) {
callbackify(fs.chmodSync, args);
};
export var fchmod = function fchmod(...args) {
callbackify(fs.fchmodSync, args);
};
export var fchown = function fchown(...args) {
callbackify(fs.fchownSync, args);
};
export var fstat = function fstat(...args) {
callbackify(fs.fstatSync, args);
};
export var fsync = function fsync(...args) {
callbackify(fs.fsyncSync, args);
};
export var ftruncate = function ftruncate(...args) {
callbackify(fs.ftruncateSync, args);
};
export var futimes = function futimes(...args) {
callbackify(fs.futimesSync, args);
};
export var lchmod = function lchmod(...args) {
callbackify(fs.lchmodSync, args);
};
export var lchown = function lchown(...args) {
callbackify(fs.lchownSync, args);
};
export var link = function link(...args) {
callbackify(fs.linkSync, args);
};
export var lstat = function lstat(...args) {
callbackify(fs.lstatSync, args);
};
export var mkdir = function mkdir(...args) {
callbackify(fs.mkdirSync, args);
};
export var mkdtemp = function mkdtemp(...args) {
callbackify(fs.mkdtempSync, args);
};
export var open = function open(...args) {
callbackify(fs.openSync, args);
};
export var read = function read(...args) {
callbackify(fs.readSync, args);
};
export var write = function write(...args) {
callbackify(fs.writeSync, args);
};
export var readdir = function readdir(...args) {
callbackify(fs.readdirSync, args);
};
export var readFile = function readFile(...args) {
callbackify(fs.readFileSync, args);
};
export var writeFile = function writeFile(...args) {
callbackify(fs.writeFileSync, args);
};
export var readlink = function readlink(...args) {
callbackify(fs.readlinkSync, args);
};
export var realpath = function realpath(...args) {
callbackify(fs.realpathSync, args);
};
export var rename = function rename(...args) {
callbackify(fs.renameSync, args);
};
export var stat = function stat(...args) {
callbackify(fs.statSync, args);
};
export var symlink = function symlink(...args) {
callbackify(fs.symlinkSync, args);
};
export var truncate = function truncate(...args) {
callbackify(fs.truncateSync, args);
};
export var unlink = function unlink(...args) {
callbackify(fs.unlinkSync, args);
};
export var utimes = function utimes(...args) {
callbackify(fs.utimesSync, args);
};
export var lutimes = function lutimes(...args) {
callbackify(fs.lutimesSync, args);
};
function callbackify(fsFunction, args) {
try {
const result = fsFunction.apply(fs, args.slice(0, args.length - 1));
queueMicrotask(() => args[args.length - 1](null, result));
} catch (e) {
queueMicrotask(() => args[args.length - 1](e));
}
}
// note: this is not quite the same as how node does it
// in some cases, node swaps around arguments or makes small tweaks to the return type
// this is just better than nothing.
function promisify(fsFunction) {
// TODO: remove variadic arguments
// we can use new Function() here instead
// based on fsFucntion.length
var obj = {
[fsFunction.name]: function (resolve, reject, args) {
var result;
try {
result = fsFunction.apply(fs, args);
args = undefined;
} catch (err) {
args = undefined;
reject(err);
return;
}
resolve(result);
},
};
var func = obj[fsFunction.name];
// TODO: consider @createPromiseCapabiilty intrinsic
return (...args) => {
return new Promise((resolve, reject) => {
func(resolve, reject, args);
});
};
}
export var accessSync = fs.accessSync.bind(fs);
export var appendFileSync = fs.appendFileSync.bind(fs);
export var closeSync = fs.closeSync.bind(fs);
export var copyFileSync = fs.copyFileSync.bind(fs);
export var existsSync = fs.existsSync.bind(fs);
export var chownSync = fs.chownSync.bind(fs);
export var chmodSync = fs.chmodSync.bind(fs);
export var fchmodSync = fs.fchmodSync.bind(fs);
export var fchownSync = fs.fchownSync.bind(fs);
export var fstatSync = fs.fstatSync.bind(fs);
export var fsyncSync = fs.fsyncSync.bind(fs);
export var ftruncateSync = fs.ftruncateSync.bind(fs);
export var futimesSync = fs.futimesSync.bind(fs);
export var lchmodSync = fs.lchmodSync.bind(fs);
export var lchownSync = fs.lchownSync.bind(fs);
export var linkSync = fs.linkSync.bind(fs);
export var lstatSync = fs.lstatSync.bind(fs);
export var mkdirSync = fs.mkdirSync.bind(fs);
export var mkdtempSync = fs.mkdtempSync.bind(fs);
export var openSync = fs.openSync.bind(fs);
export var readSync = fs.readSync.bind(fs);
export var writeSync = fs.writeSync.bind(fs);
export var readdirSync = fs.readdirSync.bind(fs);
export var readFileSync = fs.readFileSync.bind(fs);
export var writeFileSync = fs.writeFileSync.bind(fs);
export var readlinkSync = fs.readlinkSync.bind(fs);
export var realpathSync = fs.realpathSync.bind(fs);
export var renameSync = fs.renameSync.bind(fs);
export var statSync = fs.statSync.bind(fs);
export var symlinkSync = fs.symlinkSync.bind(fs);
export var truncateSync = fs.truncateSync.bind(fs);
export var unlinkSync = fs.unlinkSync.bind(fs);
export var utimesSync = fs.utimesSync.bind(fs);
export var lutimesSync = fs.lutimesSync.bind(fs);
export var rmSync = fs.rmSync.bind(fs);
// Results from Object.keys() in Node 18
// fd
// path
// flags
// mode
// start
// end
// pos
// bytesRead
// _readableState
// _events
// _eventsCount
// _maxListener
var _lazyReadStream;
var readStreamPathFastPathSymbol = Symbol.for(
"Bun.Node.readStreamPathFastPath",
);
const readStreamSymbol = Symbol.for("Bun.NodeReadStream");
const readStreamPathOrFdSymbol = Symbol.for("Bun.NodeReadStreamPathOrFd");
var writeStreamPathFastPathSymbol = Symbol.for("Bun.NodeWriteStreamFastPath");
var writeStreamPathFastPathCallSymbol = Symbol.for(
"Bun.NodeWriteStreamFastPathCall",
);
var kIoDone = Symbol.for("kIoDone");
function getLazyReadStream() {
if (_lazyReadStream) {
return _lazyReadStream;
}
var {
Readable,
_getNativeReadableStreamPrototype,
eos: eos_,
} = import.meta.require("node:stream");
var defaultReadStreamOptions = {
file: undefined,
fd: undefined,
flags: "r",
encoding: undefined,
mode: 0o666,
autoClose: true,
emitClose: true,
start: 0,
end: Infinity,
highWaterMark: 64 * 1024,
fs: {
read,
open: (path, flags, mode, cb) => {
var fd;
try {
fd = openSync(path, flags, mode);
} catch (e) {
cb(e);
return;
}
cb(null, fd);
},
openSync,
close,
},
autoDestroy: true,
};
var NativeReadable = _getNativeReadableStreamPrototype(2, Readable); // 2 means native type is a file here
var ReadStream = class ReadStream extends NativeReadable {
constructor(pathOrFd, options = defaultReadStreamOptions) {
if (typeof options !== "object" || !options) {
throw new TypeError("Expected options to be an object");
}
var {
flags = defaultReadStreamOptions.flags,
encoding = defaultReadStreamOptions.encoding,
mode = defaultReadStreamOptions.mode,
autoClose = defaultReadStreamOptions.autoClose,
emitClose = defaultReadStreamOptions.emitClose,
start = defaultReadStreamOptions.start,
end = defaultReadStreamOptions.end,
autoDestroy = defaultReadStreamOptions.autoClose,
fs = defaultReadStreamOptions.fs,
highWaterMark = defaultReadStreamOptions.highWaterMark,
} = options;
if (pathOrFd?.constructor?.name === "URL") {
pathOrFd = Bun.fileURLToPath(pathOrFd);
}
// This is kinda hacky but we create a temporary object to assign props that we will later pull into the `this` context after we call super
var tempThis = {};
if (typeof pathOrFd === "string") {
if (pathOrFd.startsWith("file://")) {
pathOrFd = Bun.fileURLToPath(pathOrFd);
}
if (pathOrFd.length === 0) {
throw new TypeError("Expected path to be a non-empty string");
}
tempThis.path =
tempThis.file =
tempThis[readStreamPathOrFdSymbol] =
pathOrFd;
} else if (typeof pathOrFd === "number") {
pathOrFd |= 0;
if (pathOrFd < 0) {
throw new TypeError("Expected fd to be a positive integer");
}
tempThis.fd = tempThis[readStreamPathOrFdSymbol] = pathOrFd;
tempThis.autoClose = false;
} else {
throw new TypeError("Expected a path or file descriptor");
}
// If fd not open for this file, open it
if (!tempThis.fd) {
// NOTE: this fs is local to constructor, from options
tempThis.fd = fs.openSync(pathOrFd, flags, mode);
}
// Get FileRef from fd
var fileRef = Bun.file(tempThis.fd);
// Get the stream controller
// We need the pointer to the underlying stream controller for the NativeReadable
var stream = fileRef.stream();
var native = direct(stream);
if (!native) {
debug("no native readable stream");
throw new Error("no native readable stream");
}
var { stream: ptr } = native;
super(ptr, {
...options,
encoding,
autoDestroy,
autoClose,
emitClose,
highWaterMark,
});
// Assign the tempThis props to this
Object.assign(this, tempThis);
this.#fileRef = fileRef;
this.end = end;
this._read = this.#internalRead;
this.start = start;
this.flags = flags;
this.mode = mode;
this.emitClose = emitClose;
this[readStreamPathFastPathSymbol] =
start === 0 &&
end === Infinity &&
autoClose &&
fs === defaultReadStreamOptions.fs &&
// is it an encoding which we don't need to decode?
(encoding === "buffer" ||
encoding === "binary" ||
encoding == null ||
encoding === "utf-8" ||
encoding === "utf8");
this._readableState.autoClose = autoDestroy = autoClose;
this._readableState.highWaterMark = highWaterMark;
if (start !== undefined) {
this.pos = start;
}
}
#fileRef;
#fs;
file;
path;
fd = null;
flags;
mode;
start;
end;
pos;
bytesRead = 0;
#fileSize = -1;
_read;
[readStreamSymbol] = true;
[readStreamPathOrFdSymbol];
[readStreamPathFastPathSymbol];
_construct(callback) {
if (super._construct) {
super._construct(callback);
} else {
callback();
}
this.emit("open", this.fd);
this.emit("ready");
}
_destroy(err, cb) {
super._destroy(err, cb);
try {
var fd = this.fd;
this[readStreamPathFastPathSymbol] = false;
if (!fd) {
cb(err);
} else {
this.#fs.close(fd, (er) => {
cb(er || err);
});
this.fd = null;
}
} catch (e) {
throw e;
}
}
close(cb) {
if (typeof cb === "function") eos_()(this, cb);
this.destroy();
}
push(chunk) {
// Is it even possible for this to be less than 1?
var bytesRead = chunk?.length ?? 0;
if (bytesRead > 0) {
this.bytesRead += bytesRead;
var currPos = this.pos;
// Handle case of going through bytes before pos if bytesRead is less than pos
// If pos is undefined, we are reading through the whole file
// Otherwise we started from somewhere in the middle of the file
if (currPos !== undefined) {
// At this point we still haven't hit our `start` point
// We should discard this chunk and exit
if (this.bytesRead < currPos) {
return true;
}
// At this point, bytes read is greater than our starting position
// If the current position is still the starting position, that means
// this is the first chunk where we care about the bytes read
// and we need to subtract the bytes read from the start position (n) and slice the last n bytes
if (currPos === this.start) {
var n = this.bytesRead - currPos;
chunk = chunk.slice(-n);
var [_, ...rest] = arguments;
this.pos = this.bytesRead;
if (this.end && this.bytesRead >= this.end) {
chunk = chunk.slice(0, this.end - this.start);
}
return super.push(chunk, ...rest);
}
var end = this.end;
// This is multi-chunk read case where we go passed the end of the what we want to read in the last chunk
if (end && this.bytesRead >= end) {
chunk = chunk.slice(0, end - currPos);
var [_, ...rest] = arguments;
this.pos = this.bytesRead;
return super.push(chunk, ...rest);
}
this.pos = this.bytesRead;
}
}
return super.push(...arguments);
}
// #
// n should be the the highwatermark passed from Readable.read when calling internal _read (_read is set to this private fn in this class)
#internalRead(n) {
// pos is the current position in the file
// by default, if a start value is provided, pos starts at this.start
var { pos, end, bytesRead, fd, encoding } = this;
n =
pos !== undefined // if there is a pos, then we are reading from that specific position in the file
? Math.min(end - pos + 1, n) // takes smaller of length of the rest of the file to read minus the cursor position, or the highwatermark
: Math.min(end - bytesRead + 1, n); // takes the smaller of the length of the rest of the file from the bytes that we have marked read, or the highwatermark
debug("n @ fs.ReadStream.#internalRead, after clamp", n);
// If n is 0 or less, then we read all the file, push null to stream, ending it
if (n <= 0) {
this.push(null);
return;
}
// At this point, n is the lesser of the length of the rest of the file to read or the highwatermark
// Which means n is the maximum number of bytes to read
// Basically if we don't know the file size yet, then check it
// Then if n is bigger than fileSize, set n to be fileSize
// This is a fast path to avoid allocating more than the file size for a small file (is this respected by native stream though)
if (this.#fileSize === -1 && bytesRead === 0 && pos === undefined) {
var stat = fstatSync(fd);
this.#fileSize = stat.size;
if (this.#fileSize > 0 && n > this.#fileSize) {
n = this.#fileSize + 1;
}
debug("fileSize", this.#fileSize);
}
// At this point, we know the file size and how much we want to read of the file
this[kIoDone] = false;
var res = super._read(n);
debug("res -- undefined? why?", res);
if (isPromise(res)) {
var then = res?.then;
if (then && isCallable(then)) {
then(
() => {
this[kIoDone] = true;
// Tell ._destroy() that it's safe to close the fd now.
if (this.destroyed) {
this.emit(kIoDone);
}
},
(er) => {
this[kIoDone] = true;
this.#errorOrDestroy(er);
},
);
}
} else {
this[kIoDone] = true;
if (this.destroyed) {
this.emit(kIoDone);
this.#errorOrDestroy(new Error("ERR_STREAM_PREMATURE_CLOSE"));
}
}
}
#errorOrDestroy(err, sync = null) {
var {
_readableState: r = { destroyed: false, autoDestroy: false },
_writableState: w = { destroyed: false, autoDestroy: false },
} = this;
if (w?.destroyed || r?.destroyed) {
return this;
}
if (r?.autoDestroy || w?.autoDestroy) this.destroy(err);
else if (err) {
this.emit("error", err);
}
}
pause() {
this[readStreamPathFastPathSymbol] = false;
return super.pause();
}
resume() {
this[readStreamPathFastPathSymbol] = false;
return super.resume();
}
unshift(...args) {
this[readStreamPathFastPathSymbol] = false;
return super.unshift(...args);
}
pipe(dest, pipeOpts) {
if (
this[readStreamPathFastPathSymbol] &&
(pipeOpts?.end ?? true) &&
this._readableState?.pipes?.length === 0
) {
if (
writeStreamPathFastPathSymbol in dest &&
dest[writeStreamPathFastPathSymbol]
) {
if (dest[writeStreamPathFastPathCallSymbol](this, pipeOpts)) {
return this;
}
}
}
this[readStreamPathFastPathSymbol] = false;
return super.pipe(dest, pipeOpts);
}
};
return (_lazyReadStream = ReadStream);
}
var internalCreateReadStream = function createReadStream(path, options) {
const ReadStream = getLazyReadStream();
return new ReadStream(path, options);
};
export var createReadStream = internalCreateReadStream;
var _lazyWriteStream;
function getLazyWriteStream() {
if (_lazyWriteStream) return _lazyWriteStream;
const { NativeWritable } = import.meta.require("node:stream");
var defaultWriteStreamOptions = {
fd: null,
start: undefined,
pos: undefined,
encoding: undefined,
flags: "w",
mode: 0o666,
fs: {
write,
close,
open,
openSync,
},
};
var WriteStream = class WriteStream extends NativeWritable {
constructor(path, options = defaultWriteStreamOptions) {
if (!options) {
throw new TypeError("Expected options to be an object");
}
var {
fs = defaultWriteStreamOptions.fs,
start = defaultWriteStreamOptions.start,
flags = defaultWriteStreamOptions.flags,
mode = defaultWriteStreamOptions.mode,
autoClose = true,
emitClose = false,
autoDestroy = autoClose,
encoding = defaultWriteStreamOptions.encoding,
fd = defaultWriteStreamOptions.fd,
pos = defaultWriteStreamOptions.pos,
} = options;
var tempThis = {};
if (typeof path === "string") {
if (path.length === 0) {
throw new TypeError("Expected a non-empty path");
}
if (path.startsWith("file:")) {
path = Bun.fileURLToPath(path);
}
tempThis.path = path;
tempThis.fd = null;
tempThis[writeStreamPathFastPathSymbol] =
autoClose &&
(start === undefined || start === 0) &&
fs.write === defaultWriteStreamOptions.fs.write &&
fs.close === defaultWriteStreamOptions.fs.close;
} else {
tempThis.fd = fd;
tempThis[writeStreamPathFastPathSymbol] = false;
}
if (!tempThis.fd) {
tempThis.fd = fs.openSync(path, flags, mode);
}
super(tempThis.fd, {
...options,
decodeStrings: false,
autoDestroy,
emitClose,
fd: tempThis,
});
Object.assign(this, tempThis);
if (typeof fs?.write !== "function") {
throw new TypeError("Expected fs.write to be a function");
}
if (typeof fs?.close !== "function") {
throw new TypeError("Expected fs.close to be a function");
}
if (typeof fs?.open !== "function") {
throw new TypeError("Expected fs.open to be a function");
}
if (typeof path === "object" && path) {
if (path instanceof URL) {
path = Bun.fileURLToPath(path);
}
}
if (typeof path !== "string" && typeof fd !== "number") {
throw new TypeError("Expected a path or file descriptor");
}
this.start = start;
this.#fs = fs;
this.flags = flags;
this.mode = mode;
if (this.start !== undefined) {
this.pos = this.start;
}
if (encoding !== defaultWriteStreamOptions.encoding) {
this.setDefaultEncoding(encoding);
if (
encoding !== "buffer" &&
encoding !== "utf8" &&
encoding !== "utf-8" &&
encoding !== "binary"
) {
this[writeStreamPathFastPathSymbol] = false;
}
}
}
get autoClose() {
return this._writableState.autoDestroy;
}
set autoClose(val) {
this._writableState.autoDestroy = val;
}
destroySoon = this.end; // TODO: what is this for?
// noop, node has deprecated this
open() {}
path;
fd;
flags;
mode;
#fs;
bytesWritten = 0;
pos;
[writeStreamPathFastPathSymbol];
start;
[writeStreamPathFastPathCallSymbol](readStream, pipeOpts) {
if (!this[writeStreamPathFastPathSymbol]) {
return false;
}
if (this.fd !== null) {
this[writeStreamPathFastPathSymbol] = false;
return false;
}
this[kIoDone] = false;
readStream[kIoDone] = false;
return Bun.write(
this[writeStreamPathFastPathSymbol],
readStream[readStreamPathOrFdSymbol],
).then(
(bytesWritten) => {
readStream[kIoDone] = this[kIoDone] = true;
this.bytesWritten += bytesWritten;
readStream.bytesRead += bytesWritten;
this.end();
readStream.close();
},
(err) => {
readStream[kIoDone] = this[kIoDone] = true;
this.#errorOrDestroy(err);
readStream.emit("error", err);
},
);
}
isBunFastPathEnabled() {
return this[writeStreamPathFastPathSymbol];
}
disableBunFastPath() {
this[writeStreamPathFastPathSymbol] = false;
}
#handleWrite(er, bytes) {
if (er) {
return this.#errorOrDestroy(er);
}
this.bytesWritten += bytes;
}
#internalClose(err, cb) {
this[writeStreamPathFastPathSymbol] = false;
var fd = this.fd;
this.#fs.close(fd, (er) => {
this.fd = null;
cb(err || er);
});
}
_construct(callback) {
if (typeof this.fd === "number") {
callback();
return;
}
callback();
this.emit("open", this.fd);
this.emit("ready");
}
_destroy(err, cb) {
if (this.fd === null) {
return cb(err);
}
if (this[kIoDone]) {
this.once(kIoDone, () => this.#internalClose(err, cb));
return;
}
this.#internalClose(err, cb);
}
[kIoDone] = false;
close(cb) {
if (cb) {
if (this.closed) {
process.nextTick(cb);
return;
}
this.on("close", cb);
}
// If we are not autoClosing, we should call
// destroy on 'finish'.
if (!this.autoClose) {
this.on("finish", this.destroy);
}
// We use end() instead of destroy() because of
// https://github.com/nodejs/node/issues/2006
this.end();
}
write(chunk, encoding = this._writableState.defaultEncoding, cb) {
this[writeStreamPathFastPathSymbol] = false;
if (typeof chunk === "string") {
chunk = Buffer.from(chunk, encoding);
}
// TODO: Replace this when something like lseek is available
var native = this.pos === undefined;
this[kIoDone] = true;
return super.write(
chunk,
encoding,
native
? (err, bytes) => {
this[kIoDone] = false;
this.#handleWrite(err, bytes);
this.emit(kIoDone);
if (cb) !err ? cb() : cb(err);
}
: () => {},
native,
);
}
#internalWriteSlow(chunk, encoding, cb) {
this.#fs.write(
this.fd,
chunk,
0,
chunk.length,
this.pos,
(err, bytes) => {
this[kIoDone] = false;
this.#handleWrite(err, bytes);
this.emit(kIoDone);
!err ? cb() : cb(err);
},
);
}
end(chunk, encoding, cb) {
var native = this.pos === undefined;
return super.end(chunk, encoding, cb, native);
}
_write = this.#internalWriteSlow;
_writev = undefined;
get pending() {
return this.fd === null;
}
_destroy(err, cb) {
this.close(err, cb);
}
#errorOrDestroy(err) {
var {
_readableState: r = { destroyed: false, autoDestroy: false },
_writableState: w = { destroyed: false, autoDestroy: false },
} = this;
if (w?.destroyed || r?.destroyed) {
return this;
}
if (r?.autoDestroy || w?.autoDestroy) this.destroy(err);
else if (err) {
this.emit("error", err);
}
}
};
return (_lazyWriteStream = WriteStream);
}
var internalCreateWriteStream = function createWriteStream(path, options) {
const WriteStream = getLazyWriteStream();
return new WriteStream(path, options);
};
export var createWriteStream = internalCreateWriteStream;
Object.defineProperties(fs, {
createReadStream: {
value: internalCreateReadStream,
},
createWriteStream: {
value: createWriteStream,
},
});
export var promises = {
access: promisify(fs.accessSync),
appendFile: promisify(fs.appendFileSync),
chmod: promisify(fs.chmodSync),
chown: promisify(fs.chownSync),
close: promisify(fs.closeSync),
copyFile: promisify(fs.copyFileSync),
exists: promisify(fs.existsSync),
fchmod: promisify(fs.fchmodSync),
fchown: promisify(fs.fchownSync),
fstat: promisify(fs.fstatSync),
fsync: promisify(fs.fsyncSync),
ftruncate: promisify(fs.ftruncateSync),
futimes: promisify(fs.futimesSync),
lchmod: promisify(fs.lchmodSync),
lchown: promisify(fs.lchownSync),
link: promisify(fs.linkSync),
lstat: promisify(fs.lstatSync),
lutimes: promisify(fs.lutimesSync),
mkdir: promisify(fs.mkdirSync),
mkdtemp: promisify(fs.mkdtempSync),
open: promisify(fs.openSync),
read: promisify(fs.readSync),
readdir: promisify(fs.readdirSync),
readlink: promisify(fs.readlinkSync),
realpath: promisify(fs.realpathSync),
rename: promisify(fs.renameSync),
rm: promisify(fs.rmSync),
stat: promisify(fs.statSync),
symlink: promisify(fs.symlinkSync),
truncate: promisify(fs.truncateSync),
unlink: promisify(fs.unlinkSync),
utimes: promisify(fs.utimesSync),
write: promisify(fs.writeSync),
writeFile: promisify(fs.writeFileSync),
};
promises.readFile = promises.readfile = promisify(fs.readFileSync);
// lol
realpath.native = realpath;
realpathSync.native = realpathSync;
export default {
[Symbol.for("CommonJS")]: 0,
access,
accessSync,
appendFile,
appendFileSync,
chmod,
chmodSync,
chown,
chownSync,
close,
closeSync,
constants,
copyFile,
copyFileSync,
createReadStream,
createWriteStream,
exists,
existsSync,
fchmod,
fchmodSync,
fchown,
fchownSync,
fstat,
fstatSync,
fsync,
fsyncSync,
ftruncate,
ftruncateSync,
futimes,
futimesSync,
lchmod,
lchmodSync,
lchown,
lchownSync,
link,
linkSync,
lstat,
lstatSync,
lutimes,
lutimesSync,
mkdir,
mkdirSync,
mkdtemp,
mkdtempSync,
open,
openSync,
promises,
read,
readFile,
readFileSync,
readSync,
readdir,
readdirSync,
readlink,
readlinkSync,
realpath,
realpathSync,
rename,
renameSync,
rm,
rmSync,
stat,
statSync,
symlink,
symlinkSync,
truncate,
truncateSync,
unlink,
unlinkSync,
utimes,
utimesSync,
write,
writeFile,
writeFileSync,
writeSync,
};
|