`TextEncoder.encode` is typed `Uint8Array<ArrayBufferLike>` in workers-types; it returns a non-shared buffer

Aperta Adatta ai principianti
#8,807 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
cpp, typescript
Ambito
api

Direzione di ricerca

La dichiarazione da modificare è TextEncoder.encode, il cui tipo viene generato dai binding C++ in src/workerd/api/encoding.h. Usa come modello il JSG_TS_OVERRIDE esistente di encodeInto in quel file e verifica se l'output dello stream basato su encodeUtf8Chunk o bytes() necessiti della stessa modifica. Il lavoro è completato quando il repro dell'issue compila in strict mode con @cloudflare/workers-types e i test dei tipi passano.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

In @cloudflare/workers-types 5.20261121.1, TextEncoder declares:

encode(input?: string): Uint8Array;

Since TypeScript 5.7 made typed arrays generic over their buffer, a bare Uint8Array means Uint8Array<ArrayBufferLike>, which includes SharedArrayBuffer. encode always returns a view over a fresh, non-shared ArrayBuffer, so the precise type is Uint8Array<ArrayBuffer>. TypeScript's own lib.dom.d.ts and lib.webworker.d.ts (5.9) declare encode(input?: string): Uint8Array<ArrayBuffer>, and @types/node declares NodeJS.NonSharedUint8Array.

Repro

TypeScript 7.0.2, @cloudflare/workers-types 5.20261121.1, "types": ["@cloudflare/workers-types"], "strict": true:

export function encode(value: string): Uint8Array<ArrayBuffer> {
  return new TextEncoder().encode(value);
}
error TS2322: Type 'Uint8Array<ArrayBufferLike>' is not assignable to type 'Uint8Array<ArrayBuffer>'.
  Type 'ArrayBufferLike' is not assignable to type 'ArrayBuffer'.
    Type 'SharedArrayBuffer' is not assignable to type 'ArrayBuffer'.

Why it matters

Code that runs on both Node and Workers has to type the result as Uint8Array<ArrayBuffer>, because Node's and lib.dom's BufferSource reject ArrayBufferLike-backed views. With workers-types loaded, the same code then needs a cast or a Uint8Array.from(...) copy.

Request

Declare encode(input?: string): Uint8Array<ArrayBuffer> (for example with a JSG_TS_OVERRIDE in src/workerd/api/encoding.h, as encodeInto already has). Other APIs that return freshly allocated buffers, such as encodeUtf8Chunk-backed streams or bytes(), may have the same widening.

Related: #7833 tracks rejecting SharedArrayBuffer-backed views at runtime in SubtleCrypto. That issue is about runtime behaviour; this one is only about the declared return type.

Lingua principale
C++
Stelle
9.3k
Fork
845
Merge medio
1g 23h
PR unite (30g)
558

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di cloudflare/workerd

Tutte le issue di cloudflare/workerd

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.