@routr/common 2.32.1 → 2.32.3
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/dist/auth.d.ts +13 -1
- package/dist/auth.js +30 -2
- package/dist/response.d.ts +16 -0
- package/dist/response.js +11 -2
- package/dist/sampler_from_env.d.ts +15 -0
- package/dist/sampler_from_env.js +70 -0
- package/dist/tracer.js +3 -0
- package/package.json +2 -2
package/dist/auth.d.ts
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
import { AuthChallengeResponse } from "./types";
|
|
2
2
|
export declare const DEFAULT_ALGORITHM = "MD5";
|
|
3
|
-
export declare const
|
|
3
|
+
export declare const NONCE_MAX_AGE_SECONDS = 300;
|
|
4
|
+
export declare const generateNonce: (algorithm?: string, now?: () => number) => string;
|
|
5
|
+
/**
|
|
6
|
+
* Whether a nonce was issued by generateNonce within the last maxAgeSeconds.
|
|
7
|
+
* A nonce in another format is never fresh, so a header captured before
|
|
8
|
+
* nonces carried their time can't be replayed either.
|
|
9
|
+
*
|
|
10
|
+
* @param {string} nonce - the nonce from the Authorization header
|
|
11
|
+
* @param {number} maxAgeSeconds - how long a nonce stays valid; 0 means forever
|
|
12
|
+
* @param {Function} now - the clock, injected by tests
|
|
13
|
+
* @return {boolean} true when the nonce is fresh
|
|
14
|
+
*/
|
|
15
|
+
export declare const isNonceFresh: (nonce: string | undefined, maxAgeSeconds?: number, now?: () => number) => boolean;
|
|
4
16
|
export declare const calculateAuthResponse: (res: AuthChallengeResponse, credentials: {
|
|
5
17
|
username: string;
|
|
6
18
|
secret: string;
|
package/dist/auth.js
CHANGED
|
@@ -3,13 +3,41 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.getCredentials = exports.calculateAuthResponse = exports.generateNonce = exports.DEFAULT_ALGORITHM = void 0;
|
|
6
|
+
exports.getCredentials = exports.calculateAuthResponse = exports.isNonceFresh = exports.generateNonce = exports.NONCE_MAX_AGE_SECONDS = exports.DEFAULT_ALGORITHM = void 0;
|
|
7
7
|
const crypto_1 = __importDefault(require("crypto"));
|
|
8
8
|
exports.DEFAULT_ALGORITHM = "MD5";
|
|
9
9
|
const md5hex = (str, algorithm = exports.DEFAULT_ALGORITHM) => crypto_1.default.createHash(algorithm).update(str).digest("hex");
|
|
10
10
|
const decToHex = (dec) => (dec + Math.pow(16, 8)).toString(16).slice(-8);
|
|
11
|
-
|
|
11
|
+
// A nonce carries the second it was issued, so a captured Authorization
|
|
12
|
+
// header stops working once NONCE_MAX_AGE_SECONDS have passed (isNonceFresh)
|
|
13
|
+
exports.NONCE_MAX_AGE_SECONDS = 300;
|
|
14
|
+
const generateNonce = (algorithm = exports.DEFAULT_ALGORITHM, now = Date.now) => {
|
|
15
|
+
const issuedAt = Math.floor(now() / 1000).toString(16);
|
|
16
|
+
return `${issuedAt}.${md5hex(`${now()}${Math.random()}`, algorithm)}`;
|
|
17
|
+
};
|
|
12
18
|
exports.generateNonce = generateNonce;
|
|
19
|
+
/**
|
|
20
|
+
* Whether a nonce was issued by generateNonce within the last maxAgeSeconds.
|
|
21
|
+
* A nonce in another format is never fresh, so a header captured before
|
|
22
|
+
* nonces carried their time can't be replayed either.
|
|
23
|
+
*
|
|
24
|
+
* @param {string} nonce - the nonce from the Authorization header
|
|
25
|
+
* @param {number} maxAgeSeconds - how long a nonce stays valid; 0 means forever
|
|
26
|
+
* @param {Function} now - the clock, injected by tests
|
|
27
|
+
* @return {boolean} true when the nonce is fresh
|
|
28
|
+
*/
|
|
29
|
+
const isNonceFresh = (nonce, maxAgeSeconds = exports.NONCE_MAX_AGE_SECONDS, now = Date.now) => {
|
|
30
|
+
if (maxAgeSeconds === 0)
|
|
31
|
+
return true;
|
|
32
|
+
const [issuedAtHex, digest] = (nonce !== null && nonce !== void 0 ? nonce : "").split(".");
|
|
33
|
+
if (!digest || !/^[0-9a-f]+$/.test(issuedAtHex))
|
|
34
|
+
return false;
|
|
35
|
+
// A nonce dated ahead of this clock (another instance's) is only as fresh
|
|
36
|
+
// as it gets: the digest binds it, so no one gains by choosing one
|
|
37
|
+
const age = Math.floor(now() / 1000) - parseInt(issuedAtHex, 16);
|
|
38
|
+
return age <= maxAgeSeconds;
|
|
39
|
+
};
|
|
40
|
+
exports.isNonceFresh = isNonceFresh;
|
|
13
41
|
const calculateAuthResponse = (res, credentials) => {
|
|
14
42
|
if (!credentials)
|
|
15
43
|
return null;
|
package/dist/response.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { ResponseType } from "./types";
|
|
|
2
2
|
export declare const createUnauthorizedResponse: (realm: string, params?: {
|
|
3
3
|
qop: string;
|
|
4
4
|
algorithm: string;
|
|
5
|
+
stale?: boolean;
|
|
5
6
|
}) => {
|
|
6
7
|
message: {
|
|
7
8
|
responseType: ResponseType;
|
|
@@ -17,6 +18,21 @@ export declare const createUnauthorizedResponse: (realm: string, params?: {
|
|
|
17
18
|
};
|
|
18
19
|
};
|
|
19
20
|
};
|
|
21
|
+
export declare const createStaleUnauthorizedResponse: (realm: string) => {
|
|
22
|
+
message: {
|
|
23
|
+
responseType: ResponseType;
|
|
24
|
+
reasonPhrase: string;
|
|
25
|
+
wwwAuthenticate: {
|
|
26
|
+
scheme: string;
|
|
27
|
+
realm: string;
|
|
28
|
+
qop: string;
|
|
29
|
+
opaque: string;
|
|
30
|
+
stale: boolean;
|
|
31
|
+
nonce: string;
|
|
32
|
+
algorithm: string;
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
};
|
|
20
36
|
export declare const createUnauthorizedResponseWithoutChallenge: (metadata?: Record<string, string>) => {
|
|
21
37
|
metadata: Record<string, string>;
|
|
22
38
|
message: {
|
package/dist/response.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createNotFoundResponse = exports.createBadRequestResponse = exports.createForbiddenResponse = exports.createServerInternalErrorResponse = exports.createUnauthorizedResponseWithoutChallenge = exports.createUnauthorizedResponse = void 0;
|
|
3
|
+
exports.createNotFoundResponse = exports.createBadRequestResponse = exports.createForbiddenResponse = exports.createServerInternalErrorResponse = exports.createUnauthorizedResponseWithoutChallenge = exports.createStaleUnauthorizedResponse = exports.createUnauthorizedResponse = void 0;
|
|
4
4
|
/*
|
|
5
5
|
* Copyright (C) 2026 by Fonoster Inc (https://fonoster.com)
|
|
6
6
|
* http://github.com/fonoster/routr
|
|
@@ -22,6 +22,7 @@ exports.createNotFoundResponse = exports.createBadRequestResponse = exports.crea
|
|
|
22
22
|
const auth_1 = require("./auth");
|
|
23
23
|
const types_1 = require("./types");
|
|
24
24
|
const createUnauthorizedResponse = (realm, params = { qop: "auth", algorithm: auth_1.DEFAULT_ALGORITHM }) => {
|
|
25
|
+
var _a;
|
|
25
26
|
return {
|
|
26
27
|
message: {
|
|
27
28
|
responseType: types_1.ResponseType.UNAUTHORIZED,
|
|
@@ -31,7 +32,7 @@ const createUnauthorizedResponse = (realm, params = { qop: "auth", algorithm: au
|
|
|
31
32
|
realm: realm,
|
|
32
33
|
qop: params.qop,
|
|
33
34
|
opaque: "",
|
|
34
|
-
stale: false,
|
|
35
|
+
stale: (_a = params.stale) !== null && _a !== void 0 ? _a : false,
|
|
35
36
|
nonce: (0, auth_1.generateNonce)(),
|
|
36
37
|
algorithm: params.algorithm
|
|
37
38
|
}
|
|
@@ -39,6 +40,14 @@ const createUnauthorizedResponse = (realm, params = { qop: "auth", algorithm: au
|
|
|
39
40
|
};
|
|
40
41
|
};
|
|
41
42
|
exports.createUnauthorizedResponse = createUnauthorizedResponse;
|
|
43
|
+
// The credentials were right but the nonce had expired: a client retries with
|
|
44
|
+
// the new nonce without asking the user again (RFC 7616 section 3.3)
|
|
45
|
+
const createStaleUnauthorizedResponse = (realm) => (0, exports.createUnauthorizedResponse)(realm, {
|
|
46
|
+
qop: "auth",
|
|
47
|
+
algorithm: auth_1.DEFAULT_ALGORITHM,
|
|
48
|
+
stale: true
|
|
49
|
+
});
|
|
50
|
+
exports.createStaleUnauthorizedResponse = createStaleUnauthorizedResponse;
|
|
42
51
|
const createUnauthorizedResponseWithoutChallenge = (metadata) => {
|
|
43
52
|
return {
|
|
44
53
|
metadata,
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Sampler } from "@opentelemetry/sdk-trace";
|
|
2
|
+
/**
|
|
3
|
+
* The trace sampler named by the standard OTEL_TRACES_SAMPLER and
|
|
4
|
+
* OTEL_TRACES_SAMPLER_ARG variables, as the OpenTelemetry specification defines
|
|
5
|
+
* them (and as the EdgePort's Java SDK reads them): always_on, always_off,
|
|
6
|
+
* traceidratio, parentbased_always_on (the default), parentbased_always_off and
|
|
7
|
+
* parentbased_traceidratio. The ratio is a number from 0 to 1 and defaults to 1.
|
|
8
|
+
* An unknown sampler or a bad ratio falls back to the default with a warning, so
|
|
9
|
+
* a typo never stops a service.
|
|
10
|
+
*
|
|
11
|
+
* @param {NodeJS.ProcessEnv} env - The environment to read.
|
|
12
|
+
* @param {Function} warn - Receives a message for each value that was ignored.
|
|
13
|
+
* @return {Sampler} The sampler for the tracer provider.
|
|
14
|
+
*/
|
|
15
|
+
export declare function samplerFromEnv(env?: NodeJS.ProcessEnv, warn?: (message: string) => void): Sampler;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.samplerFromEnv = samplerFromEnv;
|
|
4
|
+
/*
|
|
5
|
+
* Copyright (C) 2026 by Fonoster Inc (https://fonoster.com)
|
|
6
|
+
* http://github.com/fonoster/routr
|
|
7
|
+
*
|
|
8
|
+
* This file is part of Routr.
|
|
9
|
+
*
|
|
10
|
+
* Licensed under the MIT License (the "License");
|
|
11
|
+
* you may not use this file except in compliance with
|
|
12
|
+
* the License. You may obtain a copy of the License at
|
|
13
|
+
*
|
|
14
|
+
* https://opensource.org/licenses/MIT
|
|
15
|
+
*
|
|
16
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
17
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
18
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
19
|
+
* See the License for the specific language governing permissions and
|
|
20
|
+
* limitations under the License.
|
|
21
|
+
*/
|
|
22
|
+
const sdk_trace_1 = require("@opentelemetry/sdk-trace");
|
|
23
|
+
const DEFAULT_SAMPLER = "parentbased_always_on";
|
|
24
|
+
/**
|
|
25
|
+
* The trace sampler named by the standard OTEL_TRACES_SAMPLER and
|
|
26
|
+
* OTEL_TRACES_SAMPLER_ARG variables, as the OpenTelemetry specification defines
|
|
27
|
+
* them (and as the EdgePort's Java SDK reads them): always_on, always_off,
|
|
28
|
+
* traceidratio, parentbased_always_on (the default), parentbased_always_off and
|
|
29
|
+
* parentbased_traceidratio. The ratio is a number from 0 to 1 and defaults to 1.
|
|
30
|
+
* An unknown sampler or a bad ratio falls back to the default with a warning, so
|
|
31
|
+
* a typo never stops a service.
|
|
32
|
+
*
|
|
33
|
+
* @param {NodeJS.ProcessEnv} env - The environment to read.
|
|
34
|
+
* @param {Function} warn - Receives a message for each value that was ignored.
|
|
35
|
+
* @return {Sampler} The sampler for the tracer provider.
|
|
36
|
+
*/
|
|
37
|
+
function samplerFromEnv(env = process.env, warn = (message) => process.emitWarning(message)) {
|
|
38
|
+
var _a;
|
|
39
|
+
const name = (((_a = env.OTEL_TRACES_SAMPLER) === null || _a === void 0 ? void 0 : _a.trim()) || DEFAULT_SAMPLER).toLowerCase();
|
|
40
|
+
const ratio = () => {
|
|
41
|
+
var _a;
|
|
42
|
+
const arg = (_a = env.OTEL_TRACES_SAMPLER_ARG) === null || _a === void 0 ? void 0 : _a.trim();
|
|
43
|
+
if (!arg)
|
|
44
|
+
return 1;
|
|
45
|
+
const value = Number(arg);
|
|
46
|
+
if (Number.isFinite(value) && value >= 0 && value <= 1)
|
|
47
|
+
return value;
|
|
48
|
+
warn(`OTEL_TRACES_SAMPLER_ARG "${arg}" is not a number from 0 to 1; using 1`);
|
|
49
|
+
return 1;
|
|
50
|
+
};
|
|
51
|
+
switch (name) {
|
|
52
|
+
case "always_on":
|
|
53
|
+
return new sdk_trace_1.AlwaysOnSampler();
|
|
54
|
+
case "always_off":
|
|
55
|
+
return new sdk_trace_1.AlwaysOffSampler();
|
|
56
|
+
case "traceidratio":
|
|
57
|
+
return new sdk_trace_1.TraceIdRatioBasedSampler(ratio());
|
|
58
|
+
case "parentbased_always_on":
|
|
59
|
+
return new sdk_trace_1.ParentBasedSampler({ root: new sdk_trace_1.AlwaysOnSampler() });
|
|
60
|
+
case "parentbased_always_off":
|
|
61
|
+
return new sdk_trace_1.ParentBasedSampler({ root: new sdk_trace_1.AlwaysOffSampler() });
|
|
62
|
+
case "parentbased_traceidratio":
|
|
63
|
+
return new sdk_trace_1.ParentBasedSampler({
|
|
64
|
+
root: new sdk_trace_1.TraceIdRatioBasedSampler(ratio())
|
|
65
|
+
});
|
|
66
|
+
default:
|
|
67
|
+
warn(`OTEL_TRACES_SAMPLER "${name}" is not supported; using ${DEFAULT_SAMPLER}`);
|
|
68
|
+
return new sdk_trace_1.ParentBasedSampler({ root: new sdk_trace_1.AlwaysOnSampler() });
|
|
69
|
+
}
|
|
70
|
+
}
|
package/dist/tracer.js
CHANGED
|
@@ -78,6 +78,7 @@ const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
|
|
|
78
78
|
const exporter_trace_otlp_http_1 = require("@opentelemetry/exporter-trace-otlp-http");
|
|
79
79
|
const exporter_metrics_otlp_http_1 = require("@opentelemetry/exporter-metrics-otlp-http");
|
|
80
80
|
const exporter_logs_otlp_http_1 = require("@opentelemetry/exporter-logs-otlp-http");
|
|
81
|
+
const sampler_from_env_1 = require("./sampler_from_env");
|
|
81
82
|
/**
|
|
82
83
|
* Whether telemetry is on: an OTLP endpoint is configured and the SDK is not disabled.
|
|
83
84
|
*
|
|
@@ -133,6 +134,8 @@ function init(serviceName, env = process.env) {
|
|
|
133
134
|
}));
|
|
134
135
|
const tracerProvider = new sdk_trace_1.TracerProvider({
|
|
135
136
|
resource,
|
|
137
|
+
// The SDK does not read OTEL_TRACES_SAMPLER itself: without this every span is sampled.
|
|
138
|
+
sampler: (0, sampler_from_env_1.samplerFromEnv)(env),
|
|
136
139
|
spanProcessors: [
|
|
137
140
|
new sdk_trace_1.BatchSpanProcessor({
|
|
138
141
|
exporter: new exporter_trace_otlp_http_1.OTLPTraceExporter({ url: signalUrl(env, "traces") })
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@routr/common",
|
|
3
|
-
"version": "2.32.
|
|
3
|
+
"version": "2.32.3",
|
|
4
4
|
"description": "Common package",
|
|
5
5
|
"author": "Pedro Sanders <psanders@fonoster.com>",
|
|
6
6
|
"homepage": "https://github.com/fonoster/routr#readme",
|
|
@@ -61,5 +61,5 @@
|
|
|
61
61
|
"devDependencies": {
|
|
62
62
|
"@types/js-yaml": "^4.0.5"
|
|
63
63
|
},
|
|
64
|
-
"gitHead": "
|
|
64
|
+
"gitHead": "88199378914c62dbd44cf187c7892ce8435c3ca3"
|
|
65
65
|
}
|