mirror of
https://github.com/musix-org/musix-oss
synced 2025-06-17 01:16:00 +00:00
Modules
This commit is contained in:
22
node_modules/retry-request/index.d.ts
generated
vendored
Normal file
22
node_modules/retry-request/index.d.ts
generated
vendored
Normal file
@ -0,0 +1,22 @@
|
||||
declare module 'retry-request' {
|
||||
import * as request from 'request';
|
||||
|
||||
namespace retryRequest {
|
||||
function getNextRetryDelay(retryNumber: number): void;
|
||||
interface Options {
|
||||
objectMode?: boolean,
|
||||
request?: typeof request,
|
||||
retries?: number,
|
||||
noResponseRetries?: number,
|
||||
currentRetryAttempt?: number,
|
||||
shouldRetryFn?: (response: request.RequestResponse) => boolean
|
||||
}
|
||||
}
|
||||
|
||||
function retryRequest(requestOpts: request.Options, opts: retryRequest.Options, callback?: request.RequestCallback)
|
||||
: { abort: () => void };
|
||||
function retryRequest(requestOpts: request.Options, callback?: request.RequestCallback)
|
||||
: { abort: () => void };
|
||||
|
||||
export = retryRequest;
|
||||
}
|
217
node_modules/retry-request/index.js
generated
vendored
Normal file
217
node_modules/retry-request/index.js
generated
vendored
Normal file
@ -0,0 +1,217 @@
|
||||
'use strict';
|
||||
|
||||
var through = require('through2');
|
||||
var debug = require('debug')('retry-request');
|
||||
|
||||
var DEFAULTS = {
|
||||
objectMode: false,
|
||||
retries: 2,
|
||||
noResponseRetries: 2,
|
||||
currentRetryAttempt: 0,
|
||||
shouldRetryFn: function (response) {
|
||||
var retryRanges = [
|
||||
// https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
|
||||
// 1xx - Retry (Informational, request still processing)
|
||||
// 2xx - Do not retry (Success)
|
||||
// 3xx - Do not retry (Redirect)
|
||||
// 4xx - Do not retry (Client errors)
|
||||
// 429 - Retry ("Too Many Requests")
|
||||
// 5xx - Retry (Server errors)
|
||||
[100, 199],
|
||||
[429, 429],
|
||||
[500, 599]
|
||||
];
|
||||
|
||||
var statusCode = response.statusCode;
|
||||
debug(`Response status: ${statusCode}`);
|
||||
|
||||
var range;
|
||||
while ((range = retryRanges.shift())) {
|
||||
if (statusCode >= range[0] && statusCode <= range[1]) {
|
||||
// Not a successful status or redirect.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
function retryRequest(requestOpts, opts, callback) {
|
||||
var streamMode = typeof arguments[arguments.length - 1] !== 'function';
|
||||
|
||||
if (typeof opts === 'function') {
|
||||
callback = opts;
|
||||
}
|
||||
|
||||
opts = opts || DEFAULTS;
|
||||
|
||||
if (typeof opts.objectMode === 'undefined') {
|
||||
opts.objectMode = DEFAULTS.objectMode;
|
||||
}
|
||||
if (typeof opts.request === 'undefined') {
|
||||
try {
|
||||
opts.request = require('request');
|
||||
} catch (e) {
|
||||
throw new Error('A request library must be provided to retry-request.');
|
||||
}
|
||||
}
|
||||
if (typeof opts.retries !== 'number') {
|
||||
opts.retries = DEFAULTS.retries;
|
||||
}
|
||||
if (typeof opts.currentRetryAttempt !== 'number') {
|
||||
opts.currentRetryAttempt = DEFAULTS.currentRetryAttempt;
|
||||
}
|
||||
if (typeof opts.noResponseRetries !== 'number') {
|
||||
opts.noResponseRetries = DEFAULTS.noResponseRetries;
|
||||
}
|
||||
if (typeof opts.shouldRetryFn !== 'function') {
|
||||
opts.shouldRetryFn = DEFAULTS.shouldRetryFn;
|
||||
}
|
||||
|
||||
var currentRetryAttempt = opts.currentRetryAttempt;
|
||||
|
||||
var numNoResponseAttempts = 0;
|
||||
var streamResponseHandled = false;
|
||||
|
||||
var retryStream;
|
||||
var requestStream;
|
||||
var delayStream;
|
||||
|
||||
var activeRequest;
|
||||
var retryRequest = {
|
||||
abort: function () {
|
||||
if (activeRequest && activeRequest.abort) {
|
||||
activeRequest.abort();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (streamMode) {
|
||||
retryStream = through({ objectMode: opts.objectMode });
|
||||
retryStream.abort = resetStreams;
|
||||
}
|
||||
|
||||
if (currentRetryAttempt > 0) {
|
||||
retryAfterDelay(currentRetryAttempt);
|
||||
} else {
|
||||
makeRequest();
|
||||
}
|
||||
|
||||
if (streamMode) {
|
||||
return retryStream;
|
||||
} else {
|
||||
return retryRequest;
|
||||
}
|
||||
|
||||
function resetStreams() {
|
||||
delayStream = null;
|
||||
|
||||
if (requestStream) {
|
||||
requestStream.abort && requestStream.abort();
|
||||
requestStream.cancel && requestStream.cancel();
|
||||
|
||||
if (requestStream.destroy) {
|
||||
requestStream.destroy();
|
||||
} else if (requestStream.end) {
|
||||
requestStream.end();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function makeRequest() {
|
||||
currentRetryAttempt++;
|
||||
debug(`Current retry attempt: ${currentRetryAttempt}`);
|
||||
|
||||
if (streamMode) {
|
||||
streamResponseHandled = false;
|
||||
|
||||
delayStream = through({ objectMode: opts.objectMode });
|
||||
requestStream = opts.request(requestOpts);
|
||||
|
||||
setImmediate(function () {
|
||||
retryStream.emit('request');
|
||||
});
|
||||
|
||||
requestStream
|
||||
// gRPC via google-cloud-node can emit an `error` as well as a `response`
|
||||
// Whichever it emits, we run with-- we can't run with both. That's what
|
||||
// is up with the `streamResponseHandled` tracking.
|
||||
.on('error', function (err) {
|
||||
if (streamResponseHandled) {
|
||||
return;
|
||||
}
|
||||
|
||||
streamResponseHandled = true;
|
||||
onResponse(err);
|
||||
})
|
||||
.on('response', function (resp, body) {
|
||||
if (streamResponseHandled) {
|
||||
return;
|
||||
}
|
||||
|
||||
streamResponseHandled = true;
|
||||
onResponse(null, resp, body);
|
||||
})
|
||||
.on('complete', retryStream.emit.bind(retryStream, 'complete'));
|
||||
|
||||
requestStream.pipe(delayStream);
|
||||
} else {
|
||||
activeRequest = opts.request(requestOpts, onResponse);
|
||||
}
|
||||
}
|
||||
|
||||
function retryAfterDelay(currentRetryAttempt) {
|
||||
if (streamMode) {
|
||||
resetStreams();
|
||||
}
|
||||
|
||||
var nextRetryDelay = getNextRetryDelay(currentRetryAttempt);
|
||||
debug(`Next retry delay: ${nextRetryDelay}`);
|
||||
|
||||
setTimeout(makeRequest, nextRetryDelay);
|
||||
}
|
||||
|
||||
function onResponse(err, response, body) {
|
||||
// An error such as DNS resolution.
|
||||
if (err) {
|
||||
numNoResponseAttempts++;
|
||||
|
||||
if (numNoResponseAttempts <= opts.noResponseRetries) {
|
||||
retryAfterDelay(numNoResponseAttempts);
|
||||
} else {
|
||||
if (streamMode) {
|
||||
retryStream.emit('error', err);
|
||||
retryStream.end();
|
||||
} else {
|
||||
callback(err, response, body);
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Send the response to see if we should try again.
|
||||
if (currentRetryAttempt <= opts.retries && opts.shouldRetryFn(response)) {
|
||||
retryAfterDelay(currentRetryAttempt);
|
||||
return;
|
||||
}
|
||||
|
||||
// No more attempts need to be made, just continue on.
|
||||
if (streamMode) {
|
||||
retryStream.emit('response', response);
|
||||
delayStream.pipe(retryStream);
|
||||
requestStream.on('error', function (err) {
|
||||
retryStream.destroy(err);
|
||||
});
|
||||
} else {
|
||||
callback(err, response, body);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = retryRequest;
|
||||
|
||||
function getNextRetryDelay(retryNumber) {
|
||||
return (Math.pow(2, retryNumber) * 1000) + Math.floor(Math.random() * 1000);
|
||||
}
|
||||
|
||||
module.exports.getNextRetryDelay = getNextRetryDelay;
|
20
node_modules/retry-request/license
generated
vendored
Normal file
20
node_modules/retry-request/license
generated
vendored
Normal file
@ -0,0 +1,20 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 Stephen Sawchuk
|
||||
|
||||
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.
|
78
node_modules/retry-request/package.json
generated
vendored
Normal file
78
node_modules/retry-request/package.json
generated
vendored
Normal file
@ -0,0 +1,78 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
"retry-request@4.1.1",
|
||||
"C:\\Users\\matia\\Musix"
|
||||
]
|
||||
],
|
||||
"_from": "retry-request@4.1.1",
|
||||
"_id": "retry-request@4.1.1",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-BINDzVtLI2BDukjWmjAIRZ0oglnCAkpP2vQjM3jdLhmT62h0xnQgciPwBRDAvHqpkPT2Wo1XuUyLyn6nbGrZQQ==",
|
||||
"_location": "/retry-request",
|
||||
"_optional": true,
|
||||
"_phantomChildren": {
|
||||
"ms": "2.1.2"
|
||||
},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "retry-request@4.1.1",
|
||||
"name": "retry-request",
|
||||
"escapedName": "retry-request",
|
||||
"rawSpec": "4.1.1",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "4.1.1"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/@google-cloud/common",
|
||||
"/google-gax"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/retry-request/-/retry-request-4.1.1.tgz",
|
||||
"_spec": "4.1.1",
|
||||
"_where": "C:\\Users\\matia\\Musix",
|
||||
"author": {
|
||||
"name": "Stephen Sawchuk",
|
||||
"email": "sawchuk@gmail.com"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/stephenplusplus/retry-request/issues"
|
||||
},
|
||||
"dependencies": {
|
||||
"debug": "^4.1.1",
|
||||
"through2": "^3.0.1"
|
||||
},
|
||||
"description": "Retry a request.",
|
||||
"devDependencies": {
|
||||
"async": "^3.0.1",
|
||||
"lodash.range": "^3.2.0",
|
||||
"mocha": "^6.1.4",
|
||||
"request": "^2.87.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=8.10.0"
|
||||
},
|
||||
"files": [
|
||||
"index.js",
|
||||
"index.d.ts",
|
||||
"license"
|
||||
],
|
||||
"homepage": "https://github.com/stephenplusplus/retry-request#readme",
|
||||
"keywords": [
|
||||
"request",
|
||||
"retry",
|
||||
"stream"
|
||||
],
|
||||
"license": "MIT",
|
||||
"main": "index.js",
|
||||
"name": "retry-request",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/stephenplusplus/retry-request.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "mocha --timeout 0"
|
||||
},
|
||||
"types": "index.d.ts",
|
||||
"version": "4.1.1"
|
||||
}
|
175
node_modules/retry-request/readme.md
generated
vendored
Normal file
175
node_modules/retry-request/readme.md
generated
vendored
Normal file
@ -0,0 +1,175 @@
|
||||
|
|
||||
|:-:
|
||||
|Retry a [request][request] with built-in [exponential backoff](https://developers.google.com/analytics/devguides/reporting/core/v3/coreErrors#backoff).
|
||||
|
||||
```sh
|
||||
$ npm install --save request
|
||||
$ npm install --save retry-request
|
||||
```
|
||||
```js
|
||||
var request = require('retry-request', {
|
||||
request: require('request')
|
||||
});
|
||||
```
|
||||
|
||||
It should work the same as `request` in both callback mode and stream mode.
|
||||
|
||||
Note: This module only works when used as a readable stream, i.e. POST requests aren't supported ([#3](https://github.com/stephenplusplus/retry-request/issues/3)).
|
||||
|
||||
## Do I need to install `request`?
|
||||
|
||||
Yes! You must independently install `request` and provide it to this library:
|
||||
|
||||
```js
|
||||
var request = require('retry-request', {
|
||||
request: require('request')
|
||||
});
|
||||
```
|
||||
|
||||
*The code will actually look for the `request` module automatically to save you this step. But, being explicit like in the example is also welcome.*
|
||||
|
||||
#### Callback
|
||||
|
||||
`urlThatReturns503` will be requested 3 total times before giving up and executing the callback.
|
||||
|
||||
```js
|
||||
request(urlThatReturns503, function (err, resp, body) {});
|
||||
```
|
||||
|
||||
#### Stream
|
||||
|
||||
`urlThatReturns503` will be requested 3 total times before giving up and emitting the `response` and `complete` event as usual.
|
||||
|
||||
```js
|
||||
request(urlThatReturns503)
|
||||
.on('error', function () {})
|
||||
.on('response', function () {})
|
||||
.on('complete', function () {});
|
||||
```
|
||||
|
||||
## Can I monitor what retry-request is doing internally?
|
||||
|
||||
Yes! This project uses [debug](https://www.npmjs.com/package/debug) to provide the current retry attempt, each response status, and the delay computed until the next retry attempt is made. To enable the debug mode, set the environment variable `DEBUG` to *retry-request*.
|
||||
|
||||
(Thanks for the implementation, @yihaozhadan!)
|
||||
|
||||
## request(requestOptions, [opts], [cb])
|
||||
|
||||
### requestOptions
|
||||
|
||||
Passed directly to `request`. See the list of options supported: https://github.com/request/request/#requestoptions-callback.
|
||||
|
||||
### opts *(optional)*
|
||||
|
||||
#### `opts.noResponseRetries`
|
||||
|
||||
Type: `Number`
|
||||
|
||||
Default: `2`
|
||||
|
||||
The number of times to retry after a response fails to come through, such as a DNS resolution error or a socket hangup.
|
||||
|
||||
```js
|
||||
var opts = {
|
||||
noResponseRetries: 0
|
||||
};
|
||||
|
||||
request(url, opts, function (err, resp, body) {
|
||||
// url was requested 1 time before giving up and
|
||||
// executing this callback.
|
||||
});
|
||||
```
|
||||
|
||||
#### `opts.objectMode`
|
||||
|
||||
Type: `Boolean`
|
||||
|
||||
Default: `false`
|
||||
|
||||
Set to `true` if your custom `opts.request` function returns a stream in object mode.
|
||||
|
||||
#### `opts.retries`
|
||||
|
||||
Type: `Number`
|
||||
|
||||
Default: `2`
|
||||
|
||||
```js
|
||||
var opts = {
|
||||
retries: 4
|
||||
};
|
||||
|
||||
request(urlThatReturns503, opts, function (err, resp, body) {
|
||||
// urlThatReturns503 was requested a total of 5 times
|
||||
// before giving up and executing this callback.
|
||||
});
|
||||
```
|
||||
|
||||
#### `opts.currentRetryAttempt`
|
||||
|
||||
Type: `Number`
|
||||
|
||||
Default: `0`
|
||||
|
||||
```js
|
||||
var opts = {
|
||||
currentRetryAttempt: 1
|
||||
};
|
||||
|
||||
request(urlThatReturns503, opts, function (err, resp, body) {
|
||||
// urlThatReturns503 was requested as if it already failed once.
|
||||
});
|
||||
```
|
||||
|
||||
#### `opts.shouldRetryFn`
|
||||
|
||||
Type: `Function`
|
||||
|
||||
Default: Returns `true` if [http.incomingMessage](https://nodejs.org/api/http.html#http_http_incomingmessage).statusCode is < 200 or >= 400.
|
||||
|
||||
```js
|
||||
var opts = {
|
||||
shouldRetryFn: function (incomingHttpMessage) {
|
||||
return incomingHttpMessage.statusMessage !== 'OK';
|
||||
}
|
||||
};
|
||||
|
||||
request(urlThatReturnsNonOKStatusMessage, opts, function (err, resp, body) {
|
||||
// urlThatReturnsNonOKStatusMessage was requested a
|
||||
// total of 3 times, each time using `opts.shouldRetryFn`
|
||||
// to decide if it should continue before giving up and
|
||||
// executing this callback.
|
||||
});
|
||||
```
|
||||
|
||||
#### `opts.request`
|
||||
|
||||
Type: `Function`
|
||||
|
||||
Default: `try { require('request') }`
|
||||
|
||||
If we cannot locate `request`, we will throw an error advising you to provide it explicitly.
|
||||
|
||||
*NOTE: If you override the request function, and it returns a stream in object mode, be sure to set `opts.objectMode` to `true`.*
|
||||
|
||||
```js
|
||||
var originalRequest = require('request').defaults({
|
||||
pool: {
|
||||
maxSockets: Infinity
|
||||
}
|
||||
});
|
||||
|
||||
var opts = {
|
||||
request: originalRequest
|
||||
};
|
||||
|
||||
request(urlThatReturns503, opts, function (err, resp, body) {
|
||||
// Your provided `originalRequest` instance was used.
|
||||
});
|
||||
```
|
||||
|
||||
### cb *(optional)*
|
||||
|
||||
Passed directly to `request`. See the callback section: https://github.com/request/request/#requestoptions-callback.
|
||||
|
||||
[request]: https://github.com/request/request
|
Reference in New Issue
Block a user