@nocobase/plugin-license 2.0.0-alpha.8 → 2.0.0-beta.1

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 (46) hide show
  1. package/dist/client/LicenseSettingContext.d.ts +16 -0
  2. package/dist/client/ServiceValidate.d.ts +12 -0
  3. package/dist/client/index.js +1 -1
  4. package/dist/client/locale.d.ts +1 -1
  5. package/dist/client/useSubmitProps.d.ts +12 -0
  6. package/dist/externalVersion.js +9 -3
  7. package/dist/locale/de-DE.json +13 -0
  8. package/dist/locale/en-US.json +33 -9
  9. package/dist/locale/es-ES.json +13 -0
  10. package/dist/locale/fr-FR.json +13 -0
  11. package/dist/locale/hu-HU.json +13 -0
  12. package/dist/locale/id-ID.json +13 -0
  13. package/dist/locale/it-IT.json +13 -0
  14. package/dist/locale/ja-JP.json +13 -0
  15. package/dist/locale/ko-KR.json +13 -0
  16. package/dist/locale/nl-NL.json +13 -1
  17. package/dist/locale/pt-BR.json +13 -0
  18. package/dist/locale/ru-RU.json +13 -0
  19. package/dist/locale/tr-TR.json +13 -0
  20. package/dist/locale/uk-UA.json +13 -0
  21. package/dist/locale/vi-VN.json +13 -0
  22. package/dist/locale/zh-CN.json +32 -8
  23. package/dist/locale/zh-TW.json +13 -0
  24. package/dist/server/index.d.ts +1 -0
  25. package/dist/server/index.js +6 -0
  26. package/dist/server/plugin.js +69 -6
  27. package/dist/server/utils/env.d.ts +16 -0
  28. package/dist/server/utils/env.js +125 -0
  29. package/dist/server/utils/index.d.ts +15 -0
  30. package/dist/server/utils/index.js +42 -0
  31. package/dist/server/{utils.d.ts → utils/instance.d.ts} +0 -1
  32. package/dist/server/{utils.js → utils/instance.js} +5 -12
  33. package/dist/server/utils/interface.d.ts +56 -0
  34. package/dist/server/utils/interface.js +44 -0
  35. package/dist/server/utils/key.d.ts +24 -0
  36. package/dist/server/utils/key.js +211 -0
  37. package/dist/server/utils/license.d.ts +34 -0
  38. package/dist/server/utils/license.js +80 -0
  39. package/dist/server/utils/pkg.d.ts +13 -0
  40. package/dist/server/utils/pkg.js +115 -0
  41. package/dist/server/utils/plugin.d.ts +24 -0
  42. package/dist/server/utils/plugin.js +129 -0
  43. package/package.json +7 -5
  44. package/tsconfig.env.json +17 -0
  45. package/utils/env.js +79 -0
  46. package/utils/interface.js +10 -0
@@ -0,0 +1,16 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ import { Env } from '@nocobase/license-kit';
10
+ import { KeyData } from './interface';
11
+ export declare function getClientDomain(ctx: any): string;
12
+ export declare function isDomainMatch(currentDomain: string, keyData: KeyData): boolean;
13
+ export declare function isEnvMatch(env: Env, keyData: KeyData): boolean;
14
+ export declare function isDbMatch(env: any, keyData: any): boolean;
15
+ export declare function isSysMatch(env: Env, keyData: KeyData): boolean;
16
+ export declare function getEnvOnce(): any;
@@ -0,0 +1,125 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ var __create = Object.create;
11
+ var __defProp = Object.defineProperty;
12
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
13
+ var __getOwnPropNames = Object.getOwnPropertyNames;
14
+ var __getProtoOf = Object.getPrototypeOf;
15
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
16
+ var __export = (target, all) => {
17
+ for (var name in all)
18
+ __defProp(target, name, { get: all[name], enumerable: true });
19
+ };
20
+ var __copyProps = (to, from, except, desc) => {
21
+ if (from && typeof from === "object" || typeof from === "function") {
22
+ for (let key of __getOwnPropNames(from))
23
+ if (!__hasOwnProp.call(to, key) && key !== except)
24
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
25
+ }
26
+ return to;
27
+ };
28
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
29
+ // If the importer is in node compatibility mode or this is not an ESM
30
+ // file that has been converted to a CommonJS file using a Babel-
31
+ // compatible transform (i.e. "__esModule" has not been set), then set
32
+ // "default" to the CommonJS "module.exports" for node compatibility.
33
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
34
+ mod
35
+ ));
36
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
37
+ var env_exports = {};
38
+ __export(env_exports, {
39
+ getClientDomain: () => getClientDomain,
40
+ getEnvOnce: () => getEnvOnce,
41
+ isDbMatch: () => isDbMatch,
42
+ isDomainMatch: () => isDomainMatch,
43
+ isEnvMatch: () => isEnvMatch,
44
+ isSysMatch: () => isSysMatch
45
+ });
46
+ module.exports = __toCommonJS(env_exports);
47
+ var import_license_kit = require("@nocobase/license-kit");
48
+ var import_omit = __toESM(require("lodash/omit"));
49
+ var import_isEqual = __toESM(require("lodash/isEqual"));
50
+ function getClientDomain(ctx) {
51
+ var _a, _b, _c, _d;
52
+ if (!ctx) return "";
53
+ const referer = typeof ctx.get === "function" ? ctx.get("referer") : null;
54
+ if (referer) {
55
+ try {
56
+ return new URL(referer).origin;
57
+ } catch {
58
+ console.error("Invalid Referer URL:", referer);
59
+ }
60
+ }
61
+ const protocol = ((_a = ctx.headers) == null ? void 0 : _a["x-forwarded-proto"]) || ctx.protocol || ((_b = ctx.request) == null ? void 0 : _b.protocol) || "http";
62
+ const host = ((_c = ctx.headers) == null ? void 0 : _c["x-forwarded-host"]) || ctx.host || ((_d = ctx.request) == null ? void 0 : _d.host) || "";
63
+ return host ? `${protocol}://${host}` : "";
64
+ }
65
+ function matchSingleDomain(licenseDomain, currentDomain) {
66
+ let hostname = "";
67
+ let port = "";
68
+ try {
69
+ const url = new URL(currentDomain);
70
+ hostname = url.hostname;
71
+ port = url.port ? `:${url.port}` : "";
72
+ } catch {
73
+ return false;
74
+ }
75
+ const fullDomain = hostname + port;
76
+ if (!licenseDomain.includes("*")) {
77
+ return fullDomain === licenseDomain;
78
+ }
79
+ const base = licenseDomain.replace("*", "");
80
+ return fullDomain.endsWith(base);
81
+ }
82
+ function isDomainMatch(currentDomain, keyData) {
83
+ var _a;
84
+ if (!((_a = keyData == null ? void 0 : keyData.licenseKey) == null ? void 0 : _a.domain) || !currentDomain) return false;
85
+ const licenseDomains = keyData.licenseKey.domain.split(",").map((d) => d.trim()).filter(Boolean);
86
+ return licenseDomains.some((licenseDomain) => matchSingleDomain(licenseDomain, currentDomain));
87
+ }
88
+ function isEnvMatch(env, keyData) {
89
+ return isDbMatch(env, keyData) && isSysMatch(env, keyData);
90
+ }
91
+ function isDbMatch(env, keyData) {
92
+ var _a;
93
+ const a = env == null ? void 0 : env.db;
94
+ const b = (_a = keyData == null ? void 0 : keyData.instanceData) == null ? void 0 : _a.db;
95
+ if (!a || !b) return false;
96
+ if ((a == null ? void 0 : a.id) && (b == null ? void 0 : b.id)) return a.id === b.id;
97
+ return (0, import_isEqual.default)((0, import_omit.default)(a, ["id"]), (0, import_omit.default)(b, ["id"]));
98
+ }
99
+ function isSysMatch(env, keyData) {
100
+ const instance = keyData == null ? void 0 : keyData.instanceData;
101
+ if (!env || !instance) return false;
102
+ const normalize = (item) => {
103
+ if (!item) return null;
104
+ return {
105
+ sys: item.sys ?? null,
106
+ osVer: item.osVer ?? null
107
+ };
108
+ };
109
+ const a = normalize(env);
110
+ const b = normalize(instance);
111
+ return (0, import_isEqual.default)(a, b);
112
+ }
113
+ let envPromise;
114
+ function getEnvOnce() {
115
+ return envPromise ?? (envPromise = (0, import_license_kit.getEnvAsync)());
116
+ }
117
+ // Annotate the CommonJS export names for ESM import in node:
118
+ 0 && (module.exports = {
119
+ getClientDomain,
120
+ getEnvOnce,
121
+ isDbMatch,
122
+ isDomainMatch,
123
+ isEnvMatch,
124
+ isSysMatch
125
+ });
@@ -0,0 +1,15 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ export * from './key';
10
+ export * from './env';
11
+ export * from './interface';
12
+ export * from './license';
13
+ export * from './instance';
14
+ export * from './plugin';
15
+ export * from './pkg';
@@ -0,0 +1,42 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ var __defProp = Object.defineProperty;
11
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
12
+ var __getOwnPropNames = Object.getOwnPropertyNames;
13
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
14
+ var __copyProps = (to, from, except, desc) => {
15
+ if (from && typeof from === "object" || typeof from === "function") {
16
+ for (let key of __getOwnPropNames(from))
17
+ if (!__hasOwnProp.call(to, key) && key !== except)
18
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
19
+ }
20
+ return to;
21
+ };
22
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
23
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
24
+ var utils_exports = {};
25
+ module.exports = __toCommonJS(utils_exports);
26
+ __reExport(utils_exports, require("./key"), module.exports);
27
+ __reExport(utils_exports, require("./env"), module.exports);
28
+ __reExport(utils_exports, require("./interface"), module.exports);
29
+ __reExport(utils_exports, require("./license"), module.exports);
30
+ __reExport(utils_exports, require("./instance"), module.exports);
31
+ __reExport(utils_exports, require("./plugin"), module.exports);
32
+ __reExport(utils_exports, require("./pkg"), module.exports);
33
+ // Annotate the CommonJS export names for ESM import in node:
34
+ 0 && (module.exports = {
35
+ ...require("./key"),
36
+ ...require("./env"),
37
+ ...require("./interface"),
38
+ ...require("./license"),
39
+ ...require("./instance"),
40
+ ...require("./plugin"),
41
+ ...require("./pkg")
42
+ });
@@ -8,5 +8,4 @@
8
8
  */
9
9
  export declare function getInstanceId(): Promise<string>;
10
10
  export declare function createInstanceId(force?: boolean): Promise<unknown>;
11
- export declare function saveLicenseKey(licenseKey: string): Promise<void>;
12
11
  export declare function isLicenseKeyExists(): Promise<boolean>;
@@ -34,14 +34,13 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
34
34
  mod
35
35
  ));
36
36
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
37
- var utils_exports = {};
38
- __export(utils_exports, {
37
+ var instance_exports = {};
38
+ __export(instance_exports, {
39
39
  createInstanceId: () => createInstanceId,
40
40
  getInstanceId: () => getInstanceId,
41
- isLicenseKeyExists: () => isLicenseKeyExists,
42
- saveLicenseKey: () => saveLicenseKey
41
+ isLicenseKeyExists: () => isLicenseKeyExists
43
42
  });
44
- module.exports = __toCommonJS(utils_exports);
43
+ module.exports = __toCommonJS(instance_exports);
45
44
  var import_fs = __toESM(require("fs"));
46
45
  var import_path = __toESM(require("path"));
47
46
  var import_child_process = require("child_process");
@@ -63,11 +62,6 @@ async function createInstanceId(force = false) {
63
62
  });
64
63
  });
65
64
  }
66
- async function saveLicenseKey(licenseKey) {
67
- const dir = import_path.default.resolve(process.cwd(), "storage/.license");
68
- const filePath = import_path.default.resolve(dir, "license-key");
69
- import_fs.default.writeFileSync(filePath, licenseKey);
70
- }
71
65
  async function isLicenseKeyExists() {
72
66
  const dir = import_path.default.resolve(process.cwd(), "storage/.license");
73
67
  const filePath = import_path.default.resolve(dir, "license-key");
@@ -77,6 +71,5 @@ async function isLicenseKeyExists() {
77
71
  0 && (module.exports = {
78
72
  createInstanceId,
79
73
  getInstanceId,
80
- isLicenseKeyExists,
81
- saveLicenseKey
74
+ isLicenseKeyExists
82
75
  });
@@ -0,0 +1,56 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ export interface KeyData {
10
+ upgradeExpirationDate: string;
11
+ licenseKey: {
12
+ id: string;
13
+ licensee: string;
14
+ desc: string;
15
+ type: string;
16
+ domain: string;
17
+ licenseStatus: string;
18
+ };
19
+ plugins: Array<{
20
+ displayName: string;
21
+ packageName: string;
22
+ updateExpirationDate?: string;
23
+ }>;
24
+ instanceData: {
25
+ timestamp: string;
26
+ sys: string;
27
+ osVer: string;
28
+ kVer: string;
29
+ hostname: string;
30
+ mac: string;
31
+ db: {
32
+ type: string;
33
+ name: string;
34
+ oid: number;
35
+ ver: string;
36
+ id?: string;
37
+ };
38
+ container: {
39
+ name: string;
40
+ id: string;
41
+ };
42
+ };
43
+ service: {
44
+ domain: string;
45
+ [key: string]: any;
46
+ };
47
+ accessKeyId: string;
48
+ accessKeySecret: string;
49
+ timestamp: number;
50
+ }
51
+ export declare const enum LICENSE_ERROR {
52
+ KEY_FORMAT_ERROR = "KEY_FORMAT_ERROR",
53
+ KEY_NOT_FOUND = "KEY_NOT_FOUND",
54
+ KEY_READ_ERROR = "KEY_READ_ERROR"
55
+ }
56
+ export declare const CACHE_KEY = "license-key";
@@ -0,0 +1,44 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ var __defProp = Object.defineProperty;
11
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
12
+ var __getOwnPropNames = Object.getOwnPropertyNames;
13
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
14
+ var __export = (target, all) => {
15
+ for (var name in all)
16
+ __defProp(target, name, { get: all[name], enumerable: true });
17
+ };
18
+ var __copyProps = (to, from, except, desc) => {
19
+ if (from && typeof from === "object" || typeof from === "function") {
20
+ for (let key of __getOwnPropNames(from))
21
+ if (!__hasOwnProp.call(to, key) && key !== except)
22
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
23
+ }
24
+ return to;
25
+ };
26
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
27
+ var interface_exports = {};
28
+ __export(interface_exports, {
29
+ CACHE_KEY: () => CACHE_KEY,
30
+ LICENSE_ERROR: () => LICENSE_ERROR
31
+ });
32
+ module.exports = __toCommonJS(interface_exports);
33
+ var LICENSE_ERROR = /* @__PURE__ */ ((LICENSE_ERROR2) => {
34
+ LICENSE_ERROR2["KEY_FORMAT_ERROR"] = "KEY_FORMAT_ERROR";
35
+ LICENSE_ERROR2["KEY_NOT_FOUND"] = "KEY_NOT_FOUND";
36
+ LICENSE_ERROR2["KEY_READ_ERROR"] = "KEY_READ_ERROR";
37
+ return LICENSE_ERROR2;
38
+ })(LICENSE_ERROR || {});
39
+ const CACHE_KEY = "license-key";
40
+ // Annotate the CommonJS export names for ESM import in node:
41
+ 0 && (module.exports = {
42
+ CACHE_KEY,
43
+ LICENSE_ERROR
44
+ });
@@ -0,0 +1,24 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ import { Context } from 'koa';
10
+ import { KeyData } from './interface';
11
+ export declare function request({ url, method, body, headers, timeout, }: {
12
+ url: string;
13
+ method?: string;
14
+ body?: Record<string, any>;
15
+ headers?: any;
16
+ timeout?: number;
17
+ }, ctx?: Context): Promise<any>;
18
+ export declare function saveLicenseKey(licenseKey: string, ctx?: any): Promise<void>;
19
+ export declare function getLocalKeyData(): Promise<any>;
20
+ export declare function getKey(ctx?: Context): Promise<string>;
21
+ export declare function getLicenseStatus(keyData: KeyData): Promise<'active' | 'invalid'>;
22
+ export declare function parseKey(key: string): KeyData;
23
+ export declare function getKeyInfo(ctx?: Context): Promise<KeyData>;
24
+ export declare function isDateExpired(upgradeExpirationDate: string): boolean;
@@ -0,0 +1,211 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ var __create = Object.create;
11
+ var __defProp = Object.defineProperty;
12
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
13
+ var __getOwnPropNames = Object.getOwnPropertyNames;
14
+ var __getProtoOf = Object.getPrototypeOf;
15
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
16
+ var __export = (target, all) => {
17
+ for (var name in all)
18
+ __defProp(target, name, { get: all[name], enumerable: true });
19
+ };
20
+ var __copyProps = (to, from, except, desc) => {
21
+ if (from && typeof from === "object" || typeof from === "function") {
22
+ for (let key of __getOwnPropNames(from))
23
+ if (!__hasOwnProp.call(to, key) && key !== except)
24
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
25
+ }
26
+ return to;
27
+ };
28
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
29
+ // If the importer is in node compatibility mode or this is not an ESM
30
+ // file that has been converted to a CommonJS file using a Babel-
31
+ // compatible transform (i.e. "__esModule" has not been set), then set
32
+ // "default" to the CommonJS "module.exports" for node compatibility.
33
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
34
+ mod
35
+ ));
36
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
37
+ var key_exports = {};
38
+ __export(key_exports, {
39
+ getKey: () => getKey,
40
+ getKeyInfo: () => getKeyInfo,
41
+ getLicenseStatus: () => getLicenseStatus,
42
+ getLocalKeyData: () => getLocalKeyData,
43
+ isDateExpired: () => isDateExpired,
44
+ parseKey: () => parseKey,
45
+ request: () => request,
46
+ saveLicenseKey: () => saveLicenseKey
47
+ });
48
+ module.exports = __toCommonJS(key_exports);
49
+ var import_license_kit = require("@nocobase/license-kit");
50
+ var import_path = __toESM(require("path"));
51
+ var import_fs = __toESM(require("fs"));
52
+ var import_interface2 = require("./interface");
53
+ var import_axios = __toESM(require("axios"));
54
+ var import_logger = require("@nocobase/logger");
55
+ async function request({
56
+ url,
57
+ method = "GET",
58
+ body,
59
+ headers,
60
+ timeout = 5e3
61
+ }, ctx) {
62
+ var _a, _b, _c, _d, _e, _f, _g, _h;
63
+ const xHeaders = {
64
+ "Content-Type": "application/json",
65
+ "X-Hostname": new URL(url).hostname,
66
+ "X-Timezone": (_b = (_a = ctx == null ? void 0 : ctx.request) == null ? void 0 : _a.headers) == null ? void 0 : _b["x-timezone"],
67
+ "X-Locale": (_d = (_c = ctx == null ? void 0 : ctx.request) == null ? void 0 : _c.headers) == null ? void 0 : _d["x-locale"],
68
+ "X-Role": (_f = (_e = ctx == null ? void 0 : ctx.request) == null ? void 0 : _e.headers) == null ? void 0 : _f["x-role"],
69
+ "X-Authenticator": (_h = (_g = ctx == null ? void 0 : ctx.request) == null ? void 0 : _g.headers) == null ? void 0 : _h["x-authenticator"]
70
+ };
71
+ try {
72
+ const res = await (0, import_axios.default)({
73
+ url,
74
+ method,
75
+ timeout,
76
+ headers: {
77
+ ...xHeaders,
78
+ ...headers
79
+ },
80
+ data: body || {}
81
+ });
82
+ return res.data;
83
+ } catch (err) {
84
+ if (err.code === "ECONNABORTED") {
85
+ throw new Error(`Request timeout after ${timeout}ms`);
86
+ }
87
+ throw err;
88
+ }
89
+ }
90
+ async function saveLicenseKey(licenseKey, ctx) {
91
+ const dir = import_path.default.resolve(process.cwd(), "storage/.license");
92
+ const filePath = import_path.default.resolve(dir, "license-key");
93
+ await import_fs.default.promises.writeFile(filePath, licenseKey);
94
+ if (ctx && ctx.cache) {
95
+ await ctx.cache.set("license-key", licenseKey);
96
+ }
97
+ }
98
+ async function getLocalKeyData() {
99
+ const keyFile = import_path.default.resolve(process.cwd(), "storage/.license/license-key");
100
+ try {
101
+ const key = (await import_fs.default.promises.readFile(keyFile, "utf8")).trim();
102
+ return JSON.parse((0, import_license_kit.keyDecrypt)(key));
103
+ } catch (e) {
104
+ return null;
105
+ }
106
+ }
107
+ async function getKey(ctx) {
108
+ var _a, _b;
109
+ let key;
110
+ const keyFile = import_path.default.resolve(process.cwd(), "storage/.license/license-key");
111
+ if (ctx == null ? void 0 : ctx.cache) {
112
+ try {
113
+ key = await ctx.cache.get(import_interface2.CACHE_KEY);
114
+ } catch (e) {
115
+ import_logger.logger.warn("Failed to get license key from cache", e);
116
+ }
117
+ }
118
+ if (!key) {
119
+ try {
120
+ key = (await import_fs.default.promises.readFile(keyFile, "utf8")).trim();
121
+ } catch (e) {
122
+ import_logger.logger.error("Failed to read license key file", e);
123
+ throw new Error("notfound");
124
+ }
125
+ }
126
+ if (!key) {
127
+ import_logger.logger.error("License key not found");
128
+ throw new Error("notfound");
129
+ }
130
+ let keyData;
131
+ try {
132
+ keyData = JSON.parse((0, import_license_kit.keyDecrypt)(key));
133
+ } catch (e) {
134
+ import_logger.logger.error("Failed to parse license key");
135
+ throw new Error("invalid");
136
+ }
137
+ try {
138
+ const { data } = await request({
139
+ url: `${(_a = keyData == null ? void 0 : keyData.service) == null ? void 0 : _a.domain}/api/license_keys:getKey`,
140
+ method: "POST",
141
+ body: {
142
+ access_key_id: keyData == null ? void 0 : keyData.accessKeyId,
143
+ access_key_secret: keyData == null ? void 0 : keyData.accessKeySecret
144
+ },
145
+ headers: ((_b = keyData == null ? void 0 : keyData.service) == null ? void 0 : _b.headers) || {}
146
+ });
147
+ const { key: remoteKey } = data || {};
148
+ if (remoteKey && typeof remoteKey === "string") {
149
+ await saveLicenseKey(remoteKey, ctx);
150
+ return remoteKey;
151
+ }
152
+ } catch (e) {
153
+ return key;
154
+ }
155
+ return key;
156
+ }
157
+ async function getLicenseStatus(keyData) {
158
+ var _a, _b, _c;
159
+ if (!keyData) {
160
+ return "invalid";
161
+ }
162
+ if (((_a = keyData == null ? void 0 : keyData.licenseKey) == null ? void 0 : _a.licenseStatus) === "invalid") {
163
+ return "invalid";
164
+ }
165
+ try {
166
+ const { data } = await request({
167
+ url: `${(_b = keyData == null ? void 0 : keyData.service) == null ? void 0 : _b.domain}/api/license_keys:getKeyStatus`,
168
+ method: "POST",
169
+ body: {
170
+ access_key_id: keyData == null ? void 0 : keyData.accessKeyId,
171
+ access_key_secret: keyData == null ? void 0 : keyData.accessKeySecret
172
+ },
173
+ headers: ((_c = keyData == null ? void 0 : keyData.service) == null ? void 0 : _c.headers) || {}
174
+ });
175
+ if ((data == null ? void 0 : data.status) === "active") {
176
+ return "active";
177
+ } else {
178
+ return "invalid";
179
+ }
180
+ } catch (e) {
181
+ return "active";
182
+ }
183
+ }
184
+ function parseKey(key) {
185
+ try {
186
+ return JSON.parse((0, import_license_kit.keyDecrypt)(key));
187
+ } catch (e) {
188
+ throw new Error("invalid");
189
+ }
190
+ }
191
+ async function getKeyInfo(ctx) {
192
+ const key = await getKey(ctx);
193
+ return parseKey(key);
194
+ }
195
+ function isDateExpired(upgradeExpirationDate) {
196
+ if (!upgradeExpirationDate) {
197
+ return true;
198
+ }
199
+ return /* @__PURE__ */ new Date() > new Date(upgradeExpirationDate);
200
+ }
201
+ // Annotate the CommonJS export names for ESM import in node:
202
+ 0 && (module.exports = {
203
+ getKey,
204
+ getKeyInfo,
205
+ getLicenseStatus,
206
+ getLocalKeyData,
207
+ isDateExpired,
208
+ parseKey,
209
+ request,
210
+ saveLicenseKey
211
+ });
@@ -0,0 +1,34 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ import { Env } from '@nocobase/license-kit';
10
+ import { KeyData } from './interface';
11
+ import { PluginData } from './plugin';
12
+ export interface LicenseValidateResult {
13
+ current: {
14
+ env: Env;
15
+ domain: string;
16
+ };
17
+ dbMatch: boolean;
18
+ sysMatch: boolean;
19
+ envMatch: boolean;
20
+ domainMatch: boolean;
21
+ isExpired: boolean;
22
+ isServiceConnection: boolean;
23
+ isPkgLogin: boolean;
24
+ keyData: KeyData;
25
+ pkgUrl: string;
26
+ licenseStatus: 'active' | 'invalid';
27
+ pluginsLicensed: boolean;
28
+ plugins: PluginData[];
29
+ keyStatus?: 'invalid' | 'notfound';
30
+ }
31
+ export declare function getLicenseValidate({ key, ctx }: {
32
+ key?: string;
33
+ ctx?: any;
34
+ }): Promise<LicenseValidateResult>;