├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── package.json ├── README.md ├── LICENSE ├── Gulpfile.js └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | /dist -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 0.10 4 | before_script: 5 | - "npm install -g gulp" -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## Changelog 2 | 3 | ### v0.1.0 (2014-06-30) 4 | 5 | First release. 6 | 7 | ### v0.2.0 (2014-07-01) 8 | 9 | Fix streaming pipe. 10 | 11 | Add tests for streaming pipe. 12 | 13 | ### v0.2.1 (2014-07-18) 14 | 15 | Add option `strictSSL` (thank you [@Magomogo](https://github.com/Magomogo)) 16 | 17 | ### v0.3.0 (2014-09-02) 18 | 19 | Pass through [request](https://github.com/mikeal/request) options to make it flexible. 20 | 21 | ### v0.4.0 (2015-08-14) 22 | 23 | Put `request` options in a single `requestOptions`. 24 | 25 | ### v0.4.1 (2016-02-26) 26 | 27 | Improve response code check. -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-remote-src", 3 | "description": "Remote gulp.src", 4 | "version": "0.4.4", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "gulp test" 8 | }, 9 | "homepage": "https://github.com/ddliu/gulp-remote-src", 10 | "author": "dong ", 11 | "repository": { 12 | "type": "git", 13 | "url": "https://github.com/ddliu/gulp-remote-src.git" 14 | }, 15 | "bugs": { 16 | "url": "https://github.com/ddliu/gulp-remote-src/issues" 17 | }, 18 | "licenses": [ 19 | { 20 | "type": "MIT", 21 | "url": "https://github.com/ddliu/gulp-remote-src/blob/master/LICENSE" 22 | } 23 | ], 24 | "license": "MIT", 25 | "devDependencies": { 26 | "gulp": "^3.9.1", 27 | "gulp-uglify": "~2.0.1", 28 | "gulp-clean": "~0.3.1" 29 | }, 30 | "dependencies": { 31 | "event-stream": "3.3.4", 32 | "node.extend": "~1.1.2", 33 | "request": "^2.88.0", 34 | "through2": "~2.0.3", 35 | "vinyl": "~2.0.1" 36 | }, 37 | "keywords": [ 38 | "gulpplugin", 39 | "remote", 40 | "src" 41 | ] 42 | } 43 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gulp-remote-src 2 | 3 | [![Build Status](https://travis-ci.org/ddliu/gulp-remote-src.png)](https://travis-ci.org/ddliu/gulp-remote-src) 4 | 5 | Remote `gulp.src`. 6 | 7 | ## Installation 8 | 9 | Install package with NPM and add it to your development dependencies: 10 | 11 | npm install --save-dev gulp-remote-src 12 | 13 | ## Usage 14 | 15 | ```js 16 | var gulp = require('gulp'); 17 | var uglify = require('gulp-uglify'); 18 | var remoteSrc = require('gulp-remote-src'); 19 | 20 | gulp.task('remote', function() { 21 | 22 | remoteSrc(['app.js', 'jquery.js'], { 23 | base: 'http://myapps.com/assets/' 24 | }) 25 | .pipe(uglify()) 26 | .pipe(gulp.dest('./dist/')); 27 | }) 28 | ``` 29 | 30 | ## Options 31 | 32 | - `base` 33 | 34 | Url base. 35 | 36 | If you want to use absolute urls instead of relative ones, set this to 37 | `null`. When not configured, `/` is assumed. 38 | 39 | - `buffer` (default is true) 40 | 41 | Pipe out files as buffer or as stream. Note that some plugins do not support streaming. 42 | 43 | - `requestOptions` 44 | 45 | Options to be passed to [request](https://github.com/mikeal/request) 46 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | MIT License 3 | =========== 4 | 5 | Copyright (c) 2014 Dong 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a 8 | copy of this software and associated documentation files (the "Software"), 9 | to deal in the Software without restriction, including without limitation 10 | the rights to use, copy, modify, merge, publish, distribute, sublicense, 11 | and/or sell copies of the Software, and to permit persons to whom the 12 | Software is furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 | DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /Gulpfile.js: -------------------------------------------------------------------------------- 1 | var remoteSrc = require('./'); 2 | var gulp = require('gulp'); 3 | var uglify = require('gulp-uglify'); 4 | var clean = require('gulp-clean'); 5 | 6 | var FILES = [ 7 | "src/node.js", 8 | "lib/_debugger.js", 9 | "lib/_linklist.js", 10 | "lib/_stream_duplex.js", 11 | "lib/_stream_passthrough.js", 12 | "lib/_stream_readable.js", 13 | "lib/_stream_transform.js", 14 | "lib/_stream_writable.js", 15 | "lib/assert.js", 16 | "lib/buffer.js", 17 | "lib/child_process.js", 18 | "lib/cluster.js", 19 | "lib/console.js", 20 | "lib/constants.js", 21 | "lib/crypto.js", 22 | "lib/dgram.js", 23 | "lib/dns.js", 24 | "lib/domain.js", 25 | "lib/events.js", 26 | "lib/freelist.js", 27 | "lib/fs.js", 28 | "lib/http.js", 29 | "lib/https.js", 30 | "lib/module.js", 31 | "lib/net.js", 32 | "lib/os.js", 33 | "lib/path.js", 34 | "lib/punycode.js", 35 | "lib/querystring.js", 36 | "lib/readline.js", 37 | "lib/repl.js", 38 | "lib/stream.js", 39 | "lib/string_decoder.js", 40 | "lib/sys.js", 41 | "lib/timers.js", 42 | "lib/tls.js", 43 | "lib/tty.js", 44 | "lib/url.js", 45 | "lib/util.js", 46 | "lib/vm.js", 47 | "lib/zlib.js" 48 | ]; 49 | 50 | var URL = "https://raw.githubusercontent.com/joyent/node/v0.10.29/" 51 | 52 | gulp.task('clean', function() { 53 | return gulp.src('dist', {read: false}) 54 | .pipe(clean()); 55 | }); 56 | 57 | gulp.task('nostream', ['clean'], function() { 58 | return remoteSrc(FILES, { 59 | base: URL 60 | }) 61 | .pipe(uglify()) 62 | .pipe(gulp.dest('dist/nostream')); 63 | }); 64 | 65 | gulp.task('stream', ['clean'], function() { 66 | return remoteSrc(FILES, { 67 | buffer: false, 68 | base: URL 69 | }) 70 | .pipe(gulp.dest('dist/stream')); 71 | }); 72 | 73 | gulp.task('self-signed-ssl', ['clean'], function() { 74 | return remoteSrc(['index.html'], { 75 | strictSSL: false, 76 | base: 'https://example.com/' 77 | }) 78 | .pipe(gulp.dest('dist/strictSSL')); 79 | }); 80 | 81 | // run this task alone to test timeout 82 | gulp.task('timeout', ['clean'], function() { 83 | return remoteSrc(FILES, { 84 | base: URL, 85 | timeout: 1 86 | }) 87 | .pipe(gulp.dest('dist/timeout')); 88 | }); 89 | 90 | gulp.task('test', ['stream', 'nostream', 'self-signed-ssl']); -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | var es = require('event-stream'); 3 | var request = require('request'); 4 | var File = require('vinyl'); 5 | var through2 = require('through2'); 6 | var extend = require('node.extend'); 7 | 8 | module.exports = function (urls, options) { 9 | if (options === undefined) { 10 | options = {}; 11 | } 12 | 13 | if (typeof options.base !== 'string' && options.base !== null) { 14 | options.base = '/'; 15 | } 16 | 17 | if (typeof options.buffer !== 'boolean') { 18 | options.buffer = true; 19 | } 20 | 21 | if (!util.isArray(urls)) { 22 | urls = [urls]; 23 | } 24 | 25 | var allowedRequestOptions = ['qs', 'headers', 'auth', 'followRedirect', 'followAllRedirects', 'maxRedirects', 'timeout', 'proxy', 26 | 'strictSSL', 'aws', 'gzip']; 27 | 28 | var requestBaseOptions = {}; 29 | for (var i = allowedRequestOptions.length - 1; i >= 0; i--) { 30 | var k = allowedRequestOptions[i]; 31 | if (k in options) { 32 | requestBaseOptions[k] = options[k]; 33 | } 34 | } 35 | 36 | if (options.requestOptions) { 37 | for (var k in options.requestOptions) { 38 | requestBaseOptions[k] = options.requestOptions[k]; 39 | } 40 | } 41 | 42 | return es.readArray(urls).pipe(es.map(function(data, cb) { 43 | var url = [options.base, data].join(''), requestOptions = extend({url: url}, requestBaseOptions); 44 | 45 | if (!options.buffer) { 46 | var file = new File({ 47 | cwd: '/', 48 | base: options.base, 49 | path: url, 50 | // request must be piped out once created, or we'll get this error: "You cannot pipe after data has been emitted from the response." 51 | contents: request(requestOptions).pipe(through2()) 52 | }); 53 | 54 | cb(null, file); 55 | } else { 56 | // set encoding to `null` to return the body as buffer 57 | requestOptions.encoding = null; 58 | 59 | request(requestOptions, function (error, response, body) { 60 | if (!error && (response.statusCode >= 200 && response.statusCode < 300)) { 61 | var file = new File({ 62 | cwd: '/', 63 | base: options.base, 64 | path: url, 65 | contents: body 66 | }); 67 | cb(null, file); 68 | } else { 69 | if (!error) { 70 | error = new Error('Request ' + url + ' failed with status code:' + response.statusCode); 71 | } 72 | cb(error); 73 | } 74 | }); 75 | } 76 | })); 77 | }; 78 | --------------------------------------------------------------------------------