Skip to content

fix sveltekit type error in firebase-frameworks - #673

Open
leoortizz wants to merge 1 commit into
firebase:mainfrom
leoortizz:fix/sveltekit-buffer-bodyinit
Open

fix sveltekit type error in firebase-frameworks#673
leoortizz wants to merge 1 commit into
firebase:mainfrom
leoortizz:fix/sveltekit-buffer-bodyinit

Conversation

@leoortizz

@leoortizz leoortizz commented Jul 31, 2026

Copy link
Copy Markdown
Member

tsc fails under Node 24 / latest @types/node because Buffer no longer satisfies the DOM BodyInit type:

src/sveltekit/index.ts(47,5): error TS2322: Type 'Buffer<ArrayBufferLike> | null' is not assignable to type 'BodyInit | null | undefined'.

Wrap request.rawBody as new Uint8Array(...). Valid BodyInit, runtime unchanged (Buffer is a Uint8Array subclass).

Verified: build passes, and POST bodies still round-trip through a real SvelteKit app.

Node 24 / current @types/node no longer lets Buffer satisfy the DOM
BodyInit type, breaking tsc. Wrap rawBody as new Uint8Array(...) at the
Request construction site; runtime behavior is unchanged.

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

Copy link
Copy Markdown

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 updates the SvelteKit request handler to wrap the request's raw body in a Uint8Array. The reviewer pointed out that instantiating a new Uint8Array creates an unnecessary memory copy, and suggested casting the existing buffer to Uint8Array instead to improve performance and reduce memory overhead.

method: request.method,
headers: toSvelteKitHeaders(request.headers),
body: request.rawBody ? request.rawBody : null,
body: request.rawBody ? new Uint8Array(request.rawBody) : null,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Using new Uint8Array(request.rawBody) creates a copy of the entire buffer in memory, which introduces unnecessary performance and memory overhead, especially for large request bodies. Since Buffer inherits from Uint8Array at runtime, you can safely cast it to Uint8Array to satisfy the TypeScript compiler without any runtime overhead.

Suggested change
body: request.rawBody ? new Uint8Array(request.rawBody) : null,
body: request.rawBody ? (request.rawBody as Uint8Array) : null,

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant