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

オープン 初心者向け
#8,286 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
78/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
javascript, node.js, typescript
領域
api, backend

調査の方向性

src/node/internal/internal_buffer.tsを読み、alloc()とそのbuffer.fill()の呼び出しに注目してください。fillが文字列の場合と、数値またはBufferの場合で、encoding引数がどのように渡されるかを比較し、issueに示されている3つの非文字列ケースの回帰テストを追加してください。完了条件は、これらのケースがNode.jsの出力と一致し、文字列のケースも引き続き動作することです。

索引モデルが issue の本文から書いたものです。

説明

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/

主要言語
C++
スター
9.1k
フォーク
823
平均マージ
1日 18時間
マージ済み PR(30日)
352

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

cloudflare/workerd のほかの issue

cloudflare/workerd の issue をすべて見る

似ている issue

C++ の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。