-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow the usage of push streams (#19)
Push streams are disabled by default, but they can be enabled from now on.
- Loading branch information
Showing
8 changed files
with
140 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,74 @@ | ||
'use strict'; | ||
const ManyKeysMap = require('many-keys-map'); | ||
const {extend: gotExtend} = require('got'); | ||
const http2 = require('../../source'); // Note: using the local version | ||
|
||
class PushAgent extends http2.Agent { | ||
constructor(options) { | ||
super(options); | ||
this.settings.enablePush = true; | ||
|
||
this.on('session', session => { | ||
const pushCache = new ManyKeysMap(); | ||
session.pushCache = pushCache; | ||
|
||
session.on('stream', (stream, requestHeaders) => { | ||
const parsedPushHeaders = PushAgent._parsePushHeaders(requestHeaders); | ||
|
||
if (pushCache.has(parsedPushHeaders)) { | ||
stream.close(http2.constants.NGHTTP2_REFUSED_STREAM); | ||
return; | ||
} | ||
|
||
stream.once('push', pushHeaders => { | ||
pushCache.set(parsedPushHeaders, {stream, pushHeaders}); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
async request(authority, options, headers) { | ||
const session = await this.getSession(authority, options); | ||
|
||
const parsedPushHeaders = PushAgent._parsePushHeaders(headers); | ||
const cache = session.pushCache.get(parsedPushHeaders); | ||
if (cache) { | ||
const {stream, pushHeaders} = cache; | ||
delete session.pushCache.delete(parsedPushHeaders); | ||
|
||
setImmediate(() => { | ||
stream.emit('response', pushHeaders); | ||
}); | ||
|
||
return stream; | ||
} | ||
|
||
const stream = session.request(headers); | ||
|
||
return stream; | ||
} | ||
|
||
static _parsePushHeaders(headers) { | ||
// TODO: headers[':authority'] needs to be verified properly. | ||
|
||
return [ | ||
headers[':path'] || '/', | ||
headers[':method'] || 'GET' | ||
]; | ||
} | ||
} | ||
|
||
(async () => { | ||
const got = gotExtend({ | ||
baseUrl: 'https://localhost:3000', | ||
request: http2.request, | ||
rejectUnauthorized: false, | ||
agent: new PushAgent() | ||
}); | ||
|
||
const response = await got(''); | ||
console.log('/', response.body, response.headers); | ||
|
||
const pushResponse = await got('push'); | ||
console.log('/push', pushResponse.body, pushResponse.headers); | ||
})(); |
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,33 @@ | ||
'use strict'; | ||
const util = require('util'); | ||
const http2 = require('http2'); | ||
const createCert = require('create-cert'); | ||
|
||
const PORT = 3000; | ||
|
||
(async () => { | ||
const {cert, key} = await createCert(); | ||
|
||
const server = http2.createSecureServer({cert, key}); | ||
|
||
server.on('stream', stream => { | ||
stream.respond({':status': 200}); | ||
stream.pushStream({':path': '/push'}, (error, pushStream) => { | ||
if (error) { | ||
throw error; | ||
} | ||
|
||
pushStream.respond({':status': 200}); | ||
pushStream.end('some pushed data'); | ||
}); | ||
|
||
stream.end('some data'); | ||
}); | ||
|
||
server.listen = util.promisify(server.listen); | ||
server.close = util.promisify(server.close); | ||
|
||
await server.listen(PORT); | ||
|
||
console.log(`Server is listening on port ${PORT}`); | ||
})(); |
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