Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
headers-polyfill
Advanced tools
The headers-polyfill npm package provides a way to manipulate HTTP headers, allowing you to create, read, and modify HTTP headers in a standardized manner. It is designed to mimic the Headers interface provided by the Fetch API, making it useful for environments where this API is not available or for projects that aim for consistency across different environments.
Creating and appending headers
This feature allows you to create a new Headers object and append new headers to it. It's useful for setting up the headers required for an HTTP request.
{"const headers = new Headers();\nheaders.append('Content-Type', 'application/json');\nheaders.append('Authorization', 'Bearer your_token_here');"}
Reading header values
This feature enables you to read the value of a specific header by name. It's useful for inspecting headers received in an HTTP response.
{"const contentType = headers.get('Content-Type');"}
Checking for a header's existence
This feature allows you to check if a certain header exists within the Headers object. It's useful for conditional logic based on the presence of specific headers.
{"const hasContentType = headers.has('Content-Type');"}
Deleting a header
This feature enables you to remove a header from the Headers object. It's useful for modifying headers before sending an HTTP request or after processing an HTTP response.
{"headers.delete('Authorization');"}
Similar to headers-polyfill, fetch-headers provides an implementation of the Fetch API's Headers interface. It allows for manipulation of HTTP headers in environments where the Fetch API is not natively supported. The main difference lies in the specific implementation details and possibly the extent of compatibility with the Fetch API specification.
While primarily known for bringing window.fetch to Node.js, node-fetch also includes an implementation of the Headers interface. Compared to headers-polyfill, node-fetch offers a more comprehensive solution by also providing the ability to perform HTTP requests, making it a more feature-rich package for projects that require both fetching capabilities and headers manipulation.
headers-polyfill
A Headers
class polyfill and transformation library.
Various request issuing libraries utilize a different format of headers. This library chooses the Headers
instance as the middle-ground between server and client, and provides functions to convert that instance to primitives and vice-versa.
npm install headers-polyfill
This package exports the Headers
class that polyfills the native window.Headers
implementation. This allows you to construct and manage headers using the same API in non-browser environments.
import { Headers } from 'headers-polyfill'
const headers = new Headers({
Accept: '*/*',
'Content-Type': 'application/json',
})
headers.get('accept') // "*/*"
The Headers
polyfill instance supports the same methods as the standard Headers
instance:
As well as the iterator methods:
getRawHeaders()
Returns an object consisting of the header name/value pairs but preserving raw header names.
const headers = new Headers({
Accept: '*/*',
'Content-Type': 'application/json',
})
headers.raw()
// { "Accept": "*/*", "Content-Type": "application/json" }
headersToString: (h: Headers): string
import { headersToString } from 'headers-polyfill'
headersToString(
new Headers({
connection: 'keep-alive',
'content-type': ['text/plain', 'image/png'],
})
)
// connetion: keep-alive
// content-type: text/plain, image/png
headersToList: (h: Headers): Array<[string, string | string[]]>
import { headersToList } from 'headers-polyfill'
headersToList(
new Headers({
connection: 'keep-alive',
'content-type': ['text/plain', 'image/png'],
})
)
// [['connection', 'keep-alive'], ['content-type', ['text/plain', 'image/png']]]
headersToObject: (h: Headers): Record<string, string | string[]>
import { headersToObject } from 'headers-polyfill'
headersToObject(
new Headers({
connection: 'keep-alive',
'content-type': ['text/plain', 'image/png'],
})
)
// { connection: 'keep-alive', 'content-type': ['text/plain', 'image/png'] }
stringToHeaders: (s: string): Headers
import { stringToHeaders } from 'headers-polyfill'
const stringToHeaders(`
connection: keep-alive
content-type: text/plain, image/png
`)
// Headers { connection: 'keep-alive', 'content-type': ['text/plain', 'image/png'] }
listToHeaders: (l: Array<[string, string | string[]]>): Headers
import { listToHeaders } from 'headers-polyfill'
listToHeaders([
['connection', 'keep-alive'],
['content-type', ['text/plain', 'image/png']],
])
// Headers { connection: 'keep-alive', 'content-type': ['text/plain', 'image/png'] }
objectToHeaders: (o: Record<string, string | string[] | undefined>): Headers
import { objectToHeaders } from 'headers-polyfill'
objectToHeaders({
connection: 'keep-alive',
'content-type': ['text/plain', 'image/png'],
})
// Headers { connection: 'keep-alive', 'content-type': ['text/plain', 'image/png'] }
reduceHeadersObject: <R>(o: Record<string, string | string[]>, reducer: (acc: R, name: string, value: string | string[]) => R) => R
import { reduceHeadersObject } from 'headers-polyfill'
reduceHeadersObject <
HeadersObject >
({
Accept: '*/*',
'Content-Type': ['application/json', 'text/plain'],
},
(headers, name, value) => {
headers[name.toLowerCase()] = value
return headers
},
{})
// { 'accept': '*/*', 'content-type': ['application/json', 'text/plain'] }
appendHeader: (o: Record<string, string | string[]>, n: string, v: string | string[]): Record<string, string | string[]>
import { appendHeader } from 'headers-polyfill'
appendHeader(
{ 'content-type': 'application/json' },
'content-type',
'text/plain'
)
// { 'content-type': ['application/json', 'text/plain']}
flattenHeadersList: (l: Array<[string, string | string[]]>): Array<string, string>
import { flattenHeadersList } from 'headers-polyfill'
flattenHeadersList([['content-type', ['text/plain', 'image/png']]])
// ['content-type', 'text/plain, image/png']
flattenHeadersObject: (o: Record<string, string | string[]>): Record<string, string>
import { flattenHeadersObject } from 'headers-polyfill'
flattenHeadersObject({
'content-type': ['text/plain', 'image/png'],
})
// { 'content-type': 'text/plain, image/png' }
FAQs
A native "Headers" class polyfill.
The npm package headers-polyfill receives a total of 3,110,699 weekly downloads. As such, headers-polyfill popularity was classified as popular.
We found that headers-polyfill demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.