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

Đang mở Phù hợp với người mới
#8,807 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
72/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
cpp, typescript
Lĩnh vực
api

Hướng nghiên cứu

Khai báo cần thay đổi là TextEncoder.encode, kiểu của nó được sinh từ các binding C++ trong src/workerd/api/encoding.h. Hãy dùng JSG_TS_OVERRIDE hiện có của encodeInto trong tệp đó làm mẫu, và kiểm tra xem đầu ra stream dựa trên encodeUtf8Chunk hoặc bytes() có cần cùng thay đổi hay không. Hoàn thành khi repro trong issue biên dịch được ở strict mode với @cloudflare/workers-types và các bài kiểm tra kiểu đều đạt.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
C++
Star
9.3k
Fork
845
Merge trung bình
1 ngày 23 giờ
Pull request đã merge (30 ngày)
558

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của cloudflare/workerd

Tất cả issue của cloudflare/workerd

Issue tương tự

Thêm issue về C++

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.