diff --git a/README.md b/README.md index 2d97d5e..c558c71 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,8 @@ # token-types -A primitive token library used to read and write from a node `Buffer`. -Although it is possible to use this module directly, it is primary designed to be used with [strtok3 tokenizer](https://github.com/Borewit/strtok3). +A primitive token library used to read and write from a `Uint8Array`. +Although it is possible to use this module directly, it is primarily designed to be used with [strtok3 tokenizer](https://github.com/Borewit/strtok3). ## Compatibility @@ -56,7 +56,7 @@ import * as token from 'token-types'; ### Numeric tokens -`node-strtok` supports a wide variety of numerical tokens out of the box: +`token-types` supports a wide variety of numeric tokens out of the box: | Token | Number | Bits | Endianness | |---------------|------------------|------|----------------| @@ -87,19 +87,23 @@ import * as token from 'token-types'; | `Float80_BE`* | IEEE 754 float | 80 | big endian | | `Float80_LE`* | IEEE 754 float | 80 | little endian | -### Other tokens +(*) The tokens exceed the JavaScript IEEE 754 64-bit Floating Point precision, decoding and encoding is best effort based. -String types: +### String tokens + +StringType decoding is implemented using TextDecoder which supports a large number of encodings including but not limited to: + +* UTF-8 (the default) * Windows-1252 * ISO-8859-1 -*) The tokens exceed the JavaScript IEEE 754 64-bit Floating Point precision, decoding and encoding is best effort based. +Check out [the MDN web docs for the TextDecoder](https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder/encoding) for a complete list -### Custom token +### Custom tokens -Complex tokens can be added, which makes very suitable for reading binary files or network messages: +Custom tokens can be added, suitable for reading binary files or network messages: ```js - ExtendedHeader = { + ExtendedHeader = { len: 10, get: (buf, off) => { diff --git a/lib/index.ts b/lib/index.ts index 06ffdff..a2006bd 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -1,6 +1,5 @@ import * as ieee754 from 'ieee754'; -import { IToken, IGetToken } from '@tokenizer/token'; -import { Buffer } from 'node:buffer'; +import type { IToken, IGetToken } from '@tokenizer/token'; // Primitive types @@ -429,26 +428,18 @@ export class Uint8ArrayType implements IGetToken { } } -export class BufferType implements IGetToken { - - public constructor(public len: number) { - } - - public get(uint8Array: Uint8Array, off: number): Buffer { - return Buffer.from(uint8Array.subarray(off, off + this.len)); - } -} - /** * Consume a fixed number of bytes from the stream and return a string with a specified encoding. */ -export class StringType implements IGetToken { +export class StringType implements IGetToken { + private textDecoder: TextDecoder; - public constructor(public len: number, public encoding: BufferEncoding) { + public constructor(public len: number, public encoding: string) { + this.textDecoder = new TextDecoder(encoding); } public get(uint8Array: Uint8Array, offset: number): string { - return Buffer.from(uint8Array).toString(this.encoding, offset, offset + this.len); + return this.textDecoder.decode(uint8Array.subarray(offset, offset + this.len)); } } @@ -457,55 +448,13 @@ export class StringType implements IGetToken { * Using windows-1252 / ISO 8859-1 decoding */ export class AnsiStringType implements IGetToken { - - private static windows1252 = [8364, 129, 8218, 402, 8222, 8230, 8224, 8225, 710, 8240, 352, - 8249, 338, 141, 381, 143, 144, 8216, 8217, 8220, 8221, 8226, 8211, 8212, 732, - 8482, 353, 8250, 339, 157, 382, 376, 160, 161, 162, 163, 164, 165, 166, 167, 168, - 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, - 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, - 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, - 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, - 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, - 248, 249, 250, 251, 252, 253, 254, 255]; - - private static decode(buffer: Uint8Array, offset: number, until: number): string { - let str = ''; - for (let i = offset; i < until; ++i) { - str += AnsiStringType.codePointToString(AnsiStringType.singleByteDecoder(buffer[i])); - } - return str; - } - - private static inRange(a: number, min: number, max: number): boolean { - return min <= a && a <= max; - } - - private static codePointToString(cp: number): string { - if (cp <= 0xFFFF) { - return String.fromCharCode(cp); - } else { - cp -= 0x10000; - return String.fromCharCode((cp >> 10) + 0xD800, (cp & 0x3FF) + 0xDC00); - } - } - - private static singleByteDecoder(bite: number): number { - if (AnsiStringType.inRange(bite, 0x00, 0x7F)) { - return bite; - } - - const codePoint = AnsiStringType.windows1252[bite - 0x80]; - if (codePoint === null) { - throw Error('invaliding encoding'); - } - - return codePoint; - } + private textDecoder: TextDecoder; public constructor(public len: number) { + this.textDecoder = new TextDecoder('windows-1252'); } - public get(buffer: Buffer, offset: number = 0): string { - return AnsiStringType.decode(buffer, offset, offset + this.len); + public get(uint8Array: Uint8Array, offset: number = 0): string { + return this.textDecoder.decode(uint8Array.subarray(offset, offset + this.len)); } } diff --git a/test/test-classes.ts b/test/test-classes.ts index 8a242e2..7a71f27 100644 --- a/test/test-classes.ts +++ b/test/test-classes.ts @@ -1,25 +1,5 @@ import { assert } from 'chai'; -import { Buffer } from 'node:buffer'; -import { BufferType, StringType, Uint8ArrayType } from '../lib/index.js'; - -describe('BufferType', () => { - - it('Should copy data fom the source array', () => { - - const source = Buffer.from([0xa1, 0xa2, 0xb1, 0xb2, 0xc1, 0xc2]); - - const bufferTypeToken = new BufferType(2); - const bufferResult = bufferTypeToken.get(source, 2); - - assert.deepStrictEqual(bufferResult, Buffer.from([0xb1, 0xb2]), 'should be 2 middle bytes: 0xb1, 0xb2'); - - // Overwrite the result - bufferResult[0] = 0xd1; - bufferResult[1] = 0xd2; - - assert.notDeepEqual(source, Buffer.from([0xa1, 0xa2, 0xd1, 0xd2, 0xc1, 0xc2]), 'should copy the data'); - }); -}); +import { StringType, Uint8ArrayType } from '../lib/index.js'; describe('Uint8ArrayType', () => { diff --git a/test/test-ieee-754-floats.ts b/test/test-ieee-754-floats.ts index 02006b9..5319cf2 100644 --- a/test/test-ieee-754-floats.ts +++ b/test/test-ieee-754-floats.ts @@ -1,7 +1,6 @@ // Test writing and reading uint8 values. import { assert } from 'chai'; -import { Buffer } from 'node:buffer'; import * as Token from '../lib/index.js'; import * as util from './util.js'; @@ -13,7 +12,7 @@ describe('IEEE 754 floats', () => { it('should encode', () => { - const buf = Buffer.alloc(2); + const buf = new Uint8Array(2); Token.Float16_BE.put(buf, 0, 0.0); util.checkBuffer(buf, '0000'); @@ -26,7 +25,7 @@ describe('IEEE 754 floats', () => { }); it('should decode', () => { - const buf = Buffer.from('\x55\x52', 'binary'); + const buf = new Uint8Array([0x55, 0x52]); assert.strictEqual(Token.Float16_BE.get(buf, 0), 85.125); }); @@ -36,7 +35,7 @@ describe('IEEE 754 floats', () => { it('should encode', () => { - const buf = Buffer.alloc(2); + const buf = new Uint8Array(2); Token.Float16_LE.put(buf, 0, 0.0); util.checkBuffer(buf, '0000'); @@ -49,7 +48,7 @@ describe('IEEE 754 floats', () => { }); it('should decode', () => { - const buf = Buffer.from('\x52\x55', 'binary'); + const buf = new Uint8Array([0x52, 0x55]); assert.strictEqual(Token.Float16_LE.get(buf, 0), 85.125); }); @@ -62,7 +61,7 @@ describe('IEEE 754 floats', () => { it('should encode', () => { - const buffer = Buffer.alloc(4); + const buffer = new Uint8Array(4); Token.Float32_BE.put(buffer, 0, 0.0); util.checkBuffer(buffer, '00000000'); @@ -75,7 +74,7 @@ describe('IEEE 754 floats', () => { }); it('should decode', () => { - const buf = Buffer.from('\x42\xAA\x40\x00', 'binary'); + const buf = new Uint8Array([0x42, 0xAA, 0x40, 0x00]); assert.strictEqual(Token.Float32_BE.get(buf, 0), 85.125); }); @@ -85,7 +84,7 @@ describe('IEEE 754 floats', () => { it('should encode', () => { - const buf = Buffer.alloc(4); + const buf = new Uint8Array(4); Token.Float32_LE.put(buf, 0, 0.0); util.checkBuffer(buf, '00000000'); @@ -98,7 +97,7 @@ describe('IEEE 754 floats', () => { }); it('should decode', () => { - const buf = Buffer.from('\x00\x40\xAA\x42', 'binary'); + const buf = new Uint8Array([0x00, 0x40, 0xAA, 0x42]); assert.strictEqual(Token.Float32_LE.get(buf, 0), 85.125); }); @@ -111,7 +110,7 @@ describe('IEEE 754 floats', () => { it('should encode', () => { - const buf = Buffer.alloc(8); + const buf = new Uint8Array(8); Token.Float64_BE.put(buf, 0, 0.0); util.checkBuffer(buf, '0000000000000000'); @@ -124,7 +123,7 @@ describe('IEEE 754 floats', () => { }); it('should decode', () => { - const buf = Buffer.from('\x40\x55\x48\x00\x00\x00\x00\x00', 'binary'); + const buf = new Uint8Array([0x40, 0x55, 0x48, 0x00, 0x00, 0x00, 0x00, 0x00]); assert.strictEqual(Token.Float64_BE.get(buf, 0), 85.125); }); @@ -134,7 +133,7 @@ describe('IEEE 754 floats', () => { it('should encode', () => { - const buf = Buffer.alloc(8); + const buf = new Uint8Array(8); Token.Float64_LE.put(buf, 0, 0.0); util.checkBuffer(buf, '0000000000000000'); @@ -147,7 +146,7 @@ describe('IEEE 754 floats', () => { }); it('should decode', () => { - const buf = Buffer.from('\x00\x00\x00\x00\x00\x48\x55\x40', 'binary'); + const buf = new Uint8Array([0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x55, 0x40]); assert.strictEqual(Token.Float64_LE.get(buf, 0), 85.125); }); @@ -160,7 +159,7 @@ describe('IEEE 754 floats', () => { it('should encode', () => { - const buf = Buffer.alloc(10); + const buf = new Uint8Array(10); Token.Float80_BE.put(buf, 0, 0.0); util.checkBuffer(buf, '00000000000000000000'); @@ -173,7 +172,7 @@ describe('IEEE 754 floats', () => { }); it('should decode', () => { - const buf = Buffer.from('\x40\x02\xAA\x40\x00\x00\x00\x00\x00\x00\x00\x00', 'binary'); + const buf = new Uint8Array([0x40, 0x02, 0xAA, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); assert.strictEqual(Token.Float80_BE.get(buf, 0), 85.125); }); @@ -183,7 +182,7 @@ describe('IEEE 754 floats', () => { it('should encode', () => { - const buf = Buffer.alloc(10); + const buf = new Uint8Array(10); Token.Float80_LE.put(buf, 0, 0.0); util.checkBuffer(buf, '00000000000000000000'); @@ -196,7 +195,7 @@ describe('IEEE 754 floats', () => { }); it.skip('should decode', () => { - const buf = Buffer.from('\x00\x00\x00\x00\x00\x00\x00\x00\x40\xAA\x02\x40', 'binary'); + const buf = new Uint8Array([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0xAA, 0x02, 0x40]); assert.strictEqual(Token.Float80_LE.get(buf, 0), 85.125); }); diff --git a/test/test-int16.ts b/test/test-int16.ts index e460ff2..fca6f7d 100644 --- a/test/test-int16.ts +++ b/test/test-int16.ts @@ -1,7 +1,6 @@ // Test reading int16 values. import { assert } from 'chai'; -import { Buffer } from 'node:buffer'; import * as Token from '../lib/index.js'; import * as util from './util.js'; @@ -11,7 +10,7 @@ describe('Parse 16-bit signed integer', () => { it('should encode', () => { - const buf = Buffer.alloc(2); + const buf = new Uint8Array(2); Token.INT16_BE.put(buf, 0, 0x00); util.checkBuffer(buf, '0000'); @@ -25,7 +24,7 @@ describe('Parse 16-bit signed integer', () => { it('should decode', () => { - const buf = Buffer.from('\x0a\x1a\x00\x00\xff\xff\x80\x00', 'binary'); + const buf = new Uint8Array([0x0a, 0x1a, 0x00, 0x00, 0xff, 0xff, 0x80, 0x00]); assert.equal(Token.INT16_BE.get(buf, 0), 2586); assert.equal(Token.INT16_BE.get(buf, 2), 0); @@ -39,7 +38,7 @@ describe('Parse 16-bit signed integer', () => { it('should encode', () => { - const buf = Buffer.alloc(2); + const buf = new Uint8Array(2); Token.INT16_LE.put(buf, 0, 0x00); util.checkBuffer(buf, '0000'); @@ -53,7 +52,7 @@ describe('Parse 16-bit signed integer', () => { it('should decode', () => { - const buf = Buffer.from('\x1a\x0a\x00\x00\xff\xff\x00\x80', 'binary'); + const buf = new Uint8Array([0x1a, 0x0a, 0x00, 0x00, 0xff, 0xff, 0x00, 0x80]); assert.equal(Token.INT16_LE.get(buf, 0), 2586); assert.equal(Token.INT16_LE.get(buf, 2), 0); diff --git a/test/test-int24.ts b/test/test-int24.ts index b6fbb33..2dfcbc0 100644 --- a/test/test-int24.ts +++ b/test/test-int24.ts @@ -1,7 +1,6 @@ // Test reading int24 values. import { assert } from 'chai'; -import { Buffer } from 'node:buffer'; import * as Token from '../lib/index.js'; import * as util from './util.js'; @@ -11,7 +10,7 @@ describe('Parse 24-bit signed integer', () => { it('should encode', () => { - const buf = Buffer.alloc(3); + const buf = new Uint8Array(3); Token.INT24_LE.put(buf, 0, 0x00); util.checkBuffer(buf, '000000'); @@ -25,7 +24,7 @@ describe('Parse 24-bit signed integer', () => { it('should decode', () => { - const buf = Buffer.from('\x00\x00\x00\xff\xff\xff\xff\x00\x10\x00\x00\x80', 'binary'); + const buf = new Uint8Array([0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x10, 0x00, 0x00, 0x80]); assert.equal(Token.INT24_LE.get(buf, 0), 0); assert.equal(Token.INT24_LE.get(buf, 3), -1); @@ -39,7 +38,7 @@ describe('Parse 24-bit signed integer', () => { it('should encode', () => { - const buf = Buffer.alloc(3); + const buf = new Uint8Array(3); Token.INT24_BE.put(buf, 0, 0x00); util.checkBuffer(buf, '000000'); @@ -53,7 +52,7 @@ describe('Parse 24-bit signed integer', () => { it('should decode', () => { - const buf = Buffer.from('\x00\x00\x00\xff\xff\xff\x10\x00\xff\x80\x00\x00', 'binary'); + const buf = new Uint8Array([0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x10, 0x00, 0xff, 0x80, 0x00, 0x00]); assert.equal(Token.INT24_BE.get(buf, 0), 0); assert.equal(Token.INT24_BE.get(buf, 3), -1); diff --git a/test/test-int32.ts b/test/test-int32.ts index 729e9f5..a5f17d9 100644 --- a/test/test-int32.ts +++ b/test/test-int32.ts @@ -1,7 +1,6 @@ // Test reading int32 values. import { assert } from 'chai'; -import { Buffer } from 'node:buffer'; import * as Token from '../lib/index.js'; import * as util from './util.js'; @@ -11,7 +10,7 @@ describe('Parse 32-bit signed integer', () => { it('should encode', () => { - const buf = Buffer.alloc(4); + const buf = new Uint8Array(4); Token.INT32_BE.put(buf, 0, 0x00); util.checkBuffer(buf, '00000000'); @@ -28,11 +27,11 @@ describe('Parse 32-bit signed integer', () => { it('should decode', () => { - let buf = Buffer.from('\x00\x00\x00\x00\xff\xff\xff\xff', 'binary'); + let buf = new Uint8Array([0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff]); assert.equal(Token.INT32_BE.get(buf, 0), 0); assert.equal(Token.INT32_BE.get(buf, 4), -1); - buf = Buffer.from('\x00\x10\x00\xff\x80\x00\x00\x00', 'binary'); + buf = new Uint8Array([0x00, 0x10, 0x00, 0xff, 0x80, 0x00, 0x00, 0x00]); assert.equal(Token.INT32_BE.get(buf, 0), 1048831); assert.equal(Token.INT32_BE.get(buf, 4), -2147483648); @@ -44,7 +43,7 @@ describe('Parse 32-bit signed integer', () => { it('should encode', () => { - const buf = Buffer.alloc(4); + const buf = new Uint8Array(4); Token.INT32_LE.put(buf, 0, 0x00); util.checkBuffer(buf, '00000000'); @@ -61,13 +60,12 @@ describe('Parse 32-bit signed integer', () => { it('should decode', () => { - // const buf = Buffer.from('\x00\x00\x00\x00\xff\xff\xff\xff\x00\x10\x00\xff\x80\x00\x00\x00', 'binary'); - let buf = Buffer.from('\x00\x00\x00\x00\xff\xff\xff\xff', 'binary'); + let buf = new Uint8Array([0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff]); assert.equal(Token.INT32_LE.get(buf, 0), 0); assert.equal(Token.INT32_LE.get(buf, 4), -1); - buf = Buffer.from('\xff\x00\x10\x00\x00\x00\x00\x80', 'binary'); + buf = new Uint8Array([0xff, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x80]); assert.equal(Token.INT32_LE.get(buf, 0), 1048831); assert.equal(Token.INT32_LE.get(buf, 4), -2147483648); diff --git a/test/test-int64.ts b/test/test-int64.ts index f04fab9..849e3f3 100644 --- a/test/test-int64.ts +++ b/test/test-int64.ts @@ -1,7 +1,6 @@ // Test reading int64 values. import { assert } from 'chai'; -import { Buffer } from 'node:buffer'; import * as Token from '../lib/index.js'; import * as util from './util.js'; @@ -11,7 +10,7 @@ describe('Parse 64-bit signed integer', () => { it('should encode', () => { - const buf = Buffer.alloc(8); + const buf = new Uint8Array(8); Token.INT64_BE.put(buf, 0, BigInt(0x01)); util.checkBuffer(buf, '0000000000000001'); @@ -25,8 +24,8 @@ describe('Parse 64-bit signed integer', () => { it('should decode', () => { - assert.strictEqual(Token.INT64_BE.get(Buffer.from('\x00\x00\x00\x00\x00\x00\x00\x00', 'binary'), 0), BigInt(0)); - assert.strictEqual(Token.INT64_BE.get(Buffer.from('\xff\xff\xff\xff\xff\xff\xff\xff', 'binary'), 0), BigInt(-1)); + assert.strictEqual(Token.INT64_BE.get(new Uint8Array([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), 0), BigInt(0)); + assert.strictEqual(Token.INT64_BE.get(new Uint8Array([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), 0), BigInt(-1)); }); @@ -36,7 +35,7 @@ describe('Parse 64-bit signed integer', () => { it('should encode', () => { - const buf = Buffer.alloc(8); + const buf = new Uint8Array(8); Token.INT64_LE.put(buf, 0, BigInt(0x00)); util.checkBuffer(buf, '0000000000000000'); @@ -50,12 +49,12 @@ describe('Parse 64-bit signed integer', () => { it('should decode', () => { - let buf = Buffer.from('\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff', 'binary'); + let buf = new Uint8Array([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]); assert.strictEqual(Token.INT64_LE.get(buf, 0), BigInt(0)); assert.strictEqual(Token.INT64_LE.get(buf, 8), BigInt(-1)); - buf = Buffer.from('\xaa\xcc\xdd\xee\xbb\xff\x00\x00\xbb\xcc\xdd\xee\xbb\xff\x00\x00', 'binary'); + buf = new Uint8Array([0xaa, 0xcc, 0xdd, 0xee, 0xbb, 0xff, 0x00, 0x00, 0xbb, 0xcc, 0xdd, 0xee, 0xbb, 0xff, 0x00, 0x00]); assert.strictEqual(Token.INT64_LE.get(buf, 0), BigInt(0x0000ffbbeeddccaa)); assert.strictEqual(Token.INT64_LE.get(buf, 8), BigInt(0x0000ffbbeeddccbb)); diff --git a/test/test-int8.ts b/test/test-int8.ts index db0bc86..4044289 100644 --- a/test/test-int8.ts +++ b/test/test-int8.ts @@ -1,7 +1,6 @@ // Test reading int8 values. import { assert } from 'chai'; -import { Buffer } from 'node:buffer'; import * as Token from '../lib/index.js'; import * as util from './util.js'; @@ -9,7 +8,7 @@ describe('Parse 8-bit signed integer (INT8)', () => { it('should encode', () => { - const buf = Buffer.alloc(1); + const buf = new Uint8Array(1); Token.INT8.put(buf, 0, 0x00); util.checkBuffer(buf, '00'); @@ -23,7 +22,7 @@ describe('Parse 8-bit signed integer (INT8)', () => { it('should decode', () => { - const buf = Buffer.from('\x00\x7f\x80\xff\x81', 'binary'); + const buf = new Uint8Array([0x00, 0x7f, 0x80, 0xff, 0x81]); assert.strictEqual(Token.INT8.get(buf, 0), 0); assert.strictEqual(Token.INT8.get(buf, 1), 127); diff --git a/test/test-iso-8859-1.ts b/test/test-iso-8859-1.ts index 3929f1a..27ba535 100644 --- a/test/test-iso-8859-1.ts +++ b/test/test-iso-8859-1.ts @@ -1,26 +1,23 @@ import { assert } from 'chai'; -import { Buffer } from 'node:buffer'; import { AnsiStringType } from '../lib/index.js'; /* eslint-disable max-len */ describe('Decode ANSI-string (ISO-8859-1)', () => { - function decode(v: string): string { - const buf = Buffer.from(v, 'binary'); - const ansiStr = new AnsiStringType(buf.length); - return ansiStr.get(buf); + function decode(uint8Array: Uint8Array): string { + const ansiStr = new AnsiStringType(uint8Array.length); + return ansiStr.get(uint8Array); } it('should decode', () => { - assert.equal( - decode('\0\x01\x02\x03\x04\x05\x06\x07\b\t\n\x0B\f\r\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7F'), + decode(new Uint8Array(new Array(128).fill(0).map((_, index) => index))), '\0\x01\x02\x03\x04\x05\x06\x07\b\t\n\x0B\f\r\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7F', 'U+0000 to U+007F remain unchanged' ); assert.equal( - decode('\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF'), + decode(new Uint8Array(new Array(128).fill(0).map((_, index) => index + 128))), '\u20AC\x81\u201A\u0192\u201E\u2026\u2020\u2021\u02C6\u2030\u0160\u2039\u0152\x8D\u017D\x8F\x90\u2018\u2019\u201C\u201D\u2022\u2013\u2014\u02DC\u2122\u0161\u203A\u0153\x9D\u017E\u0178\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF', 'Decoding all other symbols in the character set' ); diff --git a/test/test-uint16.ts b/test/test-uint16.ts index 67e4a5d..cf9b07d 100644 --- a/test/test-uint16.ts +++ b/test/test-uint16.ts @@ -1,7 +1,6 @@ // Test writing and reading uint16 values in different endiannesses. import { assert } from 'chai'; -import { Buffer } from 'node:buffer'; import * as Token from '../lib/index.js'; import * as util from './util.js'; @@ -11,7 +10,7 @@ describe('Parse 16-bit unsigned integer', () => { it('should encode', () => { - const buf = Buffer.alloc(4); + const buf = new Uint8Array(4); Token.UINT16_LE.put(buf, 0, 0x00); Token.UINT16_LE.put(buf, 2, 0xffaa); @@ -28,7 +27,7 @@ describe('Parse 16-bit unsigned integer', () => { it('should decode', () => { - const buf = Buffer.from('\x1a\x00\x1a\x00\x1a\x00\x1a\x00', 'binary'); + const buf = new Uint8Array([0x1a, 0x00, 0x1a, 0x00, 0x1a, 0x00, 0x1a, 0x00]); assert.equal(Token.UINT16_LE.get(buf, 0), 0x001a); assert.equal(Token.UINT16_BE.get(buf, 2), 0x1a00); diff --git a/test/test-uint24.ts b/test/test-uint24.ts index 995abcd..4c32a67 100644 --- a/test/test-uint24.ts +++ b/test/test-uint24.ts @@ -1,7 +1,6 @@ // Test writing and reading uint24 values in different endiannesses. import { assert } from 'chai'; -import { Buffer } from 'node:buffer'; import * as Token from '../lib/index.js'; import * as util from './util.js'; @@ -11,7 +10,7 @@ describe('Parse 24-bit unsigned integer', () => { it('should encode', () => { - const buf = Buffer.alloc(3); + const buf = new Uint8Array(3); Token.UINT24_BE.put(buf, 0, 0x00); util.checkBuffer(buf, '000000'); @@ -25,7 +24,7 @@ describe('Parse 24-bit unsigned integer', () => { it('should decode', () => { - const buf = Buffer.from('\x00\x00\x00\x1a\x1a\x00\xff\xff\xff', 'binary'); + const buf = new Uint8Array([0x00, 0x00, 0x00, 0x1a, 0x1a, 0x00, 0xff, 0xff, 0xff]); assert.strictEqual(Token.UINT24_BE.get(buf, 0), 0x000000); assert.strictEqual(Token.UINT24_BE.get(buf, 3), 0x1a1a00); assert.strictEqual(Token.UINT24_BE.get(buf, 6), 0xffffff); @@ -37,7 +36,7 @@ describe('Parse 24-bit unsigned integer', () => { it('should encode', () => { - const buf = Buffer.alloc(3); + const buf = new Uint8Array(3); Token.UINT24_LE.put(buf, 0, 0x00); util.checkBuffer(buf, '000000'); @@ -51,7 +50,7 @@ describe('Parse 24-bit unsigned integer', () => { it('should decode', () => { - const buf = Buffer.from('\x00\x00\x00\x1a\x1a\x00\xff\xff\xff', 'binary'); + const buf = new Uint8Array([0x00, 0x00, 0x00, 0x1a, 0x1a, 0x00, 0xff, 0xff, 0xff]); assert.strictEqual(Token.UINT24_LE.get(buf, 0), 0x000000); assert.strictEqual(Token.UINT24_LE.get(buf, 3), 0x001a1a); diff --git a/test/test-uint32.ts b/test/test-uint32.ts index 303f208..d5efe73 100644 --- a/test/test-uint32.ts +++ b/test/test-uint32.ts @@ -1,18 +1,17 @@ // Test writing and reading uint32 values in different endiannesses. import { assert } from 'chai'; -import { Buffer } from 'node:buffer'; import * as Token from '../lib/index.js'; import * as util from './util.js'; describe('Parse 32-bit unsigned integer', () => { - const decbuf = Buffer.from('\x1a\x00\x1a\x00\x1a\x00\x1a\x00\x1a\x00\x1a\x00\x1a\x00\x1a\x00', 'binary'); + const decbuf = new Uint8Array([0x1a, 0x00, 0x1a, 0x00, 0x1a, 0x00, 0x1a, 0x00, 0x1a, 0x00, 0x1a, 0x00, 0x1a, 0x00, 0x1a, 0x00]); describe('big-endian', () => { it('should encode', () => { - const buf = Buffer.alloc(4); + const buf = new Uint8Array(4); Token.UINT32_BE.put(buf, 0, 0x00); util.checkBuffer(buf, '00000000'); @@ -33,7 +32,7 @@ describe('Parse 32-bit unsigned integer', () => { describe('little-endian', () => { it('should encode', () => { - const buf = Buffer.alloc(4); + const buf = new Uint8Array(4); Token.UINT32_LE.put(buf, 0, 0x00); util.checkBuffer(buf, '00000000'); diff --git a/test/test-uint64.ts b/test/test-uint64.ts index 535743b..7665ef6 100644 --- a/test/test-uint64.ts +++ b/test/test-uint64.ts @@ -1,7 +1,6 @@ // Test writing and reading uint32 values in different endiannesses. import { assert } from 'chai'; -import { Buffer } from 'node:buffer'; import * as Token from '../lib/index.js'; import * as util from './util.js'; @@ -11,7 +10,7 @@ describe('Parse 64-bit unsigned integer', () => { it('should encode', () => { - const buf = Buffer.alloc(8); + const buf = new Uint8Array(8); Token.UINT64_BE.put(buf, 0, BigInt(0x00)); util.checkBuffer(buf, '0000000000000000'); @@ -27,7 +26,7 @@ describe('Parse 64-bit unsigned integer', () => { }); it('should decode', () => { - const buf = Buffer.from('\x00\x00\x1a\x00\x1a\x00\x1a\x01\x00\x00\x1a\x00\x1a\x00\x1a\x02', 'binary'); + const buf = new Uint8Array([0x00, 0x00, 0x1a, 0x00, 0x1a, 0x00, 0x1a, 0x01, 0x00, 0x00, 0x1a, 0x00, 0x1a, 0x00, 0x1a, 0x02]); assert.strictEqual(Token.UINT64_BE.get(buf, 0), BigInt(0x00001a001a001a01)); assert.strictEqual(Token.UINT64_BE.get(buf, 8), BigInt(0x00001a001a001a02)); @@ -38,7 +37,7 @@ describe('Parse 64-bit unsigned integer', () => { describe('litle-endian', () => { it('should encode', () => { - const buf = Buffer.alloc(8); + const buf = new Uint8Array(8); Token.UINT64_LE.put(buf, 0, BigInt(0x00)); util.checkBuffer(buf, '0000000000000000'); @@ -55,7 +54,7 @@ describe('Parse 64-bit unsigned integer', () => { it('should decode', () => { - const buf = Buffer.from('\x1a\x00\x1a\x00\x1a\x00\x1a\x00\x1a\x00\x1a\x00\x1a\x00\x1a\x00', 'binary'); + const buf = new Uint8Array([0x1a, 0x00, 0x1a, 0x00, 0x1a, 0x00, 0x1a, 0x00, 0x1a, 0x00, 0x1a, 0x00, 0x1a, 0x00, 0x1a, 0x00]); it('little-endian', () => { assert.strictEqual(Token.UINT64_LE.get(buf, 0), BigInt(0x001a001a)); diff --git a/test/test-uint8.ts b/test/test-uint8.ts index 3a54505..0bcf479 100644 --- a/test/test-uint8.ts +++ b/test/test-uint8.ts @@ -1,7 +1,6 @@ // Test writing and reading uint8 values. import { assert } from 'chai'; -import { Buffer } from 'node:buffer'; import * as Token from '../lib/index.js'; import * as util from './util.js'; @@ -9,7 +8,7 @@ describe('Parse 8-bit unsigned integer (UINT8)', () => { it('should encode', () => { - const buf = Buffer.alloc(1); + const buf = new Uint8Array(1); Token.UINT8.put(buf, 0, 0x00); util.checkBuffer(buf, '00'); @@ -23,7 +22,7 @@ describe('Parse 8-bit unsigned integer (UINT8)', () => { it('should decode', () => { - const buf = Buffer.from('\x00\x1a\x01\xff', 'binary'); + const buf = new Uint8Array([0x00, 0x1a, 0x01, 0xff]); assert.equal(Token.UINT8.get(buf, 0), 0); assert.equal(Token.UINT8.get(buf, 1), 26); diff --git a/tsconfig.json b/tsconfig.json index 39ef659..731d5f6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,6 @@ { "compilerOptions": { + "skipLibCheck": true, "inlineSources": false, "module": "node16", "moduleResolution": "node16", @@ -8,4 +9,3 @@ "strict": true } } -