apitally 0.21.2 → 0.21.4
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/adonisjs/configure.cjs +6 -21
- package/dist/adonisjs/configure.cjs.map +1 -1
- package/dist/adonisjs/configure.js +3 -18
- package/dist/adonisjs/configure.js.map +1 -1
- package/dist/adonisjs/index.cjs +2 -70
- package/dist/adonisjs/index.cjs.map +1 -1
- package/dist/adonisjs/index.js +1 -66
- package/dist/adonisjs/index.js.map +1 -1
- package/dist/adonisjs/middleware.cjs +11 -116
- package/dist/adonisjs/middleware.cjs.map +1 -1
- package/dist/adonisjs/middleware.js +6 -101
- package/dist/adonisjs/middleware.js.map +1 -1
- package/dist/adonisjs/provider.cjs +15 -5663
- package/dist/adonisjs/provider.cjs.map +1 -1
- package/dist/adonisjs/provider.js +7 -5671
- package/dist/adonisjs/provider.js.map +1 -1
- package/dist/common/client.cjs +39 -800
- package/dist/common/client.cjs.map +1 -1
- package/dist/common/client.d.cts +1 -1
- package/dist/common/client.d.ts +1 -1
- package/dist/common/client.js +33 -794
- package/dist/common/client.js.map +1 -1
- package/dist/common/consumerRegistry.cjs +3 -5
- package/dist/common/consumerRegistry.cjs.map +1 -1
- package/dist/common/consumerRegistry.js +3 -5
- package/dist/common/consumerRegistry.js.map +1 -1
- package/dist/common/headers.cjs +0 -2
- package/dist/common/headers.cjs.map +1 -1
- package/dist/common/headers.js +0 -2
- package/dist/common/headers.js.map +1 -1
- package/dist/common/logging.cjs +0 -2
- package/dist/common/logging.cjs.map +1 -1
- package/dist/common/logging.js +0 -2
- package/dist/common/logging.js.map +1 -1
- package/dist/common/packageVersions.cjs +2 -4
- package/dist/common/packageVersions.cjs.map +1 -1
- package/dist/common/packageVersions.js +2 -10
- package/dist/common/packageVersions.js.map +1 -1
- package/dist/common/paramValidation.cjs +0 -2
- package/dist/common/paramValidation.cjs.map +1 -1
- package/dist/common/paramValidation.js +0 -2
- package/dist/common/paramValidation.js.map +1 -1
- package/dist/common/requestCounter.cjs +2 -4
- package/dist/common/requestCounter.cjs.map +1 -1
- package/dist/common/requestCounter.js +2 -4
- package/dist/common/requestCounter.js.map +1 -1
- package/dist/common/requestLogger.cjs +39 -162
- package/dist/common/requestLogger.cjs.map +1 -1
- package/dist/common/requestLogger.js +33 -156
- package/dist/common/requestLogger.js.map +1 -1
- package/dist/common/response.cjs +21 -4
- package/dist/common/response.cjs.map +1 -1
- package/dist/common/response.d.cts +2 -1
- package/dist/common/response.d.ts +2 -1
- package/dist/common/response.js +19 -3
- package/dist/common/response.js.map +1 -1
- package/dist/common/sentry.cjs +1 -3
- package/dist/common/sentry.cjs.map +1 -1
- package/dist/common/sentry.js +1 -3
- package/dist/common/sentry.js.map +1 -1
- package/dist/common/serverErrorCounter.cjs +7 -36
- package/dist/common/serverErrorCounter.cjs.map +1 -1
- package/dist/common/serverErrorCounter.js +6 -25
- package/dist/common/serverErrorCounter.js.map +1 -1
- package/dist/common/tempGzipFile.cjs +9 -11
- package/dist/common/tempGzipFile.cjs.map +1 -1
- package/dist/common/tempGzipFile.js +9 -11
- package/dist/common/tempGzipFile.js.map +1 -1
- package/dist/common/types.cjs +0 -2
- package/dist/common/types.cjs.map +1 -1
- package/dist/common/validationErrorCounter.cjs +3 -5
- package/dist/common/validationErrorCounter.cjs.map +1 -1
- package/dist/common/validationErrorCounter.js +3 -5
- package/dist/common/validationErrorCounter.js.map +1 -1
- package/dist/elysia/index.cjs +2 -1549
- package/dist/elysia/index.cjs.map +1 -1
- package/dist/elysia/index.js +2 -1553
- package/dist/elysia/index.js.map +1 -1
- package/dist/elysia/plugin.cjs +33 -1415
- package/dist/elysia/plugin.cjs.map +1 -1
- package/dist/elysia/plugin.js +22 -1399
- package/dist/elysia/plugin.js.map +1 -1
- package/dist/elysia/utils.cjs +3 -24
- package/dist/elysia/utils.cjs.map +1 -1
- package/dist/elysia/utils.js +1 -25
- package/dist/elysia/utils.js.map +1 -1
- package/dist/express/index.cjs +3 -6348
- package/dist/express/index.cjs.map +1 -1
- package/dist/express/index.js +1 -6361
- package/dist/express/index.js.map +1 -1
- package/dist/express/middleware.cjs +39 -6065
- package/dist/express/middleware.cjs.map +1 -1
- package/dist/express/middleware.js +14 -6056
- package/dist/express/middleware.js.map +1 -1
- package/dist/express/utils.cjs +17 -19
- package/dist/express/utils.cjs.map +1 -1
- package/dist/express/utils.js +17 -19
- package/dist/express/utils.js.map +1 -1
- package/dist/fastify/index.cjs +3 -6118
- package/dist/fastify/index.cjs.map +1 -1
- package/dist/fastify/index.js +2 -6142
- package/dist/fastify/index.js.map +1 -1
- package/dist/fastify/plugin.cjs +36 -5884
- package/dist/fastify/plugin.cjs.map +1 -1
- package/dist/fastify/plugin.js +14 -5888
- package/dist/fastify/plugin.js.map +1 -1
- package/dist/h3/index.cjs +3 -1539
- package/dist/h3/index.cjs.map +1 -1
- package/dist/h3/index.js +1 -1531
- package/dist/h3/index.js.map +1 -1
- package/dist/h3/plugin.cjs +28 -1418
- package/dist/h3/plugin.cjs.map +1 -1
- package/dist/h3/plugin.js +16 -1401
- package/dist/h3/plugin.js.map +1 -1
- package/dist/h3/utils.cjs +3 -24
- package/dist/h3/utils.cjs.map +1 -1
- package/dist/h3/utils.js +1 -25
- package/dist/h3/utils.js.map +1 -1
- package/dist/hapi/index.cjs +3 -5982
- package/dist/hapi/index.cjs.map +1 -1
- package/dist/hapi/index.js +2 -6006
- package/dist/hapi/index.js.map +1 -1
- package/dist/hapi/plugin.cjs +28 -5842
- package/dist/hapi/plugin.cjs.map +1 -1
- package/dist/hapi/plugin.js +16 -5846
- package/dist/hapi/plugin.js.map +1 -1
- package/dist/hapi/utils.cjs +3 -24
- package/dist/hapi/utils.cjs.map +1 -1
- package/dist/hapi/utils.js +1 -25
- package/dist/hapi/utils.js.map +1 -1
- package/dist/hono/index.cjs +3 -1565
- package/dist/hono/index.cjs.map +1 -1
- package/dist/hono/index.js +1 -1557
- package/dist/hono/index.js.map +1 -1
- package/dist/hono/middleware.cjs +22 -1455
- package/dist/hono/middleware.cjs.map +1 -1
- package/dist/hono/middleware.js +10 -1438
- package/dist/hono/middleware.js.map +1 -1
- package/dist/hono/utils.cjs +3 -24
- package/dist/hono/utils.cjs.map +1 -1
- package/dist/hono/utils.js +1 -27
- package/dist/hono/utils.js.map +1 -1
- package/dist/koa/index.cjs +3 -1478
- package/dist/koa/index.cjs.map +1 -1
- package/dist/koa/index.js +1 -1470
- package/dist/koa/index.js.map +1 -1
- package/dist/koa/middleware.cjs +20 -1308
- package/dist/koa/middleware.cjs.map +1 -1
- package/dist/koa/middleware.js +9 -1292
- package/dist/koa/middleware.js.map +1 -1
- package/dist/loggers/console.cjs +4 -6
- package/dist/loggers/console.cjs.map +1 -1
- package/dist/loggers/console.js +4 -6
- package/dist/loggers/console.js.map +1 -1
- package/dist/loggers/hapi.cjs +3 -5
- package/dist/loggers/hapi.cjs.map +1 -1
- package/dist/loggers/hapi.js +3 -5
- package/dist/loggers/hapi.js.map +1 -1
- package/dist/loggers/index.cjs +10 -4732
- package/dist/loggers/index.cjs.map +1 -1
- package/dist/loggers/index.js +5 -4743
- package/dist/loggers/index.js.map +1 -1
- package/dist/loggers/nestjs.cjs +5 -39
- package/dist/loggers/nestjs.cjs.map +1 -1
- package/dist/loggers/nestjs.js +4 -36
- package/dist/loggers/nestjs.js.map +1 -1
- package/dist/loggers/pino.cjs +29 -4448
- package/dist/loggers/pino.cjs.map +1 -1
- package/dist/loggers/pino.js +7 -4451
- package/dist/loggers/pino.js.map +1 -1
- package/dist/loggers/utils.cjs +1 -3
- package/dist/loggers/utils.cjs.map +1 -1
- package/dist/loggers/utils.js +1 -3
- package/dist/loggers/utils.js.map +1 -1
- package/dist/loggers/winston.cjs +6 -44
- package/dist/loggers/winston.cjs.map +1 -1
- package/dist/loggers/winston.js +4 -40
- package/dist/loggers/winston.js.map +1 -1
- package/dist/nestjs/index.cjs +17 -17779
- package/dist/nestjs/index.cjs.map +1 -1
- package/dist/nestjs/index.js +20 -17807
- package/dist/nestjs/index.js.map +1 -1
- package/package.json +1 -1
package/dist/koa/middleware.js
CHANGED
|
@@ -1,1295 +1,11 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
11
|
-
|
|
12
|
-
// src/koa/middleware.ts
|
|
13
|
-
import { AsyncLocalStorage } from "async_hooks";
|
|
14
|
-
|
|
15
|
-
// src/common/client.ts
|
|
16
|
-
import fetchRetry from "fetch-retry";
|
|
17
|
-
import { randomUUID as randomUUID3 } from "crypto";
|
|
18
|
-
|
|
19
|
-
// src/common/consumerRegistry.ts
|
|
20
|
-
var consumerFromStringOrObject = /* @__PURE__ */ __name((consumer) => {
|
|
21
|
-
var _a2, _b;
|
|
22
|
-
if (typeof consumer === "string") {
|
|
23
|
-
consumer = String(consumer).trim().substring(0, 128);
|
|
24
|
-
return consumer ? {
|
|
25
|
-
identifier: consumer
|
|
26
|
-
} : null;
|
|
27
|
-
} else {
|
|
28
|
-
consumer.identifier = String(consumer.identifier).trim().substring(0, 128);
|
|
29
|
-
consumer.name = (_a2 = consumer.name) == null ? void 0 : _a2.trim().substring(0, 64);
|
|
30
|
-
consumer.group = (_b = consumer.group) == null ? void 0 : _b.trim().substring(0, 64);
|
|
31
|
-
return consumer.identifier ? consumer : null;
|
|
32
|
-
}
|
|
33
|
-
}, "consumerFromStringOrObject");
|
|
34
|
-
var _ConsumerRegistry = class _ConsumerRegistry {
|
|
35
|
-
consumers;
|
|
36
|
-
updated;
|
|
37
|
-
constructor() {
|
|
38
|
-
this.consumers = /* @__PURE__ */ new Map();
|
|
39
|
-
this.updated = /* @__PURE__ */ new Set();
|
|
40
|
-
}
|
|
41
|
-
addOrUpdateConsumer(consumer) {
|
|
42
|
-
if (!consumer || !consumer.name && !consumer.group) {
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
const existing = this.consumers.get(consumer.identifier);
|
|
46
|
-
if (!existing) {
|
|
47
|
-
this.consumers.set(consumer.identifier, consumer);
|
|
48
|
-
this.updated.add(consumer.identifier);
|
|
49
|
-
} else {
|
|
50
|
-
if (consumer.name && consumer.name !== existing.name) {
|
|
51
|
-
existing.name = consumer.name;
|
|
52
|
-
this.updated.add(consumer.identifier);
|
|
53
|
-
}
|
|
54
|
-
if (consumer.group && consumer.group !== existing.group) {
|
|
55
|
-
existing.group = consumer.group;
|
|
56
|
-
this.updated.add(consumer.identifier);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
getAndResetUpdatedConsumers() {
|
|
61
|
-
const data = [];
|
|
62
|
-
this.updated.forEach((identifier) => {
|
|
63
|
-
const consumer = this.consumers.get(identifier);
|
|
64
|
-
if (consumer) {
|
|
65
|
-
data.push(consumer);
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
this.updated.clear();
|
|
69
|
-
return data;
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
__name(_ConsumerRegistry, "ConsumerRegistry");
|
|
73
|
-
var ConsumerRegistry = _ConsumerRegistry;
|
|
74
|
-
|
|
75
|
-
// src/common/logging.ts
|
|
76
|
-
import { createLogger, format, transports } from "winston";
|
|
77
|
-
function getLogger() {
|
|
78
|
-
return createLogger({
|
|
79
|
-
level: process.env.APITALLY_DEBUG ? "debug" : "warn",
|
|
80
|
-
format: format.combine(format.colorize(), format.timestamp(), format.printf((info) => `${info.timestamp} ${info.level}: ${info.message}`)),
|
|
81
|
-
transports: [
|
|
82
|
-
new transports.Console()
|
|
83
|
-
]
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
__name(getLogger, "getLogger");
|
|
87
|
-
|
|
88
|
-
// src/common/paramValidation.ts
|
|
89
|
-
function isValidClientId(clientId) {
|
|
90
|
-
const regexExp = /^[0-9a-f]{8}-[0-9a-f]{4}-[4][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
91
|
-
return regexExp.test(clientId);
|
|
92
|
-
}
|
|
93
|
-
__name(isValidClientId, "isValidClientId");
|
|
94
|
-
function isValidEnv(env) {
|
|
95
|
-
const regexExp = /^[\w-]{1,32}$/;
|
|
96
|
-
return regexExp.test(env);
|
|
97
|
-
}
|
|
98
|
-
__name(isValidEnv, "isValidEnv");
|
|
99
|
-
|
|
100
|
-
// src/common/requestCounter.ts
|
|
101
|
-
var _RequestCounter = class _RequestCounter {
|
|
102
|
-
requestCounts;
|
|
103
|
-
requestSizeSums;
|
|
104
|
-
responseSizeSums;
|
|
105
|
-
responseTimes;
|
|
106
|
-
requestSizes;
|
|
107
|
-
responseSizes;
|
|
108
|
-
constructor() {
|
|
109
|
-
this.requestCounts = /* @__PURE__ */ new Map();
|
|
110
|
-
this.requestSizeSums = /* @__PURE__ */ new Map();
|
|
111
|
-
this.responseSizeSums = /* @__PURE__ */ new Map();
|
|
112
|
-
this.responseTimes = /* @__PURE__ */ new Map();
|
|
113
|
-
this.requestSizes = /* @__PURE__ */ new Map();
|
|
114
|
-
this.responseSizes = /* @__PURE__ */ new Map();
|
|
115
|
-
}
|
|
116
|
-
getKey(requestInfo) {
|
|
117
|
-
return [
|
|
118
|
-
requestInfo.consumer || "",
|
|
119
|
-
requestInfo.method.toUpperCase(),
|
|
120
|
-
requestInfo.path,
|
|
121
|
-
requestInfo.statusCode
|
|
122
|
-
].join("|");
|
|
123
|
-
}
|
|
124
|
-
addRequest(requestInfo) {
|
|
125
|
-
const key = this.getKey(requestInfo);
|
|
126
|
-
this.requestCounts.set(key, (this.requestCounts.get(key) || 0) + 1);
|
|
127
|
-
if (!this.responseTimes.has(key)) {
|
|
128
|
-
this.responseTimes.set(key, /* @__PURE__ */ new Map());
|
|
129
|
-
}
|
|
130
|
-
const responseTimeMap = this.responseTimes.get(key);
|
|
131
|
-
const responseTimeMsBin = Math.floor(requestInfo.responseTime / 10) * 10;
|
|
132
|
-
responseTimeMap.set(responseTimeMsBin, (responseTimeMap.get(responseTimeMsBin) || 0) + 1);
|
|
133
|
-
if (requestInfo.requestSize !== void 0) {
|
|
134
|
-
requestInfo.requestSize = Number(requestInfo.requestSize);
|
|
135
|
-
this.requestSizeSums.set(key, (this.requestSizeSums.get(key) || 0) + requestInfo.requestSize);
|
|
136
|
-
if (!this.requestSizes.has(key)) {
|
|
137
|
-
this.requestSizes.set(key, /* @__PURE__ */ new Map());
|
|
138
|
-
}
|
|
139
|
-
const requestSizeMap = this.requestSizes.get(key);
|
|
140
|
-
const requestSizeKbBin = Math.floor(requestInfo.requestSize / 1e3);
|
|
141
|
-
requestSizeMap.set(requestSizeKbBin, (requestSizeMap.get(requestSizeKbBin) || 0) + 1);
|
|
142
|
-
}
|
|
143
|
-
if (requestInfo.responseSize !== void 0) {
|
|
144
|
-
requestInfo.responseSize = Number(requestInfo.responseSize);
|
|
145
|
-
this.responseSizeSums.set(key, (this.responseSizeSums.get(key) || 0) + requestInfo.responseSize);
|
|
146
|
-
if (!this.responseSizes.has(key)) {
|
|
147
|
-
this.responseSizes.set(key, /* @__PURE__ */ new Map());
|
|
148
|
-
}
|
|
149
|
-
const responseSizeMap = this.responseSizes.get(key);
|
|
150
|
-
const responseSizeKbBin = Math.floor(requestInfo.responseSize / 1e3);
|
|
151
|
-
responseSizeMap.set(responseSizeKbBin, (responseSizeMap.get(responseSizeKbBin) || 0) + 1);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
getAndResetRequests() {
|
|
155
|
-
const data = [];
|
|
156
|
-
this.requestCounts.forEach((count, key) => {
|
|
157
|
-
const [consumer, method, path, statusCodeStr] = key.split("|");
|
|
158
|
-
const responseTimes = this.responseTimes.get(key) || /* @__PURE__ */ new Map();
|
|
159
|
-
const requestSizes = this.requestSizes.get(key) || /* @__PURE__ */ new Map();
|
|
160
|
-
const responseSizes = this.responseSizes.get(key) || /* @__PURE__ */ new Map();
|
|
161
|
-
data.push({
|
|
162
|
-
consumer: consumer || null,
|
|
163
|
-
method,
|
|
164
|
-
path,
|
|
165
|
-
status_code: parseInt(statusCodeStr),
|
|
166
|
-
request_count: count,
|
|
167
|
-
request_size_sum: this.requestSizeSums.get(key) || 0,
|
|
168
|
-
response_size_sum: this.responseSizeSums.get(key) || 0,
|
|
169
|
-
response_times: Object.fromEntries(responseTimes),
|
|
170
|
-
request_sizes: Object.fromEntries(requestSizes),
|
|
171
|
-
response_sizes: Object.fromEntries(responseSizes)
|
|
172
|
-
});
|
|
173
|
-
});
|
|
174
|
-
this.requestCounts.clear();
|
|
175
|
-
this.requestSizeSums.clear();
|
|
176
|
-
this.responseSizeSums.clear();
|
|
177
|
-
this.responseTimes.clear();
|
|
178
|
-
this.requestSizes.clear();
|
|
179
|
-
this.responseSizes.clear();
|
|
180
|
-
return data;
|
|
181
|
-
}
|
|
182
|
-
};
|
|
183
|
-
__name(_RequestCounter, "RequestCounter");
|
|
184
|
-
var RequestCounter = _RequestCounter;
|
|
185
|
-
|
|
186
|
-
// src/common/requestLogger.ts
|
|
187
|
-
import AsyncLock from "async-lock";
|
|
188
|
-
import { Buffer as Buffer3 } from "buffer";
|
|
189
|
-
import { randomUUID as randomUUID2 } from "crypto";
|
|
190
|
-
import { unlinkSync, writeFileSync } from "fs";
|
|
191
|
-
import { tmpdir as tmpdir2 } from "os";
|
|
192
|
-
import { join as join2 } from "path";
|
|
193
|
-
|
|
194
|
-
// src/common/sentry.ts
|
|
195
|
-
var sentry;
|
|
196
|
-
(async () => {
|
|
197
|
-
try {
|
|
198
|
-
sentry = await import("@sentry/node");
|
|
199
|
-
} catch (e) {
|
|
200
|
-
}
|
|
201
|
-
})();
|
|
202
|
-
function getSentryEventId() {
|
|
203
|
-
if (sentry && sentry.lastEventId) {
|
|
204
|
-
return sentry.lastEventId();
|
|
205
|
-
}
|
|
206
|
-
return void 0;
|
|
207
|
-
}
|
|
208
|
-
__name(getSentryEventId, "getSentryEventId");
|
|
209
|
-
|
|
210
|
-
// src/common/serverErrorCounter.ts
|
|
211
|
-
import { createHash } from "crypto";
|
|
212
|
-
var MAX_MSG_LENGTH = 2048;
|
|
213
|
-
var MAX_STACKTRACE_LENGTH = 65536;
|
|
214
|
-
var _ServerErrorCounter = class _ServerErrorCounter {
|
|
215
|
-
errorCounts;
|
|
216
|
-
errorDetails;
|
|
217
|
-
sentryEventIds;
|
|
218
|
-
constructor() {
|
|
219
|
-
this.errorCounts = /* @__PURE__ */ new Map();
|
|
220
|
-
this.errorDetails = /* @__PURE__ */ new Map();
|
|
221
|
-
this.sentryEventIds = /* @__PURE__ */ new Map();
|
|
222
|
-
}
|
|
223
|
-
addServerError(serverError) {
|
|
224
|
-
const key = this.getKey(serverError);
|
|
225
|
-
if (!this.errorDetails.has(key)) {
|
|
226
|
-
this.errorDetails.set(key, serverError);
|
|
227
|
-
}
|
|
228
|
-
this.errorCounts.set(key, (this.errorCounts.get(key) || 0) + 1);
|
|
229
|
-
const sentryEventId = getSentryEventId();
|
|
230
|
-
if (sentryEventId) {
|
|
231
|
-
this.sentryEventIds.set(key, sentryEventId);
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
getAndResetServerErrors() {
|
|
235
|
-
const data = [];
|
|
236
|
-
this.errorCounts.forEach((count, key) => {
|
|
237
|
-
const serverError = this.errorDetails.get(key);
|
|
238
|
-
if (serverError) {
|
|
239
|
-
data.push({
|
|
240
|
-
consumer: serverError.consumer || null,
|
|
241
|
-
method: serverError.method,
|
|
242
|
-
path: serverError.path,
|
|
243
|
-
type: serverError.type,
|
|
244
|
-
msg: truncateExceptionMessage(serverError.msg),
|
|
245
|
-
traceback: truncateExceptionStackTrace(serverError.traceback),
|
|
246
|
-
sentry_event_id: this.sentryEventIds.get(key) || null,
|
|
247
|
-
error_count: count
|
|
248
|
-
});
|
|
249
|
-
}
|
|
250
|
-
});
|
|
251
|
-
this.errorCounts.clear();
|
|
252
|
-
this.errorDetails.clear();
|
|
253
|
-
this.sentryEventIds.clear();
|
|
254
|
-
return data;
|
|
255
|
-
}
|
|
256
|
-
getKey(serverError) {
|
|
257
|
-
const hashInput = [
|
|
258
|
-
serverError.consumer || "",
|
|
259
|
-
serverError.method.toUpperCase(),
|
|
260
|
-
serverError.path,
|
|
261
|
-
serverError.type,
|
|
262
|
-
serverError.msg.trim(),
|
|
263
|
-
serverError.traceback.trim()
|
|
264
|
-
].join("|");
|
|
265
|
-
return createHash("md5").update(hashInput).digest("hex");
|
|
266
|
-
}
|
|
267
|
-
};
|
|
268
|
-
__name(_ServerErrorCounter, "ServerErrorCounter");
|
|
269
|
-
var ServerErrorCounter = _ServerErrorCounter;
|
|
270
|
-
function truncateExceptionMessage(msg) {
|
|
271
|
-
if (msg.length <= MAX_MSG_LENGTH) {
|
|
272
|
-
return msg;
|
|
273
|
-
}
|
|
274
|
-
const suffix = "... (truncated)";
|
|
275
|
-
const cutoff = MAX_MSG_LENGTH - suffix.length;
|
|
276
|
-
return msg.substring(0, cutoff) + suffix;
|
|
277
|
-
}
|
|
278
|
-
__name(truncateExceptionMessage, "truncateExceptionMessage");
|
|
279
|
-
function truncateExceptionStackTrace(stack) {
|
|
280
|
-
const suffix = "... (truncated) ...";
|
|
281
|
-
const cutoff = MAX_STACKTRACE_LENGTH - suffix.length;
|
|
282
|
-
const lines = stack.trim().split("\n");
|
|
283
|
-
const truncatedLines = [];
|
|
284
|
-
let length = 0;
|
|
285
|
-
for (const line of lines) {
|
|
286
|
-
if (length + line.length + 1 > cutoff) {
|
|
287
|
-
truncatedLines.push(suffix);
|
|
288
|
-
break;
|
|
289
|
-
}
|
|
290
|
-
truncatedLines.push(line);
|
|
291
|
-
length += line.length + 1;
|
|
292
|
-
}
|
|
293
|
-
return truncatedLines.join("\n");
|
|
294
|
-
}
|
|
295
|
-
__name(truncateExceptionStackTrace, "truncateExceptionStackTrace");
|
|
296
|
-
|
|
297
|
-
// src/common/tempGzipFile.ts
|
|
298
|
-
import { Buffer as Buffer2 } from "buffer";
|
|
299
|
-
import { randomUUID } from "crypto";
|
|
300
|
-
import { createWriteStream, readFile } from "fs";
|
|
301
|
-
import { unlink } from "fs/promises";
|
|
302
|
-
import { tmpdir } from "os";
|
|
303
|
-
import { join } from "path";
|
|
304
|
-
import { createGzip } from "zlib";
|
|
305
|
-
var _TempGzipFile = class _TempGzipFile {
|
|
306
|
-
uuid;
|
|
307
|
-
filePath;
|
|
308
|
-
gzip;
|
|
309
|
-
writeStream;
|
|
310
|
-
readyPromise;
|
|
311
|
-
closedPromise;
|
|
312
|
-
constructor() {
|
|
313
|
-
this.uuid = randomUUID();
|
|
314
|
-
this.filePath = join(tmpdir(), `apitally-${this.uuid}.gz`);
|
|
315
|
-
this.writeStream = createWriteStream(this.filePath);
|
|
316
|
-
this.readyPromise = new Promise((resolve, reject) => {
|
|
317
|
-
this.writeStream.once("ready", resolve);
|
|
318
|
-
this.writeStream.once("error", reject);
|
|
319
|
-
});
|
|
320
|
-
this.closedPromise = new Promise((resolve, reject) => {
|
|
321
|
-
this.writeStream.once("close", resolve);
|
|
322
|
-
this.writeStream.once("error", reject);
|
|
323
|
-
});
|
|
324
|
-
this.gzip = createGzip();
|
|
325
|
-
this.gzip.pipe(this.writeStream);
|
|
326
|
-
}
|
|
327
|
-
get size() {
|
|
328
|
-
return this.writeStream.bytesWritten;
|
|
329
|
-
}
|
|
330
|
-
async writeLine(data) {
|
|
331
|
-
await this.readyPromise;
|
|
332
|
-
return new Promise((resolve, reject) => {
|
|
333
|
-
this.gzip.write(Buffer2.concat([
|
|
334
|
-
data,
|
|
335
|
-
Buffer2.from("\n")
|
|
336
|
-
]), (error) => {
|
|
337
|
-
if (error) {
|
|
338
|
-
reject(error);
|
|
339
|
-
} else {
|
|
340
|
-
resolve();
|
|
341
|
-
}
|
|
342
|
-
});
|
|
343
|
-
});
|
|
344
|
-
}
|
|
345
|
-
async getContent() {
|
|
346
|
-
return new Promise((resolve, reject) => {
|
|
347
|
-
readFile(this.filePath, (error, data) => {
|
|
348
|
-
if (error) {
|
|
349
|
-
reject(error);
|
|
350
|
-
} else {
|
|
351
|
-
resolve(data);
|
|
352
|
-
}
|
|
353
|
-
});
|
|
354
|
-
});
|
|
355
|
-
}
|
|
356
|
-
async close() {
|
|
357
|
-
await new Promise((resolve) => {
|
|
358
|
-
this.gzip.end(() => {
|
|
359
|
-
resolve();
|
|
360
|
-
});
|
|
361
|
-
});
|
|
362
|
-
await this.closedPromise;
|
|
363
|
-
}
|
|
364
|
-
async delete() {
|
|
365
|
-
await this.close();
|
|
366
|
-
await unlink(this.filePath);
|
|
367
|
-
}
|
|
368
|
-
};
|
|
369
|
-
__name(_TempGzipFile, "TempGzipFile");
|
|
370
|
-
var TempGzipFile = _TempGzipFile;
|
|
371
|
-
|
|
372
|
-
// src/common/requestLogger.ts
|
|
373
|
-
var MAX_BODY_SIZE = 5e4;
|
|
374
|
-
var MAX_FILE_SIZE = 1e6;
|
|
375
|
-
var MAX_FILES = 50;
|
|
376
|
-
var MAX_PENDING_WRITES = 100;
|
|
377
|
-
var MAX_LOG_MSG_LENGTH = 2048;
|
|
378
|
-
var BODY_TOO_LARGE = Buffer3.from("<body too large>");
|
|
379
|
-
var BODY_MASKED = Buffer3.from("<masked>");
|
|
380
|
-
var MASKED = "******";
|
|
381
|
-
var ALLOWED_CONTENT_TYPES = [
|
|
382
|
-
"application/json",
|
|
383
|
-
"application/problem+json",
|
|
384
|
-
"application/vnd.api+json",
|
|
385
|
-
"text/plain",
|
|
386
|
-
"text/html"
|
|
387
|
-
];
|
|
388
|
-
var EXCLUDE_PATH_PATTERNS = [
|
|
389
|
-
/\/_?healthz?$/i,
|
|
390
|
-
/\/_?health[_-]?checks?$/i,
|
|
391
|
-
/\/_?heart[_-]?beats?$/i,
|
|
392
|
-
/\/ping$/i,
|
|
393
|
-
/\/ready$/i,
|
|
394
|
-
/\/live$/i
|
|
395
|
-
];
|
|
396
|
-
var EXCLUDE_USER_AGENT_PATTERNS = [
|
|
397
|
-
/health[-_ ]?check/i,
|
|
398
|
-
/microsoft-azure-application-lb/i,
|
|
399
|
-
/googlehc/i,
|
|
400
|
-
/kube-probe/i
|
|
401
|
-
];
|
|
402
|
-
var MASK_QUERY_PARAM_PATTERNS = [
|
|
403
|
-
/auth/i,
|
|
404
|
-
/api-?key/i,
|
|
405
|
-
/secret/i,
|
|
406
|
-
/token/i,
|
|
407
|
-
/password/i,
|
|
408
|
-
/pwd/i
|
|
409
|
-
];
|
|
410
|
-
var MASK_HEADER_PATTERNS = [
|
|
411
|
-
/auth/i,
|
|
412
|
-
/api-?key/i,
|
|
413
|
-
/secret/i,
|
|
414
|
-
/token/i,
|
|
415
|
-
/cookie/i
|
|
416
|
-
];
|
|
417
|
-
var MASK_BODY_FIELD_PATTERNS = [
|
|
418
|
-
/password/i,
|
|
419
|
-
/pwd/i,
|
|
420
|
-
/token/i,
|
|
421
|
-
/secret/i,
|
|
422
|
-
/auth/i,
|
|
423
|
-
/card[-_ ]?number/i,
|
|
424
|
-
/ccv/i,
|
|
425
|
-
/ssn/i
|
|
426
|
-
];
|
|
427
|
-
var DEFAULT_CONFIG = {
|
|
428
|
-
enabled: false,
|
|
429
|
-
logQueryParams: true,
|
|
430
|
-
logRequestHeaders: false,
|
|
431
|
-
logRequestBody: false,
|
|
432
|
-
logResponseHeaders: true,
|
|
433
|
-
logResponseBody: false,
|
|
434
|
-
logException: true,
|
|
435
|
-
captureLogs: false,
|
|
436
|
-
maskQueryParams: [],
|
|
437
|
-
maskHeaders: [],
|
|
438
|
-
maskBodyFields: [],
|
|
439
|
-
excludePaths: []
|
|
440
|
-
};
|
|
441
|
-
var _RequestLogger = class _RequestLogger {
|
|
442
|
-
config;
|
|
443
|
-
enabled;
|
|
444
|
-
suspendUntil = null;
|
|
445
|
-
pendingWrites = [];
|
|
446
|
-
currentFile = null;
|
|
447
|
-
files = [];
|
|
448
|
-
maintainIntervalId;
|
|
449
|
-
lock = new AsyncLock();
|
|
450
|
-
constructor(config) {
|
|
451
|
-
this.config = {
|
|
452
|
-
...DEFAULT_CONFIG,
|
|
453
|
-
...config
|
|
454
|
-
};
|
|
455
|
-
this.enabled = this.config.enabled && checkWritableFs();
|
|
456
|
-
if (this.enabled) {
|
|
457
|
-
this.maintainIntervalId = setInterval(() => {
|
|
458
|
-
this.maintain();
|
|
459
|
-
}, 1e3);
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
get maxBodySize() {
|
|
463
|
-
return MAX_BODY_SIZE;
|
|
464
|
-
}
|
|
465
|
-
shouldExcludePath(urlPath) {
|
|
466
|
-
const patterns = [
|
|
467
|
-
...this.config.excludePaths,
|
|
468
|
-
...EXCLUDE_PATH_PATTERNS
|
|
469
|
-
];
|
|
470
|
-
return matchPatterns(urlPath, patterns);
|
|
471
|
-
}
|
|
472
|
-
shouldExcludeUserAgent(userAgent) {
|
|
473
|
-
return userAgent ? matchPatterns(userAgent, EXCLUDE_USER_AGENT_PATTERNS) : false;
|
|
474
|
-
}
|
|
475
|
-
shouldMaskQueryParam(name) {
|
|
476
|
-
const patterns = [
|
|
477
|
-
...this.config.maskQueryParams,
|
|
478
|
-
...MASK_QUERY_PARAM_PATTERNS
|
|
479
|
-
];
|
|
480
|
-
return matchPatterns(name, patterns);
|
|
481
|
-
}
|
|
482
|
-
shouldMaskHeader(name) {
|
|
483
|
-
const patterns = [
|
|
484
|
-
...this.config.maskHeaders,
|
|
485
|
-
...MASK_HEADER_PATTERNS
|
|
486
|
-
];
|
|
487
|
-
return matchPatterns(name, patterns);
|
|
488
|
-
}
|
|
489
|
-
shouldMaskBodyField(name) {
|
|
490
|
-
const patterns = [
|
|
491
|
-
...this.config.maskBodyFields,
|
|
492
|
-
...MASK_BODY_FIELD_PATTERNS
|
|
493
|
-
];
|
|
494
|
-
return matchPatterns(name, patterns);
|
|
495
|
-
}
|
|
496
|
-
hasSupportedContentType(headers) {
|
|
497
|
-
var _a2;
|
|
498
|
-
const contentType = (_a2 = headers.find(([k]) => k.toLowerCase() === "content-type")) == null ? void 0 : _a2[1];
|
|
499
|
-
return this.isSupportedContentType(contentType);
|
|
500
|
-
}
|
|
501
|
-
hasJsonContentType(headers) {
|
|
502
|
-
var _a2;
|
|
503
|
-
const contentType = (_a2 = headers.find(([k]) => k.toLowerCase() === "content-type")) == null ? void 0 : _a2[1];
|
|
504
|
-
return contentType ? /\bjson\b/i.test(contentType) : null;
|
|
505
|
-
}
|
|
506
|
-
isSupportedContentType(contentType) {
|
|
507
|
-
return typeof contentType === "string" && ALLOWED_CONTENT_TYPES.some((t) => contentType.startsWith(t));
|
|
508
|
-
}
|
|
509
|
-
maskQueryParams(search) {
|
|
510
|
-
const params = new URLSearchParams(search);
|
|
511
|
-
for (const [key] of params) {
|
|
512
|
-
if (this.shouldMaskQueryParam(key)) {
|
|
513
|
-
params.set(key, MASKED);
|
|
514
|
-
}
|
|
515
|
-
}
|
|
516
|
-
return params.toString();
|
|
517
|
-
}
|
|
518
|
-
maskHeaders(headers) {
|
|
519
|
-
return headers.map(([k, v]) => [
|
|
520
|
-
k,
|
|
521
|
-
this.shouldMaskHeader(k) ? MASKED : v
|
|
522
|
-
]);
|
|
523
|
-
}
|
|
524
|
-
maskBody(data) {
|
|
525
|
-
if (typeof data === "object" && data !== null && !Array.isArray(data)) {
|
|
526
|
-
const result = {};
|
|
527
|
-
for (const [key, value] of Object.entries(data)) {
|
|
528
|
-
if (typeof value === "string" && this.shouldMaskBodyField(key)) {
|
|
529
|
-
result[key] = MASKED;
|
|
530
|
-
} else {
|
|
531
|
-
result[key] = this.maskBody(value);
|
|
532
|
-
}
|
|
533
|
-
}
|
|
534
|
-
return result;
|
|
535
|
-
}
|
|
536
|
-
if (Array.isArray(data)) {
|
|
537
|
-
return data.map((item) => this.maskBody(item));
|
|
538
|
-
}
|
|
539
|
-
return data;
|
|
540
|
-
}
|
|
541
|
-
applyMasking(item) {
|
|
542
|
-
if (this.config.maskRequestBodyCallback && item.request.body && item.request.body !== BODY_TOO_LARGE) {
|
|
543
|
-
try {
|
|
544
|
-
const maskedBody = this.config.maskRequestBodyCallback(item.request);
|
|
545
|
-
item.request.body = maskedBody ?? BODY_MASKED;
|
|
546
|
-
} catch {
|
|
547
|
-
item.request.body = void 0;
|
|
548
|
-
}
|
|
549
|
-
}
|
|
550
|
-
if (this.config.maskResponseBodyCallback && item.response.body && item.response.body !== BODY_TOO_LARGE) {
|
|
551
|
-
try {
|
|
552
|
-
const maskedBody = this.config.maskResponseBodyCallback(item.request, item.response);
|
|
553
|
-
item.response.body = maskedBody ?? BODY_MASKED;
|
|
554
|
-
} catch {
|
|
555
|
-
item.response.body = void 0;
|
|
556
|
-
}
|
|
557
|
-
}
|
|
558
|
-
if (item.request.body && item.request.body.length > MAX_BODY_SIZE) {
|
|
559
|
-
item.request.body = BODY_TOO_LARGE;
|
|
560
|
-
}
|
|
561
|
-
if (item.response.body && item.response.body.length > MAX_BODY_SIZE) {
|
|
562
|
-
item.response.body = BODY_TOO_LARGE;
|
|
563
|
-
}
|
|
564
|
-
for (const key of [
|
|
565
|
-
"request",
|
|
566
|
-
"response"
|
|
567
|
-
]) {
|
|
568
|
-
const bodyData = item[key].body;
|
|
569
|
-
if (!bodyData || bodyData === BODY_TOO_LARGE || bodyData === BODY_MASKED) {
|
|
570
|
-
continue;
|
|
571
|
-
}
|
|
572
|
-
const headers = item[key].headers;
|
|
573
|
-
const hasJsonContent = this.hasJsonContentType(headers);
|
|
574
|
-
if (hasJsonContent === null || hasJsonContent) {
|
|
575
|
-
try {
|
|
576
|
-
const parsedBody = JSON.parse(bodyData.toString());
|
|
577
|
-
const maskedBody = this.maskBody(parsedBody);
|
|
578
|
-
item[key].body = Buffer3.from(JSON.stringify(maskedBody));
|
|
579
|
-
} catch {
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
item.request.headers = this.config.logRequestHeaders ? this.maskHeaders(item.request.headers) : [];
|
|
584
|
-
item.response.headers = this.config.logResponseHeaders ? this.maskHeaders(item.response.headers) : [];
|
|
585
|
-
const url = new URL(item.request.url);
|
|
586
|
-
url.search = this.config.logQueryParams ? this.maskQueryParams(url.search) : "";
|
|
587
|
-
item.request.url = url.toString();
|
|
588
|
-
return item;
|
|
589
|
-
}
|
|
590
|
-
logRequest(request, response, error, logs) {
|
|
591
|
-
var _a2, _b, _c;
|
|
592
|
-
if (!this.enabled || this.suspendUntil !== null) return;
|
|
593
|
-
const url = new URL(request.url);
|
|
594
|
-
const path = request.path ?? url.pathname;
|
|
595
|
-
const userAgent = (_a2 = request.headers.find(([k]) => k.toLowerCase() === "user-agent")) == null ? void 0 : _a2[1];
|
|
596
|
-
if (this.shouldExcludePath(path) || this.shouldExcludeUserAgent(userAgent) || (((_c = (_b = this.config).excludeCallback) == null ? void 0 : _c.call(_b, request, response)) ?? false)) {
|
|
597
|
-
return;
|
|
598
|
-
}
|
|
599
|
-
if (!this.config.logRequestBody || !this.hasSupportedContentType(request.headers)) {
|
|
600
|
-
request.body = void 0;
|
|
601
|
-
}
|
|
602
|
-
if (!this.config.logResponseBody || !this.hasSupportedContentType(response.headers)) {
|
|
603
|
-
response.body = void 0;
|
|
604
|
-
}
|
|
605
|
-
if (request.size !== void 0 && request.size < 0) {
|
|
606
|
-
request.size = void 0;
|
|
607
|
-
}
|
|
608
|
-
if (response.size !== void 0 && response.size < 0) {
|
|
609
|
-
response.size = void 0;
|
|
610
|
-
}
|
|
611
|
-
const item = {
|
|
612
|
-
uuid: randomUUID2(),
|
|
613
|
-
request,
|
|
614
|
-
response,
|
|
615
|
-
exception: error && this.config.logException ? {
|
|
616
|
-
type: error.name,
|
|
617
|
-
message: truncateExceptionMessage(error.message),
|
|
618
|
-
stacktrace: truncateExceptionStackTrace(error.stack || ""),
|
|
619
|
-
sentryEventId: getSentryEventId()
|
|
620
|
-
} : void 0
|
|
621
|
-
};
|
|
622
|
-
if (logs && logs.length > 0) {
|
|
623
|
-
item.logs = logs.map((log) => ({
|
|
624
|
-
timestamp: log.timestamp,
|
|
625
|
-
logger: log.logger,
|
|
626
|
-
level: log.level,
|
|
627
|
-
message: truncateLogMessage(log.message)
|
|
628
|
-
}));
|
|
629
|
-
}
|
|
630
|
-
this.pendingWrites.push(item);
|
|
631
|
-
if (this.pendingWrites.length > MAX_PENDING_WRITES) {
|
|
632
|
-
this.pendingWrites.shift();
|
|
633
|
-
}
|
|
634
|
-
}
|
|
635
|
-
async writeToFile() {
|
|
636
|
-
if (!this.enabled || this.pendingWrites.length === 0) {
|
|
637
|
-
return;
|
|
638
|
-
}
|
|
639
|
-
return this.lock.acquire("file", async () => {
|
|
640
|
-
if (!this.currentFile) {
|
|
641
|
-
this.currentFile = new TempGzipFile();
|
|
642
|
-
}
|
|
643
|
-
while (this.pendingWrites.length > 0) {
|
|
644
|
-
let item = this.pendingWrites.shift();
|
|
645
|
-
if (item) {
|
|
646
|
-
item = this.applyMasking(item);
|
|
647
|
-
const finalItem = {
|
|
648
|
-
uuid: item.uuid,
|
|
649
|
-
request: skipEmptyValues(item.request),
|
|
650
|
-
response: skipEmptyValues(item.response),
|
|
651
|
-
exception: item.exception,
|
|
652
|
-
logs: item.logs
|
|
653
|
-
};
|
|
654
|
-
[
|
|
655
|
-
finalItem.request.body,
|
|
656
|
-
finalItem.response.body
|
|
657
|
-
].forEach((body) => {
|
|
658
|
-
if (body) {
|
|
659
|
-
body.toJSON = function() {
|
|
660
|
-
return this.toString("base64");
|
|
661
|
-
};
|
|
662
|
-
}
|
|
663
|
-
});
|
|
664
|
-
await this.currentFile.writeLine(Buffer3.from(JSON.stringify(finalItem)));
|
|
665
|
-
}
|
|
666
|
-
}
|
|
667
|
-
});
|
|
668
|
-
}
|
|
669
|
-
getFile() {
|
|
670
|
-
return this.files.shift();
|
|
671
|
-
}
|
|
672
|
-
retryFileLater(file) {
|
|
673
|
-
this.files.unshift(file);
|
|
674
|
-
}
|
|
675
|
-
async rotateFile() {
|
|
676
|
-
return this.lock.acquire("file", async () => {
|
|
677
|
-
if (this.currentFile) {
|
|
678
|
-
await this.currentFile.close();
|
|
679
|
-
this.files.push(this.currentFile);
|
|
680
|
-
this.currentFile = null;
|
|
681
|
-
}
|
|
682
|
-
});
|
|
683
|
-
}
|
|
684
|
-
async maintain() {
|
|
685
|
-
await this.writeToFile();
|
|
686
|
-
if (this.currentFile && this.currentFile.size > MAX_FILE_SIZE) {
|
|
687
|
-
await this.rotateFile();
|
|
688
|
-
}
|
|
689
|
-
while (this.files.length > MAX_FILES) {
|
|
690
|
-
const file = this.files.shift();
|
|
691
|
-
file == null ? void 0 : file.delete();
|
|
692
|
-
}
|
|
693
|
-
if (this.suspendUntil !== null && this.suspendUntil < Date.now()) {
|
|
694
|
-
this.suspendUntil = null;
|
|
695
|
-
}
|
|
696
|
-
}
|
|
697
|
-
async clear() {
|
|
698
|
-
this.pendingWrites = [];
|
|
699
|
-
await this.rotateFile();
|
|
700
|
-
this.files.forEach((file) => {
|
|
701
|
-
file.delete();
|
|
702
|
-
});
|
|
703
|
-
this.files = [];
|
|
704
|
-
}
|
|
705
|
-
async close() {
|
|
706
|
-
this.enabled = false;
|
|
707
|
-
await this.clear();
|
|
708
|
-
if (this.maintainIntervalId) {
|
|
709
|
-
clearInterval(this.maintainIntervalId);
|
|
710
|
-
}
|
|
711
|
-
}
|
|
712
|
-
};
|
|
713
|
-
__name(_RequestLogger, "RequestLogger");
|
|
714
|
-
var RequestLogger = _RequestLogger;
|
|
715
|
-
function convertHeaders(headers) {
|
|
716
|
-
if (!headers) {
|
|
717
|
-
return [];
|
|
718
|
-
}
|
|
719
|
-
if (headers instanceof Headers) {
|
|
720
|
-
return Array.from(headers.entries());
|
|
721
|
-
}
|
|
722
|
-
return Object.entries(headers).flatMap(([key, value]) => {
|
|
723
|
-
if (value === void 0) {
|
|
724
|
-
return [];
|
|
725
|
-
}
|
|
726
|
-
if (Array.isArray(value)) {
|
|
727
|
-
return value.map((v) => [
|
|
728
|
-
key,
|
|
729
|
-
v
|
|
730
|
-
]);
|
|
731
|
-
}
|
|
732
|
-
return [
|
|
733
|
-
[
|
|
734
|
-
key,
|
|
735
|
-
value.toString()
|
|
736
|
-
]
|
|
737
|
-
];
|
|
738
|
-
});
|
|
739
|
-
}
|
|
740
|
-
__name(convertHeaders, "convertHeaders");
|
|
741
|
-
function convertBody(body, contentType) {
|
|
742
|
-
if (!body || !contentType) {
|
|
743
|
-
return;
|
|
744
|
-
}
|
|
745
|
-
try {
|
|
746
|
-
if (contentType.startsWith("application/json")) {
|
|
747
|
-
if (isValidJsonString(body)) {
|
|
748
|
-
return Buffer3.from(body);
|
|
749
|
-
} else {
|
|
750
|
-
return Buffer3.from(JSON.stringify(body));
|
|
751
|
-
}
|
|
752
|
-
}
|
|
753
|
-
if (contentType.startsWith("text/") && typeof body === "string") {
|
|
754
|
-
return Buffer3.from(body);
|
|
755
|
-
}
|
|
756
|
-
} catch (error) {
|
|
757
|
-
return;
|
|
758
|
-
}
|
|
759
|
-
}
|
|
760
|
-
__name(convertBody, "convertBody");
|
|
761
|
-
function isValidJsonString(body) {
|
|
762
|
-
if (typeof body !== "string") {
|
|
763
|
-
return false;
|
|
764
|
-
}
|
|
765
|
-
try {
|
|
766
|
-
JSON.parse(body);
|
|
767
|
-
return true;
|
|
768
|
-
} catch {
|
|
769
|
-
return false;
|
|
770
|
-
}
|
|
771
|
-
}
|
|
772
|
-
__name(isValidJsonString, "isValidJsonString");
|
|
773
|
-
function matchPatterns(value, patterns) {
|
|
774
|
-
return patterns.some((pattern) => {
|
|
775
|
-
return pattern.test(value);
|
|
776
|
-
});
|
|
777
|
-
}
|
|
778
|
-
__name(matchPatterns, "matchPatterns");
|
|
779
|
-
function skipEmptyValues(data) {
|
|
780
|
-
return Object.fromEntries(Object.entries(data).filter(([_, v]) => {
|
|
781
|
-
if (v == null || Number.isNaN(v)) return false;
|
|
782
|
-
if (Array.isArray(v) || Buffer3.isBuffer(v) || typeof v === "string") {
|
|
783
|
-
return v.length > 0;
|
|
784
|
-
}
|
|
785
|
-
return true;
|
|
786
|
-
}));
|
|
787
|
-
}
|
|
788
|
-
__name(skipEmptyValues, "skipEmptyValues");
|
|
789
|
-
function truncateLogMessage(msg) {
|
|
790
|
-
if (msg.length > MAX_LOG_MSG_LENGTH) {
|
|
791
|
-
const suffix = "... (truncated)";
|
|
792
|
-
return msg.slice(0, MAX_LOG_MSG_LENGTH - suffix.length) + suffix;
|
|
793
|
-
}
|
|
794
|
-
return msg;
|
|
795
|
-
}
|
|
796
|
-
__name(truncateLogMessage, "truncateLogMessage");
|
|
797
|
-
function checkWritableFs() {
|
|
798
|
-
try {
|
|
799
|
-
const testPath = join2(tmpdir2(), `apitally-${randomUUID2()}`);
|
|
800
|
-
writeFileSync(testPath, "test");
|
|
801
|
-
unlinkSync(testPath);
|
|
802
|
-
return true;
|
|
803
|
-
} catch (error) {
|
|
804
|
-
return false;
|
|
805
|
-
}
|
|
806
|
-
}
|
|
807
|
-
__name(checkWritableFs, "checkWritableFs");
|
|
808
|
-
|
|
809
|
-
// src/common/validationErrorCounter.ts
|
|
810
|
-
import { createHash as createHash2 } from "crypto";
|
|
811
|
-
var _ValidationErrorCounter = class _ValidationErrorCounter {
|
|
812
|
-
errorCounts;
|
|
813
|
-
errorDetails;
|
|
814
|
-
constructor() {
|
|
815
|
-
this.errorCounts = /* @__PURE__ */ new Map();
|
|
816
|
-
this.errorDetails = /* @__PURE__ */ new Map();
|
|
817
|
-
}
|
|
818
|
-
addValidationError(validationError) {
|
|
819
|
-
const key = this.getKey(validationError);
|
|
820
|
-
if (!this.errorDetails.has(key)) {
|
|
821
|
-
this.errorDetails.set(key, validationError);
|
|
822
|
-
}
|
|
823
|
-
this.errorCounts.set(key, (this.errorCounts.get(key) || 0) + 1);
|
|
824
|
-
}
|
|
825
|
-
getAndResetValidationErrors() {
|
|
826
|
-
const data = [];
|
|
827
|
-
this.errorCounts.forEach((count, key) => {
|
|
828
|
-
const validationError = this.errorDetails.get(key);
|
|
829
|
-
if (validationError) {
|
|
830
|
-
data.push({
|
|
831
|
-
consumer: validationError.consumer || null,
|
|
832
|
-
method: validationError.method,
|
|
833
|
-
path: validationError.path,
|
|
834
|
-
loc: validationError.loc.split(".").filter(Boolean),
|
|
835
|
-
msg: validationError.msg,
|
|
836
|
-
type: validationError.type,
|
|
837
|
-
error_count: count
|
|
838
|
-
});
|
|
839
|
-
}
|
|
840
|
-
});
|
|
841
|
-
this.errorCounts.clear();
|
|
842
|
-
this.errorDetails.clear();
|
|
843
|
-
return data;
|
|
844
|
-
}
|
|
845
|
-
getKey(validationError) {
|
|
846
|
-
const hashInput = [
|
|
847
|
-
validationError.consumer || "",
|
|
848
|
-
validationError.method.toUpperCase(),
|
|
849
|
-
validationError.path,
|
|
850
|
-
validationError.loc.split(".").filter(Boolean),
|
|
851
|
-
validationError.msg.trim(),
|
|
852
|
-
validationError.type
|
|
853
|
-
].join("|");
|
|
854
|
-
return createHash2("md5").update(hashInput).digest("hex");
|
|
855
|
-
}
|
|
856
|
-
};
|
|
857
|
-
__name(_ValidationErrorCounter, "ValidationErrorCounter");
|
|
858
|
-
var ValidationErrorCounter = _ValidationErrorCounter;
|
|
859
|
-
|
|
860
|
-
// src/common/client.ts
|
|
861
|
-
var SYNC_INTERVAL = 6e4;
|
|
862
|
-
var INITIAL_SYNC_INTERVAL = 1e4;
|
|
863
|
-
var INITIAL_SYNC_INTERVAL_DURATION = 36e5;
|
|
864
|
-
var MAX_QUEUE_TIME = 36e5;
|
|
865
|
-
var _a;
|
|
866
|
-
var HTTPError = (_a = class extends Error {
|
|
867
|
-
response;
|
|
868
|
-
constructor(response) {
|
|
869
|
-
const reason = response.status ? `status code ${response.status}` : "an unknown error";
|
|
870
|
-
super(`Request failed with ${reason}`);
|
|
871
|
-
this.response = response;
|
|
872
|
-
}
|
|
873
|
-
}, __name(_a, "HTTPError"), _a);
|
|
874
|
-
var _ApitallyClient = class _ApitallyClient {
|
|
875
|
-
clientId;
|
|
876
|
-
env;
|
|
877
|
-
instanceUuid;
|
|
878
|
-
syncDataQueue;
|
|
879
|
-
syncIntervalId;
|
|
880
|
-
startupData;
|
|
881
|
-
startupDataSent = false;
|
|
882
|
-
enabled = true;
|
|
883
|
-
requestCounter;
|
|
884
|
-
requestLogger;
|
|
885
|
-
validationErrorCounter;
|
|
886
|
-
serverErrorCounter;
|
|
887
|
-
consumerRegistry;
|
|
888
|
-
logger;
|
|
889
|
-
constructor({ clientId, env = "dev", requestLogging, requestLoggingConfig, logger }) {
|
|
890
|
-
if (_ApitallyClient.instance) {
|
|
891
|
-
throw new Error("Apitally client is already initialized");
|
|
892
|
-
}
|
|
893
|
-
if (!isValidClientId(clientId)) {
|
|
894
|
-
throw new Error(`Invalid Apitally client ID '${clientId}' (expecting hexadecimal UUID format)`);
|
|
895
|
-
}
|
|
896
|
-
if (!isValidEnv(env)) {
|
|
897
|
-
throw new Error(`Invalid env '${env}' (expecting 1-32 alphanumeric lowercase characters and hyphens only)`);
|
|
898
|
-
}
|
|
899
|
-
if (requestLoggingConfig && !requestLogging) {
|
|
900
|
-
console.warn("requestLoggingConfig is deprecated, use requestLogging instead.");
|
|
901
|
-
}
|
|
902
|
-
_ApitallyClient.instance = this;
|
|
903
|
-
this.clientId = clientId;
|
|
904
|
-
this.env = env;
|
|
905
|
-
this.instanceUuid = randomUUID3();
|
|
906
|
-
this.syncDataQueue = [];
|
|
907
|
-
this.requestCounter = new RequestCounter();
|
|
908
|
-
this.requestLogger = new RequestLogger(requestLogging ?? requestLoggingConfig);
|
|
909
|
-
this.validationErrorCounter = new ValidationErrorCounter();
|
|
910
|
-
this.serverErrorCounter = new ServerErrorCounter();
|
|
911
|
-
this.consumerRegistry = new ConsumerRegistry();
|
|
912
|
-
this.logger = logger ?? getLogger();
|
|
913
|
-
this.startSync();
|
|
914
|
-
this.handleShutdown = this.handleShutdown.bind(this);
|
|
915
|
-
}
|
|
916
|
-
static getInstance() {
|
|
917
|
-
if (!_ApitallyClient.instance) {
|
|
918
|
-
throw new Error("Apitally client is not initialized");
|
|
919
|
-
}
|
|
920
|
-
return _ApitallyClient.instance;
|
|
921
|
-
}
|
|
922
|
-
isEnabled() {
|
|
923
|
-
return this.enabled;
|
|
924
|
-
}
|
|
925
|
-
static async shutdown() {
|
|
926
|
-
if (_ApitallyClient.instance) {
|
|
927
|
-
await _ApitallyClient.instance.handleShutdown();
|
|
928
|
-
}
|
|
929
|
-
}
|
|
930
|
-
async handleShutdown() {
|
|
931
|
-
this.enabled = false;
|
|
932
|
-
this.stopSync();
|
|
933
|
-
await this.sendSyncData();
|
|
934
|
-
await this.sendLogData();
|
|
935
|
-
await this.requestLogger.close();
|
|
936
|
-
_ApitallyClient.instance = void 0;
|
|
937
|
-
}
|
|
938
|
-
getHubUrlPrefix() {
|
|
939
|
-
const baseURL = process.env.APITALLY_HUB_BASE_URL || "https://hub.apitally.io";
|
|
940
|
-
const version = "v2";
|
|
941
|
-
return `${baseURL}/${version}/${this.clientId}/${this.env}/`;
|
|
942
|
-
}
|
|
943
|
-
async sendData(url, payload) {
|
|
944
|
-
const fetchWithRetry = fetchRetry(fetch, {
|
|
945
|
-
retries: 3,
|
|
946
|
-
retryDelay: 1e3,
|
|
947
|
-
retryOn: [
|
|
948
|
-
408,
|
|
949
|
-
429,
|
|
950
|
-
500,
|
|
951
|
-
502,
|
|
952
|
-
503,
|
|
953
|
-
504
|
|
954
|
-
]
|
|
955
|
-
});
|
|
956
|
-
const response = await fetchWithRetry(this.getHubUrlPrefix() + url, {
|
|
957
|
-
method: "POST",
|
|
958
|
-
body: JSON.stringify(payload),
|
|
959
|
-
headers: {
|
|
960
|
-
"Content-Type": "application/json"
|
|
961
|
-
}
|
|
962
|
-
});
|
|
963
|
-
if (!response.ok) {
|
|
964
|
-
throw new HTTPError(response);
|
|
965
|
-
}
|
|
966
|
-
}
|
|
967
|
-
startSync() {
|
|
968
|
-
this.sync();
|
|
969
|
-
this.syncIntervalId = setInterval(() => {
|
|
970
|
-
this.sync();
|
|
971
|
-
}, INITIAL_SYNC_INTERVAL);
|
|
972
|
-
setTimeout(() => {
|
|
973
|
-
clearInterval(this.syncIntervalId);
|
|
974
|
-
this.syncIntervalId = setInterval(() => {
|
|
975
|
-
this.sync();
|
|
976
|
-
}, SYNC_INTERVAL);
|
|
977
|
-
}, INITIAL_SYNC_INTERVAL_DURATION);
|
|
978
|
-
}
|
|
979
|
-
async sync() {
|
|
980
|
-
try {
|
|
981
|
-
const promises = [
|
|
982
|
-
this.sendSyncData(),
|
|
983
|
-
this.sendLogData()
|
|
984
|
-
];
|
|
985
|
-
if (!this.startupDataSent) {
|
|
986
|
-
promises.push(this.sendStartupData());
|
|
987
|
-
}
|
|
988
|
-
await Promise.all(promises);
|
|
989
|
-
} catch (error) {
|
|
990
|
-
this.logger.error("Error while syncing with Apitally Hub", {
|
|
991
|
-
error
|
|
992
|
-
});
|
|
993
|
-
}
|
|
994
|
-
}
|
|
995
|
-
stopSync() {
|
|
996
|
-
if (this.syncIntervalId) {
|
|
997
|
-
clearInterval(this.syncIntervalId);
|
|
998
|
-
this.syncIntervalId = void 0;
|
|
999
|
-
}
|
|
1000
|
-
}
|
|
1001
|
-
setStartupData(data) {
|
|
1002
|
-
this.startupData = data;
|
|
1003
|
-
this.startupDataSent = false;
|
|
1004
|
-
this.sendStartupData();
|
|
1005
|
-
}
|
|
1006
|
-
async sendStartupData() {
|
|
1007
|
-
if (this.startupData) {
|
|
1008
|
-
this.logger.debug("Sending startup data to Apitally Hub");
|
|
1009
|
-
const payload = {
|
|
1010
|
-
instance_uuid: this.instanceUuid,
|
|
1011
|
-
message_uuid: randomUUID3(),
|
|
1012
|
-
...this.startupData
|
|
1013
|
-
};
|
|
1014
|
-
try {
|
|
1015
|
-
await this.sendData("startup", payload);
|
|
1016
|
-
this.startupDataSent = true;
|
|
1017
|
-
} catch (error) {
|
|
1018
|
-
const handled = this.handleHubError(error);
|
|
1019
|
-
if (!handled) {
|
|
1020
|
-
this.logger.error(error.message);
|
|
1021
|
-
this.logger.debug("Error while sending startup data to Apitally Hub (will retry)", {
|
|
1022
|
-
error
|
|
1023
|
-
});
|
|
1024
|
-
}
|
|
1025
|
-
}
|
|
1026
|
-
}
|
|
1027
|
-
}
|
|
1028
|
-
async sendSyncData() {
|
|
1029
|
-
this.logger.debug("Synchronizing data with Apitally Hub");
|
|
1030
|
-
const newPayload = {
|
|
1031
|
-
timestamp: Date.now() / 1e3,
|
|
1032
|
-
instance_uuid: this.instanceUuid,
|
|
1033
|
-
message_uuid: randomUUID3(),
|
|
1034
|
-
requests: this.requestCounter.getAndResetRequests(),
|
|
1035
|
-
validation_errors: this.validationErrorCounter.getAndResetValidationErrors(),
|
|
1036
|
-
server_errors: this.serverErrorCounter.getAndResetServerErrors(),
|
|
1037
|
-
consumers: this.consumerRegistry.getAndResetUpdatedConsumers()
|
|
1038
|
-
};
|
|
1039
|
-
this.syncDataQueue.push(newPayload);
|
|
1040
|
-
let i = 0;
|
|
1041
|
-
while (this.syncDataQueue.length > 0) {
|
|
1042
|
-
const payload = this.syncDataQueue.shift();
|
|
1043
|
-
if (payload) {
|
|
1044
|
-
try {
|
|
1045
|
-
if (Date.now() - payload.timestamp * 1e3 <= MAX_QUEUE_TIME) {
|
|
1046
|
-
if (i > 0) {
|
|
1047
|
-
await this.randomDelay();
|
|
1048
|
-
}
|
|
1049
|
-
await this.sendData("sync", payload);
|
|
1050
|
-
i += 1;
|
|
1051
|
-
}
|
|
1052
|
-
} catch (error) {
|
|
1053
|
-
const handled = this.handleHubError(error);
|
|
1054
|
-
if (!handled) {
|
|
1055
|
-
this.logger.debug("Error while synchronizing data with Apitally Hub (will retry)", {
|
|
1056
|
-
error
|
|
1057
|
-
});
|
|
1058
|
-
this.syncDataQueue.push(payload);
|
|
1059
|
-
break;
|
|
1060
|
-
}
|
|
1061
|
-
}
|
|
1062
|
-
}
|
|
1063
|
-
}
|
|
1064
|
-
}
|
|
1065
|
-
async sendLogData() {
|
|
1066
|
-
this.logger.debug("Sending request log data to Apitally Hub");
|
|
1067
|
-
await this.requestLogger.rotateFile();
|
|
1068
|
-
const fetchWithRetry = fetchRetry(fetch, {
|
|
1069
|
-
retries: 3,
|
|
1070
|
-
retryDelay: 1e3,
|
|
1071
|
-
retryOn: [
|
|
1072
|
-
408,
|
|
1073
|
-
429,
|
|
1074
|
-
500,
|
|
1075
|
-
502,
|
|
1076
|
-
503,
|
|
1077
|
-
504
|
|
1078
|
-
]
|
|
1079
|
-
});
|
|
1080
|
-
let i = 0;
|
|
1081
|
-
let logFile;
|
|
1082
|
-
while (logFile = this.requestLogger.getFile()) {
|
|
1083
|
-
if (i > 0) {
|
|
1084
|
-
await this.randomDelay();
|
|
1085
|
-
}
|
|
1086
|
-
try {
|
|
1087
|
-
const response = await fetchWithRetry(`${this.getHubUrlPrefix()}log?uuid=${logFile.uuid}`, {
|
|
1088
|
-
method: "POST",
|
|
1089
|
-
body: await logFile.getContent()
|
|
1090
|
-
});
|
|
1091
|
-
if (response.status === 402 && response.headers.has("Retry-After")) {
|
|
1092
|
-
const retryAfter = parseInt(response.headers.get("Retry-After") ?? "0");
|
|
1093
|
-
if (retryAfter > 0) {
|
|
1094
|
-
this.requestLogger.suspendUntil = Date.now() + retryAfter * 1e3;
|
|
1095
|
-
this.requestLogger.clear();
|
|
1096
|
-
return;
|
|
1097
|
-
}
|
|
1098
|
-
}
|
|
1099
|
-
if (!response.ok) {
|
|
1100
|
-
throw new HTTPError(response);
|
|
1101
|
-
}
|
|
1102
|
-
logFile.delete();
|
|
1103
|
-
} catch (error) {
|
|
1104
|
-
this.requestLogger.retryFileLater(logFile);
|
|
1105
|
-
break;
|
|
1106
|
-
}
|
|
1107
|
-
i++;
|
|
1108
|
-
if (i >= 10) break;
|
|
1109
|
-
}
|
|
1110
|
-
}
|
|
1111
|
-
handleHubError(error) {
|
|
1112
|
-
if (error instanceof HTTPError) {
|
|
1113
|
-
if (error.response.status === 404) {
|
|
1114
|
-
this.logger.error(`Invalid Apitally client ID: '${this.clientId}'`);
|
|
1115
|
-
this.enabled = false;
|
|
1116
|
-
this.stopSync();
|
|
1117
|
-
return true;
|
|
1118
|
-
}
|
|
1119
|
-
if (error.response.status === 422) {
|
|
1120
|
-
this.logger.error("Received validation error from Apitally Hub");
|
|
1121
|
-
return true;
|
|
1122
|
-
}
|
|
1123
|
-
}
|
|
1124
|
-
return false;
|
|
1125
|
-
}
|
|
1126
|
-
async randomDelay() {
|
|
1127
|
-
const delay = 100 + Math.random() * 400;
|
|
1128
|
-
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
1129
|
-
}
|
|
1130
|
-
};
|
|
1131
|
-
__name(_ApitallyClient, "ApitallyClient");
|
|
1132
|
-
__publicField(_ApitallyClient, "instance");
|
|
1133
|
-
var ApitallyClient = _ApitallyClient;
|
|
1134
|
-
|
|
1135
|
-
// src/common/packageVersions.ts
|
|
1136
|
-
import { createRequire } from "module";
|
|
1137
|
-
function getPackageVersion(name) {
|
|
1138
|
-
const packageJsonPath = `${name}/package.json`;
|
|
1139
|
-
try {
|
|
1140
|
-
return __require(packageJsonPath).version || null;
|
|
1141
|
-
} catch (error) {
|
|
1142
|
-
try {
|
|
1143
|
-
const _require = createRequire(import.meta.url);
|
|
1144
|
-
return _require(packageJsonPath).version || null;
|
|
1145
|
-
} catch (error2) {
|
|
1146
|
-
return null;
|
|
1147
|
-
}
|
|
1148
|
-
}
|
|
1149
|
-
}
|
|
1150
|
-
__name(getPackageVersion, "getPackageVersion");
|
|
1151
|
-
|
|
1152
|
-
// src/loggers/console.ts
|
|
1153
|
-
import { format as format2 } from "util";
|
|
1154
|
-
var MAX_BUFFER_SIZE = 1e3;
|
|
1155
|
-
var isPatched = false;
|
|
1156
|
-
var globalLogsContext;
|
|
1157
|
-
function patchConsole(logsContext) {
|
|
1158
|
-
globalLogsContext = logsContext;
|
|
1159
|
-
if (isPatched) {
|
|
1160
|
-
return;
|
|
1161
|
-
}
|
|
1162
|
-
const logMethods = [
|
|
1163
|
-
"log",
|
|
1164
|
-
"warn",
|
|
1165
|
-
"error",
|
|
1166
|
-
"info",
|
|
1167
|
-
"debug"
|
|
1168
|
-
];
|
|
1169
|
-
logMethods.forEach((method) => {
|
|
1170
|
-
const originalMethod = console[method];
|
|
1171
|
-
console[method] = function(...args) {
|
|
1172
|
-
captureLog(method, args);
|
|
1173
|
-
return originalMethod.apply(console, args);
|
|
1174
|
-
};
|
|
1175
|
-
});
|
|
1176
|
-
isPatched = true;
|
|
1177
|
-
}
|
|
1178
|
-
__name(patchConsole, "patchConsole");
|
|
1179
|
-
function captureLog(level, args) {
|
|
1180
|
-
const logs = globalLogsContext == null ? void 0 : globalLogsContext.getStore();
|
|
1181
|
-
if (logs && logs.length < MAX_BUFFER_SIZE) {
|
|
1182
|
-
logs.push({
|
|
1183
|
-
timestamp: Date.now() / 1e3,
|
|
1184
|
-
logger: "console",
|
|
1185
|
-
level,
|
|
1186
|
-
message: format2(...args)
|
|
1187
|
-
});
|
|
1188
|
-
}
|
|
1189
|
-
}
|
|
1190
|
-
__name(captureLog, "captureLog");
|
|
1191
|
-
|
|
1192
|
-
// src/loggers/hapi.ts
|
|
1193
|
-
import { format as format3 } from "util";
|
|
1194
|
-
|
|
1195
|
-
// src/loggers/utils.ts
|
|
1196
|
-
import { format as format4 } from "util";
|
|
1197
|
-
function formatMessage(message, ...args) {
|
|
1198
|
-
return [
|
|
1199
|
-
message,
|
|
1200
|
-
...args
|
|
1201
|
-
].map(formatArg).filter((arg) => arg !== "").join("\n");
|
|
1202
|
-
}
|
|
1203
|
-
__name(formatMessage, "formatMessage");
|
|
1204
|
-
function removeKeys(obj, keys) {
|
|
1205
|
-
return Object.fromEntries(Object.entries(obj).filter(([key]) => !keys.includes(key)));
|
|
1206
|
-
}
|
|
1207
|
-
__name(removeKeys, "removeKeys");
|
|
1208
|
-
function formatArg(arg) {
|
|
1209
|
-
if (typeof arg === "string") {
|
|
1210
|
-
return arg.trim();
|
|
1211
|
-
}
|
|
1212
|
-
if (arg instanceof Error) {
|
|
1213
|
-
return format4(arg).trim();
|
|
1214
|
-
}
|
|
1215
|
-
if (arg === void 0 || arg === null || isEmptyObject(arg)) {
|
|
1216
|
-
return "";
|
|
1217
|
-
}
|
|
1218
|
-
try {
|
|
1219
|
-
return JSON.stringify(arg);
|
|
1220
|
-
} catch {
|
|
1221
|
-
return format4(arg).trim();
|
|
1222
|
-
}
|
|
1223
|
-
}
|
|
1224
|
-
__name(formatArg, "formatArg");
|
|
1225
|
-
function isEmptyObject(obj) {
|
|
1226
|
-
return obj !== null && typeof obj === "object" && Object.getPrototypeOf(obj) === Object.prototype && Object.keys(obj).length === 0;
|
|
1227
|
-
}
|
|
1228
|
-
__name(isEmptyObject, "isEmptyObject");
|
|
1229
|
-
|
|
1230
|
-
// src/loggers/pino.ts
|
|
1231
|
-
var originalStreamSym = Symbol.for("apitally.originalStream");
|
|
1232
|
-
|
|
1233
|
-
// src/loggers/winston.ts
|
|
1234
|
-
var MAX_BUFFER_SIZE2 = 1e3;
|
|
1235
|
-
var isPatched2 = false;
|
|
1236
|
-
var globalLogsContext2;
|
|
1237
|
-
async function patchWinston(logsContext) {
|
|
1238
|
-
var _a2, _b;
|
|
1239
|
-
globalLogsContext2 = logsContext;
|
|
1240
|
-
if (isPatched2) {
|
|
1241
|
-
return;
|
|
1242
|
-
}
|
|
1243
|
-
try {
|
|
1244
|
-
const loggerModule = await import("winston/lib/winston/logger.js");
|
|
1245
|
-
if ((_b = (_a2 = loggerModule.default) == null ? void 0 : _a2.prototype) == null ? void 0 : _b.write) {
|
|
1246
|
-
const originalWrite = loggerModule.default.prototype.write;
|
|
1247
|
-
loggerModule.default.prototype.write = function(info) {
|
|
1248
|
-
captureLog2(info);
|
|
1249
|
-
return originalWrite.call(this, info);
|
|
1250
|
-
};
|
|
1251
|
-
}
|
|
1252
|
-
} catch {
|
|
1253
|
-
}
|
|
1254
|
-
isPatched2 = true;
|
|
1255
|
-
}
|
|
1256
|
-
__name(patchWinston, "patchWinston");
|
|
1257
|
-
function captureLog2(info) {
|
|
1258
|
-
const logs = globalLogsContext2 == null ? void 0 : globalLogsContext2.getStore();
|
|
1259
|
-
if (!logs || !info || logs.length >= MAX_BUFFER_SIZE2) {
|
|
1260
|
-
return;
|
|
1261
|
-
}
|
|
1262
|
-
try {
|
|
1263
|
-
const rest = removeKeys(info, [
|
|
1264
|
-
"timestamp",
|
|
1265
|
-
"level",
|
|
1266
|
-
"message",
|
|
1267
|
-
"splat"
|
|
1268
|
-
]);
|
|
1269
|
-
const formattedMessage = formatMessage(info.message, rest);
|
|
1270
|
-
if (formattedMessage) {
|
|
1271
|
-
logs.push({
|
|
1272
|
-
timestamp: parseTimestamp(info.timestamp),
|
|
1273
|
-
level: info.level || "info",
|
|
1274
|
-
message: formattedMessage.trim()
|
|
1275
|
-
});
|
|
1276
|
-
}
|
|
1277
|
-
} catch (e) {
|
|
1278
|
-
}
|
|
1279
|
-
}
|
|
1280
|
-
__name(captureLog2, "captureLog");
|
|
1281
|
-
function parseTimestamp(timestamp) {
|
|
1282
|
-
if (timestamp) {
|
|
1283
|
-
const ts = new Date(timestamp).getTime();
|
|
1284
|
-
if (!isNaN(ts)) {
|
|
1285
|
-
return ts / 1e3;
|
|
1286
|
-
}
|
|
1287
|
-
}
|
|
1288
|
-
return Date.now() / 1e3;
|
|
1289
|
-
}
|
|
1290
|
-
__name(parseTimestamp, "parseTimestamp");
|
|
1291
|
-
|
|
1292
|
-
// src/koa/middleware.ts
|
|
3
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
4
|
+
import { ApitallyClient } from "../common/client.js";
|
|
5
|
+
import { consumerFromStringOrObject } from "../common/consumerRegistry.js";
|
|
6
|
+
import { getPackageVersion } from "../common/packageVersions.js";
|
|
7
|
+
import { convertBody, convertHeaders } from "../common/requestLogger.js";
|
|
8
|
+
import { patchConsole, patchWinston } from "../loggers/index.js";
|
|
1293
9
|
function useApitally(app, config) {
|
|
1294
10
|
const client = new ApitallyClient(config);
|
|
1295
11
|
const middleware = getMiddleware(client);
|
|
@@ -1298,6 +14,7 @@ function useApitally(app, config) {
|
|
|
1298
14
|
const appInfo = getAppInfo(app, config.appVersion);
|
|
1299
15
|
if (appInfo.paths.length > 0 || attempt >= 10) {
|
|
1300
16
|
client.setStartupData(appInfo);
|
|
17
|
+
client.startSync();
|
|
1301
18
|
} else {
|
|
1302
19
|
setTimeout(() => setStartupData(attempt + 1), 500);
|
|
1303
20
|
}
|
|
@@ -1317,7 +34,7 @@ function getMiddleware(client) {
|
|
|
1317
34
|
return;
|
|
1318
35
|
}
|
|
1319
36
|
await logsContext.run([], async () => {
|
|
1320
|
-
var
|
|
37
|
+
var _a;
|
|
1321
38
|
let path;
|
|
1322
39
|
let statusCode;
|
|
1323
40
|
let serverError;
|
|
@@ -1330,7 +47,7 @@ function getMiddleware(client) {
|
|
|
1330
47
|
if (path && statusCode === 500 && error instanceof Error) {
|
|
1331
48
|
serverError = error;
|
|
1332
49
|
client.serverErrorCounter.addServerError({
|
|
1333
|
-
consumer: (
|
|
50
|
+
consumer: (_a = getConsumer(ctx)) == null ? void 0 : _a.identifier,
|
|
1334
51
|
method: ctx.request.method,
|
|
1335
52
|
path,
|
|
1336
53
|
type: error.name,
|