minigame-std
Version:
Cross-platform standard library for WeChat minigame and web browsers with unified APIs for crypto, fs, fetch, storage, and more.
142 lines (141 loc) • 4.72 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
let happy_codec = require("happy-codec");
let src_std_internal_mod_ts = require("./_internal.cjs");
let src_std_platform_mod_ts = require("./platform.cjs");
//#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$1(data) {
return typeof wx.encode === "function" && !(0, src_std_platform_mod_ts.isMiniGameHarmonyOS)() && !(0, src_std_platform_mod_ts.isMiniGameHarmonyPC)() ? new Uint8Array(wx.encode({
data,
format: FORMAT
})) : (0, happy_codec.encodeUtf8)(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$1(data, options) {
const { fatal = false, ignoreBOM = false } = options ?? {};
if (!fatal && !ignoreBOM && typeof wx.decode === "function") {
const ab = (0, src_std_internal_mod_ts.bufferSourceToAb)(data);
return wx.decode({
data: ab,
format: FORMAT
});
}
return (0, happy_codec.decodeUtf8)(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$1 : happy_codec.encodeUtf8)(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$1 : happy_codec.decodeUtf8)(data, options);
}
//#endregion
Object.defineProperty(exports, "decodeBase64", {
enumerable: true,
get: function() {
return happy_codec.decodeBase64;
}
});
Object.defineProperty(exports, "decodeByteString", {
enumerable: true,
get: function() {
return happy_codec.decodeByteString;
}
});
Object.defineProperty(exports, "decodeHex", {
enumerable: true,
get: function() {
return happy_codec.decodeHex;
}
});
exports.decodeUtf8 = decodeUtf8;
Object.defineProperty(exports, "encodeBase64", {
enumerable: true,
get: function() {
return happy_codec.encodeBase64;
}
});
Object.defineProperty(exports, "encodeByteString", {
enumerable: true,
get: function() {
return happy_codec.encodeByteString;
}
});
Object.defineProperty(exports, "encodeHex", {
enumerable: true,
get: function() {
return happy_codec.encodeHex;
}
});
exports.encodeUtf8 = encodeUtf8;
//# sourceMappingURL=codec.cjs.map