Skip to content

Commit

Permalink
test_runner: use v8.serialize instead of TAP
Browse files Browse the repository at this point in the history
  • Loading branch information
MoLow committed May 8, 2023
1 parent 0b3fcfc commit b0698ed
Show file tree
Hide file tree
Showing 17 changed files with 294 additions and 4,605 deletions.
3 changes: 2 additions & 1 deletion doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -2321,7 +2321,8 @@ on unsupported platforms will not be fixed.
### `NODE_TEST_CONTEXT=value`

If `value` equals `'child'`, test reporter options will be overridden and test
output will be sent to stdout in the TAP format.
output will be sent to stdout in the TAP format. If any other value is provided,
Node.js makes no guarantees about the reporter format used or its stability.

### `NODE_TLS_REJECT_UNAUTHORIZED=value`

Expand Down
52 changes: 45 additions & 7 deletions lib/internal/error_serdes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,24 @@ const {
ObjectGetOwnPropertyNames,
ObjectGetPrototypeOf,
ObjectKeys,
ObjectPrototypeHasOwnProperty,
ObjectPrototypeToString,
RangeError,
ReferenceError,
SafeSet,
SymbolToStringTag,
SyntaxError,
SymbolFor,
TypeError,
URIError,
} = primordials;
const { inspect: { custom: customInspectSymbol } } = require('util');

const kSerializedError = 0;
const kSerializedObject = 1;
const kInspectedError = 2;
const kInspectedSymbol = 3;
const kCustomInspecteObject = 4;

const errors = {
Error, TypeError, RangeError, URIError, SyntaxError, ReferenceError, EvalError,
Expand All @@ -52,7 +57,13 @@ function TryGetAllProperties(object, target = object) {
// Continue regardless of error.
}
}
if ('value' in descriptor && typeof descriptor.value !== 'function') {
if (key === 'cause') {
delete descriptor.get;
delete descriptor.set;
descriptor.value = serializeError(descriptor.value);
all[key] = descriptor;
} else if ('value' in descriptor &&
typeof descriptor.value !== 'function' && typeof descriptor.value !== 'symbol') {
delete descriptor.get;
delete descriptor.set;
all[key] = descriptor;
Expand Down Expand Up @@ -95,6 +106,10 @@ function inspect(...args) {
let serialize;
function serializeError(error) {
if (!serialize) serialize = require('v8').serialize;
if (typeof error === 'symbol') {
return Buffer.concat([Buffer.from([kInspectedSymbol]),
Buffer.from(inspect(error), 'utf8')]);
}
try {
if (typeof error === 'object' &&
ObjectPrototypeToString(error) === '[object Error]') {
Expand All @@ -113,6 +128,15 @@ function serializeError(error) {
} catch {
// Continue regardless of error.
}
try {
if (error != null &&
ObjectPrototypeHasOwnProperty(error, customInspectSymbol)) {
return Buffer.concat([Buffer.from([kCustomInspecteObject]),
Buffer.from(inspect(error), 'utf8')]);
}
} catch {
// Continue regardless of error.
}
try {
const serialized = serialize(error);
return Buffer.concat([Buffer.from([kSerializedObject]), serialized]);
Expand All @@ -123,6 +147,12 @@ function serializeError(error) {
Buffer.from(inspect(error), 'utf8')]);
}

function fromBuffer(error) {
return Buffer.from(error.buffer,
error.byteOffset + 1,
error.byteLength - 1);
}

let deserialize;
function deserializeError(error) {
if (!deserialize) deserialize = require('v8').deserialize;
Expand All @@ -132,19 +162,27 @@ function deserializeError(error) {
const ctor = errors[constructor];
ObjectDefineProperty(properties, SymbolToStringTag, {
__proto__: null,
value: { value: 'Error', configurable: true },
value: { __proto__: null, value: 'Error', configurable: true },
enumerable: true,
});
if ('cause' in properties && 'value' in properties.cause) {
properties.cause.value = deserializeError(properties.cause.value);
}
return ObjectCreate(ctor.prototype, properties);
}
case kSerializedObject:
return deserialize(error.subarray(1));
case kInspectedError: {
const buf = Buffer.from(error.buffer,
error.byteOffset + 1,
error.byteLength - 1);
return buf.toString('utf8');
case kInspectedError:
return fromBuffer(error).toString('utf8');
case kInspectedSymbol: {
const buf = fromBuffer(error);
return SymbolFor(buf.toString('utf8').substring('Symbol('.length, buf.length - 1));
}
case kCustomInspecteObject:
return {
__proto__: null,
[customInspectSymbol]: () => fromBuffer(error).toString('utf8'),
};
}
require('assert').fail('This should not happen');
}
Expand Down
36 changes: 36 additions & 0 deletions lib/internal/test_runner/reporter/v8.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

const { DefaultSerializer } = require('v8');
const { Buffer } = require('buffer');
const { serializeError } = require('internal/error_serdes');


module.exports = async function* v8Reporter(source) {
const serializer = new DefaultSerializer();

for await (const item of source) {
const originalError = item.data.details?.error;
if (originalError) {
item.data.details.error = serializeError(originalError);
}
// Add 4 bytes, to later populate with message length
serializer.writeRawBytes(Buffer.allocUnsafe(4));
serializer.writeHeader();
serializer.writeValue(item);

if (originalError) {
item.data.details.error = originalError;
}

const serializedMessage = serializer.releaseBuffer();
const serializedMessageLength = serializedMessage.length - 4;

serializedMessage.set([
serializedMessageLength >> 24 & 0xFF,
serializedMessageLength >> 16 & 0xFF,
serializedMessageLength >> 8 & 0xFF,
serializedMessageLength & 0xFF,
], 0);
yield serializedMessage;
}
};
Loading

0 comments on commit b0698ed

Please sign in to comment.