{{ message }}
Repository navigation
fix(types): accept ArrayBufferView for byte-array struct inputs - #7843
Open
PedroRosalba wants to merge 3 commits into
Open
PedroRosalba wants to merge 3 commits into
PedroRosalba wants to merge 3 commits into
Conversation
`WorkerLoaderModule.data` is declared `jsg::Optional<kj::Array<const byte>>`,
and the JSG unwrap for that type takes any ArrayBuffer, ArrayBufferView or
SharedArrayBuffer (`src/workerd/jsg/value.h` `tryUnwrap(kj::Array<byte>*)`).
The type generator, however, emits a non-coerced struct field of
`kj::Array<byte>` as plain `ArrayBuffer` (`types/src/generator/type.ts`), so
the published type is `data?: ArrayBuffer`.
Since TypeScript 5.7 typed arrays are no longer structurally assignable to
`ArrayBuffer`, so `{ data: new Uint8Array(...) }`, which
`worker-loader-test.js` already passes at runtime, fails to compile:
error TS2740: Type 'Uint8Array<ArrayBuffer>' is missing the following
properties from type 'ArrayBuffer': maxByteLength, resizable, resize, ...
Widen `data` with the same hand-written override the sibling `wasm` field
received in 9cdf380 (cloudflare#6997) and add a type test.
Test: //types:test/types/worker-loader and //src/workerd/api/tests:worker-loader-test@
`ssecKey` on R2GetOptions, R2PutOptions, R2MultipartOptions and R2UploadPartOptions is `jsg::Optional<kj::OneOf<kj::Array<byte>, kj::String>>`. The JSG unwrap for `kj::Array<byte>` accepts any ArrayBuffer, ArrayBufferView or SharedArrayBuffer, and `buildSsecKey()` only checks the byte length, but the generator emits non-coerced `kj::Array<byte>` struct fields as plain `ArrayBuffer`, so the published type is `ssecKey?: ArrayBuffer | string`. Since TypeScript 5.7 a Uint8Array is not assignable to `ArrayBuffer`, so the 32-byte `Uint8Array` key that `r2-test.js` passes at runtime fails to compile. The checksum fields declared next to `ssecKey` already accept `ArrayBuffer | ArrayBufferView`; declare `ssecKey` the same way in the four option structs and add a type test. Test: //types:test/types/r2 and //src/workerd/api/tests:r2-test@
…and indexes
`AnalyticsEngineDataPoint.blobs` and `.indexes` are declared
`jsg::Optional<kj::Array<kj::Maybe<kj::OneOf<kj::Array<byte>, kj::String>>>>`.
The JSG unwrap for `kj::Array<byte>` accepts any ArrayBuffer, ArrayBufferView
or SharedArrayBuffer (`src/workerd/jsg/value.h` `tryUnwrap(kj::Array<byte>*)`),
and `setBlobs()` / `setIndexes()` copy whatever bytes they get, but the type
generator emits a non-coerced `kj::Array<byte>` struct field as plain
`ArrayBuffer` (`types/src/generator/type.ts`), so the published element type
is `(ArrayBuffer | string) | null`.
Since TypeScript 5.7 a typed array is not assignable to `ArrayBuffer`, so
`writeDataPoint({ blobs: [new Uint8Array(...)] })`, which the runtime accepts
and writes as the blob bytes, fails to compile:
error TS2322: Type 'Uint8Array<ArrayBuffer>' is not assignable to type
'string | ArrayBuffer | null'.
Widen both fields with a hand-written `JSG_STRUCT_TS_OVERRIDE`, the way
`WorkerLoaderModule.wasm` is declared, keep `doubles` generated, and add a
type test.
Test: //types:test/types/analytics-engine and //src/workerd/api/tests:analytics-engine-test@
|
This pull request was not reviewed automatically because its author does not have write access to this repository. A maintainer with write access can start a review by commenting |
Author
jasnell
approved these changes
Oct 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Problem
Input struct fields of C++ type
kj::Array<byte>are emitted by the type generator as plainArrayBuffer(types/src/generator/type.ts), but JSG's unwrap for that type accepts anyArrayBuffer,ArrayBufferVieworSharedArrayBuffer(src/workerd/jsg/value.h,tryUnwrap(kj::Array<byte>*)). Since TypeScript 5.7 a typed array is no longer assignable toArrayBuffer, so code that the runtime accepts, and that the repo's own runtime tests already exercise, fails to compile:WorkerLoaderModule.data:{ data: new Uint8Array(...) }→ TS2740 (its siblingwasmwas already widened by hand in Accept WebAssembly.Module values in the dynamic worker loader #6997).ssecKeyonR2GetOptions,R2PutOptions,R2MultipartOptions,R2UploadPartOptions: the 32-byteUint8Arraykey thatr2-test.jspasses → TS2322. The checksum fields declared next to it already acceptArrayBuffer | ArrayBufferView.AnalyticsEngineDataPoint.blobs/.indexes:writeDataPoint({ blobs: [new Uint8Array(...)] })→ TS2322.Fix
One commit per API, each a hand-written
JSG_STRUCT_TS_OVERRIDEwidening the field toArrayBuffer | ArrayBufferView(same shape asWorkerLoaderModule.wasm), the four regenerated snapshot files, and a type test. Types only; no runtime change.Tests
bazel test //types:test/types/worker-loader //types:test/types/r2 //types:test/types/analytics-engine(new; each fails against the previous snapshot)bazel test //src/workerd/api/tests:worker-loader-test@ //src/workerd/api/tests:r2-test@ //src/workerd/api/tests:analytics-engine-test@(existing runtime coverage, unchanged)just generate-typesreproduces the snapshot edits byte for byte.Uint8Array,DataView, asubarray()view and aSharedArrayBufferall reach the mock backends as the expected bytes.Tested on macOS (Apple Silicon) only.