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 @@ -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.
53 changes: 52 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 = {
Expand Down
Loading
Loading