feat: Remove unnecessary nested object
parent
ff4300c306
commit
4891d78fb4
|
@ -1,6 +1,8 @@
|
||||||
import request from './request.js';
|
import request from './request.js';
|
||||||
|
import fetch from './fetch.js';
|
||||||
|
|
||||||
const tests = {
|
const tests = {
|
||||||
...request,
|
...request,
|
||||||
|
...fetch,
|
||||||
};
|
};
|
||||||
export default tests;
|
export default tests;
|
||||||
|
|
|
@ -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;
|
|
@ -1,5 +1,6 @@
|
||||||
import process from 'node:process';
|
import process from 'node:process';
|
||||||
import fetch, {Request, Response, Headers} from 'node-fetch';
|
import fetch, {Request, Response, Headers} from 'node-fetch';
|
||||||
|
import {AbortController} from 'node-abort-controller';
|
||||||
|
|
||||||
import run from './run.js';
|
import run from './run.js';
|
||||||
|
|
||||||
|
@ -7,6 +8,7 @@ global.Request = Request;
|
||||||
global.Response = Response;
|
global.Response = Response;
|
||||||
global.Headers = Headers;
|
global.Headers = Headers;
|
||||||
global.fetch = fetch;
|
global.fetch = fetch;
|
||||||
|
global.AbortController = AbortController;
|
||||||
|
|
||||||
async function main(file) {
|
async function main(file) {
|
||||||
const tests = await import('./' + file + '.js');
|
const tests = await import('./' + file + '.js');
|
||||||
|
|
|
@ -1,20 +1,48 @@
|
||||||
/* eslint-disable no-new */
|
/* eslint-disable no-new */
|
||||||
const tests = {
|
const tests = {
|
||||||
request: {
|
request: {
|
||||||
async invalidURL() {
|
async unexpectedPercent() {
|
||||||
try {
|
try {
|
||||||
new Request('http://example.com%');
|
new Request('http://example.com%');
|
||||||
return {error: null};
|
return null;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return {error: error.toString()};
|
return error.toString();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async rejectCredentials() {
|
async rejectCredentials() {
|
||||||
try {
|
try {
|
||||||
new Request('http://user:pass@example.com');
|
new Request('http://user:pass@example.com');
|
||||||
return {error: null};
|
return null;
|
||||||
} catch (error) {
|
} 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();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -4106,6 +4106,12 @@
|
||||||
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz",
|
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz",
|
||||||
"integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw=="
|
"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": {
|
"node-fetch": {
|
||||||
"version": "3.0.0",
|
"version": "3.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.0.0.tgz",
|
||||||
|
|
|
@ -28,6 +28,7 @@
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/koa": "^2.13.4",
|
"@types/koa": "^2.13.4",
|
||||||
"@types/node": "^16.9.0",
|
"@types/node": "^16.9.0",
|
||||||
|
"node-abort-controller": "^3.0.0",
|
||||||
"prettier": "^2.4.0",
|
"prettier": "^2.4.0",
|
||||||
"typescript": "^4.4.2",
|
"typescript": "^4.4.2",
|
||||||
"xo": "^0.44.0"
|
"xo": "^0.44.0"
|
||||||
|
|
36
src/index.ts
36
src/index.ts
|
@ -1,7 +1,7 @@
|
||||||
import logger from '@wdio/logger';
|
import logger from '@wdio/logger';
|
||||||
import Table from 'cli-table3';
|
import Table from 'cli-table3';
|
||||||
|
|
||||||
import {Platform, Result, Context} from './types';
|
import {Platform, Result, ResultObject, Context} from './types';
|
||||||
import PlatformBrowser from './platform-browser';
|
import PlatformBrowser from './platform-browser';
|
||||||
import PlatformNode from './platform-node';
|
import PlatformNode from './platform-node';
|
||||||
import PlatformDeno from './platform-deno';
|
import PlatformDeno from './platform-deno';
|
||||||
|
@ -9,7 +9,7 @@ import {chrome, firefox, safari} from './browser-drivers';
|
||||||
import {FixturesServer} from './server';
|
import {FixturesServer} from './server';
|
||||||
|
|
||||||
const log = logger('fetch-compare');
|
const log = logger('fetch-compare');
|
||||||
logger.setLevel('fetch-compare', 'info');
|
logger.setLevel('fetch-compare', 'warn');
|
||||||
|
|
||||||
const silent = true;
|
const silent = true;
|
||||||
const platforms: Record<string, Platform> = {
|
const platforms: Record<string, Platform> = {
|
||||||
|
@ -20,15 +20,28 @@ const platforms: Record<string, Platform> = {
|
||||||
deno: new PlatformDeno(),
|
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 */
|
/* eslint-disable guard-for-in */
|
||||||
for (const groupName in reference) {
|
for (const groupName in reference) {
|
||||||
const results = reference[groupName];
|
const results = reference[groupName];
|
||||||
for (const resultName in results) {
|
for (const resultName in results) {
|
||||||
const resultObject = results[resultName];
|
yield [groupName, resultName];
|
||||||
for (const key in resultObject) {
|
|
||||||
yield [groupName, resultName, key];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* eslint-enable guard-for-in */
|
/* eslint-enable guard-for-in */
|
||||||
|
@ -41,13 +54,9 @@ function* tabularResults(results: Record<string, Result>): Iterable<string[]> {
|
||||||
}
|
}
|
||||||
|
|
||||||
const names = Object.keys(results);
|
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) {
|
for (const n of names) {
|
||||||
yield [
|
yield [`${group}:${result}`, n, formatValue(results[n][group][result])];
|
||||||
`${group}:${result}:${key}`,
|
|
||||||
n,
|
|
||||||
results[n][group][result][key]?.toString() ?? 'null',
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -68,6 +77,7 @@ async function run() {
|
||||||
/* eslint-disable guard-for-in, no-await-in-loop */
|
/* eslint-disable guard-for-in, no-await-in-loop */
|
||||||
for (const name in platforms) {
|
for (const name in platforms) {
|
||||||
try {
|
try {
|
||||||
|
log.debug(name, 'starting');
|
||||||
results[name] = await platforms[name].run(ctx, 'all');
|
results[name] = await platforms[name].run(ctx, 'all');
|
||||||
log.debug(name, results[name]);
|
log.debug(name, results[name]);
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
|
|
|
@ -1,4 +1,9 @@
|
||||||
export type ResultObject = Record<string, string | number | null>;
|
export type ResultObject =
|
||||||
|
| string
|
||||||
|
| number
|
||||||
|
| null
|
||||||
|
| undefined
|
||||||
|
| Record<string, unknown>;
|
||||||
export type Result = Record<string, Record<string, ResultObject>>;
|
export type Result = Record<string, Record<string, ResultObject>>;
|
||||||
|
|
||||||
export interface Context {
|
export interface Context {
|
||||||
|
|
Loading…
Reference in New Issue