Skip to content

Commit

Permalink
fix: regression in serialization (#2928)
Browse files Browse the repository at this point in the history
Deeply nested nodes do not include values in CDP, thus, Object.hasOwn
throws an error because only objects are allowed as the first arg.

Drive-by: improve types
Caused by: #2622
Addresses: puppeteer/puppeteer#13433
WPT test: https://github.com/web-platform-tests/wpt/pull/49821/files
  • Loading branch information
OrKoN authored Jan 2, 2025
1 parent f56b2a5 commit 84f9983
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
15 changes: 9 additions & 6 deletions src/bidiMapper/modules/script/Realm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,26 @@ export abstract class Realm {
internalIdMap.set(weakLocalObjectReference, uuidv4());
}

(deepSerializedValue as any).internalId = internalIdMap.get(
weakLocalObjectReference,
);
(
deepSerializedValue as Protocol.Runtime.DeepSerializedValue & {
internalId?: string;
}
).internalId = internalIdMap.get(weakLocalObjectReference);
delete deepSerializedValue['weakLocalObjectReference'];
}

if (
(deepSerializedValue as any).type === 'node' &&
Object.hasOwn(deepSerializedValue?.value, 'frameId')
deepSerializedValue.type === 'node' &&
deepSerializedValue.value &&
Object.hasOwn(deepSerializedValue.value, 'frameId')
) {
// `frameId` is not needed in BiDi as it is not yet specified.
delete deepSerializedValue.value['frameId'];
}

// Platform object is a special case. It should have only `{type: object}`
// without `value` field.
if ((deepSerializedValue as any).type === 'platformobject') {
if ((deepSerializedValue.type as string) === 'platformobject') {
return {type: 'object'};
}

Expand Down
42 changes: 27 additions & 15 deletions tests/script/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
import pytest
from anys import ANY_STR
from test_helpers import (ANY_SHARED_ID, ANY_UUID, execute_command, goto_url,
read_JSON_message, send_JSON_command, subscribe)
read_JSON_message, send_JSON_command,
stabilize_key_values, subscribe)


def _strip_handle(obj):
Expand Down Expand Up @@ -394,8 +395,8 @@ async def test_serialization_internal_id(websocket, context_id):
"method": "script.evaluate",
"params": {
"expression": "(()=>{"
" const foo={a: []};"
" const bar=[1,2];"
" const foo={a: [], document};"
" const bar=[1,2, document];"
" const result={1: foo, 2: foo, 3: bar, 4: bar};"
" result.self=result;"
" return result;"
Expand All @@ -408,26 +409,35 @@ async def test_serialization_internal_id(websocket, context_id):
}
})

internal_id_1 = result["result"]["internalId"]
internal_id_2 = result["result"]["value"][0][1]["internalId"]
internal_id_3 = result["result"]["value"][2][1]["internalId"]
stabilize_key_values(result, ["internalId", "sharedId"])

assert result["result"] == {
"type": "object",
"handle": ANY_STR,
"internalId": internal_id_1,
"internalId": "stable_4",
"value": [[
'1', {
"type": "object",
"value": [["a", {
"type": "array",
"value": []
}]],
"internalId": internal_id_2
}],
[
'document', {
'internalId': 'stable_1',
'sharedId': 'stable_0',
'type': 'node',
'value': {
'childNodeCount': 1,
'nodeType': 9,
}
}
]],
"internalId": "stable_2",
}
], ['2', {
"type": "object",
"internalId": internal_id_2
"internalId": "stable_2",
}],
[
'3', {
Expand All @@ -438,16 +448,18 @@ async def test_serialization_internal_id(websocket, context_id):
}, {
"type": "number",
"value": 2
}, {
'internalId': 'stable_1',
'type': 'node',
}],
"internalId": internal_id_3
"internalId": "stable_3",
}
], ['4', {
"type": "array",
"internalId": internal_id_3
}],
["self", {
"internalId": "stable_3",
}], ["self", {
"type": "object",
"internalId": internal_id_1
"internalId": "stable_4",
}]],
}

Expand Down

0 comments on commit 84f9983

Please sign in to comment.