fix(msgpack): encode maps with null prototype (#4764)

* fix(msgpack): encode maps with null prototype

* deno fmt

* improve reusability
This commit is contained in:
sanekb 2024-05-21 01:06:56 +03:00 committed by GitHub
parent f4b0704d76
commit 52116b2dd6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 1 deletions

View File

@ -249,7 +249,9 @@ function encodeSlice(object: ValueType, byteParts: Uint8Array[]) {
}
// If object is a plain object
if (Object.getPrototypeOf(object) === Object.prototype) {
const prototype = Object.getPrototypeOf(object);
if (prototype === null || prototype === Object.prototype) {
const numKeys = Object.keys(object).length;
if (numKeys < FOUR_BITS) { // fixarray

View File

@ -156,6 +156,9 @@ Deno.test("encode() handles maps", () => {
const map0 = {};
assertEquals(decode(encode(map0)), map0);
const mapNull = Object.create(null);
assertEquals(decode(encode(mapNull)), mapNull);
const map1 = { "a": 0, "b": 2, "c": "three", "d": null };
assertEquals(decode(encode(map1)), map1);