Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add headers impl #38986

Closed
Closed
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
502e14b
add headers impl
Ethan-Arrowood Jun 10, 2021
b0f9a75
update headers and begin porting tests
Ethan-Arrowood Jun 10, 2021
499c1c6
add in progress test script
Ethan-Arrowood Jun 15, 2021
357ba5d
complete test migration
Ethan-Arrowood Jun 21, 2021
b34a484
add docs
Ethan-Arrowood Jun 21, 2021
f8f2059
fix ordering
Ethan-Arrowood Jun 21, 2021
b585716
lint fixes
Ethan-Arrowood Jun 21, 2021
865d422
Update doc/api/fetch.md
Ethan-Arrowood Jun 21, 2021
c96bf21
Update lib/internal/fetch/headers.js
Ethan-Arrowood Jun 21, 2021
e7413b1
Update lib/internal/fetch/headers.js
Ethan-Arrowood Jun 21, 2021
4d96cf4
Update test/parallel/test-headers.js
Ethan-Arrowood Jun 21, 2021
6b726a9
Update test/parallel/test-headers.js
Ethan-Arrowood Jun 21, 2021
c735d9e
use entries for iterator
Ethan-Arrowood Jun 21, 2021
173ccef
lint md
Ethan-Arrowood Jun 21, 2021
d856bd4
fix lint again
Ethan-Arrowood Jun 21, 2021
bed131e
add missing character
Ethan-Arrowood Jun 21, 2021
a87342f
Update doc/api/fetch.md
Ethan-Arrowood Jun 21, 2021
71c1aa2
Update doc/api/fetch.md
Ethan-Arrowood Jun 21, 2021
d5e3df3
Update lib/internal/fetch/headers.js
Ethan-Arrowood Jun 21, 2021
c8d156a
Update lib/internal/fetch/headers.js
Ethan-Arrowood Jun 21, 2021
8fdd64c
Update lib/internal/fetch/headers.js
Ethan-Arrowood Jun 21, 2021
d66e313
fix lint and tests
Ethan-Arrowood Jun 21, 2021
1d042f0
Update lib/internal/fetch/headers.js
Ethan-Arrowood Jun 21, 2021
0a58d93
Update lib/internal/fetch/headers.js
Ethan-Arrowood Jun 21, 2021
a85b1c0
Update lib/internal/fetch/headers.js
Ethan-Arrowood Jul 22, 2021
58da701
incorporate review and fix failing test
Ethan-Arrowood Jul 22, 2021
92b9519
export api
Ethan-Arrowood Jul 22, 2021
ce73c08
Merge branch 'master' into feature/fetch-headers
Ethan-Arrowood Jul 22, 2021
6f212c0
add inspect and docs
Ethan-Arrowood Jul 22, 2021
6f06698
Update lib/fetch.js
Ethan-Arrowood Jul 26, 2021
ae223ca
Update lib/fetch.js
Ethan-Arrowood Jul 26, 2021
1ef66a0
Update lib/internal/fetch/headers.js
Ethan-Arrowood Jul 26, 2021
a9a7b4d
Update lib/internal/fetch/headers.js
Ethan-Arrowood Jul 26, 2021
3b902a1
incorporate review changes
Ethan-Arrowood Jul 26, 2021
1568b7c
Merge branch 'master' into feature/fetch-headers
Ethan-Arrowood Jul 26, 2021
cd38842
lint fixes
Ethan-Arrowood Jul 26, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
incorporate review and fix failing test
Rate limit · GitHub

Access has been restricted

You have triggered a rate limit.

Please wait a few minutes before you try again;
in some cases this may take up to an hour.

Ethan-Arrowood committed Jul 22, 2021
commit 58da7013f6bd15b9beb1a3a487b217281d2e4097
73 changes: 66 additions & 7 deletions lib/internal/fetch/headers.js
Original file line number Diff line number Diff line change
@@ -9,15 +9,16 @@ const {
} = require('internal/errors');

const {
Array,
ArrayIsArray,
ArrayPrototypeSplice,
MathFloor,
ObjectEntries,
RegExpPrototypeSymbolReplace,
StringPrototypeLocaleCompare,
StringPrototypeToLocaleLowerCase,
Symbol,
SymbolIterator
SymbolIterator,
ObjectDefineProperties
} = primordials;

const { validateObject } = require('internal/validators');
@@ -77,8 +78,12 @@ function normalizeAndValidateHeaderValue(name, value) {
return normalizedHeaderValue;
}

function isHeaders (object) {
return kHeadersList in object
}

function fill(headers, object) {
if (kHeadersList in object) {
if (isHeaders(object)) {
// Object is instance of Headers
headers[kHeadersList] = ArrayPrototypeSlice(object[kHeadersList]);
} else if (ArrayIsArray(object)) {
@@ -87,7 +92,7 @@ function fill(headers, object) {
// Array of arrays
for (let i = 0; i < object.length; i++) {
if (object[i].length !== 2) {
throw new ERR_INVALID_ARG_VALUE('init', header,
throw new ERR_INVALID_ARG_VALUE('init', object[i],
'is not of length 2');
}
headers.append(object[i][0], object[i][1]);
@@ -123,12 +128,22 @@ function fill(headers, object) {

class Headers {
constructor(init = {}) {
// fail silently on primitives
const typeofInit = typeof init
if (typeofInit === 'number'
|| typeofInit === 'string'
|| typeofInit === 'boolean'
|| typeofInit === 'function' ) return
validateObject(init, 'init', { allowArray: true });
this[kHeadersList] = [];
fill(this, init);
}

append(name, value) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have to add an argument length check for the public methods. When you call .append() or .append('foo') on Chrome/Edge/FireFox/Safari you get a TypeError about invalid arg length / missing arguments. Should I use the ERR_MISSING_ARGS to accomplish this?

if (!isHeaders(this)) {
throw new ERR_INVALID_THIS('Headers');
}

const normalizedName = normalizeAndValidateHeaderName(name);
const normalizedValue = normalizeAndValidateHeaderValue(name, value);

@@ -137,21 +152,29 @@ class Headers {
if (this[kHeadersList][index] === normalizedName) {
this[kHeadersList][index + 1] += `, ${normalizedValue}`;
} else {
this[kHeadersList].splice(index, 0, normalizedName, normalizedValue);
ArrayPrototypeSplice(this[kHeadersList], index, 0, normalizedName, normalizedValue);
}
}

delete(name) {
if (!isHeaders(this)) {
throw new ERR_INVALID_THIS('Headers');
}

const normalizedName = normalizeAndValidateHeaderName(name);

const index = binarySearch(this[kHeadersList], normalizedName);

if (this[kHeadersList][index] === normalizedName) {
this[kHeadersList].splice(index, 2);
ArrayPrototypeSplice(this[kHeadersList], index, 2);
}
}

get(name) {
if (!isHeaders(this)) {
throw new ERR_INVALID_THIS('Headers');
}

const normalizedName = normalizeAndValidateHeaderName(name);

const index = binarySearch(this[kHeadersList], normalizedName);
@@ -164,6 +187,10 @@ class Headers {
}

has(name) {
if (!isHeaders(this)) {
throw new ERR_INVALID_THIS('Headers');
}

const normalizedName = normalizeAndValidateHeaderName(name);

const index = binarySearch(this[kHeadersList], normalizedName);
@@ -172,36 +199,56 @@ class Headers {
}

set(name, value) {
if (!isHeaders(this)) {
throw new ERR_INVALID_THIS('Headers');
}

const normalizedName = normalizeAndValidateHeaderName(name);
const normalizedValue = normalizeAndValidateHeaderValue(name, value);

const index = binarySearch(this[kHeadersList], normalizedName);
if (this[kHeadersList][index] === normalizedName) {
this[kHeadersList][index + 1] = normalizedValue;
} else {
this[kHeadersList].splice(index, 0, normalizedName, normalizedValue);
ArrayPrototypeSplice(this[kHeadersList], index, 0, normalizedName, normalizedValue);
}
}

* keys() {
if (!isHeaders(this)) {
throw new ERR_INVALID_THIS('Headers');
}

for (let index = 0; index < this[kHeadersList].length; index += 2) {
yield this[kHeadersList][index];
}
}

* values() {
if (!isHeaders(this)) {
throw new ERR_INVALID_THIS('Headers');
}

for (let index = 1; index < this[kHeadersList].length; index += 2) {
yield this[kHeadersList][index];
}
}

* entries() {
if (!isHeaders(this)) {
throw new ERR_INVALID_THIS('Headers');
}

for (let index = 0; index < this[kHeadersList].length; index += 2) {
yield [this[kHeadersList][index], this[kHeadersList][index + 1]];
}
}

forEach(callback, thisArg) {
if (!isHeaders(this)) {
throw new ERR_INVALID_THIS('Headers');
}

for (let index = 0; index < this[kHeadersList].length; index += 2) {
callback.call(
thisArg,
@@ -215,6 +262,18 @@ class Headers {

Headers.prototype[SymbolIterator] = Headers.prototype.entries;

ObjectDefineProperties(Headers.prototype, {
append: { enumerable: true },
delete: { enumerable: true },
get: { enumerable: true },
has: { enumerable: true },
set: { enumerable: true },
keys: { enumerable: true },
values: { enumerable: true },
entries: { enumerable: true },
forEach: { enumerable: true },
});

module.exports = {
Headers,
binarySearch,
1 change: 1 addition & 0 deletions test/parallel/test-headers.js
Original file line number Diff line number Diff line change
@@ -98,6 +98,7 @@ const {
function() {},
1,
'test',
true
].forEach((arg) => new Headers(arg));
}