Skip to content

Commit

Permalink
fix: attachment encoding fallback base64 + base64url by default (#239)
Browse files Browse the repository at this point in the history
Signed-off-by: Francisco Javier Ribó Labrador <[email protected]>
  • Loading branch information
elribonazo authored Jun 28, 2024
1 parent 28afb98 commit a22d3dd
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/domain/models/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { AgentError } from "./Errors";
import { CredentialType, JsonString } from ".";
import { Pluto } from "../buildingBlocks/Pluto";
import { JsonObj, asJsonObj, isArray, isNil, isObject, isString, notEmptyString, notNil } from "../../utils";
import { base64 } from "multiformats/bases/base64";
import { base64, base64url } from "multiformats/bases/base64";

export enum MessageDirection {
SENT = 0,
Expand Down Expand Up @@ -170,6 +170,13 @@ export class Message implements Pluto.Storable {
}
}

const decodeBase64 = (data: string) => {
try {
return base64.baseDecode(data);
} catch (err) {
return base64url.baseDecode(data);
}
}
export namespace Message {
export namespace Attachment {
/**
Expand All @@ -180,7 +187,7 @@ export namespace Message {
*/
export const extractJSON = (attachment: AttachmentDescriptor) => {
if (isBase64(attachment.data)) {
const decoded = Buffer.from(base64.baseDecode(attachment.data.base64)).toString();
const decoded = Buffer.from(decodeBase64(attachment.data.base64)).toString();
try {
return JSON.parse(decoded);
} catch (err) {
Expand Down

1 comment on commit a22d3dd

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines Statements Branches Functions
Coverage: 75%
75.28% (2784/3698) 64.98% (1353/2082) 80.53% (720/894)

JUnit

Tests Skipped Failures Errors Time
501 6 💤 0 ❌ 0 🔥 1m 17s ⏱️

Please sign in to comment.