From f9436f5e5863d39f180032687447d9dbe7721dbb Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Thu, 27 Mar 2014 23:40:35 +0100 Subject: [PATCH] init --- .editorconfig | 16 ++++++++++++++++ .gitattributes | 1 + .gitignore | 1 + .jshintrc | 19 +++++++++++++++++++ .travis.yml | 3 +++ index.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ package.json | 36 ++++++++++++++++++++++++++++++++++++ readme.md | 29 +++++++++++++++++++++++++++++ test.js | 27 +++++++++++++++++++++++++++ 9 files changed, 176 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 .jshintrc create mode 100644 .travis.yml create mode 100644 index.js create mode 100644 package.json create mode 100644 readme.md create mode 100644 test.js diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..8311fe1 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,16 @@ +# editorconfig.org +root = true + +[*] +indent_style = tab +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[package.json] +indent_style = space +indent_size = 2 + +[*.md] +trim_trailing_whitespace = false diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..fcadb2c --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text eol=lf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..39a8579 --- /dev/null +++ b/.jshintrc @@ -0,0 +1,19 @@ +{ + "node": true, + "esnext": true, + "bitwise": true, + "camelcase": true, + "curly": true, + "eqeqeq": true, + "immed": true, + "indent": 4, + "newcap": true, + "noarg": true, + "quotmark": "single", + "regexp": true, + "undef": true, + "unused": true, + "strict": true, + "trailing": true, + "smarttabs": true +} diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..244b7e8 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,3 @@ +language: node_js +node_js: + - '0.10' diff --git a/index.js b/index.js new file mode 100644 index 0000000..2c3c451 --- /dev/null +++ b/index.js @@ -0,0 +1,44 @@ +'use strict'; +var urlLib = require('url'); +var http = require('http'); +var https = require('https'); + +module.exports = function (url, cb) { + var redirectCount = 0; + + var get = function (url, cb) { + var fn = urlLib.parse(url).protocol === 'https:' ? https : http; + + fn.get(url, function (res) { + var ret = ''; + + // redirect + if (res.statusCode < 400 && res.statusCode >= 300 && res.headers.location) { + if (++redirectCount > 10) { + cb(new Error('Redirected 10 times. Aborting.')); + return; + } + + get(urlLib.resolve(url, res.headers.location), cb); + return; + } + + if (res.statusCode !== 200) { + cb(res.statusCode); + return; + } + + res.setEncoding('utf8'); + + res.on('data', function (data) { + ret += data; + }); + + res.on('end', function () { + cb(null, ret); + }); + }).on('error', cb); + }; + + get(url, cb); +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..79e8fc7 --- /dev/null +++ b/package.json @@ -0,0 +1,36 @@ +{ + "name": "got", + "version": "0.0.0", + "description": "Simplified HTTP/HTTPS GET requests", + "license": "MIT", + "repository": "sindresorhus/got", + "author": { + "name": "Sindre Sorhus", + "email": "sindresorhus@gmail.com", + "url": "http://sindresorhus.com" + }, + "engines": { + "node": ">=0.10.0" + }, + "scripts": { + "test": "mocha" + }, + "files": [ + "index.js" + ], + "keywords": [ + "http", + "https", + "get", + "got", + "url", + "uri", + "request", + "util", + "utility", + "simple" + ], + "devDependencies": { + "mocha": "*" + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..3e4d659 --- /dev/null +++ b/readme.md @@ -0,0 +1,29 @@ +# got [![Build Status](https://travis-ci.org/sindresorhus/got.svg?branch=master)](https://travis-ci.org/sindresorhus/got) + +> Simplified HTTP/HTTPS GET requests + +Follows redirects. Not intended to be feature-rich. Use [request](https://github.com/mikeal/request) if you need something more. + + +## Install + +```bash +$ npm install --save got +``` + + +## Usage + +```js +var got = require('got'); + +got('http://todomvc.com', function (err, data) { + console.log(data); + //=> ... +}); +``` + + +## License + +[MIT](http://opensource.org/licenses/MIT) © [Sindre Sorhus](http://sindresorhus.com) diff --git a/test.js b/test.js new file mode 100644 index 0000000..846923d --- /dev/null +++ b/test.js @@ -0,0 +1,27 @@ +'use strict'; +var assert = require('assert'); +var got = require('./index'); + +it('should request', function (done) { + got('http://google.com', function (err, data) { + if (err) { + console.error(err); + assert(false); + return; + } + + assert(/google/.test(data)); + done(); + }); + + got('https://google.com', function (err, data) { + if (err) { + console.error(err); + assert(false); + return; + } + + assert(/google/.test(data)); + done(); + }); +});