@lark-apaas/db-schema-sync 0.1.9 → 0.1.10
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/dist/bin.js +1 -1
- package/dist/{chunk-QLAAPH4Z.js → chunk-5IYLZYAX.js} +158 -4
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/bin.js
CHANGED
|
@@ -56,6 +56,7 @@ var require_dist = __commonJS({
|
|
|
56
56
|
__export(index_exports, {
|
|
57
57
|
DEFAULT_CLOCK_TOLERANCE_SEC: () => DEFAULT_CLOCK_TOLERANCE_SEC,
|
|
58
58
|
DEFAULT_JWT_EXPIRE_TIME_MS: () => DEFAULT_JWT_EXPIRE_TIME_MS,
|
|
59
|
+
FileTokenProvider: () => FileTokenProvider,
|
|
59
60
|
HttpClient: () => HttpClient2,
|
|
60
61
|
HttpError: () => HttpError,
|
|
61
62
|
generateJWTToken: () => generateJWTToken,
|
|
@@ -197,7 +198,7 @@ var require_dist = __commonJS({
|
|
|
197
198
|
getCacheKey(claims) {
|
|
198
199
|
const parts = [
|
|
199
200
|
claims.access_key,
|
|
200
|
-
claims.tenant_id
|
|
201
|
+
claims.tenant_id || "",
|
|
201
202
|
claims.user_id || "",
|
|
202
203
|
claims.app_id || "",
|
|
203
204
|
claims.app_env || "",
|
|
@@ -228,9 +229,95 @@ var require_dist = __commonJS({
|
|
|
228
229
|
};
|
|
229
230
|
}
|
|
230
231
|
};
|
|
232
|
+
var StaticTokenSource = class {
|
|
233
|
+
constructor(token) {
|
|
234
|
+
this.token = token;
|
|
235
|
+
if (!token) {
|
|
236
|
+
throw new Error("StaticTokenSource: token \u4E0D\u80FD\u4E3A\u7A7A");
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
getToken(_claims) {
|
|
240
|
+
return this.token;
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
var import_fs = __require("fs");
|
|
244
|
+
var DEFAULT_REFRESH_BEFORE_MS = 5 * 60 * 1e3;
|
|
245
|
+
var DEFAULT_TOKEN_PATHS = [
|
|
246
|
+
"/home/gem/workspace/.force/openclaw/miaoda-provider-key"
|
|
247
|
+
];
|
|
248
|
+
var FileTokenProvider = class {
|
|
249
|
+
cache = /* @__PURE__ */ new Map();
|
|
250
|
+
paths;
|
|
251
|
+
refreshBeforeMs;
|
|
252
|
+
constructor(config) {
|
|
253
|
+
this.paths = config?.paths ?? DEFAULT_TOKEN_PATHS;
|
|
254
|
+
this.refreshBeforeMs = config?.refreshBeforeMs ?? DEFAULT_REFRESH_BEFORE_MS;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* 获取 token,按路径优先级依次尝试
|
|
258
|
+
*
|
|
259
|
+
* 1. 缓存有效且不在刷新窗口 → 直接返回
|
|
260
|
+
* 2. 进入刷新窗口 → 重读文件,更新缓存
|
|
261
|
+
* 3. 文件未更新但缓存未过期 → 仍返回旧缓存
|
|
262
|
+
* 4. 所有路径均无有效 token → 返回 null
|
|
263
|
+
*/
|
|
264
|
+
getToken() {
|
|
265
|
+
const now = Date.now();
|
|
266
|
+
for (const filePath of this.paths) {
|
|
267
|
+
const cached = this.cache.get(filePath);
|
|
268
|
+
if (cached && cached.expiresAtMs - now > this.refreshBeforeMs) {
|
|
269
|
+
return { token: cached.token, accessKey: cached.accessKey };
|
|
270
|
+
}
|
|
271
|
+
const result = this.readAndParse(filePath);
|
|
272
|
+
if (result) {
|
|
273
|
+
this.cache.set(filePath, result);
|
|
274
|
+
if (result.expiresAtMs > now) {
|
|
275
|
+
return { token: result.token, accessKey: result.accessKey };
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
if (cached && cached.expiresAtMs > now) {
|
|
279
|
+
return { token: cached.token, accessKey: cached.accessKey };
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return null;
|
|
283
|
+
}
|
|
284
|
+
clearCache() {
|
|
285
|
+
this.cache.clear();
|
|
286
|
+
}
|
|
287
|
+
readAndParse(filePath) {
|
|
288
|
+
try {
|
|
289
|
+
const token = (0, import_fs.readFileSync)(filePath, "utf-8").trim();
|
|
290
|
+
if (!token) return null;
|
|
291
|
+
const payload = this.decodePayload(token);
|
|
292
|
+
if (!payload?.exp) return null;
|
|
293
|
+
return {
|
|
294
|
+
token,
|
|
295
|
+
accessKey: payload.access_key ?? "",
|
|
296
|
+
expiresAtMs: payload.exp * 1e3
|
|
297
|
+
};
|
|
298
|
+
} catch {
|
|
299
|
+
return null;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
decodePayload(token) {
|
|
303
|
+
try {
|
|
304
|
+
const segments = token.split(".");
|
|
305
|
+
if (segments.length !== 3) return null;
|
|
306
|
+
const padLength = (4 - (segments[1].length % 4 || 4)) % 4;
|
|
307
|
+
const padded = `${segments[1]}${"=".repeat(padLength)}`.replace(/-/g, "+").replace(/_/g, "/");
|
|
308
|
+
return JSON.parse(Buffer.from(padded, "base64").toString("utf8"));
|
|
309
|
+
} catch {
|
|
310
|
+
return null;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
};
|
|
231
314
|
var DEFAULT_DOMAIN_ENV = "FORCE_AUTHN_INNERAPI_DOMAIN";
|
|
232
315
|
var DEFAULT_ACCESS_KEY_ENV = "FORCE_AUTHN_ACCESS_KEY";
|
|
233
316
|
var DEFAULT_SECRET_KEY_ENV = "FORCE_AUTHN_ACCESS_SECRET";
|
|
317
|
+
var DEFAULT_TOKEN_ENV = "FORCE_AUTHN_TOKEN";
|
|
318
|
+
var DEFAULT_ADMIN_DOMAIN_ENV = "MIAODA_DEV_INNER_DOMAIN_WITH_PREFIX";
|
|
319
|
+
var DEFAULT_CLIENT_TOKEN_ENV = "MIAODA_AUTHN_CODE";
|
|
320
|
+
var MIAODA_CLIENT_TOKEN_HEADER = "X-Miaoda-Client-Token";
|
|
234
321
|
function resolvePlatformBaseURL(options) {
|
|
235
322
|
if (!options?.enabled) {
|
|
236
323
|
return options?.baseURL;
|
|
@@ -238,7 +325,7 @@ var require_dist = __commonJS({
|
|
|
238
325
|
if (options.baseURL) {
|
|
239
326
|
return options.baseURL;
|
|
240
327
|
}
|
|
241
|
-
const envName = options.domainEnv || DEFAULT_DOMAIN_ENV;
|
|
328
|
+
const envName = options.adminInnerApi ? options.adminDomainEnv || DEFAULT_ADMIN_DOMAIN_ENV : options.domainEnv || DEFAULT_DOMAIN_ENV;
|
|
242
329
|
const domain = process.env[envName];
|
|
243
330
|
if (!domain) {
|
|
244
331
|
throw new Error(`\u5E73\u53F0\u6A21\u5F0F\u9700\u8981\u57FA\u7840\u57DF\u540D\uFF0C\u8BF7\u8BBE\u7F6E\u73AF\u5883\u53D8\u91CF ${envName}`);
|
|
@@ -250,6 +337,13 @@ var require_dist = __commonJS({
|
|
|
250
337
|
return void 0;
|
|
251
338
|
}
|
|
252
339
|
ensureNoAccessKeyOverride("platform.defaultClaims", options.defaultClaims);
|
|
340
|
+
let fileTokenProvider;
|
|
341
|
+
if (options.tokenProvider?.type === "file") {
|
|
342
|
+
fileTokenProvider = new FileTokenProvider({
|
|
343
|
+
paths: options.tokenProvider.paths,
|
|
344
|
+
refreshBeforeMs: options.refreshBeforeMs
|
|
345
|
+
});
|
|
346
|
+
}
|
|
253
347
|
const accessKeyEnv = options.accessKeyEnv || DEFAULT_ACCESS_KEY_ENV;
|
|
254
348
|
const secretKeyEnv = options.secretKeyEnv || DEFAULT_SECRET_KEY_ENV;
|
|
255
349
|
const accessKey = options.accessKey ?? (process.env[accessKeyEnv] || "");
|
|
@@ -261,19 +355,49 @@ var require_dist = __commonJS({
|
|
|
261
355
|
expireTimeMs,
|
|
262
356
|
refreshBeforeMs: options.refreshBeforeMs
|
|
263
357
|
});
|
|
358
|
+
const tokenEnv = options.tokenEnv || DEFAULT_TOKEN_ENV;
|
|
359
|
+
const platformToken = options.token ?? (process.env[tokenEnv] || "");
|
|
360
|
+
const staticTokenSource = platformToken ? new StaticTokenSource(platformToken) : null;
|
|
361
|
+
const clientTokenEnv = options.clientTokenEnv || DEFAULT_CLIENT_TOKEN_ENV;
|
|
264
362
|
interceptors.request.use((config) => {
|
|
363
|
+
if (fileTokenProvider) {
|
|
364
|
+
const fileToken = fileTokenProvider.getToken();
|
|
365
|
+
if (fileToken) {
|
|
366
|
+
const headers2 = {
|
|
367
|
+
...config.headers,
|
|
368
|
+
"Authorization": `Bearer ${fileToken.token}`,
|
|
369
|
+
"x-api-key": fileToken.accessKey
|
|
370
|
+
};
|
|
371
|
+
return { ...config, headers: headers2 };
|
|
372
|
+
}
|
|
373
|
+
}
|
|
265
374
|
ensureNoAccessKeyOverride("request.platformAuth.customClaims", config.platformAuth?.customClaims);
|
|
266
375
|
const claims = {
|
|
267
376
|
...options.defaultClaims || {},
|
|
268
377
|
...config.platformAuth?.customClaims || {},
|
|
269
378
|
access_key: accessKey
|
|
270
379
|
};
|
|
271
|
-
|
|
380
|
+
let token;
|
|
381
|
+
if (staticTokenSource) {
|
|
382
|
+
try {
|
|
383
|
+
token = staticTokenSource.getToken(claims);
|
|
384
|
+
} catch {
|
|
385
|
+
token = tokenManager.getToken(claims);
|
|
386
|
+
}
|
|
387
|
+
} else {
|
|
388
|
+
token = tokenManager.getToken(claims);
|
|
389
|
+
}
|
|
272
390
|
const headers = {
|
|
273
391
|
...config.headers,
|
|
274
392
|
"Authorization": `Bearer ${token}`,
|
|
275
393
|
"x-api-key": accessKey
|
|
276
394
|
};
|
|
395
|
+
if (options.adminInnerApi) {
|
|
396
|
+
const clientToken = process.env[clientTokenEnv];
|
|
397
|
+
if (clientToken) {
|
|
398
|
+
headers[MIAODA_CLIENT_TOKEN_HEADER] = clientToken;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
277
401
|
return {
|
|
278
402
|
...config,
|
|
279
403
|
headers
|
|
@@ -401,7 +525,7 @@ var require_dist = __commonJS({
|
|
|
401
525
|
response: new InterceptorManager()
|
|
402
526
|
};
|
|
403
527
|
constructor(config) {
|
|
404
|
-
const { platform, security, ...restConfig } = config || {};
|
|
528
|
+
const { platform, security, preInterceptors, ...restConfig } = config || {};
|
|
405
529
|
this.defaultConfig = {
|
|
406
530
|
timeout: 5e3,
|
|
407
531
|
...restConfig
|
|
@@ -414,12 +538,14 @@ var require_dist = __commonJS({
|
|
|
414
538
|
strictMode,
|
|
415
539
|
...security
|
|
416
540
|
};
|
|
541
|
+
this.registerRequestPreInterceptors(preInterceptors?.request);
|
|
417
542
|
if (platform?.enabled) {
|
|
418
543
|
if (!this.defaultConfig.baseURL) {
|
|
419
544
|
this.defaultConfig.baseURL = resolvePlatformBaseURL(platform);
|
|
420
545
|
}
|
|
421
546
|
registerPlatformPlugin(this.interceptors, platform);
|
|
422
547
|
}
|
|
548
|
+
this.registerResponsePreInterceptors(preInterceptors?.response);
|
|
423
549
|
}
|
|
424
550
|
/**
|
|
425
551
|
* GET 请求
|
|
@@ -569,6 +695,34 @@ var require_dist = __commonJS({
|
|
|
569
695
|
return this.runResponseInterceptors(Promise.reject(httpError));
|
|
570
696
|
}
|
|
571
697
|
}
|
|
698
|
+
/**
|
|
699
|
+
* 注册请求 preInterceptors
|
|
700
|
+
*/
|
|
701
|
+
registerRequestPreInterceptors(interceptors) {
|
|
702
|
+
if (!interceptors?.length) {
|
|
703
|
+
return;
|
|
704
|
+
}
|
|
705
|
+
for (const interceptor of interceptors) {
|
|
706
|
+
this.interceptors.request.use(
|
|
707
|
+
interceptor.onFulfilled,
|
|
708
|
+
interceptor.onRejected
|
|
709
|
+
);
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
/**
|
|
713
|
+
* 注册响应 preInterceptors
|
|
714
|
+
*/
|
|
715
|
+
registerResponsePreInterceptors(interceptors) {
|
|
716
|
+
if (!interceptors?.length) {
|
|
717
|
+
return;
|
|
718
|
+
}
|
|
719
|
+
for (const interceptor of interceptors) {
|
|
720
|
+
this.interceptors.response.use(
|
|
721
|
+
interceptor.onFulfilled,
|
|
722
|
+
interceptor.onRejected
|
|
723
|
+
);
|
|
724
|
+
}
|
|
725
|
+
}
|
|
572
726
|
/**
|
|
573
727
|
* 执行请求拦截器链
|
|
574
728
|
*/
|
package/dist/index.js
CHANGED