1+ #! /usr/bin/env bash
2+
3+ set -Eeuo pipefail
4+
5+ source ./scripts/lib/ux.sh
6+
7+ JSOO_PATH=./src/bindings/ocaml/jsoo_exports/
8+ BUILT_PATH=./_build/default/$JSOO_PATH
9+ BINDINGS_PATH=./src/bindings/compiled/node_bindings/
10+
11+ setup_script " jsoo-build-node" " JSOO build node"
12+
13+ mkdir -p $BINDINGS_PATH
14+
15+ info " building JSOO artifacts for node..."
16+ TARGETS=(\
17+ o1js_node.bc.js \
18+ )
19+ dune build ${TARGETS[@]/#/ $JSOO_PATH / }
20+ ok " JSOO artifacts built successfully"
21+
22+ info " copying artifacts into the right place..."
23+ for target in ${TARGETS[@]} ; do
24+ cp $BUILT_PATH /$target $BINDINGS_PATH /$target
25+ chmod 660 $BINDINGS_PATH /$target
26+ done
27+
28+ info " setting up TS declaration files..."
29+ echo ' // this file intentionally left blank' > $BINDINGS_PATH /o1js_node.bc.d.cts
30+ ok " TS declaration created"
31+
32+ info " moving some files to CommonJS format..."
33+ run_cmd mv -f $BINDINGS_PATH /o1js_node.bc.js $BINDINGS_PATH /o1js_node.bc.cjs
34+ ok " Node.js bindings copied"
35+
36+ info " Updating WASM references in bindings..."
37+ run_cmd sed -i ' s/plonk_wasm.js/plonk_wasm.cjs/' $BINDINGS_PATH /o1js_node.bc.cjs
38+ ok " WASM references updated"
39+
40+ info " fixing JS bindings for better error handling..."
41+ run_cmd sed -i ' s/function failwith(s){throw \[0,Failure,s\]/function failwith(s){throw globalThis.Error(s.c)/' " ${BINDINGS_PATH} " /o1js_node.bc.cjs
42+ run_cmd sed -i ' s/function invalid_arg(s){throw \[0,Invalid_argument,s\]/function invalid_arg(s){throw globalThis.Error(s.c)/' " ${BINDINGS_PATH} " /o1js_node.bc.cjs
43+ run_cmd sed -i ' s/return \[0,Exn,t\]/return globalThis.Error(t.c)/' " ${BINDINGS_PATH} " /o1js_node.bc.cjs
44+ run_cmd sed -i ' s/function raise(t){throw caml_call1(to_exn$0,t)}/function raise(t){throw Error(t?.[1]?.c ?? "Unknown error thrown by raise")}/' " ${BINDINGS_PATH} " /o1js_node.bc.cjs
45+ ok " JS bindings fixed"
46+
47+ success " JSOO node build complete!"
0 commit comments