| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,8 +10,15 @@ const { | |||
| 10 | 10 | const dc = require('diagnostics_channel'); | |
| 11 | 11 | const { now } = require('internal/perf/utils'); | |
| 12 | 12 | const { MIMEType } = require('internal/mime'); | |
| 13 | + const { | ||
| 14 | + createGunzip, | ||
| 15 | + createInflate, | ||
| 16 | + createBrotliDecompress, | ||
| 17 | + createZstdDecompress, | ||
| 18 | + } = require('zlib'); | ||
| 13 | 19 | ||
| 14 | 20 | const kInspectorRequestId = Symbol('kInspectorRequestId'); | |
| 21 | + const kContentEncoding = Symbol('kContentEncoding'); | ||
| 15 | 22 | ||
| 16 | 23 | // https://chromedevtools.github.io/devtools-protocol/1-3/Network/#type-ResourceType | |
| 17 | 24 | const kResourceType = { | |
@@ -70,6 +77,27 @@ function sniffMimeType(contentType) { | |||
| 70 | 77 | }; | |
| 71 | 78 | } | |
| 72 | 79 | ||
| 80 | + /** | ||
| 81 | + * Creates a decompression stream based on the content encoding. | ||
| 82 | + * @param {string} encoding - The content encoding (e.g., 'gzip', 'deflate', 'br', 'zstd'). | ||
| 83 | + * @returns {import('stream').Transform|null} - A decompression stream or null if encoding is not supported. | ||
| 84 | + */ | ||
| 85 | + function createDecompressor(encoding) { | ||
| 86 | + switch (encoding) { | ||
| 87 | + case 'gzip': | ||
| 88 | + case 'x-gzip': | ||
| 89 | + return createGunzip(); | ||
| 90 | + case 'deflate': | ||
| 91 | + return createInflate(); | ||
| 92 | + case 'br': | ||
| 93 | + return createBrotliDecompress(); | ||
| 94 | + case 'zstd': | ||
| 95 | + return createZstdDecompress(); | ||
| 96 | + default: | ||
| 97 | + return null; | ||
| 98 | + } | ||
| 99 | + } | ||
| 100 | + | ||
| 73 | 101 | function registerDiagnosticChannels(listenerPairs) { | |
| 74 | 102 | function enable() { | |
| 75 | 103 | ArrayPrototypeForEach(listenerPairs, ({ 0: channel, 1: listener }) => { | |
@@ -91,9 +119,11 @@ function registerDiagnosticChannels(listenerPairs) { | |||
| 91 | 119 | ||
| 92 | 120 | module.exports = { | |
| 93 | 121 | kInspectorRequestId, | |
| 122 | + kContentEncoding, | ||
| 94 | 123 | kResourceType, | |
| 95 | 124 | getMonotonicTime, | |
| 96 | 125 | getNextRequestId, | |
| 97 | 126 | registerDiagnosticChannels, | |
| 98 | 127 | sniffMimeType, | |
| 128 | + createDecompressor, | ||
| 99 | 129 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,11 +10,13 @@ const { | |||
| 10 | 10 | ||
| 11 | 11 | const { | |
| 12 | 12 | kInspectorRequestId, | |
| 13 | + kContentEncoding, | ||
| 13 | 14 | kResourceType, | |
| 14 | 15 | getMonotonicTime, | |
| 15 | 16 | getNextRequestId, | |
| 16 | 17 | registerDiagnosticChannels, | |
| 17 | 18 | sniffMimeType, | |
| 19 | + createDecompressor, | ||
| 18 | 20 | } = require('internal/inspector/network'); | |
| 19 | 21 | const { Network } = require('inspector'); | |
| 20 | 22 | const EventEmitter = require('events'); | |
@@ -27,6 +29,7 @@ const convertHeaderObject = (headers = {}) => { | |||
| 27 | 29 | let host; | |
| 28 | 30 | let charset; | |
| 29 | 31 | let mimeType; | |
| 32 | + let contentEncoding; | ||
| 30 | 33 | const dict = {}; | |
| 31 | 34 | for (const { 0: key, 1: value } of ObjectEntries(headers)) { | |
| 32 | 35 | const lowerCasedKey = key.toLowerCase(); | |
@@ -38,6 +41,9 @@ const convertHeaderObject = (headers = {}) => { | |||
| 38 | 41 | charset = result.charset; | |
| 39 | 42 | mimeType = result.mimeType; | |
| 40 | 43 | } | |
| 44 | + if (lowerCasedKey === 'content-encoding') { | ||
| 45 | + contentEncoding = typeof value === 'string' ? value.toLowerCase() : undefined; | ||
| 46 | + } | ||
| 41 | 47 | if (typeof value === 'string') { | |
| 42 | 48 | dict[key] = value; | |
| 43 | 49 | } else if (ArrayIsArray(value)) { | |
@@ -50,7 +56,7 @@ const convertHeaderObject = (headers = {}) => { | |||
| 50 | 56 | dict[key] = String(value); | |
| 51 | 57 | } | |
| 52 | 58 | } | |
| 53 | - return [dict, host, charset, mimeType]; | ||
| 59 | + return [dict, host, charset, mimeType, contentEncoding]; | ||
| 54 | 60 | }; | |
| 55 | 61 | ||
| 56 | 62 | /** | |
@@ -105,7 +111,10 @@ function onClientResponseFinish({ request, response }) { | |||
| 105 | 111 | return; | |
| 106 | 112 | } | |
| 107 | 113 | ||
| 108 | - const { 0: headers, 2: charset, 3: mimeType } = convertHeaderObject(response.headers); | ||
| 114 | + const { 0: headers, 2: charset, 3: mimeType, 4: contentEncoding } = convertHeaderObject(response.headers); | ||
| 115 | + | ||
| 116 | + // Store content encoding on the request for later use | ||
| 117 | + request[kContentEncoding] = contentEncoding; | ||
| 109 | 118 | ||
| 110 | 119 | Network.responseReceived({ | |
| 111 | 120 | requestId: request[kInspectorRequestId], | |
@@ -121,24 +130,64 @@ function onClientResponseFinish({ request, response }) { | |||
| 121 | 130 | }, | |
| 122 | 131 | }); | |
| 123 | 132 | ||
| 124 | - // Unlike response.on('data', ...), this does not put the stream into flowing mode. | ||
| 125 | - EventEmitter.prototype.on.call(response, 'data', (chunk) => { | ||
| 126 | - Network.dataReceived({ | ||
| 127 | - requestId: request[kInspectorRequestId], | ||
| 128 | - timestamp: getMonotonicTime(), | ||
| 129 | - dataLength: chunk.byteLength, | ||
| 130 | - encodedDataLength: chunk.byteLength, | ||
| 131 | - data: chunk, | ||
| 133 | + // Create a decompressor if the response is compressed | ||
| 134 | + const decompressor = createDecompressor(contentEncoding); | ||
| 135 | + | ||
| 136 | + if (decompressor) { | ||
| 137 | + // Pipe decompressed data to DevTools | ||
| 138 | + decompressor.on('data', (decompressedChunk) => { | ||
| 139 | + Network.dataReceived({ | ||
| 140 | + requestId: request[kInspectorRequestId], | ||
| 141 | + timestamp: getMonotonicTime(), | ||
| 142 | + dataLength: decompressedChunk.byteLength, | ||
| 143 | + encodedDataLength: decompressedChunk.byteLength, | ||
| 144 | + data: decompressedChunk, | ||
| 145 | + }); | ||
| 132 | 146 | }); | |
| 133 | - }); | ||
| 134 | 147 | ||
| 135 | - // Wait until the response body is consumed by user code. | ||
| 136 | - response.once('end', () => { | ||
| 137 | - Network.loadingFinished({ | ||
| 138 | - requestId: request[kInspectorRequestId], | ||
| 139 | - timestamp: getMonotonicTime(), | ||
| 148 | + // Handle decompression errors gracefully - fall back to raw data | ||
| 149 | + decompressor.on('error', () => { | ||
| 150 | + // If decompression fails, the raw data has already been sent via the fallback | ||
| 140 | 151 | }); | |
| 141 | - }); | ||
| 152 | + | ||
| 153 | + // Unlike response.on('data', ...), this does not put the stream into flowing mode. | ||
| 154 | + EventEmitter.prototype.on.call(response, 'data', (chunk) => { | ||
| 155 | + // Feed the chunk into the decompressor | ||
| 156 | + decompressor.write(chunk); | ||
| 157 | + }); | ||
| 158 | + | ||
| 159 | + // Wait until the response body is consumed by user code. | ||
| 160 | + response.once('end', () => { | ||
| 161 | + // End the decompressor stream | ||
| 162 | + decompressor.end(); | ||
| 163 | + decompressor.once('end', () => { | ||
| 164 | + Network.loadingFinished({ | ||
| 165 | + requestId: request[kInspectorRequestId], | ||
| 166 | + timestamp: getMonotonicTime(), | ||
| 167 | + }); | ||
| 168 | + }); | ||
| 169 | + }); | ||
| 170 | + } else { | ||
| 171 | + // No decompression needed, send data directly | ||
| 172 | + // Unlike response.on('data', ...), this does not put the stream into flowing mode. | ||
| 173 | + EventEmitter.prototype.on.call(response, 'data', (chunk) => { | ||
| 174 | + Network.dataReceived({ | ||
| 175 | + requestId: request[kInspectorRequestId], | ||
| 176 | + timestamp: getMonotonicTime(), | ||
| 177 | + dataLength: chunk.byteLength, | ||
| 178 | + encodedDataLength: chunk.byteLength, | ||
| 179 | + data: chunk, | ||
| 180 | + }); | ||
| 181 | + }); | ||
| 182 | + | ||
| 183 | + // Wait until the response body is consumed by user code. | ||
| 184 | + response.once('end', () => { | ||
| 185 | + Network.loadingFinished({ | ||
| 186 | + requestId: request[kInspectorRequestId], | ||
| 187 | + timestamp: getMonotonicTime(), | ||
| 188 | + }); | ||
| 189 | + }); | ||
| 190 | + } | ||
| 142 | 191 | } | |
| 143 | 192 | ||
| 144 | 193 | module.exports = registerDiagnosticChannels([ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,15 +10,18 @@ const { | |||
| 10 | 10 | ||
| 11 | 11 | const { | |
| 12 | 12 | kInspectorRequestId, | |
| 13 | + kContentEncoding, | ||
| 13 | 14 | kResourceType, | |
| 14 | 15 | getMonotonicTime, | |
| 15 | 16 | getNextRequestId, | |
| 16 | 17 | registerDiagnosticChannels, | |
| 17 | 18 | sniffMimeType, | |
| 19 | + createDecompressor, | ||
| 18 | 20 | } = require('internal/inspector/network'); | |
| 19 | 21 | const { Network } = require('inspector'); | |
| 20 | 22 | const { | |
| 21 | 23 | HTTP2_HEADER_AUTHORITY, | |
| 24 | + HTTP2_HEADER_CONTENT_ENCODING, | ||
| 22 | 25 | HTTP2_HEADER_CONTENT_TYPE, | |
| 23 | 26 | HTTP2_HEADER_COOKIE, | |
| 24 | 27 | HTTP2_HEADER_METHOD, | |
@@ -42,6 +45,7 @@ function convertHeaderObject(headers = {}) { | |||
| 42 | 45 | let statusCode; | |
| 43 | 46 | let charset; | |
| 44 | 47 | let mimeType; | |
| 48 | + let contentEncoding; | ||
| 45 | 49 | const dict = {}; | |
| 46 | 50 | ||
| 47 | 51 | for (const { 0: key, 1: value } of ObjectEntries(headers)) { | |
@@ -61,6 +65,8 @@ function convertHeaderObject(headers = {}) { | |||
| 61 | 65 | const result = sniffMimeType(value); | |
| 62 | 66 | charset = result.charset; | |
| 63 | 67 | mimeType = result.mimeType; | |
| 68 | + } else if (lowerCasedKey === HTTP2_HEADER_CONTENT_ENCODING) { | ||
| 69 | + contentEncoding = typeof value === 'string' ? value.toLowerCase() : undefined; | ||
| 64 | 70 | } | |
| 65 | 71 | ||
| 66 | 72 | if (typeof value === 'string') { | |
@@ -78,7 +84,7 @@ function convertHeaderObject(headers = {}) { | |||
| 78 | 84 | ||
| 79 | 85 | const url = `${scheme}://${authority}${path}`; | |
| 80 | 86 | ||
| 81 | - return [dict, url, method, statusCode, charset, mimeType]; | ||
| 87 | + return [dict, url, method, statusCode, charset, mimeType, contentEncoding]; | ||
| 82 | 88 | } | |
| 83 | 89 | ||
| 84 | 90 | /** | |
@@ -194,7 +200,16 @@ function onClientStreamFinish({ stream, headers }) { | |||
| 194 | 200 | return; | |
| 195 | 201 | } | |
| 196 | 202 | ||
| 197 | - const { 0: convertedHeaderObject, 3: statusCode, 4: charset, 5: mimeType } = convertHeaderObject(headers); | ||
| 203 | + const { | ||
| 204 | + 0: convertedHeaderObject, | ||
| 205 | + 3: statusCode, | ||
| 206 | + 4: charset, | ||
| 207 | + 5: mimeType, | ||
| 208 | + 6: contentEncoding, | ||
| 209 | + } = convertHeaderObject(headers); | ||
| 210 | + | ||
| 211 | + // Store content encoding on the stream for later use | ||
| 212 | + stream[kContentEncoding] = contentEncoding; | ||
| 198 | 213 | ||
| 199 | 214 | Network.responseReceived({ | |
| 200 | 215 | requestId: stream[kInspectorRequestId], | |
@@ -210,23 +225,56 @@ function onClientStreamFinish({ stream, headers }) { | |||
| 210 | 225 | }, | |
| 211 | 226 | }); | |
| 212 | 227 | ||
| 213 | - // Unlike stream.on('data', ...), this does not put the stream into flowing mode. | ||
| 214 | - EventEmitter.prototype.on.call(stream, 'data', (chunk) => { | ||
| 215 | - /** | ||
| 216 | - * When a chunk of the response body has been received, cache it until `getResponseBody` request | ||
| 217 | - * https://chromedevtools.github.io/devtools-protocol/1-3/Network/#method-getResponseBody or | ||
| 218 | - * stream it with `streamResourceContent` request. | ||
| 219 | - * https://chromedevtools.github.io/devtools-protocol/tot/Network/#method-streamResourceContent | ||
| 220 | - */ | ||
| 221 | - | ||
| 222 | - Network.dataReceived({ | ||
| 223 | - requestId: stream[kInspectorRequestId], | ||
| 224 | - timestamp: getMonotonicTime(), | ||
| 225 | - dataLength: chunk.byteLength, | ||
| 226 | - encodedDataLength: chunk.byteLength, | ||
| 227 | - data: chunk, | ||
| 228 | + // Create a decompressor if the response is compressed | ||
| 229 | + const decompressor = createDecompressor(contentEncoding); | ||
| 230 | + | ||
| 231 | + if (decompressor) { | ||
| 232 | + // Pipe decompressed data to DevTools | ||
| 233 | + decompressor.on('data', (decompressedChunk) => { | ||
| 234 | + Network.dataReceived({ | ||
| 235 | + requestId: stream[kInspectorRequestId], | ||
| 236 | + timestamp: getMonotonicTime(), | ||
| 237 | + dataLength: decompressedChunk.byteLength, | ||
| 238 | + encodedDataLength: decompressedChunk.byteLength, | ||
| 239 | + data: decompressedChunk, | ||
| 240 | + }); | ||
| 228 | 241 | }); | |
| 229 | - }); | ||
| 242 | + | ||
| 243 | + // Handle decompression errors gracefully | ||
| 244 | + decompressor.on('error', () => { | ||
| 245 | + // If decompression fails, the raw data has already been sent via the fallback | ||
| 246 | + }); | ||
| 247 | + | ||
| 248 | + // Unlike stream.on('data', ...), this does not put the stream into flowing mode. | ||
| 249 | + EventEmitter.prototype.on.call(stream, 'data', (chunk) => { | ||
| 250 | + // Feed the chunk into the decompressor | ||
| 251 | + decompressor.write(chunk); | ||
| 252 | + }); | ||
| 253 | + | ||
| 254 | + // End the decompressor when the stream closes | ||
| 255 | + stream.once('end', () => { | ||
| 256 | + decompressor.end(); | ||
| 257 | + }); | ||
| 258 | + } else { | ||
| 259 | + // No decompression needed, send data directly | ||
| 260 | + // Unlike stream.on('data', ...), this does not put the stream into flowing mode. | ||
| 261 | + EventEmitter.prototype.on.call(stream, 'data', (chunk) => { | ||
| 262 | + /** | ||
| 263 | + * When a chunk of the response body has been received, cache it until `getResponseBody` request | ||
| 264 | + * https://chromedevtools.github.io/devtools-protocol/1-3/Network/#method-getResponseBody or | ||
| 265 | + * stream it with `streamResourceContent` request. | ||
| 266 | + * https://chromedevtools.github.io/devtools-protocol/tot/Network/#method-streamResourceContent | ||
| 267 | + */ | ||
| 268 | + | ||
| 269 | + Network.dataReceived({ | ||
| 270 | + requestId: stream[kInspectorRequestId], | ||
| 271 | + timestamp: getMonotonicTime(), | ||
| 272 | + dataLength: chunk.byteLength, | ||
| 273 | + encodedDataLength: chunk.byteLength, | ||
| 274 | + data: chunk, | ||
| 275 | + }); | ||
| 276 | + }); | ||
| 277 | + } | ||
| 230 | 278 | } | |
| 231 | 279 | ||
| 232 | 280 | /** | |
| Back | FazBrowse Home | New Git URL |
0 commit comments