-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.mjs
239 lines (192 loc) · 5.95 KB
/
index.mjs
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
import crypto from "crypto";
import fs from "fs";
import os from "os";
import path from "path";
export class ReadAfterDestroyedError extends Error {}
export class ReadStream extends fs.ReadStream {
constructor(writeStream, name) {
super("", {});
this.name = name;
this._writeStream = writeStream;
// Persist terminating events.
this.error = this._writeStream.error;
this.addListener("error", error => {
this.error = error;
});
this.open();
}
get ended() {
return this._readableState.ended;
}
_read(n) {
if (typeof this.fd !== "number")
return this.once("open", function() {
this._read(n);
});
// The writer has finished, so the reader can continue uninterupted.
if (this._writeStream.finished || this._writeStream.closed)
return super._read(n);
// Make sure there's something to read.
const unread = this._writeStream.bytesWritten - this.bytesRead;
if (unread === 0) {
const retry = () => {
this._writeStream.removeListener("finish", retry);
this._writeStream.removeListener("write", retry);
this._read(n);
};
this._writeStream.addListener("finish", retry);
this._writeStream.addListener("write", retry);
return;
}
// Make sure we don't get ahead of our writer.
return super._read(Math.min(n, unread));
}
_destroy(error, callback) {
if (typeof this.fd !== "number") {
this.once("open", this._destroy.bind(this, error, callback));
return;
}
fs.close(this.fd, closeError => {
callback(closeError || error);
this.fd = null;
this.closed = true;
this.emit("close");
});
}
open() {
if (!this._writeStream) return;
if (typeof this._writeStream.fd !== "number") {
this._writeStream.once("open", () => this.open());
return;
}
this.path = this._writeStream.path;
super.open();
}
}
export class WriteStream extends fs.WriteStream {
constructor() {
super("", {
autoClose: false
});
this._readStreams = new Set();
this.error = null;
this._cleanupSync = () => {
process.removeListener("exit", this._cleanupSync);
process.removeListener("SIGINT", this._cleanupSync);
if (typeof this.fd === "number")
try {
fs.closeSync(this.fd);
} catch (error) {
// An error here probably means the fd was already closed, but we can
// still try to unlink the file.
}
try {
fs.unlinkSync(this.path);
} catch (error) {
// If we are unable to unlink the file, the operating system will clean up
// on next restart, since we use store thes in `os.tmpdir()`
}
};
}
get finished() {
return this._writableState.finished;
}
open() {
// generage a random tmp path
crypto.randomBytes(16, (error, buffer) => {
if (error) {
this.destroy(error);
return;
}
this.path = path.join(
os.tmpdir(),
`capacitor-${buffer.toString("hex")}.tmp`
);
// create the file
fs.open(this.path, "wx", this.mode, (error, fd) => {
if (error) {
this.destroy(error);
return;
}
// cleanup when our stream closes or when the process exits
process.addListener("exit", this._cleanupSync);
process.addListener("SIGINT", this._cleanupSync);
this.fd = fd;
this.emit("open", fd);
this.emit("ready");
});
});
}
_write(chunk, encoding, callback) {
super._write(chunk, encoding, error => {
if (!error) this.emit("write");
callback(error);
});
}
_destroy(error, callback) {
if (typeof this.fd !== "number") {
this.once("open", this._destroy.bind(this, error, callback));
return;
}
process.removeListener("exit", this._cleanupSync);
process.removeListener("SIGINT", this._cleanupSync);
const unlink = error => {
fs.unlink(this.path, unlinkError => {
// If we are unable to unlink the file, the operating system will
// clean up on next restart, since we use store thes in `os.tmpdir()`
callback(unlinkError || error);
this.fd = null;
this.closed = true;
this.emit("close");
});
};
if (typeof this.fd === "number") {
fs.close(this.fd, closeError => {
// An error here probably means the fd was already closed, but we can
// still try to unlink the file.
unlink(closeError || error);
});
return;
}
unlink(error);
}
destroy(error, callback) {
if (error) this.error = error;
// This is already destroyed.
if (this.destroyed) return super.destroy(error, callback);
// Call the callback once destroyed.
if (typeof callback === "function")
this.once("close", callback.bind(this, error));
// All read streams have terminated, so we can destroy this.
if (this._readStreams.size === 0) {
super.destroy(error, callback);
return;
}
// Wait until all read streams have terminated before destroying this.
this._destroyPending = true;
// If there is an error, destroy all read streams with the error.
if (error)
for (let readStream of this._readStreams) readStream.destroy(error);
}
createReadStream(name) {
if (this.destroyed)
throw new ReadAfterDestroyedError(
"A ReadStream cannot be created from a destroyed WriteStream."
);
const readStream = new ReadStream(this, name);
this._readStreams.add(readStream);
const remove = () => {
this._deleteReadStream(readStream);
readStream.removeListener("end", remove);
readStream.removeListener("close", remove);
};
readStream.addListener("end", remove);
readStream.addListener("close", remove);
return readStream;
}
_deleteReadStream(readStream) {
if (this._readStreams.delete(readStream) && this._destroyPending)
this.destroy();
}
}
export default WriteStream;