@agoric/marshal
Advanced tools
Comparing version 0.5.1-dev-cdf58e6.0 to 0.5.1-dev-d667e9f.0
40
index.js
@@ -1,4 +0,11 @@ | ||
export { PASS_STYLE, isObject } from './src/helpers/passStyleHelpers.js'; | ||
export { getErrorConstructor } from './src/helpers/error.js'; | ||
export { mapIterable, filterIterable } from './src/helpers/iter-helpers.js'; | ||
export { | ||
PASS_STYLE, | ||
isObject, | ||
assertChecker, | ||
getTag, | ||
} from './src/helpers/passStyle-helpers.js'; | ||
export { getErrorConstructor, toPassableError } from './src/helpers/error.js'; | ||
export { | ||
getInterfaceOf, | ||
@@ -8,15 +15,26 @@ ALLOW_IMPLICIT_REMOTABLES, | ||
export { passStyleOf, everyPassableChild } from './src/passStyleOf.js'; | ||
export { | ||
nameForPassableSymbol, | ||
passableSymbolForName, | ||
} from './src/helpers/symbol.js'; | ||
export { | ||
passStyleOf, | ||
assertPassable, | ||
everyPassableChild, | ||
somePassableChild, | ||
} from './src/passStyleOf.js'; | ||
export { pureCopy, sameValueZero } from './src/pureCopy.js'; | ||
export { deeplyFulfilled } from './src/deeplyFulfilled.js'; | ||
export { makeTagged } from './src/makeTagged.js'; | ||
export { Remotable, Far, ToFarFunction } from './src/make-far.js'; | ||
export { QCLASS, makeMarshal } from './src/marshal.js'; | ||
export { stringify, parse } from './src/marshal-stringify.js'; | ||
// Works, but not yet used | ||
// export { decodeToJustin } from './src/marshal-justin.js'; | ||
export { pureCopy, Remotable, Far, ToFarFunction } from './src/make-far.js'; | ||
export { stringify, parse } from './src/marshal-stringify.js'; | ||
export { | ||
isStructure, | ||
assertStructure, | ||
sameStructure, | ||
fulfillToStructure, | ||
} from './src/structure.js'; | ||
export { | ||
assertRecord, | ||
@@ -23,0 +41,0 @@ assertCopyArray, |
{ | ||
"name": "@agoric/marshal", | ||
"version": "0.5.1-dev-cdf58e6.0+cdf58e6", | ||
"version": "0.5.1-dev-d667e9f.0+d667e9f", | ||
"description": "marshal", | ||
@@ -37,9 +37,9 @@ "type": "module", | ||
"dependencies": { | ||
"@agoric/assert": "0.3.16-dev-cdf58e6.0+cdf58e6", | ||
"@agoric/eventual-send": "0.14.1-dev-cdf58e6.0+cdf58e6", | ||
"@agoric/assert": "0.3.16-dev-d667e9f.0+d667e9f", | ||
"@agoric/eventual-send": "0.14.1-dev-d667e9f.0+d667e9f", | ||
"@agoric/nat": "^4.1.0", | ||
"@agoric/promise-kit": "0.2.30-dev-cdf58e6.0+cdf58e6" | ||
"@agoric/promise-kit": "0.2.30-dev-d667e9f.0+d667e9f" | ||
}, | ||
"devDependencies": { | ||
"@agoric/lockdown": "0.1.2-dev-cdf58e6.0+cdf58e6", | ||
"@agoric/lockdown": "0.1.2-dev-d667e9f.0+d667e9f", | ||
"@endo/ses-ava": "^0.2.8", | ||
@@ -72,3 +72,3 @@ "ava": "^3.12.1", | ||
}, | ||
"gitHead": "cdf58e696bca3e662d876212d86bad68b3e47c10" | ||
"gitHead": "d667e9f83a9ada0a4ec0ddb39096e3f64cc57a5b" | ||
} |
@@ -9,3 +9,3 @@ /* eslint-disable no-use-before-define */ | ||
import { E } from '@agoric/eventual-send'; | ||
import { isObject } from './helpers/passStyleHelpers.js'; | ||
import { isObject } from './helpers/passStyle-helpers.js'; | ||
import { getInterfaceOf } from './helpers/remotable.js'; | ||
@@ -12,0 +12,0 @@ import { Far } from './make-far.js'; |
@@ -9,3 +9,3 @@ // @ts-check | ||
import '@agoric/assert/exported.js'; | ||
import { assertChecker, checkNormalProperty } from './passStyleHelpers.js'; | ||
import { assertChecker, checkNormalProperty } from './passStyle-helpers.js'; | ||
@@ -12,0 +12,0 @@ const { details: X } = assert; |
@@ -10,3 +10,3 @@ // @ts-check | ||
checkNormalProperty, | ||
} from './passStyleHelpers.js'; | ||
} from './passStyle-helpers.js'; | ||
@@ -13,0 +13,0 @@ import '../types.js'; |
@@ -9,3 +9,3 @@ // @ts-check | ||
import '@agoric/assert/exported.js'; | ||
import { assertChecker } from './passStyleHelpers.js'; | ||
import { assertChecker } from './passStyle-helpers.js'; | ||
@@ -12,0 +12,0 @@ const { details: X } = assert; |
@@ -17,3 +17,3 @@ // @ts-check | ||
getTag, | ||
} from './passStyleHelpers.js'; | ||
} from './passStyle-helpers.js'; | ||
import { getEnvironmentOption } from './environment-options.js'; | ||
@@ -20,0 +20,0 @@ |
@@ -7,3 +7,3 @@ // @ts-check | ||
import { assert, details as X, q } from '@agoric/assert'; | ||
import { assertChecker, PASS_STYLE } from './helpers/passStyleHelpers.js'; | ||
import { assertChecker, PASS_STYLE } from './helpers/passStyle-helpers.js'; | ||
import { | ||
@@ -14,3 +14,3 @@ assertIface, | ||
} from './helpers/remotable.js'; | ||
import { passStyleOf } from './passStyleOf.js'; | ||
import { pureCopy } from './pureCopy.js'; | ||
@@ -27,69 +27,2 @@ const { prototype: functionPrototype } = Function; | ||
/** | ||
* Do a deep copy of the object, handling Proxies and recursion. | ||
* The resulting copy is guaranteed to be pure data, as well as hardened. | ||
* Such a hardened, pure copy cannot be used as a communications path. | ||
* | ||
* @template {OnlyData} T | ||
* @param {T} val input value. NOTE: Must be hardened! | ||
* @returns {T} pure, hardened copy | ||
*/ | ||
export const pureCopy = val => { | ||
// passStyleOf now asserts that val has no pass-by-copy cycles. | ||
const passStyle = passStyleOf(val); | ||
switch (passStyle) { | ||
case 'bigint': | ||
case 'boolean': | ||
case 'null': | ||
case 'number': | ||
case 'string': | ||
case 'undefined': | ||
case 'symbol': | ||
return val; | ||
case 'copyArray': | ||
case 'copyRecord': { | ||
const obj = /** @type {Object} */ (val); | ||
// Create a new identity. | ||
const copy = /** @type {T} */ (passStyle === 'copyArray' ? [] : {}); | ||
// Make a deep copy on the new identity. | ||
// Object.entries(obj) takes a snapshot (even if a Proxy). | ||
// Since we already know it is a copyRecord or copyArray, we | ||
// know that Object.entries is safe enough. On a copyRecord it | ||
// will represent all the own properties. On a copyArray it | ||
// will represent all the own properties except for the length. | ||
Object.entries(obj).forEach(([prop, value]) => { | ||
copy[prop] = pureCopy(value); | ||
}); | ||
return harden(copy); | ||
} | ||
case 'error': { | ||
assert.fail(X`Errors cannot be copied: ${val}`, TypeError); | ||
} | ||
case 'remotable': { | ||
assert.fail( | ||
X`Input value ${q( | ||
passStyle, | ||
)} cannot be copied as it must be passed by reference`, | ||
TypeError, | ||
); | ||
} | ||
case 'promise': { | ||
assert.fail(X`Promises cannot be copied`, TypeError); | ||
} | ||
default: | ||
assert.fail( | ||
X`Input value ${q(passStyle)} is not recognized as data`, | ||
TypeError, | ||
); | ||
} | ||
}; | ||
harden(pureCopy); | ||
/** | ||
* Now that the remotableProto does not provide its own `toString` method, | ||
@@ -96,0 +29,0 @@ * ensure it always inherits from something. The original prototype of |
@@ -12,3 +12,4 @@ // @ts-check | ||
import { getErrorConstructor } from './helpers/error.js'; | ||
import { isObject } from './helpers/passStyleHelpers.js'; | ||
import { isObject } from './helpers/passStyle-helpers.js'; | ||
import { AtAtPrefixPattern, passableSymbolForName } from './helpers/symbol.js'; | ||
@@ -169,20 +170,14 @@ const { ownKeys } = Reflect; | ||
} | ||
case 'error': { | ||
const { name, message } = rawTree; | ||
assert.typeof( | ||
name, | ||
'string', | ||
X`invalid error name typeof ${q(typeof name)}`, | ||
); | ||
assert( | ||
getErrorConstructor(name) !== undefined, | ||
X`Must be the name of an Error constructor ${name}`, | ||
); | ||
assert.typeof( | ||
message, | ||
'string', | ||
X`invalid error message typeof ${q(typeof message)}`, | ||
); | ||
case 'symbol': { | ||
const { name } = rawTree; | ||
const sym = passableSymbolForName(name); | ||
assert.typeof(sym, 'symbol'); | ||
return; | ||
} | ||
case 'tagged': { | ||
const { tag, payload } = rawTree; | ||
assert.typeof(tag, 'string'); | ||
prepare(payload); | ||
return; | ||
} | ||
case 'slot': { | ||
@@ -229,2 +224,20 @@ const { index, iface } = rawTree; | ||
} | ||
case 'error': { | ||
const { name, message } = rawTree; | ||
assert.typeof( | ||
name, | ||
'string', | ||
X`invalid error name typeof ${q(typeof name)}`, | ||
); | ||
assert( | ||
getErrorConstructor(name) !== undefined, | ||
X`Must be the name of an Error constructor ${name}`, | ||
); | ||
assert.typeof( | ||
message, | ||
'string', | ||
X`invalid error message typeof ${q(typeof message)}`, | ||
); | ||
return; | ||
} | ||
@@ -321,9 +334,25 @@ default: { | ||
case '@@asyncIterator': { | ||
// TODO deprecated. Eventually remove. | ||
return out.next('Symbol.asyncIterator'); | ||
} | ||
case 'error': { | ||
const { name, message } = rawTree; | ||
return out.next(`${name}(${quote(message)})`); | ||
case 'symbol': { | ||
const { name } = rawTree; | ||
const sym = passableSymbolForName(name); | ||
assert.typeof(sym, 'symbol'); | ||
const registeredName = Symbol.keyFor(sym); | ||
if (registeredName === undefined) { | ||
const match = AtAtPrefixPattern.exec(name); | ||
assert(match !== null); | ||
const suffix = match[1]; | ||
assert(Symbol[suffix] === sym); | ||
return out.next(`Symbol[${quote(suffix)}]`); | ||
} | ||
return out.next(`Symbol.for(${quote(registeredName)})`); | ||
} | ||
case 'tagged': { | ||
const { tag, payload } = rawTree; | ||
out.next(`makeTagged(${quote(tag)},`); | ||
decode(payload); | ||
return out.next(')'); | ||
} | ||
@@ -352,2 +381,7 @@ case 'slot': { | ||
case 'error': { | ||
const { name, message } = rawTree; | ||
return out.next(`${name}(${quote(message)})`); | ||
} | ||
default: { | ||
@@ -354,0 +388,0 @@ assert.fail(X`unrecognized ${q(QCLASS)} ${q(qclass)}`, TypeError); |
@@ -13,3 +13,9 @@ // @ts-check | ||
import { ErrorHelper, getErrorConstructor } from './helpers/error.js'; | ||
import { isObject } from './helpers/passStyleHelpers.js'; | ||
import { makeTagged } from './makeTagged.js'; | ||
import { isObject, getTag } from './helpers/passStyle-helpers.js'; | ||
import { | ||
assertPassableSymbol, | ||
nameForPassableSymbol, | ||
passableSymbolForName, | ||
} from './helpers/symbol.js'; | ||
@@ -138,6 +144,5 @@ const { ownKeys } = Reflect; | ||
* Must encode `val` into plain JSON data *canonically*, such that | ||
* `sameStructure(v1, v2)` implies | ||
* `JSON.stringify(encode(v1)) === JSON.stringify(encode(v2))` | ||
* For each record, we only accept sortable property names | ||
* (no anonymous symbols). On the encoded form the sort | ||
* `JSON.stringify(encode(v1)) === JSON.stringify(encode(v1))` | ||
* For each copyRecord, we only accept string property names, | ||
* not symbols. The encoded form the sort | ||
* order of these names must be the same as their enumeration | ||
@@ -190,12 +195,17 @@ * order, so a `JSON.stringify` of the encoded form agrees with | ||
case 'symbol': { | ||
switch (val) { | ||
case Symbol.asyncIterator: { | ||
return harden({ | ||
[QCLASS]: '@@asyncIterator', | ||
}); | ||
} | ||
default: { | ||
assert.fail(X`Unsupported symbol ${q(String(val))}`); | ||
} | ||
assertPassableSymbol(val); | ||
const name = /** @type {string} */ (nameForPassableSymbol(val)); | ||
if (name === '@@asyncIterator') { | ||
// Deprectated qclass. TODO make conditional | ||
// on environment variable. Eventually remove, but only after | ||
// confident that all supported receivers understand | ||
// `[QCLASS]: 'symbol'`. | ||
return harden({ | ||
[QCLASS]: '@@asyncIterator', | ||
}); | ||
} | ||
return harden({ | ||
[QCLASS]: 'symbol', | ||
name, | ||
}); | ||
} | ||
@@ -232,4 +242,10 @@ case 'copyRecord': { | ||
} | ||
case 'error': { | ||
return encodeError(val); | ||
case 'tagged': { | ||
/** @type {Encoding} */ | ||
const result = harden({ | ||
[QCLASS]: 'tagged', | ||
tag: getTag(val), | ||
payload: encode(val.payload), | ||
}); | ||
return result; | ||
} | ||
@@ -241,2 +257,5 @@ case 'remotable': { | ||
} | ||
case 'error': { | ||
return encodeError(val); | ||
} | ||
case 'promise': { | ||
@@ -352,5 +371,17 @@ // console.log(`serializeSlot: ${val}`); | ||
case '@@asyncIterator': { | ||
// Deprectated qclass. TODO make conditional | ||
// on environment variable. Eventually remove, but after confident | ||
// that there are no more supported senders. | ||
return Symbol.asyncIterator; | ||
} | ||
case 'symbol': { | ||
const { name } = rawTree; | ||
return passableSymbolForName(name); | ||
} | ||
case 'tagged': { | ||
const { tag, payload } = rawTree; | ||
return makeTagged(tag, fullRevive(payload)); | ||
} | ||
case 'error': { | ||
@@ -357,0 +388,0 @@ const { name, message, errorId } = rawTree; |
@@ -7,11 +7,13 @@ // @ts-check | ||
import { isPromise } from '@agoric/promise-kit'; | ||
import { isObject, PASS_STYLE } from './helpers/passStyleHelpers.js'; | ||
import { isObject, PASS_STYLE } from './helpers/passStyle-helpers.js'; | ||
import { CopyArrayHelper } from './helpers/copyArray.js'; | ||
import { CopyRecordHelper } from './helpers/copyRecord.js'; | ||
import { TaggedHelper } from './helpers/tagged.js'; | ||
import { RemotableHelper } from './helpers/remotable.js'; | ||
import { ErrorHelper } from './helpers/error.js'; | ||
import { RemotableHelper } from './helpers/remotable.js'; | ||
import './types.js'; | ||
import './helpers/internal-types.js'; | ||
import { assertPassableSymbol } from './helpers/symbol.js'; | ||
@@ -37,4 +39,5 @@ const { details: X, quote: q } = assert; | ||
copyRecord: undefined, | ||
tagged: undefined, | ||
remotable: undefined, | ||
error: undefined, | ||
remotable: undefined, | ||
}; | ||
@@ -119,6 +122,9 @@ for (const helper of passStyleHelpers) { | ||
case 'number': | ||
case 'bigint': | ||
case 'symbol': { | ||
case 'bigint': { | ||
return typestr; | ||
} | ||
case 'symbol': { | ||
assertPassableSymbol(inner); | ||
return 'symbol'; | ||
} | ||
case 'object': { | ||
@@ -185,7 +191,13 @@ if (inner === null) { | ||
CopyRecordHelper, | ||
TaggedHelper, | ||
RemotableHelper, | ||
ErrorHelper, | ||
RemotableHelper, | ||
]); | ||
export { passStyleOf }; | ||
export const assertPassable = val => { | ||
passStyleOf(val); // throws if val is not a passable | ||
}; | ||
harden(assertPassable); | ||
export const everyPassableChild = (passable, fn) => { | ||
@@ -202,1 +214,5 @@ const passStyle = passStyleOf(passable); | ||
harden(everyPassableChild); | ||
export const somePassableChild = (passable, fn) => | ||
!everyPassableChild(passable, (v, i) => !fn(v, i)); | ||
harden(somePassableChild); |
@@ -9,4 +9,10 @@ // @ts-nocheck TODO Fix the recursive types to it checks. Will this | ||
* @typedef { "undefined" | "null" | | ||
* "boolean" | "number" | "bigint" | "string" | "symbol" | | ||
* "copyArray" | "copyRecord" | "remotable" | | ||
* "boolean" | "number" | "bigint" | "string" | "symbol" | ||
* } PrimitiveStyle | ||
*/ | ||
/** | ||
* @typedef { PrimitiveStyle | | ||
* "copyRecord" | "copyArray" | "tagged" | | ||
* "remotable" | | ||
* "error" | "promise" | ||
@@ -28,6 +34,6 @@ * } PassStyle | ||
* "boolean" | "number" | "bigint" | "string" | "symbol"), | ||
* * the pass-by-copy containers ("copyArray" | "copyRecord") that | ||
* * the pass-by-copy containers | ||
* ("copyRecord" | "copyArray" | "tagged") that | ||
* contain other Passables, | ||
* * and the special cases ("error" | "promise"), which | ||
* also contain other Passables. | ||
* * and the special cases ("error" | "promise"). | ||
* | ||
@@ -48,14 +54,3 @@ * A Passable's pass-by-copy superstructure ends in | ||
/** | ||
* @typedef {Passable} Structure | ||
* | ||
* A Passable is a Structure when it contains only | ||
* * pass-by-copy primitives, | ||
* * pass-by-copy containers, | ||
* * remotables. | ||
* | ||
* Two Structures may be compared by for equivalence according to | ||
* `sameStructure`, which is the strongest equivalence class supported by | ||
* marshal's distributed object semantics. | ||
* | ||
* Two Structures can also be compared for full ordering, | ||
* Two Passables can also be compared for total rank ordering, | ||
* * where their passStyles are ordered according to the | ||
@@ -79,10 +74,5 @@ * PassStyle typedef above. | ||
/** | ||
* @deprecated Renamed to `Structure` | ||
* @typedef {Structure} Comparable | ||
*/ | ||
/** | ||
* @typedef {Structure} OnlyData | ||
* @typedef {Passable} OnlyData | ||
* | ||
* A Structure is OnlyData when its pass-by-copy superstructure has no | ||
* A Passable is OnlyData when its pass-by-copy superstructure has no | ||
* remotables, i.e., when all the leaves of the data structure tree are | ||
@@ -115,13 +105,11 @@ * primitive data types or empty composites. | ||
/** | ||
* @typedef {Passable} CopySet | ||
* @typedef {{ | ||
* [PASS_STYLE]: 'tagged', | ||
* [Symbol.toStringTag]: string, | ||
* payload: Passable | ||
* }} CopyTagged | ||
* | ||
* The tag is the value of the `[String.toStringTag]` property. | ||
*/ | ||
/** | ||
* @typedef {Passable} CopyMap | ||
*/ | ||
/** | ||
* @typedef {Passable} PatternNode | ||
*/ | ||
// ///////////////////////////////////////////////////////////////////////////// | ||
@@ -156,2 +144,3 @@ | ||
* EncodingClass<'@@asyncIterator'> | | ||
* EncodingClass<'symbol'> & { name: string } | | ||
* EncodingClass<'error'> & { name: string, | ||
@@ -163,10 +152,12 @@ * message: string, | ||
* EncodingClass<'hilbert'> & { original: Encoding, | ||
* rest?: Encoding } | ||
* rest?: Encoding | ||
* } | | ||
* EncodingClass<'tagged'> & { tag: string, | ||
* payload: Encoding | ||
* } | ||
* } EncodingUnion | ||
* | ||
* @typedef {{ [index: string]: Encoding, | ||
* '@qclass'?: undefined } | ||
* } EncodingRecord | ||
* '@qclass'?: undefined | ||
* }} EncodingRecord | ||
* We exclude '@qclass' as a property in encoding records. | ||
* | ||
* @typedef {EncodingUnion | null | string | | ||
@@ -256,3 +247,2 @@ * boolean | number | EncodingRecord | ||
* Simple semantics, just tell what interface (or undefined) a remotable has. | ||
* | ||
* @param {*} maybeRemotable the value to check | ||
@@ -279,3 +269,2 @@ * @returns {InterfaceSpec|undefined} the interface specification, or undefined | ||
* See the various uses for good examples. | ||
* | ||
* @param {boolean} cond | ||
@@ -282,0 +271,0 @@ * @param {Details=} details |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
136507
29
2924