@cutos/core 4.0.0 → 4.0.1
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 +28 -2
- package/dist/cutos.cjs.js +311 -29
- package/dist/cutos.esm.js +309 -30
- package/package.json +1 -1
- package/types/index.d.ts +28 -0
package/README.md
CHANGED
|
@@ -60,7 +60,8 @@ Hardware Device/Driver APIs remain available as the compatibility layer, but new
|
|
|
60
60
|
8. [Logger API](#logger-api)
|
|
61
61
|
9. [Error Model](#error-model)
|
|
62
62
|
10. [Capability Contract](#capability-contract)
|
|
63
|
-
11. [
|
|
63
|
+
11. [Mock Dev](#mock-dev)
|
|
64
|
+
12. [Documentation](#documentation)
|
|
64
65
|
|
|
65
66
|
# Quick Start
|
|
66
67
|
|
|
@@ -129,6 +130,31 @@ provider.emit("statusChanged", {
|
|
|
129
130
|
})
|
|
130
131
|
```
|
|
131
132
|
|
|
133
|
+
## Mock Dev
|
|
134
|
+
|
|
135
|
+
Use Mock Dev when developing an LWA without a local CUTOS Node.
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
import { CoreAPI, cutos, installCutosMock } from "@cutos/core"
|
|
139
|
+
|
|
140
|
+
installCutosMock({
|
|
141
|
+
capabilities: {
|
|
142
|
+
"hello-device": {
|
|
143
|
+
methods: {
|
|
144
|
+
echo: params => params
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
await CoreAPI.init()
|
|
151
|
+
|
|
152
|
+
const hello = cutos.capability("hello-device")
|
|
153
|
+
const result = await hello.call("echo", { message: "hello" })
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Mock Dev is intended for UI and SDK integration development. Real IPC, WebView host behavior, and Node Provider behavior should still be verified with a CUTOS Node before release.
|
|
157
|
+
|
|
132
158
|
# Platform API
|
|
133
159
|
|
|
134
160
|
## CoreAPI.init
|
|
@@ -630,4 +656,4 @@ Capability contract spec:
|
|
|
630
656
|
|
|
631
657
|
```text
|
|
632
658
|
docs/CUTOS-Capability-Contract-v0.md
|
|
633
|
-
```
|
|
659
|
+
```
|
package/dist/cutos.cjs.js
CHANGED
|
@@ -32,7 +32,7 @@ function getAugmentedNamespace(n) {
|
|
|
32
32
|
|
|
33
33
|
var src = {};
|
|
34
34
|
|
|
35
|
-
var cutos$
|
|
35
|
+
var cutos$3 = {};
|
|
36
36
|
|
|
37
37
|
const rnds8Pool = new Uint8Array(256); // # of random values to pre-allocate
|
|
38
38
|
|
|
@@ -398,7 +398,7 @@ const cutosAPI$2 = {
|
|
|
398
398
|
var cutosApi = cutosAPI$2;
|
|
399
399
|
|
|
400
400
|
var name = "@cutos/core";
|
|
401
|
-
var version = "4.0.
|
|
401
|
+
var version = "4.0.1";
|
|
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";
|
|
@@ -470,7 +470,7 @@ let coreApiInitialized = false;
|
|
|
470
470
|
let _logger = null;
|
|
471
471
|
let _notification = null;
|
|
472
472
|
|
|
473
|
-
let CutosError$
|
|
473
|
+
let CutosError$2 = class CutosError extends Error {
|
|
474
474
|
constructor(code = 'CUTOS_ERROR', message = 'CUTOS error', detail = undefined) {
|
|
475
475
|
super(message);
|
|
476
476
|
this.name = code;
|
|
@@ -481,7 +481,7 @@ let CutosError$1 = class CutosError extends Error {
|
|
|
481
481
|
};
|
|
482
482
|
|
|
483
483
|
function normalizeError(error, fallbackCode = 'CUTOS_ERROR') {
|
|
484
|
-
if (error instanceof CutosError$
|
|
484
|
+
if (error instanceof CutosError$2) {
|
|
485
485
|
return error;
|
|
486
486
|
}
|
|
487
487
|
if (error && error.error) {
|
|
@@ -491,9 +491,9 @@ function normalizeError(error, fallbackCode = 'CUTOS_ERROR') {
|
|
|
491
491
|
const code = error.code || error.name || fallbackCode;
|
|
492
492
|
const message = error.message || error.msg || JSON.stringify(error);
|
|
493
493
|
const detail = error.detail !== undefined ? error.detail : error;
|
|
494
|
-
return new CutosError$
|
|
494
|
+
return new CutosError$2(code, message, detail);
|
|
495
495
|
}
|
|
496
|
-
return new CutosError$
|
|
496
|
+
return new CutosError$2(fallbackCode, error === undefined || error === null ? 'CUTOS error' : String(error));
|
|
497
497
|
}
|
|
498
498
|
|
|
499
499
|
function createResult(data) {
|
|
@@ -573,7 +573,7 @@ function saveCallback(id, item) {
|
|
|
573
573
|
callbackMap.forEach((callbackItem, id) => {
|
|
574
574
|
if (currentTimestamp - callbackItem.timestamp > callbackItem.timeout) {
|
|
575
575
|
callbackMap.delete(id);
|
|
576
|
-
callbackItem.callback(null, new CutosError$
|
|
576
|
+
callbackItem.callback(null, new CutosError$2('CUTOS_TIMEOUT', 'Response is timeout!'));
|
|
577
577
|
}
|
|
578
578
|
});
|
|
579
579
|
}, callbackInterval);
|
|
@@ -585,7 +585,7 @@ function doCallback(id, body, err) {
|
|
|
585
585
|
}
|
|
586
586
|
try {
|
|
587
587
|
if (err) {
|
|
588
|
-
callbackMap.get(id).callback(null, new CutosError$
|
|
588
|
+
callbackMap.get(id).callback(null, new CutosError$2('CUTOS_CORE_ERROR', err));
|
|
589
589
|
} else {
|
|
590
590
|
callbackMap.get(id).callback(body);
|
|
591
591
|
}
|
|
@@ -1177,7 +1177,7 @@ class Device {
|
|
|
1177
1177
|
client.subscribe(this.topicResponse); // receive response from device
|
|
1178
1178
|
const timer = setTimeout(() => {
|
|
1179
1179
|
listenerMap.delete(this.topicResponse);
|
|
1180
|
-
cb(null, new CutosError$
|
|
1180
|
+
cb(null, new CutosError$2('DRIVER_INIT_TIMEOUT', `Driver ${this.type} init timeout!`));
|
|
1181
1181
|
}, callbackTimeout);
|
|
1182
1182
|
listenerMap.set(this.topicResponse, (result) => {
|
|
1183
1183
|
clearTimeout(timer);
|
|
@@ -1470,7 +1470,7 @@ class Provider {
|
|
|
1470
1470
|
const handler = this.methods.get(method);
|
|
1471
1471
|
if (!handler) {
|
|
1472
1472
|
respond(createErrorResult(
|
|
1473
|
-
new CutosError$
|
|
1473
|
+
new CutosError$2('METHOD_NOT_FOUND', `Provider method ${method} is not registered.`)
|
|
1474
1474
|
));
|
|
1475
1475
|
return;
|
|
1476
1476
|
}
|
|
@@ -1491,7 +1491,7 @@ class Provider {
|
|
|
1491
1491
|
const capabilityMap = new Map();
|
|
1492
1492
|
const providerMap = new Map();
|
|
1493
1493
|
|
|
1494
|
-
const cutos$
|
|
1494
|
+
const cutos$2 = {
|
|
1495
1495
|
capability(name, opts = {}) {
|
|
1496
1496
|
const key = opts.type || name;
|
|
1497
1497
|
if (!capabilityMap.has(key)) {
|
|
@@ -1517,7 +1517,7 @@ const cutos$1 = {
|
|
|
1517
1517
|
}
|
|
1518
1518
|
};
|
|
1519
1519
|
|
|
1520
|
-
const CoreAPI$
|
|
1520
|
+
const CoreAPI$3 = {
|
|
1521
1521
|
/**
|
|
1522
1522
|
* Initialization of CUTOS core
|
|
1523
1523
|
* @param {string} host CUTOS address.When host is equal to null, it takes the value 'localhost'. During development, it can be changed to the target address (the device address where CUTOS is installed), such as: '192.168.1.11'
|
|
@@ -1528,7 +1528,7 @@ const CoreAPI$2 = {
|
|
|
1528
1528
|
callback = host;
|
|
1529
1529
|
host = 'localhost';
|
|
1530
1530
|
}
|
|
1531
|
-
return maybePromise(callback, cb => CoreAPI$
|
|
1531
|
+
return maybePromise(callback, cb => CoreAPI$3._init(host, cb));
|
|
1532
1532
|
},
|
|
1533
1533
|
|
|
1534
1534
|
_init(host = 'localhost', callback) {
|
|
@@ -1597,11 +1597,11 @@ const CoreAPI$2 = {
|
|
|
1597
1597
|
});
|
|
1598
1598
|
|
|
1599
1599
|
client.on("error", (err) => {
|
|
1600
|
-
callback && callback(null, new CutosError$
|
|
1600
|
+
callback && callback(null, new CutosError$2('CUTOS_CORE_ERROR', err.message));
|
|
1601
1601
|
console.error("CUTOS CORE error:", err.message);
|
|
1602
1602
|
});
|
|
1603
1603
|
client.stream.on("error", (err) => {
|
|
1604
|
-
callback && callback(null, new CutosError$
|
|
1604
|
+
callback && callback(null, new CutosError$2('CUTOS_CORE_ERROR', err.message));
|
|
1605
1605
|
console.error("CUTOS CORE error:", err.message);
|
|
1606
1606
|
});
|
|
1607
1607
|
},
|
|
@@ -1726,40 +1726,40 @@ const CoreAPI$2 = {
|
|
|
1726
1726
|
},
|
|
1727
1727
|
|
|
1728
1728
|
capability(name, opts = {}) {
|
|
1729
|
-
return cutos$
|
|
1729
|
+
return cutos$2.capability(name, opts);
|
|
1730
1730
|
},
|
|
1731
1731
|
|
|
1732
1732
|
provider(name, opts = {}) {
|
|
1733
|
-
return cutos$
|
|
1733
|
+
return cutos$2.provider(name, opts);
|
|
1734
1734
|
},
|
|
1735
1735
|
|
|
1736
1736
|
device(name, opts = {}) {
|
|
1737
|
-
return cutos$
|
|
1737
|
+
return cutos$2.device(name, opts);
|
|
1738
1738
|
},
|
|
1739
1739
|
|
|
1740
1740
|
driver(name, opts = {}) {
|
|
1741
|
-
return cutos$
|
|
1741
|
+
return cutos$2.driver(name, opts);
|
|
1742
1742
|
}
|
|
1743
1743
|
};
|
|
1744
1744
|
|
|
1745
|
-
cutos$
|
|
1746
|
-
cutos$
|
|
1745
|
+
cutos$3.CoreAPI = CoreAPI$3;
|
|
1746
|
+
cutos$3.CoreDefine = {
|
|
1747
1747
|
DEVICE: cutosAPI$1.DEVICE, HEARTBEAT_STATUS: cutosAPI$1.HEARTBEAT_STATUS
|
|
1748
1748
|
};
|
|
1749
|
-
cutos$
|
|
1750
|
-
Device, Driver, Database, Capability, Provider, CutosError: CutosError$
|
|
1749
|
+
cutos$3.CoreClass = {
|
|
1750
|
+
Device, Driver, Database, Capability, Provider, CutosError: CutosError$2
|
|
1751
1751
|
};
|
|
1752
1752
|
|
|
1753
|
-
cutos$
|
|
1754
|
-
cutos$
|
|
1755
|
-
cutos$
|
|
1753
|
+
cutos$3.cutosAPI = cutosAPI$1;
|
|
1754
|
+
cutos$3.cutos = cutos$2;
|
|
1755
|
+
cutos$3.CutosError = CutosError$2;
|
|
1756
1756
|
|
|
1757
1757
|
// simulate cutos core for development
|
|
1758
|
-
const {CoreAPI: CoreAPI$
|
|
1758
|
+
const {CoreAPI: CoreAPI$2, CoreClass: CoreClass$1} = cutos$3;
|
|
1759
1759
|
|
|
1760
1760
|
const CoreSimulator$1 = {
|
|
1761
1761
|
start: function (driver, port = 1883) {
|
|
1762
|
-
CoreAPI$
|
|
1762
|
+
CoreAPI$2.init(`ws://localhost:${port}`, (result, error) => {
|
|
1763
1763
|
if (error) {
|
|
1764
1764
|
console.log('CUTOS Simulator error', error.message);
|
|
1765
1765
|
return;
|
|
@@ -1787,8 +1787,284 @@ const CoreSimulator$1 = {
|
|
|
1787
1787
|
|
|
1788
1788
|
var coreSimulator = {CoreSimulator: CoreSimulator$1};
|
|
1789
1789
|
|
|
1790
|
-
const {CoreAPI
|
|
1790
|
+
const {CoreAPI: CoreAPI$1, cutos: cutos$1, CutosError: CutosError$1} = cutos$3;
|
|
1791
|
+
|
|
1792
|
+
const original = {
|
|
1793
|
+
init: CoreAPI$1.init,
|
|
1794
|
+
connected: CoreAPI$1.connected,
|
|
1795
|
+
getPlatform: CoreAPI$1.getPlatform,
|
|
1796
|
+
getConfig: CoreAPI$1.getConfig,
|
|
1797
|
+
getBoxInfo: CoreAPI$1.getBoxInfo,
|
|
1798
|
+
getSignInfo: CoreAPI$1.getSignInfo,
|
|
1799
|
+
getDeviceInfo: CoreAPI$1.getDeviceInfo,
|
|
1800
|
+
getSignGroupInfo: CoreAPI$1.getSignGroupInfo,
|
|
1801
|
+
shell: CoreAPI$1.shell,
|
|
1802
|
+
getVolume: CoreAPI$1.getVolume,
|
|
1803
|
+
setVolume: CoreAPI$1.setVolume,
|
|
1804
|
+
setHttpProxy: CoreAPI$1.setHttpProxy,
|
|
1805
|
+
capability: CoreAPI$1.capability,
|
|
1806
|
+
provider: CoreAPI$1.provider,
|
|
1807
|
+
cutosCapability: cutos$1.capability,
|
|
1808
|
+
cutosProvider: cutos$1.provider,
|
|
1809
|
+
cutosDevice: cutos$1.device,
|
|
1810
|
+
cutosDriver: cutos$1.driver
|
|
1811
|
+
};
|
|
1812
|
+
|
|
1813
|
+
const mockState = {
|
|
1814
|
+
installed: false,
|
|
1815
|
+
connected: false,
|
|
1816
|
+
options: {},
|
|
1817
|
+
capabilities: new Map(),
|
|
1818
|
+
providers: new Map()
|
|
1819
|
+
};
|
|
1820
|
+
|
|
1821
|
+
function resolveCallback(callback, value, error) {
|
|
1822
|
+
if (typeof callback === 'function') {
|
|
1823
|
+
setTimeout(() => callback(error ? null : value, error), 0);
|
|
1824
|
+
return true;
|
|
1825
|
+
}
|
|
1826
|
+
|
|
1827
|
+
if (error) {
|
|
1828
|
+
return Promise.reject(error);
|
|
1829
|
+
}
|
|
1830
|
+
return Promise.resolve(value);
|
|
1831
|
+
}
|
|
1832
|
+
|
|
1833
|
+
function normalizeMethod(method) {
|
|
1834
|
+
if (typeof method === 'function') {
|
|
1835
|
+
return method;
|
|
1836
|
+
}
|
|
1837
|
+
return () => method;
|
|
1838
|
+
}
|
|
1839
|
+
|
|
1840
|
+
function ensureCapabilityRecord(name) {
|
|
1841
|
+
if (!mockState.capabilities.has(name)) {
|
|
1842
|
+
mockState.capabilities.set(name, {
|
|
1843
|
+
methods: new Map(),
|
|
1844
|
+
listeners: new Map()
|
|
1845
|
+
});
|
|
1846
|
+
}
|
|
1847
|
+
return mockState.capabilities.get(name);
|
|
1848
|
+
}
|
|
1849
|
+
|
|
1850
|
+
function registerCapability(name, definition = {}) {
|
|
1851
|
+
const record = ensureCapabilityRecord(name);
|
|
1852
|
+
const methods = definition.methods || {};
|
|
1853
|
+
|
|
1854
|
+
Object.keys(methods).forEach(methodName => {
|
|
1855
|
+
record.methods.set(methodName, normalizeMethod(methods[methodName]));
|
|
1856
|
+
});
|
|
1857
|
+
|
|
1858
|
+
return record;
|
|
1859
|
+
}
|
|
1860
|
+
|
|
1861
|
+
class MockCapability {
|
|
1862
|
+
constructor(name) {
|
|
1863
|
+
if (!name) {
|
|
1864
|
+
throw new Error('capability name cannot be empty.');
|
|
1865
|
+
}
|
|
1866
|
+
this.name = name;
|
|
1867
|
+
this.type = name;
|
|
1868
|
+
ensureCapabilityRecord(name);
|
|
1869
|
+
}
|
|
1870
|
+
|
|
1871
|
+
call(method, params = {}, options = null, callback = null) {
|
|
1872
|
+
if (typeof options === 'function') {
|
|
1873
|
+
callback = options;
|
|
1874
|
+
options = null;
|
|
1875
|
+
}
|
|
1876
|
+
|
|
1877
|
+
const record = ensureCapabilityRecord(this.name);
|
|
1878
|
+
const handler = record.methods.get(method);
|
|
1879
|
+
|
|
1880
|
+
if (!handler) {
|
|
1881
|
+
const error = new CutosError$1('METHOD_NOT_FOUND', `Mock capability method ${this.name}.${method} is not registered.`);
|
|
1882
|
+
return resolveCallback(callback, null, error);
|
|
1883
|
+
}
|
|
1884
|
+
|
|
1885
|
+
try {
|
|
1886
|
+
const result = handler(params, {capability: this, method, options});
|
|
1887
|
+
if (result && typeof result.then === 'function') {
|
|
1888
|
+
return typeof callback === 'function'
|
|
1889
|
+
? (result.then(value => callback(value)).catch(error => callback(null, error)), true)
|
|
1890
|
+
: result;
|
|
1891
|
+
}
|
|
1892
|
+
return resolveCallback(callback, result);
|
|
1893
|
+
} catch (error) {
|
|
1894
|
+
return resolveCallback(callback, null, error);
|
|
1895
|
+
}
|
|
1896
|
+
}
|
|
1897
|
+
|
|
1898
|
+
on(event, listener) {
|
|
1899
|
+
if (typeof listener !== 'function') {
|
|
1900
|
+
throw new Error('capability event listener must be a function.');
|
|
1901
|
+
}
|
|
1902
|
+
|
|
1903
|
+
const record = ensureCapabilityRecord(this.name);
|
|
1904
|
+
if (!record.listeners.has(event)) {
|
|
1905
|
+
record.listeners.set(event, new Set());
|
|
1906
|
+
}
|
|
1907
|
+
record.listeners.get(event).add(listener);
|
|
1908
|
+
return () => this.off(event, listener);
|
|
1909
|
+
}
|
|
1910
|
+
|
|
1911
|
+
off(event, listener = null) {
|
|
1912
|
+
const record = ensureCapabilityRecord(this.name);
|
|
1913
|
+
if (!record.listeners.has(event)) {
|
|
1914
|
+
return;
|
|
1915
|
+
}
|
|
1916
|
+
if (!listener) {
|
|
1917
|
+
record.listeners.delete(event);
|
|
1918
|
+
return;
|
|
1919
|
+
}
|
|
1920
|
+
|
|
1921
|
+
const listeners = record.listeners.get(event);
|
|
1922
|
+
listeners.delete(listener);
|
|
1923
|
+
if (listeners.size === 0) {
|
|
1924
|
+
record.listeners.delete(event);
|
|
1925
|
+
}
|
|
1926
|
+
}
|
|
1927
|
+
|
|
1928
|
+
dispose() {
|
|
1929
|
+
const record = ensureCapabilityRecord(this.name);
|
|
1930
|
+
record.listeners.clear();
|
|
1931
|
+
return true;
|
|
1932
|
+
}
|
|
1933
|
+
}
|
|
1934
|
+
|
|
1935
|
+
class MockProvider {
|
|
1936
|
+
constructor(name) {
|
|
1937
|
+
if (!name) {
|
|
1938
|
+
throw new Error('provider name cannot be empty.');
|
|
1939
|
+
}
|
|
1940
|
+
this.name = name;
|
|
1941
|
+
this.type = name;
|
|
1942
|
+
ensureCapabilityRecord(name);
|
|
1943
|
+
}
|
|
1944
|
+
|
|
1945
|
+
method(method, handler) {
|
|
1946
|
+
if (typeof handler !== 'function') {
|
|
1947
|
+
throw new Error('provider method handler must be a function.');
|
|
1948
|
+
}
|
|
1949
|
+
ensureCapabilityRecord(this.name).methods.set(method, handler);
|
|
1950
|
+
return this;
|
|
1951
|
+
}
|
|
1952
|
+
|
|
1953
|
+
emit(event, data) {
|
|
1954
|
+
const record = ensureCapabilityRecord(this.name);
|
|
1955
|
+
const listeners = record.listeners.get(event);
|
|
1956
|
+
const allListeners = record.listeners.get('*');
|
|
1957
|
+
const dispatch = listener => listener(data, {event, data});
|
|
1958
|
+
|
|
1959
|
+
listeners && listeners.forEach(dispatch);
|
|
1960
|
+
allListeners && allListeners.forEach(dispatch);
|
|
1961
|
+
return true;
|
|
1962
|
+
}
|
|
1963
|
+
|
|
1964
|
+
dispose() {
|
|
1965
|
+
const record = ensureCapabilityRecord(this.name);
|
|
1966
|
+
record.methods.clear();
|
|
1967
|
+
return true;
|
|
1968
|
+
}
|
|
1969
|
+
}
|
|
1970
|
+
|
|
1971
|
+
function getMockCapability(name) {
|
|
1972
|
+
if (!mockState.capabilities.has(name)) {
|
|
1973
|
+
registerCapability(name);
|
|
1974
|
+
}
|
|
1975
|
+
return new MockCapability(name);
|
|
1976
|
+
}
|
|
1977
|
+
|
|
1978
|
+
function getMockProvider(name) {
|
|
1979
|
+
if (!mockState.providers.has(name)) {
|
|
1980
|
+
mockState.providers.set(name, new MockProvider(name));
|
|
1981
|
+
}
|
|
1982
|
+
return mockState.providers.get(name);
|
|
1983
|
+
}
|
|
1984
|
+
|
|
1985
|
+
function installCutosMock$1(options = {}) {
|
|
1986
|
+
mockState.options = options;
|
|
1987
|
+
mockState.connected = false;
|
|
1988
|
+
mockState.capabilities.clear();
|
|
1989
|
+
mockState.providers.clear();
|
|
1990
|
+
|
|
1991
|
+
const capabilities = options.capabilities || {};
|
|
1992
|
+
Object.keys(capabilities).forEach(name => registerCapability(name, capabilities[name]));
|
|
1993
|
+
|
|
1994
|
+
CoreAPI$1.init = function (host = 'localhost', callback) {
|
|
1995
|
+
if (typeof host === 'function') {
|
|
1996
|
+
callback = host;
|
|
1997
|
+
host = 'localhost';
|
|
1998
|
+
}
|
|
1999
|
+
mockState.connected = true;
|
|
2000
|
+
return resolveCallback(callback, options.initResult || 'CoreAPI mock init success');
|
|
2001
|
+
};
|
|
2002
|
+
CoreAPI$1.connected = () => mockState.connected;
|
|
2003
|
+
CoreAPI$1.getPlatform = callback => resolveCallback(callback, options.platform || {name: 'CUTOS Mock'});
|
|
2004
|
+
CoreAPI$1.getConfig = callback => resolveCallback(callback, options.config || {});
|
|
2005
|
+
CoreAPI$1.getBoxInfo = callback => resolveCallback(callback, options.boxInfo || {name: 'CUTOS Mock Node'});
|
|
2006
|
+
CoreAPI$1.getSignInfo = callback => resolveCallback(callback, options.signInfo || {});
|
|
2007
|
+
CoreAPI$1.getDeviceInfo = callback => resolveCallback(callback, options.deviceInfo || []);
|
|
2008
|
+
CoreAPI$1.getSignGroupInfo = callback => resolveCallback(callback, options.signGroupInfo || {});
|
|
2009
|
+
CoreAPI$1.shell = (command, callback) => resolveCallback(callback, {command, stdout: '', stderr: '', code: 0});
|
|
2010
|
+
CoreAPI$1.getVolume = callback => resolveCallback(callback, options.volume ?? 50);
|
|
2011
|
+
CoreAPI$1.setVolume = (vol, callback) => {
|
|
2012
|
+
mockState.options.volume = vol;
|
|
2013
|
+
return resolveCallback(callback, vol);
|
|
2014
|
+
};
|
|
2015
|
+
CoreAPI$1.setHttpProxy = (api, target, callback) => resolveCallback(callback, {api, target});
|
|
2016
|
+
CoreAPI$1.capability = (name) => getMockCapability(name);
|
|
2017
|
+
CoreAPI$1.provider = (name) => getMockProvider(name);
|
|
2018
|
+
|
|
2019
|
+
cutos$1.capability = (name) => getMockCapability(name);
|
|
2020
|
+
cutos$1.provider = (name) => getMockProvider(name);
|
|
2021
|
+
cutos$1.device = (name) => getMockCapability(name);
|
|
2022
|
+
cutos$1.driver = (name) => getMockProvider(name);
|
|
2023
|
+
|
|
2024
|
+
mockState.installed = true;
|
|
2025
|
+
return CutosMock$1;
|
|
2026
|
+
}
|
|
2027
|
+
|
|
2028
|
+
function uninstallCutosMock$1() {
|
|
2029
|
+
CoreAPI$1.init = original.init;
|
|
2030
|
+
CoreAPI$1.connected = original.connected;
|
|
2031
|
+
CoreAPI$1.getPlatform = original.getPlatform;
|
|
2032
|
+
CoreAPI$1.getConfig = original.getConfig;
|
|
2033
|
+
CoreAPI$1.getBoxInfo = original.getBoxInfo;
|
|
2034
|
+
CoreAPI$1.getSignInfo = original.getSignInfo;
|
|
2035
|
+
CoreAPI$1.getDeviceInfo = original.getDeviceInfo;
|
|
2036
|
+
CoreAPI$1.getSignGroupInfo = original.getSignGroupInfo;
|
|
2037
|
+
CoreAPI$1.shell = original.shell;
|
|
2038
|
+
CoreAPI$1.getVolume = original.getVolume;
|
|
2039
|
+
CoreAPI$1.setVolume = original.setVolume;
|
|
2040
|
+
CoreAPI$1.setHttpProxy = original.setHttpProxy;
|
|
2041
|
+
CoreAPI$1.capability = original.capability;
|
|
2042
|
+
CoreAPI$1.provider = original.provider;
|
|
2043
|
+
cutos$1.capability = original.cutosCapability;
|
|
2044
|
+
cutos$1.provider = original.cutosProvider;
|
|
2045
|
+
cutos$1.device = original.cutosDevice;
|
|
2046
|
+
cutos$1.driver = original.cutosDriver;
|
|
2047
|
+
|
|
2048
|
+
mockState.connected = false;
|
|
2049
|
+
mockState.installed = false;
|
|
2050
|
+
mockState.capabilities.clear();
|
|
2051
|
+
mockState.providers.clear();
|
|
2052
|
+
}
|
|
2053
|
+
|
|
2054
|
+
const CutosMock$1 = {
|
|
2055
|
+
install: installCutosMock$1,
|
|
2056
|
+
uninstall: uninstallCutosMock$1,
|
|
2057
|
+
registerCapability,
|
|
2058
|
+
capability: getMockCapability,
|
|
2059
|
+
provider: getMockProvider,
|
|
2060
|
+
isInstalled: () => mockState.installed
|
|
2061
|
+
};
|
|
2062
|
+
|
|
2063
|
+
var mock = {CutosMock: CutosMock$1, installCutosMock: installCutosMock$1, uninstallCutosMock: uninstallCutosMock$1};
|
|
2064
|
+
|
|
2065
|
+
const {CoreAPI, CoreDefine, CoreClass, cutosAPI, cutos, CutosError} = cutos$3;
|
|
1791
2066
|
const {CoreSimulator} = coreSimulator;
|
|
2067
|
+
const {CutosMock, installCutosMock, uninstallCutosMock} = mock;
|
|
1792
2068
|
|
|
1793
2069
|
|
|
1794
2070
|
var CoreAPI_1 = src.CoreAPI = CoreAPI;
|
|
@@ -1798,12 +2074,18 @@ var cutosAPI_1 = src.cutosAPI = cutosAPI;
|
|
|
1798
2074
|
var cutos_1 = src.cutos = cutos;
|
|
1799
2075
|
var CutosError_1 = src.CutosError = CutosError;
|
|
1800
2076
|
var CoreSimulator_1 = src.CoreSimulator = CoreSimulator;
|
|
2077
|
+
var CutosMock_1 = src.CutosMock = CutosMock;
|
|
2078
|
+
var installCutosMock_1 = src.installCutosMock = installCutosMock;
|
|
2079
|
+
var uninstallCutosMock_1 = src.uninstallCutosMock = uninstallCutosMock;
|
|
1801
2080
|
|
|
1802
2081
|
exports.CoreAPI = CoreAPI_1;
|
|
1803
2082
|
exports.CoreClass = CoreClass_1;
|
|
1804
2083
|
exports.CoreDefine = CoreDefine_1;
|
|
1805
2084
|
exports.CoreSimulator = CoreSimulator_1;
|
|
1806
2085
|
exports.CutosError = CutosError_1;
|
|
2086
|
+
exports.CutosMock = CutosMock_1;
|
|
1807
2087
|
exports.cutos = cutos_1;
|
|
1808
2088
|
exports.cutosAPI = cutosAPI_1;
|
|
1809
2089
|
exports.default = src;
|
|
2090
|
+
exports.installCutosMock = installCutosMock_1;
|
|
2091
|
+
exports.uninstallCutosMock = uninstallCutosMock_1;
|
package/dist/cutos.esm.js
CHANGED
|
@@ -27,7 +27,7 @@ function getAugmentedNamespace(n) {
|
|
|
27
27
|
|
|
28
28
|
var src = {};
|
|
29
29
|
|
|
30
|
-
var cutos$
|
|
30
|
+
var cutos$3 = {};
|
|
31
31
|
|
|
32
32
|
// Unique ID creation requires a high quality random # generator. In the browser we therefore
|
|
33
33
|
// require the crypto API and do not support built-in fallback to lower quality random number
|
|
@@ -690,7 +690,7 @@ const cutosAPI$2 = {
|
|
|
690
690
|
var cutosApi = cutosAPI$2;
|
|
691
691
|
|
|
692
692
|
var name = "@cutos/core";
|
|
693
|
-
var version = "4.0.
|
|
693
|
+
var version = "4.0.1";
|
|
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";
|
|
@@ -762,7 +762,7 @@ let coreApiInitialized = false;
|
|
|
762
762
|
let _logger = null;
|
|
763
763
|
let _notification = null;
|
|
764
764
|
|
|
765
|
-
let CutosError$
|
|
765
|
+
let CutosError$2 = class CutosError extends Error {
|
|
766
766
|
constructor(code = 'CUTOS_ERROR', message = 'CUTOS error', detail = undefined) {
|
|
767
767
|
super(message);
|
|
768
768
|
this.name = code;
|
|
@@ -773,7 +773,7 @@ let CutosError$1 = class CutosError extends Error {
|
|
|
773
773
|
};
|
|
774
774
|
|
|
775
775
|
function normalizeError(error, fallbackCode = 'CUTOS_ERROR') {
|
|
776
|
-
if (error instanceof CutosError$
|
|
776
|
+
if (error instanceof CutosError$2) {
|
|
777
777
|
return error;
|
|
778
778
|
}
|
|
779
779
|
if (error && error.error) {
|
|
@@ -783,9 +783,9 @@ function normalizeError(error, fallbackCode = 'CUTOS_ERROR') {
|
|
|
783
783
|
const code = error.code || error.name || fallbackCode;
|
|
784
784
|
const message = error.message || error.msg || JSON.stringify(error);
|
|
785
785
|
const detail = error.detail !== undefined ? error.detail : error;
|
|
786
|
-
return new CutosError$
|
|
786
|
+
return new CutosError$2(code, message, detail);
|
|
787
787
|
}
|
|
788
|
-
return new CutosError$
|
|
788
|
+
return new CutosError$2(fallbackCode, error === undefined || error === null ? 'CUTOS error' : String(error));
|
|
789
789
|
}
|
|
790
790
|
|
|
791
791
|
function createResult(data) {
|
|
@@ -865,7 +865,7 @@ function saveCallback(id, item) {
|
|
|
865
865
|
callbackMap.forEach((callbackItem, id) => {
|
|
866
866
|
if (currentTimestamp - callbackItem.timestamp > callbackItem.timeout) {
|
|
867
867
|
callbackMap.delete(id);
|
|
868
|
-
callbackItem.callback(null, new CutosError$
|
|
868
|
+
callbackItem.callback(null, new CutosError$2('CUTOS_TIMEOUT', 'Response is timeout!'));
|
|
869
869
|
}
|
|
870
870
|
});
|
|
871
871
|
}, callbackInterval);
|
|
@@ -877,7 +877,7 @@ function doCallback(id, body, err) {
|
|
|
877
877
|
}
|
|
878
878
|
try {
|
|
879
879
|
if (err) {
|
|
880
|
-
callbackMap.get(id).callback(null, new CutosError$
|
|
880
|
+
callbackMap.get(id).callback(null, new CutosError$2('CUTOS_CORE_ERROR', err));
|
|
881
881
|
} else {
|
|
882
882
|
callbackMap.get(id).callback(body);
|
|
883
883
|
}
|
|
@@ -1469,7 +1469,7 @@ class Device {
|
|
|
1469
1469
|
client.subscribe(this.topicResponse); // receive response from device
|
|
1470
1470
|
const timer = setTimeout(() => {
|
|
1471
1471
|
listenerMap.delete(this.topicResponse);
|
|
1472
|
-
cb(null, new CutosError$
|
|
1472
|
+
cb(null, new CutosError$2('DRIVER_INIT_TIMEOUT', `Driver ${this.type} init timeout!`));
|
|
1473
1473
|
}, callbackTimeout);
|
|
1474
1474
|
listenerMap.set(this.topicResponse, (result) => {
|
|
1475
1475
|
clearTimeout(timer);
|
|
@@ -1762,7 +1762,7 @@ class Provider {
|
|
|
1762
1762
|
const handler = this.methods.get(method);
|
|
1763
1763
|
if (!handler) {
|
|
1764
1764
|
respond(createErrorResult(
|
|
1765
|
-
new CutosError$
|
|
1765
|
+
new CutosError$2('METHOD_NOT_FOUND', `Provider method ${method} is not registered.`)
|
|
1766
1766
|
));
|
|
1767
1767
|
return;
|
|
1768
1768
|
}
|
|
@@ -1783,7 +1783,7 @@ class Provider {
|
|
|
1783
1783
|
const capabilityMap = new Map();
|
|
1784
1784
|
const providerMap = new Map();
|
|
1785
1785
|
|
|
1786
|
-
const cutos$
|
|
1786
|
+
const cutos$2 = {
|
|
1787
1787
|
capability(name, opts = {}) {
|
|
1788
1788
|
const key = opts.type || name;
|
|
1789
1789
|
if (!capabilityMap.has(key)) {
|
|
@@ -1809,7 +1809,7 @@ const cutos$1 = {
|
|
|
1809
1809
|
}
|
|
1810
1810
|
};
|
|
1811
1811
|
|
|
1812
|
-
const CoreAPI$
|
|
1812
|
+
const CoreAPI$3 = {
|
|
1813
1813
|
/**
|
|
1814
1814
|
* Initialization of CUTOS core
|
|
1815
1815
|
* @param {string} host CUTOS address.When host is equal to null, it takes the value 'localhost'. During development, it can be changed to the target address (the device address where CUTOS is installed), such as: '192.168.1.11'
|
|
@@ -1820,7 +1820,7 @@ const CoreAPI$2 = {
|
|
|
1820
1820
|
callback = host;
|
|
1821
1821
|
host = 'localhost';
|
|
1822
1822
|
}
|
|
1823
|
-
return maybePromise(callback, cb => CoreAPI$
|
|
1823
|
+
return maybePromise(callback, cb => CoreAPI$3._init(host, cb));
|
|
1824
1824
|
},
|
|
1825
1825
|
|
|
1826
1826
|
_init(host = 'localhost', callback) {
|
|
@@ -1889,11 +1889,11 @@ const CoreAPI$2 = {
|
|
|
1889
1889
|
});
|
|
1890
1890
|
|
|
1891
1891
|
client.on("error", (err) => {
|
|
1892
|
-
callback && callback(null, new CutosError$
|
|
1892
|
+
callback && callback(null, new CutosError$2('CUTOS_CORE_ERROR', err.message));
|
|
1893
1893
|
console.error("CUTOS CORE error:", err.message);
|
|
1894
1894
|
});
|
|
1895
1895
|
client.stream.on("error", (err) => {
|
|
1896
|
-
callback && callback(null, new CutosError$
|
|
1896
|
+
callback && callback(null, new CutosError$2('CUTOS_CORE_ERROR', err.message));
|
|
1897
1897
|
console.error("CUTOS CORE error:", err.message);
|
|
1898
1898
|
});
|
|
1899
1899
|
},
|
|
@@ -2018,40 +2018,40 @@ const CoreAPI$2 = {
|
|
|
2018
2018
|
},
|
|
2019
2019
|
|
|
2020
2020
|
capability(name, opts = {}) {
|
|
2021
|
-
return cutos$
|
|
2021
|
+
return cutos$2.capability(name, opts);
|
|
2022
2022
|
},
|
|
2023
2023
|
|
|
2024
2024
|
provider(name, opts = {}) {
|
|
2025
|
-
return cutos$
|
|
2025
|
+
return cutos$2.provider(name, opts);
|
|
2026
2026
|
},
|
|
2027
2027
|
|
|
2028
2028
|
device(name, opts = {}) {
|
|
2029
|
-
return cutos$
|
|
2029
|
+
return cutos$2.device(name, opts);
|
|
2030
2030
|
},
|
|
2031
2031
|
|
|
2032
2032
|
driver(name, opts = {}) {
|
|
2033
|
-
return cutos$
|
|
2033
|
+
return cutos$2.driver(name, opts);
|
|
2034
2034
|
}
|
|
2035
2035
|
};
|
|
2036
2036
|
|
|
2037
|
-
cutos$
|
|
2038
|
-
cutos$
|
|
2037
|
+
cutos$3.CoreAPI = CoreAPI$3;
|
|
2038
|
+
cutos$3.CoreDefine = {
|
|
2039
2039
|
DEVICE: cutosAPI$1.DEVICE, HEARTBEAT_STATUS: cutosAPI$1.HEARTBEAT_STATUS
|
|
2040
2040
|
};
|
|
2041
|
-
cutos$
|
|
2042
|
-
Device, Driver, Database, Capability, Provider, CutosError: CutosError$
|
|
2041
|
+
cutos$3.CoreClass = {
|
|
2042
|
+
Device, Driver, Database, Capability, Provider, CutosError: CutosError$2
|
|
2043
2043
|
};
|
|
2044
2044
|
|
|
2045
|
-
cutos$
|
|
2046
|
-
cutos$
|
|
2047
|
-
cutos$
|
|
2045
|
+
cutos$3.cutosAPI = cutosAPI$1;
|
|
2046
|
+
cutos$3.cutos = cutos$2;
|
|
2047
|
+
cutos$3.CutosError = CutosError$2;
|
|
2048
2048
|
|
|
2049
2049
|
// simulate cutos core for development
|
|
2050
|
-
const {CoreAPI: CoreAPI$
|
|
2050
|
+
const {CoreAPI: CoreAPI$2, CoreClass: CoreClass$1} = cutos$3;
|
|
2051
2051
|
|
|
2052
2052
|
const CoreSimulator$1 = {
|
|
2053
2053
|
start: function (driver, port = 1883) {
|
|
2054
|
-
CoreAPI$
|
|
2054
|
+
CoreAPI$2.init(`ws://localhost:${port}`, (result, error) => {
|
|
2055
2055
|
if (error) {
|
|
2056
2056
|
console.log('CUTOS Simulator error', error.message);
|
|
2057
2057
|
return;
|
|
@@ -2079,8 +2079,284 @@ const CoreSimulator$1 = {
|
|
|
2079
2079
|
|
|
2080
2080
|
var coreSimulator = {CoreSimulator: CoreSimulator$1};
|
|
2081
2081
|
|
|
2082
|
-
const {CoreAPI
|
|
2082
|
+
const {CoreAPI: CoreAPI$1, cutos: cutos$1, CutosError: CutosError$1} = cutos$3;
|
|
2083
|
+
|
|
2084
|
+
const original = {
|
|
2085
|
+
init: CoreAPI$1.init,
|
|
2086
|
+
connected: CoreAPI$1.connected,
|
|
2087
|
+
getPlatform: CoreAPI$1.getPlatform,
|
|
2088
|
+
getConfig: CoreAPI$1.getConfig,
|
|
2089
|
+
getBoxInfo: CoreAPI$1.getBoxInfo,
|
|
2090
|
+
getSignInfo: CoreAPI$1.getSignInfo,
|
|
2091
|
+
getDeviceInfo: CoreAPI$1.getDeviceInfo,
|
|
2092
|
+
getSignGroupInfo: CoreAPI$1.getSignGroupInfo,
|
|
2093
|
+
shell: CoreAPI$1.shell,
|
|
2094
|
+
getVolume: CoreAPI$1.getVolume,
|
|
2095
|
+
setVolume: CoreAPI$1.setVolume,
|
|
2096
|
+
setHttpProxy: CoreAPI$1.setHttpProxy,
|
|
2097
|
+
capability: CoreAPI$1.capability,
|
|
2098
|
+
provider: CoreAPI$1.provider,
|
|
2099
|
+
cutosCapability: cutos$1.capability,
|
|
2100
|
+
cutosProvider: cutos$1.provider,
|
|
2101
|
+
cutosDevice: cutos$1.device,
|
|
2102
|
+
cutosDriver: cutos$1.driver
|
|
2103
|
+
};
|
|
2104
|
+
|
|
2105
|
+
const mockState = {
|
|
2106
|
+
installed: false,
|
|
2107
|
+
connected: false,
|
|
2108
|
+
options: {},
|
|
2109
|
+
capabilities: new Map(),
|
|
2110
|
+
providers: new Map()
|
|
2111
|
+
};
|
|
2112
|
+
|
|
2113
|
+
function resolveCallback(callback, value, error) {
|
|
2114
|
+
if (typeof callback === 'function') {
|
|
2115
|
+
setTimeout(() => callback(error ? null : value, error), 0);
|
|
2116
|
+
return true;
|
|
2117
|
+
}
|
|
2118
|
+
|
|
2119
|
+
if (error) {
|
|
2120
|
+
return Promise.reject(error);
|
|
2121
|
+
}
|
|
2122
|
+
return Promise.resolve(value);
|
|
2123
|
+
}
|
|
2124
|
+
|
|
2125
|
+
function normalizeMethod(method) {
|
|
2126
|
+
if (typeof method === 'function') {
|
|
2127
|
+
return method;
|
|
2128
|
+
}
|
|
2129
|
+
return () => method;
|
|
2130
|
+
}
|
|
2131
|
+
|
|
2132
|
+
function ensureCapabilityRecord(name) {
|
|
2133
|
+
if (!mockState.capabilities.has(name)) {
|
|
2134
|
+
mockState.capabilities.set(name, {
|
|
2135
|
+
methods: new Map(),
|
|
2136
|
+
listeners: new Map()
|
|
2137
|
+
});
|
|
2138
|
+
}
|
|
2139
|
+
return mockState.capabilities.get(name);
|
|
2140
|
+
}
|
|
2141
|
+
|
|
2142
|
+
function registerCapability(name, definition = {}) {
|
|
2143
|
+
const record = ensureCapabilityRecord(name);
|
|
2144
|
+
const methods = definition.methods || {};
|
|
2145
|
+
|
|
2146
|
+
Object.keys(methods).forEach(methodName => {
|
|
2147
|
+
record.methods.set(methodName, normalizeMethod(methods[methodName]));
|
|
2148
|
+
});
|
|
2149
|
+
|
|
2150
|
+
return record;
|
|
2151
|
+
}
|
|
2152
|
+
|
|
2153
|
+
class MockCapability {
|
|
2154
|
+
constructor(name) {
|
|
2155
|
+
if (!name) {
|
|
2156
|
+
throw new Error('capability name cannot be empty.');
|
|
2157
|
+
}
|
|
2158
|
+
this.name = name;
|
|
2159
|
+
this.type = name;
|
|
2160
|
+
ensureCapabilityRecord(name);
|
|
2161
|
+
}
|
|
2162
|
+
|
|
2163
|
+
call(method, params = {}, options = null, callback = null) {
|
|
2164
|
+
if (typeof options === 'function') {
|
|
2165
|
+
callback = options;
|
|
2166
|
+
options = null;
|
|
2167
|
+
}
|
|
2168
|
+
|
|
2169
|
+
const record = ensureCapabilityRecord(this.name);
|
|
2170
|
+
const handler = record.methods.get(method);
|
|
2171
|
+
|
|
2172
|
+
if (!handler) {
|
|
2173
|
+
const error = new CutosError$1('METHOD_NOT_FOUND', `Mock capability method ${this.name}.${method} is not registered.`);
|
|
2174
|
+
return resolveCallback(callback, null, error);
|
|
2175
|
+
}
|
|
2176
|
+
|
|
2177
|
+
try {
|
|
2178
|
+
const result = handler(params, {capability: this, method, options});
|
|
2179
|
+
if (result && typeof result.then === 'function') {
|
|
2180
|
+
return typeof callback === 'function'
|
|
2181
|
+
? (result.then(value => callback(value)).catch(error => callback(null, error)), true)
|
|
2182
|
+
: result;
|
|
2183
|
+
}
|
|
2184
|
+
return resolveCallback(callback, result);
|
|
2185
|
+
} catch (error) {
|
|
2186
|
+
return resolveCallback(callback, null, error);
|
|
2187
|
+
}
|
|
2188
|
+
}
|
|
2189
|
+
|
|
2190
|
+
on(event, listener) {
|
|
2191
|
+
if (typeof listener !== 'function') {
|
|
2192
|
+
throw new Error('capability event listener must be a function.');
|
|
2193
|
+
}
|
|
2194
|
+
|
|
2195
|
+
const record = ensureCapabilityRecord(this.name);
|
|
2196
|
+
if (!record.listeners.has(event)) {
|
|
2197
|
+
record.listeners.set(event, new Set());
|
|
2198
|
+
}
|
|
2199
|
+
record.listeners.get(event).add(listener);
|
|
2200
|
+
return () => this.off(event, listener);
|
|
2201
|
+
}
|
|
2202
|
+
|
|
2203
|
+
off(event, listener = null) {
|
|
2204
|
+
const record = ensureCapabilityRecord(this.name);
|
|
2205
|
+
if (!record.listeners.has(event)) {
|
|
2206
|
+
return;
|
|
2207
|
+
}
|
|
2208
|
+
if (!listener) {
|
|
2209
|
+
record.listeners.delete(event);
|
|
2210
|
+
return;
|
|
2211
|
+
}
|
|
2212
|
+
|
|
2213
|
+
const listeners = record.listeners.get(event);
|
|
2214
|
+
listeners.delete(listener);
|
|
2215
|
+
if (listeners.size === 0) {
|
|
2216
|
+
record.listeners.delete(event);
|
|
2217
|
+
}
|
|
2218
|
+
}
|
|
2219
|
+
|
|
2220
|
+
dispose() {
|
|
2221
|
+
const record = ensureCapabilityRecord(this.name);
|
|
2222
|
+
record.listeners.clear();
|
|
2223
|
+
return true;
|
|
2224
|
+
}
|
|
2225
|
+
}
|
|
2226
|
+
|
|
2227
|
+
class MockProvider {
|
|
2228
|
+
constructor(name) {
|
|
2229
|
+
if (!name) {
|
|
2230
|
+
throw new Error('provider name cannot be empty.');
|
|
2231
|
+
}
|
|
2232
|
+
this.name = name;
|
|
2233
|
+
this.type = name;
|
|
2234
|
+
ensureCapabilityRecord(name);
|
|
2235
|
+
}
|
|
2236
|
+
|
|
2237
|
+
method(method, handler) {
|
|
2238
|
+
if (typeof handler !== 'function') {
|
|
2239
|
+
throw new Error('provider method handler must be a function.');
|
|
2240
|
+
}
|
|
2241
|
+
ensureCapabilityRecord(this.name).methods.set(method, handler);
|
|
2242
|
+
return this;
|
|
2243
|
+
}
|
|
2244
|
+
|
|
2245
|
+
emit(event, data) {
|
|
2246
|
+
const record = ensureCapabilityRecord(this.name);
|
|
2247
|
+
const listeners = record.listeners.get(event);
|
|
2248
|
+
const allListeners = record.listeners.get('*');
|
|
2249
|
+
const dispatch = listener => listener(data, {event, data});
|
|
2250
|
+
|
|
2251
|
+
listeners && listeners.forEach(dispatch);
|
|
2252
|
+
allListeners && allListeners.forEach(dispatch);
|
|
2253
|
+
return true;
|
|
2254
|
+
}
|
|
2255
|
+
|
|
2256
|
+
dispose() {
|
|
2257
|
+
const record = ensureCapabilityRecord(this.name);
|
|
2258
|
+
record.methods.clear();
|
|
2259
|
+
return true;
|
|
2260
|
+
}
|
|
2261
|
+
}
|
|
2262
|
+
|
|
2263
|
+
function getMockCapability(name) {
|
|
2264
|
+
if (!mockState.capabilities.has(name)) {
|
|
2265
|
+
registerCapability(name);
|
|
2266
|
+
}
|
|
2267
|
+
return new MockCapability(name);
|
|
2268
|
+
}
|
|
2269
|
+
|
|
2270
|
+
function getMockProvider(name) {
|
|
2271
|
+
if (!mockState.providers.has(name)) {
|
|
2272
|
+
mockState.providers.set(name, new MockProvider(name));
|
|
2273
|
+
}
|
|
2274
|
+
return mockState.providers.get(name);
|
|
2275
|
+
}
|
|
2276
|
+
|
|
2277
|
+
function installCutosMock$1(options = {}) {
|
|
2278
|
+
mockState.options = options;
|
|
2279
|
+
mockState.connected = false;
|
|
2280
|
+
mockState.capabilities.clear();
|
|
2281
|
+
mockState.providers.clear();
|
|
2282
|
+
|
|
2283
|
+
const capabilities = options.capabilities || {};
|
|
2284
|
+
Object.keys(capabilities).forEach(name => registerCapability(name, capabilities[name]));
|
|
2285
|
+
|
|
2286
|
+
CoreAPI$1.init = function (host = 'localhost', callback) {
|
|
2287
|
+
if (typeof host === 'function') {
|
|
2288
|
+
callback = host;
|
|
2289
|
+
host = 'localhost';
|
|
2290
|
+
}
|
|
2291
|
+
mockState.connected = true;
|
|
2292
|
+
return resolveCallback(callback, options.initResult || 'CoreAPI mock init success');
|
|
2293
|
+
};
|
|
2294
|
+
CoreAPI$1.connected = () => mockState.connected;
|
|
2295
|
+
CoreAPI$1.getPlatform = callback => resolveCallback(callback, options.platform || {name: 'CUTOS Mock'});
|
|
2296
|
+
CoreAPI$1.getConfig = callback => resolveCallback(callback, options.config || {});
|
|
2297
|
+
CoreAPI$1.getBoxInfo = callback => resolveCallback(callback, options.boxInfo || {name: 'CUTOS Mock Node'});
|
|
2298
|
+
CoreAPI$1.getSignInfo = callback => resolveCallback(callback, options.signInfo || {});
|
|
2299
|
+
CoreAPI$1.getDeviceInfo = callback => resolveCallback(callback, options.deviceInfo || []);
|
|
2300
|
+
CoreAPI$1.getSignGroupInfo = callback => resolveCallback(callback, options.signGroupInfo || {});
|
|
2301
|
+
CoreAPI$1.shell = (command, callback) => resolveCallback(callback, {command, stdout: '', stderr: '', code: 0});
|
|
2302
|
+
CoreAPI$1.getVolume = callback => resolveCallback(callback, options.volume ?? 50);
|
|
2303
|
+
CoreAPI$1.setVolume = (vol, callback) => {
|
|
2304
|
+
mockState.options.volume = vol;
|
|
2305
|
+
return resolveCallback(callback, vol);
|
|
2306
|
+
};
|
|
2307
|
+
CoreAPI$1.setHttpProxy = (api, target, callback) => resolveCallback(callback, {api, target});
|
|
2308
|
+
CoreAPI$1.capability = (name) => getMockCapability(name);
|
|
2309
|
+
CoreAPI$1.provider = (name) => getMockProvider(name);
|
|
2310
|
+
|
|
2311
|
+
cutos$1.capability = (name) => getMockCapability(name);
|
|
2312
|
+
cutos$1.provider = (name) => getMockProvider(name);
|
|
2313
|
+
cutos$1.device = (name) => getMockCapability(name);
|
|
2314
|
+
cutos$1.driver = (name) => getMockProvider(name);
|
|
2315
|
+
|
|
2316
|
+
mockState.installed = true;
|
|
2317
|
+
return CutosMock$1;
|
|
2318
|
+
}
|
|
2319
|
+
|
|
2320
|
+
function uninstallCutosMock$1() {
|
|
2321
|
+
CoreAPI$1.init = original.init;
|
|
2322
|
+
CoreAPI$1.connected = original.connected;
|
|
2323
|
+
CoreAPI$1.getPlatform = original.getPlatform;
|
|
2324
|
+
CoreAPI$1.getConfig = original.getConfig;
|
|
2325
|
+
CoreAPI$1.getBoxInfo = original.getBoxInfo;
|
|
2326
|
+
CoreAPI$1.getSignInfo = original.getSignInfo;
|
|
2327
|
+
CoreAPI$1.getDeviceInfo = original.getDeviceInfo;
|
|
2328
|
+
CoreAPI$1.getSignGroupInfo = original.getSignGroupInfo;
|
|
2329
|
+
CoreAPI$1.shell = original.shell;
|
|
2330
|
+
CoreAPI$1.getVolume = original.getVolume;
|
|
2331
|
+
CoreAPI$1.setVolume = original.setVolume;
|
|
2332
|
+
CoreAPI$1.setHttpProxy = original.setHttpProxy;
|
|
2333
|
+
CoreAPI$1.capability = original.capability;
|
|
2334
|
+
CoreAPI$1.provider = original.provider;
|
|
2335
|
+
cutos$1.capability = original.cutosCapability;
|
|
2336
|
+
cutos$1.provider = original.cutosProvider;
|
|
2337
|
+
cutos$1.device = original.cutosDevice;
|
|
2338
|
+
cutos$1.driver = original.cutosDriver;
|
|
2339
|
+
|
|
2340
|
+
mockState.connected = false;
|
|
2341
|
+
mockState.installed = false;
|
|
2342
|
+
mockState.capabilities.clear();
|
|
2343
|
+
mockState.providers.clear();
|
|
2344
|
+
}
|
|
2345
|
+
|
|
2346
|
+
const CutosMock$1 = {
|
|
2347
|
+
install: installCutosMock$1,
|
|
2348
|
+
uninstall: uninstallCutosMock$1,
|
|
2349
|
+
registerCapability,
|
|
2350
|
+
capability: getMockCapability,
|
|
2351
|
+
provider: getMockProvider,
|
|
2352
|
+
isInstalled: () => mockState.installed
|
|
2353
|
+
};
|
|
2354
|
+
|
|
2355
|
+
var mock = {CutosMock: CutosMock$1, installCutosMock: installCutosMock$1, uninstallCutosMock: uninstallCutosMock$1};
|
|
2356
|
+
|
|
2357
|
+
const {CoreAPI, CoreDefine, CoreClass, cutosAPI, cutos, CutosError} = cutos$3;
|
|
2083
2358
|
const {CoreSimulator} = coreSimulator;
|
|
2359
|
+
const {CutosMock, installCutosMock, uninstallCutosMock} = mock;
|
|
2084
2360
|
|
|
2085
2361
|
|
|
2086
2362
|
var CoreAPI_1 = src.CoreAPI = CoreAPI;
|
|
@@ -2090,5 +2366,8 @@ var cutosAPI_1 = src.cutosAPI = cutosAPI;
|
|
|
2090
2366
|
var cutos_1 = src.cutos = cutos;
|
|
2091
2367
|
var CutosError_1 = src.CutosError = CutosError;
|
|
2092
2368
|
var CoreSimulator_1 = src.CoreSimulator = CoreSimulator;
|
|
2369
|
+
var CutosMock_1 = src.CutosMock = CutosMock;
|
|
2370
|
+
var installCutosMock_1 = src.installCutosMock = installCutosMock;
|
|
2371
|
+
var uninstallCutosMock_1 = src.uninstallCutosMock = uninstallCutosMock;
|
|
2093
2372
|
|
|
2094
|
-
export { CoreAPI_1 as CoreAPI, CoreClass_1 as CoreClass, CoreDefine_1 as CoreDefine, CoreSimulator_1 as CoreSimulator, CutosError_1 as CutosError, cutos_1 as cutos, cutosAPI_1 as cutosAPI, src as default };
|
|
2373
|
+
export { CoreAPI_1 as CoreAPI, CoreClass_1 as CoreClass, CoreDefine_1 as CoreDefine, CoreSimulator_1 as CoreSimulator, CutosError_1 as CutosError, CutosMock_1 as CutosMock, cutos_1 as cutos, cutosAPI_1 as cutosAPI, src as default, installCutosMock_1 as installCutosMock, uninstallCutosMock_1 as uninstallCutosMock };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cutos/core",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.1",
|
|
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",
|
package/types/index.d.ts
CHANGED
|
@@ -314,11 +314,39 @@ export interface CoreSimulator {
|
|
|
314
314
|
start(driver: { init(args?: unknown): void }, port?: number): void;
|
|
315
315
|
}
|
|
316
316
|
|
|
317
|
+
export interface CutosMockCapabilityDefinition {
|
|
318
|
+
methods?: Record<string, ((params: unknown, context: unknown) => unknown | Promise<unknown>) | unknown>;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
export interface CutosMockOptions {
|
|
322
|
+
initResult?: unknown;
|
|
323
|
+
platform?: unknown;
|
|
324
|
+
config?: unknown;
|
|
325
|
+
boxInfo?: unknown;
|
|
326
|
+
signInfo?: unknown;
|
|
327
|
+
deviceInfo?: unknown;
|
|
328
|
+
signGroupInfo?: unknown;
|
|
329
|
+
volume?: number;
|
|
330
|
+
capabilities?: Record<string, CutosMockCapabilityDefinition>;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
export interface CutosMock {
|
|
334
|
+
install(options?: CutosMockOptions): CutosMock;
|
|
335
|
+
uninstall(): void;
|
|
336
|
+
registerCapability(name: string, definition?: CutosMockCapabilityDefinition): unknown;
|
|
337
|
+
capability(name: string): Capability;
|
|
338
|
+
provider(name: string): Provider;
|
|
339
|
+
isInstalled(): boolean;
|
|
340
|
+
}
|
|
341
|
+
|
|
317
342
|
export const CoreAPI: CoreAPI;
|
|
318
343
|
export const CoreDefine: CoreDefine;
|
|
319
344
|
export const CoreClass: CoreClass;
|
|
320
345
|
export const cutosAPI: Record<string, unknown>;
|
|
321
346
|
export const CoreSimulator: CoreSimulator;
|
|
347
|
+
export const CutosMock: CutosMock;
|
|
348
|
+
export function installCutosMock(options?: CutosMockOptions): CutosMock;
|
|
349
|
+
export function uninstallCutosMock(): void;
|
|
322
350
|
export { CutosError };
|
|
323
351
|
export const cutos: {
|
|
324
352
|
capability(name: string, opts?: CapabilityOptions): Capability;
|