"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeTextEncoder = void 0;
class UniversalTextEncoder {
    constructor() {
        this.length = 0;
        this.encoder = new TextEncoder();
    }
    encode(text) {
        const arr = this.encoder.encode(text);
        this.length = arr.length;
        return arr;
    }
}
class NodeTextEncoder {
    constructor() {
        this.buffer = Buffer.alloc(256);
        this.length = 0;
    }
    encode(text) {
        while (true) {
            this.length = this.buffer.write(text, 'utf8');
            // buffer.write returns the number of bytes written and can write less
            // than the length of the string if the buffer is too small. If this
            // might have happened (4 bytes is the longest utf8 codepoint), make
            // the buffer bigger and try again.
            if (this.length < this.buffer.length - 4) {
                return this.buffer;
            }
            this.buffer = Buffer.alloc(this.length * 2);
            this.length = this.buffer.write(text);
        }
    }
}
const makeTextEncoder = () => typeof Buffer !== 'undefined' ? new NodeTextEncoder() : new UniversalTextEncoder();
exports.makeTextEncoder = makeTextEncoder;
//# sourceMappingURL=textEncoder.js.map