Skip to content

Commit ea7fd72

Browse files
committed
chore: made SpanId opaque to prevent type coersion
1 parent b6f6e6d commit ea7fd72

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

src/tracer/Tracer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Tracer {
1313
if (result.done || result.value == null) {
1414
throw new Error('Unexpected end of id generator');
1515
}
16-
return result.value.toMultibase('base32hex');
16+
return result.value.toMultibase('base32hex') as SpanId;
1717
}
1818

1919
protected queueSpanEvent(evt: SpanEvent) {
@@ -46,7 +46,7 @@ class Tracer {
4646

4747
public async traced<T>(
4848
name: string,
49-
parentSpanId: string | undefined,
49+
parentSpanId: SpanId | undefined,
5050
fn: () => T | Promise<T>,
5151
): Promise<T> {
5252
const fnProm = async () => {

src/tracer/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
type SpanId = string;
1+
type SpanId = string & { readonly brand: unique symbol };
22

33
/**
44
* A span is a virtual concept, not an actual object. A span is made up of

tests/asciinemaTest.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import type { SpanId } from '#tracer/index.js';
12
import fs from 'fs';
23
import * as fc from 'fast-check';
34
import tracer from '#tracer/index.js';
45

56
let parentIndex = 0;
67
let step = 0;
7-
let nestedIds: Array<string> = [];
8+
let nestedIds: Array<SpanId> = [];
89

910
type Flags = {
1011
hasForkA: boolean;
@@ -18,9 +19,9 @@ type Flags = {
1819
};
1920

2021
const current: {
21-
parentId?: string;
22-
forkAId?: string;
23-
forkBId?: string;
22+
parentId?: SpanId;
23+
forkAId?: SpanId;
24+
forkBId?: SpanId;
2425
flags: Flags;
2526
} = {
2627
flags: {

0 commit comments

Comments
 (0)