@amodx/binary 0.0.0 → 0.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/Arrays/BitArray.d.ts +13 -0
  2. package/Arrays/BitArray.js +63 -0
  3. package/Arrays/HalfNibbleArray.d.ts +13 -0
  4. package/Arrays/HalfNibbleArray.js +63 -0
  5. package/Arrays/NibbleArray.d.ts +13 -0
  6. package/Arrays/NibbleArray.js +63 -0
  7. package/Object/BinaryObject.d.ts +2 -2
  8. package/Object/Functions/BufferToObject.d.ts +3 -3
  9. package/Object/Functions/BufferToObject.js +2 -2
  10. package/Object/Functions/ObjectToTypedNodes.js +2 -1
  11. package/Struct/Classes/BinaryStruct.d.ts +4 -5
  12. package/Struct/Classes/BinaryStruct.js +11 -158
  13. package/Struct/Classes/BinraryStructBase.d.ts +3 -4
  14. package/Struct/Classes/BinraryStructBase.js +30 -144
  15. package/Struct/Classes/InstantiatedStruct.d.ts +7 -3
  16. package/Struct/Classes/InstantiatedStruct.js +18 -6
  17. package/Struct/Classes/RemoteBinaryStruct.d.ts +2 -3
  18. package/Struct/Classes/RemoteBinaryStruct.js +2 -3
  19. package/Struct/Constants/StructPropertyTypes.d.ts +4 -1
  20. package/Struct/Constants/StructPropertyTypes.js +3 -0
  21. package/Struct/Functions/CreateIndex.d.ts +3 -0
  22. package/Struct/Functions/CreateIndex.js +217 -0
  23. package/Struct/Functions/CreateInstance.d.ts +3 -0
  24. package/Struct/Functions/CreateInstance.js +323 -0
  25. package/Struct/Functions/GetIndexData.d.ts +2 -0
  26. package/Struct/Functions/GetIndexData.js +29 -0
  27. package/Struct/Types/{RemoveBinaryStructData.types.d.ts → BinaryStructData.types.d.ts} +4 -3
  28. package/Struct/Types/BinaryStructSchema.types.d.ts +37 -1
  29. package/Struct/Types/index.d.ts +1 -1
  30. package/Struct/Types/index.js +1 -1
  31. package/Util/BinaryArrays.d.ts +8 -0
  32. package/Util/BinaryArrays.js +39 -0
  33. package/Util/BinaryUtil.d.ts +2 -4
  34. package/Util/BinaryUtil.js +4 -18
  35. package/package.json +26 -26
  36. /package/Struct/Types/{RemoveBinaryStructData.types.js → BinaryStructData.types.js} +0 -0
@@ -0,0 +1,13 @@
1
+ export interface BitArray {
2
+ [index: number]: number;
3
+ }
4
+ export declare class BitArray {
5
+ buffer: ArrayBufferLike;
6
+ private data;
7
+ constructor(buffer: ArrayBufferLike);
8
+ get length(): number;
9
+ get(index: number): number;
10
+ set(index: number, value: number): void;
11
+ [Symbol.iterator](): Iterator<number>;
12
+ forEach(callback: (value: number, index: number, array: BitArray) => void): void;
13
+ }
@@ -0,0 +1,63 @@
1
+ import { BinaryArrays } from "../Util/BinaryArrays";
2
+ export class BitArray {
3
+ buffer;
4
+ data;
5
+ constructor(buffer) {
6
+ this.buffer = buffer;
7
+ this.data = new DataView(ArrayBuffer.isView(buffer) ? buffer.buffer : buffer);
8
+ return new Proxy(this, {
9
+ get: (target, property) => {
10
+ if (typeof property === "string" && !isNaN(Number(property))) {
11
+ const index = Number(property);
12
+ return target.get(index);
13
+ }
14
+ return target[property];
15
+ },
16
+ set: (target, property, value) => {
17
+ if (typeof property === "string" && !isNaN(Number(property))) {
18
+ const index = Number(property);
19
+ target.set(index, value);
20
+ return true;
21
+ }
22
+ target[property] = value;
23
+ return true;
24
+ },
25
+ });
26
+ }
27
+ get length() {
28
+ return this.buffer.byteLength * 8;
29
+ }
30
+ get(index) {
31
+ if (index < 0 || index >= this.length) {
32
+ throw new RangeError(`Index ${index} is out of bounds`);
33
+ }
34
+ return BinaryArrays.getBitArrayIndex(this.data, 0, index);
35
+ }
36
+ set(index, value) {
37
+ if (index < 0 || index >= this.length) {
38
+ throw new RangeError(`Index ${index} is out of bounds`);
39
+ }
40
+ if (value < 0 || value > 1) {
41
+ throw new RangeError(`Value ${value} is out of bounds for a bit`);
42
+ }
43
+ BinaryArrays.setBitArrayIndex(this.data, 0, index, value);
44
+ }
45
+ [Symbol.iterator]() {
46
+ let index = 0;
47
+ return {
48
+ next: () => {
49
+ if (index < this.length) {
50
+ return { value: this.get(index++), done: false };
51
+ }
52
+ else {
53
+ return { value: undefined, done: true };
54
+ }
55
+ },
56
+ };
57
+ }
58
+ forEach(callback) {
59
+ for (let i = 0; i < this.length; i++) {
60
+ callback(this.get(i), i, this);
61
+ }
62
+ }
63
+ }
@@ -0,0 +1,13 @@
1
+ export interface HalfNibbleArray {
2
+ [index: number]: number;
3
+ }
4
+ export declare class HalfNibbleArray {
5
+ buffer: ArrayBufferLike;
6
+ private data;
7
+ constructor(buffer: ArrayBufferLike);
8
+ get length(): number;
9
+ get(index: number): number;
10
+ set(index: number, value: number): void;
11
+ [Symbol.iterator](): Iterator<number>;
12
+ forEach(callback: (value: number, index: number, array: HalfNibbleArray) => void): void;
13
+ }
@@ -0,0 +1,63 @@
1
+ import { BinaryArrays } from "../Util/BinaryArrays";
2
+ export class HalfNibbleArray {
3
+ buffer;
4
+ data;
5
+ constructor(buffer) {
6
+ this.buffer = buffer;
7
+ this.data = new DataView(ArrayBuffer.isView(buffer) ? buffer.buffer : buffer);
8
+ return new Proxy(this, {
9
+ get: (target, property) => {
10
+ if (typeof property === "string" && !isNaN(Number(property))) {
11
+ const index = Number(property);
12
+ return target.get(index);
13
+ }
14
+ return target[property];
15
+ },
16
+ set: (target, property, value) => {
17
+ if (typeof property === "string" && !isNaN(Number(property))) {
18
+ const index = Number(property);
19
+ target.set(index, value);
20
+ return true;
21
+ }
22
+ target[property] = value;
23
+ return true;
24
+ },
25
+ });
26
+ }
27
+ get length() {
28
+ return this.buffer.byteLength * 4;
29
+ }
30
+ get(index) {
31
+ if (index < 0 || index >= this.length) {
32
+ throw new RangeError(`Index ${index} is out of bounds`);
33
+ }
34
+ return BinaryArrays.getHalfNibbleArrayIndex(this.data, 0, index);
35
+ }
36
+ set(index, value) {
37
+ if (index < 0 || index >= this.length) {
38
+ throw new RangeError(`Index ${index} is out of bounds`);
39
+ }
40
+ if (value < 0 || value > 3) {
41
+ throw new RangeError(`Value ${value} is out of bounds for a half nibble`);
42
+ }
43
+ BinaryArrays.setHalfNibbleArrayIndex(this.data, 0, index, value);
44
+ }
45
+ [Symbol.iterator]() {
46
+ let index = 0;
47
+ return {
48
+ next: () => {
49
+ if (index < this.length) {
50
+ return { value: this.get(index++), done: false };
51
+ }
52
+ else {
53
+ return { value: undefined, done: true };
54
+ }
55
+ },
56
+ };
57
+ }
58
+ forEach(callback) {
59
+ for (let i = 0; i < this.length; i++) {
60
+ callback(this.get(i), i, this);
61
+ }
62
+ }
63
+ }
@@ -0,0 +1,13 @@
1
+ export interface NibbleArray {
2
+ [index: number]: number;
3
+ }
4
+ export declare class NibbleArray {
5
+ buffer: ArrayBufferLike;
6
+ private data;
7
+ constructor(buffer: ArrayBufferLike);
8
+ get length(): number;
9
+ get(index: number): number;
10
+ set(index: number, value: number): void;
11
+ [Symbol.iterator](): Iterator<number>;
12
+ forEach(callback: (value: number, index: number, array: NibbleArray) => void): void;
13
+ }
@@ -0,0 +1,63 @@
1
+ import { BinaryArrays } from "../Util/BinaryArrays";
2
+ export class NibbleArray {
3
+ buffer;
4
+ data;
5
+ constructor(buffer) {
6
+ this.buffer = buffer;
7
+ this.data = new DataView(ArrayBuffer.isView(buffer) ? buffer.buffer : buffer);
8
+ return new Proxy(this, {
9
+ get: (target, property) => {
10
+ if (typeof property === "string" && !isNaN(Number(property))) {
11
+ const index = Number(property);
12
+ return target.get(index);
13
+ }
14
+ return target[property];
15
+ },
16
+ set: (target, property, value) => {
17
+ if (typeof property === "string" && !isNaN(Number(property))) {
18
+ const index = Number(property);
19
+ target.set(index, value);
20
+ return true;
21
+ }
22
+ target[property] = value;
23
+ return true;
24
+ },
25
+ });
26
+ }
27
+ get length() {
28
+ return this.buffer.byteLength * 2;
29
+ }
30
+ get(index) {
31
+ if (index < 0 || index >= this.length) {
32
+ throw new RangeError(`Index ${index} is out of bounds`);
33
+ }
34
+ return BinaryArrays.getNibbleArrayIndex(this.data, 0, index);
35
+ }
36
+ set(index, value) {
37
+ if (index < 0 || index >= this.length) {
38
+ throw new RangeError(`Index ${index} is out of bounds`);
39
+ }
40
+ if (value < 0 || value > 15) {
41
+ throw new RangeError(`Value ${value} is out of bounds for a nibble`);
42
+ }
43
+ BinaryArrays.setNibbleArrayIndex(this.data, 0, index, value);
44
+ }
45
+ [Symbol.iterator]() {
46
+ let index = 0;
47
+ return {
48
+ next: () => {
49
+ if (index < this.length) {
50
+ return { value: this.get(index++), done: false };
51
+ }
52
+ else {
53
+ return { value: undefined, done: true };
54
+ }
55
+ },
56
+ };
57
+ }
58
+ forEach(callback) {
59
+ for (let i = 0; i < this.length; i++) {
60
+ callback(this.get(i), i, this);
61
+ }
62
+ }
63
+ }
@@ -3,9 +3,9 @@ import { TypedNode } from "./Classes/TypedNode.js";
3
3
  export declare class BinaryObject {
4
4
  static nodes: typeof TypedNodes;
5
5
  static setUseSharedMemory(useSharedMemory: boolean): void;
6
- static bufferToObject<T>(buffer: ArrayBuffer, byteOffSet?: number, byteLength?: number): T;
6
+ static bufferToObject<T>(buffer: ArrayBufferLike, byteOffSet?: number, byteLength?: number): T;
7
7
  static objectToBuffer(obj: any): ArrayBuffer;
8
8
  static objectToTypedNodes(object: any): TypedNode<any>;
9
9
  static typedNodeToBuffer(data: TypedNode<any>): ArrayBuffer;
10
- static bufferToTypedNodes<T>(buffer: ArrayBuffer, byteOffSet?: number, byteLength?: number): TypedNode<T>;
10
+ static bufferToTypedNodes<T>(buffer: ArrayBufferLike, byteOffSet?: number, byteLength?: number): TypedNode<T>;
11
11
  }
@@ -15,7 +15,7 @@ export declare class BufferToObject {
15
15
  static _assign(value: any): void;
16
16
  static markFunctions: Record<BinaryObjectMarkers, (dv: DataView, index: number) => number>;
17
17
  static setUseSharedMemory(useSharedMemory: boolean): void;
18
- static toObject<T>(buffer: ArrayBuffer, byteOffSet?: number, byteOffSetEnd?: number): T;
19
- static toJSON<T>(buffer: ArrayBuffer, byteOffSet?: number, byteOffSetEnd?: number): T;
20
- static toTypedNodes<T>(buffer: ArrayBuffer, byteOffSet?: number, byteOffSetEnd?: number): T;
18
+ static toObject<T>(buffer: ArrayBufferLike, byteOffSet?: number, byteOffSetEnd?: number): T;
19
+ static toJSON<T>(buffer: ArrayBufferLike, byteOffSet?: number, byteOffSetEnd?: number): T;
20
+ static toTypedNodes<T>(buffer: ArrayBufferLike, byteOffSet?: number, byteOffSetEnd?: number): T;
21
21
  }
@@ -235,8 +235,8 @@ export class BufferToObject {
235
235
  },
236
236
  [BinaryObjectMarkers.TypedArray]: (dv, index) => {
237
237
  const type = ByteDataGet[BinaryNumberTypes.Uint8](dv, index + 1);
238
- if (!type)
239
- throw new Error();
238
+ if (type === undefined)
239
+ throw new Error(`Not a valid type for a typed array ${type}`);
240
240
  const length = ByteDataGet[BinaryNumberTypes.Uint32](dv, index + 2);
241
241
  index += ByteCounts.Uint8 * 2 + ByteCounts.Uint32;
242
242
  let array;
@@ -21,7 +21,8 @@ export class ObjectToTypedNodes {
21
21
  node.value[key] = this._addPrimitive(value);
22
22
  continue;
23
23
  }
24
- if (value instanceof Blob || value instanceof File) {
24
+ if (value instanceof Blob ||
25
+ (typeof File !== "undefined" && value instanceof File)) {
25
26
  continue;
26
27
  }
27
28
  if (typeof value == "object" && !Array.isArray(value)) {
@@ -1,5 +1,5 @@
1
- import type { BinaryPropertySchema, BinaryPropertyNodes } from "../Types/BinaryStructSchema.types";
2
- import type { RemoteBinaryStructData } from "../Types/RemoveBinaryStructData.types";
1
+ import type { BinaryPropertyNodes } from "../Types/BinaryStructSchema.types";
2
+ import type { BinaryStructData } from "../Types/BinaryStructData.types";
3
3
  import { BinraryStructBase } from "./BinraryStructBase";
4
4
  type PropertyManagerInitData = {
5
5
  indexBufferMode?: "normal" | "shared";
@@ -7,10 +7,9 @@ type PropertyManagerInitData = {
7
7
  };
8
8
  export declare class BinaryStruct extends BinraryStructBase {
9
9
  id: string;
10
- schema: BinaryPropertySchema;
11
- initData: RemoteBinaryStructData;
10
+ properties: BinaryPropertyNodes[];
12
11
  constructor(id: string);
13
12
  registerProperty(...PropertyData: BinaryPropertyNodes[]): void;
14
- init(initData?: PropertyManagerInitData): RemoteBinaryStructData;
13
+ init(initData?: PropertyManagerInitData): BinaryStructData;
15
14
  }
16
15
  export {};
@@ -1,170 +1,23 @@
1
- import { BinaryUtil } from "../../Util/BinaryUtil.js";
2
1
  import { BinraryStructBase } from "./BinraryStructBase";
3
- import { ByteCounts } from "../../Constants/BinaryTypes";
4
- import { StructPropertyTypes } from "../Constants/StructPropertyTypes";
5
- const PropertyIndexSize = ByteCounts.Uint32 + ByteCounts.Uint8 * 3;
6
- const setIndexData = (data, indexBufferIndex, byteIndex, bitOffSet, bitSize, type) => {
7
- data.setUint32(indexBufferIndex, byteIndex);
8
- indexBufferIndex += ByteCounts.Uint32;
9
- data.setUint8(indexBufferIndex, bitOffSet);
10
- indexBufferIndex += ByteCounts.Uint8;
11
- data.setUint8(indexBufferIndex, bitSize);
12
- indexBufferIndex += ByteCounts.Uint8;
13
- data.setUint8(indexBufferIndex, type);
14
- indexBufferIndex += ByteCounts.Uint8;
15
- return indexBufferIndex;
16
- };
2
+ import { CreateIndex } from "../Functions/CreateIndex";
17
3
  export class BinaryStruct extends BinraryStructBase {
18
4
  id;
19
- schema = new Map();
20
- initData = {};
5
+ properties = [];
21
6
  constructor(id) {
22
7
  super(id);
23
8
  this.id = id;
24
9
  }
25
10
  registerProperty(...PropertyData) {
26
- PropertyData.forEach((_) => this.schema.set(_.id, _));
11
+ PropertyData.forEach((_) => this.properties.push(_));
27
12
  }
28
13
  init(initData) {
29
- /*
30
- [Process Propertys]
31
- */
32
- const headers = new Map();
33
- const booleans = [];
34
- const numbers = [];
35
- const typedNumbers = new Map();
36
- const typedNumbersArrays = new Map();
37
- const bitArrays = [];
38
- this.schema.forEach((Property) => {
39
- if (Property.type == "header") {
40
- let Propertys = headers.get(Property.numberType);
41
- if (!Propertys) {
42
- Propertys = [];
43
- headers.set(Property.numberType, Propertys);
44
- }
45
- Propertys.push(Property);
46
- }
47
- if (Property.type == "boolean") {
48
- booleans.push(Property);
49
- }
50
- if (Property.type == "number") {
51
- const range = Property.range;
52
- const bitSize = BinaryUtil.calculateBitsNeeded(range[0], range[1]);
53
- numbers[bitSize] ??= [];
54
- numbers[bitSize].push(Property);
55
- }
56
- if (Property.type == "typed-number") {
57
- let Propertys = typedNumbers.get(Property.numberType);
58
- if (!Propertys) {
59
- Propertys = [];
60
- typedNumbers.set(Property.numberType, Propertys);
61
- }
62
- Propertys.push(Property);
63
- }
64
- if (Property.type == "typed-number-array") {
65
- let arrayPropertys = typedNumbersArrays.get(Property.numberType);
66
- if (!arrayPropertys) {
67
- arrayPropertys = [];
68
- typedNumbersArrays.set(Property.numberType, arrayPropertys);
69
- }
70
- arrayPropertys.push(Property);
71
- }
72
- if (Property.type == "bit-array") {
73
- bitArrays.push(Property);
74
- }
75
- });
76
- /*
77
- [Build Index]
78
- */
79
- const indexSize = this.schema.size * PropertyIndexSize;
80
- let indexBuffer = new ArrayBuffer(indexSize);
81
- if (initData?.indexBufferMode == "shared") {
82
- indexBuffer = new SharedArrayBuffer(indexSize);
83
- }
84
- const index = new DataView(indexBuffer);
85
- this.index = index;
86
- let indexBufferIndex = 0;
87
- let byteIndex = 0;
88
- let bitIndex = 0;
89
- let bitSize = 1;
90
- /*
91
- [Headers]
92
- */
93
- headers.forEach((Propertys, type) => {
94
- const byteSise = BinaryUtil.getTypedSize(type);
95
- for (let i = 0; i < Propertys.length; i++) {
96
- const Property = Propertys[i];
97
- this.indexMap.set(Property.id, indexBufferIndex);
98
- indexBufferIndex = setIndexData(index, indexBufferIndex, byteIndex, 0, Property.numberType, StructPropertyTypes.TypedNumber);
99
- byteIndex += byteSise;
100
- }
101
- });
102
- /*
103
- [Booleans]
104
- */
105
- bitSize = 1;
106
- for (let i = 0; i < booleans.length; i++) {
107
- const bool = booleans[i];
108
- this.indexMap.set(bool.id, indexBufferIndex);
109
- indexBufferIndex = setIndexData(index, indexBufferIndex, byteIndex, bitIndex, bitSize, StructPropertyTypes.Boolean);
110
- bitIndex++;
111
- if (bitIndex >= 8) {
112
- byteIndex++;
113
- bitIndex = 0;
114
- }
115
- }
116
- /*
117
- [Typed Numbers]
118
- */
119
- bitIndex = 0;
120
- byteIndex++;
121
- typedNumbers.forEach((Propertys, type) => {
122
- const byteSise = BinaryUtil.getTypedSize(type);
123
- for (let i = 0; i < Propertys.length; i++) {
124
- const Property = Propertys[i];
125
- this.indexMap.set(Property.id, indexBufferIndex);
126
- indexBufferIndex = setIndexData(index, indexBufferIndex, byteIndex, 0, Property.numberType, StructPropertyTypes.TypedNumber);
127
- byteIndex += byteSise;
128
- }
129
- });
130
- /*
131
- [Typed Numbers Arrays]
132
- */
133
- byteIndex++;
134
- typedNumbersArrays.forEach((Propertys, type) => {
135
- const byteSise = BinaryUtil.getTypedSize(type);
136
- for (let i = 0; i < Propertys.length; i++) {
137
- const Property = Propertys[i];
138
- this.indexMap.set(Property.id, indexBufferIndex);
139
- indexBufferIndex = setIndexData(index, indexBufferIndex, byteIndex, 0, Property.numberType, StructPropertyTypes.TypedNumberArray);
140
- byteIndex += byteSise * Property.length;
141
- }
142
- });
143
- byteIndex++;
144
- bitArrays.forEach((Property) => {
145
- const byteSise = Math.ceil(Property.length / 8) + 1;
146
- this.indexMap.set(Property.id, indexBufferIndex);
147
- indexBufferIndex = setIndexData(index, indexBufferIndex, byteIndex, 0, byteSise, StructPropertyTypes.BitArray);
148
- byteIndex += byteSise;
149
- });
150
- /*
151
- [Create Remote Property Manager Data]
152
- */
153
- let numberOfIndexes = 1;
154
- if (initData?.numberOfIndexes) {
155
- numberOfIndexes = initData.numberOfIndexes;
156
- }
157
- this.structArrayIndexes = numberOfIndexes;
158
- this.structSize = byteIndex;
159
- const remoteData = {
160
- bufferSize: byteIndex * numberOfIndexes,
161
- buffer: new ArrayBuffer(0),
162
- indexBuffer: indexBuffer,
163
- indexMap: this.indexMap,
164
- structSize: this.structSize,
165
- structArrayLength: numberOfIndexes,
166
- };
167
- this.initData = remoteData;
168
- return remoteData;
14
+ const data = CreateIndex(this.properties, initData?.numberOfIndexes, initData?.indexBufferMode == "shared");
15
+ this.structData = data;
16
+ this.data = new DataView(data.buffer);
17
+ this.index = new DataView(data.indexBuffer);
18
+ this.indexMap = data.indexMap;
19
+ this.structArrayIndexes = data.structArrayIndexes;
20
+ this.structSize = data.structSize;
21
+ return data;
169
22
  }
170
23
  }
@@ -1,13 +1,15 @@
1
1
  import { BufferTypes } from "../../Util/BufferTypes.js";
2
2
  import { InstantiatedStruct } from "./InstantiatedStruct.js";
3
+ import { BinaryStructData } from "../Types/BinaryStructData.types.js";
3
4
  export declare class BinraryStructBase {
4
5
  id: string;
5
6
  byteOffSet: number;
6
7
  structSize: number;
7
8
  structArrayIndexes: number;
8
9
  structArrayIndex: number;
10
+ structData: BinaryStructData;
9
11
  data: DataView;
10
- indexMap: Map<string, number>;
12
+ indexMap: Record<string, number>;
11
13
  index: DataView;
12
14
  constructor(id: string);
13
15
  setBuffer(data: BufferTypes | DataView): void;
@@ -24,9 +26,6 @@ export declare class BinraryStructBase {
24
26
  */
25
27
  getArrayPropertyByteIndex(id: string, index: number): number;
26
28
  setArrayPropertyValue(id: string, index: number, value: number): number | void;
27
- loopThroughProperties(run: (id: string, value: number) => void): void;
28
- loopThroughIndex(run: (data: number[]) => void): void;
29
- loopThroughAllIndexAndProperties(run: (id: string, value: number, index: number) => void): void;
30
29
  /**## instantiate
31
30
  * Creates an object to read/write to the struct buffer.
32
31
  * @param structArrayIndex - Default is the current index.