UNPKG

@neurosity/sdk

Version:
41 lines (40 loc) 1.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TextCodec = void 0; const index_js_1 = require("buffer/index.js"); // not including /index.js causes typescript to uses Node's native Buffer built-in and we want to use this npm package for both node and the browser const types_1 = require("../types"); /** * @hidden */ class TextCodec { constructor(transportType) { this.transportType = transportType; if (transportType === types_1.TRANSPORT_TYPE.WEB) { this.webEncoder = new TextEncoder(); this.webDecoder = new TextDecoder("utf-8"); } } encode(data) { if (this.transportType === types_1.TRANSPORT_TYPE.WEB) { const encoded = this.webEncoder.encode(data); return encoded; } if (this.transportType === types_1.TRANSPORT_TYPE.REACT_NATIVE) { // React Native BLE Manager expects a number[] instead of a Uint8Array const encoded = [...index_js_1.Buffer.from(data)]; return encoded; } const encoded = index_js_1.Buffer.from(data); return encoded; } decode(arrayBuffer) { if (this.transportType === types_1.TRANSPORT_TYPE.WEB) { const decoded = this.webDecoder.decode(arrayBuffer); return decoded; } // For React Native, and as a default const decoded = index_js_1.Buffer.from(arrayBuffer).toString("utf-8"); return decoded; } } exports.TextCodec = TextCodec;