@abtnode/blocklet-services 1.8.1 → 1.8.2

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.
@@ -0,0 +1,77 @@
1
+ const { nanoid } = require('nanoid');
2
+
3
+ const JWT = require('@arcblock/jwt');
4
+
5
+ const { getTeamInfo } = require('@abtnode/auth/lib/auth');
6
+ // eslint-disable-next-line global-require
7
+ const logger = require('@abtnode/logger')(`${require('../../../package.json').name}:notification`);
8
+
9
+ /**
10
+ * @param {Object} notification
11
+ * @param {{
12
+ * {string} name
13
+ * {Wallet} wallet
14
+ * }} senderInfo
15
+ */
16
+ const parseNotification = (notification, senderInfo) => {
17
+ const notifications = [].concat(notification);
18
+
19
+ // TODO: 如果通过 message id 实现消息去重, 消息顺序等
20
+ notifications.forEach((x) => {
21
+ if (!x.id) {
22
+ x.id = nanoid();
23
+ x.createdAt = new Date();
24
+ }
25
+
26
+ x.sender = {
27
+ did: senderInfo.wallet.address,
28
+ pk: senderInfo.wallet.pk,
29
+ name: senderInfo.name,
30
+ };
31
+ });
32
+
33
+ return notifications;
34
+ };
35
+
36
+ const broadcast = (...args) => {
37
+ const cb = typeof args[args.length - 1] === 'function' ? args.pop() : () => {};
38
+ const [wsServer, channel, event, data, options] = args;
39
+
40
+ wsServer.broadcast(channel, event, data, options || {}, ({ count } = {}) => {
41
+ logger.info('Broadcast message to channel', { channel, sendCount: count });
42
+ cb(count);
43
+ });
44
+ };
45
+
46
+ const ensureSender = async ({ sender, node, nodeInfo }) => {
47
+ let senderInfo;
48
+ try {
49
+ senderInfo = await getTeamInfo({ node, nodeInfo, teamDid: sender.appDid });
50
+ } catch (err) {
51
+ if (err.message === 'Blocklet state must be an object') {
52
+ err.message = `Sender blocklet does not exist: ${sender.appDid}`;
53
+ }
54
+ throw err;
55
+ }
56
+
57
+ const { wallet } = senderInfo;
58
+ if (!JWT.verify(sender.token, wallet.publicKey)) {
59
+ throw new Error(`Invalid authentication token for sender blocklet: ${sender.did}`);
60
+ }
61
+
62
+ return senderInfo;
63
+ };
64
+
65
+ const getDid = (jwt) => jwt.iss.replace(/^did:abt:/, '');
66
+
67
+ const EVENTS = {
68
+ MESSAGE: 'message',
69
+ };
70
+
71
+ module.exports = {
72
+ parseNotification,
73
+ broadcast,
74
+ ensureSender,
75
+ getDid,
76
+ EVENTS,
77
+ };
@@ -11,8 +11,8 @@
11
11
  "runtime-main.js": "/.blocklet/proxy/blocklet-service/static/js/runtime-main.38ffe325.js",
12
12
  "runtime-main.js.map": "/.blocklet/proxy/blocklet-service/static/js/runtime-main.38ffe325.js.map",
13
13
  "static/css/5.4db48a1d.chunk.css": "/.blocklet/proxy/blocklet-service/static/css/5.4db48a1d.chunk.css",
14
- "static/js/5.aa73241e.chunk.js": "/.blocklet/proxy/blocklet-service/static/js/5.aa73241e.chunk.js",
15
- "static/js/5.aa73241e.chunk.js.map": "/.blocklet/proxy/blocklet-service/static/js/5.aa73241e.chunk.js.map",
14
+ "static/js/5.b46d3b80.chunk.js": "/.blocklet/proxy/blocklet-service/static/js/5.b46d3b80.chunk.js",
15
+ "static/js/5.b46d3b80.chunk.js.map": "/.blocklet/proxy/blocklet-service/static/js/5.b46d3b80.chunk.js.map",
16
16
  "static/js/6.e7e812b2.chunk.js": "/.blocklet/proxy/blocklet-service/static/js/6.e7e812b2.chunk.js",
17
17
  "static/js/6.e7e812b2.chunk.js.map": "/.blocklet/proxy/blocklet-service/static/js/6.e7e812b2.chunk.js.map",
18
18
  "static/js/7.2b69ef90.chunk.js": "/.blocklet/proxy/blocklet-service/static/js/7.2b69ef90.chunk.js",
@@ -33,14 +33,14 @@
33
33
  "static/css/5.4db48a1d.chunk.css.map": "/.blocklet/proxy/blocklet-service/static/css/5.4db48a1d.chunk.css.map",
34
34
  "static/js/0.57b90a04.chunk.js.LICENSE.txt": "/.blocklet/proxy/blocklet-service/static/js/0.57b90a04.chunk.js.LICENSE.txt",
35
35
  "static/js/10.05b0cabe.chunk.js.LICENSE.txt": "/.blocklet/proxy/blocklet-service/static/js/10.05b0cabe.chunk.js.LICENSE.txt",
36
- "static/js/5.aa73241e.chunk.js.LICENSE.txt": "/.blocklet/proxy/blocklet-service/static/js/5.aa73241e.chunk.js.LICENSE.txt",
36
+ "static/js/5.b46d3b80.chunk.js.LICENSE.txt": "/.blocklet/proxy/blocklet-service/static/js/5.b46d3b80.chunk.js.LICENSE.txt",
37
37
  "static/media/400.css": "/.blocklet/proxy/blocklet-service/static/media/ubuntu-mono-latin-ext-400-normal.b56e2315.woff2",
38
38
  "static/media/700.css": "/.blocklet/proxy/blocklet-service/static/media/lato-latin-ext-700-normal.9c8812ea.woff2"
39
39
  },
40
40
  "entrypoints": [
41
41
  "static/js/runtime-main.38ffe325.js",
42
42
  "static/css/5.4db48a1d.chunk.css",
43
- "static/js/5.aa73241e.chunk.js",
43
+ "static/js/5.b46d3b80.chunk.js",
44
44
  "static/js/main.fabbd67d.chunk.js"
45
45
  ]
46
46
  }
package/build/index.html CHANGED
@@ -1 +1 @@
1
- <!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="shortcut icon" href="/.well-known/service/static/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=0"/><meta name="theme-color" content="#000000"/><title>Blocklet Service</title><script src=".well-known/service/api/env"></script><link href="/.blocklet/proxy/blocklet-service/static/css/5.4db48a1d.chunk.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div><script>!function(e){function t(t){for(var n,u,i=t[0],l=t[1],f=t[2],s=t[3]||[],d=0,b=[];d<i.length;d++)u=i[d],Object.prototype.hasOwnProperty.call(o,u)&&o[u]&&b.push(o[u][0]),o[u]=0;for(n in l)Object.prototype.hasOwnProperty.call(l,n)&&(e[n]=l[n]);for(p&&p(t),a.push.apply(a,s);b.length;)b.shift()();return c.push.apply(c,f||[]),r()}function r(){for(var e,t=0;t<c.length;t++){for(var r=c[t],n=!0,l=1;l<r.length;l++){var f=r[l];0!==o[f]&&(n=!1)}n&&(c.splice(t--,1),e=i(i.s=r[0]))}return 0===c.length&&(a.forEach((function(e){if(void 0===o[e]){o[e]=null;var t=document.createElement("link");i.nc&&t.setAttribute("nonce",i.nc),t.rel="prefetch",t.as="script",t.href=u(e),document.head.appendChild(t)}})),a.length=0),e}var n={},o={4:0},c=[],a=[];function u(e){return i.p+"static/js/"+({}[e]||e)+"."+{0:"57b90a04",1:"e079dead",2:"41c31915",6:"e7e812b2",7:"2b69ef90",8:"e7518c81",9:"b881ef87",10:"05b0cabe",11:"b924a273",12:"3d92f243",13:"e22c27bc"}[e]+".chunk.js"}function i(t){if(n[t])return n[t].exports;var r=n[t]={i:t,l:!1,exports:{}};return e[t].call(r.exports,r,r.exports,i),r.l=!0,r.exports}i.e=function(e){var t=[],r=o[e];if(0!==r)if(r)t.push(r[2]);else{var n=new Promise((function(t,n){r=o[e]=[t,n]}));t.push(r[2]=n);var c,a=document.createElement("script");a.charset="utf-8",a.timeout=120,i.nc&&a.setAttribute("nonce",i.nc),a.src=u(e);var l=new Error;c=function(t){a.onerror=a.onload=null,clearTimeout(f);var r=o[e];if(0!==r){if(r){var n=t&&("load"===t.type?"missing":t.type),c=t&&t.target&&t.target.src;l.message="Loading chunk "+e+" failed.\n("+n+": "+c+")",l.name="ChunkLoadError",l.type=n,l.request=c,r[1](l)}o[e]=void 0}};var f=setTimeout((function(){c({type:"timeout",target:a})}),12e4);a.onerror=a.onload=c,document.head.appendChild(a)}return Promise.all(t)},i.m=e,i.c=n,i.d=function(e,t,r){i.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},i.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},i.t=function(e,t){if(1&t&&(e=i(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(i.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var n in e)i.d(r,n,function(t){return e[t]}.bind(null,n));return r},i.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return i.d(t,"a",t),t},i.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},i.p="/.blocklet/proxy/blocklet-service/",i.oe=function(e){throw console.error(e),e};var l=this["webpackJsonp@abtnode/blocklet-services"]=this["webpackJsonp@abtnode/blocklet-services"]||[],f=l.push.bind(l);l.push=t,l=l.slice();for(var s=0;s<l.length;s++)t(l[s]);var p=f;r()}([])</script><script src="/.blocklet/proxy/blocklet-service/static/js/5.aa73241e.chunk.js"></script><script src="/.blocklet/proxy/blocklet-service/static/js/main.fabbd67d.chunk.js"></script></body></html>
1
+ <!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="shortcut icon" href="/.well-known/service/static/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=0"/><meta name="theme-color" content="#000000"/><title>Blocklet Service</title><script src=".well-known/service/api/env"></script><link href="/.blocklet/proxy/blocklet-service/static/css/5.4db48a1d.chunk.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div><script>!function(e){function t(t){for(var n,u,i=t[0],l=t[1],f=t[2],s=t[3]||[],d=0,b=[];d<i.length;d++)u=i[d],Object.prototype.hasOwnProperty.call(o,u)&&o[u]&&b.push(o[u][0]),o[u]=0;for(n in l)Object.prototype.hasOwnProperty.call(l,n)&&(e[n]=l[n]);for(p&&p(t),a.push.apply(a,s);b.length;)b.shift()();return c.push.apply(c,f||[]),r()}function r(){for(var e,t=0;t<c.length;t++){for(var r=c[t],n=!0,l=1;l<r.length;l++){var f=r[l];0!==o[f]&&(n=!1)}n&&(c.splice(t--,1),e=i(i.s=r[0]))}return 0===c.length&&(a.forEach((function(e){if(void 0===o[e]){o[e]=null;var t=document.createElement("link");i.nc&&t.setAttribute("nonce",i.nc),t.rel="prefetch",t.as="script",t.href=u(e),document.head.appendChild(t)}})),a.length=0),e}var n={},o={4:0},c=[],a=[];function u(e){return i.p+"static/js/"+({}[e]||e)+"."+{0:"57b90a04",1:"e079dead",2:"41c31915",6:"e7e812b2",7:"2b69ef90",8:"e7518c81",9:"b881ef87",10:"05b0cabe",11:"b924a273",12:"3d92f243",13:"e22c27bc"}[e]+".chunk.js"}function i(t){if(n[t])return n[t].exports;var r=n[t]={i:t,l:!1,exports:{}};return e[t].call(r.exports,r,r.exports,i),r.l=!0,r.exports}i.e=function(e){var t=[],r=o[e];if(0!==r)if(r)t.push(r[2]);else{var n=new Promise((function(t,n){r=o[e]=[t,n]}));t.push(r[2]=n);var c,a=document.createElement("script");a.charset="utf-8",a.timeout=120,i.nc&&a.setAttribute("nonce",i.nc),a.src=u(e);var l=new Error;c=function(t){a.onerror=a.onload=null,clearTimeout(f);var r=o[e];if(0!==r){if(r){var n=t&&("load"===t.type?"missing":t.type),c=t&&t.target&&t.target.src;l.message="Loading chunk "+e+" failed.\n("+n+": "+c+")",l.name="ChunkLoadError",l.type=n,l.request=c,r[1](l)}o[e]=void 0}};var f=setTimeout((function(){c({type:"timeout",target:a})}),12e4);a.onerror=a.onload=c,document.head.appendChild(a)}return Promise.all(t)},i.m=e,i.c=n,i.d=function(e,t,r){i.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},i.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},i.t=function(e,t){if(1&t&&(e=i(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(i.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var n in e)i.d(r,n,function(t){return e[t]}.bind(null,n));return r},i.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return i.d(t,"a",t),t},i.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},i.p="/.blocklet/proxy/blocklet-service/",i.oe=function(e){throw console.error(e),e};var l=this["webpackJsonp@abtnode/blocklet-services"]=this["webpackJsonp@abtnode/blocklet-services"]||[],f=l.push.bind(l);l.push=t,l=l.slice();for(var s=0;s<l.length;s++)t(l[s]);var p=f;r()}([])</script><script src="/.blocklet/proxy/blocklet-service/static/js/5.b46d3b80.chunk.js"></script><script src="/.blocklet/proxy/blocklet-service/static/js/main.fabbd67d.chunk.js"></script></body></html>