lint
This commit is contained in:
977 files changed
+198426
-158324
No files matched your search
@@ -1,207 +1,230 @@
|
||||
import { WireType, PRIMITIVE_TYPE_MAP, type ProtobufField, type ProtobufMessage, type MessageRegistry } from '../ast/types.js';
|
||||
import {
|
||||
WireType,
|
||||
PRIMITIVE_TYPE_MAP,
|
||||
type ProtobufField,
|
||||
type ProtobufMessage,
|
||||
type MessageRegistry,
|
||||
} from '../ast/types.js';
|
||||
|
||||
function tagValue(field: ProtobufField): number {
|
||||
const wireType = field.isMessage || field.typeName === 'string' || field.typeName === 'bytes'
|
||||
? WireType.LengthDelim
|
||||
: field.wireType;
|
||||
return ((field.fieldNumber << 3) | wireType) >>> 0;
|
||||
const wireType =
|
||||
field.isMessage || field.typeName === 'string' || field.typeName === 'bytes'
|
||||
? WireType.LengthDelim
|
||||
: field.wireType;
|
||||
return ((field.fieldNumber << 3) | wireType) >>> 0;
|
||||
}
|
||||
|
||||
/** Emit inline varint decode, storing result in `varName`. */
|
||||
function varintDec(varName: string, ind: string): string {
|
||||
return [
|
||||
`${ind}const [${varName}, ${varName}_offset] = __readVarint32(data, offset, end);`,
|
||||
`${ind}offset = ${varName}_offset;`,
|
||||
].join('\n');
|
||||
return [
|
||||
`${ind}const [${varName}, ${varName}_offset] = __readVarint32(data, offset, end);`,
|
||||
`${ind}offset = ${varName}_offset;`,
|
||||
].join('\n');
|
||||
}
|
||||
|
||||
function varintDec64(varName: string, ind: string): string {
|
||||
return [
|
||||
`${ind}const [${varName}, ${varName}_offset] = __readVarint64(data, offset, end);`,
|
||||
`${ind}offset = ${varName}_offset;`,
|
||||
].join('\n');
|
||||
return [
|
||||
`${ind}const [${varName}, ${varName}_offset] = __readVarint64(data, offset, end);`,
|
||||
`${ind}offset = ${varName}_offset;`,
|
||||
].join('\n');
|
||||
}
|
||||
|
||||
function varintDec32Value(varName: string, ind: string): string {
|
||||
return [
|
||||
`${ind}const [${varName}, ${varName}_offset] = __readVarint32Value(data, offset, end);`,
|
||||
`${ind}offset = ${varName}_offset;`,
|
||||
].join('\n');
|
||||
return [
|
||||
`${ind}const [${varName}, ${varName}_offset] = __readVarint32Value(data, offset, end);`,
|
||||
`${ind}offset = ${varName}_offset;`,
|
||||
].join('\n');
|
||||
}
|
||||
|
||||
function isVarint64(typeName: string): boolean {
|
||||
return typeName === 'uint_64' || typeName === 'int_64' || typeName === 'sint_64';
|
||||
return typeName === 'uint_64' || typeName === 'int_64' || typeName === 'sint_64';
|
||||
}
|
||||
|
||||
function isFixed64BigInt(typeName: string): boolean {
|
||||
return typeName === 'fixed_64' || typeName === 'sfixed_64';
|
||||
return typeName === 'fixed_64' || typeName === 'sfixed_64';
|
||||
}
|
||||
|
||||
const INLINE_SKIP = ` offset = __skipUnknownField(data, offset, end, wireType, _tag >>> 3);`;
|
||||
|
||||
export function generateDecoder(msg: ProtobufMessage, _registry: MessageRegistry): string {
|
||||
const locals = msg.fields.map((field, index) => {
|
||||
const keyword = field.isRepeated ? 'const' : 'let';
|
||||
return ` ${keyword} _f${index} = ${getDefault(field)};`;
|
||||
});
|
||||
const result = msg.fields.map((field, index) => `${field.name}: _f${index}`).join(', ');
|
||||
const locals = msg.fields.map((field, index) => {
|
||||
const keyword = field.isRepeated ? 'const' : 'let';
|
||||
return ` ${keyword} _f${index} = ${getDefault(field)};`;
|
||||
});
|
||||
const result = msg.fields.map((field, index) => `${field.name}: _f${index}`).join(', ');
|
||||
|
||||
const L = [
|
||||
`function protobuf_decode_${msg.name}(data, offset = 0, end = data.length) {`,
|
||||
` if (!Number.isSafeInteger(offset) || !Number.isSafeInteger(end) || offset < 0 || end < offset || end > data.length) {`,
|
||||
` throw new Error('protobuf decoder bounds are invalid');`,
|
||||
` }`,
|
||||
...locals,
|
||||
` let _unknownFields = null;`,
|
||||
` let _unknownFieldsByKey = null;`,
|
||||
` let _unknownTotalOccurrences = 0;`,
|
||||
` let _unknownOmittedOccurrences = 0;`,
|
||||
` let _unknownOmittedByteLength = 0;`,
|
||||
` while (offset < end) {`,
|
||||
` const _fieldStart = offset;`,
|
||||
` const [_tag, _tagOffset] = __readVarint32(data, offset, end);`,
|
||||
` offset = _tagOffset;`,
|
||||
` if ((_tag >>> 3) === 0) throw new Error('protobuf field number 0 is invalid');`,
|
||||
` switch (_tag) {`,
|
||||
];
|
||||
const L = [
|
||||
`function protobuf_decode_${msg.name}(data, offset = 0, end = data.length) {`,
|
||||
` if (!Number.isSafeInteger(offset) || !Number.isSafeInteger(end) || offset < 0 || end < offset || end > data.length) {`,
|
||||
` throw new Error('protobuf decoder bounds are invalid');`,
|
||||
` }`,
|
||||
...locals,
|
||||
` let _unknownFields = null;`,
|
||||
` let _unknownFieldsByKey = null;`,
|
||||
` let _unknownTotalOccurrences = 0;`,
|
||||
` let _unknownOmittedOccurrences = 0;`,
|
||||
` let _unknownOmittedByteLength = 0;`,
|
||||
` while (offset < end) {`,
|
||||
` const _fieldStart = offset;`,
|
||||
` const [_tag, _tagOffset] = __readVarint32(data, offset, end);`,
|
||||
` offset = _tagOffset;`,
|
||||
` if ((_tag >>> 3) === 0) throw new Error('protobuf field number 0 is invalid');`,
|
||||
` switch (_tag) {`,
|
||||
];
|
||||
|
||||
msg.fields.forEach((field, index) => {
|
||||
L.push(decodeField(field, index));
|
||||
});
|
||||
msg.fields.forEach((field, index) => {
|
||||
L.push(decodeField(field, index));
|
||||
});
|
||||
|
||||
L.push(
|
||||
` default: {`,
|
||||
` const _unknownStart = offset;`,
|
||||
` const wireType = _tag & 0x7;`,
|
||||
INLINE_SKIP,
|
||||
` const _unknownFieldNumber = _tag >>> 3;`,
|
||||
` const _unknownByteLength = offset - _unknownStart;`,
|
||||
` const _unknownKey = (_unknownFieldNumber * 8) + wireType;`,
|
||||
` _unknownTotalOccurrences++;`,
|
||||
` const _knownUnknown = _unknownFieldsByKey?.get(_unknownKey);`,
|
||||
` if (_knownUnknown) {`,
|
||||
` _knownUnknown.count++;`,
|
||||
` _knownUnknown.totalByteLength += _unknownByteLength;`,
|
||||
` } else if ((_unknownFields?.length ?? 0) < 64) {`,
|
||||
` const _unknown = {`,
|
||||
` fieldNumber: _unknownFieldNumber,`,
|
||||
` wireType,`,
|
||||
` count: 1,`,
|
||||
` totalByteLength: _unknownByteLength,`,
|
||||
` };`,
|
||||
` (_unknownFields ??= []).push(_unknown);`,
|
||||
` (_unknownFieldsByKey ??= new Map()).set(_unknownKey, _unknown);`,
|
||||
` } else {`,
|
||||
` _unknownOmittedOccurrences++;`,
|
||||
` _unknownOmittedByteLength += _unknownByteLength;`,
|
||||
` }`,
|
||||
` break;`,
|
||||
` }`,
|
||||
` }`,
|
||||
` if (offset <= _fieldStart || offset > end) throw new Error('protobuf decoder made invalid progress');`,
|
||||
` }`,
|
||||
` const _result = { ${result} };`,
|
||||
` if (_unknownTotalOccurrences > 0) Object.defineProperty(`,
|
||||
` _result,`,
|
||||
` Symbol.for('snowluma.proton.unknownFields'),`,
|
||||
` {`,
|
||||
` value: {`,
|
||||
` fields: _unknownFields ?? [],`,
|
||||
` totalOccurrences: _unknownTotalOccurrences,`,
|
||||
` omittedOccurrences: _unknownOmittedOccurrences,`,
|
||||
` omittedByteLength: _unknownOmittedByteLength,`,
|
||||
` },`,
|
||||
` enumerable: false,`,
|
||||
` },`,
|
||||
` );`,
|
||||
` return _result;`,
|
||||
`}`,
|
||||
);
|
||||
return L.join('\n');
|
||||
L.push(
|
||||
` default: {`,
|
||||
` const _unknownStart = offset;`,
|
||||
` const wireType = _tag & 0x7;`,
|
||||
INLINE_SKIP,
|
||||
` const _unknownFieldNumber = _tag >>> 3;`,
|
||||
` const _unknownByteLength = offset - _unknownStart;`,
|
||||
` const _unknownKey = (_unknownFieldNumber * 8) + wireType;`,
|
||||
` _unknownTotalOccurrences++;`,
|
||||
` const _knownUnknown = _unknownFieldsByKey?.get(_unknownKey);`,
|
||||
` if (_knownUnknown) {`,
|
||||
` _knownUnknown.count++;`,
|
||||
` _knownUnknown.totalByteLength += _unknownByteLength;`,
|
||||
` } else if ((_unknownFields?.length ?? 0) < 64) {`,
|
||||
` const _unknown = {`,
|
||||
` fieldNumber: _unknownFieldNumber,`,
|
||||
` wireType,`,
|
||||
` count: 1,`,
|
||||
` totalByteLength: _unknownByteLength,`,
|
||||
` };`,
|
||||
` (_unknownFields ??= []).push(_unknown);`,
|
||||
` (_unknownFieldsByKey ??= new Map()).set(_unknownKey, _unknown);`,
|
||||
` } else {`,
|
||||
` _unknownOmittedOccurrences++;`,
|
||||
` _unknownOmittedByteLength += _unknownByteLength;`,
|
||||
` }`,
|
||||
` break;`,
|
||||
` }`,
|
||||
` }`,
|
||||
` if (offset <= _fieldStart || offset > end) throw new Error('protobuf decoder made invalid progress');`,
|
||||
` }`,
|
||||
` const _result = { ${result} };`,
|
||||
` if (_unknownTotalOccurrences > 0) Object.defineProperty(`,
|
||||
` _result,`,
|
||||
` Symbol.for('snowluma.proton.unknownFields'),`,
|
||||
` {`,
|
||||
` value: {`,
|
||||
` fields: _unknownFields ?? [],`,
|
||||
` totalOccurrences: _unknownTotalOccurrences,`,
|
||||
` omittedOccurrences: _unknownOmittedOccurrences,`,
|
||||
` omittedByteLength: _unknownOmittedByteLength,`,
|
||||
` },`,
|
||||
` enumerable: false,`,
|
||||
` },`,
|
||||
` );`,
|
||||
` return _result;`,
|
||||
`}`,
|
||||
);
|
||||
return L.join('\n');
|
||||
}
|
||||
|
||||
function decodeField(field: ProtobufField, index: number): string {
|
||||
const { typeName, wireType, isMessage, isRepeated } = field;
|
||||
const I = ' ';
|
||||
const local = `_f${index}`;
|
||||
const assign = (expr: string) => isRepeated
|
||||
? `${I}${local}.push(${expr});`
|
||||
: `${I}${local} = ${expr};`;
|
||||
const { typeName, wireType, isMessage, isRepeated } = field;
|
||||
const I = ' ';
|
||||
const local = `_f${index}`;
|
||||
const assign = (expr: string) =>
|
||||
isRepeated ? `${I}${local}.push(${expr});` : `${I}${local} = ${expr};`;
|
||||
|
||||
const L: string[] = [` case ${tagValue(field)}: {`];
|
||||
const L: string[] = [` case ${tagValue(field)}: {`];
|
||||
|
||||
if (isMessage) {
|
||||
L.push(varintDec('_len', I));
|
||||
L.push(`${I}const _end = __checkedEnd(offset, _len, end);`);
|
||||
L.push(assign(`protobuf_decode_${typeName}(data, offset, _end)`));
|
||||
L.push(`${I}offset = _end;`);
|
||||
} else if (typeName === 'string') {
|
||||
L.push(varintDec('_len', I));
|
||||
L.push(`${I}const _end = __checkedEnd(offset, _len, end);`);
|
||||
L.push(assign(`__td.decode(data.subarray(offset, _end))`));
|
||||
L.push(`${I}offset = _end;`);
|
||||
} else if (typeName === 'bytes') {
|
||||
L.push(varintDec('_len', I));
|
||||
L.push(`${I}const _end = __checkedEnd(offset, _len, end);`);
|
||||
L.push(assign(`data.slice(offset, _end)`));
|
||||
L.push(`${I}offset = _end;`);
|
||||
} else if (typeName === 'bool') {
|
||||
L.push(varintDec('_val', I));
|
||||
L.push(assign(`_val !== 0`));
|
||||
} else if (isVarint64(typeName)) {
|
||||
L.push(varintDec64('_val', I));
|
||||
if (typeName === 'uint_64') {
|
||||
L.push(assign(`_val`));
|
||||
} else if (typeName === 'int_64') {
|
||||
L.push(assign(`BigInt.asIntN(64, _val)`));
|
||||
} else {
|
||||
L.push(assign(`__zigZagDecode64(_val)`));
|
||||
if (isMessage) {
|
||||
L.push(varintDec('_len', I));
|
||||
L.push(`${I}const _end = __checkedEnd(offset, _len, end);`);
|
||||
L.push(assign(`protobuf_decode_${typeName}(data, offset, _end)`));
|
||||
L.push(`${I}offset = _end;`);
|
||||
} else if (typeName === 'string') {
|
||||
L.push(varintDec('_len', I));
|
||||
L.push(`${I}const _end = __checkedEnd(offset, _len, end);`);
|
||||
L.push(assign(`__td.decode(data.subarray(offset, _end))`));
|
||||
L.push(`${I}offset = _end;`);
|
||||
} else if (typeName === 'bytes') {
|
||||
L.push(varintDec('_len', I));
|
||||
L.push(`${I}const _end = __checkedEnd(offset, _len, end);`);
|
||||
L.push(assign(`data.slice(offset, _end)`));
|
||||
L.push(`${I}offset = _end;`);
|
||||
} else if (typeName === 'bool') {
|
||||
L.push(varintDec('_val', I));
|
||||
L.push(assign(`_val !== 0`));
|
||||
} else if (isVarint64(typeName)) {
|
||||
L.push(varintDec64('_val', I));
|
||||
|
||||
if (typeName === 'uint_64') {
|
||||
L.push(assign(`_val`));
|
||||
} else if (typeName === 'int_64') {
|
||||
L.push(assign(`BigInt.asIntN(64, _val)`));
|
||||
} else {
|
||||
L.push(assign(`__zigZagDecode64(_val)`));
|
||||
}
|
||||
} else if (typeName === 'int_32') {
|
||||
// Protobuf encodes negative int32 values as a sign-extended 10-byte
|
||||
// varint. Read the full uint64 representation, then keep its signed low
|
||||
// 32 bits.
|
||||
L.push(varintDec64('_val', I));
|
||||
L.push(assign(`Number(BigInt.asIntN(32, _val))`));
|
||||
} else if (typeName === 'sint_32') {
|
||||
L.push(varintDec('_val', I));
|
||||
L.push(assign(`(_val >>> 1) ^ -(_val & 1)`));
|
||||
} else if (wireType === WireType.Varint) {
|
||||
L.push(varintDec32Value('_val', I));
|
||||
L.push(assign(`_val >>> 0`));
|
||||
} else if (typeName === 'float') {
|
||||
L.push(`${I}if (end - offset < 4) throw new Error('protobuf truncated float field');`);
|
||||
L.push(assign(`__readFloat32(data, offset)`));
|
||||
L.push(`${I}offset += 4;`);
|
||||
} else if (wireType === WireType.Bit32) {
|
||||
L.push(`${I}if (end - offset < 4) throw new Error('protobuf truncated fixed32 field');`);
|
||||
L.push(
|
||||
assign(
|
||||
`data[offset] | (data[offset + 1] << 8) | (data[offset + 2] << 16) | (data[offset + 3] << 24)`,
|
||||
),
|
||||
);
|
||||
L.push(`${I}offset += 4;`);
|
||||
} else if (typeName === 'double') {
|
||||
L.push(`${I}if (end - offset < 8) throw new Error('protobuf truncated double field');`);
|
||||
L.push(assign(`__readFloat64(data, offset)`));
|
||||
L.push(`${I}offset += 8;`);
|
||||
} else if (isFixed64BigInt(typeName)) {
|
||||
L.push(`${I}if (end - offset < 8) throw new Error('protobuf truncated fixed64 field');`);
|
||||
|
||||
if (typeName === 'fixed_64') {
|
||||
L.push(assign(`__readFixed64(data, offset)`));
|
||||
} else {
|
||||
L.push(assign(`BigInt.asIntN(64, __readFixed64(data, offset))`));
|
||||
}
|
||||
|
||||
L.push(`${I}offset += 8;`);
|
||||
} else if (wireType === WireType.Bit64) {
|
||||
L.push(`${I}if (end - offset < 8) throw new Error('protobuf truncated fixed64 field');`);
|
||||
L.push(
|
||||
assign(
|
||||
`data[offset] | (data[offset + 1] << 8) | (data[offset + 2] << 16) | (data[offset + 3] << 24)`,
|
||||
),
|
||||
);
|
||||
L.push(`${I}offset += 8;`);
|
||||
}
|
||||
} else if (typeName === 'int_32') {
|
||||
// Protobuf encodes negative int32 values as a sign-extended 10-byte
|
||||
// varint. Read the full uint64 representation, then keep its signed low
|
||||
// 32 bits.
|
||||
L.push(varintDec64('_val', I));
|
||||
L.push(assign(`Number(BigInt.asIntN(32, _val))`));
|
||||
} else if (typeName === 'sint_32') {
|
||||
L.push(varintDec('_val', I));
|
||||
L.push(assign(`(_val >>> 1) ^ -(_val & 1)`));
|
||||
} else if (wireType === WireType.Varint) {
|
||||
L.push(varintDec32Value('_val', I));
|
||||
L.push(assign(`_val >>> 0`));
|
||||
} else if (typeName === 'float') {
|
||||
L.push(`${I}if (end - offset < 4) throw new Error('protobuf truncated float field');`);
|
||||
L.push(assign(`__readFloat32(data, offset)`));
|
||||
L.push(`${I}offset += 4;`);
|
||||
} else if (wireType === WireType.Bit32) {
|
||||
L.push(`${I}if (end - offset < 4) throw new Error('protobuf truncated fixed32 field');`);
|
||||
L.push(assign(`data[offset] | (data[offset + 1] << 8) | (data[offset + 2] << 16) | (data[offset + 3] << 24)`));
|
||||
L.push(`${I}offset += 4;`);
|
||||
} else if (typeName === 'double') {
|
||||
L.push(`${I}if (end - offset < 8) throw new Error('protobuf truncated double field');`);
|
||||
L.push(assign(`__readFloat64(data, offset)`));
|
||||
L.push(`${I}offset += 8;`);
|
||||
} else if (isFixed64BigInt(typeName)) {
|
||||
L.push(`${I}if (end - offset < 8) throw new Error('protobuf truncated fixed64 field');`);
|
||||
if (typeName === 'fixed_64') {
|
||||
L.push(assign(`__readFixed64(data, offset)`));
|
||||
} else {
|
||||
L.push(assign(`BigInt.asIntN(64, __readFixed64(data, offset))`));
|
||||
}
|
||||
L.push(`${I}offset += 8;`);
|
||||
} else if (wireType === WireType.Bit64) {
|
||||
L.push(`${I}if (end - offset < 8) throw new Error('protobuf truncated fixed64 field');`);
|
||||
L.push(assign(`data[offset] | (data[offset + 1] << 8) | (data[offset + 2] << 16) | (data[offset + 3] << 24)`));
|
||||
L.push(`${I}offset += 8;`);
|
||||
}
|
||||
|
||||
L.push(`${I}break;`, ` }`);
|
||||
return L.join('\n');
|
||||
L.push(`${I}break;`, ` }`);
|
||||
return L.join('\n');
|
||||
}
|
||||
|
||||
function getDefault(field: ProtobufField): string {
|
||||
if (field.isRepeated) return '[]';
|
||||
if (field.isOptional || field.isMessage) return 'null';
|
||||
const primitive = PRIMITIVE_TYPE_MAP[field.typeName];
|
||||
return primitive ? primitive.defaultValue : 'null';
|
||||
if (field.isRepeated) {
|
||||
return '[]';
|
||||
}
|
||||
|
||||
if (field.isOptional || field.isMessage) {
|
||||
return 'null';
|
||||
}
|
||||
|
||||
const primitive = PRIMITIVE_TYPE_MAP[field.typeName];
|
||||
return primitive ? primitive.defaultValue : 'null';
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -236,11 +236,16 @@ function __readFixed64(data, offset) {
|
||||
* No runtime imports needed — all wire-format logic is inlined.
|
||||
*/
|
||||
export function generateCode(registry: MessageRegistry): string {
|
||||
if (registry.size === 0) return '';
|
||||
const parts: string[] = [CODEGEN_PREAMBLE];
|
||||
for (const msg of registry.values()) {
|
||||
parts.push(generateEncoder(msg, registry));
|
||||
parts.push(generateDecoder(msg, registry));
|
||||
}
|
||||
return parts.join('\n');
|
||||
if (registry.size === 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const parts: string[] = [CODEGEN_PREAMBLE];
|
||||
|
||||
for (const msg of registry.values()) {
|
||||
parts.push(generateEncoder(msg, registry));
|
||||
parts.push(generateDecoder(msg, registry));
|
||||
}
|
||||
|
||||
return parts.join('\n');
|
||||
}
|
||||
@@ -2,51 +2,74 @@
|
||||
// The generated code no longer depends on these (everything is inlined).
|
||||
|
||||
export function encodeVarint(value: number, buf: number[]): void {
|
||||
value = value >>> 0;
|
||||
while (value > 0x7f) {
|
||||
buf.push((value & 0x7f) | 0x80);
|
||||
value = value >>> 7;
|
||||
}
|
||||
buf.push(value & 0x7f);
|
||||
value = value >>> 0;
|
||||
|
||||
while (value > 0x7f) {
|
||||
buf.push((value & 0x7f) | 0x80);
|
||||
value = value >>> 7;
|
||||
}
|
||||
|
||||
buf.push(value & 0x7f);
|
||||
}
|
||||
|
||||
export function decodeVarint(data: Uint8Array, offset: number): [number, number] {
|
||||
let result = 0;
|
||||
let shift = 0;
|
||||
let byte: number;
|
||||
do {
|
||||
byte = data[offset++];
|
||||
result |= (byte & 0x7f) << shift;
|
||||
shift += 7;
|
||||
} while (byte & 0x80);
|
||||
return [result >>> 0, offset];
|
||||
let result = 0;
|
||||
let shift = 0;
|
||||
let byte: number;
|
||||
|
||||
do {
|
||||
byte = data[offset++];
|
||||
result |= (byte & 0x7f) << shift;
|
||||
shift += 7;
|
||||
} while (byte & 0x80);
|
||||
|
||||
return [result >>> 0, offset];
|
||||
}
|
||||
|
||||
export function writeTag(fieldNumber: number, wireType: number, buf: number[]): void {
|
||||
encodeVarint((fieldNumber << 3) | wireType, buf);
|
||||
encodeVarint((fieldNumber << 3) | wireType, buf);
|
||||
}
|
||||
|
||||
export function writeString(value: string, buf: number[]): void {
|
||||
const encoded = new TextEncoder().encode(value);
|
||||
encodeVarint(encoded.length, buf);
|
||||
for (let i = 0; i < encoded.length; i++) buf.push(encoded[i]);
|
||||
const encoded = new TextEncoder().encode(value);
|
||||
encodeVarint(encoded.length, buf);
|
||||
|
||||
for (let i = 0; i < encoded.length; i++) {
|
||||
buf.push(encoded[i]);
|
||||
}
|
||||
}
|
||||
|
||||
export function readString(data: Uint8Array, offset: number, length: number): string {
|
||||
return new TextDecoder().decode(data.subarray(offset, offset + length));
|
||||
return new TextDecoder().decode(data.subarray(offset, offset + length));
|
||||
}
|
||||
|
||||
export function writeBytes(value: Uint8Array, buf: number[]): void {
|
||||
encodeVarint(value.length, buf);
|
||||
for (let i = 0; i < value.length; i++) buf.push(value[i]);
|
||||
encodeVarint(value.length, buf);
|
||||
|
||||
for (let i = 0; i < value.length; i++) {
|
||||
buf.push(value[i]);
|
||||
}
|
||||
}
|
||||
|
||||
export function skipField(data: Uint8Array, offset: number, wireType: number): number {
|
||||
switch (wireType) {
|
||||
case 0: while (data[offset] & 0x80) offset++; return offset + 1;
|
||||
case 1: return offset + 8;
|
||||
case 2: { const [len, o] = decodeVarint(data, offset); return o + len; }
|
||||
case 5: return offset + 4;
|
||||
default: throw new Error(`Unknown wire type: ${wireType}`);
|
||||
}
|
||||
switch (wireType) {
|
||||
case 0:
|
||||
while (data[offset] & 0x80) {
|
||||
offset++;
|
||||
}
|
||||
|
||||
return offset + 1;
|
||||
case 1:
|
||||
return offset + 8;
|
||||
|
||||
case 2: {
|
||||
const [len, o] = decodeVarint(data, offset);
|
||||
return o + len;
|
||||
}
|
||||
|
||||
case 5:
|
||||
return offset + 4;
|
||||
default:
|
||||
throw new Error(`Unknown wire type: ${wireType}`);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user