UNPKG

minigame-std

Version:

Cross-platform standard library for WeChat minigame and web browsers with unified APIs for crypto, fs, fetch, storage, and more.

104 lines (103 loc) 3.93 kB
import { decodeBase64, decodeByteString, decodeHex, decodeUtf8 as decodeUtf8$1, encodeBase64, encodeByteString, encodeHex, encodeUtf8 as encodeUtf8$1 } from "happy-codec"; import { bufferSourceToAb } from "./_internal.mjs"; import { isMiniGameHarmonyOS, isMiniGameHarmonyPC } from "./platform.mjs"; //#region src/macros/env.ts /** * 如果在小游戏环境中返回 true,否则返回 false。 */ const IS_MINA = __MINIGAME_STD_MINA__; //#endregion //#region src/std/codec/mina_utf8.ts /** * 小游戏环境的 UTF-8 编解码 * * @internal */ const FORMAT = "utf8"; /** * 将字符串数据编码为 Uint8Array。 * @param data - 需要编码的字符串数据。 * @returns 编码后的 Uint8Array。 */ function encodeUtf8$2(data) { return typeof wx.encode === "function" && !isMiniGameHarmonyOS() && !isMiniGameHarmonyPC() ? new Uint8Array(wx.encode({ data, format: FORMAT })) : encodeUtf8$1(data); } /** * 将 BufferSource 数据解码为字符串。 * * 当 `options` 为默认值(`fatal = false` 且 `ignoreBOM = false`)时,优先使用 `wx.decode` 以获得更好的性能; * 否则回退到 `happy-codec` 实现以支持完整的 `TextDecoderOptions` 功能。 * * @param data - 需要解码的 BufferSource。 * @param options - 解码选项(可选)。 * @param options.fatal - 如果为 `true`,遇到无效 UTF-8 序列会抛出异常;默认为 `false`,使用 U+FFFD 替换。 * @param options.ignoreBOM - 如果为 `true`,保留 BOM;默认为 `false`,自动删除 BOM。 * @returns 解码后的字符串。 */ function decodeUtf8$2(data, options) { const { fatal = false, ignoreBOM = false } = options ?? {}; if (!fatal && !ignoreBOM && typeof wx.decode === "function") { const ab = bufferSourceToAb(data); return wx.decode({ data: ab, format: FORMAT }); } return decodeUtf8$1(data, options); } //#endregion //#region src/std/codec/mod.ts /** * Codec 模块:提供各种编码/解码功能。 * 除了 UTF-8 编码/解码功能外,其余编码/解码功能直接从 `happy-codec` 包中导出。 * * @module codec */ /** * 将字符串数据编码为 `Uint8Array`(UTF-8 编码)。 * @param data - 需要编码的字符串数据。 * @returns 编码后的 `Uint8Array`。 * @since 1.0.0 * @example * ```ts * const encoded = encodeUtf8('你好'); * console.log(encoded); // Uint8Array [228, 189, 160, 229, 165, 189] * ``` */ function encodeUtf8(data) { return (IS_MINA ? encodeUtf8$2 : encodeUtf8$1)(data); } /** * 将二进制数据解码为字符串(UTF-8 解码)。 * @param data - 需要解码的二进制数据。 * @param options - 解码选项(可选)。 * @param options.fatal - 如果为 `true`,遇到无效的 UTF-8 序列会抛出异常;如果为 `false`(默认),使用替换字符 U+FFFD 代替。 * @param options.ignoreBOM - 如果为 `true`,保留字节顺序标记(BOM);如果为 `false`(默认),自动删除 BOM。 * @returns 解码后的字符串。 * @throws {TypeError} 当 `options.fatal` 为 `true` 且输入包含无效的 UTF-8 序列时。 * @since 1.0.0 * @example * ```ts * // 基本用法 * const decoded = decodeUtf8(new Uint8Array([228, 189, 160, 229, 165, 189])); * console.log(decoded); // '你好' * * // 使用 fatal 选项处理无效字节 * const withReplacement = decodeUtf8(new Uint8Array([0xff, 0xfe])); * console.log(withReplacement); // '��'(使用替换字符) * * // 使用 ignoreBOM 选项保留 BOM * const withBOM = new Uint8Array([0xef, 0xbb, 0xbf, 0x48, 0x69]); // BOM + 'Hi' * decodeUtf8(withBOM); // 'Hi'(删除 BOM) * decodeUtf8(withBOM, { ignoreBOM: true }); // '\uFEFFHi'(保留 BOM) * ``` */ function decodeUtf8(data, options) { return (IS_MINA ? decodeUtf8$2 : decodeUtf8$1)(data, options); } //#endregion export { decodeBase64, decodeByteString, decodeHex, decodeUtf8, encodeBase64, encodeByteString, encodeHex, encodeUtf8 }; //# sourceMappingURL=codec.mjs.map