Skip to content

Commit

Permalink
fix: support uint8array in file.save (#2480)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddelgrosso1 authored Jun 7, 2024
1 parent c3fe679 commit 1477fe1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 7 deletions.
12 changes: 10 additions & 2 deletions src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ export interface PolicyDocument {
signature: string;
}

export type SaveData = string | Buffer | PipelineSource<string | Buffer>;
export type SaveData =
| string
| Buffer
| Uint8Array
| PipelineSource<string | Buffer | Uint8Array>;

export type GenerateSignedPostPolicyV2Response = [PolicyDocument];

Expand Down Expand Up @@ -3916,7 +3920,11 @@ class File extends ServiceObject<File, FileMetadata> {
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())
Expand Down
63 changes: 58 additions & 5 deletions test/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand All @@ -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);
},
});
Expand Down

0 comments on commit 1477fe1

Please sign in to comment.