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 Empty Object Uploads for Resumable Upload #1949

Merged
merged 1 commit into from
May 18, 2022
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
6 changes: 5 additions & 1 deletion src/gcs-resumable-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,11 @@ export class Upload extends Writable {

this.once('uploadFinished', fireFinishEvent);

process.nextTick(() => this.emit('upstreamFinished'));
process.nextTick(() => {
this.emit('upstreamFinished');
// it's possible `_write` may not be called - namely for empty object uploads
this.emit('writing');
});
}

/**
Expand Down
96 changes: 96 additions & 0 deletions test/gcs-resumable-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2638,5 +2638,101 @@ describe('gcs-resumable-upload', () => {
upstreamBuffer.pipe(up);
});
});

describe('empty object', () => {
let uri = '';

beforeEach(() => {
uri = 'uri';

up.contentLength = 0;
up.createURI = (
callback: (error: Error | null, uri: string) => void
) => {
up.uri = uri;
up.offset = 0;
callback(null, uri);
};
});

it('should support uploading empty objects', done => {
const CONTENT_LENGTH = 0;
const EXPECTED_NUM_REQUESTS = 1;

const upstreamBuffer = new Readable({
read() {
this.push(null);
},
});

const requests: {
dataReceived: number;
opts: GaxiosOptions;
chunkWritesInRequest: number;
}[] = [];
let overallDataReceived = 0;

up.makeRequestStream = async (opts: GaxiosOptions) => {
let dataReceived = 0;
let chunkWritesInRequest = 0;

const res = await new Promise(resolve => {
opts.body.on('data', (data: Buffer) => {
dataReceived += data.byteLength;
overallDataReceived += data.byteLength;
chunkWritesInRequest++;
});

opts.body.on('end', () => {
requests.push({dataReceived, opts, chunkWritesInRequest});

resolve({
status: 200,
data: {},
});

resolve(null);
});
});

return res;
};

up.on('error', done);

up.on('finish', () => {
// Ensure the correct number of requests and data look correct
assert.equal(requests.length, EXPECTED_NUM_REQUESTS);
assert.equal(overallDataReceived, CONTENT_LENGTH);

// Validate the single request
const request = requests[0];

assert.strictEqual(request.opts.method, 'PUT');
assert.strictEqual(request.opts.url, uri);

// We should be writing multiple chunks down the wire
assert(request.chunkWritesInRequest === 0);

assert.equal(request.dataReceived, CONTENT_LENGTH);
assert(request.opts.headers);

assert.equal(
request.opts.headers['Content-Range'],
`bytes 0-*/${CONTENT_LENGTH}`
);
assert.ok(
X_GOOG_API_HEADER_REGEX.test(
request.opts.headers['x-goog-api-client']
)
);

done();
});

// init the request
upstreamBuffer.pipe(up);
});
});
});
});