@dxos/log-store-idb 0.0.0

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.
@@ -0,0 +1,56 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ import { describe, test } from 'vitest';
6
+
7
+ import { trimJsonlToSize } from './trim';
8
+
9
+ describe('trimJsonlToSize', () => {
10
+ test('returns empty string when no input', ({ expect }) => {
11
+ expect(trimJsonlToSize([], 100)).toBe('');
12
+ });
13
+
14
+ test('returns empty string when maxSize is zero', ({ expect }) => {
15
+ expect(trimJsonlToSize(['abc'], 0)).toBe('');
16
+ });
17
+
18
+ test('returns full content when under cap', ({ expect }) => {
19
+ const lines = ['a', 'bb', 'ccc'];
20
+ // 'a\nbb\nccc' = 8 bytes.
21
+ expect(trimJsonlToSize(lines, 100)).toBe('a\nbb\nccc');
22
+ });
23
+
24
+ test('trims oldest lines and keeps newest', ({ expect }) => {
25
+ const lines = ['oldest', 'middle', 'newest'];
26
+ // 'newest' = 6 bytes; 'middle\nnewest' = 13 bytes; full = 20 bytes.
27
+ expect(trimJsonlToSize(lines, 13)).toBe('middle\nnewest');
28
+ expect(trimJsonlToSize(lines, 12)).toBe('newest');
29
+ expect(trimJsonlToSize(lines, 6)).toBe('newest');
30
+ });
31
+
32
+ test('drops a line that would not fit even alone', ({ expect }) => {
33
+ const lines = ['huge-line', 'small'];
34
+ // 'small' = 5 bytes; 'huge-line\nsmall' = 15 bytes.
35
+ expect(trimJsonlToSize(lines, 5)).toBe('small');
36
+ expect(trimJsonlToSize(lines, 4)).toBe('');
37
+ });
38
+
39
+ test('never splits a line', ({ expect }) => {
40
+ const lines = ['{"m":"hello"}', '{"m":"world"}'];
41
+ // Cap below either single line returns empty (lines are atomic).
42
+ expect(trimJsonlToSize(lines, 5)).toBe('');
43
+ // Cap fits one line exactly.
44
+ expect(trimJsonlToSize(lines, 13)).toBe('{"m":"world"}');
45
+ });
46
+
47
+ test('counts utf-8 bytes for multibyte characters', ({ expect }) => {
48
+ const lines = ['ñ', 'a'];
49
+ // 'ñ' is 2 bytes, 'a' is 1 byte; separator '\n' is 1 byte.
50
+ // maxSize = 3 bytes fits only 'ñ' alone (2) or 'a' alone (1) — newest 'a' takes priority,
51
+ // then 'ñ' would need 1 (sep) + 2 (ñ) = 3 more bytes → total 4 > 3, so dropped.
52
+ expect(trimJsonlToSize(lines, 3)).toBe('a');
53
+ // maxSize = 4 bytes fits both: 'ñ\na' = 2+1+1 = 4 bytes.
54
+ expect(trimJsonlToSize(lines, 4)).toBe('ñ\na');
55
+ });
56
+ });
package/src/trim.ts ADDED
@@ -0,0 +1,46 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ /**
6
+ * Trim a list of pre-encoded JSONL lines so the joined output (with `\n` separators)
7
+ * fits within `maxSize` bytes (UTF-8). The newest lines (end of array) are preferred —
8
+ * lines are dropped from the start. Never cuts inside a line.
9
+ *
10
+ * Returns the trimmed JSONL string (no trailing newline).
11
+ */
12
+ export const trimJsonlToSize = (lines: readonly string[], maxSize: number): string => {
13
+ if (lines.length === 0 || maxSize <= 0) {
14
+ return '';
15
+ }
16
+
17
+ const sizes = lines.map((line) => byteLengthUtf8(line));
18
+
19
+ // Walk newest -> oldest, accumulating until next addition would exceed maxSize.
20
+ let total = 0;
21
+ let firstIncludedIndex = lines.length;
22
+ for (let index = lines.length - 1; index >= 0; index--) {
23
+ const lineBytes = sizes[index];
24
+ const sepBytes = total === 0 ? 0 : 1;
25
+ if (total + lineBytes + sepBytes > maxSize) {
26
+ break;
27
+ }
28
+ total += lineBytes + sepBytes;
29
+ firstIncludedIndex = index;
30
+ }
31
+
32
+ if (firstIncludedIndex >= lines.length) {
33
+ return '';
34
+ }
35
+ return lines.slice(firstIncludedIndex).join('\n');
36
+ };
37
+
38
+ const utf8Encoder = typeof TextEncoder !== 'undefined' ? new TextEncoder() : undefined;
39
+
40
+ const byteLengthUtf8 = (value: string): number => {
41
+ if (utf8Encoder) {
42
+ return utf8Encoder.encode(value).length;
43
+ }
44
+ // Fallback: assume 1 byte per char (good enough for ASCII-heavy log lines).
45
+ return value.length;
46
+ };