Skip to content

Declare Buffer#subarray in the flow-typed node libdef - #2145

Merged
meta-codesync[bot] merged 1 commit into
mainfrom
export-D128877321
Nov 2, 2026
Merged

meta-codesync[bot] merged 1 commit into
mainfrom
export-D128877321

Conversation

@SamChou19815

Copy link
Copy Markdown
Member

Summary:
X-link: react/react-native#59737

Node's Buffer#subarray returns a Buffer that shares memory with the receiver. The node environment libdef declares slice(): this but no subarray, so subarray resolves to Uint8Array#subarray from Flow's builtins. TypeScript's library types that as returning Uint8Array<TArrayBuffer>, not this. Once Flow adopts that signature, buf.subarray(...).toString('hex') and .equals(...) stop checking.

Declare subarray(start?: number, end?: number): this next to slice, matching Node's API and types/node. Nothing changes under the current builtins.

Changelog: [Internal]

Differential Revision: D128877321

Summary:
X-link: react/react-native#59737

Node's `Buffer#subarray` returns a `Buffer` that shares memory with the receiver. The node environment libdef declares `slice(): this` but no `subarray`, so `subarray` resolves to `Uint8Array#subarray` from Flow's builtins. TypeScript's library types that as returning `Uint8Array<TArrayBuffer>`, not `this`. Once Flow adopts that signature, `buf.subarray(...).toString('hex')` and `.equals(...)` stop checking.

Declare `subarray(start?: number, end?: number): this` next to `slice`, matching Node's API and `types/node`. Nothing changes under the current builtins.

Changelog: [Internal]

Differential Revision: D128877321
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 1, 2026
@meta-codesync

meta-codesync Bot commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

@SamChou19815 has exported this pull request. If you are a Meta employee, you can view the originating Diff in D128877321.

@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

Metro review: Looks good

Risk: low · Commit: 70c0674 · 4 commands run

This adds a type-only subarray(start?: number, end?: number): this declaration to the Buffer libdef, next to slice, and has no runtime effect. A Flow probe confirms the description: subarray now resolves to the new declaration, its return type is still Buffer, and the toString/equals uses of that shape still check, the same as on base. The Flow 0.338 builtin it shadows, lib.es5.js, is also declared as returning this, so nothing changes under the current builtins.

No findings.

Verified
  • ✅ yarn --cwd <head checkout> typecheck — No errors on the PR head, 575 files checked, before adding the probe file.
  • ✅ yarn --cwd <head checkout> typecheck (with packages/metro/src/lib/reviewProbe.js) (failed as expected) — Only the 2 deliberate probe errors. subarray returns Buffer, and the bad argument error points at flow-typed/environment/node.js:121. The toString, equals and Buffer assignment lines check.
  • ✅ yarn --cwd <base checkout> typecheck (with packages/metro/src/lib/reviewProbe.js) (failed as expected) — Same 2 deliberate errors. subarray also returns Buffer on base, resolved from Flow's lib.es5.js:1886 (subarray(begin?: number, end?: number): this), so current behavior is unchanged.
  • ✅ yarn --cwd <head checkout> eslint flow-typed/environment/node.js — 0 errors, but ESLint ignores this file via an ignore pattern, so it was not actually linted.

Not run: Jest and benchmarks were not run: the change is a Flow libdef declaration with no runtime or performance effect. The future Flow builtin signature that returns Uint8Array<TArrayBuffer> was not tested, because it isn't in the installed Flow 0.338. The new declaration's return type was checked under current builtins only. An initial compound shell command (git state, Flow and ESLint together) was denied by the permission system and not re-run; Flow and ESLint were run as separate commands instead, and the git status check was skipped. Windows is not relevant to this type-only change.

Automated review by Claude. Advisory only: it never approves or blocks, and a maintainer makes the call.

@meta-codesync
meta-codesync Bot merged commit 842fae3 into main Nov 2, 2026
15 of 19 checks passed
@SamChou19815
SamChou19815 deleted the export-D128877321 branch November 2, 2026 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. meta-exported needs-meta-import

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant