Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

got

Package Overview
Dependencies
Maintainers
2
Versions
178
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

got - npm Package Compare versions

Comparing version 11.0.0-beta.1 to 11.0.0

dist/source/core/utils/weakable-map.d.ts

9

dist/source/as-promise/core.d.ts
/// <reference types="node" />
import { URL } from 'url';
import { Options, NormalizedOptions, Defaults, ResponseType } from './types';
import Request, { RequestError } from '../core';
import { Options, NormalizedOptions, Defaults, ResponseType, Response } from './types';
import Request from '../core';
export declare const knownBodyTypes: string[];
export declare const parseBody: (body: Buffer, responseType: ResponseType, encoding?: string | undefined) => unknown;
export declare const parseBody: (response: Response<unknown>, responseType: ResponseType, encoding?: string | undefined) => unknown;
export default class PromisableRequest extends Request {
['constructor']: typeof PromisableRequest;
options: NormalizedOptions;
_throwHttpErrors: boolean;
static normalizeArguments(url?: string | URL, nonNormalizedOptions?: Options, defaults?: Defaults): NormalizedOptions;
static mergeOptions(...sources: Options[]): NormalizedOptions;
_beforeError(error: RequestError): Promise<void>;
_beforeError(error: Error): Promise<void>;
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const is_1 = require("@sindresorhus/is");
const types_1 = require("./types");
const core_1 = require("../core");

@@ -10,15 +11,21 @@ if (!core_1.knownHookEvents.includes('beforeRetry')) {

// @ts-ignore The error is: Not all code paths return a value.
exports.parseBody = (body, responseType, encoding) => {
if (responseType === 'text') {
return body.toString(encoding);
exports.parseBody = (response, responseType, encoding) => {
const { rawBody } = response;
try {
if (responseType === 'text') {
return rawBody.toString(encoding);
}
if (responseType === 'json') {
return rawBody.length === 0 ? '' : JSON.parse(rawBody.toString());
}
if (responseType === 'buffer') {
return Buffer.from(rawBody);
}
if (!exports.knownBodyTypes.includes(responseType)) {
throw new TypeError(`Unknown body type '${responseType}'`);
}
}
if (responseType === 'json') {
return body.length === 0 ? '' : JSON.parse(body.toString());
catch (error) {
throw new types_1.ParseError(error, response);
}
if (responseType === 'buffer') {
return Buffer.from(body);
}
if (!exports.knownBodyTypes.includes(responseType)) {
throw new TypeError(`Unknown body type '${responseType}'`);
}
};

@@ -87,2 +94,6 @@ class PromisableRequest extends core_1.default {

}
// JSON mode
if (options.responseType === 'json' && options.headers.accept === undefined) {
options.headers.accept = 'application/json';
}
return options;

@@ -98,3 +109,5 @@ }

async _beforeError(error) {
const isHTTPError = error instanceof core_1.HTTPError;
if (!(error instanceof core_1.RequestError)) {
error = new core_1.RequestError(error.message, error, this);
}
try {

@@ -107,13 +120,9 @@ for (const hook of this.options.hooks.beforeError) {

catch (error_) {
this.destroy(error_);
this.destroy(new core_1.RequestError(error_.message, error_, this));
return;
}
if (this._throwHttpErrors && !isHTTPError) {
this.destroy(error);
}
else {
this.emit('error', error);
}
// Let the promise decide whether to abort or not
this.emit('error', error);
}
}
exports.default = PromisableRequest;

@@ -23,10 +23,11 @@ "use strict";

let retryCount = 0;
let body;
let globalRequest;
let globalResponse;
const emitter = new events_1.EventEmitter();
const promise = new PCancelable((resolve, reject, onCancel) => {
const makeRequest = () => {
if (options.responseType === 'json' && options.headers.accept === undefined) {
options.headers.accept = 'application/json';
}
// Support retries
// `options.throwHttpErrors` needs to be always true,
// so the HTTP errors are caught and the request is retried.
// The error is **eventually** thrown if the user value is true.
const { throwHttpErrors } = options;

@@ -37,5 +38,5 @@ if (!throwHttpErrors) {

const request = new core_1.default(options.url, options);
request._throwHttpErrors = throwHttpErrors;
request._noPipe = true;
onCancel(() => request.destroy());
globalRequest = request;
request.once('response', async (response) => {

@@ -53,7 +54,9 @@ response.retryCount = retryCount;

// Download body
let rawBody;
try {
body = await getStream.buffer(request);
rawBody = await getStream.buffer(request);
response.rawBody = rawBody;
}
catch (error) {
request._beforeError(new types_1.ReadError(error, options, response));
request._beforeError(new types_1.ReadError(error, request));
return;

@@ -63,10 +66,9 @@ }

try {
response.body = core_1.parseBody(body, options.responseType, options.encoding);
response.body = core_1.parseBody(response, options.responseType, options.encoding);
}
catch (error) {
// Fallback to `utf8`
response.body = body.toString('utf8');
response.body = rawBody.toString();
if (isOk()) {
const parseError = new types_1.ParseError(error, response, options);
request._beforeError(parseError);
request._beforeError(error);
return;

@@ -110,5 +112,6 @@ }

if (throwHttpErrors && !isOk()) {
reject(new types_1.HTTPError(response, options));
reject(new types_1.HTTPError(response));
return;
}
globalResponse = response;
resolve(options.resolveBodyOnly ? response.body : response);

@@ -142,3 +145,3 @@ });

request.destroy();
reject(new types_1.RequestError(error_.message, error, request.options));
reject(new types_1.RequestError(error_.message, error, request));
return;

@@ -160,3 +163,3 @@ }

request.destroy();
reject(new types_1.RequestError(error_.message, error, request.options));
reject(new types_1.RequestError(error_.message, error, request));
return;

@@ -189,4 +192,5 @@ }

const newPromise = (async () => {
// Wait until downloading has ended
await promise;
return core_1.parseBody(body, responseType);
return core_1.parseBody(globalResponse, responseType, options.encoding);
})();

@@ -197,3 +201,3 @@ Object.defineProperties(newPromise, Object.getOwnPropertyDescriptors(promise));

promise.json = () => {
if (body === undefined && options.headers.accept === undefined) {
if (!globalRequest.writableFinished && options.headers.accept === undefined) {
options.headers.accept = 'application/json';

@@ -200,0 +204,0 @@ }

@@ -67,3 +67,3 @@ /// <reference types="node" />

readonly response: Response;
constructor(error: Error, response: Response, options: NormalizedOptions);
constructor(error: Error, response: Response);
}

@@ -70,0 +70,0 @@ export interface CancelableRequest<T extends Response | Response['body'] = Response['body']> extends PCancelable<T>, RequestEvents<CancelableRequest<T>> {

@@ -17,4 +17,5 @@ "use strict";

class ParseError extends core_1.RequestError {
constructor(error, response, options) {
super(`${error.message} in "${options.url.toString()}"`, error, options);
constructor(error, response) {
const { options } = response.request;
super(`${error.message} in "${options.url.toString()}"`, error, response.request);
this.name = 'ParseError';

@@ -21,0 +22,0 @@ Object.defineProperty(this, 'response', {

@@ -28,2 +28,4 @@ /// <reference types="node" />

declare const kStartedReading: unique symbol;
declare const kStopReading: unique symbol;
declare const kTriggerRead: unique symbol;
export declare const kIsNormalizedAlready: unique symbol;

@@ -124,3 +126,2 @@ export interface Agents {

dnsCache?: CacheableLookup;
cacheableRequest?: (options: string | URL | http.RequestOptions, callback?: (response: http.ServerResponse | ResponseLike) => void) => CacheableRequest.Emitter;
http2: boolean;

@@ -179,2 +180,3 @@ allowGetBody: boolean;

body: T;
rawBody: Buffer;
retryCount: number;

@@ -197,17 +199,17 @@ }

code?: string;
}>, options: NormalizedOptions, requestOrResponse?: Request | Response);
}>, self: Request | NormalizedOptions);
}
export declare class MaxRedirectsError extends RequestError {
readonly response: Response;
constructor(response: Response, maxRedirects: number, options: NormalizedOptions);
constructor(request: Request);
}
export declare class HTTPError extends RequestError {
readonly response: Response;
constructor(response: Response, options: NormalizedOptions);
constructor(response: Response);
}
export declare class CacheError extends RequestError {
constructor(error: Error, options: NormalizedOptions);
constructor(error: Error, request: Request);
}
export declare class UploadError extends RequestError {
constructor(error: Error, options: NormalizedOptions, request: Request);
constructor(error: Error, request: Request);
}

@@ -217,6 +219,6 @@ export declare class TimeoutError extends RequestError {

readonly event: string;
constructor(error: TimedOutTimeoutError, timings: Timings, options: NormalizedOptions);
constructor(error: TimedOutTimeoutError, timings: Timings, request: Request);
}
export declare class ReadError extends RequestError {
constructor(error: Error, options: NormalizedOptions, response: Response);
constructor(error: Error, request: Request);
}

@@ -232,2 +234,4 @@ export declare class UnsupportedProtocolError extends RequestError {

[kUploadedSize]: number;
[kStopReading]: boolean;
[kTriggerRead]: boolean;
[kBodySize]?: number;

@@ -247,3 +251,2 @@ [kServerResponsesPiped]: Set<ServerResponse>;

redirects: string[];
errored: boolean;
constructor(url: string | URL, options?: Options, defaults?: Defaults);

@@ -256,2 +259,3 @@ static normalizeArguments(url?: string | URL, options?: Options, defaults?: Defaults): NormalizedOptions;

_onRequest(request: ClientRequest): void;
_createCacheableRequest(url: URL, options: RequestOptions): Promise<ClientRequest | ResponseLike>;
_makeRequest(): Promise<void>;

@@ -266,2 +270,3 @@ _beforeError(error: Error): Promise<void>;

get aborted(): boolean;
get socket(): Socket | undefined;
get downloadProgress(): Progress;

@@ -268,0 +273,0 @@ get uploadProgress(): Progress;

@@ -25,2 +25,3 @@ "use strict";

const options_to_url_1 = require("./utils/options-to-url");
const weakable_map_1 = require("./utils/weakable-map");
const kRequest = Symbol('request');

@@ -37,2 +38,4 @@ const kResponse = Symbol('response');

const kStartedReading = Symbol('startedReading');
const kStopReading = Symbol('stopReading');
const kTriggerRead = Symbol('triggerRead');
exports.kIsNormalizedAlready = Symbol('isNormalizedAlready');

@@ -54,22 +57,3 @@ const supportsBrotli = is_1.default.string(process.versions.brotli);

}
const cacheFn = async (url, options) => new Promise((resolve, reject) => {
// TODO: Remove `utils/url-to-options.ts` when `cacheable-request` is fixed
Object.assign(options, url_to_options_1.default(url));
// `http-cache-semantics` checks this
delete options.url;
// TODO: `cacheable-request` is incorrectly typed
const cacheRequest = options.cacheableRequest(options, resolve);
// Restore options
options.url = url;
cacheRequest.once('error', (error) => {
if (error instanceof CacheableRequest.RequestError) {
// TODO: `options` should be `normalizedOptions`
reject(new RequestError(error.message, error, options));
return;
}
// TODO: `options` should be `normalizedOptions`
reject(new CacheError(error, options));
});
cacheRequest.once('request', resolve);
});
const cacheableStore = new weakable_map_1.default();
const waitForOpenFile = async (file) => new Promise((resolve, reject) => {

@@ -115,3 +99,3 @@ const onError = (error) => {

class RequestError extends Error {
constructor(message, error, options, requestOrResponse) {
constructor(message, error, self) {
var _a;

@@ -122,27 +106,25 @@ super(message);

this.code = error.code;
Object.defineProperty(this, 'options', {
// This fails because of TS 3.7.2 useDefineForClassFields
// Ref: https://github.com/microsoft/TypeScript/issues/34972
enumerable: false,
value: options
});
if (requestOrResponse instanceof http_1.IncomingMessage) {
if (self instanceof Request) {
Object.defineProperty(this, 'request', {
enumerable: false,
value: self
});
Object.defineProperty(this, 'response', {
enumerable: false,
value: requestOrResponse
value: self[kResponse]
});
Object.defineProperty(this, 'request', {
Object.defineProperty(this, 'options', {
// This fails because of TS 3.7.2 useDefineForClassFields
// Ref: https://github.com/microsoft/TypeScript/issues/34972
enumerable: false,
value: requestOrResponse.request
value: self.options
});
}
else if (requestOrResponse instanceof Request) {
Object.defineProperty(this, 'request', {
else {
Object.defineProperty(this, 'options', {
// This fails because of TS 3.7.2 useDefineForClassFields
// Ref: https://github.com/microsoft/TypeScript/issues/34972
enumerable: false,
value: requestOrResponse
value: self
});
Object.defineProperty(this, 'response', {
enumerable: false,
value: requestOrResponse[kResponse]
});
}

@@ -165,9 +147,5 @@ this.timings = (_a = this.request) === null || _a === void 0 ? void 0 : _a.timings;

class MaxRedirectsError extends RequestError {
constructor(response, maxRedirects, options) {
super(`Redirected ${maxRedirects} times. Aborting.`, {}, options);
constructor(request) {
super(`Redirected ${request.options.maxRedirects} times. Aborting.`, {}, request);
this.name = 'MaxRedirectsError';
Object.defineProperty(this, 'response', {
enumerable: false,
value: response
});
}

@@ -177,9 +155,5 @@ }

class HTTPError extends RequestError {
constructor(response, options) {
super(`Response code ${response.statusCode} (${response.statusMessage})`, {}, options);
constructor(response) {
super(`Response code ${response.statusCode} (${response.statusMessage})`, {}, response.request);
this.name = 'HTTPError';
Object.defineProperty(this, 'response', {
enumerable: false,
value: response
});
}

@@ -189,4 +163,4 @@ }

class CacheError extends RequestError {
constructor(error, options) {
super(error.message, error, options);
constructor(error, request) {
super(error.message, error, request);
this.name = 'CacheError';

@@ -197,4 +171,4 @@ }

class UploadError extends RequestError {
constructor(error, options, request) {
super(error.message, error, options, request);
constructor(error, request) {
super(error.message, error, request);
this.name = 'UploadError';

@@ -205,4 +179,4 @@ }

class TimeoutError extends RequestError {
constructor(error, timings, options) {
super(error.message, error, options);
constructor(error, timings, request) {
super(error.message, error, request);
this.name = 'TimeoutError';

@@ -215,4 +189,4 @@ this.event = error.event;

class ReadError extends RequestError {
constructor(error, options, response) {
super(error.message, error, options, response);
constructor(error, request) {
super(error.message, error, request);
this.name = 'ReadError';

@@ -249,3 +223,4 @@ }

this.redirects = [];
this.errored = false;
this[kStopReading] = false;
this[kTriggerRead] = false;
// TODO: Remove this when targeting Node.js >= 12

@@ -436,16 +411,16 @@ this._progressCallbacks = [];

// `options.cookieJar`
if (options.cookieJar) {
let { setCookie, getCookieString } = options.cookieJar;
// Horrible `tough-cookie` check
const { cookieJar } = options;
if (cookieJar) {
let { setCookie, getCookieString } = cookieJar;
is_1.assert.function_(setCookie);
is_1.assert.function_(getCookieString);
/* istanbul ignore next: Horrible `tough-cookie` v3 check */
if (setCookie.length === 4 && getCookieString.length === 0) {
setCookie = util_1.promisify(setCookie.bind(options.cookieJar));
getCookieString = util_1.promisify(getCookieString.bind(options.cookieJar));
options.cookieJar = {
setCookie: setCookie.bind(cookieJar),
getCookieString: getCookieString.bind(getCookieString)
};
}
else if (setCookie.length !== 2) {
throw new TypeError('`options.cookieJar.setCookie` needs to be an async function with 2 arguments');
}
else if (getCookieString.length !== 1) {
throw new TypeError('`options.cookieJar.getCookieString` needs to be an async function with 1 argument');
}
options.cookieJar = { setCookie, getCookieString };
}

@@ -470,5 +445,7 @@ // `options.searchParams`

// `options.cache`
if (options.cache && !options.cacheableRequest) {
// Better memory management, so we don't have to generate a new object every time
options.cacheableRequest = new CacheableRequest(((requestOptions, handler) => requestOptions[kRequest](requestOptions, handler)), options.cache);
const { cache } = options;
if (cache) {
if (!cacheableStore.has(cache)) {
cacheableStore.set(cache, new CacheableRequest(((requestOptions, handler) => requestOptions[kRequest](requestOptions, handler)), cache));
}
}

@@ -649,4 +626,13 @@ // `options.dnsCache`

response.on('error', (error) => {
this._beforeError(new ReadError(error, options, response));
this._beforeError(new ReadError(error, this));
});
response.once('aborted', () => {
if (this.aborted) {
return;
}
this._beforeError(new ReadError({
name: 'Error',
message: 'The server aborted the pending request'
}, this));
});
this.emit('downloadProgress', this.downloadProgress);

@@ -694,3 +680,3 @@ const rawCookies = response.headers['set-cookie'];

if (this.redirects.length >= options.maxRedirects) {
this._beforeError(new MaxRedirectsError(typedResponse, options.maxRedirects, options));
this._beforeError(new MaxRedirectsError(this));
return;

@@ -735,3 +721,3 @@ }

if (options.throwHttpErrors && !isOk) {
await this._beforeError(new HTTPError(typedResponse, options));
await this._beforeError(new HTTPError(typedResponse));
if (this.destroyed) {

@@ -741,5 +727,4 @@ return;

}
// We need to call `_read()` only when the Request stream is flowing
response.on('readable', () => {
if (this.readableFlowing) {
if (this[kTriggerRead]) {
this._read();

@@ -783,6 +768,6 @@ }

if (error instanceof timed_out_1.TimeoutError) {
error = new TimeoutError(error, this.timings, options);
error = new TimeoutError(error, this.timings, this);
}
else {
error = new RequestError(error.message, error, options, this);
error = new RequestError(error.message, error, this);
}

@@ -799,3 +784,3 @@ this._beforeError(error);

options.body.once('error', (error) => {
this._beforeError(new UploadError(error, options, this));
this._beforeError(new UploadError(error, this));
});

@@ -820,2 +805,22 @@ options.body.once('end', () => {

}
async _createCacheableRequest(url, options) {
return new Promise((resolve, reject) => {
// TODO: Remove `utils/url-to-options.ts` when `cacheable-request` is fixed
Object.assign(options, url_to_options_1.default(url));
// `http-cache-semantics` checks this
delete options.url;
// This is ugly
const cacheRequest = cacheableStore.get(options.cache)(options, resolve);
// Restore options
options.url = url;
cacheRequest.once('error', (error) => {
if (error instanceof CacheableRequest.CacheError) {
reject(new CacheError(error, this));
return;
}
reject(error);
});
cacheRequest.once('request', resolve);
});
}
async _makeRequest() {

@@ -877,3 +882,3 @@ var _a, _b;

const realFn = (_b = options.request, (_b !== null && _b !== void 0 ? _b : fallbackFn));
const fn = options.cacheableRequest ? cacheFn : realFn;
const fn = options.cache ? this._createCacheableRequest.bind(this) : realFn;
if (agent && !options.http2) {

@@ -915,17 +920,16 @@ options.agent = agent[isHttps ? 'https' : 'http'];

}
throw new RequestError(error.message, error, options, this);
throw new RequestError(error.message, error, this);
}
}
async _beforeError(error) {
this.errored = true;
this[kStopReading] = true;
if (!(error instanceof RequestError)) {
error = new RequestError(error.message, error, this.options, this);
error = new RequestError(error.message, error, this);
}
try {
const { response } = error;
if (response && is_1.default.undefined(response.body)) {
response.body = await getStream(response, {
...this.options,
encoding: this._readableState.encoding
});
if (response) {
response.setEncoding(this._readableState.encoding);
response.rawBody = await getStream.buffer(response);
response.body = response.rawBody.toString();
}

@@ -941,3 +945,3 @@ }

catch (error_) {
error = new RequestError(error_.message, error_, this.options, this);
error = new RequestError(error_.message, error_, this);
}

@@ -947,5 +951,12 @@ this.destroy(error);

_read() {
if (kResponse in this && !this.errored) {
this[kTriggerRead] = true;
const response = this[kResponse];
if (response && !this[kStopReading]) {
// We cannot put this in the `if` above
// because `.read()` also triggers the `end` event
if (response.readableLength) {
this[kTriggerRead] = false;
}
let data;
while ((data = this[kResponse].read()) !== null) {
while ((data = response.read()) !== null) {
this[kDownloadedSize] += data.length;

@@ -1016,4 +1027,8 @@ this[kStartedReading] = true;

_destroy(error, callback) {
var _a;
if (kRequest in this) {
this[kRequest].abort();
// TODO: Remove the next `if` when https://github.com/nodejs/node/issues/32851 gets fixed
if (!((_a = this[kResponse]) === null || _a === void 0 ? void 0 : _a.complete)) {
this[kRequest].abort();
}
}

@@ -1028,3 +1043,3 @@ else {

if (error !== null && !is_1.default.undefined(error) && !(error instanceof RequestError)) {
error = new RequestError(error.message, error, this.options, this);
error = new RequestError(error.message, error, this);
}

@@ -1041,2 +1056,6 @@ callback(error);

}
get socket() {
var _a;
return (_a = this[kRequest]) === null || _a === void 0 ? void 0 : _a.socket;
}
get downloadProgress() {

@@ -1043,0 +1062,0 @@ let percent;

{
"name": "got",
"version": "11.0.0-beta.1",
"version": "11.0.0",
"description": "Human-friendly and powerful HTTP request library for Node.js",

@@ -69,3 +69,3 @@ "license": "MIT",

"@types/sinon": "^9.0.0",
"@types/tough-cookie": "^2.3.5",
"@types/tough-cookie": "^4.0.0",
"@typescript-eslint/eslint-plugin": "^2.27.0",

@@ -93,3 +93,3 @@ "@typescript-eslint/parser": "^2.27.0",

"to-readable-stream": "^2.1.0",
"tough-cookie": "^3.0.0",
"tough-cookie": "^4.0.0",
"typescript": "3.7.5",

@@ -96,0 +96,0 @@ "xo": "^0.29.0"

@@ -787,2 +787,8 @@ <div align="center">

##### rawBody
Type: `Buffer`
The raw result of the request.
##### url

@@ -938,2 +944,6 @@

##### .socket
The same as `response.socket`.
##### .on('error', error)

@@ -1044,3 +1054,3 @@

const handler = (options, next) => {
if (options.stream) {
if (options.isStream) {
// It's a Stream

@@ -1317,3 +1327,3 @@ return next(options);

Got implements [RFC 7234](http://httpwg.org/specs/rfc7234.html) compliant HTTP caching which works out of the box in-memory and is easily pluggable with a wide range of storage adapters. Fresh cache entries are served directly from the cache, and stale cache entries are revalidated with `If-None-Match`/`If-Modified-Since` headers. You can read more about the underlying cache behavior in the [`cacheable-request` documentation](https://github.com/lukechilds/cacheable-request). For DNS cache, Got uses [`cacheable-lookup`](https://github.com/szmarczak/cacheable-lookup).
Got implements [RFC 7234](https://httpwg.org/specs/rfc7234.html) compliant HTTP caching which works out of the box in-memory and is easily pluggable with a wide range of storage adapters. Fresh cache entries are served directly from the cache, and stale cache entries are revalidated with `If-None-Match`/`If-Modified-Since` headers. You can read more about the underlying cache behavior in the [`cacheable-request` documentation](https://github.com/lukechilds/cacheable-request). For DNS cache, Got uses [`cacheable-lookup`](https://github.com/szmarczak/cacheable-lookup).

@@ -1320,0 +1330,0 @@ You can use the JavaScript `Map` type as an in-memory cache:

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc