@oxy.so/core 1.0.0 → 1.1.1
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/cjs/.tsbuildinfo +1 -1
- package/dist/cjs/OxyServices.errors.js +28 -1
- package/dist/cjs/crypto/keyManager.js +5 -3
- package/dist/cjs/i18n/locales/en-US.json +18 -5
- package/dist/cjs/i18n/locales/es-ES.json +18 -5
- package/dist/cjs/i18n/locales/locales/en-US.json +18 -5
- package/dist/cjs/i18n/locales/locales/es-ES.json +18 -5
- package/dist/cjs/mixins/OxyServices.agency.js +113 -0
- package/dist/cjs/mixins/OxyServices.assets.js +90 -0
- package/dist/cjs/mixins/OxyServices.connectedApps.js +20 -3
- package/dist/cjs/mixins/OxyServices.utility.js +1 -21
- package/dist/cjs/mixins/index.js +3 -4
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/server/index.js +6 -1
- package/dist/cjs/server/traffic.js +107 -0
- package/dist/cjs/server/trafficNodeHttp.js +86 -0
- package/dist/cjs/utils/textNormalization.js +1 -1
- package/dist/esm/.tsbuildinfo +1 -1
- package/dist/esm/OxyServices.errors.js +26 -0
- package/dist/esm/crypto/keyManager.js +5 -3
- package/dist/esm/i18n/locales/en-US.json +18 -5
- package/dist/esm/i18n/locales/es-ES.json +18 -5
- package/dist/esm/i18n/locales/locales/en-US.json +18 -5
- package/dist/esm/i18n/locales/locales/es-ES.json +18 -5
- package/dist/esm/mixins/OxyServices.agency.js +110 -0
- package/dist/esm/mixins/OxyServices.assets.js +91 -1
- package/dist/esm/mixins/OxyServices.connectedApps.js +20 -3
- package/dist/esm/mixins/OxyServices.utility.js +1 -21
- package/dist/esm/mixins/index.js +3 -4
- package/dist/esm/package.json +3 -0
- package/dist/esm/server/index.js +2 -0
- package/dist/esm/server/traffic.js +104 -0
- package/dist/esm/server/trafficNodeHttp.js +79 -0
- package/dist/esm/utils/textNormalization.js +1 -1
- package/dist/types/.tsbuildinfo +1 -1
- package/dist/types/OxyServices.errors.d.ts +24 -0
- package/dist/types/crypto/keyManager.d.ts +4 -2
- package/dist/types/index.d.ts +2 -1
- package/dist/types/mixins/OxyServices.agency.d.ts +135 -0
- package/dist/types/mixins/OxyServices.assets.d.ts +43 -1
- package/dist/types/mixins/OxyServices.connectedApps.d.ts +15 -0
- package/dist/types/mixins/OxyServices.utility.d.ts +0 -9
- package/dist/types/mixins/index.d.ts +2 -2
- package/dist/types/models/interfaces.d.ts +26 -0
- package/dist/types/server/index.d.ts +3 -0
- package/dist/types/server/traffic.d.ts +27 -0
- package/dist/types/server/trafficNodeHttp.d.ts +6 -0
- package/dist/types/utils/textNormalization.d.ts +1 -1
- package/package.json +7 -6
- package/src/OxyServices.errors.ts +36 -0
- package/src/crypto/keyManager.ts +5 -4
- package/src/i18n/locales/en-US.json +18 -5
- package/src/i18n/locales/es-ES.json +18 -5
- package/src/index.ts +15 -0
- package/src/mixins/OxyServices.agency.ts +277 -0
- package/src/mixins/OxyServices.assets.ts +105 -2
- package/src/mixins/OxyServices.connectedApps.ts +42 -4
- package/src/mixins/OxyServices.utility.ts +1 -26
- package/src/mixins/__tests__/OxyServices.serviceLinkedDownloadUrl.test.ts +139 -0
- package/src/mixins/__tests__/agency.test.ts +120 -0
- package/src/mixins/__tests__/connectedApps.test.ts +23 -0
- package/src/mixins/index.ts +4 -5
- package/src/models/interfaces.ts +27 -0
- package/src/server/__tests__/traffic.test.ts +49 -0
- package/src/server/__tests__/trafficNodeHttp.test.ts +38 -0
- package/src/server/index.ts +5 -0
- package/src/server/traffic.ts +116 -0
- package/src/server/trafficNodeHttp.ts +69 -0
- package/src/utils/textNormalization.ts +1 -1
- package/dist/cjs/mixins/OxyServices.links.js +0 -72
- package/dist/esm/mixins/OxyServices.links.js +0 -69
- package/dist/types/mixins/OxyServices.links.d.ts +0 -104
- package/src/mixins/OxyServices.links.ts +0 -107
- package/src/mixins/__tests__/OxyServices.links.test.ts +0 -154
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { observeNodeHttp, withoutNodeHttpObservation } from './trafficNodeHttp.js';
|
|
2
|
+
import { observeTrafficSocket, observeTrafficWebSocket } from '@oxy.so/telemetry/socket';
|
|
3
|
+
import { randomUUID } from 'node:crypto';
|
|
4
|
+
import { OxyServices } from '../OxyServices.js';
|
|
5
|
+
import { createTrafficCollector, instrumentTrafficFetch, trafficMiddleware, resolveOxyServiceEndpoint, infrastructureLocation } from '@oxy.so/telemetry/collector';
|
|
6
|
+
/** Install once per service process, before registering HTTP routes. */
|
|
7
|
+
export function createEcosystemTraffic(options) {
|
|
8
|
+
const region = options.region ?? process.env.AWS_REGION ?? 'unknown';
|
|
9
|
+
const baseURL = options.baseURL ?? process.env.OXY_API_URL ?? 'https://api.oxy.so';
|
|
10
|
+
const activityKey = process.env.OXY_ACTIVITY_API_KEY;
|
|
11
|
+
const activitySecret = process.env.OXY_ACTIVITY_API_SECRET;
|
|
12
|
+
if (!options.credential && Boolean(activityKey) !== Boolean(activitySecret)) {
|
|
13
|
+
throw new Error('Ecosystem activity requires a complete OXY_ACTIVITY_API_KEY and OXY_ACTIVITY_API_SECRET pair');
|
|
14
|
+
}
|
|
15
|
+
const apiKey = activityKey || process.env.OXY_SERVICE_API_KEY;
|
|
16
|
+
const apiSecret = activitySecret || process.env.OXY_SERVICE_API_SECRET;
|
|
17
|
+
if (!options.credential && (!apiKey || !apiSecret))
|
|
18
|
+
throw new Error('Ecosystem activity requires OXY_SERVICE_API_KEY and OXY_SERVICE_API_SECRET');
|
|
19
|
+
const oxy = new OxyServices({ baseURL });
|
|
20
|
+
if (apiKey && apiSecret)
|
|
21
|
+
oxy.configureServiceAuth(apiKey, apiSecret);
|
|
22
|
+
const credential = options.credential ?? (() => oxy.getServiceToken());
|
|
23
|
+
const fetcher = globalThis.fetch.bind(globalThis);
|
|
24
|
+
const instanceId = randomUUID();
|
|
25
|
+
const location = options.location ?? infrastructureLocation(region);
|
|
26
|
+
if (!location)
|
|
27
|
+
throw new Error('Ecosystem activity requires a known infrastructure region or explicit location');
|
|
28
|
+
const post = async (path, data) => {
|
|
29
|
+
const token = await credential();
|
|
30
|
+
const response = await fetcher(new URL(path, baseURL), {
|
|
31
|
+
method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
|
|
32
|
+
body: JSON.stringify(data), signal: AbortSignal.timeout(4000),
|
|
33
|
+
});
|
|
34
|
+
if (!response.ok)
|
|
35
|
+
throw new Error('Activity collection failed');
|
|
36
|
+
};
|
|
37
|
+
const collector = createTrafficCollector(events => post('/internal/activity', events));
|
|
38
|
+
let stopped = false;
|
|
39
|
+
let heartbeatWork = Promise.resolve();
|
|
40
|
+
let heartbeatPending = false;
|
|
41
|
+
const reportError = options.onError ?? (() => console.warn('Ecosystem activity publisher unavailable'));
|
|
42
|
+
const heartbeat = (removed = false) => {
|
|
43
|
+
if (!removed && (stopped || heartbeatPending))
|
|
44
|
+
return heartbeatWork;
|
|
45
|
+
heartbeatPending = true;
|
|
46
|
+
heartbeatWork = heartbeatWork.then(() => post('/internal/activity/infrastructure', { instanceId, service: options.service, region, ...location, status: options.ready?.() ? 'online' : 'unknown', removed })).catch(reportError).finally(() => { heartbeatPending = false; });
|
|
47
|
+
return heartbeatWork;
|
|
48
|
+
};
|
|
49
|
+
const flush = () => collector.flush().catch(reportError);
|
|
50
|
+
const cleanups = new Set();
|
|
51
|
+
const sockets = new WeakMap();
|
|
52
|
+
let fetchInstalled = false;
|
|
53
|
+
const wrapFetch = (fetcher) => {
|
|
54
|
+
const wrapped = instrumentTrafficFetch(fetcher, collector, options.service, region, options.resolveEndpoint ?? resolveOxyServiceEndpoint);
|
|
55
|
+
return Object.assign((...args) => withoutNodeHttpObservation(() => wrapped(...args)), fetcher);
|
|
56
|
+
};
|
|
57
|
+
const observeHttp = trafficMiddleware(collector, options.service, region);
|
|
58
|
+
const timer = setInterval(() => { void flush(); }, 2000);
|
|
59
|
+
timer.unref?.();
|
|
60
|
+
void heartbeat();
|
|
61
|
+
const heartbeatTimer = setInterval(() => { void heartbeat(); }, 10000);
|
|
62
|
+
heartbeatTimer.unref?.();
|
|
63
|
+
return {
|
|
64
|
+
record: flow => collector.record({ ...flow, service: options.service, region }),
|
|
65
|
+
middleware: observeHttp,
|
|
66
|
+
observeHttp,
|
|
67
|
+
wrapFetch,
|
|
68
|
+
installFetch() {
|
|
69
|
+
if (fetchInstalled || stopped)
|
|
70
|
+
return;
|
|
71
|
+
fetchInstalled = true;
|
|
72
|
+
const original = globalThis.fetch;
|
|
73
|
+
const wrapped = wrapFetch(original);
|
|
74
|
+
globalThis.fetch = wrapped;
|
|
75
|
+
cleanups.add(() => { if (globalThis.fetch === wrapped)
|
|
76
|
+
globalThis.fetch = original; });
|
|
77
|
+
cleanups.add(observeNodeHttp(collector, options.service, region, options.resolveEndpoint ?? resolveOxyServiceEndpoint));
|
|
78
|
+
},
|
|
79
|
+
observeWebSocket(socket, socketOptions) {
|
|
80
|
+
const existing = sockets.get(socket);
|
|
81
|
+
if (existing)
|
|
82
|
+
return existing;
|
|
83
|
+
const unobserve = observeTrafficWebSocket(socket, collector, options.service, region, socketOptions);
|
|
84
|
+
const cleanup = () => { unobserve(); socket.off('close', cleanup); cleanups.delete(cleanup); sockets.delete(socket); };
|
|
85
|
+
sockets.set(socket, cleanup);
|
|
86
|
+
socket.once('close', cleanup);
|
|
87
|
+
cleanups.add(cleanup);
|
|
88
|
+
return cleanup;
|
|
89
|
+
},
|
|
90
|
+
observeSocket(socket, socketOptions) {
|
|
91
|
+
const existing = sockets.get(socket);
|
|
92
|
+
if (existing)
|
|
93
|
+
return existing;
|
|
94
|
+
const unobserve = observeTrafficSocket(socket, collector, options.service, region, socketOptions);
|
|
95
|
+
const cleanup = () => { unobserve(); socket.off('disconnect', cleanup); cleanups.delete(cleanup); sockets.delete(socket); };
|
|
96
|
+
sockets.set(socket, cleanup);
|
|
97
|
+
socket.once('disconnect', cleanup);
|
|
98
|
+
cleanups.add(cleanup);
|
|
99
|
+
return cleanup;
|
|
100
|
+
},
|
|
101
|
+
async stop() { if (stopped)
|
|
102
|
+
return; stopped = true; cleanups.forEach(cleanup => cleanup()); cleanups.clear(); clearInterval(timer); clearInterval(heartbeatTimer); await flush(); await heartbeat(true); },
|
|
103
|
+
};
|
|
104
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import http from 'node:http';
|
|
2
|
+
import https from 'node:https';
|
|
3
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
4
|
+
import { syncBuiltinESMExports } from 'node:module';
|
|
5
|
+
import { normalizeInfrastructureRegion, trafficType } from '@oxy.so/telemetry/collector';
|
|
6
|
+
const suppressed = new AsyncLocalStorage();
|
|
7
|
+
export function withoutNodeHttpObservation(callback) { return suppressed.run(true, callback); }
|
|
8
|
+
/** Node and Bun share the HTTP facade; Bun does not emit Node's HTTP diagnostic channels. */
|
|
9
|
+
export function observeNodeHttp(collector, service, region, resolveEndpoint) {
|
|
10
|
+
const seen = new WeakSet();
|
|
11
|
+
const restoreRequests = new Set();
|
|
12
|
+
const observe = (request) => {
|
|
13
|
+
if (seen.has(request) || suppressed.getStore())
|
|
14
|
+
return request;
|
|
15
|
+
seen.add(request);
|
|
16
|
+
const host = request.getHeader('host') ?? request.host;
|
|
17
|
+
if (typeof host !== 'string')
|
|
18
|
+
return request;
|
|
19
|
+
const url = new URL(request.path || '/', `${request.protocol || 'http:'}//${host}`);
|
|
20
|
+
if (/^\/(?:internal\/activity|auth\/service-token|platform-infrastructure)(?:\/|$)/.test(url.pathname))
|
|
21
|
+
return request;
|
|
22
|
+
const peer = resolveEndpoint(url);
|
|
23
|
+
if (peer && !request.headersSent)
|
|
24
|
+
request.setHeader('X-Oxy-Source-Region', region);
|
|
25
|
+
const flow = { service, region, activityType: trafficType(url.pathname), scope: peer ? 'internal' : 'external', direction: 'outbound', sourceRegion: region, sourceService: service, targetService: peer?.service };
|
|
26
|
+
const emit = request.emit;
|
|
27
|
+
let recorded = false;
|
|
28
|
+
const restore = () => { if (request.emit === observedEmit)
|
|
29
|
+
request.emit = emit; restoreRequests.delete(restore); };
|
|
30
|
+
// Intercept notifications without adding an 'error' listener: observation
|
|
31
|
+
// must not turn an unhandled application error into a swallowed error.
|
|
32
|
+
const observedEmit = function (event, ...args) {
|
|
33
|
+
try {
|
|
34
|
+
if (!recorded && event === 'response') {
|
|
35
|
+
recorded = true;
|
|
36
|
+
const response = args[0];
|
|
37
|
+
const targetRegion = peer ? normalizeInfrastructureRegion(response.headers['x-oxy-region']) : undefined;
|
|
38
|
+
collector.record({ ...flow, targetRegion });
|
|
39
|
+
collector.record({ ...flow, direction: 'inbound', sourceRegion: targetRegion, targetRegion: region, sourceService: peer?.service, targetService: service });
|
|
40
|
+
}
|
|
41
|
+
else if (!recorded && (event === 'error' || event === 'close')) {
|
|
42
|
+
recorded = true;
|
|
43
|
+
collector.record(flow);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
catch { /* A metrics failure cannot change a request's behavior. */ }
|
|
47
|
+
if (event === 'close')
|
|
48
|
+
restore();
|
|
49
|
+
return emit.call(this, event, ...args);
|
|
50
|
+
};
|
|
51
|
+
request.emit = observedEmit;
|
|
52
|
+
restoreRequests.add(restore);
|
|
53
|
+
return request;
|
|
54
|
+
};
|
|
55
|
+
const safelyObserve = (request) => {
|
|
56
|
+
try {
|
|
57
|
+
return observe(request);
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
return request;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
const patches = [http, https].map(module => {
|
|
64
|
+
const request = module.request;
|
|
65
|
+
const get = module.get;
|
|
66
|
+
const wrappedRequest = function (...args) { return safelyObserve(request.apply(module, args)); };
|
|
67
|
+
const wrappedGet = function (...args) { return safelyObserve(get.apply(module, args)); };
|
|
68
|
+
module.request = wrappedRequest;
|
|
69
|
+
module.get = wrappedGet;
|
|
70
|
+
return () => {
|
|
71
|
+
if (module.request === wrappedRequest)
|
|
72
|
+
module.request = request;
|
|
73
|
+
if (module.get === wrappedGet)
|
|
74
|
+
module.get = get;
|
|
75
|
+
};
|
|
76
|
+
});
|
|
77
|
+
syncBuiltinESMExports();
|
|
78
|
+
return () => { patches.forEach(restore => restore()); restoreRequests.forEach(restore => restore()); syncBuiltinESMExports(); };
|
|
79
|
+
}
|
|
@@ -83,7 +83,7 @@ const LEADING_HORIZONTAL_WHITESPACE = /\n +/g;
|
|
|
83
83
|
/** Three or more line breaks: more than one blank line between paragraphs. */
|
|
84
84
|
const EXCESS_BLANK_LINES = /\n{3,}/g;
|
|
85
85
|
/**
|
|
86
|
-
* Normalize a SINGLE-LINE text value
|
|
86
|
+
* Normalize a SINGLE-LINE text value such as a page title or `siteName`,
|
|
87
87
|
* display names, image alt text, handles, profile field labels.
|
|
88
88
|
*
|
|
89
89
|
* A line break in such a value is never meaningful — it is an artifact of the
|