From 967bc972f7764b777dcbaf9a4eb8e9efda36a374 Mon Sep 17 00:00:00 2001 From: hillct Date: Wed, 30 Nov 2016 19:02:28 -0500 Subject: [PATCH 1/6] Alpine based Docker image, metadata labels & Autobuild hooks --- Dockerfile | 23 +++++++++++++++++++- README.md | 27 ++++++++++++++++++------ gun.js | 56 +++++++++++++++++++++++++------------------------ hooks/build | 5 +++++ hooks/post_push | 3 +++ 5 files changed, 80 insertions(+), 34 deletions(-) create mode 100755 hooks/build create mode 100755 hooks/post_push diff --git a/Dockerfile b/Dockerfile index 09650cd7..d71e03cf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,24 @@ -FROM node:6-onbuild +FROM alpine:edge +# Build-time metadata as defined at http://label-schema.org +ARG BUILD_DATE +ARG VCS_REF +ARG VCS_URL +ARG VERSION +LABEL org.label-schema.build-date=$BUILD_DATE \ + org.label-schema.name="Gun - Offline First, Javascript Graph Database" \ +# org.label-schema.description="Let it be pulled from Readme.md..." \ + org.label-schema.url="http://gun.js.org" \ + org.label-schema.vcs-ref=$VCS_REF \ + org.label-schema.vcs-url=$VCS_URL \ + org.label-schema.vendor="Pinnacle Digital" \ + org.label-schema.version=$VERSION \ + org.label-schema.schema-version="1.0" +WORKDIR /app +ADD . . ENV NPM_CONFIG_LOGLEVEL warn +RUN apk update && apk upgrade \ + && apk add --no-cache ca-certificates nodejs \ + && npm install \ + && rm -rf /var/cache/* -rf /tmp/npm* EXPOSE 8080 +CMD ["npm","start"] diff --git a/README.md b/README.md index b5beea73..0b0ea210 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,12 @@ -# gun [![NPM downloads](https://img.shields.io/npm/dm/gun.svg?style=flat)](https://npmjs.org/package/gun) [![Build Status](https://travis-ci.org/amark/gun.svg?branch=master)](https://travis-ci.org/amark/gun) [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/amark/gun?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) +

+ + gun + +

+[![NPM downloads](https://img.shields.io/npm/dm/gun.svg?style=flat)](https://npmjs.org/package/gun) [![Build Status](https://travis-ci.org/amark/gun.svg?branch=master)](https://travis-ci.org/amark/gun) [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/amark/gun?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) +[![Docker Automated buil](https://img.shields.io/docker/automated/gundb/gun.svg)](https://hub.docker.com/r/gundb/gun/) [![](https://images.microbadger.com/badges/image/gundb/gun.svg)](https://microbadger.com/images/gundb/gun "Get your own image badge on microbadger.com") [![Docker Pulls](https://img.shields.io/docker/pulls/gundb/gun.svg)](https://hub.docker.com/r/gundb/gun/) [![Docker Stars](https://img.shields.io/docker/stars/gundb/gun.svg)](https://hub.docker.com/r/gundb/gun/) GUN is a realtime, distributed, offline-first, graph database engine. Lightweight and powerful, at just **~9KB** gzipped. @@ -47,16 +55,23 @@ Try the [interactive tutorial](http://gun.js.org/think.html) in the browser (**5 - To quickly spin up a Gun test server for your development team, uilize eiher [Heroku](http://heroku.com) or [Docker](http://docker.com) or any variant thereof ([Dokku](http://dokku.viewdocs.io/dokku/), [Flynn.io](http://flynn.io), [now.sh](https://zeit.co/now), etc) -### Docker - ```bash +### [Docker](https://www.docker.com/) + - Either (fastest) from the [Docker Hub](https://hub.docker.com/r/gundb/gun/): + +```bash + docker run -p 8080:8080 gundb/gun +``` + - Or build the [Docker](https://docs.docker.com/engine/installation/) image locally: + +```bash git clone https://github.com/amark/gun.git cd gun docker build -t myrepo/gundb:v1 . docker run -p 8080:8080 myrepo/gundb:v1 - ``` +``` Then visit [http://localhost:8080](http://localhost:8080) in your browser. -### Heroku +### [Heroku](https://www.heroku.com/) ```bash git clone https://github.com/amark/gun.git cd gun @@ -65,7 +80,7 @@ Try the [interactive tutorial](http://gun.js.org/think.html) in the browser (**5 ``` Then visit the URL in the output of the 'heroku create' step, in your browser. -### Now.sh +### [Now.sh](https://zeit.co/now/) ```bash npm install -g now git clone https://github.com/amark/gun.git diff --git a/gun.js b/gun.js index 49daa5e8..00c635e2 100644 --- a/gun.js +++ b/gun.js @@ -908,7 +908,9 @@ Gun.chain.opt = function(opt){ opt = opt || {}; var gun = this, at = gun._, tmp, u; - if(!at.root){ root(at) } + at.root = at.root || gun; + at.graph = at.graph || {}; + at.dedup = new Dedup(); at.opt = at.opt || {}; if(text_is(opt)){ opt = {peers: opt} } else if(list_is(opt)){ opt = {peers: opt} } @@ -922,15 +924,13 @@ this[f] = v; }, at.opt); Gun.on('opt', at); + if(!at.once){ + gun.on('in', input, at); + gun.on('out', output, at); + } + at.once = true; return gun; } - function root(at){ - var gun = at.gun; - at.root = gun; - at.graph = {}; - gun.on('in', input, at); - gun.on('out', output, at); - } function output(at){ var cat = this, gun = cat.gun, tmp; // TODO: BUG! Outgoing `get` to read from in memory!!! @@ -939,7 +939,7 @@ cat.on('in', obj_to(at, {gun: cat.gun})); // TODO: PERF! input now goes to output so it would be nice to reduce the circularity here for perf purposes. //} if(at['#']){ - dedup.track(at['#']); + cat.dedup.track(at['#']); } if(!at.gun){ at = Gun.obj.to(at, {gun: gun}); @@ -974,12 +974,12 @@ if(!at['#'] && at['@']){ at['#'] = Gun.text.random(); // TODO: Use what is used other places instead. Gun.on.ack(at['@'], at); - dedup.track(at['#']); + cat.dedup.track(at['#']); cat.on('out', at); return; } - if(at['#'] && dedup.check(at['#'])){ return } - dedup.track(at['#']); + if(at['#'] && cat.dedup.check(at['#'])){ return } + cat.dedup.track(at['#']); Gun.on.ack(at['@'], at); if(at.put){ if(cat.graph){ @@ -1011,40 +1011,41 @@ via: this.at }); } - function dedup(){} - dedup.cache = {}; - dedup.track = function (id) { - dedup.cache[id] = Gun.time.is(); + function Dedup(){ + this.cache = {}; + } + Dedup.prototype.track = function (id) { + this.cache[id] = Gun.time.is(); // Engage GC. - if (!dedup.to) { - dedup.gc(); + if (!this.to) { + this.gc(); } return id; }; - dedup.check = function(id){ + Dedup.prototype.check = function(id){ // Have we seen this ID recently? - return Gun.obj.has(dedup.cache, id); + return Gun.obj.has(this.cache, id); } - dedup.gc = function(){ + Dedup.prototype.gc = function(){ var now = Gun.time.is(); var oldest = now; var maxAge = 5 * 60 * 1000; // TODO: Gun.scheduler already does this? Reuse that. - Gun.obj.map(dedup.cache, function (time, id) { + Gun.obj.map(this.cache, function (time, id) { oldest = Math.min(now, time); if ((now - time) < maxAge) { return; } - delete dedup.cache[id]; + delete this.cache[id]; }); - var done = Gun.obj.empty(dedup.cache); + var done = Gun.obj.empty(this.cache); // Disengage GC. if (done) { - dedup.to = null; + this.to = null; return; } @@ -1055,7 +1056,8 @@ var nextGC = maxAge - elapsed; // Schedule the next GC event. - dedup.to = setTimeout(dedup.gc, nextGC); + var dedup = this; + this.to = setTimeout(function(){ dedup.gc() }, nextGC); } }()); var text = Type.text, text_is = text.is, text_random = text.random; @@ -2484,4 +2486,4 @@ })(require, './adapters/wsp'); -}()); +}()); \ No newline at end of file diff --git a/hooks/build b/hooks/build new file mode 100755 index 00000000..fdf5be51 --- /dev/null +++ b/hooks/build @@ -0,0 +1,5 @@ +#!/bin/bash +docker build --build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \ + --build-arg VCS_REF=`git rev-parse --short HEAD` \ + --build-arg VCS_URL=`git remote get-url origin` \ + --build-arg VERSION=$SOURCE_BRANCH -t $IMAGE_NAME . diff --git a/hooks/post_push b/hooks/post_push new file mode 100755 index 00000000..38af73a6 --- /dev/null +++ b/hooks/post_push @@ -0,0 +1,3 @@ +#!/bin/bash +docker tag $IMAGE_NAME $DOCKER_REPO:latest +docker push $DOCKER_REPO:latest From 6d98b19c67b35816e4bb5c8f76f09f99e66e5921 Mon Sep 17 00:00:00 2001 From: hillct Date: Wed, 30 Nov 2016 20:04:27 -0500 Subject: [PATCH 2/6] Dwitched to an older style git call, to get the repo URL --- hooks/build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hooks/build b/hooks/build index fdf5be51..e04a6b38 100755 --- a/hooks/build +++ b/hooks/build @@ -1,5 +1,5 @@ #!/bin/bash docker build --build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \ --build-arg VCS_REF=`git rev-parse --short HEAD` \ - --build-arg VCS_URL=`git remote get-url origin` \ + --build-arg VCS_URL=`git config --get remote.origin.url` \ --build-arg VERSION=$SOURCE_BRANCH -t $IMAGE_NAME . From fbbd5d58993e3882020dc0d8c147f543d65eb1a6 Mon Sep 17 00:00:00 2001 From: hillct Date: Wed, 30 Nov 2016 22:06:37 -0500 Subject: [PATCH 3/6] Commit Badge from Microbadger --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0b0ea210..a46a98f4 100644 --- a/README.md +++ b/README.md @@ -55,8 +55,8 @@ Try the [interactive tutorial](http://gun.js.org/think.html) in the browser (**5 - To quickly spin up a Gun test server for your development team, uilize eiher [Heroku](http://heroku.com) or [Docker](http://docker.com) or any variant thereof ([Dokku](http://dokku.viewdocs.io/dokku/), [Flynn.io](http://flynn.io), [now.sh](https://zeit.co/now), etc) -### [Docker](https://www.docker.com/) - - Either (fastest) from the [Docker Hub](https://hub.docker.com/r/gundb/gun/): +### [Docker](https://www.docker.com/) + - Either (fastest) from the [Docker Hub](https://hub.docker.com/r/gundb/gun/)(Built at [![](https://images.microbadger.com/badges/commit/gundb/gun.svg)](https://microbadger.com/images/gundb/gun "Get your own commit badge on microbadger.com")): ```bash docker run -p 8080:8080 gundb/gun @@ -119,7 +119,7 @@ Designed with ♥ by Mark Nadal, the gun team, and many amazing contributors. L Thanks to the following people who have contributed to GUN, via code, issues, or conversation (this list has quickly become tremendously behind! We'll probably turn this into a dedicated wiki page so you can add yourself): -[agborkowski](https://github.com/agborkowski); [alexlafroscia](https://github.com/alexlafroscia); [anubiann00b](https://github.com/anubiann00b); [bromagosa](https://github.com/bromagosa); [coolaj86](https://github.com/coolaj86); [d-oliveros](https://github.com/d-oliveros), [danscan](https://github.com/danscan); **[forrestjt](https://github.com/forrestjt) ([file.js](https://github.com/amark/gun/blob/master/lib/file.js))**; [gedw99](https://github.com/gedw99); [HelloCodeMing](https://github.com/HelloCodeMing); **[JosePedroDias](https://github.com/josepedrodias) (graph visualizer)**; **[jveres](https://github.com/jveres) ([todoMVC](https://github.com/jveres/todomvc) [live demo](http://todos.loqali.com/))**; [ndarilek](https://github.com/ndarilek); [onetom](https://github.com/onetom); [phpnode](https://github.com/phpnode); [PsychoLlama](https://github.com/PsychoLlama); **[RangerMauve](https://github.com/RangerMauve) ([schema](https://github.com/gundb/gun-schema))**; [riston](https://github.com/riston); [rootsical](https://github.com/rootsical); [rrrene](https://github.com/rrrene); [ssr1ram](https://github.com/ssr1ram); [Xe](https://github.com/Xe); [zot](https://github.com/zot); +[agborkowski](https://github.com/agborkowski); [alexlafroscia](https://github.com/alexlafroscia); [anubiann00b](https://github.com/anubiann00b); [bromagosa](https://github.com/bromagosa); [coolaj86](https://github.com/coolaj86); [d-oliveros](https://github.com/d-oliveros), [danscan](https://github.com/danscan); **[forrestjt](https://github.com/forrestjt) ([file.js](https://github.com/amark/gun/blob/master/lib/file.js))**; [gedw99](https://github.com/gedw99); [HelloCodeMing](https://github.com/HelloCodeMing); **[Hillct](https://github.com/hillct) (Deployment Tools); **[JosePedroDias](https://github.com/josepedrodias) (graph visualizer)**; **[jveres](https://github.com/jveres) ([todoMVC](https://github.com/jveres/todomvc) [live demo](http://todos.loqali.com/))**; [ndarilek](https://github.com/ndarilek); [onetom](https://github.com/onetom); [phpnode](https://github.com/phpnode); [PsychoLlama](https://github.com/PsychoLlama); **[RangerMauve](https://github.com/RangerMauve) ([schema](https://github.com/gundb/gun-schema))**; [riston](https://github.com/riston); [rootsical](https://github.com/rootsical); [rrrene](https://github.com/rrrene); [ssr1ram](https://github.com/ssr1ram); [Xe](https://github.com/Xe); [zot](https://github.com/zot); [ayurmedia](https://github.com/ayurmedia); This list of contributors was manually compiled and alphabetically sorted. If we missed you, please submit an issue so we can get you added! From ba1ca8f54fb3b942b5f1d53df1978e3e9c8389a7 Mon Sep 17 00:00:00 2001 From: hillct Date: Thu, 1 Dec 2016 00:11:38 -0500 Subject: [PATCH 4/6] Made the Docker Hub Automated build hook usable offline via 'npm docker' Incorporated the unbuild step into the prepublish lifecycle workflow Assured that /src files from Unbuild will never be comitted accidentally Updated the uglify-js depencency and prepublish script for module rename --- README.md | 6 +++--- hooks/build | 2 ++ package.json | 5 +++-- src/.gitignore | 4 ++++ 4 files changed, 12 insertions(+), 5 deletions(-) create mode 100644 src/.gitignore diff --git a/README.md b/README.md index 0b0ea210..a46a98f4 100644 --- a/README.md +++ b/README.md @@ -55,8 +55,8 @@ Try the [interactive tutorial](http://gun.js.org/think.html) in the browser (**5 - To quickly spin up a Gun test server for your development team, uilize eiher [Heroku](http://heroku.com) or [Docker](http://docker.com) or any variant thereof ([Dokku](http://dokku.viewdocs.io/dokku/), [Flynn.io](http://flynn.io), [now.sh](https://zeit.co/now), etc) -### [Docker](https://www.docker.com/) - - Either (fastest) from the [Docker Hub](https://hub.docker.com/r/gundb/gun/): +### [Docker](https://www.docker.com/) + - Either (fastest) from the [Docker Hub](https://hub.docker.com/r/gundb/gun/)(Built at [![](https://images.microbadger.com/badges/commit/gundb/gun.svg)](https://microbadger.com/images/gundb/gun "Get your own commit badge on microbadger.com")): ```bash docker run -p 8080:8080 gundb/gun @@ -119,7 +119,7 @@ Designed with ♥ by Mark Nadal, the gun team, and many amazing contributors. L Thanks to the following people who have contributed to GUN, via code, issues, or conversation (this list has quickly become tremendously behind! We'll probably turn this into a dedicated wiki page so you can add yourself): -[agborkowski](https://github.com/agborkowski); [alexlafroscia](https://github.com/alexlafroscia); [anubiann00b](https://github.com/anubiann00b); [bromagosa](https://github.com/bromagosa); [coolaj86](https://github.com/coolaj86); [d-oliveros](https://github.com/d-oliveros), [danscan](https://github.com/danscan); **[forrestjt](https://github.com/forrestjt) ([file.js](https://github.com/amark/gun/blob/master/lib/file.js))**; [gedw99](https://github.com/gedw99); [HelloCodeMing](https://github.com/HelloCodeMing); **[JosePedroDias](https://github.com/josepedrodias) (graph visualizer)**; **[jveres](https://github.com/jveres) ([todoMVC](https://github.com/jveres/todomvc) [live demo](http://todos.loqali.com/))**; [ndarilek](https://github.com/ndarilek); [onetom](https://github.com/onetom); [phpnode](https://github.com/phpnode); [PsychoLlama](https://github.com/PsychoLlama); **[RangerMauve](https://github.com/RangerMauve) ([schema](https://github.com/gundb/gun-schema))**; [riston](https://github.com/riston); [rootsical](https://github.com/rootsical); [rrrene](https://github.com/rrrene); [ssr1ram](https://github.com/ssr1ram); [Xe](https://github.com/Xe); [zot](https://github.com/zot); +[agborkowski](https://github.com/agborkowski); [alexlafroscia](https://github.com/alexlafroscia); [anubiann00b](https://github.com/anubiann00b); [bromagosa](https://github.com/bromagosa); [coolaj86](https://github.com/coolaj86); [d-oliveros](https://github.com/d-oliveros), [danscan](https://github.com/danscan); **[forrestjt](https://github.com/forrestjt) ([file.js](https://github.com/amark/gun/blob/master/lib/file.js))**; [gedw99](https://github.com/gedw99); [HelloCodeMing](https://github.com/HelloCodeMing); **[Hillct](https://github.com/hillct) (Deployment Tools); **[JosePedroDias](https://github.com/josepedrodias) (graph visualizer)**; **[jveres](https://github.com/jveres) ([todoMVC](https://github.com/jveres/todomvc) [live demo](http://todos.loqali.com/))**; [ndarilek](https://github.com/ndarilek); [onetom](https://github.com/onetom); [phpnode](https://github.com/phpnode); [PsychoLlama](https://github.com/PsychoLlama); **[RangerMauve](https://github.com/RangerMauve) ([schema](https://github.com/gundb/gun-schema))**; [riston](https://github.com/riston); [rootsical](https://github.com/rootsical); [rrrene](https://github.com/rrrene); [ssr1ram](https://github.com/ssr1ram); [Xe](https://github.com/Xe); [zot](https://github.com/zot); [ayurmedia](https://github.com/ayurmedia); This list of contributors was manually compiled and alphabetically sorted. If we missed you, please submit an issue so we can get you added! diff --git a/hooks/build b/hooks/build index e04a6b38..1f6fd415 100755 --- a/hooks/build +++ b/hooks/build @@ -1,4 +1,6 @@ #!/bin/bash +FOO=${IMAGE_NAME:=`whoami`/gun:git-local} +BAR=${SOURCE_BRANCH:=`git rev-parse --abbrev-ref HEAD`} docker build --build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` \ --build-arg VCS_REF=`git rev-parse --short HEAD` \ --build-arg VCS_URL=`git config --get remote.origin.url` \ diff --git a/package.json b/package.json index b5969385..ab9f8c81 100644 --- a/package.json +++ b/package.json @@ -5,8 +5,9 @@ "main": "index.js", "scripts": { "start": "node examples/http.js 8080", - "prepublish": "uglifyjs2 gun.js -o gun.min.js -c -m", + "prepublish": "npm run unbuild && uglifyjs gun.js -o gun.min.js -c -m", "test": "mocha", + "docker": "hooks/build", "unbuild": "node lib/unbuild.js" }, "repository": { @@ -54,6 +55,6 @@ "mocha": "~>1.9.0", "panic-server": "~>0.3.0", "selenium-webdriver": "~>2.53.2", - "uglify-js2": "^2.1.11" + "uglify-js": "^2.2.0" } } diff --git a/src/.gitignore b/src/.gitignore new file mode 100644 index 00000000..5e7d2734 --- /dev/null +++ b/src/.gitignore @@ -0,0 +1,4 @@ +# Ignore everything in this directory +* +# Except this file +!.gitignore From 9fe11f6e9f8005bd73749c7411f824cd52225fb7 Mon Sep 17 00:00:00 2001 From: hillct Date: Thu, 1 Dec 2016 00:53:08 -0500 Subject: [PATCH 5/6] Added local Docker hook usage example --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index a46a98f4..1c93a01d 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,12 @@ Try the [interactive tutorial](http://gun.js.org/think.html) in the browser (**5 docker build -t myrepo/gundb:v1 . docker run -p 8080:8080 myrepo/gundb:v1 ``` + - Or, if you prefer your Docker image with metadata labels (Linux/Mac only): + ```bash + npm run docker + docker run -p 8080:8080 usenameHere/gun:git +``` + Then visit [http://localhost:8080](http://localhost:8080) in your browser. ### [Heroku](https://www.heroku.com/) From 6b78b2ee91fc3442fe0976812db868cdd49b4709 Mon Sep 17 00:00:00 2001 From: hillct Date: Thu, 1 Dec 2016 00:57:48 -0500 Subject: [PATCH 6/6] Fixed Vendor Name --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index d71e03cf..2fe0ef39 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,7 +10,7 @@ LABEL org.label-schema.build-date=$BUILD_DATE \ org.label-schema.url="http://gun.js.org" \ org.label-schema.vcs-ref=$VCS_REF \ org.label-schema.vcs-url=$VCS_URL \ - org.label-schema.vendor="Pinnacle Digital" \ + org.label-schema.vendor="The Gun Database Team" \ org.label-schema.version=$VERSION \ org.label-schema.schema-version="1.0" WORKDIR /app