@cutos/core 4.0.8 → 4.0.9

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/dist/cutos.cjs.js CHANGED
@@ -393,19 +393,19 @@ const HEARTBEAT_STATUS = Object.freeze({
393
393
  });
394
394
 
395
395
  const cutosAPI$2 = {
396
- version: '3.0.0', CORE, CORE_NORESP, CORE_APP, DEVICE, HEARTBEAT_STATUS
396
+ version: '4.0', CORE, CORE_NORESP, CORE_APP, DEVICE, HEARTBEAT_STATUS
397
397
  };
398
398
  var cutosApi = cutosAPI$2;
399
399
 
400
400
  var name = "@cutos/core";
401
- var version = "4.0.8";
401
+ var version = "4.0.9";
402
402
  var description = "The CUTOS (CUT Operating System) Core API is a JavaScript library that provides essential functionalities for LWA (Local Web Application) edge computing and communication in the CUTOS ecosystem. ";
403
403
  var main = "dist/cutos.cjs.js";
404
404
  var module$1 = "dist/cutos.esm.js";
405
405
  var types = "types/index.d.ts";
406
406
  var scripts = {
407
407
  "build:rollup": "rollup -c rollup.config.cjs",
408
- test: "node test/device-provider-base.mjs && node test/mock-capability.mjs"
408
+ test: "node test/device-provider-base.mjs && node test/mock-capability.mjs && node test/mock-core-services.mjs"
409
409
  };
410
410
  var keywords = [
411
411
  "CUTOS",
@@ -623,7 +623,7 @@ function doCallback(id, body, err) {
623
623
  * @Database
624
624
  * The basic table structure of CUTOS database is in Key-Value format, and Value can be any json object.
625
625
  */
626
- class Database {
626
+ let Database$1 = class Database {
627
627
  KEY_TYPE_TEXT = "TEXT";
628
628
  KEY_TYPE_INTEGER = "INTEGER";
629
629
 
@@ -661,9 +661,9 @@ class Database {
661
661
  setupMirror(gwIPC, lwaName, confirm = false) {
662
662
  this.mirror = {gwIPC, lwaName, confirm};
663
663
  }
664
- }
664
+ };
665
665
 
666
- Database.Table = class {
666
+ Database$1.Table = class {
667
667
 
668
668
  /**
669
669
  * @constructor The constructor of Table
@@ -1819,7 +1819,7 @@ cutos.CoreDefine = {
1819
1819
  DEVICE: cutosAPI$1.DEVICE, HEARTBEAT_STATUS: cutosAPI$1.HEARTBEAT_STATUS
1820
1820
  };
1821
1821
  cutos.CoreClass = {
1822
- Device, Driver, Database, Capability, Provider, DeviceCapabilityBase: DeviceCapabilityBase$1, DeviceProviderBase: DeviceProviderBase$1, CutosError: CutosError$2
1822
+ Device, Driver, Database: Database$1, Capability, Provider, DeviceCapabilityBase: DeviceCapabilityBase$1, DeviceProviderBase: DeviceProviderBase$1, CutosError: CutosError$2
1823
1823
  };
1824
1824
 
1825
1825
  cutos.cutosAPI = cutosAPI$1;
@@ -1829,7 +1829,7 @@ cutos.DeviceProviderBase = DeviceProviderBase$1;
1829
1829
  cutos.configureMockTransports = configureMockTransports$1;
1830
1830
 
1831
1831
  // simulate cutos core for development
1832
- const {CoreAPI: CoreAPI$2, CoreClass: CoreClass$1} = cutos;
1832
+ const {CoreAPI: CoreAPI$2, CoreClass: CoreClass$2} = cutos;
1833
1833
 
1834
1834
  const CoreSimulator$1 = {
1835
1835
  start: function (driver, port = 1883) {
@@ -1841,7 +1841,7 @@ const CoreSimulator$1 = {
1841
1841
 
1842
1842
  console.log('CUTOS Simulator started and listening on port ', port);
1843
1843
 
1844
- let drvDefault = new CoreClass$1.Driver();
1844
+ let drvDefault = new CoreClass$2.Driver();
1845
1845
  drvDefault.onCommand((command) => {
1846
1846
  console.log('drvDefault onCommand', JSON.stringify(command));
1847
1847
  if (command.cmd === "init") {
@@ -1861,7 +1861,10 @@ const CoreSimulator$1 = {
1861
1861
 
1862
1862
  var coreSimulator = {CoreSimulator: CoreSimulator$1};
1863
1863
 
1864
- const {CoreAPI: CoreAPI$1, CutosError: CutosError$1, configureMockTransports} = cutos;
1864
+ const {CoreAPI: CoreAPI$1, CoreClass: CoreClass$1, CutosError: CutosError$1, configureMockTransports} = cutos;
1865
+
1866
+ const Database = CoreClass$1.Database;
1867
+ const DatabaseTable = CoreClass$1.Database.Table;
1865
1868
 
1866
1869
  const original = {
1867
1870
  init: CoreAPI$1.init,
@@ -1870,17 +1873,42 @@ const original = {
1870
1873
  getConfig: CoreAPI$1.getConfig,
1871
1874
  getBoxInfo: CoreAPI$1.getBoxInfo,
1872
1875
  getDeviceInfo: CoreAPI$1.getDeviceInfo,
1876
+ getIPC: CoreAPI$1.getIPC,
1877
+ getNotification: CoreAPI$1.getNotification,
1878
+ getLogger: CoreAPI$1.getLogger,
1873
1879
  shell: CoreAPI$1.shell,
1874
1880
  getVolume: CoreAPI$1.getVolume,
1875
1881
  setVolume: CoreAPI$1.setVolume,
1876
- setHttpProxy: CoreAPI$1.setHttpProxy
1882
+ setHttpProxy: CoreAPI$1.setHttpProxy,
1883
+ database: {
1884
+ connect: Database.prototype.connect,
1885
+ run: Database.prototype.run,
1886
+ tableCreate: DatabaseTable.prototype.create,
1887
+ tableInsert: DatabaseTable.prototype.insert,
1888
+ tableInsertById: DatabaseTable.prototype.insertById,
1889
+ tableInsertByKey: DatabaseTable.prototype.insertByKey,
1890
+ tableUpdate: DatabaseTable.prototype.update,
1891
+ tableUpdateByKey: DatabaseTable.prototype.updateByKey,
1892
+ tableDelete: DatabaseTable.prototype.delete,
1893
+ tableQuery: DatabaseTable.prototype.query,
1894
+ tableQueryByKey: DatabaseTable.prototype.queryByKey,
1895
+ tableQueryByTid: DatabaseTable.prototype.queryByTid,
1896
+ tableQueryUnsynced: DatabaseTable.prototype.queryUnsynced,
1897
+ tableQueryAll: DatabaseTable.prototype.queryAll,
1898
+ tableSync: DatabaseTable.prototype.sync
1899
+ }
1877
1900
  };
1878
1901
 
1879
1902
  const mockState = {
1880
1903
  installed: false,
1881
1904
  connected: false,
1882
1905
  options: {},
1883
- capabilities: new Map()
1906
+ capabilities: new Map(),
1907
+ databases: new Map(),
1908
+ ipcListeners: new Map(),
1909
+ notificationListeners: new Set(),
1910
+ logs: [],
1911
+ httpProxies: new Map()
1884
1912
  };
1885
1913
 
1886
1914
  function resolveCallback(callback, value, error) {
@@ -1891,6 +1919,23 @@ function resolveCallback(callback, value, error) {
1891
1919
  return error ? Promise.reject(error) : Promise.resolve(value);
1892
1920
  }
1893
1921
 
1922
+ function resolveCoreResult(callback, value, error) {
1923
+ if (typeof callback === 'function') {
1924
+ const result = error
1925
+ ? {status: false, msg: normalizeMockError(error)}
1926
+ : {status: true, msg: value};
1927
+ setTimeout(() => callback(result), 0);
1928
+ return true;
1929
+ }
1930
+ return error ? Promise.reject(normalizeMockError(error)) : Promise.resolve(value);
1931
+ }
1932
+
1933
+ function normalizeMockError(error) {
1934
+ return error instanceof CutosError$1
1935
+ ? error
1936
+ : new CutosError$1('CUTOS_MOCK_ERROR', error && error.message ? error.message : String(error));
1937
+ }
1938
+
1894
1939
  function normalizeMethod(method) {
1895
1940
  return typeof method === 'function' ? method : () => method;
1896
1941
  }
@@ -1998,18 +2043,390 @@ function createMockDriver(name, type) {
1998
2043
  startBeat() {},
1999
2044
  updateStatusInfo(status, info) {
2000
2045
  record.heartbeat = {status, info};
2046
+ record.dataListeners.forEach(listener => listener({event: 'status', data: info}));
2001
2047
  }
2002
2048
  };
2003
2049
  }
2004
2050
 
2051
+ function getDatabaseName(db) {
2052
+ return db || 'default';
2053
+ }
2054
+
2055
+ function ensureDatabase(db) {
2056
+ const dbName = getDatabaseName(db);
2057
+ if (!mockState.databases.has(dbName)) {
2058
+ mockState.databases.set(dbName, {name: dbName, tables: new Map()});
2059
+ }
2060
+ return mockState.databases.get(dbName);
2061
+ }
2062
+
2063
+ function ensureTable(table) {
2064
+ const database = ensureDatabase(table.db);
2065
+ if (!database.tables.has(table.name)) {
2066
+ database.tables.set(table.name, {
2067
+ name: table.name,
2068
+ opts: {keyName: 'id', keyType: 'INTEGER'},
2069
+ rows: new Map(),
2070
+ keyIndex: new Map(),
2071
+ nextId: 1
2072
+ });
2073
+ }
2074
+ return database.tables.get(table.name);
2075
+ }
2076
+
2077
+ function clone(value) {
2078
+ if (value === undefined || value === null) return value;
2079
+ try {
2080
+ const json = JSON.stringify(value);
2081
+ return json === undefined ? value : JSON.parse(json);
2082
+ } catch (error) {
2083
+ return value;
2084
+ }
2085
+ }
2086
+
2087
+ function createRow(tableRecord, id, key, value, tid) {
2088
+ const timestamp = Date.now();
2089
+ const row = {
2090
+ id,
2091
+ key: key ?? id,
2092
+ value: clone(value),
2093
+ tid,
2094
+ timestamp,
2095
+ synced: false
2096
+ };
2097
+ tableRecord.rows.set(id, row);
2098
+ if (row.key !== undefined && row.key !== null) {
2099
+ tableRecord.keyIndex.set(String(row.key), id);
2100
+ }
2101
+ if (typeof id === 'number' && id >= tableRecord.nextId) {
2102
+ tableRecord.nextId = id + 1;
2103
+ }
2104
+ return row;
2105
+ }
2106
+
2107
+ function formatRow(row) {
2108
+ if (!row) return null;
2109
+ return {
2110
+ id: row.id,
2111
+ key: row.key,
2112
+ value: clone(row.value),
2113
+ tid: row.tid,
2114
+ timestamp: row.timestamp,
2115
+ synced: row.synced
2116
+ };
2117
+ }
2118
+
2119
+ function getTableRowByKey(tableRecord, key) {
2120
+ const id = tableRecord.keyIndex.get(String(key));
2121
+ return id === undefined ? null : tableRecord.rows.get(id);
2122
+ }
2123
+
2124
+ function installDatabaseMock() {
2125
+ Database.prototype.connect = function (callback) {
2126
+ const database = ensureDatabase(this.db);
2127
+ return resolveCoreResult(callback, {db: database.name, connected: true});
2128
+ };
2129
+
2130
+ Database.prototype.run = function (sql, callback) {
2131
+ return resolveCoreResult(callback, {
2132
+ db: getDatabaseName(this.db),
2133
+ sql,
2134
+ changes: 0
2135
+ });
2136
+ };
2137
+
2138
+ DatabaseTable.prototype.create = function (opts, callback) {
2139
+ if (typeof opts === 'function') {
2140
+ callback = opts;
2141
+ opts = {};
2142
+ }
2143
+ opts = opts || {};
2144
+ opts.keyName = opts.keyName || 'id';
2145
+ opts.keyType = opts.keyType || 'INTEGER';
2146
+ if (opts.keyType !== 'INTEGER' && opts.keyType !== 'TEXT') {
2147
+ throw new Error('keyType MUST be INTEGER or TEXT');
2148
+ }
2149
+ this.opts = opts;
2150
+
2151
+ const tableRecord = ensureTable(this);
2152
+ tableRecord.opts = {
2153
+ keyName: opts.keyName,
2154
+ keyType: opts.keyType,
2155
+ retentionTime: opts.retentionTime
2156
+ };
2157
+ return resolveCoreResult(callback, {
2158
+ db: getDatabaseName(this.db),
2159
+ name: this.name,
2160
+ keyName: tableRecord.opts.keyName,
2161
+ keyType: tableRecord.opts.keyType
2162
+ });
2163
+ };
2164
+
2165
+ DatabaseTable.prototype.insert = function (value, opts, callback) {
2166
+ if (typeof opts === 'function') {
2167
+ callback = opts;
2168
+ opts = {};
2169
+ }
2170
+ opts = opts || {};
2171
+ const tableRecord = ensureTable(this);
2172
+ const row = createRow(tableRecord, tableRecord.nextId, undefined, value, opts.tid);
2173
+ return resolveCoreResult(callback, {row: row.id, timestamp: row.timestamp});
2174
+ };
2175
+
2176
+ DatabaseTable.prototype.insertById = function (id, value, opts, callback) {
2177
+ if (typeof opts === 'function') {
2178
+ callback = opts;
2179
+ opts = {};
2180
+ }
2181
+ opts = opts || {};
2182
+ const tableRecord = ensureTable(this);
2183
+ const row = createRow(tableRecord, id, id, value, opts.tid);
2184
+ return resolveCoreResult(callback, {row: row.id, timestamp: row.timestamp});
2185
+ };
2186
+
2187
+ DatabaseTable.prototype.insertByKey = function (key, value, opts, callback) {
2188
+ if (typeof opts === 'function') {
2189
+ callback = opts;
2190
+ opts = {};
2191
+ }
2192
+ opts = opts || {};
2193
+ const tableRecord = ensureTable(this);
2194
+ const row = createRow(tableRecord, tableRecord.nextId, key, value, opts.tid);
2195
+ return resolveCoreResult(callback, {row: row.id, key: row.key, timestamp: row.timestamp});
2196
+ };
2197
+
2198
+ DatabaseTable.prototype.update = function (id, value, callback) {
2199
+ const tableRecord = ensureTable(this);
2200
+ const row = tableRecord.rows.get(id);
2201
+ if (!row) {
2202
+ return resolveCoreResult(callback, null, new CutosError$1('MOCK_DB_ROW_NOT_FOUND', `Row ${id} does not exist.`));
2203
+ }
2204
+ row.value = clone(value);
2205
+ row.timestamp = Date.now();
2206
+ row.synced = false;
2207
+ return resolveCoreResult(callback, {row: row.id, timestamp: row.timestamp});
2208
+ };
2209
+
2210
+ DatabaseTable.prototype.updateByKey = function (key, value, callback) {
2211
+ const tableRecord = ensureTable(this);
2212
+ const row = getTableRowByKey(tableRecord, key);
2213
+ if (!row) {
2214
+ return resolveCoreResult(callback, null, new CutosError$1('MOCK_DB_ROW_NOT_FOUND', `Key ${key} does not exist.`));
2215
+ }
2216
+ row.value = clone(value);
2217
+ row.timestamp = Date.now();
2218
+ row.synced = false;
2219
+ return resolveCoreResult(callback, {row: row.id, key: row.key, timestamp: row.timestamp});
2220
+ };
2221
+
2222
+ DatabaseTable.prototype.delete = function (id, callback) {
2223
+ const tableRecord = ensureTable(this);
2224
+ const row = tableRecord.rows.get(id);
2225
+ if (row && row.key !== undefined && row.key !== null) {
2226
+ tableRecord.keyIndex.delete(String(row.key));
2227
+ }
2228
+ const deleted = tableRecord.rows.delete(id);
2229
+ return resolveCoreResult(callback, {row: id, deleted});
2230
+ };
2231
+
2232
+ DatabaseTable.prototype.query = function (id, callback) {
2233
+ const row = ensureTable(this).rows.get(id);
2234
+ return resolveCoreResult(callback, formatRow(row));
2235
+ };
2236
+
2237
+ DatabaseTable.prototype.queryByKey = function (key, callback) {
2238
+ const row = getTableRowByKey(ensureTable(this), key);
2239
+ return resolveCoreResult(callback, formatRow(row));
2240
+ };
2241
+
2242
+ DatabaseTable.prototype.queryByTid = function (tid, callback) {
2243
+ const rows = [...ensureTable(this).rows.values()]
2244
+ .filter(row => row.tid === tid)
2245
+ .map(formatRow);
2246
+ return resolveCoreResult(callback, rows);
2247
+ };
2248
+
2249
+ DatabaseTable.prototype.queryUnsynced = function (opts, callback) {
2250
+ if (typeof opts === 'function') {
2251
+ callback = opts;
2252
+ opts = {limit: 100};
2253
+ }
2254
+ opts = opts || {};
2255
+ const limit = opts.limit && opts.limit > 0 ? opts.limit : 100;
2256
+ const rows = [...ensureTable(this).rows.values()]
2257
+ .filter(row => !row.synced)
2258
+ .slice(0, limit)
2259
+ .map(formatRow);
2260
+ return resolveCoreResult(callback, rows);
2261
+ };
2262
+
2263
+ DatabaseTable.prototype.queryAll = function (callback) {
2264
+ const rows = [...ensureTable(this).rows.values()].map(formatRow);
2265
+ return resolveCoreResult(callback, rows);
2266
+ };
2267
+
2268
+ DatabaseTable.prototype.sync = function (id, callback) {
2269
+ const row = ensureTable(this).rows.get(id);
2270
+ if (!row) {
2271
+ return resolveCoreResult(callback, null, new CutosError$1('MOCK_DB_ROW_NOT_FOUND', `Row ${id} does not exist.`));
2272
+ }
2273
+ row.synced = true;
2274
+ return resolveCoreResult(callback, {row: id, synced: true});
2275
+ };
2276
+ }
2277
+
2278
+ function uninstallDatabaseMock() {
2279
+ Database.prototype.connect = original.database.connect;
2280
+ Database.prototype.run = original.database.run;
2281
+ DatabaseTable.prototype.create = original.database.tableCreate;
2282
+ DatabaseTable.prototype.insert = original.database.tableInsert;
2283
+ DatabaseTable.prototype.insertById = original.database.tableInsertById;
2284
+ DatabaseTable.prototype.insertByKey = original.database.tableInsertByKey;
2285
+ DatabaseTable.prototype.update = original.database.tableUpdate;
2286
+ DatabaseTable.prototype.updateByKey = original.database.tableUpdateByKey;
2287
+ DatabaseTable.prototype.delete = original.database.tableDelete;
2288
+ DatabaseTable.prototype.query = original.database.tableQuery;
2289
+ DatabaseTable.prototype.queryByKey = original.database.tableQueryByKey;
2290
+ DatabaseTable.prototype.queryByTid = original.database.tableQueryByTid;
2291
+ DatabaseTable.prototype.queryUnsynced = original.database.tableQueryUnsynced;
2292
+ DatabaseTable.prototype.queryAll = original.database.tableQueryAll;
2293
+ DatabaseTable.prototype.sync = original.database.tableSync;
2294
+ }
2295
+
2296
+ function invokeIpcListener(channel, args, callback) {
2297
+ const listeners = mockState.ipcListeners.get(channel);
2298
+ const listener = listeners && listeners.values().next().value;
2299
+ if (!listener) {
2300
+ return resolveCallback(callback, null, new CutosError$1('IPC_CHANNEL_NOT_FOUND', `IPC channel ${channel} is not registered.`));
2301
+ }
2302
+
2303
+ const execute = (resolve, reject) => {
2304
+ let responded = false;
2305
+ const respond = result => {
2306
+ if (!responded) {
2307
+ responded = true;
2308
+ resolve(result);
2309
+ }
2310
+ };
2311
+
2312
+ try {
2313
+ const result = listener(args, respond);
2314
+ if (result !== undefined) {
2315
+ Promise.resolve(result).then(respond).catch(reject);
2316
+ }
2317
+ } catch (error) {
2318
+ reject(error);
2319
+ }
2320
+ };
2321
+
2322
+ if (typeof callback === 'function') {
2323
+ execute(
2324
+ result => callback(result),
2325
+ error => callback(null, normalizeMockError(error))
2326
+ );
2327
+ return true;
2328
+ }
2329
+
2330
+ return new Promise((resolve, reject) => {
2331
+ execute(resolve, error => reject(normalizeMockError(error)));
2332
+ });
2333
+ }
2334
+
2335
+ function createMockIPC(destAddress) {
2336
+ return {
2337
+ destAddress,
2338
+ sendTo(channel, args, callback = null) {
2339
+ if (typeof callback === 'function') {
2340
+ invokeIpcListener(channel, args, callback);
2341
+ } else {
2342
+ invokeIpcListener(channel, args, () => {});
2343
+ }
2344
+ return true;
2345
+ },
2346
+ sendToWithOptions(channel, args, opts = {}, callback = null) {
2347
+ if (typeof opts === 'function') {
2348
+ callback = opts;
2349
+ }
2350
+ return this.sendTo(channel, args, callback);
2351
+ },
2352
+ call(channel, args, callback = null) {
2353
+ return invokeIpcListener(channel, args, callback);
2354
+ },
2355
+ callWithOptions(channel, args, opts = {}, callback = null) {
2356
+ if (typeof opts === 'function') {
2357
+ callback = opts;
2358
+ }
2359
+ return this.call(channel, args, callback);
2360
+ },
2361
+ on(channel, listener) {
2362
+ if (!mockState.ipcListeners.has(channel)) {
2363
+ mockState.ipcListeners.set(channel, new Set());
2364
+ }
2365
+ mockState.ipcListeners.get(channel).add(listener);
2366
+ return () => this.removeListener(channel, listener);
2367
+ },
2368
+ removeListener(channel, listener = null) {
2369
+ if (!mockState.ipcListeners.has(channel)) return;
2370
+ if (!listener) {
2371
+ mockState.ipcListeners.delete(channel);
2372
+ return;
2373
+ }
2374
+ const listeners = mockState.ipcListeners.get(channel);
2375
+ listeners.delete(listener);
2376
+ if (listeners.size === 0) {
2377
+ mockState.ipcListeners.delete(channel);
2378
+ }
2379
+ }
2380
+ };
2381
+ }
2382
+
2383
+ function createMockNotification() {
2384
+ return {
2385
+ register(listener) {
2386
+ mockState.notificationListeners.add(listener);
2387
+ },
2388
+ unregister() {
2389
+ mockState.notificationListeners.clear();
2390
+ },
2391
+ emit(event, msg) {
2392
+ const payload = {event, msg};
2393
+ mockState.notificationListeners.forEach(listener => listener(payload));
2394
+ return true;
2395
+ }
2396
+ };
2397
+ }
2398
+
2399
+ function createMockLogger() {
2400
+ const write = (level, remark, content, type = 'LWA') => {
2401
+ const entry = {type, level, remark, content, timestamp: Date.now()};
2402
+ mockState.logs.push(entry);
2403
+ if (typeof mockState.options.onLog === 'function') {
2404
+ mockState.options.onLog(entry);
2405
+ }
2406
+ return true;
2407
+ };
2408
+ return {
2409
+ info: (remark, content, type) => write('info', remark, content, type),
2410
+ warning: (remark, content, type) => write('warning', remark, content, type),
2411
+ error: (remark, content, type) => write('error', remark, content, type),
2412
+ debug: (remark, content, type) => write('debug', remark, content, type)
2413
+ };
2414
+ }
2415
+
2005
2416
  function installCutosMock$1(options = {}) {
2006
2417
  mockState.options = options;
2007
2418
  mockState.connected = false;
2008
2419
  mockState.capabilities.clear();
2420
+ mockState.databases.clear();
2421
+ mockState.ipcListeners.clear();
2422
+ mockState.notificationListeners.clear();
2423
+ mockState.logs = [];
2424
+ mockState.httpProxies.clear();
2009
2425
 
2010
2426
  const capabilities = options.capabilities || {};
2011
2427
  Object.keys(capabilities).forEach(name => registerCapability(name, capabilities[name]));
2012
2428
  configureMockTransports(createMockDevice, createMockDriver);
2429
+ installDatabaseMock();
2013
2430
 
2014
2431
  CoreAPI$1.init = function (host = 'localhost', callback) {
2015
2432
  if (typeof host === 'function') callback = host;
@@ -2021,13 +2438,20 @@ function installCutosMock$1(options = {}) {
2021
2438
  CoreAPI$1.getConfig = callback => resolveCallback(callback, options.config ?? {});
2022
2439
  CoreAPI$1.getBoxInfo = callback => resolveCallback(callback, options.boxInfo ?? {name: 'CUTOS Mock Node'});
2023
2440
  CoreAPI$1.getDeviceInfo = callback => resolveCallback(callback, options.deviceInfo ?? {name: 'CUTOS Mock Device'});
2441
+ CoreAPI$1.getIPC = destAddress => createMockIPC(destAddress);
2442
+ CoreAPI$1.getNotification = () => createMockNotification();
2443
+ CoreAPI$1.getLogger = () => createMockLogger();
2024
2444
  CoreAPI$1.shell = (command, callback) => resolveCallback(callback, {command, stdout: '', stderr: '', code: 0});
2025
2445
  CoreAPI$1.getVolume = callback => resolveCallback(callback, options.volume ?? 50);
2026
2446
  CoreAPI$1.setVolume = (vol, callback) => {
2027
2447
  mockState.options.volume = vol;
2028
2448
  return resolveCallback(callback, vol);
2029
2449
  };
2030
- CoreAPI$1.setHttpProxy = (api, target, callback) => resolveCallback(callback, {api, target});
2450
+ CoreAPI$1.setHttpProxy = (api, target, callback) => {
2451
+ const proxy = {api, target};
2452
+ mockState.httpProxies.set(api, proxy);
2453
+ return resolveCallback(callback, proxy);
2454
+ };
2031
2455
 
2032
2456
  mockState.installed = true;
2033
2457
  return CutosMock$1;
@@ -2035,12 +2459,16 @@ function installCutosMock$1(options = {}) {
2035
2459
 
2036
2460
  function uninstallCutosMock$1() {
2037
2461
  configureMockTransports();
2462
+ uninstallDatabaseMock();
2038
2463
  CoreAPI$1.init = original.init;
2039
2464
  CoreAPI$1.connected = original.connected;
2040
2465
  CoreAPI$1.getPlatform = original.getPlatform;
2041
2466
  CoreAPI$1.getConfig = original.getConfig;
2042
2467
  CoreAPI$1.getBoxInfo = original.getBoxInfo;
2043
2468
  CoreAPI$1.getDeviceInfo = original.getDeviceInfo;
2469
+ CoreAPI$1.getIPC = original.getIPC;
2470
+ CoreAPI$1.getNotification = original.getNotification;
2471
+ CoreAPI$1.getLogger = original.getLogger;
2044
2472
  CoreAPI$1.shell = original.shell;
2045
2473
  CoreAPI$1.getVolume = original.getVolume;
2046
2474
  CoreAPI$1.setVolume = original.setVolume;
@@ -2049,12 +2477,19 @@ function uninstallCutosMock$1() {
2049
2477
  mockState.connected = false;
2050
2478
  mockState.installed = false;
2051
2479
  mockState.capabilities.clear();
2480
+ mockState.databases.clear();
2481
+ mockState.ipcListeners.clear();
2482
+ mockState.notificationListeners.clear();
2483
+ mockState.logs = [];
2484
+ mockState.httpProxies.clear();
2052
2485
  }
2053
2486
 
2054
2487
  const CutosMock$1 = {
2055
2488
  install: installCutosMock$1,
2056
2489
  uninstall: uninstallCutosMock$1,
2057
2490
  registerCapability,
2491
+ getLogs: () => mockState.logs.map(clone),
2492
+ getHttpProxies: () => [...mockState.httpProxies.values()].map(clone),
2058
2493
  isInstalled: () => mockState.installed
2059
2494
  };
2060
2495
 
package/dist/cutos.esm.js CHANGED
@@ -685,19 +685,19 @@ const HEARTBEAT_STATUS = Object.freeze({
685
685
  });
686
686
 
687
687
  const cutosAPI$2 = {
688
- version: '3.0.0', CORE, CORE_NORESP, CORE_APP, DEVICE, HEARTBEAT_STATUS
688
+ version: '4.0', CORE, CORE_NORESP, CORE_APP, DEVICE, HEARTBEAT_STATUS
689
689
  };
690
690
  var cutosApi = cutosAPI$2;
691
691
 
692
692
  var name = "@cutos/core";
693
- var version = "4.0.8";
693
+ var version = "4.0.9";
694
694
  var description = "The CUTOS (CUT Operating System) Core API is a JavaScript library that provides essential functionalities for LWA (Local Web Application) edge computing and communication in the CUTOS ecosystem. ";
695
695
  var main = "dist/cutos.cjs.js";
696
696
  var module = "dist/cutos.esm.js";
697
697
  var types = "types/index.d.ts";
698
698
  var scripts = {
699
699
  "build:rollup": "rollup -c rollup.config.cjs",
700
- test: "node test/device-provider-base.mjs && node test/mock-capability.mjs"
700
+ test: "node test/device-provider-base.mjs && node test/mock-capability.mjs && node test/mock-core-services.mjs"
701
701
  };
702
702
  var keywords = [
703
703
  "CUTOS",
@@ -915,7 +915,7 @@ function doCallback(id, body, err) {
915
915
  * @Database
916
916
  * The basic table structure of CUTOS database is in Key-Value format, and Value can be any json object.
917
917
  */
918
- class Database {
918
+ let Database$1 = class Database {
919
919
  KEY_TYPE_TEXT = "TEXT";
920
920
  KEY_TYPE_INTEGER = "INTEGER";
921
921
 
@@ -953,9 +953,9 @@ class Database {
953
953
  setupMirror(gwIPC, lwaName, confirm = false) {
954
954
  this.mirror = {gwIPC, lwaName, confirm};
955
955
  }
956
- }
956
+ };
957
957
 
958
- Database.Table = class {
958
+ Database$1.Table = class {
959
959
 
960
960
  /**
961
961
  * @constructor The constructor of Table
@@ -2111,7 +2111,7 @@ cutos.CoreDefine = {
2111
2111
  DEVICE: cutosAPI$1.DEVICE, HEARTBEAT_STATUS: cutosAPI$1.HEARTBEAT_STATUS
2112
2112
  };
2113
2113
  cutos.CoreClass = {
2114
- Device, Driver, Database, Capability, Provider, DeviceCapabilityBase: DeviceCapabilityBase$1, DeviceProviderBase: DeviceProviderBase$1, CutosError: CutosError$2
2114
+ Device, Driver, Database: Database$1, Capability, Provider, DeviceCapabilityBase: DeviceCapabilityBase$1, DeviceProviderBase: DeviceProviderBase$1, CutosError: CutosError$2
2115
2115
  };
2116
2116
 
2117
2117
  cutos.cutosAPI = cutosAPI$1;
@@ -2121,7 +2121,7 @@ cutos.DeviceProviderBase = DeviceProviderBase$1;
2121
2121
  cutos.configureMockTransports = configureMockTransports$1;
2122
2122
 
2123
2123
  // simulate cutos core for development
2124
- const {CoreAPI: CoreAPI$2, CoreClass: CoreClass$1} = cutos;
2124
+ const {CoreAPI: CoreAPI$2, CoreClass: CoreClass$2} = cutos;
2125
2125
 
2126
2126
  const CoreSimulator$1 = {
2127
2127
  start: function (driver, port = 1883) {
@@ -2133,7 +2133,7 @@ const CoreSimulator$1 = {
2133
2133
 
2134
2134
  console.log('CUTOS Simulator started and listening on port ', port);
2135
2135
 
2136
- let drvDefault = new CoreClass$1.Driver();
2136
+ let drvDefault = new CoreClass$2.Driver();
2137
2137
  drvDefault.onCommand((command) => {
2138
2138
  console.log('drvDefault onCommand', JSON.stringify(command));
2139
2139
  if (command.cmd === "init") {
@@ -2153,7 +2153,10 @@ const CoreSimulator$1 = {
2153
2153
 
2154
2154
  var coreSimulator = {CoreSimulator: CoreSimulator$1};
2155
2155
 
2156
- const {CoreAPI: CoreAPI$1, CutosError: CutosError$1, configureMockTransports} = cutos;
2156
+ const {CoreAPI: CoreAPI$1, CoreClass: CoreClass$1, CutosError: CutosError$1, configureMockTransports} = cutos;
2157
+
2158
+ const Database = CoreClass$1.Database;
2159
+ const DatabaseTable = CoreClass$1.Database.Table;
2157
2160
 
2158
2161
  const original = {
2159
2162
  init: CoreAPI$1.init,
@@ -2162,17 +2165,42 @@ const original = {
2162
2165
  getConfig: CoreAPI$1.getConfig,
2163
2166
  getBoxInfo: CoreAPI$1.getBoxInfo,
2164
2167
  getDeviceInfo: CoreAPI$1.getDeviceInfo,
2168
+ getIPC: CoreAPI$1.getIPC,
2169
+ getNotification: CoreAPI$1.getNotification,
2170
+ getLogger: CoreAPI$1.getLogger,
2165
2171
  shell: CoreAPI$1.shell,
2166
2172
  getVolume: CoreAPI$1.getVolume,
2167
2173
  setVolume: CoreAPI$1.setVolume,
2168
- setHttpProxy: CoreAPI$1.setHttpProxy
2174
+ setHttpProxy: CoreAPI$1.setHttpProxy,
2175
+ database: {
2176
+ connect: Database.prototype.connect,
2177
+ run: Database.prototype.run,
2178
+ tableCreate: DatabaseTable.prototype.create,
2179
+ tableInsert: DatabaseTable.prototype.insert,
2180
+ tableInsertById: DatabaseTable.prototype.insertById,
2181
+ tableInsertByKey: DatabaseTable.prototype.insertByKey,
2182
+ tableUpdate: DatabaseTable.prototype.update,
2183
+ tableUpdateByKey: DatabaseTable.prototype.updateByKey,
2184
+ tableDelete: DatabaseTable.prototype.delete,
2185
+ tableQuery: DatabaseTable.prototype.query,
2186
+ tableQueryByKey: DatabaseTable.prototype.queryByKey,
2187
+ tableQueryByTid: DatabaseTable.prototype.queryByTid,
2188
+ tableQueryUnsynced: DatabaseTable.prototype.queryUnsynced,
2189
+ tableQueryAll: DatabaseTable.prototype.queryAll,
2190
+ tableSync: DatabaseTable.prototype.sync
2191
+ }
2169
2192
  };
2170
2193
 
2171
2194
  const mockState = {
2172
2195
  installed: false,
2173
2196
  connected: false,
2174
2197
  options: {},
2175
- capabilities: new Map()
2198
+ capabilities: new Map(),
2199
+ databases: new Map(),
2200
+ ipcListeners: new Map(),
2201
+ notificationListeners: new Set(),
2202
+ logs: [],
2203
+ httpProxies: new Map()
2176
2204
  };
2177
2205
 
2178
2206
  function resolveCallback(callback, value, error) {
@@ -2183,6 +2211,23 @@ function resolveCallback(callback, value, error) {
2183
2211
  return error ? Promise.reject(error) : Promise.resolve(value);
2184
2212
  }
2185
2213
 
2214
+ function resolveCoreResult(callback, value, error) {
2215
+ if (typeof callback === 'function') {
2216
+ const result = error
2217
+ ? {status: false, msg: normalizeMockError(error)}
2218
+ : {status: true, msg: value};
2219
+ setTimeout(() => callback(result), 0);
2220
+ return true;
2221
+ }
2222
+ return error ? Promise.reject(normalizeMockError(error)) : Promise.resolve(value);
2223
+ }
2224
+
2225
+ function normalizeMockError(error) {
2226
+ return error instanceof CutosError$1
2227
+ ? error
2228
+ : new CutosError$1('CUTOS_MOCK_ERROR', error && error.message ? error.message : String(error));
2229
+ }
2230
+
2186
2231
  function normalizeMethod(method) {
2187
2232
  return typeof method === 'function' ? method : () => method;
2188
2233
  }
@@ -2290,18 +2335,390 @@ function createMockDriver(name, type) {
2290
2335
  startBeat() {},
2291
2336
  updateStatusInfo(status, info) {
2292
2337
  record.heartbeat = {status, info};
2338
+ record.dataListeners.forEach(listener => listener({event: 'status', data: info}));
2293
2339
  }
2294
2340
  };
2295
2341
  }
2296
2342
 
2343
+ function getDatabaseName(db) {
2344
+ return db || 'default';
2345
+ }
2346
+
2347
+ function ensureDatabase(db) {
2348
+ const dbName = getDatabaseName(db);
2349
+ if (!mockState.databases.has(dbName)) {
2350
+ mockState.databases.set(dbName, {name: dbName, tables: new Map()});
2351
+ }
2352
+ return mockState.databases.get(dbName);
2353
+ }
2354
+
2355
+ function ensureTable(table) {
2356
+ const database = ensureDatabase(table.db);
2357
+ if (!database.tables.has(table.name)) {
2358
+ database.tables.set(table.name, {
2359
+ name: table.name,
2360
+ opts: {keyName: 'id', keyType: 'INTEGER'},
2361
+ rows: new Map(),
2362
+ keyIndex: new Map(),
2363
+ nextId: 1
2364
+ });
2365
+ }
2366
+ return database.tables.get(table.name);
2367
+ }
2368
+
2369
+ function clone(value) {
2370
+ if (value === undefined || value === null) return value;
2371
+ try {
2372
+ const json = JSON.stringify(value);
2373
+ return json === undefined ? value : JSON.parse(json);
2374
+ } catch (error) {
2375
+ return value;
2376
+ }
2377
+ }
2378
+
2379
+ function createRow(tableRecord, id, key, value, tid) {
2380
+ const timestamp = Date.now();
2381
+ const row = {
2382
+ id,
2383
+ key: key ?? id,
2384
+ value: clone(value),
2385
+ tid,
2386
+ timestamp,
2387
+ synced: false
2388
+ };
2389
+ tableRecord.rows.set(id, row);
2390
+ if (row.key !== undefined && row.key !== null) {
2391
+ tableRecord.keyIndex.set(String(row.key), id);
2392
+ }
2393
+ if (typeof id === 'number' && id >= tableRecord.nextId) {
2394
+ tableRecord.nextId = id + 1;
2395
+ }
2396
+ return row;
2397
+ }
2398
+
2399
+ function formatRow(row) {
2400
+ if (!row) return null;
2401
+ return {
2402
+ id: row.id,
2403
+ key: row.key,
2404
+ value: clone(row.value),
2405
+ tid: row.tid,
2406
+ timestamp: row.timestamp,
2407
+ synced: row.synced
2408
+ };
2409
+ }
2410
+
2411
+ function getTableRowByKey(tableRecord, key) {
2412
+ const id = tableRecord.keyIndex.get(String(key));
2413
+ return id === undefined ? null : tableRecord.rows.get(id);
2414
+ }
2415
+
2416
+ function installDatabaseMock() {
2417
+ Database.prototype.connect = function (callback) {
2418
+ const database = ensureDatabase(this.db);
2419
+ return resolveCoreResult(callback, {db: database.name, connected: true});
2420
+ };
2421
+
2422
+ Database.prototype.run = function (sql, callback) {
2423
+ return resolveCoreResult(callback, {
2424
+ db: getDatabaseName(this.db),
2425
+ sql,
2426
+ changes: 0
2427
+ });
2428
+ };
2429
+
2430
+ DatabaseTable.prototype.create = function (opts, callback) {
2431
+ if (typeof opts === 'function') {
2432
+ callback = opts;
2433
+ opts = {};
2434
+ }
2435
+ opts = opts || {};
2436
+ opts.keyName = opts.keyName || 'id';
2437
+ opts.keyType = opts.keyType || 'INTEGER';
2438
+ if (opts.keyType !== 'INTEGER' && opts.keyType !== 'TEXT') {
2439
+ throw new Error('keyType MUST be INTEGER or TEXT');
2440
+ }
2441
+ this.opts = opts;
2442
+
2443
+ const tableRecord = ensureTable(this);
2444
+ tableRecord.opts = {
2445
+ keyName: opts.keyName,
2446
+ keyType: opts.keyType,
2447
+ retentionTime: opts.retentionTime
2448
+ };
2449
+ return resolveCoreResult(callback, {
2450
+ db: getDatabaseName(this.db),
2451
+ name: this.name,
2452
+ keyName: tableRecord.opts.keyName,
2453
+ keyType: tableRecord.opts.keyType
2454
+ });
2455
+ };
2456
+
2457
+ DatabaseTable.prototype.insert = function (value, opts, callback) {
2458
+ if (typeof opts === 'function') {
2459
+ callback = opts;
2460
+ opts = {};
2461
+ }
2462
+ opts = opts || {};
2463
+ const tableRecord = ensureTable(this);
2464
+ const row = createRow(tableRecord, tableRecord.nextId, undefined, value, opts.tid);
2465
+ return resolveCoreResult(callback, {row: row.id, timestamp: row.timestamp});
2466
+ };
2467
+
2468
+ DatabaseTable.prototype.insertById = function (id, value, opts, callback) {
2469
+ if (typeof opts === 'function') {
2470
+ callback = opts;
2471
+ opts = {};
2472
+ }
2473
+ opts = opts || {};
2474
+ const tableRecord = ensureTable(this);
2475
+ const row = createRow(tableRecord, id, id, value, opts.tid);
2476
+ return resolveCoreResult(callback, {row: row.id, timestamp: row.timestamp});
2477
+ };
2478
+
2479
+ DatabaseTable.prototype.insertByKey = function (key, value, opts, callback) {
2480
+ if (typeof opts === 'function') {
2481
+ callback = opts;
2482
+ opts = {};
2483
+ }
2484
+ opts = opts || {};
2485
+ const tableRecord = ensureTable(this);
2486
+ const row = createRow(tableRecord, tableRecord.nextId, key, value, opts.tid);
2487
+ return resolveCoreResult(callback, {row: row.id, key: row.key, timestamp: row.timestamp});
2488
+ };
2489
+
2490
+ DatabaseTable.prototype.update = function (id, value, callback) {
2491
+ const tableRecord = ensureTable(this);
2492
+ const row = tableRecord.rows.get(id);
2493
+ if (!row) {
2494
+ return resolveCoreResult(callback, null, new CutosError$1('MOCK_DB_ROW_NOT_FOUND', `Row ${id} does not exist.`));
2495
+ }
2496
+ row.value = clone(value);
2497
+ row.timestamp = Date.now();
2498
+ row.synced = false;
2499
+ return resolveCoreResult(callback, {row: row.id, timestamp: row.timestamp});
2500
+ };
2501
+
2502
+ DatabaseTable.prototype.updateByKey = function (key, value, callback) {
2503
+ const tableRecord = ensureTable(this);
2504
+ const row = getTableRowByKey(tableRecord, key);
2505
+ if (!row) {
2506
+ return resolveCoreResult(callback, null, new CutosError$1('MOCK_DB_ROW_NOT_FOUND', `Key ${key} does not exist.`));
2507
+ }
2508
+ row.value = clone(value);
2509
+ row.timestamp = Date.now();
2510
+ row.synced = false;
2511
+ return resolveCoreResult(callback, {row: row.id, key: row.key, timestamp: row.timestamp});
2512
+ };
2513
+
2514
+ DatabaseTable.prototype.delete = function (id, callback) {
2515
+ const tableRecord = ensureTable(this);
2516
+ const row = tableRecord.rows.get(id);
2517
+ if (row && row.key !== undefined && row.key !== null) {
2518
+ tableRecord.keyIndex.delete(String(row.key));
2519
+ }
2520
+ const deleted = tableRecord.rows.delete(id);
2521
+ return resolveCoreResult(callback, {row: id, deleted});
2522
+ };
2523
+
2524
+ DatabaseTable.prototype.query = function (id, callback) {
2525
+ const row = ensureTable(this).rows.get(id);
2526
+ return resolveCoreResult(callback, formatRow(row));
2527
+ };
2528
+
2529
+ DatabaseTable.prototype.queryByKey = function (key, callback) {
2530
+ const row = getTableRowByKey(ensureTable(this), key);
2531
+ return resolveCoreResult(callback, formatRow(row));
2532
+ };
2533
+
2534
+ DatabaseTable.prototype.queryByTid = function (tid, callback) {
2535
+ const rows = [...ensureTable(this).rows.values()]
2536
+ .filter(row => row.tid === tid)
2537
+ .map(formatRow);
2538
+ return resolveCoreResult(callback, rows);
2539
+ };
2540
+
2541
+ DatabaseTable.prototype.queryUnsynced = function (opts, callback) {
2542
+ if (typeof opts === 'function') {
2543
+ callback = opts;
2544
+ opts = {limit: 100};
2545
+ }
2546
+ opts = opts || {};
2547
+ const limit = opts.limit && opts.limit > 0 ? opts.limit : 100;
2548
+ const rows = [...ensureTable(this).rows.values()]
2549
+ .filter(row => !row.synced)
2550
+ .slice(0, limit)
2551
+ .map(formatRow);
2552
+ return resolveCoreResult(callback, rows);
2553
+ };
2554
+
2555
+ DatabaseTable.prototype.queryAll = function (callback) {
2556
+ const rows = [...ensureTable(this).rows.values()].map(formatRow);
2557
+ return resolveCoreResult(callback, rows);
2558
+ };
2559
+
2560
+ DatabaseTable.prototype.sync = function (id, callback) {
2561
+ const row = ensureTable(this).rows.get(id);
2562
+ if (!row) {
2563
+ return resolveCoreResult(callback, null, new CutosError$1('MOCK_DB_ROW_NOT_FOUND', `Row ${id} does not exist.`));
2564
+ }
2565
+ row.synced = true;
2566
+ return resolveCoreResult(callback, {row: id, synced: true});
2567
+ };
2568
+ }
2569
+
2570
+ function uninstallDatabaseMock() {
2571
+ Database.prototype.connect = original.database.connect;
2572
+ Database.prototype.run = original.database.run;
2573
+ DatabaseTable.prototype.create = original.database.tableCreate;
2574
+ DatabaseTable.prototype.insert = original.database.tableInsert;
2575
+ DatabaseTable.prototype.insertById = original.database.tableInsertById;
2576
+ DatabaseTable.prototype.insertByKey = original.database.tableInsertByKey;
2577
+ DatabaseTable.prototype.update = original.database.tableUpdate;
2578
+ DatabaseTable.prototype.updateByKey = original.database.tableUpdateByKey;
2579
+ DatabaseTable.prototype.delete = original.database.tableDelete;
2580
+ DatabaseTable.prototype.query = original.database.tableQuery;
2581
+ DatabaseTable.prototype.queryByKey = original.database.tableQueryByKey;
2582
+ DatabaseTable.prototype.queryByTid = original.database.tableQueryByTid;
2583
+ DatabaseTable.prototype.queryUnsynced = original.database.tableQueryUnsynced;
2584
+ DatabaseTable.prototype.queryAll = original.database.tableQueryAll;
2585
+ DatabaseTable.prototype.sync = original.database.tableSync;
2586
+ }
2587
+
2588
+ function invokeIpcListener(channel, args, callback) {
2589
+ const listeners = mockState.ipcListeners.get(channel);
2590
+ const listener = listeners && listeners.values().next().value;
2591
+ if (!listener) {
2592
+ return resolveCallback(callback, null, new CutosError$1('IPC_CHANNEL_NOT_FOUND', `IPC channel ${channel} is not registered.`));
2593
+ }
2594
+
2595
+ const execute = (resolve, reject) => {
2596
+ let responded = false;
2597
+ const respond = result => {
2598
+ if (!responded) {
2599
+ responded = true;
2600
+ resolve(result);
2601
+ }
2602
+ };
2603
+
2604
+ try {
2605
+ const result = listener(args, respond);
2606
+ if (result !== undefined) {
2607
+ Promise.resolve(result).then(respond).catch(reject);
2608
+ }
2609
+ } catch (error) {
2610
+ reject(error);
2611
+ }
2612
+ };
2613
+
2614
+ if (typeof callback === 'function') {
2615
+ execute(
2616
+ result => callback(result),
2617
+ error => callback(null, normalizeMockError(error))
2618
+ );
2619
+ return true;
2620
+ }
2621
+
2622
+ return new Promise((resolve, reject) => {
2623
+ execute(resolve, error => reject(normalizeMockError(error)));
2624
+ });
2625
+ }
2626
+
2627
+ function createMockIPC(destAddress) {
2628
+ return {
2629
+ destAddress,
2630
+ sendTo(channel, args, callback = null) {
2631
+ if (typeof callback === 'function') {
2632
+ invokeIpcListener(channel, args, callback);
2633
+ } else {
2634
+ invokeIpcListener(channel, args, () => {});
2635
+ }
2636
+ return true;
2637
+ },
2638
+ sendToWithOptions(channel, args, opts = {}, callback = null) {
2639
+ if (typeof opts === 'function') {
2640
+ callback = opts;
2641
+ }
2642
+ return this.sendTo(channel, args, callback);
2643
+ },
2644
+ call(channel, args, callback = null) {
2645
+ return invokeIpcListener(channel, args, callback);
2646
+ },
2647
+ callWithOptions(channel, args, opts = {}, callback = null) {
2648
+ if (typeof opts === 'function') {
2649
+ callback = opts;
2650
+ }
2651
+ return this.call(channel, args, callback);
2652
+ },
2653
+ on(channel, listener) {
2654
+ if (!mockState.ipcListeners.has(channel)) {
2655
+ mockState.ipcListeners.set(channel, new Set());
2656
+ }
2657
+ mockState.ipcListeners.get(channel).add(listener);
2658
+ return () => this.removeListener(channel, listener);
2659
+ },
2660
+ removeListener(channel, listener = null) {
2661
+ if (!mockState.ipcListeners.has(channel)) return;
2662
+ if (!listener) {
2663
+ mockState.ipcListeners.delete(channel);
2664
+ return;
2665
+ }
2666
+ const listeners = mockState.ipcListeners.get(channel);
2667
+ listeners.delete(listener);
2668
+ if (listeners.size === 0) {
2669
+ mockState.ipcListeners.delete(channel);
2670
+ }
2671
+ }
2672
+ };
2673
+ }
2674
+
2675
+ function createMockNotification() {
2676
+ return {
2677
+ register(listener) {
2678
+ mockState.notificationListeners.add(listener);
2679
+ },
2680
+ unregister() {
2681
+ mockState.notificationListeners.clear();
2682
+ },
2683
+ emit(event, msg) {
2684
+ const payload = {event, msg};
2685
+ mockState.notificationListeners.forEach(listener => listener(payload));
2686
+ return true;
2687
+ }
2688
+ };
2689
+ }
2690
+
2691
+ function createMockLogger() {
2692
+ const write = (level, remark, content, type = 'LWA') => {
2693
+ const entry = {type, level, remark, content, timestamp: Date.now()};
2694
+ mockState.logs.push(entry);
2695
+ if (typeof mockState.options.onLog === 'function') {
2696
+ mockState.options.onLog(entry);
2697
+ }
2698
+ return true;
2699
+ };
2700
+ return {
2701
+ info: (remark, content, type) => write('info', remark, content, type),
2702
+ warning: (remark, content, type) => write('warning', remark, content, type),
2703
+ error: (remark, content, type) => write('error', remark, content, type),
2704
+ debug: (remark, content, type) => write('debug', remark, content, type)
2705
+ };
2706
+ }
2707
+
2297
2708
  function installCutosMock$1(options = {}) {
2298
2709
  mockState.options = options;
2299
2710
  mockState.connected = false;
2300
2711
  mockState.capabilities.clear();
2712
+ mockState.databases.clear();
2713
+ mockState.ipcListeners.clear();
2714
+ mockState.notificationListeners.clear();
2715
+ mockState.logs = [];
2716
+ mockState.httpProxies.clear();
2301
2717
 
2302
2718
  const capabilities = options.capabilities || {};
2303
2719
  Object.keys(capabilities).forEach(name => registerCapability(name, capabilities[name]));
2304
2720
  configureMockTransports(createMockDevice, createMockDriver);
2721
+ installDatabaseMock();
2305
2722
 
2306
2723
  CoreAPI$1.init = function (host = 'localhost', callback) {
2307
2724
  if (typeof host === 'function') callback = host;
@@ -2313,13 +2730,20 @@ function installCutosMock$1(options = {}) {
2313
2730
  CoreAPI$1.getConfig = callback => resolveCallback(callback, options.config ?? {});
2314
2731
  CoreAPI$1.getBoxInfo = callback => resolveCallback(callback, options.boxInfo ?? {name: 'CUTOS Mock Node'});
2315
2732
  CoreAPI$1.getDeviceInfo = callback => resolveCallback(callback, options.deviceInfo ?? {name: 'CUTOS Mock Device'});
2733
+ CoreAPI$1.getIPC = destAddress => createMockIPC(destAddress);
2734
+ CoreAPI$1.getNotification = () => createMockNotification();
2735
+ CoreAPI$1.getLogger = () => createMockLogger();
2316
2736
  CoreAPI$1.shell = (command, callback) => resolveCallback(callback, {command, stdout: '', stderr: '', code: 0});
2317
2737
  CoreAPI$1.getVolume = callback => resolveCallback(callback, options.volume ?? 50);
2318
2738
  CoreAPI$1.setVolume = (vol, callback) => {
2319
2739
  mockState.options.volume = vol;
2320
2740
  return resolveCallback(callback, vol);
2321
2741
  };
2322
- CoreAPI$1.setHttpProxy = (api, target, callback) => resolveCallback(callback, {api, target});
2742
+ CoreAPI$1.setHttpProxy = (api, target, callback) => {
2743
+ const proxy = {api, target};
2744
+ mockState.httpProxies.set(api, proxy);
2745
+ return resolveCallback(callback, proxy);
2746
+ };
2323
2747
 
2324
2748
  mockState.installed = true;
2325
2749
  return CutosMock$1;
@@ -2327,12 +2751,16 @@ function installCutosMock$1(options = {}) {
2327
2751
 
2328
2752
  function uninstallCutosMock$1() {
2329
2753
  configureMockTransports();
2754
+ uninstallDatabaseMock();
2330
2755
  CoreAPI$1.init = original.init;
2331
2756
  CoreAPI$1.connected = original.connected;
2332
2757
  CoreAPI$1.getPlatform = original.getPlatform;
2333
2758
  CoreAPI$1.getConfig = original.getConfig;
2334
2759
  CoreAPI$1.getBoxInfo = original.getBoxInfo;
2335
2760
  CoreAPI$1.getDeviceInfo = original.getDeviceInfo;
2761
+ CoreAPI$1.getIPC = original.getIPC;
2762
+ CoreAPI$1.getNotification = original.getNotification;
2763
+ CoreAPI$1.getLogger = original.getLogger;
2336
2764
  CoreAPI$1.shell = original.shell;
2337
2765
  CoreAPI$1.getVolume = original.getVolume;
2338
2766
  CoreAPI$1.setVolume = original.setVolume;
@@ -2341,12 +2769,19 @@ function uninstallCutosMock$1() {
2341
2769
  mockState.connected = false;
2342
2770
  mockState.installed = false;
2343
2771
  mockState.capabilities.clear();
2772
+ mockState.databases.clear();
2773
+ mockState.ipcListeners.clear();
2774
+ mockState.notificationListeners.clear();
2775
+ mockState.logs = [];
2776
+ mockState.httpProxies.clear();
2344
2777
  }
2345
2778
 
2346
2779
  const CutosMock$1 = {
2347
2780
  install: installCutosMock$1,
2348
2781
  uninstall: uninstallCutosMock$1,
2349
2782
  registerCapability,
2783
+ getLogs: () => mockState.logs.map(clone),
2784
+ getHttpProxies: () => [...mockState.httpProxies.values()].map(clone),
2350
2785
  isInstalled: () => mockState.installed
2351
2786
  };
2352
2787
 
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@cutos/core",
3
- "version": "4.0.8",
3
+ "version": "4.0.9",
4
4
  "description": "The CUTOS (CUT Operating System) Core API is a JavaScript library that provides essential functionalities for LWA (Local Web Application) edge computing and communication in the CUTOS ecosystem. ",
5
5
  "main": "dist/cutos.cjs.js",
6
6
  "module": "dist/cutos.esm.js",
7
7
  "types": "types/index.d.ts",
8
8
  "scripts": {
9
9
  "build:rollup": "rollup -c rollup.config.cjs",
10
- "test": "node test/device-provider-base.mjs && node test/mock-capability.mjs"
10
+ "test": "node test/device-provider-base.mjs && node test/mock-capability.mjs && node test/mock-core-services.mjs"
11
11
  },
12
12
  "keywords": [
13
13
  "CUTOS", "core", "database", "IPC", "notification", "logging", "device&driver"
package/types/index.d.ts CHANGED
@@ -397,6 +397,8 @@ export interface CutosMock {
397
397
  install(options?: CutosMockOptions): CutosMock;
398
398
  uninstall(): void;
399
399
  registerCapability(name: string, definition?: CutosMockCapabilityDefinition): unknown;
400
+ getLogs(): unknown[];
401
+ getHttpProxies(): unknown[];
400
402
  isInstalled(): boolean;
401
403
  }
402
404