Skip to content

Commit

Permalink
fix(node): implement v8 serialize and deserialize (denoland#22975)
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy committed Mar 18, 2024
1 parent becdad5 commit bd6938a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
13 changes: 9 additions & 4 deletions ext/node/polyfills/v8.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// Copyright Joyent and Node contributors. All rights reserved. MIT license.

/// <reference path="../../core/internal.d.ts" />

// TODO(petamoriken): enable prefer-primordials for node polyfills
// deno-lint-ignore-file prefer-primordials

import { core } from "ext:core/mod.js";
import {
op_v8_cached_data_version_tag,
op_v8_get_heap_statistics,
} from "ext:core/ops";

import { Buffer } from "node:buffer";

import { notImplemented } from "ext:deno_node/_utils.ts";

export function cachedDataVersionTag() {
Expand Down Expand Up @@ -66,11 +71,11 @@ export function takeCoverage() {
export function writeHeapSnapshot() {
notImplemented("v8.writeHeapSnapshot");
}
export function serialize() {
notImplemented("v8.serialize");
export function serialize(value) {
return Buffer.from(core.serialize(value));
}
export function deserialize() {
notImplemented("v8.deserialize");
export function deserialize(data) {
return core.deserialize(data);
}
export class Serializer {
constructor() {
Expand Down
11 changes: 11 additions & 0 deletions tests/unit_node/v8_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import {
cachedDataVersionTag,
deserialize,
getHeapStatistics,
serialize,
setFlagsFromString,
} from "node:v8";
import { assertEquals } from "@std/assert/mod.ts";
Expand Down Expand Up @@ -53,3 +55,12 @@ Deno.test({
setFlagsFromString("--allow_natives_syntax");
},
});

Deno.test({
name: "serialize deserialize",
fn() {
const s = serialize({ a: 1 });
const d = deserialize(s);
assertEquals(d, { a: 1 });
},
});

0 comments on commit bd6938a

Please sign in to comment.