@arcblock/bridge 3.4.14 → 3.5.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcblock/bridge",
3
- "version": "3.4.14",
3
+ "version": "3.5.0",
4
4
  "description": "a bridge sdk support all js api from DID Wallet.",
5
5
  "keywords": [
6
6
  "bridge",
@@ -27,6 +27,9 @@
27
27
  "bugs": {
28
28
  "url": "https://github.com/ArcBlock/ux/issues"
29
29
  },
30
+ "files": [
31
+ "lib"
32
+ ],
30
33
  "publishConfig": {
31
34
  "access": "public"
32
35
  },
@@ -34,5 +37,5 @@
34
37
  "eslint-plugin-react-hooks": "^4.6.2",
35
38
  "jest": "^29.7.0"
36
39
  },
37
- "gitHead": "d663390d3c6a9b2d8d71790c770dd0025ae309de"
40
+ "gitHead": "1cfc816004525cf1b352ec2b64d459f4769f0237"
38
41
  }
package/src/dsbridge.js DELETED
@@ -1,144 +0,0 @@
1
- /* eslint-disable */
2
- /**
3
- * @copyright https://www.npmjs.com/package/dsbridge
4
- * @description 只添加方法劫持前的判断,其余不做任何改动,确保不会有任何影响
5
- */
6
-
7
- var bridge = {
8
- call: function (method, args, cb) {
9
- var ret = '';
10
- if (typeof args == 'function') {
11
- cb = args;
12
- args = {};
13
- }
14
- var arg = { data: args === undefined ? null : args };
15
- if (typeof cb == 'function') {
16
- var cbName = 'dscb' + window.dscb++;
17
- window[cbName] = cb;
18
- arg['_dscbstub'] = cbName;
19
- }
20
- arg = JSON.stringify(arg);
21
-
22
- //if in webview that dsBridge provided, call!
23
- if (window._dsbridge) {
24
- ret = _dsbridge.call(method, arg);
25
- } else if (window._dswk || navigator.userAgent.indexOf('_dsbridge') != -1) {
26
- ret = prompt('_dsbridge=' + method, arg);
27
- }
28
-
29
- return JSON.parse(ret || '{}').data;
30
- },
31
- register: function (name, fun, asyn) {
32
- var q = asyn ? window._dsaf : window._dsf;
33
- if (!window._dsInit) {
34
- window._dsInit = true;
35
- //notify native that js apis register successfully on next event loop
36
- setTimeout(function () {
37
- bridge.call('_dsb.dsinit');
38
- }, 0);
39
- }
40
- if (typeof fun == 'object') {
41
- q._obs[name] = fun;
42
- } else {
43
- q[name] = fun;
44
- }
45
- },
46
- registerAsyn: function (name, fun) {
47
- this.register(name, fun, true);
48
- },
49
- hasNativeMethod: function (name, type) {
50
- return this.call('_dsb.hasNativeMethod', { name: name, type: type || 'all' });
51
- },
52
- disableJavascriptDialogBlock: function (disable) {
53
- this.call('_dsb.disableJavascriptDialogBlock', {
54
- disable: disable !== false,
55
- });
56
- },
57
- canUseBridge() {
58
- return !!(window._dsbridge || window._dswk || navigator.userAgent.indexOf('_dsbridge') != -1);
59
- },
60
- };
61
-
62
- !(function () {
63
- // 只有在 dsbridge 注入过的环境,才执行下方的代码
64
- if (!bridge.canUseBridge()) return;
65
- if (window._dsf) return;
66
- const originClose = window.close;
67
- var ob = {
68
- _dsf: {
69
- _obs: {},
70
- },
71
- _dsaf: {
72
- _obs: {},
73
- },
74
- dscb: 0,
75
- dsBridge: bridge,
76
- close: function () {
77
- bridge.call('_dsb.closePage');
78
- originClose();
79
- },
80
- _handleMessageFromNative: function (info) {
81
- var arg = JSON.parse(info.data);
82
- var ret = {
83
- id: info.callbackId,
84
- complete: true,
85
- };
86
- var f = this._dsf[info.method];
87
- var af = this._dsaf[info.method];
88
- var callSyn = function (f, ob) {
89
- ret.data = f.apply(ob, arg);
90
- bridge.call('_dsb.returnValue', ret);
91
- };
92
- var callAsyn = function (f, ob) {
93
- arg.push(function (data, complete) {
94
- ret.data = data;
95
- ret.complete = complete !== false;
96
- bridge.call('_dsb.returnValue', ret);
97
- });
98
- f.apply(ob, arg);
99
- };
100
- if (f) {
101
- callSyn(f, this._dsf);
102
- } else if (af) {
103
- callAsyn(af, this._dsaf);
104
- } else {
105
- //with namespace
106
- var name = info.method.split('.');
107
- if (name.length < 2) return;
108
- var method = name.pop();
109
- var namespace = name.join('.');
110
- var obs = this._dsf._obs;
111
- var ob = obs[namespace] || {};
112
- var m = ob[method];
113
- if (m && typeof m == 'function') {
114
- callSyn(m, ob);
115
- return;
116
- }
117
- obs = this._dsaf._obs;
118
- ob = obs[namespace] || {};
119
- m = ob[method];
120
- if (m && typeof m == 'function') {
121
- callAsyn(m, ob);
122
- return;
123
- }
124
- }
125
- },
126
- };
127
- for (var attr in ob) {
128
- window[attr] = ob[attr];
129
- }
130
- bridge.register('_hasJavascriptMethod', function (method, tag) {
131
- var name = method.split('.');
132
- if (name.length < 2) {
133
- return !!(_dsf[name] || _dsaf[name]);
134
- } else {
135
- // with namespace
136
- var method = name.pop();
137
- var namespace = name.join('.');
138
- var ob = _dsf._obs[namespace] || _dsaf._obs[namespace];
139
- return ob && !!ob[method];
140
- }
141
- });
142
- })();
143
-
144
- export default bridge;
package/src/index.js DELETED
@@ -1,173 +0,0 @@
1
- import dsbridge from './dsbridge';
2
-
3
- const BRIDGE_VERSION = '1';
4
-
5
- export default {
6
- register(...args) {
7
- if (dsbridge.canUseBridge()) {
8
- dsbridge.register(...args);
9
- } else {
10
- console.warn('dsbridge is not available');
11
- }
12
- },
13
- registerAsyn(...args) {
14
- if (dsbridge.canUseBridge()) {
15
- dsbridge.registerAsyn(...args);
16
- } else {
17
- console.warn('dsbridge is not available');
18
- }
19
- },
20
- call(...args) {
21
- dsbridge.call(...args);
22
- },
23
- asyncCall(...args) {
24
- return new Promise((resolve, reject) => {
25
- dsbridge.call(...args, (resString) => {
26
- try {
27
- const result = JSON.parse(resString);
28
- if (result.code === 0) {
29
- const data = JSON.parse(result.data);
30
- resolve(data);
31
- } else {
32
- reject(new Error(result.error));
33
- }
34
- } catch {
35
- resolve(resString);
36
- }
37
- });
38
- });
39
- },
40
- // 基本功能
41
- shareMessage(params) {
42
- const { content, imgUrl, success, fail } = params;
43
- if (!content && !imgUrl) {
44
- fail('params error');
45
- return;
46
- }
47
- dsbridge.call('arcShare', JSON.stringify({ content, imgUrl }), (result) => {
48
- const parseResult = JSON.parse(result);
49
- if (parseResult && parseResult.code === 0) {
50
- if (typeof success === 'function') {
51
- success(result);
52
- }
53
- } else if (typeof fail === 'function') {
54
- fail(result);
55
- }
56
- });
57
- },
58
- showLoading() {
59
- dsbridge.call('arcShowLoading');
60
- },
61
- hideLoading() {
62
- dsbridge.call('arcHideLoading');
63
- },
64
- showToast(msg) {
65
- if (msg && typeof msg === 'string') {
66
- dsbridge.call('arcToast', msg);
67
- }
68
- },
69
- // 图片功能
70
- chooseImgs(params) {
71
- const { limit, success, fail } = params;
72
- dsbridge.call('arcChooseImgs', JSON.stringify({ limit }), (result) => {
73
- const parseResult = JSON.parse(result);
74
- if (parseResult && parseResult.code === 0) {
75
- if (typeof success === 'function') {
76
- success(result);
77
- }
78
- } else if (typeof fail === 'function') {
79
- fail(result);
80
- }
81
- });
82
- },
83
- previewImgs(params) {
84
- const { imgs } = params;
85
- if (!imgs || imgs.length === 0) {
86
- return;
87
- }
88
- dsbridge.call('arcPreviewImgs', JSON.stringify(params));
89
- },
90
- // 钱包功能
91
- isSetPWD(params) {
92
- const { success, fail } = params;
93
- dsbridge.call('arcIsSetPWD', '', (result) => {
94
- const parseResult = JSON.parse(result);
95
- if (parseResult && parseResult.code === 0) {
96
- if (typeof success === 'function') {
97
- success(result);
98
- }
99
- } else if (typeof fail === 'function') {
100
- fail(result);
101
- }
102
- });
103
- },
104
- isCodeABOk(params) {
105
- const { success, fail } = params;
106
- dsbridge.call('arcIsCodeABOk', '', (result) => {
107
- const parseResult = JSON.parse(result);
108
- if (parseResult && parseResult.code === 0) {
109
- if (typeof success === 'function') {
110
- success(result);
111
- }
112
- } else if (typeof fail === 'function') {
113
- fail(result);
114
- }
115
- });
116
- },
117
-
118
- /**
119
- * 带有 arc 命名空间的方法调用
120
- * @param {string} eventName 目标的方法名
121
- * @param {object} [params] 调用的参数,只能为 object
122
- * @returns {Promise<any>}
123
- */
124
- callArc(eventName, params = {}) {
125
- return new Promise((resolve, reject) => {
126
- const namespace = 'arc';
127
- const fnName = [namespace, eventName].join('__');
128
- const mergedParams = { ...params, _version: BRIDGE_VERSION };
129
- dsbridge.call(fnName, mergedParams, (resString) => {
130
- try {
131
- const result = JSON.parse(resString);
132
- if (result?._version === BRIDGE_VERSION) {
133
- if (result.code === 0) {
134
- try {
135
- const data = JSON.parse(result.data);
136
- resolve(data);
137
- } catch {
138
- console.warn('Failed to parse result.data');
139
- resolve(result.data);
140
- }
141
- } else {
142
- reject(new Error(result.error));
143
- }
144
- } else {
145
- // DEPRECATED: @zhanghan 兼容旧版本的 native 响应
146
- resolve(result);
147
- }
148
- } catch {
149
- console.warn('Failed to parse resString');
150
- resolve(resString);
151
- }
152
- });
153
- });
154
- },
155
- registerBlocklet(eventName, cb = () => {}) {
156
- if (dsbridge.canUseBridge()) {
157
- const namespace = 'blocklet';
158
- const cbName = [namespace, eventName].join('__');
159
- dsbridge.registerAsyn(cbName, (...args) => {
160
- const parsedArgs = args.map((x) => {
161
- try {
162
- return JSON.parse(x);
163
- } catch {
164
- return x;
165
- }
166
- });
167
- cb(...parsedArgs);
168
- });
169
- } else {
170
- console.warn('dsbridge is not available');
171
- }
172
- },
173
- };
package/vite.config.mjs DELETED
@@ -1,16 +0,0 @@
1
- import { defineConfig } from 'vite';
2
- import fg from 'fast-glob';
3
-
4
- export default defineConfig({
5
- build: {
6
- lib: {
7
- entry: fg.sync('src/**/*.{tsx,ts,jsx,js}', {
8
- ignore: ['**/stories/**', '**/demo/**', '**/*.d.ts', '**/*.stories.*'],
9
- }),
10
- formats: ['es'],
11
- fileName: (format, entryName) => `${entryName}.js`,
12
- },
13
- outDir: 'lib',
14
- emptyOutDir: true,
15
- },
16
- });