UNPKG

5.69 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Uint8ArrayReadBuffer = exports.Uint8ArrayWriteBuffer = void 0;
4const event_1 = require("../event");
5/**
6 * The default {@link WriteBuffer} implementation. Uses a {@link Uint8Array} for buffering.
7 * The {@link Uint8ArrayWriteBuffer.onCommit} hook can be used to rect to on-commit events.
8 * After the {@link Uint8ArrayWriteBuffer.commit} method has been called the buffer is disposed
9 * and can no longer be used for writing data. If the writer buffer is no longer needed but the message
10 * has not been committed yet it has to be disposed manually.
11 */
12class Uint8ArrayWriteBuffer {
13 constructor(buffer = new Uint8Array(1024), writePosition = 0) {
14 this.buffer = buffer;
15 this.encoder = new TextEncoder();
16 this.isDisposed = false;
17 this.onCommitEmitter = new event_1.Emitter();
18 this.offset = buffer.byteOffset + writePosition;
19 this.msg = new DataView(buffer.buffer);
20 }
21 ensureCapacity(value) {
22 let newLength = this.buffer.byteLength;
23 while (newLength < this.offset + value) {
24 newLength *= 2;
25 }
26 if (newLength !== this.buffer.byteLength) {
27 const newBuffer = new Uint8Array(newLength);
28 newBuffer.set(this.buffer);
29 this.buffer = newBuffer;
30 this.msg = new DataView(this.buffer.buffer);
31 }
32 return this;
33 }
34 writeLength(length) {
35 if (length < 0 || (length % 1) !== 0) {
36 throw new Error(`Could not write the given length value. '${length}' is not an integer >= 0`);
37 }
38 if (length < 127) {
39 this.writeUint8(length);
40 }
41 else {
42 this.writeUint8(128 + (length & 127));
43 this.writeLength(length >> 7);
44 }
45 return this;
46 }
47 writeNumber(value) {
48 this.ensureCapacity(8);
49 this.msg.setFloat64(this.offset, value);
50 this.offset += 8;
51 return this;
52 }
53 writeUint8(value) {
54 this.ensureCapacity(1);
55 this.buffer[this.offset++] = value;
56 return this;
57 }
58 writeUint16(value) {
59 this.ensureCapacity(2);
60 this.msg.setUint16(this.offset, value);
61 this.offset += 2;
62 return this;
63 }
64 writeUint32(value) {
65 this.ensureCapacity(4);
66 this.msg.setUint32(this.offset, value);
67 this.offset += 4;
68 return this;
69 }
70 writeString(value) {
71 this.ensureCapacity(4 * value.length);
72 const result = this.encoder.encodeInto(value, this.buffer.subarray(this.offset + 4));
73 this.msg.setUint32(this.offset, result.written);
74 this.offset += 4 + result.written;
75 return this;
76 }
77 writeBytes(value) {
78 this.writeLength(value.byteLength);
79 this.ensureCapacity(value.length);
80 this.buffer.set(value, this.offset);
81 this.offset += value.length;
82 return this;
83 }
84 get onCommit() {
85 return this.onCommitEmitter.event;
86 }
87 commit() {
88 if (this.isDisposed) {
89 throw new Error("Could not invoke 'commit'. The WriteBuffer is already disposed.");
90 }
91 this.onCommitEmitter.fire(this.getCurrentContents());
92 this.dispose();
93 }
94 getCurrentContents() {
95 return this.buffer.slice(this.buffer.byteOffset, this.offset);
96 }
97 dispose() {
98 if (!this.isDisposed) {
99 this.onCommitEmitter.dispose();
100 this.isDisposed = true;
101 }
102 }
103}
104exports.Uint8ArrayWriteBuffer = Uint8ArrayWriteBuffer;
105/**
106 * The default {@link ReadBuffer} implementation. Uses a {@link Uint8Array} for buffering.
107 * Is for single message read. A message can only be read once.
108 */
109class Uint8ArrayReadBuffer {
110 constructor(buffer, readPosition = 0) {
111 this.buffer = buffer;
112 this.offset = 0;
113 this.decoder = new TextDecoder();
114 this.offset = buffer.byteOffset + readPosition;
115 this.msg = new DataView(buffer.buffer);
116 }
117 readUint8() {
118 return this.msg.getUint8(this.offset++);
119 }
120 readUint16() {
121 const result = this.msg.getUint16(this.offset);
122 this.offset += 2;
123 return result;
124 }
125 readUint32() {
126 const result = this.msg.getUint32(this.offset);
127 this.offset += 4;
128 return result;
129 }
130 readLength() {
131 let shift = 0;
132 let byte = this.readUint8();
133 let value = (byte & 127) << shift;
134 while (byte > 127) {
135 shift += 7;
136 byte = this.readUint8();
137 value = value + ((byte & 127) << shift);
138 }
139 return value;
140 }
141 readNumber() {
142 const result = this.msg.getFloat64(this.offset);
143 this.offset += 8;
144 return result;
145 }
146 readString() {
147 const len = this.readUint32();
148 const sliceOffset = this.offset - this.buffer.byteOffset;
149 const result = this.decodeString(this.buffer.slice(sliceOffset, sliceOffset + len));
150 this.offset += len;
151 return result;
152 }
153 decodeString(buf) {
154 return this.decoder.decode(buf);
155 }
156 readBytes() {
157 const length = this.readLength();
158 const sliceOffset = this.offset - this.buffer.byteOffset;
159 const result = this.buffer.slice(sliceOffset, sliceOffset + length);
160 this.offset += length;
161 return result;
162 }
163 sliceAtReadPosition() {
164 const sliceOffset = this.offset - this.buffer.byteOffset;
165 return new Uint8ArrayReadBuffer(this.buffer, sliceOffset);
166 }
167}
168exports.Uint8ArrayReadBuffer = Uint8ArrayReadBuffer;
169//# sourceMappingURL=uint8-array-message-buffer.js.map
\No newline at end of file