mcard-js 2.1.47 → 2.1.49

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 (52) hide show
  1. package/dist/AbstractSqlEngine-DKka6XjT.d.cts +451 -0
  2. package/dist/AbstractSqlEngine-DKka6XjT.d.ts +451 -0
  3. package/dist/CardCollection-TYC67XOH.js +10 -0
  4. package/dist/CardCollection-ZQ3G3Q3A.js +10 -0
  5. package/dist/EventProducer-VFDOM5W2.js +47 -0
  6. package/dist/IndexedDBEngine-5CEFZDOG.js +12 -0
  7. package/dist/IndexedDBEngine-BWXAB46W.js +12 -0
  8. package/dist/LLMRuntime-PH3MOQ2Y.js +17 -0
  9. package/dist/LambdaRuntime-DMEBYJIN.js +19 -0
  10. package/dist/LambdaRuntime-YH74FHIW.js +19 -0
  11. package/dist/Loader-OBPDJNFH.js +12 -0
  12. package/dist/Loader-WZXYG4GE.js +12 -0
  13. package/dist/MCard-RHTWJPHJ.js +8 -0
  14. package/dist/NetworkRuntime-KBQURQ6A.js +1598 -0
  15. package/dist/NetworkRuntime-S4DZCGVN.js +1598 -0
  16. package/dist/OllamaProvider-SPGO5Z5E.js +9 -0
  17. package/dist/chunk-3FFEA2XK.js +149 -0
  18. package/dist/chunk-7AXRV7NS.js +112 -0
  19. package/dist/chunk-AAO4GDBI.js +2360 -0
  20. package/dist/chunk-ASW6AOA7.js +140 -0
  21. package/dist/chunk-BJJZWPIF.js +112 -0
  22. package/dist/chunk-GGQCF7ZK.js +170 -0
  23. package/dist/chunk-HIVVDGE5.js +497 -0
  24. package/dist/chunk-HWBEGVEN.js +364 -0
  25. package/dist/chunk-ISY5LYLF.js +217 -0
  26. package/dist/chunk-KVZYFZJ5.js +427 -0
  27. package/dist/chunk-NGTY4P6A.js +275 -0
  28. package/dist/chunk-OAHWTOEB.js +275 -0
  29. package/dist/chunk-OUW2SUGM.js +368 -0
  30. package/dist/chunk-QKH3N62B.js +2360 -0
  31. package/dist/chunk-QPVEUPMU.js +299 -0
  32. package/dist/chunk-RZENJZGX.js +299 -0
  33. package/dist/chunk-VYDZR4ZD.js +364 -0
  34. package/dist/chunk-XJZOEM5F.js +903 -0
  35. package/dist/chunk-Z7EFXSTO.js +217 -0
  36. package/dist/index.browser.cjs +58 -20
  37. package/dist/index.browser.d.cts +34 -17
  38. package/dist/index.browser.d.ts +34 -17
  39. package/dist/index.browser.js +12 -8
  40. package/dist/index.cjs +644 -167
  41. package/dist/index.d.cts +725 -5
  42. package/dist/index.d.ts +725 -5
  43. package/dist/index.js +536 -95
  44. package/dist/storage/SqliteNodeEngine.cjs +28 -20
  45. package/dist/storage/SqliteNodeEngine.d.cts +1 -1
  46. package/dist/storage/SqliteNodeEngine.d.ts +1 -1
  47. package/dist/storage/SqliteNodeEngine.js +5 -5
  48. package/dist/storage/SqliteWasmEngine.cjs +28 -20
  49. package/dist/storage/SqliteWasmEngine.d.cts +1 -1
  50. package/dist/storage/SqliteWasmEngine.d.ts +1 -1
  51. package/dist/storage/SqliteWasmEngine.js +5 -5
  52. package/package.json +3 -1
@@ -0,0 +1,140 @@
1
+ // src/model/hash/HashValidator.ts
2
+ var HashValidator = class {
3
+ /**
4
+ * Compute hash of content using specified algorithm
5
+ */
6
+ static async computeHash(content, algorithm = "sha256") {
7
+ const data = typeof content === "string" ? new TextEncoder().encode(content) : content;
8
+ let algoName = "SHA-256";
9
+ switch (algorithm.toLowerCase()) {
10
+ case "sha1":
11
+ algoName = "SHA-1";
12
+ break;
13
+ case "sha-1":
14
+ algoName = "SHA-1";
15
+ break;
16
+ case "sha256":
17
+ algoName = "SHA-256";
18
+ break;
19
+ case "sha-256":
20
+ algoName = "SHA-256";
21
+ break;
22
+ case "sha384":
23
+ algoName = "SHA-384";
24
+ break;
25
+ case "sha-384":
26
+ algoName = "SHA-384";
27
+ break;
28
+ case "sha512":
29
+ algoName = "SHA-512";
30
+ break;
31
+ case "sha-512":
32
+ algoName = "SHA-512";
33
+ break;
34
+ default:
35
+ console.warn(`Algorithm ${algorithm} not natively supported or mapped, defaulting to SHA-256`);
36
+ algoName = "SHA-256";
37
+ }
38
+ const buffer = new Uint8Array(data).buffer;
39
+ const hashBuffer = await crypto.subtle.digest(algoName, buffer);
40
+ return Array.from(new Uint8Array(hashBuffer)).map((b) => b.toString(16).padStart(2, "0")).join("");
41
+ }
42
+ /**
43
+ * Validate that content matches expected hash
44
+ */
45
+ static async validate(content, expectedHash) {
46
+ const computedHash = await this.computeHash(content);
47
+ return computedHash === expectedHash;
48
+ }
49
+ };
50
+
51
+ // src/model/GTime.ts
52
+ var VALID_HASH_ALGORITHMS = ["md5", "sha1", "sha224", "sha256", "sha384", "sha512"];
53
+ var GTime = class {
54
+ static DEFAULT_ALGORITHM = "sha256";
55
+ /**
56
+ * Generate a GTime stamp for the current moment
57
+ * Format: ALGORITHM|TIMESTAMP|LOCALE_OR_DID
58
+ * Representing the mathematical coordinate (a,b,c) for an event signature.
59
+ */
60
+ static stampNow(hashAlgorithm = this.DEFAULT_ALGORITHM, localeOrDID = "UTC") {
61
+ const algo = hashAlgorithm.toLowerCase();
62
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString();
63
+ return `${algo}|${timestamp}|${localeOrDID}`;
64
+ }
65
+ /**
66
+ * Parse a GTime string
67
+ */
68
+ static parse(gtime) {
69
+ const parts = gtime.split("|");
70
+ if (parts.length !== 3) {
71
+ throw new Error(`Invalid GTime format: ${gtime}`);
72
+ }
73
+ return {
74
+ algorithm: parts[0],
75
+ timestamp: new Date(parts[1]),
76
+ localeOrDID: parts[2]
77
+ };
78
+ }
79
+ /** Extract coordinate a: Algorithm */
80
+ static getAlgorithm(gtime) {
81
+ return this.parse(gtime).algorithm;
82
+ }
83
+ /** Alias for backward compatibility */
84
+ static getHashAlgorithm(gtime) {
85
+ return this.getAlgorithm(gtime);
86
+ }
87
+ /** Extract coordinate b: Timestamp */
88
+ static getTimestamp(gtime) {
89
+ return this.parse(gtime).timestamp;
90
+ }
91
+ /** Extract coordinate c: Locale or DID */
92
+ static getLocaleOrDID(gtime) {
93
+ return this.parse(gtime).localeOrDID;
94
+ }
95
+ /** Alias for backward compatibility */
96
+ static getRegionCode(gtime) {
97
+ return this.getLocaleOrDID(gtime);
98
+ }
99
+ /**
100
+ * Check if the provided hash function is valid.
101
+ * Matches Python's GTime.is_valid_hash_function()
102
+ */
103
+ static isValidHashFunction(hashFunction) {
104
+ if (!hashFunction || typeof hashFunction !== "string") {
105
+ return false;
106
+ }
107
+ return VALID_HASH_ALGORITHMS.includes(hashFunction.toLowerCase());
108
+ }
109
+ /**
110
+ * Check if the provided locale/region code is valid.
111
+ * Maintains backward compatibility by checking for strings.
112
+ */
113
+ static isValidRegionCode(regionCode) {
114
+ return Boolean(regionCode && typeof regionCode === "string");
115
+ }
116
+ /**
117
+ * Check if the provided timestamp is in ISO format.
118
+ * Matches Python's GTime.is_iso_format()
119
+ */
120
+ static isIsoFormat(timestamp) {
121
+ if (!timestamp || typeof timestamp !== "string") {
122
+ return false;
123
+ }
124
+ try {
125
+ const date = new Date(timestamp);
126
+ if (isNaN(date.getTime())) {
127
+ return false;
128
+ }
129
+ const isoPattern = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/;
130
+ return isoPattern.test(timestamp);
131
+ } catch {
132
+ return false;
133
+ }
134
+ }
135
+ };
136
+
137
+ export {
138
+ HashValidator,
139
+ GTime
140
+ };
@@ -0,0 +1,112 @@
1
+ import {
2
+ AbstractSqlEngine,
3
+ CORE_SCHEMAS
4
+ } from "./chunk-FPW33LMV.js";
5
+ import {
6
+ DEFAULT_SQLJS_WASM_URL
7
+ } from "./chunk-J3IDWMDN.js";
8
+ import {
9
+ MCard
10
+ } from "./chunk-GGQCF7ZK.js";
11
+
12
+ // src/storage/engines/SqliteWasmEngine.ts
13
+ var SqliteWasmEngine = class extends AbstractSqlEngine {
14
+ db = null;
15
+ SQL = null;
16
+ /**
17
+ * Initialize the database
18
+ * @param wasmUrl URL to sql-wasm.wasm file (optional, defaults to CDN)
19
+ * @param existingData Optional existing database as Uint8Array
20
+ */
21
+ async init(wasmUrl, existingData) {
22
+ const initSqlJs = (await import("sql.js")).default;
23
+ const SQL = await initSqlJs({
24
+ locateFile: (file) => wasmUrl || `${DEFAULT_SQLJS_WASM_URL}${file}`
25
+ });
26
+ this.SQL = SQL;
27
+ this.db = existingData ? new SQL.Database(existingData) : new SQL.Database();
28
+ this.db.run(CORE_SCHEMAS.card);
29
+ this.db.run(CORE_SCHEMAS.handleRegistry);
30
+ this.db.run(CORE_SCHEMAS.handleHistory);
31
+ this.db.run(CORE_SCHEMAS.handleIndex);
32
+ }
33
+ ensureDb() {
34
+ if (!this.db) throw new Error("Database not initialized. Call init() first.");
35
+ return this.db;
36
+ }
37
+ // ======================================================================
38
+ // AbstractSqlEngine primitives
39
+ // ======================================================================
40
+ async queryRows(sql, ...params) {
41
+ const db = this.ensureDb();
42
+ const stmt = db.prepare(sql);
43
+ if (params.length > 0) stmt.bind(params);
44
+ const results = [];
45
+ while (stmt.step()) {
46
+ const row = stmt.getAsObject();
47
+ results.push(row);
48
+ }
49
+ stmt.free();
50
+ return results;
51
+ }
52
+ async execSql(sql, ...params) {
53
+ const db = this.ensureDb();
54
+ if (params.length === 0) {
55
+ db.run(sql);
56
+ } else {
57
+ db.run(sql, params);
58
+ }
59
+ return 0;
60
+ }
61
+ // ======================================================================
62
+ // Row → MCard conversion (sql.js returns Uint8Array for BLOBs)
63
+ // ======================================================================
64
+ rowToCard(row) {
65
+ const rawContent = row.content;
66
+ const content = rawContent instanceof Uint8Array ? rawContent : typeof rawContent === "string" ? new TextEncoder().encode(rawContent) : new Uint8Array(0);
67
+ return MCard.fromData(content, String(row.hash), String(row.g_time));
68
+ }
69
+ // ======================================================================
70
+ // sql.js-specific helpers
71
+ // ======================================================================
72
+ /**
73
+ * Export database as Uint8Array (for persistence)
74
+ */
75
+ export() {
76
+ return this.ensureDb().export();
77
+ }
78
+ /**
79
+ * Get raw sql.js Database for use with VectorStore adapter.
80
+ */
81
+ getRawDb() {
82
+ return this.ensureDb();
83
+ }
84
+ // =========== pruneHandleHistory override (needs count before delete) ===========
85
+ async pruneHandleHistory(handle, options = {}) {
86
+ const { validateHandle } = await import("./Handle-3N4QOA3U.js");
87
+ const db = this.ensureDb();
88
+ const normalized = validateHandle(handle);
89
+ if (options.deleteAll) {
90
+ const stmt = db.prepare("SELECT COUNT(*) FROM handle_history WHERE handle = ?");
91
+ stmt.bind([normalized]);
92
+ stmt.step();
93
+ const count = stmt.get()[0];
94
+ stmt.free();
95
+ db.run("DELETE FROM handle_history WHERE handle = ?", [normalized]);
96
+ return count;
97
+ } else if (options.olderThan) {
98
+ const stmt = db.prepare("SELECT COUNT(*) FROM handle_history WHERE handle = ? AND changed_at < ?");
99
+ stmt.bind([normalized, options.olderThan]);
100
+ stmt.step();
101
+ const count = stmt.get()[0];
102
+ stmt.free();
103
+ db.run("DELETE FROM handle_history WHERE handle = ? AND changed_at < ?", [normalized, options.olderThan]);
104
+ return count;
105
+ }
106
+ return 0;
107
+ }
108
+ };
109
+
110
+ export {
111
+ SqliteWasmEngine
112
+ };
@@ -0,0 +1,170 @@
1
+ import {
2
+ GTime,
3
+ HashValidator
4
+ } from "./chunk-ASW6AOA7.js";
5
+
6
+ // src/model/ContentTypeInterpreter.ts
7
+ import { detect_content_type, is_binary_content as wasm_is_binary, get_extension as wasm_ext } from "mcard-wasm";
8
+ var ContentTypeInterpreter = class {
9
+ static detect(content) {
10
+ return this.detectContentType(content).mimeType;
11
+ }
12
+ static detectContentType(content, fileExtension) {
13
+ const buffer = typeof content === "string" ? new TextEncoder().encode(content) : content;
14
+ const result = detect_content_type(buffer, fileExtension);
15
+ return { mimeType: result[0], extension: result[1] };
16
+ }
17
+ static getExtension(mimeType) {
18
+ return wasm_ext(mimeType) || "bin";
19
+ }
20
+ static isBinaryContent(content, mimeType) {
21
+ const buffer = typeof content === "string" ? new TextEncoder().encode(content) : content;
22
+ return wasm_is_binary(buffer);
23
+ }
24
+ // Stub or bypass irrelevant manual TS rules now managed by mcard-core
25
+ static registerExtension(mimeType, extension) {
26
+ }
27
+ static registerExtensions(mapping) {
28
+ }
29
+ static isKnownLongLineExtension(extension) {
30
+ return false;
31
+ }
32
+ static isUnstructuredBinary(sample) {
33
+ if (sample.length < 512) return false;
34
+ let nullCount = 0;
35
+ let controlChars = 0;
36
+ for (let i = 0; i < sample.length; i++) {
37
+ const b = sample[i];
38
+ if (b === 0) nullCount++;
39
+ if (b < 32 && b !== 9 && b !== 10 && b !== 13) controlChars++;
40
+ }
41
+ const nullRatio = nullCount / sample.length;
42
+ const controlRatio = controlChars / sample.length;
43
+ return nullRatio > 0.1 || controlRatio > 0.2;
44
+ }
45
+ static hasPathologicalLines(sample, isKnownType) {
46
+ return false;
47
+ }
48
+ };
49
+
50
+ // src/types/dots.ts
51
+ function createMCardDOTSMetadata(tightRefs = [], looseRefs = []) {
52
+ return {
53
+ role: "Carrier" /* CARRIER */,
54
+ eosRole: "InvariantContent" /* INVARIANT_CONTENT */,
55
+ plane: "Data" /* DATA */,
56
+ tightRefs,
57
+ looseRefs
58
+ };
59
+ }
60
+ function createPCardDOTSMetadata(isLens, tightRefs = [], looseRefs = []) {
61
+ return {
62
+ role: isLens ? "Lens" /* LENS */ : "Chart" /* CHART */,
63
+ eosRole: "GenerativeLens" /* GENERATIVE_LENS */,
64
+ plane: "Control" /* CONTROL */,
65
+ tightRefs,
66
+ looseRefs
67
+ };
68
+ }
69
+ function createVCardDOTSMetadata() {
70
+ return {
71
+ role: "Arena" /* ARENA */,
72
+ eosRole: "SovereignDecision" /* SOVEREIGN_DECISION */,
73
+ plane: "Application" /* APPLICATION */
74
+ };
75
+ }
76
+
77
+ // src/model/MCard.ts
78
+ var MCard = class _MCard {
79
+ content;
80
+ hash;
81
+ g_time;
82
+ contentType;
83
+ // Defaulting to specific string or null
84
+ hashFunction;
85
+ constructor(content, hash, g_time, contentType, hashFunction) {
86
+ this.content = content;
87
+ this.hash = hash;
88
+ this.g_time = g_time;
89
+ this.contentType = contentType;
90
+ this.hashFunction = hashFunction;
91
+ }
92
+ /**
93
+ * Create a new MCard from content
94
+ */
95
+ static async create(content, hashAlgorithm = "sha256") {
96
+ if (content === null || content === void 0) {
97
+ throw new Error("Content cannot be null or undefined");
98
+ }
99
+ const bytes = typeof content === "string" ? new TextEncoder().encode(content) : content;
100
+ if (bytes.length === 0) {
101
+ throw new Error("Content cannot be empty");
102
+ }
103
+ const hash = await HashValidator.computeHash(bytes, hashAlgorithm);
104
+ const g_time = GTime.stampNow(hashAlgorithm);
105
+ const contentType = ContentTypeInterpreter.detect(bytes);
106
+ return new _MCard(bytes, hash, g_time, contentType, hashAlgorithm);
107
+ }
108
+ /**
109
+ * Create an MCard from existing data (e.g., from database)
110
+ */
111
+ static fromData(content, hash, g_time) {
112
+ const alg = GTime.getHashAlgorithm(g_time);
113
+ const contentType = ContentTypeInterpreter.detect(content);
114
+ return new _MCard(content, hash, g_time, contentType, alg);
115
+ }
116
+ /**
117
+ * Get content as text (UTF-8 decoded)
118
+ */
119
+ getContentAsText() {
120
+ return new TextDecoder().decode(this.content);
121
+ }
122
+ /**
123
+ * Get content as raw bytes
124
+ */
125
+ getContent() {
126
+ return this.content;
127
+ }
128
+ /**
129
+ * Convert to plain object
130
+ */
131
+ toObject() {
132
+ return {
133
+ hash: this.hash,
134
+ content: this.getContentAsText(),
135
+ g_time: this.g_time,
136
+ contentType: this.contentType,
137
+ hashFunction: this.hashFunction
138
+ };
139
+ }
140
+ /**
141
+ * Get DOTS vocabulary metadata for this MCard
142
+ *
143
+ * Returns the DOTS role information that positions this MCard
144
+ * in the Double Operadic Theory of Systems framework.
145
+ *
146
+ * MCard is always a CARRIER object in the Data Plane.
147
+ *
148
+ * @param tightRefs - Optional array of prerequisite MCard hashes (vertical composition)
149
+ * @param looseRefs - Optional array of alternative MCard hashes (horizontal composition)
150
+ * @returns DOTSMetadata describing this card's role in the compositional system
151
+ *
152
+ * @example
153
+ * ```typescript
154
+ * const card = await MCard.create('Hello World');
155
+ * const meta = card.getDOTSMetadata();
156
+ * console.log(meta.role); // 'Carrier'
157
+ * console.log(meta.plane); // 'Data'
158
+ * ```
159
+ */
160
+ getDOTSMetadata(tightRefs = [], looseRefs = []) {
161
+ return createMCardDOTSMetadata(tightRefs, looseRefs);
162
+ }
163
+ };
164
+
165
+ export {
166
+ ContentTypeInterpreter,
167
+ createPCardDOTSMetadata,
168
+ createVCardDOTSMetadata,
169
+ MCard
170
+ };