이세린

Trying to embed random puppy, img not working

Showing 80 changed files with 3914 additions and 0 deletions
'use strict';
module.exports = Error.captureStackTrace || function (error) {
var container = new Error();
Object.defineProperty(error, 'stack', {
configurable: true,
get: function getStack() {
var stack = container.stack;
Object.defineProperty(this, 'stack', {
value: stack
});
return stack;
}
});
};
The MIT License (MIT)
Copyright (c) Vsevolod Strukchinsky <floatdrop@gmail.com> (github.com/floatdrop)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
{
"name": "capture-stack-trace",
"version": "1.0.1",
"description": "Error.captureStackTrace ponyfill",
"license": "MIT",
"repository": "floatdrop/capture-stack-trace",
"author": {
"name": "Vsevolod Strukchinsky",
"email": "floatdrop@gmail.com",
"url": "github.com/floatdrop"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "mocha"
},
"files": [
"index.js"
],
"keywords": [
"Error",
"captureStackTrace"
],
"dependencies": {},
"devDependencies": {
"mocha": "*"
}
}
# capture-stack-trace [![Build Status](https://travis-ci.org/floatdrop/capture-stack-trace.svg?branch=master)](https://travis-ci.org/floatdrop/capture-stack-trace)
> Ponyfill for Error.captureStackTrace
## Install
```
$ npm install --save capture-stack-trace
```
## Usage
```js
var captureStackTrace = require('capture-stack-trace');
captureStackTrace({});
// => {stack: ...}
```
## API
### captureStackTrace(error)
#### error
*Required*
Type: `Object`
Target Object, that will recieve stack property.
## License
MIT © [Vsevolod Strukchinsky](http://github.com/floatdrop)
'use strict';
var captureStackTrace = require('capture-stack-trace');
function inherits(ctor, superCtor) {
ctor.super_ = superCtor;
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false,
writable: true,
configurable: true
}
});
}
module.exports = function createErrorClass(className, setup) {
if (typeof className !== 'string') {
throw new TypeError('Expected className to be a string');
}
if (/[^0-9a-zA-Z_$]/.test(className)) {
throw new Error('className contains invalid characters');
}
setup = setup || function (message) {
this.message = message;
};
var ErrorClass = function () {
Object.defineProperty(this, 'name', {
configurable: true,
value: className,
writable: true
});
captureStackTrace(this, this.constructor);
setup.apply(this, arguments);
};
inherits(ErrorClass, Error);
return ErrorClass;
};
The MIT License (MIT)
Copyright (c) Vsevolod Strukchinsky <floatdrop@gmail.com> (github.com/floatdrop)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
{
"name": "create-error-class",
"version": "3.0.2",
"description": "Create Error classes",
"license": "MIT",
"repository": "floatdrop/create-error-class",
"author": {
"name": "Vsevolod Strukchinsky",
"email": "floatdrop@gmail.com",
"url": "github.com/floatdrop"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "mocha"
},
"files": [
"index.js"
],
"keywords": [
""
],
"dependencies": {
"capture-stack-trace": "^1.0.0"
},
"devDependencies": {
"mocha": "*"
}
}
# create-error-class [![Build Status](https://travis-ci.org/floatdrop/create-error-class.svg?branch=master)](https://travis-ci.org/floatdrop/create-error-class)
> Create error class
## Install
```
$ npm install --save create-error-class
```
## Usage
```js
var createErrorClass = require('create-error-class');
var HTTPError = createErrorClass('HTTPError', function (props) {
this.message = 'Status code is ' + props.statusCode;
});
throw new HTTPError({statusCode: 404});
```
## API
### createErrorClass(className, [setup])
Return constructor of Errors with `className`.
#### className
*Required*
Type: `string`
Class name of Error Object. Should contain characters from `[0-9a-zA-Z_$]` range.
#### setup
Type: `function`
Setup function, that will be called after each Error object is created from constructor with context of Error object.
By default `setup` function sets `this.message` as first argument:
```js
var MyError = createErrorClass('MyError');
new MyError('Something gone wrong!').message; // => 'Something gone wrong!'
```
## License
MIT © [Vsevolod Strukchinsky](http://github.com/floatdrop)
The MIT License (MIT)
Copyright (c) 2016 David Frank
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
This diff is collapsed. Click to expand it.
"use strict";
// ref: https://github.com/tc39/proposal-global
var getGlobal = function () {
// the only reliable means to get the global object is
// `Function('return this')()`
// However, this causes CSP violations in Chrome apps.
if (typeof self !== 'undefined') { return self; }
if (typeof window !== 'undefined') { return window; }
if (typeof global !== 'undefined') { return global; }
throw new Error('unable to locate global object');
}
var global = getGlobal();
module.exports = exports = global.fetch;
// Needed for TypeScript and Webpack.
if (global.fetch) {
exports.default = global.fetch.bind(global);
}
exports.Headers = global.Headers;
exports.Request = global.Request;
exports.Response = global.Response;
\ No newline at end of file
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
{
"name": "node-fetch",
"version": "2.6.7",
"description": "A light-weight module that brings window.fetch to node.js",
"main": "lib/index.js",
"browser": "./browser.js",
"module": "lib/index.mjs",
"files": [
"lib/index.js",
"lib/index.mjs",
"lib/index.es.js",
"browser.js"
],
"engines": {
"node": "4.x || >=6.0.0"
},
"scripts": {
"build": "cross-env BABEL_ENV=rollup rollup -c",
"prepare": "npm run build",
"test": "cross-env BABEL_ENV=test mocha --require babel-register --throw-deprecation test/test.js",
"report": "cross-env BABEL_ENV=coverage nyc --reporter lcov --reporter text mocha -R spec test/test.js",
"coverage": "cross-env BABEL_ENV=coverage nyc --reporter json --reporter text mocha -R spec test/test.js && codecov -f coverage/coverage-final.json"
},
"repository": {
"type": "git",
"url": "https://github.com/bitinn/node-fetch.git"
},
"keywords": [
"fetch",
"http",
"promise"
],
"author": "David Frank",
"license": "MIT",
"bugs": {
"url": "https://github.com/bitinn/node-fetch/issues"
},
"homepage": "https://github.com/bitinn/node-fetch",
"dependencies": {
"whatwg-url": "^5.0.0"
},
"peerDependencies": {
"encoding": "^0.1.0"
},
"peerDependenciesMeta": {
"encoding": {
"optional": true
}
},
"devDependencies": {
"@ungap/url-search-params": "^0.1.2",
"abort-controller": "^1.1.0",
"abortcontroller-polyfill": "^1.3.0",
"babel-core": "^6.26.3",
"babel-plugin-istanbul": "^4.1.6",
"babel-preset-env": "^1.6.1",
"babel-register": "^6.16.3",
"chai": "^3.5.0",
"chai-as-promised": "^7.1.1",
"chai-iterator": "^1.1.1",
"chai-string": "~1.3.0",
"codecov": "3.3.0",
"cross-env": "^5.2.0",
"form-data": "^2.3.3",
"is-builtin-module": "^1.0.0",
"mocha": "^5.0.0",
"nyc": "11.9.0",
"parted": "^0.1.1",
"promise": "^8.0.3",
"resumer": "0.0.0",
"rollup": "^0.63.4",
"rollup-plugin-babel": "^3.0.7",
"string-to-arraybuffer": "^1.0.2",
"teeny-request": "3.7.0"
}
}
Copyright (c) 2013, Deoxxa Development
======================================
All rights reserved.
--------------------
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of Deoxxa Development nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY DEOXXA DEVELOPMENT ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL DEOXXA DEVELOPMENT BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# duplexer3 [![Build Status](https://travis-ci.org/floatdrop/duplexer3.svg?branch=master)](https://travis-ci.org/floatdrop/duplexer3) [![Coverage Status](https://coveralls.io/repos/floatdrop/duplexer3/badge.svg?branch=master&service=github)](https://coveralls.io/github/floatdrop/duplexer3?branch=master)
Like [duplexer2](https://github.com/deoxxa/duplexer2) but using Streams3 without readable-stream dependency
```javascript
var stream = require("stream");
var duplexer3 = require("duplexer3");
var writable = new stream.Writable({objectMode: true}),
readable = new stream.Readable({objectMode: true});
writable._write = function _write(input, encoding, done) {
if (readable.push(input)) {
return done();
} else {
readable.once("drain", done);
}
};
readable._read = function _read(n) {
// no-op
};
// simulate the readable thing closing after a bit
writable.once("finish", function() {
setTimeout(function() {
readable.push(null);
}, 500);
});
var duplex = duplexer3(writable, readable);
duplex.on("data", function(e) {
console.log("got data", JSON.stringify(e));
});
duplex.on("finish", function() {
console.log("got finish event");
});
duplex.on("end", function() {
console.log("got end event");
});
duplex.write("oh, hi there", function() {
console.log("finished writing");
});
duplex.end(function() {
console.log("finished ending");
});
```
```
got data "oh, hi there"
finished writing
got finish event
finished ending
got end event
```
## Overview
This is a reimplementation of [duplexer](https://www.npmjs.com/package/duplexer) using the
Streams3 API which is standard in Node as of v4. Everything largely
works the same.
## Installation
[Available via `npm`](https://docs.npmjs.com/cli/install):
```
$ npm i duplexer3
```
## API
### duplexer3
Creates a new `DuplexWrapper` object, which is the actual class that implements
most of the fun stuff. All that fun stuff is hidden. DON'T LOOK.
```javascript
duplexer3([options], writable, readable)
```
```javascript
const duplex = duplexer3(new stream.Writable(), new stream.Readable());
```
Arguments
* __options__ - an object specifying the regular `stream.Duplex` options, as
well as the properties described below.
* __writable__ - a writable stream
* __readable__ - a readable stream
Options
* __bubbleErrors__ - a boolean value that specifies whether to bubble errors
from the underlying readable/writable streams. Default is `true`.
## License
3-clause BSD. [A copy](./LICENSE) is included with the source.
## Contact
* GitHub ([deoxxa](http://github.com/deoxxa))
* Twitter ([@deoxxa](http://twitter.com/deoxxa))
* Email ([deoxxa@fknsrs.biz](mailto:deoxxa@fknsrs.biz))
"use strict";
var stream = require("stream");
function DuplexWrapper(options, writable, readable) {
if (typeof readable === "undefined") {
readable = writable;
writable = options;
options = null;
}
stream.Duplex.call(this, options);
if (typeof readable.read !== "function") {
readable = (new stream.Readable(options)).wrap(readable);
}
this._writable = writable;
this._readable = readable;
this._waiting = false;
var self = this;
writable.once("finish", function() {
self.end();
});
this.once("finish", function() {
writable.end();
});
readable.on("readable", function() {
if (self._waiting) {
self._waiting = false;
self._read();
}
});
readable.once("end", function() {
self.push(null);
});
if (!options || typeof options.bubbleErrors === "undefined" || options.bubbleErrors) {
writable.on("error", function(err) {
self.emit("error", err);
});
readable.on("error", function(err) {
self.emit("error", err);
});
}
}
DuplexWrapper.prototype = Object.create(stream.Duplex.prototype, {constructor: {value: DuplexWrapper}});
DuplexWrapper.prototype._write = function _write(input, encoding, done) {
this._writable.write(input, encoding, done);
};
DuplexWrapper.prototype._read = function _read() {
var buf;
var reads = 0;
while ((buf = this._readable.read()) !== null) {
this.push(buf);
reads++;
}
if (reads === 0) {
this._waiting = true;
}
};
module.exports = function duplex2(options, writable, readable) {
return new DuplexWrapper(options, writable, readable);
};
module.exports.DuplexWrapper = DuplexWrapper;
{
"name": "duplexer3",
"version": "0.1.4",
"description": "Like duplexer but using streams3",
"engine": {
"node": ">=4"
},
"files": [
"index.js"
],
"scripts": {
"test": "mocha -R tap"
},
"repository": "floatdrop/duplexer3",
"keywords": [
"duplex",
"duplexer",
"stream",
"stream3",
"join",
"combine"
],
"author": "Conrad Pankoff <deoxxa@fknsrs.biz> (http://www.fknsrs.biz/)",
"license": "BSD-3-Clause",
"devDependencies": {
"mocha": "^2.2.5"
}
}
The MIT License (MIT)
Copyright (c) 2014 Arnout Kazemier
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# EventEmitter3
[![Version npm](https://img.shields.io/npm/v/eventemitter3.svg?style=flat-square)](http://browsenpm.org/package/eventemitter3)[![Build Status](https://img.shields.io/travis/primus/eventemitter3/master.svg?style=flat-square)](https://travis-ci.org/primus/eventemitter3)[![Dependencies](https://img.shields.io/david/primus/eventemitter3.svg?style=flat-square)](https://david-dm.org/primus/eventemitter3)[![Coverage Status](https://img.shields.io/coveralls/primus/eventemitter3/master.svg?style=flat-square)](https://coveralls.io/r/primus/eventemitter3?branch=master)[![IRC channel](https://img.shields.io/badge/IRC-irc.freenode.net%23primus-00a8ff.svg?style=flat-square)](https://webchat.freenode.net/?channels=primus)
[![Sauce Test Status](https://saucelabs.com/browser-matrix/eventemitter3.svg)](https://saucelabs.com/u/eventemitter3)
EventEmitter3 is a high performance EventEmitter. It has been micro-optimized
for various of code paths making this, one of, if not the fastest EventEmitter
available for Node.js and browsers. The module is API compatible with the
EventEmitter that ships by default with Node.js but there are some slight
differences:
- Domain support has been removed.
- We do not `throw` an error when you emit an `error` event and nobody is
listening.
- The `newListener` event is removed as the use-cases for this functionality are
really just edge cases.
- No `setMaxListeners` and it's pointless memory leak warnings. If you want to
add `end` listeners you should be able to do that without modules complaining.
- No `listenerCount` function. Use `EE.listeners(event).length` instead.
- Support for custom context for events so there is no need to use `fn.bind`.
- `listeners` method can do existence checking instead of returning only arrays.
It's a drop in replacement for existing EventEmitters, but just faster. Free
performance, who wouldn't want that? The EventEmitter is written in EcmaScript 3
so it will work in the oldest browsers and node versions that you need to
support.
## Installation
```bash
$ npm install --save eventemitter3 # npm
$ component install primus/eventemitter3 # Component
$ bower install eventemitter3 # Bower
```
## Usage
After installation the only thing you need to do is require the module:
```js
var EventEmitter = require('eventemitter3');
```
And you're ready to create your own EventEmitter instances. For the API
documentation, please follow the official Node.js documentation:
http://nodejs.org/api/events.html
### Contextual emits
We've upgraded the API of the `EventEmitter.on`, `EventEmitter.once` and
`EventEmitter.removeListener` to accept an extra argument which is the `context`
or `this` value that should be set for the emitted events. This means you no
longer have the overhead of an event that required `fn.bind` in order to get a
custom `this` value.
```js
var EE = new EventEmitter()
, context = { foo: 'bar' };
function emitted() {
console.log(this === context); // true
}
EE.once('event-name', emitted, context);
EE.on('another-event', emitted, context);
EE.removeListener('another-event', emitted, context);
```
### Existence
To check if there is already a listener for a given event you can supply the
`listeners` method with an extra boolean argument. This will transform the
output from an array, to a boolean value which indicates if there are listeners
in place for the given event:
```js
var EE = new EventEmitter();
EE.once('event-name', function () {});
EE.on('another-event', function () {});
EE.listeners('event-name', true); // returns true
EE.listeners('unknown-name', true); // returns false
```
## License
[MIT](LICENSE)
'use strict';
var has = Object.prototype.hasOwnProperty;
//
// We store our EE objects in a plain object whose properties are event names.
// If `Object.create(null)` is not supported we prefix the event names with a
// `~` to make sure that the built-in object properties are not overridden or
// used as an attack vector.
// We also assume that `Object.create(null)` is available when the event name
// is an ES6 Symbol.
//
var prefix = typeof Object.create !== 'function' ? '~' : false;
/**
* Representation of a single EventEmitter function.
*
* @param {Function} fn Event handler to be called.
* @param {Mixed} context Context for function execution.
* @param {Boolean} [once=false] Only emit once
* @api private
*/
function EE(fn, context, once) {
this.fn = fn;
this.context = context;
this.once = once || false;
}
/**
* Minimal EventEmitter interface that is molded against the Node.js
* EventEmitter interface.
*
* @constructor
* @api public
*/
function EventEmitter() { /* Nothing to set */ }
/**
* Hold the assigned EventEmitters by name.
*
* @type {Object}
* @private
*/
EventEmitter.prototype._events = undefined;
/**
* Return an array listing the events for which the emitter has registered
* listeners.
*
* @returns {Array}
* @api public
*/
EventEmitter.prototype.eventNames = function eventNames() {
var events = this._events
, names = []
, name;
if (!events) return names;
for (name in events) {
if (has.call(events, name)) names.push(prefix ? name.slice(1) : name);
}
if (Object.getOwnPropertySymbols) {
return names.concat(Object.getOwnPropertySymbols(events));
}
return names;
};
/**
* Return a list of assigned event listeners.
*
* @param {String} event The events that should be listed.
* @param {Boolean} exists We only need to know if there are listeners.
* @returns {Array|Boolean}
* @api public
*/
EventEmitter.prototype.listeners = function listeners(event, exists) {
var evt = prefix ? prefix + event : event
, available = this._events && this._events[evt];
if (exists) return !!available;
if (!available) return [];
if (available.fn) return [available.fn];
for (var i = 0, l = available.length, ee = new Array(l); i < l; i++) {
ee[i] = available[i].fn;
}
return ee;
};
/**
* Emit an event to all registered event listeners.
*
* @param {String} event The name of the event.
* @returns {Boolean} Indication if we've emitted an event.
* @api public
*/
EventEmitter.prototype.emit = function emit(event, a1, a2, a3, a4, a5) {
var evt = prefix ? prefix + event : event;
if (!this._events || !this._events[evt]) return false;
var listeners = this._events[evt]
, len = arguments.length
, args
, i;
if ('function' === typeof listeners.fn) {
if (listeners.once) this.removeListener(event, listeners.fn, undefined, true);
switch (len) {
case 1: return listeners.fn.call(listeners.context), true;
case 2: return listeners.fn.call(listeners.context, a1), true;
case 3: return listeners.fn.call(listeners.context, a1, a2), true;
case 4: return listeners.fn.call(listeners.context, a1, a2, a3), true;
case 5: return listeners.fn.call(listeners.context, a1, a2, a3, a4), true;
case 6: return listeners.fn.call(listeners.context, a1, a2, a3, a4, a5), true;
}
for (i = 1, args = new Array(len -1); i < len; i++) {
args[i - 1] = arguments[i];
}
listeners.fn.apply(listeners.context, args);
} else {
var length = listeners.length
, j;
for (i = 0; i < length; i++) {
if (listeners[i].once) this.removeListener(event, listeners[i].fn, undefined, true);
switch (len) {
case 1: listeners[i].fn.call(listeners[i].context); break;
case 2: listeners[i].fn.call(listeners[i].context, a1); break;
case 3: listeners[i].fn.call(listeners[i].context, a1, a2); break;
default:
if (!args) for (j = 1, args = new Array(len -1); j < len; j++) {
args[j - 1] = arguments[j];
}
listeners[i].fn.apply(listeners[i].context, args);
}
}
}
return true;
};
/**
* Register a new EventListener for the given event.
*
* @param {String} event Name of the event.
* @param {Function} fn Callback function.
* @param {Mixed} [context=this] The context of the function.
* @api public
*/
EventEmitter.prototype.on = function on(event, fn, context) {
var listener = new EE(fn, context || this)
, evt = prefix ? prefix + event : event;
if (!this._events) this._events = prefix ? {} : Object.create(null);
if (!this._events[evt]) this._events[evt] = listener;
else {
if (!this._events[evt].fn) this._events[evt].push(listener);
else this._events[evt] = [
this._events[evt], listener
];
}
return this;
};
/**
* Add an EventListener that's only called once.
*
* @param {String} event Name of the event.
* @param {Function} fn Callback function.
* @param {Mixed} [context=this] The context of the function.
* @api public
*/
EventEmitter.prototype.once = function once(event, fn, context) {
var listener = new EE(fn, context || this, true)
, evt = prefix ? prefix + event : event;
if (!this._events) this._events = prefix ? {} : Object.create(null);
if (!this._events[evt]) this._events[evt] = listener;
else {
if (!this._events[evt].fn) this._events[evt].push(listener);
else this._events[evt] = [
this._events[evt], listener
];
}
return this;
};
/**
* Remove event listeners.
*
* @param {String} event The event we want to remove.
* @param {Function} fn The listener that we need to find.
* @param {Mixed} context Only remove listeners matching this context.
* @param {Boolean} once Only remove once listeners.
* @api public
*/
EventEmitter.prototype.removeListener = function removeListener(event, fn, context, once) {
var evt = prefix ? prefix + event : event;
if (!this._events || !this._events[evt]) return this;
var listeners = this._events[evt]
, events = [];
if (fn) {
if (listeners.fn) {
if (
listeners.fn !== fn
|| (once && !listeners.once)
|| (context && listeners.context !== context)
) {
events.push(listeners);
}
} else {
for (var i = 0, length = listeners.length; i < length; i++) {
if (
listeners[i].fn !== fn
|| (once && !listeners[i].once)
|| (context && listeners[i].context !== context)
) {
events.push(listeners[i]);
}
}
}
}
//
// Reset the array, or remove it completely if we have no more listeners.
//
if (events.length) {
this._events[evt] = events.length === 1 ? events[0] : events;
} else {
delete this._events[evt];
}
return this;
};
/**
* Remove all listeners or only the listeners for the specified event.
*
* @param {String} event The event want to remove all listeners for.
* @api public
*/
EventEmitter.prototype.removeAllListeners = function removeAllListeners(event) {
if (!this._events) return this;
if (event) delete this._events[prefix ? prefix + event : event];
else this._events = prefix ? {} : Object.create(null);
return this;
};
//
// Alias methods names because people roll like that.
//
EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
EventEmitter.prototype.addListener = EventEmitter.prototype.on;
//
// This function doesn't apply anymore.
//
EventEmitter.prototype.setMaxListeners = function setMaxListeners() {
return this;
};
//
// Expose the prefix.
//
EventEmitter.prefixed = prefix;
//
// Expose the module.
//
if ('undefined' !== typeof module) {
module.exports = EventEmitter;
}
{
"name": "eventemitter3",
"version": "1.2.0",
"description": "EventEmitter3 focuses on performance while maintaining a Node.js AND browser compatible interface.",
"main": "index.js",
"scripts": {
"test-node": "istanbul cover _mocha --report lcovonly -- test.js",
"coverage": "istanbul cover _mocha -- test.js",
"test-browser": "zuul -- test.js",
"sync": "node versions.js",
"test": "mocha test.js"
},
"repository": {
"type": "git",
"url": "git://github.com/primus/eventemitter3.git"
},
"keywords": [
"EventEmitter",
"EventEmitter2",
"EventEmitter3",
"Events",
"addEventListener",
"addListener",
"emit",
"emits",
"emitter",
"event",
"once",
"pub/sub",
"publish",
"reactor",
"subscribe"
],
"author": "Arnout Kazemier",
"license": "MIT",
"bugs": {
"url": "https://github.com/primus/eventemitter3/issues"
},
"pre-commit": "sync, test",
"devDependencies": {
"assume": "1.3.x",
"istanbul": "0.4.x",
"mocha": "2.4.x",
"pre-commit": "1.1.x",
"zuul": "3.10.x"
}
}
'use strict';
const PassThrough = require('stream').PassThrough;
module.exports = opts => {
opts = Object.assign({}, opts);
const array = opts.array;
let encoding = opts.encoding;
const buffer = encoding === 'buffer';
let objectMode = false;
if (array) {
objectMode = !(encoding || buffer);
} else {
encoding = encoding || 'utf8';
}
if (buffer) {
encoding = null;
}
let len = 0;
const ret = [];
const stream = new PassThrough({objectMode});
if (encoding) {
stream.setEncoding(encoding);
}
stream.on('data', chunk => {
ret.push(chunk);
if (objectMode) {
len = ret.length;
} else {
len += chunk.length;
}
});
stream.getBufferedValue = () => {
if (array) {
return ret;
}
return buffer ? Buffer.concat(ret, len) : ret.join('');
};
stream.getBufferedLength = () => len;
return stream;
};
'use strict';
const bufferStream = require('./buffer-stream');
function getStream(inputStream, opts) {
if (!inputStream) {
return Promise.reject(new Error('Expected a stream'));
}
opts = Object.assign({maxBuffer: Infinity}, opts);
const maxBuffer = opts.maxBuffer;
let stream;
let clean;
const p = new Promise((resolve, reject) => {
const error = err => {
if (err) { // null check
err.bufferedData = stream.getBufferedValue();
}
reject(err);
};
stream = bufferStream(opts);
inputStream.once('error', error);
inputStream.pipe(stream);
stream.on('data', () => {
if (stream.getBufferedLength() > maxBuffer) {
reject(new Error('maxBuffer exceeded'));
}
});
stream.once('error', error);
stream.on('end', resolve);
clean = () => {
// some streams doesn't implement the `stream.Readable` interface correctly
if (inputStream.unpipe) {
inputStream.unpipe(stream);
}
};
});
p.then(clean, clean);
return p.then(() => stream.getBufferedValue());
}
module.exports = getStream;
module.exports.buffer = (stream, opts) => getStream(stream, Object.assign({}, opts, {encoding: 'buffer'}));
module.exports.array = (stream, opts) => getStream(stream, Object.assign({}, opts, {array: true}));
The MIT License (MIT)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
{
"name": "get-stream",
"version": "3.0.0",
"description": "Get a stream as a string, buffer, or array",
"license": "MIT",
"repository": "sindresorhus/get-stream",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js",
"buffer-stream.js"
],
"keywords": [
"get",
"stream",
"promise",
"concat",
"string",
"str",
"text",
"buffer",
"read",
"data",
"consume",
"readable",
"readablestream",
"array",
"object",
"obj"
],
"devDependencies": {
"ava": "*",
"into-stream": "^3.0.0",
"xo": "*"
},
"xo": {
"esnext": true
}
}
# get-stream [![Build Status](https://travis-ci.org/sindresorhus/get-stream.svg?branch=master)](https://travis-ci.org/sindresorhus/get-stream)
> Get a stream as a string, buffer, or array
## Install
```
$ npm install --save get-stream
```
## Usage
```js
const fs = require('fs');
const getStream = require('get-stream');
const stream = fs.createReadStream('unicorn.txt');
getStream(stream).then(str => {
console.log(str);
/*
,,))))))));,
__)))))))))))))),
\|/ -\(((((''''((((((((.
-*-==//////(('' . `)))))),
/|\ ))| o ;-. '((((( ,(,
( `| / ) ;))))' ,_))^;(~
| | | ,))((((_ _____------~~~-. %,;(;(>';'~
o_); ; )))(((` ~---~ `:: \ %%~~)(v;(`('~
; ''''```` `: `:::|\,__,%% );`'; ~
| _ ) / `:|`----' `-'
______/\/~ | / /
/~;;.____/;;' / ___--,-( `;;;/
/ // _;______;'------~~~~~ /;;/\ /
// | | / ; \;;,\
(<_ | ; /',/-----' _>
\_| ||_ //~;~~~~~~~~~
`\_| (,~~
\~\
~~
*/
});
```
## API
The methods returns a promise that resolves when the `end` event fires on the stream, indicating that there is no more data to be read. The stream is switched to flowing mode.
### getStream(stream, [options])
Get the `stream` as a string.
#### options
##### encoding
Type: `string`<br>
Default: `utf8`
[Encoding](https://nodejs.org/api/buffer.html#buffer_buffer) of the incoming stream.
##### maxBuffer
Type: `number`<br>
Default: `Infinity`
Maximum length of the returned string. If it exceeds this value before the stream ends, the promise will be rejected.
### getStream.buffer(stream, [options])
Get the `stream` as a buffer.
It honors the `maxBuffer` option as above, but it refers to byte length rather than string length.
### getStream.array(stream, [options])
Get the `stream` as an array of values.
It honors both the `maxBuffer` and `encoding` options. The behavior changes slightly based on the encoding chosen:
- When `encoding` is unset, it assumes an [object mode stream](https://nodesource.com/blog/understanding-object-streams/) and collects values emitted from `stream` unmodified. In this case `maxBuffer` refers to the number of items in the array (not the sum of their sizes).
- When `encoding` is set to `buffer`, it collects an array of buffers. `maxBuffer` refers to the summed byte lengths of every buffer in the array.
- When `encoding` is set to anything else, it collects an array of strings. `maxBuffer` refers to the summed character lengths of every string in the array.
## Errors
If the input stream emits an `error` event, the promise will be rejected with the error. The buffered data will be attached to the `bufferedData` property of the error.
```js
getStream(streamThatErrorsAtTheEnd('unicorn'))
.catch(err => {
console.log(err.bufferedData);
//=> 'unicorn'
});
```
## FAQ
### How is this different from [`concat-stream`](https://github.com/maxogden/concat-stream)?
This module accepts a stream instead of being one and returns a promise instead of using a callback. The API is simpler and it only supports returning a string, buffer, or array. It doesn't have a fragile type inference. You explicitly choose what you want. And it doesn't depend on the huge `readable-stream` package.
## Related
- [get-stdin](https://github.com/sindresorhus/get-stdin) - Get stdin as a string or buffer
## License
MIT © [Sindre Sorhus](https://sindresorhus.com)
'use strict';
const EventEmitter = require('events');
const http = require('http');
const https = require('https');
const PassThrough = require('stream').PassThrough;
const urlLib = require('url');
const querystring = require('querystring');
const duplexer3 = require('duplexer3');
const isStream = require('is-stream');
const getStream = require('get-stream');
const timedOut = require('timed-out');
const urlParseLax = require('url-parse-lax');
const lowercaseKeys = require('lowercase-keys');
const isRedirect = require('is-redirect');
const unzipResponse = require('unzip-response');
const createErrorClass = require('create-error-class');
const isRetryAllowed = require('is-retry-allowed');
const Buffer = require('safe-buffer').Buffer;
const pkg = require('./package');
function requestAsEventEmitter(opts) {
opts = opts || {};
const ee = new EventEmitter();
const requestUrl = opts.href || urlLib.resolve(urlLib.format(opts), opts.path);
let redirectCount = 0;
let retryCount = 0;
let redirectUrl;
const get = opts => {
const fn = opts.protocol === 'https:' ? https : http;
const req = fn.request(opts, res => {
const statusCode = res.statusCode;
if (isRedirect(statusCode) && opts.followRedirect && 'location' in res.headers && (opts.method === 'GET' || opts.method === 'HEAD')) {
res.resume();
if (++redirectCount > 10) {
ee.emit('error', new got.MaxRedirectsError(statusCode, opts), null, res);
return;
}
const bufferString = Buffer.from(res.headers.location, 'binary').toString();
redirectUrl = urlLib.resolve(urlLib.format(opts), bufferString);
const redirectOpts = Object.assign({}, opts, urlLib.parse(redirectUrl));
ee.emit('redirect', res, redirectOpts);
get(redirectOpts);
return;
}
setImmediate(() => {
const response = typeof unzipResponse === 'function' && req.method !== 'HEAD' ? unzipResponse(res) : res;
response.url = redirectUrl || requestUrl;
response.requestUrl = requestUrl;
ee.emit('response', response);
});
});
req.once('error', err => {
const backoff = opts.retries(++retryCount, err);
if (backoff) {
setTimeout(get, backoff, opts);
return;
}
ee.emit('error', new got.RequestError(err, opts));
});
if (opts.gotTimeout) {
timedOut(req, opts.gotTimeout);
}
setImmediate(() => {
ee.emit('request', req);
});
};
get(opts);
return ee;
}
function asPromise(opts) {
return new Promise((resolve, reject) => {
const ee = requestAsEventEmitter(opts);
ee.on('request', req => {
if (isStream(opts.body)) {
opts.body.pipe(req);
opts.body = undefined;
return;
}
req.end(opts.body);
});
ee.on('response', res => {
const stream = opts.encoding === null ? getStream.buffer(res) : getStream(res, opts);
stream
.catch(err => reject(new got.ReadError(err, opts)))
.then(data => {
const statusCode = res.statusCode;
const limitStatusCode = opts.followRedirect ? 299 : 399;
res.body = data;
if (opts.json && res.body) {
try {
res.body = JSON.parse(res.body);
} catch (e) {
throw new got.ParseError(e, statusCode, opts, data);
}
}
if (statusCode < 200 || statusCode > limitStatusCode) {
throw new got.HTTPError(statusCode, opts);
}
resolve(res);
})
.catch(err => {
Object.defineProperty(err, 'response', {value: res});
reject(err);
});
});
ee.on('error', reject);
});
}
function asStream(opts) {
const input = new PassThrough();
const output = new PassThrough();
const proxy = duplexer3(input, output);
if (opts.json) {
throw new Error('got can not be used as stream when options.json is used');
}
if (opts.body) {
proxy.write = () => {
throw new Error('got\'s stream is not writable when options.body is used');
};
}
const ee = requestAsEventEmitter(opts);
ee.on('request', req => {
proxy.emit('request', req);
if (isStream(opts.body)) {
opts.body.pipe(req);
return;
}
if (opts.body) {
req.end(opts.body);
return;
}
if (opts.method === 'POST' || opts.method === 'PUT' || opts.method === 'PATCH') {
input.pipe(req);
return;
}
req.end();
});
ee.on('response', res => {
const statusCode = res.statusCode;
res.pipe(output);
if (statusCode < 200 || statusCode > 299) {
proxy.emit('error', new got.HTTPError(statusCode, opts), null, res);
return;
}
proxy.emit('response', res);
});
ee.on('redirect', proxy.emit.bind(proxy, 'redirect'));
ee.on('error', proxy.emit.bind(proxy, 'error'));
return proxy;
}
function normalizeArguments(url, opts) {
if (typeof url !== 'string' && typeof url !== 'object') {
throw new Error(`Parameter \`url\` must be a string or object, not ${typeof url}`);
}
if (typeof url === 'string') {
url = url.replace(/^unix:/, 'http://$&');
url = urlParseLax(url);
if (url.auth) {
throw new Error('Basic authentication must be done with auth option');
}
}
opts = Object.assign(
{
protocol: 'http:',
path: '',
retries: 5
},
url,
opts
);
opts.headers = Object.assign({
'user-agent': `${pkg.name}/${pkg.version} (https://github.com/sindresorhus/got)`,
'accept-encoding': 'gzip,deflate'
}, lowercaseKeys(opts.headers));
const query = opts.query;
if (query) {
if (typeof query !== 'string') {
opts.query = querystring.stringify(query);
}
opts.path = `${opts.path.split('?')[0]}?${opts.query}`;
delete opts.query;
}
if (opts.json && opts.headers.accept === undefined) {
opts.headers.accept = 'application/json';
}
let body = opts.body;
if (body) {
if (typeof body !== 'string' && !(body !== null && typeof body === 'object')) {
throw new Error('options.body must be a ReadableStream, string, Buffer or plain Object');
}
opts.method = opts.method || 'POST';
if (isStream(body) && typeof body.getBoundary === 'function') {
// Special case for https://github.com/form-data/form-data
opts.headers['content-type'] = opts.headers['content-type'] || `multipart/form-data; boundary=${body.getBoundary()}`;
} else if (body !== null && typeof body === 'object' && !Buffer.isBuffer(body) && !isStream(body)) {
opts.headers['content-type'] = opts.headers['content-type'] || 'application/x-www-form-urlencoded';
body = opts.body = querystring.stringify(body);
}
if (opts.headers['content-length'] === undefined && opts.headers['transfer-encoding'] === undefined && !isStream(body)) {
const length = typeof body === 'string' ? Buffer.byteLength(body) : body.length;
opts.headers['content-length'] = length;
}
}
opts.method = (opts.method || 'GET').toUpperCase();
if (opts.hostname === 'unix') {
const matches = /(.+):(.+)/.exec(opts.path);
if (matches) {
opts.socketPath = matches[1];
opts.path = matches[2];
opts.host = null;
}
}
if (typeof opts.retries !== 'function') {
const retries = opts.retries;
opts.retries = (iter, err) => {
if (iter > retries || !isRetryAllowed(err)) {
return 0;
}
const noise = Math.random() * 100;
return ((1 << iter) * 1000) + noise;
};
}
if (opts.followRedirect === undefined) {
opts.followRedirect = true;
}
if (opts.timeout) {
opts.gotTimeout = opts.timeout;
delete opts.timeout;
}
return opts;
}
function got(url, opts) {
try {
return asPromise(normalizeArguments(url, opts));
} catch (err) {
return Promise.reject(err);
}
}
const helpers = [
'get',
'post',
'put',
'patch',
'head',
'delete'
];
helpers.forEach(el => {
got[el] = (url, opts) => got(url, Object.assign({}, opts, {method: el}));
});
got.stream = (url, opts) => asStream(normalizeArguments(url, opts));
for (const el of helpers) {
got.stream[el] = (url, opts) => got.stream(url, Object.assign({}, opts, {method: el}));
}
function stdError(error, opts) {
if (error.code !== undefined) {
this.code = error.code;
}
Object.assign(this, {
message: error.message,
host: opts.host,
hostname: opts.hostname,
method: opts.method,
path: opts.path
});
}
got.RequestError = createErrorClass('RequestError', stdError);
got.ReadError = createErrorClass('ReadError', stdError);
got.ParseError = createErrorClass('ParseError', function (e, statusCode, opts, data) {
stdError.call(this, e, opts);
this.statusCode = statusCode;
this.statusMessage = http.STATUS_CODES[this.statusCode];
this.message = `${e.message} in "${urlLib.format(opts)}": \n${data.slice(0, 77)}...`;
});
got.HTTPError = createErrorClass('HTTPError', function (statusCode, opts) {
stdError.call(this, {}, opts);
this.statusCode = statusCode;
this.statusMessage = http.STATUS_CODES[this.statusCode];
this.message = `Response code ${this.statusCode} (${this.statusMessage})`;
});
got.MaxRedirectsError = createErrorClass('MaxRedirectsError', function (statusCode, opts) {
stdError.call(this, {}, opts);
this.statusCode = statusCode;
this.statusMessage = http.STATUS_CODES[this.statusCode];
this.message = 'Redirected 10 times. Aborting.';
});
module.exports = got;
The MIT License (MIT)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
{
"name": "got",
"version": "6.7.1",
"description": "Simplified HTTP requests",
"license": "MIT",
"repository": "sindresorhus/got",
"maintainers": [
{
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
{
"name": "Vsevolod Strukchinsky",
"email": "floatdrop@gmail.com",
"url": "github.com/floatdrop"
}
],
"engines": {
"node": ">=4"
},
"browser": {
"unzip-response": false
},
"scripts": {
"test": "xo && nyc ava",
"coveralls": "nyc report --reporter=text-lcov | coveralls"
},
"files": [
"index.js"
],
"keywords": [
"http",
"https",
"get",
"got",
"url",
"uri",
"request",
"util",
"utility",
"simple",
"curl",
"wget",
"fetch"
],
"dependencies": {
"create-error-class": "^3.0.0",
"duplexer3": "^0.1.4",
"get-stream": "^3.0.0",
"is-redirect": "^1.0.0",
"is-retry-allowed": "^1.0.0",
"is-stream": "^1.0.0",
"lowercase-keys": "^1.0.0",
"safe-buffer": "^5.0.1",
"timed-out": "^4.0.0",
"unzip-response": "^2.0.1",
"url-parse-lax": "^1.0.0"
},
"devDependencies": {
"ava": "^0.17.0",
"coveralls": "^2.11.4",
"form-data": "^2.1.1",
"get-port": "^2.0.0",
"into-stream": "^3.0.0",
"nyc": "^10.0.0",
"pem": "^1.4.4",
"pify": "^2.3.0",
"tempfile": "^1.1.1",
"xo": "*"
},
"xo": {
"esnext": true
},
"ava": {
"concurrency": 4
}
}
<h1 align="center">
<br>
<img width="360" src="https://rawgit.com/sindresorhus/got/master/media/logo.svg" alt="got">
<br>
<br>
<br>
</h1>
> Simplified HTTP requests
[![Build Status](https://travis-ci.org/sindresorhus/got.svg?branch=master)](https://travis-ci.org/sindresorhus/got) [![Coverage Status](https://coveralls.io/repos/github/sindresorhus/got/badge.svg?branch=master)](https://coveralls.io/github/sindresorhus/got?branch=master) [![Downloads](https://img.shields.io/npm/dm/got.svg)](https://npmjs.com/got)
A nicer interface to the built-in [`http`](http://nodejs.org/api/http.html) module.
It supports following redirects, promises, streams, retries, automagically handling gzip/deflate and some convenience options.
Created because [`request`](https://github.com/request/request) is bloated *(several megabytes!)*.
## Install
**WARNING: Node.js 4 or higher is required for got@6 and above.** For older Node.js versions use [got@5](https://github.com/sindresorhus/got/tree/v5.x).
```
$ npm install --save got
```
## Usage
```js
const fs = require('fs');
const got = require('got');
got('todomvc.com')
.then(response => {
console.log(response.body);
//=> '<!doctype html> ...'
})
.catch(error => {
console.log(error.response.body);
//=> 'Internal server error ...'
});
// Streams
got.stream('todomvc.com').pipe(fs.createWriteStream('index.html'));
// For POST, PUT and PATCH methods got.stream returns a WritableStream
fs.createReadStream('index.html').pipe(got.stream.post('todomvc.com'));
```
### API
It's a `GET` request by default, but can be changed in `options`.
#### got(url, [options])
Returns a Promise for a `response` object with a `body` property, a `url` property with the request URL or the final URL after redirects, and a `requestUrl` property with the original request URL.
##### url
Type: `string`, `object`
The URL to request or a [`http.request` options](https://nodejs.org/api/http.html#http_http_request_options_callback) object.
Properties from `options` will override properties in the parsed `url`.
##### options
Type: `object`
Any of the [`http.request`](http://nodejs.org/api/http.html#http_http_request_options_callback) options.
###### body
Type: `string`, `buffer`, `readableStream`, `object`
*This is mutually exclusive with stream mode.*
Body that will be sent with a `POST` request.
If present in `options` and `options.method` is not set, `options.method` will be set to `POST`.
If `content-length` or `transfer-encoding` is not set in `options.headers` and `body` is a string or buffer, `content-length` will be set to the body length.
If `body` is a plain object, it will be stringified with [`querystring.stringify`](https://nodejs.org/api/querystring.html#querystring_querystring_stringify_obj_sep_eq_options) and sent as `application/x-www-form-urlencoded`.
###### encoding
Type: `string`, `null`<br>
Default: `'utf8'`
Encoding to be used on `setEncoding` of the response data. If `null`, the body is returned as a Buffer.
###### json
Type: `boolean`<br>
Default: `false`
*This is mutually exclusive with stream mode.*
Parse response body with `JSON.parse` and set `accept` header to `application/json`.
###### query
Type: `string`, `object`<br>
Query string object that will be added to the request URL. This will override the query string in `url`.
###### timeout
Type: `number`, `object`
Milliseconds to wait for a server to send response headers before aborting request with `ETIMEDOUT` error.
Option accepts `object` with separate `connect` and `socket` fields for connection and socket inactivity timeouts.
###### retries
Type: `number`, `function`<br>
Default: `5`
Number of request retries when network errors happens. Delays between retries counts with function `1000 * Math.pow(2, retry) + Math.random() * 100`, where `retry` is attempt number (starts from 0).
Option accepts `function` with `retry` and `error` arguments. Function must return delay in milliseconds (`0` return value cancels retry).
**Note:** if `retries` is `number`, `ENOTFOUND` and `ENETUNREACH` error will not be retried (see full list in [`is-retry-allowed`](https://github.com/floatdrop/is-retry-allowed/blob/master/index.js#L12) module).
###### followRedirect
Type: `boolean`<br>
Default: `true`
Defines if redirect responses should be followed automatically.
#### Streams
#### got.stream(url, [options])
`stream` method will return Duplex stream with additional events:
##### .on('request', request)
`request` event to get the request object of the request.
**Tip**: You can use `request` event to abort request:
```js
got.stream('github.com')
.on('request', req => setTimeout(() => req.abort(), 50));
```
##### .on('response', response)
`response` event to get the response object of the final request.
##### .on('redirect', response, nextOptions)
`redirect` event to get the response object of a redirect. The second argument is options for the next request to the redirect location.
##### .on('error', error, body, response)
`error` event emitted in case of protocol error (like `ENOTFOUND` etc.) or status error (4xx or 5xx). The second argument is the body of the server response in case of status error. The third argument is response object.
#### got.get(url, [options])
#### got.post(url, [options])
#### got.put(url, [options])
#### got.patch(url, [options])
#### got.head(url, [options])
#### got.delete(url, [options])
Sets `options.method` to the method name and makes a request.
## Errors
Each error contains (if available) `statusCode`, `statusMessage`, `host`, `hostname`, `method` and `path` properties to make debugging easier.
In Promise mode, the `response` is attached to the error.
#### got.RequestError
When a request fails. Contains a `code` property with error class code, like `ECONNREFUSED`.
#### got.ReadError
When reading from response stream fails.
#### got.ParseError
When `json` option is enabled and `JSON.parse` fails.
#### got.HTTPError
When server response code is not 2xx. Contains `statusCode` and `statusMessage`.
#### got.MaxRedirectsError
When server redirects you more than 10 times.
## Proxies
You can use the [`tunnel`](https://github.com/koichik/node-tunnel) module with the `agent` option to work with proxies:
```js
const got = require('got');
const tunnel = require('tunnel');
got('todomvc.com', {
agent: tunnel.httpOverHttp({
proxy: {
host: 'localhost'
}
})
});
```
## Cookies
You can use the [`cookie`](https://github.com/jshttp/cookie) module to include cookies in a request:
```js
const got = require('got');
const cookie = require('cookie');
got('google.com', {
headers: {
cookie: cookie.serialize('foo', 'bar')
}
});
```
## Form data
You can use the [`form-data`](https://github.com/form-data/form-data) module to create POST request with form data:
```js
const fs = require('fs');
const got = require('got');
const FormData = require('form-data');
const form = new FormData();
form.append('my_file', fs.createReadStream('/foo/bar.jpg'));
got.post('google.com', {
body: form
});
```
## OAuth
You can use the [`oauth-1.0a`](https://github.com/ddo/oauth-1.0a) module to create a signed OAuth request:
```js
const got = require('got');
const crypto = require('crypto');
const OAuth = require('oauth-1.0a');
const oauth = OAuth({
consumer: {
key: process.env.CONSUMER_KEY,
secret: process.env.CONSUMER_SECRET
},
signature_method: 'HMAC-SHA1',
hash_function: (baseString, key) => crypto.createHmac('sha1', key).update(baseString).digest('base64')
});
const token = {
key: process.env.ACCESS_TOKEN,
secret: process.env.ACCESS_TOKEN_SECRET
};
const url = 'https://api.twitter.com/1.1/statuses/home_timeline.json';
got(url, {
headers: oauth.toHeader(oauth.authorize({url, method: 'GET'}, token)),
json: true
});
```
## Unix Domain Sockets
Requests can also be sent via [unix domain sockets](http://serverfault.com/questions/124517/whats-the-difference-between-unix-socket-and-tcp-ip-socket). Use the following URL scheme: `PROTOCOL://unix:SOCKET:PATH`.
- `PROTOCOL` - `http` or `https` *(optional)*
- `SOCKET` - absolute path to a unix domain socket, e.g. `/var/run/docker.sock`
- `PATH` - request path, e.g. `/v2/keys`
```js
got('http://unix:/var/run/docker.sock:/containers/json');
// or without protocol (http by default)
got('unix:/var/run/docker.sock:/containers/json');
```
## Tip
It's a good idea to set the `'user-agent'` header so the provider can more easily see how their resource is used. By default, it's the URL to this repo.
```js
const got = require('got');
const pkg = require('./package.json');
got('todomvc.com', {
headers: {
'user-agent': `my-module/${pkg.version} (https://github.com/username/my-module)`
}
});
```
## Related
- [gh-got](https://github.com/sindresorhus/gh-got) - Convenience wrapper for interacting with the GitHub API
- [travis-got](https://github.com/samverschueren/travis-got) - Convenience wrapper for interacting with the Travis API
## Created by
[![Sindre Sorhus](https://avatars.githubusercontent.com/u/170270?v=3&s=100)](https://sindresorhus.com) | [![Vsevolod Strukchinsky](https://avatars.githubusercontent.com/u/365089?v=3&s=100)](https://github.com/floatdrop)
---|---
[Sindre Sorhus](https://sindresorhus.com) | [Vsevolod Strukchinsky](https://github.com/floatdrop)
## License
MIT © [Sindre Sorhus](https://sindresorhus.com)
'use strict';
module.exports = function (x) {
if (typeof x !== 'number') {
throw new TypeError('Expected a number');
}
return x === 300 ||
x === 301 ||
x === 302 ||
x === 303 ||
x === 305 ||
x === 307 ||
x === 308;
};
The MIT License (MIT)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
{
"name": "is-redirect",
"version": "1.0.0",
"description": "Check if a number is a redirect HTTP status code",
"license": "MIT",
"repository": "sindresorhus/is-redirect",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "node test.js"
},
"files": [
"index.js"
],
"keywords": [
"redirect",
"http",
"https",
"status",
"code",
"codes",
"is",
"check",
"detect"
],
"devDependencies": {
"ava": "0.0.4"
}
}
# is-redirect [![Build Status](https://travis-ci.org/sindresorhus/is-redirect.svg?branch=master)](https://travis-ci.org/sindresorhus/is-redirect)
> Check if a number is a [redirect HTTP status code](http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_Redirection)
## Install
```
$ npm install --save is-redirect
```
## Usage
```js
var isRedirect = require('is-redirect');
isRedirect(302);
//=> true
isRedirect(200);
//=> false
```
## License
MIT © [Sindre Sorhus](http://sindresorhus.com)
'use strict';
var WHITELIST = [
'ETIMEDOUT',
'ECONNRESET',
'EADDRINUSE',
'ESOCKETTIMEDOUT',
'ECONNREFUSED',
'EPIPE',
'EHOSTUNREACH',
'EAI_AGAIN'
];
var BLACKLIST = [
'ENOTFOUND',
'ENETUNREACH',
// SSL errors from https://github.com/nodejs/node/blob/ed3d8b13ee9a705d89f9e0397d9e96519e7e47ac/src/node_crypto.cc#L1950
'UNABLE_TO_GET_ISSUER_CERT',
'UNABLE_TO_GET_CRL',
'UNABLE_TO_DECRYPT_CERT_SIGNATURE',
'UNABLE_TO_DECRYPT_CRL_SIGNATURE',
'UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY',
'CERT_SIGNATURE_FAILURE',
'CRL_SIGNATURE_FAILURE',
'CERT_NOT_YET_VALID',
'CERT_HAS_EXPIRED',
'CRL_NOT_YET_VALID',
'CRL_HAS_EXPIRED',
'ERROR_IN_CERT_NOT_BEFORE_FIELD',
'ERROR_IN_CERT_NOT_AFTER_FIELD',
'ERROR_IN_CRL_LAST_UPDATE_FIELD',
'ERROR_IN_CRL_NEXT_UPDATE_FIELD',
'OUT_OF_MEM',
'DEPTH_ZERO_SELF_SIGNED_CERT',
'SELF_SIGNED_CERT_IN_CHAIN',
'UNABLE_TO_GET_ISSUER_CERT_LOCALLY',
'UNABLE_TO_VERIFY_LEAF_SIGNATURE',
'CERT_CHAIN_TOO_LONG',
'CERT_REVOKED',
'INVALID_CA',
'PATH_LENGTH_EXCEEDED',
'INVALID_PURPOSE',
'CERT_UNTRUSTED',
'CERT_REJECTED'
];
module.exports = function (err) {
if (!err || !err.code) {
return true;
}
if (WHITELIST.indexOf(err.code) !== -1) {
return true;
}
if (BLACKLIST.indexOf(err.code) !== -1) {
return false;
}
return true;
};
The MIT License (MIT)
Copyright (c) Vsevolod Strukchinsky <floatdrop@gmail.com> (github.com/floatdrop)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
{
"name": "is-retry-allowed",
"version": "1.2.0",
"description": "Is retry allowed for Error?",
"license": "MIT",
"repository": "floatdrop/is-retry-allowed",
"author": {
"name": "Vsevolod Strukchinsky",
"email": "floatdrop@gmail.com",
"url": "github.com/floatdrop"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
""
],
"dependencies": {},
"devDependencies": {
"ava": "^0.8.0",
"xo": "^0.12.1"
}
}
# is-retry-allowed [![Build Status](https://travis-ci.org/floatdrop/is-retry-allowed.svg?branch=master)](https://travis-ci.org/floatdrop/is-retry-allowed)
Is retry allowed for Error?
## Install
```
$ npm install --save is-retry-allowed
```
## Usage
```js
const isRetryAllowed = require('is-retry-allowed');
isRetryAllowed({code: 'ETIMEDOUT'});
//=> true
isRetryAllowed({code: 'ENOTFOUND'});
//=> false
isRetryAllowed({});
//=> true
```
## API
### isRetryAllowed(error)
#### error
Type: `object`
Object with `code` property, which will be used to determine retry.
## License
MIT © [Vsevolod Strukchinsky](http://github.com/floatdrop)
'use strict';
var isStream = module.exports = function (stream) {
return stream !== null && typeof stream === 'object' && typeof stream.pipe === 'function';
};
isStream.writable = function (stream) {
return isStream(stream) && stream.writable !== false && typeof stream._write === 'function' && typeof stream._writableState === 'object';
};
isStream.readable = function (stream) {
return isStream(stream) && stream.readable !== false && typeof stream._read === 'function' && typeof stream._readableState === 'object';
};
isStream.duplex = function (stream) {
return isStream.writable(stream) && isStream.readable(stream);
};
isStream.transform = function (stream) {
return isStream.duplex(stream) && typeof stream._transform === 'function' && typeof stream._transformState === 'object';
};
The MIT License (MIT)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
{
"name": "is-stream",
"version": "1.1.0",
"description": "Check if something is a Node.js stream",
"license": "MIT",
"repository": "sindresorhus/is-stream",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"stream",
"type",
"streams",
"writable",
"readable",
"duplex",
"transform",
"check",
"detect",
"is"
],
"devDependencies": {
"ava": "*",
"tempfile": "^1.1.0",
"xo": "*"
}
}
# is-stream [![Build Status](https://travis-ci.org/sindresorhus/is-stream.svg?branch=master)](https://travis-ci.org/sindresorhus/is-stream)
> Check if something is a [Node.js stream](https://nodejs.org/api/stream.html)
## Install
```
$ npm install --save is-stream
```
## Usage
```js
const fs = require('fs');
const isStream = require('is-stream');
isStream(fs.createReadStream('unicorn.png'));
//=> true
isStream({});
//=> false
```
## API
### isStream(stream)
#### isStream.writable(stream)
#### isStream.readable(stream)
#### isStream.duplex(stream)
#### isStream.transform(stream)
## License
MIT © [Sindre Sorhus](https://sindresorhus.com)
'use strict';
module.exports = function (obj) {
var ret = {};
var keys = Object.keys(Object(obj));
for (var i = 0; i < keys.length; i++) {
ret[keys[i].toLowerCase()] = obj[keys[i]];
}
return ret;
};
The MIT License (MIT)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
{
"name": "lowercase-keys",
"version": "1.0.1",
"description": "Lowercase the keys of an object",
"license": "MIT",
"repository": "sindresorhus/lowercase-keys",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "ava"
},
"files": [
"index.js"
],
"keywords": [
"object",
"assign",
"extend",
"properties",
"lowercase",
"lower-case",
"case",
"keys",
"key"
],
"devDependencies": {
"ava": "*"
}
}
# lowercase-keys [![Build Status](https://travis-ci.org/sindresorhus/lowercase-keys.svg?branch=master)](https://travis-ci.org/sindresorhus/lowercase-keys)
> Lowercase the keys of an object
## Install
```
$ npm install --save lowercase-keys
```
## Usage
```js
var lowercaseKeys = require('lowercase-keys');
lowercaseKeys({FOO: true, bAr: false});
//=> {foo: true, bar: false}
```
## API
### lowercaseKeys(object)
Lowercases the keys and returns a new object.
## License
MIT © [Sindre Sorhus](http://sindresorhus.com)
'use strict';
module.exports = function (url) {
if (typeof url !== 'string') {
throw new TypeError('Expected a string, got ' + typeof url);
}
url = url.trim();
if (/^\.*\/|^(?!localhost)\w+:/.test(url)) {
return url;
}
return url.replace(/^(?!(?:\w+:)?\/\/)/, 'http://');
};
The MIT License (MIT)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
{
"name": "prepend-http",
"version": "1.0.4",
"description": "Prepend `http://` to humanized URLs like todomvc.com and localhost",
"license": "MIT",
"repository": "sindresorhus/prepend-http",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"prepend",
"protocol",
"scheme",
"url",
"uri",
"http",
"https",
"humanized"
],
"devDependencies": {
"ava": "*",
"xo": "*"
}
}
# prepend-http [![Build Status](https://travis-ci.org/sindresorhus/prepend-http.svg?branch=master)](https://travis-ci.org/sindresorhus/prepend-http)
> Prepend `http://` to humanized URLs like `todomvc.com` and `localhost`
## Install
```
$ npm install --save prepend-http
```
## Usage
```js
const prependHttp = require('prepend-http');
prependHttp('todomvc.com');
//=> 'http://todomvc.com'
prependHttp('localhost');
//=> 'http://localhost'
prependHttp('http://todomvc.com');
//=> 'http://todomvc.com'
```
## License
MIT © [Sindre Sorhus](https://sindresorhus.com)
'use strict';
const got = require('got');
const uniqueRandomArray = require('unique-random-array');
const EventEmitter = require('eventemitter3');
const randomCache = {};
function formatResult(getRandomImage) {
const imageData = getRandomImage();
if (!imageData) {
return;
}
return `http://imgur.com/${imageData.hash}${imageData.ext.replace(/\?.*/, '')}`;
}
function storeResults(images, subreddit) {
const getRandomImage = uniqueRandomArray(images);
randomCache[subreddit] = getRandomImage;
return getRandomImage;
}
function randomPuppy(subreddit) {
subreddit = (typeof subreddit === 'string' && subreddit.length !== 0) ? subreddit : 'puppies';
if (randomCache[subreddit]) {
return Promise.resolve(formatResult(randomCache[subreddit]));
}
return got(`https://imgur.com/r/${subreddit}/hot.json`, {json: true})
.then(response => storeResults(response.body.data, subreddit))
.then(getRandomImage => formatResult(getRandomImage));
}
// silly feature to play with observables
function all(subreddit) {
const eventEmitter = new EventEmitter();
function emitRandomImage(subreddit) {
randomPuppy(subreddit).then(imageUrl => {
eventEmitter.emit('data', imageUrl + '#' + subreddit);
if (eventEmitter.listeners('data').length) {
setTimeout(() => emitRandomImage(subreddit), 200);
}
});
}
emitRandomImage(subreddit);
return eventEmitter;
}
function callback(subreddit, cb) {
randomPuppy(subreddit)
.then(url => cb(null, url))
.catch(err => cb(err));
}
// subreddit is optional
// callback support is provided for a training exercise
module.exports = (subreddit, cb) => {
if (typeof cb === 'function') {
callback(subreddit, cb);
} else if (typeof subreddit === 'function') {
callback(null, subreddit);
} else {
return randomPuppy(subreddit);
}
};
module.exports.all = all;
The MIT License (MIT)
Copyright (c) Dylan Greene <dylang@gmail.com> (github.com/dylang)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
{
"name": "random-puppy",
"version": "1.1.0",
"description": "Get a random imgur image url, by default a puppy.",
"license": "MIT",
"repository": "dylang/random-puppy",
"author": {
"name": "Dylan Greene",
"email": "dylang@gmail.com",
"url": "github.com/dylang"
},
"engines": {
"node": ">=4.0.0"
},
"scripts": {
"test": "xo && ava",
"watch": "ava --watch"
},
"files": [
"index.js"
],
"keywords": [
"puppy",
"doggie",
"dog",
"imgur",
"random",
"placeholder"
],
"dependencies": {
"eventemitter3": "^1.2.0",
"got": "^6.3.0",
"unique-random-array": "^1.0.0"
},
"devDependencies": {
"ava": "^0.14.0",
"rx-lite": "^4.0.8",
"xo": "^0.14.0"
},
"xo": {
"space": 4
}
}
# random-puppy [![Build Status](https://travis-ci.org/dylang/random-puppy.svg?branch=master)](https://travis-ci.org/dylang/random-puppy)
> Get a random puppy image url.
<img src="http://i.imgur.com/0zZ8m6B.jpg" width="300px">
## Install
```
$ npm install --save random-puppy
```
## Usage
```js
const randomPuppy = require('random-puppy');
randomPuppy()
.then(url => {
console.log(url);
})
//=> 'http://imgur.com/IoI8uS5'
```
## API
### `randomPuppy()`
Returns a `promise` for a random puppy image url from http://imgur.com/ from https://www.reddit.com/r/puppy
### `randomPuppy(subreddit)`
Returns a `promise` for a random image url from the selected subreddit. *Warning: We cannot promise it will be a image of a puppy!*
### `randomPuppy.all(subreddit)`
Returns an `eventemitter` for getting all random images for a subreddit.
```js
const event = randomPuppy.all(subreddit);
event.on('data', url => console.log(url));
```
Or:
```js
const event = randomPuppy.all('puppies');
Observable.fromEvent(event, 'data')
.subscribe(data => {
console.log(data);
});
```
## Notes
* Node 4 or newer.
* Caches results from imgur in memory.
* Created for the purpose of using in a training exercise on different ways to do async in JavaScript at [Opower](https://opower.com/).
## License
MIT © [Dylan Greene](https://github.com/dylang)
The MIT License (MIT)
Copyright (c) Feross Aboukhadijeh
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
This diff is collapsed. Click to expand it.
declare module "safe-buffer" {
export class Buffer {
length: number
write(string: string, offset?: number, length?: number, encoding?: string): number;
toString(encoding?: string, start?: number, end?: number): string;
toJSON(): { type: 'Buffer', data: any[] };
equals(otherBuffer: Buffer): boolean;
compare(otherBuffer: Buffer, targetStart?: number, targetEnd?: number, sourceStart?: number, sourceEnd?: number): number;
copy(targetBuffer: Buffer, targetStart?: number, sourceStart?: number, sourceEnd?: number): number;
slice(start?: number, end?: number): Buffer;
writeUIntLE(value: number, offset: number, byteLength: number, noAssert?: boolean): number;
writeUIntBE(value: number, offset: number, byteLength: number, noAssert?: boolean): number;
writeIntLE(value: number, offset: number, byteLength: number, noAssert?: boolean): number;
writeIntBE(value: number, offset: number, byteLength: number, noAssert?: boolean): number;
readUIntLE(offset: number, byteLength: number, noAssert?: boolean): number;
readUIntBE(offset: number, byteLength: number, noAssert?: boolean): number;
readIntLE(offset: number, byteLength: number, noAssert?: boolean): number;
readIntBE(offset: number, byteLength: number, noAssert?: boolean): number;
readUInt8(offset: number, noAssert?: boolean): number;
readUInt16LE(offset: number, noAssert?: boolean): number;
readUInt16BE(offset: number, noAssert?: boolean): number;
readUInt32LE(offset: number, noAssert?: boolean): number;
readUInt32BE(offset: number, noAssert?: boolean): number;
readInt8(offset: number, noAssert?: boolean): number;
readInt16LE(offset: number, noAssert?: boolean): number;
readInt16BE(offset: number, noAssert?: boolean): number;
readInt32LE(offset: number, noAssert?: boolean): number;
readInt32BE(offset: number, noAssert?: boolean): number;
readFloatLE(offset: number, noAssert?: boolean): number;
readFloatBE(offset: number, noAssert?: boolean): number;
readDoubleLE(offset: number, noAssert?: boolean): number;
readDoubleBE(offset: number, noAssert?: boolean): number;
swap16(): Buffer;
swap32(): Buffer;
swap64(): Buffer;
writeUInt8(value: number, offset: number, noAssert?: boolean): number;
writeUInt16LE(value: number, offset: number, noAssert?: boolean): number;
writeUInt16BE(value: number, offset: number, noAssert?: boolean): number;
writeUInt32LE(value: number, offset: number, noAssert?: boolean): number;
writeUInt32BE(value: number, offset: number, noAssert?: boolean): number;
writeInt8(value: number, offset: number, noAssert?: boolean): number;
writeInt16LE(value: number, offset: number, noAssert?: boolean): number;
writeInt16BE(value: number, offset: number, noAssert?: boolean): number;
writeInt32LE(value: number, offset: number, noAssert?: boolean): number;
writeInt32BE(value: number, offset: number, noAssert?: boolean): number;
writeFloatLE(value: number, offset: number, noAssert?: boolean): number;
writeFloatBE(value: number, offset: number, noAssert?: boolean): number;
writeDoubleLE(value: number, offset: number, noAssert?: boolean): number;
writeDoubleBE(value: number, offset: number, noAssert?: boolean): number;
fill(value: any, offset?: number, end?: number): this;
indexOf(value: string | number | Buffer, byteOffset?: number, encoding?: string): number;
lastIndexOf(value: string | number | Buffer, byteOffset?: number, encoding?: string): number;
includes(value: string | number | Buffer, byteOffset?: number, encoding?: string): boolean;
/**
* Allocates a new buffer containing the given {str}.
*
* @param str String to store in buffer.
* @param encoding encoding to use, optional. Default is 'utf8'
*/
constructor (str: string, encoding?: string);
/**
* Allocates a new buffer of {size} octets.
*
* @param size count of octets to allocate.
*/
constructor (size: number);
/**
* Allocates a new buffer containing the given {array} of octets.
*
* @param array The octets to store.
*/
constructor (array: Uint8Array);
/**
* Produces a Buffer backed by the same allocated memory as
* the given {ArrayBuffer}.
*
*
* @param arrayBuffer The ArrayBuffer with which to share memory.
*/
constructor (arrayBuffer: ArrayBuffer);
/**
* Allocates a new buffer containing the given {array} of octets.
*
* @param array The octets to store.
*/
constructor (array: any[]);
/**
* Copies the passed {buffer} data onto a new {Buffer} instance.
*
* @param buffer The buffer to copy.
*/
constructor (buffer: Buffer);
prototype: Buffer;
/**
* Allocates a new Buffer using an {array} of octets.
*
* @param array
*/
static from(array: any[]): Buffer;
/**
* When passed a reference to the .buffer property of a TypedArray instance,
* the newly created Buffer will share the same allocated memory as the TypedArray.
* The optional {byteOffset} and {length} arguments specify a memory range
* within the {arrayBuffer} that will be shared by the Buffer.
*
* @param arrayBuffer The .buffer property of a TypedArray or a new ArrayBuffer()
* @param byteOffset
* @param length
*/
static from(arrayBuffer: ArrayBuffer, byteOffset?: number, length?: number): Buffer;
/**
* Copies the passed {buffer} data onto a new Buffer instance.
*
* @param buffer
*/
static from(buffer: Buffer): Buffer;
/**
* Creates a new Buffer containing the given JavaScript string {str}.
* If provided, the {encoding} parameter identifies the character encoding.
* If not provided, {encoding} defaults to 'utf8'.
*
* @param str
*/
static from(str: string, encoding?: string): Buffer;
/**
* Returns true if {obj} is a Buffer
*
* @param obj object to test.
*/
static isBuffer(obj: any): obj is Buffer;
/**
* Returns true if {encoding} is a valid encoding argument.
* Valid string encodings in Node 0.12: 'ascii'|'utf8'|'utf16le'|'ucs2'(alias of 'utf16le')|'base64'|'binary'(deprecated)|'hex'
*
* @param encoding string to test.
*/
static isEncoding(encoding: string): boolean;
/**
* Gives the actual byte length of a string. encoding defaults to 'utf8'.
* This is not the same as String.prototype.length since that returns the number of characters in a string.
*
* @param string string to test.
* @param encoding encoding used to evaluate (defaults to 'utf8')
*/
static byteLength(string: string, encoding?: string): number;
/**
* Returns a buffer which is the result of concatenating all the buffers in the list together.
*
* If the list has no items, or if the totalLength is 0, then it returns a zero-length buffer.
* If the list has exactly one item, then the first item of the list is returned.
* If the list has more than one item, then a new Buffer is created.
*
* @param list An array of Buffer objects to concatenate
* @param totalLength Total length of the buffers when concatenated.
* If totalLength is not provided, it is read from the buffers in the list. However, this adds an additional loop to the function, so it is faster to provide the length explicitly.
*/
static concat(list: Buffer[], totalLength?: number): Buffer;
/**
* The same as buf1.compare(buf2).
*/
static compare(buf1: Buffer, buf2: Buffer): number;
/**
* Allocates a new buffer of {size} octets.
*
* @param size count of octets to allocate.
* @param fill if specified, buffer will be initialized by calling buf.fill(fill).
* If parameter is omitted, buffer will be filled with zeros.
* @param encoding encoding used for call to buf.fill while initalizing
*/
static alloc(size: number, fill?: string | Buffer | number, encoding?: string): Buffer;
/**
* Allocates a new buffer of {size} octets, leaving memory not initialized, so the contents
* of the newly created Buffer are unknown and may contain sensitive data.
*
* @param size count of octets to allocate
*/
static allocUnsafe(size: number): Buffer;
/**
* Allocates a new non-pooled buffer of {size} octets, leaving memory not initialized, so the contents
* of the newly created Buffer are unknown and may contain sensitive data.
*
* @param size count of octets to allocate
*/
static allocUnsafeSlow(size: number): Buffer;
}
}
\ No newline at end of file
/*! safe-buffer. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
/* eslint-disable node/no-deprecated-api */
var buffer = require('buffer')
var Buffer = buffer.Buffer
// alternative to using Object.keys for old browsers
function copyProps (src, dst) {
for (var key in src) {
dst[key] = src[key]
}
}
if (Buffer.from && Buffer.alloc && Buffer.allocUnsafe && Buffer.allocUnsafeSlow) {
module.exports = buffer
} else {
// Copy properties from require('buffer')
copyProps(buffer, exports)
exports.Buffer = SafeBuffer
}
function SafeBuffer (arg, encodingOrOffset, length) {
return Buffer(arg, encodingOrOffset, length)
}
SafeBuffer.prototype = Object.create(Buffer.prototype)
// Copy static methods from Buffer
copyProps(Buffer, SafeBuffer)
SafeBuffer.from = function (arg, encodingOrOffset, length) {
if (typeof arg === 'number') {
throw new TypeError('Argument must not be a number')
}
return Buffer(arg, encodingOrOffset, length)
}
SafeBuffer.alloc = function (size, fill, encoding) {
if (typeof size !== 'number') {
throw new TypeError('Argument must be a number')
}
var buf = Buffer(size)
if (fill !== undefined) {
if (typeof encoding === 'string') {
buf.fill(fill, encoding)
} else {
buf.fill(fill)
}
} else {
buf.fill(0)
}
return buf
}
SafeBuffer.allocUnsafe = function (size) {
if (typeof size !== 'number') {
throw new TypeError('Argument must be a number')
}
return Buffer(size)
}
SafeBuffer.allocUnsafeSlow = function (size) {
if (typeof size !== 'number') {
throw new TypeError('Argument must be a number')
}
return buffer.SlowBuffer(size)
}
{
"name": "safe-buffer",
"description": "Safer Node.js Buffer API",
"version": "5.2.1",
"author": {
"name": "Feross Aboukhadijeh",
"email": "feross@feross.org",
"url": "https://feross.org"
},
"bugs": {
"url": "https://github.com/feross/safe-buffer/issues"
},
"devDependencies": {
"standard": "*",
"tape": "^5.0.0"
},
"homepage": "https://github.com/feross/safe-buffer",
"keywords": [
"buffer",
"buffer allocate",
"node security",
"safe",
"safe-buffer",
"security",
"uninitialized"
],
"license": "MIT",
"main": "index.js",
"types": "index.d.ts",
"repository": {
"type": "git",
"url": "git://github.com/feross/safe-buffer.git"
},
"scripts": {
"test": "standard && tape test/*.js"
},
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
]
}
'use strict';
module.exports = function (req, time) {
if (req.timeoutTimer) {
return req;
}
var delays = isNaN(time) ? time : {socket: time, connect: time};
var host = req._headers ? (' to ' + req._headers.host) : '';
if (delays.connect !== undefined) {
req.timeoutTimer = setTimeout(function timeoutHandler() {
req.abort();
var e = new Error('Connection timed out on request' + host);
e.code = 'ETIMEDOUT';
req.emit('error', e);
}, delays.connect);
}
// Clear the connection timeout timer once a socket is assigned to the
// request and is connected.
req.on('socket', function assign(socket) {
// Socket may come from Agent pool and may be already connected.
if (!(socket.connecting || socket._connecting)) {
connect();
return;
}
socket.once('connect', connect);
});
function clear() {
if (req.timeoutTimer) {
clearTimeout(req.timeoutTimer);
req.timeoutTimer = null;
}
}
function connect() {
clear();
if (delays.socket !== undefined) {
// Abort the request if there is no activity on the socket for more
// than `delays.socket` milliseconds.
req.setTimeout(delays.socket, function socketTimeoutHandler() {
req.abort();
var e = new Error('Socket timed out on request' + host);
e.code = 'ESOCKETTIMEDOUT';
req.emit('error', e);
});
}
}
return req.on('error', clear);
};
The MIT License (MIT)
Copyright (c) Vsevolod Strukchinsky <floatdrop@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
{
"name": "timed-out",
"version": "4.0.1",
"description": "Emit `ETIMEDOUT` or `ESOCKETTIMEDOUT` when ClientRequest is hanged",
"license": "MIT",
"repository": "floatdrop/timed-out",
"author": {
"name": "Vsevolod Strukchinsky",
"email": "floatdrop@gmail.com"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "xo && mocha"
},
"files": [
"index.js"
],
"keywords": [
"http",
"https",
"get",
"got",
"url",
"uri",
"request",
"util",
"utility",
"simple"
],
"devDependencies": {
"mocha": "*",
"xo": "^0.16.0"
}
}
# timed-out [![Build Status](https://travis-ci.org/floatdrop/timed-out.svg?branch=master)](https://travis-ci.org/floatdrop/timed-out)
> Timeout HTTP/HTTPS requests
Emit Error object with `code` property equal `ETIMEDOUT` or `ESOCKETTIMEDOUT` when ClientRequest is hanged.
## Usage
```js
var get = require('http').get;
var timeout = require('timed-out');
var req = get('http://www.google.ru');
timeout(req, 2000); // Set 2 seconds limit
```
### API
#### timedout(request, time)
##### request
*Required*
Type: [`ClientRequest`](http://nodejs.org/api/http.html#http_class_http_clientrequest)
The request to watch on.
##### time
*Required*
Type: `number` or `object`
Time in milliseconds to wait for `connect` event on socket and also time to wait on inactive socket.
Or you can pass Object with following fields:
- `connect` - time to wait for connection
- `socket` - time to wait for activity on socket
## License
MIT © [Vsevolod Strukchinsky](floatdrop@gmail.com)
'use strict';
var uniqueRandom = require('unique-random');
module.exports = function (arr) {
var rand = uniqueRandom(0, arr.length - 1);
return function () {
return arr[rand()];
};
};
The MIT License (MIT)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
{
"name": "unique-random-array",
"version": "1.0.1",
"description": "Get consecutively unique elements from an array",
"keywords": [
"unique",
"uniq",
"random",
"rand",
"number",
"single",
"generate",
"non-repeating",
"array",
"arr",
"item",
"element"
],
"license": "MIT",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"files": [
"index.js"
],
"repository": "sindresorhus/unique-random-array",
"scripts": {
"test": "ava"
},
"dependencies": {
"unique-random": "^1.0.0"
},
"devDependencies": {
"ava": "*"
},
"engines": {
"node": ">=0.10.0"
}
}
# unique-random-array [![Build Status](https://travis-ci.org/sindresorhus/unique-random-array.svg?branch=master)](https://travis-ci.org/sindresorhus/unique-random-array)
> Get consecutively unique elements from an array
Useful for things like slideshows where you don't want to have the same slide twice in a row.
## Install
```sh
$ npm install --save unique-random-array
```
## Usage
```js
var uniqueRandomArray = require('unique-random-array');
var rand = uniqueRandomArray([1, 2, 3, 4]);
console.log(rand(), rand(), rand(), rand());
//=> 4 2 1 4
```
## API
### uniqueRandomArray(input)
Returns a function that when called will return a random element that's never the same as the previous.
#### input
*Required*
Type: `array`
## Related
- [unique-random](https://github.com/sindresorhus/unique-random) - Generate random numbers that are consecutively unique
- [random-int](https://github.com/sindresorhus/random-int) - Generate a random integer
- [random-float](https://github.com/sindresorhus/random-float) - Generate a random float
- [random-item](https://github.com/sindresorhus/random-item) - Get a random item from an array
- [random-obj-key](https://github.com/sindresorhus/random-obj-key) - Get a random key from an object
- [random-obj-prop](https://github.com/sindresorhus/random-obj-prop) - Get a random property from an object
- [crypto-random-string](https://github.com/sindresorhus/crypto-random-string) - Generate a cryptographically strong random string
## License
MIT © [Sindre Sorhus](http://sindresorhus.com)
'use strict';
module.exports = function (min, max) {
var prev;
return function rand() {
var num = Math.floor(Math.random() * (max - min + 1) + min);
return prev = num === prev && min !== max ? rand() : num;
};
};
{
"name": "unique-random",
"version": "1.0.0",
"description": "Generate random numbers that are consecutively unique",
"keywords": [
"unique",
"random",
"rand",
"number",
"single",
"generate",
"non-repeating"
],
"license": "MIT",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "http://sindresorhus.com"
},
"files": [
"index.js"
],
"repository": "sindresorhus/unique-random",
"scripts": {
"test": "mocha"
},
"devDependencies": {
"mocha": "*"
},
"engines": {
"node": ">=0.10.0"
}
}
# unique-random [![Build Status](https://travis-ci.org/sindresorhus/unique-random.svg?branch=master)](https://travis-ci.org/sindresorhus/unique-random)
> Generate random numbers that are consecutively unique.
Useful for eg. slideshows where you don't want to have the same slide twice in a row.
## Install
```sh
$ npm install --save unique-random
```
## Usage
```js
var rand = require('unique-random')(1, 10);
console.log(rand(), rand(), rand());
//=> 5 2 6
```
## API
### uniqueRandom(*min*, *max*)
Returns a function which when called will return a random number that's never the same as the previous number.
## License
MIT © [Sindre Sorhus](http://sindresorhus.com)
'use strict';
const PassThrough = require('stream').PassThrough;
const zlib = require('zlib');
module.exports = res => {
// TODO: use Array#includes when targeting Node.js 6
if (['gzip', 'deflate'].indexOf(res.headers['content-encoding']) === -1) {
return res;
}
const unzip = zlib.createUnzip();
const stream = new PassThrough();
stream.httpVersion = res.httpVersion;
stream.headers = res.headers;
stream.rawHeaders = res.rawHeaders;
stream.trailers = res.trailers;
stream.rawTrailers = res.rawTrailers;
stream.setTimeout = res.setTimeout.bind(res);
stream.statusCode = res.statusCode;
stream.statusMessage = res.statusMessage;
stream.socket = res.socket;
unzip.on('error', err => {
if (err.code === 'Z_BUF_ERROR') {
stream.end();
return;
}
stream.emit('error', err);
});
res.pipe(unzip).pipe(stream);
return stream;
};
`The MIT License (MIT)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
{
"name": "unzip-response",
"version": "2.0.1",
"description": "Unzip a HTTP response if needed",
"license": "MIT",
"repository": "sindresorhus/unzip-response",
"maintainers": [
{
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
{
"name": "Vsevolod Strukchinsky",
"email": "floatdrop@gmail.com",
"url": "github.com/floatdrop"
}
],
"engines": {
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"http",
"unzip",
"zlib",
"gzip",
"deflate",
"incoming",
"message",
"response",
"stream"
],
"devDependencies": {
"ava": "*",
"get-stream": "^2.3.0",
"pify": "^2.3.0",
"rfpify": "^1.0.0",
"xo": "*"
},
"xo": {
"esnext": true
}
}
# unzip-response [![Build Status](https://travis-ci.org/sindresorhus/unzip-response.svg?branch=master)](https://travis-ci.org/sindresorhus/unzip-response)
> Unzip a HTTP response if needed
Unzips the response from [`http.request`](https://nodejs.org/api/http.html#http_http_request_options_callback) if it's gzipped/deflated, otherwise just passes it through.
## Install
```
$ npm install --save unzip-response
```
## Usage
```js
const http = require('http');
const unzipResponse = require('unzip-response');
http.get('http://sindresorhus.com', res => {
res = unzipResponse(res);
});
```
## License
MIT © [Sindre Sorhus](https://sindresorhus.com)
'use strict';
var url = require('url');
var prependHttp = require('prepend-http');
module.exports = function (x) {
var withProtocol = prependHttp(x);
var parsed = url.parse(withProtocol);
if (withProtocol !== x) {
parsed.protocol = null;
}
return parsed;
};
The MIT License (MIT)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
{
"name": "url-parse-lax",
"version": "1.0.0",
"description": "url.parse() with support for protocol-less URLs & IPs",
"license": "MIT",
"repository": "sindresorhus/url-parse-lax",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "node test.js"
},
"files": [
"index.js"
],
"keywords": [
"url",
"uri",
"parse",
"parser",
"loose",
"lax",
"protocol",
"less",
"protocol-less",
"ip",
"ipv4",
"ipv6"
],
"dependencies": {
"prepend-http": "^1.0.1"
},
"devDependencies": {
"ava": "0.0.4"
}
}
# url-parse-lax [![Build Status](https://travis-ci.org/sindresorhus/url-parse-lax.svg?branch=master)](https://travis-ci.org/sindresorhus/url-parse-lax)
> [`url.parse()`](https://nodejs.org/docs/latest/api/url.html#url_url_parse_urlstr_parsequerystring_slashesdenotehost) with support for protocol-less URLs & IPs
## Install
```
$ npm install --save url-parse-lax
```
## Usage
```js
var urlParseLax = require('url-parse-lax');
urlParseLax('sindresorhus.com');
/*
{
protocol: null,
slashes: true,
auth: null,
host: 'sindresorhus.com',
port: null,
hostname: 'sindresorhus.com',
hash: null,
search: null,
query: null,
pathname: '/',
path: '/',
href: 'http://sindresorhus.com/'
}
*/
urlParseLax('[2001:db8::]:8000');
/*
{
protocol: null,
slashes: true,
auth: null,
host: '[2001:db8::]:8000',
port: '8000',
hostname: '2001:db8::',
hash: null,
search: null,
query: null,
pathname: '/',
path: '/',
href: 'http://[2001:db8::]:8000/'
}
*/
```
And with the built-in `url.parse()`:
```js
var url = require('url');
url.parse('sindresorhus.com');
/*
{
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: null,
query: null,
pathname: 'sindresorhus',
path: 'sindresorhus',
href: 'sindresorhus'
}
*/
url.parse('[2001:db8::]:8000');
/*
{
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: null,
query: null,
pathname: '[2001:db8::]:8000',
path: '[2001:db8::]:8000',
href: '[2001:db8::]:8000'
}
*/
```
## License
MIT © [Sindre Sorhus](http://sindresorhus.com)