From ff5c57901d4762a83219ea7ecb22231e34f3b575 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Thu, 19 Nov 2026 18:53:03 +0100 Subject: [PATCH] refactor(codec)!: hide serialization internals The field decoders and buffer writers expose calling conventions that the public byte APIs already handle. Keep the hooks internal without changing the wire format or same-version CommonJS and ESM support. BREAKING CHANGE: Use encode(), encodeAsync() where available, or decode() instead of field-level decode and internal buffer methods. --- README.md | 4 + src/index.test.ts | 53 ++++- src/index.ts | 147 +++++++------ src/otel.test.ts | 7 +- src/otel.ts | 486 ++++++++++++++++++++++++++----------------- src/protobuf.test.ts | 28 ++- src/protobuf.ts | 40 ++-- src/string-table.ts | 38 +--- tsconfig.json | 1 + 9 files changed, 483 insertions(+), 321 deletions(-) diff --git a/README.md b/README.md index 511c2d0..a08ba94 100644 --- a/README.md +++ b/README.md @@ -164,3 +164,7 @@ const body = request.encode() // Decode from Uint8Array const copied = ExportProfilesServiceRequest.decode(body) ``` + +Use `encode()` and `decode()` for message bytes. If you supply an output +buffer, pass it to `encode(buffer)`. The field-level decode and buffer-writing +helpers are not part of the public API. diff --git a/src/index.test.ts b/src/index.test.ts index 9202dd7..39c4960 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -9,12 +9,13 @@ import type { TestContext } from 'node:test' import assert from 'node:assert' import { Buffer as NodeBuffer } from 'node:buffer' import { spawnSync } from 'node:child_process' +import { createRequire } from 'node:module' import { fileURLToPath } from 'node:url' import { gunzipSync } from 'zlib' import * as fs from 'fs' import proto from '../testing/proto/profile.js' -import { ProfilesDictionary } from './otel.js' +import { ProfilesData, ProfilesDictionary } from './otel.js' const {decode, toObject} = proto.perftools.profiles.Profile @@ -459,6 +460,56 @@ function bufToHex(buf: Uint8Array) { return Array.from(buf).map(hexNum).join('') } +test('string tables work across the CommonJS and ESM entry points', async () => { + const esm = await import('pprof-format') + const cjs: typeof esm = createRequire(import.meta.url)('pprof-format') + + for (const [input, output] of [[cjs, esm], [esm, cjs]]) { + const table = new input.StringTable() + table.dedup('cpu') + const profile = new output.Profile({ stringTable: table }) + assert.deepStrictEqual(output.Profile.decode(profile.encode()).stringTable.strings, ['', 'cpu']) + } +}) + +test('CommonJS labels are assignable to ESM samples', async () => { + const esm = await import('pprof-format') + const cjs: typeof import('pprof-format', { with: { 'resolution-mode': 'require' } }) = + createRequire(import.meta.url)('pprof-format') + const sample = new esm.Sample({}) + sample.label.push(new cjs.Label({ key: 1 })) + assert.strictEqual(esm.Sample.decode(sample.encode()).label[0].key, 1) +}) + +test('OTel dictionaries work across entry points with both encoders', async () => { + const esm = await import('pprof-format/otel') + const cjs: typeof esm = createRequire(import.meta.url)('pprof-format/otel') + + for (const [input, output] of [[cjs, esm], [esm, cjs]]) { + const table = new input.StringTable() + table.dedup('cpu') + const profiles = new output.ProfilesData({}) + profiles.dictionary = new input.ProfilesDictionary({ stringTable: table }) + + const encoded = await profiles.encodeAsync() + assert.deepStrictEqual(encoded, profiles.encode()) + assert.deepStrictEqual(output.ProfilesData.decode(encoded).dictionary?.stringTable.strings, ['', 'cpu']) + } +}) + +test('codec implementation methods are not exposed by the public classes', () => { + for (const type of [ValueType, Label, Sample, Profile, ProfilesData, ProfilesDictionary, StringTable]) { + assert.strictEqual(Reflect.has(type, 'decodeValue'), false) + assert.strictEqual(Reflect.has(type, 'decodeInput'), false) + assert.strictEqual(Reflect.has(type.prototype, '_measure'), false) + assert.strictEqual(Reflect.has(type.prototype, '_encodeToBuffer'), false) + assert.strictEqual(Reflect.has(type.prototype, '_encodeToBufferAsync'), false) + } + assert.strictEqual(Reflect.has(StringTable, '_encodeString'), false) + assert.strictEqual(Reflect.has(StringTable, '_encodeStringFromUtf8'), false) + assert.strictEqual(Reflect.has(StringTable.prototype, '_decodeString'), false) +}) + test('StringTable', async (t) => { await t.test('encodes correctly', async () => { const encodings = { diff --git a/src/index.ts b/src/index.ts index f48d009..c2e824d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,6 +19,9 @@ import { isLengthDelim, isPackedOrVarInt, isVarInt, + kDecodeValue, + kEncodeToBuffer, + kMeasure, measureBoolField, measureMessageArrayField, measureMessageField, @@ -31,7 +34,7 @@ import { verifyEncodedLength, yieldToEventLoop, } from './protobuf.js' -import { StringTable, emptyTableToken } from './string-table.js' +import { StringTable, emptyTableToken, kDecodeString } from './string-table.js' export { StringTable, emptyTableToken } @@ -129,20 +132,23 @@ export class ValueType extends Message { this.unit = data.unit || 0 } - _measure() { + /** @internal */ + [kMeasure]() { let total = 0 total += measureNumberField(ValueTypeField.type, this.type) total += measureNumberField(ValueTypeField.unit, this.unit) return total } - _encodeToBuffer(buffer: Uint8Array, offset = 0): number { + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset = 0): number { offset = encodeNumberField(buffer, offset, ValueTypeField.type, this.type) offset = encodeNumberField(buffer, offset, ValueTypeField.unit, this.unit) return offset } - static decodeValue(data: ValueTypeInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) { + /** @internal */ + static [kDecodeValue](data: ValueTypeInput, field: number, buffer: Uint8Array, start: number, end: number, wireType: number) { switch (field) { case ValueTypeField.type: if (isVarInt(wireType)) data.type = decodeNumber(buffer, start, end) @@ -154,7 +160,7 @@ export class ValueType extends Message { } static decode(buffer: Uint8Array, start = 0, end = buffer.length): ValueType { - return new this(decodeFields(buffer, this.decodeValue, start, end)) + return new this(decodeFields(buffer, this[kDecodeValue], start, end)) } } @@ -183,7 +189,8 @@ export class Label extends Message { this.numUnit = data.numUnit || 0 } - _measure() { + /** @internal */ + [kMeasure]() { let total = 0 total += measureNumberField(LabelField.key, this.key) total += measureNumberField(LabelField.str, this.str) @@ -192,7 +199,8 @@ export class Label extends Message { return total } - _encodeToBuffer(buffer: Uint8Array, offset = 0): number { + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset = 0): number { offset = encodeNumberField(buffer, offset, LabelField.key, this.key) offset = encodeNumberField(buffer, offset, LabelField.str, this.str) offset = encodeNumberField(buffer, offset, LabelField.num, this.num) @@ -200,7 +208,8 @@ export class Label extends Message { return offset } - static decodeValue(data: LabelInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) { + /** @internal */ + static [kDecodeValue](data: LabelInput, field: number, buffer: Uint8Array, start: number, end: number, wireType: number) { switch (field) { case LabelField.key: if (isVarInt(wireType)) data.key = decodeNumber(buffer, start, end) @@ -218,7 +227,7 @@ export class Label extends Message { } static decode(buffer: Uint8Array, start = 0, end = buffer.length): Label { - return new this(decodeFields(buffer, this.decodeValue, start, end)) + return new this(decodeFields(buffer, this[kDecodeValue], start, end)) } } @@ -244,7 +253,8 @@ export class Sample extends Message { this.label = (data.label || []).map(Label.create) } - _measure(context: EncodeContext) { + /** @internal */ + [kMeasure](context: EncodeContext) { let total = 0 total += measurePackedNumbersField(SampleField.locationId, this.locationId, context) total += measurePackedNumbersField(SampleField.value, this.value, context) @@ -252,14 +262,16 @@ export class Sample extends Message { return total } - _encodeToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset: number, context: EncodeContext): number { offset = encodePackedNumbersField(buffer, offset, SampleField.locationId, this.locationId, context) offset = encodePackedNumbersField(buffer, offset, SampleField.value, this.value, context) offset = encodeMessageArrayField(buffer, offset, SampleField.label, this.label, context) return offset } - static decodeValue(data: SampleInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) { + /** @internal */ + static [kDecodeValue](data: SampleInput, field: number, buffer: Uint8Array, start: number, end: number, wireType: number) { switch (field) { case SampleField.locationId: if (isPackedOrVarInt(wireType)) data.locationId = pushAll(decodeNumbers(buffer, start, end), data.locationId) @@ -274,7 +286,7 @@ export class Sample extends Message { } static decode(buffer: Uint8Array, start = 0, end = buffer.length): Sample { - return new this(decodeFields(buffer, this.decodeValue, start, end)) + return new this(decodeFields(buffer, this[kDecodeValue], start, end)) } } @@ -321,7 +333,8 @@ export class Mapping extends Message { this.hasInlineFrames = !!data.hasInlineFrames } - _measure() { + /** @internal */ + [kMeasure]() { let total = 0 total += measureNumberField(MappingField.id, this.id) total += measureNumberField(MappingField.memoryStart, this.memoryStart) @@ -336,7 +349,8 @@ export class Mapping extends Message { return total } - _encodeToBuffer(buffer: Uint8Array, offset = 0): number { + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset = 0): number { offset = encodeNumberField(buffer, offset, MappingField.id, this.id) offset = encodeNumberField(buffer, offset, MappingField.memoryStart, this.memoryStart) offset = encodeNumberField(buffer, offset, MappingField.memoryLimit, this.memoryLimit) @@ -350,7 +364,8 @@ export class Mapping extends Message { return offset } - static decodeValue(data: MappingInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) { + /** @internal */ + static [kDecodeValue](data: MappingInput, field: number, buffer: Uint8Array, start: number, end: number, wireType: number) { switch (field) { case MappingField.id: if (isVarInt(wireType)) data.id = decodeNumber(buffer, start, end) @@ -386,7 +401,7 @@ export class Mapping extends Message { } static decode(buffer: Uint8Array, start = 0, end = buffer.length): Mapping { - return new this(decodeFields(buffer, this.decodeValue, start, end)) + return new this(decodeFields(buffer, this[kDecodeValue], start, end)) } } @@ -412,7 +427,8 @@ export class Line extends Message { this.column = data.column || 0 } - _measure() { + /** @internal */ + [kMeasure]() { let total = 0 total += measureNumberField(LineField.functionId, this.functionId) total += measureNumberField(LineField.line, this.line) @@ -420,14 +436,16 @@ export class Line extends Message { return total } - _encodeToBuffer(buffer: Uint8Array, offset = 0): number { + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset = 0): number { offset = encodeNumberField(buffer, offset, LineField.functionId, this.functionId) offset = encodeNumberField(buffer, offset, LineField.line, this.line) offset = encodeNumberField(buffer, offset, LineField.column, this.column) return offset } - static decodeValue(data: LineInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) { + /** @internal */ + static [kDecodeValue](data: LineInput, field: number, buffer: Uint8Array, start: number, end: number, wireType: number) { switch (field) { case LineField.functionId: if (isVarInt(wireType)) data.functionId = decodeNumber(buffer, start, end) @@ -442,7 +460,7 @@ export class Line extends Message { } static decode(buffer: Uint8Array, start = 0, end = buffer.length): Line { - return new this(decodeFields(buffer, this.decodeValue, start, end)) + return new this(decodeFields(buffer, this[kDecodeValue], start, end)) } } @@ -474,7 +492,8 @@ export class Location extends Message { this.isFolded = !!data.isFolded } - _measure(context: EncodeContext) { + /** @internal */ + [kMeasure](context: EncodeContext) { let total = 0 total += measureNumberField(LocationField.id, this.id) total += measureNumberField(LocationField.mappingId, this.mappingId) @@ -484,7 +503,8 @@ export class Location extends Message { return total } - _encodeToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset: number, context: EncodeContext): number { offset = encodeNumberField(buffer, offset, LocationField.id, this.id) offset = encodeNumberField(buffer, offset, LocationField.mappingId, this.mappingId) offset = encodeNumberField(buffer, offset, LocationField.address, this.address) @@ -493,7 +513,8 @@ export class Location extends Message { return offset } - static decodeValue(data: LocationInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) { + /** @internal */ + static [kDecodeValue](data: LocationInput, field: number, buffer: Uint8Array, start: number, end: number, wireType: number) { switch (field) { case LocationField.id: if (isVarInt(wireType)) data.id = decodeNumber(buffer, start, end) @@ -514,7 +535,7 @@ export class Location extends Message { } static decode(buffer: Uint8Array, start = 0, end = buffer.length): Location { - return new this(decodeFields(buffer, this.decodeValue, start, end)) + return new this(decodeFields(buffer, this[kDecodeValue], start, end)) } } @@ -546,7 +567,8 @@ export class Function extends Message { this.startLine = data.startLine || 0 } - _measure() { + /** @internal */ + [kMeasure]() { let total = 0 total += measureNumberField(FunctionField.id, this.id) total += measureNumberField(FunctionField.name, this.name) @@ -556,7 +578,8 @@ export class Function extends Message { return total } - _encodeToBuffer(buffer: Uint8Array, offset = 0): number { + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset = 0): number { offset = encodeNumberField(buffer, offset, FunctionField.id, this.id) offset = encodeNumberField(buffer, offset, FunctionField.name, this.name) offset = encodeNumberField(buffer, offset, FunctionField.systemName, this.systemName) @@ -565,7 +588,8 @@ export class Function extends Message { return offset } - static decodeValue(data: FunctionInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) { + /** @internal */ + static [kDecodeValue](data: FunctionInput, field: number, buffer: Uint8Array, start: number, end: number, wireType: number) { switch (field) { case FunctionField.id: if (isVarInt(wireType)) data.id = decodeNumber(buffer, start, end) @@ -586,7 +610,7 @@ export class Function extends Message { } static decode(buffer: Uint8Array, start = 0, end = buffer.length): Function { - return new this(decodeFields(buffer, this.decodeValue, start, end)) + return new this(decodeFields(buffer, this[kDecodeValue], start, end)) } } @@ -644,14 +668,15 @@ export class Profile extends Message { this.docUrl = data.docUrl || 0 } - _measure(context: EncodeContext) { + /** @internal */ + [kMeasure](context: EncodeContext) { let total = 0 total += measureMessageArrayField(ProfileField.sampleType, this.sampleType, context) total += measureMessageArrayField(ProfileField.sample, this.sample, context) total += measureMessageArrayField(ProfileField.mapping, this.mapping, context) total += measureMessageArrayField(ProfileField.location, this.location, context) total += measureMessageArrayField(ProfileField.function, this.function, context) - total += this.stringTable._measure(ProfileField.stringTable) + total += this.stringTable[kMeasure](ProfileField.stringTable) total += measureNumberField(ProfileField.dropFrames, this.dropFrames) total += measureNumberField(ProfileField.keepFrames, this.keepFrames) total += measureNumberField(ProfileField.timeNanos, this.timeNanos) @@ -664,27 +689,27 @@ export class Profile extends Message { return total } - _encodeSampleTypesToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { + #encodeSampleTypesToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { return encodeMessageArrayField(buffer, offset, ProfileField.sampleType, this.sampleType, context) } - _encodeSamplesToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { + #encodeSamplesToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { return encodeMessageArrayField(buffer, offset, ProfileField.sample, this.sample, context) } - _encodeMappingsToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { + #encodeMappingsToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { return encodeMessageArrayField(buffer, offset, ProfileField.mapping, this.mapping, context) } - _encodeLocationsToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { + #encodeLocationsToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { return encodeMessageArrayField(buffer, offset, ProfileField.location, this.location, context) } - _encodeFunctionsToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { + #encodeFunctionsToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { return encodeMessageArrayField(buffer, offset, ProfileField.function, this.function, context) } - _encodeBasicValuesToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { + #encodeBasicValuesToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { offset = encodeNumberField(buffer, offset, ProfileField.dropFrames, this.dropFrames) offset = encodeNumberField(buffer, offset, ProfileField.keepFrames, this.keepFrames) offset = encodeNumberField(buffer, offset, ProfileField.timeNanos, this.timeNanos) @@ -697,51 +722,53 @@ export class Profile extends Message { return offset } - _encodeToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { - offset = this._encodeSampleTypesToBuffer(buffer, offset, context) - offset = this._encodeSamplesToBuffer(buffer, offset, context) - offset = this._encodeMappingsToBuffer(buffer, offset, context) - offset = this._encodeLocationsToBuffer(buffer, offset, context) - offset = this._encodeFunctionsToBuffer(buffer, offset, context) - offset = this.stringTable._encodeToBuffer(buffer, offset, ProfileField.stringTable) - offset = this._encodeBasicValuesToBuffer(buffer, offset, context) + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset: number, context: EncodeContext): number { + offset = this.#encodeSampleTypesToBuffer(buffer, offset, context) + offset = this.#encodeSamplesToBuffer(buffer, offset, context) + offset = this.#encodeMappingsToBuffer(buffer, offset, context) + offset = this.#encodeLocationsToBuffer(buffer, offset, context) + offset = this.#encodeFunctionsToBuffer(buffer, offset, context) + offset = this.stringTable[kEncodeToBuffer](buffer, offset, ProfileField.stringTable) + offset = this.#encodeBasicValuesToBuffer(buffer, offset, context) return offset } - /** Like _encodeToBuffer(), but yields between phases. */ - async _encodeToBufferAsync(buffer: Uint8Array, offset: number, context: EncodeContext): Promise { - offset = this._encodeSampleTypesToBuffer(buffer, offset, context) + /** Like [kEncodeToBuffer](), but yields between phases. */ + async #encodeToBufferAsync(buffer: Uint8Array, offset: number, context: EncodeContext): Promise { + offset = this.#encodeSampleTypesToBuffer(buffer, offset, context) await yieldToEventLoop() - offset = this._encodeSamplesToBuffer(buffer, offset, context) + offset = this.#encodeSamplesToBuffer(buffer, offset, context) await yieldToEventLoop() - offset = this._encodeMappingsToBuffer(buffer, offset, context) + offset = this.#encodeMappingsToBuffer(buffer, offset, context) await yieldToEventLoop() - offset = this._encodeLocationsToBuffer(buffer, offset, context) + offset = this.#encodeLocationsToBuffer(buffer, offset, context) await yieldToEventLoop() - offset = this._encodeFunctionsToBuffer(buffer, offset, context) + offset = this.#encodeFunctionsToBuffer(buffer, offset, context) await yieldToEventLoop() - offset = this.stringTable._encodeToBuffer(buffer, offset, ProfileField.stringTable) + offset = this.stringTable[kEncodeToBuffer](buffer, offset, ProfileField.stringTable) await yieldToEventLoop() - offset = this._encodeBasicValuesToBuffer(buffer, offset, context) + offset = this.#encodeBasicValuesToBuffer(buffer, offset, context) return offset } /** Like encode(), but yields between phases. Do not mutate until it settles. */ async encodeAsync(buffer?: Uint8Array): Promise { const context = newEncodeContext() - const length = this._measure(context) + const length = this[kMeasure](context) buffer = prepareBuffer(buffer, length) - verifyEncodedLength(await this._encodeToBufferAsync(buffer, 0, context), length) + verifyEncodedLength(await this.#encodeToBufferAsync(buffer, 0, context), length) return buffer } - static decodeValue(data: ProfileInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) { + /** @internal */ + static [kDecodeValue](data: ProfileInput, field: number, buffer: Uint8Array, start: number, end: number, wireType: number) { switch (field) { case ProfileField.sampleType: if (isLengthDelim(wireType)) data.sampleType = push(ValueType.decode(buffer, start, end), data.sampleType) @@ -763,7 +790,7 @@ export class Profile extends Message { if (data.stringTable === undefined) { data.stringTable = new StringTable(emptyTableToken) } - data.stringTable._decodeString(buffer.subarray(start, end)) + data.stringTable[kDecodeString](buffer.subarray(start, end)) break } case ProfileField.dropFrames: @@ -782,7 +809,7 @@ export class Profile extends Message { // Occurrences of a singular message field are merged: // https://protobuf.dev/programming-guides/encoding/#last-one-wins if (isLengthDelim(wireType)) { - data.periodType = new ValueType(decodeFields(buffer, ValueType.decodeValue, start, end, { ...data.periodType })) + data.periodType = new ValueType(decodeFields(buffer, ValueType[kDecodeValue], start, end, { ...data.periodType })) } break case ProfileField.period: @@ -805,6 +832,6 @@ export class Profile extends Message { * fields and fields whose wire type differs from the declared one. */ static decode(buffer: Uint8Array, start = 0, end = buffer.length): Profile { - return new this(decodeFields(buffer, this.decodeValue, start, end)) + return new this(decodeFields(buffer, this[kDecodeValue], start, end)) } } diff --git a/src/otel.test.ts b/src/otel.test.ts index 8ef243b..043c595 100644 --- a/src/otel.test.ts +++ b/src/otel.test.ts @@ -27,6 +27,7 @@ import { import * as otel from './otel.js' import { StringTable as PprofStringTable } from './index.js' import type { EncodeContext } from './protobuf.js' +import { kMeasure } from './protobuf.js' const pb = otelProto.opentelemetry.proto @@ -246,9 +247,9 @@ test('ExportProfilesServiceRequest.encodeAsync', async (t) => { }) await t.test('measures each submessage once', async () => { - const measure = Sample.prototype._measure + const measure = Sample.prototype[kMeasure] let calls = 0 - Sample.prototype._measure = function (this: Sample, context: EncodeContext) { + Sample.prototype[kMeasure] = function (this: Sample, context: EncodeContext) { calls++ return measure.call(this, context) } @@ -256,7 +257,7 @@ test('ExportProfilesServiceRequest.encodeAsync', async (t) => { await new ExportProfilesServiceRequest(exampleRequest()).encodeAsync() assert.strictEqual(calls, 2) } finally { - Sample.prototype._measure = measure + Sample.prototype[kMeasure] = measure } }) diff --git a/src/otel.ts b/src/otel.ts index 22220eb..410f4e2 100644 --- a/src/otel.ts +++ b/src/otel.ts @@ -41,6 +41,9 @@ import { isLengthDelim, isPackedOrVarInt, isVarInt, + kDecodeValue, + kEncodeToBuffer, + kMeasure, measureBoolField, measureBytesField, measureDoubleField, @@ -60,10 +63,13 @@ import { verifyEncodedLength, yieldToEventLoop, } from './protobuf.js' -import { StringTable, emptyTableToken } from './string-table.js' +import { StringTable, emptyTableToken, kDecodeString } from './string-table.js' export { StringTable, emptyTableToken } +const kDecodeInput = Symbol() +const kEncodeToBufferAsync = Symbol() + /* * Each message is preceded by a const enum of its field numbers, as declared * in the OTel protos. Const enums are inlined at compile time, so they cost @@ -188,7 +194,8 @@ export class AnyValue extends Message { this.stringValueStrindex = data.stringValueStrindex } - _measure(context: EncodeContext) { + /** @internal */ + [kMeasure](context: EncodeContext) { if (this.stringValue !== undefined) return measureStringField(AnyValueField.stringValue, this.stringValue, true) if (this.boolValue !== undefined) return measureBoolField(AnyValueField.boolValue, this.boolValue, true) if (this.intValue !== undefined) return measureNumberField(AnyValueField.intValue, this.intValue, true) @@ -200,7 +207,8 @@ export class AnyValue extends Message { return 0 } - _encodeToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset: number, context: EncodeContext): number { if (this.stringValue !== undefined) return encodeStringField(buffer, offset, AnyValueField.stringValue, this.stringValue, true) if (this.boolValue !== undefined) return encodeBoolField(buffer, offset, AnyValueField.boolValue, this.boolValue, true) if (this.intValue !== undefined) return encodeNumberField(buffer, offset, AnyValueField.intValue, this.intValue, true) @@ -214,7 +222,8 @@ export class AnyValue extends Message { return offset } - static decodeValue(data: AnyValueInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) { + /** @internal */ + static [kDecodeValue](data: AnyValueInput, field: number, buffer: Uint8Array, start: number, end: number, wireType: number) { switch (field) { case AnyValueField.stringValue: if (isLengthDelim(wireType)) setAnyValue(data, 'stringValue', decodeString(buffer, start, end)) @@ -229,10 +238,10 @@ export class AnyValue extends Message { if (isFixed64(wireType)) setAnyValue(data, 'doubleValue', decodeDouble(buffer, start)) break case AnyValueField.arrayValue: - if (isLengthDelim(wireType)) setAnyValue(data, 'arrayValue', ArrayValue.decodeInput(buffer, start, end, data.arrayValue)) + if (isLengthDelim(wireType)) setAnyValue(data, 'arrayValue', ArrayValue[kDecodeInput](buffer, start, end, data.arrayValue)) break case AnyValueField.kvlistValue: - if (isLengthDelim(wireType)) setAnyValue(data, 'kvlistValue', KeyValueList.decodeInput(buffer, start, end, data.kvlistValue)) + if (isLengthDelim(wireType)) setAnyValue(data, 'kvlistValue', KeyValueList[kDecodeInput](buffer, start, end, data.kvlistValue)) break case AnyValueField.bytesValue: if (isLengthDelim(wireType)) setAnyValue(data, 'bytesValue', decodeBytes(buffer, start, end)) @@ -243,12 +252,13 @@ export class AnyValue extends Message { } } - static decodeInput(buffer: Uint8Array, start = 0, end = buffer.length, into?: AnyValueInput): AnyValueInput { - return decodeFields(buffer, this.decodeValue, start, end, into) + /** @internal */ + static [kDecodeInput](buffer: Uint8Array, start = 0, end = buffer.length, into?: AnyValueInput): AnyValueInput { + return decodeFields(buffer, this[kDecodeValue], start, end, into) } static decode(buffer: Uint8Array, start = 0, end = buffer.length): AnyValue { - return new this(this.decodeInput(buffer, start, end)) + return new this(this[kDecodeInput](buffer, start, end)) } } @@ -272,28 +282,32 @@ export class ArrayValue extends Message { this.values = (data.values || []).map(AnyValue.create) } - _measure(context: EncodeContext) { + /** @internal */ + [kMeasure](context: EncodeContext) { return measureMessageArrayField(ArrayValueField.values, this.values, context) } - _encodeToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset: number, context: EncodeContext): number { return encodeMessageArrayField(buffer, offset, ArrayValueField.values, this.values, context) } - static decodeValue(data: ArrayValueInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) { + /** @internal */ + static [kDecodeValue](data: ArrayValueInput, field: number, buffer: Uint8Array, start: number, end: number, wireType: number) { switch (field) { case ArrayValueField.values: - if (isLengthDelim(wireType)) data.values = push(AnyValue.decodeInput(buffer, start, end), data.values) + if (isLengthDelim(wireType)) data.values = push(AnyValue[kDecodeInput](buffer, start, end), data.values) break } } - static decodeInput(buffer: Uint8Array, start = 0, end = buffer.length, into?: ArrayValueInput): ArrayValueInput { - return decodeFields(buffer, this.decodeValue, start, end, into) + /** @internal */ + static [kDecodeInput](buffer: Uint8Array, start = 0, end = buffer.length, into?: ArrayValueInput): ArrayValueInput { + return decodeFields(buffer, this[kDecodeValue], start, end, into) } static decode(buffer: Uint8Array, start = 0, end = buffer.length): ArrayValue { - return new this(this.decodeInput(buffer, start, end)) + return new this(this[kDecodeInput](buffer, start, end)) } } @@ -317,28 +331,32 @@ export class KeyValueList extends Message { this.values = (data.values || []).map(KeyValue.create) } - _measure(context: EncodeContext) { + /** @internal */ + [kMeasure](context: EncodeContext) { return measureMessageArrayField(KeyValueListField.values, this.values, context) } - _encodeToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset: number, context: EncodeContext): number { return encodeMessageArrayField(buffer, offset, KeyValueListField.values, this.values, context) } - static decodeValue(data: KeyValueListInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) { + /** @internal */ + static [kDecodeValue](data: KeyValueListInput, field: number, buffer: Uint8Array, start: number, end: number, wireType: number) { switch (field) { case KeyValueListField.values: - if (isLengthDelim(wireType)) data.values = push(KeyValue.decodeInput(buffer, start, end), data.values) + if (isLengthDelim(wireType)) data.values = push(KeyValue[kDecodeInput](buffer, start, end), data.values) break } } - static decodeInput(buffer: Uint8Array, start = 0, end = buffer.length, into?: KeyValueListInput): KeyValueListInput { - return decodeFields(buffer, this.decodeValue, start, end, into) + /** @internal */ + static [kDecodeInput](buffer: Uint8Array, start = 0, end = buffer.length, into?: KeyValueListInput): KeyValueListInput { + return decodeFields(buffer, this[kDecodeValue], start, end, into) } static decode(buffer: Uint8Array, start = 0, end = buffer.length): KeyValueList { - return new this(this.decodeInput(buffer, start, end)) + return new this(this[kDecodeInput](buffer, start, end)) } } @@ -370,7 +388,8 @@ export class KeyValue extends Message { this.keyStrindex = data.keyStrindex || 0 } - _measure(context: EncodeContext) { + /** @internal */ + [kMeasure](context: EncodeContext) { let total = 0 total += measureStringField(KeyValueField.key, this.key) total += measureMessageField(KeyValueField.value, this.value, context) @@ -378,20 +397,22 @@ export class KeyValue extends Message { return total } - _encodeToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset: number, context: EncodeContext): number { offset = encodeStringField(buffer, offset, KeyValueField.key, this.key) offset = encodeMessageField(buffer, offset, KeyValueField.value, this.value, context) offset = encodeNumberField(buffer, offset, KeyValueField.keyStrindex, this.keyStrindex) return offset } - static decodeValue(data: KeyValueInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) { + /** @internal */ + static [kDecodeValue](data: KeyValueInput, field: number, buffer: Uint8Array, start: number, end: number, wireType: number) { switch (field) { case KeyValueField.key: if (isLengthDelim(wireType)) data.key = decodeString(buffer, start, end) break case KeyValueField.value: - if (isLengthDelim(wireType)) data.value = AnyValue.decodeInput(buffer, start, end, data.value) + if (isLengthDelim(wireType)) data.value = AnyValue[kDecodeInput](buffer, start, end, data.value) break case KeyValueField.keyStrindex: if (isVarInt(wireType)) data.keyStrindex = decodeInt32(buffer, start, end) @@ -399,12 +420,13 @@ export class KeyValue extends Message { } } - static decodeInput(buffer: Uint8Array, start = 0, end = buffer.length, into?: KeyValueInput): KeyValueInput { - return decodeFields(buffer, this.decodeValue, start, end, into) + /** @internal */ + static [kDecodeInput](buffer: Uint8Array, start = 0, end = buffer.length, into?: KeyValueInput): KeyValueInput { + return decodeFields(buffer, this[kDecodeValue], start, end, into) } static decode(buffer: Uint8Array, start = 0, end = buffer.length): KeyValue { - return new this(this.decodeInput(buffer, start, end)) + return new this(this[kDecodeInput](buffer, start, end)) } } @@ -440,7 +462,8 @@ export class InstrumentationScope extends Message { this.droppedAttributesCount = data.droppedAttributesCount || 0 } - _measure(context: EncodeContext) { + /** @internal */ + [kMeasure](context: EncodeContext) { let total = 0 total += measureStringField(InstrumentationScopeField.name, this.name) total += measureStringField(InstrumentationScopeField.version, this.version) @@ -449,7 +472,8 @@ export class InstrumentationScope extends Message { return total } - _encodeToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset: number, context: EncodeContext): number { offset = encodeStringField(buffer, offset, InstrumentationScopeField.name, this.name) offset = encodeStringField(buffer, offset, InstrumentationScopeField.version, this.version) offset = encodeMessageArrayField(buffer, offset, InstrumentationScopeField.attributes, this.attributes, context) @@ -457,7 +481,8 @@ export class InstrumentationScope extends Message { return offset } - static decodeValue(data: InstrumentationScopeInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) { + /** @internal */ + static [kDecodeValue](data: InstrumentationScopeInput, field: number, buffer: Uint8Array, start: number, end: number, wireType: number) { switch (field) { case InstrumentationScopeField.name: if (isLengthDelim(wireType)) data.name = decodeString(buffer, start, end) @@ -466,7 +491,7 @@ export class InstrumentationScope extends Message { if (isLengthDelim(wireType)) data.version = decodeString(buffer, start, end) break case InstrumentationScopeField.attributes: - if (isLengthDelim(wireType)) data.attributes = push(KeyValue.decodeInput(buffer, start, end), data.attributes) + if (isLengthDelim(wireType)) data.attributes = push(KeyValue[kDecodeInput](buffer, start, end), data.attributes) break case InstrumentationScopeField.droppedAttributesCount: if (isVarInt(wireType)) data.droppedAttributesCount = decodeUint32(buffer, start, end) @@ -474,12 +499,13 @@ export class InstrumentationScope extends Message { } } - static decodeInput(buffer: Uint8Array, start = 0, end = buffer.length, into?: InstrumentationScopeInput): InstrumentationScopeInput { - return decodeFields(buffer, this.decodeValue, start, end, into) + /** @internal */ + static [kDecodeInput](buffer: Uint8Array, start = 0, end = buffer.length, into?: InstrumentationScopeInput): InstrumentationScopeInput { + return decodeFields(buffer, this[kDecodeValue], start, end, into) } static decode(buffer: Uint8Array, start = 0, end = buffer.length): InstrumentationScope { - return new this(this.decodeInput(buffer, start, end)) + return new this(this[kDecodeInput](buffer, start, end)) } } @@ -515,7 +541,8 @@ export class EntityRef extends Message { this.descriptionKeys = data.descriptionKeys || [] } - _measure() { + /** @internal */ + [kMeasure]() { let total = 0 total += measureStringField(EntityRefField.schemaUrl, this.schemaUrl) total += measureStringField(EntityRefField.type, this.type) @@ -524,7 +551,8 @@ export class EntityRef extends Message { return total } - _encodeToBuffer(buffer: Uint8Array, offset = 0): number { + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset = 0): number { offset = encodeStringField(buffer, offset, EntityRefField.schemaUrl, this.schemaUrl) offset = encodeStringField(buffer, offset, EntityRefField.type, this.type) offset = encodeStringArrayField(buffer, offset, EntityRefField.idKeys, this.idKeys) @@ -532,7 +560,8 @@ export class EntityRef extends Message { return offset } - static decodeValue(data: EntityRefInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) { + /** @internal */ + static [kDecodeValue](data: EntityRefInput, field: number, buffer: Uint8Array, start: number, end: number, wireType: number) { switch (field) { case EntityRefField.schemaUrl: if (isLengthDelim(wireType)) data.schemaUrl = decodeString(buffer, start, end) @@ -549,12 +578,13 @@ export class EntityRef extends Message { } } - static decodeInput(buffer: Uint8Array, start = 0, end = buffer.length, into?: EntityRefInput): EntityRefInput { - return decodeFields(buffer, this.decodeValue, start, end, into) + /** @internal */ + static [kDecodeInput](buffer: Uint8Array, start = 0, end = buffer.length, into?: EntityRefInput): EntityRefInput { + return decodeFields(buffer, this[kDecodeValue], start, end, into) } static decode(buffer: Uint8Array, start = 0, end = buffer.length): EntityRef { - return new this(this.decodeInput(buffer, start, end)) + return new this(this[kDecodeInput](buffer, start, end)) } } @@ -590,7 +620,8 @@ export class Resource extends Message { this.entityRefs = (data.entityRefs || []).map(EntityRef.create) } - _measure(context: EncodeContext) { + /** @internal */ + [kMeasure](context: EncodeContext) { let total = 0 total += measureMessageArrayField(ResourceField.attributes, this.attributes, context) total += measureNumberField(ResourceField.droppedAttributesCount, this.droppedAttributesCount) @@ -598,33 +629,36 @@ export class Resource extends Message { return total } - _encodeToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset: number, context: EncodeContext): number { offset = encodeMessageArrayField(buffer, offset, ResourceField.attributes, this.attributes, context) offset = encodeNumberField(buffer, offset, ResourceField.droppedAttributesCount, this.droppedAttributesCount) offset = encodeMessageArrayField(buffer, offset, ResourceField.entityRefs, this.entityRefs, context) return offset } - static decodeValue(data: ResourceInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) { + /** @internal */ + static [kDecodeValue](data: ResourceInput, field: number, buffer: Uint8Array, start: number, end: number, wireType: number) { switch (field) { case ResourceField.attributes: - if (isLengthDelim(wireType)) data.attributes = push(KeyValue.decodeInput(buffer, start, end), data.attributes) + if (isLengthDelim(wireType)) data.attributes = push(KeyValue[kDecodeInput](buffer, start, end), data.attributes) break case ResourceField.droppedAttributesCount: if (isVarInt(wireType)) data.droppedAttributesCount = decodeUint32(buffer, start, end) break case ResourceField.entityRefs: - if (isLengthDelim(wireType)) data.entityRefs = push(EntityRef.decodeInput(buffer, start, end), data.entityRefs) + if (isLengthDelim(wireType)) data.entityRefs = push(EntityRef[kDecodeInput](buffer, start, end), data.entityRefs) break } } - static decodeInput(buffer: Uint8Array, start = 0, end = buffer.length, into?: ResourceInput): ResourceInput { - return decodeFields(buffer, this.decodeValue, start, end, into) + /** @internal */ + static [kDecodeInput](buffer: Uint8Array, start = 0, end = buffer.length, into?: ResourceInput): ResourceInput { + return decodeFields(buffer, this[kDecodeValue], start, end, into) } static decode(buffer: Uint8Array, start = 0, end = buffer.length): Resource { - return new this(this.decodeInput(buffer, start, end)) + return new this(this[kDecodeInput](buffer, start, end)) } } @@ -681,86 +715,70 @@ export class ProfilesDictionary extends Message { this.stackTable = data.stackTable ? data.stackTable.map(Stack.create) : [new Stack()] } - _measure(context: EncodeContext) { + /** @internal */ + [kMeasure](context: EncodeContext) { let total = 0 total += measureMessageArrayField(ProfilesDictionaryField.mappingTable, this.mappingTable, context) total += measureMessageArrayField(ProfilesDictionaryField.locationTable, this.locationTable, context) total += measureMessageArrayField(ProfilesDictionaryField.functionTable, this.functionTable, context) total += measureMessageArrayField(ProfilesDictionaryField.linkTable, this.linkTable, context) - total += this.stringTable._measure(ProfilesDictionaryField.stringTable) + total += this.stringTable[kMeasure](ProfilesDictionaryField.stringTable) total += measureMessageArrayField(ProfilesDictionaryField.attributeTable, this.attributeTable, context) total += measureMessageArrayField(ProfilesDictionaryField.stackTable, this.stackTable, context) return total } - _encodeToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { - offset = encodeMessageArrayField(buffer, offset, ProfilesDictionaryField.mappingTable, this.mappingTable, context) - offset = encodeMessageArrayField(buffer, offset, ProfilesDictionaryField.locationTable, this.locationTable, context) - offset = encodeMessageArrayField(buffer, offset, ProfilesDictionaryField.functionTable, this.functionTable, context) - offset = encodeMessageArrayField(buffer, offset, ProfilesDictionaryField.linkTable, this.linkTable, context) - offset = this.stringTable._encodeToBuffer(buffer, offset, ProfilesDictionaryField.stringTable) - offset = encodeMessageArrayField(buffer, offset, ProfilesDictionaryField.attributeTable, this.attributeTable, context) - offset = encodeMessageArrayField(buffer, offset, ProfilesDictionaryField.stackTable, this.stackTable, context) - return offset - } - - /** Like _encodeToBuffer(), but yields between tables. */ - async _encodeToBufferAsync(buffer: Uint8Array, offset: number, context: EncodeContext): Promise { + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset: number, context: EncodeContext): number { offset = encodeMessageArrayField(buffer, offset, ProfilesDictionaryField.mappingTable, this.mappingTable, context) - await yieldToEventLoop() - offset = encodeMessageArrayField(buffer, offset, ProfilesDictionaryField.locationTable, this.locationTable, context) - await yieldToEventLoop() - offset = encodeMessageArrayField(buffer, offset, ProfilesDictionaryField.functionTable, this.functionTable, context) offset = encodeMessageArrayField(buffer, offset, ProfilesDictionaryField.linkTable, this.linkTable, context) - await yieldToEventLoop() - - offset = this.stringTable._encodeToBuffer(buffer, offset, ProfilesDictionaryField.stringTable) - await yieldToEventLoop() - + offset = this.stringTable[kEncodeToBuffer](buffer, offset, ProfilesDictionaryField.stringTable) offset = encodeMessageArrayField(buffer, offset, ProfilesDictionaryField.attributeTable, this.attributeTable, context) offset = encodeMessageArrayField(buffer, offset, ProfilesDictionaryField.stackTable, this.stackTable, context) return offset } - static decodeValue(data: ProfilesDictionaryInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) { + /** @internal */ + static [kDecodeValue](data: ProfilesDictionaryInput, field: number, buffer: Uint8Array, start: number, end: number, wireType: number) { switch (field) { case ProfilesDictionaryField.mappingTable: - if (isLengthDelim(wireType)) data.mappingTable = push(Mapping.decodeInput(buffer, start, end), data.mappingTable) + if (isLengthDelim(wireType)) data.mappingTable = push(Mapping[kDecodeInput](buffer, start, end), data.mappingTable) break case ProfilesDictionaryField.locationTable: - if (isLengthDelim(wireType)) data.locationTable = push(Location.decodeInput(buffer, start, end), data.locationTable) + if (isLengthDelim(wireType)) data.locationTable = push(Location[kDecodeInput](buffer, start, end), data.locationTable) break case ProfilesDictionaryField.functionTable: - if (isLengthDelim(wireType)) data.functionTable = push(Function.decodeInput(buffer, start, end), data.functionTable) + if (isLengthDelim(wireType)) data.functionTable = push(Function[kDecodeInput](buffer, start, end), data.functionTable) break case ProfilesDictionaryField.linkTable: - if (isLengthDelim(wireType)) data.linkTable = push(Link.decodeInput(buffer, start, end), data.linkTable) + if (isLengthDelim(wireType)) data.linkTable = push(Link[kDecodeInput](buffer, start, end), data.linkTable) break case ProfilesDictionaryField.stringTable: { if (!isLengthDelim(wireType)) break if (data.stringTable === undefined) { data.stringTable = new StringTable(emptyTableToken) } - data.stringTable._decodeString(buffer.subarray(start, end)) + data.stringTable[kDecodeString](buffer.subarray(start, end)) break } case ProfilesDictionaryField.attributeTable: - if (isLengthDelim(wireType)) data.attributeTable = push(KeyValueAndUnit.decodeInput(buffer, start, end), data.attributeTable) + if (isLengthDelim(wireType)) data.attributeTable = push(KeyValueAndUnit[kDecodeInput](buffer, start, end), data.attributeTable) break case ProfilesDictionaryField.stackTable: - if (isLengthDelim(wireType)) data.stackTable = push(Stack.decodeInput(buffer, start, end), data.stackTable) + if (isLengthDelim(wireType)) data.stackTable = push(Stack[kDecodeInput](buffer, start, end), data.stackTable) break } } - static decodeInput(buffer: Uint8Array, start = 0, end = buffer.length, into?: ProfilesDictionaryInput): ProfilesDictionaryInput { - return decodeFields(buffer, this.decodeValue, start, end, into) + /** @internal */ + static [kDecodeInput](buffer: Uint8Array, start = 0, end = buffer.length, into?: ProfilesDictionaryInput): ProfilesDictionaryInput { + return decodeFields(buffer, this[kDecodeValue], start, end, into) } static decode(buffer: Uint8Array, start = 0, end = buffer.length): ProfilesDictionary { - return new this(this.decodeInput(buffer, start, end)) + return new this(this[kDecodeInput](buffer, start, end)) } } @@ -769,6 +787,25 @@ const enum ProfilesDataField { dictionary = 2, } +async function encodeDictionaryToBufferAsync(dictionary: ProfilesDictionary, buffer: Uint8Array, offset: number, context: EncodeContext): Promise { + offset = encodeMessageArrayField(buffer, offset, ProfilesDictionaryField.mappingTable, dictionary.mappingTable, context) + await yieldToEventLoop() + + offset = encodeMessageArrayField(buffer, offset, ProfilesDictionaryField.locationTable, dictionary.locationTable, context) + await yieldToEventLoop() + + offset = encodeMessageArrayField(buffer, offset, ProfilesDictionaryField.functionTable, dictionary.functionTable, context) + offset = encodeMessageArrayField(buffer, offset, ProfilesDictionaryField.linkTable, dictionary.linkTable, context) + await yieldToEventLoop() + + offset = dictionary.stringTable[kEncodeToBuffer](buffer, offset, ProfilesDictionaryField.stringTable) + await yieldToEventLoop() + + offset = encodeMessageArrayField(buffer, offset, ProfilesDictionaryField.attributeTable, dictionary.attributeTable, context) + offset = encodeMessageArrayField(buffer, offset, ProfilesDictionaryField.stackTable, dictionary.stackTable, context) + return offset +} + export type ProfilesDataInput = { resourceProfiles?: Array dictionary?: ProfilesDictionaryInput @@ -789,21 +826,24 @@ export class ProfilesData extends Message { this.dictionary = data.dictionary && ProfilesDictionary.create(data.dictionary) } - _measure(context: EncodeContext) { + /** @internal */ + [kMeasure](context: EncodeContext) { let total = 0 total += measureMessageArrayField(ProfilesDataField.resourceProfiles, this.resourceProfiles, context) total += measureMessageField(ProfilesDataField.dictionary, this.dictionary, context) return total } - _encodeToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset: number, context: EncodeContext): number { offset = encodeMessageArrayField(buffer, offset, ProfilesDataField.resourceProfiles, this.resourceProfiles, context) offset = encodeMessageField(buffer, offset, ProfilesDataField.dictionary, this.dictionary, context) return offset } - /** Like _encodeToBuffer(), but yields between phases. */ - async _encodeToBufferAsync(buffer: Uint8Array, offset: number, context: EncodeContext): Promise { + /** Like [kEncodeToBuffer](), but yields between phases. */ + /** @internal */ + async [kEncodeToBufferAsync](buffer: Uint8Array, offset: number, context: EncodeContext): Promise { const dictionary = this.dictionary offset = encodeMessageArrayField(buffer, offset, ProfilesDataField.resourceProfiles, this.resourceProfiles, context) @@ -812,35 +852,37 @@ export class ProfilesData extends Message { // Read the dictionary's recorded length in the same order as encode() does offset = encodeMessageFieldHeader(buffer, offset, ProfilesDataField.dictionary, context) - return dictionary._encodeToBufferAsync(buffer, offset, context) + return encodeDictionaryToBufferAsync(dictionary, buffer, offset, context) } /** Like encode(), but yields between phases. Do not mutate until it settles. */ async encodeAsync(buffer?: Uint8Array): Promise { const context = newEncodeContext() - const length = this._measure(context) + const length = this[kMeasure](context) buffer = prepareBuffer(buffer, length) - verifyEncodedLength(await this._encodeToBufferAsync(buffer, 0, context), length) + verifyEncodedLength(await this[kEncodeToBufferAsync](buffer, 0, context), length) return buffer } - static decodeValue(data: ProfilesDataInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) { + /** @internal */ + static [kDecodeValue](data: ProfilesDataInput, field: number, buffer: Uint8Array, start: number, end: number, wireType: number) { switch (field) { case ProfilesDataField.resourceProfiles: - if (isLengthDelim(wireType)) data.resourceProfiles = push(ResourceProfiles.decodeInput(buffer, start, end), data.resourceProfiles) + if (isLengthDelim(wireType)) data.resourceProfiles = push(ResourceProfiles[kDecodeInput](buffer, start, end), data.resourceProfiles) break case ProfilesDataField.dictionary: - if (isLengthDelim(wireType)) data.dictionary = ProfilesDictionary.decodeInput(buffer, start, end, data.dictionary) + if (isLengthDelim(wireType)) data.dictionary = ProfilesDictionary[kDecodeInput](buffer, start, end, data.dictionary) break } } - static decodeInput(buffer: Uint8Array, start = 0, end = buffer.length, into?: ProfilesDataInput): ProfilesDataInput { - return decodeFields(buffer, this.decodeValue, start, end, into) + /** @internal */ + static [kDecodeInput](buffer: Uint8Array, start = 0, end = buffer.length, into?: ProfilesDataInput): ProfilesDataInput { + return decodeFields(buffer, this[kDecodeValue], start, end, into) } static decode(buffer: Uint8Array, start = 0, end = buffer.length): ProfilesData { - return new this(this.decodeInput(buffer, start, end)) + return new this(this[kDecodeInput](buffer, start, end)) } } @@ -872,7 +914,8 @@ export class ResourceProfiles extends Message { this.schemaUrl = data.schemaUrl || '' } - _measure(context: EncodeContext) { + /** @internal */ + [kMeasure](context: EncodeContext) { let total = 0 total += measureMessageField(ResourceProfilesField.resource, this.resource, context) total += measureMessageArrayField(ResourceProfilesField.scopeProfiles, this.scopeProfiles, context) @@ -880,20 +923,22 @@ export class ResourceProfiles extends Message { return total } - _encodeToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset: number, context: EncodeContext): number { offset = encodeMessageField(buffer, offset, ResourceProfilesField.resource, this.resource, context) offset = encodeMessageArrayField(buffer, offset, ResourceProfilesField.scopeProfiles, this.scopeProfiles, context) offset = encodeStringField(buffer, offset, ResourceProfilesField.schemaUrl, this.schemaUrl) return offset } - static decodeValue(data: ResourceProfilesInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) { + /** @internal */ + static [kDecodeValue](data: ResourceProfilesInput, field: number, buffer: Uint8Array, start: number, end: number, wireType: number) { switch (field) { case ResourceProfilesField.resource: - if (isLengthDelim(wireType)) data.resource = Resource.decodeInput(buffer, start, end, data.resource) + if (isLengthDelim(wireType)) data.resource = Resource[kDecodeInput](buffer, start, end, data.resource) break case ResourceProfilesField.scopeProfiles: - if (isLengthDelim(wireType)) data.scopeProfiles = push(ScopeProfiles.decodeInput(buffer, start, end), data.scopeProfiles) + if (isLengthDelim(wireType)) data.scopeProfiles = push(ScopeProfiles[kDecodeInput](buffer, start, end), data.scopeProfiles) break case ResourceProfilesField.schemaUrl: if (isLengthDelim(wireType)) data.schemaUrl = decodeString(buffer, start, end) @@ -901,12 +946,13 @@ export class ResourceProfiles extends Message { } } - static decodeInput(buffer: Uint8Array, start = 0, end = buffer.length, into?: ResourceProfilesInput): ResourceProfilesInput { - return decodeFields(buffer, this.decodeValue, start, end, into) + /** @internal */ + static [kDecodeInput](buffer: Uint8Array, start = 0, end = buffer.length, into?: ResourceProfilesInput): ResourceProfilesInput { + return decodeFields(buffer, this[kDecodeValue], start, end, into) } static decode(buffer: Uint8Array, start = 0, end = buffer.length): ResourceProfiles { - return new this(this.decodeInput(buffer, start, end)) + return new this(this[kDecodeInput](buffer, start, end)) } } @@ -938,7 +984,8 @@ export class ScopeProfiles extends Message { this.schemaUrl = data.schemaUrl || '' } - _measure(context: EncodeContext) { + /** @internal */ + [kMeasure](context: EncodeContext) { let total = 0 total += measureMessageField(ScopeProfilesField.scope, this.scope, context) total += measureMessageArrayField(ScopeProfilesField.profiles, this.profiles, context) @@ -946,20 +993,22 @@ export class ScopeProfiles extends Message { return total } - _encodeToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset: number, context: EncodeContext): number { offset = encodeMessageField(buffer, offset, ScopeProfilesField.scope, this.scope, context) offset = encodeMessageArrayField(buffer, offset, ScopeProfilesField.profiles, this.profiles, context) offset = encodeStringField(buffer, offset, ScopeProfilesField.schemaUrl, this.schemaUrl) return offset } - static decodeValue(data: ScopeProfilesInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) { + /** @internal */ + static [kDecodeValue](data: ScopeProfilesInput, field: number, buffer: Uint8Array, start: number, end: number, wireType: number) { switch (field) { case ScopeProfilesField.scope: - if (isLengthDelim(wireType)) data.scope = InstrumentationScope.decodeInput(buffer, start, end, data.scope) + if (isLengthDelim(wireType)) data.scope = InstrumentationScope[kDecodeInput](buffer, start, end, data.scope) break case ScopeProfilesField.profiles: - if (isLengthDelim(wireType)) data.profiles = push(Profile.decodeInput(buffer, start, end), data.profiles) + if (isLengthDelim(wireType)) data.profiles = push(Profile[kDecodeInput](buffer, start, end), data.profiles) break case ScopeProfilesField.schemaUrl: if (isLengthDelim(wireType)) data.schemaUrl = decodeString(buffer, start, end) @@ -967,12 +1016,13 @@ export class ScopeProfiles extends Message { } } - static decodeInput(buffer: Uint8Array, start = 0, end = buffer.length, into?: ScopeProfilesInput): ScopeProfilesInput { - return decodeFields(buffer, this.decodeValue, start, end, into) + /** @internal */ + static [kDecodeInput](buffer: Uint8Array, start = 0, end = buffer.length, into?: ScopeProfilesInput): ScopeProfilesInput { + return decodeFields(buffer, this[kDecodeValue], start, end, into) } static decode(buffer: Uint8Array, start = 0, end = buffer.length): ScopeProfiles { - return new this(this.decodeInput(buffer, start, end)) + return new this(this[kDecodeInput](buffer, start, end)) } } @@ -1036,7 +1086,8 @@ export class Profile extends Message { this.attributeIndices = data.attributeIndices || [] } - _measure(context: EncodeContext) { + /** @internal */ + [kMeasure](context: EncodeContext) { let total = 0 total += measureMessageField(ProfileField.sampleType, this.sampleType, context) total += measureMessageArrayField(ProfileField.samples, this.samples, context) @@ -1052,7 +1103,8 @@ export class Profile extends Message { return total } - _encodeToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset: number, context: EncodeContext): number { offset = encodeMessageField(buffer, offset, ProfileField.sampleType, this.sampleType, context) offset = encodeMessageArrayField(buffer, offset, ProfileField.samples, this.samples, context) offset = encodeFixed64Field(buffer, offset, ProfileField.timeUnixNano, this.timeUnixNano) @@ -1067,13 +1119,14 @@ export class Profile extends Message { return offset } - static decodeValue(data: ProfileInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) { + /** @internal */ + static [kDecodeValue](data: ProfileInput, field: number, buffer: Uint8Array, start: number, end: number, wireType: number) { switch (field) { case ProfileField.sampleType: - if (isLengthDelim(wireType)) data.sampleType = ValueType.decodeInput(buffer, start, end, data.sampleType) + if (isLengthDelim(wireType)) data.sampleType = ValueType[kDecodeInput](buffer, start, end, data.sampleType) break case ProfileField.samples: - if (isLengthDelim(wireType)) data.samples = push(Sample.decodeInput(buffer, start, end), data.samples) + if (isLengthDelim(wireType)) data.samples = push(Sample[kDecodeInput](buffer, start, end), data.samples) break case ProfileField.timeUnixNano: if (isFixed64(wireType)) data.timeUnixNano = decodeFixed64(buffer, start) @@ -1082,7 +1135,7 @@ export class Profile extends Message { if (isVarInt(wireType)) data.durationNano = decodeNumber(buffer, start, end) break case ProfileField.periodType: - if (isLengthDelim(wireType)) data.periodType = ValueType.decodeInput(buffer, start, end, data.periodType) + if (isLengthDelim(wireType)) data.periodType = ValueType[kDecodeInput](buffer, start, end, data.periodType) break case ProfileField.period: if (isVarInt(wireType)) data.period = decodeInt64(buffer, start, end) @@ -1105,12 +1158,13 @@ export class Profile extends Message { } } - static decodeInput(buffer: Uint8Array, start = 0, end = buffer.length, into?: ProfileInput): ProfileInput { - return decodeFields(buffer, this.decodeValue, start, end, into) + /** @internal */ + static [kDecodeInput](buffer: Uint8Array, start = 0, end = buffer.length, into?: ProfileInput): ProfileInput { + return decodeFields(buffer, this[kDecodeValue], start, end, into) } static decode(buffer: Uint8Array, start = 0, end = buffer.length): Profile { - return new this(this.decodeInput(buffer, start, end)) + return new this(this[kDecodeInput](buffer, start, end)) } } @@ -1138,20 +1192,23 @@ export class Link extends Message { this.spanId = data.spanId || new Uint8Array(0) } - _measure() { + /** @internal */ + [kMeasure]() { let total = 0 total += measureBytesField(LinkField.traceId, this.traceId) total += measureBytesField(LinkField.spanId, this.spanId) return total } - _encodeToBuffer(buffer: Uint8Array, offset = 0): number { + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset = 0): number { offset = encodeBytesField(buffer, offset, LinkField.traceId, this.traceId) offset = encodeBytesField(buffer, offset, LinkField.spanId, this.spanId) return offset } - static decodeValue(data: LinkInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) { + /** @internal */ + static [kDecodeValue](data: LinkInput, field: number, buffer: Uint8Array, start: number, end: number, wireType: number) { switch (field) { case LinkField.traceId: if (isLengthDelim(wireType)) data.traceId = decodeBytes(buffer, start, end) @@ -1162,12 +1219,13 @@ export class Link extends Message { } } - static decodeInput(buffer: Uint8Array, start = 0, end = buffer.length, into?: LinkInput): LinkInput { - return decodeFields(buffer, this.decodeValue, start, end, into) + /** @internal */ + static [kDecodeInput](buffer: Uint8Array, start = 0, end = buffer.length, into?: LinkInput): LinkInput { + return decodeFields(buffer, this[kDecodeValue], start, end, into) } static decode(buffer: Uint8Array, start = 0, end = buffer.length): Link { - return new this(this.decodeInput(buffer, start, end)) + return new this(this[kDecodeInput](buffer, start, end)) } } @@ -1195,20 +1253,23 @@ export class ValueType extends Message { this.unitStrindex = data.unitStrindex || 0 } - _measure() { + /** @internal */ + [kMeasure]() { let total = 0 total += measureNumberField(ValueTypeField.typeStrindex, this.typeStrindex) total += measureNumberField(ValueTypeField.unitStrindex, this.unitStrindex) return total } - _encodeToBuffer(buffer: Uint8Array, offset = 0): number { + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset = 0): number { offset = encodeNumberField(buffer, offset, ValueTypeField.typeStrindex, this.typeStrindex) offset = encodeNumberField(buffer, offset, ValueTypeField.unitStrindex, this.unitStrindex) return offset } - static decodeValue(data: ValueTypeInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) { + /** @internal */ + static [kDecodeValue](data: ValueTypeInput, field: number, buffer: Uint8Array, start: number, end: number, wireType: number) { switch (field) { case ValueTypeField.typeStrindex: if (isVarInt(wireType)) data.typeStrindex = decodeInt32(buffer, start, end) @@ -1219,12 +1280,13 @@ export class ValueType extends Message { } } - static decodeInput(buffer: Uint8Array, start = 0, end = buffer.length, into?: ValueTypeInput): ValueTypeInput { - return decodeFields(buffer, this.decodeValue, start, end, into) + /** @internal */ + static [kDecodeInput](buffer: Uint8Array, start = 0, end = buffer.length, into?: ValueTypeInput): ValueTypeInput { + return decodeFields(buffer, this[kDecodeValue], start, end, into) } static decode(buffer: Uint8Array, start = 0, end = buffer.length): ValueType { - return new this(this.decodeInput(buffer, start, end)) + return new this(this[kDecodeInput](buffer, start, end)) } } @@ -1264,7 +1326,8 @@ export class Sample extends Message { this.timestampsUnixNano = data.timestampsUnixNano || [] } - _measure(context: EncodeContext) { + /** @internal */ + [kMeasure](context: EncodeContext) { let total = 0 total += measureNumberField(SampleField.stackIndex, this.stackIndex) total += measurePackedNumbersField(SampleField.attributeIndices, this.attributeIndices, context) @@ -1274,7 +1337,8 @@ export class Sample extends Message { return total } - _encodeToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset: number, context: EncodeContext): number { offset = encodeNumberField(buffer, offset, SampleField.stackIndex, this.stackIndex) offset = encodePackedNumbersField(buffer, offset, SampleField.attributeIndices, this.attributeIndices, context) offset = encodeNumberField(buffer, offset, SampleField.linkIndex, this.linkIndex) @@ -1283,7 +1347,8 @@ export class Sample extends Message { return offset } - static decodeValue(data: SampleInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) { + /** @internal */ + static [kDecodeValue](data: SampleInput, field: number, buffer: Uint8Array, start: number, end: number, wireType: number) { switch (field) { case SampleField.stackIndex: if (isVarInt(wireType)) data.stackIndex = decodeInt32(buffer, start, end) @@ -1308,12 +1373,13 @@ export class Sample extends Message { } } - static decodeInput(buffer: Uint8Array, start = 0, end = buffer.length, into?: SampleInput): SampleInput { - return decodeFields(buffer, this.decodeValue, start, end, into) + /** @internal */ + static [kDecodeInput](buffer: Uint8Array, start = 0, end = buffer.length, into?: SampleInput): SampleInput { + return decodeFields(buffer, this[kDecodeValue], start, end, into) } static decode(buffer: Uint8Array, start = 0, end = buffer.length): Sample { - return new this(this.decodeInput(buffer, start, end)) + return new this(this[kDecodeInput](buffer, start, end)) } } @@ -1353,7 +1419,8 @@ export class Mapping extends Message { this.attributeIndices = data.attributeIndices || [] } - _measure(context: EncodeContext) { + /** @internal */ + [kMeasure](context: EncodeContext) { let total = 0 total += measureNumberField(MappingField.memoryStart, this.memoryStart) total += measureNumberField(MappingField.memoryLimit, this.memoryLimit) @@ -1363,7 +1430,8 @@ export class Mapping extends Message { return total } - _encodeToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset: number, context: EncodeContext): number { offset = encodeNumberField(buffer, offset, MappingField.memoryStart, this.memoryStart) offset = encodeNumberField(buffer, offset, MappingField.memoryLimit, this.memoryLimit) offset = encodeNumberField(buffer, offset, MappingField.fileOffset, this.fileOffset) @@ -1372,7 +1440,8 @@ export class Mapping extends Message { return offset } - static decodeValue(data: MappingInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) { + /** @internal */ + static [kDecodeValue](data: MappingInput, field: number, buffer: Uint8Array, start: number, end: number, wireType: number) { switch (field) { case MappingField.memoryStart: if (isVarInt(wireType)) data.memoryStart = decodeNumber(buffer, start, end) @@ -1392,12 +1461,13 @@ export class Mapping extends Message { } } - static decodeInput(buffer: Uint8Array, start = 0, end = buffer.length, into?: MappingInput): MappingInput { - return decodeFields(buffer, this.decodeValue, start, end, into) + /** @internal */ + static [kDecodeInput](buffer: Uint8Array, start = 0, end = buffer.length, into?: MappingInput): MappingInput { + return decodeFields(buffer, this[kDecodeValue], start, end, into) } static decode(buffer: Uint8Array, start = 0, end = buffer.length): Mapping { - return new this(this.decodeInput(buffer, start, end)) + return new this(this[kDecodeInput](buffer, start, end)) } } @@ -1421,15 +1491,18 @@ export class Stack extends Message { this.locationIndices = data.locationIndices || [] } - _measure(context: EncodeContext) { + /** @internal */ + [kMeasure](context: EncodeContext) { return measurePackedNumbersField(StackField.locationIndices, this.locationIndices, context) } - _encodeToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset: number, context: EncodeContext): number { return encodePackedNumbersField(buffer, offset, StackField.locationIndices, this.locationIndices, context) } - static decodeValue(data: StackInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) { + /** @internal */ + static [kDecodeValue](data: StackInput, field: number, buffer: Uint8Array, start: number, end: number, wireType: number) { switch (field) { case StackField.locationIndices: if (isPackedOrVarInt(wireType)) data.locationIndices = pushAll(decodeInt32s(buffer, start, end), data.locationIndices) @@ -1437,12 +1510,13 @@ export class Stack extends Message { } } - static decodeInput(buffer: Uint8Array, start = 0, end = buffer.length, into?: StackInput): StackInput { - return decodeFields(buffer, this.decodeValue, start, end, into) + /** @internal */ + static [kDecodeInput](buffer: Uint8Array, start = 0, end = buffer.length, into?: StackInput): StackInput { + return decodeFields(buffer, this[kDecodeValue], start, end, into) } static decode(buffer: Uint8Array, start = 0, end = buffer.length): Stack { - return new this(this.decodeInput(buffer, start, end)) + return new this(this[kDecodeInput](buffer, start, end)) } } @@ -1478,7 +1552,8 @@ export class Location extends Message { this.attributeIndices = data.attributeIndices || [] } - _measure(context: EncodeContext) { + /** @internal */ + [kMeasure](context: EncodeContext) { let total = 0 total += measureNumberField(LocationField.mappingIndex, this.mappingIndex) total += measureNumberField(LocationField.address, this.address) @@ -1487,7 +1562,8 @@ export class Location extends Message { return total } - _encodeToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset: number, context: EncodeContext): number { offset = encodeNumberField(buffer, offset, LocationField.mappingIndex, this.mappingIndex) offset = encodeNumberField(buffer, offset, LocationField.address, this.address) offset = encodeMessageArrayField(buffer, offset, LocationField.lines, this.lines, context) @@ -1495,7 +1571,8 @@ export class Location extends Message { return offset } - static decodeValue(data: LocationInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) { + /** @internal */ + static [kDecodeValue](data: LocationInput, field: number, buffer: Uint8Array, start: number, end: number, wireType: number) { switch (field) { case LocationField.mappingIndex: if (isVarInt(wireType)) data.mappingIndex = decodeInt32(buffer, start, end) @@ -1504,7 +1581,7 @@ export class Location extends Message { if (isVarInt(wireType)) data.address = decodeNumber(buffer, start, end) break case LocationField.lines: - if (isLengthDelim(wireType)) data.lines = push(Line.decodeInput(buffer, start, end), data.lines) + if (isLengthDelim(wireType)) data.lines = push(Line[kDecodeInput](buffer, start, end), data.lines) break case LocationField.attributeIndices: if (isPackedOrVarInt(wireType)) data.attributeIndices = pushAll(decodeInt32s(buffer, start, end), data.attributeIndices) @@ -1512,12 +1589,13 @@ export class Location extends Message { } } - static decodeInput(buffer: Uint8Array, start = 0, end = buffer.length, into?: LocationInput): LocationInput { - return decodeFields(buffer, this.decodeValue, start, end, into) + /** @internal */ + static [kDecodeInput](buffer: Uint8Array, start = 0, end = buffer.length, into?: LocationInput): LocationInput { + return decodeFields(buffer, this[kDecodeValue], start, end, into) } static decode(buffer: Uint8Array, start = 0, end = buffer.length): Location { - return new this(this.decodeInput(buffer, start, end)) + return new this(this[kDecodeInput](buffer, start, end)) } } @@ -1549,7 +1627,8 @@ export class Line extends Message { this.column = data.column || 0 } - _measure() { + /** @internal */ + [kMeasure]() { let total = 0 total += measureNumberField(LineField.functionIndex, this.functionIndex) total += measureNumberField(LineField.line, this.line) @@ -1557,14 +1636,16 @@ export class Line extends Message { return total } - _encodeToBuffer(buffer: Uint8Array, offset = 0): number { + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset = 0): number { offset = encodeNumberField(buffer, offset, LineField.functionIndex, this.functionIndex) offset = encodeNumberField(buffer, offset, LineField.line, this.line) offset = encodeNumberField(buffer, offset, LineField.column, this.column) return offset } - static decodeValue(data: LineInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) { + /** @internal */ + static [kDecodeValue](data: LineInput, field: number, buffer: Uint8Array, start: number, end: number, wireType: number) { switch (field) { case LineField.functionIndex: if (isVarInt(wireType)) data.functionIndex = decodeInt32(buffer, start, end) @@ -1578,12 +1659,13 @@ export class Line extends Message { } } - static decodeInput(buffer: Uint8Array, start = 0, end = buffer.length, into?: LineInput): LineInput { - return decodeFields(buffer, this.decodeValue, start, end, into) + /** @internal */ + static [kDecodeInput](buffer: Uint8Array, start = 0, end = buffer.length, into?: LineInput): LineInput { + return decodeFields(buffer, this[kDecodeValue], start, end, into) } static decode(buffer: Uint8Array, start = 0, end = buffer.length): Line { - return new this(this.decodeInput(buffer, start, end)) + return new this(this[kDecodeInput](buffer, start, end)) } } @@ -1619,7 +1701,8 @@ export class Function extends Message { this.startLine = data.startLine || 0 } - _measure() { + /** @internal */ + [kMeasure]() { let total = 0 total += measureNumberField(FunctionField.nameStrindex, this.nameStrindex) total += measureNumberField(FunctionField.systemNameStrindex, this.systemNameStrindex) @@ -1628,7 +1711,8 @@ export class Function extends Message { return total } - _encodeToBuffer(buffer: Uint8Array, offset = 0): number { + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset = 0): number { offset = encodeNumberField(buffer, offset, FunctionField.nameStrindex, this.nameStrindex) offset = encodeNumberField(buffer, offset, FunctionField.systemNameStrindex, this.systemNameStrindex) offset = encodeNumberField(buffer, offset, FunctionField.filenameStrindex, this.filenameStrindex) @@ -1636,7 +1720,8 @@ export class Function extends Message { return offset } - static decodeValue(data: FunctionInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) { + /** @internal */ + static [kDecodeValue](data: FunctionInput, field: number, buffer: Uint8Array, start: number, end: number, wireType: number) { switch (field) { case FunctionField.nameStrindex: if (isVarInt(wireType)) data.nameStrindex = decodeInt32(buffer, start, end) @@ -1653,12 +1738,13 @@ export class Function extends Message { } } - static decodeInput(buffer: Uint8Array, start = 0, end = buffer.length, into?: FunctionInput): FunctionInput { - return decodeFields(buffer, this.decodeValue, start, end, into) + /** @internal */ + static [kDecodeInput](buffer: Uint8Array, start = 0, end = buffer.length, into?: FunctionInput): FunctionInput { + return decodeFields(buffer, this[kDecodeValue], start, end, into) } static decode(buffer: Uint8Array, start = 0, end = buffer.length): Function { - return new this(this.decodeInput(buffer, start, end)) + return new this(this[kDecodeInput](buffer, start, end)) } } @@ -1690,7 +1776,8 @@ export class KeyValueAndUnit extends Message { this.unitStrindex = data.unitStrindex || 0 } - _measure(context: EncodeContext) { + /** @internal */ + [kMeasure](context: EncodeContext) { let total = 0 total += measureNumberField(KeyValueAndUnitField.keyStrindex, this.keyStrindex) total += measureMessageField(KeyValueAndUnitField.value, this.value, context) @@ -1698,20 +1785,22 @@ export class KeyValueAndUnit extends Message { return total } - _encodeToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset: number, context: EncodeContext): number { offset = encodeNumberField(buffer, offset, KeyValueAndUnitField.keyStrindex, this.keyStrindex) offset = encodeMessageField(buffer, offset, KeyValueAndUnitField.value, this.value, context) offset = encodeNumberField(buffer, offset, KeyValueAndUnitField.unitStrindex, this.unitStrindex) return offset } - static decodeValue(data: KeyValueAndUnitInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) { + /** @internal */ + static [kDecodeValue](data: KeyValueAndUnitInput, field: number, buffer: Uint8Array, start: number, end: number, wireType: number) { switch (field) { case KeyValueAndUnitField.keyStrindex: if (isVarInt(wireType)) data.keyStrindex = decodeInt32(buffer, start, end) break case KeyValueAndUnitField.value: - if (isLengthDelim(wireType)) data.value = AnyValue.decodeInput(buffer, start, end, data.value) + if (isLengthDelim(wireType)) data.value = AnyValue[kDecodeInput](buffer, start, end, data.value) break case KeyValueAndUnitField.unitStrindex: if (isVarInt(wireType)) data.unitStrindex = decodeInt32(buffer, start, end) @@ -1719,12 +1808,13 @@ export class KeyValueAndUnit extends Message { } } - static decodeInput(buffer: Uint8Array, start = 0, end = buffer.length, into?: KeyValueAndUnitInput): KeyValueAndUnitInput { - return decodeFields(buffer, this.decodeValue, start, end, into) + /** @internal */ + static [kDecodeInput](buffer: Uint8Array, start = 0, end = buffer.length, into?: KeyValueAndUnitInput): KeyValueAndUnitInput { + return decodeFields(buffer, this[kDecodeValue], start, end, into) } static decode(buffer: Uint8Array, start = 0, end = buffer.length): KeyValueAndUnit { - return new this(this.decodeInput(buffer, start, end)) + return new this(this[kDecodeInput](buffer, start, end)) } } @@ -1741,7 +1831,7 @@ export class ExportProfilesServiceRequest extends ProfilesData { } static decode(buffer: Uint8Array, start = 0, end = buffer.length): ExportProfilesServiceRequest { - return new this(this.decodeInput(buffer, start, end)) + return new this(this[kDecodeInput](buffer, start, end)) } } @@ -1765,28 +1855,32 @@ export class ExportProfilesServiceResponse extends Message { this.partialSuccess = data.partialSuccess && ExportProfilesPartialSuccess.create(data.partialSuccess) } - _measure(context: EncodeContext) { + /** @internal */ + [kMeasure](context: EncodeContext) { return measureMessageField(ExportProfilesServiceResponseField.partialSuccess, this.partialSuccess, context) } - _encodeToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number { + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset: number, context: EncodeContext): number { return encodeMessageField(buffer, offset, ExportProfilesServiceResponseField.partialSuccess, this.partialSuccess, context) } - static decodeValue(data: ExportProfilesServiceResponseInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) { + /** @internal */ + static [kDecodeValue](data: ExportProfilesServiceResponseInput, field: number, buffer: Uint8Array, start: number, end: number, wireType: number) { switch (field) { case ExportProfilesServiceResponseField.partialSuccess: - if (isLengthDelim(wireType)) data.partialSuccess = ExportProfilesPartialSuccess.decodeInput(buffer, start, end, data.partialSuccess) + if (isLengthDelim(wireType)) data.partialSuccess = ExportProfilesPartialSuccess[kDecodeInput](buffer, start, end, data.partialSuccess) break } } - static decodeInput(buffer: Uint8Array, start = 0, end = buffer.length, into?: ExportProfilesServiceResponseInput): ExportProfilesServiceResponseInput { - return decodeFields(buffer, this.decodeValue, start, end, into) + /** @internal */ + static [kDecodeInput](buffer: Uint8Array, start = 0, end = buffer.length, into?: ExportProfilesServiceResponseInput): ExportProfilesServiceResponseInput { + return decodeFields(buffer, this[kDecodeValue], start, end, into) } static decode(buffer: Uint8Array, start = 0, end = buffer.length): ExportProfilesServiceResponse { - return new this(this.decodeInput(buffer, start, end)) + return new this(this[kDecodeInput](buffer, start, end)) } } @@ -1814,20 +1908,23 @@ export class ExportProfilesPartialSuccess extends Message { this.errorMessage = data.errorMessage || '' } - _measure() { + /** @internal */ + [kMeasure]() { let total = 0 total += measureNumberField(ExportProfilesPartialSuccessField.rejectedProfiles, this.rejectedProfiles) total += measureStringField(ExportProfilesPartialSuccessField.errorMessage, this.errorMessage) return total } - _encodeToBuffer(buffer: Uint8Array, offset = 0): number { + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset = 0): number { offset = encodeNumberField(buffer, offset, ExportProfilesPartialSuccessField.rejectedProfiles, this.rejectedProfiles) offset = encodeStringField(buffer, offset, ExportProfilesPartialSuccessField.errorMessage, this.errorMessage) return offset } - static decodeValue(data: ExportProfilesPartialSuccessInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) { + /** @internal */ + static [kDecodeValue](data: ExportProfilesPartialSuccessInput, field: number, buffer: Uint8Array, start: number, end: number, wireType: number) { switch (field) { case ExportProfilesPartialSuccessField.rejectedProfiles: if (isVarInt(wireType)) data.rejectedProfiles = decodeInt64(buffer, start, end) @@ -1838,11 +1935,12 @@ export class ExportProfilesPartialSuccess extends Message { } } - static decodeInput(buffer: Uint8Array, start = 0, end = buffer.length, into?: ExportProfilesPartialSuccessInput): ExportProfilesPartialSuccessInput { - return decodeFields(buffer, this.decodeValue, start, end, into) + /** @internal */ + static [kDecodeInput](buffer: Uint8Array, start = 0, end = buffer.length, into?: ExportProfilesPartialSuccessInput): ExportProfilesPartialSuccessInput { + return decodeFields(buffer, this[kDecodeValue], start, end, into) } static decode(buffer: Uint8Array, start = 0, end = buffer.length): ExportProfilesPartialSuccess { - return new this(this.decodeInput(buffer, start, end)) + return new this(this[kDecodeInput](buffer, start, end)) } } diff --git a/src/protobuf.test.ts b/src/protobuf.test.ts index 8bbca06..fbf4d7e 100644 --- a/src/protobuf.test.ts +++ b/src/protobuf.test.ts @@ -22,6 +22,8 @@ import { encodeMessageField, encodeNumberField, encodePackedFixed64Field, + kEncodeToBuffer, + kMeasure, kTypeFixed32, kTypeFixed64, kTypeLengthDelim, @@ -34,7 +36,7 @@ import { measurePackedFixed64Field, newEncodeContext } from './protobuf.js' -import { Label, LabelInput, Profile, Sample, StringTable, ValueType, emptyTableToken } from './index.js' +import { Label, Profile, Sample, StringTable, ValueType, emptyTableToken } from './index.js' function hex(buf: Uint8Array) { return Buffer.from(buf).toString('hex') @@ -285,9 +287,9 @@ test('message fields', async (t) => { }) await t.test('measures each submessage once per encode', async () => { - const measure = Label.prototype._measure + const measure = Label.prototype[kMeasure] let calls = 0 - Label.prototype._measure = function (this: Label) { + Label.prototype[kMeasure] = function (this: Label) { calls++ return measure.call(this) } @@ -299,7 +301,7 @@ test('message fields', async (t) => { await profile.encodeAsync() assert.strictEqual(calls, 2, 'encodeAsync()') } finally { - Label.prototype._measure = measure + Label.prototype[kMeasure] = measure } }) @@ -319,11 +321,11 @@ test('message fields', async (t) => { super() } - _measure(context: EncodeContext) { + [kMeasure](context: EncodeContext) { return measureMessageField(1, this.inner, context) } - _encodeToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext) { + [kEncodeToBuffer](buffer: Uint8Array, offset: number, context: EncodeContext) { return encodeMessageField(buffer, offset, 1, this.inner, context) } } @@ -363,8 +365,8 @@ test('StringTable is encoded as the field its message supplies', () => { const table = new StringTable() table.dedup('hi') for (const [field, entries] of [[5, '2a00' + '2a026869'], [16, '820100' + '8201026869']] as const) { - const buffer = new Uint8Array(table._measure(field)) - assert.strictEqual(table._encodeToBuffer(buffer, 0, field), buffer.length, `measured length of field ${field}`) + const buffer = new Uint8Array(table[kMeasure](field)) + assert.strictEqual(table[kEncodeToBuffer](buffer, 0, field), buffer.length, `measured length of field ${field}`) assert.strictEqual(hex(buffer), entries) } // On its own, and in a profile, the table is pprof's string_table @@ -375,11 +377,11 @@ test('StringTable is encoded as the field its message supplies', () => { test('encoding checks', async (t) => { class Lying extends Message { - _measure() { + [kMeasure]() { return 3 } - _encodeToBuffer(buffer: Uint8Array, offset = 0) { + [kEncodeToBuffer](buffer: Uint8Array, offset = 0) { return encodeNumberField(buffer, offset, 1, 1) } } @@ -427,10 +429,4 @@ test('decoders skip fields with mismatched wire types', async (t) => { assert.deepStrictEqual(profile.stringTable.strings, ['']) assert.strictEqual(profile.period, 7) }) - - await t.test('decodeValue without a wire type decodes as before', () => { - const data: LabelInput = {} - Label.decodeValue(data, 1, fromHex('05')) - assert.strictEqual(data.key, 5) - }) }) diff --git a/src/protobuf.ts b/src/protobuf.ts index fd44957..1b13260 100644 --- a/src/protobuf.ts +++ b/src/protobuf.ts @@ -18,10 +18,9 @@ export const kTypeFixed64 = 1 export const kTypeLengthDelim = 2 export const kTypeFixed32 = 5 -// Prefer Buffer.from for speed; use TextEncoder in browsers. -export const toUtf8 = typeof Buffer === 'undefined' - ? (value: string) => new TextEncoder().encode(value) - : (value: string) => Buffer.from(value, 'utf8') +export const kDecodeValue = Symbol() +export const kEncodeToBuffer = Symbol.for('pprof-format.codec.v3.encodeToBuffer') +export const kMeasure = Symbol.for('pprof-format.codec.v3.measure') /* Private helpers. */ @@ -291,21 +290,23 @@ export function newEncodeContext(): EncodeContext { export abstract class Message { /** The encoded length of the message in bytes. */ get length(): number { - return this._measure(newEncodeContext()) + return this[kMeasure](newEncodeContext()) } /** Measures the message, recording its submessages' lengths in `context`. */ - abstract _measure(context: EncodeContext): number + /** @internal */ + abstract [kMeasure](context: EncodeContext): number /** Encodes the message, reading its submessages' lengths from `context`. */ - abstract _encodeToBuffer(buffer: Uint8Array, offset: number, context: EncodeContext): number + /** @internal */ + abstract [kEncodeToBuffer](buffer: Uint8Array, offset: number, context: EncodeContext): number /** Encodes into the given buffer or a new one. Throws RangeError if too small. */ encode(buffer?: Uint8Array): Uint8Array { const context = newEncodeContext() - const length = this._measure(context) + const length = this[kMeasure](context) const target = prepareBuffer(buffer, length) - verifyEncodedLength(this._encodeToBuffer(target, 0, context), length) + verifyEncodedLength(this[kEncodeToBuffer](target, 0, context), length) return target } } @@ -507,7 +508,7 @@ export function measureMessageField(field: number, message: Message | undefined, if (message === undefined) return 0 // Reserve the parent's slot first to match length-prefix order. const slot = context.lengths.push(0) - 1 - const length = message._measure(context) + const length = message[kMeasure](context) context.lengths[slot] = length return measureTag(field) + varintLength(length) + length } @@ -515,7 +516,7 @@ export function measureMessageField(field: number, message: Message | undefined, export function encodeMessageField(buffer: Uint8Array, offset: number, field: number, message: Message | undefined, context: EncodeContext): number { if (message === undefined) return offset offset = encodeMessageFieldHeader(buffer, offset, field, context) - return message._encodeToBuffer(buffer, offset, context) + return message[kEncodeToBuffer](buffer, offset, context) } /** Encodes a message field's tag and recorded length, for encoding its fields separately. */ @@ -540,24 +541,23 @@ export function encodeMessageArrayField(buffer: Uint8Array, offset: number, fiel } /* - * Skip mismatched wire types like unknown fields. An undefined wire type means - * the caller omitted it when calling decodeValue directly, and is accepted. + * Skip mismatched wire types like unknown fields. */ -export function isVarInt(wireType: number | undefined): boolean { - return wireType === undefined || wireType === kTypeVarInt +export function isVarInt(wireType: number): boolean { + return wireType === kTypeVarInt } -export function isLengthDelim(wireType: number | undefined): boolean { - return wireType === undefined || wireType === kTypeLengthDelim +export function isLengthDelim(wireType: number): boolean { + return wireType === kTypeLengthDelim } -export function isFixed64(wireType: number | undefined): boolean { - return wireType === undefined || wireType === kTypeFixed64 +export function isFixed64(wireType: number): boolean { + return wireType === kTypeFixed64 } /** Accepts both packed and unpacked varints for repeated fields. */ -export function isPackedOrVarInt(wireType: number | undefined): boolean { +export function isPackedOrVarInt(wireType: number): boolean { return isVarInt(wireType) || wireType === kTypeLengthDelim } diff --git a/src/string-table.ts b/src/string-table.ts index 14a495c..44449a7 100644 --- a/src/string-table.ts +++ b/src/string-table.ts @@ -8,29 +8,22 @@ import { decodeString, encodeNumber, encodeTag, + kEncodeToBuffer, + kMeasure, kTypeLengthDelim, measureTag, prepareBuffer, - toUtf8, utf8Length, varintLength, } from './protobuf.js' export const emptyTableToken = Symbol() +export const kDecodeString = Symbol() // pprof's Profile.string_table, which the table's own encode() produces const pprofStringTableField = 6 // A string's length prefix and UTF-8 bytes, which don't depend on the field -function encodeLengthDelimited(utf8: Uint8Array | Buffer): Uint8Array { - const buffer = new Uint8Array(varintLength(utf8.length) + utf8.length) - const offset = encodeNumber(buffer, 0, utf8.length) - if (utf8.length > 0) { - buffer.set(utf8, offset) - } - return buffer -} - const utf8ByteLength = typeof Buffer === 'undefined' ? utf8Length : (value: string) => Buffer.byteLength(value, 'utf8') @@ -68,16 +61,18 @@ export class StringTable { /** The encoded length of the strings as pprof's string_table. */ get encodedLength(): number { - return this._measure(pprofStringTableField) + return this[kMeasure](pprofStringTableField) } /** Measures the strings as the repeated string field `field`. */ - _measure(field: number): number { + /** @internal */ + [kMeasure](field: number): number { return this.#byteLengths.length * measureTag(field) + this.#encodedLength } /** Encodes the strings as the repeated string field `field`. */ - _encodeToBuffer(buffer: Uint8Array, offset: number, field: number): number { + /** @internal */ + [kEncodeToBuffer](buffer: Uint8Array, offset: number, field: number): number { const writeUtf8 = utf8Writer(buffer) for (let i = 0; i < this.#byteLengths.length; i++) { const byteLength = this.#byteLengths[i] @@ -97,22 +92,10 @@ export class StringTable { /** Encodes the strings as pprof's string_table. */ encode(buffer?: Uint8Array): Uint8Array { buffer = prepareBuffer(buffer, this.encodedLength) - this._encodeToBuffer(buffer, 0, pprofStringTableField) - return buffer - } - - /** Encodes one pprof string_table entry, including its tag and length. */ - static _encodeStringFromUtf8(stringBuffer: Uint8Array | Buffer): Uint8Array { - const entry = encodeLengthDelimited(stringBuffer) - const buffer = new Uint8Array(measureTag(pprofStringTableField) + entry.length) - buffer.set(entry, encodeTag(buffer, 0, pprofStringTableField, kTypeLengthDelim)) + this[kEncodeToBuffer](buffer, 0, pprofStringTableField) return buffer } - static _encodeString(string: string): Uint8Array { - return StringTable._encodeStringFromUtf8(toUtf8(string)) - } - dedup(value: string): number { if (typeof value === 'number') return value let position = this.#positions.get(value) @@ -122,7 +105,8 @@ export class StringTable { return position } - _decodeString(buffer: Uint8Array) { + /** @internal */ + [kDecodeString](buffer: Uint8Array) { const position = this.#add(decodeString(buffer), buffer.length) this.#decoded[position] = buffer.length <= 64 || typeof Buffer === 'undefined' ? new Uint8Array(buffer) diff --git a/tsconfig.json b/tsconfig.json index eb40c70..9fe9fc5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,6 +5,7 @@ "rootDir": "src", "moduleResolution": "Node16", "declaration": true, + "stripInternal": true, "noImplicitAny": true, "preserveConstEnums": true, "sourceMap": true,