Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34.2k
http: add writeEarlyHints function to ServerResponse#44180
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
Merged
nodejs-github-bot merged 6 commits into nodejs:main from wingleung:add-writeEarlyHints-functionAug 17, 2022
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
6 commits Select commit Hold shift + click to select a range
3f31f7b http: add writeEarlyHints function to ServerResponse
35ea4b9 add better regex pattern to match rfc8288 specs
961a8a5 http2: add writeEarlyHints to http2
e9d0be0 Update lib/internal/validators.js
wingleung 82dea4c http: tweak regex
c204024 add writeEarlyHints to http2 docs
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -80,7 +80,8 @@ const{ | ||
| } = codes; | ||
| const{ | ||
| validateInteger, | ||
| validateBoolean | ||
| validateBoolean, | ||
| validateLinkHeaderValue | ||
| } = require('internal/validators'); | ||
| const Buffer = require('buffer').Buffer; | ||
| const{setInterval, clearInterval } = require('timers'); | ||
| @@ -295,6 +296,43 @@ ServerResponse.prototype.writeProcessing = function writeProcessing(cb){ | ||
| this._writeRaw('HTTP/1.1 102 Processing\r\n\r\n', 'ascii', cb); | ||
| }; | ||
| ServerResponse.prototype.writeEarlyHints = function writeEarlyHints(links, cb){ | ||
| let head = 'HTTP/1.1 103 Early Hints\r\n' | ||
| if (typeof links === 'string'){ | ||
wingleung marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| validateLinkHeaderValue(links, 'links'); | ||
| head += 'Link: ' + links + '\r\n' | ||
| } else if (ArrayIsArray(links)){ | ||
| if (!links.length){ | ||
| return; | ||
| } | ||
| head += 'Link: ' | ||
| for (let i = 0; i < links.length; i++){ | ||
| const link = links[i]; | ||
| validateLinkHeaderValue(link, 'links'); | ||
| head += link; | ||
| if (i !== links.length - 1){ | ||
| head += ', ' | ||
| } | ||
| } | ||
| head += '\r\n' | ||
| } else{ | ||
| throw new ERR_INVALID_ARG_VALUE( | ||
| 'links', | ||
| links, | ||
| 'must be an array or string of format "</styles.css> rel=preload; as=style"' | ||
| ); | ||
| } | ||
| head += '\r\n' | ||
| this._writeRaw(head, 'ascii', cb); | ||
| }; | ||
| ServerResponse.prototype._implicitHeader = function _implicitHeader(){ | ||
| this.writeHead(this.statusCode); | ||
| }; | ||
This file contains hidden or 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 hidden or 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
33 changes: 33 additions & 0 deletions 33 test/parallel/test-http-early-hints-invalid-argument-type.js
This file contains hidden or 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 common = require('../common'); | ||
| const assert = require('node:assert'); | ||
| const http = require('node:http'); | ||
| const debug = require('node:util').debuglog('test'); | ||
| const testResBody = 'response content\n' | ||
| const server = http.createServer(common.mustCall((req, res) =>{ | ||
| debug('Server sending early hints...'); | ||
| res.writeEarlyHints({links: 'bad argument object'}); | ||
| debug('Server sending full response...'); | ||
| res.end(testResBody); | ||
| })); | ||
| server.listen(0, common.mustCall(() =>{ | ||
| const req = http.request({ | ||
| port: server.address().port, path: '/' | ||
| }); | ||
| req.end(); | ||
| debug('Client sending request...'); | ||
| req.on('information', common.mustNotCall()); | ||
| process.on('uncaughtException', (err) =>{ | ||
| debug(`Caught an exception: ${JSON.stringify(err)}`); | ||
| if (err.name === 'AssertionError') throw err; | ||
| assert.strictEqual(err.code, 'ERR_INVALID_ARG_VALUE'); | ||
| process.exit(0); | ||
| }); | ||
| })); |
This file contains hidden or 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 common = require('../common'); | ||
| const assert = require('node:assert'); | ||
| const http = require('node:http'); | ||
| const debug = require('node:util').debuglog('test'); | ||
| const testResBody = 'response content\n' | ||
| const server = http.createServer(common.mustCall((req, res) =>{ | ||
| debug('Server sending early hints...'); | ||
| res.writeEarlyHints('bad argument value'); | ||
| debug('Server sending full response...'); | ||
| res.end(testResBody); | ||
| })); | ||
| server.listen(0, common.mustCall(() =>{ | ||
| const req = http.request({ | ||
| port: server.address().port, path: '/' | ||
| }); | ||
| req.end(); | ||
| debug('Client sending request...'); | ||
| req.on('information', common.mustNotCall()); | ||
| process.on('uncaughtException', (err) =>{ | ||
| debug(`Caught an exception: ${JSON.stringify(err)}`); | ||
| if (err.name === 'AssertionError') throw err; | ||
| assert.strictEqual(err.code, 'ERR_INVALID_ARG_VALUE'); | ||
| process.exit(0); | ||
| }); | ||
| })); |
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.