Skip to content

Commit

Permalink
Merge pull request #5 from jimmywarting/fix
Browse files Browse the repository at this point in the history
fix: Correctly converted to sequence and thisArg
  • Loading branch information
jimmywarting authored Dec 31, 2021
2 parents 0bac872 + 66d63ac commit 5377d8e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
2 changes: 1 addition & 1 deletion headers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class Headers {
key: string,
parent: Headers
) => void,
thisArg: Headers
thisArg: globalThis
): void

toString(): string
Expand Down
13 changes: 11 additions & 2 deletions headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,19 @@ function fillHeaders (bag, object) {

if (iterable) {
// @ts-ignore
for (const header of object) {
for (let header of object) {
if (typeof header === 'string') {
throw new TypeError('The provided value cannot be converted to a sequence.')
}

if (header[Symbol.iterator] && !Array.isArray(header)) {
header = [...header]
}

if (header.length !== 2) {
throw new TypeError(`Invalid header. Length must be 2, but is ${header.length}`)
}

appendHeader(bag, header[0], header[1])
}
} else {
Expand Down Expand Up @@ -166,7 +175,7 @@ export class Headers {
appendHeader(bag, name, value)
}

forEach (callback, thisArg = this) {
forEach (callback, thisArg = globalThis) {
const bag = assert(this, arguments.length, 1, 'forEach')
if (typeof callback !== 'function') {
throw new TypeError(
Expand Down
41 changes: 27 additions & 14 deletions test/own-misc-test.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,40 @@ test(() => {
const headers = new Headers()
bag.get(headers).guard = 'immutable'
assert_throws_js(TypeError, () => headers.append('foo', 'bar'))
assert_throws_js(TypeError, () => headers.set('foo', 'bar'))
}, 'it throws when guard is immutable')

test(() => {
const headers = new Headers([
['a', 'baz'],
['a', 'baz'],
['a', 'bar']
['a', '1'].values(),
new Set(['b', '2']),
])
bag.get(headers).guard = 'immutable'
assert_throws_js(TypeError, () => headers.append('foo', 'bar'))
}, 'it throws when guard is immutable')
assert_equals(headers.get('a'), '1')
assert_equals(headers.get('b'), '2')
}, 'it can convert iterables to sequence')

test(() => {
const headers = new Headers([
['a', 'baz'],
['a', 'baz'],
['a', 'bar']
])
bag.get(headers).guard = 'immutable'
assert_throws_js(TypeError, () => headers.append('foo', 'bar'))
}, 'it throws when guard is immutable')
// string is also iterable but not possible to converted to a sequence
assert_throws_js(TypeError, () => new Headers([ 'a1' ]))
}, 'it cant convert string to sequence')

test(() => {
new Headers({a: '1'}).forEach(function () {
assert_equals(this, globalThis)
})
}, '`this` in foreach is global object')

test(() => {
new Headers({a: '1'}).forEach(function () {
assert_equals(this, globalThis)
})
}, '`this` in foreach is global object')

test(() => {
new Headers({a: '1'}).forEach(function () {
assert_true(Array.isArray(this))
}, [])
}, 'change thisArg in foreach')

const headers = new Headers()
headers[Symbol.for('nodejs.util.inspect.custom')]()

0 comments on commit 5377d8e

Please sign in to comment.