Debian Codegraph @

node/@bufbuild/protobuf/dist/esm/wire/text-encoding.js

raw
1 // Copyright 2021-2026 Buf Technologies, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 let te;
15 /**
16 * Protobuf-ES requires the Text Encoding API to convert UTF-8 from and to
17 * binary. This WHATWG API is widely available, but it is not part of the
18 * ECMAScript standard. This function used to be the way to supply an
19 * implementation on runtimes that do not provide one.
20 *
21 * It only configures the copy of the library it is imported from. To provide
22 * the encoding API for every copy of Protobuf-ES in an isolate, install
23 * `TextEncoder` (with the methods `encode` and optionally `encodeInto`) and
24 * `TextDecoder` (with the `fatal: true` constructor argument and the `decode`
25 * method) on `globalThis`.
26 *
27 * Providing `encodeUtf8Into` is optional for backwards compatibility. If it
28 * is omitted, we emulate it with a wrapper that calls `encodeUtf8`.
29 *
30 * Note that the Text Encoding API does not provide a way to validate UTF-8.
31 * Our implementation uses String.prototype.isWellFormed, and falls back
32 * to use encodeURIComponent().
33 *
34 * @deprecated Install `TextEncoder` and `TextDecoder` on `globalThis` instead.
35 */
36 export function configureTextEncoding(textEncoding) {
37 var _a;
38 te = Object.assign(Object.assign({}, textEncoding), { encodeUtf8Into: (_a = textEncoding.encodeUtf8Into) !== null && _a !== void 0 ? _a : emulateEncodeInto(textEncoding.encodeUtf8.bind(textEncoding)) });
39 }
40 export function getTextEncoding() {
41 if (!te) {
42 const globals = globalThis;
43 if (!globals.TextEncoder || !globals.TextDecoder) {
44 throw new Error("encoding API missing: install TextEncoder and TextDecoder on globalThis");
45 }
46 const textEncoder = new globals.TextEncoder();
47 const textDecoder = new globals.TextDecoder();
48 let textDecoderStrict;
49 const config = {
50 encodeUtf8(text) {
51 return textEncoder.encode(text);
52 },
53 decodeUtf8(bytes, strict) {
54 if (strict) {
55 if (!textDecoderStrict) {
56 textDecoderStrict = new globals.TextDecoder("utf-8", {
57 fatal: true,
58 });
59 }
60 return textDecoderStrict.decode(bytes);
61 }
62 return textDecoder.decode(bytes);
63 },
64 checkUtf8(text) {
65 try {
66 encodeURIComponent(text);
67 return true;
68 }
69 catch (_) {
70 return false;
71 }
72 },
73 };
74 // If encodeInto is available, use it. Otherwise, configureTextEncoding
75 // fills in a slower fallback that uses encodeUtf8.
76 if (textEncoder.encodeInto) {
77 config.encodeUtf8Into = textEncoder.encodeInto.bind(textEncoder);
78 }
79 // Native String.prototype.isWellFormed, if the runtime provides it.
80 const nativeStringIsWellFormed = String.prototype.isWellFormed;
81 if (nativeStringIsWellFormed) {
82 config.checkUtf8 = (text) => {
83 return nativeStringIsWellFormed.call(text);
84 };
85 }
86 configureTextEncoding(config);
87 }
88 return te;
89 }
90 /**
91 * Simplistic polyfill for encodeUtf8Into.
92 *
93 * @private
94 */
95 export function emulateEncodeInto(encodeUtf8) {
96 return (text, dest) => {
97 const bytes = encodeUtf8(text);
98 dest.set(bytes);
99 return { written: bytes.byteLength };
100 };
101 }