Wyle.Gong-巩文昕 2623f9aa09 ui
2025-04-23 14:47:06 +08:00

60 lines
1.6 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.dump = exports.decode = exports.encode = void 0;
/*
* Copyright 2018-2020 The NATS Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Encode binary data to a base64 string
* @param {Uint8Array} bytes to encode to base64
*/
function encode(bytes) {
return btoa(String.fromCharCode(...bytes));
}
exports.encode = encode;
/**
* Decode a base64 encoded string to a binary Uint8Array
* @param {string} b64str encoded string
*/
function decode(b64str) {
const bin = atob(b64str);
const bytes = new Uint8Array(bin.length);
for (let i = 0; i < bin.length; i++) {
bytes[i] = bin.charCodeAt(i);
}
return bytes;
}
exports.decode = decode;
/**
* @ignore
*/
function dump(buf, msg) {
if (msg) {
console.log(msg);
}
let a = [];
for (let i = 0; i < buf.byteLength; i++) {
if (i % 8 === 0) {
a.push("\n");
}
let v = buf[i].toString(16);
if (v.length === 1) {
v = "0" + v;
}
a.push(v);
}
console.log(a.join(" "));
}
exports.dump = dump;