2 /*---------------------------------------------------------------------------------------------
3 * Copyright (c) Microsoft Corporation. All rights reserved.
4 * Licensed under the MIT License. See License.txt in the project root for license information.
5 *--------------------------------------------------------------------------------------------*/
6 Object.defineProperty(exports, "__esModule", { value: true });
7 exports.AbstractMessageBuffer = void 0;
11 class AbstractMessageBuffer {
12 constructor(encoding = 'utf-8') {
13 this._encoding = encoding;
15 this._totalLength = 0;
18 return this._encoding;
21 const toAppend = typeof chunk === 'string' ? this.fromString(chunk, this._encoding) : chunk;
22 this._chunks.push(toAppend);
23 this._totalLength += toAppend.byteLength;
26 if (this._chunks.length === 0) {
32 let chunkBytesRead = 0;
33 row: while (chunkIndex < this._chunks.length) {
34 const chunk = this._chunks[chunkIndex];
36 column: while (offset < chunk.length) {
37 const value = chunk[offset];
69 chunkBytesRead += chunk.byteLength;
75 // The buffer contains the two CRLF at the end. So we will
76 // have two empty lines after the split at the end as well.
77 const buffer = this._read(chunkBytesRead + offset);
78 const result = new Map();
79 const headers = this.toString(buffer, 'ascii').split(CRLF);
80 if (headers.length < 2) {
83 for (let i = 0; i < headers.length - 2; i++) {
84 const header = headers[i];
85 const index = header.indexOf(':');
87 throw new Error('Message header must separate key and value using :');
89 const key = header.substr(0, index);
90 const value = header.substr(index + 1).trim();
91 result.set(key, value);
96 if (this._totalLength < length) {
99 return this._read(length);
101 get numberOfBytes() {
102 return this._totalLength;
105 if (byteCount === 0) {
106 return this.emptyBuffer();
108 if (byteCount > this._totalLength) {
109 throw new Error(`Cannot read so many bytes!`);
111 if (this._chunks[0].byteLength === byteCount) {
112 // super fast path, precisely first chunk must be returned
113 const chunk = this._chunks[0];
114 this._chunks.shift();
115 this._totalLength -= byteCount;
116 return this.asNative(chunk);
118 if (this._chunks[0].byteLength > byteCount) {
119 // fast path, the reading is entirely within the first chunk
120 const chunk = this._chunks[0];
121 const result = this.asNative(chunk, byteCount);
122 this._chunks[0] = chunk.slice(byteCount);
123 this._totalLength -= byteCount;
126 const result = this.allocNative(byteCount);
127 let resultOffset = 0;
129 while (byteCount > 0) {
130 const chunk = this._chunks[chunkIndex];
131 if (chunk.byteLength > byteCount) {
132 // this chunk will survive
133 const chunkPart = chunk.slice(0, byteCount);
134 result.set(chunkPart, resultOffset);
135 resultOffset += byteCount;
136 this._chunks[chunkIndex] = chunk.slice(byteCount);
137 this._totalLength -= byteCount;
138 byteCount -= byteCount;
141 // this chunk will be entirely read
142 result.set(chunk, resultOffset);
143 resultOffset += chunk.byteLength;
144 this._chunks.shift();
145 this._totalLength -= chunk.byteLength;
146 byteCount -= chunk.byteLength;
152 exports.AbstractMessageBuffer = AbstractMessageBuffer;
153 //# sourceMappingURL=messageBuffer.js.map