All files / ethers.js/src.ts/utils geturl.ts

88.57% Statements 124/140
70.37% Branches 19/27
66.66% Functions 2/3
88.57% Lines 124/140

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 1421x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1197x 1197x 1197x 1197x 1197x 1197x 1197x 1197x 1197x 1197x 1197x 1197x 1197x 1197x 1197x 1197x 1197x 1197x 1197x     1197x 1197x 1197x 1197x 1197x 1197x 1197x 1197x 1197x 1197x 1197x 1197x 1197x 1197x 1197x 1197x 1197x 1197x 1197x 1197x 1197x     1197x 1197x 1197x 1197x   1197x 1197x 1197x 1196x 1196x 1196x 13738x 13738x 222x 222x 13738x 13738x 1196x 1196x 1196x 1196x 1196x 1196x 1422x 1422x 1422x 1422x     1422x 1422x 1422x 1196x 1422x 226x 226x 226x 226x 226x 1196x 1196x 1196x 1196x 1196x 966x 966x 1196x 1196x 1196x 1196x         1196x 1196x 1196x       1196x 1197x 1197x 1197x 1197x 1197x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x        
import http from "http";
import https from "https";
import { gunzipSync } from "zlib";
 
import { assert, makeError } from "./errors.js";
import { getBytes } from "./data.js";
 
import type {
    FetchGetUrlFunc, FetchRequest, FetchCancelSignal, GetUrlResponse
} from "./fetch.js";
 
/**
 *  @_ignore:
 */
export function createGetUrl(options?: Record<string, any>): FetchGetUrlFunc {
 
    async function getUrl(req: FetchRequest, signal?: FetchCancelSignal): Promise<GetUrlResponse> {
        // Make sure we weren't cancelled before sending
        assert(signal == null || !signal.cancelled, "request cancelled before sending", "CANCELLED");
 
        const protocol = req.url.split(":")[0].toLowerCase();
 
        assert(protocol === "http" || protocol === "https", `unsupported protocol ${ protocol }`, "UNSUPPORTED_OPERATION", {
            info: { protocol },
            operation: "request"
        });
 
        assert(protocol === "https" || !req.credentials || req.allowInsecureAuthentication, "insecure authorized connections unsupported", "UNSUPPORTED_OPERATION", {
            operation: "request"
        });
 
        const method = req.method;
        const headers = Object.assign({ }, req.headers);
 
        const reqOptions: any = { method, headers };
        if (options) {
            if (options.agent) { reqOptions.agent = options.agent; }
        }
 
        // Create a Node-specific AbortController, if available
        let abort: null | AbortController = null;
        try {
            abort = new AbortController();
            reqOptions.abort = abort.signal;
        } catch (e) { console.log(e); }
 
        const request = ((protocol === "http") ? http: https).request(req.url, reqOptions);
 
        request.setTimeout(req.timeout);
 
        const body = req.body;
        if (body) { request.write(Buffer.from(body)); }
 
        request.end();
 
        return new Promise((resolve, reject) => {
 
            if (signal) {
                signal.addListener(() => {
                    if (abort) { abort.abort(); }
                    reject(makeError("request cancelled", "CANCELLED"));
                });
            }
 
            request.on("timeout", () => {
                reject(makeError("request timeout", "TIMEOUT"));
            });
 
            request.once("response", (resp: http.IncomingMessage) => {
                const statusCode = resp.statusCode || 0;
                const statusMessage = resp.statusMessage || "";
                const 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 }>{ });
 
                let body: null | Uint8Array = null;
                //resp.setEncoding("utf8");
 
                resp.on("data", (chunk: Uint8Array) => {
                    if (signal) {
                        try {
                            signal.checkSignal();
                        } catch (error) {
                            return reject(error);
                        }
                    }
 
                    if (body == null) {
                        body = chunk;
                    } else {
                        const newBody = new Uint8Array(body.length + chunk.length);
                        newBody.set(body, 0);
                        newBody.set(chunk, body.length);
                        body = newBody;
                    }
                });
 
                resp.on("end", () => {
                    try {
                        if (headers["content-encoding"] === "gzip" && body) {
                            body = getBytes(gunzipSync(body));
                        }
 
                        resolve({ statusCode, statusMessage, headers, body });
 
                    } catch (error) {
                        reject(makeError("bad response data", "SERVER_ERROR", {
                            request: req, info: { response: resp, error }
                        }));
                    }
                });
 
                resp.on("error", (error) => {
                //@TODO: Should this just return nornal response with a server error?
                    (<any>error).response = { statusCode, statusMessage, headers, body };
                    reject(error);
                });
            });
 
            request.on("error", (error) => { reject(error); });
        });
    }
 
    return getUrl;
}
 
// @TODO: remove in v7; provided for backwards compat
const defaultGetUrl: FetchGetUrlFunc = createGetUrl({ });
 
/**
 *  @_ignore:
 */
export async function getUrl(req: FetchRequest, signal?: FetchCancelSignal): Promise<GetUrlResponse> {
    return defaultGetUrl(req, signal);
}