Making this work on iOS/Safari

pull/87/head
Zef Hemel 2022-10-08 15:46:56 +02:00
parent 915c05b483
commit b285af0198
12 changed files with 1278 additions and 17 deletions

View File

@ -7,7 +7,7 @@ import { copy } from "https://deno.land/std@0.158.0/fs/copy.ts";
import sass from "https://deno.land/x/denosass@1.0.4/mod.ts"; import sass from "https://deno.land/x/denosass@1.0.4/mod.ts";
import { bundleFolder } from "./json_bundle.ts"; import { bundleFolder } from "./json_bundle.ts";
import { bundleRun } from "./packages/plugos/bin/plugos-bundle.ts"; import { patchDenoLibJS } from "./packages/common/hack.ts";
// @ts-ignore trust me // @ts-ignore trust me
const esbuild: typeof esbuildWasm = Deno.run === undefined const esbuild: typeof esbuildWasm = Deno.run === undefined
@ -36,6 +36,10 @@ async function prepareAssets(dest: string) {
// exclude: [], // exclude: [],
// }); // });
let bundleJs = await Deno.readTextFile("dist/client.js");
bundleJs = patchDenoLibJS(bundleJs);
await Deno.writeTextFile("dist/client.js", bundleJs);
await bundleFolder("dist", "dist_bundle.json"); await bundleFolder("dist", "dist_bundle.json");
} }

View File

@ -10,11 +10,6 @@ export * as path from "https://deno.land/std@0.158.0/path/mod.ts";
export { readAll } from "https://deno.land/std@0.158.0/streams/conversion.ts"; export { readAll } from "https://deno.land/std@0.158.0/streams/conversion.ts";
export {
decode as b64decode,
encode as b64encode,
} from "https://deno.land/std@0.158.0/encoding/base64.ts";
export { export {
history, history,
historyKeymap, historyKeymap,

File diff suppressed because one or more lines are too long

View File

@ -1,11 +1,11 @@
import { walk } from "https://deno.land/std@0.159.0/fs/mod.ts"; import { walk } from "https://deno.land/std@0.159.0/fs/mod.ts";
import { b64encode } from "./dep_common.ts"; import { base64Encode } from "./packages/common/base64.ts";
export async function bundleFolder(path: string, bundlePath: string) { export async function bundleFolder(path: string, bundlePath: string) {
const bundle: Record<string, string> = {}; const bundle: Record<string, string> = {};
for await (const { path: filePath } of walk(path, { includeDirs: false })) { for await (const { path: filePath } of walk(path, { includeDirs: false })) {
console.log("Bundling", filePath); console.log("Bundling", filePath);
const b64content = b64encode(await Deno.readFile(filePath)); const b64content = base64Encode(await Deno.readFile(filePath));
bundle[filePath] = b64content; bundle[filePath] = b64content;
} }
await Deno.writeTextFile(bundlePath, JSON.stringify(bundle, null, 2)); await Deno.writeTextFile(bundlePath, JSON.stringify(bundle, null, 2));

1223
out.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
import { b64decode } from "../../dep_common.ts"; import { base64Decode } from "./base64.ts";
type AssetBundle = Record<string, string>; type AssetBundle = Record<string, string>;
@ -10,7 +10,7 @@ export function assetReadFileSync(
if (!content) { if (!content) {
throw new Error(`No such file ${path}`); throw new Error(`No such file ${path}`);
} }
return b64decode(content); return base64Decode(content);
} }
export function assetReadTextFileSync( export function assetReadTextFileSync(

View File

@ -0,0 +1,12 @@
import { assertEquals } from "../../test_dep.ts";
import { base64Decode } from "./base64.ts";
import { base64Encode } from "./base64.ts";
Deno.test("Base 64 encoding", () => {
const buf = new Uint8Array(3);
buf[0] = 1;
buf[1] = 2;
buf[2] = 3;
assertEquals(buf, base64Decode(base64Encode(buf)));
});

20
packages/common/base64.ts Normal file
View File

@ -0,0 +1,20 @@
import { buf } from "https://deno.land/x/sqlite3@0.6.1/src/util.ts";
export function base64Decode(s: string): Uint8Array {
const binString = atob(s);
const len = binString.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binString.charCodeAt(i);
}
return bytes;
}
export function base64Encode(buffer: Uint8Array): string {
let binary = "";
const len = buffer.byteLength;
for (let i = 0; i < len; i++) {
binary += String.fromCharCode(buffer[i]);
}
return btoa(binary);
}

4
packages/common/hack.ts Normal file
View File

@ -0,0 +1,4 @@
export function patchDenoLibJS(code: string): string {
// The Deno std lib has one occurence of a regex that Webkit JS doesn't (yet parse), we'll strip it because it's likely never invoked anyway, YOLO
return code.replaceAll("/(?<=\\n)/", "/()/");
}

View File

@ -1,11 +1,11 @@
// import { mkdir, readdir, readFile, stat, unlink, writeFile } from "fs/promises"; // import { mkdir, readdir, readFile, stat, unlink, writeFile } from "fs/promises";
import { path } from "../../../dep_common.ts"; import { path } from "../../../dep_common.ts";
import { b64decode, b64encode } from "../../../dep_common.ts";
import { readAll } from "../../../dep_common.ts"; import { readAll } from "../../../dep_common.ts";
import { FileMeta } from "../types.ts"; import { FileMeta } from "../types.ts";
import { FileData, FileEncoding, SpacePrimitives } from "./space_primitives.ts"; import { FileData, FileEncoding, SpacePrimitives } from "./space_primitives.ts";
import { Plug } from "../../plugos/plug.ts"; import { Plug } from "../../plugos/plug.ts";
import { mime } from "https://deno.land/x/mimetypes@v1.0.0/mod.ts"; import { mime } from "https://deno.land/x/mimetypes@v1.0.0/mod.ts";
import { base64Decode, base64Encode } from "../base64.ts";
function lookupContentType(path: string): string { function lookupContentType(path: string): string {
return mime.getType(path) || "application/octet-stream"; return mime.getType(path) || "application/octet-stream";
@ -50,7 +50,7 @@ export class DiskSpacePrimitives implements SpacePrimitives {
case "dataurl": case "dataurl":
{ {
const f = await Deno.open(localPath, { read: true }); const f = await Deno.open(localPath, { read: true });
const buf = b64encode(await readAll(f)); const buf = base64Encode(await readAll(f));
Deno.close(f.rid); Deno.close(f.rid);
data = `data:${contentType};base64,${buf}`; data = `data:${contentType};base64,${buf}`;
@ -101,7 +101,7 @@ export class DiskSpacePrimitives implements SpacePrimitives {
case "dataurl": case "dataurl":
await Deno.writeFile( await Deno.writeFile(
localPath, localPath,
b64decode((data as string).split(",")[1]), base64Decode((data as string).split(",")[1]),
); );
break; break;
case "arraybuffer": case "arraybuffer":

View File

@ -8,6 +8,7 @@ export const esbuild: typeof esbuildWasm = Deno.run === undefined
import { path } from "../../dep_server.ts"; import { path } from "../../dep_server.ts";
import { denoPlugin } from "../esbuild_deno_loader/mod.ts"; import { denoPlugin } from "../esbuild_deno_loader/mod.ts";
import { patchDenoLibJS } from "../common/hack.ts";
export async function compile( export async function compile(
filePath: string, filePath: string,
@ -72,6 +73,7 @@ export async function compile(
} }
let jsCode = await Deno.readTextFile(outFile); let jsCode = await Deno.readTextFile(outFile);
jsCode = patchDenoLibJS(jsCode);
await Deno.remove(outFile); await Deno.remove(outFile);
return `(() => { ${jsCode} return mod;})()`; return `(() => { ${jsCode} return mod;})()`;
} finally { } finally {

View File

@ -1,6 +1,7 @@
export function safeRun(fn: () => Promise<void>) { export function safeRun(fn: () => Promise<void>) {
fn().catch((e: any) => { fn().catch((e: any) => {
console.error("Caught error", e.message); console.error("Caught error", e.message);
// throw e; // throw e;
}); });
} }