@libo11y/test 0.0.0-stage → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +120 -2
- package/dist/assertions.d.ts +47 -0
- package/dist/assertions.js +170 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +7 -0
- package/dist/otlp-schema.d.ts +20 -0
- package/dist/otlp-schema.js +294 -0
- package/dist/protowire.d.ts +68 -0
- package/dist/protowire.js +299 -0
- package/dist/recorder.d.ts +94 -0
- package/dist/recorder.js +301 -0
- package/dist/sink.d.ts +48 -0
- package/dist/sink.js +199 -0
- package/package.json +38 -4
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The OTLP subset the sink decodes, as protowire schemas: the three Export*ServiceRequest messages
|
|
3
|
+
* of opentelemetry-proto v1, with every field upstream's protobuf serializers emit plus the metric
|
|
4
|
+
* exemplars (NumberDataPoint field 5, HistogramDataPoint field 8, ExponentialHistogramDataPoint
|
|
5
|
+
* field 11), summaries, and the trace and log fields upstream leaves out. Field names are the
|
|
6
|
+
* OTLP/JSON (lowerCamelCase) names.
|
|
7
|
+
*/
|
|
8
|
+
import { decodeMessage } from "./protowire.js";
|
|
9
|
+
// keyValues is a repeated KeyValue field, as every attribute list is.
|
|
10
|
+
function keyValues(name) {
|
|
11
|
+
return { name, type: "message", message: "KeyValue", repeated: true };
|
|
12
|
+
}
|
|
13
|
+
const SCHEMAS = {
|
|
14
|
+
// common/v1
|
|
15
|
+
AnyValue: {
|
|
16
|
+
1: { name: "stringValue", type: "string", oneof: "value" },
|
|
17
|
+
2: { name: "boolValue", type: "bool", oneof: "value" },
|
|
18
|
+
3: { name: "intValue", type: "int64", oneof: "value" },
|
|
19
|
+
4: { name: "doubleValue", type: "double", oneof: "value" },
|
|
20
|
+
5: { name: "arrayValue", type: "message", message: "ArrayValue", oneof: "value" },
|
|
21
|
+
6: { name: "kvlistValue", type: "message", message: "KeyValueList", oneof: "value" },
|
|
22
|
+
7: { name: "bytesValue", type: "base64", oneof: "value" },
|
|
23
|
+
},
|
|
24
|
+
ArrayValue: { 1: { name: "values", type: "message", message: "AnyValue", repeated: true } },
|
|
25
|
+
KeyValueList: { 1: { name: "values", type: "message", message: "KeyValue", repeated: true } },
|
|
26
|
+
KeyValue: {
|
|
27
|
+
1: { name: "key", type: "string" },
|
|
28
|
+
2: { name: "value", type: "message", message: "AnyValue" },
|
|
29
|
+
},
|
|
30
|
+
InstrumentationScope: {
|
|
31
|
+
1: { name: "name", type: "string" },
|
|
32
|
+
2: { name: "version", type: "string" },
|
|
33
|
+
3: keyValues("attributes"),
|
|
34
|
+
4: { name: "droppedAttributesCount", type: "uint32" },
|
|
35
|
+
},
|
|
36
|
+
// resource/v1
|
|
37
|
+
Resource: {
|
|
38
|
+
1: keyValues("attributes"),
|
|
39
|
+
2: { name: "droppedAttributesCount", type: "uint32" },
|
|
40
|
+
},
|
|
41
|
+
// collector/trace/v1 and trace/v1
|
|
42
|
+
ExportTraceServiceRequest: {
|
|
43
|
+
1: { name: "resourceSpans", type: "message", message: "ResourceSpans", repeated: true },
|
|
44
|
+
},
|
|
45
|
+
ResourceSpans: {
|
|
46
|
+
1: { name: "resource", type: "message", message: "Resource" },
|
|
47
|
+
2: { name: "scopeSpans", type: "message", message: "ScopeSpans", repeated: true },
|
|
48
|
+
3: { name: "schemaUrl", type: "string" },
|
|
49
|
+
},
|
|
50
|
+
ScopeSpans: {
|
|
51
|
+
1: { name: "scope", type: "message", message: "InstrumentationScope" },
|
|
52
|
+
2: { name: "spans", type: "message", message: "Span", repeated: true },
|
|
53
|
+
3: { name: "schemaUrl", type: "string" },
|
|
54
|
+
},
|
|
55
|
+
Span: {
|
|
56
|
+
1: { name: "traceId", type: "hex" },
|
|
57
|
+
2: { name: "spanId", type: "hex" },
|
|
58
|
+
3: { name: "traceState", type: "string" },
|
|
59
|
+
4: { name: "parentSpanId", type: "hex" },
|
|
60
|
+
5: { name: "name", type: "string" },
|
|
61
|
+
6: { name: "kind", type: "int32" },
|
|
62
|
+
7: { name: "startTimeUnixNano", type: "time" },
|
|
63
|
+
8: { name: "endTimeUnixNano", type: "time" },
|
|
64
|
+
9: keyValues("attributes"),
|
|
65
|
+
10: { name: "droppedAttributesCount", type: "uint32" },
|
|
66
|
+
11: { name: "events", type: "message", message: "SpanEvent", repeated: true },
|
|
67
|
+
12: { name: "droppedEventsCount", type: "uint32" },
|
|
68
|
+
13: { name: "links", type: "message", message: "SpanLink", repeated: true },
|
|
69
|
+
14: { name: "droppedLinksCount", type: "uint32" },
|
|
70
|
+
15: { name: "status", type: "message", message: "Status" },
|
|
71
|
+
16: { name: "flags", type: "fixed32" },
|
|
72
|
+
},
|
|
73
|
+
SpanEvent: {
|
|
74
|
+
1: { name: "timeUnixNano", type: "time" },
|
|
75
|
+
2: { name: "name", type: "string" },
|
|
76
|
+
3: keyValues("attributes"),
|
|
77
|
+
4: { name: "droppedAttributesCount", type: "uint32" },
|
|
78
|
+
},
|
|
79
|
+
SpanLink: {
|
|
80
|
+
1: { name: "traceId", type: "hex" },
|
|
81
|
+
2: { name: "spanId", type: "hex" },
|
|
82
|
+
3: { name: "traceState", type: "string" },
|
|
83
|
+
4: keyValues("attributes"),
|
|
84
|
+
5: { name: "droppedAttributesCount", type: "uint32" },
|
|
85
|
+
6: { name: "flags", type: "fixed32" },
|
|
86
|
+
},
|
|
87
|
+
Status: {
|
|
88
|
+
2: { name: "message", type: "string" },
|
|
89
|
+
3: { name: "code", type: "int32" },
|
|
90
|
+
},
|
|
91
|
+
// collector/metrics/v1 and metrics/v1
|
|
92
|
+
ExportMetricsServiceRequest: {
|
|
93
|
+
1: { name: "resourceMetrics", type: "message", message: "ResourceMetrics", repeated: true },
|
|
94
|
+
},
|
|
95
|
+
ResourceMetrics: {
|
|
96
|
+
1: { name: "resource", type: "message", message: "Resource" },
|
|
97
|
+
2: { name: "scopeMetrics", type: "message", message: "ScopeMetrics", repeated: true },
|
|
98
|
+
3: { name: "schemaUrl", type: "string" },
|
|
99
|
+
},
|
|
100
|
+
ScopeMetrics: {
|
|
101
|
+
1: { name: "scope", type: "message", message: "InstrumentationScope" },
|
|
102
|
+
2: { name: "metrics", type: "message", message: "Metric", repeated: true },
|
|
103
|
+
3: { name: "schemaUrl", type: "string" },
|
|
104
|
+
},
|
|
105
|
+
Metric: {
|
|
106
|
+
1: { name: "name", type: "string" },
|
|
107
|
+
2: { name: "description", type: "string" },
|
|
108
|
+
3: { name: "unit", type: "string" },
|
|
109
|
+
5: { name: "gauge", type: "message", message: "Gauge", oneof: "data" },
|
|
110
|
+
7: { name: "sum", type: "message", message: "Sum", oneof: "data" },
|
|
111
|
+
9: { name: "histogram", type: "message", message: "Histogram", oneof: "data" },
|
|
112
|
+
10: { name: "exponentialHistogram", type: "message", message: "ExponentialHistogram", oneof: "data" },
|
|
113
|
+
11: { name: "summary", type: "message", message: "Summary", oneof: "data" },
|
|
114
|
+
12: keyValues("metadata"),
|
|
115
|
+
},
|
|
116
|
+
Gauge: { 1: { name: "dataPoints", type: "message", message: "NumberDataPoint", repeated: true } },
|
|
117
|
+
Sum: {
|
|
118
|
+
1: { name: "dataPoints", type: "message", message: "NumberDataPoint", repeated: true },
|
|
119
|
+
2: { name: "aggregationTemporality", type: "int32" },
|
|
120
|
+
3: { name: "isMonotonic", type: "bool" },
|
|
121
|
+
},
|
|
122
|
+
Histogram: {
|
|
123
|
+
1: { name: "dataPoints", type: "message", message: "HistogramDataPoint", repeated: true },
|
|
124
|
+
2: { name: "aggregationTemporality", type: "int32" },
|
|
125
|
+
},
|
|
126
|
+
ExponentialHistogram: {
|
|
127
|
+
1: { name: "dataPoints", type: "message", message: "ExponentialHistogramDataPoint", repeated: true },
|
|
128
|
+
2: { name: "aggregationTemporality", type: "int32" },
|
|
129
|
+
},
|
|
130
|
+
Summary: { 1: { name: "dataPoints", type: "message", message: "SummaryDataPoint", repeated: true } },
|
|
131
|
+
NumberDataPoint: {
|
|
132
|
+
7: keyValues("attributes"),
|
|
133
|
+
2: { name: "startTimeUnixNano", type: "time" },
|
|
134
|
+
3: { name: "timeUnixNano", type: "time" },
|
|
135
|
+
4: { name: "asDouble", type: "double", oneof: "value" },
|
|
136
|
+
6: { name: "asInt", type: "sfixed64", oneof: "value" },
|
|
137
|
+
5: { name: "exemplars", type: "message", message: "Exemplar", repeated: true },
|
|
138
|
+
8: { name: "flags", type: "uint32" },
|
|
139
|
+
},
|
|
140
|
+
HistogramDataPoint: {
|
|
141
|
+
9: keyValues("attributes"),
|
|
142
|
+
2: { name: "startTimeUnixNano", type: "time" },
|
|
143
|
+
3: { name: "timeUnixNano", type: "time" },
|
|
144
|
+
4: { name: "count", type: "fixed64" },
|
|
145
|
+
5: { name: "sum", type: "double" },
|
|
146
|
+
6: { name: "bucketCounts", type: "fixed64", repeated: true },
|
|
147
|
+
7: { name: "explicitBounds", type: "double", repeated: true },
|
|
148
|
+
8: { name: "exemplars", type: "message", message: "Exemplar", repeated: true },
|
|
149
|
+
10: { name: "flags", type: "uint32" },
|
|
150
|
+
11: { name: "min", type: "double" },
|
|
151
|
+
12: { name: "max", type: "double" },
|
|
152
|
+
},
|
|
153
|
+
ExponentialHistogramDataPoint: {
|
|
154
|
+
1: keyValues("attributes"),
|
|
155
|
+
2: { name: "startTimeUnixNano", type: "time" },
|
|
156
|
+
3: { name: "timeUnixNano", type: "time" },
|
|
157
|
+
4: { name: "count", type: "fixed64" },
|
|
158
|
+
5: { name: "sum", type: "double" },
|
|
159
|
+
6: { name: "scale", type: "sint32" },
|
|
160
|
+
7: { name: "zeroCount", type: "fixed64" },
|
|
161
|
+
8: { name: "positive", type: "message", message: "Buckets" },
|
|
162
|
+
9: { name: "negative", type: "message", message: "Buckets" },
|
|
163
|
+
10: { name: "flags", type: "uint32" },
|
|
164
|
+
11: { name: "exemplars", type: "message", message: "Exemplar", repeated: true },
|
|
165
|
+
12: { name: "min", type: "double" },
|
|
166
|
+
13: { name: "max", type: "double" },
|
|
167
|
+
14: { name: "zeroThreshold", type: "double" },
|
|
168
|
+
},
|
|
169
|
+
Buckets: {
|
|
170
|
+
1: { name: "offset", type: "sint32" },
|
|
171
|
+
2: { name: "bucketCounts", type: "uint64", repeated: true },
|
|
172
|
+
},
|
|
173
|
+
SummaryDataPoint: {
|
|
174
|
+
7: keyValues("attributes"),
|
|
175
|
+
2: { name: "startTimeUnixNano", type: "time" },
|
|
176
|
+
3: { name: "timeUnixNano", type: "time" },
|
|
177
|
+
4: { name: "count", type: "fixed64" },
|
|
178
|
+
5: { name: "sum", type: "double" },
|
|
179
|
+
6: { name: "quantileValues", type: "message", message: "ValueAtQuantile", repeated: true },
|
|
180
|
+
8: { name: "flags", type: "uint32" },
|
|
181
|
+
},
|
|
182
|
+
ValueAtQuantile: {
|
|
183
|
+
1: { name: "quantile", type: "double" },
|
|
184
|
+
2: { name: "value", type: "double" },
|
|
185
|
+
},
|
|
186
|
+
Exemplar: {
|
|
187
|
+
7: keyValues("filteredAttributes"),
|
|
188
|
+
2: { name: "timeUnixNano", type: "time" },
|
|
189
|
+
3: { name: "asDouble", type: "double", oneof: "value" },
|
|
190
|
+
6: { name: "asInt", type: "sfixed64", oneof: "value" },
|
|
191
|
+
4: { name: "spanId", type: "hex" },
|
|
192
|
+
5: { name: "traceId", type: "hex" },
|
|
193
|
+
},
|
|
194
|
+
// collector/logs/v1 and logs/v1
|
|
195
|
+
ExportLogsServiceRequest: {
|
|
196
|
+
1: { name: "resourceLogs", type: "message", message: "ResourceLogs", repeated: true },
|
|
197
|
+
},
|
|
198
|
+
ResourceLogs: {
|
|
199
|
+
1: { name: "resource", type: "message", message: "Resource" },
|
|
200
|
+
2: { name: "scopeLogs", type: "message", message: "ScopeLogs", repeated: true },
|
|
201
|
+
3: { name: "schemaUrl", type: "string" },
|
|
202
|
+
},
|
|
203
|
+
ScopeLogs: {
|
|
204
|
+
1: { name: "scope", type: "message", message: "InstrumentationScope" },
|
|
205
|
+
2: { name: "logRecords", type: "message", message: "LogRecord", repeated: true },
|
|
206
|
+
3: { name: "schemaUrl", type: "string" },
|
|
207
|
+
},
|
|
208
|
+
LogRecord: {
|
|
209
|
+
1: { name: "timeUnixNano", type: "time" },
|
|
210
|
+
11: { name: "observedTimeUnixNano", type: "time" },
|
|
211
|
+
2: { name: "severityNumber", type: "int32" },
|
|
212
|
+
3: { name: "severityText", type: "string" },
|
|
213
|
+
5: { name: "body", type: "message", message: "AnyValue" },
|
|
214
|
+
6: keyValues("attributes"),
|
|
215
|
+
7: { name: "droppedAttributesCount", type: "uint32" },
|
|
216
|
+
8: { name: "flags", type: "fixed32" },
|
|
217
|
+
9: { name: "traceId", type: "hex" },
|
|
218
|
+
10: { name: "spanId", type: "hex" },
|
|
219
|
+
12: { name: "eventName", type: "string" },
|
|
220
|
+
},
|
|
221
|
+
};
|
|
222
|
+
const REQUEST = {
|
|
223
|
+
traces: "ExportTraceServiceRequest",
|
|
224
|
+
metrics: "ExportMetricsServiceRequest",
|
|
225
|
+
logs: "ExportLogsServiceRequest",
|
|
226
|
+
};
|
|
227
|
+
/**
|
|
228
|
+
* Decodes an OTLP/HTTP protobuf request body for signal into its OTLP/JSON shape: IDs are lowercase
|
|
229
|
+
* hex, *UnixNano timestamps decimal strings, other 64-bit integers numbers when safe (else decimal
|
|
230
|
+
* strings), bytes values base64 and enums numbers. Throws on malformed input.
|
|
231
|
+
*/
|
|
232
|
+
export function decodeOtlpProtobuf(signal, body) {
|
|
233
|
+
return decodeMessage(SCHEMAS, REQUEST[signal], body);
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* The fields that have explicit presence, so a zero value is data: every oneof member (AnyValue's
|
|
237
|
+
* values, the data points' and exemplars' asInt and asDouble, a Metric's data kind) and the proto3
|
|
238
|
+
* optional sum, min and max of the histogram data points. A summary point's plain sum shares the
|
|
239
|
+
* name and is kept too, which only makes the comparison stricter.
|
|
240
|
+
*/
|
|
241
|
+
const PRESENT = new Set([
|
|
242
|
+
"stringValue",
|
|
243
|
+
"boolValue",
|
|
244
|
+
"intValue",
|
|
245
|
+
"doubleValue",
|
|
246
|
+
"arrayValue",
|
|
247
|
+
"kvlistValue",
|
|
248
|
+
"bytesValue",
|
|
249
|
+
"asInt",
|
|
250
|
+
"asDouble",
|
|
251
|
+
"gauge",
|
|
252
|
+
"sum",
|
|
253
|
+
"histogram",
|
|
254
|
+
"exponentialHistogram",
|
|
255
|
+
"summary",
|
|
256
|
+
"min",
|
|
257
|
+
"max",
|
|
258
|
+
]);
|
|
259
|
+
function isEmpty(v) {
|
|
260
|
+
if (v === undefined || v === null || v === 0 || v === false || v === "")
|
|
261
|
+
return true;
|
|
262
|
+
if (Array.isArray(v))
|
|
263
|
+
return v.length === 0;
|
|
264
|
+
return typeof v === "object" && Object.keys(v).length === 0;
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Normalizes an OTLP/JSON-shaped value so a decoded protobuf payload and the equivalent JSON
|
|
268
|
+
* payload compare equal:
|
|
269
|
+
* - a plain scalar field whose value is a proto3 default (0, false, ""), an empty list or an empty
|
|
270
|
+
* object after normalization is removed, because protobuf does not carry it and upstream's JSON
|
|
271
|
+
* serializers write some of them. A oneof member or proto3 optional field keeps its zero value,
|
|
272
|
+
* since there zero is data; only undefined and null are removed;
|
|
273
|
+
* - `schemaUrl` inside a `resource` is removed: upstream's JSON writes it there as well as on the
|
|
274
|
+
* Resource* message, but the OTLP Resource message has no such field.
|
|
275
|
+
* List elements are normalized but never removed, so bucket counts keep their zeros.
|
|
276
|
+
*/
|
|
277
|
+
export function normalizeOtlp(value) {
|
|
278
|
+
return normalize(value, "");
|
|
279
|
+
}
|
|
280
|
+
function normalize(value, key) {
|
|
281
|
+
if (Array.isArray(value))
|
|
282
|
+
return value.map((v) => normalize(v, ""));
|
|
283
|
+
if (typeof value !== "object" || value === null || value instanceof Uint8Array)
|
|
284
|
+
return value;
|
|
285
|
+
const out = {};
|
|
286
|
+
for (const [k, v] of Object.entries(value)) {
|
|
287
|
+
if (key === "resource" && k === "schemaUrl")
|
|
288
|
+
continue;
|
|
289
|
+
const n = normalize(v, k);
|
|
290
|
+
if (PRESENT.has(k) ? n !== undefined && n !== null : !isEmpty(n))
|
|
291
|
+
out[k] = n;
|
|
292
|
+
}
|
|
293
|
+
return out;
|
|
294
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A dependency-free protobuf wire decoder for wire types 0 (varint), 1 (64-bit), 2 (length
|
|
3
|
+
* delimited) and 5 (32-bit), driven by a schema table. It decodes into OTLP/JSON-shaped objects,
|
|
4
|
+
* matching how upstream's JSON serializers represent each value:
|
|
5
|
+
* - bytes named as IDs are lowercase hex, other bytes are standard base64;
|
|
6
|
+
* - a "time" (a fixed64 nanosecond timestamp) is a decimal string;
|
|
7
|
+
* - other 64-bit integers are numbers when they are safe integers, else decimal strings;
|
|
8
|
+
* - enums are their numbers, and absent fields are absent (proto3 has no presence for scalars).
|
|
9
|
+
* Unknown fields are skipped. A repeated scalar accepts packed and unpacked encodings, and a
|
|
10
|
+
* singular embedded message seen twice is merged as protobuf requires (lists append, embedded
|
|
11
|
+
* messages merge, scalars last-win), and the last member of a oneof wins. Messages nest at most
|
|
12
|
+
* MAX_DEPTH deep, and field number 0 is refused.
|
|
13
|
+
*/
|
|
14
|
+
export declare const WIRE_VARINT = 0;
|
|
15
|
+
export declare const WIRE_I64 = 1;
|
|
16
|
+
export declare const WIRE_LEN = 2;
|
|
17
|
+
export declare const WIRE_FIXED32 = 5;
|
|
18
|
+
/** How a field's value is represented. */
|
|
19
|
+
export type FieldType = "string" | "bool" | "uint32" | "int32" | "sint32" | "uint64" | "int64" | "fixed32" | "fixed64" | "sfixed64" | "double" | "time" | "hex" | "base64" | "message";
|
|
20
|
+
/** One field of a message schema. */
|
|
21
|
+
export interface FieldSchema {
|
|
22
|
+
/** The OTLP/JSON (lowerCamelCase) name. */
|
|
23
|
+
readonly name: string;
|
|
24
|
+
readonly type: FieldType;
|
|
25
|
+
/** The schema name of an embedded message. */
|
|
26
|
+
readonly message?: string;
|
|
27
|
+
readonly repeated?: boolean;
|
|
28
|
+
/** The oneof the field belongs to: setting it clears the oneof's other members. */
|
|
29
|
+
readonly oneof?: string;
|
|
30
|
+
}
|
|
31
|
+
/** A message schema: its fields by field number. */
|
|
32
|
+
export type MessageSchema = Readonly<Record<number, FieldSchema>>;
|
|
33
|
+
/** Reads protobuf wire format from bytes. */
|
|
34
|
+
export declare class ProtoReader {
|
|
35
|
+
private readonly buf;
|
|
36
|
+
private readonly view;
|
|
37
|
+
private pos;
|
|
38
|
+
constructor(buf: Uint8Array);
|
|
39
|
+
/** Reports whether every byte has been read. */
|
|
40
|
+
done(): boolean;
|
|
41
|
+
tag(): {
|
|
42
|
+
field: number;
|
|
43
|
+
wireType: number;
|
|
44
|
+
};
|
|
45
|
+
/** A varint as an unsigned 64-bit value. */
|
|
46
|
+
varint(): bigint;
|
|
47
|
+
private need;
|
|
48
|
+
/** A little-endian 64-bit value, unsigned. */
|
|
49
|
+
fixed64(): bigint;
|
|
50
|
+
/** A little-endian 64-bit IEEE 754 double. */
|
|
51
|
+
double(): number;
|
|
52
|
+
/** A little-endian 32-bit value, unsigned. */
|
|
53
|
+
fixed32(): number;
|
|
54
|
+
/** A length-delimited field's bytes, as a view into the input. */
|
|
55
|
+
bytes(): Uint8Array;
|
|
56
|
+
/** Skips one value of wireType. */
|
|
57
|
+
skip(wireType: number): void;
|
|
58
|
+
}
|
|
59
|
+
/** How deep messages may nest, counting the outermost: deeper input is refused. */
|
|
60
|
+
export declare const MAX_DEPTH = 100;
|
|
61
|
+
type Schemas = Readonly<Record<string, MessageSchema>>;
|
|
62
|
+
/**
|
|
63
|
+
* Decodes bytes as the message schemas[type] describes. Throws a protowire: error for malformed
|
|
64
|
+
* input, field number 0, a wire type that does not match the schema, or messages nested deeper
|
|
65
|
+
* than MAX_DEPTH.
|
|
66
|
+
*/
|
|
67
|
+
export declare function decodeMessage(schemas: Schemas, type: string, bytes: Uint8Array): Record<string, unknown>;
|
|
68
|
+
export {};
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A dependency-free protobuf wire decoder for wire types 0 (varint), 1 (64-bit), 2 (length
|
|
3
|
+
* delimited) and 5 (32-bit), driven by a schema table. It decodes into OTLP/JSON-shaped objects,
|
|
4
|
+
* matching how upstream's JSON serializers represent each value:
|
|
5
|
+
* - bytes named as IDs are lowercase hex, other bytes are standard base64;
|
|
6
|
+
* - a "time" (a fixed64 nanosecond timestamp) is a decimal string;
|
|
7
|
+
* - other 64-bit integers are numbers when they are safe integers, else decimal strings;
|
|
8
|
+
* - enums are their numbers, and absent fields are absent (proto3 has no presence for scalars).
|
|
9
|
+
* Unknown fields are skipped. A repeated scalar accepts packed and unpacked encodings, and a
|
|
10
|
+
* singular embedded message seen twice is merged as protobuf requires (lists append, embedded
|
|
11
|
+
* messages merge, scalars last-win), and the last member of a oneof wins. Messages nest at most
|
|
12
|
+
* MAX_DEPTH deep, and field number 0 is refused.
|
|
13
|
+
*/
|
|
14
|
+
export const WIRE_VARINT = 0;
|
|
15
|
+
export const WIRE_I64 = 1;
|
|
16
|
+
export const WIRE_LEN = 2;
|
|
17
|
+
export const WIRE_FIXED32 = 5;
|
|
18
|
+
/** Reads protobuf wire format from bytes. */
|
|
19
|
+
export class ProtoReader {
|
|
20
|
+
buf;
|
|
21
|
+
view;
|
|
22
|
+
pos = 0;
|
|
23
|
+
constructor(buf) {
|
|
24
|
+
this.buf = buf;
|
|
25
|
+
this.view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
26
|
+
}
|
|
27
|
+
/** Reports whether every byte has been read. */
|
|
28
|
+
done() {
|
|
29
|
+
return this.pos >= this.buf.length;
|
|
30
|
+
}
|
|
31
|
+
tag() {
|
|
32
|
+
const key = this.varint();
|
|
33
|
+
return { field: Number(key >> 3n), wireType: Number(key & 7n) };
|
|
34
|
+
}
|
|
35
|
+
/** A varint as an unsigned 64-bit value. */
|
|
36
|
+
varint() {
|
|
37
|
+
let result = 0n;
|
|
38
|
+
for (let shift = 0n; shift < 70n; shift += 7n) {
|
|
39
|
+
if (this.pos >= this.buf.length)
|
|
40
|
+
throw new Error("protowire: truncated varint");
|
|
41
|
+
const b = this.buf[this.pos++];
|
|
42
|
+
result |= BigInt(b & 0x7f) << shift;
|
|
43
|
+
if ((b & 0x80) === 0)
|
|
44
|
+
return BigInt.asUintN(64, result);
|
|
45
|
+
}
|
|
46
|
+
throw new Error("protowire: varint longer than 10 bytes");
|
|
47
|
+
}
|
|
48
|
+
need(n, what) {
|
|
49
|
+
if (this.pos + n > this.buf.length)
|
|
50
|
+
throw new Error(`protowire: truncated ${what}`);
|
|
51
|
+
}
|
|
52
|
+
/** A little-endian 64-bit value, unsigned. */
|
|
53
|
+
fixed64() {
|
|
54
|
+
this.need(8, "fixed64");
|
|
55
|
+
const v = this.view.getBigUint64(this.pos, true);
|
|
56
|
+
this.pos += 8;
|
|
57
|
+
return v;
|
|
58
|
+
}
|
|
59
|
+
/** A little-endian 64-bit IEEE 754 double. */
|
|
60
|
+
double() {
|
|
61
|
+
this.need(8, "double");
|
|
62
|
+
const v = this.view.getFloat64(this.pos, true);
|
|
63
|
+
this.pos += 8;
|
|
64
|
+
return v;
|
|
65
|
+
}
|
|
66
|
+
/** A little-endian 32-bit value, unsigned. */
|
|
67
|
+
fixed32() {
|
|
68
|
+
this.need(4, "fixed32");
|
|
69
|
+
const v = this.view.getUint32(this.pos, true);
|
|
70
|
+
this.pos += 4;
|
|
71
|
+
return v;
|
|
72
|
+
}
|
|
73
|
+
/** A length-delimited field's bytes, as a view into the input. */
|
|
74
|
+
bytes() {
|
|
75
|
+
const n = Number(this.varint());
|
|
76
|
+
this.need(n, "bytes");
|
|
77
|
+
const out = this.buf.subarray(this.pos, this.pos + n);
|
|
78
|
+
this.pos += n;
|
|
79
|
+
return out;
|
|
80
|
+
}
|
|
81
|
+
/** Skips one value of wireType. */
|
|
82
|
+
skip(wireType) {
|
|
83
|
+
switch (wireType) {
|
|
84
|
+
case WIRE_VARINT:
|
|
85
|
+
this.varint();
|
|
86
|
+
return;
|
|
87
|
+
case WIRE_I64:
|
|
88
|
+
this.need(8, "fixed64");
|
|
89
|
+
this.pos += 8;
|
|
90
|
+
return;
|
|
91
|
+
case WIRE_LEN:
|
|
92
|
+
this.bytes();
|
|
93
|
+
return;
|
|
94
|
+
case WIRE_FIXED32:
|
|
95
|
+
this.need(4, "fixed32");
|
|
96
|
+
this.pos += 4;
|
|
97
|
+
return;
|
|
98
|
+
default:
|
|
99
|
+
throw new Error(`protowire: unsupported wire type ${wireType}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/** The wire type a field of type carries when it is not packed. */
|
|
104
|
+
function wireTypeOf(type) {
|
|
105
|
+
switch (type) {
|
|
106
|
+
case "fixed64":
|
|
107
|
+
case "sfixed64":
|
|
108
|
+
case "double":
|
|
109
|
+
case "time":
|
|
110
|
+
return WIRE_I64;
|
|
111
|
+
case "fixed32":
|
|
112
|
+
return WIRE_FIXED32;
|
|
113
|
+
case "string":
|
|
114
|
+
case "hex":
|
|
115
|
+
case "base64":
|
|
116
|
+
case "message":
|
|
117
|
+
return WIRE_LEN;
|
|
118
|
+
default:
|
|
119
|
+
return WIRE_VARINT;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/** A 64-bit integer as a number when it is safe, else its decimal string. */
|
|
123
|
+
function int64Value(v) {
|
|
124
|
+
return v >= BigInt(Number.MIN_SAFE_INTEGER) && v <= BigInt(Number.MAX_SAFE_INTEGER)
|
|
125
|
+
? Number(v)
|
|
126
|
+
: v.toString();
|
|
127
|
+
}
|
|
128
|
+
function hex(bytes) {
|
|
129
|
+
let out = "";
|
|
130
|
+
for (const b of bytes)
|
|
131
|
+
out += b.toString(16).padStart(2, "0");
|
|
132
|
+
return out;
|
|
133
|
+
}
|
|
134
|
+
const BASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
135
|
+
function base64(bytes) {
|
|
136
|
+
let out = "";
|
|
137
|
+
for (let i = 0; i < bytes.length; i += 3) {
|
|
138
|
+
const a = bytes[i];
|
|
139
|
+
const b = bytes[i + 1];
|
|
140
|
+
const c = bytes[i + 2];
|
|
141
|
+
const n = (a << 16) | ((b ?? 0) << 8) | (c ?? 0);
|
|
142
|
+
out += BASE64[(n >> 18) & 63];
|
|
143
|
+
out += BASE64[(n >> 12) & 63];
|
|
144
|
+
out += b === undefined ? "=" : BASE64[(n >> 6) & 63];
|
|
145
|
+
out += c === undefined ? "=" : BASE64[n & 63];
|
|
146
|
+
}
|
|
147
|
+
return out;
|
|
148
|
+
}
|
|
149
|
+
const UTF8 = new TextDecoder("utf-8");
|
|
150
|
+
// scalar reads one non-message value of type, whose wire type has been checked.
|
|
151
|
+
function scalar(r, type) {
|
|
152
|
+
switch (type) {
|
|
153
|
+
case "string":
|
|
154
|
+
return UTF8.decode(r.bytes());
|
|
155
|
+
case "hex":
|
|
156
|
+
return hex(r.bytes());
|
|
157
|
+
case "base64":
|
|
158
|
+
return base64(r.bytes());
|
|
159
|
+
case "bool":
|
|
160
|
+
return r.varint() !== 0n;
|
|
161
|
+
case "uint32":
|
|
162
|
+
return Number(BigInt.asUintN(32, r.varint()));
|
|
163
|
+
case "int32":
|
|
164
|
+
return Number(BigInt.asIntN(32, r.varint()));
|
|
165
|
+
case "sint32": {
|
|
166
|
+
const n = Number(BigInt.asUintN(32, r.varint()));
|
|
167
|
+
return (n >>> 1) ^ -(n & 1);
|
|
168
|
+
}
|
|
169
|
+
case "uint64":
|
|
170
|
+
return int64Value(r.varint());
|
|
171
|
+
case "int64":
|
|
172
|
+
return int64Value(BigInt.asIntN(64, r.varint()));
|
|
173
|
+
case "fixed32":
|
|
174
|
+
return r.fixed32();
|
|
175
|
+
case "fixed64":
|
|
176
|
+
return int64Value(r.fixed64());
|
|
177
|
+
case "sfixed64":
|
|
178
|
+
return int64Value(BigInt.asIntN(64, r.fixed64()));
|
|
179
|
+
case "double":
|
|
180
|
+
return r.double();
|
|
181
|
+
case "time":
|
|
182
|
+
return r.fixed64().toString();
|
|
183
|
+
default:
|
|
184
|
+
throw new Error(`protowire: ${type} is not a scalar`);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
/** How deep messages may nest, counting the outermost: deeper input is refused. */
|
|
188
|
+
export const MAX_DEPTH = 100;
|
|
189
|
+
function schemaOf(schemas, type) {
|
|
190
|
+
const schema = schemas[type];
|
|
191
|
+
if (schema === undefined)
|
|
192
|
+
throw new Error(`protowire: no schema for ${type}`);
|
|
193
|
+
return schema;
|
|
194
|
+
}
|
|
195
|
+
// clearOneof removes the other members of spec's oneof from out, so the last member wins.
|
|
196
|
+
function clearOneof(schema, out, spec) {
|
|
197
|
+
if (spec.oneof === undefined)
|
|
198
|
+
return;
|
|
199
|
+
for (const other of Object.values(schema)) {
|
|
200
|
+
if (other.oneof === spec.oneof && other.name !== spec.name)
|
|
201
|
+
delete out[other.name];
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
function isRecord(v) {
|
|
205
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
206
|
+
}
|
|
207
|
+
// merge folds a later occurrence of a message of type into target, as protobuf merges: repeated
|
|
208
|
+
// fields append, embedded messages merge recursively, scalars and oneof members last-win.
|
|
209
|
+
function merge(schemas, type, target, later) {
|
|
210
|
+
const schema = schemaOf(schemas, type);
|
|
211
|
+
const byName = new Map(Object.values(schema).map((f) => [f.name, f]));
|
|
212
|
+
for (const [name, value] of Object.entries(later)) {
|
|
213
|
+
const spec = byName.get(name);
|
|
214
|
+
if (spec === undefined)
|
|
215
|
+
continue;
|
|
216
|
+
const current = target[name];
|
|
217
|
+
if (spec.repeated === true && Array.isArray(current) && Array.isArray(value)) {
|
|
218
|
+
current.push(...value);
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
if (spec.type === "message" && spec.message !== undefined && isRecord(current) && isRecord(value)) {
|
|
222
|
+
merge(schemas, spec.message, current, value);
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
clearOneof(schema, target, spec);
|
|
226
|
+
target[name] = value;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
// listOf returns the list out[name] holds, creating it first.
|
|
230
|
+
function listOf(out, name) {
|
|
231
|
+
const list = out[name];
|
|
232
|
+
if (Array.isArray(list))
|
|
233
|
+
return list;
|
|
234
|
+
const created = [];
|
|
235
|
+
out[name] = created;
|
|
236
|
+
return created;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Decodes bytes as the message schemas[type] describes. Throws a protowire: error for malformed
|
|
240
|
+
* input, field number 0, a wire type that does not match the schema, or messages nested deeper
|
|
241
|
+
* than MAX_DEPTH.
|
|
242
|
+
*/
|
|
243
|
+
export function decodeMessage(schemas, type, bytes) {
|
|
244
|
+
return decodeAt(schemas, type, bytes, 1);
|
|
245
|
+
}
|
|
246
|
+
function decodeAt(schemas, type, bytes, depth) {
|
|
247
|
+
if (depth > MAX_DEPTH)
|
|
248
|
+
throw new Error(`protowire: messages nested deeper than ${MAX_DEPTH}`);
|
|
249
|
+
const schema = schemaOf(schemas, type);
|
|
250
|
+
const out = {};
|
|
251
|
+
const r = new ProtoReader(bytes);
|
|
252
|
+
while (!r.done()) {
|
|
253
|
+
const { field, wireType } = r.tag();
|
|
254
|
+
if (field === 0)
|
|
255
|
+
throw new Error("protowire: invalid field number 0");
|
|
256
|
+
const spec = schema[field];
|
|
257
|
+
if (spec === undefined) {
|
|
258
|
+
r.skip(wireType);
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
const expected = wireTypeOf(spec.type);
|
|
262
|
+
if (spec.repeated === true && wireType === WIRE_LEN && expected !== WIRE_LEN) {
|
|
263
|
+
// A packed run of scalars.
|
|
264
|
+
const run = new ProtoReader(r.bytes());
|
|
265
|
+
const list = listOf(out, spec.name);
|
|
266
|
+
while (!run.done())
|
|
267
|
+
list.push(scalar(run, spec.type));
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
if (wireType !== expected) {
|
|
271
|
+
throw new Error(`protowire: ${type}.${spec.name}: wire type ${wireType}, want ${expected}`);
|
|
272
|
+
}
|
|
273
|
+
if (spec.type !== "message") {
|
|
274
|
+
const value = scalar(r, spec.type);
|
|
275
|
+
if (spec.repeated === true) {
|
|
276
|
+
listOf(out, spec.name).push(value);
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
clearOneof(schema, out, spec);
|
|
280
|
+
out[spec.name] = value;
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
if (spec.message === undefined)
|
|
284
|
+
throw new Error(`protowire: ${type}.${spec.name}: no message type`);
|
|
285
|
+
const value = decodeAt(schemas, spec.message, r.bytes(), depth + 1);
|
|
286
|
+
if (spec.repeated === true) {
|
|
287
|
+
listOf(out, spec.name).push(value);
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
const current = out[spec.name];
|
|
291
|
+
if (isRecord(current)) {
|
|
292
|
+
merge(schemas, spec.message, current, value);
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
clearOneof(schema, out, spec);
|
|
296
|
+
out[spec.name] = value;
|
|
297
|
+
}
|
|
298
|
+
return out;
|
|
299
|
+
}
|