MCPcopy Create free account
hub / github.com/nodejs/node / fill

Function fill

deps/undici/src/lib/web/fetch/headers.js:45–80  ·  view source on GitHub ↗

* @param {Headers} headers * @param {Array|Object} object

(headers, object)

Source from the content-addressed store, hash-verified

43 * @param {Array|Object} object
44 */
45function fill (headers, object) {
46 // To fill a Headers object headers with a given object object, run these steps:
47
48 // 1. If object is a sequence, then for each header in object:
49 // Note: webidl conversion to array has already been done.
50 if (Array.isArray(object)) {
51 for (let i = 0; i < object.length; ++i) {
52 const header = object[i]
53 // 1. If header does not contain exactly two items, then throw a TypeError.
54 if (header.length !== 2) {
55 throw webidl.errors.exception({
56 header: 'Headers constructor',
57 message: `expected name/value pair to be length 2, found ${header.length}.`
58 })
59 }
60
61 // 2. Append (header’s first item, header’s second item) to headers.
62 appendHeader(headers, header[0], header[1])
63 }
64 } else if (typeof object === 'object' && object !== null) {
65 // Note: null should throw
66
67 // 2. Otherwise, object is a record, then for each key → value in object,
68 // append (key, value) to headers
69 const keys = Object.keys(object)
70 for (let i = 0; i < keys.length; ++i) {
71 appendHeader(headers, keys[i], object[keys[i]])
72 }
73 } else {
74 throw webidl.errors.conversionFailed({
75 prefix: 'Headers constructor',
76 argument: 'Argument 1',
77 types: ['sequence<sequence<ByteString>>', 'record<ByteString, ByteString>']
78 })
79 }
80}
81
82/**
83 * @see https://fetch.spec.whatwg.org/#concept-headers-append

Callers 2

constructorMethod · 0.70
initializeResponseFunction · 0.70

Calls 4

exceptionMethod · 0.80
conversionFailedMethod · 0.80
appendHeaderFunction · 0.70
keysMethod · 0.65

Tested by

no test coverage detected