├── LICENSE ├── README.md ├── bin.js ├── example.js ├── index.js └── package.json /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Mathias Buus 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 | # cat 2 | 3 | cat will read the contents of an url. it's available through npm 4 | 5 | npm install cat 6 | 7 | it will read your files 8 | 9 | ```js 10 | var cat = require('cat'); 11 | 12 | cat('myfile.txt', console.log); // reads the file as utf-8 and returns it output 13 | cat('file://myfile.txt', console.log); // same as above 14 | ``` 15 | 16 | and your `http` / `https` urls 17 | 18 | ```js 19 | cat('http://google.com', console.log); // cat also follows any redirects 20 | cat('https://github.com', console.log); // and cat read https 21 | cat('http://fail.google.com', console.log); // if a status code != 2xx is received it will 22 | // call the callback with an error. 23 | 24 | ``` -------------------------------------------------------------------------------- /bin.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var fs = require('fs') 4 | 5 | var input = process.argv.slice(2) 6 | 7 | var loop = function() { 8 | if (!input.length) return 9 | var next = input.shift() 10 | var s = next === '-' ? process.stdin : fs.createReadStream(next) 11 | s.on('end', loop).pipe(process.stdout) 12 | } 13 | 14 | loop() -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | var cat = require('./index'); 2 | 3 | cat(__filename, console.log); 4 | 5 | cat('https://raw.github.com/mafintosh/cat/master/example.js', console.log); -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | var parse = require('url').parse; 3 | 4 | var catter = function(lib) { 5 | var cat = function(url, callback) { 6 | if (typeof url === 'string') { 7 | url = parse(url); 8 | } 9 | lib.get({host:url.hostname, port:url.port, path:url.pathname}, function(response) { 10 | if (/3\d\d/.test(response.statusCode) && response.headers.location) { 11 | cat(parse(response.headers.location), callback); 12 | return; 13 | } 14 | if (!(/2\d\d/).test(response.statusCode)) { 15 | callback(new Error('non 2xx status code: ' + response.statusCode)); 16 | return; 17 | } 18 | var buffer = ''; 19 | 20 | response.setEncoding('utf-8'); 21 | response.on('data', function(data) { 22 | buffer += data; 23 | }); 24 | response.on('close', function() { 25 | callback(new Error('unexpected close of response')); 26 | }); 27 | response.on('end', function() { 28 | callback(null, buffer); 29 | }); 30 | }).on('error', callback); 31 | }; 32 | return cat; 33 | }; 34 | 35 | var http = catter(require('http')); 36 | var https = catter(require('https')); 37 | 38 | module.exports = function(location, callback) { 39 | var protocol = (location.match(/^(\w+):\/\//) || [])[1] || 'file'; 40 | 41 | if (protocol === 'file') { 42 | fs.readFile(location.replace(/^(file:\/\/localhost|file:\/\/)/, ''), 'utf-8', callback); 43 | return; 44 | } 45 | if (protocol === 'http') { 46 | http(location, callback); 47 | return; 48 | } 49 | if (protocol === 'https') { 50 | https(location, callback); 51 | return; 52 | } 53 | throw new Error('protocol '+protocol+' currently not supported :('); 54 | }; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cat", 3 | "description": "cat will read the contents of an url", 4 | "keywords": [ 5 | "cat", 6 | "util", 7 | "request" 8 | ], 9 | "version": "0.2.0", 10 | "homepage": "https://github.com/mafintosh/cat", 11 | "author": { 12 | "name": "Mathias Buus Madsen", 13 | "email": "mathiasbuus@gmail.com" 14 | }, 15 | "bin": { 16 | "cat": "bin.js" 17 | } 18 | } 19 | --------------------------------------------------------------------------------