diff --git a/.eslintrc b/.eslintrc index 465a96e9..466fc67d 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,10 +1,13 @@ { - "extends": "eslint-config-egg/typescript", + "extends": [ + "eslint-config-egg/typescript", + "eslint-config-egg/lib/rules/enforce-node-prefix" + ], "parserOptions": { // recommend to use another config file like tsconfig.eslint.json and extends tsconfig.json in it. // because you may be need to lint test/**/*.test.ts but no need to emit to js. // @see https://github.com/typescript-eslint/typescript-eslint/issues/890 - "project": "./tsconfig.json" + "project": "./tsconfig.eslint.json" }, "ignorePatterns": [ "src/**/*.js", diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 52a73f0d..4e50fc74 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -15,4 +15,4 @@ jobs: uses: artusjs/github-actions/.github/workflows/node-test.yml@v1 with: os: 'ubuntu-latest, macos-latest, windows-latest' - version: '14, 16, 18, 19' + version: '14.19.3, 14, 16, 18, 19' diff --git a/package.json b/package.json index 95358cd4..0b019081 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "url": "git://github.com/node-modules/urllib.git" }, "scripts": { - "lint": "eslint src --ext .ts", + "lint": "eslint src test --ext .ts", "build": "npm run build:dist", "build:cjs": "tsc -p ./tsconfig.build.cjs.json", "build:esm": "tsc -p ./tsconfig.build.esm.json && node ./scripts/esm_import_fix.js", @@ -74,7 +74,7 @@ "@types/pump": "^1.1.1", "@types/selfsigned": "^2.0.1", "@types/tar-stream": "^2.2.2", - "@vitest/coverage-c8": "^0.23.4", + "@vitest/coverage-c8": "^0.29.7", "busboy": "^1.6.0", "eslint": "^8.25.0", "eslint-config-egg": "^12.1.0", @@ -84,10 +84,10 @@ "selfsigned": "^2.0.1", "tar-stream": "^2.2.0", "typescript": "^4.8.3", - "vitest": "^0.28.5" + "vitest": "^0.29.7" }, "engines": { - "node": ">= 14.17.0" + "node": ">= 14.19.3" }, "license": "MIT" } diff --git a/src/HttpAgent.ts b/src/HttpAgent.ts index 9b7a00f2..62990a9a 100644 --- a/src/HttpAgent.ts +++ b/src/HttpAgent.ts @@ -1,5 +1,5 @@ -import dns from 'dns'; -import { LookupFunction, isIP } from 'net'; +import dns from 'node:dns'; +import { LookupFunction, isIP } from 'node:net'; import { Agent, } from 'undici'; diff --git a/src/HttpClient.ts b/src/HttpClient.ts index 1eb06125..44b2f5c3 100644 --- a/src/HttpClient.ts +++ b/src/HttpClient.ts @@ -1,20 +1,21 @@ -import { EventEmitter } from 'events'; -import { LookupFunction } from 'net'; -import { STATUS_CODES } from 'http'; -import { debuglog } from 'util'; +import { EventEmitter } from 'node:events'; +import { LookupFunction } from 'node:net'; +import { STATUS_CODES } from 'node:http'; +import type { IncomingHttpHeaders } from 'node:http'; +import { debuglog } from 'node:util'; import { createGunzip, createBrotliDecompress, gunzipSync, brotliDecompressSync, -} from 'zlib'; -import { Blob } from 'buffer'; -import { Readable, pipeline } from 'stream'; -import stream from 'stream'; -import { basename } from 'path'; -import { createReadStream } from 'fs'; -import { format as urlFormat } from 'url'; -import { performance } from 'perf_hooks'; +} from 'node:zlib'; +import { Blob } from 'node:buffer'; +import { Readable, pipeline } from 'node:stream'; +import stream from 'node:stream'; +import { basename } from 'node:path'; +import { createReadStream } from 'node:fs'; +import { format as urlFormat } from 'node:url'; +import { performance } from 'node:perf_hooks'; import { FormData as FormDataNative, request as undiciRequest, @@ -33,7 +34,6 @@ import { RawResponseWithMeta, HttpClientResponse, SocketInfo } from './Response' import { parseJSON, sleep, digestAuthHeader, globalId, performanceTime, isReadable } from './utils'; import symbols from './symbols'; import { initDiagnosticsChannel } from './diagnosticsChannel'; -import type { IncomingHttpHeaders } from 'http'; type Exists = T extends undefined ? never : T; type UndiciRequestOption = Exists[1]>; diff --git a/src/Request.ts b/src/Request.ts index c1e1fb79..1aa73c29 100644 --- a/src/Request.ts +++ b/src/Request.ts @@ -1,5 +1,5 @@ -import { Readable, Writable } from 'stream'; -import type { IncomingHttpHeaders } from 'http'; +import { Readable, Writable } from 'node:stream'; +import type { IncomingHttpHeaders } from 'node:http'; import type Dispatcher from 'undici/types/dispatcher'; import type { HttpClientResponse, diff --git a/src/Response.ts b/src/Response.ts index edb3cf46..7b4cfe04 100644 --- a/src/Response.ts +++ b/src/Response.ts @@ -1,5 +1,5 @@ -import { Readable } from 'stream'; -import { IncomingHttpHeaders } from 'http'; +import { Readable } from 'node:stream'; +import { IncomingHttpHeaders } from 'node:http'; export type SocketInfo = { id: number; diff --git a/src/diagnosticsChannel.ts b/src/diagnosticsChannel.ts index f8656c2f..d5d35ad8 100644 --- a/src/diagnosticsChannel.ts +++ b/src/diagnosticsChannel.ts @@ -1,6 +1,6 @@ -import diagnosticsChannel from 'diagnostics_channel'; -import { performance } from 'perf_hooks'; -import { debuglog } from 'util'; +import diagnosticsChannel from 'node:diagnostics_channel'; +import { performance } from 'node:perf_hooks'; +import { debuglog } from 'node:util'; import { DiagnosticsChannel } from 'undici'; import symbols from './symbols'; import { globalId, performanceTime } from './utils'; diff --git a/src/utils.ts b/src/utils.ts index cd5b8c7c..2e12c7ac 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,6 @@ -import { randomBytes, createHash } from 'crypto'; -import { Readable } from 'stream'; -import { performance } from 'perf_hooks'; +import { randomBytes, createHash } from 'node:crypto'; +import { Readable } from 'node:stream'; +import { performance } from 'node:perf_hooks'; import { FixJSONCtlChars } from './Request'; const JSONCtlCharsMap = { diff --git a/test/HttpClient.connect.rejectUnauthorized.test.ts b/test/HttpClient.connect.rejectUnauthorized.test.ts index 6edc1ed1..47591c8b 100644 --- a/test/HttpClient.connect.rejectUnauthorized.test.ts +++ b/test/HttpClient.connect.rejectUnauthorized.test.ts @@ -1,5 +1,5 @@ +import { strict as assert } from 'node:assert'; import { describe, it, beforeAll, afterAll } from 'vitest'; -import { strict as assert } from 'assert'; import { HttpClient } from '../src'; import { startServer } from './fixtures/server'; diff --git a/test/HttpClient.events.test.ts b/test/HttpClient.events.test.ts index 419da489..ab8f6889 100644 --- a/test/HttpClient.events.test.ts +++ b/test/HttpClient.events.test.ts @@ -1,6 +1,6 @@ +import { strict as assert } from 'node:assert'; import { describe, it, beforeAll, afterAll } from 'vitest'; -import { strict as assert } from 'assert'; import { HttpClient } from '../src'; import { startServer } from './fixtures/server'; @@ -91,7 +91,7 @@ describe('HttpClient.events.test.ts', () => { const httpclient = new HttpClient(); let requestCount = 0; let responseCount = 0; - httpclient.on('request', info => { + httpclient.on('request', () => { requestCount++; }); httpclient.on('response', info => { @@ -124,5 +124,7 @@ describe('HttpClient.events.test.ts', () => { assert.equal(err.status, -1); return true; }); + assert.equal(requestCount, 1); + assert.equal(responseCount, 1); }); }); diff --git a/test/HttpClient.test.ts b/test/HttpClient.test.ts index 2710bde4..0d4b97c3 100644 --- a/test/HttpClient.test.ts +++ b/test/HttpClient.test.ts @@ -1,8 +1,8 @@ +import { strict as assert } from 'node:assert'; +import dns from 'node:dns'; import { describe, it, beforeAll, afterAll } from 'vitest'; -import { strict as assert } from 'assert'; -import dns from 'dns'; import { HttpClient } from '../src'; -import { HttpClientResponseMeta } from '../src/Response'; +import { RawResponseWithMeta } from '../src/Response'; import { startServer } from './fixtures/server'; describe('HttpClient.test.ts', () => { @@ -50,7 +50,7 @@ describe('HttpClient.test.ts', () => { let response = await httpclient.request(_url); assert.equal(lookupCallCounter, 1); assert.equal(response.status, 200); - const res = response.res as HttpClientResponseMeta; + const res = response.res as RawResponseWithMeta; assert(res.rt > 100, `rt ${res.rt} should bigger than 100`); // will cache dns lookup let count = 10; @@ -74,7 +74,7 @@ describe('HttpClient.test.ts', () => { let response = await httpclient.request('https://registry.npmmirror.com/urllib'); assert.equal(lookupCallCounter, 1); assert.equal(response.status, 200); - const res = response.res as HttpClientResponseMeta; + const res = response.res as RawResponseWithMeta; // console.log(res); assert(res.rt > 100, `rt ${res.rt} should bigger than 100`); // will cache dns lookup @@ -96,7 +96,7 @@ describe('HttpClient.test.ts', () => { return true; }, }); - + await assert.rejects(async () => { await httpclient.request(_url); }, (err: any) => { @@ -130,7 +130,7 @@ describe('HttpClient.test.ts', () => { return true; }, }); - + await assert.rejects(async () => { await httpclient.request(_url); }, (err: any) => { @@ -224,7 +224,7 @@ describe('HttpClient.test.ts', () => { it('should throw error when follow redirect and redirect address illegal', async () => { const httpclient = new HttpClient({ - checkAddress(address) { + checkAddress() { // return address !== '127.0.0.1'; return false; }, diff --git a/test/diagnostics_channel.test.ts b/test/diagnostics_channel.test.ts index 52393bd7..9a8bea31 100644 --- a/test/diagnostics_channel.test.ts +++ b/test/diagnostics_channel.test.ts @@ -1,6 +1,6 @@ +import { strict as assert } from 'node:assert'; +import diagnosticsChannel from 'node:diagnostics_channel'; import { describe, it, beforeAll, afterAll } from 'vitest'; -import { strict as assert } from 'assert'; -import diagnosticsChannel from 'diagnostics_channel'; import urllib from '../src'; import symbols from '../src/symbols'; import { startServer } from './fixtures/server'; diff --git a/test/fixtures/server.ts b/test/fixtures/server.ts index 6b130d74..2ee554ea 100644 --- a/test/fixtures/server.ts +++ b/test/fixtures/server.ts @@ -1,12 +1,12 @@ -import { Socket } from 'net'; -import { createServer, Server, IncomingMessage, ServerResponse } from 'http'; -import { createServer as createHttpsServer } from 'https'; -import { createBrotliCompress, createGzip, gzipSync, brotliCompressSync } from 'zlib'; -import { createReadStream } from 'fs'; +import { Socket } from 'node:net'; +import { createServer, Server, IncomingMessage, ServerResponse } from 'node:http'; +import { createServer as createHttpsServer } from 'node:https'; +import { createBrotliCompress, createGzip, gzipSync, brotliCompressSync } from 'node:zlib'; +import { createReadStream } from 'node:fs'; import busboy from 'busboy'; import iconv from 'iconv-lite'; -import { readableToBytes, sleep } from '../utils'; import selfsigned from 'selfsigned'; +import { readableToBytes, sleep } from '../utils'; const requestsPerSocket = Symbol('requestsPerSocket'); @@ -188,7 +188,7 @@ export async function startServer(options?: { if (pathname === '/wrongjson-gbk') { res.setHeader('content-type', 'application/json'); createReadStream(__filename).pipe(res); - return + return; } if (pathname === '/json_with_controls_unicode') { return res.end(Buffer.from('{"foo":"\b\f\n\r\tbar\u000e!1!\u0086!2!\u0000!3!\u001F!4!\u005C!5!end\u005C\\"}')); @@ -289,7 +289,7 @@ export async function startServer(options?: { if (req.headers['content-type']?.startsWith('application/x-www-form-urlencoded')) { const searchParams = new URLSearchParams(requestBytes.toString()); requestBody = {}; - for (const [field, value] of searchParams.entries()) { + for (const [ field, value ] of searchParams.entries()) { requestBody[field] = value; } } else if (req.headers['content-type']?.startsWith('application/json')) { diff --git a/test/fixtures/socket_server.ts b/test/fixtures/socket_server.ts index 44310385..27fd9907 100644 --- a/test/fixtures/socket_server.ts +++ b/test/fixtures/socket_server.ts @@ -1,5 +1,4 @@ -import { createServer, Server } from 'http'; - +import { createServer, Server } from 'node:http'; const socketPath = '/tmp/urllib.unix.sock'; export async function startServer(): Promise<{ @@ -19,11 +18,11 @@ export async function startServer(): Promise<{ return new Promise(resolve => { unixSocketServer.listen(socketPath, () => { resolve({ - url: `http://localhost/`, + url: 'http://localhost/', server: unixSocketServer, socketPath, closeServer: () => new Promise(resolve => unixSocketServer.close(resolve)), - }) + }); }); - }) + }); } diff --git a/test/fixtures/ts/hello.ts b/test/fixtures/ts/hello.ts index 0a56df91..1090ef1a 100644 --- a/test/fixtures/ts/hello.ts +++ b/test/fixtures/ts/hello.ts @@ -10,3 +10,5 @@ async function request(url: RequestURL, options: RequestOptions): Promise { return await urllib2.curl(url, options); } + +console.log(request, request2); diff --git a/test/index.test.ts b/test/index.test.ts index 5ee06a00..1b42db2e 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1,8 +1,7 @@ +import { strict as assert } from 'node:assert'; +import { parse as urlparse } from 'node:url'; +import { readFileSync } from 'node:fs'; import { describe, it, beforeAll, afterAll } from 'vitest'; -import { strict as assert } from 'assert'; -import { parse as urlparse } from 'url'; -import { createReadStream, readFileSync } from 'fs'; -import { Readable } from 'stream'; import urllib from '../src'; import { MockAgent, setGlobalDispatcher, getGlobalDispatcher } from '../src'; import { startServer } from './fixtures/server'; @@ -140,7 +139,7 @@ describe('index.test.ts', () => { describe('Mocking request', () => { let mockAgent: MockAgent; - let globalAgent = getGlobalDispatcher(); + const globalAgent = getGlobalDispatcher(); beforeAll(() => { mockAgent = new MockAgent(); setGlobalDispatcher(mockAgent); diff --git a/test/keep-alive-header.test.ts b/test/keep-alive-header.test.ts index 2a28507f..d42a125c 100644 --- a/test/keep-alive-header.test.ts +++ b/test/keep-alive-header.test.ts @@ -1,5 +1,5 @@ +import { strict as assert } from 'node:assert'; import { describe, it, beforeAll, afterAll } from 'vitest'; -import { strict as assert } from 'assert'; import urllib from '../src'; import { startServer } from './fixtures/server'; import { sleep } from './utils'; diff --git a/test/non-ascii-request-header.test.ts b/test/non-ascii-request-header.test.ts index 67687df7..e7073f95 100644 --- a/test/non-ascii-request-header.test.ts +++ b/test/non-ascii-request-header.test.ts @@ -1,5 +1,5 @@ +import { strict as assert } from 'node:assert'; import { describe, it, beforeAll, afterAll } from 'vitest'; -import { strict as assert } from 'assert'; import urllib from '../src'; import { startServer } from './fixtures/server'; diff --git a/test/options.auth.test.ts b/test/options.auth.test.ts index e04c96ba..aaeb8ea1 100644 --- a/test/options.auth.test.ts +++ b/test/options.auth.test.ts @@ -1,5 +1,5 @@ +import { strict as assert } from 'node:assert'; import { describe, it, beforeAll, afterAll } from 'vitest'; -import { strict as assert } from 'assert'; import urllib from '../src'; import { startServer } from './fixtures/server'; diff --git a/test/options.compressed.test.ts b/test/options.compressed.test.ts index 0f5980dc..f4b752d1 100644 --- a/test/options.compressed.test.ts +++ b/test/options.compressed.test.ts @@ -1,6 +1,6 @@ +import { strict as assert } from 'node:assert'; +import { createWriteStream, createReadStream } from 'node:fs'; import { describe, it, beforeAll, afterAll, beforeEach, afterEach } from 'vitest'; -import { strict as assert } from 'assert'; -import { createWriteStream, createReadStream } from 'fs'; import urllib from '../src'; import { startServer } from './fixtures/server'; import { readableToString, createTempfile } from './utils'; diff --git a/test/options.content.test.ts b/test/options.content.test.ts index e847e36c..d8399522 100644 --- a/test/options.content.test.ts +++ b/test/options.content.test.ts @@ -1,7 +1,7 @@ +import { strict as assert } from 'node:assert'; +import { createReadStream } from 'node:fs'; import { describe, it, beforeAll, afterAll } from 'vitest'; -import { strict as assert } from 'assert'; -import { createReadStream } from 'fs'; -import fs from 'fs/promises'; +import fs from 'node:fs/promises'; import urllib from '../src'; import { startServer } from './fixtures/server'; diff --git a/test/options.data.test.ts b/test/options.data.test.ts index 3630254c..35ab6778 100644 --- a/test/options.data.test.ts +++ b/test/options.data.test.ts @@ -1,7 +1,7 @@ +import { strict as assert } from 'node:assert'; +import { createReadStream } from 'node:fs'; +import { Readable } from 'node:stream'; import { describe, it, beforeAll, afterAll } from 'vitest'; -import { strict as assert } from 'assert'; -import { createReadStream } from 'fs'; -import { Readable } from 'stream'; import urllib from '../src'; import { startServer } from './fixtures/server'; diff --git a/test/options.dataType.test.ts b/test/options.dataType.test.ts index 92dd5770..2892d314 100644 --- a/test/options.dataType.test.ts +++ b/test/options.dataType.test.ts @@ -1,6 +1,5 @@ +import { strict as assert } from 'node:assert'; import { describe, it, beforeAll, afterAll } from 'vitest'; -import { strict as assert } from 'assert'; -import { Readable } from 'stream'; import urllib from '../src'; import { startServer } from './fixtures/server'; import { readableToBytes } from './utils'; diff --git a/test/options.digestAuth.test.ts b/test/options.digestAuth.test.ts index 6c20730a..463c9b61 100644 --- a/test/options.digestAuth.test.ts +++ b/test/options.digestAuth.test.ts @@ -1,5 +1,5 @@ +import { strict as assert } from 'node:assert'; import { describe, it, beforeAll, afterAll } from 'vitest'; -import { strict as assert } from 'assert'; import urllib from '../src'; import { startServer } from './fixtures/server'; @@ -72,7 +72,7 @@ describe('options.digestAuth.test.ts', () => { }); it.skip('should request with digest auth success in httpbin', async () => { - var url = 'https://httpbin.org/digest-auth/auth/user/passwd'; + const url = 'https://httpbin.org/digest-auth/auth/user/passwd'; const response = await urllib.request(url, { digestAuth: 'user:passwd', dataType: 'json', @@ -87,7 +87,7 @@ describe('options.digestAuth.test.ts', () => { }); it.skip('should request with digest auth fail in httpbin', async () => { - var url = 'https://httpbin.org/digest-auth/auth/user/passwd'; + const url = 'https://httpbin.org/digest-auth/auth/user/passwd'; const response = await urllib.request(url, { digestAuth: 'user:passwdfail', dataType: 'json', diff --git a/test/options.dispatcher.test.ts b/test/options.dispatcher.test.ts index a7222007..e23fac48 100644 --- a/test/options.dispatcher.test.ts +++ b/test/options.dispatcher.test.ts @@ -1,5 +1,5 @@ +import { strict as assert } from 'node:assert'; import { describe, it, beforeAll, afterAll } from 'vitest'; -import { strict as assert } from 'assert'; import setup from 'proxy'; import { request, ProxyAgent, getGlobalDispatcher, setGlobalDispatcher } from '../src'; import { startServer } from './fixtures/server'; @@ -14,7 +14,7 @@ describe('options.dispatcher.test.ts', () => { close = closeServer; _url = url; proxyServer = setup(); - await new Promise((resolve) => { + await new Promise(resolve => { proxyServer.listen(0, () => { // console.log('HTTP proxy server listening on port %d', proxyServer.address().port); proxyServerUrl = `http://127.0.0.1:${proxyServer.address().port}`; diff --git a/test/options.files.test.ts b/test/options.files.test.ts index d2dd41b5..397b1124 100644 --- a/test/options.files.test.ts +++ b/test/options.files.test.ts @@ -1,9 +1,9 @@ +import { strict as assert } from 'node:assert'; +import path from 'node:path'; +import fs from 'node:fs/promises'; +import { createReadStream } from 'node:fs'; +import { Readable } from 'node:stream'; import { describe, it, beforeAll, afterAll } from 'vitest'; -import { strict as assert } from 'assert'; -import path from 'path'; -import fs from 'fs/promises'; -import { createReadStream } from 'fs'; -import { Readable } from 'stream'; import urllib from '../src'; import { startServer } from './fixtures/server'; diff --git a/test/options.fixJSONCtlChars.test.ts b/test/options.fixJSONCtlChars.test.ts index 7b72b23a..381362e3 100644 --- a/test/options.fixJSONCtlChars.test.ts +++ b/test/options.fixJSONCtlChars.test.ts @@ -1,5 +1,5 @@ +import { strict as assert } from 'node:assert'; import { describe, it, beforeAll, afterAll } from 'vitest'; -import { strict as assert } from 'assert'; import urllib from '../src'; import { startServer } from './fixtures/server'; diff --git a/test/options.followRedirect.test.ts b/test/options.followRedirect.test.ts index 754270ff..4841f1b6 100644 --- a/test/options.followRedirect.test.ts +++ b/test/options.followRedirect.test.ts @@ -1,5 +1,5 @@ +import { strict as assert } from 'node:assert'; import { describe, it, beforeAll, afterAll } from 'vitest'; -import { strict as assert } from 'assert'; import urllib from '../src'; import { startServer } from './fixtures/server'; diff --git a/test/options.gzip.test.ts b/test/options.gzip.test.ts index 98830e1e..6f30d43e 100644 --- a/test/options.gzip.test.ts +++ b/test/options.gzip.test.ts @@ -1,5 +1,5 @@ +import { strict as assert } from 'node:assert'; import { describe, it, beforeAll, afterAll } from 'vitest'; -import { strict as assert } from 'assert'; import urllib from '../src'; import { startServer } from './fixtures/server'; diff --git a/test/options.headers.test.ts b/test/options.headers.test.ts index 283f168e..b6b4d137 100644 --- a/test/options.headers.test.ts +++ b/test/options.headers.test.ts @@ -1,5 +1,5 @@ +import { strict as assert } from 'node:assert'; import { describe, it, beforeAll, afterAll } from 'vitest'; -import { strict as assert } from 'assert'; import urllib from '../src'; import { startServer } from './fixtures/server'; @@ -41,7 +41,7 @@ describe('options.headers.test.ts', () => { // 'CASE-KEy': 'case3,case33', 'lower-key': 'lower', }, - dataType: 'json' + dataType: 'json', }); assert.equal(response.status, 200); assert.equal(response.data.headers['case-key'], 'case2'); diff --git a/test/options.method.test.ts b/test/options.method.test.ts index 2008a9fc..6590d162 100644 --- a/test/options.method.test.ts +++ b/test/options.method.test.ts @@ -1,5 +1,5 @@ +import { strict as assert } from 'node:assert'; import { describe, it, beforeAll, afterAll } from 'vitest'; -import { strict as assert } from 'assert'; import urllib from '../src'; import { startServer } from './fixtures/server'; diff --git a/test/options.opaque.test.ts b/test/options.opaque.test.ts index a698f029..02d3287d 100644 --- a/test/options.opaque.test.ts +++ b/test/options.opaque.test.ts @@ -1,5 +1,5 @@ +import { strict as assert } from 'node:assert'; import { describe, it, beforeAll, afterAll } from 'vitest'; -import { strict as assert } from 'assert'; import urllib from '../src'; import { startServer } from './fixtures/server'; diff --git a/test/options.reset.test.ts b/test/options.reset.test.ts index ef7a905c..889ffcd0 100644 --- a/test/options.reset.test.ts +++ b/test/options.reset.test.ts @@ -1,5 +1,5 @@ +import { strict as assert } from 'node:assert'; import { describe, it, beforeAll, afterAll } from 'vitest'; -import { strict as assert } from 'assert'; import urllib from '../src'; import { startServer } from './fixtures/server'; @@ -22,6 +22,6 @@ describe('options.reset.test.ts', () => { reset: true, }); assert.equal(response.status, 200); - assert(response.data.headers['connection'] === 'close'); + assert(response.data.headers.connection === 'close'); }); }); diff --git a/test/options.retry.test.ts b/test/options.retry.test.ts index fbe79e82..f6912dbf 100644 --- a/test/options.retry.test.ts +++ b/test/options.retry.test.ts @@ -1,6 +1,6 @@ +import { strict as assert } from 'node:assert'; +import { createWriteStream, createReadStream } from 'node:fs'; import { describe, it, beforeAll, afterAll, beforeEach, afterEach } from 'vitest'; -import { strict as assert } from 'assert'; -import { createWriteStream, createReadStream } from 'fs'; import urllib from '../src'; import { startServer } from './fixtures/server'; import { readableToString, createTempfile } from './utils'; @@ -116,7 +116,7 @@ describe('options.retry.test.ts', () => { }); it('should custom isRetry', async () => { - let response = await urllib.request(`${_url}mock-status?status=400`, { + const response = await urllib.request(`${_url}mock-status?status=400`, { dataType: 'text', retry: 2, isRetry(response) { @@ -125,20 +125,20 @@ describe('options.retry.test.ts', () => { }); assert.equal(response.status, 400); assert.equal(response.data, 'Mock status 400'); - let requestHeaders = JSON.parse(response.headers['x-request-headers'] as string); + const requestHeaders = JSON.parse(response.headers['x-request-headers'] as string); assert.equal(requestHeaders['x-urllib-retry'], '2/2'); }); it('should retry with delay', async () => { const startTime = Date.now(); - let response = await urllib.request(`${_url}mock-status?status=500`, { + const response = await urllib.request(`${_url}mock-status?status=500`, { dataType: 'text', retry: 3, retryDelay: 110, }); assert.equal(response.status, 500); assert.equal(response.data, 'Mock status 500'); - let requestHeaders = JSON.parse(response.headers['x-request-headers'] as string); + const requestHeaders = JSON.parse(response.headers['x-request-headers'] as string); assert.equal(requestHeaders['x-urllib-retry'], '3/3'); const use = Date.now() - startTime; assert(use >= 330); diff --git a/test/options.socketPath.test.ts b/test/options.socketPath.test.ts index a03d4b4f..ed6a171b 100644 --- a/test/options.socketPath.test.ts +++ b/test/options.socketPath.test.ts @@ -1,9 +1,8 @@ +import { strict as assert } from 'node:assert'; +import os from 'node:os'; import { describe, it, beforeAll, afterAll } from 'vitest'; -import { strict as assert } from 'assert'; import urllib from '../src'; import { startServer } from './fixtures/socket_server'; -import os from 'os'; - describe.skipIf(os.platform() === 'win32')('options.socketPath.test.ts', () => { let close: any; @@ -28,7 +27,7 @@ describe.skipIf(os.platform() === 'win32')('options.socketPath.test.ts', () => { dataType: 'json', }); - assert.deepStrictEqual(result.data, {a: 1}); + assert.deepStrictEqual(result.data, { a: 1 }); }); }); diff --git a/test/options.stream.test.ts b/test/options.stream.test.ts index e9275386..44e0d844 100644 --- a/test/options.stream.test.ts +++ b/test/options.stream.test.ts @@ -1,17 +1,16 @@ +import { strict as assert } from 'node:assert'; +import { createReadStream } from 'node:fs'; +import path from 'node:path'; +import { writeFile, readFile } from 'node:fs/promises'; +import { createGunzip } from 'node:zlib'; +import { Readable } from 'node:stream'; import { describe, it, beforeAll, afterAll, beforeEach, afterEach } from 'vitest'; -import { strict as assert } from 'assert'; -import { createReadStream } from 'fs'; -import os from 'os'; -import path from 'path'; -import { writeFile, readFile } from 'fs/promises'; +import tar from 'tar-stream'; +import FormStream from 'formstream'; import urllib from '../src'; import { isReadable } from '../src/utils'; import { startServer } from './fixtures/server'; import { createTempfile } from './utils'; -import tar from 'tar-stream'; -import { createGunzip } from 'zlib'; -import { Readable } from 'stream'; -import FormStream from 'formstream'; describe('options.stream.test.ts', () => { let close: any; @@ -109,7 +108,7 @@ describe('options.stream.test.ts', () => { const stream = createReadStream(tmpfile); assert.equal(stream.destroyed, false); - await assert.rejects(async () => { + await assert.rejects(async () => { await urllib.request(`${_url}block`, { method: 'post', timeout: 100, @@ -158,7 +157,7 @@ describe('options.stream.test.ts', () => { await urllib.request(`${_url}block`, { method: 'post', stream, - }); + }); }, (err: any) => { // console.error(err); assert.equal(err.name, 'Error'); diff --git a/test/options.streaming.test.ts b/test/options.streaming.test.ts index dc79aa07..23e1e9f1 100644 --- a/test/options.streaming.test.ts +++ b/test/options.streaming.test.ts @@ -1,7 +1,7 @@ +import { strict as assert } from 'node:assert'; +import { pipeline } from 'node:stream'; +import { createBrotliDecompress } from 'node:zlib'; import { describe, it, beforeAll, afterAll } from 'vitest'; -import { strict as assert } from 'assert'; -import { pipeline } from 'stream'; -import { createBrotliDecompress } from 'zlib'; import urllib from '../src'; import { isReadable } from '../src/utils'; import { startServer } from './fixtures/server'; @@ -85,7 +85,9 @@ describe('options.streaming.test.ts', () => { // response.res stream is not decompressed assert(isReadable(response.res)); let decoder = createBrotliDecompress(); - bytes = await readableToBytes(pipeline(response.res, decoder, () => {})); + bytes = await readableToBytes(pipeline(response.res, decoder, () => { + return; + })); data = bytes.toString(); assert.match(data, /export async function startServer/); @@ -106,7 +108,9 @@ describe('options.streaming.test.ts', () => { // response.res stream is not decompressed assert(isReadable(response.res)); decoder = createBrotliDecompress(); - bytes = await readableToBytes(pipeline(response.res, decoder, () => {})); + bytes = await readableToBytes(pipeline(response.res, decoder, () => { + return; + })); data = bytes.toString(); assert.match(data, /export async function startServer/); }); diff --git a/test/options.timeout.test.ts b/test/options.timeout.test.ts index 9e7916c4..5ca54000 100644 --- a/test/options.timeout.test.ts +++ b/test/options.timeout.test.ts @@ -1,5 +1,5 @@ +import { strict as assert } from 'node:assert'; import { describe, it, beforeAll, afterAll } from 'vitest'; -import { strict as assert } from 'assert'; import urllib from '../src'; import { startServer } from './fixtures/server'; diff --git a/test/options.timing.test.ts b/test/options.timing.test.ts index 5af58160..bb9cf8f2 100644 --- a/test/options.timing.test.ts +++ b/test/options.timing.test.ts @@ -1,7 +1,7 @@ +import { strict as assert } from 'node:assert'; import { describe, it, beforeAll, afterAll } from 'vitest'; -import { strict as assert } from 'assert'; import urllib from '../src'; -import { HttpClientResponseMeta } from '../src/Response'; +import { RawResponseWithMeta } from '../src/Response'; import { startServer } from './fixtures/server'; import { sleep } from './utils'; @@ -24,7 +24,7 @@ describe('options.timing.test.ts', () => { timing: true, }); assert.equal(response.status, 200); - let res = response.res as HttpClientResponseMeta; + let res = response.res as RawResponseWithMeta; assert(res.timing.waiting > 0); assert(res.timing.queuing > 0); assert(res.timing.connected > 0); @@ -36,14 +36,14 @@ describe('options.timing.test.ts', () => { assert(res.socket.handledResponses === 1); // again connected should be zero - await sleep(1) + await sleep(1); response = await urllib.request(`${_url}?content-encoding=gzip`, { dataType: 'json', timing: true, }); assert.equal(response.status, 200); - res = response.res as HttpClientResponseMeta; + res = response.res as RawResponseWithMeta; assert(res.timing.waiting > 0); assert(res.timing.queuing > 0); assert(res.timing.connected === 0); @@ -61,7 +61,7 @@ describe('options.timing.test.ts', () => { timing: false, }); assert.equal(response.status, 200); - const res = response.res as HttpClientResponseMeta; + const res = response.res as RawResponseWithMeta; assert.equal(res.timing.waiting, 0); assert.equal(res.timing.contentDownload, 0); assert(res.rt > 0); @@ -72,7 +72,7 @@ describe('options.timing.test.ts', () => { dataType: 'json', }); assert.equal(response.status, 200); - const res = response.res as HttpClientResponseMeta; + const res = response.res as RawResponseWithMeta; assert.equal(res.timing.waiting, 0); assert.equal(res.timing.contentDownload, 0); assert(res.rt > 0); diff --git a/test/options.writeStream.test.ts b/test/options.writeStream.test.ts index fe860c43..ee721d3d 100644 --- a/test/options.writeStream.test.ts +++ b/test/options.writeStream.test.ts @@ -1,9 +1,9 @@ +import { strict as assert } from 'node:assert'; +import { createWriteStream } from 'node:fs'; +import { join } from 'node:path'; +import { gunzipSync } from 'node:zlib'; +import { stat, readFile } from 'node:fs/promises'; import { describe, it, beforeAll, afterAll, beforeEach, afterEach } from 'vitest'; -import { strict as assert } from 'assert'; -import { createWriteStream } from 'fs'; -import { join } from 'path'; -import { gunzipSync } from 'zlib'; -import { stat, readFile } from 'fs/promises'; import urllib from '../src'; import { startServer } from './fixtures/server'; import { createTempfile, sleep } from './utils'; diff --git a/test/response-charset-gbk.test.ts b/test/response-charset-gbk.test.ts index ab84989b..c6735da1 100644 --- a/test/response-charset-gbk.test.ts +++ b/test/response-charset-gbk.test.ts @@ -1,5 +1,5 @@ +import { strict as assert } from 'node:assert'; import { describe, it, beforeAll, afterAll } from 'vitest'; -import { strict as assert } from 'assert'; import urllib from '../src'; import { startServer } from './fixtures/server'; diff --git a/test/user-agent.test.ts b/test/user-agent.test.ts index ad63319b..729d39d8 100644 --- a/test/user-agent.test.ts +++ b/test/user-agent.test.ts @@ -1,5 +1,5 @@ +import { strict as assert } from 'node:assert'; import { describe, it, beforeAll, afterAll } from 'vitest'; -import { strict as assert } from 'assert'; import urllib from '../src'; import { startServer } from './fixtures/server'; @@ -26,30 +26,30 @@ describe('keep-alive-header.test.ts', () => { assert.match(response.data.headers['user-agent'], /^node\-urllib\/3\.\d+\.\d+ Node\.js\/\d+\.\d+\.\d+ \(/); }); - it('should return no user agent if user-agent header is set to empty string', async () => { + it('should return no user agent if user-agent header is set to empty string', async () => { const response = await urllib.request(_url, { dataType: 'json', - headers: {'user-agent': ''}, + headers: { 'user-agent': '' }, }); assert.equal(response.status, 200); // console.log(response.data.headers); assert.equal(response.data.headers['user-agent'], undefined); }); - it('should return no user agent if user-agent header is set undefined', async () => { + it('should return no user agent if user-agent header is set undefined', async () => { const response = await urllib.request(_url, { dataType: 'json', - headers: {'user-agent': undefined}, + headers: { 'user-agent': undefined }, }); assert.equal(response.status, 200); // console.log(response.data.headers); assert.equal(response.data.headers['user-agent'], undefined); }); - it('should return mock user agent', async () => { + it('should return mock user agent', async () => { let response = await urllib.request(_url, { dataType: 'json', - headers: {'user-agent': 'mock agent'}, + headers: { 'user-agent': 'mock agent' }, }); assert.equal(response.status, 200); // console.log(response.data.headers); @@ -57,7 +57,7 @@ describe('keep-alive-header.test.ts', () => { response = await urllib.request(_url, { dataType: 'json', - headers: {'User-Agent': 'mock agent upper case'}, + headers: { 'User-Agent': 'mock agent upper case' }, }); assert.equal(response.status, 200); // console.log(response.data.headers); diff --git a/test/utils.test.ts b/test/utils.test.ts index afb51ea4..92ba28a6 100644 --- a/test/utils.test.ts +++ b/test/utils.test.ts @@ -1,5 +1,5 @@ +import { strict as assert } from 'node:assert'; import { describe, it } from 'vitest'; -import { strict as assert } from 'assert'; import { globalId } from '../src/utils'; describe('utils.test.ts', () => { diff --git a/test/utils.ts b/test/utils.ts index f3df9038..b98e8f3a 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -1,9 +1,9 @@ -import { Readable } from 'stream'; -import { ReadableStream } from 'stream/web'; -import { rm, writeFile } from 'fs/promises'; -import { join } from 'path'; -import { tmpdir } from 'os'; -import { randomUUID } from 'crypto'; +import { Readable } from 'node:stream'; +import { ReadableStream } from 'node:stream/web'; +import { rm, writeFile } from 'node:fs/promises'; +import { join } from 'node:path'; +import { tmpdir } from 'node:os'; +import { randomUUID } from 'node:crypto'; export async function sleep(ms: number) { await new Promise(resolve => { diff --git a/tsconfig.eslint.json b/tsconfig.eslint.json new file mode 100644 index 00000000..91644d12 --- /dev/null +++ b/tsconfig.eslint.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "include": [ + "src/**/*.ts", + "test/**/*.ts" + ] +} diff --git a/tsconfig.json b/tsconfig.json index 941b2969..628ce916 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -17,7 +17,7 @@ "suppressImplicitAnyIndexErrors": true, "suppressExcessPropertyErrors": true, "forceConsistentCasingInFileNames": true, - "target": "ES2020", + "target": "ES2022", "sourceMap": true, "esModuleInterop": true, "stripInternal": true,