Title: doc: add missing `zstd` to mjs example of zlib by deokjinkim · Pull Request #60915 · nodejs/node · GitHub
Open Graph Title: doc: add missing `zstd` to mjs example of zlib by deokjinkim · Pull Request #60915 · nodejs/node
X Title: doc: add missing `zstd` to mjs example of zlib by deokjinkim · Pull Request #60915 · nodejs/node
Description: For reference, cjs example already has zstd as compression encoding. Client request example (1) mjs node/doc/api/zlib.md Lines 237 to 273 in 6f7f51b // Client request example import fs from 'node:fs'; import zlib from 'node:zlib'; import http from 'node:http'; import process from 'node:process'; import { pipeline } from 'node:stream'; const request = http.get({ host: 'example.com', path: '/', port: 80, headers: { 'Accept-Encoding': 'br,gzip,deflate' } }); request.on('response', (response) => { const output = fs.createWriteStream('example.com_index.html'); const onError = (err) => { if (err) { console.error('An error occurred:', err); process.exitCode = 1; } }; switch (response.headers['content-encoding']) { case 'br': pipeline(response, zlib.createBrotliDecompress(), output, onError); break; // Or, just use zlib.createUnzip() to handle both of the following cases: case 'gzip': pipeline(response, zlib.createGunzip(), output, onError); break; case 'deflate': pipeline(response, zlib.createInflate(), output, onError); break; default: pipeline(response, output, onError); break; } }); (2) cjs node/doc/api/zlib.md Lines 277 to 314 in 6f7f51b // Client request example const zlib = require('node:zlib'); const http = require('node:http'); const fs = require('node:fs'); const { pipeline } = require('node:stream'); const request = http.get({ host: 'example.com', path: '/', port: 80, headers: { 'Accept-Encoding': 'br,gzip,deflate,zstd' } }); request.on('response', (response) => { const output = fs.createWriteStream('example.com_index.html'); const onError = (err) => { if (err) { console.error('An error occurred:', err); process.exitCode = 1; } }; switch (response.headers['content-encoding']) { case 'br': pipeline(response, zlib.createBrotliDecompress(), output, onError); break; // Or, just use zlib.createUnzip() to handle both of the following cases: case 'gzip': pipeline(response, zlib.createGunzip(), output, onError); break; case 'deflate': pipeline(response, zlib.createInflate(), output, onError); break; case 'zstd': pipeline(response, zlib.createZstdDecompress(), output, onError); break; default: pipeline(response, output, onError); break; } Server example (1) mjs node/doc/api/zlib.md Lines 319 to 360 in 6f7f51b // server example // Running a gzip operation on every request is quite expensive. // It would be much more efficient to cache the compressed buffer. import zlib from 'node:zlib'; import http from 'node:http'; import fs from 'node:fs'; import { pipeline } from 'node:stream'; http.createServer((request, response) => { const raw = fs.createReadStream('index.html'); // Store both a compressed and an uncompressed version of the resource. response.setHeader('Vary', 'Accept-Encoding'); const acceptEncoding = request.headers['accept-encoding'] || ''; const onError = (err) => { if (err) { // If an error occurs, there's not much we can do because // the server has already sent the 200 response code and // some amount of data has already been sent to the client. // The best we can do is terminate the response immediately // and log the error. response.end(); console.error('An error occurred:', err); } }; // Note: This is not a conformant accept-encoding parser. // See https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3 if (/\bdeflate\b/.test(acceptEncoding)) { response.writeHead(200, { 'Content-Encoding': 'deflate' }); pipeline(raw, zlib.createDeflate(), response, onError); } else if (/\bgzip\b/.test(acceptEncoding)) { response.writeHead(200, { 'Content-Encoding': 'gzip' }); pipeline(raw, zlib.createGzip(), response, onError); } else if (/\bbr\b/.test(acceptEncoding)) { response.writeHead(200, { 'Content-Encoding': 'br' }); pipeline(raw, zlib.createBrotliCompress(), response, onError); } else { response.writeHead(200, {}); pipeline(raw, response, onError); } }).listen(1337); (2) cjs node/doc/api/zlib.md Lines 364 to 408 in 6f7f51b // server example // Running a gzip operation on every request is quite expensive. // It would be much more efficient to cache the compressed buffer. const zlib = require('node:zlib'); const http = require('node:http'); const fs = require('node:fs'); const { pipeline } = require('node:stream'); http.createServer((request, response) => { const raw = fs.createReadStream('index.html'); // Store both a compressed and an uncompressed version of the resource. response.setHeader('Vary', 'Accept-Encoding'); const acceptEncoding = request.headers['accept-encoding'] || ''; const onError = (err) => { if (err) { // If an error occurs, there's not much we can do because // the server has already sent the 200 response code and // some amount of data has already been sent to the client. // The best we can do is terminate the response immediately // and log the error. response.end(); console.error('An error occurred:', err); } }; // Note: This is not a conformant accept-encoding parser. // See https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3 if (/\bdeflate\b/.test(acceptEncoding)) { response.writeHead(200, { 'Content-Encoding': 'deflate' }); pipeline(raw, zlib.createDeflate(), response, onError); } else if (/\bgzip\b/.test(acceptEncoding)) { response.writeHead(200, { 'Content-Encoding': 'gzip' }); pipeline(raw, zlib.createGzip(), response, onError); } else if (/\bbr\b/.test(acceptEncoding)) { response.writeHead(200, { 'Content-Encoding': 'br' }); pipeline(raw, zlib.createBrotliCompress(), response, onError); } else if (/\bzstd\b/.test(acceptEncoding)) { response.writeHead(200, { 'Content-Encoding': 'zstd' }); pipeline(raw, zlib.createZstdCompress(), response, onError); } else { response.writeHead(200, {}); pipeline(raw, response, onError); } }).listen(1337); Refs: #52100
Open Graph Description: For reference, cjs example already has zstd as compression encoding. Client request example (1) mjs node/doc/api/zlib.md Lines 237 to 273 in 6f7f51b ...
X Description: For reference, cjs example already has zstd as compression encoding. Client request example (1) mjs node/doc/api/zlib.md Lines 237 to 273 in 6f7f51b ...
Opengraph URL: https://github.com/nodejs/node/pull/60915
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:60d8ad72-01bc-c85c-d6b8-8cd518ea72e4 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | A598:19F979:BE1738:104A552:6A4C932B |
| html-safe-nonce | 0f654e30dfbd8b278649f777dc369ee46f9d2a285aa4549bcc93ff003e7063cb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNTk4OjE5Rjk3OTpCRTE3Mzg6MTA0QTU1Mjo2QTRDOTMyQiIsInZpc2l0b3JfaWQiOiI2MDYwMDk1NzEyNzk5Nzg5ODY3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 8e3379fda79649095c1247d07dcb839e1edf80bce1e414a1111b633ca49720bb |
| hovercard-subject-tag | pull_request:3057302959 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/nodejs/node/pull/60915/files |
| twitter:image | https://avatars.githubusercontent.com/u/5592478?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/5592478?s=400&v=4 |
| og:image:alt | For reference, cjs example already has zstd as compression encoding. Client request example (1) mjs node/doc/api/zlib.md Lines 237 to 273 in 6f7f51b ... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 3d11bb817438277de2a940854450e83a7d32b6aeb5014e9e6b00a6423900251c |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/nodejs/node git https://github.com/nodejs/node.git |
| octolytics-dimension-user_id | 9950313 |
| octolytics-dimension-user_login | nodejs |
| octolytics-dimension-repository_id | 27193779 |
| octolytics-dimension-repository_nwo | nodejs/node |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 27193779 |
| octolytics-dimension-repository_network_root_nwo | nodejs/node |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 7a2dede45637df6b92ddcfe2a557e67d4b5854ae |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width