Repository navigation
Conversation
…ebase#11583) The PUT handler for /upload/storage/v1/b/:bucketId/o finalized the upload on the first request, so @google-cloud/storage clients streaming more than one chunk got a 200 after the first chunk and either hung or failed. Non-final chunks now get 308 Resume Incomplete with a Range header, and the upload is only finalized once the bytes received reach the total in Content-Range. Status checks (bytes */total) are answered, resent bytes are skipped, and gaps or mismatched lengths are rejected with 400. Requests without Content-Range still finalize in one shot.
|
Warning Gemini encountered an error creating the review. You can try again by commenting |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request fixes an issue in the Storage emulator where GCS resumable uploads were finalized prematurely after the first chunk, causing multi-chunk uploads to hang or fail. The changes introduce robust Content-Range header parsing and update the PUT upload handler to correctly support chunked uploads, status checks, and completion. Additionally, comprehensive unit and integration tests have been added. The review feedback recommends moving Sinon stub initialization and restoration to beforeEach and afterEach blocks in the new test file to comply with the repository's mock cleanup style guide.
| before(() => { | ||
| // Object and bucket metadata build self links from the registered address. | ||
| sinon | ||
| .stub(EmulatorRegistry, "getInfo") | ||
| .returns({ name: Emulators.STORAGE, host: "127.0.0.1", port: 9199 }); | ||
| }); |
There was a problem hiding this comment.
To ensure stubs are fresh for each test and to prevent leakage, they should be initialized in beforeEach rather than before, in accordance with the repository style guide's mock cleanup rules.
| before(() => { | |
| // Object and bucket metadata build self links from the registered address. | |
| sinon | |
| .stub(EmulatorRegistry, "getInfo") | |
| .returns({ name: Emulators.STORAGE, host: "127.0.0.1", port: 9199 }); | |
| }); | |
| beforeEach(() => {\n // Object and bucket metadata build self links from the registered address.\n sinon\n .stub(EmulatorRegistry, \"getInfo\")\n .returns({ name: Emulators.STORAGE, host: \"127.0.0.1\", port: 9199 });\n }); |
References
- Clean up mocks in afterEach: Always restore stubs and clean intercepts in afterEach (sinon.restore(), nock.cleanAll()), never in suite-level after(). (link)
There was a problem hiding this comment.
Done in a8b7435: the stub is now created in beforeEach and restored in afterEach.
| after(async () => { | ||
| sinon.restore(); | ||
| await persistence.deleteAll(); | ||
| }); |
There was a problem hiding this comment.
According to the repository style guide, stubs and mocks should always be restored in afterEach rather than the suite-level after block to prevent mock leakage between tests.
afterEach(() => {\n sinon.restore();\n });\n\n after(async () => {\n await persistence.deleteAll();\n });References
- Clean up mocks in afterEach: Always restore stubs and clean intercepts in afterEach (sinon.restore(), nock.cleanAll()), never in suite-level after(). (link)
There was a problem hiding this comment.
Done in a8b7435: the stub is now created in beforeEach and restored in afterEach.
Stub EmulatorRegistry.getInfo in beforeEach and restore it in afterEach, as the style guide asks, instead of once for the whole suite.
Description
Fixes #11583.
The Storage emulator's GCS resumable upload handler (
PUT /upload/storage/v1/b/:bucketId/o) ignoredContent-Rangeand finalized the upload on the first request. Clients that stream more than one chunk, such as@google-cloud/storage'screateWriteStream({ resumable: true, chunkSize }), got a200after the first chunk and then hung or failed.The handler now follows the chunked resumable upload protocol:
bytes start-end/*, orbytes start-end/totalbefore the last byte) are appended and answered with308and aRange: bytes=0-Nheader. TheRangeheader is left out while nothing has been received.Content-Range, either on the last data chunk or on an emptybytes */totalrequest.bytes */*,bytes */total) report progress. On an upload that has already finished, they return the object's metadata with200.Content-Rangegets400.Content-Rangestill finalize in a single request, as before.This also lifts the 130 MiB cap that
bodyParser.rawplaced on chunked uploads, since the cap now applies to each chunk rather than the whole object.Scenarios Tested
src/emulator/storage/apis/gcloud.spec.ts(20 tests) drives the real router with supertest. It coversContent-Rangeparsing, single-shot uploads, chunks with unknown and known totals, thebytes */totalend marker, status checks, resent chunks, status checks after completion, gaps, length mismatches, malformed headers and unknown upload IDs. Against the previous handler, 9 of these fail.@google-cloud/storageSDK (8.4.0) against the router. A 787,666-byte upload in 256 KiB chunks round-trips byte-identical; before the fix it fails with "The uploaded data did not match the data from the server."createWriteStreamtest toscripts/storage-emulator-integration/conformance/gcs-js-sdk.test.ts. I didn't run this suite locally because it needs the emulators started.npx mocha src/emulator/storage/**/*.spec.tspasses (48 tests), andeslintandtscare clean on the changed files.Sample Commands
N/A