Skip to content

Commit

Permalink
chore: use native base64
Browse files Browse the repository at this point in the history
  • Loading branch information
lpatiny committed Jul 30, 2024
1 parent ed8caea commit cfe8b6c
Showing 1 changed file with 2 additions and 41 deletions.
43 changes: 2 additions & 41 deletions scripts/build_help.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const homedir = new URL('../openchemlib/src/main/resources/html/', import.meta.u
const pngNames = await readdir(new URL('editor', homedir)).then(files => files.filter(file => file.endsWith('.png')))
const images = {}
for (const pngName of pngNames) {
const blob = await readFile(new URL(`editor/${pngName}`, homedir))
const blob = await readFile(new URL(`editor/${pngName}`, homedir), 'base64')
const encoded = toBase64URL(blob, 'image/png')
images[pngName] = encoded
}
Expand All @@ -23,45 +23,6 @@ html = html.replace('<link type="text/css" href="../styles.css" rel="stylesheet"

await writeFile(new URL('../help.html', import.meta.url), html)

/*
* base64-arraybuffer
* https://github.com/niklasvh/base64-arraybuffer
*
* Copyright (c) 2012 Niklas von Hertzen
* Licensed under the MIT license.
*/

function encode(bytes) {

const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';

// Use a lookup table to find the index.
const lookup = new Uint8Array(256);
for (let i = 0; i < chars.length; i++) {
lookup[chars.charCodeAt(i)] = i;
}

let i;
let len = bytes.length;
let base64 = '';

for (i = 0; i < len; i += 3) {
base64 += chars[bytes[i] >> 2];
base64 += chars[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)];
base64 += chars[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)];
base64 += chars[bytes[i + 2] & 63];
}

if (len % 3 === 2) {
base64 = `${base64.substring(0, base64.length - 1)}=`;
} else if (len % 3 === 1) {
base64 = `${base64.substring(0, base64.length - 2)}==`;
}

return base64;
}

function toBase64URL(bytes, type) {
const base64 = encode(bytes);
function toBase64URL(base64, type) {
return `data:${type};base64,${base64}`;
}

0 comments on commit cfe8b6c

Please sign in to comment.