Compare commits

..

27 Commits

Author SHA1 Message Date
semantic-release-bot da7036496d chore(release): 0.2.0-develop.2 [skip ci]
# [0.2.0-develop.2](https://git.lumeweb.com/LumeWeb/libportal/compare/v0.2.0-develop.1...v0.2.0-develop.2) (2023-06-23)

### Features

* initial verified streaming support ([41b20b1](41b20b1465))
2023-06-23 07:05:27 +00:00
Derrick Hammer a8e262bb7b
ci: fix wasm output path 2023-06-23 03:03:40 -04:00
Derrick Hammer cfc04b2509
ci: add quotes around bash 2023-06-23 02:54:39 -04:00
Derrick Hammer 2c77cfa478
ci: ensure bash runs 2023-06-23 02:53:02 -04:00
Derrick Hammer 4103ad4652
ci: switch to sourcing gvm in wasm script 2023-06-23 02:51:05 -04:00
Derrick Hammer 891397e535
ci: add gvm use call to script 2023-06-23 02:47:59 -04:00
Derrick Hammer 2f3a116f8d
ci: change wasm build to cd into folder 2023-06-23 02:45:17 -04:00
Derrick Hammer 1077c4d510
ci: fix typo 2023-06-23 02:37:42 -04:00
Derrick Hammer b9b35ab60c
ci: need go 1.19 2023-06-23 02:34:12 -04:00
Derrick Hammer c090a49f54
ci: try fix go path 2023-06-23 02:15:18 -04:00
Derrick Hammer 516d53d45c
ci: move name under step 2023-06-23 02:13:40 -04:00
Derrick Hammer 80638d62c2
ci: add name under step 2023-06-23 02:11:43 -04:00
Derrick Hammer 29b7333d5a
ci: remove name 2023-06-23 02:08:14 -04:00
Derrick Hammer af283f63e5
ci: move go get to node/run setup 2023-06-23 02:07:06 -04:00
Derrick Hammer 7edc7d78db
ci: missing ; 2023-06-23 02:02:33 -04:00
Derrick Hammer 6b98e2d6b0
ci: missing ; 2023-06-23 02:01:40 -04:00
Derrick Hammer fb5848f5e5
ci: enable script debug 2023-06-23 02:01:05 -04:00
Derrick Hammer 10f64fb05f
ci: fix tinygo download 2023-06-23 01:59:48 -04:00
Derrick Hammer 3b39b2ee88
ci: typo, installing same version twice 2023-06-23 01:58:23 -04:00
Derrick Hammer 2591ea9451
ci: only use go binary install 2023-06-23 01:57:16 -04:00
Derrick Hammer 8b08ecf68c
ci: add apt-get update 2023-06-23 01:55:35 -04:00
Derrick Hammer ebcd7fb8b9
ci: add bison 2023-06-23 01:54:56 -04:00
Derrick Hammer 0a502bd2f3
ci: add semi-colons 2023-06-23 01:54:17 -04:00
Derrick Hammer b8e08cbe05
ci: add source for gvm 2023-06-23 01:53:10 -04:00
Derrick Hammer 8ffceebb84
ci: fix duplicate job 2023-06-23 01:50:59 -04:00
Derrick Hammer 0f8c4f4780
ci: update build process 2023-06-23 01:49:29 -04:00
Derrick Hammer 41b20b1465
feat: initial verified streaming support 2023-06-23 01:49:29 -04:00
9 changed files with 979 additions and 3 deletions

View File

@ -9,6 +9,31 @@ workflows:
- node/run:
name: build
npm-run: build
pre-steps:
- run:
name: Setup Golang
command: >
sudo apt-get update;
sudo apt-get install bison;
bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer);
source ~/.gvm/scripts/gvm;
gvm install go1.4 -B;
gvm use go1.4 --default;
gvm install go1.19 -B;
VERSION=0.28.1;
TINYGO="tinygo_${VERSION}_amd64.deb";
wget https://github.com/tinygo-org/tinygo/releases/download/v$VERSION/$TINYGO;
sudo dpkg -i $TINYGO && rm $TINYGO;
setup:
- run:
name: Fetch Wasm Deps
command: >
source ~/.gvm/scripts/gvm;
gvm use go1.19 --default;
cd src/golang;
go get;
post-steps:
- persist_to_workspace:
root: .

4
npm-shrinkwrap.json generated
View File

@ -1,12 +1,12 @@
{
"name": "@lumeweb/libportal",
"version": "0.2.0-develop.1",
"version": "0.2.0-develop.2",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@lumeweb/libportal",
"version": "0.2.0-develop.1",
"version": "0.2.0-develop.2",
"dependencies": {
"@noble/curves": "^1.1.0",
"@noble/hashes": "^1.3.1",

View File

@ -1,6 +1,6 @@
{
"name": "@lumeweb/libportal",
"version": "0.2.0-develop.1",
"version": "0.2.0-develop.2",
"main": "lib/index.js",
"module": "lib/index.mjs",
"types": "lib/index.d.ts",
@ -30,6 +30,7 @@
"scripts": {
"prepare": "presetter bootstrap",
"build": "presetter run build",
"build:wasm": "bash -c \"source ~/.gvm/scripts/gvm; mkdir ./lib/wasm && cd src/golang; tinygo build -o ../../lib/wasm/bao.wasm -target wasm ./main.go\"",
"semantic-release": "semantic-release"
},
"dependencies": {

652
src/go_wasm.ts Normal file
View File

@ -0,0 +1,652 @@
const encoder = new TextEncoder();
const decoder = new TextDecoder();
type ImportObject = {
wasi_snapshot_preview1: {
proc_exit: (code) => void;
random_get: (bufPtr, bufLen) => number;
fd_seek: () => number;
fd_write: (fd, iovs_ptr, iovs_len, nwritten_ptr) => number;
fd_close: () => number;
fd_fdstat_get: () => number;
};
env: {
"syscall/js.valueInvoke": (
ret_addr,
v_addr,
args_ptr,
args_len,
args_cap,
) => void;
"syscall/js.valueDelete": (v_addr, p_ptr, p_len) => void;
"syscall/js.copyBytesToGo": (
ret_addr,
dest_addr,
dest_len,
dest_cap,
source_addr,
) => void;
"syscall/js.valueSet": (v_addr, p_ptr, p_len, x_addr) => void;
"syscall/js.copyBytesToJS": (
ret_addr,
dest_addr,
source_addr,
source_len,
source_cap,
) => void;
"syscall/js.valueNew": (
ret_addr,
v_addr,
args_ptr,
args_len,
args_cap,
) => void;
"syscall/js.valueInstanceOf": (v_addr, t_addr) => boolean;
"runtime.ticks": () => number;
"runtime.sleepTicks": (timeout) => void;
"syscall/js.valueLoadString": (
v_addr,
slice_ptr,
slice_len,
slice_cap,
) => void;
"syscall/js.stringVal": (ret_ptr, value_ptr, value_len) => void;
"syscall/js.valueIndex": (ret_addr, v_addr, i) => void;
"syscall/js.valueLength": (v_addr) => any;
"syscall/js.valueCall": (
ret_addr,
v_addr,
m_ptr,
m_len,
args_ptr,
args_len,
args_cap,
) => void;
"syscall/js.finalizeRef": (sp) => void;
"syscall/js.valueGet": (retval, v_addr, p_ptr, p_len) => void;
"syscall/js.valuePrepareString": (ret_addr, v_addr) => void;
"syscall/js.valueSetIndex": (v_addr, i, x_addr) => void;
};
};
let logLine: any = [];
export default class Go {
private _callbackTimeouts: Map<any, any>;
private _nextCallbackTimeoutID: number;
private _inst?: any;
private _values: any[] = [NaN, 0, null, true, false, global, this];
private _ids: Map<any, any> = new Map<any, any>();
private _idPool: any[] = [];
private _goRefCounts: any[] = [];
private exited = false;
private _resolveCallbackPromise?: () => void;
importObject: ImportObject;
constructor() {
this._callbackTimeouts = new Map();
this._nextCallbackTimeoutID = 1;
const mem = () => {
// The buffer may change when requesting more memory.
return new DataView(this._inst.exports.memory.buffer);
};
const setInt64 = (addr, v) => {
mem().setUint32(addr + 0, v, true);
mem().setUint32(addr + 4, Math.floor(v / 4294967296), true);
};
const getInt64 = (addr) => {
const low = mem().getUint32(addr + 0, true);
const high = mem().getInt32(addr + 4, true);
return low + high * 4294967296;
};
const loadValue = (addr) => {
const f = mem().getFloat64(addr, true);
if (f === 0) {
return undefined;
}
if (!isNaN(f)) {
return f;
}
const id = mem().getUint32(addr, true);
return this._values[id];
};
const storeValue = (addr, v) => {
const nanHead = 0x7ff80000;
if (typeof v === "number") {
if (isNaN(v)) {
mem().setUint32(addr + 4, nanHead, true);
mem().setUint32(addr, 0, true);
return;
}
if (v === 0) {
mem().setUint32(addr + 4, nanHead, true);
mem().setUint32(addr, 1, true);
return;
}
mem().setFloat64(addr, v, true);
return;
}
switch (v) {
case undefined:
mem().setFloat64(addr, 0, true);
return;
case null:
mem().setUint32(addr + 4, nanHead, true);
mem().setUint32(addr, 2, true);
return;
case true:
mem().setUint32(addr + 4, nanHead, true);
mem().setUint32(addr, 3, true);
return;
case false:
mem().setUint32(addr + 4, nanHead, true);
mem().setUint32(addr, 4, true);
return;
}
let id = this._ids.get(v);
if (id === undefined) {
id = this._idPool.pop();
if (id === undefined) {
id = this._values.length;
}
this._values[id] = v;
this._goRefCounts[id] = 0;
this._ids.set(v, id);
}
this._goRefCounts[id]++;
let typeFlag = 1;
switch (typeof v) {
case "string":
typeFlag = 2;
break;
case "symbol":
typeFlag = 3;
break;
case "function":
typeFlag = 4;
break;
}
mem().setUint32(addr + 4, nanHead | typeFlag, true);
mem().setUint32(addr, id, true);
};
const loadSlice = (array, len, cap?) => {
return new Uint8Array(this._inst.exports.memory.buffer, array, len);
};
const loadSliceOfValues = (array, len, cap) => {
const a = new Array(len);
for (let i = 0; i < len; i++) {
a[i] = loadValue(array + i * 8);
}
return a;
};
const loadString = (ptr, len) => {
return decoder.decode(
new DataView(this._inst.exports.memory.buffer, ptr, len),
);
};
const timeOrigin = Date.now() - performance.now();
this.importObject = {
wasi_snapshot_preview1: {
fd_write: (fd, iovs_ptr, iovs_len, nwritten_ptr) => {
let nwritten = 0;
if (fd == 1) {
for (let iovs_i = 0; iovs_i < iovs_len; iovs_i++) {
let iov_ptr = iovs_ptr + iovs_i * 8;
let ptr = mem().getUint32(iov_ptr + 0, true);
let len = mem().getUint32(iov_ptr + 4, true);
nwritten += len;
for (let i = 0; i < len; i++) {
let c = mem().getUint8(ptr + i);
if (c == 13) {
// CR
// ignore
} else if (c == 10) {
// LF
// write line
let line = decoder.decode(new Uint8Array(logLine));
logLine = [];
console.log(line);
} else {
logLine.push(c);
}
}
}
} else {
console.error("Invalid file descriptor:", fd);
}
mem().setUint32(nwritten_ptr, nwritten, true);
return 0;
},
fd_close: () => 0, // dummy
fd_fdstat_get: () => 0, // dummy
fd_seek: () => 0, // dummy
proc_exit: (code) => {
if (global.process) {
// Node.js
process.exit(code);
} else {
// Can't exit in a browser.
throw "Trying to exit with code " + code;
}
},
random_get: (bufPtr, bufLen) => {
crypto.getRandomValues(loadSlice(bufPtr, bufLen));
return 0;
},
},
env: {
"runtime.ticks": () => {
return timeOrigin + performance.now();
},
"runtime.sleepTicks": (timeout) => {
setTimeout(this._inst.exports.go_scheduler, timeout);
},
"syscall/js.finalizeRef": (sp) => {
console.error("syscall/js.finalizeRef not implemented");
},
"syscall/js.stringVal": (ret_ptr, value_ptr, value_len) => {
const s = loadString(value_ptr, value_len);
storeValue(ret_ptr, s);
},
"syscall/js.valueGet": (retval, v_addr, p_ptr, p_len) => {
let prop = loadString(p_ptr, p_len);
let value = loadValue(v_addr);
let result = Reflect.get(value, prop);
storeValue(retval, result);
},
"syscall/js.valueSet": (v_addr, p_ptr, p_len, x_addr) => {
const v = loadValue(v_addr);
const p = loadString(p_ptr, p_len);
const x = loadValue(x_addr);
Reflect.set(v, p, x);
},
"syscall/js.valueDelete": (v_addr, p_ptr, p_len) => {
const v = loadValue(v_addr);
const p = loadString(p_ptr, p_len);
Reflect.deleteProperty(v, p);
},
"syscall/js.valueIndex": (ret_addr, v_addr, i) => {
storeValue(ret_addr, Reflect.get(loadValue(v_addr), i));
},
"syscall/js.valueSetIndex": (v_addr, i, x_addr) => {
Reflect.set(loadValue(v_addr), i, loadValue(x_addr));
},
"syscall/js.valueCall": (
ret_addr,
v_addr,
m_ptr,
m_len,
args_ptr,
args_len,
args_cap,
) => {
const v = loadValue(v_addr);
const name = loadString(m_ptr, m_len);
const args = loadSliceOfValues(args_ptr, args_len, args_cap);
try {
const m = Reflect.get(v, name);
storeValue(ret_addr, Reflect.apply(m, v, args));
mem().setUint8(ret_addr + 8, 1);
} catch (err) {
storeValue(ret_addr, err);
mem().setUint8(ret_addr + 8, 0);
}
},
"syscall/js.valueInvoke": (
ret_addr,
v_addr,
args_ptr,
args_len,
args_cap,
) => {
try {
const v = loadValue(v_addr);
const args = loadSliceOfValues(args_ptr, args_len, args_cap);
storeValue(ret_addr, Reflect.apply(v, undefined, args));
mem().setUint8(ret_addr + 8, 1);
} catch (err) {
storeValue(ret_addr, err);
mem().setUint8(ret_addr + 8, 0);
}
},
"syscall/js.valueNew": (
ret_addr,
v_addr,
args_ptr,
args_len,
args_cap,
) => {
const v = loadValue(v_addr);
const args = loadSliceOfValues(args_ptr, args_len, args_cap);
try {
storeValue(ret_addr, Reflect.construct(v, args));
mem().setUint8(ret_addr + 8, 1);
} catch (err) {
storeValue(ret_addr, err);
mem().setUint8(ret_addr + 8, 0);
}
},
"syscall/js.valueLength": (v_addr) => {
return loadValue(v_addr).length;
},
"syscall/js.valuePrepareString": (ret_addr, v_addr) => {
const s = String(loadValue(v_addr));
const str = encoder.encode(s);
storeValue(ret_addr, str);
setInt64(ret_addr + 8, str.length);
},
"syscall/js.valueLoadString": (
v_addr,
slice_ptr,
slice_len,
slice_cap,
) => {
const str = loadValue(v_addr);
loadSlice(slice_ptr, slice_len, slice_cap).set(str);
},
"syscall/js.valueInstanceOf": (v_addr, t_addr) => {
return loadValue(v_addr) instanceof loadValue(t_addr);
},
"syscall/js.copyBytesToGo": (
ret_addr,
dest_addr,
dest_len,
dest_cap,
source_addr,
) => {
let num_bytes_copied_addr = ret_addr;
let returned_status_addr = ret_addr + 4;
const dst = loadSlice(dest_addr, dest_len, dest_cap);
const src = loadValue(source_addr);
if (
!(src instanceof Uint8Array || src instanceof Uint8ClampedArray)
) {
mem().setUint8(returned_status_addr, 0);
return;
}
const toCopy = src.subarray(0, dst.length);
dst.set(toCopy);
setInt64(num_bytes_copied_addr, toCopy.length);
mem().setUint8(returned_status_addr, 1);
},
"syscall/js.copyBytesToJS": (
ret_addr,
dest_addr,
source_addr,
source_len,
source_cap,
) => {
let num_bytes_copied_addr = ret_addr;
let returned_status_addr = ret_addr + 4;
const dst = loadValue(dest_addr);
const src = loadSlice(source_addr, source_len, source_cap);
if (
!(dst instanceof Uint8Array || dst instanceof Uint8ClampedArray)
) {
mem().setUint8(returned_status_addr, 0);
return;
}
const toCopy = src.subarray(0, dst.length);
dst.set(toCopy);
setInt64(num_bytes_copied_addr, toCopy.length);
mem().setUint8(returned_status_addr, 1);
},
},
};
}
async run(instance) {
this._inst = instance;
const mem = new DataView(this._inst.exports.memory.buffer);
while (true) {
const callbackPromise = new Promise((resolve) => {
this._resolveCallbackPromise = () => {
if (this.exited) {
throw new Error("Bad callback: Go program has already exited");
}
setTimeout(resolve, 0);
};
});
this._inst.exports._start();
if (this.exited) {
break;
}
await callbackPromise;
}
}
_resume() {
if (this.exited) {
throw new Error("Go program has already exited");
}
this._inst.exports.resume();
}
}
if (
typeof global !== "undefined" ||
typeof window !== "undefined" ||
typeof self !== "undefined"
) {
if (typeof global !== "undefined") {
// global already exists
} else if (typeof window !== "undefined") {
window.global = window;
} else if (typeof self !== "undefined") {
self.global = self;
} else {
throw new Error(
"Cannot export Go (neither global, window nor self is defined)",
);
}
if (!global.require && typeof require !== "undefined") {
global.require = require;
}
// @ts-ignore
if (!global.fs && global.require) {
global.fs = require("fs");
}
const enosys = () => {
const err = new Error("not implemented");
// @ts-ignore
err.code = "ENOSYS";
return err;
};
if (!global.fs) {
let outputBuf = "";
global.fs = {
constants: {
O_WRONLY: -1,
O_RDWR: -1,
O_CREAT: -1,
O_TRUNC: -1,
O_APPEND: -1,
O_EXCL: -1,
},
writeSync: (fd, buf) => {
outputBuf += decoder.decode(buf);
const nl = outputBuf.lastIndexOf("\n");
if (nl != -1) {
console.log(outputBuf.substr(0, nl));
outputBuf = outputBuf.substr(nl + 1);
}
return buf.length;
},
write: (fd, buf, offset, length, position, callback) => {
if (offset !== 0 || length !== buf.length || position !== null) {
callback(enosys());
return;
}
const n = global.fs.writeSync(fd, buf);
callback(null, n);
},
open: (path, flags, mode, callback) => {
callback(enosys());
},
fsync: (fd, callback) => {
callback(null);
},
fdatasync: (fd, callback) => {
callback(null);
},
close: (fd, callback) => {
callback(null);
},
createReadStream: enosys,
createWriteStream: enosys,
ftruncate: (fd, length, callback) => {
callback(enosys());
},
readFile: (path, callback) => {
callback(enosys());
},
writeFile: (path, data, callback) => {
callback(enosys());
},
truncate: (path, length, callback) => {
callback(enosys());
},
readdir: (path, callback) => {
callback(enosys());
},
unlink: (path, callback) => {
callback(enosys());
},
rmdir: (path, callback) => {
callback(enosys());
},
mkdir: (path, perm, callback) => {
callback(enosys());
},
stat: (path, callback) => {
callback(enosys());
},
lstat: (path, callback) => {
callback(enosys());
},
fstat: (fd, callback) => {
callback(enosys());
},
rename: (oldPath, newPath, callback) => {
callback(enosys());
},
symlink: (target, path, callback) => {
callback(enosys());
},
link: (existingPath, newPath, callback) => {
callback(enosys());
},
readlink: (path, callback) => {
callback(enosys());
},
chmod: (path, mode, callback) => {
callback(enosys());
},
lchmod: (path, mode, callback) => {
callback(enosys());
},
fchmod: (fd, mode, callback) => {
callback(enosys());
},
chown: (path, uid, gid, callback) => {
callback(enosys());
},
lchown: (path, uid, gid, callback) => {
callback(enosys());
},
fchown: (fd, uid, gid, callback) => {
callback(enosys());
},
utimes: (path, atime, mtime, callback) => {
callback(enosys());
},
futimes: (fd, atime, mtime, callback) => {
callback(enosys());
},
realpath: (path, callback) => {
callback(enosys());
},
fallocate: (fd, mode, offset, length, callback) => {
callback(enosys());
},
copyFile: (src, dest, flags, callback) => {
callback(enosys());
},
};
}
if (!global.crypto) {
global.crypto = {
// @ts-ignore
getRandomValues: (arr: number[]) => {
for (let i = 0; i < arr.length; i++) {
arr[i] = Math.floor(Math.random() * 256);
}
},
};
}
if (!global.performance) {
// @ts-ignore
global.performance = {
now: () => Date.now(),
};
}
if (!global.TextEncoder) {
global.TextEncoder = TextEncoder;
}
if (!global.TextDecoder) {
// @ts-ignore
global.TextDecoder = TextDecoder;
}
if (!global.Buffer) {
global.Buffer = {
// @ts-ignore
isBuffer: (x) => false,
};
}
if (!global.process) {
// @ts-ignore
global.process = {
getuid: () => -1,
getgid: () => -1,
geteuid: () => -1,
getegid: () => -1,
getgroups: () => [],
pid: -1,
ppid: -1,
umask: () => 0,
cwd: () => "/",
chdir: (dir) => {},
};
}
if (!global.Error) {
// @ts-ignore
global.Error = class extends Error {};
}
}

8
src/golang/go.mod Normal file
View File

@ -0,0 +1,8 @@
module main
go 1.19
require (
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
lukechampine.com/blake3 v1.2.1 // indirect
)

4
src/golang/go.sum Normal file
View File

@ -0,0 +1,4 @@
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
lukechampine.com/blake3 v1.2.1 h1:YuqqRuaqsGV71BV/nm9xlI0MKUv4QC54jQnBChWbGnI=
lukechampine.com/blake3 v1.2.1/go.mod h1:0OFRp7fBtAylGVCO40o87sbupkyIGgbpv1+M1k1LM6k=

125
src/golang/main.go Normal file
View File

@ -0,0 +1,125 @@
package main
import (
"bytes"
"io"
"lukechampine.com/blake3"
"math/rand"
"strconv"
"syscall/js"
)
var activeReader *reader
var pipe chan []byte
var callbackId int
var nextBytes chan int
var exitChan chan bool
func main() {
pipe = make(chan []byte)
nextBytes = make(chan int)
exitChan = make(chan bool)
callbackId = rand.Int()
<-exitChan
}
type reader struct {
}
func (r *reader) Read(p []byte) (n int, err error) {
nextBytes <- len(p)
data := <-pipe
copy(p[:], data[:])
resetWritePromise()
return len(data), nil
}
//export start
func start() int {
rootChan := make(chan []byte)
proofChan := make(chan []byte)
resetWritePromise()
setGlobalObject(getGlobalPrefix()+"_set_root", js.FuncOf(func(this js.Value, args []js.Value) any {
jsroot := args[0]
root := make([]byte, jsroot.Get("length").Int())
js.CopyBytesToGo(root, jsroot)
rootChan <- root
return nil
}))
setGlobalObject(getGlobalPrefix()+"_set_proof", js.FuncOf(func(this js.Value, args []js.Value) any {
jsproof := args[0]
proofSlice := make([]byte, jsproof.Get("length").Int())
js.CopyBytesToGo(proofSlice, jsproof)
proofChan <- proofSlice
return nil
}))
setGlobalObject(getGlobalPrefix()+"_write", js.FuncOf(func(this js.Value, args []js.Value) any {
d := args[0]
data := make([]byte, d.Get("length").Int())
js.CopyBytesToGo(data, d)
pipe <- data
return nil
}))
go func() {
rootSlice := <-rootChan
proof := <-proofChan
var root [32]byte
copy(root[:], rootSlice)
ret, err := blake3.BaoDecode(io.Discard, activeReader, bytes.NewReader(proof), root)
setGlobalObject(getGlobalPrefix()+"_result", ret)
setGlobalObject(getGlobalPrefix()+"_error", err)
}()
return callbackId
}
// exit write
func exit() {
exitChan <- true
}
func createWritePromiseHandler() js.Value {
return createPromiseHandler(func(this js.Value, args []js.Value) {
bytesToRead := <-nextBytes
args[0].Invoke(bytesToRead)
})
}
func createPromiseHandler(cb func(this js.Value, args []js.Value)) js.Value {
return js.Global().Get("Promise").New(js.FuncOf(func(this js.Value, args []js.Value) any {
cb(this, args)
return nil
}))
}
func getGlobalPrefix() string {
return "bao_" + strconv.FormatInt(int64(callbackId), 10)
}
func getWritePromiseName() string {
return getGlobalPrefix() + "_write_promise"
}
func resetWritePromise() {
setGlobalObject(getWritePromiseName(), createWritePromiseHandler())
}
func setGlobalObject(name string, p any) {
js.Global().Set(name, p)
}

View File

@ -1,2 +1,3 @@
export * from "./client.js";
export * from "./cid.js";
export * from "./verify.js";

160
src/verify.ts Normal file
View File

@ -0,0 +1,160 @@
// @ts-ignore
import baoWasm from "#wasm/bao.wasm";
import Go from "#go_wasm.js";
export async function getVerifiableStream(
root: Uint8Array,
proof: Uint8Array,
data: ReadableStream,
) {
const wasm = await getWasmInstance();
// @ts-ignore
const reader = new VariableChunkStream(data);
let bytesToRead;
const getNextBytes = async () => {
bytesToRead = getWasmProperty(wasmId, "write_promise");
bytesToRead = await bytesToRead;
};
const callExports = (name: string) => {
// @ts-ignore
return wasm.exports[name]();
};
// @ts-ignore
const exit = () => {
callExports("exit");
cleanup();
};
const done = (controller: ReadableStreamDefaultController) => {
controller.close();
exit();
};
const cleanup = () => {
const win = getWin();
const props = Object.getOwnPropertyNames(win);
props
.filter((item) => item.startsWith(`bao_${wasmId}`))
.forEach((item) => {
delete win[item];
});
};
await getNextBytes();
// @ts-ignore
const wasmId = callExports("start");
getWasmProperty(wasmId, "set_root")(root);
getWasmProperty(wasmId, "set_proof")(proof);
return new ReadableStream({
async pull(controller) {
let chunk;
try {
chunk = await reader.read(bytesToRead);
if (chunk.value) {
getWasmProperty(wasmId, "write")(chunk.value);
}
} catch (e) {
// @ts-ignore
exit();
controller.error(e);
}
const result = getWasmProperty(wasmId, "result");
const wasmDone = result === undefined;
if (result === undefined) {
await getNextBytes();
}
if (chunk.done || wasmDone) {
if (wasmDone) {
if (result) {
done(controller);
} else {
controller.error(getWasmProperty(wasmId, "error"));
}
} else {
done(controller);
}
}
},
async cancel(reason: any) {
await reader.cancel(reason);
exit();
},
});
}
function getWin() {
return globalThis || self || window;
}
function getWasmProperty(id: number, prop: string) {
return getWin()[`bao_${id}_${prop}`];
}
async function getWasmInstance() {
const go = new Go();
let wasm = (await baoWasm(
go.importObject,
)) as WebAssembly.WebAssemblyInstantiatedSource;
go.run(wasm);
return wasm;
}
class VariableChunkStream {
private reader: ReadableStreamDefaultReader;
private currentChunk: Uint8Array = new Uint8Array();
private currentChunkSize = 0;
private readerDone = false;
constructor(stream: ReadableStream) {
this.reader = stream.getReader();
}
async read(bytes: number) {
if (this.currentChunk.length === 0 && !this.readerDone) {
const { done, value } = await this.reader.read();
if (done) {
return { done: true };
}
this.currentChunk = value;
this.currentChunkSize = this.currentChunk.length;
}
if (this.currentChunkSize > bytes) {
const chunk = this.currentChunk.slice(0, bytes);
this.currentChunk = this.currentChunk.slice(bytes);
this.currentChunkSize -= bytes;
return { value: chunk, done: false };
}
if (this.currentChunkSize < bytes && !this.readerDone) {
const { done, value } = await this.reader.read();
if (done) {
this.readerDone = true;
}
this.currentChunk = new Uint8Array([...this.currentChunk, ...value]);
this.currentChunkSize += value.length;
return this.read(bytes);
}
const chunk = this.currentChunk;
this.currentChunk = new Uint8Array();
this.currentChunkSize = 0;
return { value: chunk, done: this.readerDone };
}
async cancel(reason: any) {
await this.reader.cancel(reason);
this.currentChunk = new Uint8Array();
this.currentChunkSize = 0;
}
}