You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
GIT for nodejs and the browser using [libgit2](https://libgit2.org/) compiled to WebAssembly with [Emscripten](https://emscripten.org).
8
8
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.
**Please do not abuse, this is open for you to test and see the proof of concept**
16
18
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]/
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.
54
20
55
-
`<script>new Worker('yourworker.js')</script>;`
21
+
# Demo videos
56
22
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.
59
24
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
62
26
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
-
<htmllang="en">
75
-
<head>
76
-
<scriptsrc='/build/lg2.js'></script>
77
-
78
-
<script>
79
-
window.lg2Ready=false;
80
-
window.lg2= Module;
81
-
window.lg2ReadyPromise=newPromise((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.
88
28
89
-
<scriptdefersrc='/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.
91
30
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
-
asyncfunctioninitApp() {
99
-
awaitwindow.lg2ReadyPromise;
100
-
awaittest();
101
-
}
31
+
Examples of using Wasm-git can be found in the tests:
102
32
103
-
asyncfunctiontest() {
104
-
constlg=window.lg2;
105
-
constFS=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
106
36
107
-
constlg= 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.
The [Github actions test pipeline](./.github/workflows/main.yml) shows all the commands needed to install dependencies, build the packages and run the tests.
116
42
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.
# Emscripten fixes that were needed for making Wasm-git work
120
46
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.
123
48
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:
@@ -167,12 +56,3 @@ for using with `NODEFS` you'll also need https://github.com/emscripten-core/emsc
167
56
168
57
All of these pull requests are merged to emscripten master as of 2020-03-29.
169
58
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.
0 commit comments