mcard-js 2.1.49 → 2.1.51

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 (109) hide show
  1. package/dist/CardCollection-EMSBVZP3.js +10 -0
  2. package/dist/CardCollection-KQWR4PCV.js +10 -0
  3. package/dist/CardCollection-ORGE2XBG.js +10 -0
  4. package/dist/EngineRegistry-ABZXHZWO.js +17 -0
  5. package/dist/EngineRegistry-EIOT4MUZ.js +17 -0
  6. package/dist/EngineRegistry-IQ6EVO72.js +17 -0
  7. package/dist/EngineRegistry-PHRFXEOE.js +17 -0
  8. package/dist/IndexedDBEngine-EWA3SLAO.js +12 -0
  9. package/dist/IndexedDBEngine-FXAD42F3.js +12 -0
  10. package/dist/IndexedDBEngine-RD4447IS.js +12 -0
  11. package/dist/LLMRuntime-ARUWOX52.js +17 -0
  12. package/dist/LLMRuntime-C3XCO7WF.js +17 -0
  13. package/dist/LLMRuntime-CQ7X43QR.js +17 -0
  14. package/dist/LLMRuntime-PD45COKE.js +17 -0
  15. package/dist/LLMRuntime-QOUMLT33.js +17 -0
  16. package/dist/LLMRuntime-SZNLTHD7.js +17 -0
  17. package/dist/LLMRuntime-TVJGK2BG.js +17 -0
  18. package/dist/LambdaRuntime-25GMEJCU.js +19 -0
  19. package/dist/LambdaRuntime-7KQUMHPI.js +19 -0
  20. package/dist/LambdaRuntime-DRT7ODPC.js +19 -0
  21. package/dist/LambdaRuntime-HSREEYQG.js +19 -0
  22. package/dist/LambdaRuntime-IH7NVG6Z.js +19 -0
  23. package/dist/LambdaRuntime-MPG27FM2.js +19 -0
  24. package/dist/LambdaRuntime-ODSWIMNM.js +19 -0
  25. package/dist/LambdaRuntime-PHGRZYAW.js +19 -0
  26. package/dist/LambdaRuntime-QOEYR37L.js +19 -0
  27. package/dist/LambdaRuntime-RT33TFN2.js +19 -0
  28. package/dist/LambdaRuntime-W6TQBP5O.js +19 -0
  29. package/dist/Loader-35WSUC53.js +14 -0
  30. package/dist/Loader-STS3G4OQ.js +16 -0
  31. package/dist/Loader-W22AEM6F.js +12 -0
  32. package/dist/Loader-YBPWP43S.js +12 -0
  33. package/dist/Loader-ZYSS7B4D.js +12 -0
  34. package/dist/NetworkRuntime-KR2QITXV.js +987 -0
  35. package/dist/NetworkRuntime-S6V2CMZV.js +1575 -0
  36. package/dist/OllamaProvider-2ANW6EB2.js +9 -0
  37. package/dist/OllamaProvider-5QFJKYAC.js +9 -0
  38. package/dist/OllamaProvider-6QXJGR7V.js +9 -0
  39. package/dist/OllamaProvider-ABEEFX7M.js +9 -0
  40. package/dist/OllamaProvider-Z2CGY5LY.js +9 -0
  41. package/dist/VCard-225X42W7.js +25 -0
  42. package/dist/chunk-2APJYBH4.js +368 -0
  43. package/dist/chunk-4DFTWDRB.js +497 -0
  44. package/dist/chunk-4PBRTFSY.js +112 -0
  45. package/dist/chunk-4T3H25AP.js +299 -0
  46. package/dist/chunk-5DFXPIRL.js +42 -0
  47. package/dist/chunk-5HRZV4R3.js +217 -0
  48. package/dist/chunk-6ZRJXVJ3.js +529 -0
  49. package/dist/chunk-7N7JYGN2.js +364 -0
  50. package/dist/chunk-7QTJUGYQ.js +74 -0
  51. package/dist/chunk-7TXIPJI2.js +2360 -0
  52. package/dist/chunk-BFJUD527.js +2369 -0
  53. package/dist/chunk-CHXIVTQV.js +364 -0
  54. package/dist/chunk-DM2ABCA4.js +497 -0
  55. package/dist/chunk-DTPHGTBQ.js +275 -0
  56. package/dist/chunk-EDAJ5FO6.js +405 -0
  57. package/dist/chunk-ETJWXHKZ.js +246 -0
  58. package/dist/chunk-FLYGNPUC.js +2369 -0
  59. package/dist/chunk-FSDRDWOP.js +34 -0
  60. package/dist/chunk-GIKMCG4D.js +497 -0
  61. package/dist/chunk-IJKS3LGK.js +428 -0
  62. package/dist/chunk-JUQ2VQZA.js +428 -0
  63. package/dist/chunk-JVW4J7BY.js +2369 -0
  64. package/dist/chunk-JWTRVEC3.js +2369 -0
  65. package/dist/chunk-KJM4C65U.js +299 -0
  66. package/dist/chunk-KMC566CN.js +591 -0
  67. package/dist/chunk-KMNP6DBL.js +455 -0
  68. package/dist/chunk-LVU7O5IY.js +597 -0
  69. package/dist/chunk-M4C6RWLA.js +373 -0
  70. package/dist/chunk-NAAAKSEO.js +541 -0
  71. package/dist/chunk-NKIXLPHL.js +373 -0
  72. package/dist/chunk-NOEDMK7I.js +428 -0
  73. package/dist/chunk-NOPYSBOQ.js +2360 -0
  74. package/dist/chunk-P4G42QCY.js +2369 -0
  75. package/dist/chunk-PKLONZCF.js +253 -0
  76. package/dist/chunk-PNGECWPN.js +597 -0
  77. package/dist/chunk-PYP6T64W.js +217 -0
  78. package/dist/chunk-QFT3COE2.js +217 -0
  79. package/dist/chunk-QFZFXMNX.js +275 -0
  80. package/dist/chunk-QZGRQRJP.js +2369 -0
  81. package/dist/chunk-R3XRBAM7.js +253 -0
  82. package/dist/chunk-RYP66UMH.js +74 -0
  83. package/dist/chunk-RZIZYRLF.js +112 -0
  84. package/dist/chunk-T43V44RS.js +2369 -0
  85. package/dist/chunk-UCNVX5BZ.js +74 -0
  86. package/dist/chunk-UDF7HS4V.js +368 -0
  87. package/dist/chunk-VJPXJVEH.js +299 -0
  88. package/dist/chunk-VW3KBDK5.js +74 -0
  89. package/dist/chunk-X72XIYSN.js +364 -0
  90. package/dist/chunk-XETU7TV4.js +112 -0
  91. package/dist/chunk-Y4BT6LHA.js +368 -0
  92. package/dist/chunk-YQGB6BIA.js +2369 -0
  93. package/dist/chunk-ZEQPO3XV.js +217 -0
  94. package/dist/chunk-ZKRKWXEQ.js +2369 -0
  95. package/dist/chunk-ZMK2HTZ5.js +275 -0
  96. package/dist/constants-CLB7B6MN.js +101 -0
  97. package/dist/constants-O343SMHL.js +103 -0
  98. package/dist/constants-YPGDEX5X.js +103 -0
  99. package/dist/index.browser.cjs +11 -5
  100. package/dist/index.browser.js +12 -12
  101. package/dist/index.cjs +2358 -1896
  102. package/dist/index.d.cts +934 -776
  103. package/dist/index.d.ts +934 -776
  104. package/dist/index.js +1353 -1271
  105. package/dist/storage/SqliteNodeEngine.cjs +12 -6
  106. package/dist/storage/SqliteNodeEngine.js +4 -4
  107. package/dist/storage/SqliteWasmEngine.cjs +11 -5
  108. package/dist/storage/SqliteWasmEngine.js +4 -4
  109. package/package.json +5 -3
@@ -0,0 +1,299 @@
1
+ import {
2
+ MCard
3
+ } from "./chunk-GGQCF7ZK.js";
4
+ import {
5
+ DEFAULT_PAGE_SIZE
6
+ } from "./chunk-R3XRBAM7.js";
7
+
8
+ // src/monads/Maybe.ts
9
+ var Maybe = class _Maybe {
10
+ constructor(_value, _isNothing) {
11
+ this._value = _value;
12
+ this._isNothing = _isNothing;
13
+ }
14
+ /**
15
+ * Create a Just (has value)
16
+ */
17
+ static just(value) {
18
+ return new _Maybe(value, false);
19
+ }
20
+ /**
21
+ * Create a Nothing (no value)
22
+ */
23
+ static nothing() {
24
+ return new _Maybe(null, true);
25
+ }
26
+ /**
27
+ * Check if this is Nothing
28
+ */
29
+ get isNothing() {
30
+ return this._isNothing;
31
+ }
32
+ /**
33
+ * Check if this is Just
34
+ */
35
+ get isJust() {
36
+ return !this._isNothing;
37
+ }
38
+ /**
39
+ * Get the value (throws if Nothing)
40
+ */
41
+ get value() {
42
+ if (this._isNothing) {
43
+ throw new Error("Cannot get value from Nothing");
44
+ }
45
+ return this._value;
46
+ }
47
+ /**
48
+ * Monadic bind - chain operations
49
+ * Short-circuits on Nothing
50
+ */
51
+ bind(fn) {
52
+ if (this._isNothing) {
53
+ return _Maybe.nothing();
54
+ }
55
+ return fn(this._value);
56
+ }
57
+ /**
58
+ * Map a function over the value
59
+ */
60
+ map(fn) {
61
+ if (this._isNothing) {
62
+ return _Maybe.nothing();
63
+ }
64
+ return _Maybe.just(fn(this._value));
65
+ }
66
+ /**
67
+ * Get value or default
68
+ */
69
+ getOrElse(defaultValue) {
70
+ return this._isNothing ? defaultValue : this._value;
71
+ }
72
+ };
73
+
74
+ // src/model/CardCollection.ts
75
+ var CardCollection = class {
76
+ engine;
77
+ constructor(engine) {
78
+ this.engine = engine;
79
+ }
80
+ // =========== Standard Operations ===========
81
+ /**
82
+ * Add a card to the collection
83
+ * Handles duplicates (same content, same hash) and collisions (diff content, same hash)
84
+ */
85
+ async add(card) {
86
+ const existingCard = await this.engine.get(card.hash);
87
+ if (existingCard) {
88
+ const isDuplicate = this.areContentsEqual(existingCard.content, card.content);
89
+ if (isDuplicate) {
90
+ const { generateDuplicationEvent } = await import("./EventProducer-VFDOM5W2.js");
91
+ const eventStr = generateDuplicationEvent(card);
92
+ const eventCard = await MCard.create(eventStr);
93
+ await this.engine.add(eventCard);
94
+ return card.hash;
95
+ } else {
96
+ const { generateCollisionEvent } = await import("./EventProducer-VFDOM5W2.js");
97
+ const eventStr = await generateCollisionEvent(card);
98
+ const eventCard = await MCard.create(eventStr);
99
+ await this.engine.add(eventCard);
100
+ const eventObj = JSON.parse(eventStr);
101
+ const nextAlgo = eventObj.upgraded_function;
102
+ if (!nextAlgo) {
103
+ throw new Error("Failed to determine next hash algorithm for collision");
104
+ }
105
+ const upgradedCard = await MCard.create(card.content, nextAlgo);
106
+ return this.engine.add(upgradedCard);
107
+ }
108
+ }
109
+ return this.engine.add(card);
110
+ }
111
+ areContentsEqual(a, b) {
112
+ if (a.length !== b.length) return false;
113
+ for (let i = 0; i < a.length; i++) {
114
+ if (a[i] !== b[i]) return false;
115
+ }
116
+ return true;
117
+ }
118
+ /**
119
+ * Get a card by hash
120
+ */
121
+ async get(hash) {
122
+ return this.engine.get(hash);
123
+ }
124
+ /**
125
+ * Delete a card by hash
126
+ */
127
+ async delete(hash) {
128
+ return this.engine.delete(hash);
129
+ }
130
+ /**
131
+ * Get a page of cards
132
+ */
133
+ async getPage(pageNumber = 1, pageSize = DEFAULT_PAGE_SIZE) {
134
+ return this.engine.getPage(pageNumber, pageSize);
135
+ }
136
+ /**
137
+ * Count total cards
138
+ */
139
+ async count() {
140
+ return this.engine.count();
141
+ }
142
+ // =========== Handle Operations ===========
143
+ /**
144
+ * Add a card and register a handle for it
145
+ */
146
+ async addWithHandle(card, handle) {
147
+ const hash = await this.add(card);
148
+ await this.engine.registerHandle(handle, hash);
149
+ return hash;
150
+ }
151
+ /**
152
+ * Get card by handle
153
+ */
154
+ async getByHandle(handle) {
155
+ return this.engine.getByHandle(handle);
156
+ }
157
+ /**
158
+ * Resolve handle to hash
159
+ */
160
+ async resolveHandle(handle) {
161
+ return this.engine.resolveHandle(handle);
162
+ }
163
+ /**
164
+ * Update handle to point to new card
165
+ */
166
+ async updateHandle(handle, newCard) {
167
+ const hash = await this.add(newCard);
168
+ await this.engine.updateHandle(handle, hash);
169
+ return hash;
170
+ }
171
+ /**
172
+ * Get version history for a handle
173
+ */
174
+ async getHandleHistory(handle) {
175
+ return this.engine.getHandleHistory(handle);
176
+ }
177
+ // =========== Monadic Operations ===========
178
+ /**
179
+ * Monadic get - returns Maybe<MCard>
180
+ */
181
+ async getM(hash) {
182
+ const card = await this.get(hash);
183
+ return card ? Maybe.just(card) : Maybe.nothing();
184
+ }
185
+ /**
186
+ * Monadic getByHandle - returns Maybe<MCard>
187
+ */
188
+ async getByHandleM(handle) {
189
+ const card = await this.getByHandle(handle);
190
+ return card ? Maybe.just(card) : Maybe.nothing();
191
+ }
192
+ /**
193
+ * Monadic resolveHandle - returns Maybe<string>
194
+ */
195
+ async resolveHandleM(handle) {
196
+ const hash = await this.resolveHandle(handle);
197
+ return hash ? Maybe.just(hash) : Maybe.nothing();
198
+ }
199
+ /**
200
+ * Resolve handle and get card in one monadic operation
201
+ */
202
+ async resolveAndGetM(handle) {
203
+ const maybeHash = await this.resolveHandleM(handle);
204
+ if (maybeHash.isNothing) return Maybe.nothing();
205
+ return this.getM(maybeHash.value);
206
+ }
207
+ /**
208
+ * Prune version history for a handle.
209
+ * @param handle The handle string.
210
+ * @param options Options for pruning (olderThan date, or deleteAll).
211
+ * @returns Number of deleted entries.
212
+ */
213
+ async pruneHandleHistory(handle, options = {}) {
214
+ if (this.engine.pruneHandleHistory) {
215
+ return this.engine.pruneHandleHistory(handle, options);
216
+ }
217
+ return 0;
218
+ }
219
+ // =========== Search & Bulk Operations ===========
220
+ async clear() {
221
+ return this.engine.clear();
222
+ }
223
+ async searchByString(query, pageNumber = 1, pageSize = DEFAULT_PAGE_SIZE) {
224
+ return this.engine.search(query, pageNumber, pageSize);
225
+ }
226
+ async searchByContent(query, pageNumber = 1, pageSize = DEFAULT_PAGE_SIZE) {
227
+ return this.engine.search(query, pageNumber, pageSize);
228
+ }
229
+ async searchByHash(hashPrefix) {
230
+ return this.engine.searchByHash(hashPrefix);
231
+ }
232
+ async getAllMCardsRaw() {
233
+ return this.engine.getAll();
234
+ }
235
+ async getAllCards(pageSize = DEFAULT_PAGE_SIZE, processCallback) {
236
+ const cards = [];
237
+ let pageNumber = 1;
238
+ let total = 0;
239
+ while (true) {
240
+ const page = await this.getPage(pageNumber, pageSize);
241
+ if (!page.items || page.items.length === 0) break;
242
+ for (const card of page.items) {
243
+ if (processCallback) {
244
+ processCallback(card);
245
+ }
246
+ cards.push(card);
247
+ }
248
+ total = page.totalItems;
249
+ if (!page.hasNext) break;
250
+ pageNumber++;
251
+ }
252
+ return { cards, total };
253
+ }
254
+ async printAllCards() {
255
+ const cards = await this.getAllMCardsRaw();
256
+ cards.forEach((card) => {
257
+ console.log(`Hash: ${card.hash}`);
258
+ try {
259
+ const text = new TextDecoder().decode(card.content);
260
+ const preview = text.slice(0, 100).replace(/\n/g, " ");
261
+ console.log(`Content: ${preview}${text.length > 100 ? "..." : ""}`);
262
+ } catch {
263
+ console.log(`Content (binary): ${card.content.length} bytes`);
264
+ }
265
+ console.log("---");
266
+ });
267
+ }
268
+ // =========== Handle Management Operations ===========
269
+ /**
270
+ * List all registered handles with their current hashes.
271
+ */
272
+ async getAllHandles() {
273
+ return this.engine.getAllHandles();
274
+ }
275
+ /**
276
+ * Remove a handle and all its history entries.
277
+ * Does NOT delete the underlying card(s).
278
+ */
279
+ async removeHandle(handle) {
280
+ return this.engine.removeHandle(handle);
281
+ }
282
+ /**
283
+ * Rename a handle atomically: updates handle registry and all history entries.
284
+ */
285
+ async renameHandle(oldHandle, newHandle) {
286
+ return this.engine.renameHandle(oldHandle, newHandle);
287
+ }
288
+ /**
289
+ * Delete a single history entry by handle + previousHash, stitching the chain.
290
+ */
291
+ async deleteHistoryEntry(handle, previousHash) {
292
+ return this.engine.deleteHistoryEntry(handle, previousHash);
293
+ }
294
+ };
295
+
296
+ export {
297
+ Maybe,
298
+ CardCollection
299
+ };
@@ -0,0 +1,42 @@
1
+ // src/ptr/node/NetworkConfig.ts
2
+ var HTTP_DEFAULT_MAX_ATTEMPTS = 1;
3
+ var HTTP_DEFAULT_BACKOFF = "exponential";
4
+ var HTTP_DEFAULT_BASE_DELAY_MS = 1e3;
5
+ var HTTP_DEFAULT_MAX_DELAY_MS = 3e4;
6
+ var HTTP_DEFAULT_TIMEOUT_MS = 3e4;
7
+ var HTTP_DEFAULT_CACHE_TTL_SECONDS = 300;
8
+ var RATE_LIMIT_DEFAULT_TOKENS_PER_SECOND = 10;
9
+ var RATE_LIMIT_DEFAULT_MAX_BURST = 20;
10
+ var RATE_LIMIT_WAIT_INTERVAL_MS = 100;
11
+ var NETWORK_DEFAULT_LISTEN_PORT = 3e3;
12
+ var NETWORK_DEFAULT_SIGNALING_MAX_PORT_TRIES = 10;
13
+ var NETWORK_DEFAULT_HOST = "localhost";
14
+ var NETWORK_DEFAULT_ORCHESTRATOR_SLEEP_MS = 1e3;
15
+ var WEBSOCKET_SERVER_START_WAIT_MS = 3e3;
16
+ var STATIC_SERVER_START_WAIT_MS = 1500;
17
+ var PROCESS_TERMINATE_WAIT_MS = 500;
18
+ var SESSION_RECORD_DEFAULT_MAX_BUFFER_SIZE = 5;
19
+ var WEBRTC_DEFAULT_TIMEOUT_MS = 3e4;
20
+ var WEBRTC_DEFAULT_MOCK_DELAY_MS = 100;
21
+
22
+ export {
23
+ HTTP_DEFAULT_MAX_ATTEMPTS,
24
+ HTTP_DEFAULT_BACKOFF,
25
+ HTTP_DEFAULT_BASE_DELAY_MS,
26
+ HTTP_DEFAULT_MAX_DELAY_MS,
27
+ HTTP_DEFAULT_TIMEOUT_MS,
28
+ HTTP_DEFAULT_CACHE_TTL_SECONDS,
29
+ RATE_LIMIT_DEFAULT_TOKENS_PER_SECOND,
30
+ RATE_LIMIT_DEFAULT_MAX_BURST,
31
+ RATE_LIMIT_WAIT_INTERVAL_MS,
32
+ NETWORK_DEFAULT_LISTEN_PORT,
33
+ NETWORK_DEFAULT_SIGNALING_MAX_PORT_TRIES,
34
+ NETWORK_DEFAULT_HOST,
35
+ NETWORK_DEFAULT_ORCHESTRATOR_SLEEP_MS,
36
+ WEBSOCKET_SERVER_START_WAIT_MS,
37
+ STATIC_SERVER_START_WAIT_MS,
38
+ PROCESS_TERMINATE_WAIT_MS,
39
+ SESSION_RECORD_DEFAULT_MAX_BUFFER_SIZE,
40
+ WEBRTC_DEFAULT_TIMEOUT_MS,
41
+ WEBRTC_DEFAULT_MOCK_DELAY_MS
42
+ };
@@ -0,0 +1,217 @@
1
+ import {
2
+ AbstractSqlEngine,
3
+ initCoreSchemas
4
+ } from "./chunk-GIKMCG4D.js";
5
+ import {
6
+ createPage
7
+ } from "./chunk-3EIBJPNF.js";
8
+ import {
9
+ MCard
10
+ } from "./chunk-GGQCF7ZK.js";
11
+ import {
12
+ Handle_exports,
13
+ init_Handle
14
+ } from "./chunk-ADV52544.js";
15
+ import {
16
+ DEFAULT_PAGE_SIZE,
17
+ SQLITE_BUSY_TIMEOUT_MS
18
+ } from "./chunk-ETJWXHKZ.js";
19
+ import {
20
+ __toCommonJS
21
+ } from "./chunk-PNKVD2UK.js";
22
+
23
+ // src/storage/engines/SqliteNodeEngine.ts
24
+ var SqliteNodeEngine = class _SqliteNodeEngine extends AbstractSqlEngine {
25
+ db;
26
+ dbPath;
27
+ /**
28
+ * Convert a database row into an MCard instance.
29
+ */
30
+ rowToCard(row) {
31
+ const rawContent = row.content;
32
+ const content = rawContent instanceof Buffer ? new Uint8Array(rawContent) : new Uint8Array(rawContent);
33
+ return MCard.fromData(content, String(row.hash), String(row.g_time));
34
+ }
35
+ /**
36
+ * Create a new SqliteNodeEngine via async factory.
37
+ * @param dbPath Path to database file, or ':memory:' for in-memory database
38
+ */
39
+ static async create(dbPath = ":memory:") {
40
+ const engine = new _SqliteNodeEngine(dbPath);
41
+ await engine.init();
42
+ return engine;
43
+ }
44
+ constructor(dbPath) {
45
+ super();
46
+ this.dbPath = dbPath;
47
+ }
48
+ async init() {
49
+ try {
50
+ const mod = await import("better-sqlite3");
51
+ const Database = mod.default || mod;
52
+ this.db = new Database(this.dbPath);
53
+ this.setupDatabase();
54
+ } catch (e) {
55
+ console.error(`SqliteNodeEngine: Failed to load better-sqlite3: ${e.message}`);
56
+ throw e;
57
+ }
58
+ }
59
+ /**
60
+ * Initialize database schema
61
+ */
62
+ setupDatabase() {
63
+ this.db.pragma("journal_mode = WAL");
64
+ this.db.pragma(`busy_timeout = ${SQLITE_BUSY_TIMEOUT_MS}`);
65
+ this.db.pragma("synchronous = NORMAL");
66
+ initCoreSchemas(this.db);
67
+ }
68
+ // ======================================================================
69
+ // AbstractSqlEngine primitives
70
+ // ======================================================================
71
+ async queryRows(sql, ...params) {
72
+ const stmt = this.db.prepare(sql);
73
+ return stmt.all(...params);
74
+ }
75
+ async execSql(sql, ...params) {
76
+ if (params.length === 0) {
77
+ this.db.exec(sql);
78
+ return 0;
79
+ }
80
+ const stmt = this.db.prepare(sql);
81
+ const result = stmt.run(...params);
82
+ return result.changes;
83
+ }
84
+ /**
85
+ * Close the database connection
86
+ */
87
+ close() {
88
+ this.db.close();
89
+ }
90
+ /**
91
+ * Get the database path
92
+ */
93
+ getDbPath() {
94
+ return this.dbPath;
95
+ }
96
+ // =========== Card Operations (overrides to use Buffer.from for better-sqlite3) ===========
97
+ async add(card) {
98
+ const stmt = this.db.prepare(
99
+ "INSERT OR REPLACE INTO card (hash, content, g_time) VALUES (?, ?, ?)"
100
+ );
101
+ stmt.run(card.hash, Buffer.from(card.content), card.g_time);
102
+ return card.hash;
103
+ }
104
+ // =========== Sync card operations (for performance-critical callers) ===========
105
+ addSync(card) {
106
+ const stmt = this.db.prepare(
107
+ "INSERT OR REPLACE INTO card (hash, content, g_time) VALUES (?, ?, ?)"
108
+ );
109
+ stmt.run(card.hash, Buffer.from(card.content), card.g_time);
110
+ return card.hash;
111
+ }
112
+ getSync(hash) {
113
+ const stmt = this.db.prepare("SELECT hash, content, g_time FROM card WHERE hash = ?");
114
+ const row = stmt.get(hash);
115
+ if (!row) return null;
116
+ return this.rowToCard(row);
117
+ }
118
+ deleteSync(hash) {
119
+ const stmt = this.db.prepare("DELETE FROM card WHERE hash = ?");
120
+ const result = stmt.run(hash);
121
+ return result.changes > 0;
122
+ }
123
+ getPageSync(pageNumber = 1, pageSize = DEFAULT_PAGE_SIZE) {
124
+ if (pageNumber < 1) throw new Error("Page number must be >= 1");
125
+ if (pageSize < 1) throw new Error("Page size must be >= 1");
126
+ const totalItems = this.countSync();
127
+ const offset = (pageNumber - 1) * pageSize;
128
+ const stmt = this.db.prepare(
129
+ "SELECT hash, content, g_time FROM card ORDER BY g_time DESC LIMIT ? OFFSET ?"
130
+ );
131
+ const rows = stmt.all(pageSize, offset);
132
+ const items = rows.map((row) => this.rowToCard(row));
133
+ return createPage(items, totalItems, pageNumber, pageSize);
134
+ }
135
+ countSync() {
136
+ const stmt = this.db.prepare("SELECT COUNT(*) as cnt FROM card");
137
+ const row = stmt.get();
138
+ return row.cnt;
139
+ }
140
+ clearSync() {
141
+ this.db.exec("DELETE FROM handle_history");
142
+ this.db.exec("DELETE FROM handle_registry");
143
+ this.db.exec("DELETE FROM card");
144
+ }
145
+ // =========== Additional sync search operations ===========
146
+ searchByString(searchString, pageNumber = 1, pageSize = DEFAULT_PAGE_SIZE) {
147
+ if (pageNumber < 1) throw new Error("Page number must be >= 1");
148
+ if (pageSize < 1) throw new Error("Page size must be >= 1");
149
+ const pattern = `%${searchString}%`;
150
+ const offset = (pageNumber - 1) * pageSize;
151
+ const countStmt = this.db.prepare(`
152
+ SELECT COUNT(*) as cnt FROM card
153
+ WHERE hash LIKE ? OR g_time LIKE ? OR CAST(content AS TEXT) LIKE ?
154
+ `);
155
+ const countRow = countStmt.get(pattern, pattern, pattern);
156
+ const totalItems = countRow.cnt;
157
+ const stmt = this.db.prepare(`
158
+ SELECT hash, content, g_time FROM card
159
+ WHERE hash LIKE ? OR g_time LIKE ? OR CAST(content AS TEXT) LIKE ?
160
+ ORDER BY g_time DESC LIMIT ? OFFSET ?
161
+ `);
162
+ const rows = stmt.all(pattern, pattern, pattern, pageSize, offset);
163
+ const items = rows.map((row) => this.rowToCard(row));
164
+ return createPage(items, totalItems, pageNumber, pageSize);
165
+ }
166
+ searchByContent(searchPattern, pageNumber = 1, pageSize = DEFAULT_PAGE_SIZE) {
167
+ if (pageNumber < 1) throw new Error("Page number must be >= 1");
168
+ if (pageSize < 1) throw new Error("Page size must be >= 1");
169
+ if (!searchPattern || typeof searchPattern === "string" && searchPattern.length === 0) {
170
+ throw new Error("Search pattern cannot be empty");
171
+ }
172
+ const searchBytes = typeof searchPattern === "string" ? Buffer.from(searchPattern, "utf-8") : Buffer.from(searchPattern);
173
+ const offset = (pageNumber - 1) * pageSize;
174
+ const countStmt = this.db.prepare("SELECT COUNT(*) as cnt FROM card WHERE INSTR(content, ?) > 0");
175
+ const countRow = countStmt.get(searchBytes);
176
+ const totalItems = countRow.cnt;
177
+ const stmt = this.db.prepare(`
178
+ SELECT hash, content, g_time FROM card
179
+ WHERE INSTR(content, ?) > 0
180
+ ORDER BY g_time DESC LIMIT ? OFFSET ?
181
+ `);
182
+ const rows = stmt.all(searchBytes, pageSize, offset);
183
+ const items = rows.map((row) => this.rowToCard(row));
184
+ return createPage(items, totalItems, pageNumber, pageSize);
185
+ }
186
+ getAllCards() {
187
+ const stmt = this.db.prepare("SELECT hash, content, g_time FROM card ORDER BY g_time DESC");
188
+ const rows = stmt.all();
189
+ const items = rows.map((row) => this.rowToCard(row));
190
+ return createPage(items, items.length, 1, items.length || 1);
191
+ }
192
+ // =========== Unique sync method (not in AbstractSqlEngine) ===========
193
+ deleteHistoryEntrySync(handle, previousHash) {
194
+ const { validateHandle } = (init_Handle(), __toCommonJS(Handle_exports));
195
+ const normalized = validateHandle(handle);
196
+ const performStitch = this.db.transaction(() => {
197
+ const rOut = this.db.prepare(
198
+ "SELECT id, previous_hash FROM handle_history WHERE handle = ? AND previous_hash = ? ORDER BY id DESC LIMIT 1"
199
+ ).get(normalized, previousHash);
200
+ if (!rOut) return;
201
+ const rIn = this.db.prepare(
202
+ "SELECT id, previous_hash FROM handle_history WHERE handle = ? AND id < ? ORDER BY id DESC LIMIT 1"
203
+ ).get(normalized, rOut.id);
204
+ if (rIn) {
205
+ this.db.prepare("UPDATE handle_history SET previous_hash = ? WHERE id = ?").run(rIn.previous_hash, rOut.id);
206
+ this.db.prepare("DELETE FROM handle_history WHERE id = ?").run(rIn.id);
207
+ } else {
208
+ this.db.prepare("DELETE FROM handle_history WHERE id = ?").run(rOut.id);
209
+ }
210
+ });
211
+ performStitch();
212
+ }
213
+ };
214
+
215
+ export {
216
+ SqliteNodeEngine
217
+ };