Skip to content

Commit 1477fe1

Browse files
authored
fix: support uint8array in file.save (#2480)
1 parent c3fe679 commit 1477fe1

File tree

2 files changed

+68
-7
lines changed

2 files changed

+68
-7
lines changed

src/file.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ export interface PolicyDocument {
9393
signature: string;
9494
}
9595

96-
export type SaveData = string | Buffer | PipelineSource<string | Buffer>;
96+
export type SaveData =
97+
| string
98+
| Buffer
99+
| Uint8Array
100+
| PipelineSource<string | Buffer | Uint8Array>;
97101

98102
export type GenerateSignedPostPolicyV2Response = [PolicyDocument];
99103

@@ -3916,7 +3920,11 @@ class File extends ServiceObject<File, FileMetadata> {
39163920
return bail(err);
39173921
};
39183922

3919-
if (typeof data === 'string' || Buffer.isBuffer(data)) {
3923+
if (
3924+
typeof data === 'string' ||
3925+
Buffer.isBuffer(data) ||
3926+
data instanceof Uint8Array
3927+
) {
39203928
writable
39213929
.on('error', handleError)
39223930
.on('finish', () => resolve())

test/file.ts

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4281,6 +4281,9 @@ describe('File', () => {
42814281
describe('save', () => {
42824282
const DATA = 'Data!';
42834283
const BUFFER_DATA = Buffer.from(DATA, 'utf8');
4284+
const UINT8_ARRAY_DATA = Uint8Array.from(
4285+
Array.from(DATA).map(l => l.charCodeAt(0))
4286+
);
42844287

42854288
class DelayedStreamNoError extends Transform {
42864289
_transform(chunk: string | Buffer, _encoding: string, done: Function) {
@@ -4318,6 +4321,22 @@ describe('File', () => {
43184321
await file.save(DATA, options, assert.ifError);
43194322
});
43204323

4324+
it('should save a buffer with no errors', async () => {
4325+
const options = {resumable: false};
4326+
file.createWriteStream = () => {
4327+
return new DelayedStreamNoError();
4328+
};
4329+
await file.save(BUFFER_DATA, options, assert.ifError);
4330+
});
4331+
4332+
it('should save a Uint8Array with no errors', async () => {
4333+
const options = {resumable: false};
4334+
file.createWriteStream = () => {
4335+
return new DelayedStreamNoError();
4336+
};
4337+
await file.save(UINT8_ARRAY_DATA, options, assert.ifError);
4338+
});
4339+
43214340
it('string upload should retry on first failure', async () => {
43224341
const options = {
43234342
resumable: false,
@@ -4363,15 +4382,28 @@ describe('File', () => {
43634382
}
43644383
});
43654384

4366-
it('should save a buffer with no errors', async () => {
4385+
it('should save a Readable with no errors (String)', done => {
43674386
const options = {resumable: false};
43684387
file.createWriteStream = () => {
4369-
return new DelayedStreamNoError();
4388+
const writeStream = new PassThrough();
4389+
writeStream.on('data', data => {
4390+
assert.strictEqual(data.toString(), DATA);
4391+
});
4392+
writeStream.once('finish', done);
4393+
return writeStream;
43704394
};
4371-
await file.save(DATA, options, assert.ifError);
4395+
4396+
const readable = new Readable({
4397+
read() {
4398+
this.push(DATA);
4399+
this.push(null);
4400+
},
4401+
});
4402+
4403+
void file.save(readable, options);
43724404
});
43734405

4374-
it('should save a Readable with no errors', done => {
4406+
it('should save a Readable with no errors (Buffer)', done => {
43754407
const options = {resumable: false};
43764408
file.createWriteStream = () => {
43774409
const writeStream = new PassThrough();
@@ -4384,7 +4416,28 @@ describe('File', () => {
43844416

43854417
const readable = new Readable({
43864418
read() {
4387-
this.push(DATA);
4419+
this.push(BUFFER_DATA);
4420+
this.push(null);
4421+
},
4422+
});
4423+
4424+
void file.save(readable, options);
4425+
});
4426+
4427+
it('should save a Readable with no errors (Uint8Array)', done => {
4428+
const options = {resumable: false};
4429+
file.createWriteStream = () => {
4430+
const writeStream = new PassThrough();
4431+
writeStream.on('data', data => {
4432+
assert.strictEqual(data.toString(), DATA);
4433+
});
4434+
writeStream.once('finish', done);
4435+
return writeStream;
4436+
};
4437+
4438+
const readable = new Readable({
4439+
read() {
4440+
this.push(UINT8_ARRAY_DATA);
43884441
this.push(null);
43894442
},
43904443
});

0 commit comments

Comments
 (0)