mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-28 17:23:09 -06:00
Add initial Wasm test harness
This commit is contained in:
parent
00632edc82
commit
0f6200f81d
40
.github/workflows/wasm.yml
vendored
Normal file
40
.github/workflows/wasm.yml
vendored
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
name: WebAssembly
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [master]
|
||||
pull_request:
|
||||
branches: [master]
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
- name: Install Go
|
||||
uses: actions/setup-go@v2
|
||||
with:
|
||||
go-version: 1.16.5
|
||||
|
||||
- uses: actions/cache@v2
|
||||
with:
|
||||
path: |
|
||||
~/.cache/go-build
|
||||
~/go/pkg/mod
|
||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-go-
|
||||
|
||||
- name: Install Node
|
||||
uses: actions/setup-node@v2
|
||||
with:
|
||||
node-version: 14
|
||||
cache: npm
|
||||
|
||||
- name: Install test dependencies
|
||||
working-directory: ./test/wasm
|
||||
run: npm install
|
||||
|
||||
- name: Test
|
||||
run: ./test-dendritejs.sh
|
||||
7
.gitignore
vendored
7
.gitignore
vendored
|
|
@ -3,6 +3,9 @@
|
|||
# Hidden files
|
||||
.*
|
||||
|
||||
# Allow GitHub config
|
||||
!.github
|
||||
|
||||
# Downloads
|
||||
/.downloads
|
||||
|
||||
|
|
@ -36,6 +39,7 @@ _testmain.go
|
|||
*.exe
|
||||
*.test
|
||||
*.prof
|
||||
*.wasm
|
||||
|
||||
# Generated keys
|
||||
*.pem
|
||||
|
|
@ -53,3 +57,6 @@ dendrite.yaml
|
|||
|
||||
# Generated code
|
||||
cmd/dendrite-demo-yggdrasil/embed/fs*.go
|
||||
|
||||
# Test dependencies
|
||||
test/wasm/node_modules
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
#!/bin/sh -eu
|
||||
|
||||
export GIT_COMMIT=$(git rev-list -1 HEAD) && \
|
||||
GOOS=js GOARCH=wasm go build -ldflags "-X main.GitCommit=$GIT_COMMIT" -o main.wasm ./cmd/dendritejs
|
||||
GOOS=js GOARCH=wasm go build -ldflags "-X main.GitCommit=$GIT_COMMIT" -o ./bin/main.wasm ./cmd/dendritejs-pinecone
|
||||
|
|
|
|||
2
build.sh
2
build.sh
|
|
@ -21,4 +21,4 @@ mkdir -p bin
|
|||
|
||||
CGO_ENABLED=1 go build -trimpath -ldflags "$FLAGS" -v -o "bin/" ./cmd/...
|
||||
|
||||
CGO_ENABLED=0 GOOS=js GOARCH=wasm go build -trimpath -ldflags "$FLAGS" -o bin/main.wasm ./cmd/dendritejs
|
||||
CGO_ENABLED=0 GOOS=js GOARCH=wasm go build -trimpath -ldflags "$FLAGS" -o bin/main.wasm ./cmd/dendritejs-pinecone
|
||||
|
|
|
|||
|
|
@ -144,6 +144,13 @@ func generateKey() ed25519.PrivateKey {
|
|||
}
|
||||
|
||||
func main() {
|
||||
startup()
|
||||
|
||||
// We want to block forever to let the fetch and libp2p handler serve the APIs
|
||||
select {}
|
||||
}
|
||||
|
||||
func startup() {
|
||||
sk := generateKey()
|
||||
pk := sk.Public().(ed25519.PublicKey)
|
||||
|
||||
|
|
@ -250,7 +257,4 @@ func main() {
|
|||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// We want to block forever to let the fetch and libp2p handler serve the APIs
|
||||
select {}
|
||||
}
|
||||
|
|
|
|||
25
cmd/dendritejs-pinecone/main_test.go
Normal file
25
cmd/dendritejs-pinecone/main_test.go
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
// Copyright 2021 The Matrix.org Foundation C.I.C.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// +build wasm
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestStartup(t *testing.T) {
|
||||
startup()
|
||||
}
|
||||
15
docs/p2p.md
15
docs/p2p.md
|
|
@ -2,14 +2,23 @@
|
|||
|
||||
These are the instructions for setting up P2P Dendrite, current as of May 2020. There's both Go stuff and JS stuff to do to set this up.
|
||||
|
||||
|
||||
### Dendrite
|
||||
|
||||
#### Build
|
||||
|
||||
- The `master` branch has a WASM-only binary for dendrite: `./cmd/dendritejs`.
|
||||
- Build it and copy assets to riot-web.
|
||||
```
|
||||
$ GOOS=js GOARCH=wasm go build -o main.wasm ./cmd/dendritejs
|
||||
$ cp main.wasm ../riot-web/src/vector/dendrite.wasm
|
||||
$ ./build-dendritejs.sh
|
||||
$ cp bin/main.wasm ../riot-web/src/vector/dendrite.wasm
|
||||
```
|
||||
|
||||
#### Test
|
||||
|
||||
To check that the Dendrite side is working well as Wasm, you can run the
|
||||
Wasm-specific tests:
|
||||
```
|
||||
$ ./test-dendritejs.sh
|
||||
```
|
||||
|
||||
### Rendezvous
|
||||
|
|
|
|||
3
test-dendritejs.sh
Executable file
3
test-dendritejs.sh
Executable file
|
|
@ -0,0 +1,3 @@
|
|||
#!/bin/sh -eu
|
||||
|
||||
GOOS=js GOARCH=wasm go test -v -exec "$(pwd)/test/wasm/index.js" ./cmd/dendritejs-pinecone
|
||||
48
test/wasm/index.js
Executable file
48
test/wasm/index.js
Executable file
|
|
@ -0,0 +1,48 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
/*
|
||||
Copyright 2021 The Matrix.org Foundation C.I.C.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const childProcess = require('child_process');
|
||||
|
||||
(async function() {
|
||||
// sql.js
|
||||
const initSqlJs = require('sql.js');
|
||||
await initSqlJs().then(SQL => {
|
||||
global._go_sqlite = SQL;
|
||||
console.log("Loaded sqlite")
|
||||
});
|
||||
// dendritejs expects to write to `/idb` so we create that here
|
||||
// Since this is testing only, we use the default in-memory FS
|
||||
global._go_sqlite.FS.mkdir("/idb");
|
||||
|
||||
// Load the generic Go Wasm exec helper inline to trigger built-in run call
|
||||
// This approach avoids copying `wasm_exec.js` into the repo, which is nice
|
||||
// to aim for since it can differ between Go versions.
|
||||
const goRoot = await new Promise((resolve, reject) => {
|
||||
childProcess.execFile('go', ['env', 'GOROOT'], (err, out) => {
|
||||
if (err) {
|
||||
reject("Can't find go");
|
||||
}
|
||||
resolve(out.trim());
|
||||
});
|
||||
});
|
||||
const execPath = path.join(goRoot, 'misc/wasm/wasm_exec.js');
|
||||
const execCode = fs.readFileSync(execPath, 'utf8');
|
||||
eval(execCode);
|
||||
})();
|
||||
25
test/wasm/package-lock.json
generated
Normal file
25
test/wasm/package-lock.json
generated
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
{
|
||||
"name": "wasm",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"dependencies": {
|
||||
"sql.js": "github:neilalexander/sql.js#252a72bf57b0538cbd49bbd6f70af71e516966ae"
|
||||
}
|
||||
},
|
||||
"node_modules/sql.js": {
|
||||
"version": "1.5.0",
|
||||
"resolved": "git+ssh://git@github.com/neilalexander/sql.js.git#252a72bf57b0538cbd49bbd6f70af71e516966ae",
|
||||
"integrity": "sha512-EFYI/yMoQ1U08nZxQOZ7+4S0nOpKF45EVoWGef8L1kvSCMP3B3xSzwZeOmoF2tBVpbMssAgHEz43cf0ZulRDSQ==",
|
||||
"license": "MIT"
|
||||
}
|
||||
},
|
||||
"dependencies": {
|
||||
"sql.js": {
|
||||
"version": "git+ssh://git@github.com/neilalexander/sql.js.git#252a72bf57b0538cbd49bbd6f70af71e516966ae",
|
||||
"integrity": "sha512-EFYI/yMoQ1U08nZxQOZ7+4S0nOpKF45EVoWGef8L1kvSCMP3B3xSzwZeOmoF2tBVpbMssAgHEz43cf0ZulRDSQ==",
|
||||
"from": "sql.js@github:neilalexander/sql.js#252a72bf57b0538cbd49bbd6f70af71e516966ae"
|
||||
}
|
||||
}
|
||||
}
|
||||
5
test/wasm/package.json
Normal file
5
test/wasm/package.json
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"dependencies": {
|
||||
"sql.js": "github:neilalexander/sql.js#252a72bf57b0538cbd49bbd6f70af71e516966ae"
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue