Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require Node.js 12 #1575

Merged
merged 3 commits into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ jobs:
- 15
- 14
- 12
- 10
os:
- ubuntu-latest
- macos-latest
Expand Down
11 changes: 5 additions & 6 deletions benchmark/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
'use strict';
import {URL} from 'url';
import https = require('https');
import * as https from 'https';
szmarczak marked this conversation as resolved.
Show resolved Hide resolved
import axios from 'axios';
import Benchmark = require('benchmark');
import * as Benchmark from 'benchmark';
import fetch from 'node-fetch';
import request = require('request');
import got from '../source';
import Request, {kIsNormalizedAlready} from '../source/core';
import * as request from 'request';
import got from '../source/index';
import Request, {kIsNormalizedAlready} from '../source/core/index';

const {normalizeArguments} = Request;

Expand Down
4 changes: 2 additions & 2 deletions benchmark/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {AddressInfo} from 'net';
import https = require('https');
import * as https from 'https';
// @ts-expect-error No types
import createCert = require('create-cert');
import * as createCert from 'create-cert';

(async () => {
const keys = await createCert({days: 365, commonName: 'localhost'});
Expand Down
16 changes: 13 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"funding": "https://github.com/sindresorhus/got?sponsor=1",
"main": "dist/source",
"engines": {
"node": ">=10.19.0"
"node": ">=12.13"
},
"scripts": {
"test": "xo && npm run build && nyc ava",
Expand Down Expand Up @@ -68,9 +68,11 @@
"@types/request": "^2.48.5",
"@types/sinon": "^9.0.9",
"@types/tough-cookie": "^4.0.0",
"ava": "^3.14.0",
"ava": "^3.15.0",
"axios": "^0.21.0",
"benchmark": "^2.1.4",
"body-parser": "^1.19.0",
"create-cert": "^1.0.6",
"create-test-server": "^3.0.1",
"del-cli": "^3.0.1",
"delay": "^4.4.0",
Expand All @@ -84,6 +86,7 @@
"p-event": "^4.2.0",
"pem": "^1.14.4",
"pify": "^5.0.0",
"request": "^2.88.2",
"sinon": "^9.2.2",
"slow-stream": "0.0.4",
"tempy": "^1.0.0",
Expand All @@ -103,6 +106,9 @@
"rewritePaths": {
"test/": "dist/test/"
}
},
"nonSemVerExperiments": {
"nextGenConfig": true
}
},
"nyc": {
Expand All @@ -124,10 +130,14 @@
],
"rules": {
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-duplicate-imports": "off",
"node/no-deprecated-api": "off",
"node/prefer-global/url": "off",
"node/prefer-global/url-search-params": "off",
"import/no-anonymous-default-export": "off",
"@typescript-eslint/no-implicit-any-catch": "off"
"@typescript-eslint/no-implicit-any-catch": "off",
"unicorn/import-index": "off",
"import/no-useless-path-segments": "off"
}
},
"runkitExampleFilename": "./documentation/examples/runkit-example.js"
Expand Down
4 changes: 2 additions & 2 deletions source/as-promise/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {EventEmitter} from 'events';
import is from '@sindresorhus/is';
import PCancelable = require('p-cancelable');
import * as PCancelable from 'p-cancelable';
import {
NormalizedOptions,
CancelableRequest,
Expand All @@ -10,7 +10,7 @@ import {
CancelError
} from './types';
import parseBody from './parse-body';
import Request from '../core';
import Request from '../core/index';
import proxyEvents from '../core/utils/proxy-events';
import getBuffer from '../core/utils/get-buffer';
import {isResponseOk} from '../core/utils/is-response-ok';
Expand Down
4 changes: 2 additions & 2 deletions source/as-promise/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import PCancelable = require('p-cancelable');
import * as PCancelable from 'p-cancelable';
import Request, {
Options,
Response,
RequestError,
RequestEvents
} from '../core';
} from '../core/index';

/**
All parsing methods supported by Got.
Expand Down
2 changes: 1 addition & 1 deletion source/core/calculate-retry-delay.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {RetryFunction} from '.';
import {RetryFunction} from './index';

type Returns<T extends (...args: any) => unknown, V> = (...args: Parameters<T>) => V;

Expand Down
40 changes: 13 additions & 27 deletions source/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import {ReadStream} from 'fs';
import {URL, URLSearchParams} from 'url';
import {Socket} from 'net';
import {SecureContextOptions, DetailedPeerCertificate} from 'tls';
import http = require('http');
import * as http from 'http';
import {ClientRequest, RequestOptions, IncomingMessage, ServerResponse, request as httpRequest} from 'http';
import https = require('https');
import * as https from 'https';
import timer, {ClientRequestWithTimings, Timings, IncomingMessageWithTimings} from '@szmarczak/http-timer';
import CacheableLookup from 'cacheable-lookup';
import CacheableRequest = require('cacheable-request');
import * as CacheableRequest from 'cacheable-request';
import decompressResponse = require('decompress-response');
// @ts-expect-error Missing types
import http2wrapper = require('http2-wrapper');
import * as http2wrapper from 'http2-wrapper';
import lowercaseKeys = require('lowercase-keys');
import ResponseLike = require('responselike');
import is, {assert} from '@sindresorhus/is';
Expand Down Expand Up @@ -1361,7 +1361,6 @@ export default class Request extends Duplex implements RequestEvents<Request> {
[kOriginalResponse]?: IncomingMessageWithTimings;
[kRequest]?: ClientRequest;
_noPipe?: boolean;
_progressCallbacks: Array<() => void>;

declare options: NormalizedOptions;
declare requestUrl: string;
Expand All @@ -1388,9 +1387,6 @@ export default class Request extends Duplex implements RequestEvents<Request> {
this[kJobs] = [];
this.retryCount = 0;

// TODO: Remove this when targeting Node.js >= 12
this._progressCallbacks = [];

const unlockWrite = (): void => this._unlockWrite();
const lockWrite = (): void => this._lockWrite();

Expand Down Expand Up @@ -2635,21 +2631,17 @@ export default class Request extends Duplex implements RequestEvents<Request> {
return;
}

this._progressCallbacks.push((): void => {
this[kUploadedSize] += Buffer.byteLength(chunk, encoding);

const progress = this.uploadProgress;

if (progress.percent < 1) {
this.emit('uploadProgress', progress);
}
});

// TODO: What happens if it's from cache? Then this[kRequest] won't be defined.

this[kRequest]!.write(chunk, encoding!, (error?: Error | null) => {
if (!error && this._progressCallbacks.length > 0) {
this._progressCallbacks.shift()!();
if (!error) {
this[kUploadedSize] += Buffer.byteLength(chunk, encoding);

const progress = this.uploadProgress;

if (progress.percent < 1) {
this.emit('uploadProgress', progress);
}
}

callback(error);
Expand All @@ -2658,11 +2650,6 @@ export default class Request extends Duplex implements RequestEvents<Request> {

_final(callback: (error?: Error | null) => void): void {
const endRequest = (): void => {
// FIX: Node.js 10 calls the write callback AFTER the end callback!
while (this._progressCallbacks.length !== 0) {
this._progressCallbacks.shift()!();
}

// We need to check if `this[kRequest]` is present,
// because it isn't when we use cache.
if (!(kRequest in this)) {
Expand Down Expand Up @@ -2703,8 +2690,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
if (kRequest in this) {
this[kCancelTimeouts]!();

// TODO: Remove the next `if` when these get fixed:
// - https://github.com/nodejs/node/issues/32851
// TODO: Remove the next `if` when targeting Node.js 14.
if (!this[kResponse]?.complete) {
this[kRequest]!.destroy();
}
Expand Down
2 changes: 1 addition & 1 deletion source/core/utils/is-response-ok.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Response} from '..';
import {Response} from '../index';

export const isResponseOk = (response: Response): boolean => {
const {statusCode} = response;
Expand Down
2 changes: 1 addition & 1 deletion source/core/utils/timed-out.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import net = require('net');
import * as net from 'net';
import {ClientRequest, IncomingMessage} from 'http';
import unhandler from './unhandle';

Expand Down
2 changes: 1 addition & 1 deletion source/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
StreamOptions
} from './types';
import createRejection from './as-promise/create-rejection';
import Request, {kIsNormalizedAlready, setNonEnumerableProperties, Defaults} from './core';
import Request, {kIsNormalizedAlready, setNonEnumerableProperties, Defaults} from './core/index';
import deepFreeze from './utils/deep-freeze';

const errors = {
Expand Down
2 changes: 1 addition & 1 deletion source/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
UnsupportedProtocolError,
UploadError
} from './as-promise';
import Request from './core';
import Request from './core/index';

// `type-fest` utilities
type Except<ObjectType, KeysType extends keyof ObjectType> = Pick<ObjectType, Exclude<keyof ObjectType, KeysType>>;
Expand Down
2 changes: 1 addition & 1 deletion test/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Agent as HttpAgent} from 'http';
import {Agent as HttpsAgent} from 'https';
import {Socket} from 'net';
import test, {Constructor} from 'ava';
import sinon = require('sinon');
import * as sinon from 'sinon';
import withServer, {withHttpsServer} from './helpers/with-server';

const createAgentSpy = <T extends HttpsAgent>(AgentClass: Constructor): {agent: T; spy: sinon.SinonSpy} => {
Expand Down
5 changes: 2 additions & 3 deletions test/arguments.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/* eslint-disable node/no-deprecated-api */
import {parse, URL, URLSearchParams} from 'url';
import test from 'ava';
import {Handler} from 'express';
import pEvent = require('p-event');
import got, {StrictOptions} from '../source';
import * as pEvent from 'p-event';
import got, {StrictOptions} from '../source/index';
import withServer, {withBodyParsingServer} from './helpers/with-server';

const echoUrl: Handler = (request, response) => {
Expand Down
10 changes: 5 additions & 5 deletions test/cache.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {promisify} from 'util';
import {gzip} from 'zlib';
import test from 'ava';
import pEvent = require('p-event');
import getStream = require('get-stream');
import * as pEvent from 'p-event';
import * as getStream from 'get-stream';
import {Handler} from 'express';
import got, {Response} from '../source';
import withServer from './helpers/with-server';
import CacheableLookup from 'cacheable-lookup';
import delay = require('delay');
import * as delay from 'delay';
import got, {Response} from '../source/index';
import withServer from './helpers/with-server';

const cacheEndpoint: Handler = (_request, response) => {
response.setHeader('Cache-Control', 'public, max-age=60');
Expand Down
10 changes: 5 additions & 5 deletions test/cancel.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {EventEmitter} from 'events';
import {Readable as ReadableStream} from 'stream';
import stream = require('stream');
import * as stream from 'stream';
import test from 'ava';
import delay = require('delay');
import pEvent = require('p-event');
import getStream = require('get-stream');
import * as delay from 'delay';
import * as pEvent from 'p-event';
import * as getStream from 'get-stream';
import {Handler} from 'express';
import got, {CancelError} from '../source';
import got, {CancelError} from '../source/index';
import slowDataStream from './helpers/slow-data-stream';
import {GlobalClock} from './helpers/types';
import {ExtendedHttpTestServer} from './helpers/create-http-test-server';
Expand Down
8 changes: 4 additions & 4 deletions test/cookies.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import net = require('net');
import * as net from 'net';
import test from 'ava';
import toughCookie = require('tough-cookie');
import delay = require('delay');
import got from '../source';
import * as toughCookie from 'tough-cookie';
import * as delay from 'delay';
import got from '../source/index';
import withServer from './helpers/with-server';

test('reads a cookie', withServer, async (t, server, got) => {
Expand Down
2 changes: 1 addition & 1 deletion test/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import got, {
Headers,
Hooks,
RequestFunction
} from '../source';
} from '../source/index';
import withServer from './helpers/with-server';

const echoHeaders: Handler = (request, response) => {
Expand Down
10 changes: 5 additions & 5 deletions test/error.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {promisify} from 'util';
import net = require('net');
import http = require('http');
import stream = require('stream');
import * as net from 'net';
import * as http from 'http';
import * as stream from 'stream';
import test from 'ava';
import getStream = require('get-stream');
import * as getStream from 'get-stream';
import is from '@sindresorhus/is';
import got, {RequestError, HTTPError, TimeoutError} from '../source';
import got, {RequestError, HTTPError, TimeoutError} from '../source/index';
import withServer from './helpers/with-server';

const pStreamPipeline = promisify(stream.pipeline);
Expand Down
6 changes: 3 additions & 3 deletions test/gzip.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {promisify} from 'util';
import zlib = require('zlib');
import * as zlib from 'zlib';
import test from 'ava';
import getStream = require('get-stream');
import * as getStream from 'get-stream';
import withServer from './helpers/with-server';
import {HTTPError, ReadError} from '../source';
import {HTTPError, ReadError} from '../source/index';

const testContent = 'Compressible response content.\n';
const testContentUncompressed = 'Uncompressed response content.\n';
Expand Down
8 changes: 4 additions & 4 deletions test/headers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fs = require('fs');
import path = require('path');
import * as fs from 'fs';
import * as path from 'path';
import test from 'ava';
import {Handler} from 'express';
import FormData = require('form-data');
import got, {Headers} from '../source';
import * as FormData from 'form-data';
import got, {Headers} from '../source/index';
import withServer from './helpers/with-server';

const supportsBrotli = typeof (process.versions as any).brotli === 'string';
Expand Down
2 changes: 1 addition & 1 deletion test/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import got, {HTTPError} from '../source';
import got, {HTTPError} from '../source/index';
import withServer from './helpers/with-server';

test('works', withServer, async (t, server) => {
Expand Down
10 changes: 5 additions & 5 deletions test/helpers/create-http-test-server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import http = require('http');
import net = require('net');
import express = require('express');
import pify = require('pify');
import bodyParser = require('body-parser');
import * as http from 'http';
import * as net from 'net';
import * as express from 'express';
import * as pify from 'pify';
import * as bodyParser from 'body-parser';

export type HttpServerOptions = {
bodyParser?: express.NextFunction | false;
Expand Down
Loading