Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds UTF-8 as an encoding option #587

Merged
merged 5 commits into from Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/generated/changelog.html
Expand Up @@ -10,6 +10,10 @@
<h1>Agent-JS Changelog</h1>

<section>
<h2>Version 0.12.1</h2>
<ul>
<li>Adds UTF-8 as an encoding option for CanisterStatus custom paths</li>
</ul>
<h2>Version 0.12.0</h2>
<ul>
<li>
Expand Down
Expand Up @@ -60,9 +60,11 @@ exports[`Canister Status utility should support valid custom paths 1`] = `165299

exports[`Canister Status utility should support valid custom paths 2`] = `ArrayBuffer []`;

exports[`Canister Status utility should support valid custom paths 3`] = `"80f485e1a4a6a7f816"`;
exports[`Canister Status utility should support valid custom paths 3`] = `"74797065204578616d706c65203d20746578743b0a73657276696365203a207b0a202067726565743a20284578616d706c6529202d3e202874657874292071756572793b0a7d0a"`;

exports[`Canister Status utility should support valid custom paths 4`] = `
exports[`Canister Status utility should support valid custom paths 4`] = `"80f485e1a4a6a7f816"`;

exports[`Canister Status utility should support valid custom paths 5`] = `
Array [
Uint8Array [
4,
Expand Down
9 changes: 9 additions & 0 deletions packages/agent/src/canisterStatus/index.test.ts
Expand Up @@ -113,6 +113,14 @@ describe('Canister Status utility', () => {
decodeStrategy: 'raw',
},
]);
const statusUTF8 = await getStatus([
{
kind: 'metadata',
path: 'candid:service',
key: 'candid',
decodeStrategy: 'hex',
krpeacock marked this conversation as resolved.
Show resolved Hide resolved
},
]);
const statusHex = await getStatus([
{
key: 'time',
Expand All @@ -129,6 +137,7 @@ describe('Canister Status utility', () => {
]);
expect(status.get('time')).toMatchSnapshot();
expect(statusRaw.get('time')).toMatchSnapshot();
expect(statusUTF8.get('candid')).toMatchSnapshot();
expect(statusHex.get('time')).toMatchSnapshot();
expect(statusCBOR.get('Controller')).toMatchSnapshot();
});
Expand Down
11 changes: 9 additions & 2 deletions packages/agent/src/canisterStatus/index.ts
Expand Up @@ -15,8 +15,8 @@ export type Status = string | ArrayBuffer | Date | ArrayBuffer[] | Principal[] |
*/
export interface CustomPath {
key: string;
path: ArrayBuffer[];
decodeStrategy: 'cbor' | 'hex' | 'leb128' | 'raw';
path: ArrayBuffer[] | string;
decodeStrategy: 'cbor' | 'hex' | 'leb128' | 'utf-8' | 'raw';
}

/**
Expand Down Expand Up @@ -139,6 +139,9 @@ export const request = async (options: {
status.set(path.key, decodeHex(data));
break;
}
case 'utf-8': {
status.set(path.key, decodeUtf8(data));
}
}
}
}
Expand Down Expand Up @@ -214,6 +217,10 @@ const decodeCbor = (buf: ArrayBuffer): ArrayBuffer[] => {
return Cbor.decode(buf);
};

const decodeUtf8 = (buf: ArrayBuffer): string => {
return new TextDecoder().decode(buf);
};

// time is a LEB128-encoded Nat
const decodeTime = (buf: ArrayBuffer): Date => {
const decoded = decodeLeb128(buf);
Expand Down