-
-
Notifications
You must be signed in to change notification settings - Fork 957
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #1751
- Loading branch information
Showing
6 changed files
with
80 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import http2 from 'http2-wrapper'; | ||
import got from 'got'; | ||
|
||
let sessions = {}; | ||
const getSession = ({origin}) => { | ||
if (sessions[origin] && !sessions[origin].destroyed) { | ||
return sessions[origin]; | ||
} | ||
|
||
const session = http2.connect(origin); | ||
session.once('error', () => { | ||
delete sessions[origin]; | ||
}); | ||
|
||
sessions[origin] = session; | ||
|
||
return session; | ||
}; | ||
|
||
const closeSessions = () => { | ||
for (const key in sessions) { | ||
sessions[key].close(); | ||
} | ||
|
||
sessions = {}; | ||
}; | ||
|
||
const instance = got.extend({ | ||
hooks: { | ||
beforeRequest: [ | ||
options => { | ||
options.h2session = getSession(options.url); | ||
options.http2 = true; | ||
options.request = http2.request; | ||
} | ||
] | ||
} | ||
}); | ||
|
||
const server = http2.createServer((request, response) => { | ||
response.end('{}'); | ||
}); | ||
|
||
server.listen(async () => { | ||
const url = `http://localhost:${server.address().port}`; | ||
const {body, headers} = await instance(url, {context: {h2c: true}}); | ||
console.log(headers, body); | ||
|
||
closeSessions(); | ||
server.close(); | ||
}); |
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