token-types
Advanced tools
Comparing version 4.0.0 to 4.0.1
@@ -116,8 +116,8 @@ /// <reference types="node" /> | ||
constructor(len: number); | ||
get(buffer: Buffer, offset: number): void; | ||
get(array: Uint8Array, off: number): void; | ||
} | ||
export declare class BufferType implements IGetToken<Buffer> { | ||
export declare class Uint8ArrayType implements IGetToken<Uint8Array> { | ||
len: number; | ||
constructor(len: number); | ||
get(buffer: Buffer, offset: number): Buffer; | ||
get(array: Uint8Array, off: number): Uint8Array; | ||
} | ||
@@ -127,3 +127,3 @@ /** | ||
*/ | ||
export declare class StringType implements IGetToken<string> { | ||
export declare class StringType implements IGetToken<string, Buffer> { | ||
len: number; | ||
@@ -130,0 +130,0 @@ encoding: BufferEncoding; |
257
lib/index.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.AnsiStringType = exports.StringType = exports.BufferType = exports.IgnoreType = exports.Float80_LE = exports.Float80_BE = exports.Float64_LE = exports.Float64_BE = exports.Float32_LE = exports.Float32_BE = exports.Float16_LE = exports.Float16_BE = exports.INT64_BE = exports.UINT64_BE = exports.INT64_LE = exports.UINT64_LE = exports.INT32_LE = exports.INT32_BE = exports.INT24_BE = exports.INT24_LE = exports.INT16_LE = exports.INT16_BE = exports.INT8 = exports.UINT32_BE = exports.UINT32_LE = exports.UINT24_BE = exports.UINT24_LE = exports.UINT16_BE = exports.UINT16_LE = exports.UINT8 = void 0; | ||
exports.AnsiStringType = exports.StringType = exports.Uint8ArrayType = exports.IgnoreType = exports.Float80_LE = exports.Float80_BE = exports.Float64_LE = exports.Float64_BE = exports.Float32_LE = exports.Float32_BE = exports.Float16_LE = exports.Float16_BE = exports.INT64_BE = exports.UINT64_BE = exports.INT64_LE = exports.UINT64_LE = exports.INT32_LE = exports.INT32_BE = exports.INT24_BE = exports.INT24_LE = exports.INT16_LE = exports.INT16_BE = exports.INT8 = exports.UINT32_BE = exports.UINT32_LE = exports.UINT24_BE = exports.UINT24_LE = exports.UINT16_BE = exports.UINT16_LE = exports.UINT8 = void 0; | ||
const ieee754 = require("ieee754"); | ||
// Primitive types | ||
function dv(array) { | ||
return new DataView(array.buffer, array.byteOffset); | ||
} | ||
/** | ||
@@ -11,7 +14,8 @@ * 8-bit unsigned integer | ||
len: 1, | ||
get(buffer, offset) { | ||
return buffer.readUInt8(offset); | ||
get(array, offset) { | ||
return dv(array).getUint8(offset); | ||
}, | ||
put(buffer, offset, v) { | ||
return buffer.writeUInt8(v, offset); | ||
put(array, offset, value) { | ||
dv(array).setUint8(offset, value); | ||
return offset + 1; | ||
} | ||
@@ -24,7 +28,8 @@ }; | ||
len: 2, | ||
get(buffer, offset) { | ||
return buffer.readUInt16LE(offset); | ||
get(array, offset) { | ||
return dv(array).getUint16(offset, true); | ||
}, | ||
put(buffer, offset, v) { | ||
return buffer.writeUInt16LE(v, offset); | ||
put(array, offset, value) { | ||
dv(array).setUint16(offset, value, true); | ||
return offset + 2; | ||
} | ||
@@ -37,7 +42,8 @@ }; | ||
len: 2, | ||
get(buffer, offset) { | ||
return buffer.readUInt16BE(offset); | ||
get(array, offset) { | ||
return dv(array).getUint16(offset); | ||
}, | ||
put(buffer, offset, v) { | ||
return buffer.writeUInt16BE(v, offset); | ||
put(array, offset, value) { | ||
dv(array).setUint16(offset, value); | ||
return offset + 2; | ||
} | ||
@@ -50,7 +56,11 @@ }; | ||
len: 3, | ||
get(buffer, offset) { | ||
return buffer.readUIntLE(offset, 3); | ||
get(array, offset) { | ||
const dataView = dv(array); | ||
return dataView.getUint8(offset) + (dataView.getUint16(offset + 1, true) << 8); | ||
}, | ||
put(buffer, offset, v) { | ||
return buffer.writeUIntLE(v, offset, 3); | ||
put(array, offset, value) { | ||
const dataView = dv(array); | ||
dataView.setUint8(offset, value & 0xff); | ||
dataView.setUint16(offset + 1, value >> 8, true); | ||
return offset + 3; | ||
} | ||
@@ -63,7 +73,11 @@ }; | ||
len: 3, | ||
get(buffer, offset) { | ||
return buffer.readUIntBE(offset, 3); | ||
get(array, offset) { | ||
const dataView = dv(array); | ||
return (dataView.getUint16(offset) << 8) + dataView.getUint8(offset + 2); | ||
}, | ||
put(buffer, offset, v) { | ||
return buffer.writeUIntBE(v, offset, 3); | ||
put(array, offset, value) { | ||
const dataView = dv(array); | ||
dataView.setUint16(offset, value >> 8); | ||
dataView.setUint8(offset + 2, value & 0xff); | ||
return offset + 3; | ||
} | ||
@@ -76,7 +90,8 @@ }; | ||
len: 4, | ||
get(buffer, offset) { | ||
return buffer.readUInt32LE(offset); | ||
get(array, offset) { | ||
return dv(array).getUint32(offset, true); | ||
}, | ||
put(b, o, v) { | ||
return b.writeUInt32LE(v, o); | ||
put(array, offset, value) { | ||
dv(array).setUint32(offset, value, true); | ||
return offset + 4; | ||
} | ||
@@ -89,7 +104,8 @@ }; | ||
len: 4, | ||
get(buffer, offset) { | ||
return buffer.readUInt32BE(offset); | ||
get(array, offset) { | ||
return dv(array).getUint32(offset); | ||
}, | ||
put(buffer, offset, v) { | ||
return buffer.writeUInt32BE(v, offset); | ||
put(array, offset, value) { | ||
dv(array).setUint32(offset, value); | ||
return offset + 4; | ||
} | ||
@@ -102,7 +118,8 @@ }; | ||
len: 1, | ||
get(buffer, offset) { | ||
return buffer.readInt8(offset); | ||
get(array, offset) { | ||
return dv(array).getInt8(offset); | ||
}, | ||
put(buffer, offset, v) { | ||
return buffer.writeInt8(v, offset); | ||
put(array, offset, value) { | ||
dv(array).setInt8(offset, value); | ||
return offset + 2; | ||
} | ||
@@ -115,7 +132,8 @@ }; | ||
len: 2, | ||
get(buffer, offset) { | ||
return buffer.readInt16BE(offset); | ||
get(array, offset) { | ||
return dv(array).getInt16(offset); | ||
}, | ||
put(b, o, v) { | ||
return b.writeInt16BE(v, o); | ||
put(array, offset, value) { | ||
dv(array).setInt16(offset, value); | ||
return offset + 2; | ||
} | ||
@@ -128,7 +146,8 @@ }; | ||
len: 2, | ||
get(buffer, offset) { | ||
return buffer.readInt16LE(offset); | ||
get(array, offset) { | ||
return dv(array).getInt16(offset, true); | ||
}, | ||
put(b, o, v) { | ||
return b.writeInt16LE(v, o); | ||
put(array, offset, value) { | ||
dv(array).setInt16(offset, value, true); | ||
return offset + 2; | ||
} | ||
@@ -141,7 +160,11 @@ }; | ||
len: 3, | ||
get(buffer, offset) { | ||
return buffer.readIntLE(offset, 3); | ||
get(array, offset) { | ||
const unsigned = exports.UINT24_LE.get(array, offset); | ||
return unsigned > 0x7fffff ? unsigned - 0x1000000 : unsigned; | ||
}, | ||
put(b, o, v) { | ||
return b.writeIntLE(v, o, 3); | ||
put(array, offset, value) { | ||
const dataView = dv(array); | ||
dataView.setUint8(offset, value & 0xff); | ||
dataView.setUint16(offset + 1, value >> 8, true); | ||
return offset + 3; | ||
} | ||
@@ -154,7 +177,11 @@ }; | ||
len: 3, | ||
get(buffer, offset) { | ||
return buffer.readIntBE(offset, 3); | ||
get(array, offset) { | ||
const unsigned = exports.UINT24_BE.get(array, offset); | ||
return unsigned > 0x7fffff ? unsigned - 0x1000000 : unsigned; | ||
}, | ||
put(b, o, v) { | ||
return b.writeIntBE(v, o, 3); | ||
put(array, offset, value) { | ||
const dataView = dv(array); | ||
dataView.setUint16(offset, value >> 8); | ||
dataView.setUint8(offset + 2, value & 0xff); | ||
return offset + 3; | ||
} | ||
@@ -167,7 +194,8 @@ }; | ||
len: 4, | ||
get(buffer, offset) { | ||
return buffer.readInt32BE(offset); | ||
get(array, offset) { | ||
return dv(array).getInt32(offset); | ||
}, | ||
put(b, o, v) { | ||
return b.writeInt32BE(v, o); | ||
put(array, offset, value) { | ||
dv(array).setInt32(offset, value); | ||
return offset + 4; | ||
} | ||
@@ -180,7 +208,8 @@ }; | ||
len: 4, | ||
get(buffer, offset) { | ||
return buffer.readInt32LE(offset); | ||
get(array, offset) { | ||
return dv(array).getInt32(offset, true); | ||
}, | ||
put(b, o, v) { | ||
return b.writeInt32LE(v, o); | ||
put(array, offset, value) { | ||
dv(array).setInt32(offset, value, true); | ||
return offset + 4; | ||
} | ||
@@ -193,7 +222,8 @@ }; | ||
len: 8, | ||
get(buffer, offset) { | ||
return buffer.readBigUInt64LE(offset); | ||
get(array, offset) { | ||
return dv(array).getBigUint64(offset, true); | ||
}, | ||
put(buffer, offset, value) { | ||
return buffer.writeBigUInt64LE(value, offset); | ||
put(array, offset, value) { | ||
dv(array).setBigUint64(offset, value, true); | ||
return offset + 8; | ||
} | ||
@@ -206,7 +236,8 @@ }; | ||
len: 8, | ||
get(buffer, offset) { | ||
return buffer.readBigInt64LE(offset); | ||
get(array, offset) { | ||
return dv(array).getBigInt64(offset, true); | ||
}, | ||
put(buffer, offset, value) { | ||
return buffer.writeBigInt64LE(value, offset); | ||
put(array, offset, value) { | ||
dv(array).setBigInt64(offset, value, true); | ||
return offset + 8; | ||
} | ||
@@ -219,7 +250,8 @@ }; | ||
len: 8, | ||
get(buffer, offset) { | ||
return buffer.readBigUInt64BE(offset); | ||
get(array, offset) { | ||
return dv(array).getBigUint64(offset); | ||
}, | ||
put(buffer, offset, value) { | ||
return buffer.writeBigUInt64BE(value, offset); | ||
put(array, offset, value) { | ||
dv(array).setBigUint64(offset, value); | ||
return offset + 8; | ||
} | ||
@@ -232,7 +264,8 @@ }; | ||
len: 8, | ||
get(buffer, offset) { | ||
return buffer.readBigInt64BE(offset); | ||
get(array, offset) { | ||
return dv(array).getBigInt64(offset); | ||
}, | ||
put(buffer, offset, value) { | ||
return buffer.writeBigInt64BE(value, offset); | ||
put(array, offset, value) { | ||
dv(array).setBigInt64(offset, value); | ||
return offset + 8; | ||
} | ||
@@ -245,7 +278,7 @@ }; | ||
len: 2, | ||
get(b, offset) { | ||
return ieee754.read(b, offset, false, 10, this.len); | ||
get(dataView, offset) { | ||
return ieee754.read(dataView, offset, false, 10, this.len); | ||
}, | ||
put(b, offset, v) { | ||
ieee754.write(b, v, offset, false, 10, this.len); | ||
put(dataView, offset, value) { | ||
ieee754.write(dataView, value, offset, false, 10, this.len); | ||
return offset + this.len; | ||
@@ -259,7 +292,7 @@ } | ||
len: 2, | ||
get(buffer, offset) { | ||
return ieee754.read(buffer, offset, true, 10, this.len); | ||
get(array, offset) { | ||
return ieee754.read(array, offset, true, 10, this.len); | ||
}, | ||
put(buffer, offset, v) { | ||
ieee754.write(buffer, v, offset, true, 10, this.len); | ||
put(array, offset, value) { | ||
ieee754.write(array, value, offset, true, 10, this.len); | ||
return offset + this.len; | ||
@@ -273,7 +306,8 @@ } | ||
len: 4, | ||
get(buffer, offset) { | ||
return buffer.readFloatBE(offset); | ||
get(array, offset) { | ||
return dv(array).getFloat32(offset); | ||
}, | ||
put(buffer, offset, v) { | ||
return buffer.writeFloatBE(v, offset); | ||
put(array, offset, value) { | ||
dv(array).setFloat32(offset, value); | ||
return offset + 4; | ||
} | ||
@@ -286,7 +320,8 @@ }; | ||
len: 4, | ||
get(buffer, offset) { | ||
return buffer.readFloatLE(offset); | ||
get(array, offset) { | ||
return dv(array).getFloat32(offset, true); | ||
}, | ||
put(bufferb, offset, v) { | ||
return bufferb.writeFloatLE(v, offset); | ||
put(array, offset, value) { | ||
dv(array).setFloat32(offset, value, true); | ||
return offset + 4; | ||
} | ||
@@ -299,7 +334,8 @@ }; | ||
len: 8, | ||
get(buffer, offset) { | ||
return buffer.readDoubleBE(offset); | ||
get(array, offset) { | ||
return dv(array).getFloat64(offset); | ||
}, | ||
put(buffer, offset, v) { | ||
return buffer.writeDoubleBE(v, offset); | ||
put(array, offset, value) { | ||
dv(array).setFloat64(offset, value); | ||
return offset + 8; | ||
} | ||
@@ -312,7 +348,8 @@ }; | ||
len: 8, | ||
get(buffer, offset) { | ||
return buffer.readDoubleLE(offset); | ||
get(array, offset) { | ||
return dv(array).getFloat64(offset, true); | ||
}, | ||
put(buffer, offset, v) { | ||
return buffer.writeDoubleLE(v, offset); | ||
put(array, offset, value) { | ||
dv(array).setFloat64(offset, value, true); | ||
return offset + 8; | ||
} | ||
@@ -325,7 +362,7 @@ }; | ||
len: 10, | ||
get(buffer, offset) { | ||
return ieee754.read(buffer, offset, false, 63, this.len); | ||
get(array, offset) { | ||
return ieee754.read(array, offset, false, 63, this.len); | ||
}, | ||
put(buffer, offset, v) { | ||
ieee754.write(buffer, v, offset, false, 63, this.len); | ||
put(array, offset, value) { | ||
ieee754.write(array, value, offset, false, 63, this.len); | ||
return offset + this.len; | ||
@@ -339,7 +376,7 @@ } | ||
len: 10, | ||
get(buffer, offset) { | ||
return ieee754.read(buffer, offset, true, 63, this.len); | ||
get(array, offset) { | ||
return ieee754.read(array, offset, true, 63, this.len); | ||
}, | ||
put(buffer, offset, v) { | ||
ieee754.write(buffer, v, offset, true, 63, this.len); | ||
put(array, offset, value) { | ||
ieee754.write(array, value, offset, true, 63, this.len); | ||
return offset + this.len; | ||
@@ -359,15 +396,15 @@ } | ||
// ToDo: don't read, but skip data | ||
get(buffer, offset) { | ||
get(array, off) { | ||
} | ||
} | ||
exports.IgnoreType = IgnoreType; | ||
class BufferType { | ||
class Uint8ArrayType { | ||
constructor(len) { | ||
this.len = len; | ||
} | ||
get(buffer, offset) { | ||
return buffer.slice(offset, offset + this.len); | ||
get(array, off) { | ||
return Uint8Array.prototype.slice(off, off + this.len); | ||
} | ||
} | ||
exports.BufferType = BufferType; | ||
exports.Uint8ArrayType = Uint8ArrayType; | ||
/** | ||
@@ -374,0 +411,0 @@ * Consume a fixed number of bytes from the stream and return a string with a specified encoding. |
{ | ||
"name": "token-types", | ||
"version": "4.0.0", | ||
"version": "4.0.1", | ||
"description": "Common token types for decoding and encoding numeric and string values", | ||
@@ -44,2 +44,3 @@ "author": { | ||
"devDependencies": { | ||
"@tokenizer/token": "^0.3.0", | ||
"@types/chai": "^4.2.21", | ||
@@ -61,3 +62,2 @@ "@types/mocha": "^8.2.3", | ||
"dependencies": { | ||
"@tokenizer/token": "^0.1.1", | ||
"ieee754": "^1.2.1" | ||
@@ -64,0 +64,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
24944
1
593
0
15
- Removed@tokenizer/token@^0.1.1
- Removed@tokenizer/token@0.1.1(transitive)