Skip to content

Commit 7236b20

Browse files
Switch from Karma to Web Test Runner, and ES6 module exports (#97)
* devcontainer setup * replace commonjs with es6 modules * replace karma with web test runner * remove examples folder, since the tests serve as examples * v0.0.13
1 parent 42a4245 commit 7236b20

37 files changed

+6811
-2550
lines changed

.devcontainer/devcontainer.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"postCreateCommand": "/workspaces/wasm-git/.devcontainer/post-create.sh",
3+
"customizations": {
4+
"vscode": {
5+
"extensions": [
6+
"dtsvet.vscode-wasm",
7+
"ms-vscode.cpptools-extension-pack",
8+
"github.vscode-github-actions"
9+
]
10+
}
11+
}
12+
}

.devcontainer/post-create.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
curl https://wasmtime.dev/install.sh -sSf | bash
3+
npm install
4+
sh setup.sh
5+
6+
git clone https://github.com/emscripten-core/emsdk.git
7+
cd emsdk
8+
git pull
9+
./emsdk install latest
10+
./emsdk activate latest
11+
cd ..
12+
13+
npx playwright install-deps
14+
npx playwright install

.github/workflows/main.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ jobs:
2222
- name: Default
2323
run: |
2424
npm install
25+
npx playwright install-deps
26+
npx playwright install
2527
sh setup.sh
2628
git clone https://github.com/emscripten-core/emsdk.git
2729
cd emsdk
@@ -43,6 +45,8 @@ jobs:
4345
- name: Default
4446
run: |
4547
npm install
48+
npx playwright install-deps
49+
npx playwright install
4650
sh setup.sh
4751
git clone https://github.com/emscripten-core/emsdk.git
4852
cd emsdk

.github/workflows/publish.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ jobs:
1717
- name: Default
1818
run: |
1919
npm install
20+
npx playwright install-deps
21+
npx playwright install
2022
sh setup.sh
2123
git clone https://github.com/emscripten-core/emsdk.git
2224
cd emsdk

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ libgit2-*
33
wasm-git-*.tgz
44
lg2.js
55
lg2.wasm
6+
lg2_async.js
7+
lg2_async.wasm
68
.DS_Store
79
.vscode
810
node_modules
911
package
1012
.idea
1113
emsdk
1214
nodefsclonetest
13-
google-chrome-stable_current_amd64.deb
15+
google-chrome-stable_current_amd64.deb

README.md

Lines changed: 20 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ Wasm-git
22
========
33
(Wasm should be pronounced like `awesome` starting with a `W` ).
44

5-
![](https://travis-ci.com/petersalomonsen/wasm-git.svg?branch=master)
5+
![](https://github.com/petersalomonsen/wasm-git/actions/workflows/main.yml/badge.svg)
66

77
GIT for nodejs and the browser using [libgit2](https://libgit2.org/) compiled to WebAssembly with [Emscripten](https://emscripten.org).
88

9+
The main purpose of bringing git to the browser, is to enable storage of web application data locally in the users web browser, with the option to synchronize with a remote server.
10+
911
# Demo in the browser
1012

1113
A simple demo in the browser can be found at:
@@ -14,150 +16,37 @@ https://wasm-git.petersalomonsen.com/
1416

1517
**Please do not abuse, this is open for you to test and see the proof of concept**
1618

17-
The sources for the demo can be found in the [githttpserver](https://github.com/petersalomonsen/githttpserver) project, which is a simple git server deployable to [kubernetes](https://github.com/kubernetes/kubernetes). Showing basic operations like cloning, edit files, add and commit, push and pull.
18-
19-
# Example WebWorker with pre built binaries
20-
21-
For running in the browser you should have your git interaction code in a [webworker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers). This is because of the use of synchronous http requests and long running operations that would block if running on the main thread.
22-
23-
Here's an example of a simple webworker that uses pre-built binaries from https://unpkg.com/[email protected]/
24-
25-
```js
26-
var Module = {
27-
locateFile: function(s) {
28-
return 'https://unpkg.com/[email protected]/' + s;
29-
}
30-
};
31-
32-
importScripts('https://unpkg.com/[email protected]/lg2.js');
33-
34-
Module.onRuntimeInitialized = () => {
35-
const lg = Module;
36-
37-
FS.mkdir('/working');
38-
FS.mount(MEMFS, { }, '/working');
39-
FS.chdir('/working');
40-
41-
FS.writeFile('/home/web_user/.gitconfig', '[user]\n' +
42-
'name = Test User\n' +
43-
'email = [email protected]');
44-
45-
// clone a local git repository and make some commits
46-
47-
lg.callMain(['clone',`http://localhost:5000/test.git`, 'testrepo']);
48-
49-
FS.readdir('testrepo');
50-
}
51-
```
52-
53-
You'll start the worker from your html with the tag:
19+
The sources for the demo can be found in the [githttpserver](https://github.com/petersalomonsen/githttpserver) project. It shows basic operations like cloning, edit files, add and commit, push and pull.
5420

55-
`<script>new Worker('yourworker.js')</script>;`
21+
# Demo videos
5622

57-
The example above expects to find a git repository at `http://localhost:5000/test.git`. If you want to clone from github you'd need a proxy running locally because of [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) restrictions that would prevent you
58-
accessing github directly. For testing you can use the proxy found in [examples/webserverwithgithubproxy.js](examples/webserverwithgithubproxy.js)
23+
Videos showing example applications using wasm-git can bee seen in [this playlist](https://www.youtube.com/watch?v=1Hqy7cVkygU&list=PLv5wm4YuO4Iyx00ifs6xUwIRSFnBI8GZh). Wasm-git is used for local and offline storage of web application data, and for syncing with a remote server.
5924

60-
# Use in Browser without a WebWorker
61-
A webworker is more performant but for cases where you really need to work in the browser, the http requests must be asynchronous and not synchronous as in the default builds.
25+
# Examples
6226

63-
If you use the `emscriptenbuilds/build.sh` you can build `async` versions with:
64-
```
65-
./build.sh Release-async
66-
```
67-
and
68-
```
69-
./build.sh Debug-async
70-
```
71-
To use the async wasm-git build you need to load `lg2.js` using a `<script>` tag in HTML, and set up a method to wait for initialisation of the lg2 module. For example, your `index.html` can include the following, and set up a promise for your JavaScript implementation to `await`:
72-
```html
73-
<!DOCTYPE html>
74-
<html lang="en">
75-
<head>
76-
<script src='/build/lg2.js'></script>
77-
78-
<script>
79-
window.lg2Ready = false;
80-
window.lg2 = Module;
81-
window.lg2ReadyPromise = new Promise((resolve) => {
82-
Module.onRuntimeInitialized = () => {
83-
window.lg2Ready = true;
84-
resolve(true);
85-
};
86-
});
87-
</script>
27+
Wasm-git packages are built in two variants: Synchronuous and Asynchronuous. To run the sync version in the browser, a [webworker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers) is needed. This is because of the use of synchronous http requests and long running operations that would block if running on the main thread. The sync version has the smallest binary, but need extra client code to communicate with the web worker. When using the sync version in nodejs [worker_threads](https://nodejs.org/api/worker_threads.html) are used, with [Atomics](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics) to exchange data between threads.
8828

89-
<script defer src='/build/bundle.js'></script>
90-
</head>
29+
The async version use [Emscripten Asyncify](https://emscripten.org/docs/porting/asyncify.html), which allows calling the Wasm-git functions with `async` / `await`. It can also run from the main thread in the browser. Asyncify increase the binary size because of instrumentation to unwind and rewind WebAssembly state, but makes it possible to have simple client code without exchanging data with worker threads like in the sync version.
9130

92-
<body>
93-
</body>
94-
</html>
95-
```
96-
In your bundle, you will have JavaScript to `await window.lg2ReadyPromise` and can then use `await callMain()` to invoke `libgit2` features exposed by `wasm-git`. Notice you MUST use `await` on every `callMain()` which interacts with a remote repository (e.g. 'clone', 'push' and so on). Example:
97-
```javascript
98-
async function initApp() {
99-
await window.lg2ReadyPromise;
100-
await test();
101-
}
31+
Examples of using Wasm-git can be found in the tests:
10232

103-
async function test() {
104-
const lg = window.lg2;
105-
const FS = lg.FS;
33+
- [test](./test/) for NodeJS
34+
- [test-browser](./test-browser/) for the sync version in the browser with a web worker
35+
- [test-browser-async](./test-browser-async/)] for the async version in the browser
10636

107-
const lg = Module;
37+
The examples shows importing the `lg2.js` / `lg2-async.js` modules from the local build, but you may also access these from releases available at public CDNs.
10838

109-
FS.mkdir('/working');
110-
FS.mount(MEMFS, { }, '/working');
111-
FS.chdir('/working');
39+
# Building and developing
11240

113-
FS.writeFile('/home/web_user/.gitconfig', '[user]\n' +
114-
'name = Test User\n' +
115-
'email = [email protected]');
41+
The [Github actions test pipeline](./.github/workflows/main.yml) shows all the commands needed to install dependencies, build the packages and run the tests.
11642

117-
// clone a local git repository and make some commits
43+
Another option is loading the repository into a github codespace, where the configuration in [.devcontainer](./.devcontainer) folder will be used to install dependencies and set up a full development environment.
11844

119-
await lg.callMain(['clone',`http://localhost:5000/test.git`, 'testrepo']);
45+
# Emscripten fixes that were needed for making Wasm-git work
12046

121-
FS.readdir('testrepo');
122-
}
47+
As part of being able to compile libgit2 to WebAssembly and run it in a Javascript environment, some fixes to [Emscripten](https://emscripten.org/) were needed.
12348

124-
```
125-
126-
Note that the compiled output is about twice the size for non-async builds, and that git operations will take place on the main thread which can affect reponsiveness of your web UI.
127-
128-
See below for more details on building using `build.sh`.
129-
# Use from nodejs with pre built binaries
130-
131-
You may install the npm package containing the binaries:
132-
133-
`npm install wasm-git`
134-
135-
example source code for cloning a repository from github:
136-
137-
```js
138-
const lg = require('./node_modules/wasm-git/lg2.js');
139-
140-
lg.onRuntimeInitialized = () => {
141-
const FS = lg.FS;
142-
const MEMFS = FS.filesystems.MEMFS;
143-
144-
FS.mkdir('/working');
145-
FS.mount(MEMFS, { }, '/working');
146-
FS.chdir('/working');
147-
148-
FS.writeFile('/home/web_user/.gitconfig', '[user]\n' +
149-
'name = Test User\n' +
150-
'email = [email protected]');
151-
152-
// clone a repository from github
153-
lg.callMain(['clone','https://github.com/torch2424/made-with-webassembly.git', 'made-with-webassembly']);
154-
FS.readdir('made-with-webassembly');
155-
}
156-
```
157-
158-
# Building
159-
160-
**IMPORTANT: This depends on the following mmap fixes for emscripten:**
49+
Here are the Pull Requests that resolved the issues identified when the first version was developed:
16150

16251
- https://github.com/emscripten-core/emscripten/pull/10095
16352
- https://github.com/emscripten-core/emscripten/pull/10526
@@ -167,12 +56,3 @@ for using with `NODEFS` you'll also need https://github.com/emscripten-core/emsc
16756

16857
All of these pull requests are merged to emscripten master as of 2020-03-29.
16958

170-
See [.github/workflows/main.yml](./.github/workflows/main.yml) for a full build and test pipeline including installing emscripten.
171-
172-
Run [setup.sh](setup.sh) first to download libgit2 and apply patches.
173-
174-
Given you have installed and activated emscripten, you can use the script in [emscriptenbuild/build.sh](emscriptenbuild/build.sh) to configure and build, and you'll find the resulting `lg2.js` / `lg2.wasm` under the generated `emscriptenbuild/examples` folder.
175-
176-
An example of interacting with libgit2 from nodejs can be found in [examples/example_node.js](examples/example_node.js).
177-
178-
An example for the browser (using webworkers) can be found in [examples/example_webworker.js](examples/example_webworker.js). You can start a webserver for this by running the [examples/webserverwithgithubproxy.js](examples/webserverwithgithubproxy.js) script, which will launch a http server at http://localhost:5000 with a proxy to github. Proxy instead of direct calls is needed because of CORS restrictions in a browser environment.

emscriptenbuild/build.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ elif [ "$1" == "Debug-async" ]; then
3232
export LG2_OUTPUT_NAME=lg2_async
3333
fi
3434

35-
# Before building, remove any ../libgit2/src/transports/emscriptenhttp.c left from running setup.sh
35+
# Before building, remove any ../libgit2/src/ transports/emscriptenhttp.c left from running setup.sh
3636
[ -f "../libgit2/src/libgit2/transports/emscriptenhttp-async.c" ] && rm ../libgit2/src/libgit2/transports/emscriptenhttp-async.c
3737

38-
emcmake cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_C_FLAGS="$EXTRA_CMAKE_C_FLAGS --pre-js $(pwd)/pre.js $POST_JS -s \"EXTRA_EXPORTED_RUNTIME_METHODS=['FS','callMain']\" -lnodefs.js -lidbfs.js -s INVOKE_RUN=0 -s ALLOW_MEMORY_GROWTH=1 -s STACK_SIZE=131072" -DREGEX_BACKEND=regcomp -DSONAME=OFF -DUSE_HTTPS=OFF -DBUILD_SHARED_LIBS=OFF -DTHREADSAFE=OFF -DUSE_SSH=OFF -DBUILD_CLAR=OFF -DBUILD_EXAMPLES=ON ..
38+
emcmake cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_C_FLAGS="$EXTRA_CMAKE_C_FLAGS --pre-js $(pwd)/pre.js $POST_JS -s \"EXPORTED_RUNTIME_METHODS=['FS','MEMFS','IDBFS','NODEFS','callMain']\" -sFORCE_FILESYSTEM -sEXPORT_ES6 -s INVOKE_RUN=0 -s ALLOW_MEMORY_GROWTH=1 -s STACK_SIZE=131072 -lidbfs.js -lnodefs.js" -DREGEX_BACKEND=regcomp -DSONAME=OFF -DUSE_HTTPS=OFF -DBUILD_SHARED_LIBS=OFF -DTHREADSAFE=OFF -DUSE_SSH=OFF -DBUILD_CLAR=OFF -DBUILD_EXAMPLES=ON ..
3939
emmake make lg2

emscriptenbuild/pre.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
Object.assign(Module, globalThis.wasmGitModuleOverrides);
12

23
if (!Module.print && !Module.printErr) {
34
let capturedOutput = null;

examples/.gitignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

examples/example_node.js

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)