-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Runtime error on TextEncoder/TextDecoder #663
Comments
In addition to various polyfills, the protobuf-es/packages/protobuf/src/binary-encoding.ts Lines 73 to 74 in 246e6df
// my-binary-format.ts
import {BinaryWriter, BinaryReader} from '@bufbuild/protobuf';
const myTextDecoderLike = {
decode: (input?: Uint8Array): string => {
/* Implement this yourself */
}
};
const myTextEncoderLike = {
encode: (input?: string): Uint8Array => {
/* Implement this yourself */
}
};
export const readerFactory = (bytes: Uint8Array) => new BinaryReader(bytes, myTextDecoderLike);
export const writerFactory = (): IBinaryWriter => new BinaryWriter(myTextEncoderLike);
// usage
import {readerFactory, writerFactory} from './my-binary-format'
// read
export const featureSetDefaults = FeatureSetDefaults.fromBinary(
protoBase64.dec(...),
{ readerFactory }
);
// write
const featureSetDefaultsBin = FeatureSetDefaults.toBinary(
featureSetDefaults,
{ writerFactory }
);
Probably not since UTF-8 encoding/decoding is notoriously difficult to do correctly and fast. The TextEncoder/TextDecoder are available in all browsers along with Node, Deno, and Bun; and you can polyfill or customize the binary options as shown above if your runtime does not have them available. |
@Autokaka, we've just updated the docs to explain this a bit better:
For the specific case of For QuickJS, the best approach is probably to do the work in C with a solid library, and make it available to the JS world conforming to the TextDecoder / TextEncoder interfaces. A quick search suggests that it may have been done before: https://github.com/rsenn/qjs-modules/blob/main/quickjs-textcode.h |
Thanks, these answers help a lot! I'll have a try. |
v1.7.1 includes a fix - you will still have to bring your own implementation of the text encoding API, but we don't try to access it at module init time. |
I'm running @bufbuild/protobuf module on a JavaScript Runtime that could only accept ECMAScript (For example, QuickJS::Eval), and the runtime throw exception that said:
This issue occurs at 'feature-set.ts':
The TextEncoder/TextDecoder could only be used in browser/NodeJS platform.
According to the source code, there is no way to make configurations for TextEncoder/TextDecoder.
Should we implement the TextEncoder/TextDecoder using pure JavaScript since this library can run in any ECMAScript runtime?
Or will there be a configuration for this?
The text was updated successfully, but these errors were encountered: