hermes icon indicating copy to clipboard operation
hermes copied to clipboard

using TextEncoder with hermes

Open ahmed-adly-khalil opened this issue 3 years ago • 13 comments

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 avatar Mar 26 '23 21:03 ahmed-adly-khalil

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';

ahmed-adly-khalil avatar Mar 26 '23 22:03 ahmed-adly-khalil

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 avatar Mar 27 '23 17:03 tmikov

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

tmikov avatar Jul 28 '23 17:07 tmikov

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

tmikov avatar Feb 17 '24 21:02 tmikov

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

Is it already available with RN 0.73.6?

anfearco avatar Mar 21 '24 14:03 anfearco

@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 avatar Mar 21 '24 20:03 tmikov

@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

ranile avatar Mar 29 '24 11:03 ranile

@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!

anfearco avatar Apr 05 '24 12:04 anfearco

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

abhinav-0107 avatar May 01 '24 09:05 abhinav-0107

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

iamkem avatar Jun 03 '24 09:06 iamkem

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

g-otn avatar Jul 15 '24 10:07 g-otn

import "text-encoding-polyfill" fixed it here

webjay avatar Sep 14 '24 18:09 webjay

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

Sorono-Dev avatar Oct 22 '24 16:10 Sorono-Dev