| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,12 +1,17 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | + const { | ||
| 4 | + TypedArrayPrototypeGetLength, | ||
| 5 | + } = primordials; | ||
| 3 | 6 | const { DefaultSerializer } = require('v8'); | |
| 4 | 7 | const { Buffer } = require('buffer'); | |
| 5 | 8 | const { serializeError } = require('internal/error_serdes'); | |
| 6 | 9 | ||
| 7 | 10 | ||
| 8 | 11 | module.exports = async function* v8Reporter(source) { | |
| 9 | 12 | const serializer = new DefaultSerializer(); | |
| 13 | + serializer.writeHeader(); | ||
| 14 | + const headerLength = TypedArrayPrototypeGetLength(serializer.releaseBuffer()); | ||
| 10 | 15 | ||
| 11 | 16 | for await (const item of source) { | |
| 12 | 17 | const originalError = item.data.details?.error; | |
@@ -16,6 +21,7 @@ module.exports = async function* v8Reporter(source) { | |||
| 16 | 21 | // Error is restored after serialization. | |
| 17 | 22 | item.data.details.error = serializeError(originalError); | |
| 18 | 23 | } | |
| 24 | + serializer.writeHeader(); | ||
| 19 | 25 | // Add 4 bytes, to later populate with message length | |
| 20 | 26 | serializer.writeRawBytes(Buffer.allocUnsafe(4)); | |
| 21 | 27 | serializer.writeHeader(); | |
@@ -26,14 +32,14 @@ module.exports = async function* v8Reporter(source) { | |||
| 26 | 32 | } | |
| 27 | 33 | ||
| 28 | 34 | const serializedMessage = serializer.releaseBuffer(); | |
| 29 | - const serializedMessageLength = serializedMessage.length - 4; | ||
| 35 | + const serializedMessageLength = serializedMessage.length - (4 + headerLength); | ||
| 30 | 36 | ||
| 31 | 37 | serializedMessage.set([ | |
| 32 | 38 | serializedMessageLength >> 24 & 0xFF, | |
| 33 | 39 | serializedMessageLength >> 16 & 0xFF, | |
| 34 | 40 | serializedMessageLength >> 8 & 0xFF, | |
| 35 | 41 | serializedMessageLength & 0xFF, | |
| 36 | - ], 0); | ||
| 42 | + ], headerLength); | ||
| 37 | 43 | yield serializedMessage; | |
| 38 | 44 | } | |
| 39 | 45 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -163,7 +163,8 @@ function getRunArgs({ path, inspectPort, testNamePatterns }) { | |||
| 163 | 163 | const serializer = new DefaultSerializer(); | |
| 164 | 164 | serializer.writeHeader(); | |
| 165 | 165 | const v8Header = serializer.releaseBuffer(); | |
| 166 | - const kSerializedSizeHeader = 4; | ||
| 166 | + const kV8HeaderLength = TypedArrayPrototypeGetLength(v8Header); | ||
| 167 | + const kSerializedSizeHeader = 4 + kV8HeaderLength; | ||
| 167 | 168 | ||
| 168 | 169 | class FileTest extends Test { | |
| 169 | 170 | // This class maintains two buffers: | |
@@ -236,22 +237,42 @@ class FileTest extends Test { | |||
| 236 | 237 | this.#handleReportItem(item); | |
| 237 | 238 | } | |
| 238 | 239 | reportStarted() {} | |
| 239 | - report() { | ||
| 240 | + drain() { | ||
| 240 | 241 | this.#drainRawBuffer(); | |
| 241 | 242 | this.#drainReportBuffer(); | |
| 243 | + } | ||
| 244 | + report() { | ||
| 245 | + this.drain(); | ||
| 242 | 246 | const skipReporting = this.#skipReporting(); | |
| 243 | 247 | if (!skipReporting) { | |
| 244 | 248 | super.reportStarted(); | |
| 245 | 249 | super.report(); | |
| 246 | 250 | } | |
| 247 | 251 | } | |
| 248 | 252 | parseMessage(readData) { | |
| 249 | - const dataLength = TypedArrayPrototypeGetLength(readData); | ||
| 253 | + let dataLength = TypedArrayPrototypeGetLength(readData); | ||
| 250 | 254 | if (dataLength === 0) return; | |
| 255 | + const partialV8Header = readData[dataLength - 1] === v8Header[0]; | ||
| 256 | + | ||
| 257 | + if (partialV8Header) { | ||
| 258 | + // This will break if v8Header length (2 bytes) is changed. | ||
| 259 | + // However it is covered by tests. | ||
| 260 | + readData = TypedArrayPrototypeSubarray(readData, 0, dataLength - 1); | ||
| 261 | + dataLength--; | ||
| 262 | + } | ||
| 251 | 263 | ||
| 252 | - ArrayPrototypePush(this.#rawBuffer, readData); | ||
| 264 | + if (this.#rawBuffer[0] && TypedArrayPrototypeGetLength(this.#rawBuffer[0]) < kSerializedSizeHeader) { | ||
| 265 | + this.#rawBuffer[0] = Buffer.concat([this.#rawBuffer[0], readData]); | ||
| 266 | + } else { | ||
| 267 | + ArrayPrototypePush(this.#rawBuffer, readData); | ||
| 268 | + } | ||
| 253 | 269 | this.#rawBufferSize += dataLength; | |
| 254 | 270 | this.#proccessRawBuffer(); | |
| 271 | + | ||
| 272 | + if (partialV8Header) { | ||
| 273 | + ArrayPrototypePush(this.#rawBuffer, TypedArrayPrototypeSubarray(v8Header, 0, 1)); | ||
| 274 | + this.#rawBufferSize++; | ||
| 275 | + } | ||
| 255 | 276 | } | |
| 256 | 277 | #drainRawBuffer() { | |
| 257 | 278 | while (this.#rawBuffer.length > 0) { | |
@@ -264,16 +285,16 @@ class FileTest extends Test { | |||
| 264 | 285 | let headerIndex = bufferHead.indexOf(v8Header); | |
| 265 | 286 | let nonSerialized = Buffer.alloc(0); | |
| 266 | 287 | ||
| 267 | - while (bufferHead && headerIndex !== kSerializedSizeHeader) { | ||
| 288 | + while (bufferHead && headerIndex !== 0) { | ||
| 268 | 289 | const nonSerializedData = headerIndex === -1 ? | |
| 269 | 290 | bufferHead : | |
| 270 | - bufferHead.slice(0, headerIndex - kSerializedSizeHeader); | ||
| 291 | + bufferHead.slice(0, headerIndex); | ||
| 271 | 292 | nonSerialized = Buffer.concat([nonSerialized, nonSerializedData]); | |
| 272 | 293 | this.#rawBufferSize -= TypedArrayPrototypeGetLength(nonSerializedData); | |
| 273 | 294 | if (headerIndex === -1) { | |
| 274 | 295 | ArrayPrototypeShift(this.#rawBuffer); | |
| 275 | 296 | } else { | |
| 276 | - this.#rawBuffer[0] = bufferHead.subarray(headerIndex - kSerializedSizeHeader); | ||
| 297 | + this.#rawBuffer[0] = TypedArrayPrototypeSubarray(bufferHead, headerIndex); | ||
| 277 | 298 | } | |
| 278 | 299 | bufferHead = this.#rawBuffer[0]; | |
| 279 | 300 | headerIndex = bufferHead?.indexOf(v8Header); | |
@@ -295,10 +316,10 @@ class FileTest extends Test { | |||
| 295 | 316 | // We call `readUInt32BE` manually here, because this is faster than first converting | |
| 296 | 317 | // it to a buffer and using `readUInt32BE` on that. | |
| 297 | 318 | const fullMessageSize = ( | |
| 298 | - bufferHead[0] << 24 | | ||
| 299 | - bufferHead[1] << 16 | | ||
| 300 | - bufferHead[2] << 8 | | ||
| 301 | - bufferHead[3] | ||
| 319 | + bufferHead[kV8HeaderLength] << 24 | | ||
| 320 | + bufferHead[kV8HeaderLength + 1] << 16 | | ||
| 321 | + bufferHead[kV8HeaderLength + 2] << 8 | | ||
| 322 | + bufferHead[kV8HeaderLength + 3] | ||
| 302 | 323 | ) + kSerializedSizeHeader; | |
| 303 | 324 | ||
| 304 | 325 | if (this.#rawBufferSize < fullMessageSize) break; | |
@@ -474,4 +495,7 @@ function run(options) { | |||
| 474 | 495 | return root.reporter; | |
| 475 | 496 | } | |
| 476 | 497 | ||
| 477 | - module.exports = { run }; | ||
| 498 | + module.exports = { | ||
| 499 | + FileTest, // Exported for tests only | ||
| 500 | + run, | ||
| 501 | + }; | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,103 @@ | |||
| 1 | + // Flags: --expose-internals --no-warnings | ||
| 2 | + | ||
| 3 | + import '../common/index.mjs'; | ||
| 4 | + import { describe, it, beforeEach } from 'node:test'; | ||
| 5 | + import assert from 'node:assert'; | ||
| 6 | + import { finished } from 'node:stream/promises'; | ||
| 7 | + import { DefaultSerializer } from 'node:v8'; | ||
| 8 | + import serializer from 'internal/test_runner/reporter/v8-serializer'; | ||
| 9 | + import runner from 'internal/test_runner/runner'; | ||
| 10 | + | ||
| 11 | + async function toArray(chunks) { | ||
| 12 | + const arr = []; | ||
| 13 | + for await (const i of chunks) arr.push(i); | ||
| 14 | + return arr; | ||
| 15 | + } | ||
| 16 | + | ||
| 17 | + const chunks = await toArray(serializer([ | ||
| 18 | + { type: 'test:diagnostic', data: { nesting: 0, details: {}, message: 'diagnostic' } }, | ||
| 19 | + ])); | ||
| 20 | + const defaultSerializer = new DefaultSerializer(); | ||
| 21 | + defaultSerializer.writeHeader(); | ||
| 22 | + const headerLength = defaultSerializer.releaseBuffer().length; | ||
| 23 | + | ||
| 24 | + describe('v8 deserializer', () => { | ||
| 25 | + let fileTest; | ||
| 26 | + let reported; | ||
| 27 | + beforeEach(() => { | ||
| 28 | + reported = []; | ||
| 29 | + fileTest = new runner.FileTest({ name: 'filetest' }); | ||
| 30 | + fileTest.reporter.on('data', (data) => reported.push(data)); | ||
| 31 | + assert(fileTest.isClearToSend()); | ||
| 32 | + }); | ||
| 33 | + | ||
| 34 | + async function collectReported(chunks) { | ||
| 35 | + chunks.forEach((chunk) => fileTest.parseMessage(chunk)); | ||
| 36 | + fileTest.drain(); | ||
| 37 | + fileTest.reporter.end(); | ||
| 38 | + await finished(fileTest.reporter); | ||
| 39 | + return reported; | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + it('should do nothing when no chunks', async () => { | ||
| 43 | + const reported = await collectReported([]); | ||
| 44 | + assert.deepStrictEqual(reported, []); | ||
| 45 | + }); | ||
| 46 | + | ||
| 47 | + it('should deserialize a chunk with no serialization', async () => { | ||
| 48 | + const reported = await collectReported([Buffer.from('unknown')]); | ||
| 49 | + assert.deepStrictEqual(reported, [ | ||
| 50 | + { data: { __proto__: null, file: 'filetest', message: 'unknown' }, type: 'test:stdout' }, | ||
| 51 | + ]); | ||
| 52 | + }); | ||
| 53 | + | ||
| 54 | + it('should deserialize a serialized chunk', async () => { | ||
| 55 | + const reported = await collectReported(chunks); | ||
| 56 | + assert.deepStrictEqual(reported, [ | ||
| 57 | + { data: { nesting: 0, details: {}, message: 'diagnostic' }, type: 'test:diagnostic' }, | ||
| 58 | + ]); | ||
| 59 | + }); | ||
| 60 | + | ||
| 61 | + it('should deserialize a serialized chunk after non-serialized chunk', async () => { | ||
| 62 | + const reported = await collectReported([Buffer.concat([Buffer.from('unknown'), ...chunks])]); | ||
| 63 | + assert.deepStrictEqual(reported, [ | ||
| 64 | + { data: { __proto__: null, file: 'filetest', message: 'unknown' }, type: 'test:stdout' }, | ||
| 65 | + { data: { nesting: 0, details: {}, message: 'diagnostic' }, type: 'test:diagnostic' }, | ||
| 66 | + ]); | ||
| 67 | + }); | ||
| 68 | + | ||
| 69 | + it('should deserialize a serialized chunk before non-serialized output', async () => { | ||
| 70 | + const reported = await collectReported([Buffer.concat([ ...chunks, Buffer.from('unknown')])]); | ||
| 71 | + assert.deepStrictEqual(reported, [ | ||
| 72 | + { data: { nesting: 0, details: {}, message: 'diagnostic' }, type: 'test:diagnostic' }, | ||
| 73 | + { data: { __proto__: null, file: 'filetest', message: 'unknown' }, type: 'test:stdout' }, | ||
| 74 | + ]); | ||
| 75 | + }); | ||
| 76 | + | ||
| 77 | + const headerPosition = headerLength * 2 + 4; | ||
| 78 | + for (let i = 0; i < headerPosition + 5; i++) { | ||
| 79 | + const message = `should deserialize a serialized message split into two chunks {...${i},${i + 1}...}`; | ||
| 80 | + it(message, async () => { | ||
| 81 | + const data = chunks[0]; | ||
| 82 | + const reported = await collectReported([data.subarray(0, i), data.subarray(i)]); | ||
| 83 | + assert.deepStrictEqual(reported, [ | ||
| 84 | + { data: { nesting: 0, details: {}, message: 'diagnostic' }, type: 'test:diagnostic' }, | ||
| 85 | + ]); | ||
| 86 | + }); | ||
| 87 | + | ||
| 88 | + it(`${message} wrapped by non-serialized data`, async () => { | ||
| 89 | + const data = chunks[0]; | ||
| 90 | + const reported = await collectReported([ | ||
| 91 | + Buffer.concat([Buffer.from('unknown'), data.subarray(0, i)]), | ||
| 92 | + Buffer.concat([data.subarray(i), Buffer.from('unknown')]), | ||
| 93 | + ]); | ||
| 94 | + assert.deepStrictEqual(reported, [ | ||
| 95 | + { data: { __proto__: null, file: 'filetest', message: 'unknown' }, type: 'test:stdout' }, | ||
| 96 | + { data: { nesting: 0, details: {}, message: 'diagnostic' }, type: 'test:diagnostic' }, | ||
| 97 | + { data: { __proto__: null, file: 'filetest', message: 'unknown' }, type: 'test:stdout' }, | ||
| 98 | + ]); | ||
| 99 | + } | ||
| 100 | + ); | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments