deno.land / std@0.157.0 / node / internal / fs / streams.ts

نووسراو ببینە
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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.// Copyright Joyent, Inc. and Node.js contributors. All rights reserved. MIT license.
import { Writable } from "../../stream.ts";import { toPathIfFileURL } from "../url.ts";import { open, type openFlags } from "../../_fs/_fs_open.ts";import { write } from "../../_fs/_fs_write.mjs";import { close } from "../../_fs/_fs_close.ts";import { Buffer } from "../../buffer.ts";import { notImplemented } from "../../_utils.ts";import type { WritableOptions } from "../../_stream.d.ts";
const kFs = Symbol("kFs");const kIsPerformingIO = Symbol("kIsPerformingIO");const kIoDone = Symbol("kIoDone");
interface WriteStreamOptions { flags?: openFlags; encoding?: string; fd?: number | null; mode?: number; autoClose?: boolean; emitClose?: boolean; start?: number; fs?: FS;}
interface FS { open: typeof open; write: typeof write; close: typeof close;}
export interface WriteStream extends Writable { fd: number | null; path: string | Buffer; flags: openFlags; mode: number; bytesWritten: number; pos: number; [kFs]: FS; [kIsPerformingIO]: boolean;}
export function WriteStream( this: WriteStream | unknown, path: string | Buffer, options?: WriteStreamOptions & WritableOptions,): WriteStream { const hasBadOptions = options && ( options.fd || options.start ); if (hasBadOptions) { notImplemented( `fs.WriteStream.prototype.constructor with unsupported options (${ JSON.stringify(options) })`, ); }
if (!(this instanceof WriteStream)) { // deno-lint-ignore ban-ts-comment // @ts-ignore return new WriteStream(path, options); }
const self = this as WriteStream;
Writable.call(self, { highWaterMark: options?.highWaterMark, decodeStrings: options?.decodeStrings, defaultEncoding: options?.defaultEncoding, objectMode: options?.objectMode, emitClose: options?.emitClose, autoDestroy: options?.autoClose ?? options?.autoDestroy, signal: options?.signal, });
self.fd = null; self.path = toPathIfFileURL(path); self.flags = options?.flags ?? "w"; self.mode = options?.mode ?? 0o666; self.bytesWritten = 0; self.pos = 0; self[kFs] = options?.fs ?? { open, write, close }; self[kIsPerformingIO] = false;
if (options?.encoding) { self.setDefaultEncoding(options?.encoding); }
return self;}
Object.setPrototypeOf(WriteStream.prototype, Writable.prototype);
WriteStream.prototype._construct = function ( this: WriteStream, callback: (err?: Error) => void,) { this[kFs].open( this.path.toString(), this.flags, this.mode, (err: Error | null, fd: number) => { if (err) { callback(err); return; }
this.fd = fd; callback(); this.emit("open", this.fd); this.emit("ready"); }, );};
WriteStream.prototype._write = function ( this: WriteStream, data: Buffer, _encoding: string, cb: (err?: Error | null) => void,) { this[kIsPerformingIO] = true; this[kFs].write( this.fd!, data, 0, data.length, this.pos, (er: Error | null, bytes: number) => { this[kIsPerformingIO] = false; if (this.destroyed) { // Tell ._destroy() that it's safe to close the fd now. cb(er); return this.emit(kIoDone, er); }
if (er) { return cb(er); }
this.bytesWritten += bytes; cb(); }, );
if (this.pos !== undefined) { this.pos += data.length; }};
WriteStream.prototype._destroy = function ( this: WriteStream, err: Error, cb: (err?: Error | null) => void,) { if (this[kIsPerformingIO]) { this.once(kIoDone, (er: Error) => closeStream(this, err || er, cb)); } else { closeStream(this, err, cb); }};
function closeStream( stream: WriteStream, err: Error, cb: (err?: Error | null) => void,) { if (!stream.fd) { cb(err); } else { stream[kFs].close(stream.fd, (er?: Error | null) => { cb(er || err); }); stream.fd = null; }}
export function createWriteStream( path: string | Buffer, options?: WriteStreamOptions,): WriteStream { // deno-lint-ignore ban-ts-comment // @ts-ignore return new WriteStream(path, options);}
std

Version Info

Tagged at
2 years ago