diff --git a/README.md b/README.md index 511c2d0..7219260 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,10 @@ a dictionary shared by all profiles of a request. Each dictionary table starts with the zero value of its type at index 0, which means "not set"; tables that aren't given default to just that entry. +In both codecs, read strings by index or iterate over `table.strings`. It +returns a frozen snapshot. Read it again after adding strings with +`table.dedup()`. + ```js import { ExportProfilesServiceRequest, diff --git a/src/index.test.ts b/src/index.test.ts index 9202dd7..2d973b6 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -10,6 +10,7 @@ import assert from 'node:assert' import { Buffer as NodeBuffer } from 'node:buffer' import { spawnSync } from 'node:child_process' import { fileURLToPath } from 'node:url' +import { inspect } from 'node:util' import { gunzipSync } from 'zlib' import * as fs from 'fs' @@ -460,6 +461,28 @@ function bufToHex(buf: Uint8Array) { } test('StringTable', async (t) => { + await t.test('exposes a read-only snapshot without changing encoded strings', () => { + const table = new StringTable() + const index = table.dedup('abc') + const strings = table.strings + + assert.strictEqual(Reflect.set(strings, index, 'x'), false) + assert.strictEqual(Reflect.set(table, 'strings', []), false) + assert.strictEqual(table.dedup('abc'), index) + assert.strictEqual(table.strings, strings) + + table.dedup('def') + assert.deepStrictEqual(strings, ['', 'abc']) + assert.deepStrictEqual(table.strings, ['', 'abc', 'def']) + const decoded = Profile.decode(new Profile({ stringTable: table }).encode()) + assert.deepStrictEqual(decoded.stringTable.strings, table.strings) + assert.deepStrictEqual({ ...decoded.stringTable }, { strings: ['', 'abc', 'def'] }) + assert.strictEqual(JSON.stringify(decoded.stringTable), '{"strings":["","abc","def"]}') + assert.match(inspect(decoded), /'abc'/) + assert.strictEqual(Reflect.set(decoded.stringTable.strings, index, 'x'), false) + assert.deepStrictEqual(Profile.decode(decoded.encode()).stringTable.strings, table.strings) + }) + await t.test('encodes correctly', async () => { const encodings = { '': '3200', diff --git a/src/string-table.ts b/src/string-table.ts index 14a495c..1a064ae 100644 --- a/src/string-table.ts +++ b/src/string-table.ts @@ -18,6 +18,8 @@ import { export const emptyTableToken = Symbol() +const inspect = Symbol.for('nodejs.util.inspect.custom') + // pprof's Profile.string_table, which the table's own encode() produces const pprofStringTableField = 6 @@ -52,20 +54,37 @@ function utf8Writer(buffer: Uint8Array): (value: string, offset: number) => void * table, so one table type serves both pprof and OTel. */ export class StringTable { - strings = new Array() + declare readonly strings: readonly string[] + #strings = new Array() + #snapshot?: readonly string[] #byteLengths = new Array() // The bytes of strings read from a profile, which re-encoding reproduces exactly #decoded = new Array() #positions = new Map() #encodedLength = 0 + static #stringsDescriptor = { + enumerable: true, + get(this: StringTable): readonly string[] { + if (this.#snapshot?.length !== this.#strings.length) { + this.#snapshot = Object.freeze(this.#strings.slice()) + } + return this.#snapshot + }, + } + /** @param tok - emptyTableToken omits the initial empty string. */ constructor(tok?: typeof emptyTableToken) { + Object.defineProperty(this, 'strings', StringTable.#stringsDescriptor) if (tok !== emptyTableToken) { this.dedup('') } } + [inspect]() { + return { strings: this.strings } + } + /** The encoded length of the strings as pprof's string_table. */ get encodedLength(): number { return this._measure(pprofStringTableField) @@ -87,7 +106,7 @@ export class StringTable { if (decoded !== undefined) { buffer.set(decoded, offset) } else if (byteLength > 0) { - writeUtf8(this.strings[i], offset) + writeUtf8(this.#strings[i], offset) } offset += byteLength } @@ -130,7 +149,7 @@ export class StringTable { } #add(value: string, byteLength: number): number { - const position = this.strings.push(value) - 1 + const position = this.#strings.push(value) - 1 this.#positions.set(value, position) this.#byteLengths.push(byteLength) this.#encodedLength += varintLength(byteLength) + byteLength