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

feat: build target up to ES2022 #439

Merged
merged 9 commits into from
Mar 25, 2023
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
7 changes: 5 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"extends": "eslint-config-egg/typescript",
"extends": [
"eslint-config-egg/typescript",
"eslint-config-egg/lib/rules/enforce-node-prefix"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

],
"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",
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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"
}
4 changes: 2 additions & 2 deletions src/HttpAgent.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
26 changes: 13 additions & 13 deletions src/HttpClient.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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> = T extends undefined ? never : T;
type UndiciRequestOption = Exists<Parameters<typeof undiciRequest>[1]>;
Expand Down
4 changes: 2 additions & 2 deletions src/Request.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/Response.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/diagnosticsChannel.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion test/HttpClient.connect.rejectUnauthorized.test.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
6 changes: 4 additions & 2 deletions test/HttpClient.events.test.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -124,5 +124,7 @@ describe('HttpClient.events.test.ts', () => {
assert.equal(err.status, -1);
return true;
});
assert.equal(requestCount, 1);
assert.equal(responseCount, 1);
});
});
16 changes: 8 additions & 8 deletions test/HttpClient.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -96,7 +96,7 @@ describe('HttpClient.test.ts', () => {
return true;
},
});

await assert.rejects(async () => {
await httpclient.request(_url);
}, (err: any) => {
Expand Down Expand Up @@ -130,7 +130,7 @@ describe('HttpClient.test.ts', () => {
return true;
},
});

await assert.rejects(async () => {
await httpclient.request(_url);
}, (err: any) => {
Expand Down Expand Up @@ -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;
},
Expand Down
4 changes: 2 additions & 2 deletions test/diagnostics_channel.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
16 changes: 8 additions & 8 deletions test/fixtures/server.ts
Original file line number Diff line number Diff line change
@@ -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');

Expand Down Expand Up @@ -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\\"}'));
Expand Down Expand Up @@ -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')) {
Expand Down
9 changes: 4 additions & 5 deletions test/fixtures/socket_server.ts
Original file line number Diff line number Diff line change
@@ -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<{
Expand All @@ -19,11 +18,11 @@ export async function startServer(): Promise<{
return new Promise(resolve => {
unixSocketServer.listen(socketPath, () => {
resolve({
url: `http://localhost/`,
url: 'http://localhost/',

Check failure

Code scanning / CodeQL

Hard-coded credentials

The hard-coded value "http://localhost/" is used as [authorization header](1).
server: unixSocketServer,
socketPath,
closeServer: () => new Promise(resolve => unixSocketServer.close(resolve)),
})
});
});
})
});
}
2 changes: 2 additions & 0 deletions test/fixtures/ts/hello.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ async function request(url: RequestURL, options: RequestOptions): Promise<HttpCl
async function request2(url: RequestURL, options: RequestOptions2): Promise<HttpClientResponse> {
return await urllib2.curl(url, options);
}

console.log(request, request2);
9 changes: 4 additions & 5 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/keep-alive-header.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion test/non-ascii-request-header.test.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
2 changes: 1 addition & 1 deletion test/options.auth.test.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
4 changes: 2 additions & 2 deletions test/options.compressed.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
6 changes: 3 additions & 3 deletions test/options.content.test.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
6 changes: 3 additions & 3 deletions test/options.data.test.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
3 changes: 1 addition & 2 deletions test/options.dataType.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
Loading