From f40ce0a2eb415528d32c684ff2ec073930b24402 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sat, 25 Mar 2023 22:12:10 +0800 Subject: [PATCH] fix: ignore undefined value data on GET query (#441) closes https://github.com/node-modules/urllib/pull/436 --- .gitignore | 1 + AUTHORS | 18 ------------------ README.md | 16 ++++++++-------- package.json | 4 ++-- src/HttpClient.ts | 4 +++- test/options.data.test.ts | 10 +++++++--- test/options.streaming.test.ts | 12 ++++++------ 7 files changed, 27 insertions(+), 38 deletions(-) delete mode 100644 AUTHORS diff --git a/.gitignore b/.gitignore index 509ddd10..0e3c17b1 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ src/**/*.d.ts !src/formstream.d.ts .DS_Store test/fixtures/ts/*.js +.eslintcache diff --git a/AUTHORS b/AUTHORS deleted file mode 100644 index 4cfad364..00000000 --- a/AUTHORS +++ /dev/null @@ -1,18 +0,0 @@ -# Ordered by date of first contribution. - -fengmk2 (http://fengmk2.com) -aleafs (https://github.com/aleafs) -Jackson Tian (https://github.com/JacksonTian) -ibigbug (https://github.com/ibigbug) -XiNGRZ (https://github.com/XiNGRZ) -dead_horse (http://deadhorse.me/) -coderhaoxin (https://github.com/coderhaoxin) -alsotang (https://github.com/alsotang) -Jonathan Dahan (https://github.com/jedahan) -popomore (https://github.com/popomore) -fishbar (https://github.com/fishbar) -coolme200 (https://github.com/coolme200) -amunu (https://github.com/Amunu) -Yuwei Ba (https://github.com/ibigbug) -Daniel Wang (https://github.com/danielwpz) - diff --git a/README.md b/README.md index 72f6b475..a6578b05 100644 --- a/README.md +++ b/README.md @@ -332,14 +332,14 @@ Fork [undici benchmarks script](https://github.com/fengmk2/undici/blob/urllib-be |[
fengmk2](https://github.com/fengmk2)
|[
dead-horse](https://github.com/dead-horse)
|[
xingrz](https://github.com/xingrz)
|[
popomore](https://github.com/popomore)
|[
JacksonTian](https://github.com/JacksonTian)
|[
ibigbug](https://github.com/ibigbug)
| | :---: | :---: | :---: | :---: | :---: | :---: | -|[
greenkeeperio-bot](https://github.com/greenkeeperio-bot)
|[
atian25](https://github.com/atian25)
|[
paambaati](https://github.com/paambaati)
|[
denghongcai](https://github.com/denghongcai)
|[
gemwuu](https://github.com/gemwuu)
|[
XadillaX](https://github.com/XadillaX)
| -|[
alsotang](https://github.com/alsotang)
|[
leoner](https://github.com/leoner)
|[
hyj1991](https://github.com/hyj1991)
|[
isayme](https://github.com/isayme)
|[
killagu](https://github.com/killagu)
|[
cyjake](https://github.com/cyjake)
| -|[
whxaxes](https://github.com/whxaxes)
|[
chadxz](https://github.com/chadxz)
|[
adapt0](https://github.com/adapt0)
|[
danielwpz](https://github.com/danielwpz)
|[
danielsss](https://github.com/danielsss)
|[
Jeff-Tian](https://github.com/Jeff-Tian)
| -|[
nick-ng](https://github.com/nick-ng)
|[
rishavsharan](https://github.com/rishavsharan)
|[
willizm](https://github.com/willizm)
|[
davidkhala](https://github.com/davidkhala)
|[
aleafs](https://github.com/aleafs)
|[
Amunu](https://github.com/Amunu)
| -|[
azure-pipelines[bot]](https://github.com/apps/azure-pipelines)
|[
capsice](https://github.com/capsice)
|[
changzhiwin](https://github.com/changzhiwin)
|[
yuzhigang33](https://github.com/yuzhigang33)
|[
fishbar](https://github.com/fishbar)
|[
gxcsoccer](https://github.com/gxcsoccer)
| -[
mars-coder](https://github.com/mars-coder)
|[
rockdai](https://github.com/rockdai)
|[
dickeylth](https://github.com/dickeylth)
|[
aladdin-add](https://github.com/aladdin-add)
- -This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Sat Nov 19 2022 02:16:13 GMT+0800`. +|[
greenkeeperio-bot](https://github.com/greenkeeperio-bot)
|[
semantic-release-bot](https://github.com/semantic-release-bot)
|[
atian25](https://github.com/atian25)
|[
killagu](https://github.com/killagu)
|[
paambaati](https://github.com/paambaati)
|[
denghongcai](https://github.com/denghongcai)
| +|[
gemwuu](https://github.com/gemwuu)
|[
XadillaX](https://github.com/XadillaX)
|[
alsotang](https://github.com/alsotang)
|[
leoner](https://github.com/leoner)
|[
hyj1991](https://github.com/hyj1991)
|[
isayme](https://github.com/isayme)
| +|[
cyjake](https://github.com/cyjake)
|[
whxaxes](https://github.com/whxaxes)
|[
chadxz](https://github.com/chadxz)
|[
adapt0](https://github.com/adapt0)
|[
danielwpz](https://github.com/danielwpz)
|[
danielsss](https://github.com/danielsss)
| +|[
Jeff-Tian](https://github.com/Jeff-Tian)
|[
nick-ng](https://github.com/nick-ng)
|[
rishavsharan](https://github.com/rishavsharan)
|[
willizm](https://github.com/willizm)
|[
davidkhala](https://github.com/davidkhala)
|[
aleafs](https://github.com/aleafs)
| +|[
Amunu](https://github.com/Amunu)
|[
azure-pipelines[bot]](https://github.com/apps/azure-pipelines)
|[
capsice](https://github.com/capsice)
|[
changzhiwin](https://github.com/changzhiwin)
|[
yuzhigang33](https://github.com/yuzhigang33)
|[
elrrrrrrr](https://github.com/elrrrrrrr)
| +[
fishbar](https://github.com/fishbar)
|[
gxcsoccer](https://github.com/gxcsoccer)
|[
mars-coder](https://github.com/mars-coder)
|[
rockdai](https://github.com/rockdai)
|[
dickeylth](https://github.com/dickeylth)
|[
aladdin-add](https://github.com/aladdin-add)
+ +This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Sat Mar 25 2023 22:00:34 GMT+0800`. diff --git a/package.json b/package.json index 682bec52..99407fd9 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "url": "git://github.com/node-modules/urllib.git" }, "scripts": { - "lint": "eslint src test --ext .ts", + "lint": "eslint src test --ext .ts --cache", "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", @@ -50,7 +50,7 @@ "build:esm:test": "cd test/esm && rm -rf node_modules && npm link ../.. && node index.js", "build:test": "npm run build && npm run build:cjs:test && npm run build:esm:test && npm run test-tsc", "test-tsc": "tsc -p ./test/fixtures/ts/tsconfig.json", - "test": "vitest run", + "test": "npm run lint && vitest run", "cov": "vitest run --coverage --no-threads", "ci": "npm run lint && npm run cov && npm run build:test", "contributor": "git-contributor", diff --git a/src/HttpClient.ts b/src/HttpClient.ts index 44b2f5c3..cfdd5bd4 100644 --- a/src/HttpClient.ts +++ b/src/HttpClient.ts @@ -409,7 +409,9 @@ export class HttpClient extends EventEmitter { if (isGETOrHEAD) { if (!isStringOrBufferOrReadable) { for (const field in args.data) { - requestUrl.searchParams.append(field, args.data[field]); + const fieldValue = args.data[field]; + if (fieldValue === undefined) continue; + requestUrl.searchParams.append(field, fieldValue); } } } else { diff --git a/test/options.data.test.ts b/test/options.data.test.ts index 35ab6778..4b773c7a 100644 --- a/test/options.data.test.ts +++ b/test/options.data.test.ts @@ -5,7 +5,7 @@ import { describe, it, beforeAll, afterAll } from 'vitest'; import urllib from '../src'; import { startServer } from './fixtures/server'; -describe('options.data.test.ts2', () => { +describe('options.data.test.ts', () => { let close: any; let _url: string; beforeAll(async () => { @@ -23,6 +23,11 @@ describe('options.data.test.ts2', () => { data: { sql: 'SELECT * from table', data: '哈哈', + b: undefined, + c: '2222', + d: 1111, + e() { return ''; }, + f: true, }, dataType: 'json', }); @@ -31,8 +36,7 @@ describe('options.data.test.ts2', () => { assert.equal(response.data.method, 'GET'); assert(response.url.startsWith(_url)); assert(!response.redirected); - // console.log(response.headers); - assert.equal(response.data.url, '/?sql=SELECT+*+from+table&data=%E5%93%88%E5%93%88'); + assert.equal(response.data.url, '/?sql=SELECT+*+from+table&data=%E5%93%88%E5%93%88&c=2222&d=1111&e=e%28%29+%7B%0A++++++++++return+%22%22%3B%0A++++++++%7D&f=true'); const url = new URL(response.data.href); assert.equal(url.searchParams.get('sql'), 'SELECT * from table'); assert.equal(url.searchParams.get('data'), '哈哈'); diff --git a/test/options.streaming.test.ts b/test/options.streaming.test.ts index 23e1e9f1..12079fb5 100644 --- a/test/options.streaming.test.ts +++ b/test/options.streaming.test.ts @@ -115,9 +115,9 @@ describe('options.streaming.test.ts', () => { assert.match(data, /export async function startServer/); }); - it('should get big streaming response', async () => { - const response = await urllib.request(`${_url}mock-bytes?size=1024102400`, { - streaming: true, + it('should customResponse alias to streaming', async () => { + const response = await urllib.request(`${_url}mock-bytes?size=1024`, { + customResponse: true, }); assert.equal(response.status, 200); assert.equal(response.headers['content-type'], undefined); @@ -125,12 +125,12 @@ describe('options.streaming.test.ts', () => { // console.log(response.headers); assert(isReadable(response.res as any)); const bytes = await readableToBytes(response.res); - assert.equal(bytes.length, 1024102400); + assert.equal(bytes.length, 1024); }); - it('should customResponse alias to streaming', async () => { + it('should get big streaming response', async () => { const response = await urllib.request(`${_url}mock-bytes?size=1024102400`, { - customResponse: true, + streaming: true, }); assert.equal(response.status, 200); assert.equal(response.headers['content-type'], undefined);