Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions lib/v8.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ const {
Serializer,
Deserializer,
} = internalBinding('serdes');
const {
DOMException,
} = internalBinding('messaging');
const {
messaging_clone_symbol,
messaging_deserialize_symbol,
} = internalBinding('symbols');
const {
namespace: startupSnapshot,
} = require('internal/v8/startup_snapshot');
Expand Down Expand Up @@ -381,6 +388,8 @@ function arrayBufferViewIndexToType(index) {
return undefined;
}

const kDOMExceptionHostObjectTag = 14;

class DefaultSerializer extends Serializer {
constructor() {
super();
Expand All @@ -395,6 +404,14 @@ class DefaultSerializer extends Serializer {
* @returns {void}
*/
_writeHostObject(abView) {
// Route DOMException through the same hooks used by structuredClone
if (abView instanceof DOMException) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DOMException is a per-context concept in Node.js, so I don't think instanceof is a reliable brand check (as usual in JS)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to check the symbols instead?

Something like:

const cloneSymbol = abView?.[messaging_clone_symbol];
if (typeof cloneSymbol === 'function') {
// ...call
}

const { data } = abView[messaging_clone_symbol]();
this.writeUint32(kDOMExceptionHostObjectTag);
this.writeValue(data);
return;
}

// Keep track of how to handle different ArrayBufferViews. The default
// Serializer for Node does not use the V8 methods for serializing those
// objects because Node's `Buffer` objects use pooled allocation in many
Expand Down Expand Up @@ -426,6 +443,14 @@ class DefaultDeserializer extends Deserializer {
*/
_readHostObject() {
const typeIndex = this.readUint32();
// Route DOMException through the same hooks used by structuredClone
if (typeIndex === kDOMExceptionHostObjectTag) {
const data = this.readValue();
const domException = new DOMException();
domException[messaging_deserialize_symbol](data);
return domException;
}

const ctor = arrayBufferViewIndexToType(typeIndex);
const byteLength = this.readUint32();
const byteOffset = this._readRawBytes(byteLength);
Expand Down
24 changes: 24 additions & 0 deletions src/node_serdes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class SerializerContext : public BaseObject,

~SerializerContext() override = default;

bool HasCustomHostObject(Isolate* isolate) override { return true; }
Maybe<bool> IsHostObject(Isolate* isolate, Local<Object> object) override;
void ThrowDataCloneError(Local<String> message) override;
Maybe<bool> WriteHostObject(Isolate* isolate, Local<Object> object) override;
Maybe<uint32_t> GetSharedArrayBufferId(
Expand Down Expand Up @@ -145,6 +147,28 @@ Maybe<uint32_t> SerializerContext::GetSharedArrayBufferId(
return id->Uint32Value(env()->context());
}

Maybe<bool> SerializerContext::IsHostObject(Isolate* isolate,
Local<Object> object) {
Local<Object> per_context_bindings;
Local<Value> domexception_ctor_val;
if (!GetPerContextExports(env()->context()).ToLocal(&per_context_bindings) ||
!per_context_bindings
->Get(env()->context(),
FIXED_ONE_BYTE_STRING(env()->isolate(), "DOMException"))
.ToLocal(&domexception_ctor_val) ||
!domexception_ctor_val->IsFunction()) {
return Just(object->InternalFieldCount() != 0);
}

bool is_domexception = false;
if (!object
->InstanceOf(env()->context(), domexception_ctor_val.As<Function>())
.To(&is_domexception)) {
return Nothing<bool>();
}
return Just(is_domexception || object->InternalFieldCount() != 0);
}

Maybe<bool> SerializerContext::WriteHostObject(Isolate* isolate,
Local<Object> input) {
Local<Value> args[1] = { input };
Expand Down
30 changes: 30 additions & 0 deletions test/parallel/test-v8-serdes-domexception.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

require('../common');
const assert = require('node:assert');
const { serialize, deserialize } = require('node:v8');

// Simple case, should preserve message/name/stack like structuredClone
{
const e = AbortSignal.abort().reason;

const cloned = deserialize(serialize(e));

assert(cloned instanceof DOMException);
assert.strictEqual(cloned.name, e.name);
assert.strictEqual(cloned.message, e.message);

assert.strictEqual(typeof cloned.stack, 'string');
assert(cloned.stack.includes(e.name));
}

// Nested case
{
const e = AbortSignal.abort().reason;
const obj = { e };

const clonedObj = deserialize(serialize(obj));
assert(clonedObj.e instanceof DOMException);
assert.strictEqual(clonedObj.e.name, e.name);
assert.strictEqual(clonedObj.e.message, e.message);
}
Loading