Skip to content

Commit

Permalink
src: add writeEarlyHints function to ServerResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
Wing Leung committed Aug 8, 2022
1 parent f136e7e commit 3c6b924
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
5 changes: 5 additions & 0 deletions doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -2131,6 +2131,11 @@ Sends a HTTP/1.1 100 Continue message to the client, indicating that
the request body should be sent. See the [`'checkContinue'`][] event on
`Server`.

### `response.writeEarlyHints()`

Sends a HTTP/1.1 103 Early Hints message to the client with a Link header,
indicating that the user agent can preload/preconnect the linked resources.

### `response.writeHead(statusCode[, statusMessage][, headers])`

<!-- YAML
Expand Down
14 changes: 14 additions & 0 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,20 @@ 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') {
head += 'Link: ' + links + '\r\n';
} else if (ArrayIsArray(links)) {
head += 'Link: ' + links.join(',') + '\r\n';
}

head += '\r\n';

this._writeRaw(head, 'ascii', cb);
};

ServerResponse.prototype._implicitHeader = function _implicitHeader() {
this.writeHead(this.statusCode);
};
Expand Down
54 changes: 54 additions & 0 deletions test/parallel/test-http-early-hints-multiple-links.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';
require('../common');
const assert = require('assert');
const http = require('http');
const debug = require('util').debuglog('test');

const testResBody = 'response content\n';

const server = http.createServer((req, res) => {
debug('Server sending early hints...');

res.writeEarlyHints([
'</styles.css>; rel=preload; as=style',
'</scripts.js>; rel=preload; as=script',
]);

debug('Server sending full response...');
res.writeHead(200, {
'Content-Type': 'text/plain',
'ABCD': '1'
});

res.end(testResBody);
});

server.listen(0, function() {
const req = http.request({
port: this.address().port,
path: '/'
});

req.end();
debug('Client sending request...');

req.on('information', (res) => {
assert.strictEqual(res.headers.link, '</styles.css>; rel=preload; as=style,</scripts.js>; rel=preload; as=script');
});

req.on('response', function(res) {
let body = '';

assert.strictEqual(
res.statusCode, 200,
`Final status code was ${res.statusCode}, not 200.`
);
res.setEncoding('utf8');
res.on('data', function(chunk) { body += chunk; });
res.on('end', function() {
debug('Got full response.');
assert.strictEqual(body, testResBody);
server.close();
});
});
});
51 changes: 51 additions & 0 deletions test/parallel/test-http-early-hints.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';
require('../common');
const assert = require('assert');
const http = require('http');
const debug = require('util').debuglog('test');

const testResBody = 'response content\n';

const server = http.createServer((req, res) => {
debug('Server sending early hints...');

res.writeEarlyHints('</styles.css>; rel=preload; as=style');

debug('Server sending full response...');
res.writeHead(200, {
'Content-Type': 'text/plain',
'ABCD': '1'
});

res.end(testResBody);
});

server.listen(0, function() {
const req = http.request({
port: this.address().port,
path: '/'
});

req.end();
debug('Client sending request...');

req.on('information', (res) => {
assert.strictEqual(res.headers.link, '</styles.css>; rel=preload; as=style');
});

req.on('response', function(res) {
let body = '';

assert.strictEqual(
res.statusCode, 200,
`Final status code was ${res.statusCode}, not 200.`
);
res.setEncoding('utf8');
res.on('data', function(chunk) { body += chunk; });
res.on('end', function() {
debug('Got full response.');
assert.strictEqual(body, testResBody);
server.close();
});
});
});

0 comments on commit 3c6b924

Please sign in to comment.