@blocklet/sdk 1.16.34-beta-20241126-104450-d60e25c2 → 1.16.34-beta-20241127-121930-0923dfae

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.
@@ -45,6 +45,7 @@ const constant_1 = require("@blocklet/constant");
45
45
  const config_1 = require("../config");
46
46
  const Util = __importStar(require("./util"));
47
47
  const component_api_1 = __importDefault(require("../util/component-api"));
48
+ const parse_docker_endpoint_1 = require("../util/parse-docker-endpoint");
48
49
  const doCall = async ({ url, headers = {}, ...options }) => {
49
50
  try {
50
51
  const resp = await (0, component_api_1.default)({
@@ -72,7 +73,8 @@ const getComponent = (name) => {
72
73
  };
73
74
  const getComponentWebEndpoint = (keyword) => {
74
75
  const item = getComponent(keyword);
75
- return item ? item.webEndpoint : '';
76
+ const endpoint = item ? item.webEndpoint : '';
77
+ return (0, parse_docker_endpoint_1.parseDockerComponentEndpoint)(endpoint, item);
76
78
  };
77
79
  exports.getComponentWebEndpoint = getComponentWebEndpoint;
78
80
  const call = async ({ name, method = 'POST', path: _path, ...options }) => {
@@ -83,7 +85,7 @@ const call = async ({ name, method = 'POST', path: _path, ...options }) => {
83
85
  if (!component) {
84
86
  throw new Error(`can not find component ${name}`);
85
87
  }
86
- const baseURL = component.webEndpoint;
88
+ const baseURL = (0, parse_docker_endpoint_1.parseDockerComponentEndpoint)(component.webEndpoint, component);
87
89
  if (!baseURL) {
88
90
  throw new Error(`can not find web endpoint for ${name}`);
89
91
  }
@@ -159,7 +161,7 @@ const waitForComponentRunning = async (name, timeout = 30000, interval = 250) =>
159
161
  if (!port) {
160
162
  throw new Error(`can not find port for component ${name}`);
161
163
  }
162
- const result = await (0, wait_port_1.default)({ host: '127.0.0.1', port, timeout, interval });
164
+ const result = await (0, wait_port_1.default)({ host: (0, parse_docker_endpoint_1.parseDockerComponentHost)(component), port, timeout, interval });
163
165
  if (typeof result === 'boolean' && result) {
164
166
  return true;
165
167
  }
package/lib/config.js CHANGED
@@ -23,6 +23,7 @@ const version_1 = require("./version");
23
23
  const notification_1 = __importDefault(require("./service/notification"));
24
24
  const server_version_1 = __importDefault(require("./util/server-version"));
25
25
  const parse_env_file_1 = require("./util/parse-env-file");
26
+ const parse_docker_endpoint_1 = require("./util/parse-docker-endpoint");
26
27
  const debug = (0, debug_1.default)('@blocklet/sdk:config');
27
28
  const events = new events_1.EventEmitter();
28
29
  exports.events = events;
@@ -107,7 +108,7 @@ exports.env = env;
107
108
  const _fillWebEndpoint = (components) => {
108
109
  components.forEach((x) => {
109
110
  if (x.port) {
110
- x.webEndpoint = `http://127.0.0.1:${x.port}`;
111
+ x.webEndpoint = (0, parse_docker_endpoint_1.parseDockerComponentEndpoint)(`http://127.0.0.1:${x.port}`, x);
111
112
  }
112
113
  });
113
114
  };
@@ -138,7 +139,7 @@ const _handleComponentUpdateOld = (data) => {
138
139
  componentStore.splice(0, componentStore.length);
139
140
  componentStore.push(...data.components.map((x) => {
140
141
  if (x.port) {
141
- x.webEndpoint = `http://127.0.0.1:${x.port}`;
142
+ x.webEndpoint = (0, parse_docker_endpoint_1.parseDockerComponentEndpoint)(`http://127.0.0.1:${x.port}`, x);
142
143
  }
143
144
  return x;
144
145
  }));
@@ -19,6 +19,7 @@ const check_blocklet_env_1 = __importDefault(require("../util/check-blocklet-env
19
19
  const version_1 = require("../version");
20
20
  const wallet_2 = __importDefault(require("../wallet"));
21
21
  const service_api_1 = __importDefault(require("../util/service-api"));
22
+ const parse_docker_endpoint_1 = require("../util/parse-docker-endpoint");
22
23
  const { WELLKNOWN_SERVICE_PATH_PREFIX, USER_AVATAR_URL_PREFIX, USER_AVATAR_PATH_PREFIX } = constant_1.default;
23
24
  const VERSION = version_1.version; // version of notification sdk
24
25
  const isNotNullOrUndefined = (x) => ![null, undefined].includes(x);
@@ -38,7 +39,7 @@ const toBuffer = (str) => (str.startsWith('0x') ? Buffer.from(str.slice(2), 'hex
38
39
  class AuthClient extends client_1.default {
39
40
  constructor(httpEndpoint) {
40
41
  (0, check_blocklet_env_1.default)();
41
- super(httpEndpoint || `http://127.0.0.1:${process.env.ABT_NODE_PORT}/api/gql`.trim(), `BlockletSDK/${VERSION}`);
42
+ super(httpEndpoint || `http://${(0, parse_docker_endpoint_1.getServerHost)()}:${process.env.ABT_NODE_PORT}/api/gql`.trim(), `BlockletSDK/${VERSION}`);
42
43
  const wallet = (0, wallet_2.default)();
43
44
  const useNewSigner = !env_1.serverVersion || semver_1.default.gte(env_1.serverVersion, '1.16.7');
44
45
  this.setAuthAccessKey({
@@ -49,6 +49,7 @@ const send_notification_1 = require("../util/send-notification");
49
49
  const constants_1 = require("../util/constants");
50
50
  const wallet_1 = __importStar(require("../wallet"));
51
51
  const notification_1 = require("../validators/notification");
52
+ const parse_docker_endpoint_1 = require("../util/parse-docker-endpoint");
52
53
  const debug = (0, debug_1.default)('@blocklet/sdk:notification');
53
54
  const getSender = () => {
54
55
  const wallet = (0, wallet_1.default)();
@@ -126,7 +127,7 @@ const initClient = () => {
126
127
  const wallet = (0, wallet_1.getPermanentWallet)();
127
128
  const componentDid = process.env.BLOCKLET_COMPONENT_DID;
128
129
  const { address: did, publicKey: pk, secretKey: sk } = wallet;
129
- const url = `ws://127.0.0.1:${process.env.ABT_NODE_SERVICE_PORT}${constants_1.SERVICE_PREFIX}`;
130
+ const url = `ws://${(0, parse_docker_endpoint_1.getServerHost)()}:${process.env.ABT_NODE_SERVICE_PORT}${constants_1.SERVICE_PREFIX}`;
130
131
  const token = () => Jwt.sign(did, sk, {});
131
132
  client = new ws_1.WsClient(url, {
132
133
  heartbeatIntervalMs: 10 * 1000,
@@ -0,0 +1,8 @@
1
+ import { TComponentInternalInfo } from '@blocklet/meta/lib/blocklet';
2
+ type MountPoint = TComponentInternalInfo & {
3
+ webEndpoint?: string;
4
+ };
5
+ export declare function parseDockerComponentHost(component: MountPoint): string;
6
+ export declare function parseDockerComponentEndpoint(endpoint: string, component: MountPoint): string;
7
+ export declare function getServerHost(): string;
8
+ export {};
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseDockerComponentHost = parseDockerComponentHost;
4
+ exports.parseDockerComponentEndpoint = parseDockerComponentEndpoint;
5
+ exports.getServerHost = getServerHost;
6
+ function parseDockerHost(input) {
7
+ return input
8
+ .replace(/[^a-zA-Z0-9_-]/g, '-')
9
+ .replace(/^"|"$/g, '')
10
+ .replace('docker-network-', 'blocklet-')
11
+ .replace(/\\/g, '_')
12
+ .toLocaleLowerCase();
13
+ }
14
+ function parseDockerComponentHost(component) {
15
+ if (process.env.BLOCKLET_DOCKER_NETWORK && process.env.BLOCKLET_DOCKER_NETWORK !== 'host') {
16
+ return parseDockerHost(`${process.env.BLOCKLET_DOCKER_NETWORK}-${component.name || component.did}`);
17
+ }
18
+ return '127.0.0.1';
19
+ }
20
+ function parseDockerComponentEndpoint(endpoint, component) {
21
+ if (process.env.BLOCKLET_DOCKER_NETWORK && process.env.BLOCKLET_DOCKER_NETWORK !== 'host') {
22
+ const url = new URL(endpoint);
23
+ url.host = parseDockerHost(`${process.env.BLOCKLET_DOCKER_NETWORK}-${component.name || component.did}`);
24
+ return url.toString();
25
+ }
26
+ return endpoint;
27
+ }
28
+ function getServerHost() {
29
+ return process.env.BLOCKLET_HOST || '127.0.0.1';
30
+ }
@@ -47,6 +47,7 @@ const ufo_1 = require("ufo");
47
47
  const index_1 = require("../validators/index");
48
48
  const constants_1 = require("./constants");
49
49
  const version_1 = require("../version");
50
+ const parse_docker_endpoint_1 = require("./parse-docker-endpoint");
50
51
  const axios = axios_1.default.create({ proxy: false });
51
52
  const { NODE_MODES } = constant_1.default;
52
53
  const VERSION = version_1.version; // version of notification sdk
@@ -76,7 +77,7 @@ const sendToUser = async (receiver, notification, { appDid, appSk, type }, port
76
77
  await (0, index_1.validateNotification)(notification);
77
78
  }
78
79
  try {
79
- const { data: res } = await axios.post((0, ufo_1.joinURL)(`http://127.0.0.1:${port}`, constants_1.SERVICE_PREFIX, 'api', pathname), {
80
+ const { data: res } = await axios.post((0, ufo_1.joinURL)(`http://${(0, parse_docker_endpoint_1.getServerHost)()}:${port}`, constants_1.SERVICE_PREFIX, 'api', pathname), {
80
81
  apiVersion: VERSION,
81
82
  data: {
82
83
  sender: {
@@ -118,7 +119,7 @@ const sendToAppChannel = async (channel, event, notification, { appDid, appSk },
118
119
  await (0, index_1.validateNotification)(notification);
119
120
  }
120
121
  try {
121
- const { data: res } = await axios.post(`http://127.0.0.1:${port}${constants_1.SERVICE_PREFIX}/api/send-to-app-channel`, {
122
+ const { data: res } = await axios.post(`http://${(0, parse_docker_endpoint_1.getServerHost)()}:${port}${constants_1.SERVICE_PREFIX}/api/send-to-app-channel`, {
122
123
  apiVersion: VERSION,
123
124
  data: {
124
125
  sender: { appDid, token: JWT.sign(appDid, appSk) },
@@ -150,7 +151,7 @@ const sendToRelay = async (topic, event, data, { appDid, appSk }, port = process
150
151
  throw new Error('data is required');
151
152
  }
152
153
  try {
153
- const { data: res } = await axios.post(`http://127.0.0.1:${port}${constants_1.SERVICE_PREFIX}/relay/api/send-to-relay-channel`, {
154
+ const { data: res } = await axios.post(`http://${(0, parse_docker_endpoint_1.getServerHost)()}:${port}${constants_1.SERVICE_PREFIX}/relay/api/send-to-relay-channel`, {
154
155
  apiVersion: VERSION,
155
156
  data: {
156
157
  sender: { appDid, token: JWT.sign(appDid, appSk) },
@@ -12,10 +12,11 @@ const env_1 = require("@blocklet/env");
12
12
  const ufo_1 = require("ufo");
13
13
  const constants_1 = require("./constants");
14
14
  const verify_sign_1 = require("./verify-sign");
15
+ const parse_docker_endpoint_1 = require("./parse-docker-endpoint");
15
16
  const axios = axios_1.default.create({
16
17
  // 为当前机器的内部调用,必须禁止 proxy 配置
17
18
  proxy: false,
18
- baseURL: `http://127.0.0.1:${process.env.ABT_NODE_SERVICE_PORT}${constants_1.SERVICE_PREFIX}`,
19
+ baseURL: `http://${(0, parse_docker_endpoint_1.getServerHost)()}:${process.env.ABT_NODE_SERVICE_PORT}${constants_1.SERVICE_PREFIX}`,
19
20
  // 内部调用,超时时间不用过长
20
21
  timeout: 6 * 1000,
21
22
  headers: {
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.16.34-beta-20241126-104450-d60e25c2",
6
+ "version": "1.16.34-beta-20241127-121930-0923dfae",
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",
@@ -27,21 +27,21 @@
27
27
  "author": "linchen1987 <linchen.1987@foxmail.com> (http://github.com/linchen1987)",
28
28
  "license": "Apache-2.0",
29
29
  "dependencies": {
30
- "@abtnode/client": "1.16.34-beta-20241126-104450-d60e25c2",
31
- "@abtnode/constant": "1.16.34-beta-20241126-104450-d60e25c2",
32
- "@abtnode/util": "1.16.34-beta-20241126-104450-d60e25c2",
33
- "@arcblock/did": "1.18.145",
34
- "@arcblock/did-auth": "1.18.145",
35
- "@arcblock/jwt": "1.18.145",
36
- "@arcblock/ws": "1.18.145",
37
- "@blocklet/constant": "1.16.34-beta-20241126-104450-d60e25c2",
38
- "@blocklet/env": "1.16.34-beta-20241126-104450-d60e25c2",
39
- "@blocklet/meta": "1.16.34-beta-20241126-104450-d60e25c2",
30
+ "@abtnode/client": "1.16.34-beta-20241127-121930-0923dfae",
31
+ "@abtnode/constant": "1.16.34-beta-20241127-121930-0923dfae",
32
+ "@abtnode/util": "1.16.34-beta-20241127-121930-0923dfae",
33
+ "@arcblock/did": "1.18.150",
34
+ "@arcblock/did-auth": "1.18.150",
35
+ "@arcblock/jwt": "1.18.150",
36
+ "@arcblock/ws": "1.18.150",
37
+ "@blocklet/constant": "1.16.34-beta-20241127-121930-0923dfae",
38
+ "@blocklet/env": "1.16.34-beta-20241127-121930-0923dfae",
39
+ "@blocklet/meta": "1.16.34-beta-20241127-121930-0923dfae",
40
40
  "@did-connect/authenticator": "^2.2.4",
41
41
  "@did-connect/handler": "^2.2.4",
42
42
  "@nedb/core": "^2.1.5",
43
- "@ocap/mcrypto": "1.18.145",
44
- "@ocap/wallet": "1.18.145",
43
+ "@ocap/mcrypto": "1.18.150",
44
+ "@ocap/wallet": "1.18.150",
45
45
  "axios": "^1.7.5",
46
46
  "cheerio": "1.0.0-rc.12",
47
47
  "debug": "^4.3.7",
@@ -80,5 +80,5 @@
80
80
  "ts-node": "^10.9.1",
81
81
  "typescript": "^5.6.3"
82
82
  },
83
- "gitHead": "83e7a949a06a636da61d932db47d439069e95214"
83
+ "gitHead": "a466014851e9b34a9d71819278df3a7bfb8e7c2d"
84
84
  }