Skip to content

Commit

Permalink
Partially support TextEncoder Web API (#5863)
Browse files Browse the repository at this point in the history
  • Loading branch information
takuro-sato authored Dec 12, 2023
1 parent 98091b5 commit cb684d5
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
61 changes: 61 additions & 0 deletions js/ccf-app/src/textcodec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the Apache 2.0 License.

/**
* The `textcodec` module provides access to TextEncoder Web API class.
*
* Example:
* ```
* import * as ccftextcodec from '@microsoft/ccf-app/textcodec.js';
*
* const bytes = new ccftextcodec.TextEncoder().encode("foo")
* ```
*
* If you need TextEncoder Web API as a globally accessible class:
* ```
* import * as ccftextcodec from '@microsoft/ccf-app/textcodec.js';
*
* if (globalThis != undefined && (globalThis as any).TextEncoder == undefined) {
* (globalThis as any).TextEncoder = ccftextcodec.TextEncoder;
* }
*
* ```
*
* @module
*/

import { ccf } from "./global.js";

export type TextEncoderEncodeIntoResult = {
read?: number;
written?: number;
};

/**
* TextEncoder can be used to encode string to Uint8Array.
*/
export class TextEncoder {
/**
* Always returns "utf-8".
*/
public readonly encoding: string = "utf-8";

/**
* Returns Uint8Array containing UTF-8 encoded text.
* @param input Input string to encode.
* @returns Encoded bytes.
*/
encode(input: string): Uint8Array {
return new Uint8Array(ccf.strToBuf(input));
}

/**
* Not implemented.
* @param input
* @param output
* @throws Always throws an Error object.
*/
encodeInto(input: string, output: Uint8Array): TextEncoderEncodeIntoResult {
throw new Error("Not implemented");
}
}
34 changes: 34 additions & 0 deletions js/ccf-app/test/polyfill.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
RsaOaepAesKwpParams,
RsaOaepParams,
} from "../src/global.js";
import * as textcodec from "../src/textcodec.js";
import { generateSelfSignedCert, generateCertChain } from "./crypto.js";

beforeEach(function () {
Expand All @@ -24,6 +25,39 @@ describe("polyfill", function () {
assert.equal(ccf.bufToStr(ccf.strToBuf(s)), s);
});
});
describe("TextEncoder", function () {
it("returns utf-8 for encoding field", function () {
const encoder = new textcodec.TextEncoder();
assert.equal(encoder.encoding, "utf-8");
const s = encoder.encode("foo");
});
it("returns an empty array for default empty input", function () {
assert.deepEqual(
new textcodec.TextEncoder().encode(""),
new Uint8Array(),
);
});
it("encodes ascii strings correctly", function () {
const sample = "foo";
assert.deepEqual(
new textcodec.TextEncoder().encode(sample),
new Uint8Array([0x66, 0x6f, 0x6f]),
);
});
it("encodes UTF-8 strings correctly", function () {
// a (U+0061, 0x61 in UTF-8), pound sign (U+00A3, 0xC2 0xA3 in UTF-8)
const sample = "\u0061\u00A3";
assert.deepEqual(
new textcodec.TextEncoder().encode(sample),
new Uint8Array([0x61, 0xc2, 0xa3]),
);
});
it("throws when unsupported method is called", function () {
assert.throws(() =>
new textcodec.TextEncoder().encodeInto("test", new Uint8Array([])),
);
});
});
describe("jsonCompatibleToBuf/bufToJsonCompatible", function () {
it("converts JSON-compatible <--> ArrayBuffer", function () {
const s = { foo: "bar" };
Expand Down

0 comments on commit cb684d5

Please sign in to comment.