Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Unhandled promise rejections when self-signed in jsdom
Close #3128
- Loading branch information
Showing
3 changed files
with
104 additions
and
6 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
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,64 @@ | ||
import { Requester } from '@hint/utils-connector-tools'; | ||
import { ResourceLoader } from 'jsdom'; | ||
import { NetworkData } from 'hint'; | ||
import { debug as d } from '@hint/utils'; | ||
|
||
const debug: debug.IDebugger = d(__filename); | ||
|
||
export class EvaluateCustomResourceLoader extends ResourceLoader { | ||
private _requester: Requester; | ||
private _baseUrl: string; | ||
|
||
public constructor(options: any, url: string) { | ||
super(); | ||
|
||
this._requester = new Requester(options); | ||
this._baseUrl = url; | ||
} | ||
|
||
public async fetch(url: string): Promise<Buffer | null> { | ||
if (!url) { | ||
const promise = Promise.resolve(null); | ||
|
||
(promise as any).abort = () => { }; | ||
|
||
return await promise; | ||
} | ||
|
||
const urlAsUrl = new URL(url); | ||
let resourceUrl: string = urlAsUrl.href; | ||
|
||
if (!urlAsUrl.protocol) { | ||
resourceUrl = new URL(resourceUrl, this._baseUrl).href; | ||
} | ||
|
||
let abort: Function; | ||
|
||
const promise = new Promise<Buffer>(async (resolve, reject) => { | ||
abort = reject; | ||
|
||
try { | ||
const resourceNetworkData: NetworkData = await this._requester.get(resourceUrl); | ||
|
||
debug(`resource ${resourceUrl} fetched`); | ||
|
||
return resolve(resourceNetworkData.response.body.rawContent); | ||
} catch (err) { | ||
return reject(err); | ||
} | ||
}); | ||
|
||
/* | ||
* When jsdom is close (because of an exception) it will try to close | ||
* all the pending connections calling to `abort`. | ||
*/ | ||
/* istanbul ignore next */ | ||
(promise as any).abort = () => { | ||
const error = new Error('request canceled by user'); | ||
|
||
abort(error); | ||
}; | ||
|
||
return promise; | ||
} | ||
} |
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