fix saving file on chrome

This commit is contained in:
Reimar 2025-10-16 08:35:34 +02:00
parent 978a658857
commit a0e983d5c3
2 changed files with 16 additions and 5 deletions

View File

@ -1,3 +1,5 @@
import { readFileToBytes, strToBytes } from "./utils.js";
const KEYWORDS = ["asset", "code"];
const MIN_KW_LENGTH = Math.min(...KEYWORDS.map((x) => x.length));
const MAX_KW_LENGTH = Math.max(...KEYWORDS.map((x) => x.length));
@ -8,10 +10,6 @@ function isKeyword(buffer) {
return KEYWORDS.includes(kw);
}
function strToBytes(str) {
return str.split("").map((x) => x.charCodeAt(0));
}
export class KarlkoderCodec {
static async en(name, code, assets) {
const ret = [];
@ -25,7 +23,7 @@ export class KarlkoderCodec {
ret.push(...strToBytes(";"));
ret.push(...strToBytes(asset.name));
ret.push(...strToBytes(asset.file.type));
ret.push(...await asset.file.bytes());
ret.push(...await readFileToBytes(asset.file));
}
ret.push(...strToBytes("code"));
ret.push(...strToBytes(name.length.toString()));

View File

@ -18,3 +18,16 @@ export function minifyJs(code) {
.filter((line) => line.length > 0)
.join("\n");
}
export function strToBytes(str) {
return str.split("").map((x) => x.charCodeAt(0));
}
export function readFileToBytes(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsArrayBuffer(file);
reader.onload = () => resolve(new Uint8Array(reader.result));
reader.onerror = () => reject(reader.error);
});
}