• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const {
4  TypedArrayPrototypeGetLength,
5} = primordials;
6const { DefaultSerializer } = require('v8');
7const { Buffer } = require('buffer');
8const { serializeError } = require('internal/error_serdes');
9
10
11module.exports = async function* v8Reporter(source) {
12  const serializer = new DefaultSerializer();
13  serializer.writeHeader();
14  const headerLength = TypedArrayPrototypeGetLength(serializer.releaseBuffer());
15
16  for await (const item of source) {
17    const originalError = item.data.details?.error;
18    if (originalError) {
19      // Error is overriden with a serialized version, so that it can be
20      // deserialized in the parent process.
21      // Error is restored after serialization.
22      item.data.details.error = serializeError(originalError);
23    }
24    serializer.writeHeader();
25    // Add 4 bytes, to later populate with message length
26    serializer.writeRawBytes(Buffer.allocUnsafe(4));
27    serializer.writeHeader();
28    serializer.writeValue(item);
29
30    if (originalError) {
31      item.data.details.error = originalError;
32    }
33
34    const serializedMessage = serializer.releaseBuffer();
35    const serializedMessageLength = serializedMessage.length - (4 + headerLength);
36
37    serializedMessage.set([
38      serializedMessageLength >> 24 & 0xFF,
39      serializedMessageLength >> 16 & 0xFF,
40      serializedMessageLength >> 8 & 0xFF,
41      serializedMessageLength & 0xFF,
42    ], headerLength);
43    yield serializedMessage;
44  }
45};
46