Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 23 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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',
Expand Down
25 changes: 22 additions & 3 deletions src/string-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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<string>()
declare readonly strings: readonly string[]
#strings = new Array<string>()
#snapshot?: readonly string[]
#byteLengths = new Array<number>()
// The bytes of strings read from a profile, which re-encoding reproduces exactly
#decoded = new Array<Uint8Array | undefined>()
#positions = new Map<string, number>()
#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)
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
Loading