securenow 3.0.10 → 3.0.11

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/package.json CHANGED
@@ -1,16 +1,23 @@
1
1
  {
2
2
  "name": "securenow",
3
- "version": "3.0.10",
3
+ "version": "3.0.11",
4
4
  "type": "commonjs",
5
5
  "main": "register.js",
6
6
  "exports": {
7
7
  ".": "./register.js",
8
8
  "./register": "./register.js",
9
- "./tracing": "./tracing.js"
9
+ "./tracing": "./tracing.js",
10
+ "./register-vite": "./register-vite.js",
11
+ "./web-vite": {
12
+ "import": "./web-vite.mjs",
13
+ "default": "./web-vite.mjs"
14
+ }
10
15
  },
11
16
  "files": [
12
17
  "register.js",
13
18
  "tracing.js",
19
+ "register-vite.js",
20
+ "web-vite.mjs",
14
21
  "README.md"
15
22
  ],
16
23
  "dependencies": {
@@ -20,6 +27,12 @@
20
27
  "@opentelemetry/resources": "1.20.0",
21
28
  "@opentelemetry/sdk-node": "0.47.0",
22
29
  "@opentelemetry/semantic-conventions": "1.20.0",
30
+ "@opentelemetry/sdk-trace-web": "1.20.0",
31
+ "@opentelemetry/instrumentation": "0.47.0",
32
+ "@opentelemetry/instrumentation-document-load": "0.47.0",
33
+ "@opentelemetry/instrumentation-user-interaction": "0.47.0",
34
+ "@opentelemetry/instrumentation-fetch": "0.47.0",
35
+ "@opentelemetry/instrumentation-xml-http-request": "0.47.0",
23
36
  "dotenv": "^17.2.1",
24
37
  "uuid": "^9.0.0"
25
38
  },
@@ -0,0 +1,14 @@
1
+ // securenow/preload.js
2
+ 'use strict';
3
+
4
+ // load .env into process.env before anything else
5
+ try {
6
+ require('dotenv').config();
7
+ console.log('[securenow] dotenv loaded from', process.env.DOTENV_CONFIG_PATH || '.env');
8
+ } catch (e) {
9
+ // dotenv is optional — only warn if it’s missing
10
+ console.warn('[securenow] dotenv not found or failed to load');
11
+ }
12
+
13
+ // then run the real tracer preload
14
+ require('./web-vite.mjs');
package/web-vite.mjs ADDED
@@ -0,0 +1,156 @@
1
+ // web-vite.ts — Browser OTel for Vite (ESM)
2
+ // Defaults & naming rules match your Node tracing.js
3
+
4
+ import { WebTracerProvider, BatchSpanProcessor } from '@opentelemetry/sdk-trace-web';
5
+ import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
6
+ import { Resource } from '@opentelemetry/resources';
7
+ import { SemanticResourceAttributes as S } from '@opentelemetry/semantic-conventions';
8
+ import { registerInstrumentations } from '@opentelemetry/instrumentation';
9
+ import { DocumentLoadInstrumentation } from '@opentelemetry/instrumentation-document-load';
10
+ import { UserInteractionInstrumentation } from '@opentelemetry/instrumentation-user-interaction';
11
+ import { FetchInstrumentation } from '@opentelemetry/instrumentation-fetch';
12
+ import { XMLHttpRequestInstrumentation } from '@opentelemetry/instrumentation-xml-http-request';
13
+
14
+ // ---- helpers / env ----
15
+ const viteEnv: any = (import.meta as any).env || {};
16
+
17
+ function env(k: string): string | undefined {
18
+ // Accept both Vite envs (VITE_*) and raw names for window.__SECURENOW__
19
+ const direct =
20
+ viteEnv[k] ??
21
+ viteEnv[k.toUpperCase()] ??
22
+ viteEnv[k.toLowerCase()];
23
+ if (direct != null) return String(direct);
24
+
25
+ // Optionally support runtime overrides via window.__SECURENOW__
26
+ const w = (globalThis as any).window as any;
27
+ if (w && w.__SECURENOW__ && k in w.__SECURENOW__) return String(w.__SECURENOW__[k]);
28
+ return undefined;
29
+ }
30
+
31
+ function parseHeaders(str?: string) {
32
+ const out: Record<string, string> = {};
33
+ if (!str) return out;
34
+ String(str).split(',').forEach(raw => {
35
+ const s = raw.trim();
36
+ if (!s) return;
37
+ const i = s.indexOf('=');
38
+ if (i === -1) return;
39
+ out[s.slice(0, i).trim().toLowerCase()] = s.slice(i + 1).trim();
40
+ });
41
+ return out;
42
+ }
43
+
44
+ // ---- endpoints (same defaults as tracing.js) ----
45
+ const endpointBase =
46
+ (env('SECURENOW_INSTANCE') || env('OTEL_EXPORTER_OTLP_ENDPOINT') || 'http://46.62.173.237:4318')
47
+ .replace(/\/$/, '');
48
+ const tracesUrl =
49
+ env('OTEL_EXPORTER_OTLP_TRACES_ENDPOINT') || `${endpointBase}/v1/traces`;
50
+ const headers = parseHeaders(env('OTEL_EXPORTER_OTLP_HEADERS'));
51
+
52
+ // ---- naming rules (mirrors tracing.js) ----
53
+ const rawBase = (env('OTEL_SERVICE_NAME') || env('SECURENOW_APPID') || '').trim().replace(/^['"]|['"]$/g, '');
54
+ const baseName = rawBase || null;
55
+ const noUuid = String(env('SECURENOW_NO_UUID')) === '1' || String(env('SECURENOW_NO_UUID')).toLowerCase() === 'true';
56
+ const strict = String(env('SECURENOW_STRICT')) === '1' || String(env('SECURENOW_STRICT')).toLowerCase() === 'true';
57
+
58
+ // Simple UUID v4 (no crypto dependency needed)
59
+ function uuidv4(): string {
60
+ const rnd = (n = 16) => Array.from({ length: n }, () => Math.floor(Math.random() * 16).toString(16)).join('');
61
+ return `${rnd(8)}-${rnd(4)}-4${rnd(3)}-${((8 + Math.random()*4)|0).toString(16)}${rnd(3)}-${rnd(12)}`;
62
+ }
63
+
64
+ let serviceName: string;
65
+ if (baseName) {
66
+ serviceName = noUuid ? baseName : `${baseName}-${uuidv4()}`;
67
+ } else {
68
+ if (strict) {
69
+ console.error('[securenow/web-vite] FATAL: SECURENOW_APPID/OTEL_SERVICE_NAME missing and SECURENOW_STRICT=1. Tracing disabled.');
70
+ // Do not start tracing
71
+ // @ts-expect-error
72
+ window.__SECURENOW_DISABLED__ = true;
73
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
74
+ const _noop = true;
75
+ // early return by throwing a no-op error caught below:
76
+ throw new Error('__SECURENOW_NO_START__');
77
+ }
78
+ serviceName = `securenow-free-${uuidv4()}`;
79
+ }
80
+
81
+ const instancePrefix = baseName || 'securenow';
82
+ const serviceInstanceId = `${instancePrefix}-${uuidv4()}`;
83
+
84
+ // Loud line
85
+ try {
86
+ // eslint-disable-next-line no-console
87
+ console.log(
88
+ '[securenow] web preload loaded SECURENOW_APPID=%s OTEL_SERVICE_NAME=%s SECURENOW_NO_UUID=%s SECURENOW_STRICT=%s → service.name=%s instance.id=%s',
89
+ JSON.stringify(env('SECURENOW_APPID')),
90
+ JSON.stringify(env('OTEL_SERVICE_NAME')),
91
+ JSON.stringify(env('SECURENOW_NO_UUID')),
92
+ JSON.stringify(env('SECURENOW_STRICT')),
93
+ serviceName,
94
+ serviceInstanceId
95
+ );
96
+ } catch {}
97
+
98
+ // ---- Provider / Exporter ----
99
+ let started = false;
100
+
101
+ export function startSecurenowWeb() {
102
+ if (started) return;
103
+ started = true;
104
+
105
+ const exporter = new OTLPTraceExporter({
106
+ url: tracesUrl,
107
+ headers,
108
+ });
109
+
110
+ const provider = new WebTracerProvider({
111
+ resource: new Resource({
112
+ [S.SERVICE_NAME]: serviceName,
113
+ [S.SERVICE_INSTANCE_ID]: serviceInstanceId,
114
+ [S.DEPLOYMENT_ENVIRONMENT]: viteEnv.MODE || 'production',
115
+ [S.SERVICE_VERSION]: viteEnv.VITE_APP_VERSION || undefined,
116
+ }),
117
+ });
118
+
119
+ provider.addSpanProcessor(new BatchSpanProcessor(exporter));
120
+ provider.register();
121
+
122
+ registerInstrumentations({
123
+ instrumentations: [
124
+ new DocumentLoadInstrumentation(),
125
+ new UserInteractionInstrumentation(),
126
+ new FetchInstrumentation({
127
+ propagateTraceHeaderCorsUrls: [/.*/],
128
+ ignoreUrls: [/\/vite\/hmr/, /^chrome-extension:\/\//, /sockjs/],
129
+ }),
130
+ new XMLHttpRequestInstrumentation({
131
+ propagateTraceHeaderCorsUrls: [/.*/],
132
+ }),
133
+ ],
134
+ });
135
+
136
+ // Optional smoke span (same flag name)
137
+ if (String(env('SECURENOW_TEST_SPAN')) === '1') {
138
+ const api = await import('@opentelemetry/api');
139
+ const tracer = api.trace.getTracer('securenow-smoke');
140
+ const span = tracer.startSpan('securenow.startup.smoke.web'); span.end();
141
+ }
142
+
143
+ // eslint-disable-next-line no-console
144
+ console.log('[securenow] Web OTel started → %s', tracesUrl);
145
+ }
146
+
147
+ // Auto-start
148
+ try {
149
+ startSecurenowWeb();
150
+ } catch (e: any) {
151
+ if (String(e?.message) !== '__SECURENOW_NO_START__') {
152
+ console.error('[securenow/web-vite] failed to start:', e);
153
+ }
154
+ }
155
+
156
+ export default startSecurenowWeb;