facebook / hermes

A JavaScript engine optimized for running React Native.
https://hermesengine.dev/
MIT License
9.91k stars 641 forks source link

using TextEncoder with hermes #948

Closed ahmed-adly-khalil closed 1 year ago

ahmed-adly-khalil commented 1 year ago

Problem

i'm trying to use https://github.com/nats-io/nats.ws in react native app, and getting the error ReferenceError: Property 'TextEncoder' doesn't exist, js engine: hermes

The package https://github.com/nats-io/nats.ws uses web sockets to connect to NATS server and used TextEncoder to serialize messages before sending to NATS

Solution

I understand that hermes leaves out some features from other JS engines like v8 in favor of performance, is there a way to add specific features like TextEncoder on the app level?

More Context

https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder has the spec for TextEncoder

ahmed-adly-khalil commented 1 year ago

this worked for me npm install --save text-encoding create globals.js on the app root and add global.TextEncoder = require('text-encoding').TextEncoder; import it inside index.js import './globals.js';

tmikov commented 1 year ago

I also want to note that TextEncoder is not a part of the ECMAScript spec. As far as I can tell, it is defined by WHATWG as part of the web APIs.

The distinction is subtle but important. JS engines implement ECMAScript, but web browsers implement the web standards. Hermes is only a JS engine, not a web browser.

With that said, undeniably there are useful APIs defined as part of the web standards. Theoretically it would be nice if there was a second repository with "add-ons" like this one.

tmikov commented 1 year ago

FYI. https://github.com/facebook/hermes/discussions/1072 .

tmikov commented 9 months ago

@ahmed-adly-khalil as of https://github.com/facebook/hermes/commit/3863a36a53005dd1e6d39ea0d4ef5573bafde910, Hermes now supports TextEncoder.

anfearco commented 8 months ago

@ahmed-adly-khalil as of 3863a36, Hermes now supports TextEncoder.

Is it already available with RN 0.73.6?

tmikov commented 7 months ago

@anfearco new features are not automatically picked in RN point releases. I think RN's policy is that point releases only contain bug fixes, but I am not sure. It is possible that if people requested it, they might consider including it in the next point release.

ranile commented 7 months ago

@ahmed-adly-khalil as of 3863a36, Hermes now supports TextEncoder.

Is it already available with RN 0.73.6?

The commit is tagged hermes-2024-02-20-RNv0.74.0-999cfd9979b5f57b1269119679ab8cdf60897de9 so I would assume not

anfearco commented 7 months ago

@anfearco new features are not automatically picked in RN point releases. I think RN's policy is that point releases only contain bug fixes, but I am not sure. It is possible that if people requested it, they might consider including it in the next point release.

@tmikov That makes sense. Thanks for the response!

abhinav-0107 commented 6 months ago

@ahmed-adly-khalil Worked for me. Thanks!

iamkem commented 5 months ago

this worked for me npm install --save text-encoding create globals.js on the app root and add global.TextEncoder = require('text-encoding').TextEncoder; import it inside index.js import './globals.js';

Thanks, it's save my day

g-otn commented 4 months ago

If you need to polyfill, text-encoding is deprecated, use fast-text-encoder instead.

webjay commented 2 months ago

import "text-encoding-polyfill" fixed it here

Sorono-Dev commented 3 weeks ago

This was the best solution for me : https://github.com/EvanBacon/text-decoder