@blamejs/core 0.7.18 → 0.7.19

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.
Files changed (167) hide show
  1. package/CHANGELOG.md +425 -423
  2. package/README.md +150 -150
  3. package/bin/blamejs.js +0 -0
  4. package/index.js +310 -308
  5. package/lib/api-key.js +660 -660
  6. package/lib/api-snapshot.js +338 -338
  7. package/lib/app-shutdown.js +385 -385
  8. package/lib/app.js +365 -365
  9. package/lib/archive.js +250 -250
  10. package/lib/atomic-file.js +544 -544
  11. package/lib/audit-chain.js +177 -177
  12. package/lib/audit-sign.js +344 -344
  13. package/lib/audit-tools.js +677 -677
  14. package/lib/audit.js +766 -766
  15. package/lib/auth/jwt-external.js +365 -0
  16. package/lib/auth/jwt.js +337 -311
  17. package/lib/auth/lockout.js +436 -436
  18. package/lib/auth/oauth.js +721 -721
  19. package/lib/auth/passkey.js +181 -181
  20. package/lib/auth/password.js +628 -594
  21. package/lib/backup/bundle.js +217 -217
  22. package/lib/backup/crypto.js +176 -176
  23. package/lib/backup/index.js +515 -515
  24. package/lib/backup/manifest.js +282 -282
  25. package/lib/break-glass.js +1338 -1338
  26. package/lib/bundler.js +441 -441
  27. package/lib/cache-redis.js +256 -256
  28. package/lib/cache.js +1206 -1206
  29. package/lib/canonical-json.js +115 -115
  30. package/lib/chain-writer.js +234 -234
  31. package/lib/cli-helpers.js +206 -206
  32. package/lib/cli.js +2334 -2334
  33. package/lib/cluster-provider-db.js +317 -317
  34. package/lib/cluster-storage.js +226 -226
  35. package/lib/cluster.js +703 -703
  36. package/lib/config-drift.js +301 -301
  37. package/lib/consent.js +222 -222
  38. package/lib/constants.js +191 -191
  39. package/lib/cookies.js +315 -315
  40. package/lib/credential-hash.js +322 -322
  41. package/lib/crypto.js +266 -266
  42. package/lib/csv.js +275 -275
  43. package/lib/db-declare-row-policy.js +267 -267
  44. package/lib/db-declare-view.js +420 -420
  45. package/lib/db-query.js +406 -406
  46. package/lib/db-schema.js +319 -319
  47. package/lib/db.js +1288 -1288
  48. package/lib/deprecate.js +222 -222
  49. package/lib/dev.js +335 -335
  50. package/lib/dual-control.js +473 -473
  51. package/lib/error-page.js +420 -420
  52. package/lib/external-db-migrate.js +441 -441
  53. package/lib/external-db.js +1061 -1061
  54. package/lib/file-type.js +273 -273
  55. package/lib/forms.js +422 -422
  56. package/lib/framework-error.js +293 -293
  57. package/lib/framework-schema.js +717 -717
  58. package/lib/handlers.js +350 -350
  59. package/lib/http-client-cookie-jar.js +508 -508
  60. package/lib/http-client.js +1195 -1195
  61. package/lib/i18n.js +878 -878
  62. package/lib/jobs.js +185 -185
  63. package/lib/log-stream-cloudwatch.js +369 -369
  64. package/lib/log-stream-local.js +146 -146
  65. package/lib/log-stream-otlp-grpc.js +410 -410
  66. package/lib/log-stream-otlp.js +286 -286
  67. package/lib/log-stream-syslog.js +302 -302
  68. package/lib/log-stream-webhook.js +199 -199
  69. package/lib/log-stream.js +330 -330
  70. package/lib/log.js +500 -500
  71. package/lib/mail-bounce.js +528 -528
  72. package/lib/mail-dkim.js +369 -369
  73. package/lib/mail.js +981 -981
  74. package/lib/metrics.js +683 -683
  75. package/lib/middleware/api-encrypt.js +936 -936
  76. package/lib/middleware/attach-user.js +157 -157
  77. package/lib/middleware/bearer-auth.js +152 -0
  78. package/lib/middleware/body-parser.js +1170 -1170
  79. package/lib/middleware/bot-guard.js +178 -178
  80. package/lib/middleware/compression.js +452 -452
  81. package/lib/middleware/cors.js +314 -314
  82. package/lib/middleware/csp-nonce.js +348 -348
  83. package/lib/middleware/csrf-protect.js +316 -316
  84. package/lib/middleware/db-role-for.js +264 -264
  85. package/lib/middleware/health.js +392 -392
  86. package/lib/middleware/index.js +82 -79
  87. package/lib/middleware/rate-limit.js +358 -358
  88. package/lib/middleware/request-id.js +61 -61
  89. package/lib/middleware/request-log.js +168 -168
  90. package/lib/middleware/require-auth.js +104 -104
  91. package/lib/middleware/security-headers.js +116 -116
  92. package/lib/middleware/sse.js +166 -166
  93. package/lib/migrations.js +383 -383
  94. package/lib/mtls-ca.js +518 -518
  95. package/lib/mtls-engine-default.js +481 -481
  96. package/lib/network-dns.js +632 -632
  97. package/lib/network-heartbeat.js +290 -290
  98. package/lib/network-nts.js +574 -574
  99. package/lib/network-proxy.js +265 -265
  100. package/lib/network-tls.js +328 -328
  101. package/lib/network.js +233 -233
  102. package/lib/notify.js +612 -612
  103. package/lib/ntp-check.js +229 -229
  104. package/lib/numeric-bounds.js +111 -111
  105. package/lib/object-store/azure-blob-bucket-ops.js +349 -349
  106. package/lib/object-store/azure-blob.js +488 -488
  107. package/lib/object-store/gcs-bucket-ops.js +351 -351
  108. package/lib/object-store/gcs.js +519 -519
  109. package/lib/object-store/http-put.js +153 -153
  110. package/lib/object-store/index.js +197 -197
  111. package/lib/object-store/sigv4-bucket-ops.js +1092 -1092
  112. package/lib/object-store/sigv4.js +903 -903
  113. package/lib/observability.js +151 -151
  114. package/lib/otel-export.js +269 -269
  115. package/lib/pagination.js +464 -464
  116. package/lib/parsers/index.js +80 -80
  117. package/lib/parsers/safe-env.js +642 -642
  118. package/lib/parsers/safe-ini.js +292 -292
  119. package/lib/parsers/safe-toml.js +784 -784
  120. package/lib/parsers/safe-xml.js +390 -390
  121. package/lib/parsers/safe-yaml.js +1015 -1015
  122. package/lib/permissions.js +708 -708
  123. package/lib/pqc-agent.js +87 -87
  124. package/lib/pqc-gate.js +279 -279
  125. package/lib/protobuf-encoder.js +190 -190
  126. package/lib/protocol-dispatcher.js +161 -161
  127. package/lib/pubsub-redis.js +167 -167
  128. package/lib/pubsub.js +429 -429
  129. package/lib/queue-local.js +476 -476
  130. package/lib/queue-redis.js +745 -745
  131. package/lib/queue-sqs.js +319 -319
  132. package/lib/queue.js +695 -695
  133. package/lib/redis-client.js +519 -519
  134. package/lib/request-helpers.js +340 -340
  135. package/lib/restore-bundle.js +237 -237
  136. package/lib/restore-rollback.js +259 -259
  137. package/lib/restore.js +409 -409
  138. package/lib/retry.js +376 -376
  139. package/lib/router.js +748 -748
  140. package/lib/safe-async.js +735 -735
  141. package/lib/safe-buffer.js +237 -237
  142. package/lib/safe-json.js +541 -541
  143. package/lib/safe-schema.js +1266 -1266
  144. package/lib/safe-url.js +159 -159
  145. package/lib/scheduler.js +706 -706
  146. package/lib/security-assert.js +373 -373
  147. package/lib/seeders.js +618 -618
  148. package/lib/session.js +535 -478
  149. package/lib/slug.js +269 -269
  150. package/lib/ssrf-guard.js +401 -401
  151. package/lib/storage.js +471 -471
  152. package/lib/subject.js +281 -281
  153. package/lib/template.js +791 -791
  154. package/lib/testing.js +798 -798
  155. package/lib/time.js +310 -310
  156. package/lib/totp.js +302 -302
  157. package/lib/tracing.js +494 -494
  158. package/lib/uuid.js +132 -132
  159. package/lib/validate-opts.js +340 -340
  160. package/lib/vault/index.js +308 -308
  161. package/lib/vault/rotate.js +784 -784
  162. package/lib/vault/wrap.js +296 -296
  163. package/lib/vendor/noble-ciphers.cjs +9 -9
  164. package/lib/webhook.js +595 -595
  165. package/lib/websocket.js +1048 -1048
  166. package/package.json +77 -77
  167. package/sbom.cyclonedx.json +7 -7
@@ -1,410 +1,410 @@
1
- "use strict";
2
- /**
3
- * OTLP gRPC log sink — OpenTelemetry Protocol logs over gRPC.
4
- *
5
- * Companion to lib/log-stream-otlp.js (HTTP/JSON). The OTel collector
6
- * accepts both transports; gRPC is the higher-throughput path with
7
- * less per-batch overhead and is preferred for production deployments
8
- * pushing >100K logs/s.
9
- *
10
- * Wire format:
11
- * - HTTP/2 (node:http2) POST to
12
- * /opentelemetry.proto.collector.logs.v1.LogsService/Export
13
- * - content-type: application/grpc+proto
14
- * - te: trailers
15
- * - Body: 1-byte compression flag (0 = none) + 4-byte big-endian
16
- * length + protobuf-encoded ExportLogsServiceRequest
17
- * - Response status: HTTP/2 trailer `grpc-status` (0 = OK)
18
- *
19
- * The protobuf body is encoded by hand via lib/protobuf-encoder.js —
20
- * no schema parser ships in the bundle, consistent with the framework's
21
- * vendoring stance. The OTel logs schema is small and stable
22
- * (https://github.com/open-telemetry/opentelemetry-proto/blob/main/
23
- * opentelemetry/proto/logs/v1/logs.proto).
24
- *
25
- * Severity mapping follows the OTel spec: debug=5 / info=9 / warn=13 /
26
- * error=17.
27
- */
28
- var http2 = require("node:http2");
29
- var C = require("./constants");
30
- var { boot } = require("./log");
31
- var pb = require("./protobuf-encoder");
32
- var safeAsync = require("./safe-async");
33
- var safeUrl = require("./safe-url");
34
- var { tearDownH2Session } = require("./http2-teardown");
35
- var { LogStreamError } = require("./framework-error");
36
-
37
- var _err = LogStreamError.factory;
38
- var _log = boot("log-stream-otlp-grpc");
39
-
40
- var DEFAULTS = {
41
- scopeName: "blamejs",
42
- batchSize: 100,
43
- maxBatchAgeMs: C.TIME.seconds(5),
44
- timeoutMs: C.TIME.seconds(30),
45
- // Ring-buffer cap (event count, not bytes); routed through C.BYTES
46
- // identity passthrough so the file's literal arithmetic has a single
47
- // source of truth.
48
- bufferLimit: C.BYTES.bytes(10000),
49
- };
50
-
51
- var SEVERITY = {
52
- debug: { number: 5, text: "DEBUG" },
53
- info: { number: 9, text: "INFO" },
54
- warn: { number: 13, text: "WARN" },
55
- error: { number: 17, text: "ERROR" },
56
- };
57
-
58
- // ---- Protobuf message shapes (OTel logs.proto) ----
59
- //
60
- // Field numbers match
61
- // https://github.com/open-telemetry/opentelemetry-proto/blob/main/
62
- // opentelemetry/proto/{common,resource,logs}/v1/*.proto
63
- //
64
- // AnyValue (common.proto) — proto3 oneof; we emit one of:
65
- // string_value = 1, bool_value = 2, int_value = 3 (int64 varint),
66
- // double_value = 4, bytes_value = 7
67
- function _encodeAnyValue(v) {
68
- if (v == null) return pb.string(1, ""); // string_value=""
69
- if (typeof v === "string") return pb.string(1, v);
70
- if (typeof v === "boolean")return pb.bool(2, v);
71
- if (typeof v === "number") {
72
- if (Number.isInteger(v) && v >= 0) return pb.uint64(3, v); // int_value (proto3 int64 varint)
73
- return pb.double(4, v); // double_value
74
- }
75
- if (Buffer.isBuffer(v)) return pb.bytes(7, v);
76
- // Fallback — JSON-stringify objects/arrays into string_value rather
77
- // than implementing the full ArrayValue / KeyValueList branches.
78
- // Operators wanting structured nested attributes shape their record
79
- // up-front into top-level scalars or stringified JSON.
80
- try { return pb.string(1, JSON.stringify(v)); }
81
- catch (_e) { return pb.string(1, String(v)); }
82
- }
83
-
84
- // KeyValue (common.proto): key=1 (string), value=2 (AnyValue)
85
- function _encodeKeyValue(key, value) {
86
- return Buffer.concat([
87
- pb.string(1, key),
88
- pb.embeddedMessage(2, _encodeAnyValue(value)),
89
- ]);
90
- }
91
-
92
- function _encodeAttributes(obj) {
93
- if (!obj) return [];
94
- var out = [];
95
- var keys = Object.keys(obj);
96
- for (var i = 0; i < keys.length; i++) {
97
- var k = keys[i];
98
- var v = obj[k];
99
- if (v === undefined) continue;
100
- out.push(_encodeKeyValue(k, v));
101
- }
102
- return out;
103
- }
104
-
105
- // Resource (resource.proto): attributes=1 (repeated KeyValue),
106
- // dropped_attributes_count=2
107
- function _encodeResource(attrs) {
108
- var kvs = _encodeAttributes(attrs);
109
- if (kvs.length === 0) return Buffer.alloc(0);
110
- var pieces = kvs.map(function (kvBody) { return pb.embeddedMessage(1, kvBody); });
111
- return Buffer.concat(pieces);
112
- }
113
-
114
- // InstrumentationScope (common.proto): name=1, version=2
115
- function _encodeScope(name, version) {
116
- return Buffer.concat([
117
- pb.string(1, name || DEFAULTS.scopeName),
118
- pb.string(2, version || ""),
119
- ]);
120
- }
121
-
122
- // LogRecord (logs.proto):
123
- // time_unix_nano = 1 (fixed64)
124
- // severity_number = 2 (enum / varint)
125
- // severity_text = 3 (string)
126
- // body = 5 (AnyValue)
127
- // attributes = 6 (repeated KeyValue)
128
- // observed_time_unix_nano = 11 (fixed64)
129
- function _encodeLogRecord(record) {
130
- var sev = SEVERITY[record.level] || SEVERITY.info;
131
- var tsMs = record.ts || Date.now();
132
- // Convert ms to ns. Use BigInt to avoid 53-bit precision loss on
133
- // operators emitting > year-2255 timestamps. For ms-resolution
134
- // records the LSB nanos are 0; we still send fixed64.
135
- var tsNs = BigInt(tsMs) * 1000000n;
136
- var attrPieces = _encodeAttributes(record.meta).map(function (kvBody) {
137
- return pb.embeddedMessage(6, kvBody);
138
- });
139
- var msg = (record.message != null ? String(record.message) : "");
140
- return Buffer.concat([
141
- pb.fixed64(1, tsNs),
142
- pb.uint32(2, sev.number),
143
- pb.string(3, sev.text),
144
- pb.embeddedMessage(5, pb.string(1, msg)), // body.string_value
145
- Buffer.concat(attrPieces),
146
- pb.fixed64(11, tsNs),
147
- ]);
148
- }
149
-
150
- // ScopeLogs (logs.proto): scope=1 (InstrumentationScope),
151
- // log_records=2 (repeated LogRecord), schema_url=3
152
- function _encodeScopeLogs(records, scopeName, scopeVersion) {
153
- var recordPieces = records.map(function (rec) {
154
- return pb.embeddedMessage(2, _encodeLogRecord(rec));
155
- });
156
- return Buffer.concat([
157
- pb.embeddedMessage(1, _encodeScope(scopeName, scopeVersion)),
158
- Buffer.concat(recordPieces),
159
- ]);
160
- }
161
-
162
- // ResourceLogs (logs.proto): resource=1 (Resource),
163
- // scope_logs=2 (repeated ScopeLogs), schema_url=3
164
- function _encodeResourceLogs(records, cfg) {
165
- var resourceBody = _encodeResource(_resourceAttrs(cfg));
166
- var scopeLogsBody = _encodeScopeLogs(records, cfg.scopeName, cfg.scopeVersion);
167
- return Buffer.concat([
168
- pb.embeddedMessage(1, resourceBody),
169
- pb.embeddedMessage(2, scopeLogsBody),
170
- ]);
171
- }
172
-
173
- // ExportLogsServiceRequest (collector logs.proto):
174
- // resource_logs = 1 (repeated ResourceLogs)
175
- function _encodeExportRequest(records, cfg) {
176
- return pb.embeddedMessage(1, _encodeResourceLogs(records, cfg));
177
- }
178
-
179
- function _resourceAttrs(cfg) {
180
- var attrs = Object.assign({}, cfg.resourceAttributes || {});
181
- if (cfg.serviceName) attrs["service.name"] = cfg.serviceName;
182
- if (cfg.serviceVersion) attrs["service.version"] = cfg.serviceVersion;
183
- return attrs;
184
- }
185
-
186
- // ---- gRPC framing ----
187
-
188
- function _frame(messageBuf) {
189
- // 1 byte (compression flag, 0 = uncompressed) + 4 bytes (length, BE)
190
- // + message bytes.
191
- var hdr = Buffer.alloc(5);
192
- hdr[0] = 0;
193
- hdr.writeUInt32BE(messageBuf.length, 1);
194
- return Buffer.concat([hdr, messageBuf]);
195
- }
196
-
197
- // ---- HTTP/2 client ----
198
- //
199
- // One client per sink instance. The OTLP gRPC server keeps the
200
- // connection alive across many Export calls; we re-create on
201
- // disconnect.
202
- function _makeClient(cfg) {
203
- var url = safeUrl.parse(cfg.url, {
204
- allowedProtocols: cfg.allowedProtocols || safeUrl.ALLOW_HTTP_TLS,
205
- errorClass: LogStreamError,
206
- });
207
- var authority = url.protocol + "//" + url.host;
208
- var sessionOpts = {};
209
- if (cfg.ca) sessionOpts.ca = cfg.ca;
210
- if (cfg.servername) sessionOpts.servername = cfg.servername;
211
- if (cfg.allowInsecure) sessionOpts.rejectUnauthorized = false;
212
- var session = http2.connect(authority, sessionOpts);
213
- session.on("error", function () { /* surfaced through request err */ });
214
- if (typeof session.unref === "function") session.unref();
215
- return session;
216
- }
217
-
218
- function _doExport(session, cfg, records) {
219
- return new Promise(function (resolve, reject) {
220
- var body = _encodeExportRequest(records, cfg);
221
- var framed = _frame(body);
222
-
223
- var headers = Object.assign({
224
- ":method": "POST",
225
- ":path": "/opentelemetry.proto.collector.logs.v1.LogsService/Export",
226
- "content-type": "application/grpc+proto",
227
- "te": "trailers",
228
- "grpc-encoding": "identity",
229
- "grpc-accept-encoding": "identity",
230
- }, cfg.headers || {});
231
-
232
- var req = session.request(headers);
233
- var resStatus = null;
234
- var trailers = null;
235
- var errored = false;
236
-
237
- var timer = setTimeout(function () {
238
- errored = true;
239
- // Best-effort cancel of the in-flight HTTP/2 request — close()
240
- // can throw on an already-torn-down stream; the timeout error
241
- // below is the authoritative signal so we swallow + log only.
242
- try { req.close(http2.constants.NGHTTP2_CANCEL); }
243
- catch (e) { _log.debug("otlp-grpc-timer-cancel: " + (e && e.message || e)); }
244
- reject(_err("ETIMEDOUT",
245
- "otlp-grpc: request timed out after " + cfg.timeoutMs + "ms"));
246
- }, cfg.timeoutMs);
247
-
248
- req.on("response", function (h) {
249
- resStatus = h[":status"];
250
- });
251
- req.on("trailers", function (t) { trailers = t; });
252
- req.on("error", function (e) {
253
- if (errored) return;
254
- errored = true;
255
- clearTimeout(timer);
256
- reject(_err("HTTP2_ERROR", "otlp-grpc: " + (e && e.message || String(e))));
257
- });
258
- req.on("close", function () {
259
- if (errored) return;
260
- clearTimeout(timer);
261
- if (resStatus !== 200) {
262
- return reject(_err("HTTP_ERROR",
263
- "otlp-grpc: HTTP/2 status " + resStatus));
264
- }
265
- // Trailers MUST carry grpc-status. The OTel collector returns
266
- // grpc-status: 0 on success.
267
- var grpcStatus = trailers && trailers["grpc-status"];
268
- var grpcMessage = trailers && trailers["grpc-message"];
269
- if (grpcStatus === undefined) {
270
- return reject(_err("HTTP_ERROR",
271
- "otlp-grpc: response missing grpc-status trailer"));
272
- }
273
- if (String(grpcStatus) !== "0") {
274
- return reject(_err("HTTP_ERROR",
275
- "otlp-grpc: grpc-status " + grpcStatus +
276
- (grpcMessage ? " — " + grpcMessage : "")));
277
- }
278
- resolve();
279
- });
280
-
281
- req.end(framed);
282
- });
283
- }
284
-
285
- // ---- Public sink factory ----
286
-
287
- function create(config) {
288
- if (!config || !config.url) {
289
- throw _err("BAD_OPT", "log-stream otlp-grpc: { url } is required");
290
- }
291
- // Reject http:// without explicit allowInsecure — gRPC endpoints
292
- // are virtually always TLS in real deployments.
293
- var allowedProtocols = config.allowedProtocols || safeUrl.ALLOW_HTTP_TLS;
294
- safeUrl.parse(config.url, {
295
- allowedProtocols: allowedProtocols,
296
- errorClass: LogStreamError,
297
- });
298
-
299
- var cfg = Object.assign({}, DEFAULTS, config);
300
- cfg.batchSize = Number(cfg.batchSize) || DEFAULTS.batchSize;
301
- cfg.maxBatchAgeMs = Number(cfg.maxBatchAgeMs) || DEFAULTS.maxBatchAgeMs;
302
- cfg.timeoutMs = Number(cfg.timeoutMs) || DEFAULTS.timeoutMs;
303
- cfg.bufferLimit = Number(cfg.bufferLimit) || DEFAULTS.bufferLimit;
304
-
305
- var onDrop = typeof config.onDrop === "function" ? config.onDrop : null;
306
- var _emitDrop = safeAsync.makeDropCallback(onDrop,
307
- function (e) { _log.debug("otlp-grpc-onDrop-threw: " + (e && e.message || e)); });
308
-
309
- var buffer = [];
310
- var inFlight = false;
311
- var closed = false;
312
- var session = null;
313
- var inflightPromise = null;
314
- var flushScheduler = safeAsync.makeScheduledFlush(cfg.maxBatchAgeMs, function () { return _flush(); });
315
-
316
- function _ensureSession() {
317
- if (session && !session.destroyed) return session;
318
- session = _makeClient(cfg);
319
- return session;
320
- }
321
-
322
- async function _flush() {
323
- if (inFlight || buffer.length === 0) return;
324
- inFlight = true;
325
- try {
326
- while (buffer.length > 0 && !closed) {
327
- var batch = buffer.splice(0, cfg.batchSize);
328
- try {
329
- var s = _ensureSession();
330
- inflightPromise = _doExport(s, cfg, batch);
331
- await inflightPromise;
332
- } catch (e) {
333
- _emitDrop("send-failed", batch, e);
334
- } finally {
335
- inflightPromise = null;
336
- }
337
- }
338
- } finally {
339
- inFlight = false;
340
- }
341
- }
342
-
343
- function emit(record) {
344
- if (closed) {
345
- _emitDrop("sink-closed", [record], null);
346
- return Promise.resolve({ accepted: false, reason: "closed" });
347
- }
348
- if (buffer.length >= cfg.bufferLimit) {
349
- var dropped = buffer.shift();
350
- _emitDrop("overflow", [dropped], null);
351
- }
352
- buffer.push(record);
353
- if (buffer.length >= cfg.batchSize) {
354
- // Drain immediately; don't await — emit is fire-and-forget on the
355
- // hot path. Errors surface via onDrop.
356
- _flush().catch(function () {});
357
- } else if (!closed) {
358
- flushScheduler.schedule();
359
- }
360
- return Promise.resolve({ accepted: true, queued: buffer.length });
361
- }
362
-
363
- async function close() {
364
- closed = true;
365
- flushScheduler.cancel();
366
- // Drain the in-flight Export before tearing down the HTTP/2 session.
367
- // The promise rejection (if any) was already routed through onDrop
368
- // by _flush — we only await here to avoid racing the teardown.
369
- if (inflightPromise) {
370
- try { await inflightPromise; }
371
- catch (e) { _log.debug("otlp-grpc-close-drain: " + (e && e.message || e)); }
372
- }
373
- var pending = buffer.splice(0, buffer.length);
374
- if (pending.length > 0) {
375
- try {
376
- var s = _ensureSession();
377
- await _doExport(s, cfg, pending);
378
- } catch (e) {
379
- _emitDrop("send-failed", pending, e);
380
- }
381
- }
382
- // Tear down the HTTP/2 session via the shared close+destroy helper.
383
- // By this point we've already awaited inflightPromise and run a
384
- // final _doExport for any buffered records, so there's nothing left
385
- // to drain. See lib/http2-teardown.js for the rationale on why
386
- // close() alone leaves the underlying socket connected on Linux.
387
- tearDownH2Session(session);
388
- session = null;
389
- }
390
-
391
- return {
392
- protocol: "otlp-grpc",
393
- emit: emit,
394
- close: close,
395
- // Test hooks — encode without sending.
396
- _encodeForTest: function (records) { return _encodeExportRequest(records, cfg); },
397
- _frameForTest: _frame,
398
- };
399
- }
400
-
401
- module.exports = {
402
- create: create,
403
- // Exposed for layer-0 tests that verify the wire encoding without
404
- // standing up an HTTP/2 server.
405
- _encodeAnyValue: _encodeAnyValue,
406
- _encodeKeyValue: _encodeKeyValue,
407
- _encodeLogRecord: _encodeLogRecord,
408
- _encodeExportRequest: _encodeExportRequest,
409
- _frame: _frame,
410
- };
1
+ "use strict";
2
+ /**
3
+ * OTLP gRPC log sink — OpenTelemetry Protocol logs over gRPC.
4
+ *
5
+ * Companion to lib/log-stream-otlp.js (HTTP/JSON). The OTel collector
6
+ * accepts both transports; gRPC is the higher-throughput path with
7
+ * less per-batch overhead and is preferred for production deployments
8
+ * pushing >100K logs/s.
9
+ *
10
+ * Wire format:
11
+ * - HTTP/2 (node:http2) POST to
12
+ * /opentelemetry.proto.collector.logs.v1.LogsService/Export
13
+ * - content-type: application/grpc+proto
14
+ * - te: trailers
15
+ * - Body: 1-byte compression flag (0 = none) + 4-byte big-endian
16
+ * length + protobuf-encoded ExportLogsServiceRequest
17
+ * - Response status: HTTP/2 trailer `grpc-status` (0 = OK)
18
+ *
19
+ * The protobuf body is encoded by hand via lib/protobuf-encoder.js —
20
+ * no schema parser ships in the bundle, consistent with the framework's
21
+ * vendoring stance. The OTel logs schema is small and stable
22
+ * (https://github.com/open-telemetry/opentelemetry-proto/blob/main/
23
+ * opentelemetry/proto/logs/v1/logs.proto).
24
+ *
25
+ * Severity mapping follows the OTel spec: debug=5 / info=9 / warn=13 /
26
+ * error=17.
27
+ */
28
+ var http2 = require("node:http2");
29
+ var C = require("./constants");
30
+ var { boot } = require("./log");
31
+ var pb = require("./protobuf-encoder");
32
+ var safeAsync = require("./safe-async");
33
+ var safeUrl = require("./safe-url");
34
+ var { tearDownH2Session } = require("./http2-teardown");
35
+ var { LogStreamError } = require("./framework-error");
36
+
37
+ var _err = LogStreamError.factory;
38
+ var _log = boot("log-stream-otlp-grpc");
39
+
40
+ var DEFAULTS = {
41
+ scopeName: "blamejs",
42
+ batchSize: 100,
43
+ maxBatchAgeMs: C.TIME.seconds(5),
44
+ timeoutMs: C.TIME.seconds(30),
45
+ // Ring-buffer cap (event count, not bytes); routed through C.BYTES
46
+ // identity passthrough so the file's literal arithmetic has a single
47
+ // source of truth.
48
+ bufferLimit: C.BYTES.bytes(10000),
49
+ };
50
+
51
+ var SEVERITY = {
52
+ debug: { number: 5, text: "DEBUG" },
53
+ info: { number: 9, text: "INFO" },
54
+ warn: { number: 13, text: "WARN" },
55
+ error: { number: 17, text: "ERROR" },
56
+ };
57
+
58
+ // ---- Protobuf message shapes (OTel logs.proto) ----
59
+ //
60
+ // Field numbers match
61
+ // https://github.com/open-telemetry/opentelemetry-proto/blob/main/
62
+ // opentelemetry/proto/{common,resource,logs}/v1/*.proto
63
+ //
64
+ // AnyValue (common.proto) — proto3 oneof; we emit one of:
65
+ // string_value = 1, bool_value = 2, int_value = 3 (int64 varint),
66
+ // double_value = 4, bytes_value = 7
67
+ function _encodeAnyValue(v) {
68
+ if (v == null) return pb.string(1, ""); // string_value=""
69
+ if (typeof v === "string") return pb.string(1, v);
70
+ if (typeof v === "boolean")return pb.bool(2, v);
71
+ if (typeof v === "number") {
72
+ if (Number.isInteger(v) && v >= 0) return pb.uint64(3, v); // int_value (proto3 int64 varint)
73
+ return pb.double(4, v); // double_value
74
+ }
75
+ if (Buffer.isBuffer(v)) return pb.bytes(7, v);
76
+ // Fallback — JSON-stringify objects/arrays into string_value rather
77
+ // than implementing the full ArrayValue / KeyValueList branches.
78
+ // Operators wanting structured nested attributes shape their record
79
+ // up-front into top-level scalars or stringified JSON.
80
+ try { return pb.string(1, JSON.stringify(v)); }
81
+ catch (_e) { return pb.string(1, String(v)); }
82
+ }
83
+
84
+ // KeyValue (common.proto): key=1 (string), value=2 (AnyValue)
85
+ function _encodeKeyValue(key, value) {
86
+ return Buffer.concat([
87
+ pb.string(1, key),
88
+ pb.embeddedMessage(2, _encodeAnyValue(value)),
89
+ ]);
90
+ }
91
+
92
+ function _encodeAttributes(obj) {
93
+ if (!obj) return [];
94
+ var out = [];
95
+ var keys = Object.keys(obj);
96
+ for (var i = 0; i < keys.length; i++) {
97
+ var k = keys[i];
98
+ var v = obj[k];
99
+ if (v === undefined) continue;
100
+ out.push(_encodeKeyValue(k, v));
101
+ }
102
+ return out;
103
+ }
104
+
105
+ // Resource (resource.proto): attributes=1 (repeated KeyValue),
106
+ // dropped_attributes_count=2
107
+ function _encodeResource(attrs) {
108
+ var kvs = _encodeAttributes(attrs);
109
+ if (kvs.length === 0) return Buffer.alloc(0);
110
+ var pieces = kvs.map(function (kvBody) { return pb.embeddedMessage(1, kvBody); });
111
+ return Buffer.concat(pieces);
112
+ }
113
+
114
+ // InstrumentationScope (common.proto): name=1, version=2
115
+ function _encodeScope(name, version) {
116
+ return Buffer.concat([
117
+ pb.string(1, name || DEFAULTS.scopeName),
118
+ pb.string(2, version || ""),
119
+ ]);
120
+ }
121
+
122
+ // LogRecord (logs.proto):
123
+ // time_unix_nano = 1 (fixed64)
124
+ // severity_number = 2 (enum / varint)
125
+ // severity_text = 3 (string)
126
+ // body = 5 (AnyValue)
127
+ // attributes = 6 (repeated KeyValue)
128
+ // observed_time_unix_nano = 11 (fixed64)
129
+ function _encodeLogRecord(record) {
130
+ var sev = SEVERITY[record.level] || SEVERITY.info;
131
+ var tsMs = record.ts || Date.now();
132
+ // Convert ms to ns. Use BigInt to avoid 53-bit precision loss on
133
+ // operators emitting > year-2255 timestamps. For ms-resolution
134
+ // records the LSB nanos are 0; we still send fixed64.
135
+ var tsNs = BigInt(tsMs) * 1000000n;
136
+ var attrPieces = _encodeAttributes(record.meta).map(function (kvBody) {
137
+ return pb.embeddedMessage(6, kvBody);
138
+ });
139
+ var msg = (record.message != null ? String(record.message) : "");
140
+ return Buffer.concat([
141
+ pb.fixed64(1, tsNs),
142
+ pb.uint32(2, sev.number),
143
+ pb.string(3, sev.text),
144
+ pb.embeddedMessage(5, pb.string(1, msg)), // body.string_value
145
+ Buffer.concat(attrPieces),
146
+ pb.fixed64(11, tsNs),
147
+ ]);
148
+ }
149
+
150
+ // ScopeLogs (logs.proto): scope=1 (InstrumentationScope),
151
+ // log_records=2 (repeated LogRecord), schema_url=3
152
+ function _encodeScopeLogs(records, scopeName, scopeVersion) {
153
+ var recordPieces = records.map(function (rec) {
154
+ return pb.embeddedMessage(2, _encodeLogRecord(rec));
155
+ });
156
+ return Buffer.concat([
157
+ pb.embeddedMessage(1, _encodeScope(scopeName, scopeVersion)),
158
+ Buffer.concat(recordPieces),
159
+ ]);
160
+ }
161
+
162
+ // ResourceLogs (logs.proto): resource=1 (Resource),
163
+ // scope_logs=2 (repeated ScopeLogs), schema_url=3
164
+ function _encodeResourceLogs(records, cfg) {
165
+ var resourceBody = _encodeResource(_resourceAttrs(cfg));
166
+ var scopeLogsBody = _encodeScopeLogs(records, cfg.scopeName, cfg.scopeVersion);
167
+ return Buffer.concat([
168
+ pb.embeddedMessage(1, resourceBody),
169
+ pb.embeddedMessage(2, scopeLogsBody),
170
+ ]);
171
+ }
172
+
173
+ // ExportLogsServiceRequest (collector logs.proto):
174
+ // resource_logs = 1 (repeated ResourceLogs)
175
+ function _encodeExportRequest(records, cfg) {
176
+ return pb.embeddedMessage(1, _encodeResourceLogs(records, cfg));
177
+ }
178
+
179
+ function _resourceAttrs(cfg) {
180
+ var attrs = Object.assign({}, cfg.resourceAttributes || {});
181
+ if (cfg.serviceName) attrs["service.name"] = cfg.serviceName;
182
+ if (cfg.serviceVersion) attrs["service.version"] = cfg.serviceVersion;
183
+ return attrs;
184
+ }
185
+
186
+ // ---- gRPC framing ----
187
+
188
+ function _frame(messageBuf) {
189
+ // 1 byte (compression flag, 0 = uncompressed) + 4 bytes (length, BE)
190
+ // + message bytes.
191
+ var hdr = Buffer.alloc(5);
192
+ hdr[0] = 0;
193
+ hdr.writeUInt32BE(messageBuf.length, 1);
194
+ return Buffer.concat([hdr, messageBuf]);
195
+ }
196
+
197
+ // ---- HTTP/2 client ----
198
+ //
199
+ // One client per sink instance. The OTLP gRPC server keeps the
200
+ // connection alive across many Export calls; we re-create on
201
+ // disconnect.
202
+ function _makeClient(cfg) {
203
+ var url = safeUrl.parse(cfg.url, {
204
+ allowedProtocols: cfg.allowedProtocols || safeUrl.ALLOW_HTTP_TLS,
205
+ errorClass: LogStreamError,
206
+ });
207
+ var authority = url.protocol + "//" + url.host;
208
+ var sessionOpts = {};
209
+ if (cfg.ca) sessionOpts.ca = cfg.ca;
210
+ if (cfg.servername) sessionOpts.servername = cfg.servername;
211
+ if (cfg.allowInsecure) sessionOpts.rejectUnauthorized = false;
212
+ var session = http2.connect(authority, sessionOpts);
213
+ session.on("error", function () { /* surfaced through request err */ });
214
+ if (typeof session.unref === "function") session.unref();
215
+ return session;
216
+ }
217
+
218
+ function _doExport(session, cfg, records) {
219
+ return new Promise(function (resolve, reject) {
220
+ var body = _encodeExportRequest(records, cfg);
221
+ var framed = _frame(body);
222
+
223
+ var headers = Object.assign({
224
+ ":method": "POST",
225
+ ":path": "/opentelemetry.proto.collector.logs.v1.LogsService/Export",
226
+ "content-type": "application/grpc+proto",
227
+ "te": "trailers",
228
+ "grpc-encoding": "identity",
229
+ "grpc-accept-encoding": "identity",
230
+ }, cfg.headers || {});
231
+
232
+ var req = session.request(headers);
233
+ var resStatus = null;
234
+ var trailers = null;
235
+ var errored = false;
236
+
237
+ var timer = setTimeout(function () {
238
+ errored = true;
239
+ // Best-effort cancel of the in-flight HTTP/2 request — close()
240
+ // can throw on an already-torn-down stream; the timeout error
241
+ // below is the authoritative signal so we swallow + log only.
242
+ try { req.close(http2.constants.NGHTTP2_CANCEL); }
243
+ catch (e) { _log.debug("otlp-grpc-timer-cancel: " + (e && e.message || e)); }
244
+ reject(_err("ETIMEDOUT",
245
+ "otlp-grpc: request timed out after " + cfg.timeoutMs + "ms"));
246
+ }, cfg.timeoutMs);
247
+
248
+ req.on("response", function (h) {
249
+ resStatus = h[":status"];
250
+ });
251
+ req.on("trailers", function (t) { trailers = t; });
252
+ req.on("error", function (e) {
253
+ if (errored) return;
254
+ errored = true;
255
+ clearTimeout(timer);
256
+ reject(_err("HTTP2_ERROR", "otlp-grpc: " + (e && e.message || String(e))));
257
+ });
258
+ req.on("close", function () {
259
+ if (errored) return;
260
+ clearTimeout(timer);
261
+ if (resStatus !== 200) {
262
+ return reject(_err("HTTP_ERROR",
263
+ "otlp-grpc: HTTP/2 status " + resStatus));
264
+ }
265
+ // Trailers MUST carry grpc-status. The OTel collector returns
266
+ // grpc-status: 0 on success.
267
+ var grpcStatus = trailers && trailers["grpc-status"];
268
+ var grpcMessage = trailers && trailers["grpc-message"];
269
+ if (grpcStatus === undefined) {
270
+ return reject(_err("HTTP_ERROR",
271
+ "otlp-grpc: response missing grpc-status trailer"));
272
+ }
273
+ if (String(grpcStatus) !== "0") {
274
+ return reject(_err("HTTP_ERROR",
275
+ "otlp-grpc: grpc-status " + grpcStatus +
276
+ (grpcMessage ? " — " + grpcMessage : "")));
277
+ }
278
+ resolve();
279
+ });
280
+
281
+ req.end(framed);
282
+ });
283
+ }
284
+
285
+ // ---- Public sink factory ----
286
+
287
+ function create(config) {
288
+ if (!config || !config.url) {
289
+ throw _err("BAD_OPT", "log-stream otlp-grpc: { url } is required");
290
+ }
291
+ // Reject http:// without explicit allowInsecure — gRPC endpoints
292
+ // are virtually always TLS in real deployments.
293
+ var allowedProtocols = config.allowedProtocols || safeUrl.ALLOW_HTTP_TLS;
294
+ safeUrl.parse(config.url, {
295
+ allowedProtocols: allowedProtocols,
296
+ errorClass: LogStreamError,
297
+ });
298
+
299
+ var cfg = Object.assign({}, DEFAULTS, config);
300
+ cfg.batchSize = Number(cfg.batchSize) || DEFAULTS.batchSize;
301
+ cfg.maxBatchAgeMs = Number(cfg.maxBatchAgeMs) || DEFAULTS.maxBatchAgeMs;
302
+ cfg.timeoutMs = Number(cfg.timeoutMs) || DEFAULTS.timeoutMs;
303
+ cfg.bufferLimit = Number(cfg.bufferLimit) || DEFAULTS.bufferLimit;
304
+
305
+ var onDrop = typeof config.onDrop === "function" ? config.onDrop : null;
306
+ var _emitDrop = safeAsync.makeDropCallback(onDrop,
307
+ function (e) { _log.debug("otlp-grpc-onDrop-threw: " + (e && e.message || e)); });
308
+
309
+ var buffer = [];
310
+ var inFlight = false;
311
+ var closed = false;
312
+ var session = null;
313
+ var inflightPromise = null;
314
+ var flushScheduler = safeAsync.makeScheduledFlush(cfg.maxBatchAgeMs, function () { return _flush(); });
315
+
316
+ function _ensureSession() {
317
+ if (session && !session.destroyed) return session;
318
+ session = _makeClient(cfg);
319
+ return session;
320
+ }
321
+
322
+ async function _flush() {
323
+ if (inFlight || buffer.length === 0) return;
324
+ inFlight = true;
325
+ try {
326
+ while (buffer.length > 0 && !closed) {
327
+ var batch = buffer.splice(0, cfg.batchSize);
328
+ try {
329
+ var s = _ensureSession();
330
+ inflightPromise = _doExport(s, cfg, batch);
331
+ await inflightPromise;
332
+ } catch (e) {
333
+ _emitDrop("send-failed", batch, e);
334
+ } finally {
335
+ inflightPromise = null;
336
+ }
337
+ }
338
+ } finally {
339
+ inFlight = false;
340
+ }
341
+ }
342
+
343
+ function emit(record) {
344
+ if (closed) {
345
+ _emitDrop("sink-closed", [record], null);
346
+ return Promise.resolve({ accepted: false, reason: "closed" });
347
+ }
348
+ if (buffer.length >= cfg.bufferLimit) {
349
+ var dropped = buffer.shift();
350
+ _emitDrop("overflow", [dropped], null);
351
+ }
352
+ buffer.push(record);
353
+ if (buffer.length >= cfg.batchSize) {
354
+ // Drain immediately; don't await — emit is fire-and-forget on the
355
+ // hot path. Errors surface via onDrop.
356
+ _flush().catch(function () {});
357
+ } else if (!closed) {
358
+ flushScheduler.schedule();
359
+ }
360
+ return Promise.resolve({ accepted: true, queued: buffer.length });
361
+ }
362
+
363
+ async function close() {
364
+ closed = true;
365
+ flushScheduler.cancel();
366
+ // Drain the in-flight Export before tearing down the HTTP/2 session.
367
+ // The promise rejection (if any) was already routed through onDrop
368
+ // by _flush — we only await here to avoid racing the teardown.
369
+ if (inflightPromise) {
370
+ try { await inflightPromise; }
371
+ catch (e) { _log.debug("otlp-grpc-close-drain: " + (e && e.message || e)); }
372
+ }
373
+ var pending = buffer.splice(0, buffer.length);
374
+ if (pending.length > 0) {
375
+ try {
376
+ var s = _ensureSession();
377
+ await _doExport(s, cfg, pending);
378
+ } catch (e) {
379
+ _emitDrop("send-failed", pending, e);
380
+ }
381
+ }
382
+ // Tear down the HTTP/2 session via the shared close+destroy helper.
383
+ // By this point we've already awaited inflightPromise and run a
384
+ // final _doExport for any buffered records, so there's nothing left
385
+ // to drain. See lib/http2-teardown.js for the rationale on why
386
+ // close() alone leaves the underlying socket connected on Linux.
387
+ tearDownH2Session(session);
388
+ session = null;
389
+ }
390
+
391
+ return {
392
+ protocol: "otlp-grpc",
393
+ emit: emit,
394
+ close: close,
395
+ // Test hooks — encode without sending.
396
+ _encodeForTest: function (records) { return _encodeExportRequest(records, cfg); },
397
+ _frameForTest: _frame,
398
+ };
399
+ }
400
+
401
+ module.exports = {
402
+ create: create,
403
+ // Exposed for layer-0 tests that verify the wire encoding without
404
+ // standing up an HTTP/2 server.
405
+ _encodeAnyValue: _encodeAnyValue,
406
+ _encodeKeyValue: _encodeKeyValue,
407
+ _encodeLogRecord: _encodeLogRecord,
408
+ _encodeExportRequest: _encodeExportRequest,
409
+ _frame: _frame,
410
+ };