-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: custom ALPN - better header generation and proxy handling (#20)
* Feature: custom ALPN - better header generation and proxy handling * chore: Bumped version. Updated changelog.
- Loading branch information
Showing
11 changed files
with
117 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const httpResolver = require('../http-resolver'); | ||
|
||
/** | ||
* @param {object} options | ||
* @param {function} next | ||
* @returns {import('got').GotReturn} | ||
*/ | ||
async function alpnHandler(options, next) { | ||
const { url, http2 } = options; | ||
|
||
if (http2) { | ||
const parsedUrl = new URL(url); | ||
|
||
if (parsedUrl.protocol === 'https:') { | ||
const protocol = await httpResolver.resolveHttpVersion(parsedUrl); | ||
|
||
options.http2 = protocol === 'h2'; | ||
} else { | ||
// http2 is https | ||
options.http2 = false; | ||
} | ||
} | ||
|
||
return next(options); | ||
} | ||
|
||
module.exports = { | ||
alpnHandler, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const { alpnHandler } = require('../src/handlers/alpn'); | ||
const httpResolver = require('../src/http-resolver'); | ||
|
||
describe('ALPN', () => { | ||
let next; | ||
let options; | ||
|
||
beforeEach(() => { | ||
options = { | ||
context: {}, | ||
https: {}, | ||
}; | ||
next = () => { }; | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('should start alpn only if http2 and https', async () => { | ||
options.url = 'https://test.com'; | ||
options.http2 = true; | ||
jest.spyOn(httpResolver, 'resolveHttpVersion').mockResolvedValue('h2'); | ||
|
||
await alpnHandler(options, next); | ||
expect(options.http2).toBe(true); | ||
|
||
jest.spyOn(httpResolver, 'resolveHttpVersion').mockResolvedValue('http/1.1'); | ||
|
||
await alpnHandler(options, next); | ||
expect(options.http2).toBe(false); | ||
}); | ||
|
||
test('should skip alpn and assume http/1.1 if not https', async () => { | ||
options.url = 'http://test.com'; | ||
options.http2 = true; | ||
jest.spyOn(httpResolver, 'resolveHttpVersion'); | ||
|
||
await alpnHandler(options, next); | ||
expect(httpResolver.resolveHttpVersion).toBeCalledTimes(0); | ||
expect(options.http2).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters