@routr/common 2.16.0 → 2.19.0
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/README.md +1 -1
- package/dist/connect/config.d.ts +5 -1
- package/dist/connect/mappers/agent.js +1 -0
- package/dist/connect/mappers/domain.js +1 -0
- package/dist/connect/mappers/peer.js +1 -0
- package/dist/connect/mappers/trunk.js +1 -0
- package/dist/connect/protos/agents.proto +7 -0
- package/dist/connect/protos/domains.proto +7 -0
- package/dist/connect/protos/peers.proto +7 -0
- package/dist/connect/protos/trace.proto +28 -0
- package/dist/connect/protos/trunks.proto +7 -0
- package/dist/connect/schemas/agent.d.ts +10 -0
- package/dist/connect/schemas/agent.js +8 -0
- package/dist/connect/schemas/domain.d.ts +10 -0
- package/dist/connect/schemas/domain.js +8 -0
- package/dist/connect/schemas/peer.d.ts +10 -0
- package/dist/connect/schemas/peer.js +8 -0
- package/dist/connect/schemas/trunk.d.ts +10 -0
- package/dist/connect/schemas/trunk.js +8 -0
- package/dist/connect/types.d.ts +7 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -1
- package/dist/protos/processor.proto +4 -0
- package/dist/trace/compat.d.ts +37 -0
- package/dist/trace/compat.js +131 -0
- package/dist/trace/diff.d.ts +48 -0
- package/dist/trace/diff.js +152 -0
- package/dist/trace/index.d.ts +11 -0
- package/dist/trace/index.js +68 -0
- package/dist/trace/metadata.d.ts +28 -0
- package/dist/trace/metadata.js +75 -0
- package/dist/trace/normalize.d.ts +11 -0
- package/dist/trace/normalize.js +39 -0
- package/dist/trace/redact.d.ts +19 -0
- package/dist/trace/redact.js +52 -0
- package/dist/trace/rules/facts.d.ts +43 -0
- package/dist/trace/rules/facts.js +117 -0
- package/dist/trace/rules/index.d.ts +34 -0
- package/dist/trace/rules/index.js +153 -0
- package/dist/trace/rules/sip/ack-2xx.d.ts +2 -0
- package/dist/trace/rules/sip/ack-2xx.js +56 -0
- package/dist/trace/rules/sip/cancel-matches.d.ts +2 -0
- package/dist/trace/rules/sip/cancel-matches.js +46 -0
- package/dist/trace/rules/sip/credentials-stripped.d.ts +2 -0
- package/dist/trace/rules/sip/credentials-stripped.js +41 -0
- package/dist/trace/rules/sip/cseq-ack.d.ts +2 -0
- package/dist/trace/rules/sip/cseq-ack.js +40 -0
- package/dist/trace/rules/sip/cseq-monotonic.d.ts +2 -0
- package/dist/trace/rules/sip/cseq-monotonic.js +50 -0
- package/dist/trace/rules/sip/max-forwards.d.ts +2 -0
- package/dist/trace/rules/sip/max-forwards.js +47 -0
- package/dist/trace/rules/sip/private-address.d.ts +2 -0
- package/dist/trace/rules/sip/private-address.js +48 -0
- package/dist/trace/rules/sip/record-route-self.d.ts +2 -0
- package/dist/trace/rules/sip/record-route-self.js +46 -0
- package/dist/trace/rules/sip/register-ok.d.ts +2 -0
- package/dist/trace/rules/sip/register-ok.js +52 -0
- package/dist/trace/rules/sip/retransmission.d.ts +2 -0
- package/dist/trace/rules/sip/retransmission.js +47 -0
- package/dist/trace/rules/sip/route-self-removed.d.ts +2 -0
- package/dist/trace/rules/sip/route-self-removed.js +38 -0
- package/dist/trace/rules/sip/via.d.ts +2 -0
- package/dist/trace/rules/sip/via.js +50 -0
- package/dist/trace/rules/sip-connect/from-e164-or-anonymous.d.ts +2 -0
- package/dist/trace/rules/sip-connect/from-e164-or-anonymous.js +43 -0
- package/dist/trace/rules/sip-connect/pai-e164.d.ts +2 -0
- package/dist/trace/rules/sip-connect/pai-e164.js +44 -0
- package/dist/trace/rules/sip-connect/pai-single.d.ts +2 -0
- package/dist/trace/rules/sip-connect/pai-single.js +41 -0
- package/dist/trace/rules/sip-connect/privacy.d.ts +2 -0
- package/dist/trace/rules/sip-connect/privacy.js +50 -0
- package/dist/trace/rules/sip-connect/request-uri-e164.d.ts +2 -0
- package/dist/trace/rules/sip-connect/request-uri-e164.js +44 -0
- package/dist/trace/rules/sip-connect/scope.d.ts +11 -0
- package/dist/trace/rules/sip-connect/scope.js +19 -0
- package/dist/trace/rules/types.d.ts +34 -0
- package/dist/trace/rules/types.js +13 -0
- package/dist/trace/sanitize.d.ts +40 -0
- package/dist/trace/sanitize.js +85 -0
- package/dist/trace/schema.d.ts +338 -0
- package/dist/trace/schema.js +197 -0
- package/dist/trace/status.d.ts +15 -0
- package/dist/trace/status.js +102 -0
- package/dist/trace/types.d.ts +126 -0
- package/dist/trace/types.js +82 -0
- package/dist/trace/validate.d.ts +56 -0
- package/dist/trace/validate.js +140 -0
- package/dist/types.d.ts +2 -0
- package/package.json +2 -2
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { MessageRequest, SIPMessage } from "../types";
|
|
2
|
+
/**
|
|
3
|
+
* Version of the trace record format written by this build. Readers reject
|
|
4
|
+
* records whose traceVersion is not in SUPPORTED_TRACE_VERSIONS.
|
|
5
|
+
*/
|
|
6
|
+
export declare const TRACE_VERSION = 1;
|
|
7
|
+
export declare const SUPPORTED_TRACE_VERSIONS: ReadonlyArray<number>;
|
|
8
|
+
/**
|
|
9
|
+
* Prefix of the metadata keys reserved for the trace. A middleware or a
|
|
10
|
+
* processor may put any `trace.*` key in the metadata of its response: the
|
|
11
|
+
* dispatcher copies those keys into the hop's `metadata` and removes them
|
|
12
|
+
* before the message reaches the EdgePort. Routr draws no conclusion from
|
|
13
|
+
* them (components are swappable); they are there for people and tools
|
|
14
|
+
* reading the file.
|
|
15
|
+
*/
|
|
16
|
+
export declare const TRACE_METADATA_PREFIX = "trace.";
|
|
17
|
+
/**
|
|
18
|
+
* The one reserved key with a meaning: a processor sets it on a routed
|
|
19
|
+
* message to ask the dispatcher to record the call. The value is the ref
|
|
20
|
+
* of the resource (trunk, agent, peer, domain) that asked for it. It is
|
|
21
|
+
* never copied into a hop.
|
|
22
|
+
*/
|
|
23
|
+
export declare const TRACE_FLAG_METADATA_KEY = "trace.record";
|
|
24
|
+
/**
|
|
25
|
+
* Extended attribute of a connect resource (Trunk, Agent, Peer, Domain)
|
|
26
|
+
* that turns recording on for the calls that touch it.
|
|
27
|
+
*/
|
|
28
|
+
export declare const RECORD_MESSAGES_ATTRIBUTE = "recordMessages";
|
|
29
|
+
/** Rule profiles a lint run can evaluate. */
|
|
30
|
+
export declare enum Profile {
|
|
31
|
+
SIP = "sip",
|
|
32
|
+
SIP_CONNECT = "sip-connect"
|
|
33
|
+
}
|
|
34
|
+
export declare enum Severity {
|
|
35
|
+
ERROR = "error",
|
|
36
|
+
WARNING = "warning"
|
|
37
|
+
}
|
|
38
|
+
/** One rule firing on one record. */
|
|
39
|
+
export interface Finding {
|
|
40
|
+
rule: string;
|
|
41
|
+
profile: Profile;
|
|
42
|
+
severity: Severity;
|
|
43
|
+
spec: string;
|
|
44
|
+
message: string;
|
|
45
|
+
side: "in" | "out" | "call";
|
|
46
|
+
header?: string;
|
|
47
|
+
}
|
|
48
|
+
/** Which rule set produced the findings written on a record. */
|
|
49
|
+
export interface TraceLint {
|
|
50
|
+
version: string;
|
|
51
|
+
profiles: Profile[];
|
|
52
|
+
}
|
|
53
|
+
export declare enum TraceKind {
|
|
54
|
+
REQUEST = "request",
|
|
55
|
+
RESPONSE = "response"
|
|
56
|
+
}
|
|
57
|
+
export declare enum TraceOutcome {
|
|
58
|
+
FORWARDED = "forwarded",
|
|
59
|
+
RELAYED = "relayed",
|
|
60
|
+
ANSWERED = "answered",
|
|
61
|
+
REJECTED = "rejected",
|
|
62
|
+
ERROR = "error"
|
|
63
|
+
}
|
|
64
|
+
export declare enum HopKind {
|
|
65
|
+
DISPATCHER = "dispatcher",
|
|
66
|
+
MIDDLEWARE = "middleware",
|
|
67
|
+
PROCESSOR = "processor"
|
|
68
|
+
}
|
|
69
|
+
export declare enum HopAction {
|
|
70
|
+
MATCHED = "matched",
|
|
71
|
+
PASS_THROUGH = "pass-through",
|
|
72
|
+
FORWARDED = "forwarded",
|
|
73
|
+
RESPONDED = "responded",
|
|
74
|
+
RELAYED = "relayed",
|
|
75
|
+
ERROR = "error"
|
|
76
|
+
}
|
|
77
|
+
export interface HeaderChange {
|
|
78
|
+
from: string;
|
|
79
|
+
to: string;
|
|
80
|
+
}
|
|
81
|
+
export interface MessageDiff {
|
|
82
|
+
requestUri?: HeaderChange;
|
|
83
|
+
headers: {
|
|
84
|
+
added: Record<string, string>;
|
|
85
|
+
removed: Record<string, string>;
|
|
86
|
+
changed: Record<string, HeaderChange>;
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
export interface TraceHop {
|
|
90
|
+
component: string;
|
|
91
|
+
kind: HopKind;
|
|
92
|
+
version?: string;
|
|
93
|
+
durationMs: number;
|
|
94
|
+
action: HopAction;
|
|
95
|
+
diff?: MessageDiff;
|
|
96
|
+
error?: string;
|
|
97
|
+
metadata?: Record<string, string>;
|
|
98
|
+
}
|
|
99
|
+
export interface TraceComponent {
|
|
100
|
+
ref: string;
|
|
101
|
+
version?: string;
|
|
102
|
+
}
|
|
103
|
+
export interface TraceResponse {
|
|
104
|
+
message: SIPMessage;
|
|
105
|
+
metadata?: Record<string, string>;
|
|
106
|
+
}
|
|
107
|
+
export interface TraceRecord {
|
|
108
|
+
traceVersion: number;
|
|
109
|
+
recordedAt: string;
|
|
110
|
+
callId: string;
|
|
111
|
+
seq: number;
|
|
112
|
+
tOffsetMs: number;
|
|
113
|
+
kind: TraceKind;
|
|
114
|
+
method: string;
|
|
115
|
+
status?: number;
|
|
116
|
+
edgePort: TraceComponent;
|
|
117
|
+
dispatcher: TraceComponent;
|
|
118
|
+
trunkRef?: string;
|
|
119
|
+
request: MessageRequest;
|
|
120
|
+
hops: TraceHop[];
|
|
121
|
+
response: TraceResponse | null;
|
|
122
|
+
outcome: TraceOutcome;
|
|
123
|
+
totalMs: number;
|
|
124
|
+
lint?: TraceLint;
|
|
125
|
+
findings?: Finding[];
|
|
126
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HopAction = exports.HopKind = exports.TraceOutcome = exports.TraceKind = exports.Severity = exports.Profile = exports.RECORD_MESSAGES_ATTRIBUTE = exports.TRACE_FLAG_METADATA_KEY = exports.TRACE_METADATA_PREFIX = exports.SUPPORTED_TRACE_VERSIONS = exports.TRACE_VERSION = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Version of the trace record format written by this build. Readers reject
|
|
6
|
+
* records whose traceVersion is not in SUPPORTED_TRACE_VERSIONS.
|
|
7
|
+
*/
|
|
8
|
+
exports.TRACE_VERSION = 1;
|
|
9
|
+
exports.SUPPORTED_TRACE_VERSIONS = [1];
|
|
10
|
+
/**
|
|
11
|
+
* Prefix of the metadata keys reserved for the trace. A middleware or a
|
|
12
|
+
* processor may put any `trace.*` key in the metadata of its response: the
|
|
13
|
+
* dispatcher copies those keys into the hop's `metadata` and removes them
|
|
14
|
+
* before the message reaches the EdgePort. Routr draws no conclusion from
|
|
15
|
+
* them (components are swappable); they are there for people and tools
|
|
16
|
+
* reading the file.
|
|
17
|
+
*/
|
|
18
|
+
exports.TRACE_METADATA_PREFIX = "trace.";
|
|
19
|
+
/**
|
|
20
|
+
* The one reserved key with a meaning: a processor sets it on a routed
|
|
21
|
+
* message to ask the dispatcher to record the call. The value is the ref
|
|
22
|
+
* of the resource (trunk, agent, peer, domain) that asked for it. It is
|
|
23
|
+
* never copied into a hop.
|
|
24
|
+
*/
|
|
25
|
+
exports.TRACE_FLAG_METADATA_KEY = "trace.record";
|
|
26
|
+
/**
|
|
27
|
+
* Extended attribute of a connect resource (Trunk, Agent, Peer, Domain)
|
|
28
|
+
* that turns recording on for the calls that touch it.
|
|
29
|
+
*/
|
|
30
|
+
exports.RECORD_MESSAGES_ATTRIBUTE = "recordMessages";
|
|
31
|
+
/** Rule profiles a lint run can evaluate. */
|
|
32
|
+
var Profile;
|
|
33
|
+
(function (Profile) {
|
|
34
|
+
// RFC 3261 and companions; always on
|
|
35
|
+
Profile["SIP"] = "sip";
|
|
36
|
+
// SIPconnect 1.1 Technical Recommendation; opt-in
|
|
37
|
+
Profile["SIP_CONNECT"] = "sip-connect";
|
|
38
|
+
})(Profile || (exports.Profile = Profile = {}));
|
|
39
|
+
var Severity;
|
|
40
|
+
(function (Severity) {
|
|
41
|
+
Severity["ERROR"] = "error";
|
|
42
|
+
Severity["WARNING"] = "warning";
|
|
43
|
+
})(Severity || (exports.Severity = Severity = {}));
|
|
44
|
+
var TraceKind;
|
|
45
|
+
(function (TraceKind) {
|
|
46
|
+
TraceKind["REQUEST"] = "request";
|
|
47
|
+
TraceKind["RESPONSE"] = "response";
|
|
48
|
+
})(TraceKind || (exports.TraceKind = TraceKind = {}));
|
|
49
|
+
var TraceOutcome;
|
|
50
|
+
(function (TraceOutcome) {
|
|
51
|
+
// A request left the dispatcher as a request (towards the next hop)
|
|
52
|
+
TraceOutcome["FORWARDED"] = "forwarded";
|
|
53
|
+
// A response left the dispatcher as a response (towards the previous hop)
|
|
54
|
+
TraceOutcome["RELAYED"] = "relayed";
|
|
55
|
+
// A request was answered by a component with a final 2xx (e.g. REGISTER 200)
|
|
56
|
+
TraceOutcome["ANSWERED"] = "answered";
|
|
57
|
+
// A request was answered by a component with a 3xx-6xx response
|
|
58
|
+
TraceOutcome["REJECTED"] = "rejected";
|
|
59
|
+
// A component failed and the dispatcher returned an error to the EdgePort
|
|
60
|
+
TraceOutcome["ERROR"] = "error";
|
|
61
|
+
})(TraceOutcome || (exports.TraceOutcome = TraceOutcome = {}));
|
|
62
|
+
var HopKind;
|
|
63
|
+
(function (HopKind) {
|
|
64
|
+
HopKind["DISPATCHER"] = "dispatcher";
|
|
65
|
+
HopKind["MIDDLEWARE"] = "middleware";
|
|
66
|
+
HopKind["PROCESSOR"] = "processor";
|
|
67
|
+
})(HopKind || (exports.HopKind = HopKind = {}));
|
|
68
|
+
var HopAction;
|
|
69
|
+
(function (HopAction) {
|
|
70
|
+
// The dispatcher selected a processor for the message
|
|
71
|
+
HopAction["MATCHED"] = "matched";
|
|
72
|
+
// A middleware returned the message unchanged
|
|
73
|
+
HopAction["PASS_THROUGH"] = "pass-through";
|
|
74
|
+
// A component returned the message, possibly altered, as a request
|
|
75
|
+
HopAction["FORWARDED"] = "forwarded";
|
|
76
|
+
// A component answered the request with a response
|
|
77
|
+
HopAction["RESPONDED"] = "responded";
|
|
78
|
+
// A component relayed a response
|
|
79
|
+
HopAction["RELAYED"] = "relayed";
|
|
80
|
+
// A component failed or was unavailable
|
|
81
|
+
HopAction["ERROR"] = "error";
|
|
82
|
+
})(HopAction || (exports.HopAction = HopAction = {}));
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { TraceRecord } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Thrown when a trace line is not valid JSON or does not match the schema.
|
|
4
|
+
*/
|
|
5
|
+
export declare class TraceValidationError extends Error {
|
|
6
|
+
readonly line?: number;
|
|
7
|
+
/**
|
|
8
|
+
* @param {string} message - Human readable description
|
|
9
|
+
* @param {number} line - 1-based line number in the trace file, if known
|
|
10
|
+
*/
|
|
11
|
+
constructor(message: string, line?: number);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Thrown when a record declares a traceVersion this build does not read.
|
|
15
|
+
*/
|
|
16
|
+
export declare class UnsupportedTraceVersionError extends TraceValidationError {
|
|
17
|
+
readonly version: unknown;
|
|
18
|
+
/**
|
|
19
|
+
* @param {unknown} version - The version found in the record
|
|
20
|
+
* @param {number} line - 1-based line number in the trace file, if known
|
|
21
|
+
*/
|
|
22
|
+
constructor(version: unknown, line?: number);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Checks the version before anything else, so a newer file fails with a
|
|
26
|
+
* version message rather than with a list of unknown fields.
|
|
27
|
+
*
|
|
28
|
+
* @param {unknown} input - A parsed JSON value
|
|
29
|
+
* @param {number} line - 1-based line number, if known
|
|
30
|
+
*/
|
|
31
|
+
export declare function assertSupportedVersion(input: unknown, line?: number): void;
|
|
32
|
+
/**
|
|
33
|
+
* Validates one parsed record against the schema and returns it typed.
|
|
34
|
+
*
|
|
35
|
+
* @param {unknown} input - A parsed JSON value
|
|
36
|
+
* @param {number} line - 1-based line number, if known
|
|
37
|
+
* @return {TraceRecord} The same object, now known to be a TraceRecord
|
|
38
|
+
*/
|
|
39
|
+
export declare function validateRecord(input: unknown, line?: number): TraceRecord;
|
|
40
|
+
/**
|
|
41
|
+
* Parses the text of a trace file (one JSON record per line).
|
|
42
|
+
*
|
|
43
|
+
* Every line is validated. Blank lines are skipped. A truncated last line
|
|
44
|
+
* is reported as an error, never silently dropped.
|
|
45
|
+
*
|
|
46
|
+
* @param {string} text - File contents
|
|
47
|
+
* @return {TraceRecord[]} The records in file order
|
|
48
|
+
*/
|
|
49
|
+
export declare function parseTrace(text: string): TraceRecord[];
|
|
50
|
+
/**
|
|
51
|
+
* Reads and validates a trace file.
|
|
52
|
+
*
|
|
53
|
+
* @param {string} path - Path to a .jsonl trace file
|
|
54
|
+
* @return {TraceRecord[]} The records in file order
|
|
55
|
+
*/
|
|
56
|
+
export declare function readTrace(path: string): TraceRecord[];
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.UnsupportedTraceVersionError = exports.TraceValidationError = void 0;
|
|
7
|
+
exports.assertSupportedVersion = assertSupportedVersion;
|
|
8
|
+
exports.validateRecord = validateRecord;
|
|
9
|
+
exports.parseTrace = parseTrace;
|
|
10
|
+
exports.readTrace = readTrace;
|
|
11
|
+
/*
|
|
12
|
+
* Copyright (C) 2026 by Fonoster Inc (https://fonoster.com)
|
|
13
|
+
* http://github.com/fonoster/routr
|
|
14
|
+
*
|
|
15
|
+
* This file is part of Routr.
|
|
16
|
+
*
|
|
17
|
+
* Licensed under the MIT License (the "License");
|
|
18
|
+
* you may not use this file except in compliance with
|
|
19
|
+
* the License. You may obtain a copy of the License at
|
|
20
|
+
*
|
|
21
|
+
* https://opensource.org/licenses/MIT
|
|
22
|
+
*
|
|
23
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
24
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
25
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
26
|
+
* See the License for the specific language governing permissions and
|
|
27
|
+
* limitations under the License.
|
|
28
|
+
*/
|
|
29
|
+
const ajv_1 = __importDefault(require("ajv"));
|
|
30
|
+
const fs_1 = require("fs");
|
|
31
|
+
const schema_1 = require("./schema");
|
|
32
|
+
const types_1 = require("./types");
|
|
33
|
+
const ajv = new ajv_1.default({ allErrors: true });
|
|
34
|
+
const validateSchema = ajv.compile(schema_1.traceRecordSchema);
|
|
35
|
+
/**
|
|
36
|
+
* Thrown when a trace line is not valid JSON or does not match the schema.
|
|
37
|
+
*/
|
|
38
|
+
class TraceValidationError extends Error {
|
|
39
|
+
/**
|
|
40
|
+
* @param {string} message - Human readable description
|
|
41
|
+
* @param {number} line - 1-based line number in the trace file, if known
|
|
42
|
+
*/
|
|
43
|
+
constructor(message, line) {
|
|
44
|
+
super(line ? `line ${line}: ${message}` : message);
|
|
45
|
+
this.name = "TraceValidationError";
|
|
46
|
+
this.line = line;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.TraceValidationError = TraceValidationError;
|
|
50
|
+
/**
|
|
51
|
+
* Thrown when a record declares a traceVersion this build does not read.
|
|
52
|
+
*/
|
|
53
|
+
class UnsupportedTraceVersionError extends TraceValidationError {
|
|
54
|
+
/**
|
|
55
|
+
* @param {unknown} version - The version found in the record
|
|
56
|
+
* @param {number} line - 1-based line number in the trace file, if known
|
|
57
|
+
*/
|
|
58
|
+
constructor(version, line) {
|
|
59
|
+
super(`trace version ${JSON.stringify(version)} is not supported ` +
|
|
60
|
+
`(this build reads ${types_1.SUPPORTED_TRACE_VERSIONS.join(", ")}); ` +
|
|
61
|
+
"upgrade rctl to read this file", line);
|
|
62
|
+
this.name = "UnsupportedTraceVersionError";
|
|
63
|
+
this.version = version;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
exports.UnsupportedTraceVersionError = UnsupportedTraceVersionError;
|
|
67
|
+
/**
|
|
68
|
+
* Checks the version before anything else, so a newer file fails with a
|
|
69
|
+
* version message rather than with a list of unknown fields.
|
|
70
|
+
*
|
|
71
|
+
* @param {unknown} input - A parsed JSON value
|
|
72
|
+
* @param {number} line - 1-based line number, if known
|
|
73
|
+
*/
|
|
74
|
+
function assertSupportedVersion(input, line) {
|
|
75
|
+
const version = input === null || input === void 0 ? void 0 : input.traceVersion;
|
|
76
|
+
if (!types_1.SUPPORTED_TRACE_VERSIONS.includes(version)) {
|
|
77
|
+
throw new UnsupportedTraceVersionError(version, line);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Validates one parsed record against the schema and returns it typed.
|
|
82
|
+
*
|
|
83
|
+
* @param {unknown} input - A parsed JSON value
|
|
84
|
+
* @param {number} line - 1-based line number, if known
|
|
85
|
+
* @return {TraceRecord} The same object, now known to be a TraceRecord
|
|
86
|
+
*/
|
|
87
|
+
function validateRecord(input, line) {
|
|
88
|
+
var _a;
|
|
89
|
+
if (input === null || typeof input !== "object" || Array.isArray(input)) {
|
|
90
|
+
throw new TraceValidationError("record is not a JSON object", line);
|
|
91
|
+
}
|
|
92
|
+
assertSupportedVersion(input, line);
|
|
93
|
+
if (!validateSchema(input)) {
|
|
94
|
+
const first = (_a = validateSchema.errors) === null || _a === void 0 ? void 0 : _a[0];
|
|
95
|
+
// ajv 6 reports dataPath (".outcome"), ajv 8 instancePath ("/outcome")
|
|
96
|
+
const where = ((first === null || first === void 0 ? void 0 : first.instancePath) || (first === null || first === void 0 ? void 0 : first.dataPath) || "/")
|
|
97
|
+
.replace(/^\./, "/")
|
|
98
|
+
.replace(/\./g, "/");
|
|
99
|
+
throw new TraceValidationError(`${where} ${first === null || first === void 0 ? void 0 : first.message}`, line);
|
|
100
|
+
}
|
|
101
|
+
return input;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Parses the text of a trace file (one JSON record per line).
|
|
105
|
+
*
|
|
106
|
+
* Every line is validated. Blank lines are skipped. A truncated last line
|
|
107
|
+
* is reported as an error, never silently dropped.
|
|
108
|
+
*
|
|
109
|
+
* @param {string} text - File contents
|
|
110
|
+
* @return {TraceRecord[]} The records in file order
|
|
111
|
+
*/
|
|
112
|
+
function parseTrace(text) {
|
|
113
|
+
return text
|
|
114
|
+
.split("\n")
|
|
115
|
+
.map((raw, index) => ({ raw: raw.trim(), line: index + 1 }))
|
|
116
|
+
.filter(({ raw }) => raw.length > 0)
|
|
117
|
+
.map(({ raw, line }) => {
|
|
118
|
+
let parsed;
|
|
119
|
+
try {
|
|
120
|
+
parsed = JSON.parse(raw);
|
|
121
|
+
}
|
|
122
|
+
catch (e) {
|
|
123
|
+
throw new TraceValidationError(`invalid JSON (${e.message})`, line);
|
|
124
|
+
}
|
|
125
|
+
return validateRecord(parsed, line);
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Reads and validates a trace file.
|
|
130
|
+
*
|
|
131
|
+
* @param {string} path - Path to a .jsonl trace file
|
|
132
|
+
* @return {TraceRecord[]} The records in file order
|
|
133
|
+
*/
|
|
134
|
+
function readTrace(path) {
|
|
135
|
+
const records = parseTrace((0, fs_1.readFileSync)(path, "utf8"));
|
|
136
|
+
if (records.length === 0) {
|
|
137
|
+
throw new TraceValidationError("file has no records");
|
|
138
|
+
}
|
|
139
|
+
return records;
|
|
140
|
+
}
|
package/dist/types.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@routr/common",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.19.0",
|
|
4
4
|
"description": "Common package",
|
|
5
5
|
"author": "Pedro Sanders <psanders@fonoster.com>",
|
|
6
6
|
"homepage": "https://github.com/fonoster/routr#readme",
|
|
@@ -53,5 +53,5 @@
|
|
|
53
53
|
"devDependencies": {
|
|
54
54
|
"@types/js-yaml": "^4.0.5"
|
|
55
55
|
},
|
|
56
|
-
"gitHead": "
|
|
56
|
+
"gitHead": "357116800e150d8bbfbd0ad2406689c5793675b1"
|
|
57
57
|
}
|