Skip to content

Commit 6c9c123

Browse files
committed
init
0 parents  commit 6c9c123

11 files changed

+594
-0
lines changed

.gitignore

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Compiled source #
2+
###################
3+
*.com
4+
*.class
5+
*.dll
6+
*.exe
7+
*.o
8+
*.so
9+
10+
# Packages #
11+
############
12+
# it's better to unpack these files and commit the raw source
13+
# git has its own built in compression methods
14+
*.7z
15+
*.dmg
16+
*.gz
17+
*.iso
18+
*.jar
19+
*.rar
20+
*.tar
21+
*.zip
22+
23+
# Logs and databases #
24+
######################
25+
*.log
26+
*.sql
27+
*.sqlite
28+
29+
# OS generated files #
30+
######################
31+
.DS_Store*
32+
ehthumbs.db
33+
Icon?
34+
Thumbs.db
35+
36+
# Node.js #
37+
###########
38+
lib-cov
39+
*.seed
40+
*.log
41+
*.csv
42+
*.dat
43+
*.out
44+
*.pid
45+
*.gz
46+
47+
pids
48+
logs
49+
results
50+
51+
node_modules
52+
npm-debug.log
53+
54+
# Git #
55+
#######
56+
*.orig
57+
*.BASE.*
58+
*.BACKUP.*
59+
*.LOCAL.*
60+
*.REMOTE.*
61+
62+
# Components #
63+
##############
64+
65+
/build
66+
/components
67+
/landing/components
68+
.elasticbeanstalk/

.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test

.travis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_js:
2+
- "0.11"
3+
language: node_js

Makefile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
BIN = ./node_modules/.bin/
2+
3+
test tests:
4+
@${BIN}mocha \
5+
--require should \
6+
--reporter spec \
7+
--harmony-generators
8+
9+
.PHONY: test tests

README.md

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Remotes.js [![Build Status](https://travis-ci.org/component/remotes.js.png)](https://travis-ci.org/component/remotes.js)
2+
3+
The goal of this repo is to normalize different remote endpoints for Component into a unified API. Currently, only GitHub is supported.
4+
5+
Example:
6+
7+
```js
8+
var remotes = require('remotes')
9+
var github = new remotes.GitHub({
10+
auth: 'jonathanong:password'
11+
})
12+
13+
co(function* () {
14+
var versions = yield* github.getVersions('component/emitter')
15+
// do stuff with the versions
16+
})
17+
```
18+
19+
## new Remotes()
20+
21+
NOT IMPLEMENTED!
22+
23+
Right now in component, we set `remotes: []`. Ideally, this repo will allow you to create a set of remotes, then query this set. Something like:
24+
25+
```js
26+
var remotes = require('remotes')
27+
28+
var controller = remotes()
29+
controller.use(remotes.github)
30+
controller.use(remotes.bitbucket)
31+
32+
// then actually querying the remotes will check each, one by one
33+
co(function* () {
34+
var versions = yield* controller.getVersions('component/emitter')
35+
// do stuff with the versions
36+
})
37+
```
38+
39+
## remotes.Remote
40+
41+
A base Constructor to extend any additional remotes.
42+
43+
## remotes[remote]
44+
45+
Any already constructed remotes. The current remotes are:
46+
47+
- `remotes.github`
48+
49+
## Remote.extend(Child)
50+
51+
Extend a new `Remote` class with the current Remote. Example:
52+
53+
```js
54+
function GitHub(options) {
55+
options = options || {}
56+
Remote.call(this, options)
57+
58+
this.something = 'asdf'
59+
}
60+
61+
Remote.extend(GitHub)
62+
63+
Github.prototype.something = function () {
64+
65+
}
66+
67+
```
68+
69+
## var remote = new remote.Remote([options])
70+
71+
Creates a new remote instance. Some options are:
72+
73+
- `concurrency` <5> - maximum number of concurrent downloads per `.getFiles`
74+
75+
Other options are passed to [cogent](https://github.com/cojs/cogent#var-response--yield-requesturl-options), so this is where you set your `auth`, `proxy`, etc. on a per-remote basis.
76+
77+
## var versions[] = yield* remote.getVersions(repo)
78+
79+
Repo is of the form `<username>/<project>`. This will return all the semantically versioned releases in the repository. This will not normalize versions (i.e. strip leading `v`s). It will return an array of strings in descending versions.
80+
81+
## var json = yield* remote.getJSON(repo, ref)
82+
83+
Returns the `component.json` of a repo's reference.
84+
85+
## var tree = yield* remote.getTree(repo, ref)
86+
87+
Returns the list of files in the repository and reference. Will return a list of objects with properties:
88+
89+
- `path` - file path in the repo
90+
- `sha` - sha1 check sum
91+
- `fize` - file byte length
92+
93+
## var filename = yield* remote.getFile(repo, ref, obj, folder)
94+
95+
Downloads a file `obj` to a destination `folder` from a repository's reference. `obj` should be an object as returned from the `tree`. You can always just do `{path: ''}` if you don't care to download the tree.
96+
97+
Returns the abolute path of the resulting file.
98+
99+
## yield* remote.getFiles(repo, ref, objs, folder)
100+
101+
Downloads all the files in `objs` to the folder.
102+
103+
## License
104+
105+
The MIT License (MIT)
106+
107+
Copyright (c) 2014 Jonathan Ong [email protected]
108+
109+
Permission is hereby granted, free of charge, to any person obtaining a copy
110+
of this software and associated documentation files (the "Software"), to deal
111+
in the Software without restriction, including without limitation the rights
112+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
113+
copies of the Software, and to permit persons to whom the Software is
114+
furnished to do so, subject to the following conditions:
115+
116+
The above copyright notice and this permission notice shall be included in
117+
all copies or substantial portions of the Software.
118+
119+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
120+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
121+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
122+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
123+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
124+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
125+
THE SOFTWARE.

lib/bitbucket.js

Whitespace-only changes.

lib/github.js

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
var mkdirp = require('mkdirp')
2+
var semver = require('semver')
3+
var join = require('path').join
4+
5+
var Remote = require('./remote')
6+
7+
var GITHUB_USERNAME = process.env.GITHUB_USERNAME
8+
var GITHUB_PASSWORD = process.env.GITHUB_PASSWORD
9+
10+
module.exports = GitHub
11+
12+
function GitHub(options) {
13+
options = options || {}
14+
15+
if (!options.auth) {
16+
if (GITHUB_USERNAME && GITHUB_PASSWORD)
17+
options.auth = GITHUB_USERNAME + ':' + GITHUB_PASSWORD
18+
else
19+
this.emit('warning', {
20+
type: 'suggestion',
21+
message: 'you should add your github auth as <username>:<password> to avoid GitHub\'s API rate limit.'
22+
})
23+
}
24+
25+
Remote.call(this, options)
26+
}
27+
28+
Remote.extend(GitHub)
29+
30+
/**
31+
* Get all git tags that are valid semver versions.
32+
*
33+
* DOES NOT SOLVE REDIRECTS!
34+
*/
35+
36+
Remote.prototype.getVersions = function* (repo) {
37+
repo = repo.toLowerCase()
38+
if (repo in this.versions)
39+
return this.versions[repo]
40+
41+
var uri = 'https://api.github.com/repos/' + repo + '/tags'
42+
var res = yield* this.request(uri, true)
43+
if (res.statusCode === 400)
44+
return this.versions[repo] = null // return nothing on a 404
45+
if (res.statusCode !== 200) {
46+
var err = new Error('failed to get ' + repo + '\'s tags')
47+
err.res = res
48+
err.remote = 'github'
49+
throw err
50+
}
51+
52+
return this.versions[repo] = res.body
53+
.map(name)
54+
.filter(semver.valid)
55+
.sort(semver.rcompare)
56+
}
57+
58+
/**
59+
* Get a component and references's component.json.
60+
*/
61+
62+
Remote.prototype.getJSON = function* (repo, ref) {
63+
repo = repo.toLowerCase()
64+
var slug = repo + '#' + ref
65+
if (slug in this.components)
66+
return this.components[slug]
67+
68+
var uri = 'https://raw.github.com/' + repo + '/' + ref + '/component.json'
69+
var res = yield* this.request(uri, true)
70+
if (res.statusCode === 400)
71+
return this.components[slug] = null // return nothing on a 404
72+
if (res.statusCode !== 200) {
73+
var err = new Error('failed to get ' + repo + '\'s component.json')
74+
err.res = res
75+
err.remote = 'github'
76+
throw err
77+
}
78+
79+
// to do: sha1sum check
80+
return this.components[slug] = res.body
81+
}
82+
83+
/**
84+
* Get all the files in a component.
85+
* Should be a list of files with the following properties:
86+
*
87+
* - sha - sha1sum
88+
* - path
89+
* - size
90+
*
91+
* DOES NOT SOLVE REDIRECTS!
92+
*/
93+
94+
Remote.prototype.getTree = function* (repo, ref) {
95+
repo = repo.toLowerCase()
96+
var slug = repo + '#' + ref
97+
if (slug in this.tree)
98+
return this.tree[slug]
99+
100+
var uri = 'https://api.github.com/repos/' + repo + '/git/trees/' + ref
101+
var res = yield* this.request(uri, true)
102+
if (res.statusCode === 400)
103+
return this.tree[slug] = null
104+
if (res.statusCode !== 200) {
105+
var err = new Error('failed to get ' + repo + '\'s git tree')
106+
err.res = res
107+
err.remote = 'github'
108+
throw err
109+
}
110+
111+
return this.tree[slug] = res.body.tree
112+
}
113+
114+
/**
115+
* Download a single file to a destination folder and do a sha1sum check.
116+
*/
117+
118+
Remote.prototype.getFile = function* (repo, ref, obj, destination) {
119+
var uri = 'https://raw.github.com/' + repo + '/' + ref + '/' + obj.path
120+
var filename = join(destination, obj.path)
121+
yield function (done) {
122+
mkdirp(join(filename, '..'), done)
123+
}
124+
125+
var res = yield* this.request(uri, filename)
126+
if (res.statusCode !== 200) {
127+
var err = new Error('failed to download ' + repo + '\'s file ' + obj.path)
128+
err.res = res
129+
err.remote = 'github'
130+
err.destination = destination
131+
err.obj = obj
132+
err.filename = filename
133+
throw err
134+
}
135+
136+
// to do: sha1sum check
137+
138+
return filename
139+
}
140+
141+
function name(x) {
142+
return x.name
143+
}

lib/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = Remotes
2+
3+
function Remotes() {
4+
5+
}
6+
7+
Remotes.github =
8+
Remotes.Github =
9+
Remotes.GitHub = require('./github')

0 commit comments

Comments
 (0)