@onekeyfe/cross-inpage-provider-core 2.0.0-alpha.2 → 2.0.0-alpha.3

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.
@@ -8,8 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  /* eslint-disable @typescript-eslint/ban-ts-comment */
11
- import isPlainObject from 'lodash/isPlainObject';
12
- import isString from 'lodash/isString';
11
+ import { isPlainObject, isString } from 'lodash-es';
13
12
  import { CrossEventEmitter } from './CrossEventEmitter';
14
13
  import { appDebugLogger, consoleErrorInDev } from './loggers';
15
14
  import { web3Errors } from '@onekeyfe/cross-inpage-provider-errors';
@@ -8,7 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  /* eslint-disable @typescript-eslint/no-unsafe-assignment,@typescript-eslint/ban-ts-comment */
11
- import isFunction from 'lodash/isFunction';
11
+ import { isFunction } from 'lodash-es';
12
12
  import { CrossEventEmitter } from './CrossEventEmitter';
13
13
  import siteMetadata from './siteMetadata';
14
14
  import { fakeLogger, fakeDebugLogger, consoleErrorInDev } from './loggers';
@@ -14,8 +14,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.isLegacyExtMessage = exports.JsBridgeBase = void 0;
16
16
  /* eslint-disable @typescript-eslint/ban-ts-comment */
17
- const isPlainObject_1 = __importDefault(require("lodash/isPlainObject"));
18
- const isString_1 = __importDefault(require("lodash/isString"));
17
+ const lodash_es_1 = require("lodash-es");
19
18
  const CrossEventEmitter_1 = require("./CrossEventEmitter");
20
19
  const loggers_1 = require("./loggers");
21
20
  const cross_inpage_provider_errors_1 = require("@onekeyfe/cross-inpage-provider-errors");
@@ -261,10 +260,10 @@ class JsBridgeBase extends CrossEventEmitter_1.CrossEventEmitter {
261
260
  let payload = {
262
261
  data: null,
263
262
  };
264
- if ((0, isPlainObject_1.default)(payloadReceived)) {
263
+ if ((0, lodash_es_1.isPlainObject)(payloadReceived)) {
265
264
  payload = payloadReceived;
266
265
  }
267
- if ((0, isString_1.default)(payloadReceived)) {
266
+ if ((0, lodash_es_1.isString)(payloadReceived)) {
268
267
  try {
269
268
  payload = JSON.parse(payloadReceived);
270
269
  }
@@ -14,7 +14,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.ProviderBase = void 0;
16
16
  /* eslint-disable @typescript-eslint/no-unsafe-assignment,@typescript-eslint/ban-ts-comment */
17
- const isFunction_1 = __importDefault(require("lodash/isFunction"));
17
+ const lodash_es_1 = require("lodash-es");
18
18
  const CrossEventEmitter_1 = require("./CrossEventEmitter");
19
19
  const siteMetadata_1 = __importDefault(require("./siteMetadata"));
20
20
  const loggers_1 = require("./loggers");
@@ -124,7 +124,7 @@ class ProviderBase extends CrossEventEmitter_1.CrossEventEmitter {
124
124
  var _a, _b, _c, _d;
125
125
  return __awaiter(this, void 0, void 0, function* () {
126
126
  let hasCallback = false;
127
- if (callback && (0, isFunction_1.default)(callback)) {
127
+ if (callback && (0, lodash_es_1.isFunction)(callback)) {
128
128
  hasCallback = true;
129
129
  }
130
130
  try {
@@ -18,7 +18,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
18
18
  * This is the web browser implementation of `debug()`.
19
19
  */
20
20
  const consts_1 = require("../consts");
21
- const lodash_1 = require("lodash");
21
+ const lodash_es_1 = require("lodash-es");
22
22
  const ms_1 = __importDefault(require("ms"));
23
23
  const storageKey = consts_1.DEBUG_LOGGER_STORAGE_KEY;
24
24
  const exportsBrowser = {};
@@ -223,7 +223,7 @@ function save(namespaces) {
223
223
  yield exportsBrowser.storage.setItem(storageKey, namespaces);
224
224
  }
225
225
  else {
226
- if ((0, lodash_1.isNil)(namespaces)) {
226
+ if ((0, lodash_es_1.isNil)(namespaces)) {
227
227
  yield exportsBrowser.storage.removeItem(storageKey);
228
228
  }
229
229
  else {
package/dist/cjs/index.js CHANGED
@@ -29,6 +29,7 @@ exports.versionInfo = exports.siteMetadata = exports.injectedFactory = exports.c
29
29
  __exportStar(require("./JsBridgeBase"), exports);
30
30
  __exportStar(require("./ProviderBase"), exports);
31
31
  __exportStar(require("./loggers"), exports);
32
+ __exportStar(require("./loggerConsole"), exports);
32
33
  __exportStar(require("./injectJsBridge"), exports);
33
34
  __exportStar(require("./injectedProviderReceiveHandler"), exports);
34
35
  __exportStar(require("./JsBridgeSimple"), exports);
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.injectJsBridge = void 0;
4
- const consts_1 = require("./consts");
4
+ const loggerConsole_1 = require("./loggerConsole");
5
5
  // function fixGlobalShim() {
6
6
  // // FIX errors in ReactNative
7
7
  // // ReferenceError: Can't find variable: global
@@ -11,17 +11,15 @@ const consts_1 = require("./consts");
11
11
  // window.global = window.global || window || window.globalThis;
12
12
  // }
13
13
  function injectJsBridge(bridgeCreator) {
14
- // remove fixGlobalShim,
15
- // because fixGlobalShim make some website not work properly
14
+ // remove fixGlobalShim,
15
+ // because fixGlobalShim make some website not work properly
16
16
  // make cloudfare dead loop and make zhihu.com search functionally down
17
17
  // fixGlobalShim();
18
18
  var _a;
19
19
  if (!((_a = window === null || window === void 0 ? void 0 : window.$onekey) === null || _a === void 0 ? void 0 : _a.jsBridge)) {
20
20
  window.$onekey = window.$onekey || {};
21
21
  window.$onekey.jsBridge = bridgeCreator();
22
- if (typeof localStorage !== 'undefined' && localStorage.getItem(consts_1.DEBUG_LOGGER_STORAGE_KEY)) {
23
- console.log('===== jsBridge injected success! >>>>> ', performance.now());
24
- }
22
+ loggerConsole_1.commonLogger.debug('JsBridge injected success!', performance.now());
25
23
  }
26
24
  return window.$onekey.jsBridge;
27
25
  }
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setStoredLogConfig = exports.commonLogger = exports.LogLevel = exports.Logger = void 0;
4
+ const consts_1 = require("./consts");
5
+ var LogLevel;
6
+ (function (LogLevel) {
7
+ LogLevel[LogLevel["DEBUG"] = 0] = "DEBUG";
8
+ LogLevel[LogLevel["LOG"] = 1] = "LOG";
9
+ LogLevel[LogLevel["WARN"] = 2] = "WARN";
10
+ LogLevel[LogLevel["ERROR"] = 3] = "ERROR";
11
+ })(LogLevel || (LogLevel = {}));
12
+ exports.LogLevel = LogLevel;
13
+ function getStoredLogConfig() {
14
+ if (typeof localStorage === 'undefined') {
15
+ return undefined;
16
+ }
17
+ const config = localStorage.getItem(consts_1.DEBUG_LOGGER_STORAGE_KEY);
18
+ if (config !== null) {
19
+ try {
20
+ const level = parseInt(config, 10);
21
+ if (level in LogLevel) {
22
+ return level;
23
+ }
24
+ }
25
+ catch (_a) {
26
+ return undefined;
27
+ }
28
+ }
29
+ return undefined;
30
+ }
31
+ function setStoredLogConfig(config) {
32
+ if (typeof localStorage !== 'undefined') {
33
+ localStorage.setItem(consts_1.DEBUG_LOGGER_STORAGE_KEY, config.toString());
34
+ }
35
+ }
36
+ exports.setStoredLogConfig = setStoredLogConfig;
37
+ class Logger {
38
+ constructor(module = null) {
39
+ this.module = module;
40
+ const config = getStoredLogConfig();
41
+ this.level = config !== null && config !== void 0 ? config : LogLevel.DEBUG;
42
+ if (process.env.NODE_ENV === 'production') {
43
+ this.level = config !== null && config !== void 0 ? config : LogLevel.ERROR;
44
+ }
45
+ }
46
+ shouldLog(level) {
47
+ return level >= this.level;
48
+ }
49
+ formatMessage(...args) {
50
+ return this.module ? [`[${this.module}]:`, ...args] : args;
51
+ }
52
+ debug(...args) {
53
+ if (this.shouldLog(LogLevel.DEBUG)) {
54
+ console.debug(...this.formatMessage(...args));
55
+ }
56
+ }
57
+ log(...args) {
58
+ if (this.shouldLog(LogLevel.LOG)) {
59
+ console.log(...this.formatMessage(...args));
60
+ }
61
+ }
62
+ warn(...args) {
63
+ if (this.shouldLog(LogLevel.WARN)) {
64
+ console.warn(...this.formatMessage(...args));
65
+ }
66
+ }
67
+ error(...args) {
68
+ if (this.shouldLog(LogLevel.ERROR)) {
69
+ console.error(...this.formatMessage(...args));
70
+ }
71
+ }
72
+ }
73
+ exports.Logger = Logger;
74
+ const commonLogger = new Logger();
75
+ exports.commonLogger = commonLogger;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const version = '2.0.0-alpha.2';
3
+ const version = '2.0.0-alpha.3';
4
4
  const versionBuild = '2020-0101-1';
5
5
  exports.default = {
6
6
  version,
@@ -5,6 +5,7 @@
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.defineWindowProperty = exports.checkEnableDefineProperty = exports.checkWalletSwitchEnable = exports.ISpecialPropertyProviderNamesReflection = void 0;
7
7
  const consts_1 = require("./consts");
8
+ const loggerConsole_1 = require("./loggerConsole");
8
9
  /**
9
10
  * An enumeration mapping specific blockchain provider names to their corresponding blockchain identifiers.
10
11
  *
@@ -40,7 +41,7 @@ function checkWalletSwitchEnable() {
40
41
  return true;
41
42
  }
42
43
  catch (e) {
43
- console.error(e);
44
+ loggerConsole_1.commonLogger.warn(e);
44
45
  }
45
46
  return true;
46
47
  }
@@ -54,7 +55,7 @@ function checkEnableDefineProperty(property) {
54
55
  return !!(walletInfoLocal === null || walletInfoLocal === void 0 ? void 0 : walletInfoLocal.platformEnv.isExtension);
55
56
  }
56
57
  catch (e) {
57
- console.error(e);
58
+ loggerConsole_1.commonLogger.warn(e);
58
59
  }
59
60
  return false;
60
61
  }
@@ -92,12 +93,12 @@ function defineWindowProperty(property, provider, options) {
92
93
  }
93
94
  }
94
95
  catch (ex) {
95
- console.error(ex);
96
+ loggerConsole_1.commonLogger.error(ex);
96
97
  try {
97
98
  window[property] = provider;
98
99
  }
99
100
  catch (error) {
100
- console.error(error);
101
+ loggerConsole_1.commonLogger.warn(error);
101
102
  }
102
103
  }
103
104
  }
@@ -13,7 +13,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
13
13
  * This is the web browser implementation of `debug()`.
14
14
  */
15
15
  import { DEBUG_LOGGER_STORAGE_KEY } from '../consts';
16
- import { isNil } from 'lodash';
16
+ import { isNil } from 'lodash-es';
17
17
  import humanize from 'ms';
18
18
  const storageKey = DEBUG_LOGGER_STORAGE_KEY;
19
19
  const exportsBrowser = {};
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from './JsBridgeBase';
2
2
  export * from './ProviderBase';
3
3
  export * from './loggers';
4
+ export * from './loggerConsole';
4
5
  export * from './injectJsBridge';
5
6
  export * from './injectedProviderReceiveHandler';
6
7
  export * from './JsBridgeSimple';
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from './JsBridgeBase';
2
2
  export * from './ProviderBase';
3
3
  export * from './loggers';
4
+ export * from './loggerConsole';
4
5
  export * from './injectJsBridge';
5
6
  export * from './injectedProviderReceiveHandler';
6
7
  export * from './JsBridgeSimple';
@@ -1,4 +1,4 @@
1
- import { DEBUG_LOGGER_STORAGE_KEY } from './consts';
1
+ import { commonLogger } from './loggerConsole';
2
2
  // function fixGlobalShim() {
3
3
  // // FIX errors in ReactNative
4
4
  // // ReferenceError: Can't find variable: global
@@ -8,17 +8,15 @@ import { DEBUG_LOGGER_STORAGE_KEY } from './consts';
8
8
  // window.global = window.global || window || window.globalThis;
9
9
  // }
10
10
  function injectJsBridge(bridgeCreator) {
11
- // remove fixGlobalShim,
12
- // because fixGlobalShim make some website not work properly
11
+ // remove fixGlobalShim,
12
+ // because fixGlobalShim make some website not work properly
13
13
  // make cloudfare dead loop and make zhihu.com search functionally down
14
14
  // fixGlobalShim();
15
15
  var _a;
16
16
  if (!((_a = window === null || window === void 0 ? void 0 : window.$onekey) === null || _a === void 0 ? void 0 : _a.jsBridge)) {
17
17
  window.$onekey = window.$onekey || {};
18
18
  window.$onekey.jsBridge = bridgeCreator();
19
- if (typeof localStorage !== 'undefined' && localStorage.getItem(DEBUG_LOGGER_STORAGE_KEY)) {
20
- console.log('===== jsBridge injected success! >>>>> ', performance.now());
21
- }
19
+ commonLogger.debug('JsBridge injected success!', performance.now());
22
20
  }
23
21
  return window.$onekey.jsBridge;
24
22
  }
@@ -0,0 +1,20 @@
1
+ declare enum LogLevel {
2
+ DEBUG = 0,
3
+ LOG = 1,
4
+ WARN = 2,
5
+ ERROR = 3
6
+ }
7
+ declare function setStoredLogConfig(config: LogLevel): void;
8
+ declare class Logger {
9
+ private module?;
10
+ private level;
11
+ constructor(module?: string | null);
12
+ private shouldLog;
13
+ private formatMessage;
14
+ debug(...args: unknown[]): void;
15
+ log(...args: unknown[]): void;
16
+ warn(...args: unknown[]): void;
17
+ error(...args: unknown[]): void;
18
+ }
19
+ declare const commonLogger: Logger;
20
+ export { Logger, LogLevel, commonLogger, setStoredLogConfig };
@@ -0,0 +1,69 @@
1
+ import { DEBUG_LOGGER_STORAGE_KEY } from './consts';
2
+ var LogLevel;
3
+ (function (LogLevel) {
4
+ LogLevel[LogLevel["DEBUG"] = 0] = "DEBUG";
5
+ LogLevel[LogLevel["LOG"] = 1] = "LOG";
6
+ LogLevel[LogLevel["WARN"] = 2] = "WARN";
7
+ LogLevel[LogLevel["ERROR"] = 3] = "ERROR";
8
+ })(LogLevel || (LogLevel = {}));
9
+ function getStoredLogConfig() {
10
+ if (typeof localStorage === 'undefined') {
11
+ return undefined;
12
+ }
13
+ const config = localStorage.getItem(DEBUG_LOGGER_STORAGE_KEY);
14
+ if (config !== null) {
15
+ try {
16
+ const level = parseInt(config, 10);
17
+ if (level in LogLevel) {
18
+ return level;
19
+ }
20
+ }
21
+ catch (_a) {
22
+ return undefined;
23
+ }
24
+ }
25
+ return undefined;
26
+ }
27
+ function setStoredLogConfig(config) {
28
+ if (typeof localStorage !== 'undefined') {
29
+ localStorage.setItem(DEBUG_LOGGER_STORAGE_KEY, config.toString());
30
+ }
31
+ }
32
+ class Logger {
33
+ constructor(module = null) {
34
+ this.module = module;
35
+ const config = getStoredLogConfig();
36
+ this.level = config !== null && config !== void 0 ? config : LogLevel.DEBUG;
37
+ if (process.env.NODE_ENV === 'production') {
38
+ this.level = config !== null && config !== void 0 ? config : LogLevel.ERROR;
39
+ }
40
+ }
41
+ shouldLog(level) {
42
+ return level >= this.level;
43
+ }
44
+ formatMessage(...args) {
45
+ return this.module ? [`[${this.module}]:`, ...args] : args;
46
+ }
47
+ debug(...args) {
48
+ if (this.shouldLog(LogLevel.DEBUG)) {
49
+ console.debug(...this.formatMessage(...args));
50
+ }
51
+ }
52
+ log(...args) {
53
+ if (this.shouldLog(LogLevel.LOG)) {
54
+ console.log(...this.formatMessage(...args));
55
+ }
56
+ }
57
+ warn(...args) {
58
+ if (this.shouldLog(LogLevel.WARN)) {
59
+ console.warn(...this.formatMessage(...args));
60
+ }
61
+ }
62
+ error(...args) {
63
+ if (this.shouldLog(LogLevel.ERROR)) {
64
+ console.error(...this.formatMessage(...args));
65
+ }
66
+ }
67
+ }
68
+ const commonLogger = new Logger();
69
+ export { Logger, LogLevel, commonLogger, setStoredLogConfig };
@@ -1,4 +1,4 @@
1
- const version = '2.0.0-alpha.2';
1
+ const version = '2.0.0-alpha.3';
2
2
  const versionBuild = '2020-0101-1';
3
3
  export default {
4
4
  version,
@@ -2,6 +2,7 @@
2
2
  /* eslint-disable @typescript-eslint/no-unsafe-member-access */
3
3
  /* eslint-disable @typescript-eslint/no-unsafe-assignment */
4
4
  import { WALLET_INFO_LOACAL_KEY_V5 } from './consts';
5
+ import { commonLogger } from './loggerConsole';
5
6
  /**
6
7
  * An enumeration mapping specific blockchain provider names to their corresponding blockchain identifiers.
7
8
  *
@@ -37,7 +38,7 @@ export function checkWalletSwitchEnable() {
37
38
  return true;
38
39
  }
39
40
  catch (e) {
40
- console.error(e);
41
+ commonLogger.warn(e);
41
42
  }
42
43
  return true;
43
44
  }
@@ -50,7 +51,7 @@ export function checkEnableDefineProperty(property) {
50
51
  return !!(walletInfoLocal === null || walletInfoLocal === void 0 ? void 0 : walletInfoLocal.platformEnv.isExtension);
51
52
  }
52
53
  catch (e) {
53
- console.error(e);
54
+ commonLogger.warn(e);
54
55
  }
55
56
  return false;
56
57
  }
@@ -87,12 +88,12 @@ export function defineWindowProperty(property, provider, options) {
87
88
  }
88
89
  }
89
90
  catch (ex) {
90
- console.error(ex);
91
+ commonLogger.error(ex);
91
92
  try {
92
93
  window[property] = provider;
93
94
  }
94
95
  catch (error) {
95
- console.error(error);
96
+ commonLogger.warn(error);
96
97
  }
97
98
  }
98
99
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/cross-inpage-provider-core",
3
- "version": "2.0.0-alpha.2",
3
+ "version": "2.0.0-alpha.3",
4
4
  "keywords": [
5
5
  "cross-inpage-provider"
6
6
  ],
@@ -29,12 +29,15 @@
29
29
  "build-version-info": "node ./scripts/buildVersionInfo.js"
30
30
  },
31
31
  "dependencies": {
32
- "@onekeyfe/cross-inpage-provider-errors": "2.0.0-alpha.2",
33
- "@onekeyfe/cross-inpage-provider-events": "2.0.0-alpha.2",
34
- "@onekeyfe/cross-inpage-provider-types": "2.0.0-alpha.2",
32
+ "@onekeyfe/cross-inpage-provider-errors": "2.0.0-alpha.3",
33
+ "@onekeyfe/cross-inpage-provider-events": "2.0.0-alpha.3",
34
+ "@onekeyfe/cross-inpage-provider-types": "2.0.0-alpha.3",
35
35
  "events": "^3.3.0",
36
- "lodash": "^4.17.21",
36
+ "lodash-es": "^4.17.21",
37
37
  "ms": "^2.1.3"
38
38
  },
39
- "gitHead": "0792eb8ecafd6dfdf58d48cc7c6bc3f89a25cd21"
39
+ "devDependencies": {
40
+ "@types/lodash-es": "^4.17.12"
41
+ },
42
+ "gitHead": "6cdb4879da71641e4c90e3f6bdc0a07d3edc0e63"
40
43
  }