From 4891d78fb4c5a6964e0d82b67c4a89482fd1f5cf Mon Sep 17 00:00:00 2001 From: Ambrose Chua Date: Fri, 10 Sep 2021 01:55:07 +0800 Subject: [PATCH] feat: Remove unnecessary nested object --- fixtures/all.js | 2 ++ fixtures/fetch.js | 20 ++++++++++++++++++++ fixtures/index-node.js | 2 ++ fixtures/request.js | 38 +++++++++++++++++++++++++++++++++----- package-lock.json | 6 ++++++ package.json | 1 + src/index.ts | 36 +++++++++++++++++++++++------------- src/types.ts | 7 ++++++- 8 files changed, 93 insertions(+), 19 deletions(-) create mode 100644 fixtures/fetch.js diff --git a/fixtures/all.js b/fixtures/all.js index 3f2c82f..527988a 100644 --- a/fixtures/all.js +++ b/fixtures/all.js @@ -1,6 +1,8 @@ import request from './request.js'; +import fetch from './fetch.js'; const tests = { ...request, + ...fetch, }; export default tests; diff --git a/fixtures/fetch.js b/fixtures/fetch.js new file mode 100644 index 0000000..3f9f671 --- /dev/null +++ b/fixtures/fetch.js @@ -0,0 +1,20 @@ +const tests = { + fetch: { + async acceptContentLength() { + try { + const ctrl = new AbortController(); + setTimeout(() => ctrl.abort(), 5000); + const response = await fetch('https://httpbin.org/get', { + headers: {'Content-Length': 2}, + signal: ctrl.signal, + }); + const o = await response.json(); + const length = o.headers['Content-Length']; + return `Content-Length: ${length}`; + } catch (error) { + return error.toString(); + } + }, + }, +}; +export default tests; diff --git a/fixtures/index-node.js b/fixtures/index-node.js index 6a11665..2d95b7c 100644 --- a/fixtures/index-node.js +++ b/fixtures/index-node.js @@ -1,5 +1,6 @@ import process from 'node:process'; import fetch, {Request, Response, Headers} from 'node-fetch'; +import {AbortController} from 'node-abort-controller'; import run from './run.js'; @@ -7,6 +8,7 @@ global.Request = Request; global.Response = Response; global.Headers = Headers; global.fetch = fetch; +global.AbortController = AbortController; async function main(file) { const tests = await import('./' + file + '.js'); diff --git a/fixtures/request.js b/fixtures/request.js index 88f8cc6..8c66552 100644 --- a/fixtures/request.js +++ b/fixtures/request.js @@ -1,20 +1,48 @@ /* eslint-disable no-new */ const tests = { request: { - async invalidURL() { + async unexpectedPercent() { try { new Request('http://example.com%'); - return {error: null}; + return null; } catch (error) { - return {error: error.toString()}; + return error.toString(); } }, async rejectCredentials() { try { new Request('http://user:pass@example.com'); - return {error: null}; + return null; } catch (error) { - return {error: error.toString()}; + return error.toString(); + } + }, + async rejectBodyInGET() { + try { + new Request('http://example.com', {body: 'a'}); + return null; + } catch (error) { + return error.toString(); + } + }, + async rejectEmptyBodyInGET() { + try { + new Request('http://example.com', {body: ''}); + return null; + } catch (error) { + return error.toString(); + } + }, + async acceptContentLength() { + try { + new Request('http://example.com', { + headers: { + 'Content-Length': 0, + }, + }); + return null; + } catch (error) { + return error.toString(); } }, }, diff --git a/package-lock.json b/package-lock.json index 3bf21d1..fa3d5d5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4106,6 +4106,12 @@ "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz", "integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw==" }, + "node-abort-controller": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/node-abort-controller/-/node-abort-controller-3.0.0.tgz", + "integrity": "sha512-IqMCPbihDpbHV4bNws015hU0svIBGyzPjJearwXMGJyungWdblbBcboNojTz9bWOrrJD3zIwmcr5w5c+NH+2+A==", + "dev": true + }, "node-fetch": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.0.0.tgz", diff --git a/package.json b/package.json index 02e7c4b..f33429d 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "devDependencies": { "@types/koa": "^2.13.4", "@types/node": "^16.9.0", + "node-abort-controller": "^3.0.0", "prettier": "^2.4.0", "typescript": "^4.4.2", "xo": "^0.44.0" diff --git a/src/index.ts b/src/index.ts index 8a3cc69..3fb923a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ import logger from '@wdio/logger'; import Table from 'cli-table3'; -import {Platform, Result, Context} from './types'; +import {Platform, Result, ResultObject, Context} from './types'; import PlatformBrowser from './platform-browser'; import PlatformNode from './platform-node'; import PlatformDeno from './platform-deno'; @@ -9,7 +9,7 @@ import {chrome, firefox, safari} from './browser-drivers'; import {FixturesServer} from './server'; const log = logger('fetch-compare'); -logger.setLevel('fetch-compare', 'info'); +logger.setLevel('fetch-compare', 'warn'); const silent = true; const platforms: Record = { @@ -20,15 +20,28 @@ const platforms: Record = { deno: new PlatformDeno(), }; -function* tabularNames(reference: Result): Iterable<[string, string, string]> { +function formatValue(v: ResultObject): string { + if (v === null) { + return 'null'; + } + + if (v === undefined) { + return 'undefined'; + } + + if (typeof v === 'string' || typeof v === 'number') { + return v.toString(); + } + + return JSON.stringify(v); +} + +function* tabularNames(reference: Result): Iterable<[string, string]> { /* eslint-disable guard-for-in */ for (const groupName in reference) { const results = reference[groupName]; for (const resultName in results) { - const resultObject = results[resultName]; - for (const key in resultObject) { - yield [groupName, resultName, key]; - } + yield [groupName, resultName]; } } /* eslint-enable guard-for-in */ @@ -41,13 +54,9 @@ function* tabularResults(results: Record): Iterable { } const names = Object.keys(results); - for (const [group, result, key] of tabularNames(ref)) { + for (const [group, result] of tabularNames(ref)) { for (const n of names) { - yield [ - `${group}:${result}:${key}`, - n, - results[n][group][result][key]?.toString() ?? 'null', - ]; + yield [`${group}:${result}`, n, formatValue(results[n][group][result])]; } } } @@ -68,6 +77,7 @@ async function run() { /* eslint-disable guard-for-in, no-await-in-loop */ for (const name in platforms) { try { + log.debug(name, 'starting'); results[name] = await platforms[name].run(ctx, 'all'); log.debug(name, results[name]); } catch (error: unknown) { diff --git a/src/types.ts b/src/types.ts index ff26c02..c0c666c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,9 @@ -export type ResultObject = Record; +export type ResultObject = + | string + | number + | null + | undefined + | Record; export type Result = Record>; export interface Context {