pw-js-world 0.4.0 → 0.4.1-dev.766d8c6

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.
package/cm/Block.d.ts ADDED
@@ -0,0 +1,119 @@
1
+ import type { BlockArg, Point, SendableBlockPacket } from "./types/index.js";
2
+ import BufferReader, { ComponentTypeHeader } from "./BufferReader.js";
3
+ import { LayerType } from "./Constants.js";
4
+ import { type BlockKeys } from "pw-js-api";
5
+ export default class Block {
6
+ bId: number;
7
+ args: BlockArg[];
8
+ constructor(bId: number | BlockKeys | string, args?: BlockArg[]);
9
+ /**
10
+ * I mean... Just use .args.length !== 0 to see if it has args.
11
+ *
12
+ * But anyway, this will return true if there is at least one args, otherwise false.
13
+ */
14
+ hasArgs(): boolean;
15
+ /**
16
+ * For helper.
17
+ *
18
+ * This is in Block class for organisation.
19
+ *
20
+ * This will deserialise by using the reader to get the block ID then retrieve the args, if applicable.
21
+ */
22
+ static deserialize(reader: BufferReader): Block;
23
+ protected deserializeArgs(reader: BufferReader, flag?: boolean): this;
24
+ /**
25
+ * For helper.
26
+ *
27
+ * This is in Block class for organisation.
28
+ */
29
+ static deserializeArgs(reader: BufferReader): BlockArg[];
30
+ /**
31
+ * Serializes the block into a buffer. This is used to convert
32
+ * the block into a binary format that can be sent over the game
33
+ * server. As this is static, block id and args are required.
34
+ *
35
+ * - Little Endian
36
+ * - With Id
37
+ * - Type Byte omitted
38
+ */
39
+ static serializeArgs(bId: number, args: BlockArg[]): Buffer;
40
+ /**
41
+ * Serializes the block into a buffer. This is used to convert
42
+ * the block into a binary format that can be sent over the game
43
+ * server. As this is static, block id and args are required.
44
+ *
45
+ * - Big Endian
46
+ * - No Id
47
+ * - Type Byte included
48
+ */
49
+ static serializeArgs(bId: number, args: BlockArg[], options: {
50
+ endian: "big";
51
+ writeId: false;
52
+ readTypeByte: true;
53
+ }): Buffer;
54
+ static serializeArgs(bId: number, args: BlockArg[], options: {
55
+ endian: "little";
56
+ writeId: false;
57
+ readTypeByte: true;
58
+ }): Buffer;
59
+ /**
60
+ *
61
+ * @param pos List of points (X and Y)
62
+ */
63
+ toPacket(pos: Point[], layer: LayerType): SendableBlockPacket;
64
+ toPacket(x: number, y: number, layer: LayerType): SendableBlockPacket;
65
+ /**
66
+ * This will return the block name in UPPER_CASE form.
67
+ *
68
+ * For eg EFFECTS_INVULNERABILITY.
69
+ *
70
+ * @throws {MissingBlockError}
71
+ * If the ID of this block is not known.
72
+ */
73
+ get name(): string;
74
+ /**
75
+ * Returns a copy of the block.
76
+ */
77
+ clone(obj?: false): Block;
78
+ clone(obj: true): {
79
+ bId: number;
80
+ args: BlockArg[];
81
+ name: string;
82
+ };
83
+ /**
84
+ * This can be convenient as it will always return the ID if it exists, and it will throw an error if it doesn't.
85
+ *
86
+ * This expects the name sent to be in full upper capital form though.
87
+ *
88
+ * @throws {MissingBlockError}
89
+ * If the connection is unknown, this can be because you're trying to use this function when Api#getListBlocks has never been invoked, or the object is missing.
90
+ */
91
+ static getIdByName(paletteId: string): number;
92
+ /**
93
+ * This will return the corresponding palette id by the ID of that block.
94
+ *
95
+ * The name sent will be in full upper capital if it exists.
96
+ *
97
+ * @throws {MissingBlockError}
98
+ * If the connection is unknown, this can be because you're trying to use this function when Api#getListBlocks has never been invoked, or the object is missing.
99
+ */
100
+ static getPaletteIdById(blockId: number): string;
101
+ /**
102
+ * Returns the arg types for that block by given block ID.
103
+ *
104
+ * If a block don't have args, it will return an empty array.
105
+ *
106
+ * If the block don't exist, it may throw an exception.
107
+ */
108
+ static getArgTypesByBlockId(blockId: number): ComponentTypeHeader[];
109
+ /**
110
+ * Returns the arg types for that block by given palette ID (full upper case).
111
+ *
112
+ * For eg "EMPTY" or "SIGN_GOLD"
113
+ *
114
+ * If a block don't have args, it will return an empty array.
115
+ *
116
+ * If the block don't exist, it may throw an exception.
117
+ */
118
+ static getArgTypesByPaletteId(paletteId: string): ComponentTypeHeader[];
119
+ }
package/cm/Block.js ADDED
@@ -0,0 +1,183 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ const BufferReader_js_1 = tslib_1.__importDefault(require("./BufferReader.js"));
5
+ const pw_js_api_1 = require("pw-js-api");
6
+ const Error_js_1 = require("./util/Error.js");
7
+ class Block {
8
+ constructor(bId, args) {
9
+ this.args = [];
10
+ if (typeof bId === "number")
11
+ this.bId = bId;
12
+ else {
13
+ this.bId = Block.getIdByName(bId);
14
+ }
15
+ if (args)
16
+ this.args = args;
17
+ }
18
+ /**
19
+ * I mean... Just use .args.length !== 0 to see if it has args.
20
+ *
21
+ * But anyway, this will return true if there is at least one args, otherwise false.
22
+ */
23
+ hasArgs() {
24
+ return this.args.length !== 0;
25
+ }
26
+ /**
27
+ * For helper.
28
+ *
29
+ * This is in Block class for organisation.
30
+ *
31
+ * This will deserialise by using the reader to get the block ID then retrieve the args, if applicable.
32
+ */
33
+ static deserialize(reader) {
34
+ return new Block(reader.readUInt32LE()).deserializeArgs(reader);
35
+ }
36
+ deserializeArgs(reader, flag = false) {
37
+ var _a;
38
+ const format = Block.getArgTypesByBlockId(this.bId); //(BlockArgsHeadings as any)[this.name];
39
+ for (let i = 0; i < ((_a = format === null || format === void 0 ? void 0 : format.length) !== null && _a !== void 0 ? _a : 0); i++) {
40
+ if (flag) {
41
+ reader.expectUInt8(format[i]);
42
+ }
43
+ this.args[i] = reader.read(format[i], !flag);
44
+ }
45
+ return this;
46
+ }
47
+ /**
48
+ * For helper.
49
+ *
50
+ * This is in Block class for organisation.
51
+ */
52
+ static deserializeArgs(reader) {
53
+ // const args =
54
+ return reader.deserialize();
55
+ // for (let i = 0; i < (format?.length ?? 0); i++) {
56
+ // if (flag) {
57
+ // reader.expectUInt8(format[i]);
58
+ // }
59
+ // args[i] = reader.read(format[i], !flag);
60
+ // }
61
+ // return args;
62
+ }
63
+ static serializeArgs(bId, args, options) {
64
+ options || (options = {
65
+ endian: "little",
66
+ writeId: true,
67
+ readTypeByte: false,
68
+ });
69
+ const buffer = [];
70
+ if (options.writeId) {
71
+ const idBuffer = Buffer.alloc(4);
72
+ idBuffer.writeUInt32LE(bId);
73
+ buffer.push(idBuffer);
74
+ }
75
+ const blockData = Block.getArgTypesByBlockId(bId);
76
+ for (let i = 0, len = blockData.length; i < len; i++) {
77
+ const entry = BufferReader_js_1.default.Dynamic(blockData[i], args[i]);
78
+ buffer.push(entry);
79
+ }
80
+ return Buffer.concat(buffer);
81
+ }
82
+ toPacket(pos, y, layer) {
83
+ if (typeof pos === "number") {
84
+ pos = [{
85
+ x: pos, y
86
+ }];
87
+ layer = layer !== null && layer !== void 0 ? layer : 0;
88
+ }
89
+ else
90
+ layer = y !== null && y !== void 0 ? y : 0;
91
+ return {
92
+ isFillOperation: false,
93
+ blockId: this.bId,
94
+ layer,
95
+ positions: pos,
96
+ extraFields: Block.serializeArgs(this.bId, this.args, { endian: "big", writeId: false, readTypeByte: true })
97
+ };
98
+ }
99
+ /**
100
+ * This will return the block name in UPPER_CASE form.
101
+ *
102
+ * For eg EFFECTS_INVULNERABILITY.
103
+ *
104
+ * @throws {MissingBlockError}
105
+ * If the ID of this block is not known.
106
+ */
107
+ get name() {
108
+ var _a;
109
+ const block = (_a = pw_js_api_1.PWApiClient.listBlocks) === null || _a === void 0 ? void 0 : _a[this.bId];
110
+ if (block === undefined)
111
+ throw new Error_js_1.MissingBlockError("Current block data is missing, run Api#listBlocks first?", this.bId);
112
+ return block.PaletteId.toUpperCase();
113
+ }
114
+ clone(obj = false) {
115
+ if (obj === true)
116
+ return { bId: this.bId, args: this.args, name: this.name };
117
+ return new Block(this.bId, this.args);
118
+ }
119
+ /**
120
+ * This can be convenient as it will always return the ID if it exists, and it will throw an error if it doesn't.
121
+ *
122
+ * This expects the name sent to be in full upper capital form though.
123
+ *
124
+ * @throws {MissingBlockError}
125
+ * If the connection is unknown, this can be because you're trying to use this function when Api#getListBlocks has never been invoked, or the object is missing.
126
+ */
127
+ static getIdByName(paletteId) {
128
+ var _a;
129
+ const block = (_a = pw_js_api_1.PWApiClient.listBlocksObj) === null || _a === void 0 ? void 0 : _a[paletteId];
130
+ if (block === undefined)
131
+ throw new Error_js_1.MissingBlockError("Current block data is missing, run Api#listBlocks first?", paletteId);
132
+ return block.Id;
133
+ }
134
+ /**
135
+ * This will return the corresponding palette id by the ID of that block.
136
+ *
137
+ * The name sent will be in full upper capital if it exists.
138
+ *
139
+ * @throws {MissingBlockError}
140
+ * If the connection is unknown, this can be because you're trying to use this function when Api#getListBlocks has never been invoked, or the object is missing.
141
+ */
142
+ static getPaletteIdById(blockId) {
143
+ var _a;
144
+ const block = (_a = pw_js_api_1.PWApiClient.listBlocks) === null || _a === void 0 ? void 0 : _a[blockId];
145
+ if (block === undefined)
146
+ throw new Error_js_1.MissingBlockError("Current block data is missing, run Api#listBlocks first?", blockId);
147
+ return block.PaletteId.toUpperCase();
148
+ }
149
+ /**
150
+ * Returns the arg types for that block by given block ID.
151
+ *
152
+ * If a block don't have args, it will return an empty array.
153
+ *
154
+ * If the block don't exist, it may throw an exception.
155
+ */
156
+ static getArgTypesByBlockId(blockId) {
157
+ var _a, _b;
158
+ return (_b = (_a = pw_js_api_1.PWApiClient.listBlocks) === null || _a === void 0 ? void 0 : _a[blockId].BlockDataArgs) !== null && _b !== void 0 ? _b : [];
159
+ // const block = PWApiClient.listBlocks?.[blockId];
160
+ // return block ? MissingBlockData[block?.PaletteId.toUpperCase()] ?? (block.BlockDataArgs) as ComponentTypeHeader[] ?? [] : [];
161
+ }
162
+ /**
163
+ * Returns the arg types for that block by given palette ID (full upper case).
164
+ *
165
+ * For eg "EMPTY" or "SIGN_GOLD"
166
+ *
167
+ * If a block don't have args, it will return an empty array.
168
+ *
169
+ * If the block don't exist, it may throw an exception.
170
+ */
171
+ static getArgTypesByPaletteId(paletteId) {
172
+ var _a, _b;
173
+ return (_b = (_a = pw_js_api_1.PWApiClient.listBlocksObj) === null || _a === void 0 ? void 0 : _a[paletteId].BlockDataArgs) !== null && _b !== void 0 ? _b : [];
174
+ //MissingBlockData[paletteId] ?? (PWApiClient.listBlocksObj?.[paletteId].BlockDataArgs) as ComponentTypeHeader[] ?? []
175
+ }
176
+ }
177
+ exports.default = Block;
178
+ // Temporary fix as some blocks currently have incorrect args
179
+ // const MissingBlockData = {
180
+ // SWITCH_LOCAL_ACTIVATOR: [ComponentTypeHeader.Int32, ComponentTypeHeader.Byte],
181
+ // SWITCH_GLOBAL_ACTIVATOR: [ComponentTypeHeader.Int32, ComponentTypeHeader.Byte],
182
+ // } as Record<string, ComponentTypeHeader[]>;
183
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiQmxvY2suanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9saWIvQmxvY2sudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7O0FBQ0EsZ0ZBQXNFO0FBRXRFLHlDQUF3RDtBQUN4RCw4Q0FBb0Q7QUFFcEQsTUFBcUIsS0FBSztJQUl0QixZQUFZLEdBQWdDLEVBQUUsSUFBaUI7UUFGL0QsU0FBSSxHQUFlLEVBQUUsQ0FBQztRQUdsQixJQUFJLE9BQU8sR0FBRyxLQUFLLFFBQVE7WUFBRSxJQUFJLENBQUMsR0FBRyxHQUFHLEdBQUcsQ0FBQzthQUN2QyxDQUFDO1lBQ0YsSUFBSSxDQUFDLEdBQUcsR0FBRyxLQUFLLENBQUMsV0FBVyxDQUFDLEdBQUcsQ0FBQyxDQUFDO1FBQ3RDLENBQUM7UUFFRCxJQUFJLElBQUk7WUFBRSxJQUFJLENBQUMsSUFBSSxHQUFHLElBQUksQ0FBQztJQUMvQixDQUFDO0lBRUQ7Ozs7T0FJRztJQUNILE9BQU87UUFDSCxPQUFPLElBQUksQ0FBQyxJQUFJLENBQUMsTUFBTSxLQUFLLENBQUMsQ0FBQztJQUNsQyxDQUFDO0lBRUQ7Ozs7OztPQU1HO0lBQ0gsTUFBTSxD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FXLENBQUMsVUFBVSwwQ0FBRyxPQUFPLEVBQUUsYUFBYSxtQ0FBSSxFQUFFLENBQUM7UUFFN0QsbURBQW1EO1FBRW5ELGdJQUFnSTtJQUNwSSxDQUFDO0lBRUQ7Ozs7Ozs7O09BUUc7SUFDSCxNQUFNLENBQUMsc0JBQXNCLENBQUMsU0FBaUI7O1FBQzNDLE9BQU8sTUFBQSxNQUFBLHVCQUFXLENBQUMsYUFBYSwwQ0FBRyxTQUFTLEVBQUUsYUFBYSxtQ0FBSSxFQUFFLENBQUM7UUFDbEUsc0hBQXNIO0lBQzFILENBQUM7Q0FDSjtBQW5PRCx3QkFtT0M7QUFFRCw2REFBNkQ7QUFDN0QsNkJBQTZCO0FBQzdCLHFGQUFxRjtBQUNyRixzRkFBc0Y7QUFDdEYsOENBQThDIn0=
@@ -0,0 +1,330 @@
1
+ /**
2
+ * CREDIT: Anatoly for making this Buffer Reader so I don't have to!
3
+ * Source: https://github.com/Anatoly03/pixelwalker.js/blob/9bb3c7e39a45006086a2abae8c515599bd3db835/src/util/buffer-reader.ts
4
+ * License: ISC
5
+ */
6
+ /**
7
+ * Data during the communication in the process is dynamic
8
+ * typed. Every entry is followed by a byte identifying the
9
+ * type, followed by data. The type header is noted by its'
10
+ * 7-bit notation.
11
+ */
12
+ export declare enum ComponentTypeHeader {
13
+ String = 0,
14
+ Byte = 1,
15
+ Int16 = 2,
16
+ Int32 = 3,
17
+ Int64 = 4,
18
+ Float = 5,
19
+ Double = 6,
20
+ Boolean = 7,
21
+ ByteArray = 8,
22
+ UInt32 = 9
23
+ }
24
+ /**
25
+ * A Buffer reader is a special buffer extension made to perform
26
+ * game-specific tasks in the field of communication.
27
+ *
28
+ * @implements Buffer
29
+ */
30
+ export default class BufferReader {
31
+ private buffer;
32
+ private offset;
33
+ /**
34
+ *
35
+ */
36
+ private constructor();
37
+ /**
38
+ *
39
+ */
40
+ static from(from: Uint8Array | Buffer): BufferReader;
41
+ /**
42
+ *
43
+ */
44
+ static alloc(amount: number): BufferReader;
45
+ /**
46
+ * @param {string} value
47
+ * @returns {Buffer}
48
+ */
49
+ static String(value?: string): Buffer;
50
+ /**
51
+ * @param {number} value
52
+ * @returns {Buffer}
53
+ */
54
+ static Byte(value?: number): Buffer;
55
+ /**
56
+ * @param {number} value
57
+ * @returns {Buffer}
58
+ */
59
+ static Int16(value?: number): Buffer;
60
+ /**
61
+ * @param {number} value
62
+ * @returns {Buffer}
63
+ */
64
+ static Int32(value?: number): Buffer;
65
+ /**
66
+ * @param {bigint} value
67
+ * @returns {Buffer}
68
+ */
69
+ static Int64(value?: bigint): Buffer;
70
+ /**
71
+ * @param {number} value
72
+ * @returns {Buffer}
73
+ */
74
+ static Float(value?: number): Buffer;
75
+ /**
76
+ * @param {number} value
77
+ * @returns {Buffer}
78
+ */
79
+ static Double(value?: number): Buffer;
80
+ /**
81
+ * @param {boolean} value
82
+ * @returns {Buffer}
83
+ */
84
+ static Boolean(value?: boolean): Buffer;
85
+ /**
86
+ * @param {Uint8Array} buffer
87
+ * @returns {Buffer}
88
+ */
89
+ static ByteArray(buffer?: Buffer): Buffer;
90
+ /**
91
+ * @param {number} value
92
+ * @returns {Buffer}
93
+ */
94
+ static UInt32(value?: number): Buffer;
95
+ /**
96
+ * @param {number} value
97
+ * @returns {Buffer}
98
+ */
99
+ static Magic(value: number): Buffer;
100
+ /**
101
+ * @param {number} value
102
+ * @returns {Buffer}
103
+ */
104
+ static Bit7(value?: number): Buffer;
105
+ /**
106
+ * @param tt
107
+ * @param value
108
+ */
109
+ static Dynamic(tt: ComponentTypeHeader, value: boolean | number | bigint | string | Buffer): Buffer;
110
+ /**
111
+ *
112
+ */
113
+ get length(): number;
114
+ /**
115
+ *
116
+ */
117
+ subarray(start?: number, end?: number): BufferReader;
118
+ /**
119
+ *
120
+ */
121
+ write(value: string): number;
122
+ /**
123
+ *
124
+ */
125
+ writeBigInt64BE(value: bigint): number;
126
+ /**
127
+ *
128
+ */
129
+ writeBigInt64LE(value: bigint): number;
130
+ /**
131
+ *
132
+ */
133
+ writeUInt8(value: number): number;
134
+ /**
135
+ *
136
+ */
137
+ writeUInt16LE(value: number): number;
138
+ /**
139
+ *
140
+ */
141
+ writeUInt16BE(value: number): number;
142
+ /**
143
+ *
144
+ */
145
+ writeUInt32LE(value: number): number;
146
+ /**
147
+ *
148
+ */
149
+ writeUInt32BE(value: number): number;
150
+ /**
151
+ *
152
+ */
153
+ writeInt8(value: number): number;
154
+ /**
155
+ *
156
+ */
157
+ writeInt16LE(value: number): number;
158
+ /**
159
+ *
160
+ */
161
+ writeInt16BE(value: number): number;
162
+ /**
163
+ *
164
+ */
165
+ writeInt32LE(value: number): number;
166
+ /**
167
+ *
168
+ */
169
+ writeInt32BE(value: number): number;
170
+ /**
171
+ *
172
+ */
173
+ writeFloatLE(value: number): number;
174
+ /**
175
+ *
176
+ */
177
+ writeFloatBE(value: number): number;
178
+ /**
179
+ *
180
+ */
181
+ writeDoubleLE(value: number): number;
182
+ /**
183
+ *
184
+ */
185
+ writeDoubleBE(value: number): number;
186
+ /**
187
+ *
188
+ */
189
+ readBigUInt64BE(): bigint;
190
+ /**
191
+ *
192
+ */
193
+ readBigUInt64LE(): bigint;
194
+ /**
195
+ *
196
+ */
197
+ readBigInt64BE(): bigint;
198
+ /**
199
+ *
200
+ */
201
+ readBigInt64LE(): bigint;
202
+ /**
203
+ *
204
+ */
205
+ expectUInt8(value: number): number;
206
+ /**
207
+ *
208
+ */
209
+ readUInt8(): number;
210
+ /**
211
+ *
212
+ */
213
+ readUInt16LE(): number;
214
+ /**
215
+ *
216
+ */
217
+ readUInt16BE(): number;
218
+ /**
219
+ *
220
+ */
221
+ readUInt32LE(): number;
222
+ /**
223
+ *
224
+ */
225
+ readUInt32BE(): number;
226
+ /**
227
+ *
228
+ */
229
+ readInt8(): number;
230
+ /**
231
+ *
232
+ */
233
+ readInt16LE(): number;
234
+ /**
235
+ *
236
+ */
237
+ readInt16BE(): number;
238
+ /**
239
+ *
240
+ */
241
+ readInt32LE(): number;
242
+ /**
243
+ *
244
+ */
245
+ readInt32BE(): number;
246
+ /**
247
+ *
248
+ */
249
+ readFloatLE(): number;
250
+ /**
251
+ *
252
+ */
253
+ readFloatBE(): number;
254
+ /**
255
+ *
256
+ */
257
+ readDoubleLE(): number;
258
+ /**
259
+ *
260
+ */
261
+ readDoubleBE(): number;
262
+ read(tt: ComponentTypeHeader, littleEndian?: boolean): string | number | bigint | boolean | Buffer;
263
+ read(tt: ComponentTypeHeader.String, littleEndian?: boolean): string;
264
+ read(tt: ComponentTypeHeader.Byte, littleEndian?: boolean): number;
265
+ read(tt: ComponentTypeHeader.Int16, littleEndian?: boolean): number;
266
+ read(tt: ComponentTypeHeader.Int32, littleEndian?: boolean): number;
267
+ read(tt: ComponentTypeHeader.Int64, littleEndian?: boolean): bigint;
268
+ read(tt: ComponentTypeHeader.Float, littleEndian?: boolean): number;
269
+ read(tt: ComponentTypeHeader.Double, littleEndian?: boolean): number;
270
+ read(tt: ComponentTypeHeader.Boolean, littleEndian?: boolean): boolean;
271
+ read(tt: ComponentTypeHeader.ByteArray, littleEndian?: boolean): Buffer;
272
+ /**
273
+ *
274
+ */
275
+ toBuffer(): Buffer;
276
+ /**
277
+ * https://stackoverflow.com/questions/8609289/convert-a-binary-nodejs-buffer-to-javascript-arraybuffer
278
+ */
279
+ toArrayBuffer(): ArrayBuffer;
280
+ /**
281
+ *
282
+ */
283
+ at(idx: number): number;
284
+ /**
285
+ * Advanced the buffer reader by pffset.
286
+ */
287
+ advanceOffset(relativeOffset?: number): this;
288
+ /**
289
+ * This function reads how many bytes a normal integer would take
290
+ * as a 7-bit number
291
+ *
292
+ * 1(000 0001) 0(111 1110)
293
+ */
294
+ static length7BitInt(value: number): number;
295
+ /**
296
+ * Reads in an integer in 7-bit notation. A 7-bit integer
297
+ * encoding splits a number into a variable size of bits,
298
+ * in which the first bit is set while bytes are following.
299
+ *
300
+ * @example
301
+ *
302
+ * ```
303
+ * 1111 0000 1010 1010 1000 0000 0000 0001 Reading In
304
+ * ^--- ---- ^--- ---- ^--- ---- ^--- ----
305
+ * 111 0000 010 1010 000 0000 000 0001 Writing Out
306
+ * ```
307
+ */
308
+ read7BitInt(): number;
309
+ /**
310
+ * Write a normal integer value into buffer at offset.
311
+ */
312
+ write7BitInt(value: number): void;
313
+ /**
314
+ * Reads a dynamic buffer which is prepended by its' length
315
+ * in 7-bit encoding.
316
+ */
317
+ readDynamicBuffer(): Buffer<ArrayBufferLike>;
318
+ /**
319
+ * Append a buffer to the current buffer. Asserts the cursor
320
+ * to be at the end of the current buffer.
321
+ */
322
+ append(buffer: Buffer): this;
323
+ /**
324
+ * Keep Deserializing the buffer for typed data until
325
+ * you reach the end of the buffer. Typed data consists
326
+ * of a type indicator in 7-bit-encoding and data following
327
+ * accordingly.
328
+ */
329
+ deserialize(): (string | number | bigint | boolean | Buffer<ArrayBufferLike>)[];
330
+ }