Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support uint8array in file.save #2480

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading