|
1 | 1 | /* eslint-disable @typescript-eslint/no-unused-vars */
|
2 |
| -/* eslint-disable @typescript-eslint/no-non-null-assertion */ |
3 |
| -import { once, Writable } from 'node:stream'; |
| 2 | + |
| 3 | +import { once, Readable, Writable } from 'node:stream'; |
| 4 | +import { pipeline } from 'node:stream/promises'; |
4 | 5 |
|
5 | 6 | import { AssertionError, expect } from 'chai';
|
6 | 7 |
|
@@ -519,27 +520,28 @@ operations.set('delete', async ({ entities, operation }) => {
|
519 | 520 | operations.set('download', async ({ entities, operation }) => {
|
520 | 521 | const bucket = entities.getEntity('bucket', operation.object);
|
521 | 522 |
|
522 |
| - const stream = bucket.openDownloadStream(operation.arguments!.id); |
523 |
| - return new Promise((resolve, reject) => { |
524 |
| - const chunks: any[] = []; |
525 |
| - stream.on('data', chunk => chunks.push(...chunk)); |
526 |
| - stream.on('error', reject); |
527 |
| - stream.on('end', () => resolve(chunks)); |
528 |
| - }); |
| 523 | + const { id, ...options } = operation.arguments ?? {}; |
| 524 | + const stream = bucket.openDownloadStream(id, options); |
| 525 | + return Buffer.concat(await stream.toArray()); |
529 | 526 | });
|
530 | 527 |
|
531 |
| -operations.set('upload', async ({ entities, operation }) => { |
| 528 | +operations.set('downloadByName', async ({ entities, operation }) => { |
532 | 529 | const bucket = entities.getEntity('bucket', operation.object);
|
533 | 530 |
|
534 |
| - const stream = bucket.openUploadStream(operation.arguments!.filename, { |
535 |
| - chunkSizeBytes: operation.arguments?.chunkSizeBytes |
536 |
| - }); |
| 531 | + const { filename, ...options } = operation.arguments ?? {}; |
| 532 | + const stream: Readable = bucket.openDownloadStreamByName(filename, options); |
| 533 | + |
| 534 | + return Buffer.concat(await stream.toArray()); |
| 535 | +}); |
| 536 | + |
| 537 | +operations.set('upload', async ({ entities, operation }) => { |
| 538 | + const bucket = entities.getEntity('bucket', operation.object); |
| 539 | + const { filename, source, ...options } = operation.arguments ?? {}; |
537 | 540 |
|
538 |
| - const data = Buffer.from(operation.arguments!.source.$$hexBytes, 'hex'); |
539 |
| - const willFinish = once(stream, 'finish'); |
540 |
| - stream.end(data); |
541 |
| - await willFinish; |
| 541 | + const stream = bucket.openUploadStream(operation.arguments!.filename, options); |
| 542 | + const filestream = Readable.from(Buffer.from(operation.arguments!.source.$$hexBytes, 'hex')); |
542 | 543 |
|
| 544 | + await pipeline(filestream, stream); |
543 | 545 | return stream.gridFSFile?._id;
|
544 | 546 | });
|
545 | 547 |
|
|
0 commit comments