Skip to content

feat(fetch): add support for HTTP2 pseudo headers #104

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions .changeset/wise-dogs-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@web-std/fetch": patch
---

Support HTTP2 pseudo-headers like `:authority`, `:method`, etc.
3 changes: 2 additions & 1 deletion .github/workflows/blob.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
name: Typecheck
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 16
Expand All @@ -42,9 +43,9 @@ jobs:
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
node-version:
- 14
- 16
os:
- ubuntu-latest
Expand Down
16 changes: 2 additions & 14 deletions .github/workflows/fetch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
name: Typecheck
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 16
Expand All @@ -44,22 +45,16 @@ jobs:
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
node-version:
- 14
- 16
os:
- ubuntu-latest
- windows-latest
- macos-latest
project:
- fetch
exclude:
- os: windows-latest
node-version: 14
# On macOS, run tests with only the LTS environments.
- os: macos-latest
node-version: 14

steps:
- uses: actions/checkout@v2
Expand All @@ -75,12 +70,5 @@ jobs:
- name: Test (ESM)
run: yarn --cwd packages/${{matrix.project}} test -- --colors

# upload coverage only once
- name: Coveralls
uses: coverallsapp/github-action@master
if: matrix.node == '14' && matrix.os == 'ubuntu-latest'
with:
github-token: ${{ secrets.GITHUB_TOKEN }}

- name: Test (CJS)
run: yarn --cwd packages/${{matrix.project}} test:cjs
3 changes: 2 additions & 1 deletion .github/workflows/file.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
name: Typecheck
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 16
Expand All @@ -42,9 +43,9 @@ jobs:
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
node-version:
- 14
- 16
os:
- ubuntu-latest
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/form-data.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
name: Typecheck
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 16
Expand All @@ -43,9 +44,9 @@ jobs:
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
node-version:
- 14
- 16
os:
- ubuntu-latest
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/stream.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
name: Typecheck
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 16
Expand All @@ -43,9 +44,9 @@ jobs:
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
node-version:
- 14
- 16
os:
- ubuntu-latest
Expand Down Expand Up @@ -76,6 +77,7 @@ jobs:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
node-version:
- 16
Expand Down
38 changes: 18 additions & 20 deletions packages/fetch/src/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,25 @@ import {types} from 'util';
import http from 'http';
import { isIterable } from './utils/is.js'

const validators = /** @type {{validateHeaderName?:(name:string) => any, validateHeaderValue?:(name:string, value:string) => any}} */
(http)
/** @type {{validateHeaderValue?:(name:string, value:string) => any}} */
const validators = (http)

const validateHeaderName = typeof validators.validateHeaderName === 'function' ?
validators.validateHeaderName :
/**
* @param {string} name
*/
name => {
if (!/^[\^`\-\w!#$%&'*+.|~]+$/.test(name)) {
const err = new TypeError(`Header name must be a valid HTTP token [${name}]`);
Object.defineProperty(err, 'code', {value: 'ERR_INVALID_HTTP_TOKEN'});
throw err;
}
};
/**
* @param {string} name
*/
const validateHeaderName = name => {
if (!/^[\^`\-\w!#$%&'*+.|~:]+$/.test(name)) {
const err = new TypeError(`Header name must be a valid HTTP token [${name}]`);
Object.defineProperty(err, 'code', {value: 'ERR_INVALID_HTTP_TOKEN'});
throw err;
}
};

const validateHeaderValue = typeof validators.validateHeaderValue === 'function' ?
validators.validateHeaderValue :
/**
* @param {string} name
* @param {string} value
* @param {string} name
* @param {string} value
*/
(name, value) => {
if (/[^\t\u0020-\u007E\u0080-\u00FF]/.test(value)) {
Expand Down Expand Up @@ -166,8 +164,8 @@ export default class Headers extends URLSearchParams {
}

/**
*
* @param {string} name
*
* @param {string} name
*/
get(name) {
const values = this.getAll(name);
Expand All @@ -184,8 +182,8 @@ export default class Headers extends URLSearchParams {
}

/**
* @param {(value: string, key: string, parent: this) => void} callback
* @param {any} thisArg
* @param {(value: string, key: string, parent: this) => void} callback
* @param {any} thisArg
* @returns {void}
*/
forEach(callback, thisArg = undefined) {
Expand Down
13 changes: 13 additions & 0 deletions packages/fetch/test/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,19 @@ describe('Headers', () => {
expect(() => headers.append('', 'ok')).to.throw(TypeError);
});

it('should allow HTTP2 pseudo-headers', () => {
let headers = new Headers({':authority': 'something'});
headers.append(":method", "something else")

const result = [];
for (const pair of headers) {
result.push(pair);
}

expect(result).to.deep.equal([[':authority', 'something'], [':method', 'something else']]);

})

it('should ignore unsupported attributes while reading headers', () => {
const FakeHeader = function () { };
// Prototypes are currently ignored
Expand Down