@plotdb/registry 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,150 @@
1
+ # @plotdb/registry
2
+
3
+ `@plotdb/registry` is a local package cache service for dynamic package loading. Similar to CDN but it's meant to provide package access control directly via the backend access point.
4
+
5
+ Basic idea:
6
+
7
+ - reverse proxy (nginx) check if a file exists. return it directly if found.
8
+ - if the file is not found, request is passed to registry backend.
9
+ - registry backend looks up the given package based on the file's path, and download the package if found.
10
+ - registry backend instruct nginx to redirect for the downloaded file.
11
+
12
+ You can config the backend router to allow only certain packages to be downloaded, or provide additional information to download private packages.
13
+
14
+ `@plotdb/registry` provides:
15
+
16
+ - a package provider interface for fetching, gatekeeping and chaining other package providers.
17
+ - an express router for accepting fetching requests from users.
18
+ - a nginx config generator for accessing requested files based on availability of file.
19
+
20
+
21
+ ## Usage
22
+
23
+ `@plotdb/registry` is expected to be used along with expressjs and nginx.
24
+
25
+ First, include `@plotdb/registry`:
26
+
27
+ registry = require("@plotdb/registry")
28
+
29
+
30
+ prepare a provider:
31
+
32
+ # our own provider, only fetch modules in @plotdb scope
33
+ myprovider = new registry do
34
+ check: ({name, version, path}) ->
35
+ if /@plotdb/.exec(name) => return Promise.resolve!
36
+ return lderror.reject 403
37
+ # chain a default jsdelivr provider
38
+ myprovider.chain(registry.provider.jsdelivr);
39
+
40
+ add a route:
41
+
42
+ app.get("/mylib/*", registry.route({
43
+ provide: myprovider
44
+ root:
45
+ pub: "/lib" # root path in URL users access.
46
+ fs: "/var/lib/cdn/cache" # root folder for `@plotdb/registry` to store file.
47
+ internal: "/ilib" # internal redirect URL from server to nginx.
48
+ });
49
+
50
+ generate customied nginx config:
51
+
52
+ npx registry-nginx -c myconfig.yaml > reg.ngx
53
+
54
+
55
+ include the generated file in the server block of your main nginx config:
56
+
57
+ server {
58
+ include reg.ngx
59
+ }
60
+
61
+ where the config file ( `myconfig.yaml` above ) should contains following fields:
62
+
63
+ - `internal`: internal path for `X-Accel-Redirect`. should be a dummy path which is not used.
64
+ - `pub`: `pub` in the `root` parameter in `registry.route` call.
65
+ - `fs`: `fs` in the `root` parameter in `registry.route` call.
66
+ - `upstream`: upstream url for your backend server defined in your nginx config file. e.g., `backend_api`.
67
+ - `cache`: with two subfields:
68
+ - `name`: cache name.
69
+ - for caching to work, you should manually add a cache path directive using the name configed here, e.g.,
70
+
71
+ proxy_cache_path /tmp/cache keys_zone=mycache:10m levels=1:2 inactive=600s max_size=100m;
72
+
73
+ - `period`: how long the nginx proxy cache should last. e.g., `10m`.
74
+
75
+ Check `sample/config.ngx` and `sample/config.yaml` for a reference of nginx config file and corresponding config yaml.
76
+
77
+
78
+ ## Registry Provider Specification
79
+
80
+ Registry providers are used to access a requested resource which is defined by its `namespace`, `name`, `version`, `path` and optionally `type`.
81
+
82
+ A registry provider can be either following format:
83
+ - object: described below
84
+ - (TBD) string: indicate a root path for a requested resource
85
+ - (TBD) function: return an URL for a requested resource when called
86
+
87
+ A registry provider object should contain following fields:
88
+
89
+ - `name`: provider name
90
+ - `check({name, version})`: access control for the given package `{name, version}`.
91
+ - when omitted, no check will be done.
92
+ - return:
93
+ - if the given package is not allowed:
94
+ - a Promise rejects with `{id: 403, name: 'lderror'}` (or, `lderror.reject(403)`)
95
+ - otherwise:
96
+ - a Promise resolves with nothing.
97
+
98
+ - `fetchRealVersion(opt)`: fetch version and tarball informations of a given package.
99
+ - return a promise resolves with an object.
100
+ - resolved object should contains following fields:
101
+ - `version`: actual version for the given package information.
102
+ - `url`: tarball url
103
+ - `opt` is an object with following fields:
104
+ - `root`: root directory of registry cache.
105
+ - `path`: an object with following fields:
106
+ - `base`: an object with following fields:
107
+ - `pkg`: package root directory ( exclude version name )
108
+ - `version`: package directory of specific version.
109
+ - `version`: path of the internal file storing return object of `fetchRealVersion` call.
110
+ - `404`: path of the internal file that if it exists then the given version of this package is not found.
111
+ - `name`: package name.
112
+ - `version`: package version. should be semver. could be version range, `latest` or `main`.
113
+ - `cachetime`: expected cache time in seconds. default 3600 if omitted.`
114
+ - `versionType`: either `latest`, `specifc` or `range`.
115
+ - `force`: when true, should always try to fetch, ignoring cache or current status.
116
+ - `fetchBundleFile(opt)`: fetch the given package and ectract it.
117
+ - return a promise resolves when bundle file is extracted completely.
118
+ - check `fetchRealVersion` for the definition of `opt`.
119
+
120
+ A provider provides following APIs:
121
+
122
+ - `opt(opt)`: provide additional options for this provider.
123
+ - once provided, this can be accessed via `this._opt` internally.
124
+ - `fetch(opt)`: download the indicated released packages and create a local copy at specified location.
125
+ - `opt` is an object with following fields:
126
+ - `root`: root directory for keeping cached files.
127
+ - `name`: package name. scope is possible, such as `@plotdb/block`.
128
+ - `version`: package version. should always in semver format (e.g., `1.0.0`) or one of `latest` or `main`.
129
+ - in `@plotdb/block` `main` is the locked version, however there is no locked version defined here,
130
+ so `main` is equivalent to `latest`.
131
+ - in github, tags is used for fetching release. tags should be in format `vx.y.z`. e.g., `v1.0.0`.
132
+ - `force`: default false. when true, ignore cache / 404 status and always try fetching package again.
133
+ - `cachetime`: default 3600 seconds. cache for how long (in seconds) since the last fetch attempts.
134
+ - return value: a Promise, resolves if package is found and downloaded. reject `e` in following situation:
135
+ - lderror.id(e) is not 0, but following:
136
+ - 404: package not found.
137
+ - 998: package either found or not found. result cached so no fetch is performed.
138
+ - by our design, router send a `X-Accel-Redirect` to nginx if 998.
139
+ if it's actually 404, nginx will then look up the file and report 404 after found not found.
140
+ - the result will then cached by nginx and won't hit registry backend until cache expires.
141
+ - otherwise, it's an internal exception and should be logged and tracked.
142
+ - `check({name, version})`: call the `check()` function provided in constructor.
143
+ - `chain(providers)`: chain given `providers` in this provider.
144
+ - `providers`: either another provider, or a list of other providers.
145
+ - chained providers will be called if current provider return 404.
146
+
147
+
148
+ ## License
149
+
150
+ MIT License
package/bin/ngxgen.js ADDED
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env node
2
+ (function(){
3
+ var fs, path, templateText, jsYaml, yargs, lib, cfgfile, argv, temp, code, cfg, e;
4
+ fs = require('fs');
5
+ path = require('path');
6
+ templateText = require('template-text');
7
+ jsYaml = require('js-yaml');
8
+ yargs = require('yargs');
9
+ lib = fs.realpathSync(path.dirname(__filename));
10
+ cfgfile = 'config.json';
11
+ argv = yargs.option('config', {
12
+ alias: 'c',
13
+ description: "config json",
14
+ type: 'string'
15
+ }).help('help').alias('help', 'h').check(function(argv, options){
16
+ if (!argv.c) {
17
+ throw new Error("config file required");
18
+ }
19
+ cfgfile = path.join(argv.c);
20
+ if (!fs.existsSync(cfgfile)) {
21
+ throw new Error("config file not found: " + cfgfile);
22
+ }
23
+ return true;
24
+ }).argv;
25
+ temp = fs.readFileSync(path.join(lib, '..', 'dist', 'config.ngx')).toString();
26
+ code = fs.readFileSync(cfgfile).toString();
27
+ try {
28
+ cfg = JSON.parse(code);
29
+ } catch (e$) {
30
+ e = e$;
31
+ cfg = jsYaml.load(code);
32
+ }
33
+ console.log(templateText(temp, cfg, lib));
34
+ }).call(this);
package/build ADDED
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env bash
2
+
3
+ rm -rf dist
4
+ mkdir -p dist
5
+ rm -rf bin
6
+ mkdir -p bin
7
+
8
+ echo "transpile livescript in src to dist ..."
9
+ ./node_modules/.bin/lsc -co dist --no-header src
10
+ echo "build src/ngxgen.ls -> bin/ngxgen.js ..."
11
+ echo "#!/usr/bin/env node" > bin/ngxgen.js
12
+ ./node_modules/.bin/lsc -cp --no-header src/ngxgen.ls >> bin/ngxgen.js
13
+ chmod 755 bin/ngxgen.js
14
+
15
+ echo "copy src/config.ngx to dist ..."
16
+ cp src/config.ngx dist
17
+ echo "done."
18
+
19
+
@@ -0,0 +1,43 @@
1
+
2
+ # this should be put outside server block, where `<cache>` should be the same with `cache.name` config.
3
+ # proxy_cache_path /tmp/cache keys_zone=<cache>:10m levels=1:2 inactive=600s max_size=100m;
4
+
5
+ # requests to path under `internal` are from `X-Accel-Redirect` from registry backend
6
+ # and thus should not hit backend again.
7
+
8
+
9
+ location ~ ^/!{internal}/(.*)$ {
10
+ internal;
11
+ root !{root};
12
+ try_files /!{pub}/$1 /!{pub}/$1/index.html /$1/index.min.js =404;
13
+ }
14
+
15
+ location ~ ^/!{pub}/(.*)$ {
16
+ root !{root};
17
+ # proxy_cache is file-based. here we use `.reg.404` for pkg-based 404 detection
18
+ # however, we don't know how to invalidate this. So this is not used for now.
19
+ # to use this, we have to use location pattern such as:
20
+ #
21
+ # location ~ ^/lib/(?<scope>.+)/(?<name>.+)/(?<version>.+)/(?<path>.*)$ { ... }
22
+ # location ~ ^/lib/(?<name>.+)/(?<version>.+)/(?<path>.*)$ { ... }
23
+ #
24
+ # if (-f /var/lib/cdn/cache/lib/$name/$version/.reg.404) {
25
+ # return 404 "";
26
+ # break;
27
+ # }
28
+ try_files /!{pub}/$1 /!{pub}/$1/index.html /!{pub}/$1/index.min.js @registry_backend;
29
+ }
30
+
31
+ # a dedicated location for registry_backend so we can use a dedicated server for registry
32
+ # to offload requests from main backend. This is controlled by `upstream` config.
33
+ location @registry_backend {
34
+ proxy_set_header X-Real-IP $remote_addr;
35
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
36
+ proxy_set_header Host $http_host;
37
+ proxy_set_header X-NginX-Proxy true;
38
+ proxy_set_header X-Forwarded-Proto $scheme;
39
+ # cache 200 / 404 result for `cache-period` so user can't bombard our backend
40
+ proxy_cache !{cache.name};
41
+ proxy_cache_valid 200 404 !{cache.period};
42
+ proxy_pass http://!{upstream};
43
+ }
package/dist/index.js ADDED
@@ -0,0 +1,11 @@
1
+ (function(){
2
+ var route, provider, providers;
3
+ route = require('./route');
4
+ provider = require('./provider');
5
+ providers = require('./providers');
6
+ module.exports = {
7
+ route: route,
8
+ provider: provider,
9
+ providers: providers
10
+ };
11
+ }).call(this);
package/dist/ngxgen.js ADDED
@@ -0,0 +1,33 @@
1
+ (function(){
2
+ var fs, path, templateText, jsYaml, yargs, lib, cfgfile, argv, temp, code, cfg, e;
3
+ fs = require('fs');
4
+ path = require('path');
5
+ templateText = require('template-text');
6
+ jsYaml = require('js-yaml');
7
+ yargs = require('yargs');
8
+ lib = fs.realpathSync(path.dirname(__filename));
9
+ cfgfile = 'config.json';
10
+ argv = yargs.option('config', {
11
+ alias: 'c',
12
+ description: "config json",
13
+ type: 'string'
14
+ }).help('help').alias('help', 'h').check(function(argv, options){
15
+ if (!argv.c) {
16
+ throw new Error("config file required");
17
+ }
18
+ cfgfile = path.join(argv.c);
19
+ if (!fs.existsSync(cfgfile)) {
20
+ throw new Error("config file not found: " + cfgfile);
21
+ }
22
+ return true;
23
+ }).argv;
24
+ temp = fs.readFileSync(path.join(lib, '..', 'dist', 'config.ngx')).toString();
25
+ code = fs.readFileSync(cfgfile).toString();
26
+ try {
27
+ cfg = JSON.parse(code);
28
+ } catch (e$) {
29
+ e = e$;
30
+ cfg = jsYaml.load(code);
31
+ }
32
+ console.log(templateText(temp, cfg, lib));
33
+ }).call(this);
@@ -0,0 +1,212 @@
1
+ (function(){
2
+ var nodeFetch, lderror, yauzl, pthk, tar, fs, fetch, getVersionType, provider, ref$;
3
+ nodeFetch = require('node-fetch');
4
+ lderror = require('lderror');
5
+ yauzl = require('yauzl');
6
+ pthk = require('pthk');
7
+ tar = require('tar');
8
+ fs = require("fs-extra");
9
+ fetch = nodeFetch;
10
+ getVersionType = function(v){
11
+ if (v === 'latest' || v === 'main') {
12
+ return 'latest';
13
+ } else if (/^\d+\.\d+\.\d+$/.test(v)) {
14
+ return 'specific';
15
+ } else {
16
+ return null;
17
+ }
18
+ };
19
+ provider = function(o){
20
+ o == null && (o = {});
21
+ this._name = o.name || "unnamed" + (provider._idx++);
22
+ this._ps = [].concat(o.chain || []);
23
+ this._fetchRealVersion = o.fetchRealVersion;
24
+ this._fetchBundleFile = o.fetchBundleFile;
25
+ this._check = o.check;
26
+ this._opt = o.opt || {};
27
+ return this;
28
+ };
29
+ provider._hash = {};
30
+ provider._idx = 0;
31
+ provider.add = function(p){
32
+ return this._hash[p._name] = p;
33
+ };
34
+ provider.get = function(n){
35
+ return this._hash[n];
36
+ };
37
+ provider.prototype = (ref$ = Object.create(Object.prototype), ref$.opt = function(o){
38
+ return this._opt = o || {};
39
+ }, ref$.fetch = function(o){
40
+ var this$ = this;
41
+ o == null && (o = {});
42
+ return Promise.resolve().then(function(){
43
+ var path, root, name, version, force, cachetime, versionType, params, _;
44
+ path = {
45
+ base: {}
46
+ };
47
+ root = o.root, name = o.name, version = o.version, force = o.force, cachetime = o.cachetime;
48
+ cachetime = cachetime || 60 * 60;
49
+ versionType = getVersionType(version);
50
+ if (!versionType) {
51
+ return lderror.reject(400);
52
+ }
53
+ params = {
54
+ path: path,
55
+ versionType: versionType,
56
+ root: root,
57
+ name: name,
58
+ version: version,
59
+ force: force,
60
+ cachetime: cachetime
61
+ };
62
+ if (!/^(?:@[0-9a-z._-]+\/)?[0-9a-z._-]+$/.test(name)) {
63
+ return lderror.reject(400);
64
+ }
65
+ path.base.pkg = pthk.join(root, pthk.rectify(name));
66
+ path.base.version = pthk.join(path.base.pkg, version);
67
+ path.version = pthk.join(path.base.version, '.reg.version');
68
+ path[404] = pthk.join(path.base.version, '.reg.404');
69
+ _ = function(idx){
70
+ var pr, p;
71
+ idx == null && (idx = -1);
72
+ if (idx >= 0) {
73
+ if (!(pr = this$._ps[idx])) {
74
+ return lderror.reject(404);
75
+ }
76
+ p = pr.check({
77
+ name: name,
78
+ version: version
79
+ }).then(function(){
80
+ return pr._fetch(params);
81
+ });
82
+ } else {
83
+ p = this$.check({
84
+ name: name,
85
+ version: version
86
+ }).then(function(){
87
+ if (!this$._fetchRealVersion) {
88
+ return lderror.reject(404);
89
+ } else {
90
+ return this$._fetch(params);
91
+ }
92
+ });
93
+ }
94
+ return p['catch'](function(e){
95
+ var id;
96
+ return (id = lderror.id(e)) !== 404
97
+ ? Promise.reject(e)
98
+ : _(idx + 1);
99
+ });
100
+ };
101
+ return _()['catch'](function(e){
102
+ var id;
103
+ if (!((id = lderror.id(e)) === 403 || id === 404)) {
104
+ return Promise.reject(e);
105
+ }
106
+ return fs.ensureDir(path.base.version).then(function(){
107
+ return fs.writeFile(path[404], '');
108
+ }).then(function(){
109
+ return lderror.reject(404);
110
+ });
111
+ });
112
+ });
113
+ }, ref$.check = function(arg$){
114
+ var name, version;
115
+ name = arg$.name, version = arg$.version;
116
+ if (this._check) {
117
+ return this._check({
118
+ name: name,
119
+ version: version
120
+ });
121
+ } else {
122
+ return Promise.resolve();
123
+ }
124
+ }, ref$.chain = function(ps){
125
+ return this._ps.splice.apply(this._ps, [0, 0].concat(Array.isArray(ps)
126
+ ? ps
127
+ : [ps]));
128
+ }, ref$._fetch = function(params){
129
+ var root, name, version, cachetime, force, path, versionType, this$ = this;
130
+ root = params.root, name = params.name, version = params.version, cachetime = params.cachetime, force = params.force, path = params.path, versionType = params.versionType;
131
+ return Promise.resolve().then(function(){
132
+ if (force) {
133
+ return;
134
+ }
135
+ return fs.exists(path.version).then(function(isExisted){
136
+ if (!isExisted) {
137
+ return fs.exists(path[404]).then(function(is404){
138
+ if (!is404) {
139
+ return true;
140
+ }
141
+ return fs.stat(path[404]).then(function(s){
142
+ var dirty;
143
+ dirty = Date.now() > s.mtime.getTime() + cachetime * 1000;
144
+ return !dirty
145
+ ? false
146
+ : fs.remove(path[404]).then(function(){
147
+ return true;
148
+ });
149
+ });
150
+ });
151
+ }
152
+ if (versionType === 'specific') {
153
+ return false;
154
+ }
155
+ if (versionType === 'latest') {
156
+ return fs.stat(path.version).then(function(s){
157
+ return Date.now() > s.mtime.getTime() + cachetime * 1000;
158
+ });
159
+ }
160
+ return false;
161
+ }).then(function(isDirty){
162
+ if (!isDirty) {
163
+ return lderror.reject(998);
164
+ }
165
+ });
166
+ }).then(function(){
167
+ return this$._fetchRealVersion(params).then(function(remoteInfo){
168
+ return Promise.resolve().then(function(){
169
+ if (force || versionType === 'specific') {
170
+ return remoteInfo;
171
+ }
172
+ return fs.exists(path.version).then(function(isExisted){
173
+ if (!isExisted) {
174
+ return;
175
+ }
176
+ return fs.readFile(path.version).then(function(r){
177
+ var now;
178
+ if (remoteInfo.version > JSON.parse(r).version) {
179
+ return;
180
+ }
181
+ now = new Date();
182
+ fs.utimes(path.version, now, now);
183
+ return lderror.reject(998);
184
+ });
185
+ });
186
+ }).then(function(){
187
+ return fs.remove(path.base.version);
188
+ }).then(function(){
189
+ return fs.ensureDir(path.base.version);
190
+ }).then(function(){
191
+ return fs.writeFile(path.version, JSON.stringify(remoteInfo));
192
+ }).then(function(){
193
+ return remoteInfo;
194
+ });
195
+ });
196
+ }).then(function(remoteInfo){
197
+ return this$._fetchBundleFile(import$({
198
+ remoteInfo: remoteInfo
199
+ }, params));
200
+ });
201
+ }, ref$);
202
+ if (typeof module != 'undefined' && module !== null) {
203
+ module.exports = provider;
204
+ } else if (typeof window != 'undefined' && window !== null) {
205
+ window.registry = provider;
206
+ }
207
+ function import$(obj, src){
208
+ var own = {}.hasOwnProperty;
209
+ for (var key in src) if (own.call(src, key)) obj[key] = src[key];
210
+ return obj;
211
+ }
212
+ }).call(this);
@@ -0,0 +1,88 @@
1
+ (function(){
2
+ var nodeFetch, lderror, provider, unbox, getToken, pvd;
3
+ nodeFetch = require('node-fetch');
4
+ lderror = require('lderror');
5
+ provider = require('../provider');
6
+ unbox = require('../unbox');
7
+ getToken = function(arg$){
8
+ var name, opt, ret, scope, token;
9
+ name = arg$.name, opt = arg$.opt;
10
+ ret = /^@([^/]+)\/(.+)$/.exec(name);
11
+ scope = ret ? ret[1] : null;
12
+ return token = !opt
13
+ ? null
14
+ : (opt.scope || (opt.scope = {}))[scope] || (opt.repo || (opt.repo = {}))[name] || opt.token || null;
15
+ };
16
+ pvd = new provider({
17
+ name: 'github',
18
+ fetchRealVersion: function(arg$){
19
+ var root, path, name, cachetime, version, versionType, force, headers, token, release, jsonurl;
20
+ root = arg$.root, path = arg$.path, name = arg$.name, cachetime = arg$.cachetime, version = arg$.version, versionType = arg$.versionType, force = arg$.force;
21
+ headers = (token = getToken({
22
+ name: name,
23
+ opt: this._opt
24
+ }))
25
+ ? {
26
+ "Authorization": "token " + token
27
+ }
28
+ : {};
29
+ release = versionType === 'latest'
30
+ ? "releases/latest"
31
+ : "releases/tags/v" + version;
32
+ jsonurl = "https://api.github.com/repos/" + name.replace('@', '') + "/" + release;
33
+ return nodeFetch(jsonurl, {
34
+ method: 'GET',
35
+ headers: headers
36
+ }).then(function(r){
37
+ if (r.status !== 200) {
38
+ return lderror.reject(404);
39
+ } else {
40
+ return r.json();
41
+ }
42
+ }).then(function(r){
43
+ return {
44
+ version: (r.tag_name || '').replace(/^v/, ''),
45
+ url: r.tarball_url
46
+ };
47
+ });
48
+ },
49
+ fetchBundleFile: function(arg$){
50
+ var root, path, name, cachetime, version, remoteInfo, versionType, force, headers, token;
51
+ root = arg$.root, path = arg$.path, name = arg$.name, cachetime = arg$.cachetime, version = arg$.version, remoteInfo = arg$.remoteInfo, versionType = arg$.versionType, force = arg$.force;
52
+ headers = (token = getToken({
53
+ name: name,
54
+ opt: this._opt
55
+ }))
56
+ ? {
57
+ "Authorization": "token " + token
58
+ }
59
+ : {};
60
+ return nodeFetch(remoteInfo.url, {
61
+ method: 'GET',
62
+ headers: headers
63
+ }).then(function(r){
64
+ return r.buffer();
65
+ }).then(function(buf){
66
+ return unbox.untar({
67
+ path: path,
68
+ buf: buf
69
+ });
70
+ });
71
+ }
72
+ });
73
+ module.exports = pvd;
74
+ /*
75
+ params =
76
+ root: 'lib'
77
+ name: 'some-repo'
78
+ version: 'latest'
79
+ cachetime: 1
80
+ opt: {token: "some-token"}
81
+
82
+ pvd.fetch-module params
83
+ .catch (e) ->
84
+ if !(lderror.id(e) in [404 998]) => return Promise.reject e
85
+ else console.log "return code: ", lderror.id(e)
86
+ .then -> console.log \done.
87
+ */
88
+ }).call(this);
@@ -0,0 +1,9 @@
1
+ (function(){
2
+ var github, npm;
3
+ github = require('./github');
4
+ npm = require('./npm');
5
+ module.exports = {
6
+ github: github,
7
+ npm: npm
8
+ };
9
+ }).call(this);
@@ -0,0 +1,68 @@
1
+ (function(){
2
+ var nodeFetch, lderror, provider, unbox, pvd;
3
+ nodeFetch = require('node-fetch');
4
+ lderror = require('lderror');
5
+ provider = require('../provider');
6
+ unbox = require('../unbox');
7
+ pvd = new provider({
8
+ name: 'github',
9
+ fetchRealVersion: function(arg$){
10
+ var root, path, name, cachetime, version, versionType, force, opt, jsonurl;
11
+ root = arg$.root, path = arg$.path, name = arg$.name, cachetime = arg$.cachetime, version = arg$.version, versionType = arg$.versionType, force = arg$.force, opt = arg$.opt;
12
+ jsonurl = "https://registry.npmjs.org/" + name;
13
+ return nodeFetch(jsonurl, {
14
+ method: 'GET'
15
+ }).then(function(r){
16
+ if (r.status !== 200) {
17
+ return lderror.reject(404);
18
+ } else {
19
+ return r.json();
20
+ }
21
+ }).then(function(r){
22
+ var v, url, ref$, ref1$;
23
+ v = versionType === 'latest' ? r["dist-tags"].latest : version;
24
+ if (!(url = ((ref$ = (ref1$ = r.versions)[v] || (ref1$[v] = {})).dist || (ref$.dist = {})).tarball)) {
25
+ return lderror.reject(404);
26
+ }
27
+ return {
28
+ version: v,
29
+ url: url
30
+ };
31
+ });
32
+ },
33
+ fetchBundleFile: function(arg$){
34
+ var root, path, name, cachetime, version, remoteInfo, versionType, force, opt, headers;
35
+ root = arg$.root, path = arg$.path, name = arg$.name, cachetime = arg$.cachetime, version = arg$.version, remoteInfo = arg$.remoteInfo, versionType = arg$.versionType, force = arg$.force, opt = arg$.opt;
36
+ headers = (opt || {}).token
37
+ ? {
38
+ "Authorization": "token " + opt.token
39
+ }
40
+ : {};
41
+ return nodeFetch(remoteInfo.url, {
42
+ method: 'GET',
43
+ headers: headers
44
+ }).then(function(r){
45
+ return r.buffer();
46
+ }).then(function(buf){
47
+ return unbox.untar({
48
+ path: path,
49
+ buf: buf
50
+ });
51
+ });
52
+ }
53
+ });
54
+ module.exports = pvd;
55
+ /*
56
+ params =
57
+ root: 'npmlib'
58
+ name: 'ldcover'
59
+ version: 'latest'
60
+ cachetime: 60 * 60
61
+
62
+ pvd.fetch-module params
63
+ .catch (e) ->
64
+ if !(lderror.id(e) in [404 998]) => return Promise.reject e
65
+ else console.log "return code: ", lderror.id(e)
66
+ .then -> console.log \done.
67
+ */
68
+ }).call(this);