From dc75da8f69913221c37124a6fcdbe8934ad40037 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Tue, 5 Jan 2021 19:43:20 +0700 Subject: [PATCH 1/3] Require Node.js 12 --- .github/workflows/main.yml | 1 - package.json | 2 +- source/core/index.ts | 8 +------- test/https.ts | 18 +++--------------- tsconfig.json | 5 ++--- 5 files changed, 7 insertions(+), 27 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 2976a26bf..70997ee90 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -13,7 +13,6 @@ jobs: - 15 - 14 - 12 - - 10 os: - ubuntu-latest - macos-latest diff --git a/package.json b/package.json index 6385a2f27..d08bedf23 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/source/core/index.ts b/source/core/index.ts index 1b7c1bb3c..3b1bf1efd 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -2658,11 +2658,6 @@ export default class Request extends Duplex implements RequestEvents { _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)) { @@ -2703,8 +2698,7 @@ export default class Request extends Duplex implements RequestEvents { 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(); } diff --git a/test/https.ts b/test/https.ts index 28bd8cf48..4f846fa6e 100644 --- a/test/https.ts +++ b/test/https.ts @@ -406,8 +406,6 @@ test('invalid key passphrase', withHttpsServer(), async (t, server, got) => { }); const clientCert = clientResult.certificate; - const NODE_10 = process.versions.node.split('.')[0] === '10'; - const request = got({ https: { key: clientKey, @@ -416,19 +414,9 @@ test('invalid key passphrase', withHttpsServer(), async (t, server, got) => { } }); - // Node.JS 10 does not have an error code, it only has a mesage - if (NODE_10) { - try { - await request; - t.fail(); - } catch (error) { - t.true((error.message as string).includes('bad decrypt'), error.message); - } - } else { - await t.throwsAsync(request, { - code: 'ERR_OSSL_EVP_BAD_DECRYPT' - }); - } + await t.throwsAsync(request, { + code: 'ERR_OSSL_EVP_BAD_DECRYPT' + }); }); test('client certificate PFX', withHttpsServer(), async (t, server, got) => { diff --git a/tsconfig.json b/tsconfig.json index a0637d361..a834e9fde 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,10 +2,9 @@ "extends": "@sindresorhus/tsconfig", "compilerOptions": { "outDir": "dist", - "target": "es2018", // Node.js 10 + "target": "es2019", // Node.js 12 "lib": [ - "es2018", - "es2019.string" + "es2019" ], "useDefineForClassFields": false }, From 03677aa223d4605aadac39b4d704db1400be9cdf Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Tue, 5 Jan 2021 20:56:15 +0700 Subject: [PATCH 2/3] Prepare moving to ESM --- benchmark/index.ts | 11 +++++------ benchmark/server.ts | 4 ++-- package.json | 14 ++++++++++++-- source/as-promise/index.ts | 4 ++-- source/as-promise/types.ts | 4 ++-- source/core/calculate-retry-delay.ts | 2 +- source/core/index.ts | 8 ++++---- source/core/utils/is-response-ok.ts | 2 +- source/core/utils/timed-out.ts | 2 +- source/create.ts | 2 +- source/types.ts | 2 +- test/agent.ts | 2 +- test/arguments.ts | 5 ++--- test/cache.ts | 10 +++++----- test/cancel.ts | 10 +++++----- test/cookies.ts | 8 ++++---- test/create.ts | 2 +- test/error.ts | 10 +++++----- test/gzip.ts | 6 +++--- test/headers.ts | 8 ++++---- test/helpers.ts | 2 +- test/helpers/create-http-test-server.ts | 10 +++++----- test/helpers/create-https-test-server.ts | 10 +++++----- test/helpers/with-server.ts | 8 ++++---- test/hooks.ts | 12 ++++++------ test/http.ts | 10 +++++----- test/https.ts | 8 ++++---- test/merge-instances.ts | 2 +- test/normalize-arguments.ts | 2 +- test/pagination.ts | 6 +++--- test/post.ts | 16 ++++++++-------- test/progress.ts | 16 ++++++++-------- test/promise.ts | 2 +- test/redirects.ts | 4 ++-- test/response-parse.ts | 4 ++-- test/retry.ts | 8 ++++---- test/stream.ts | 14 +++++++------- test/timeout.ts | 14 +++++++------- test/unix-socket.ts | 2 +- test/url-to-options.ts | 9 ++++----- tsconfig.json | 5 ++++- 41 files changed, 145 insertions(+), 135 deletions(-) diff --git a/benchmark/index.ts b/benchmark/index.ts index a2b279034..9a1e27d43 100644 --- a/benchmark/index.ts +++ b/benchmark/index.ts @@ -1,12 +1,11 @@ -'use strict'; import {URL} from 'url'; -import https = require('https'); +import * as https from 'https'; 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; diff --git a/benchmark/server.ts b/benchmark/server.ts index 5069ea98f..8c5591e78 100644 --- a/benchmark/server.ts +++ b/benchmark/server.ts @@ -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'}); diff --git a/package.json b/package.json index d08bedf23..d4e7884c4 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", @@ -103,6 +106,9 @@ "rewritePaths": { "test/": "dist/test/" } + }, + "nonSemVerExperiments": { + "nextGenConfig": true } }, "nyc": { @@ -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" diff --git a/source/as-promise/index.ts b/source/as-promise/index.ts index 8f3698ede..677f8f31d 100644 --- a/source/as-promise/index.ts +++ b/source/as-promise/index.ts @@ -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, @@ -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'; diff --git a/source/as-promise/types.ts b/source/as-promise/types.ts index ab4d28623..c4572d9fc 100644 --- a/source/as-promise/types.ts +++ b/source/as-promise/types.ts @@ -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. diff --git a/source/core/calculate-retry-delay.ts b/source/core/calculate-retry-delay.ts index 56ea3c0db..6c9085ff0 100644 --- a/source/core/calculate-retry-delay.ts +++ b/source/core/calculate-retry-delay.ts @@ -1,4 +1,4 @@ -import {RetryFunction} from '.'; +import {RetryFunction} from './index'; type Returns unknown, V> = (...args: Parameters) => V; diff --git a/source/core/index.ts b/source/core/index.ts index 3b1bf1efd..80d8b2e25 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -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'; diff --git a/source/core/utils/is-response-ok.ts b/source/core/utils/is-response-ok.ts index fe4186869..866745f29 100644 --- a/source/core/utils/is-response-ok.ts +++ b/source/core/utils/is-response-ok.ts @@ -1,4 +1,4 @@ -import {Response} from '..'; +import {Response} from '../index'; export const isResponseOk = (response: Response): boolean => { const {statusCode} = response; diff --git a/source/core/utils/timed-out.ts b/source/core/utils/timed-out.ts index cea5b8e5b..ae14ce1ac 100644 --- a/source/core/utils/timed-out.ts +++ b/source/core/utils/timed-out.ts @@ -1,4 +1,4 @@ -import net = require('net'); +import * as net from 'net'; import {ClientRequest, IncomingMessage} from 'http'; import unhandler from './unhandle'; diff --git a/source/create.ts b/source/create.ts index ee2078d5a..77fbdaa73 100644 --- a/source/create.ts +++ b/source/create.ts @@ -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 = { diff --git a/source/types.ts b/source/types.ts index d78866740..8e62cf87c 100644 --- a/source/types.ts +++ b/source/types.ts @@ -22,7 +22,7 @@ import { UnsupportedProtocolError, UploadError } from './as-promise'; -import Request from './core'; +import Request from './core/index'; // `type-fest` utilities type Except = Pick>; diff --git a/test/agent.ts b/test/agent.ts index 0d2c24edf..0831efe5d 100644 --- a/test/agent.ts +++ b/test/agent.ts @@ -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 = (AgentClass: Constructor): {agent: T; spy: sinon.SinonSpy} => { diff --git a/test/arguments.ts b/test/arguments.ts index aedee1a59..8c8ca181c 100644 --- a/test/arguments.ts +++ b/test/arguments.ts @@ -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) => { diff --git a/test/cache.ts b/test/cache.ts index e9532211e..9e62df4f5 100644 --- a/test/cache.ts +++ b/test/cache.ts @@ -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'); diff --git a/test/cancel.ts b/test/cancel.ts index c13dbdbb6..63d62e131 100644 --- a/test/cancel.ts +++ b/test/cancel.ts @@ -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'; diff --git a/test/cookies.ts b/test/cookies.ts index 4abe04c6f..248a89df0 100644 --- a/test/cookies.ts +++ b/test/cookies.ts @@ -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) => { diff --git a/test/create.ts b/test/create.ts index 4b767ac95..b081038cc 100644 --- a/test/create.ts +++ b/test/create.ts @@ -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) => { diff --git a/test/error.ts b/test/error.ts index 6e7898e2d..93e9672bc 100644 --- a/test/error.ts +++ b/test/error.ts @@ -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); diff --git a/test/gzip.ts b/test/gzip.ts index 06d680a1f..455319cf4 100644 --- a/test/gzip.ts +++ b/test/gzip.ts @@ -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'; diff --git a/test/headers.ts b/test/headers.ts index e7830de45..d4a5b1518 100644 --- a/test/headers.ts +++ b/test/headers.ts @@ -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'; diff --git a/test/helpers.ts b/test/helpers.ts index 15bb4c266..1ed05e181 100644 --- a/test/helpers.ts +++ b/test/helpers.ts @@ -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) => { diff --git a/test/helpers/create-http-test-server.ts b/test/helpers/create-http-test-server.ts index bf3446a52..a6dac3d0f 100644 --- a/test/helpers/create-http-test-server.ts +++ b/test/helpers/create-http-test-server.ts @@ -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; diff --git a/test/helpers/create-https-test-server.ts b/test/helpers/create-https-test-server.ts index 4dc8a12d9..f822d7eb6 100644 --- a/test/helpers/create-https-test-server.ts +++ b/test/helpers/create-https-test-server.ts @@ -1,8 +1,8 @@ -import https = require('https'); -import net = require('net'); -import express = require('express'); -import pify = require('pify'); -import pem = require('pem'); +import * as https from 'https'; +import * as net from 'net'; +import * as express from 'express'; +import * as pify from 'pify'; +import * as pem from 'pem'; export type HttpsServerOptions = { commonName?: string; diff --git a/test/helpers/with-server.ts b/test/helpers/with-server.ts index 51d560ab2..fe50ddb54 100644 --- a/test/helpers/with-server.ts +++ b/test/helpers/with-server.ts @@ -1,12 +1,12 @@ import {promisify} from 'util'; import * as test from 'ava'; import is from '@sindresorhus/is'; -import http = require('http'); -import tempy = require('tempy'); +import * as http from 'http'; +import * as tempy from 'tempy'; import createHttpsTestServer, {ExtendedHttpsTestServer, HttpsServerOptions} from './create-https-test-server'; import createHttpTestServer, {ExtendedHttpTestServer, HttpServerOptions} from './create-http-test-server'; -import FakeTimers = require('@sinonjs/fake-timers'); -import got, {InstanceDefaults, Got} from '../../source'; +import * as FakeTimers from '@sinonjs/fake-timers'; +import got, {InstanceDefaults, Got} from '../../source/index'; import {ExtendedHttpServer, GlobalClock, InstalledClock} from './types'; export type RunTestWithServer = (t: test.ExecutionContext, server: ExtendedHttpTestServer, got: Got, clock: GlobalClock) => Promise | void; diff --git a/test/hooks.ts b/test/hooks.ts index 0a721d57a..55c8d0b79 100644 --- a/test/hooks.ts +++ b/test/hooks.ts @@ -1,14 +1,14 @@ import {URL} from 'url'; import {Agent as HttpAgent} from 'http'; import test, {Constructor} from 'ava'; -import nock = require('nock'); -import getStream = require('get-stream'); -import FormData = require('form-data'); -import sinon = require('sinon'); -import delay = require('delay'); +import * as nock from 'nock'; +import * as getStream from 'get-stream'; +import * as FormData from 'form-data'; +import * as sinon from 'sinon'; +import * as delay from 'delay'; import {Handler} from 'express'; import Responselike = require('responselike'); -import got, {RequestError, HTTPError, Response} from '../source'; +import got, {RequestError, HTTPError, Response} from '../source/index'; import withServer from './helpers/with-server'; const errorString = 'oops'; diff --git a/test/http.ts b/test/http.ts index 6acea36c4..fe70ce6e5 100644 --- a/test/http.ts +++ b/test/http.ts @@ -1,13 +1,13 @@ import {STATUS_CODES, Agent} from 'http'; +import * as os from 'os'; +import {isIPv4, isIPv6} from 'net'; import test from 'ava'; import {Handler} from 'express'; -import {isIPv4, isIPv6} from 'net'; -import nock = require('nock'); -import getStream = require('get-stream'); +import * as nock from 'nock'; +import * as getStream from 'get-stream'; import pEvent from 'p-event'; -import got, {HTTPError, UnsupportedProtocolError, CancelableRequest, ReadError} from '../source'; +import got, {HTTPError, UnsupportedProtocolError, CancelableRequest, ReadError} from '../source/index'; import withServer from './helpers/with-server'; -import os = require('os'); const IPv6supported = Object.values(os.networkInterfaces()).some(iface => iface?.some(addr => !addr.internal && addr.family === 'IPv6')); diff --git a/test/https.ts b/test/https.ts index 4f846fa6e..a9c4aa2fe 100644 --- a/test/https.ts +++ b/test/https.ts @@ -1,10 +1,10 @@ import test from 'ava'; -import got, {CancelableRequest} from '../source'; -import {withHttpsServer} from './helpers/with-server'; import {DetailedPeerCertificate} from 'tls'; import pEvent from 'p-event'; -import pify = require('pify'); -import pem = require('pem'); +import * as pify from 'pify'; +import * as pem from 'pem'; +import got, {CancelableRequest} from '../source/index'; +import {withHttpsServer} from './helpers/with-server'; const createPrivateKey = pify(pem.createPrivateKey); const createCSR = pify(pem.createCSR); diff --git a/test/merge-instances.ts b/test/merge-instances.ts index f32141e39..3bb79a43c 100644 --- a/test/merge-instances.ts +++ b/test/merge-instances.ts @@ -1,6 +1,6 @@ import test from 'ava'; import {Handler} from 'express'; -import got, {BeforeRequestHook, Got, Headers, NormalizedOptions} from '../source'; +import got, {BeforeRequestHook, Got, Headers, NormalizedOptions} from '../source/index'; import withServer from './helpers/with-server'; const echoHeaders: Handler = (request, response) => { diff --git a/test/normalize-arguments.ts b/test/normalize-arguments.ts index a620e741b..096fbdb94 100644 --- a/test/normalize-arguments.ts +++ b/test/normalize-arguments.ts @@ -1,6 +1,6 @@ import {URL, URLSearchParams} from 'url'; import test from 'ava'; -import got from '../source'; +import got from '../source/index'; test('should merge options replacing responseType', t => { const responseType = 'json'; diff --git a/test/pagination.ts b/test/pagination.ts index 804708792..754e99888 100644 --- a/test/pagination.ts +++ b/test/pagination.ts @@ -1,8 +1,8 @@ import {URL} from 'url'; import test from 'ava'; -import delay = require('delay'); -import getStream = require('get-stream'); -import got, {Response} from '../source'; +import * as delay from 'delay'; +import * as getStream from 'get-stream'; +import got, {Response} from '../source/index'; import withServer, {withBodyParsingServer} from './helpers/with-server'; import {ExtendedHttpTestServer} from './helpers/create-http-test-server'; diff --git a/test/post.ts b/test/post.ts index 58d2e814f..bde08b645 100644 --- a/test/post.ts +++ b/test/post.ts @@ -1,14 +1,14 @@ import {promisify} from 'util'; -import stream = require('stream'); -import fs = require('fs'); -import path = require('path'); +import * as stream from 'stream'; +import * as fs from 'fs'; +import * as path from 'path'; import test from 'ava'; -import delay = require('delay'); -import pEvent = require('p-event'); +import * as delay from 'delay'; +import * as pEvent from 'p-event'; import {Handler} from 'express'; -import getStream = require('get-stream'); -import toReadableStream = require('to-readable-stream'); -import got, {UploadError} from '../source'; +import * as getStream from 'get-stream'; +import * as toReadableStream from 'to-readable-stream'; +import got, {UploadError} from '../source/index'; import withServer from './helpers/with-server'; const pStreamPipeline = promisify(stream.pipeline); diff --git a/test/progress.ts b/test/progress.ts index 5945d7e90..610cba23f 100644 --- a/test/progress.ts +++ b/test/progress.ts @@ -1,15 +1,15 @@ import {promisify} from 'util'; -import stream = require('stream'); -import fs = require('fs'); -import SlowStream = require('slow-stream'); -import toReadableStream = require('to-readable-stream'); -import getStream = require('get-stream'); -import FormData = require('form-data'); -import tempy = require('tempy'); +import * as stream from 'stream'; +import * as fs from 'fs'; +import * as SlowStream from 'slow-stream'; +import * as toReadableStream from 'to-readable-stream'; +import * as getStream from 'get-stream'; +import * as FormData from 'form-data'; +import * as tempy from 'tempy'; import is from '@sindresorhus/is'; import test, {ExecutionContext} from 'ava'; import {Handler} from 'express'; -import {Progress} from '../source'; +import {Progress} from '../source/index'; import withServer from './helpers/with-server'; const checkEvents = (t: ExecutionContext, events: Progress[], bodySize?: number) => { diff --git a/test/promise.ts b/test/promise.ts index 043612d11..bca0ea8c0 100644 --- a/test/promise.ts +++ b/test/promise.ts @@ -1,7 +1,7 @@ import {ReadStream} from 'fs'; import {ClientRequest, IncomingMessage} from 'http'; import test from 'ava'; -import {Response, CancelError} from '../source'; +import {Response, CancelError} from '../source/index'; import withServer from './helpers/with-server'; test('emits request event as promise', withServer, async (t, server, got) => { diff --git a/test/redirects.ts b/test/redirects.ts index c2e555fa7..d35588fbc 100644 --- a/test/redirects.ts +++ b/test/redirects.ts @@ -1,7 +1,7 @@ import test from 'ava'; import {Handler} from 'express'; -import nock = require('nock'); -import got, {MaxRedirectsError} from '../source'; +import * as nock from 'nock'; +import got, {MaxRedirectsError} from '../source/index'; import withServer, {withHttpsServer} from './helpers/with-server'; const reachedHandler: Handler = (_request, response) => { diff --git a/test/response-parse.ts b/test/response-parse.ts index 963b6a0f2..7a336dbaf 100644 --- a/test/response-parse.ts +++ b/test/response-parse.ts @@ -1,7 +1,7 @@ import test from 'ava'; import {Handler} from 'express'; -import getStream = require('get-stream'); -import {HTTPError, ParseError} from '../source'; +import * as getStream from 'get-stream'; +import {HTTPError, ParseError} from '../source/index'; import withServer from './helpers/with-server'; const dog = {data: 'dog'}; diff --git a/test/retry.ts b/test/retry.ts index 4647cc7a4..be55e3d44 100644 --- a/test/retry.ts +++ b/test/retry.ts @@ -1,13 +1,13 @@ import {EventEmitter} from 'events'; import {PassThrough as PassThroughStream} from 'stream'; import {Socket} from 'net'; -import http = require('http'); +import * as http from 'http'; import test from 'ava'; import is from '@sindresorhus/is'; import {Handler} from 'express'; -import getStream = require('get-stream'); -import pEvent = require('p-event'); -import got, {HTTPError} from '../source'; +import * as getStream from 'get-stream'; +import * as pEvent from 'p-event'; +import got, {HTTPError} from '../source/index'; import withServer from './helpers/with-server'; const retryAfterOn413 = 2; diff --git a/test/stream.ts b/test/stream.ts index b2af5763b..686b052cc 100644 --- a/test/stream.ts +++ b/test/stream.ts @@ -1,15 +1,15 @@ import {promisify} from 'util'; -import fs = require('fs'); +import * as fs from 'fs'; import {PassThrough as PassThroughStream} from 'stream'; -import stream = require('stream'); +import * as stream from 'stream'; import test from 'ava'; import {Handler} from 'express'; -import toReadableStream = require('to-readable-stream'); -import getStream = require('get-stream'); -import pEvent = require('p-event'); -import FormData = require('form-data'); +import * as toReadableStream from 'to-readable-stream'; +import * as getStream from 'get-stream'; +import * as pEvent from 'p-event'; +import * as FormData from 'form-data'; import is from '@sindresorhus/is'; -import got, {RequestError} from '../source'; +import got, {RequestError} from '../source/index'; import withServer from './helpers/with-server'; const pStreamPipeline = promisify(stream.pipeline); diff --git a/test/timeout.ts b/test/timeout.ts index fc1cebdeb..6faf8d13a 100644 --- a/test/timeout.ts +++ b/test/timeout.ts @@ -1,16 +1,16 @@ import {promisify} from 'util'; import {EventEmitter} from 'events'; import {PassThrough as PassThroughStream} from 'stream'; -import stream = require('stream'); -import http = require('http'); -import net = require('net'); -import getStream = require('get-stream'); +import * as stream from 'stream'; +import * as http from 'http'; +import * as net from 'net'; +import * as getStream from 'get-stream'; import test from 'ava'; -import delay = require('delay'); +import * as delay from 'delay'; import CacheableLookup from 'cacheable-lookup'; import {Handler} from 'express'; -import pEvent = require('p-event'); -import got, {TimeoutError} from '../source'; +import * as pEvent from 'p-event'; +import got, {TimeoutError} from '../source/index'; import timedOut from '../source/core/utils/timed-out'; import slowDataStream from './helpers/slow-data-stream'; import {GlobalClock} from './helpers/types'; diff --git a/test/unix-socket.ts b/test/unix-socket.ts index 99a8beb11..3198d5ad1 100644 --- a/test/unix-socket.ts +++ b/test/unix-socket.ts @@ -1,7 +1,7 @@ import {format} from 'util'; import test from 'ava'; import {Handler} from 'express'; -import got from '../source'; +import got from '../source/index'; import {withSocketServer} from './helpers/with-server'; const okHandler: Handler = (_request, response) => { diff --git a/test/url-to-options.ts b/test/url-to-options.ts index a9aaf320f..0136f8fa2 100644 --- a/test/url-to-options.ts +++ b/test/url-to-options.ts @@ -1,11 +1,10 @@ -import url = require('url'); -import {URL} from 'url'; +import {parse as urlParse, URL} from 'url'; import test from 'ava'; import urlToOptions from '../source/core/utils/url-to-options'; test('converts node legacy URL to options', t => { const exampleUrl = 'https://user:password@github.com:443/say?hello=world#bang'; - const parsedUrl = url.parse(exampleUrl); + const parsedUrl = urlParse(exampleUrl); const options = urlToOptions(parsedUrl); const expected = { hash: '#bang', @@ -80,7 +79,7 @@ test('only adds port to options for URLs with ports', t => { test('does not concat null search to path', t => { const exampleUrl = 'https://github.com/'; - const parsedUrl = url.parse(exampleUrl); + const parsedUrl = urlParse(exampleUrl); t.is(parsedUrl.search, null); @@ -101,7 +100,7 @@ test('does not concat null search to path', t => { test('does not add null port to options', t => { const exampleUrl = 'https://github.com/'; - const parsedUrl = url.parse(exampleUrl); + const parsedUrl = urlParse(exampleUrl); t.is(parsedUrl.port, null); diff --git a/tsconfig.json b/tsconfig.json index a834e9fde..272fe16ee 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,7 +6,10 @@ "lib": [ "es2019" ], - "useDefineForClassFields": false + "useDefineForClassFields": false, + // "allowSyntheticDefaultImports": true, + // "esModuleInterop": true + // "module": "es2020" }, "include": [ "source", From 304c7a2ac0bc36bd6ec266162d22414ba8af9880 Mon Sep 17 00:00:00 2001 From: Szymon Marczak <36894700+szmarczak@users.noreply.github.com> Date: Tue, 5 Jan 2021 16:58:33 +0100 Subject: [PATCH 3/3] Update index.ts --- source/core/index.ts | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/source/core/index.ts b/source/core/index.ts index 80d8b2e25..5fdf0c6e5 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -1361,7 +1361,6 @@ export default class Request extends Duplex implements RequestEvents { [kOriginalResponse]?: IncomingMessageWithTimings; [kRequest]?: ClientRequest; _noPipe?: boolean; - _progressCallbacks: Array<() => void>; declare options: NormalizedOptions; declare requestUrl: string; @@ -1388,9 +1387,6 @@ export default class Request extends Duplex implements RequestEvents { 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(); @@ -2635,21 +2631,17 @@ export default class Request extends Duplex implements RequestEvents { 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);