44 lines
1.1 KiB
JavaScript
44 lines
1.1 KiB
JavaScript
|
|
const text_encoder = new TextEncoder();
|
||
|
|
const text_decoder = new TextDecoder();
|
||
|
|
function get_relative_path(from, to) {
|
||
|
|
const from_parts = from.split(/[/\\]/);
|
||
|
|
const to_parts = to.split(/[/\\]/);
|
||
|
|
from_parts.pop();
|
||
|
|
while (from_parts[0] === to_parts[0]) {
|
||
|
|
from_parts.shift();
|
||
|
|
to_parts.shift();
|
||
|
|
}
|
||
|
|
let i = from_parts.length;
|
||
|
|
while (i--) from_parts[i] = "..";
|
||
|
|
return from_parts.concat(to_parts).join("/");
|
||
|
|
}
|
||
|
|
function base64_encode(bytes) {
|
||
|
|
if (globalThis.Buffer) {
|
||
|
|
return globalThis.Buffer.from(bytes).toString("base64");
|
||
|
|
}
|
||
|
|
let binary = "";
|
||
|
|
for (let i = 0; i < bytes.length; i++) {
|
||
|
|
binary += String.fromCharCode(bytes[i]);
|
||
|
|
}
|
||
|
|
return btoa(binary);
|
||
|
|
}
|
||
|
|
function base64_decode(encoded) {
|
||
|
|
if (globalThis.Buffer) {
|
||
|
|
const buffer = globalThis.Buffer.from(encoded, "base64");
|
||
|
|
return new Uint8Array(buffer);
|
||
|
|
}
|
||
|
|
const binary = atob(encoded);
|
||
|
|
const bytes = new Uint8Array(binary.length);
|
||
|
|
for (let i = 0; i < binary.length; i++) {
|
||
|
|
bytes[i] = binary.charCodeAt(i);
|
||
|
|
}
|
||
|
|
return bytes;
|
||
|
|
}
|
||
|
|
export {
|
||
|
|
text_encoder as a,
|
||
|
|
base64_encode as b,
|
||
|
|
base64_decode as c,
|
||
|
|
get_relative_path as g,
|
||
|
|
text_decoder as t
|
||
|
|
};
|