├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── example.js ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | npm-debug.log 3 | ipfs.tar.gz 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | 3 | node_js: 4 | - 6 5 | - 8 6 | - 10 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 Lars-Magnus Skog 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # github-archive-stream 2 | 3 | > Streams a `.tar.gz` (or `.zip`) archive from GitHub using the [archive link api](https://developer.github.com/v3/repos/contents/#get-archive-link). It makes two http requests since the first is _always_ a `30x` redirect response. 4 | 5 | [![npm](https://img.shields.io/npm/v/github-archive-stream.svg)](https://www.npmjs.com/package/github-archive-stream) 6 | ![Node version](https://img.shields.io/node/v/github-archive-stream.svg) 7 | [![build status](http://img.shields.io/travis/ralphtheninja/github-archive-stream.svg?style=flat)](http://travis-ci.org/ralphtheninja/github-archive-stream) 8 | [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) 9 | 10 | ## Install 11 | 12 | ``` 13 | $ npm i github-archive-stream -S 14 | ``` 15 | 16 | ## Usage 17 | 18 | Stream latest master branch of `ipfs/go-ipfs` to the file `'ipfs.tar.gz'`. 19 | 20 | ```js 21 | const archive = require('github-archive-stream') 22 | archive('ipfs/go-ipfs').pipe(require('fs').createWriteStream('ipfs.tar.gz')) 23 | ``` 24 | 25 | ## API 26 | 27 | ### `const stream = archive(repo|opts)` 28 | 29 | Create an archive stream. 30 | 31 | Parameter can either be a string or an object. Use the string version to stream an open source archive. For private repositories you need to provide the `opts.auth` property, see below. 32 | 33 | Options takes the following properties: 34 | 35 | ```js 36 | { 37 | repo: 'user/repo', // github repository, required 38 | auth: { 39 | user: 'username', // github user, required if private repo 40 | token: 'agithubtoken' // github token, required if private repo 41 | }, 42 | ref: '315ae99', // git ref, defaults to 'master' 43 | format: 'zipball' // archive format, defaults to 'tarball' 44 | } 45 | ``` 46 | 47 | ## License 48 | 49 | MIT 50 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | var archive = require('./') 2 | archive('ipfs/go-ipfs').pipe(require('fs').createWriteStream('ipfs.tar.gz')) 3 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var hyperquest = require('hyperquest') 2 | var through = require('through2') 3 | var assert = require('assert') 4 | 5 | function archiveStream (opts) { 6 | if (typeof opts === 'string') { 7 | opts = { repo: opts } 8 | } 9 | 10 | var repo = opts.repo 11 | assert.strictEqual(typeof repo, 'string', '.repo required') 12 | 13 | var format = opts.format || 'tarball' 14 | var ref = opts.ref || 'master' 15 | 16 | var url = [ 'https://api.github.com/repos', repo, format, ref ].join('/') 17 | var options = { 18 | headers: { 19 | 'User-Agent': 'github-archive-stream' 20 | } 21 | } 22 | 23 | if (opts.auth) { 24 | assert.strictEqual(typeof opts.auth.user, 'string', '.user required') 25 | assert.strictEqual(typeof opts.auth.token, 'string', '.token required') 26 | options.auth = opts.auth.user + ':' + opts.auth.token 27 | } 28 | 29 | var pass = through() 30 | 31 | var req = hyperquest.get(url, options) 32 | req.on('response', function (res) { 33 | var redirect = isRedirect(req.request, res) 34 | if (typeof redirect !== 'string') { 35 | return pass.emit('error', new Error( 36 | 'Expected redirect url. Error code: ' + res.statusCode 37 | )) 38 | } 39 | hyperquest.get(redirect, options).pipe(pass) 40 | }) 41 | 42 | return pass 43 | } 44 | 45 | function isRedirect (req, res) { 46 | var codes = [ 301, 302, 307, 308 ] 47 | return (req.method === 'GET' && 48 | codes.indexOf(res.statusCode) !== -1 && 49 | res.headers.location) 50 | } 51 | 52 | module.exports = archiveStream 53 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "github-archive-stream", 3 | "version": "3.0.1", 4 | "description": "Stream an archive from a github ref", 5 | "main": "index.js", 6 | "dependencies": { 7 | "hyperquest": "^2.1.2", 8 | "through2": "^2.0.1" 9 | }, 10 | "devDependencies": { 11 | "standard": "^12.0.0" 12 | }, 13 | "scripts": { 14 | "test": "standard" 15 | }, 16 | "keywords": [ 17 | "github", 18 | "archive", 19 | "devops", 20 | "tarball", 21 | ".tar.gz", 22 | "zip", 23 | "stream" 24 | ], 25 | "author": "Lars-Magnus Skog ", 26 | "repository": { 27 | "type": "git", 28 | "url": "git://github.com/ralphtheninja/github-archive-stream" 29 | }, 30 | "engines": { 31 | "node": ">=6.0.0" 32 | }, 33 | "license": "MIT" 34 | } 35 | --------------------------------------------------------------------------------