UNPKG

2.42 kBJavaScriptView Raw
1"use strict";
2/**
3 * Copyright 2019, OpenCensus Authors
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.isValidOption = exports.isValidSpanId = exports.isValidTraceId = void 0;
19/**
20 * Determines if the given hex string is truely a hex value. False if value is
21 * null.
22 * @param value
23 */
24const isHex = (value) => {
25 return typeof value === 'string' && /^[0-9A-F]*$/i.test(value);
26};
27/**
28 * Determines if the given hex string is all zeros. False if value is null.
29 * @param value
30 */
31const isNotAllZeros = (value) => {
32 return typeof value === 'string' && !/^[0]*$/i.test(value);
33};
34/**
35 * Determines if the given hex string is of the given length. False if value is
36 * null.
37 * @param value
38 */
39const isLength = (length) => {
40 return (value) => {
41 return typeof value === 'string' && value.length === length;
42 };
43};
44/**
45 * Compose a set of validation functions into a single validation call.
46 */
47const compose = (...fns) => {
48 // @ts-expect-error ts-migrate(2322) FIXME: Type '(value: string) => ValidationFn' is not assi... Remove this comment to see the full error message
49 return (value) => {
50 // @ts-expect-error ts-migrate(2769) FIXME: No overload matches this call.
51 return fns.reduce((isValid, fn) => isValid && fn(value), true);
52 };
53};
54/**
55 * Determines if the given traceId is valid based on section 2.2.2.1 of the
56 * Trace Context spec.
57 */
58exports.isValidTraceId = compose(isHex, isNotAllZeros, isLength(32));
59/**
60 * Determines if the given spanId is valid based on section 2.2.2.2 of the Trace
61 * Context spec.
62 */
63exports.isValidSpanId = compose(isHex, isNotAllZeros, isLength(16));
64/**
65 * Determines if the given option is valid based on section 2.2.3 of the Trace
66 * Context spec.
67 */
68exports.isValidOption = compose(isHex, isLength(2));
69//# sourceMappingURL=validators.js.map
\No newline at end of file