constructor()

in desktop/src/client/core/fetch/headers.ts [41:94]


    constructor(init?) {
        this[MAP] = Object.create(null);

        if (init instanceof Headers) {
            const rawHeaders = init.raw();
            const headerNames = Object.keys(rawHeaders);

            for (const headerName of headerNames) {
                for (const value of rawHeaders[headerName]) {
                    this.append(headerName, value);
                }
            }

            return;
        }

        // We don't worry about converting prop to ByteString here as append()
        // will handle it.
        if (init == null) {
            // no op
        } else if (typeof init === "object") {
            const method = init[Symbol.iterator];
            if (method != null) {
                if (typeof method !== "function") {
                    throw new TypeError("Header pairs must be iterable");
                }

                // sequence<sequence<ByteString>>
                // Note: per spec we have to first exhaust the lists then process them
                const pairs: any[] = [];
                for (const pair of init) {
                    if (typeof pair !== "object" || typeof pair[Symbol.iterator] !== "function") {
                        throw new TypeError("Each header pair must be iterable");
                    }
                    pairs.push(Array.from(pair));
                }

                for (const pair of pairs) {
                    if (pair.length !== 2) {
                        throw new TypeError("Each header pair must be a name/value tuple");
                    }
                    this.append(pair[0], pair[1]);
                }
            } else {
                // record<ByteString, ByteString>
                for (const key of Object.keys(init)) {
                    const value = init[key];
                    this.append(key, value);
                }
            }
        } else {
            throw new TypeError("Provided initializer must be an object");
        }
    }