This commit is contained in:
PLBXNebulia-Formation 2025-11-21 09:23:11 +01:00
commit d1c8cae2c1
1417 changed files with 326736 additions and 0 deletions

305
node_modules/mongodb/lib/gridfs/download.js generated vendored Normal file
View file

@ -0,0 +1,305 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GridFSBucketReadStream = void 0;
const stream_1 = require("stream");
const abstract_cursor_1 = require("../cursor/abstract_cursor");
const error_1 = require("../error");
const timeout_1 = require("../timeout");
/**
* A readable stream that enables you to read buffers from GridFS.
*
* Do not instantiate this class directly. Use `openDownloadStream()` instead.
* @public
*/
class GridFSBucketReadStream extends stream_1.Readable {
/**
* Fires when the stream loaded the file document corresponding to the provided id.
* @event
*/
static { this.FILE = 'file'; }
/**
* @param chunks - Handle for chunks collection
* @param files - Handle for files collection
* @param readPreference - The read preference to use
* @param filter - The filter to use to find the file document
* @internal
*/
constructor(chunks, files, readPreference, filter, options) {
super({ emitClose: true });
this.s = {
bytesToTrim: 0,
bytesToSkip: 0,
bytesRead: 0,
chunks,
expected: 0,
files,
filter,
init: false,
expectedEnd: 0,
options: {
start: 0,
end: 0,
...options
},
readPreference,
timeoutContext: options?.timeoutMS != null
? new timeout_1.CSOTTimeoutContext({ timeoutMS: options.timeoutMS, serverSelectionTimeoutMS: 0 })
: undefined
};
}
/**
* Reads from the cursor and pushes to the stream.
* Private Impl, do not call directly
* @internal
*/
_read() {
if (this.destroyed)
return;
waitForFile(this, () => doRead(this));
}
/**
* Sets the 0-based offset in bytes to start streaming from. Throws
* an error if this stream has entered flowing mode
* (e.g. if you've already called `on('data')`)
*
* @param start - 0-based offset in bytes to start streaming from
*/
start(start = 0) {
throwIfInitialized(this);
this.s.options.start = start;
return this;
}
/**
* Sets the 0-based offset in bytes to start streaming from. Throws
* an error if this stream has entered flowing mode
* (e.g. if you've already called `on('data')`)
*
* @param end - Offset in bytes to stop reading at
*/
end(end = 0) {
throwIfInitialized(this);
this.s.options.end = end;
return this;
}
/**
* Marks this stream as aborted (will never push another `data` event)
* and kills the underlying cursor. Will emit the 'end' event, and then
* the 'close' event once the cursor is successfully killed.
*/
async abort() {
this.push(null);
this.destroy();
const remainingTimeMS = this.s.timeoutContext?.getRemainingTimeMSOrThrow();
await this.s.cursor?.close({ timeoutMS: remainingTimeMS });
}
}
exports.GridFSBucketReadStream = GridFSBucketReadStream;
function throwIfInitialized(stream) {
if (stream.s.init) {
throw new error_1.MongoGridFSStreamError('Options cannot be changed after the stream is initialized');
}
}
function doRead(stream) {
if (stream.destroyed)
return;
if (!stream.s.cursor)
return;
if (!stream.s.file)
return;
const handleReadResult = (doc) => {
if (stream.destroyed)
return;
if (!doc) {
stream.push(null);
stream.s.cursor?.close().then(undefined, error => stream.destroy(error));
return;
}
if (!stream.s.file)
return;
const bytesRemaining = stream.s.file.length - stream.s.bytesRead;
const expectedN = stream.s.expected++;
const expectedLength = Math.min(stream.s.file.chunkSize, bytesRemaining);
if (doc.n > expectedN) {
return stream.destroy(new error_1.MongoGridFSChunkError(`ChunkIsMissing: Got unexpected n: ${doc.n}, expected: ${expectedN}`));
}
if (doc.n < expectedN) {
return stream.destroy(new error_1.MongoGridFSChunkError(`ExtraChunk: Got unexpected n: ${doc.n}, expected: ${expectedN}`));
}
let buf = Buffer.isBuffer(doc.data) ? doc.data : doc.data.buffer;
if (buf.byteLength !== expectedLength) {
if (bytesRemaining <= 0) {
return stream.destroy(new error_1.MongoGridFSChunkError(`ExtraChunk: Got unexpected n: ${doc.n}, expected file length ${stream.s.file.length} bytes but already read ${stream.s.bytesRead} bytes`));
}
return stream.destroy(new error_1.MongoGridFSChunkError(`ChunkIsWrongSize: Got unexpected length: ${buf.byteLength}, expected: ${expectedLength}`));
}
stream.s.bytesRead += buf.byteLength;
if (buf.byteLength === 0) {
return stream.push(null);
}
let sliceStart = null;
let sliceEnd = null;
if (stream.s.bytesToSkip != null) {
sliceStart = stream.s.bytesToSkip;
stream.s.bytesToSkip = 0;
}
const atEndOfStream = expectedN === stream.s.expectedEnd - 1;
const bytesLeftToRead = stream.s.options.end - stream.s.bytesToSkip;
if (atEndOfStream && stream.s.bytesToTrim != null) {
sliceEnd = stream.s.file.chunkSize - stream.s.bytesToTrim;
}
else if (stream.s.options.end && bytesLeftToRead < doc.data.byteLength) {
sliceEnd = bytesLeftToRead;
}
if (sliceStart != null || sliceEnd != null) {
buf = buf.slice(sliceStart || 0, sliceEnd || buf.byteLength);
}
stream.push(buf);
return;
};
stream.s.cursor.next().then(handleReadResult, error => {
if (stream.destroyed)
return;
stream.destroy(error);
});
}
function init(stream) {
const findOneOptions = {};
if (stream.s.readPreference) {
findOneOptions.readPreference = stream.s.readPreference;
}
if (stream.s.options && stream.s.options.sort) {
findOneOptions.sort = stream.s.options.sort;
}
if (stream.s.options && stream.s.options.skip) {
findOneOptions.skip = stream.s.options.skip;
}
const handleReadResult = (doc) => {
if (stream.destroyed)
return;
if (!doc) {
const identifier = stream.s.filter._id
? stream.s.filter._id.toString()
: stream.s.filter.filename;
const errmsg = `FileNotFound: file ${identifier} was not found`;
// TODO(NODE-3483)
const err = new error_1.MongoRuntimeError(errmsg);
err.code = 'ENOENT'; // TODO: NODE-3338 set property as part of constructor
return stream.destroy(err);
}
// If document is empty, kill the stream immediately and don't
// execute any reads
if (doc.length <= 0) {
stream.push(null);
return;
}
if (stream.destroyed) {
// If user destroys the stream before we have a cursor, wait
// until the query is done to say we're 'closed' because we can't
// cancel a query.
stream.destroy();
return;
}
try {
stream.s.bytesToSkip = handleStartOption(stream, doc, stream.s.options);
}
catch (error) {
return stream.destroy(error);
}
const filter = { files_id: doc._id };
// Currently (MongoDB 3.4.4) skip function does not support the index,
// it needs to retrieve all the documents first and then skip them. (CS-25811)
// As work around we use $gte on the "n" field.
if (stream.s.options && stream.s.options.start != null) {
const skip = Math.floor(stream.s.options.start / doc.chunkSize);
if (skip > 0) {
filter['n'] = { $gte: skip };
}
}
let remainingTimeMS;
try {
remainingTimeMS = stream.s.timeoutContext?.getRemainingTimeMSOrThrow(`Download timed out after ${stream.s.timeoutContext?.timeoutMS}ms`);
}
catch (error) {
return stream.destroy(error);
}
stream.s.cursor = stream.s.chunks
.find(filter, {
timeoutMode: stream.s.options.timeoutMS != null ? abstract_cursor_1.CursorTimeoutMode.LIFETIME : undefined,
timeoutMS: remainingTimeMS
})
.sort({ n: 1 });
if (stream.s.readPreference) {
stream.s.cursor.withReadPreference(stream.s.readPreference);
}
stream.s.expectedEnd = Math.ceil(doc.length / doc.chunkSize);
stream.s.file = doc;
try {
stream.s.bytesToTrim = handleEndOption(stream, doc, stream.s.cursor, stream.s.options);
}
catch (error) {
return stream.destroy(error);
}
stream.emit(GridFSBucketReadStream.FILE, doc);
return;
};
let remainingTimeMS;
try {
remainingTimeMS = stream.s.timeoutContext?.getRemainingTimeMSOrThrow(`Download timed out after ${stream.s.timeoutContext?.timeoutMS}ms`);
}
catch (error) {
if (!stream.destroyed)
stream.destroy(error);
return;
}
findOneOptions.timeoutMS = remainingTimeMS;
stream.s.files.findOne(stream.s.filter, findOneOptions).then(handleReadResult, error => {
if (stream.destroyed)
return;
stream.destroy(error);
});
}
function waitForFile(stream, callback) {
if (stream.s.file) {
return callback();
}
if (!stream.s.init) {
init(stream);
stream.s.init = true;
}
stream.once('file', () => {
callback();
});
}
function handleStartOption(stream, doc, options) {
if (options && options.start != null) {
if (options.start > doc.length) {
throw new error_1.MongoInvalidArgumentError(`Stream start (${options.start}) must not be more than the length of the file (${doc.length})`);
}
if (options.start < 0) {
throw new error_1.MongoInvalidArgumentError(`Stream start (${options.start}) must not be negative`);
}
if (options.end != null && options.end < options.start) {
throw new error_1.MongoInvalidArgumentError(`Stream start (${options.start}) must not be greater than stream end (${options.end})`);
}
stream.s.bytesRead = Math.floor(options.start / doc.chunkSize) * doc.chunkSize;
stream.s.expected = Math.floor(options.start / doc.chunkSize);
return options.start - stream.s.bytesRead;
}
throw new error_1.MongoInvalidArgumentError('Start option must be defined');
}
function handleEndOption(stream, doc, cursor, options) {
if (options && options.end != null) {
if (options.end > doc.length) {
throw new error_1.MongoInvalidArgumentError(`Stream end (${options.end}) must not be more than the length of the file (${doc.length})`);
}
if (options.start == null || options.start < 0) {
throw new error_1.MongoInvalidArgumentError(`Stream end (${options.end}) must not be negative`);
}
const start = options.start != null ? Math.floor(options.start / doc.chunkSize) : 0;
cursor.limit(Math.ceil(options.end / doc.chunkSize) - start);
stream.s.expectedEnd = Math.ceil(options.end / doc.chunkSize);
return Math.ceil(options.end / doc.chunkSize) * doc.chunkSize - options.end;
}
throw new error_1.MongoInvalidArgumentError('End option must be defined');
}
//# sourceMappingURL=download.js.map

1
node_modules/mongodb/lib/gridfs/download.js.map generated vendored Normal file

File diff suppressed because one or more lines are too long

164
node_modules/mongodb/lib/gridfs/index.js generated vendored Normal file
View file

@ -0,0 +1,164 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GridFSBucket = void 0;
const error_1 = require("../error");
const mongo_types_1 = require("../mongo_types");
const timeout_1 = require("../timeout");
const utils_1 = require("../utils");
const write_concern_1 = require("../write_concern");
const download_1 = require("./download");
const upload_1 = require("./upload");
const DEFAULT_GRIDFS_BUCKET_OPTIONS = {
bucketName: 'fs',
chunkSizeBytes: 255 * 1024
};
/**
* Constructor for a streaming GridFS interface
* @public
*/
class GridFSBucket extends mongo_types_1.TypedEventEmitter {
/**
* When the first call to openUploadStream is made, the upload stream will
* check to see if it needs to create the proper indexes on the chunks and
* files collections. This event is fired either when 1) it determines that
* no index creation is necessary, 2) when it successfully creates the
* necessary indexes.
* @event
*/
static { this.INDEX = 'index'; }
constructor(db, options) {
super();
this.on('error', utils_1.noop);
this.setMaxListeners(0);
const privateOptions = (0, utils_1.resolveOptions)(db, {
...DEFAULT_GRIDFS_BUCKET_OPTIONS,
...options,
writeConcern: write_concern_1.WriteConcern.fromOptions(options)
});
this.s = {
db,
options: privateOptions,
_chunksCollection: db.collection(privateOptions.bucketName + '.chunks'),
_filesCollection: db.collection(privateOptions.bucketName + '.files'),
checkedIndexes: false,
calledOpenUploadStream: false
};
}
/**
* Returns a writable stream (GridFSBucketWriteStream) for writing
* buffers to GridFS. The stream's 'id' property contains the resulting
* file's id.
*
* @param filename - The value of the 'filename' key in the files doc
* @param options - Optional settings.
*/
openUploadStream(filename, options) {
return new upload_1.GridFSBucketWriteStream(this, filename, {
timeoutMS: this.s.options.timeoutMS,
...options
});
}
/**
* Returns a writable stream (GridFSBucketWriteStream) for writing
* buffers to GridFS for a custom file id. The stream's 'id' property contains the resulting
* file's id.
*/
openUploadStreamWithId(id, filename, options) {
return new upload_1.GridFSBucketWriteStream(this, filename, {
timeoutMS: this.s.options.timeoutMS,
...options,
id
});
}
/** Returns a readable stream (GridFSBucketReadStream) for streaming file data from GridFS. */
openDownloadStream(id, options) {
return new download_1.GridFSBucketReadStream(this.s._chunksCollection, this.s._filesCollection, this.s.options.readPreference, { _id: id }, { timeoutMS: this.s.options.timeoutMS, ...options });
}
/**
* Deletes a file with the given id
*
* @param id - The id of the file doc
*/
async delete(id, options) {
const { timeoutMS } = (0, utils_1.resolveOptions)(this.s.db, options);
let timeoutContext = undefined;
if (timeoutMS) {
timeoutContext = new timeout_1.CSOTTimeoutContext({
timeoutMS,
serverSelectionTimeoutMS: this.s.db.client.s.options.serverSelectionTimeoutMS
});
}
const { deletedCount } = await this.s._filesCollection.deleteOne({ _id: id }, { timeoutMS: timeoutContext?.remainingTimeMS });
const remainingTimeMS = timeoutContext?.remainingTimeMS;
if (remainingTimeMS != null && remainingTimeMS <= 0)
throw new error_1.MongoOperationTimeoutError(`Timed out after ${timeoutMS}ms`);
// Delete orphaned chunks before returning FileNotFound
await this.s._chunksCollection.deleteMany({ files_id: id }, { timeoutMS: remainingTimeMS });
if (deletedCount === 0) {
// TODO(NODE-3483): Replace with more appropriate error
// Consider creating new error MongoGridFSFileNotFoundError
throw new error_1.MongoRuntimeError(`File not found for id ${id}`);
}
}
/** Convenience wrapper around find on the files collection */
find(filter = {}, options = {}) {
return this.s._filesCollection.find(filter, options);
}
/**
* Returns a readable stream (GridFSBucketReadStream) for streaming the
* file with the given name from GridFS. If there are multiple files with
* the same name, this will stream the most recent file with the given name
* (as determined by the `uploadDate` field). You can set the `revision`
* option to change this behavior.
*/
openDownloadStreamByName(filename, options) {
let sort = { uploadDate: -1 };
let skip = undefined;
if (options && options.revision != null) {
if (options.revision >= 0) {
sort = { uploadDate: 1 };
skip = options.revision;
}
else {
skip = -options.revision - 1;
}
}
return new download_1.GridFSBucketReadStream(this.s._chunksCollection, this.s._filesCollection, this.s.options.readPreference, { filename }, { timeoutMS: this.s.options.timeoutMS, ...options, sort, skip });
}
/**
* Renames the file with the given _id to the given string
*
* @param id - the id of the file to rename
* @param filename - new name for the file
*/
async rename(id, filename, options) {
const filter = { _id: id };
const update = { $set: { filename } };
const { matchedCount } = await this.s._filesCollection.updateOne(filter, update, options);
if (matchedCount === 0) {
throw new error_1.MongoRuntimeError(`File with id ${id} not found`);
}
}
/** Removes this bucket's files collection, followed by its chunks collection. */
async drop(options) {
const { timeoutMS } = (0, utils_1.resolveOptions)(this.s.db, options);
let timeoutContext = undefined;
if (timeoutMS) {
timeoutContext = new timeout_1.CSOTTimeoutContext({
timeoutMS,
serverSelectionTimeoutMS: this.s.db.client.s.options.serverSelectionTimeoutMS
});
}
if (timeoutContext) {
await this.s._filesCollection.drop({ timeoutMS: timeoutContext.remainingTimeMS });
const remainingTimeMS = timeoutContext.getRemainingTimeMSOrThrow(`Timed out after ${timeoutMS}ms`);
await this.s._chunksCollection.drop({ timeoutMS: remainingTimeMS });
}
else {
await this.s._filesCollection.drop();
await this.s._chunksCollection.drop();
}
}
}
exports.GridFSBucket = GridFSBucket;
//# sourceMappingURL=index.js.map

1
node_modules/mongodb/lib/gridfs/index.js.map generated vendored Normal file
View file

@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/gridfs/index.ts"],"names":[],"mappings":";;;AAIA,oCAAyE;AACzE,gDAAgE;AAGhE,wCAAgD;AAChD,oCAAgD;AAChD,oDAA0E;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I,CAAC,EAAE,SAAS,EAAE,eAAe,EAAE,CAAC,CAAC;QACtE,CAAC;aAAM,CAAC;YACN,MAAM,IAAI,CAAC,CAAC,CAAC,gBAAgB,CAAC,IAAI,EAAE,CAAC;YACrC,MAAM,IAAI,CAAC,CAAC,CAAC,iBAAiB,CAAC,IAAI,EAAE,CAAC;QACxC,CAAC;IACH,CAAC;;AA7LH,oCA8LC"}

358
node_modules/mongodb/lib/gridfs/upload.js generated vendored Normal file
View file

@ -0,0 +1,358 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GridFSBucketWriteStream = void 0;
const stream_1 = require("stream");
const bson_1 = require("../bson");
const abstract_cursor_1 = require("../cursor/abstract_cursor");
const error_1 = require("../error");
const timeout_1 = require("../timeout");
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 {
/**
* @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.
* @internal
*/
constructor(bucket, filename, options) {
super();
/**
* The document containing information about the inserted file.
* This property is defined _after_ the finish event has been emitted.
* It will remain `null` if an error occurs.
*
* @example
* ```ts
* fs.createReadStream('file.txt')
* .pipe(bucket.openUploadStream('file.txt'))
* .on('finish', function () {
* console.log(this.gridFSFile)
* })
* ```
*/
this.gridFSFile = null;
options = 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 (options.timeoutMS != null)
this.timeoutContext = new timeout_1.CSOTTimeoutContext({
timeoutMS: options.timeoutMS,
serverSelectionTimeoutMS: (0, utils_1.resolveTimeoutOptions)(this.bucket.s.db.client, {})
.serverSelectionTimeoutMS
});
}
/**
* @internal
*
* The stream is considered constructed when the indexes are done being created
*/
_construct(callback) {
if (!this.bucket.s.calledOpenUploadStream) {
this.bucket.s.calledOpenUploadStream = true;
checkIndexes(this).then(() => {
this.bucket.s.checkedIndexes = true;
this.bucket.emit('index');
callback();
}, error => {
if (error instanceof error_1.MongoOperationTimeoutError) {
return handleError(this, error, callback);
}
(0, utils_1.squashError)(error);
callback();
});
}
else {
return process.nextTick(callback);
}
}
/**
* @internal
* Write a buffer to the stream.
*
* @param chunk - Buffer to write
* @param encoding - Optional encoding for the buffer
* @param callback - Function to call when the chunk was added to the buffer, or if the entire chunk was persisted to MongoDB if this chunk caused a flush.
*/
_write(chunk, encoding, callback) {
doWrite(this, chunk, encoding, callback);
}
/** @internal */
_final(callback) {
if (this.state.streamEnd) {
return process.nextTick(callback);
}
this.state.streamEnd = true;
writeRemnant(this, callback);
}
/**
* Places this write stream into an aborted state (all future writes fail)
* and deletes all chunks that have already been written.
*/
async abort() {
if (this.state.streamEnd) {
// TODO(NODE-3485): Replace with MongoGridFSStreamClosed
throw new error_1.MongoAPIError('Cannot abort a stream that has already completed');
}
if (this.state.aborted) {
// TODO(NODE-3485): Replace with MongoGridFSStreamClosed
throw new error_1.MongoAPIError('Cannot call abort() on a stream twice');
}
this.state.aborted = true;
const remainingTimeMS = this.timeoutContext?.getRemainingTimeMSOrThrow(`Upload timed out after ${this.timeoutContext?.timeoutMS}ms`);
await this.chunks.deleteMany({ files_id: this.id }, { timeoutMS: remainingTimeMS });
}
}
exports.GridFSBucketWriteStream = GridFSBucketWriteStream;
function handleError(stream, error, callback) {
if (stream.state.errored) {
process.nextTick(callback);
return;
}
stream.state.errored = true;
process.nextTick(callback, error);
}
function createChunkDoc(filesId, n, data) {
return {
_id: new bson_1.ObjectId(),
files_id: filesId,
n,
data
};
}
async function checkChunksIndex(stream) {
const index = { files_id: 1, n: 1 };
let remainingTimeMS;
remainingTimeMS = stream.timeoutContext?.getRemainingTimeMSOrThrow(`Upload timed out after ${stream.timeoutContext?.timeoutMS}ms`);
let indexes;
try {
indexes = await stream.chunks
.listIndexes({
timeoutMode: remainingTimeMS != null ? abstract_cursor_1.CursorTimeoutMode.LIFETIME : undefined,
timeoutMS: remainingTimeMS
})
.toArray();
}
catch (error) {
if (error instanceof error_1.MongoError && error.code === error_1.MONGODB_ERROR_CODES.NamespaceNotFound) {
indexes = [];
}
else {
throw error;
}
}
const hasChunksIndex = !!indexes.find(index => {
const keys = Object.keys(index.key);
if (keys.length === 2 && index.key.files_id === 1 && index.key.n === 1) {
return true;
}
return false;
});
if (!hasChunksIndex) {
remainingTimeMS = stream.timeoutContext?.getRemainingTimeMSOrThrow(`Upload timed out after ${stream.timeoutContext?.timeoutMS}ms`);
await stream.chunks.createIndex(index, {
...stream.writeConcern,
background: true,
unique: true,
timeoutMS: remainingTimeMS
});
}
}
function checkDone(stream, callback) {
if (stream.done) {
return process.nextTick(callback);
}
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 gridFSFile = createFilesDoc(stream.id, stream.length, stream.chunkSizeBytes, stream.filename, stream.options.metadata);
if (isAborted(stream, callback)) {
return;
}
const remainingTimeMS = stream.timeoutContext?.remainingTimeMS;
if (remainingTimeMS != null && remainingTimeMS <= 0) {
return handleError(stream, new error_1.MongoOperationTimeoutError(`Upload timed out after ${stream.timeoutContext?.timeoutMS}ms`), callback);
}
stream.files
.insertOne(gridFSFile, { writeConcern: stream.writeConcern, timeoutMS: remainingTimeMS })
.then(() => {
stream.gridFSFile = gridFSFile;
callback();
}, error => {
return handleError(stream, error, callback);
});
return;
}
process.nextTick(callback);
}
async function checkIndexes(stream) {
let remainingTimeMS = stream.timeoutContext?.getRemainingTimeMSOrThrow(`Upload timed out after ${stream.timeoutContext?.timeoutMS}ms`);
const doc = await stream.files.findOne({}, {
projection: { _id: 1 },
timeoutMS: remainingTimeMS
});
if (doc != null) {
// If at least one document exists assume the collection has the required index
return;
}
const index = { filename: 1, uploadDate: 1 };
let indexes;
remainingTimeMS = stream.timeoutContext?.getRemainingTimeMSOrThrow(`Upload timed out after ${stream.timeoutContext?.timeoutMS}ms`);
const listIndexesOptions = {
timeoutMode: remainingTimeMS != null ? abstract_cursor_1.CursorTimeoutMode.LIFETIME : undefined,
timeoutMS: remainingTimeMS
};
try {
indexes = await stream.files.listIndexes(listIndexesOptions).toArray();
}
catch (error) {
if (error instanceof error_1.MongoError && error.code === error_1.MONGODB_ERROR_CODES.NamespaceNotFound) {
indexes = [];
}
else {
throw error;
}
}
const hasFileIndex = !!indexes.find(index => {
const keys = Object.keys(index.key);
if (keys.length === 2 && index.key.filename === 1 && index.key.uploadDate === 1) {
return true;
}
return false;
});
if (!hasFileIndex) {
remainingTimeMS = stream.timeoutContext?.getRemainingTimeMSOrThrow(`Upload timed out after ${stream.timeoutContext?.timeoutMS}ms`);
await stream.files.createIndex(index, { background: false, timeoutMS: remainingTimeMS });
}
await checkChunksIndex(stream);
}
function createFilesDoc(_id, length, chunkSize, filename, metadata) {
const ret = {
_id,
length,
chunkSize,
uploadDate: new Date(),
filename
};
if (metadata) {
ret.metadata = metadata;
}
return ret;
}
function doWrite(stream, chunk, encoding, callback) {
if (isAborted(stream, callback)) {
return;
}
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;
process.nextTick(callback);
return;
}
// 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));
const remainingTimeMS = stream.timeoutContext?.remainingTimeMS;
if (remainingTimeMS != null && remainingTimeMS <= 0) {
return handleError(stream, new error_1.MongoOperationTimeoutError(`Upload timed out after ${stream.timeoutContext?.timeoutMS}ms`), callback);
}
++stream.state.outstandingRequests;
++outstandingRequests;
if (isAborted(stream, callback)) {
return;
}
stream.chunks
.insertOne(doc, { writeConcern: stream.writeConcern, timeoutMS: remainingTimeMS })
.then(() => {
--stream.state.outstandingRequests;
--outstandingRequests;
if (!outstandingRequests) {
checkDone(stream, callback);
}
}, error => {
return handleError(stream, error, callback);
});
spaceRemaining = stream.chunkSizeBytes;
stream.pos = 0;
++stream.n;
}
inputBufRemaining -= numToCopy;
numToCopy = Math.min(spaceRemaining, inputBufRemaining);
}
}
function writeRemnant(stream, callback) {
// Buffer is empty, so don't bother to insert
if (stream.pos === 0) {
return checkDone(stream, callback);
}
// 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 (isAborted(stream, callback)) {
return;
}
const remainingTimeMS = stream.timeoutContext?.remainingTimeMS;
if (remainingTimeMS != null && remainingTimeMS <= 0) {
return handleError(stream, new error_1.MongoOperationTimeoutError(`Upload timed out after ${stream.timeoutContext?.timeoutMS}ms`), callback);
}
++stream.state.outstandingRequests;
stream.chunks
.insertOne(doc, { writeConcern: stream.writeConcern, timeoutMS: remainingTimeMS })
.then(() => {
--stream.state.outstandingRequests;
checkDone(stream, callback);
}, error => {
return handleError(stream, error, callback);
});
}
function isAborted(stream, callback) {
if (stream.state.aborted) {
process.nextTick(callback, new error_1.MongoAPIError('Stream has been aborted'));
return true;
}
return false;
}
//# sourceMappingURL=upload.js.map

1
node_modules/mongodb/lib/gridfs/upload.js.map generated vendored Normal file

File diff suppressed because one or more lines are too long