@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.esm.js
CHANGED
|
@@ -685,19 +685,19 @@ const HEARTBEAT_STATUS = Object.freeze({
|
|
|
685
685
|
});
|
|
686
686
|
|
|
687
687
|
const cutosAPI$2 = {
|
|
688
|
-
version: '
|
|
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.
|
|
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"
|
|
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",
|
|
@@ -762,6 +762,13 @@ let callbackTimer = null;
|
|
|
762
762
|
let coreApiInitialized = false;
|
|
763
763
|
let _logger = null;
|
|
764
764
|
let _notification = null;
|
|
765
|
+
let mockDeviceFactory = null;
|
|
766
|
+
let mockDriverFactory = null;
|
|
767
|
+
|
|
768
|
+
function configureMockTransports$1(deviceFactory = null, driverFactory = null) {
|
|
769
|
+
mockDeviceFactory = deviceFactory;
|
|
770
|
+
mockDriverFactory = driverFactory;
|
|
771
|
+
}
|
|
765
772
|
|
|
766
773
|
let CutosError$2 = class CutosError extends Error {
|
|
767
774
|
constructor(code = 'CUTOS_ERROR', message = 'CUTOS error', detail = undefined) {
|
|
@@ -908,7 +915,7 @@ function doCallback(id, body, err) {
|
|
|
908
915
|
* @Database
|
|
909
916
|
* The basic table structure of CUTOS database is in Key-Value format, and Value can be any json object.
|
|
910
917
|
*/
|
|
911
|
-
class Database {
|
|
918
|
+
let Database$1 = class Database {
|
|
912
919
|
KEY_TYPE_TEXT = "TEXT";
|
|
913
920
|
KEY_TYPE_INTEGER = "INTEGER";
|
|
914
921
|
|
|
@@ -946,9 +953,9 @@ class Database {
|
|
|
946
953
|
setupMirror(gwIPC, lwaName, confirm = false) {
|
|
947
954
|
this.mirror = {gwIPC, lwaName, confirm};
|
|
948
955
|
}
|
|
949
|
-
}
|
|
956
|
+
};
|
|
950
957
|
|
|
951
|
-
Database.Table = class {
|
|
958
|
+
Database$1.Table = class {
|
|
952
959
|
|
|
953
960
|
/**
|
|
954
961
|
* @constructor The constructor of Table
|
|
@@ -1463,6 +1470,9 @@ class Device {
|
|
|
1463
1470
|
* @param type
|
|
1464
1471
|
*/
|
|
1465
1472
|
constructor(name = 'default-device-name', type = cutosAPI$1.DEVICE.DEFAULT) {
|
|
1473
|
+
if (mockDeviceFactory) {
|
|
1474
|
+
return mockDeviceFactory(name, type);
|
|
1475
|
+
}
|
|
1466
1476
|
this.name = name;
|
|
1467
1477
|
this.type = type;
|
|
1468
1478
|
this.topicData = cutosAPI$1.DEVICE.CHANNEL + '-' + type + '-data';
|
|
@@ -1536,6 +1546,9 @@ class Driver {
|
|
|
1536
1546
|
* @param counter
|
|
1537
1547
|
*/
|
|
1538
1548
|
constructor(name = 'default-driver-name', type = cutosAPI$1.DEVICE.DEFAULT, counter = 10) {
|
|
1549
|
+
if (mockDriverFactory) {
|
|
1550
|
+
return mockDriverFactory(name, type, counter);
|
|
1551
|
+
}
|
|
1539
1552
|
this.name = name;
|
|
1540
1553
|
this.type = type;
|
|
1541
1554
|
this.heartbeat = new Heartbeat(type, counter);
|
|
@@ -1646,11 +1659,15 @@ class Capability {
|
|
|
1646
1659
|
this.eventListeners.get(event).add(listener);
|
|
1647
1660
|
|
|
1648
1661
|
if (!this.dataListenerRegistered) {
|
|
1649
|
-
if (
|
|
1650
|
-
|
|
1662
|
+
if (typeof this.device.onData === "function") {
|
|
1663
|
+
this.unsubscribeData = this.device.onData(message => this._dispatchEvent(message));
|
|
1664
|
+
} else {
|
|
1665
|
+
if (!listenerMap.has(this.device.topicData)) {
|
|
1666
|
+
client.subscribe(this.device.topicData);
|
|
1667
|
+
}
|
|
1668
|
+
listenerMap.set(this.device.topicData, message => this._dispatchEvent(message));
|
|
1669
|
+
this.unsubscribeData = () => listenerMap.delete(this.device.topicData);
|
|
1651
1670
|
}
|
|
1652
|
-
listenerMap.set(this.device.topicData, message => this._dispatchEvent(message));
|
|
1653
|
-
this.unsubscribeData = () => listenerMap.delete(this.device.topicData);
|
|
1654
1671
|
this.dataListenerRegistered = true;
|
|
1655
1672
|
}
|
|
1656
1673
|
|
|
@@ -2094,16 +2111,17 @@ cutos.CoreDefine = {
|
|
|
2094
2111
|
DEVICE: cutosAPI$1.DEVICE, HEARTBEAT_STATUS: cutosAPI$1.HEARTBEAT_STATUS
|
|
2095
2112
|
};
|
|
2096
2113
|
cutos.CoreClass = {
|
|
2097
|
-
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
|
|
2098
2115
|
};
|
|
2099
2116
|
|
|
2100
2117
|
cutos.cutosAPI = cutosAPI$1;
|
|
2101
2118
|
cutos.CutosError = CutosError$2;
|
|
2102
2119
|
cutos.DeviceCapabilityBase = DeviceCapabilityBase$1;
|
|
2103
2120
|
cutos.DeviceProviderBase = DeviceProviderBase$1;
|
|
2121
|
+
cutos.configureMockTransports = configureMockTransports$1;
|
|
2104
2122
|
|
|
2105
2123
|
// simulate cutos core for development
|
|
2106
|
-
const {CoreAPI: CoreAPI$2, CoreClass: CoreClass$
|
|
2124
|
+
const {CoreAPI: CoreAPI$2, CoreClass: CoreClass$2} = cutos;
|
|
2107
2125
|
|
|
2108
2126
|
const CoreSimulator$1 = {
|
|
2109
2127
|
start: function (driver, port = 1883) {
|
|
@@ -2115,7 +2133,7 @@ const CoreSimulator$1 = {
|
|
|
2115
2133
|
|
|
2116
2134
|
console.log('CUTOS Simulator started and listening on port ', port);
|
|
2117
2135
|
|
|
2118
|
-
let drvDefault = new CoreClass$
|
|
2136
|
+
let drvDefault = new CoreClass$2.Driver();
|
|
2119
2137
|
drvDefault.onCommand((command) => {
|
|
2120
2138
|
console.log('drvDefault onCommand', JSON.stringify(command));
|
|
2121
2139
|
if (command.cmd === "init") {
|
|
@@ -2135,7 +2153,10 @@ const CoreSimulator$1 = {
|
|
|
2135
2153
|
|
|
2136
2154
|
var coreSimulator = {CoreSimulator: CoreSimulator$1};
|
|
2137
2155
|
|
|
2138
|
-
const {CoreAPI: CoreAPI$1, CutosError: CutosError$1} = 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;
|
|
2139
2160
|
|
|
2140
2161
|
const original = {
|
|
2141
2162
|
init: CoreAPI$1.init,
|
|
@@ -2144,10 +2165,30 @@ const original = {
|
|
|
2144
2165
|
getConfig: CoreAPI$1.getConfig,
|
|
2145
2166
|
getBoxInfo: CoreAPI$1.getBoxInfo,
|
|
2146
2167
|
getDeviceInfo: CoreAPI$1.getDeviceInfo,
|
|
2168
|
+
getIPC: CoreAPI$1.getIPC,
|
|
2169
|
+
getNotification: CoreAPI$1.getNotification,
|
|
2170
|
+
getLogger: CoreAPI$1.getLogger,
|
|
2147
2171
|
shell: CoreAPI$1.shell,
|
|
2148
2172
|
getVolume: CoreAPI$1.getVolume,
|
|
2149
2173
|
setVolume: CoreAPI$1.setVolume,
|
|
2150
|
-
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
|
+
}
|
|
2151
2192
|
};
|
|
2152
2193
|
|
|
2153
2194
|
const mockState = {
|
|
@@ -2155,7 +2196,11 @@ const mockState = {
|
|
|
2155
2196
|
connected: false,
|
|
2156
2197
|
options: {},
|
|
2157
2198
|
capabilities: new Map(),
|
|
2158
|
-
|
|
2199
|
+
databases: new Map(),
|
|
2200
|
+
ipcListeners: new Map(),
|
|
2201
|
+
notificationListeners: new Set(),
|
|
2202
|
+
logs: [],
|
|
2203
|
+
httpProxies: new Map()
|
|
2159
2204
|
};
|
|
2160
2205
|
|
|
2161
2206
|
function resolveCallback(callback, value, error) {
|
|
@@ -2163,25 +2208,37 @@ function resolveCallback(callback, value, error) {
|
|
|
2163
2208
|
setTimeout(() => callback(error ? null : value, error), 0);
|
|
2164
2209
|
return true;
|
|
2165
2210
|
}
|
|
2211
|
+
return error ? Promise.reject(error) : Promise.resolve(value);
|
|
2212
|
+
}
|
|
2166
2213
|
|
|
2167
|
-
|
|
2168
|
-
|
|
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;
|
|
2169
2221
|
}
|
|
2170
|
-
return Promise.resolve(value);
|
|
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));
|
|
2171
2229
|
}
|
|
2172
2230
|
|
|
2173
2231
|
function normalizeMethod(method) {
|
|
2174
|
-
|
|
2175
|
-
return method;
|
|
2176
|
-
}
|
|
2177
|
-
return () => method;
|
|
2232
|
+
return typeof method === 'function' ? method : () => method;
|
|
2178
2233
|
}
|
|
2179
2234
|
|
|
2180
2235
|
function ensureCapabilityRecord(name) {
|
|
2181
2236
|
if (!mockState.capabilities.has(name)) {
|
|
2182
2237
|
mockState.capabilities.set(name, {
|
|
2183
2238
|
methods: new Map(),
|
|
2184
|
-
|
|
2239
|
+
dataListeners: new Set(),
|
|
2240
|
+
commandListener: null,
|
|
2241
|
+
heartbeat: null
|
|
2185
2242
|
});
|
|
2186
2243
|
}
|
|
2187
2244
|
return mockState.capabilities.get(name);
|
|
@@ -2190,55 +2247,520 @@ function ensureCapabilityRecord(name) {
|
|
|
2190
2247
|
function registerCapability(name, definition = {}) {
|
|
2191
2248
|
const record = ensureCapabilityRecord(name);
|
|
2192
2249
|
const methods = definition.methods || {};
|
|
2193
|
-
|
|
2194
2250
|
Object.keys(methods).forEach(methodName => {
|
|
2195
2251
|
record.methods.set(methodName, normalizeMethod(methods[methodName]));
|
|
2196
2252
|
});
|
|
2197
|
-
|
|
2198
2253
|
return record;
|
|
2199
2254
|
}
|
|
2200
2255
|
|
|
2256
|
+
function invokeMockMethod(record, command, callback) {
|
|
2257
|
+
if (record.commandListener) {
|
|
2258
|
+
record.commandListener(command, callback);
|
|
2259
|
+
return true;
|
|
2260
|
+
}
|
|
2261
|
+
|
|
2262
|
+
const handler = record.methods.get(command.cmd);
|
|
2263
|
+
if (!handler) {
|
|
2264
|
+
callback({
|
|
2265
|
+
status: false,
|
|
2266
|
+
msg: new CutosError$1('METHOD_NOT_FOUND', `Mock capability method ${command.cmd} is not registered.`)
|
|
2267
|
+
});
|
|
2268
|
+
return true;
|
|
2269
|
+
}
|
|
2270
|
+
|
|
2271
|
+
Promise.resolve()
|
|
2272
|
+
.then(() => handler(command.args, {command}))
|
|
2273
|
+
.then(result => callback({status: true, msg: result}))
|
|
2274
|
+
.catch(error => callback({status: false, msg: error}));
|
|
2275
|
+
return true;
|
|
2276
|
+
}
|
|
2277
|
+
|
|
2278
|
+
function invokeMockTransport(record, command, callback) {
|
|
2279
|
+
if (typeof callback === 'function') {
|
|
2280
|
+
return invokeMockMethod(record, command, callback);
|
|
2281
|
+
}
|
|
2282
|
+
return new Promise((resolve, reject) => {
|
|
2283
|
+
invokeMockMethod(record, command, result => {
|
|
2284
|
+
if (result && result.status === false) {
|
|
2285
|
+
reject(result.msg);
|
|
2286
|
+
} else {
|
|
2287
|
+
resolve(result && result.status === true ? result.msg : result);
|
|
2288
|
+
}
|
|
2289
|
+
});
|
|
2290
|
+
});
|
|
2291
|
+
}
|
|
2292
|
+
|
|
2293
|
+
function createMockDevice(name, type) {
|
|
2294
|
+
const record = ensureCapabilityRecord(type);
|
|
2295
|
+
return {
|
|
2296
|
+
name,
|
|
2297
|
+
type,
|
|
2298
|
+
topicData: `mock-${type}-data`,
|
|
2299
|
+
init(opts, callback) {
|
|
2300
|
+
if (typeof opts === 'function') callback = opts;
|
|
2301
|
+
return resolveCallback(callback, mockState.options.initResult ?? `Mock device ${type} initialized`);
|
|
2302
|
+
},
|
|
2303
|
+
sendCommand(command, callback) {
|
|
2304
|
+
return invokeMockTransport(record, command, callback);
|
|
2305
|
+
},
|
|
2306
|
+
sendCommandWithOptions(command, opts, callback) {
|
|
2307
|
+
if (typeof opts === 'function') callback = opts;
|
|
2308
|
+
return invokeMockTransport(record, command, callback);
|
|
2309
|
+
},
|
|
2310
|
+
onData(listener) {
|
|
2311
|
+
record.dataListeners.add(listener);
|
|
2312
|
+
return () => record.dataListeners.delete(listener);
|
|
2313
|
+
}
|
|
2314
|
+
};
|
|
2315
|
+
}
|
|
2316
|
+
|
|
2317
|
+
function createMockDriver(name, type) {
|
|
2318
|
+
const record = ensureCapabilityRecord(type);
|
|
2319
|
+
return {
|
|
2320
|
+
name,
|
|
2321
|
+
type,
|
|
2322
|
+
beatInterval: 3000,
|
|
2323
|
+
onCommand(listener) {
|
|
2324
|
+
record.commandListener = listener;
|
|
2325
|
+
return () => {
|
|
2326
|
+
if (record.commandListener === listener) record.commandListener = null;
|
|
2327
|
+
};
|
|
2328
|
+
},
|
|
2329
|
+
offCommand() {
|
|
2330
|
+
record.commandListener = null;
|
|
2331
|
+
},
|
|
2332
|
+
sendData(data) {
|
|
2333
|
+
record.dataListeners.forEach(listener => listener(data));
|
|
2334
|
+
},
|
|
2335
|
+
startBeat() {},
|
|
2336
|
+
updateStatusInfo(status, info) {
|
|
2337
|
+
record.heartbeat = {status, info};
|
|
2338
|
+
record.dataListeners.forEach(listener => listener({event: 'status', data: info}));
|
|
2339
|
+
}
|
|
2340
|
+
};
|
|
2341
|
+
}
|
|
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
|
+
|
|
2201
2708
|
function installCutosMock$1(options = {}) {
|
|
2202
2709
|
mockState.options = options;
|
|
2203
2710
|
mockState.connected = false;
|
|
2204
2711
|
mockState.capabilities.clear();
|
|
2205
|
-
mockState.
|
|
2712
|
+
mockState.databases.clear();
|
|
2713
|
+
mockState.ipcListeners.clear();
|
|
2714
|
+
mockState.notificationListeners.clear();
|
|
2715
|
+
mockState.logs = [];
|
|
2716
|
+
mockState.httpProxies.clear();
|
|
2206
2717
|
|
|
2207
2718
|
const capabilities = options.capabilities || {};
|
|
2208
2719
|
Object.keys(capabilities).forEach(name => registerCapability(name, capabilities[name]));
|
|
2720
|
+
configureMockTransports(createMockDevice, createMockDriver);
|
|
2721
|
+
installDatabaseMock();
|
|
2209
2722
|
|
|
2210
2723
|
CoreAPI$1.init = function (host = 'localhost', callback) {
|
|
2211
|
-
if (typeof host === 'function')
|
|
2212
|
-
callback = host;
|
|
2213
|
-
host = 'localhost';
|
|
2214
|
-
}
|
|
2724
|
+
if (typeof host === 'function') callback = host;
|
|
2215
2725
|
mockState.connected = true;
|
|
2216
|
-
return resolveCallback(callback, options.initResult
|
|
2726
|
+
return resolveCallback(callback, options.initResult ?? 'CoreAPI mock init success');
|
|
2217
2727
|
};
|
|
2218
2728
|
CoreAPI$1.connected = () => mockState.connected;
|
|
2219
|
-
CoreAPI$1.getPlatform = callback => resolveCallback(callback, options.platform
|
|
2220
|
-
CoreAPI$1.getConfig = callback => resolveCallback(callback, options.config
|
|
2221
|
-
CoreAPI$1.getBoxInfo = callback => resolveCallback(callback, options.boxInfo
|
|
2222
|
-
CoreAPI$1.getDeviceInfo = callback => resolveCallback(callback, options.deviceInfo
|
|
2729
|
+
CoreAPI$1.getPlatform = callback => resolveCallback(callback, options.platform ?? {name: 'CUTOS Mock'});
|
|
2730
|
+
CoreAPI$1.getConfig = callback => resolveCallback(callback, options.config ?? {});
|
|
2731
|
+
CoreAPI$1.getBoxInfo = callback => resolveCallback(callback, options.boxInfo ?? {name: 'CUTOS Mock Node'});
|
|
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();
|
|
2223
2736
|
CoreAPI$1.shell = (command, callback) => resolveCallback(callback, {command, stdout: '', stderr: '', code: 0});
|
|
2224
2737
|
CoreAPI$1.getVolume = callback => resolveCallback(callback, options.volume ?? 50);
|
|
2225
2738
|
CoreAPI$1.setVolume = (vol, callback) => {
|
|
2226
2739
|
mockState.options.volume = vol;
|
|
2227
2740
|
return resolveCallback(callback, vol);
|
|
2228
2741
|
};
|
|
2229
|
-
CoreAPI$1.setHttpProxy = (api, target, callback) =>
|
|
2742
|
+
CoreAPI$1.setHttpProxy = (api, target, callback) => {
|
|
2743
|
+
const proxy = {api, target};
|
|
2744
|
+
mockState.httpProxies.set(api, proxy);
|
|
2745
|
+
return resolveCallback(callback, proxy);
|
|
2746
|
+
};
|
|
2230
2747
|
|
|
2231
2748
|
mockState.installed = true;
|
|
2232
2749
|
return CutosMock$1;
|
|
2233
2750
|
}
|
|
2234
2751
|
|
|
2235
2752
|
function uninstallCutosMock$1() {
|
|
2753
|
+
configureMockTransports();
|
|
2754
|
+
uninstallDatabaseMock();
|
|
2236
2755
|
CoreAPI$1.init = original.init;
|
|
2237
2756
|
CoreAPI$1.connected = original.connected;
|
|
2238
2757
|
CoreAPI$1.getPlatform = original.getPlatform;
|
|
2239
2758
|
CoreAPI$1.getConfig = original.getConfig;
|
|
2240
2759
|
CoreAPI$1.getBoxInfo = original.getBoxInfo;
|
|
2241
2760
|
CoreAPI$1.getDeviceInfo = original.getDeviceInfo;
|
|
2761
|
+
CoreAPI$1.getIPC = original.getIPC;
|
|
2762
|
+
CoreAPI$1.getNotification = original.getNotification;
|
|
2763
|
+
CoreAPI$1.getLogger = original.getLogger;
|
|
2242
2764
|
CoreAPI$1.shell = original.shell;
|
|
2243
2765
|
CoreAPI$1.getVolume = original.getVolume;
|
|
2244
2766
|
CoreAPI$1.setVolume = original.setVolume;
|
|
@@ -2247,13 +2769,19 @@ function uninstallCutosMock$1() {
|
|
|
2247
2769
|
mockState.connected = false;
|
|
2248
2770
|
mockState.installed = false;
|
|
2249
2771
|
mockState.capabilities.clear();
|
|
2250
|
-
mockState.
|
|
2772
|
+
mockState.databases.clear();
|
|
2773
|
+
mockState.ipcListeners.clear();
|
|
2774
|
+
mockState.notificationListeners.clear();
|
|
2775
|
+
mockState.logs = [];
|
|
2776
|
+
mockState.httpProxies.clear();
|
|
2251
2777
|
}
|
|
2252
2778
|
|
|
2253
2779
|
const CutosMock$1 = {
|
|
2254
2780
|
install: installCutosMock$1,
|
|
2255
2781
|
uninstall: uninstallCutosMock$1,
|
|
2256
2782
|
registerCapability,
|
|
2783
|
+
getLogs: () => mockState.logs.map(clone),
|
|
2784
|
+
getHttpProxies: () => [...mockState.httpProxies.values()].map(clone),
|
|
2257
2785
|
isInstalled: () => mockState.installed
|
|
2258
2786
|
};
|
|
2259
2787
|
|