Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions serial.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { encode } from 'https://esm.run/buffer-to-base64/encode';

const CONTROL_A = '\x01';
const CONTROL_B = '\x02';
const CONTROL_C = '\x03';
Expand Down Expand Up @@ -430,14 +432,6 @@ export default function Board({
upload: async (path, content, onprogress = noop) => {
if (port && !evaluating) {
const { stringify } = JSON;
const { fromCharCode } = String;

const base64 = view => {
const b64 = '';
for (let args = 2000, i = 0; i < view.length; i += args)
b64 += fromCharCode(...view.slice(i, i + args));
return btoa(b64);
};

const update = (i, length) => {
onprogress(i, length);
Expand All @@ -450,11 +444,14 @@ export default function Board({
new Uint8Array(await content.arrayBuffer())
;

const bytes = `binascii.a2b_base64(${stringify(await encode(view))})`;
const code = dedent(`
with open(${stringify(path)},"wb") as f:
import binascii
f.write(binascii.a2b_base64("${base64(view)}"))
f.close()
import binascii, deflate, io
with deflate.DeflateIO(io.BytesIO(${bytes}), deflate.ZLIB) as d:
f.write(d.read())
f.close()
d.close()
`);

let i = 0, { length } = code;
Expand Down