node:buffer: Buffer.alloc(size, fill, encoding) throws when fill is a number or a Buffer

Đang mở Phù hợp với người mới
#8,286 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
78/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ệ
javascript, node.js, typescript
Lĩnh vực
api, backend

Hướng nghiên cứu

Đọc src/node/internal/internal_buffer.ts, tập trung vào alloc() và lời gọi buffer.fill() của nó. So sánh cách truyền đối số encoding khi fill là chuỗi so với khi là số hoặc Buffer, rồi bổ sung kiểm thử hồi quy cho ba trường hợp không phải chuỗi được nêu trong issue. Kết quả cần đạt là các trường hợp đó khớp với đầu ra của Node.js và trường hợp chuỗi vẫn hoạt động.

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

Mô tả

Buffer.alloc(size, fill, encoding) throws ERR_INVALID_ARG_TYPE in workerd whenever fill is not a string and encoding is passed. Node.js ignores encoding in that case (per https://nodejs.org/api/buffer.html#static-method-bufferallocsize-fill-encoding, it only applies "if fill is a string") and returns the filled buffer.

worker.mjs:

import { Buffer } from "node:buffer";
const show = (f) => { try { return f().toString("hex"); } catch (e) { return `${e.code}: ${e.message}`; } };
export default { test() {
  console.log(show(() => Buffer.alloc(3, 5, "utf8")));
  console.log(show(() => Buffer.alloc(3, 0, "utf8")));
  console.log(show(() => Buffer.alloc(3, Buffer.from([7]), "utf8")));
  console.log(show(() => Buffer.alloc(3, "ab", "utf8")));
} };

config.capnp:

using Workerd = import "/workerd/workerd.capnp";
const config :Workerd.Config = (services = [(name = "main", worker = .w)]);
const w :Workerd.Worker = (
  modules = [(name = "worker.mjs", esModule = embed "worker.mjs")],
  compatibilityDate = "2026-11-07",
);

npx workerd test --experimental config.capnp

Actual (workerd):

ERR_INVALID_ARG_TYPE: The "start" argument must be of type number. Received type string ('utf8')
ERR_INVALID_ARG_TYPE: The "start" argument must be of type number. Received type string ('utf8')
ERR_INVALID_ARG_TYPE: The "start" argument must be of type number. Received type string ('utf8')
616261

Expected (Node.js v26.13.0):

050505
000000
070707
616261

Tested with workerd 1.20261107.1 (workerd 2026-11-07), compatibility date 2026-11-07, and src/node/internal/internal_buffer.ts on main (d85cb017).

In src/node/internal/internal_buffer.ts, alloc() calls buffer.fill(fill as any, encoding), passing encoding in the start parameter slot of fill(), which only reinterprets a string start as encoding when typeof val === 'string'.

Credit: https://ucsbplse.github.io/

Ngôn ngữ chính
C++
Star
9.1k
Fork
823
Merge trung bình
1 ngày 18 giờ
Pull request đã merge (30 ngày)
352

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.