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

Update CRT #522

Merged
merged 2 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions lib/eventstream_rpc_utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

import * as eventstream_rpc_utils from "./eventstream_rpc_utils";

jest.setTimeout(5000);

test('Encode string payload as base64', async () => {
let encodedPayload = eventstream_rpc_utils.encodePayloadAsString("HelloWorld");
expect(encodedPayload).toEqual("SGVsbG9Xb3JsZA==");
});

test('Encode ArrayBuffer payload as base64', async () => {
let buffer = new ArrayBuffer(10);
let writeView = new Uint8Array(buffer);
for (let i = 0; i < 10; i++) {
writeView[i] = 65;
}

let encodedPayload = eventstream_rpc_utils.encodePayloadAsString(buffer);
expect(encodedPayload).toEqual("QUFBQUFBQUFBQQ==");
});

test('Encode view payload as base64', async () => {
let buffer = new ArrayBuffer(10);
let writeView = new Uint8Array(buffer);
for (let i = 0; i < 10; i++) {
writeView[i] = 65;
}

let view = new DataView(buffer, 1, 8);

let encodedPayload = eventstream_rpc_utils.encodePayloadAsString(view);
expect(encodedPayload).toEqual("QUFBQUFBQUE=");
});
10 changes: 9 additions & 1 deletion lib/eventstream_rpc_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ import {CrtError, eventstream} from "aws-crt";
* @return a base64-encoded string
*/
export function encodePayloadAsString(payload : eventstream.Payload) : string {
return Buffer.from(payload).toString("base64");
if (ArrayBuffer.isView(payload)) {
return Buffer.from(payload.buffer, payload.byteOffset, payload.byteLength).toString("base64");
} else if (typeof payload === "string") {
let value = payload as string;
return Buffer.from(value).toString("base64");
} else {
let value = payload as ArrayBuffer;
return Buffer.from(value).toString("base64");
}
}

/**
Expand Down
Loading
Loading