upload.js
13.5 KB
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GridFSBucketWriteStream = void 0;
const stream_1 = require("stream");
const bson_1 = require("../bson");
const error_1 = require("../error");
const utils_1 = require("../utils");
const write_concern_1 = require("./../write_concern");
/**
* A writable stream that enables you to write buffers to GridFS.
*
* Do not instantiate this class directly. Use `openUploadStream()` instead.
* @public
*/
class GridFSBucketWriteStream extends stream_1.Writable {
/** @internal
* @param bucket - Handle for this stream's corresponding bucket
* @param filename - The value of the 'filename' key in the files doc
* @param options - Optional settings.
*/
constructor(bucket, filename, options) {
super();
options = options !== null && options !== void 0 ? options : {};
this.bucket = bucket;
this.chunks = bucket.s._chunksCollection;
this.filename = filename;
this.files = bucket.s._filesCollection;
this.options = options;
this.writeConcern = write_concern_1.WriteConcern.fromOptions(options) || bucket.s.options.writeConcern;
// Signals the write is all done
this.done = false;
this.id = options.id ? options.id : new bson_1.ObjectId();
// properly inherit the default chunksize from parent
this.chunkSizeBytes = options.chunkSizeBytes || this.bucket.s.options.chunkSizeBytes;
this.bufToStore = Buffer.alloc(this.chunkSizeBytes);
this.length = 0;
this.n = 0;
this.pos = 0;
this.state = {
streamEnd: false,
outstandingRequests: 0,
errored: false,
aborted: false
};
if (!this.bucket.s.calledOpenUploadStream) {
this.bucket.s.calledOpenUploadStream = true;
checkIndexes(this, () => {
this.bucket.s.checkedIndexes = true;
this.bucket.emit('index');
});
}
}
write(chunk, encodingOrCallback, callback) {
const encoding = typeof encodingOrCallback === 'function' ? undefined : encodingOrCallback;
callback = typeof encodingOrCallback === 'function' ? encodingOrCallback : callback;
return waitForIndexes(this, () => doWrite(this, chunk, encoding, callback));
}
abort(callback) {
return (0, utils_1.maybePromise)(callback, callback => {
if (this.state.streamEnd) {
// TODO(NODE-3485): Replace with MongoGridFSStreamClosed
return callback(new error_1.MongoAPIError('Cannot abort a stream that has already completed'));
}
if (this.state.aborted) {
// TODO(NODE-3485): Replace with MongoGridFSStreamClosed
return callback(new error_1.MongoAPIError('Cannot call abort() on a stream twice'));
}
this.state.aborted = true;
this.chunks.deleteMany({ files_id: this.id }, error => callback(error));
});
}
end(chunkOrCallback, encodingOrCallback, callback) {
const chunk = typeof chunkOrCallback === 'function' ? undefined : chunkOrCallback;
const encoding = typeof encodingOrCallback === 'function' ? undefined : encodingOrCallback;
callback =
typeof chunkOrCallback === 'function'
? chunkOrCallback
: typeof encodingOrCallback === 'function'
? encodingOrCallback
: callback;
if (checkAborted(this, callback))
return this;
this.state.streamEnd = true;
if (callback) {
this.once(GridFSBucketWriteStream.FINISH, (result) => {
if (callback)
callback(undefined, result);
});
}
if (!chunk) {
waitForIndexes(this, () => !!writeRemnant(this));
return this;
}
this.write(chunk, encoding, () => {
writeRemnant(this);
});
return this;
}
}
exports.GridFSBucketWriteStream = GridFSBucketWriteStream;
/** @event */
GridFSBucketWriteStream.CLOSE = 'close';
/** @event */
GridFSBucketWriteStream.ERROR = 'error';
/**
* `end()` was called and the write stream successfully wrote the file metadata and all the chunks to MongoDB.
* @event
*/
GridFSBucketWriteStream.FINISH = 'finish';
function __handleError(stream, error, callback) {
if (stream.state.errored) {
return;
}
stream.state.errored = true;
if (callback) {
return callback(error);
}
stream.emit(GridFSBucketWriteStream.ERROR, error);
}
function createChunkDoc(filesId, n, data) {
return {
_id: new bson_1.ObjectId(),
files_id: filesId,
n,
data
};
}
function checkChunksIndex(stream, callback) {
stream.chunks.listIndexes().toArray((error, indexes) => {
let index;
if (error) {
// Collection doesn't exist so create index
if (error instanceof error_1.MongoError && error.code === error_1.MONGODB_ERROR_CODES.NamespaceNotFound) {
index = { files_id: 1, n: 1 };
stream.chunks.createIndex(index, { background: false, unique: true }, error => {
if (error) {
return callback(error);
}
callback();
});
return;
}
return callback(error);
}
let hasChunksIndex = false;
if (indexes) {
indexes.forEach((index) => {
if (index.key) {
const keys = Object.keys(index.key);
if (keys.length === 2 && index.key.files_id === 1 && index.key.n === 1) {
hasChunksIndex = true;
}
}
});
}
if (hasChunksIndex) {
callback();
}
else {
index = { files_id: 1, n: 1 };
const writeConcernOptions = getWriteOptions(stream);
stream.chunks.createIndex(index, {
...writeConcernOptions,
background: true,
unique: true
}, callback);
}
});
}
function checkDone(stream, callback) {
if (stream.done)
return true;
if (stream.state.streamEnd && stream.state.outstandingRequests === 0 && !stream.state.errored) {
// Set done so we do not trigger duplicate createFilesDoc
stream.done = true;
// Create a new files doc
const filesDoc = createFilesDoc(stream.id, stream.length, stream.chunkSizeBytes, stream.filename, stream.options.contentType, stream.options.aliases, stream.options.metadata);
if (checkAborted(stream, callback)) {
return false;
}
stream.files.insertOne(filesDoc, getWriteOptions(stream), (error) => {
if (error) {
return __handleError(stream, error, callback);
}
stream.emit(GridFSBucketWriteStream.FINISH, filesDoc);
stream.emit(GridFSBucketWriteStream.CLOSE);
});
return true;
}
return false;
}
function checkIndexes(stream, callback) {
stream.files.findOne({}, { projection: { _id: 1 } }, (error, doc) => {
if (error) {
return callback(error);
}
if (doc) {
return callback();
}
stream.files.listIndexes().toArray((error, indexes) => {
let index;
if (error) {
// Collection doesn't exist so create index
if (error instanceof error_1.MongoError && error.code === error_1.MONGODB_ERROR_CODES.NamespaceNotFound) {
index = { filename: 1, uploadDate: 1 };
stream.files.createIndex(index, { background: false }, (error) => {
if (error) {
return callback(error);
}
checkChunksIndex(stream, callback);
});
return;
}
return callback(error);
}
let hasFileIndex = false;
if (indexes) {
indexes.forEach((index) => {
const keys = Object.keys(index.key);
if (keys.length === 2 && index.key.filename === 1 && index.key.uploadDate === 1) {
hasFileIndex = true;
}
});
}
if (hasFileIndex) {
checkChunksIndex(stream, callback);
}
else {
index = { filename: 1, uploadDate: 1 };
const writeConcernOptions = getWriteOptions(stream);
stream.files.createIndex(index, {
...writeConcernOptions,
background: false
}, (error) => {
if (error) {
return callback(error);
}
checkChunksIndex(stream, callback);
});
}
});
});
}
function createFilesDoc(_id, length, chunkSize, filename, contentType, aliases, metadata) {
const ret = {
_id,
length,
chunkSize,
uploadDate: new Date(),
filename
};
if (contentType) {
ret.contentType = contentType;
}
if (aliases) {
ret.aliases = aliases;
}
if (metadata) {
ret.metadata = metadata;
}
return ret;
}
function doWrite(stream, chunk, encoding, callback) {
if (checkAborted(stream, callback)) {
return false;
}
const inputBuf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk, encoding);
stream.length += inputBuf.length;
// Input is small enough to fit in our buffer
if (stream.pos + inputBuf.length < stream.chunkSizeBytes) {
inputBuf.copy(stream.bufToStore, stream.pos);
stream.pos += inputBuf.length;
callback && callback();
// Note that we reverse the typical semantics of write's return value
// to be compatible with node's `.pipe()` function.
// True means client can keep writing.
return true;
}
// Otherwise, buffer is too big for current chunk, so we need to flush
// to MongoDB.
let inputBufRemaining = inputBuf.length;
let spaceRemaining = stream.chunkSizeBytes - stream.pos;
let numToCopy = Math.min(spaceRemaining, inputBuf.length);
let outstandingRequests = 0;
while (inputBufRemaining > 0) {
const inputBufPos = inputBuf.length - inputBufRemaining;
inputBuf.copy(stream.bufToStore, stream.pos, inputBufPos, inputBufPos + numToCopy);
stream.pos += numToCopy;
spaceRemaining -= numToCopy;
let doc;
if (spaceRemaining === 0) {
doc = createChunkDoc(stream.id, stream.n, Buffer.from(stream.bufToStore));
++stream.state.outstandingRequests;
++outstandingRequests;
if (checkAborted(stream, callback)) {
return false;
}
stream.chunks.insertOne(doc, getWriteOptions(stream), (error) => {
if (error) {
return __handleError(stream, error);
}
--stream.state.outstandingRequests;
--outstandingRequests;
if (!outstandingRequests) {
stream.emit('drain', doc);
callback && callback();
checkDone(stream);
}
});
spaceRemaining = stream.chunkSizeBytes;
stream.pos = 0;
++stream.n;
}
inputBufRemaining -= numToCopy;
numToCopy = Math.min(spaceRemaining, inputBufRemaining);
}
// Note that we reverse the typical semantics of write's return value
// to be compatible with node's `.pipe()` function.
// False means the client should wait for the 'drain' event.
return false;
}
function getWriteOptions(stream) {
const obj = {};
if (stream.writeConcern) {
obj.writeConcern = {
w: stream.writeConcern.w,
wtimeout: stream.writeConcern.wtimeout,
j: stream.writeConcern.j
};
}
return obj;
}
function waitForIndexes(stream, callback) {
if (stream.bucket.s.checkedIndexes) {
return callback(false);
}
stream.bucket.once('index', () => {
callback(true);
});
return true;
}
function writeRemnant(stream, callback) {
// Buffer is empty, so don't bother to insert
if (stream.pos === 0) {
return checkDone(stream, callback);
}
++stream.state.outstandingRequests;
// Create a new buffer to make sure the buffer isn't bigger than it needs
// to be.
const remnant = Buffer.alloc(stream.pos);
stream.bufToStore.copy(remnant, 0, 0, stream.pos);
const doc = createChunkDoc(stream.id, stream.n, remnant);
// If the stream was aborted, do not write remnant
if (checkAborted(stream, callback)) {
return false;
}
stream.chunks.insertOne(doc, getWriteOptions(stream), (error) => {
if (error) {
return __handleError(stream, error);
}
--stream.state.outstandingRequests;
checkDone(stream);
});
return true;
}
function checkAborted(stream, callback) {
if (stream.state.aborted) {
if (typeof callback === 'function') {
// TODO(NODE-3485): Replace with MongoGridFSStreamClosedError
callback(new error_1.MongoAPIError('Stream has been aborted'));
}
return true;
}
return false;
}
//# sourceMappingURL=upload.js.map