@blocklet/sdk 1.16.14-beta-a90ba909 → 1.16.14-beta-c2843ec5

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/lib/config.d.ts CHANGED
@@ -45,7 +45,8 @@ declare const handleComponentUpdate: (data: {
45
45
  components: TComponents;
46
46
  }) => void;
47
47
  declare const handleConfigUpdate: (data: any) => void;
48
- export { logger, env, components, MountPoint, events, handleComponentUpdate, handleConfigUpdate };
48
+ declare const getBlockletJs: (pageGroup?: string, pathPrefix?: string, source?: string) => string;
49
+ export { logger, env, components, MountPoint, events, getBlockletJs, handleComponentUpdate, handleConfigUpdate };
49
50
  declare const _default: {
50
51
  logger: {
51
52
  info: {
@@ -87,5 +88,7 @@ declare const _default: {
87
88
  components: TComponents;
88
89
  }) => void;
89
90
  handleConfigUpdate: (data: any) => void;
91
+ fetchBlockletJs: () => Promise<any>;
92
+ getBlockletJs: (pageGroup?: string, pathPrefix?: string, source?: string) => string;
90
93
  };
91
94
  export default _default;
package/lib/config.js CHANGED
@@ -3,13 +3,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.handleConfigUpdate = exports.handleComponentUpdate = exports.events = exports.components = exports.env = exports.logger = void 0;
6
+ exports.handleConfigUpdate = exports.handleComponentUpdate = exports.getBlockletJs = exports.events = exports.components = exports.env = exports.logger = void 0;
7
+ const url_join_1 = __importDefault(require("url-join"));
7
8
  const events_1 = __importDefault(require("events"));
8
9
  const cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
10
+ const throttle_1 = __importDefault(require("lodash/throttle"));
9
11
  const env_1 = __importDefault(require("@blocklet/env"));
10
12
  const constant_1 = require("@blocklet/constant");
11
13
  const util_1 = require("@blocklet/env/lib/util");
12
14
  const util_2 = require("@blocklet/meta/lib/util");
15
+ const axios_1 = __importDefault(require("./util/axios"));
16
+ const version_1 = require("./version");
13
17
  const notification_1 = __importDefault(require("./service/notification"));
14
18
  const events = new events_1.default();
15
19
  exports.events = events;
@@ -28,7 +32,7 @@ const AppConfigKeyMap = {
28
32
  };
29
33
  const logger = {
30
34
  // eslint-disable-next-line no-console
31
- info: console.log,
35
+ info: console.info,
32
36
  debug: (...args) => {
33
37
  if (process.env.NODE_ENV === 'development') {
34
38
  // eslint-disable-next-line no-console
@@ -83,10 +87,53 @@ const handleConfigUpdate = (data) => {
83
87
  events.emit(Events.envUpdate, updates);
84
88
  };
85
89
  exports.handleConfigUpdate = handleConfigUpdate;
90
+ let blockletJs = '';
91
+ const fetchBlockletJs = async () => {
92
+ try {
93
+ const componentDid = process.env.BLOCKLET_COMPONENT_DID;
94
+ const { mountPoint } = components.find((x) => x.did === componentDid);
95
+ const res = await axios_1.default.get((0, url_join_1.default)(env.appUrl, mountPoint === '/' ? '' : mountPoint, `__blocklet__.js?t=${Date.now()}`), {
96
+ timeout: 8000,
97
+ headers: {
98
+ 'User-Agent': `BlockletSDK/${version_1.version}`,
99
+ },
100
+ });
101
+ blockletJs = res.data;
102
+ // eslint-disable-next-line no-console
103
+ logger.info('Fetch blocklet.js succeed', { componentDid });
104
+ return res.data;
105
+ }
106
+ catch (err) {
107
+ logger.error('Failed to fetch blocklet.js', err.message);
108
+ return null;
109
+ }
110
+ };
111
+ // Page group is dynamic, so we need to create it on the fly
112
+ const normalize = (prefix) => `/${prefix}/`.replace(/\/+/g, '/');
113
+ const getBlockletJs = (pageGroup = '', pathPrefix = '', source = blockletJs) => {
114
+ let copy = source;
115
+ if (pageGroup) {
116
+ copy = copy.replace('pageGroup: "",', `pageGroup: "${pageGroup}",`);
117
+ }
118
+ if (pathPrefix) {
119
+ const componentDid = process.env.BLOCKLET_COMPONENT_DID;
120
+ const { mountPoint } = components.find((x) => x.did === componentDid);
121
+ copy = copy.replace(`prefix: "${normalize(mountPoint)}",`, `prefix: "${normalize(pathPrefix)}",`);
122
+ }
123
+ return copy;
124
+ };
125
+ exports.getBlockletJs = getBlockletJs;
126
+ const refreshBlockletJs = (0, throttle_1.default)(fetchBlockletJs, 8000);
86
127
  const inRuntimeEnv = !!process.env.BLOCKLET_APP_SK;
87
128
  if (inRuntimeEnv && !process.env.BLOCKLET_HOOK_NAME) {
88
129
  notification_1.default.on(constant_1.BlockletInternalEvents.componentsUpdated, handleComponentUpdate);
89
130
  notification_1.default.on(constant_1.BlockletInternalEvents.appConfigChanged, handleConfigUpdate);
131
+ // Reactive fetch
132
+ notification_1.default.on(constant_1.BlockletInternalEvents.componentsUpdated, refreshBlockletJs);
133
+ notification_1.default.on(constant_1.BlockletInternalEvents.appConfigChanged, refreshBlockletJs);
134
+ notification_1.default.on(constant_1.BlockletInternalEvents.appSettingChanged, refreshBlockletJs);
135
+ // Do an initial fetch
136
+ fetchBlockletJs();
90
137
  }
91
138
  exports.default = {
92
139
  logger,
@@ -95,4 +142,6 @@ exports.default = {
95
142
  events,
96
143
  handleComponentUpdate,
97
144
  handleConfigUpdate,
145
+ fetchBlockletJs,
146
+ getBlockletJs,
98
147
  };
@@ -0,0 +1,8 @@
1
+ import { NextFunction, Request, Response } from 'express';
2
+ type FallbackOptions = {
3
+ cacheControl?: boolean | undefined;
4
+ maxAge?: string | number | undefined;
5
+ root?: string | undefined;
6
+ };
7
+ declare const fallback: (file: string, options?: FallbackOptions) => (req: Request, res: Response, next: NextFunction) => void;
8
+ export = fallback;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ const fs_1 = require("fs");
3
+ const path_1 = require("path");
4
+ const config_1 = require("../config");
5
+ const fallback = (file, options = {}) => {
6
+ const filePath = options.root ? (0, path_1.join)(options.root, file) : file;
7
+ if ((0, fs_1.existsSync)(filePath) === false) {
8
+ throw new Error(`Fallback file not found at: ${filePath}`);
9
+ }
10
+ const source = (0, fs_1.readFileSync)(filePath).toString();
11
+ return (req, res, next) => {
12
+ if ((req.method === 'GET' || req.method === 'HEAD') && req.accepts('html')) {
13
+ res.type('html');
14
+ const pageGroup = req.headers['x-page-group'] || '';
15
+ const blockletJs = (0, config_1.getBlockletJs)(pageGroup);
16
+ if (blockletJs) {
17
+ res.send(source
18
+ .replace('<script src="__blocklet__.js"></script>', `<script>${blockletJs}</script>`)
19
+ .replace('<script src="__meta__.js"></script>', `<script>${blockletJs}</script>`));
20
+ }
21
+ else {
22
+ res.send(source);
23
+ }
24
+ }
25
+ else {
26
+ next();
27
+ }
28
+ };
29
+ };
30
+ module.exports = fallback;
@@ -1,14 +1,17 @@
1
1
  import user from './user';
2
2
  import auth from './auth';
3
3
  import component from './component';
4
+ import fallback from './fallback';
4
5
  export { user };
5
6
  export { auth };
6
7
  export { component };
8
+ export { fallback };
7
9
  declare const _default: {
8
10
  user: () => (req: import("express").Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>> & {
9
11
  user?: {
10
12
  did: string;
11
13
  role: string;
14
+ provider: string;
12
15
  fullName: string;
13
16
  };
14
17
  }, res: import("express").Response<any, Record<string, any>>, next: import("express").NextFunction) => void;
@@ -20,5 +23,10 @@ declare const _default: {
20
23
  component: {
21
24
  verifySig: (req: import("express").Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>, res: import("express").Response<any, Record<string, any>>, next: import("express").NextFunction) => void | import("express").Response<any, Record<string, any>>;
22
25
  };
26
+ fallback: (file: string, options?: {
27
+ cacheControl?: boolean;
28
+ maxAge?: string | number;
29
+ root?: string;
30
+ }) => (req: import("express").Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>, res: import("express").Response<any, Record<string, any>>, next: import("express").NextFunction) => void;
23
31
  };
24
32
  export default _default;
@@ -3,15 +3,18 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.component = exports.auth = exports.user = void 0;
6
+ exports.fallback = exports.component = exports.auth = exports.user = void 0;
7
7
  const user_1 = __importDefault(require("./user"));
8
8
  exports.user = user_1.default;
9
9
  const auth_1 = __importDefault(require("./auth"));
10
10
  exports.auth = auth_1.default;
11
11
  const component_1 = __importDefault(require("./component"));
12
12
  exports.component = component_1.default;
13
+ const fallback_1 = __importDefault(require("./fallback"));
14
+ exports.fallback = fallback_1.default;
13
15
  exports.default = {
14
16
  user: user_1.default,
15
17
  auth: auth_1.default,
16
18
  component: component_1.default,
19
+ fallback: fallback_1.default,
17
20
  };
@@ -2,6 +2,7 @@ import { NextFunction, Request, Response } from 'express';
2
2
  type User = {
3
3
  did: string;
4
4
  role: string | undefined;
5
+ provider: string;
5
6
  fullName: string;
6
7
  };
7
8
  declare const userMiddleware: () => (req: Request & {
@@ -4,6 +4,7 @@ const userMiddleware = () => (req, res, next) => {
4
4
  req.user = {
5
5
  did: req.headers['x-user-did'],
6
6
  role: req.headers['x-user-role'],
7
+ provider: req.headers['x-user-provider'],
7
8
  fullName: decodeURIComponent(req.headers['x-user-fullname']),
8
9
  };
9
10
  }
@@ -153,7 +153,8 @@ const initClient = () => {
153
153
  });
154
154
  }
155
155
  });
156
- [constant_1.BlockletInternalEvents.componentsUpdated, constant_1.BlockletInternalEvents.appConfigChanged].forEach((event) => {
156
+ Object.keys(constant_1.BlockletInternalEvents).forEach((key) => {
157
+ const event = constant_1.BlockletInternalEvents[key];
157
158
  messageChannel.on(event, ({ status, response } = {}) => {
158
159
  if (status === 'ok') {
159
160
  const { data, sender } = response;
@@ -0,0 +1,2 @@
1
+ declare const axios: any;
2
+ export = axios;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ const Axios = require('axios');
3
+ const http = require('http');
4
+ const https = require('https');
5
+ // Force all requests routed to local host
6
+ const getAgent = (type) => {
7
+ const agent = { http, https }[type].Agent();
8
+ agent.originCreateConnection = agent.createConnection;
9
+ agent.createConnection = (options, callback) => {
10
+ options.lookup = (hostname, opts, cb) => {
11
+ const host = {
12
+ address: '127.0.0.1',
13
+ family: 4,
14
+ };
15
+ return cb(null, host.address, host.family);
16
+ };
17
+ return agent.originCreateConnection(options, callback);
18
+ };
19
+ return agent;
20
+ };
21
+ const axios = Axios.create({
22
+ proxy: false,
23
+ httpAgent: getAgent('http'),
24
+ httpsAgent: getAgent('https'),
25
+ });
26
+ module.exports = axios;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.16.14-beta-a90ba909",
6
+ "version": "1.16.14-beta-c2843ec5",
7
7
  "description": "graphql client to read/write data on abt node",
8
8
  "main": "lib/index.js",
9
9
  "typings": "lib/index.d.ts",
@@ -26,15 +26,15 @@
26
26
  "author": "linchen1987 <linchen.1987@foxmail.com> (http://github.com/linchen1987)",
27
27
  "license": "Apache-2.0",
28
28
  "dependencies": {
29
- "@abtnode/client": "1.16.14-beta-a90ba909",
30
- "@abtnode/constant": "1.16.14-beta-a90ba909",
29
+ "@abtnode/client": "1.16.14-beta-c2843ec5",
30
+ "@abtnode/constant": "1.16.14-beta-c2843ec5",
31
31
  "@arcblock/did": "1.18.87",
32
32
  "@arcblock/did-auth": "1.18.87",
33
33
  "@arcblock/jwt": "1.18.87",
34
34
  "@arcblock/ws": "1.18.87",
35
- "@blocklet/constant": "1.16.14-beta-a90ba909",
36
- "@blocklet/env": "1.16.14-beta-a90ba909",
37
- "@blocklet/meta": "1.16.14-beta-a90ba909",
35
+ "@blocklet/constant": "1.16.14-beta-c2843ec5",
36
+ "@blocklet/env": "1.16.14-beta-c2843ec5",
37
+ "@blocklet/meta": "1.16.14-beta-c2843ec5",
38
38
  "@did-connect/authenticator": "^2.2.0",
39
39
  "@did-connect/handler": "^2.2.0",
40
40
  "@nedb/core": "^2.1.5",
@@ -73,5 +73,5 @@
73
73
  "ts-node": "^10.9.1",
74
74
  "typescript": "^5.0.4"
75
75
  },
76
- "gitHead": "a59c9970f447462db0ccfee75b277e98bb11a6fa"
76
+ "gitHead": "968bfad93779a21dd1374eede3ac7d4d6d40822b"
77
77
  }