UNPKG

typia

Version:

Superfast runtime validators with only one line

1 lines 5.6 kB
{"version":3,"file":"_ProtobufSizer.mjs","names":[],"sources":["../../src/internal/_ProtobufSizer.ts"],"sourcesContent":["import { _IProtobufWriter } from \"./_IProtobufWriter\";\n\n/// @reference https://github.com/piotr-oles/as-proto/blob/main/packages/as-proto/assembly/internal/FixedSizer.ts\nexport class _ProtobufSizer implements _IProtobufWriter {\n /** Total length. */\n public len: number;\n\n /** Position stack. */\n public readonly pos: Array<number>;\n\n /** Variable length list. */\n public readonly varlen: Array<number>;\n\n /** Variable length index stack. */\n public readonly varlenidx: Array<number>;\n\n public constructor(length: number = 0) {\n this.len = length;\n this.pos = [];\n this.varlen = [];\n this.varlenidx = [];\n }\n\n public bool(): void {\n this.len += 1;\n }\n public int32(value: number): void {\n if (value < 0) {\n // 10 bytes to encode negative number\n this.len += 10;\n } else {\n this.varint32(value);\n }\n }\n public sint32(value: number): void {\n this.varint32((value << 1) ^ (value >> 31));\n }\n public uint32(value: number): void {\n this.varint32(value);\n }\n\n public int64(value: bigint | number): void {\n this.varint64(typeof value === \"number\" ? BigInt(value) : value);\n }\n public sint64(value: bigint | number): void {\n if (typeof value === \"number\") value = BigInt(value);\n this.varint64((value << BigInt(1)) ^ (value >> BigInt(63)));\n }\n public uint64(value: bigint | number): void {\n this.varint64(typeof value === \"number\" ? BigInt(value) : value);\n }\n\n // public fixed32(_value: number): void {\n // this.len += 4;\n // }\n // public sfixed32(_value: number): void {\n // this.len += 4;\n // }\n // public fixed64(_value: number | bigint): void {\n // this.len += 8;\n // }\n // public sfixed64(_value: number | bigint): void {\n // this.len += 8;\n // }\n public float(_value: number): void {\n this.len += 4;\n }\n public double(_value: number): void {\n this.len += 8;\n }\n\n public bytes(value: Uint8Array): void {\n this.uint32(value.byteLength);\n this.len += value.byteLength;\n }\n public string(value: string): void {\n const len: number = strlen(value);\n this.varlen.push(len);\n this.uint32(len);\n this.len += len;\n }\n\n public fork(): void {\n this.pos.push(this.len); // save current position\n this.varlenidx.push(this.varlen.length); // save current index in varlen array\n this.varlen.push(0); // add 0 length to varlen array (to be updated in ldelim())\n }\n\n public ldelim(): void {\n if (!(this.pos.length && this.varlenidx.length))\n throw new Error(\n \"Error on typia.protobuf.encode(): missing fork() before ldelim() call.\",\n );\n\n const endPos = this.len; // current position is end position\n const startPos = this.pos.pop()!; // get start position from stack\n const idx = this.varlenidx.pop()!; // get varlen index from stack\n const len = endPos - startPos; // calculate length\n\n this.varlen[idx] = len; // update variable length\n this.uint32(len); // add uint32 that should be called in fork()\n }\n\n public reset(): void {\n this.len = 0;\n // re-use arrays\n this.pos.length = 0;\n this.varlen.length = 0;\n this.varlenidx.length = 0;\n }\n\n private varint32(value: number): void {\n this.len +=\n value < 0\n ? 10 // 10 bits with leading 1's\n : value < 0x80\n ? 1\n : value < 0x4000\n ? 2\n : value < 0x200000\n ? 3\n : value < 0x10000000\n ? 4\n : 5;\n }\n\n private varint64(val: bigint): void {\n val = BigInt.asUintN(64, val);\n while (val > BigInt(0x7f)) {\n ++this.len;\n val = val >> BigInt(0x07);\n }\n ++this.len;\n }\n}\n\nconst strlen = (str: string): number => new Blob([str]).size;\n"],"mappings":";AAGA,IAAa,iBAAb,MAAwD;CAatD,YAAmB,SAAiB,GAAG;EACrC,KAAK,MAAM;EACX,KAAK,MAAM,CAAC;EACZ,KAAK,SAAS,CAAC;EACf,KAAK,YAAY,CAAC;CACpB;CAEA,OAAoB;EAClB,KAAK,OAAO;CACd;CACA,MAAa,OAAqB;EAChC,IAAI,QAAQ,GAEV,KAAK,OAAO;OAEZ,KAAK,SAAS,KAAK;CAEvB;CACA,OAAc,OAAqB;EACjC,KAAK,SAAU,SAAS,IAAM,SAAS,EAAG;CAC5C;CACA,OAAc,OAAqB;EACjC,KAAK,SAAS,KAAK;CACrB;CAEA,MAAa,OAA8B;EACzC,KAAK,SAAS,OAAO,UAAU,WAAW,OAAO,KAAK,IAAI,KAAK;CACjE;CACA,OAAc,OAA8B;EAC1C,IAAI,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