edinburgh 0.1.3 → 0.4.1

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.
@@ -0,0 +1,138 @@
1
+ /**
2
+ * A byte buffer for efficient reading and writing of primitive values and bit sequences.
3
+ *
4
+ * The DataPack class provides methods for serializing and deserializing various data types
5
+ * including numbers, strings, bit sequences, and other primitive values to/from byte buffers.
6
+ * It supports both reading and writing operations with automatic buffer management.
7
+ */
8
+ export default class DataPack {
9
+ private buffer;
10
+ private dataView?;
11
+ readPos: number;
12
+ writePos: number;
13
+ /**
14
+ * Backward compatibility: Access to the internal buffer
15
+ */
16
+ get _buffer(): Uint8Array;
17
+ /**
18
+ * Create a new DataPack instance.
19
+ * @param data - Optional initial data as Uint8Array or buffer size as number.
20
+ */
21
+ constructor(data?: Uint8Array | number);
22
+ /**
23
+ * Helper function to write a multi-byte integer with length prefix
24
+ * @param value - The value to write
25
+ * @param headerType - The type bits (0-7) for the header
26
+ * @param invertBytes - Whether to invert bytes (for negative numbers)
27
+ * @param invertByteCount - Whether to invert the byte count (for type 0)
28
+ */
29
+ private writeMultiByteNumber;
30
+ /**
31
+ * Helper function to read a multi-byte integer with length prefix
32
+ * @param byteCount - Number of bytes to read
33
+ * @param invertBytes - Whether to invert bytes (for negative numbers)
34
+ * @returns The read value
35
+ */
36
+ private readMultiByteNumber;
37
+ private notEnoughData;
38
+ /**
39
+ * Each data item starts with a single byte. The high 3 bits indicate the type.
40
+ * The low 5 bits indicate a size or a subtype, depending on the type.
41
+ *
42
+ * 0: negative integer (byte count encoded as bitwise NOT in lower bits)
43
+ * 1: small integer 0..31 (encoded in lower bits)
44
+ * 2: small integer 32...63 (encoded in lower bits)
45
+ * 3: integer (byte count encoded in lower bits)
46
+ * 4:
47
+ * 0: float64 (8 bytes follow)
48
+ * 1: undefined
49
+ * 2: null
50
+ * 3: true
51
+ * 4: false
52
+ * 5: array start (followed by a items until EOD)
53
+ * 6: object start (followed by key+value pairs until EOD)
54
+ * 7: map start (followed by key+value pairs until EOD)
55
+ * 8: set start (followed by items until EOD)
56
+ * 9: EOD
57
+ * 10: identifier (6 byte positive int follows, represented as a base64 string of length 8)
58
+ * 11: null-terminated string
59
+ * 12: Date/Time (varint with whole seconds since epoch follows)
60
+ * 13: custom type, followed by name string and data value
61
+ * 5: short string (length in lower bits, 0-31)
62
+ * 6: string (byte count of length in lower bits)
63
+ * 7: blob (byte count of length in lower bits)
64
+ */
65
+ write(data: any): DataPack;
66
+ read(customConverters?: {
67
+ [name: string]: ((data: any) => any);
68
+ } | undefined): any;
69
+ /**
70
+ * Ensure the buffer has capacity for additional bytes.
71
+ * @param bytesNeeded - Number of additional bytes needed.
72
+ */
73
+ private ensureCapacity;
74
+ readNumber(): number;
75
+ readDate(): Date;
76
+ readPositiveInt(limit?: number): number;
77
+ readBoolean(): boolean;
78
+ readUint8Array(): Uint8Array;
79
+ readString(): string;
80
+ writeIdentifier(id: string | number): DataPack;
81
+ readIdentifier(): string;
82
+ readIdentifierNumber(): number;
83
+ /**
84
+ * Like writeString but writes without a length prefix and with a null terminator, for ordered storage.
85
+ * Can be read with {@link read} or {@link readString} just like any other string.
86
+ * @param str - The string to write. May not contain null characters.
87
+ */
88
+ writeOrderedString(str: string): DataPack;
89
+ toUint8Array(copyBuffer?: boolean, startPos?: number, endPos?: number): Uint8Array;
90
+ toBuffer(): ArrayBuffer;
91
+ clone(copyBuffer: boolean, readPos?: number, writePos?: number): DataPack;
92
+ writeCustom(name: string, data: any): void;
93
+ writeAsObject(obj: Record<string, any>): this;
94
+ /**
95
+ * Write a collection (array, set, object, or map) using a callback to add items/fields.
96
+ * @param type 'array' | 'set' | 'object' | 'map'
97
+ * @param bodyFunc Callback function to add items/fields. It accepts a function to add values or key-value pairs (depending on the collection type).
98
+ * @returns The DataPack instance for chaining.
99
+ * @example
100
+ * // Writing an array
101
+ * pack.writeCollection('array', add => {
102
+ * add(1);
103
+ * add(2);
104
+ * add(3);
105
+ * });
106
+ *
107
+ * // Writing an object
108
+ * pack.writeCollection('object', (add) => {
109
+ * add('key1', 'value1');
110
+ * add('key2', 42);
111
+ * });
112
+ */
113
+ writeCollection(type: 'array' | 'set', bodyFunc: (addField: (value: any) => void) => void): DataPack;
114
+ writeCollection(type: 'object', bodyFunc: (addField: (field: number | string | symbol, value: any) => void) => void): DataPack;
115
+ writeCollection(type: 'map', bodyFunc: (addField: (field: any, value: any) => void) => void): DataPack;
116
+ /**
117
+ * Increment the last byte of the buffer. If it was already 255 set it to 0 and
118
+ * increment the previous byte, and so on. If all bytes were 255, return undefined.
119
+ * This is useful for creating an exclusive end key for range scans.
120
+ * This may result in a DataPack instance that cannot be parsed (or represented by
121
+ * {@link toString}).
122
+ */
123
+ increment(): DataPack | undefined;
124
+ toString(extended?: boolean | undefined, startPos?: number, endPos?: number): string;
125
+ readAvailable(): boolean;
126
+ static generateIdentifier(): string;
127
+ static createBuffer(...args: any): ArrayBuffer;
128
+ static createUint8Array(...args: any): Uint8Array<ArrayBufferLike>;
129
+ }
130
+ declare class CustomData {
131
+ name: string;
132
+ data: any;
133
+ constructor(name: string, data: any);
134
+ }
135
+ export default interface DataPack {
136
+ CustomData: typeof CustomData;
137
+ }
138
+ export {};