The http2.getPackedSettings() method in Node.js converts an HTTP/2 settings object into a packed Buffer that can be used with supported HTTP/2 APIs.
- It packs an HTTP/2 settings object into a
Buffer. - It follows the HTTP/2 settings format.
- It is provided by the
node:http2module. - It is useful when working with HTTP/2 settings data directly.
Syntax:
http2.getPackedSettings(settings)Where:
http2:The built-in Node.js HTTP/2 module.getPackedSettings():Packs an HTTP/2 settings object into aBuffer.settings:An object containing one or more HTTP/2 settings to be packed.
Return Value: The method returns a Buffer containing the packed binary representation of the provided HTTP/2 settings object.
Example 1:
const http2 = require('http2');
const fs = require('fs');
// Private key and public certificate for access
const options = {
key: fs.readFileSync('private-key.pem'),
cert: fs.readFileSync('public-cert.pem'),
};
// Creating and initializing server
// by using http2.createServer() method
const server = http2.createServer(options);
// Getting packed setting
// by using getPackedSettings() method
const value = http2.getPackedSettings(
{ enablePush: false })
console.log("packed setting :- "
+ value.toString('base64'));
server.on('stream', (stream, requestHeaders) => {
stream.respond({ ':status': 200, 'content-type':
'text/plain' });
stream.write('hello ');
stream.end('world');
// Stopping the server
// by using the close() method
server.close(() => {
console.log("server closed");
})
});
server.listen(8000);
// Creating and initializing client
// by using tls.connect() method
const client = http2.connect('http://localhost:8000');
const req = client.request({
':method': 'GET', ':path': '/' });
req.on('response', (responseHeaders) => {
console.log("status : "
+ responseHeaders[":status"]);
});
req.on('data', (data) => {
console.log('Received: %s ',
data.toString().replace(/(\n)/gm,""));
});
req.on('end', () => {
client.close(() => {
console.log("client closed");
})
});
Output:
packed setting :- AAIAAAAA
status : 200
Received: hello
Received: world
client closed
server closed
Example 2: Filename: index.js
const http2 = require('http2');
const fs = require('fs');
// Private key and public certificate for access
const options = {
key: fs.readFileSync('private-key.pem'),
cert: fs.readFileSync('public-cert.pem'),
};
// Creating and initializing server
// by using http2.createServer() method
const server = http2.createServer(options);
// Getting packed setting
// by using getPackedSettings() method
const value = http2.getPackedSettings(
{ enablePush: true })
console.log("packed setting :- "
+ value.toString('base64'));
server.on('stream', (stream, requestHeaders) => {
stream.end('world');
// Stopping the server
// by using the close() method
server.close(() => {
console.log("server closed");
})
});
server.listen(8000);
// Creating and initializing client
// by using tls.connect() method
const client = http2.connect('http://localhost:8000');
const req = client.request({
':method': 'GET', ':path': '/' });
req.on('data', (data) => {
console.log('Received: %s ',
data.toString().replace(/(\n)/gm,""));
});
req.on('end', () => {
client.close(() => {
console.log("client closed");
})
});
Output:
packed setting :- AAIAAAAB
Received: world
client closed
server closed
Use Cases of http2.getPackedSettings() Method
- Packing HTTP/2 settings before sending them through supported APIs.
- Encoding HTTP/2 settings into the required binary format.
- Working with low-level HTTP/2 protocol operations.
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http2.html#http2_http2_getpackedsettings_settings