@cutos/core 4.0.7 → 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/README.md +112 -7
- package/dist/cutos.cjs.js +566 -38
- package/dist/cutos.esm.js +566 -38
- package/package.json +2 -2
- package/types/index.d.ts +2 -0
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: '
|
|
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.
|
|
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"
|
|
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",
|
|
@@ -470,6 +470,13 @@ let callbackTimer = null;
|
|
|
470
470
|
let coreApiInitialized = false;
|
|
471
471
|
let _logger = null;
|
|
472
472
|
let _notification = null;
|
|
473
|
+
let mockDeviceFactory = null;
|
|
474
|
+
let mockDriverFactory = null;
|
|
475
|
+
|
|
476
|
+
function configureMockTransports$1(deviceFactory = null, driverFactory = null) {
|
|
477
|
+
mockDeviceFactory = deviceFactory;
|
|
478
|
+
mockDriverFactory = driverFactory;
|
|
479
|
+
}
|
|
473
480
|
|
|
474
481
|
let CutosError$2 = class CutosError extends Error {
|
|
475
482
|
constructor(code = 'CUTOS_ERROR', message = 'CUTOS error', detail = undefined) {
|
|
@@ -616,7 +623,7 @@ function doCallback(id, body, err) {
|
|
|
616
623
|
* @Database
|
|
617
624
|
* The basic table structure of CUTOS database is in Key-Value format, and Value can be any json object.
|
|
618
625
|
*/
|
|
619
|
-
class Database {
|
|
626
|
+
let Database$1 = class Database {
|
|
620
627
|
KEY_TYPE_TEXT = "TEXT";
|
|
621
628
|
KEY_TYPE_INTEGER = "INTEGER";
|
|
622
629
|
|
|
@@ -654,9 +661,9 @@ class Database {
|
|
|
654
661
|
setupMirror(gwIPC, lwaName, confirm = false) {
|
|
655
662
|
this.mirror = {gwIPC, lwaName, confirm};
|
|
656
663
|
}
|
|
657
|
-
}
|
|
664
|
+
};
|
|
658
665
|
|
|
659
|
-
Database.Table = class {
|
|
666
|
+
Database$1.Table = class {
|
|
660
667
|
|
|
661
668
|
/**
|
|
662
669
|
* @constructor The constructor of Table
|
|
@@ -1171,6 +1178,9 @@ class Device {
|
|
|
1171
1178
|
* @param type
|
|
1172
1179
|
*/
|
|
1173
1180
|
constructor(name = 'default-device-name', type = cutosAPI$1.DEVICE.DEFAULT) {
|
|
1181
|
+
if (mockDeviceFactory) {
|
|
1182
|
+
return mockDeviceFactory(name, type);
|
|
1183
|
+
}
|
|
1174
1184
|
this.name = name;
|
|
1175
1185
|
this.type = type;
|
|
1176
1186
|
this.topicData = cutosAPI$1.DEVICE.CHANNEL + '-' + type + '-data';
|
|
@@ -1244,6 +1254,9 @@ class Driver {
|
|
|
1244
1254
|
* @param counter
|
|
1245
1255
|
*/
|
|
1246
1256
|
constructor(name = 'default-driver-name', type = cutosAPI$1.DEVICE.DEFAULT, counter = 10) {
|
|
1257
|
+
if (mockDriverFactory) {
|
|
1258
|
+
return mockDriverFactory(name, type, counter);
|
|
1259
|
+
}
|
|
1247
1260
|
this.name = name;
|
|
1248
1261
|
this.type = type;
|
|
1249
1262
|
this.heartbeat = new Heartbeat(type, counter);
|
|
@@ -1354,11 +1367,15 @@ class Capability {
|
|
|
1354
1367
|
this.eventListeners.get(event).add(listener);
|
|
1355
1368
|
|
|
1356
1369
|
if (!this.dataListenerRegistered) {
|
|
1357
|
-
if (
|
|
1358
|
-
|
|
1370
|
+
if (typeof this.device.onData === "function") {
|
|
1371
|
+
this.unsubscribeData = this.device.onData(message => this._dispatchEvent(message));
|
|
1372
|
+
} else {
|
|
1373
|
+
if (!listenerMap.has(this.device.topicData)) {
|
|
1374
|
+
client.subscribe(this.device.topicData);
|
|
1375
|
+
}
|
|
1376
|
+
listenerMap.set(this.device.topicData, message => this._dispatchEvent(message));
|
|
1377
|
+
this.unsubscribeData = () => listenerMap.delete(this.device.topicData);
|
|
1359
1378
|
}
|
|
1360
|
-
listenerMap.set(this.device.topicData, message => this._dispatchEvent(message));
|
|
1361
|
-
this.unsubscribeData = () => listenerMap.delete(this.device.topicData);
|
|
1362
1379
|
this.dataListenerRegistered = true;
|
|
1363
1380
|
}
|
|
1364
1381
|
|
|
@@ -1802,16 +1819,17 @@ cutos.CoreDefine = {
|
|
|
1802
1819
|
DEVICE: cutosAPI$1.DEVICE, HEARTBEAT_STATUS: cutosAPI$1.HEARTBEAT_STATUS
|
|
1803
1820
|
};
|
|
1804
1821
|
cutos.CoreClass = {
|
|
1805
|
-
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
|
|
1806
1823
|
};
|
|
1807
1824
|
|
|
1808
1825
|
cutos.cutosAPI = cutosAPI$1;
|
|
1809
1826
|
cutos.CutosError = CutosError$2;
|
|
1810
1827
|
cutos.DeviceCapabilityBase = DeviceCapabilityBase$1;
|
|
1811
1828
|
cutos.DeviceProviderBase = DeviceProviderBase$1;
|
|
1829
|
+
cutos.configureMockTransports = configureMockTransports$1;
|
|
1812
1830
|
|
|
1813
1831
|
// simulate cutos core for development
|
|
1814
|
-
const {CoreAPI: CoreAPI$2, CoreClass: CoreClass$
|
|
1832
|
+
const {CoreAPI: CoreAPI$2, CoreClass: CoreClass$2} = cutos;
|
|
1815
1833
|
|
|
1816
1834
|
const CoreSimulator$1 = {
|
|
1817
1835
|
start: function (driver, port = 1883) {
|
|
@@ -1823,7 +1841,7 @@ const CoreSimulator$1 = {
|
|
|
1823
1841
|
|
|
1824
1842
|
console.log('CUTOS Simulator started and listening on port ', port);
|
|
1825
1843
|
|
|
1826
|
-
let drvDefault = new CoreClass$
|
|
1844
|
+
let drvDefault = new CoreClass$2.Driver();
|
|
1827
1845
|
drvDefault.onCommand((command) => {
|
|
1828
1846
|
console.log('drvDefault onCommand', JSON.stringify(command));
|
|
1829
1847
|
if (command.cmd === "init") {
|
|
@@ -1843,7 +1861,10 @@ const CoreSimulator$1 = {
|
|
|
1843
1861
|
|
|
1844
1862
|
var coreSimulator = {CoreSimulator: CoreSimulator$1};
|
|
1845
1863
|
|
|
1846
|
-
const {CoreAPI: CoreAPI$1, CutosError: CutosError$1} = 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;
|
|
1847
1868
|
|
|
1848
1869
|
const original = {
|
|
1849
1870
|
init: CoreAPI$1.init,
|
|
@@ -1852,10 +1873,30 @@ const original = {
|
|
|
1852
1873
|
getConfig: CoreAPI$1.getConfig,
|
|
1853
1874
|
getBoxInfo: CoreAPI$1.getBoxInfo,
|
|
1854
1875
|
getDeviceInfo: CoreAPI$1.getDeviceInfo,
|
|
1876
|
+
getIPC: CoreAPI$1.getIPC,
|
|
1877
|
+
getNotification: CoreAPI$1.getNotification,
|
|
1878
|
+
getLogger: CoreAPI$1.getLogger,
|
|
1855
1879
|
shell: CoreAPI$1.shell,
|
|
1856
1880
|
getVolume: CoreAPI$1.getVolume,
|
|
1857
1881
|
setVolume: CoreAPI$1.setVolume,
|
|
1858
|
-
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
|
+
}
|
|
1859
1900
|
};
|
|
1860
1901
|
|
|
1861
1902
|
const mockState = {
|
|
@@ -1863,7 +1904,11 @@ const mockState = {
|
|
|
1863
1904
|
connected: false,
|
|
1864
1905
|
options: {},
|
|
1865
1906
|
capabilities: new Map(),
|
|
1866
|
-
|
|
1907
|
+
databases: new Map(),
|
|
1908
|
+
ipcListeners: new Map(),
|
|
1909
|
+
notificationListeners: new Set(),
|
|
1910
|
+
logs: [],
|
|
1911
|
+
httpProxies: new Map()
|
|
1867
1912
|
};
|
|
1868
1913
|
|
|
1869
1914
|
function resolveCallback(callback, value, error) {
|
|
@@ -1871,25 +1916,37 @@ function resolveCallback(callback, value, error) {
|
|
|
1871
1916
|
setTimeout(() => callback(error ? null : value, error), 0);
|
|
1872
1917
|
return true;
|
|
1873
1918
|
}
|
|
1919
|
+
return error ? Promise.reject(error) : Promise.resolve(value);
|
|
1920
|
+
}
|
|
1874
1921
|
|
|
1875
|
-
|
|
1876
|
-
|
|
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;
|
|
1877
1929
|
}
|
|
1878
|
-
return Promise.resolve(value);
|
|
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));
|
|
1879
1937
|
}
|
|
1880
1938
|
|
|
1881
1939
|
function normalizeMethod(method) {
|
|
1882
|
-
|
|
1883
|
-
return method;
|
|
1884
|
-
}
|
|
1885
|
-
return () => method;
|
|
1940
|
+
return typeof method === 'function' ? method : () => method;
|
|
1886
1941
|
}
|
|
1887
1942
|
|
|
1888
1943
|
function ensureCapabilityRecord(name) {
|
|
1889
1944
|
if (!mockState.capabilities.has(name)) {
|
|
1890
1945
|
mockState.capabilities.set(name, {
|
|
1891
1946
|
methods: new Map(),
|
|
1892
|
-
|
|
1947
|
+
dataListeners: new Set(),
|
|
1948
|
+
commandListener: null,
|
|
1949
|
+
heartbeat: null
|
|
1893
1950
|
});
|
|
1894
1951
|
}
|
|
1895
1952
|
return mockState.capabilities.get(name);
|
|
@@ -1898,55 +1955,520 @@ function ensureCapabilityRecord(name) {
|
|
|
1898
1955
|
function registerCapability(name, definition = {}) {
|
|
1899
1956
|
const record = ensureCapabilityRecord(name);
|
|
1900
1957
|
const methods = definition.methods || {};
|
|
1901
|
-
|
|
1902
1958
|
Object.keys(methods).forEach(methodName => {
|
|
1903
1959
|
record.methods.set(methodName, normalizeMethod(methods[methodName]));
|
|
1904
1960
|
});
|
|
1905
|
-
|
|
1906
1961
|
return record;
|
|
1907
1962
|
}
|
|
1908
1963
|
|
|
1964
|
+
function invokeMockMethod(record, command, callback) {
|
|
1965
|
+
if (record.commandListener) {
|
|
1966
|
+
record.commandListener(command, callback);
|
|
1967
|
+
return true;
|
|
1968
|
+
}
|
|
1969
|
+
|
|
1970
|
+
const handler = record.methods.get(command.cmd);
|
|
1971
|
+
if (!handler) {
|
|
1972
|
+
callback({
|
|
1973
|
+
status: false,
|
|
1974
|
+
msg: new CutosError$1('METHOD_NOT_FOUND', `Mock capability method ${command.cmd} is not registered.`)
|
|
1975
|
+
});
|
|
1976
|
+
return true;
|
|
1977
|
+
}
|
|
1978
|
+
|
|
1979
|
+
Promise.resolve()
|
|
1980
|
+
.then(() => handler(command.args, {command}))
|
|
1981
|
+
.then(result => callback({status: true, msg: result}))
|
|
1982
|
+
.catch(error => callback({status: false, msg: error}));
|
|
1983
|
+
return true;
|
|
1984
|
+
}
|
|
1985
|
+
|
|
1986
|
+
function invokeMockTransport(record, command, callback) {
|
|
1987
|
+
if (typeof callback === 'function') {
|
|
1988
|
+
return invokeMockMethod(record, command, callback);
|
|
1989
|
+
}
|
|
1990
|
+
return new Promise((resolve, reject) => {
|
|
1991
|
+
invokeMockMethod(record, command, result => {
|
|
1992
|
+
if (result && result.status === false) {
|
|
1993
|
+
reject(result.msg);
|
|
1994
|
+
} else {
|
|
1995
|
+
resolve(result && result.status === true ? result.msg : result);
|
|
1996
|
+
}
|
|
1997
|
+
});
|
|
1998
|
+
});
|
|
1999
|
+
}
|
|
2000
|
+
|
|
2001
|
+
function createMockDevice(name, type) {
|
|
2002
|
+
const record = ensureCapabilityRecord(type);
|
|
2003
|
+
return {
|
|
2004
|
+
name,
|
|
2005
|
+
type,
|
|
2006
|
+
topicData: `mock-${type}-data`,
|
|
2007
|
+
init(opts, callback) {
|
|
2008
|
+
if (typeof opts === 'function') callback = opts;
|
|
2009
|
+
return resolveCallback(callback, mockState.options.initResult ?? `Mock device ${type} initialized`);
|
|
2010
|
+
},
|
|
2011
|
+
sendCommand(command, callback) {
|
|
2012
|
+
return invokeMockTransport(record, command, callback);
|
|
2013
|
+
},
|
|
2014
|
+
sendCommandWithOptions(command, opts, callback) {
|
|
2015
|
+
if (typeof opts === 'function') callback = opts;
|
|
2016
|
+
return invokeMockTransport(record, command, callback);
|
|
2017
|
+
},
|
|
2018
|
+
onData(listener) {
|
|
2019
|
+
record.dataListeners.add(listener);
|
|
2020
|
+
return () => record.dataListeners.delete(listener);
|
|
2021
|
+
}
|
|
2022
|
+
};
|
|
2023
|
+
}
|
|
2024
|
+
|
|
2025
|
+
function createMockDriver(name, type) {
|
|
2026
|
+
const record = ensureCapabilityRecord(type);
|
|
2027
|
+
return {
|
|
2028
|
+
name,
|
|
2029
|
+
type,
|
|
2030
|
+
beatInterval: 3000,
|
|
2031
|
+
onCommand(listener) {
|
|
2032
|
+
record.commandListener = listener;
|
|
2033
|
+
return () => {
|
|
2034
|
+
if (record.commandListener === listener) record.commandListener = null;
|
|
2035
|
+
};
|
|
2036
|
+
},
|
|
2037
|
+
offCommand() {
|
|
2038
|
+
record.commandListener = null;
|
|
2039
|
+
},
|
|
2040
|
+
sendData(data) {
|
|
2041
|
+
record.dataListeners.forEach(listener => listener(data));
|
|
2042
|
+
},
|
|
2043
|
+
startBeat() {},
|
|
2044
|
+
updateStatusInfo(status, info) {
|
|
2045
|
+
record.heartbeat = {status, info};
|
|
2046
|
+
record.dataListeners.forEach(listener => listener({event: 'status', data: info}));
|
|
2047
|
+
}
|
|
2048
|
+
};
|
|
2049
|
+
}
|
|
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
|
+
|
|
1909
2416
|
function installCutosMock$1(options = {}) {
|
|
1910
2417
|
mockState.options = options;
|
|
1911
2418
|
mockState.connected = false;
|
|
1912
2419
|
mockState.capabilities.clear();
|
|
1913
|
-
mockState.
|
|
2420
|
+
mockState.databases.clear();
|
|
2421
|
+
mockState.ipcListeners.clear();
|
|
2422
|
+
mockState.notificationListeners.clear();
|
|
2423
|
+
mockState.logs = [];
|
|
2424
|
+
mockState.httpProxies.clear();
|
|
1914
2425
|
|
|
1915
2426
|
const capabilities = options.capabilities || {};
|
|
1916
2427
|
Object.keys(capabilities).forEach(name => registerCapability(name, capabilities[name]));
|
|
2428
|
+
configureMockTransports(createMockDevice, createMockDriver);
|
|
2429
|
+
installDatabaseMock();
|
|
1917
2430
|
|
|
1918
2431
|
CoreAPI$1.init = function (host = 'localhost', callback) {
|
|
1919
|
-
if (typeof host === 'function')
|
|
1920
|
-
callback = host;
|
|
1921
|
-
host = 'localhost';
|
|
1922
|
-
}
|
|
2432
|
+
if (typeof host === 'function') callback = host;
|
|
1923
2433
|
mockState.connected = true;
|
|
1924
|
-
return resolveCallback(callback, options.initResult
|
|
2434
|
+
return resolveCallback(callback, options.initResult ?? 'CoreAPI mock init success');
|
|
1925
2435
|
};
|
|
1926
2436
|
CoreAPI$1.connected = () => mockState.connected;
|
|
1927
|
-
CoreAPI$1.getPlatform = callback => resolveCallback(callback, options.platform
|
|
1928
|
-
CoreAPI$1.getConfig = callback => resolveCallback(callback, options.config
|
|
1929
|
-
CoreAPI$1.getBoxInfo = callback => resolveCallback(callback, options.boxInfo
|
|
1930
|
-
CoreAPI$1.getDeviceInfo = callback => resolveCallback(callback, options.deviceInfo
|
|
2437
|
+
CoreAPI$1.getPlatform = callback => resolveCallback(callback, options.platform ?? {name: 'CUTOS Mock'});
|
|
2438
|
+
CoreAPI$1.getConfig = callback => resolveCallback(callback, options.config ?? {});
|
|
2439
|
+
CoreAPI$1.getBoxInfo = callback => resolveCallback(callback, options.boxInfo ?? {name: 'CUTOS Mock Node'});
|
|
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();
|
|
1931
2444
|
CoreAPI$1.shell = (command, callback) => resolveCallback(callback, {command, stdout: '', stderr: '', code: 0});
|
|
1932
2445
|
CoreAPI$1.getVolume = callback => resolveCallback(callback, options.volume ?? 50);
|
|
1933
2446
|
CoreAPI$1.setVolume = (vol, callback) => {
|
|
1934
2447
|
mockState.options.volume = vol;
|
|
1935
2448
|
return resolveCallback(callback, vol);
|
|
1936
2449
|
};
|
|
1937
|
-
CoreAPI$1.setHttpProxy = (api, target, callback) =>
|
|
2450
|
+
CoreAPI$1.setHttpProxy = (api, target, callback) => {
|
|
2451
|
+
const proxy = {api, target};
|
|
2452
|
+
mockState.httpProxies.set(api, proxy);
|
|
2453
|
+
return resolveCallback(callback, proxy);
|
|
2454
|
+
};
|
|
1938
2455
|
|
|
1939
2456
|
mockState.installed = true;
|
|
1940
2457
|
return CutosMock$1;
|
|
1941
2458
|
}
|
|
1942
2459
|
|
|
1943
2460
|
function uninstallCutosMock$1() {
|
|
2461
|
+
configureMockTransports();
|
|
2462
|
+
uninstallDatabaseMock();
|
|
1944
2463
|
CoreAPI$1.init = original.init;
|
|
1945
2464
|
CoreAPI$1.connected = original.connected;
|
|
1946
2465
|
CoreAPI$1.getPlatform = original.getPlatform;
|
|
1947
2466
|
CoreAPI$1.getConfig = original.getConfig;
|
|
1948
2467
|
CoreAPI$1.getBoxInfo = original.getBoxInfo;
|
|
1949
2468
|
CoreAPI$1.getDeviceInfo = original.getDeviceInfo;
|
|
2469
|
+
CoreAPI$1.getIPC = original.getIPC;
|
|
2470
|
+
CoreAPI$1.getNotification = original.getNotification;
|
|
2471
|
+
CoreAPI$1.getLogger = original.getLogger;
|
|
1950
2472
|
CoreAPI$1.shell = original.shell;
|
|
1951
2473
|
CoreAPI$1.getVolume = original.getVolume;
|
|
1952
2474
|
CoreAPI$1.setVolume = original.setVolume;
|
|
@@ -1955,13 +2477,19 @@ function uninstallCutosMock$1() {
|
|
|
1955
2477
|
mockState.connected = false;
|
|
1956
2478
|
mockState.installed = false;
|
|
1957
2479
|
mockState.capabilities.clear();
|
|
1958
|
-
mockState.
|
|
2480
|
+
mockState.databases.clear();
|
|
2481
|
+
mockState.ipcListeners.clear();
|
|
2482
|
+
mockState.notificationListeners.clear();
|
|
2483
|
+
mockState.logs = [];
|
|
2484
|
+
mockState.httpProxies.clear();
|
|
1959
2485
|
}
|
|
1960
2486
|
|
|
1961
2487
|
const CutosMock$1 = {
|
|
1962
2488
|
install: installCutosMock$1,
|
|
1963
2489
|
uninstall: uninstallCutosMock$1,
|
|
1964
2490
|
registerCapability,
|
|
2491
|
+
getLogs: () => mockState.logs.map(clone),
|
|
2492
|
+
getHttpProxies: () => [...mockState.httpProxies.values()].map(clone),
|
|
1965
2493
|
isInstalled: () => mockState.installed
|
|
1966
2494
|
};
|
|
1967
2495
|
|