@abtnode/webapp 1.8.63-beta-c51e554d → 1.8.63-beta-b0b23c2b

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.
Files changed (199) hide show
  1. package/api/gql/config.js +392 -0
  2. package/api/gql/index.js +102 -0
  3. package/api/gql/middlewares/create-audit-log.js +3 -0
  4. package/api/gql/middlewares/get-blocklet-list.js +14 -0
  5. package/api/gql/middlewares/install-blocklet.js +13 -0
  6. package/api/gql/middlewares/verify-blocklet.js +21 -0
  7. package/api/gql/middlewares/verify-team.js +9 -0
  8. package/api/index.js +249 -0
  9. package/api/libs/auth.js +78 -0
  10. package/api/libs/env.js +3 -0
  11. package/api/libs/login.js +212 -0
  12. package/api/libs/security.js +90 -0
  13. package/api/libs/storage.js +69 -0
  14. package/api/middlewares/mutate-blocklet-permission.js +18 -0
  15. package/api/routes/auth/accept-server.js +28 -0
  16. package/api/routes/auth/connect-owner.js +143 -0
  17. package/api/routes/auth/delegate-transfer-owner-nft.js +86 -0
  18. package/api/routes/auth/invite.js +93 -0
  19. package/api/routes/auth/issue-passport.js +61 -0
  20. package/api/routes/auth/launch-free-blocklet-by-nft.js +9 -0
  21. package/api/routes/auth/launch-free-blocklet.js +9 -0
  22. package/api/routes/auth/launch-paid-blocklet-by-nft.js +9 -0
  23. package/api/routes/auth/launch-paid-blocklet.js +9 -0
  24. package/api/routes/auth/login.js +63 -0
  25. package/api/routes/auth/lost-passport-issue.js +5 -0
  26. package/api/routes/auth/lost-passport-list.js +5 -0
  27. package/api/routes/auth/switch-passport.js +47 -0
  28. package/api/routes/auth/switch-profile.js +69 -0
  29. package/api/routes/auth/util.js +135 -0
  30. package/api/routes/auth/verify-owner.js +39 -0
  31. package/api/routes/auth/verify-purchase.js +185 -0
  32. package/api/routes/blocklet-info.js +246 -0
  33. package/api/routes/blocklet-preference.js +161 -0
  34. package/api/routes/blocklet-proxy.js +220 -0
  35. package/api/routes/did-resolver.js +38 -0
  36. package/api/routes/dns-resolver.js +73 -0
  37. package/api/routes/log.js +31 -0
  38. package/api/routes/passport.js +19 -0
  39. package/api/routes/session.js +61 -0
  40. package/api/routes/user.js +38 -0
  41. package/api/util/find-routing-rule.js +95 -0
  42. package/api/util/get-configs.js +31 -0
  43. package/api/util/index.js +5 -0
  44. package/api/util/navigation.js +84 -0
  45. package/api/webpack.blocklet.js +43 -0
  46. package/api/ws/index.js +146 -0
  47. package/blocklet.js +1 -1
  48. package/blocklet.yml +1 -1
  49. package/package.json +1 -1
  50. package/build/asset-manifest.json +0 -93
  51. package/build/favicon.ico +0 -0
  52. package/build/icons/css/all.css +0 -5919
  53. package/build/icons/css/brands.css +0 -14
  54. package/build/icons/css/brands.min.css +0 -12
  55. package/build/icons/css/fontawesome.css +0 -7816
  56. package/build/icons/css/fontawesome.min.css +0 -5864
  57. package/build/icons/css/light.css +0 -15
  58. package/build/icons/css/light.min.css +0 -13
  59. package/build/icons/webfonts/fa-brands-400.eot +0 -0
  60. package/build/icons/webfonts/fa-brands-400.svg +0 -1256
  61. package/build/icons/webfonts/fa-brands-400.ttf +0 -0
  62. package/build/icons/webfonts/fa-brands-400.woff +0 -0
  63. package/build/icons/webfonts/fa-brands-400.woff2 +0 -0
  64. package/build/icons/webfonts/fa-light-300.eot +0 -0
  65. package/build/icons/webfonts/fa-light-300.svg +0 -4445
  66. package/build/icons/webfonts/fa-light-300.ttf +0 -0
  67. package/build/icons/webfonts/fa-light-300.woff +0 -0
  68. package/build/icons/webfonts/fa-light-300.woff2 +0 -0
  69. package/build/images/blocklet.png +0 -0
  70. package/build/images/blocklets-00c2c4.png +0 -0
  71. package/build/images/blocklets-222222.png +0 -0
  72. package/build/images/celebrate.png +0 -0
  73. package/build/images/community.svg +0 -6
  74. package/build/images/console-00c2c4.png +0 -0
  75. package/build/images/console-222222.png +0 -0
  76. package/build/images/dashboard-00c2c4.png +0 -0
  77. package/build/images/dashboard-222222.png +0 -0
  78. package/build/images/doc-222222.png +0 -0
  79. package/build/images/hourglass-222222.png +0 -0
  80. package/build/images/https-certificate-icon.png +0 -0
  81. package/build/images/labs-00c2c4.png +0 -0
  82. package/build/images/labs-222222.png +0 -0
  83. package/build/images/log-00c2c4.png +0 -0
  84. package/build/images/log-222222.png +0 -0
  85. package/build/images/member-222222.png +0 -0
  86. package/build/images/node.png +0 -0
  87. package/build/images/official.svg +0 -1
  88. package/build/images/private-222222.png +0 -0
  89. package/build/images/public-222222.png +0 -0
  90. package/build/images/public_avatar.png +0 -0
  91. package/build/images/router-00c2c4.png +0 -0
  92. package/build/images/router-222222.png +0 -0
  93. package/build/images/settings-00c2c4.png +0 -0
  94. package/build/images/settings-222222.png +0 -0
  95. package/build/images/slack.png +0 -0
  96. package/build/images/storage-222222.png +0 -0
  97. package/build/images/store-00c2c4.png +0 -0
  98. package/build/images/store-222222.png +0 -0
  99. package/build/images/team-00c2c4.png +0 -0
  100. package/build/images/team-222222.png +0 -0
  101. package/build/images/time-222222.png +0 -0
  102. package/build/images/upgrade.png +0 -0
  103. package/build/index.html +0 -1
  104. package/build/manifest.json +0 -15
  105. package/build/static/css/4381.5ade355d.chunk.css +0 -1
  106. package/build/static/css/4691.56c2f951.chunk.css +0 -1
  107. package/build/static/css/5245.5d6e9197.chunk.css +0 -1
  108. package/build/static/css/8213.f696fdbf.chunk.css +0 -1
  109. package/build/static/css/9779.f696fdbf.chunk.css +0 -1
  110. package/build/static/css/main.c9e90622.css +0 -1
  111. package/build/static/images/logo.png +0 -0
  112. package/build/static/js/1057.8280393f.chunk.js +0 -1
  113. package/build/static/js/1361.85e46159.chunk.js +0 -1
  114. package/build/static/js/1366.2f174c9b.chunk.js +0 -2
  115. package/build/static/js/1366.2f174c9b.chunk.js.LICENSE.txt +0 -37
  116. package/build/static/js/154.94956824.chunk.js +0 -1
  117. package/build/static/js/1826.07c285de.chunk.js +0 -1
  118. package/build/static/js/2129.120e36ae.chunk.js +0 -1
  119. package/build/static/js/2308.b65bc299.chunk.js +0 -1
  120. package/build/static/js/2564.eef717f3.chunk.js +0 -1
  121. package/build/static/js/2657.d3c62d57.chunk.js +0 -1
  122. package/build/static/js/2720.26eb1788.chunk.js +0 -1
  123. package/build/static/js/2828.4f318970.chunk.js +0 -1
  124. package/build/static/js/2878.5626558d.chunk.js +0 -1
  125. package/build/static/js/3111.84e02823.chunk.js +0 -1
  126. package/build/static/js/3476.c0b3411b.chunk.js +0 -1
  127. package/build/static/js/3633.8c7cf02d.chunk.js +0 -1
  128. package/build/static/js/3732.f5d8b39b.chunk.js +0 -1
  129. package/build/static/js/3776.1490b0f7.chunk.js +0 -1
  130. package/build/static/js/3802.8ecd1e74.chunk.js +0 -2
  131. package/build/static/js/3802.8ecd1e74.chunk.js.LICENSE.txt +0 -194
  132. package/build/static/js/3829.b6d6cf45.chunk.js +0 -1
  133. package/build/static/js/4074.19db40ec.chunk.js +0 -1
  134. package/build/static/js/4104.b47ae0e1.chunk.js +0 -1
  135. package/build/static/js/4241.83e4f741.chunk.js +0 -1
  136. package/build/static/js/4274.e18ffddd.chunk.js +0 -1
  137. package/build/static/js/4349.7c2f6507.chunk.js +0 -1
  138. package/build/static/js/4381.631ef61d.chunk.js +0 -2
  139. package/build/static/js/4381.631ef61d.chunk.js.LICENSE.txt +0 -22
  140. package/build/static/js/4557.3907550e.chunk.js +0 -1
  141. package/build/static/js/4590.5a14012c.chunk.js +0 -1
  142. package/build/static/js/4633.9f946ed5.chunk.js +0 -1
  143. package/build/static/js/4691.550fcb40.chunk.js +0 -2
  144. package/build/static/js/4691.550fcb40.chunk.js.LICENSE.txt +0 -8
  145. package/build/static/js/5074.9ea094a9.chunk.js +0 -1
  146. package/build/static/js/5082.18d16cb9.chunk.js +0 -1
  147. package/build/static/js/519.adae6e43.chunk.js +0 -1
  148. package/build/static/js/556.381dcbd8.chunk.js +0 -1
  149. package/build/static/js/5984.5ed1e6ee.chunk.js +0 -1
  150. package/build/static/js/6158.8a43296d.chunk.js +0 -1
  151. package/build/static/js/6189.d6d74d14.chunk.js +0 -1
  152. package/build/static/js/6239.5620b031.chunk.js +0 -2
  153. package/build/static/js/6239.5620b031.chunk.js.LICENSE.txt +0 -14
  154. package/build/static/js/629.1a7f2553.chunk.js +0 -1
  155. package/build/static/js/6413.ab4a8e01.chunk.js +0 -1
  156. package/build/static/js/6602.c417a217.chunk.js +0 -1
  157. package/build/static/js/6780.64314507.chunk.js +0 -1
  158. package/build/static/js/6830.88b993a0.chunk.js +0 -1
  159. package/build/static/js/6866.2131ea60.chunk.js +0 -2
  160. package/build/static/js/6866.2131ea60.chunk.js.LICENSE.txt +0 -8
  161. package/build/static/js/6898.6fe0c32d.chunk.js +0 -2
  162. package/build/static/js/6898.6fe0c32d.chunk.js.LICENSE.txt +0 -5
  163. package/build/static/js/7016.f26d73d0.chunk.js +0 -1
  164. package/build/static/js/7298.a7286c09.chunk.js +0 -1
  165. package/build/static/js/7327.56a5e016.chunk.js +0 -1
  166. package/build/static/js/744.8b656dd2.chunk.js +0 -1
  167. package/build/static/js/7652.722b9180.chunk.js +0 -1
  168. package/build/static/js/8034.c4dcdeff.chunk.js +0 -1
  169. package/build/static/js/8058.0b2d4727.chunk.js +0 -1
  170. package/build/static/js/8213.fe07b958.chunk.js +0 -1
  171. package/build/static/js/8235.add24931.chunk.js +0 -1
  172. package/build/static/js/8249.63c55b3a.chunk.js +0 -1
  173. package/build/static/js/8262.869cc799.chunk.js +0 -1
  174. package/build/static/js/8352.b57b0e44.chunk.js +0 -1
  175. package/build/static/js/8464.b4b82828.chunk.js +0 -1
  176. package/build/static/js/8606.66db67e2.chunk.js +0 -1
  177. package/build/static/js/9038.2908c176.chunk.js +0 -1
  178. package/build/static/js/9076.db12fce0.chunk.js +0 -1
  179. package/build/static/js/9301.a65db324.chunk.js +0 -2
  180. package/build/static/js/9301.a65db324.chunk.js.LICENSE.txt +0 -8
  181. package/build/static/js/9779.bbf0dd81.chunk.js +0 -1
  182. package/build/static/js/main.8a8e54c6.js +0 -2
  183. package/build/static/js/main.8a8e54c6.js.LICENSE.txt +0 -159
  184. package/build/static/media/iconify.32b54549e843e448ee9b.cjs +0 -2
  185. package/build/static/media/iconify.32b54549e843e448ee9b.cjs.LICENSE.txt +0 -13
  186. package/build/static/media/lato-all-400-normal.3dc1eff492ab1f598560.woff +0 -0
  187. package/build/static/media/lato-all-700-normal.1e7707c9ec98d9b97e7f.woff +0 -0
  188. package/build/static/media/lato-latin-400-normal.be36596da218e1eec01c.woff2 +0 -0
  189. package/build/static/media/lato-latin-700-normal.8f28e0e1fdb195149f1c.woff2 +0 -0
  190. package/build/static/media/lato-latin-ext-400-normal.361f3dbb9db6a5980326.woff2 +0 -0
  191. package/build/static/media/lato-latin-ext-700-normal.9c8812eaec45956201e1.woff2 +0 -0
  192. package/build/static/media/logo.60f66bbe1ce9674a4df4e374c9d97fc4.svg +0 -16
  193. package/build/static/media/ubuntu-mono-all-400-normal.c879328bc62e9c68268f.woff +0 -0
  194. package/build/static/media/ubuntu-mono-cyrillic-400-normal.c367f416832eb8f1b846.woff2 +0 -0
  195. package/build/static/media/ubuntu-mono-cyrillic-ext-400-normal.eda1c4946b1f7bf58386.woff2 +0 -0
  196. package/build/static/media/ubuntu-mono-greek-400-normal.22f3bfc91f79c342bdf4.woff2 +0 -0
  197. package/build/static/media/ubuntu-mono-greek-ext-400-normal.b3459900ea8a25d1f7c2.woff2 +0 -0
  198. package/build/static/media/ubuntu-mono-latin-400-normal.18e32d9d743af28f913e.woff2 +0 -0
  199. package/build/static/media/ubuntu-mono-latin-ext-400-normal.b56e2315611d10838ad5.woff2 +0 -0
@@ -0,0 +1,95 @@
1
+ const {
2
+ DOMAIN_FOR_IP_SITE_REGEXP,
3
+ DOMAIN_FOR_DEFAULT_SITE,
4
+ ROUTING_RULE_TYPES,
5
+ SLOT_FOR_IP_DNS_SITE,
6
+ } = require('@abtnode/constant');
7
+ const checkDomainMatch = require('@abtnode/util/lib/check-domain-match');
8
+
9
+ const LOCALHOST = 'localhost';
10
+
11
+ const findSite = (sites, req) => {
12
+ const site = sites.find(x => {
13
+ if (x.domain === DOMAIN_FOR_IP_SITE_REGEXP || x.domain === DOMAIN_FOR_DEFAULT_SITE) {
14
+ return false;
15
+ }
16
+ if (checkDomainMatch(req.hostname, x.domain)) {
17
+ return true;
18
+ }
19
+ if ((x.domainAliases || []).some(y => checkDomainMatch(req.hostname, y.value))) {
20
+ return true;
21
+ }
22
+
23
+ const ipDomain = (x.domainAliases || []).find(y => (y.value || '').includes(SLOT_FOR_IP_DNS_SITE));
24
+ if (ipDomain && ipDomain.value) {
25
+ const [startWith, endWith] = ipDomain.value.split(SLOT_FOR_IP_DNS_SITE);
26
+ if (req.hostname.startsWith(startWith) && req.hostname.endsWith(endWith)) {
27
+ return true;
28
+ }
29
+ }
30
+
31
+ if (x.domain.includes(SLOT_FOR_IP_DNS_SITE)) {
32
+ const [startWith, endWith] = x.domain.split(SLOT_FOR_IP_DNS_SITE);
33
+ if (req.hostname.startsWith(startWith) && req.hostname.endsWith(endWith)) {
34
+ return true;
35
+ }
36
+ }
37
+ return false;
38
+ });
39
+ if (site) {
40
+ return site;
41
+ }
42
+
43
+ const dashboardSite = sites.find(x => {
44
+ if (x.domain === DOMAIN_FOR_IP_SITE_REGEXP) {
45
+ if (req.hostname === LOCALHOST) {
46
+ return true;
47
+ }
48
+ // eslint-disable-next-line prefer-regex-literals
49
+ if (new RegExp('^\\d+.\\d+.\\d+.\\d+$').test(req.hostname)) {
50
+ return true;
51
+ }
52
+ }
53
+ return (x.domainAliases || []).some(y => checkDomainMatch(req.hostname, y.value));
54
+ });
55
+ if (dashboardSite) {
56
+ return dashboardSite;
57
+ }
58
+
59
+ const defaultSite = sites.find(x => x.domain === DOMAIN_FOR_DEFAULT_SITE);
60
+ const mergeDashboardSite = sites.find(x => x.domain === DOMAIN_FOR_IP_SITE_REGEXP);
61
+ if (defaultSite) {
62
+ const x = Object.assign({}, defaultSite);
63
+ if (mergeDashboardSite) {
64
+ x.rules = [...x.rules, ...mergeDashboardSite.rules];
65
+ }
66
+ return x;
67
+ }
68
+
69
+ return null;
70
+ };
71
+
72
+ module.exports = function findRoutingRule(sites, req, urlPrefix = '') {
73
+ if (!req || !req.hostname || !req.headers || !req.headers.referer) {
74
+ return null;
75
+ }
76
+
77
+ const site = findSite(sites, req);
78
+
79
+ if (!site) {
80
+ return null;
81
+ }
82
+
83
+ const rules = (site.rules || [])
84
+ .filter(x => x.to.type === ROUTING_RULE_TYPES.BLOCKLET)
85
+ .sort((a, b) => (a.from.pathPrefix.length > b.from.pathPrefix.length ? -1 : 1));
86
+
87
+ const refererPath = new URL(req.headers.referer).pathname;
88
+
89
+ // return root blocklet's rule (usually last in sorted rules) for favicon
90
+ if (req.url.startsWith(`${urlPrefix}/favicon`)) {
91
+ return rules[rules.length - 1];
92
+ }
93
+
94
+ return rules.find(x => refererPath.startsWith(x.from.pathPrefix.replace(/\/+$/, '')));
95
+ };
@@ -0,0 +1,31 @@
1
+ const { forEachBlockletSync, getSharedConfigObj, isEnvShareable } = require('@blocklet/meta/lib/util');
2
+
3
+ /**
4
+ * Get configs of root blocklet or child blocklet
5
+ * @param {*} blocklet
6
+ * @param {*} componentId
7
+ * @returns
8
+ */
9
+ module.exports = (blocklet, componentId) => {
10
+ let res = {};
11
+ forEachBlockletSync(blocklet, (component, { id, ancestors }) => {
12
+ if (id !== componentId) {
13
+ return;
14
+ }
15
+
16
+ const configs = component.configs.filter(x => isEnvShareable(x));
17
+ const configObj = configs.reduce((o, x) => {
18
+ o[x.key] = x.value;
19
+ return o;
20
+ }, {});
21
+
22
+ const sharedObj = getSharedConfigObj(component, ancestors);
23
+ for (const [key, value] of Object.entries(sharedObj)) {
24
+ configObj[key] = value;
25
+ }
26
+
27
+ res = configObj;
28
+ });
29
+
30
+ return Object.entries(res).map(([key, value]) => ({ key, value }));
31
+ };
@@ -0,0 +1,5 @@
1
+ const joinUrl = require('url-join');
2
+
3
+ const getBlockletUrl = (registryUrl, did) => joinUrl(registryUrl, `/api/blocklets/${did}/blocklet.json`);
4
+
5
+ module.exports = { getBlockletUrl };
@@ -0,0 +1,84 @@
1
+ const { WELLKNOWN_SERVICE_PATH_PREFIX } = require('@abtnode/constant');
2
+
3
+ const fillDashboardNavigation = navigation => {
4
+ if (!navigation.some(x => x.id === '/access')) {
5
+ navigation.push({
6
+ id: '/access',
7
+ from: 'tmpl',
8
+ title: {
9
+ en: 'Access',
10
+ zh: '访问控制',
11
+ },
12
+ role: ['owner', 'admin', 'member'],
13
+ section: ['dashboard'],
14
+ link: '',
15
+ items: [
16
+ {
17
+ id: 'member',
18
+ from: 'tmpl',
19
+ title: {
20
+ en: 'Members',
21
+ zh: '成员',
22
+ },
23
+ icon: 'ant-design:user-outlined',
24
+ link: `${WELLKNOWN_SERVICE_PATH_PREFIX}/admin/members`,
25
+ },
26
+ {
27
+ id: 'passport',
28
+ from: 'tmpl',
29
+ title: {
30
+ en: 'Passport',
31
+ zh: '通行证',
32
+ },
33
+ icon: 'icon-park-outline:passport',
34
+ link: `${WELLKNOWN_SERVICE_PATH_PREFIX}/admin/passports`,
35
+ },
36
+ ],
37
+ });
38
+ }
39
+
40
+ if (!navigation.some(x => x.id === '/dashboard')) {
41
+ navigation.push({
42
+ id: '/dashboard',
43
+ from: 'tmpl',
44
+ title: 'Blocklet',
45
+ role: ['owner', 'admin'],
46
+ section: ['dashboard'],
47
+ link: '',
48
+ items: [
49
+ {
50
+ id: 'dashboard',
51
+ from: 'tmpl',
52
+ title: {
53
+ en: 'Dashboard',
54
+ zh: '仪表盘',
55
+ },
56
+ icon: 'ant-design:dashboard-outlined',
57
+ link: `${WELLKNOWN_SERVICE_PATH_PREFIX}/admin`,
58
+ },
59
+ ],
60
+ });
61
+ }
62
+ };
63
+
64
+ const ensureSessionManagerNavigation = navigation => {
65
+ if (!navigation.some(x => (x.section || []).includes('sessionManager'))) {
66
+ navigation.push({
67
+ id: '/sessionManager',
68
+ from: 'tmpl',
69
+ title: {
70
+ en: 'Manage',
71
+ zh: '管理',
72
+ },
73
+ section: ['sessionManager'],
74
+ icon: 'ion:settings-outline',
75
+ link: `${WELLKNOWN_SERVICE_PATH_PREFIX}/admin`,
76
+ role: ['owner', 'admin'],
77
+ });
78
+ }
79
+ };
80
+
81
+ module.exports = {
82
+ fillDashboardNavigation,
83
+ ensureSessionManagerNavigation,
84
+ };
@@ -0,0 +1,43 @@
1
+ const fs = require('fs');
2
+ // eslint-disable-next-line import/no-extraneous-dependencies
3
+ const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
4
+
5
+ const env = process.env.NODE_ENV || 'development';
6
+
7
+ const extraPlugins = [];
8
+ if (process.env.ANALYZE_BUNDLE) {
9
+ extraPlugins.push(new BundleAnalyzerPlugin({ analyzerMode: 'static' }));
10
+ }
11
+
12
+ const extraAliases = {};
13
+ if (process.env.NODE_ENV === 'production') {
14
+ const targetPath = require.resolve('express-graphql/renderGraphiQL');
15
+ const sourcePath = require.resolve('@abtnode/mocks/lib/graphiql');
16
+ fs.unlinkSync(targetPath);
17
+ fs.copyFileSync(sourcePath, targetPath);
18
+ }
19
+
20
+ module.exports = webpack => ({
21
+ optimization: {
22
+ nodeEnv: env, // @link https://github.com/webpack/webpack/issues/7470#issuecomment-394259698
23
+ },
24
+ resolve: {
25
+ alias: {
26
+ axios: require.resolve('axios'),
27
+ debug: require.resolve('debug'),
28
+ jspdf: require.resolve('@abtnode/mocks/lib/jspdf'),
29
+ isobject: require.resolve('@abtnode/mocks/lib/isobject'),
30
+ 'vue-template-compiler': require.resolve('@abtnode/mocks/lib/vue-template-compiler'),
31
+ 'bn.js': require.resolve('bn.js'),
32
+ 'mime-db': require.resolve('mime-db'),
33
+ ...extraAliases,
34
+ },
35
+ },
36
+ plugins: [
37
+ new webpack.DefinePlugin({
38
+ 'process.env.NODE_ENV': JSON.stringify(env),
39
+ 'process.env.TEST_BUILD': JSON.stringify(process.env.TEST_BUILD),
40
+ }),
41
+ ...extraPlugins,
42
+ ],
43
+ });
@@ -0,0 +1,146 @@
1
+ const { WsServer } = require('@arcblock/ws');
2
+ const { types } = require('@ocap/mcrypto');
3
+ const { fromSecretKey, WalletType } = require('@ocap/wallet');
4
+
5
+ const { BlockletEvents } = require('@blocklet/constant');
6
+ const { EVENTS } = require('@abtnode/constant');
7
+ const { createStreamLogManager, getLogFiles } = require('@abtnode/util/lib/log');
8
+
9
+ // eslint-disable-next-line global-require
10
+ const logger = require('@abtnode/logger')(`${require('../../package.json').name}:ws`);
11
+
12
+ const { authWithJwt } = require('../libs/login');
13
+
14
+ logger.debug = () => null;
15
+
16
+ module.exports = function createWebsocketServer(node) {
17
+ const wsServer = new WsServer({
18
+ logger,
19
+ pathname: '/websocket',
20
+ authenticate: (req, cb) => {
21
+ const { searchParams } = new URL(req.url, `http://${req.headers.host || 'unknown'}`);
22
+ const token = searchParams.get('token');
23
+ const timestamp = searchParams.get('timestamp');
24
+ if (!token) {
25
+ cb(new Error('token not found'), null);
26
+ return;
27
+ }
28
+
29
+ // Authentication CLI: who knows the SK of the node
30
+ if (token && timestamp) {
31
+ node.getNodeInfo().then(info => {
32
+ const wallet = fromSecretKey(
33
+ info.sk,
34
+ WalletType({
35
+ role: types.RoleType.ROLE_APPLICATION,
36
+ pk: types.KeyType.ED25519,
37
+ hash: types.HashType.SHA3,
38
+ })
39
+ );
40
+
41
+ if (wallet.verify(timestamp, token)) {
42
+ cb(null, { did: info.did });
43
+ } else {
44
+ cb(new Error('Signature is invalid'), null);
45
+ }
46
+ });
47
+ } else {
48
+ // Authentication web users
49
+ authWithJwt(token, node)
50
+ .then(user => {
51
+ cb(null, { did: user.did });
52
+ })
53
+ .catch(() => {
54
+ cb(new Error('token parse fail'), null);
55
+ });
56
+ }
57
+ },
58
+ hooks: {
59
+ receiveMessage: ({ socket, topic, event, payload }) => {
60
+ wsServer.emit(`${topic}/${event}`, { socket, topic, event, payload });
61
+ logger.info('client pushed socket message', { topic, event });
62
+ },
63
+ },
64
+ });
65
+
66
+ // Publish CLI generated events to web dashboard clients
67
+ const onCLIEvent = ({ event, payload }) => {
68
+ if (event) {
69
+ if (Object.values(BlockletEvents).includes(event) && typeof node.handleBlockletEvent === 'function') {
70
+ node.handleBlockletEvent(event, payload);
71
+ return;
72
+ }
73
+
74
+ if ([EVENTS.NODE_STARTED, EVENTS.NODE_STOPPED].includes(event) && typeof node.handleServerEvent === 'function') {
75
+ node.handleServerEvent(event, payload);
76
+ return;
77
+ }
78
+
79
+ wsServer.broadcast(event, payload);
80
+ }
81
+ };
82
+ wsServer.on('cli/blocklet.installed', onCLIEvent);
83
+ wsServer.on('cli/blocklet.upgraded', onCLIEvent);
84
+ wsServer.on('cli/blocklet.statusChange', onCLIEvent);
85
+ wsServer.on('cli/blocklet.started', onCLIEvent);
86
+ wsServer.on('cli/blocklet.removed', onCLIEvent);
87
+ wsServer.on('cli/blocklet.updated', onCLIEvent);
88
+ wsServer.on('cli/routing.updated', onCLIEvent);
89
+ wsServer.on('cli/node.stopped', onCLIEvent);
90
+ wsServer.on('cli/node.started', onCLIEvent);
91
+ wsServer.on('cli/node.upgrading', onCLIEvent);
92
+
93
+ const streamLogManager = createStreamLogManager({
94
+ onGetLogFiles: name => getLogFiles({ name, node }),
95
+ onLog: ({ topic, level, logFiles, data }) => {
96
+ wsServer.broadcast(topic, topic, { level, logFiles, data }, { enableLog: false });
97
+ },
98
+ });
99
+
100
+ const onChannelEvent = async (eventName, { socket, topic }) => {
101
+ const [type, ...names] = topic.split('.');
102
+ if (type === 'log') {
103
+ const name = names.join('.');
104
+ if (eventName === 'channel.join') {
105
+ streamLogManager.add(name, topic, (level, data, logFiles) => {
106
+ wsServer.send(socket, topic, topic, { level, data, logFiles }, { enableLog: false });
107
+ });
108
+ }
109
+
110
+ if (eventName === 'channel.destroy') {
111
+ streamLogManager.destroy(name);
112
+ }
113
+ }
114
+
115
+ if (topic === EVENTS.NODE_RUNTIME_INFO) {
116
+ if (eventName === 'channel.join') {
117
+ const info = node.getNodeRuntimeInfo();
118
+ wsServer.send(socket, topic, topic, info, { enableLog: false });
119
+ }
120
+ }
121
+ };
122
+
123
+ ['channel.join', 'channel.leave', 'channel.destroy'].forEach(name => {
124
+ wsServer.on(name, payload => onChannelEvent(name, payload));
125
+ });
126
+
127
+ node.setEventHandler(({ name, data }) => {
128
+ if (name === BlockletEvents.removed) {
129
+ // remove blocklet log stream
130
+ try {
131
+ const { did } = data.meta;
132
+ const logName = `blocklet-${did}`;
133
+ logger.info('destroy blocklet log stream', { name: logName });
134
+ streamLogManager.destroy(logName);
135
+ } catch (error) {
136
+ logger.error('destroy blocklet log stream error', { error });
137
+ }
138
+ }
139
+
140
+ if (name) {
141
+ wsServer.broadcast(name, data);
142
+ }
143
+ });
144
+
145
+ return wsServer;
146
+ };