@onekeyfe/hd-shared 0.1.6

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 ADDED
@@ -0,0 +1,3 @@
1
+ # `shared`
2
+
3
+ > Hardware SDK's shared tool library
@@ -0,0 +1,58 @@
1
+ export interface IHardwareError {
2
+ errorCode: ValueOf<typeof HardwareErrorCode>;
3
+ message?: string;
4
+ }
5
+ declare type ValueOf<P extends object> = P[keyof P];
6
+ declare type HardwareErrorCodeMessageMapping = {
7
+ [P in ValueOf<typeof HardwareErrorCode>]: string;
8
+ };
9
+ declare type ErrorCodeUnion = ValueOf<typeof HardwareErrorCode>;
10
+ export declare class HardwareError extends Error {
11
+ errorCode: ErrorCodeUnion;
12
+ message: string;
13
+ constructor(hardwareError: IHardwareError | string);
14
+ }
15
+ export declare const HardwareErrorCode: {
16
+ readonly UnknownError: 0;
17
+ readonly DeviceFwException: 101;
18
+ readonly DeviceUnexpectedMode: 102;
19
+ readonly DeviceListNotInitialized: 103;
20
+ readonly SelectDevice: 104;
21
+ readonly DeviceNotFound: 105;
22
+ readonly DeviceInitializeFailed: 106;
23
+ readonly DeviceInterruptedFromOutside: 107;
24
+ readonly NotInitialized: 200;
25
+ readonly IFrameNotInitialized: 300;
26
+ readonly IFrameAleradyInitialized: 301;
27
+ readonly IFrameLoadFail: 302;
28
+ readonly IframeTimeout: 303;
29
+ readonly IframeBlocked: 304;
30
+ readonly CallMethodError: 400;
31
+ readonly CallMethodNotResponse: 404;
32
+ readonly CallMethodInvalidParameter: 405;
33
+ readonly FirmwareUpdateDownloadFailed: 406;
34
+ readonly NetworkError: 500;
35
+ readonly TransportNotConfigured: 600;
36
+ readonly TransportCallInProgress: 601;
37
+ readonly TransportNotFound: 602;
38
+ readonly TransportInvalidProtobuf: 603;
39
+ readonly BleScanError: 700;
40
+ readonly BlePermissionError: 701;
41
+ readonly BleLocationError: 702;
42
+ readonly BleRequiredUUID: 703;
43
+ readonly BleConnectedError: 704;
44
+ readonly BleDeviceNotBonded: 705;
45
+ readonly BleServiceNotFound: 706;
46
+ readonly BleCharacteristicNotFound: 707;
47
+ readonly BleMonitorError: 708;
48
+ readonly RuntimeError: 800;
49
+ readonly PinInvalid: 801;
50
+ readonly PinCancelled: 802;
51
+ readonly ActionCancelled: 803;
52
+ readonly FirmwareError: 804;
53
+ };
54
+ export declare const HardwareErrorCodeMessage: HardwareErrorCodeMessageMapping;
55
+ export declare const TypedError: (hardwareError: ErrorCodeUnion | string, message?: string | undefined) => HardwareError;
56
+ export declare const serializeError: (payload: any) => any;
57
+ export {};
58
+ //# sourceMappingURL=HardwareError.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"HardwareError.d.ts","sourceRoot":"","sources":["../src/HardwareError.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,cAAc;IAC7B,SAAS,EAAE,OAAO,CAAC,OAAO,iBAAiB,CAAC,CAAC;IAC7C,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,aAAK,OAAO,CAAC,CAAC,SAAS,MAAM,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;AAE5C,aAAK,+BAA+B,GAAG;KAAG,CAAC,IAAI,OAAO,CAAC,OAAO,iBAAiB,CAAC,GAAG,MAAM;CAAE,CAAC;AAE5F,aAAK,cAAc,GAAG,OAAO,CAAC,OAAO,iBAAiB,CAAC,CAAC;AAMxD,qBAAa,aAAc,SAAQ,KAAK;IACtC,SAAS,EAAE,cAAc,CAAkC;IAE3D,OAAO,SAAM;gBAED,aAAa,EAAE,cAAc,GAAG,MAAM;CAkBnD;AAED,eAAO,MAAM,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA0JpB,CAAC;AAEX,eAAO,MAAM,wBAAwB,EAAE,+BAqE7B,CAAC;AAEX,eAAO,MAAM,UAAU,kBAAmB,cAAc,GAAG,MAAM,gDAKhE,CAAC;AAEF,eAAO,MAAM,cAAc,YAAa,GAAG,QAQ1C,CAAC"}
@@ -0,0 +1,9 @@
1
+ export declare type Deferred<T, I = any, D = any> = {
2
+ id?: I;
3
+ data?: D;
4
+ promise: Promise<T>;
5
+ resolve: (t: T) => void;
6
+ reject: (e: Error) => void;
7
+ };
8
+ export declare function createDeferred<T, I = any, D = any>(arg?: I, data?: D): Deferred<T, I, D>;
9
+ //# sourceMappingURL=deferred.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deferred.d.ts","sourceRoot":"","sources":["../src/deferred.ts"],"names":[],"mappings":"AAAA,oBAAY,QAAQ,CAAC,CAAC,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,GAAG,GAAG,IAAI;IAC1C,EAAE,CAAC,EAAE,CAAC,CAAC;IACP,IAAI,CAAC,EAAE,CAAC,CAAC;IACT,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;IACpB,OAAO,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,IAAI,CAAC;IACxB,MAAM,EAAE,CAAC,CAAC,EAAE,KAAK,KAAK,IAAI,CAAC;CAC5B,CAAC;AAEF,wBAAgB,cAAc,CAAC,CAAC,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,GAAG,GAAG,EAAE,GAAG,CAAC,EAAE,CAAC,EAAE,IAAI,CAAC,EAAE,CAAC,GAAG,QAAQ,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CA0BxF"}
@@ -0,0 +1,85 @@
1
+ declare type Deferred<T, I = any, D = any> = {
2
+ id?: I;
3
+ data?: D;
4
+ promise: Promise<T>;
5
+ resolve: (t: T) => void;
6
+ reject: (e: Error) => void;
7
+ };
8
+ declare function createDeferred<T, I = any, D = any>(arg?: I, data?: D): Deferred<T, I, D>;
9
+
10
+ interface IHardwareError {
11
+ errorCode: ValueOf<typeof HardwareErrorCode>;
12
+ message?: string;
13
+ }
14
+ declare type ValueOf<P extends object> = P[keyof P];
15
+ declare type HardwareErrorCodeMessageMapping = {
16
+ [P in ValueOf<typeof HardwareErrorCode>]: string;
17
+ };
18
+ declare type ErrorCodeUnion = ValueOf<typeof HardwareErrorCode>;
19
+ declare class HardwareError extends Error {
20
+ errorCode: ErrorCodeUnion;
21
+ message: string;
22
+ constructor(hardwareError: IHardwareError | string);
23
+ }
24
+ declare const HardwareErrorCode: {
25
+ readonly UnknownError: 0;
26
+ readonly DeviceFwException: 101;
27
+ readonly DeviceUnexpectedMode: 102;
28
+ readonly DeviceListNotInitialized: 103;
29
+ readonly SelectDevice: 104;
30
+ readonly DeviceNotFound: 105;
31
+ readonly DeviceInitializeFailed: 106;
32
+ readonly DeviceInterruptedFromOutside: 107;
33
+ readonly NotInitialized: 200;
34
+ readonly IFrameNotInitialized: 300;
35
+ readonly IFrameAleradyInitialized: 301;
36
+ readonly IFrameLoadFail: 302;
37
+ readonly IframeTimeout: 303;
38
+ readonly IframeBlocked: 304;
39
+ readonly CallMethodError: 400;
40
+ readonly CallMethodNotResponse: 404;
41
+ readonly CallMethodInvalidParameter: 405;
42
+ readonly FirmwareUpdateDownloadFailed: 406;
43
+ readonly NetworkError: 500;
44
+ readonly TransportNotConfigured: 600;
45
+ readonly TransportCallInProgress: 601;
46
+ readonly TransportNotFound: 602;
47
+ readonly TransportInvalidProtobuf: 603;
48
+ readonly BleScanError: 700;
49
+ readonly BlePermissionError: 701;
50
+ readonly BleLocationError: 702;
51
+ readonly BleRequiredUUID: 703;
52
+ readonly BleConnectedError: 704;
53
+ readonly BleDeviceNotBonded: 705;
54
+ readonly BleServiceNotFound: 706;
55
+ readonly BleCharacteristicNotFound: 707;
56
+ readonly BleMonitorError: 708;
57
+ readonly RuntimeError: 800;
58
+ readonly PinInvalid: 801;
59
+ readonly PinCancelled: 802;
60
+ readonly ActionCancelled: 803;
61
+ readonly FirmwareError: 804;
62
+ };
63
+ declare const HardwareErrorCodeMessage: HardwareErrorCodeMessageMapping;
64
+ declare const TypedError: (hardwareError: ErrorCodeUnion | string, message?: string | undefined) => HardwareError;
65
+ declare const serializeError: (payload: any) => any;
66
+
67
+ type HardwareError$1_IHardwareError = IHardwareError;
68
+ type HardwareError$1_HardwareError = HardwareError;
69
+ declare const HardwareError$1_HardwareError: typeof HardwareError;
70
+ declare const HardwareError$1_HardwareErrorCode: typeof HardwareErrorCode;
71
+ declare const HardwareError$1_HardwareErrorCodeMessage: typeof HardwareErrorCodeMessage;
72
+ declare const HardwareError$1_TypedError: typeof TypedError;
73
+ declare const HardwareError$1_serializeError: typeof serializeError;
74
+ declare namespace HardwareError$1 {
75
+ export {
76
+ HardwareError$1_IHardwareError as IHardwareError,
77
+ HardwareError$1_HardwareError as HardwareError,
78
+ HardwareError$1_HardwareErrorCode as HardwareErrorCode,
79
+ HardwareError$1_HardwareErrorCodeMessage as HardwareErrorCodeMessage,
80
+ HardwareError$1_TypedError as TypedError,
81
+ HardwareError$1_serializeError as serializeError,
82
+ };
83
+ }
84
+
85
+ export { Deferred, HardwareError$1 as ERRORS, HardwareError, HardwareErrorCode, HardwareErrorCodeMessage, IHardwareError, TypedError, createDeferred, serializeError };
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,OAAO,KAAK,MAAM,MAAM,iBAAiB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,191 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ /******************************************************************************
6
+ Copyright (c) Microsoft Corporation.
7
+
8
+ Permission to use, copy, modify, and/or distribute this software for any
9
+ purpose with or without fee is hereby granted.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ ***************************************************************************** */
19
+
20
+ function __awaiter(thisArg, _arguments, P, generator) {
21
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
22
+ return new (P || (P = Promise))(function (resolve, reject) {
23
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
24
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
25
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
26
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
27
+ });
28
+ }
29
+
30
+ function createDeferred(arg, data) {
31
+ let localResolve = (_t) => { };
32
+ let localReject = (_e) => { };
33
+ let id;
34
+ const promise = new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
35
+ localResolve = resolve;
36
+ localReject = reject;
37
+ if (typeof arg === 'function') {
38
+ try {
39
+ yield arg();
40
+ }
41
+ catch (error) {
42
+ reject(error);
43
+ }
44
+ }
45
+ if (typeof arg === 'string')
46
+ id = arg;
47
+ }));
48
+ return {
49
+ id,
50
+ data,
51
+ resolve: localResolve,
52
+ reject: localReject,
53
+ promise,
54
+ };
55
+ }
56
+
57
+ function fillStringWithArguments(value, object) {
58
+ return value.replace(/\{([^}]+)\}/g, (_, arg) => object[arg] || '?');
59
+ }
60
+ class HardwareError extends Error {
61
+ constructor(hardwareError) {
62
+ var _a;
63
+ super();
64
+ this.errorCode = HardwareErrorCode.UnknownError;
65
+ this.message = '';
66
+ const errorMessageMapping = HardwareErrorCodeMessage;
67
+ this.message = errorMessageMapping[HardwareErrorCode.UnknownError];
68
+ if (typeof hardwareError === 'string') {
69
+ this.errorCode = HardwareErrorCode.UnknownError;
70
+ this.message = hardwareError;
71
+ }
72
+ else {
73
+ const message = (_a = (hardwareError.message || errorMessageMapping[hardwareError.errorCode])) !== null && _a !== void 0 ? _a : '';
74
+ if (message) {
75
+ this.message = fillStringWithArguments(message, hardwareError);
76
+ }
77
+ this.errorCode = hardwareError.errorCode;
78
+ }
79
+ this.name = 'HardwareError';
80
+ }
81
+ }
82
+ const HardwareErrorCode = {
83
+ UnknownError: 0,
84
+ DeviceFwException: 101,
85
+ DeviceUnexpectedMode: 102,
86
+ DeviceListNotInitialized: 103,
87
+ SelectDevice: 104,
88
+ DeviceNotFound: 105,
89
+ DeviceInitializeFailed: 106,
90
+ DeviceInterruptedFromOutside: 107,
91
+ NotInitialized: 200,
92
+ IFrameNotInitialized: 300,
93
+ IFrameAleradyInitialized: 301,
94
+ IFrameLoadFail: 302,
95
+ IframeTimeout: 303,
96
+ IframeBlocked: 304,
97
+ CallMethodError: 400,
98
+ CallMethodNotResponse: 404,
99
+ CallMethodInvalidParameter: 405,
100
+ FirmwareUpdateDownloadFailed: 406,
101
+ NetworkError: 500,
102
+ TransportNotConfigured: 600,
103
+ TransportCallInProgress: 601,
104
+ TransportNotFound: 602,
105
+ TransportInvalidProtobuf: 603,
106
+ BleScanError: 700,
107
+ BlePermissionError: 701,
108
+ BleLocationError: 702,
109
+ BleRequiredUUID: 703,
110
+ BleConnectedError: 704,
111
+ BleDeviceNotBonded: 705,
112
+ BleServiceNotFound: 706,
113
+ BleCharacteristicNotFound: 707,
114
+ BleMonitorError: 708,
115
+ RuntimeError: 800,
116
+ PinInvalid: 801,
117
+ PinCancelled: 802,
118
+ ActionCancelled: 803,
119
+ FirmwareError: 804,
120
+ };
121
+ const HardwareErrorCodeMessage = {
122
+ [HardwareErrorCode.UnknownError]: 'Unknown error occurred. Check message property.',
123
+ [HardwareErrorCode.DeviceFwException]: 'Firmware version mismatch',
124
+ [HardwareErrorCode.DeviceUnexpectedMode]: 'Device unexpected mode',
125
+ [HardwareErrorCode.DeviceListNotInitialized]: 'Device list is not initialized',
126
+ [HardwareErrorCode.SelectDevice]: 'Please select the connected device',
127
+ [HardwareErrorCode.DeviceNotFound]: 'Device not found',
128
+ [HardwareErrorCode.DeviceInitializeFailed]: 'Device initialization failed',
129
+ [HardwareErrorCode.DeviceInterruptedFromOutside]: 'Device interrupted',
130
+ [HardwareErrorCode.NotInitialized]: 'Not initialized',
131
+ [HardwareErrorCode.IFrameNotInitialized]: 'IFrame not initialized',
132
+ [HardwareErrorCode.IFrameAleradyInitialized]: 'IFrame alerady initialized',
133
+ [HardwareErrorCode.IFrameLoadFail]: 'IFrame load fail',
134
+ [HardwareErrorCode.IframeTimeout]: 'init iframe time out',
135
+ [HardwareErrorCode.IframeBlocked]: 'IFrame blocked',
136
+ [HardwareErrorCode.CallMethodError]: 'Runtime errors during method execution',
137
+ [HardwareErrorCode.CallMethodNotResponse]: 'Method does not responding',
138
+ [HardwareErrorCode.CallMethodInvalidParameter]: 'Call method invalid parameter',
139
+ [HardwareErrorCode.FirmwareUpdateDownloadFailed]: 'Firmware update download failed',
140
+ [HardwareErrorCode.NetworkError]: 'Network request error',
141
+ [HardwareErrorCode.TransportNotConfigured]: 'Transport not configured',
142
+ [HardwareErrorCode.TransportCallInProgress]: 'Transport call in progress',
143
+ [HardwareErrorCode.TransportNotFound]: 'Transport not found',
144
+ [HardwareErrorCode.TransportInvalidProtobuf]: 'Transport invalid protobuf',
145
+ [HardwareErrorCode.BleScanError]: 'BLE scan error',
146
+ [HardwareErrorCode.BlePermissionError]: 'Bluetooth required to be turned on',
147
+ [HardwareErrorCode.BleLocationError]: 'Device is not authorized to use BluetoothLE',
148
+ [HardwareErrorCode.BleRequiredUUID]: 'uuid is required',
149
+ [HardwareErrorCode.BleConnectedError]: 'connected error is always runtime error',
150
+ [HardwareErrorCode.BleDeviceNotBonded]: 'device is not bonded',
151
+ [HardwareErrorCode.BleServiceNotFound]: 'BLEServiceNotFound: service not found',
152
+ [HardwareErrorCode.BleCharacteristicNotFound]: 'BLEServiceNotFound: service not found',
153
+ [HardwareErrorCode.BleMonitorError]: 'Monitor Error: characteristic not found',
154
+ [HardwareErrorCode.RuntimeError]: 'Runtime error',
155
+ [HardwareErrorCode.PinInvalid]: 'Pin invalid',
156
+ [HardwareErrorCode.PinCancelled]: 'Pin cancelled',
157
+ [HardwareErrorCode.ActionCancelled]: 'Action cancelled by user',
158
+ [HardwareErrorCode.FirmwareError]: 'Firmware installation failed',
159
+ };
160
+ const TypedError = (hardwareError, message) => {
161
+ if (typeof hardwareError === 'string') {
162
+ return new HardwareError(hardwareError);
163
+ }
164
+ return new HardwareError({ errorCode: hardwareError, message: message !== null && message !== void 0 ? message : '' });
165
+ };
166
+ const serializeError = (payload) => {
167
+ if (payload && payload.error instanceof HardwareError) {
168
+ return { error: payload.error.message, code: payload.error.errorCode };
169
+ }
170
+ if (payload && payload.error instanceof Error) {
171
+ return { error: payload.error.message, code: payload.error.code };
172
+ }
173
+ return payload;
174
+ };
175
+
176
+ var HardwareError$1 = /*#__PURE__*/Object.freeze({
177
+ __proto__: null,
178
+ HardwareError: HardwareError,
179
+ HardwareErrorCode: HardwareErrorCode,
180
+ HardwareErrorCodeMessage: HardwareErrorCodeMessage,
181
+ TypedError: TypedError,
182
+ serializeError: serializeError
183
+ });
184
+
185
+ exports.ERRORS = HardwareError$1;
186
+ exports.HardwareError = HardwareError;
187
+ exports.HardwareErrorCode = HardwareErrorCode;
188
+ exports.HardwareErrorCodeMessage = HardwareErrorCodeMessage;
189
+ exports.TypedError = TypedError;
190
+ exports.createDeferred = createDeferred;
191
+ exports.serializeError = serializeError;
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@onekeyfe/hd-shared",
3
+ "version": "0.1.6",
4
+ "description": "Hardware SDK's shared tool library",
5
+ "keywords": [
6
+ "Hardware-SDK",
7
+ "shared",
8
+ "utils"
9
+ ],
10
+ "author": "Leon <lixiao.dev@gmail.com>",
11
+ "homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
12
+ "license": "ISC",
13
+ "main": "dist/index.js",
14
+ "types": "dist/index.d.ts",
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/OneKeyHQ/hardware-js-sdk.git"
21
+ },
22
+ "scripts": {
23
+ "dev": "rimraf dist && rollup -c ../../build/rollup.config.js -w",
24
+ "build": "rimraf dist && rollup -c ../../build/rollup.config.js",
25
+ "lint": "eslint .",
26
+ "lint:fix": "eslint . --fix"
27
+ },
28
+ "gitHead": "ebe2789fa24cbfb798c9be445678e71e67e33d71"
29
+ }
@@ -0,0 +1,283 @@
1
+ export interface IHardwareError {
2
+ errorCode: ValueOf<typeof HardwareErrorCode>;
3
+ message?: string;
4
+ }
5
+
6
+ type ValueOf<P extends object> = P[keyof P];
7
+
8
+ type HardwareErrorCodeMessageMapping = { [P in ValueOf<typeof HardwareErrorCode>]: string };
9
+
10
+ type ErrorCodeUnion = ValueOf<typeof HardwareErrorCode>;
11
+
12
+ function fillStringWithArguments(value: string, object: object) {
13
+ return value.replace(/\{([^}]+)\}/g, (_, arg: string) => (object as unknown as any)[arg] || '?');
14
+ }
15
+
16
+ export class HardwareError extends Error {
17
+ errorCode: ErrorCodeUnion = HardwareErrorCode.UnknownError;
18
+
19
+ message = '';
20
+
21
+ constructor(hardwareError: IHardwareError | string) {
22
+ super();
23
+ const errorMessageMapping = HardwareErrorCodeMessage;
24
+ this.message = errorMessageMapping[HardwareErrorCode.UnknownError];
25
+
26
+ if (typeof hardwareError === 'string') {
27
+ this.errorCode = HardwareErrorCode.UnknownError;
28
+ this.message = hardwareError;
29
+ } else {
30
+ const message = (hardwareError.message || errorMessageMapping[hardwareError.errorCode]) ?? '';
31
+ if (message) {
32
+ this.message = fillStringWithArguments(message, hardwareError);
33
+ }
34
+ this.errorCode = hardwareError.errorCode;
35
+ }
36
+
37
+ this.name = 'HardwareError';
38
+ }
39
+ }
40
+
41
+ export const HardwareErrorCode = {
42
+ /**
43
+ * This error can be thrown when unexpected error occurred and in most cases it is related to implementation bug.
44
+ * Original message is available in message property.
45
+ */
46
+ UnknownError: 0,
47
+
48
+ /**
49
+ * Firmware version mismatch
50
+ */
51
+ DeviceFwException: 101,
52
+
53
+ /**
54
+ * Device unexpected mode
55
+ */
56
+ DeviceUnexpectedMode: 102,
57
+
58
+ /**
59
+ * Device list is not initialized
60
+ */
61
+ DeviceListNotInitialized: 103,
62
+
63
+ /**
64
+ * Please select the connected device
65
+ */
66
+ SelectDevice: 104,
67
+
68
+ /**
69
+ * Device not found
70
+ */
71
+ DeviceNotFound: 105,
72
+
73
+ /**
74
+ * Device is not initialized
75
+ */
76
+ DeviceInitializeFailed: 106,
77
+
78
+ /**
79
+ * Device interrupted from another operation
80
+ */
81
+ DeviceInterruptedFromOutside: 107,
82
+
83
+ /**
84
+ * Not initialized
85
+ */
86
+ NotInitialized: 200,
87
+
88
+ /**
89
+ * Iframe not initialized
90
+ */
91
+ IFrameNotInitialized: 300,
92
+
93
+ /**
94
+ * iframe repeat initialization
95
+ */
96
+ IFrameAleradyInitialized: 301,
97
+
98
+ /**
99
+ * iframe load failure
100
+ */
101
+ IFrameLoadFail: 302,
102
+
103
+ /**
104
+ * init iframe time out
105
+ */
106
+ IframeTimeout: 303,
107
+
108
+ /**
109
+ * iframe blocked
110
+ */
111
+ IframeBlocked: 304,
112
+
113
+ /**
114
+ * Runtime errors during method execution
115
+ */
116
+ CallMethodError: 400,
117
+
118
+ /**
119
+ * Method does not responding
120
+ */
121
+ CallMethodNotResponse: 404,
122
+
123
+ /**
124
+ * Call method invalid parameter
125
+ */
126
+ CallMethodInvalidParameter: 405,
127
+
128
+ /**
129
+ * firmware update download failed
130
+ */
131
+ FirmwareUpdateDownloadFailed: 406,
132
+
133
+ /**
134
+ * Netword request error
135
+ */
136
+ NetworkError: 500,
137
+
138
+ /**
139
+ * Transport not configured
140
+ */
141
+ TransportNotConfigured: 600,
142
+
143
+ /**
144
+ * Transport call in progress
145
+ */
146
+ TransportCallInProgress: 601,
147
+
148
+ /**
149
+ * Transport not found
150
+ */
151
+ TransportNotFound: 602,
152
+
153
+ /**
154
+ * Transport invalid protobuf
155
+ */
156
+ TransportInvalidProtobuf: 603,
157
+
158
+ /**
159
+ * Bluetooth error code
160
+ */
161
+ BleScanError: 700,
162
+ BlePermissionError: 701,
163
+ BleLocationError: 702,
164
+ BleRequiredUUID: 703,
165
+ BleConnectedError: 704,
166
+ BleDeviceNotBonded: 705,
167
+ BleServiceNotFound: 706,
168
+ BleCharacteristicNotFound: 707,
169
+ BleMonitorError: 708,
170
+
171
+ /**
172
+ * Hardware runtiome errors
173
+ */
174
+ RuntimeError: 800,
175
+
176
+ /**
177
+ * invalid pin
178
+ */
179
+ PinInvalid: 801,
180
+
181
+ /**
182
+ * pin cancelled by user
183
+ */
184
+ PinCancelled: 802,
185
+
186
+ /**
187
+ * Action cancelled by user
188
+ */
189
+ ActionCancelled: 803,
190
+
191
+ /**
192
+ * Firmware installation failed
193
+ */
194
+ FirmwareError: 804,
195
+ } as const;
196
+
197
+ export const HardwareErrorCodeMessage: HardwareErrorCodeMessageMapping = {
198
+ [HardwareErrorCode.UnknownError]: 'Unknown error occurred. Check message property.',
199
+
200
+ /**
201
+ * Device Errors
202
+ */
203
+ [HardwareErrorCode.DeviceFwException]: 'Firmware version mismatch',
204
+ [HardwareErrorCode.DeviceUnexpectedMode]: 'Device unexpected mode',
205
+ [HardwareErrorCode.DeviceListNotInitialized]: 'Device list is not initialized',
206
+ [HardwareErrorCode.SelectDevice]: 'Please select the connected device',
207
+ [HardwareErrorCode.DeviceNotFound]: 'Device not found',
208
+ [HardwareErrorCode.DeviceInitializeFailed]: 'Device initialization failed',
209
+ [HardwareErrorCode.DeviceInterruptedFromOutside]: 'Device interrupted',
210
+
211
+ /**
212
+ * Node Errors
213
+ */
214
+ [HardwareErrorCode.NotInitialized]: 'Not initialized',
215
+ /**
216
+ * Iframe Errors
217
+ */
218
+ [HardwareErrorCode.IFrameNotInitialized]: 'IFrame not initialized',
219
+ [HardwareErrorCode.IFrameAleradyInitialized]: 'IFrame alerady initialized',
220
+ [HardwareErrorCode.IFrameLoadFail]: 'IFrame load fail',
221
+ [HardwareErrorCode.IframeTimeout]: 'init iframe time out',
222
+ [HardwareErrorCode.IframeBlocked]: 'IFrame blocked',
223
+
224
+ /**
225
+ * Method Errors
226
+ */
227
+ [HardwareErrorCode.CallMethodError]: 'Runtime errors during method execution',
228
+ [HardwareErrorCode.CallMethodNotResponse]: 'Method does not responding',
229
+ [HardwareErrorCode.CallMethodInvalidParameter]: 'Call method invalid parameter',
230
+ [HardwareErrorCode.FirmwareUpdateDownloadFailed]: 'Firmware update download failed',
231
+
232
+ /**
233
+ * Network Errors
234
+ */
235
+ [HardwareErrorCode.NetworkError]: 'Network request error',
236
+
237
+ /**
238
+ * Transport Errors
239
+ */
240
+ [HardwareErrorCode.TransportNotConfigured]: 'Transport not configured',
241
+ [HardwareErrorCode.TransportCallInProgress]: 'Transport call in progress',
242
+ [HardwareErrorCode.TransportNotFound]: 'Transport not found',
243
+ [HardwareErrorCode.TransportInvalidProtobuf]: 'Transport invalid protobuf',
244
+
245
+ /**
246
+ * Bluetooth Error
247
+ */
248
+ [HardwareErrorCode.BleScanError]: 'BLE scan error',
249
+ [HardwareErrorCode.BlePermissionError]: 'Bluetooth required to be turned on',
250
+ [HardwareErrorCode.BleLocationError]: 'Device is not authorized to use BluetoothLE',
251
+ [HardwareErrorCode.BleRequiredUUID]: 'uuid is required',
252
+ [HardwareErrorCode.BleConnectedError]: 'connected error is always runtime error',
253
+ [HardwareErrorCode.BleDeviceNotBonded]: 'device is not bonded',
254
+ [HardwareErrorCode.BleServiceNotFound]: 'BLEServiceNotFound: service not found',
255
+ [HardwareErrorCode.BleCharacteristicNotFound]: 'BLEServiceNotFound: service not found',
256
+ [HardwareErrorCode.BleMonitorError]: 'Monitor Error: characteristic not found',
257
+
258
+ /**
259
+ * Runtime Error
260
+ */
261
+ [HardwareErrorCode.RuntimeError]: 'Runtime error',
262
+ [HardwareErrorCode.PinInvalid]: 'Pin invalid',
263
+ [HardwareErrorCode.PinCancelled]: 'Pin cancelled',
264
+ [HardwareErrorCode.ActionCancelled]: 'Action cancelled by user',
265
+ [HardwareErrorCode.FirmwareError]: 'Firmware installation failed',
266
+ } as const;
267
+
268
+ export const TypedError = (hardwareError: ErrorCodeUnion | string, message?: string) => {
269
+ if (typeof hardwareError === 'string') {
270
+ return new HardwareError(hardwareError);
271
+ }
272
+ return new HardwareError({ errorCode: hardwareError, message: message ?? '' });
273
+ };
274
+
275
+ export const serializeError = (payload: any) => {
276
+ if (payload && payload.error instanceof HardwareError) {
277
+ return { error: payload.error.message, code: payload.error.errorCode };
278
+ }
279
+ if (payload && payload.error instanceof Error) {
280
+ return { error: payload.error.message, code: payload.error.code };
281
+ }
282
+ return payload;
283
+ };
@@ -0,0 +1,35 @@
1
+ export type Deferred<T, I = any, D = any> = {
2
+ id?: I;
3
+ data?: D;
4
+ promise: Promise<T>;
5
+ resolve: (t: T) => void;
6
+ reject: (e: Error) => void;
7
+ };
8
+
9
+ export function createDeferred<T, I = any, D = any>(arg?: I, data?: D): Deferred<T, I, D> {
10
+ let localResolve: (t: T) => void = (_t: T) => {};
11
+ let localReject: (e?: Error) => void = (_e?: Error) => {};
12
+ let id: I | undefined;
13
+
14
+ // eslint-disable-next-line no-async-promise-executor
15
+ const promise: Promise<T> = new Promise(async (resolve, reject) => {
16
+ localResolve = resolve;
17
+ localReject = reject;
18
+ if (typeof arg === 'function') {
19
+ try {
20
+ await arg();
21
+ } catch (error) {
22
+ reject(error);
23
+ }
24
+ }
25
+ if (typeof arg === 'string') id = arg;
26
+ });
27
+
28
+ return {
29
+ id,
30
+ data,
31
+ resolve: localResolve,
32
+ reject: localReject,
33
+ promise,
34
+ };
35
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './deferred';
2
+ export * from './HardwareError';
3
+ export * as ERRORS from './HardwareError';
package/tsconfig.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "extends": "../../tsconfig.lib.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ },
6
+ "include": ["./src"]
7
+ }