Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bugs, increase coverage, update benchmark results
Fixes #1187
- Loading branch information
Showing
11 changed files
with
135 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -183,18 +183,18 @@ const internalBenchmark = (): void => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Results (i7-7700k, CPU governor: performance): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// got - promise x 3,092 ops/sec 卤5.25% (73 runs sampled) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// got - stream x 4,313 ops/sec 卤5.61% (72 runs sampled) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// got - promise core x 6,756 ops/sec 卤5.32% (80 runs sampled) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// got - stream core x 6,863 ops/sec 卤4.68% (76 runs sampled) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// got - stream core - normalized options x 7,960 ops/sec 卤3.83% (81 runs sampled) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// request - callback x 6,912 ops/sec 卤6.50% (76 runs sampled) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// request - stream x 7,821 ops/sec 卤4.28% (80 runs sampled) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// node-fetch - promise x 7,036 ops/sec 卤8.17% (78 runs sampled) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// node-fetch - stream x 7,877 ops/sec 卤4.17% (80 runs sampled) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// axios - promise x 6,613 ops/sec 卤3.22% (76 runs sampled) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// axios - stream x 8,642 ops/sec 卤2.84% (79 runs sampled) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// https - stream x 9,955 ops/sec 卤6.36% (76 runs sampled) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// got - promise x 3,204 ops/sec 卤5.27% (73 runs sampled) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// got - stream x 5,045 ops/sec 卤3.85% (77 runs sampled) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// got - promise core x 6,499 ops/sec 卤3.67% (77 runs sampled) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
szmarczak
Author
Collaborator
|
const got: Got = ((url: string | URL, options?: Options): GotReturn => { | |
let iteration = 0; | |
const iterateHandlers = (newOptions: NormalizedOptions): GotReturn => { | |
return defaults.handlers[iteration++]( | |
newOptions, | |
iteration === defaults.handlers.length ? getPromiseOrStream : iterateHandlers | |
) as GotReturn; | |
}; | |
if (is.plainObject(url)) { | |
options = { | |
...url as Options, | |
...options | |
}; | |
url = undefined as any; | |
} | |
try { | |
// Call `init` hooks | |
let initHookError: Error | undefined; | |
try { | |
callInitHooks(defaults.options.hooks.init, options); | |
callInitHooks(options?.hooks?.init, options); | |
} catch (error) { | |
initHookError = error; | |
} | |
// Normalize options & call handlers | |
const normalizedOptions = normalizeArguments(url, options, defaults.options); | |
normalizedOptions[kIsNormalizedAlready] = true; | |
if (initHookError) { | |
throw new RequestError(initHookError.message, initHookError, normalizedOptions); | |
} | |
// A bug. | |
// eslint-disable-next-line @typescript-eslint/return-await | |
return iterateHandlers(normalizedOptions); | |
} catch (error) { | |
if (options?.isStream) { | |
throw error; | |
} else { | |
// A bug. | |
// eslint-disable-next-line @typescript-eslint/return-await | |
return createRejection(error, defaults.options.hooks.beforeError, options?.hooks?.beforeError); | |
} | |
} | |
}) as Got; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -469,7 +469,7 @@ export default class Request extends Duplex implements RequestEvents<Request> { | |
|
||
declare options: NormalizedOptions; | ||
declare requestUrl: string; | ||
finalized: boolean; | ||
requestInitialized: boolean; | ||
redirects: string[]; | ||
|
||
constructor(url: string | URL, options: Options = {}, defaults?: Defaults) { | ||
|
@@ -480,7 +480,7 @@ export default class Request extends Duplex implements RequestEvents<Request> { | |
|
||
this[kDownloadedSize] = 0; | ||
this[kUploadedSize] = 0; | ||
this.finalized = false; | ||
this.requestInitialized = false; | ||
this[kServerResponsesPiped] = new Set<ServerResponse>(); | ||
this.redirects = []; | ||
this[kStopReading] = false; | ||
|
@@ -558,7 +558,7 @@ export default class Request extends Duplex implements RequestEvents<Request> { | |
job(); | ||
} | ||
|
||
this.finalized = true; | ||
this.requestInitialized = true; | ||
} catch (error) { | ||
if (error instanceof RequestError) { | ||
this._beforeError(error); | ||
|
@@ -674,11 +674,9 @@ export default class Request extends Duplex implements RequestEvents<Request> { | |
options.searchParams = new URLSearchParams(options.searchParams as Record<string, string>); | ||
|
||
// `normalizeArguments()` is also used to merge options | ||
if (defaults?.searchParams) { | ||
defaults.searchParams.forEach((value, key) => { | ||
(options!.searchParams as URLSearchParams).append(key, value); | ||
}); | ||
} | ||
defaults?.searchParams?.forEach((value, key) => { | ||
(options!.searchParams as URLSearchParams).append(key, value); | ||
}); | ||
This comment has been minimized.
Sorry, something went wrong.
jaulz
Contributor
|
||
} else { | ||
options.searchParams = defaults?.searchParams; | ||
} | ||
|
@@ -992,7 +990,7 @@ export default class Request extends Duplex implements RequestEvents<Request> { | |
this.emit('downloadProgress', this.downloadProgress); | ||
}); | ||
|
||
response.on('error', (error: Error) => { | ||
response.once('error', (error: Error) => { | ||
this._beforeError(new ReadError(error, this)); | ||
}); | ||
|
||
|
@@ -1415,7 +1413,7 @@ export default class Request extends Duplex implements RequestEvents<Request> { | |
this._writeRequest(chunk, encoding, callback); | ||
}; | ||
|
||
if (this.finalized) { | ||
if (this.requestInitialized) { | ||
write(); | ||
} else { | ||
this[kJobs].push(write); | ||
|
@@ -1470,7 +1468,7 @@ export default class Request extends Duplex implements RequestEvents<Request> { | |
}); | ||
}; | ||
|
||
if (this.finalized) { | ||
if (this.requestInitialized) { | ||
endRequest(); | ||
} else { | ||
this[kJobs].push(endRequest); | ||
|
@@ -1479,6 +1477,8 @@ export default class Request extends Duplex implements RequestEvents<Request> { | |
|
||
_destroy(error: Error | null, callback: (error: Error | null) => void): void { | ||
if (kRequest in this) { | ||
this[kCancelTimeouts]!(); | ||
|
||
// TODO: Remove the next `if` when these get fixed: | ||
// - https://github.com/nodejs/node/issues/32851 | ||
// - https://github.com/nock/nock/issues/1981 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import test from 'ava'; | ||
import WeakableMap from '../source/core/utils/weakable-map'; | ||
|
||
test('works as expected', t => { | ||
const weakable = new WeakableMap(); | ||
|
||
weakable.set('hello', 'world'); | ||
|
||
t.true(weakable.has('hello')); | ||
t.false(weakable.has('foobar')); | ||
t.is(weakable.get('hello'), 'world'); | ||
t.is(weakable.get('foobar'), undefined); | ||
|
||
const object = {}; | ||
const anotherObject = {}; | ||
weakable.set(object, 'world'); | ||
|
||
t.true(weakable.has(object)); | ||
t.false(weakable.has(anotherObject)); | ||
t.is(weakable.get(object), 'world'); | ||
t.is(weakable.get(anotherObject), undefined); | ||
}); |
We can speed up the
promise core
by mergingPromisableRequest
intoRequest
. Performance of extended classes is quite slow.