@exodus/patch-broken-hermes-typed-arrays

中文文档
1.0.0-alpha.1发布于 2024/08/18MITES Module零依赖近 30 天下载 4.2万

Fix broken Hermes engine TypedArray implementation for React Native

原始使用文档
文档目录5 节
  1. What is this about?
  2. The fix
  3. The problem, in more details
  4. This is not limited to buffer
  5. License

@exodus/patch-broken-hermes-typed-arrays

Fix broken Hermes engine TypedArray implementation for React Native

Simply:

import '@exodus/patch-broken-hermes-typed-arrays'

What is this about?

The problem behind this issue:

> Buffer.alloc(10).subarray(0).toString('hex')
'0,0,0,0,0,0,0,0,0,0'
// What?

You might be inclined to fix the specific location where this is throwing (e.g. with a Buffer.from), but that is a mistake.

Most important: it is very hard to track all those.

Also, Buffer.from(arg) is a copy and inefficiency, Buffer.from(x.buffer, x.byteOffset, x.byteLength) is awkward, prone to human errors and can trigger security checks for doing something on buffer.buffer manually (which is an unsafe practice, e.g. a mistype like x.byteLegnth can leak passwords/secrets to other users by exposing unrelated application memory). You don't want a ton of copies of that pattern in your codebase.

Fixing this on Buffer (e.g. by monkey-patching it) is also insufficient — your codebase could include multiple dependencies which bundled buffer, and all those instances won't be fixed that way!

Also, this affects more than Buffer and more than subarray — Hermes engine doesn't implement TypedArray correctly.

i.e. Hermes implementation of TypedArray doesn't follow these sections of the specification:

The fix

Overall, this module is just a glorified version of the following snippet (but with .map/.filter support and safeguards against Hermes updates, to detect if things change).

TypedArray.prototype.subarray = function (...args) {
  var arr = subarray.apply(this, args)
  if (!this.constructor || arr.constructor === this.constructor) return arr
  return new this.constructor(arr.buffer, arr.byteOffset, arr.length)
}

Note: the above version might be not spec-compliant if Hermes implements resizable TypedArrays from ECMAScript 2024, or e.g. starts supporting Symbol.Species

Also the snippet above doesn't have a Hermes check, so it will also blindly patch arrays if you switch to JavaScriptCore

This is why this module exists — it handles all that

The problem, in more details

E.g. with buffer:

// ... import Buffer polyfill from https://npmjs.com/package/buffer
// ... and a console.log polyfill
console.log(Buffer.alloc(2).subarray(0, 1).constructor.name)
console.log(Buffer.alloc(2).map(() => 1).constructor.name)
console.log(Buffer.alloc(2).filter(() => true).constructor.name)
% node buftest.0.js                                         
Buffer
Buffer
Buffer
% jsc buftest.0.js 
Buffer
Buffer
Buffer
% hermes buftest.0.js
Uint8Array
Uint8Array
Uint8Array

This is not limited to buffer

// Let's assume this will get transpiled for a demo, Hermes has no `class` support

class TestArray extends Uint16Array {
  static instances = 0
  constructor(...args) {
    super(...args)
    // console.log('We can do something here!')
    TestArray.instances++ // count constructor calls
    return this
  }

  hello() {
    return 'hi there'
  }
}

var arr = new TestArray(10)
// TestArray.instances: 1

var mapped = arr.map((_, i) => i * 10)
// TestArray.instances: 2

console.log(TestArray.instances) // 2 everywhere, but 1 in Hermes
console.log(mapped.constructor.name) // 'TestArray' everywhere, but 'Uint16Array' in Hermes
console.log(mapped.hello()) // throws in Hermes

License

MIT

版本与发布记录

@exodus/patch-broken-hermes-typed-arrays 共发布 2 个版本,首次发布于 2024/08/15;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.0-alpha.1latest2024/08/182 年前
1.0.0-alpha.02024/08/152 年前

关于 @exodus/patch-broken-hermes-typed-arrays 的常见问题

@exodus/patch-broken-hermes-typed-arrays 是什么?

Fix broken Hermes engine TypedArray implementation for React Native

如何安装 @exodus/patch-broken-hermes-typed-arrays?

在项目目录运行 npm install @exodus/patch-broken-hermes-typed-arrays,或使用 pnpm add / yarn add / bun add。

@exodus/patch-broken-hermes-typed-arrays 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/exodus__patch-broken-hermes-typed-arrays 是否存在。

@exodus/patch-broken-hermes-typed-arrays 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@exodus/patch-broken-hermes-typed-arrays 可以商用吗?

@exodus/patch-broken-hermes-typed-arrays 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@exodus/patch-broken-hermes-typed-arrays 的下载量有多少?

根据 npm 官方下载统计,@exodus/patch-broken-hermes-typed-arrays 在 2026/10/14 – 2026/11/12 这 30 天内被下载 42,203 次,日均约 1,407 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@exodus/patch-broken-hermes-typed-arrays 还在维护吗?

最新版本 1.0.0-alpha.1 发布于 2024/08/18(2 年前);至今共发布 2 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/08/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。