From 8ce11ddb1262126b4ff9878e651ec49b07a73740 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Thu, 28 Mar 2024 12:36:13 +0100 Subject: [PATCH] More flexible `decodeStandardRoute` --- packages/ws-relay/src/index.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/ws-relay/src/index.ts b/packages/ws-relay/src/index.ts index 2ba2485..4bb7075 100644 --- a/packages/ws-relay/src/index.ts +++ b/packages/ws-relay/src/index.ts @@ -20,6 +20,7 @@ import { decode, fromJS, isEmbedded, + parse, stringify, underlying, Embeddable, @@ -434,10 +435,19 @@ export function unpackStandardRoute(route: E.StandardRoute }); } +// We support a couple of variations here: a string containing Base64-encoded machine-oriented +// Preserves, or a string containing text-syntax Preserves. export function decodeStandardRoute(s: string): G.Route | null { try { - const route = E.toStandardRoute(decode( - Bytes.fromBase64(s.replace(/[^-_+/A-Za-z0-9=]/g, '')))); + let routeValue: AnyValue; + // A valid text-syntax representation of a StandardRoute must start with + // either '[' or '<', neither of which is valid Base64. + if (s[0] === '[' || s[0] === '<') { + routeValue = parse(s); + } else { + routeValue = decode(Bytes.fromBase64(s.replace(/[^-_+/A-Za-z0-9=]/g, ''))); + } + const route = E.toStandardRoute(routeValue); return route === void 0 ? null : unpackStandardRoute(route); } catch (e) { console.error('Decoding standard route:', e);