@ckirg/corelib 0.1.22

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.
@@ -0,0 +1,120 @@
1
+ import {
2
+ SysInfo
3
+ } from "./chunk-HPE2XSTW.js";
4
+
5
+ // src/loggers/common/index.ts
6
+ var TELEMETRY_CACHE_TTL_MS = 5e3;
7
+ var StrictLoggerWrapper = class _StrictLoggerWrapper {
8
+ pinoInstance;
9
+ state;
10
+ context;
11
+ telemetryCache = null;
12
+ constructor(pinoInstance, state = { telemetryEnabled: false }, context = {}) {
13
+ this.pinoInstance = pinoInstance;
14
+ this.state = state;
15
+ this.context = context;
16
+ }
17
+ getTelemetry() {
18
+ if (!this.state.telemetryEnabled) return void 0;
19
+ const now = Date.now();
20
+ if (!this.telemetryCache || now >= this.telemetryCache.expiresAt) {
21
+ this.telemetryCache = {
22
+ value: SysInfo.get(),
23
+ expiresAt: now + TELEMETRY_CACHE_TTL_MS
24
+ };
25
+ }
26
+ return this.telemetryCache.value;
27
+ }
28
+ validate(msg, extras) {
29
+ if (typeof msg !== "string") {
30
+ throw new Error(
31
+ "Logger requires string message first, optional object second"
32
+ );
33
+ }
34
+ if (extras !== void 0 && (typeof extras !== "object" || extras === null || Array.isArray(extras))) {
35
+ throw new Error(
36
+ "Logger requires string message first, optional object second"
37
+ );
38
+ }
39
+ }
40
+ trace(msg, extras) {
41
+ this.validate(msg, extras);
42
+ this.pinoInstance.trace(
43
+ { ...this.context, ...extras, telemetry: this.getTelemetry() },
44
+ msg
45
+ );
46
+ }
47
+ debug(msg, extras) {
48
+ this.validate(msg, extras);
49
+ this.pinoInstance.debug(
50
+ { ...this.context, ...extras, telemetry: this.getTelemetry() },
51
+ msg
52
+ );
53
+ }
54
+ info(msg, extras) {
55
+ this.validate(msg, extras);
56
+ this.pinoInstance.info(
57
+ { ...this.context, ...extras, telemetry: this.getTelemetry() },
58
+ msg
59
+ );
60
+ }
61
+ warn(msg, extras) {
62
+ this.validate(msg, extras);
63
+ this.pinoInstance.warn(
64
+ { ...this.context, ...extras, telemetry: this.getTelemetry() },
65
+ msg
66
+ );
67
+ }
68
+ error(msg, extras) {
69
+ this.validate(msg, extras);
70
+ this.pinoInstance.error(
71
+ { ...this.context, ...extras, telemetry: this.getTelemetry() },
72
+ msg
73
+ );
74
+ }
75
+ fatal(msg, extras) {
76
+ this.validate(msg, extras);
77
+ this.pinoInstance.fatal(
78
+ { ...this.context, ...extras, telemetry: this.getTelemetry() },
79
+ msg
80
+ );
81
+ }
82
+ child(bindings) {
83
+ return new _StrictLoggerWrapper(this.pinoInstance, this.state, {
84
+ ...this.context,
85
+ ...bindings
86
+ });
87
+ }
88
+ setTelemetry(mode) {
89
+ if (mode !== "on" && mode !== "off") {
90
+ throw new Error("setTelemetry accepts only 'on' or 'off'");
91
+ }
92
+ this.state.telemetryEnabled = mode === "on";
93
+ }
94
+ get level() {
95
+ return this.pinoInstance.level;
96
+ }
97
+ set level(val) {
98
+ this.pinoInstance.level = val;
99
+ }
100
+ get levelVal() {
101
+ return this.pinoInstance.levelVal;
102
+ }
103
+ bindings() {
104
+ return { ...this.context };
105
+ }
106
+ silent() {
107
+ this.pinoInstance.level = "silent";
108
+ }
109
+ flush(cb) {
110
+ if (typeof this.pinoInstance.flush === "function") {
111
+ this.pinoInstance.flush(cb);
112
+ } else {
113
+ cb?.();
114
+ }
115
+ }
116
+ };
117
+
118
+ export {
119
+ StrictLoggerWrapper
120
+ };
@@ -0,0 +1,202 @@
1
+ // src/utils/runtime.ts
2
+ var _cachedRuntime;
3
+ function computeRuntime() {
4
+ try {
5
+ if (typeof process !== "undefined" && process.env && process.env.RUNTIME) {
6
+ const envRuntime = process.env.RUNTIME.toLowerCase();
7
+ if ([
8
+ "node",
9
+ "bun",
10
+ "deno",
11
+ "cloudflare",
12
+ "aws-lambda",
13
+ "gcp-cloudrun"
14
+ ].includes(envRuntime)) {
15
+ return envRuntime;
16
+ }
17
+ }
18
+ } catch {
19
+ }
20
+ try {
21
+ if (typeof globalThis !== "undefined" && (!!globalThis.cloudflare || !!globalThis.caches || !!globalThis.WebSocketPair || !!globalThis.__CFW__) || typeof process !== "undefined" && process.env && process.env.PLATFORM === "cloudflare") {
22
+ return "cloudflare";
23
+ }
24
+ } catch {
25
+ }
26
+ try {
27
+ if (typeof process !== "undefined" && process.env && process.env.AWS_LAMBDA_FUNCTION_NAME) {
28
+ return "aws-lambda";
29
+ }
30
+ } catch {
31
+ }
32
+ try {
33
+ if (typeof process !== "undefined" && process.env && (process.env.K_SERVICE || process.env.K_REVISION || process.env.GOOGLE_CLOUD_PROJECT)) {
34
+ return "gcp-cloudrun";
35
+ }
36
+ } catch {
37
+ }
38
+ if (typeof Bun !== "undefined") return "bun";
39
+ if (typeof Deno !== "undefined" && Deno.version && Deno.version.deno) {
40
+ return "deno";
41
+ }
42
+ return "node";
43
+ }
44
+ function detectRuntime() {
45
+ if (_cachedRuntime !== void 0) return _cachedRuntime;
46
+ _cachedRuntime = computeRuntime();
47
+ return _cachedRuntime;
48
+ }
49
+
50
+ // src/utils/SysInfo.ts
51
+ var _createRequire;
52
+ if (typeof __EDGE_RUNTIME__ === "undefined" || !__EDGE_RUNTIME__) {
53
+ _createRequire = (await import("module")).createRequire;
54
+ }
55
+ var _require;
56
+ var getRequire = () => {
57
+ if (!_require) {
58
+ const runtime = detectRuntime();
59
+ if (_createRequire && typeof import.meta !== "undefined" && import.meta.url) {
60
+ try {
61
+ _require = _createRequire(import.meta.url);
62
+ } catch (_e) {
63
+ }
64
+ }
65
+ if (!_require && typeof globalThis.require === "function") {
66
+ _require = globalThis.require;
67
+ }
68
+ if (!_require) {
69
+ _require = (path) => {
70
+ throw new Error(
71
+ `require("${path}") is not available in this runtime (${runtime}).`
72
+ );
73
+ };
74
+ }
75
+ }
76
+ return _require;
77
+ };
78
+ function redactEnv(env) {
79
+ const redacted = {};
80
+ const secretKeywords = [
81
+ "KEY",
82
+ "SECRET",
83
+ "PASSWORD",
84
+ "TOKEN",
85
+ "AUTH",
86
+ "CREDENTIAL",
87
+ "APIKEY",
88
+ "PRIVATE",
89
+ "CERT",
90
+ "KEYSTORE"
91
+ ];
92
+ for (const [key, value] of Object.entries(env)) {
93
+ if (value === void 0) continue;
94
+ const upperKey = key.toUpperCase();
95
+ const isSecret = secretKeywords.some((k) => upperKey.includes(k));
96
+ redacted[key] = isSecret ? "[REDACTED]" : value;
97
+ }
98
+ return redacted;
99
+ }
100
+ function fromNodeLike(runtime) {
101
+ const os = getRequire()("node:os");
102
+ const mem = typeof process.memoryUsage === "function" ? process.memoryUsage() : {};
103
+ return {
104
+ /** Current runtime name. */
105
+ runtime,
106
+ /** Operating system platform. */
107
+ os: process.platform,
108
+ /** System architecture. */
109
+ arch: process.arch,
110
+ /** Process ID. */
111
+ pid: process.pid,
112
+ /** Parent process ID. */
113
+ ppid: process.ppid ?? null,
114
+ /** Current working directory. */
115
+ cwd: process.cwd(),
116
+ /** Process uptime in seconds. */
117
+ uptime: process.uptime(),
118
+ /** Operating system version/release. */
119
+ osVersion: os.release?.() ?? null,
120
+ /** System load averages for 1, 5, and 15 minutes. */
121
+ loadAvg: os.loadavg?.() ?? [0, 0, 0],
122
+ /** Memory usage statistics. */
123
+ memory: {
124
+ /** Resident Set Size. */
125
+ rss: mem.rss ?? null,
126
+ /** Total heap size. */
127
+ heapTotal: mem.heapTotal ?? null,
128
+ /** Used heap size. */
129
+ heapUsed: mem.heapUsed ?? null,
130
+ /** Memory used by C++ objects bound to JavaScript objects. */
131
+ external: mem.external ?? null
132
+ },
133
+ /** Redacted environment variables. */
134
+ env: redactEnv({ ...process.env })
135
+ };
136
+ }
137
+ function fromDeno() {
138
+ const DenoAny = typeof Deno !== "undefined" ? Deno : null;
139
+ const mem = DenoAny && typeof DenoAny.systemMemoryInfo === "function" ? DenoAny.systemMemoryInfo() : {};
140
+ return {
141
+ runtime: "deno",
142
+ os: DenoAny?.build?.os ?? "unknown",
143
+ arch: DenoAny?.build?.arch ?? "unknown",
144
+ pid: DenoAny?.pid ?? null,
145
+ ppid: DenoAny?.ppid ?? null,
146
+ cwd: DenoAny?.cwd?.() ?? "",
147
+ uptime: typeof performance !== "undefined" ? performance.now() / 1e3 : 0,
148
+ osVersion: DenoAny?.osRelease?.() ?? null,
149
+ loadAvg: DenoAny?.loadavg?.() ?? [0, 0, 0],
150
+ memory: {
151
+ rss: mem.total ?? null,
152
+ heapTotal: null,
153
+ heapUsed: null,
154
+ external: null
155
+ },
156
+ env: redactEnv(DenoAny?.env?.toObject() ?? {})
157
+ };
158
+ }
159
+ function fallback() {
160
+ return {
161
+ runtime: "unknown",
162
+ os: "unknown",
163
+ arch: "unknown",
164
+ pid: null,
165
+ ppid: null,
166
+ cwd: "",
167
+ uptime: 0,
168
+ osVersion: null,
169
+ loadAvg: [0, 0, 0],
170
+ memory: {
171
+ rss: null,
172
+ heapTotal: null,
173
+ heapUsed: null,
174
+ external: null
175
+ },
176
+ env: {}
177
+ };
178
+ }
179
+ function getSysInfo() {
180
+ const runtime = detectRuntime();
181
+ switch (runtime) {
182
+ case "node":
183
+ case "bun":
184
+ return fromNodeLike(runtime);
185
+ case "deno":
186
+ return fromDeno();
187
+ default:
188
+ return fallback();
189
+ }
190
+ }
191
+ var SysInfo = {
192
+ /**
193
+ * Gets system information.
194
+ */
195
+ get: getSysInfo
196
+ };
197
+
198
+ export {
199
+ detectRuntime,
200
+ getSysInfo,
201
+ SysInfo
202
+ };