Skip to content

Commit

Permalink
Switch from Karma to Web Test Runner, and ES6 module exports (#97)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
petersalomonsen authored May 24, 2024
1 parent 42a4245 commit 7236b20
Show file tree
Hide file tree
Showing 37 changed files with 6,811 additions and 2,550 deletions.
12 changes: 12 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"postCreateCommand": "/workspaces/wasm-git/.devcontainer/post-create.sh",
"customizations": {
"vscode": {
"extensions": [
"dtsvet.vscode-wasm",
"ms-vscode.cpptools-extension-pack",
"github.vscode-github-actions"
]
}
}
}
14 changes: 14 additions & 0 deletions .devcontainer/post-create.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
curl https://wasmtime.dev/install.sh -sSf | bash
npm install
sh setup.sh

git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
git pull
./emsdk install latest
./emsdk activate latest
cd ..

npx playwright install-deps
npx playwright install
4 changes: 4 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ jobs:
- name: Default
run: |
npm install
npx playwright install-deps
npx playwright install
sh setup.sh
git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
Expand All @@ -43,6 +45,8 @@ jobs:
- name: Default
run: |
npm install
npx playwright install-deps
npx playwright install
sh setup.sh
git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ jobs:
- name: Default
run: |
npm install
npx playwright install-deps
npx playwright install
sh setup.sh
git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ libgit2-*
wasm-git-*.tgz
lg2.js
lg2.wasm
lg2_async.js
lg2_async.wasm
.DS_Store
.vscode
node_modules
package
.idea
emsdk
nodefsclonetest
google-chrome-stable_current_amd64.deb
google-chrome-stable_current_amd64.deb
160 changes: 20 additions & 140 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ Wasm-git
========
(Wasm should be pronounced like `awesome` starting with a `W` ).

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

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

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.

# Demo in the browser

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

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

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.

# Example WebWorker with pre built binaries

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.

Here's an example of a simple webworker that uses pre-built binaries from https://unpkg.com/[email protected]/

```js
var Module = {
locateFile: function(s) {
return 'https://unpkg.com/[email protected]/' + s;
}
};

importScripts('https://unpkg.com/[email protected]/lg2.js');

Module.onRuntimeInitialized = () => {
const lg = Module;

FS.mkdir('/working');
FS.mount(MEMFS, { }, '/working');
FS.chdir('/working');

FS.writeFile('/home/web_user/.gitconfig', '[user]\n' +
'name = Test User\n' +
'email = [email protected]');

// clone a local git repository and make some commits

lg.callMain(['clone',`http://localhost:5000/test.git`, 'testrepo']);

FS.readdir('testrepo');
}
```

You'll start the worker from your html with the tag:
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.

`<script>new Worker('yourworker.js')</script>;`
# Demo videos

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
accessing github directly. For testing you can use the proxy found in [examples/webserverwithgithubproxy.js](examples/webserverwithgithubproxy.js)
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.

# Use in Browser without a WebWorker
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.
# Examples

If you use the `emscriptenbuilds/build.sh` you can build `async` versions with:
```
./build.sh Release-async
```
and
```
./build.sh Debug-async
```
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`:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<script src='/build/lg2.js'></script>

<script>
window.lg2Ready = false;
window.lg2 = Module;
window.lg2ReadyPromise = new Promise((resolve) => {
Module.onRuntimeInitialized = () => {
window.lg2Ready = true;
resolve(true);
};
});
</script>
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.

<script defer src='/build/bundle.js'></script>
</head>
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.

<body>
</body>
</html>
```
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:
```javascript
async function initApp() {
await window.lg2ReadyPromise;
await test();
}
Examples of using Wasm-git can be found in the tests:

async function test() {
const lg = window.lg2;
const FS = lg.FS;
- [test](./test/) for NodeJS
- [test-browser](./test-browser/) for the sync version in the browser with a web worker
- [test-browser-async](./test-browser-async/)] for the async version in the browser

const lg = Module;
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.

FS.mkdir('/working');
FS.mount(MEMFS, { }, '/working');
FS.chdir('/working');
# Building and developing

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

// clone a local git repository and make some commits
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.

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

FS.readdir('testrepo');
}
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.

```

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.

See below for more details on building using `build.sh`.
# Use from nodejs with pre built binaries

You may install the npm package containing the binaries:

`npm install wasm-git`

example source code for cloning a repository from github:

```js
const lg = require('./node_modules/wasm-git/lg2.js');

lg.onRuntimeInitialized = () => {
const FS = lg.FS;
const MEMFS = FS.filesystems.MEMFS;

FS.mkdir('/working');
FS.mount(MEMFS, { }, '/working');
FS.chdir('/working');

FS.writeFile('/home/web_user/.gitconfig', '[user]\n' +
'name = Test User\n' +
'email = [email protected]');

// clone a repository from github
lg.callMain(['clone','https://github.com/torch2424/made-with-webassembly.git', 'made-with-webassembly']);
FS.readdir('made-with-webassembly');
}
```

# Building

**IMPORTANT: This depends on the following mmap fixes for emscripten:**
Here are the Pull Requests that resolved the issues identified when the first version was developed:

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

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

See [.github/workflows/main.yml](./.github/workflows/main.yml) for a full build and test pipeline including installing emscripten.

Run [setup.sh](setup.sh) first to download libgit2 and apply patches.

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.

An example of interacting with libgit2 from nodejs can be found in [examples/example_node.js](examples/example_node.js).

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.
4 changes: 2 additions & 2 deletions emscriptenbuild/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ elif [ "$1" == "Debug-async" ]; then
export LG2_OUTPUT_NAME=lg2_async
fi

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

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 ..
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 ..
emmake make lg2
1 change: 1 addition & 0 deletions emscriptenbuild/pre.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Object.assign(Module, globalThis.wasmGitModuleOverrides);

if (!Module.print && !Module.printErr) {
let capturedOutput = null;
Expand Down
2 changes: 0 additions & 2 deletions examples/.gitignore

This file was deleted.

50 changes: 0 additions & 50 deletions examples/example_node.js

This file was deleted.

21 changes: 0 additions & 21 deletions examples/example_node_nodefs.js

This file was deleted.

Loading

0 comments on commit 7236b20

Please sign in to comment.