Skip to content

Commit

Permalink
chore(marshal): Consistently assert behavior of makeEncodePassable re…
Browse files Browse the repository at this point in the history
…motable/promise/error callbacks
  • Loading branch information
gibson042 committed Aug 29, 2023
1 parent 3f39f36 commit c97046e
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions packages/marshal/src/encodePassable.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,21 @@ const decodeTagged = (encoded, decodeArray, decodePassable) => {
return makeTagged(tag, payload);
};

const assertEncodedRemotable = encoding => {
encoding.startsWith('r') ||
Fail`internal: Remotable encoding must start with "r": ${encoding}`;
};

const assertEncodedPromise = encoding => {
encoding.startsWith('?') ||
Fail`internal: Promise encoding must start with "?": ${encoding}`;
};

const assertEncodedError = encoding => {
encoding.startsWith('!') ||
Fail`internal: Error encoding must start with "!": ${encoding}`;
};

/**
* @typedef {object} EncodeOptions
* @property {(
Expand Down Expand Up @@ -469,7 +484,19 @@ export const makeEncodePassable = (encodeOptions = {}) => {

const innerEncode = passable => {
if (isErrorLike(passable)) {
return encodeError(passable, innerEncode);
// We pull out this special case to accommodate errors that are not
// valid Passables. For example, because they're not frozen.
// The special case can only ever apply at the root, and therefore
// outside the recursion, since an error could only be deeper in
// a passable structure if it were passable.
//
// We pull out this special case because, for these errors, we're much
// more interested in reporting whatever diagnostic information they
// carry than we are about reporting problems encountered in reporting
// this information.
const result = encodeError(passable, innerEncode);
assertEncodedError(result);
return result;
}
const passStyle = passStyleOf(passable);
switch (passStyle) {
Expand All @@ -493,20 +520,17 @@ export const makeEncodePassable = (encodeOptions = {}) => {
}
case 'remotable': {
const result = encodeRemotable(passable, innerEncode);
result.startsWith('r') ||
Fail`internal: Remotable encoding must start with "r": ${result}`;
assertEncodedRemotable(result);
return result;
}
case 'error': {
const result = encodeError(passable, innerEncode);
result.startsWith('!') ||
Fail`internal: Error encoding must start with "!": ${result}`;
assertEncodedError(result);
return result;
}
case 'promise': {
const result = encodePromise(passable, innerEncode);
result.startsWith('?') ||
Fail`internal: Promise encoding must start with "?": ${result}`;
assertEncodedPromise(result);
return result;
}
case 'symbol': {
Expand Down

0 comments on commit c97046e

Please sign in to comment.