From f83bcafd461c9c8398449a25995e8dbe8d025ca1 Mon Sep 17 00:00:00 2001 From: yue Date: Wed, 24 Jul 2024 19:16:37 +0900 Subject: [PATCH] fix: remove unnecessary Buffer.from usage --- src/util.ts | 10 ++-------- test/test.ts | 17 ++--------------- 2 files changed, 4 insertions(+), 23 deletions(-) diff --git a/src/util.ts b/src/util.ts index d85de90..90203bf 100644 --- a/src/util.ts +++ b/src/util.ts @@ -47,18 +47,12 @@ import type { KTX2Container } from './container.js'; /** Encodes text to an ArrayBuffer. */ export function encodeText(text: string): Uint8Array { - if (typeof TextEncoder !== 'undefined') { - return new TextEncoder().encode(text); - } - return Buffer.from(text); + return new TextEncoder().encode(text); } /** Decodes an ArrayBuffer to text. */ export function decodeText(buffer: Uint8Array): string { - if (typeof TextDecoder !== 'undefined') { - return new TextDecoder().decode(buffer); - } - return Buffer.from(buffer).toString('utf8'); + return new TextDecoder().decode(buffer); } /** Concatenates N ArrayBuffers. */ diff --git a/test/test.ts b/test/test.ts index c36d9e5..d8599c5 100644 --- a/test/test.ts +++ b/test/test.ts @@ -2,7 +2,6 @@ import { URL } from 'url'; import { readFileSync } from 'fs'; import { readFile } from 'fs/promises'; import { basename, join } from 'path'; -import { TextDecoder, TextEncoder } from 'util'; import test from 'ava'; import { glob } from 'glob'; import { KTX2Container, read, write } from 'ktx-parse'; @@ -189,20 +188,8 @@ test('write::uastc', (t) => { }); test('platform::web', (t) => { - // Emulate browser API. - global.TextEncoder = TextEncoder as any; - global.TextDecoder = TextDecoder as any; - const _from = Buffer.from; - Buffer.from = (() => { - throw new Error('Should not be called.'); - }) as any; - - try { - const result = write(read(SAMPLE_UASTC)); - t.true(result instanceof Uint8Array, 'success'); - } finally { - Buffer.from = _from; - } + const result = write(read(SAMPLE_UASTC)); + t.true(result instanceof Uint8Array, 'success'); }); test('data format descriptors', (t) => {