Security News
The Unpaid Backbone of Open Source: Solo Maintainers Face Increasing Security Demands
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
browser-headers
Advanced tools
Compatibility Layer for the Headers class
The Headers class defined in the fetch spec has been implemented slightly differently across browser vendors at the time of writing (Feb 2017).
This package intends to provide a wrapper for the Headers
class to ensure a consistent API and provides headers parsing from CLRF-delimited strings.
This package is written in TypeScript, but is designed to be used just as easily by JavaScript projects.
via npm:
$ npm install browser-headers
This library is tested against Chrome, Safari, Firefox, Opera, Edge, IE 10 and IE 9.
import BrowserHeaders from 'browser-headers';
const headers = new BrowserHeaders({
"content-type": "application/json",
"my-header": ["value-one","value-two"]
});
headers.forEach((key, values) => {
console.log(key, values);
});
// Output:
// "content-type", ["application/json"]
// "my-header", ["value-one","value-two"]
The BrowserHeaders
class has the following methods:
init
can be one of:
Headers
key-a: one\r\nkey-b: two
)BrowserHeaders
string->(string|string[])
(e.g. {"key-a":["one","two"],"key-b":"three"}
)Map<string, string|string[]>
The constructor takes an additional optional options
parameter of { splitValues: boolean = false }
, where
splitValues
defines whether the header values should be split by comma (,
) into separate strings - this is useful
to unify the .append
functionality of Headers
implementations (see the warning at the end of this README).
splitValues
should be used with caution and defaults to false
because it might split what is actually a single
logical value that contained a ,
.
Returns all of the values for that header key
as an array
Invokes the provided callback with each key and it's associated values as an array
Overwrites the key
with the value(s) specified.
Appends the value(s) to specified key
.
If the value
is specified:
Removes the specified value
from the key
if it is present.
Otherwise:
Removes all values for the key
if it is present.
If the value is specified:
Returns true if the key
contains the corresponding value
.
Otherwise:
Returns true if the key
has at least one value.
Appends the headers defined in the provided CLRF-delimited string (e.g. key-a: one\r\nkey-b: two
)
Returns an instance of the browser's Headers
class. This will throw an exception if the current browser does not have
the Headers
class.
.append
in native Headers
The .append
function of the Headers
class differs significantly between browsers.
Some browsers concatenate the values with ", "
or just ","
and others actually maintain the individual values such that
they can return later return an array. There is a constructor option (see above: splitValues
) that can be enabled to
attempt to parse these concatenated strings back into individual values.
const headers = new Headers();
headers.append("key-A", "one");
headers.append("key-A", "two");
const keyA = headers.get("key-A"); // or .getAll depending on the browser
console.log(typeof keyA);
console.log(keyA);
// Output in Edge 14:
// string
// one, two
// Output in Safari 10:
// string
// one,two
// Output in Chrome 56:
// object
// ["one", "two"]
FAQs
> Compatibility Layer for the Headers class
The npm package browser-headers receives a total of 116,398 weekly downloads. As such, browser-headers popularity was classified as popular.
We found that browser-headers demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.
Security News
A developer is accusing Tencent of violating the GPL by modifying a Python utility and changing its license to BSD, highlighting the importance of copyleft compliance.