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