-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
8 changed files
with
215 additions
and
85 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
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,51 @@ | ||
"use strict"; | ||
|
||
export type GetUrlResponse = { | ||
statusCode: number, | ||
statusMessage: string; | ||
headers: { [ key: string] : string }; | ||
body: string; | ||
}; | ||
|
||
export type Options = { | ||
method?: string, | ||
body?: string | ||
headers?: { [ key: string] : string }, | ||
}; | ||
|
||
export async function getUrl(href: string, options?: Options): Promise<GetUrlResponse> { | ||
if (options == null) { options = { }; } | ||
|
||
const request = { | ||
method: (options.method || "GET"), | ||
headers: (options.headers || { }), | ||
body: (options.body || undefined), | ||
|
||
mode: <RequestMode>"cors", // no-cors, cors, *same-origin | ||
cache: <RequestCache>"no-cache", // *default, no-cache, reload, force-cache, only-if-cached | ||
credentials: <RequestCredentials>"same-origin", // include, *same-origin, omit | ||
redirect: <RequestRedirect>"follow", // manual, *follow, error | ||
referrer: "client", // no-referrer, *client | ||
}; | ||
|
||
const response = await fetch(href, request); | ||
const body = await response.text(); | ||
|
||
const headers: { [ name: string ]: string } = { }; | ||
if (response.headers.forEach) { | ||
response.headers.forEach((value, key) => { | ||
headers[key.toLowerCase()] = value; | ||
}); | ||
} else { | ||
(<() => Array<string>>((<any>(response.headers)).keys))().forEach((key) => { | ||
headers[key.toLowerCase()] = response.headers.get(key); | ||
}); | ||
} | ||
|
||
return { | ||
headers: headers, | ||
statusCode: response.status, | ||
statusMessage: response.statusText, | ||
body: body, | ||
} | ||
} |
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,95 @@ | ||
"use strict"; | ||
|
||
import http from "http"; | ||
import https from "https"; | ||
import { URL } from "url" | ||
|
||
import { Logger } from "@ethersproject/logger"; | ||
import { version } from "./_version"; | ||
const logger = new Logger(version); | ||
|
||
export type GetUrlResponse = { | ||
statusCode: number, | ||
statusMessage: string; | ||
headers: { [ key: string] : string }; | ||
body: string; | ||
}; | ||
|
||
export type Options = { | ||
method?: string, | ||
body?: string | ||
headers?: { [ key: string] : string }, | ||
}; | ||
|
||
function getResponse(request: http.ClientRequest): Promise<GetUrlResponse> { | ||
return new Promise((resolve, reject) => { | ||
request.once("response", (resp: http.IncomingMessage) => { | ||
const response: GetUrlResponse = { | ||
statusCode: resp.statusCode, | ||
statusMessage: resp.statusMessage, | ||
headers: Object.keys(resp.headers).reduce((accum, name) => { | ||
let value = resp.headers[name]; | ||
if (Array.isArray(value)) { | ||
value = value.join(", "); | ||
} | ||
accum[name] = value; | ||
return accum; | ||
}, <{ [ name: string ]: string }>{ }), | ||
body: null | ||
}; | ||
resp.setEncoding("utf8"); | ||
|
||
resp.on("data", (chunk: string) => { | ||
if (response.body == null) { response.body = ""; } | ||
response.body += chunk; | ||
}); | ||
|
||
resp.on("end", () => { | ||
resolve(response); | ||
}); | ||
|
||
resp.on("error", (error) => { | ||
(<any>error).response = response; | ||
reject(error); | ||
}); | ||
}); | ||
|
||
request.on("error", (error) => { reject(error); }); | ||
}); | ||
} | ||
|
||
export async function getUrl(href: string, options?: Options): Promise<GetUrlResponse> { | ||
if (options == null) { options = { }; } | ||
|
||
const url = new URL(href); | ||
|
||
const request = { | ||
method: (options.method || "GET"), | ||
headers: (options.headers || { }), | ||
}; | ||
|
||
let req: http.ClientRequest = null; | ||
switch (url.protocol) { | ||
case "http:": { | ||
req = http.request(url, request); | ||
break; | ||
} | ||
case "https:": | ||
req = https.request(url, request); | ||
break; | ||
default: | ||
logger.throwError(`unsupported protocol ${ url.protocol }`, Logger.errors.UNSUPPORTED_OPERATION, { | ||
protocol: url.protocol, | ||
operation: "request" | ||
}); | ||
} | ||
|
||
if (options.body) { | ||
req.write(options.body); | ||
} | ||
req.end(); | ||
|
||
const response = await getResponse(req); | ||
return response; | ||
} | ||
|
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 |
---|---|---|
@@ -1,17 +1,4 @@ | ||
declare module "xmlhttprequest" { | ||
export class XMLHttpRequest { | ||
readyState: number; | ||
status: number; | ||
responseText: string; | ||
|
||
constructor(); | ||
open(method: string, url: string, async?: boolean): void; | ||
setRequestHeader(key: string, value: string): void; | ||
send(body?: string): void; | ||
abort(): void; | ||
|
||
onreadystatechange: () => void; | ||
onerror: (error: Error) => void; | ||
} | ||
declare module "node-fetch" { | ||
function fetch(url: string, options: any): Promise<Response>; | ||
export default fetch; | ||
} | ||
|
Oops, something went wrong.