@metamask/ocap-kernel
Version:
OCap kernel core components
55 lines • 1.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseRef = parseRef;
const errors_1 = require("@endo/errors");
/**
* Parse an alleged ref string into its components.
*
* @param ref - The string to be parsed.
*
* @returns an object with all of the ref string components as individual properties.
*/
function parseRef(ref) {
let context;
let typeIdx = 1;
switch (ref[0]) {
case 'k':
context = 'kernel';
break;
case 'o':
case 'p':
typeIdx = 0;
context = 'vat';
break;
case 'r':
context = 'remote';
break;
case undefined:
default:
(0, errors_1.Fail) `invalid reference context ${ref[0]}`;
}
if (ref[typeIdx] !== 'p' && ref[typeIdx] !== 'o') {
(0, errors_1.Fail) `invalid reference type ${ref[typeIdx]}`;
}
const isPromise = ref[typeIdx] === 'p';
let direction;
let index;
if (context === 'kernel') {
index = ref.slice(2);
}
else {
const dirIdx = typeIdx + 1;
if (ref[dirIdx] !== '+' && ref[dirIdx] !== '-') {
(0, errors_1.Fail) `invalid reference direction ${ref[dirIdx]}`;
}
direction = ref[dirIdx] === '+' ? 'export' : 'import';
index = ref.slice(dirIdx + 1);
}
return {
context,
direction,
isPromise,
index,
};
}
//# sourceMappingURL=parse-ref.cjs.map