devalue serialization with cycles and repeated refs: working versions (tested 2026-10-16)
Serialize and parse values with devalue, covering cyclic references, Dates, Maps and repeated refs, and assert that functions throw offline.
passed5 passed · 0 failed2026-10-16 19:54 UTC- Status
- passed
- Last passed
- 2026-10-16 19:54 UTC
- Tests
- 5 / 5
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions3
devalue@6.0.3@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false npm test
test environment
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 38958328921
The code
Exactly the files that passed. Your agent can pull them with get_recipe("devalue-serialize-cycles") (install), or see them on GitHub.
package.json
{
"name": "devalue-serialize-cycles",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"devalue": "6.0.3"
},
"devDependencies": {
"@types/node": "26.6.5",
"tsx": "4.23.15"
}
}
src/serialize.ts
import { parse, stringify } from "devalue";
export type GraphNode = {
id: string;
createdAt: Date;
scores: Map<string, number>;
parent?: GraphNode;
peer?: GraphNode;
};
export function serialize(value: unknown): string {
return stringify(value);
}
export function deserialize<T>(value: string): T {
return parse(value) as T;
}
export function roundTrip<T>(value: T): T {
return deserialize<T>(serialize(value));
}
export function buildGraph(): GraphNode {
const root: GraphNode = {
id: "root",
createdAt: new Date("2024-01-02T03:04:05.678Z"),
scores: new Map([["a", 1]]),
};
root.parent = root;
const peer: GraphNode = {
id: "peer",
createdAt: new Date("2024-02-03T00:00:00.000Z"),
scores: new Map([["b", 2]]),
parent: root,
};
peer.peer = peer;
root.peer = peer;
return root;
}
test/serialize.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { stringify } from "devalue";
import { buildGraph, roundTrip } from "../src/serialize.ts";
test("preserves cyclic references as the same object", () => {
const restored = roundTrip(buildGraph());
assert.equal(restored.parent, restored);
assert.equal(restored.peer?.peer, restored.peer);
assert.equal(restored.peer?.parent, restored);
});
test("preserves repeated references by identity", () => {
const shared = { id: "shared" };
const restored = roundTrip({ a: shared, b: shared });
assert.equal(restored.a, restored.b);
});
test("preserves Date values", () => {
const when = new Date("2024-01-02T03:04:05.678Z");
const restored = roundTrip({ when });
assert.ok(restored.when instanceof Date);
assert.equal(restored.when.getTime(), when.getTime());
});
test("preserves Map entries", () => {
const counts = new Map<string, number>([
["a", 1],
["b", 2],
]);
const restored = roundTrip({ counts });
assert.ok(restored.counts instanceof Map);
assert.equal(restored.counts.get("a"), 1);
assert.equal(restored.counts.get("b"), 2);
});
test("throws when stringifying a function", () => {
assert.throws(() => stringify({ fn: () => 1 }), /Cannot stringify a function/);
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.