Skip to content

Commit 144bbf2

Browse files
committed
feat: Add Base91 wasm
Let browser use emscripten generated loader. Refactor CLI source code. Signed-off-by: Gordon Smith <[email protected]>
1 parent 0667815 commit 144bbf2

30 files changed

+875
-236
lines changed

.gitignore

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
/.nyc_output
2-
.vscode/ipch
2+
/.vscode/c_cpp_properties.json
3+
/.vscode/ipch
4+
/.vscode/settings.json
5+
/bin
36
/build
47
/coverage
58
/dist
69
/dist-test
710
/emsdk*
8-
/src-expat
9-
/src-graphviz
1011
/lib-*
1112
/node_modules
13+
/rust
14+
/src-*
1215
/tmp
1316
/types
1417
/vcpkg
1518
*.tsbuildinfo
16-
/.vscode/c_cpp_properties.json
17-
/.vscode/settings.json

.vscode/launch.json

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@
2626
"url": "http://localhost:8000/hw-zstd.html",
2727
"webRoot": "${workspaceRoot}"
2828
},
29+
{
30+
"name": "hw-base91.html",
31+
"type": "msedge",
32+
"request": "launch",
33+
"url": "http://localhost:8000/hw-base91.html",
34+
"webRoot": "${workspaceRoot}"
35+
},
2936
{
3037
"name": "test-browser",
3138
"type": "msedge",
@@ -50,9 +57,31 @@
5057
],
5158
"type": "node"
5259
},
60+
{
61+
"name": "sfx-wasm",
62+
"program": "${workspaceFolder}/bin/sfx-wasm.mjs",
63+
"request": "launch",
64+
"args": [
65+
"./build/cpp/base91/base91lib.wasm"
66+
],
67+
"skipFiles": [
68+
"<node_internals>/**"
69+
],
70+
"type": "node"
71+
},
72+
{
73+
"name": "cli rollup wasm",
74+
"program": "${workspaceFolder}/output-node/test-wasm.js",
75+
"request": "launch",
76+
"args": [],
77+
"skipFiles": [
78+
"<node_internals>/**"
79+
],
80+
"type": "node"
81+
},
5382
{
5483
"name": "CLI",
55-
"program": "${workspaceFolder}/bin/cli.js",
84+
"program": "${workspaceFolder}/bin/dot-wasm.mjs",
5685
"request": "launch",
5786
"args": [
5887
"-K neato",
@@ -66,7 +95,7 @@
6695
},
6796
{
6897
"name": "CLI Version",
69-
"program": "${workspaceFolder}/bin/cli.js",
98+
"program": "${workspaceFolder}/bin/dot-wasm.mjs",
7099
"request": "launch",
71100
"args": [
72101
"-v"

CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ SET(EM_FLAGS
1111
"-s INVOKE_RUN=0"
1212
"-s ALLOW_MEMORY_GROWTH=1"
1313
"-s WASM=1"
14-
"-s MODULARIZE"
1514
"-s ENVIRONMENT=web"
1615
"-s FILESYSTEM=0"
1716
# "-s MINIMAL_RUNTIME=1"
@@ -21,7 +20,7 @@ SET(EM_FLAGS
2120
"-s USE_ES6_IMPORT_META=0"
2221
# "-s WASM_BIGINT=1"
2322
"--pre-js ${CMAKE_CURRENT_SOURCE_DIR}/cpp/src/pre.js"
24-
# "--post-js ${CMAKE_CURRENT_SOURCE_DIR}/cpp/src/post.js"
23+
"--post-js ${CMAKE_CURRENT_SOURCE_DIR}/cpp/src/post.js"
2524
)
2625

2726
IF (CMAKE_BUILD_TYPE STREQUAL "Debug")

README.md

Lines changed: 138 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ Built with:
2929
- [Zstandard (zstd)](#zstandard)
3030
- [Hello World](#zstandard-hello-world)
3131
- [API](#zstandard-api)
32+
- [Base91](#base91)
33+
- [Hello World](#base91-hello-world)
34+
- [API](#base91-api)
3235
- [Utilities](#utility)
3336
- [Building @hpcc-js/wasm](#building-hpcc-js-wasm)
3437

@@ -108,7 +111,7 @@ GraphViz WASM library, see [graphviz.org](https://www.graphviz.org/) for c++ det
108111

109112
To call `dot-wasm` without installing:
110113
```
111-
npx @hpcc-js/wasm [options] fileOrDot
114+
npx -p @hpcc-js/wasm dot-wasm [options] fileOrDot
112115
```
113116

114117
To install the global command `dot-wasm` via NPM:
@@ -121,38 +124,35 @@ Usage:
121124
Usage: dot-wasm [options] fileOrDot
122125
123126
Options:
124-
--version Show version number [boolean]
125-
-K, --layout Set layout engine (circo | dot | fdp | sfdp | neato | osage
126-
| patchwork | twopi). By default, dot is used.
127-
-T, --format Set output language to one of the supported formats (svg,
128-
dot, json, dot_json, xdot_json, plain, plain-ext). By
129-
default, svg is produced.
127+
--version Show version number [boolean]
128+
-K, --layout Set layout engine (circo | dot | fdp | sfdp | neato | osage | patchwo
129+
rk | twopi). By default, dot is used.
130+
-T, --format Set output language to one of the supported formats (svg | dot | json
131+
| dot_json | xdot_json | plain | plain-ext). By default, svg is prod
132+
uced.
130133
-n, --neato-no-op Sets no-op flag in neato.
131-
"-n 1" assumes neato nodes have already been positioned and
132-
all nodes have a pos attribute giving the positions. It
133-
then performs an optional adjustment to remove node-node
134-
overlap, depending on the value of the overlap attribute,
135-
computes the edge layouts, depending on the value of the
136-
splines attribute, and emits the graph in the appropriate
137-
format.
138-
"-n 2" Use node positions as specified, with no adjustment
139-
to remove node-node overlaps, and use any edge layouts
140-
already specified by the pos attribute. neato computes an
141-
edge layout for any edge that does not have a pos
142-
attribute. As usual, edge layout is guided by the splines
143-
attribute.
144-
-y, --invert-y By default, the coordinate system used in generic output
145-
formats, such as attributed dot, extended dot, plain and
146-
plain-ext, is the standard cartesian system with the origin
147-
in the lower left corner, and with increasing y coordinates
148-
as points move from bottom to top. If the -y flag is used,
149-
the coordinate system is inverted, so that increasing
150-
values of y correspond to movement from top to bottom.
151-
-h, --help Show help [boolean]
134+
"-n 1" assumes neato nodes have already bee
135+
n positioned and all nodes have a pos attribute giving the positions.
136+
It then performs an optional adjustment to remove node-node overlap,
137+
depending on the value of the overlap attribute, computes the edge l
138+
ayouts, depending on the value of the splines attribute, and emits th
139+
e graph in the appropriate format.
140+
"-n 2" Use node positions as speci
141+
fied, with no adjustment to remove node-node overlaps, and use any ed
142+
ge layouts already specified by the pos attribute. neato computes an
143+
edge layout for any edge that does not have a pos attribute. As usual
144+
, edge layout is guided by the splines attribute.
145+
-y, --invert-y By default, the coordinate system used in generic output formats, suc
146+
h as attributed dot, extended dot, plain and plain-ext, is the standa
147+
rd cartesian system with the origin in the lower left corner, and wit
148+
h increasing y coordinates as points move from bottom to top. If the
149+
-y flag is used, the coordinate system is inverted, so that increasin
150+
g values of y correspond to movement from top to bottom.
151+
-v Echo GraphViz library version
152+
-h, --help Show help [boolean]
152153
153154
Examples:
154-
dot-wasm -K neato -T xdot ./input.dot Execute NEATO layout and outputs XDOT
155-
format.
155+
dot-wasm -K neato -T xdot ./input.dot Execute NEATO layout and outputs XDOT format.
156156
```
157157

158158
### GraphViz Hello World
@@ -515,6 +515,114 @@ A note on compressionLevel: The library supports regular compression levels fro
515515

516516
---
517517

518+
## Base91
519+
_Similar to Base 64, but uses more characters resulting in smaller strings._
520+
Base 91 WASM library, similar to Base 64 but uses more characters resulting in smaller strings, see [Base91](https://base91.sourceforge.net/) for more details.
521+
### Base91 Hello World
522+
```html
523+
<!DOCTYPE html>
524+
<html>
525+
<head>
526+
<meta charset="UTF-8">
527+
<title>Base91 WASM</title>
528+
</head>
529+
<body>
530+
<div id="placeholder"></div>
531+
<script type="module">
532+
import { Base91 } from "./dist/index.es6.js";
533+
//import { Base91 } from "https://cdn.jsdelivr.net/npm/@hpcc-js/wasm/dist/index.es6.js";
534+
const base91 = await Base91.load();
535+
const data = new Uint8Array(Array.from({ length: 100 }, (_, i) => Math.random() * 100));
536+
const encoded_data = await base91.encode(data);
537+
const decoded_data = await base91.decode(encoded_data);
538+
document.getElementById("placeholder").innerHTML = `\
539+
<ul>
540+
<li>Data Size (bytes): ${data.byteLength}</li>
541+
<li>Endoded Size (bytes): ${encoded_data.length}</li>
542+
<li>Decoded Size (bytes): ${decoded_data.byteLength}</li>
543+
</ul>
544+
<h4>Data: </h4>
545+
<code>
546+
${data}
547+
</code>
548+
<h4>Base 91: </h4>
549+
<code id="base91">
550+
</code>
551+
<h4>Decoded: </h4>
552+
<code>
553+
${decoded_data}
554+
</code>
555+
`;
556+
document.getElementById("base91").innerText = encoded_data;
557+
</script>
558+
</body>
559+
</html>
560+
```
561+
562+
### Base91 API
563+
564+
#### Interfaces
565+
566+
<a name="Options" href="#Base91Options">#</a> **Options** · [<>](https://github.com/hpcc-systems/hpcc-js-wasm/blob/trunk/src/base91.ts "Source")
567+
568+
Options structure for advanced loading.
569+
570+
```typescript
571+
interface Options {
572+
wasmFolder?: string;
573+
wasmBinary?: ArrayBuffer;
574+
}
575+
```
576+
577+
* _wasmFolder_: An optional `string` specifying the location of wasm file.
578+
* _wasmBinary_: An optional "pre-fetched" copy of the wasm binary as returned from `XHR` or `fetch`.
579+
580+
<a name="Base91" href="#Base91">#</a> **Base91** · [<>](https://github.com/hpcc-systems/hpcc-js-wasm/blob/trunk/src/base91.ts "Source")
581+
582+
Conceptual interface for TypeScript/JavaScript wrapper API
583+
584+
```typescript
585+
interface Base91 {
586+
static load(options?: Options): Promise<Base91>;
587+
version(): string;
588+
589+
encode(data: Uint8Array): string;
590+
decode(array: string): Uint8Array;
591+
}
592+
```
593+
594+
<a name="base91Load" href="#base91Load">#</a> **Base91.load**(_options_?: **Options**): **Promise\<Base91\>** · [<>](https://github.com/hpcc-systems/hpcc-js-wasm/blob/trunk/src/base91.ts "Source")
595+
596+
Loads and initializes the Base91 wasm library, returns a Promise to `Base91`:
597+
```typescript
598+
const base91 = await Base91.load();
599+
...dostuff...
600+
```
601+
or
602+
```typescript
603+
Base91.load().then(base91 => {...dostuff...});
604+
```
605+
606+
<a name="base91Version" href="#base91Version">#</a> **base91.version**(): **string** · [<>](https://github.com/hpcc-systems/hpcc-js-wasm/blob/trunk/src/base91.ts "Source")
607+
608+
* **_returns_**: The Base91 library Version.
609+
610+
<a name="base91Encode" href="#base91Encode">#</a> **base91.encode**(_data_: **Uint8Array**): **string** · [<>](https://github.com/hpcc-systems/hpcc-js-wasm/blob/trunk/src/base91.ts "Source")
611+
612+
* **_data_**: Raw data to encode.
613+
* **_returns_**: Encoded string.
614+
615+
Encodes the raw data.
616+
617+
<a name="base91Decode" href="#base91Decode">#</a> **base91.decode**(_str_: **string**): **Uint8Array** · [<>](https://github.com/hpcc-systems/hpcc-js-wasm/blob/trunk/src/base91.ts "Source")
618+
619+
* **_str_**: String to decode.
620+
* **_returns_**: Decoded data.
621+
622+
Decodes the raw data.
623+
624+
---
625+
518626
## Utility
519627

520628
Utility functions unrelated to any specific wasm APIs

cpp/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
ADD_SUBDIRECTORY(base91)
12
ADD_SUBDIRECTORY(expat)
23
ADD_SUBDIRECTORY(graphviz)
34
ADD_SUBDIRECTORY(zstd)

cpp/base91/CMakeLists.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
PROJECT(base91lib)
2+
3+
find_package(unofficial-base91 CONFIG REQUIRED)
4+
5+
# See: https://github.com/emscripten-core/emscripten/blob/main/src/settings.js
6+
SET(EM_FLAGS
7+
${EM_FLAGS}
8+
"-s EXPORT_NAME='${CMAKE_PROJECT_NAME}'"
9+
"-s EXPORTED_FUNCTIONS=\"[]\""
10+
"-s EXPORTED_RUNTIME_METHODS=\"[]\""
11+
"--post-js ${CMAKE_CURRENT_BINARY_DIR}/main_glue.js"
12+
)
13+
STRING(REPLACE ";" " " LINK_FLAGS "${EM_FLAGS}")
14+
15+
# Generate Glue from IDL file ---
16+
ADD_CUSTOM_COMMAND(
17+
MAIN_DEPENDENCY ${CMAKE_CURRENT_SOURCE_DIR}/main.idl
18+
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/main_glue.js ${CMAKE_CURRENT_BINARY_DIR}/main_glue.cpp
19+
COMMAND python3 ${CMAKE_BINARY_DIR}/../emsdk/upstream/emscripten/tools/webidl_binder.py ${CMAKE_CURRENT_SOURCE_DIR}/main.idl ${CMAKE_CURRENT_BINARY_DIR}/main_glue
20+
)
21+
SET_PROPERTY(SOURCE main.cpp APPEND PROPERTY OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/main_glue.cpp)
22+
# --- --- ---
23+
24+
INCLUDE_DIRECTORIES(
25+
${VCPKG_INCLUDE_DIR}
26+
${CMAKE_CURRENT_BINARY_DIR}
27+
)
28+
29+
ADD_EXECUTABLE(base91lib
30+
main.cpp
31+
)
32+
33+
SET_TARGET_PROPERTIES(base91lib PROPERTIES LINK_FLAGS "${LINK_FLAGS} -s ENVIRONMENT=webview")
34+
35+
TARGET_LINK_LIBRARIES(base91lib
36+
PRIVATE unofficial::base91::base91
37+
)
38+
39+
INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/base91lib.wasm DESTINATION dist COMPONENT runtime)
40+
41+
IF (CMAKE_BUILD_TYPE STREQUAL "Debug")
42+
INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/base91lib.wasm.map DESTINATION dist COMPONENT runtime)
43+
ENDIF ()

0 commit comments

Comments
 (0)