| 1 |
// Copyright 2021-2026 Buf Technologies, Inc. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
var __asyncValues = (this && this.__asyncValues) || function (o) { |
| 15 |
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); |
| 16 |
var m = o[Symbol.asyncIterator], i; |
| 17 |
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); |
| 18 |
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } |
| 19 |
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } |
| 20 |
}; |
| 21 |
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); } |
| 22 |
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) { |
| 23 |
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); |
| 24 |
var g = generator.apply(thisArg, _arguments || []), i, q = []; |
| 25 |
return i = Object.create((typeof AsyncIterator === "function" ? AsyncIterator : Object).prototype), verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i; |
| 26 |
function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; } |
| 27 |
function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } } |
| 28 |
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } |
| 29 |
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } |
| 30 |
function fulfill(value) { resume("next", value); } |
| 31 |
function reject(value) { resume("throw", value); } |
| 32 |
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } |
| 33 |
}; |
| 34 |
import { toBinary } from "../to-binary.js"; |
| 35 |
import { BinaryReader, BinaryWriter } from "./binary-encoding.js"; |
| 36 |
import { fromBinary } from "../from-binary.js"; |
| 37 |
/** |
| 38 |
* Serialize a message, prefixing it with its size. |
| 39 |
* |
| 40 |
* A size-delimited message is a varint size in bytes, followed by exactly |
| 41 |
* that many bytes of a message serialized with the binary format. |
| 42 |
* |
| 43 |
* This size-delimited format is compatible with other implementations. |
| 44 |
* For details, see https://github.com/protocolbuffers/protobuf/issues/10229 |
| 45 |
*/ |
| 46 |
export function sizeDelimitedEncode(messageDesc, message, options) { |
| 47 |
const writer = new BinaryWriter(); |
| 48 |
writer.bytes(toBinary(messageDesc, message, options)); |
| 49 |
return writer.finish(); |
| 50 |
} |
| 51 |
// Default for SizeDelimitedDecodeOptions.readMaxBytes. |
| 52 |
const defaultReadMaxBytes = 64 * 1024 * 1024; // 64 MiB |
| 53 |
/** |
| 54 |
* A growable byte buffer. Used in place of a resizable ArrayBuffer, which is |
| 55 |
* not widely available. |
| 56 |
*/ |
| 57 |
class ByteBuffer { |
| 58 |
constructor() { |
| 59 |
this.buffer = new Uint8Array(0); |
| 60 |
this.length = 0; |
| 61 |
} |
| 62 |
get byteLength() { |
| 63 |
return this.length; |
| 64 |
} |
| 65 |
bytes() { |
| 66 |
return this.buffer.subarray(0, this.length); |
| 67 |
} |
| 68 |
append(chunk) { |
| 69 |
const newByteLength = this.length + chunk.byteLength; |
| 70 |
if (newByteLength > this.buffer.byteLength) { |
| 71 |
const grown = new Uint8Array(Math.max(this.buffer.byteLength * 2, newByteLength)); |
| 72 |
grown.set(this.buffer.subarray(0, this.length)); |
| 73 |
this.buffer = grown; |
| 74 |
} |
| 75 |
this.buffer.set(chunk, this.length); |
| 76 |
this.length += chunk.byteLength; |
| 77 |
} |
| 78 |
} |
| 79 |
/** |
| 80 |
* Parse a stream of size-delimited messages. |
| 81 |
* |
| 82 |
* A size-delimited message is a varint size in bytes, followed by exactly |
| 83 |
* that many bytes of a message serialized with the binary format. |
| 84 |
* |
| 85 |
* This size-delimited format is compatible with other implementations. |
| 86 |
* For details, see https://github.com/protocolbuffers/protobuf/issues/10229 |
| 87 |
* |
| 88 |
* Messages exceeding the limit given with the option readMaxBytes raise an |
| 89 |
* error. The limit is 64 MiB by default. All other options are the |
| 90 |
* standard binary read options, passed through to decode each message. |
| 91 |
*/ |
| 92 |
export function sizeDelimitedDecodeStream(messageDesc, iterable, options) { |
| 93 |
return __asyncGenerator(this, arguments, function* sizeDelimitedDecodeStream_1() { |
| 94 |
var _a, e_1, _b, _c; |
| 95 |
var _d; |
| 96 |
const readMaxBytes = (_d = options === null || options === void 0 ? void 0 : options.readMaxBytes) !== null && _d !== void 0 ? _d : defaultReadMaxBytes; |
| 97 |
let buffer = new ByteBuffer(); |
| 98 |
try { |
| 99 |
for (var _e = true, iterable_1 = __asyncValues(iterable), iterable_1_1; iterable_1_1 = yield __await(iterable_1.next()), _a = iterable_1_1.done, !_a; _e = true) { |
| 100 |
_c = iterable_1_1.value; |
| 101 |
_e = false; |
| 102 |
const chunk = _c; |
| 103 |
buffer.append(chunk); |
| 104 |
const bytes = buffer.bytes(); |
| 105 |
let offset = 0; |
| 106 |
for (;;) { |
| 107 |
const size = sizeDelimitedPeek(bytes.subarray(offset)); |
| 108 |
if (size.eof) { |
| 109 |
// size is incomplete, buffer more data |
| 110 |
break; |
| 111 |
} |
| 112 |
if (size.size > readMaxBytes) { |
| 113 |
throw new Error(`message size ${size.size} is larger than configured readMaxBytes ${readMaxBytes}`); |
| 114 |
} |
| 115 |
const messageStart = offset + size.offset; |
| 116 |
const messageEnd = messageStart + size.size; |
| 117 |
if (messageEnd > bytes.byteLength) { |
| 118 |
// message is incomplete, buffer more data |
| 119 |
break; |
| 120 |
} |
| 121 |
yield yield __await(fromBinary(messageDesc, bytes.subarray(messageStart, messageEnd), options)); |
| 122 |
offset = messageEnd; |
| 123 |
} |
| 124 |
if (offset > 0) { |
| 125 |
buffer = new ByteBuffer(); |
| 126 |
buffer.append(bytes.subarray(offset)); |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
catch (e_1_1) { e_1 = { error: e_1_1 }; } |
| 131 |
finally { |
| 132 |
try { |
| 133 |
if (!_e && !_a && (_b = iterable_1.return)) yield __await(_b.call(iterable_1)); |
| 134 |
} |
| 135 |
finally { if (e_1) throw e_1.error; } |
| 136 |
} |
| 137 |
if (buffer.byteLength > 0) { |
| 138 |
throw new Error("incomplete data"); |
| 139 |
} |
| 140 |
}); |
| 141 |
} |
| 142 |
/** |
| 143 |
* Decodes the size from the given size-delimited message, which may be |
| 144 |
* incomplete. |
| 145 |
* |
| 146 |
* Returns an object with the following properties: |
| 147 |
* - size: The size of the delimited message in bytes |
| 148 |
* - offset: The offset in the given byte array where the message starts |
| 149 |
* - eof: true |
| 150 |
* |
| 151 |
* If the size-delimited data does not include all bytes of the varint size, |
| 152 |
* the following object is returned: |
| 153 |
* - size: null |
| 154 |
* - offset: null |
| 155 |
* - eof: false |
| 156 |
* |
| 157 |
* This function can be used to implement parsing of size-delimited messages |
| 158 |
* from a stream. |
| 159 |
*/ |
| 160 |
export function sizeDelimitedPeek(data) { |
| 161 |
const sizeEof = { eof: true, size: null, offset: null }; |
| 162 |
for (let i = 0; i < 10; i++) { |
| 163 |
if (i > data.byteLength) { |
| 164 |
return sizeEof; |
| 165 |
} |
| 166 |
if ((data[i] & 0x80) == 0) { |
| 167 |
const reader = new BinaryReader(data); |
| 168 |
let size; |
| 169 |
try { |
| 170 |
size = reader.uint32(); |
| 171 |
} |
| 172 |
catch (e) { |
| 173 |
if (e instanceof RangeError) { |
| 174 |
return sizeEof; |
| 175 |
} |
| 176 |
throw e; |
| 177 |
} |
| 178 |
return { |
| 179 |
eof: false, |
| 180 |
size, |
| 181 |
offset: reader.pos, |
| 182 |
}; |
| 183 |
} |
| 184 |
} |
| 185 |
throw new Error("invalid varint"); |
| 186 |
} |