@outray/runtime-shared 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +4 -2
- package/dist/buffered-logger.cjs +102 -0
- package/dist/buffered-logger.cjs.map +1 -0
- package/dist/buffered-logger.d.cts +26 -0
- package/dist/buffered-logger.d.ts +26 -0
- package/dist/buffered-logger.js +77 -0
- package/dist/buffered-logger.js.map +1 -0
- package/dist/chunk-7Z55KL36.js +1111 -0
- package/dist/chunk-7Z55KL36.js.map +1 -0
- package/dist/chunk-EBXUVSOO.js +37 -0
- package/dist/chunk-EBXUVSOO.js.map +1 -0
- package/dist/chunk-XAMVXW6B.js +19 -0
- package/dist/chunk-XAMVXW6B.js.map +1 -0
- package/dist/durable-queue.cjs +316 -0
- package/dist/durable-queue.cjs.map +1 -0
- package/dist/durable-queue.d.cts +41 -0
- package/dist/durable-queue.d.ts +41 -0
- package/dist/durable-queue.js +290 -0
- package/dist/durable-queue.js.map +1 -0
- package/dist/helpers.cjs +1167 -0
- package/dist/helpers.cjs.map +1 -0
- package/dist/helpers.d.cts +6 -0
- package/dist/helpers.d.ts +6 -0
- package/dist/helpers.js +14 -0
- package/dist/helpers.js.map +1 -0
- package/dist/postgres-ssl.cjs +37 -0
- package/dist/postgres-ssl.cjs.map +1 -0
- package/dist/postgres-ssl.d.cts +9 -0
- package/dist/postgres-ssl.d.ts +9 -0
- package/dist/postgres-ssl.js +12 -0
- package/dist/postgres-ssl.js.map +1 -0
- package/dist/protocol.cjs +38 -0
- package/dist/protocol.cjs.map +1 -0
- package/dist/protocol.d.cts +8 -0
- package/dist/protocol.d.ts +8 -0
- package/dist/protocol.js +13 -0
- package/dist/protocol.js.map +1 -0
- package/dist/public-hosts.cjs +116 -0
- package/dist/public-hosts.cjs.map +1 -0
- package/dist/public-hosts.d.cts +29 -0
- package/dist/public-hosts.d.ts +29 -0
- package/dist/public-hosts.js +82 -0
- package/dist/public-hosts.js.map +1 -0
- package/dist/tinybird-records.cjs +160 -0
- package/dist/tinybird-records.cjs.map +1 -0
- package/dist/tinybird-records.d.cts +62 -0
- package/dist/tinybird-records.d.ts +62 -0
- package/dist/tinybird-records.js +129 -0
- package/dist/tinybird-records.js.map +1 -0
- package/dist/tunnel-accounting.cjs +71 -0
- package/dist/tunnel-accounting.cjs.map +1 -0
- package/dist/tunnel-accounting.d.cts +12 -0
- package/dist/tunnel-accounting.d.ts +12 -0
- package/dist/tunnel-accounting.js +32 -0
- package/dist/tunnel-accounting.js.map +1 -0
- package/dist/tunnel-product-usage.cjs +107 -0
- package/dist/tunnel-product-usage.cjs.map +1 -0
- package/dist/tunnel-product-usage.d.cts +26 -0
- package/dist/tunnel-product-usage.d.ts +26 -0
- package/dist/tunnel-product-usage.js +81 -0
- package/dist/tunnel-product-usage.js.map +1 -0
- package/dist/types.cjs +19 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.cts +105 -0
- package/dist/types.d.ts +105 -0
- package/dist/types.js +1 -0
- package/dist/types.js.map +1 -0
- package/dist/usage-writer.cjs +43 -0
- package/dist/usage-writer.cjs.map +1 -0
- package/dist/usage-writer.d.cts +14 -0
- package/dist/usage-writer.d.ts +14 -0
- package/dist/usage-writer.js +7 -0
- package/dist/usage-writer.js.map +1 -0
- package/dist/utils.cjs +1171 -0
- package/dist/utils.cjs.map +1 -0
- package/dist/utils.d.cts +2 -0
- package/dist/utils.d.ts +2 -0
- package/dist/utils.js +19 -0
- package/dist/utils.js.map +1 -0
- package/dist/words.cjs +1136 -0
- package/dist/words.cjs.map +1 -0
- package/dist/words.d.cts +4 -0
- package/dist/words.d.ts +4 -0
- package/dist/words.js +9 -0
- package/dist/words.js.map +1 -0
- package/package.json +99 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 OutRay contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
9
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
|
10
|
+
so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @outray/runtime-shared
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
MIT-licensed server primitives shared by the public edge and private platform. Use explicit subpaths for protocol types, host/TLS helpers, bounded Redis queues and payload-free tunnel accounting. This is not a client instrumentation SDK.
|
|
4
|
+
|
|
5
|
+
Product catalogs, entitlement policies and database schemas are deliberately excluded. Accounting preserves the existing `record_product_usage` SQL-function contract; this package does not provision a platform database.
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/buffered-logger.ts
|
|
21
|
+
var buffered_logger_exports = {};
|
|
22
|
+
__export(buffered_logger_exports, {
|
|
23
|
+
BufferedAnalyticsLogger: () => BufferedAnalyticsLogger
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(buffered_logger_exports);
|
|
26
|
+
var BufferedAnalyticsLogger = class {
|
|
27
|
+
constructor(queue, name, batchSize, flushIntervalMs, maxBufferBytes = 16 * 1024 * 1024, maxRecordBytes = 0) {
|
|
28
|
+
this.queue = queue;
|
|
29
|
+
this.name = name;
|
|
30
|
+
this.batchSize = batchSize;
|
|
31
|
+
this.flushIntervalMs = flushIntervalMs;
|
|
32
|
+
this.maxBufferBytes = maxBufferBytes;
|
|
33
|
+
this.maxRecordBytes = maxRecordBytes;
|
|
34
|
+
}
|
|
35
|
+
queue;
|
|
36
|
+
name;
|
|
37
|
+
batchSize;
|
|
38
|
+
flushIntervalMs;
|
|
39
|
+
maxBufferBytes;
|
|
40
|
+
maxRecordBytes;
|
|
41
|
+
buffer = [];
|
|
42
|
+
bufferBytes = 0;
|
|
43
|
+
flushing = null;
|
|
44
|
+
interval = null;
|
|
45
|
+
start(deliver = true) {
|
|
46
|
+
if (this.interval) return;
|
|
47
|
+
if (deliver) this.queue.start();
|
|
48
|
+
this.interval = setInterval(() => {
|
|
49
|
+
void this.flush().catch(() => {
|
|
50
|
+
console.error(`${this.name} analytics could not reach Redis; buffered records will retry`);
|
|
51
|
+
});
|
|
52
|
+
}, this.flushIntervalMs);
|
|
53
|
+
this.interval.unref();
|
|
54
|
+
}
|
|
55
|
+
log(record) {
|
|
56
|
+
const bytes = Buffer.byteLength(JSON.stringify(record));
|
|
57
|
+
if (this.maxRecordBytes && bytes > this.maxRecordBytes) {
|
|
58
|
+
console.error(`${this.name} analytics record exceeds the capture size limit; only this record was rejected`);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
if (this.bufferBytes + bytes > this.maxBufferBytes) {
|
|
62
|
+
console.error(`${this.name} analytics buffer is full; one record was rejected`);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
this.buffer.push({ record, bytes });
|
|
66
|
+
this.bufferBytes += bytes;
|
|
67
|
+
if (this.buffer.length >= this.batchSize || this.bufferBytes >= 4 * 1024 * 1024) {
|
|
68
|
+
void this.flush().catch(() => {
|
|
69
|
+
console.error(`${this.name} analytics could not reach Redis; buffered records will retry`);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
flush() {
|
|
74
|
+
if (this.flushing) return this.flushing;
|
|
75
|
+
this.flushing = this.flushPending().finally(() => {
|
|
76
|
+
this.flushing = null;
|
|
77
|
+
});
|
|
78
|
+
return this.flushing;
|
|
79
|
+
}
|
|
80
|
+
async flushPending() {
|
|
81
|
+
while (this.buffer.length) {
|
|
82
|
+
const batch = this.buffer.slice(0, this.batchSize);
|
|
83
|
+
await this.queue.enqueue(batch.map(({ record }) => record));
|
|
84
|
+
this.buffer.splice(0, batch.length);
|
|
85
|
+
this.bufferBytes -= batch.reduce((total, item) => total + item.bytes, 0);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
async shutdown() {
|
|
89
|
+
if (this.interval) clearInterval(this.interval);
|
|
90
|
+
this.interval = null;
|
|
91
|
+
try {
|
|
92
|
+
await this.flush();
|
|
93
|
+
} finally {
|
|
94
|
+
await this.queue.close();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
99
|
+
0 && (module.exports = {
|
|
100
|
+
BufferedAnalyticsLogger
|
|
101
|
+
});
|
|
102
|
+
//# sourceMappingURL=buffered-logger.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/buffered-logger.ts"],"sourcesContent":["export interface AnalyticsQueue<T> {\n enqueue(records: T[]): Promise<void>;\n start(): void;\n close(): Promise<void>;\n}\n\n/** Retain failed Redis writes, bound memory, and serialize flushes. */\nexport class BufferedAnalyticsLogger<T> {\n private readonly buffer: { record: T; bytes: number }[] = [];\n private bufferBytes = 0;\n private flushing: Promise<void> | null = null;\n private interval: NodeJS.Timeout | null = null;\n\n constructor(\n private readonly queue: AnalyticsQueue<T>,\n private readonly name: string,\n private readonly batchSize: number,\n private readonly flushIntervalMs: number,\n private readonly maxBufferBytes = 16 * 1024 * 1024,\n private readonly maxRecordBytes = 0,\n ) {}\n\n start(deliver = true): void {\n if (this.interval) return;\n if (deliver) this.queue.start();\n this.interval = setInterval(() => {\n void this.flush().catch(() => {\n console.error(`${this.name} analytics could not reach Redis; buffered records will retry`);\n });\n }, this.flushIntervalMs);\n this.interval.unref();\n }\n\n log(record: T): void {\n const bytes = Buffer.byteLength(JSON.stringify(record));\n if (this.maxRecordBytes && bytes > this.maxRecordBytes) {\n console.error(`${this.name} analytics record exceeds the capture size limit; only this record was rejected`);\n return;\n }\n if (this.bufferBytes + bytes > this.maxBufferBytes) {\n // A bounded in-memory fallback cannot safely absorb an unlimited outage.\n // Make overload explicit without exposing request data in the error.\n console.error(`${this.name} analytics buffer is full; one record was rejected`);\n return;\n }\n this.buffer.push({ record, bytes });\n this.bufferBytes += bytes;\n // Captures can contain two large base64 bodies. Flush on bytes as well as\n // rows so a normal low-count burst does not fill the fallback buffer.\n if (this.buffer.length >= this.batchSize || this.bufferBytes >= 4 * 1024 * 1024) {\n void this.flush().catch(() => {\n console.error(`${this.name} analytics could not reach Redis; buffered records will retry`);\n });\n }\n }\n\n flush(): Promise<void> {\n if (this.flushing) return this.flushing;\n this.flushing = this.flushPending().finally(() => { this.flushing = null; });\n return this.flushing;\n }\n\n private async flushPending(): Promise<void> {\n while (this.buffer.length) {\n const batch = this.buffer.slice(0, this.batchSize);\n await this.queue.enqueue(batch.map(({ record }) => record));\n this.buffer.splice(0, batch.length);\n this.bufferBytes -= batch.reduce((total, item) => total + item.bytes, 0);\n }\n }\n\n async shutdown(): Promise<void> {\n if (this.interval) clearInterval(this.interval);\n this.interval = null;\n try {\n await this.flush();\n } finally {\n await this.queue.close();\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAOO,IAAM,0BAAN,MAAiC;AAAA,EAMtC,YACmB,OACA,MACA,WACA,iBACA,iBAAiB,KAAK,OAAO,MAC7B,iBAAiB,GAClC;AANiB;AACA;AACA;AACA;AACA;AACA;AAAA,EAChB;AAAA,EANgB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAXF,SAAyC,CAAC;AAAA,EACnD,cAAc;AAAA,EACd,WAAiC;AAAA,EACjC,WAAkC;AAAA,EAW1C,MAAM,UAAU,MAAY;AAC1B,QAAI,KAAK,SAAU;AACnB,QAAI,QAAS,MAAK,MAAM,MAAM;AAC9B,SAAK,WAAW,YAAY,MAAM;AAChC,WAAK,KAAK,MAAM,EAAE,MAAM,MAAM;AAC5B,gBAAQ,MAAM,GAAG,KAAK,IAAI,+DAA+D;AAAA,MAC3F,CAAC;AAAA,IACH,GAAG,KAAK,eAAe;AACvB,SAAK,SAAS,MAAM;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AACA,YAAM,KAAK,MAAM,MAAM;AAAA,IACzB;AAAA,EACF;AACF;","names":[]}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
interface AnalyticsQueue<T> {
|
|
2
|
+
enqueue(records: T[]): Promise<void>;
|
|
3
|
+
start(): void;
|
|
4
|
+
close(): Promise<void>;
|
|
5
|
+
}
|
|
6
|
+
/** Retain failed Redis writes, bound memory, and serialize flushes. */
|
|
7
|
+
declare class BufferedAnalyticsLogger<T> {
|
|
8
|
+
private readonly queue;
|
|
9
|
+
private readonly name;
|
|
10
|
+
private readonly batchSize;
|
|
11
|
+
private readonly flushIntervalMs;
|
|
12
|
+
private readonly maxBufferBytes;
|
|
13
|
+
private readonly maxRecordBytes;
|
|
14
|
+
private readonly buffer;
|
|
15
|
+
private bufferBytes;
|
|
16
|
+
private flushing;
|
|
17
|
+
private interval;
|
|
18
|
+
constructor(queue: AnalyticsQueue<T>, name: string, batchSize: number, flushIntervalMs: number, maxBufferBytes?: number, maxRecordBytes?: number);
|
|
19
|
+
start(deliver?: boolean): void;
|
|
20
|
+
log(record: T): void;
|
|
21
|
+
flush(): Promise<void>;
|
|
22
|
+
private flushPending;
|
|
23
|
+
shutdown(): Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export { type AnalyticsQueue, BufferedAnalyticsLogger };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
interface AnalyticsQueue<T> {
|
|
2
|
+
enqueue(records: T[]): Promise<void>;
|
|
3
|
+
start(): void;
|
|
4
|
+
close(): Promise<void>;
|
|
5
|
+
}
|
|
6
|
+
/** Retain failed Redis writes, bound memory, and serialize flushes. */
|
|
7
|
+
declare class BufferedAnalyticsLogger<T> {
|
|
8
|
+
private readonly queue;
|
|
9
|
+
private readonly name;
|
|
10
|
+
private readonly batchSize;
|
|
11
|
+
private readonly flushIntervalMs;
|
|
12
|
+
private readonly maxBufferBytes;
|
|
13
|
+
private readonly maxRecordBytes;
|
|
14
|
+
private readonly buffer;
|
|
15
|
+
private bufferBytes;
|
|
16
|
+
private flushing;
|
|
17
|
+
private interval;
|
|
18
|
+
constructor(queue: AnalyticsQueue<T>, name: string, batchSize: number, flushIntervalMs: number, maxBufferBytes?: number, maxRecordBytes?: number);
|
|
19
|
+
start(deliver?: boolean): void;
|
|
20
|
+
log(record: T): void;
|
|
21
|
+
flush(): Promise<void>;
|
|
22
|
+
private flushPending;
|
|
23
|
+
shutdown(): Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export { type AnalyticsQueue, BufferedAnalyticsLogger };
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// src/buffered-logger.ts
|
|
2
|
+
var BufferedAnalyticsLogger = class {
|
|
3
|
+
constructor(queue, name, batchSize, flushIntervalMs, maxBufferBytes = 16 * 1024 * 1024, maxRecordBytes = 0) {
|
|
4
|
+
this.queue = queue;
|
|
5
|
+
this.name = name;
|
|
6
|
+
this.batchSize = batchSize;
|
|
7
|
+
this.flushIntervalMs = flushIntervalMs;
|
|
8
|
+
this.maxBufferBytes = maxBufferBytes;
|
|
9
|
+
this.maxRecordBytes = maxRecordBytes;
|
|
10
|
+
}
|
|
11
|
+
queue;
|
|
12
|
+
name;
|
|
13
|
+
batchSize;
|
|
14
|
+
flushIntervalMs;
|
|
15
|
+
maxBufferBytes;
|
|
16
|
+
maxRecordBytes;
|
|
17
|
+
buffer = [];
|
|
18
|
+
bufferBytes = 0;
|
|
19
|
+
flushing = null;
|
|
20
|
+
interval = null;
|
|
21
|
+
start(deliver = true) {
|
|
22
|
+
if (this.interval) return;
|
|
23
|
+
if (deliver) this.queue.start();
|
|
24
|
+
this.interval = setInterval(() => {
|
|
25
|
+
void this.flush().catch(() => {
|
|
26
|
+
console.error(`${this.name} analytics could not reach Redis; buffered records will retry`);
|
|
27
|
+
});
|
|
28
|
+
}, this.flushIntervalMs);
|
|
29
|
+
this.interval.unref();
|
|
30
|
+
}
|
|
31
|
+
log(record) {
|
|
32
|
+
const bytes = Buffer.byteLength(JSON.stringify(record));
|
|
33
|
+
if (this.maxRecordBytes && bytes > this.maxRecordBytes) {
|
|
34
|
+
console.error(`${this.name} analytics record exceeds the capture size limit; only this record was rejected`);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
if (this.bufferBytes + bytes > this.maxBufferBytes) {
|
|
38
|
+
console.error(`${this.name} analytics buffer is full; one record was rejected`);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
this.buffer.push({ record, bytes });
|
|
42
|
+
this.bufferBytes += bytes;
|
|
43
|
+
if (this.buffer.length >= this.batchSize || this.bufferBytes >= 4 * 1024 * 1024) {
|
|
44
|
+
void this.flush().catch(() => {
|
|
45
|
+
console.error(`${this.name} analytics could not reach Redis; buffered records will retry`);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
flush() {
|
|
50
|
+
if (this.flushing) return this.flushing;
|
|
51
|
+
this.flushing = this.flushPending().finally(() => {
|
|
52
|
+
this.flushing = null;
|
|
53
|
+
});
|
|
54
|
+
return this.flushing;
|
|
55
|
+
}
|
|
56
|
+
async flushPending() {
|
|
57
|
+
while (this.buffer.length) {
|
|
58
|
+
const batch = this.buffer.slice(0, this.batchSize);
|
|
59
|
+
await this.queue.enqueue(batch.map(({ record }) => record));
|
|
60
|
+
this.buffer.splice(0, batch.length);
|
|
61
|
+
this.bufferBytes -= batch.reduce((total, item) => total + item.bytes, 0);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
async shutdown() {
|
|
65
|
+
if (this.interval) clearInterval(this.interval);
|
|
66
|
+
this.interval = null;
|
|
67
|
+
try {
|
|
68
|
+
await this.flush();
|
|
69
|
+
} finally {
|
|
70
|
+
await this.queue.close();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
export {
|
|
75
|
+
BufferedAnalyticsLogger
|
|
76
|
+
};
|
|
77
|
+
//# sourceMappingURL=buffered-logger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/buffered-logger.ts"],"sourcesContent":["export interface AnalyticsQueue<T> {\n enqueue(records: T[]): Promise<void>;\n start(): void;\n close(): Promise<void>;\n}\n\n/** Retain failed Redis writes, bound memory, and serialize flushes. */\nexport class BufferedAnalyticsLogger<T> {\n private readonly buffer: { record: T; bytes: number }[] = [];\n private bufferBytes = 0;\n private flushing: Promise<void> | null = null;\n private interval: NodeJS.Timeout | null = null;\n\n constructor(\n private readonly queue: AnalyticsQueue<T>,\n private readonly name: string,\n private readonly batchSize: number,\n private readonly flushIntervalMs: number,\n private readonly maxBufferBytes = 16 * 1024 * 1024,\n private readonly maxRecordBytes = 0,\n ) {}\n\n start(deliver = true): void {\n if (this.interval) return;\n if (deliver) this.queue.start();\n this.interval = setInterval(() => {\n void this.flush().catch(() => {\n console.error(`${this.name} analytics could not reach Redis; buffered records will retry`);\n });\n }, this.flushIntervalMs);\n this.interval.unref();\n }\n\n log(record: T): void {\n const bytes = Buffer.byteLength(JSON.stringify(record));\n if (this.maxRecordBytes && bytes > this.maxRecordBytes) {\n console.error(`${this.name} analytics record exceeds the capture size limit; only this record was rejected`);\n return;\n }\n if (this.bufferBytes + bytes > this.maxBufferBytes) {\n // A bounded in-memory fallback cannot safely absorb an unlimited outage.\n // Make overload explicit without exposing request data in the error.\n console.error(`${this.name} analytics buffer is full; one record was rejected`);\n return;\n }\n this.buffer.push({ record, bytes });\n this.bufferBytes += bytes;\n // Captures can contain two large base64 bodies. Flush on bytes as well as\n // rows so a normal low-count burst does not fill the fallback buffer.\n if (this.buffer.length >= this.batchSize || this.bufferBytes >= 4 * 1024 * 1024) {\n void this.flush().catch(() => {\n console.error(`${this.name} analytics could not reach Redis; buffered records will retry`);\n });\n }\n }\n\n flush(): Promise<void> {\n if (this.flushing) return this.flushing;\n this.flushing = this.flushPending().finally(() => { this.flushing = null; });\n return this.flushing;\n }\n\n private async flushPending(): Promise<void> {\n while (this.buffer.length) {\n const batch = this.buffer.slice(0, this.batchSize);\n await this.queue.enqueue(batch.map(({ record }) => record));\n this.buffer.splice(0, batch.length);\n this.bufferBytes -= batch.reduce((total, item) => total + item.bytes, 0);\n }\n }\n\n async shutdown(): Promise<void> {\n if (this.interval) clearInterval(this.interval);\n this.interval = null;\n try {\n await this.flush();\n } finally {\n await this.queue.close();\n }\n }\n}\n"],"mappings":";AAOO,IAAM,0BAAN,MAAiC;AAAA,EAMtC,YACmB,OACA,MACA,WACA,iBACA,iBAAiB,KAAK,OAAO,MAC7B,iBAAiB,GAClC;AANiB;AACA;AACA;AACA;AACA;AACA;AAAA,EAChB;AAAA,EANgB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAXF,SAAyC,CAAC;AAAA,EACnD,cAAc;AAAA,EACd,WAAiC;AAAA,EACjC,WAAkC;AAAA,EAW1C,MAAM,UAAU,MAAY;AAC1B,QAAI,KAAK,SAAU;AACnB,QAAI,QAAS,MAAK,MAAM,MAAM;AAC9B,SAAK,WAAW,YAAY,MAAM;AAChC,WAAK,KAAK,MAAM,EAAE,MAAM,MAAM;AAC5B,gBAAQ,MAAM,GAAG,KAAK,IAAI,+DAA+D;AAAA,MAC3F,CAAC;AAAA,IACH,GAAG,KAAK,eAAe;AACvB,SAAK,SAAS,MAAM;AAAA,EACtB;AAAA,EAEA,IAAI,QAAiB;AACnB,UAAM,QAAQ,OAAO,WAAW,KAAK,UAAU,MAAM,CAAC;AACtD,QAAI,KAAK,kBAAkB,QAAQ,KAAK,gBAAgB;AACtD,cAAQ,MAAM,GAAG,KAAK,IAAI,iFAAiF;AAC3G;AAAA,IACF;AACA,QAAI,KAAK,cAAc,QAAQ,KAAK,gBAAgB;AAGlD,cAAQ,MAAM,GAAG,KAAK,IAAI,oDAAoD;AAC9E;AAAA,IACF;AACA,SAAK,OAAO,KAAK,EAAE,QAAQ,MAAM,CAAC;AAClC,SAAK,eAAe;AAGpB,QAAI,KAAK,OAAO,U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