Skip to content

fix(storage): honor Content-Range in GCS resumable upload chunks - #11613

Open
rarya618 wants to merge 4 commits into
firebase:mainfrom
rarya618:fix/storage-emulator-chunked-resumable
Open

rarya618 wants to merge 4 commits into
firebase:mainfrom
rarya618:fix/storage-emulator-chunked-resumable

Conversation

@rarya618

@rarya618 rarya618 commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes #11583.

The Storage emulator's GCS resumable upload handler (PUT /upload/storage/v1/b/:bucketId/o) ignored Content-Range and finalized the upload on the first request. Clients that stream more than one chunk, such as @google-cloud/storage's createWriteStream({ resumable: true, chunkSize }), got a 200 after the first chunk and then hung or failed.

The handler now follows the chunked resumable upload protocol:

  • Non-final chunks (bytes start-end/*, or bytes start-end/total before the last byte) are appended and answered with 308 and a Range: bytes=0-N header. The Range header is left out while nothing has been received.
  • The upload is finalized only once the bytes received reach the total in Content-Range, either on the last data chunk or on an empty bytes */total request.
  • Status checks (bytes */*, bytes */total) report progress. On an upload that has already finished, they return the object's metadata with 200.
  • Bytes the emulator already has are skipped when a chunk is resent after a lost response.
  • A chunk that leaves a gap, a body whose length doesn't match its range, or a malformed Content-Range gets 400.
  • Requests without Content-Range still finalize in a single request, as before.

This also lifts the 130 MiB cap that bodyParser.raw placed on chunked uploads, since the cap now applies to each chunk rather than the whole object.

Scenarios Tested

  • New unit spec src/emulator/storage/apis/gcloud.spec.ts (20 tests) drives the real router with supertest. It covers Content-Range parsing, single-shot uploads, chunks with unknown and known totals, the bytes */total end 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.
  • Ran the issue's repro with the real @google-cloud/storage SDK (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."
  • Added a multi-chunk createWriteStream test to scripts/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.ts passes (48 tests), and eslint and tsc are clean on the changed files.

Sample Commands

N/A

…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.
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

@rarya618

rarya618 commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +55 to +60
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 });
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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
  1. Clean up mocks in afterEach: Always restore stubs and clean intercepts in afterEach (sinon.restore(), nock.cleanAll()), never in suite-level after(). (link)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in a8b7435: the stub is now created in beforeEach and restored in afterEach.

Comment on lines +78 to +81
after(async () => {
sinon.restore();
await persistence.deleteAll();
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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
  1. Clean up mocks in afterEach: Always restore stubs and clean intercepts in afterEach (sinon.restore(), nock.cleanAll()), never in suite-level after(). (link)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant