From 1477fe15e5b6dae7cdfb2a1d00121e5d674c8f7c Mon Sep 17 00:00:00 2001 From: Denis DelGrosso <85250797+ddelgrosso1@users.noreply.github.com> Date: Fri, 7 Jun 2024 12:02:41 -0400 Subject: [PATCH] fix: support uint8array in file.save (#2480) --- src/file.ts | 12 ++++++++-- test/file.ts | 63 +++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 68 insertions(+), 7 deletions(-) diff --git a/src/file.ts b/src/file.ts index 32f1f62b7..02aa3d505 100644 --- a/src/file.ts +++ b/src/file.ts @@ -93,7 +93,11 @@ export interface PolicyDocument { signature: string; } -export type SaveData = string | Buffer | PipelineSource; +export type SaveData = + | string + | Buffer + | Uint8Array + | PipelineSource; export type GenerateSignedPostPolicyV2Response = [PolicyDocument]; @@ -3916,7 +3920,11 @@ class File extends ServiceObject { return bail(err); }; - if (typeof data === 'string' || Buffer.isBuffer(data)) { + if ( + typeof data === 'string' || + Buffer.isBuffer(data) || + data instanceof Uint8Array + ) { writable .on('error', handleError) .on('finish', () => resolve()) diff --git a/test/file.ts b/test/file.ts index 5369083af..46d0c125a 100644 --- a/test/file.ts +++ b/test/file.ts @@ -4281,6 +4281,9 @@ describe('File', () => { describe('save', () => { const DATA = 'Data!'; const BUFFER_DATA = Buffer.from(DATA, 'utf8'); + const UINT8_ARRAY_DATA = Uint8Array.from( + Array.from(DATA).map(l => l.charCodeAt(0)) + ); class DelayedStreamNoError extends Transform { _transform(chunk: string | Buffer, _encoding: string, done: Function) { @@ -4318,6 +4321,22 @@ describe('File', () => { await file.save(DATA, options, assert.ifError); }); + it('should save a buffer with no errors', async () => { + const options = {resumable: false}; + file.createWriteStream = () => { + return new DelayedStreamNoError(); + }; + await file.save(BUFFER_DATA, options, assert.ifError); + }); + + it('should save a Uint8Array with no errors', async () => { + const options = {resumable: false}; + file.createWriteStream = () => { + return new DelayedStreamNoError(); + }; + await file.save(UINT8_ARRAY_DATA, options, assert.ifError); + }); + it('string upload should retry on first failure', async () => { const options = { resumable: false, @@ -4363,15 +4382,28 @@ describe('File', () => { } }); - it('should save a buffer with no errors', async () => { + it('should save a Readable with no errors (String)', done => { const options = {resumable: false}; file.createWriteStream = () => { - return new DelayedStreamNoError(); + const writeStream = new PassThrough(); + writeStream.on('data', data => { + assert.strictEqual(data.toString(), DATA); + }); + writeStream.once('finish', done); + return writeStream; }; - await file.save(DATA, options, assert.ifError); + + const readable = new Readable({ + read() { + this.push(DATA); + this.push(null); + }, + }); + + void file.save(readable, options); }); - it('should save a Readable with no errors', done => { + it('should save a Readable with no errors (Buffer)', done => { const options = {resumable: false}; file.createWriteStream = () => { const writeStream = new PassThrough(); @@ -4384,7 +4416,28 @@ describe('File', () => { const readable = new Readable({ read() { - this.push(DATA); + this.push(BUFFER_DATA); + this.push(null); + }, + }); + + void file.save(readable, options); + }); + + it('should save a Readable with no errors (Uint8Array)', done => { + const options = {resumable: false}; + file.createWriteStream = () => { + const writeStream = new PassThrough(); + writeStream.on('data', data => { + assert.strictEqual(data.toString(), DATA); + }); + writeStream.once('finish', done); + return writeStream; + }; + + const readable = new Readable({ + read() { + this.push(UINT8_ARRAY_DATA); this.push(null); }, });