Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPIKE: Pure-Data Object Notation #10701

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions packages/internal/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from './config.js';
export * from './debug.js';
export * from './errors.js';
export * from './js-utils.js';
export * from './pdon.js';
export * from './method-tools.js';
export * from './ses-utils.js';
export * from './typeCheck.js';
Expand Down
38 changes: 38 additions & 0 deletions packages/internal/src/pdon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Fail } from '@endo/errors';
import { makeMarshal } from '@endo/marshal';

/**
* @typedef {`#{string}`} PureDataObjectNotation
*/
/**
* @import {PureData} from '@endo/pass-style';
*/

/** @param {import('@endo/marshal').CapData<unknown>} cap */
const rejectOCap = cap => Fail`${cap} is not pure data`;
const pureDataMarshaller = makeMarshal(rejectOCap, rejectOCap, {
serializeBodyFormat: 'smallcaps',
});

/**
* Pure-Data Object Notation
*/
export const pdon = {
/**
* @type {(value: PureData) => PureDataObjectNotation}
*/
stringify(value) {
harden(value);
const { body, slots } = pureDataMarshaller.toCapData(value);
slots.length === 0 || Fail`slots ${slots} unexpected for pure data`;
// Constraining to pure data guarantees empty slots and allows for direct use of `body`.
return /** @type {PureDataObjectNotation} */ (body);
},
/**
* @type {(text: PureDataObjectNotation) => PureData}
*/
parse(txt) {
return pureDataMarshaller.fromCapData({ body: txt, slots: [] });
},
};
harden(pdon);
74 changes: 74 additions & 0 deletions packages/internal/test/pdon.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import test from 'ava';
import { pdon } from '../src/pdon.js';

test('pdon stringify basic types', t => {
t.is(pdon.stringify(null), '#null');
t.is(pdon.stringify(true), '#true');
t.is(pdon.stringify(false), '#false');
t.is(pdon.stringify(123), '#123');
t.is(pdon.stringify('hello'), '#"hello"');
t.is(pdon.stringify([1, 2, 3]), '#[1,2,3]');
t.is(pdon.stringify({ a: 1, b: 2 }), '#{"a":1,"b":2}');
});

test('pdon parse basic types', t => {
t.is(pdon.parse('#null'), null);
t.is(pdon.parse('#true'), true);
t.is(pdon.parse('#false'), false);
t.is(pdon.parse('#123'), 123);
t.is(pdon.parse('#"hello"'), 'hello');
t.deepEqual(pdon.parse('#[1,2,3]'), [1, 2, 3]);
t.deepEqual(pdon.parse('#{"a":1,"b":2}'), { a: 1, b: 2 });
});

test('pdon nested structures', t => {
const nested = {
array: [1, 'two', { three: 3 }],
obj: {
nested: {
deeper: [true, null],
},
},
};
const stringified = pdon.stringify(nested);
const parsed = pdon.parse(stringified);
t.deepEqual(parsed, nested);
});

test('pdon rejects non-pure data', t => {
// Functions are not pure data
t.throws(() => pdon.stringify(() => {}), { instanceOf: Error });

// Objects with methods are not pure data
t.throws(
() =>
pdon.stringify({
method() {},
}),
{ instanceOf: Error },
);

// Objects with symbol properties are not pure data
const sym = Symbol('test');
t.throws(
() =>
pdon.stringify({
[sym]: 'value',
}),
{ instanceOf: Error },
);
});

test('pdon parse invalid input', t => {
// Lax about missing '#'
t.deepEqual(pdon.parse('{"not":"valid"}'), { not: 'valid' });

// Invalid JSON after #
t.throws(() => pdon.parse('#not json'), { instanceOf: Error });

// Empty string
t.throws(() => pdon.parse(''), { instanceOf: Error });

// Just #
t.throws(() => pdon.parse('#'), { instanceOf: Error });
});
1 change: 1 addition & 0 deletions packages/internal/test/snapshots/exports.test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Generated by [AVA](https://avajs.dev).
'mustMatch',
'objectMap',
'objectMetaMap',
'pdon',
'synchronizedTee',
'untilTrue',
'whileTrue',
Expand Down
Binary file modified packages/internal/test/snapshots/exports.test.js.snap
Binary file not shown.
Loading