@hubspot/local-dev-lib 0.0.8 → 0.0.9
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/api/fileMapper.d.ts +2 -0
- package/api/fileMapper.js +3 -2
- package/config/CLIConfiguration.js +20 -18
- package/config/configFile.js +1 -2
- package/config/configUtils.js +2 -1
- package/config/config_DEPRECATED.js +24 -23
- package/config/environment.js +4 -3
- package/constants/ports.d.ts +3 -0
- package/constants/ports.js +6 -0
- package/errors/apiErrors.js +1 -1
- package/errors/fileSystemErrors.js +1 -1
- package/errors/standardErrors.d.ts +4 -3
- package/errors/standardErrors.js +2 -2
- package/http/index.js +7 -4
- package/lang/en.json +383 -0
- package/lang/lang/en.json +383 -0
- package/lib/archive.js +10 -9
- package/lib/cms/functions.js +13 -13
- package/lib/cms/handleFieldsJS.js +8 -6
- package/lib/cms/modules.js +5 -4
- package/lib/cms/processFieldsJs.js +6 -8
- package/lib/cms/templates.js +10 -5
- package/lib/cms/uploadFolder.js +11 -7
- package/lib/cms/watch.js +33 -21
- package/lib/customObjects.d.ts +4 -4
- package/lib/customObjects.js +21 -4
- package/lib/fileMapper.js +27 -20
- package/lib/github.js +15 -11
- package/lib/gitignore.js +2 -1
- package/lib/hubdb.js +4 -3
- package/lib/logging/git.js +1 -1
- package/lib/logging/logs.js +5 -1
- package/lib/oauth.js +5 -4
- package/lib/personalAccessKey.js +3 -2
- package/lib/portManager.d.ts +13 -0
- package/lib/portManager.js +45 -0
- package/lib/sandboxes.js +7 -7
- package/lib/trackUsage.js +1 -1
- package/models/OAuth2Manager.js +2 -2
- package/package.json +6 -1
- package/types/Lang.d.ts +7 -0
- package/types/Lang.js +2 -0
- package/types/PortManager.d.ts +4 -0
- package/types/PortManager.js +2 -0
- package/types/Schemas.d.ts +18 -33
- package/types/Utils.d.ts +5 -0
- package/utils/PortManagerServer.d.ts +29 -0
- package/utils/PortManagerServer.js +158 -0
- package/utils/cms/modules.js +2 -1
- package/utils/detectPort.d.ts +1 -0
- package/utils/detectPort.js +102 -0
- package/utils/lang.d.ts +2 -4
- package/utils/lang.js +12 -14
- package/utils/logger.d.ts +4 -3
- package/utils/logger.js +4 -3
- package/utils/notify.js +2 -1
- package/lang/en.lyaml +0 -247
package/lib/logging/logs.js
CHANGED
|
@@ -7,6 +7,8 @@ exports.outputLogs = void 0;
|
|
|
7
7
|
const moment_1 = __importDefault(require("moment"));
|
|
8
8
|
const chalk_1 = __importDefault(require("chalk"));
|
|
9
9
|
const logger_1 = require("./logger");
|
|
10
|
+
const lang_1 = require("../../utils/lang");
|
|
11
|
+
const i18nKey = 'lib.logging.logs';
|
|
10
12
|
const SEPARATOR = ' - ';
|
|
11
13
|
const LOG_STATUS_COLORS = {
|
|
12
14
|
SUCCESS: logger_1.Styles.success,
|
|
@@ -61,7 +63,9 @@ function processLog(log, options) {
|
|
|
61
63
|
return logHandler[log.status](log, options);
|
|
62
64
|
}
|
|
63
65
|
catch (e) {
|
|
64
|
-
logger_1.logger.error(
|
|
66
|
+
logger_1.logger.error((0, lang_1.i18n)(`${i18nKey}.unableToProcessLog`, {
|
|
67
|
+
log: JSON.stringify(log),
|
|
68
|
+
}));
|
|
65
69
|
}
|
|
66
70
|
}
|
|
67
71
|
function processLogs(logsResp, options) {
|
package/lib/oauth.js
CHANGED
|
@@ -11,10 +11,11 @@ const logger_1 = require("../utils/logger");
|
|
|
11
11
|
const logger_2 = require("../utils/logger");
|
|
12
12
|
const getAccountIdentifier_1 = require("../utils/getAccountIdentifier");
|
|
13
13
|
const config_1 = require("../config");
|
|
14
|
+
const i18nKey = 'lib.oauth';
|
|
14
15
|
const oauthManagers = new Map();
|
|
15
16
|
function writeOauthTokenInfo(accountConfig) {
|
|
16
17
|
const accountId = (0, getAccountIdentifier_1.getAccountIdentifier)(accountConfig);
|
|
17
|
-
(0, logger_1.debug)(
|
|
18
|
+
(0, logger_1.debug)(`${i18nKey}.writeTokenInfo`, { portalId: accountId || '' });
|
|
18
19
|
(0, config_1.updateAccountConfig)(accountConfig);
|
|
19
20
|
(0, config_1.writeConfig)();
|
|
20
21
|
}
|
|
@@ -27,15 +28,15 @@ function getOauthManager(accountId, accountConfig) {
|
|
|
27
28
|
exports.getOauthManager = getOauthManager;
|
|
28
29
|
const addOauthToAccountConfigCallbackKeys = ['init', 'success'];
|
|
29
30
|
function addOauthToAccountConfig(oauth, logCallbacks) {
|
|
30
|
-
const logger = (0, logger_2.makeTypedLogger)(logCallbacks
|
|
31
|
-
logger('init');
|
|
31
|
+
const logger = (0, logger_2.makeTypedLogger)(logCallbacks);
|
|
32
|
+
logger('init', `${i18nKey}.addOauthToAccountConfig.init`);
|
|
32
33
|
try {
|
|
33
34
|
(0, config_1.updateAccountConfig)({
|
|
34
35
|
...oauth.toObj(),
|
|
35
36
|
authType: auth_1.AUTH_METHODS.oauth.value,
|
|
36
37
|
});
|
|
37
38
|
(0, config_1.writeConfig)();
|
|
38
|
-
logger('success');
|
|
39
|
+
logger('success', `${i18nKey}.addOauthToAccountConfig.success`);
|
|
39
40
|
}
|
|
40
41
|
catch (err) {
|
|
41
42
|
(0, standardErrors_1.throwError)(err);
|
package/lib/personalAccessKey.js
CHANGED
|
@@ -11,6 +11,7 @@ const standardErrors_1 = require("../errors/standardErrors");
|
|
|
11
11
|
const localDevAuth_1 = require("../api/localDevAuth");
|
|
12
12
|
const sandboxHubs_1 = require("../api/sandboxHubs");
|
|
13
13
|
const config_1 = require("../config");
|
|
14
|
+
const i18nKey = 'lib.personalAccessKey';
|
|
14
15
|
const refreshRequests = new Map();
|
|
15
16
|
function getRefreshKey(personalAccessKey, expiration) {
|
|
16
17
|
return `${personalAccessKey}-${expiration || 'fresh'}`;
|
|
@@ -23,7 +24,7 @@ async function getAccessToken(personalAccessKey, env = environments_1.ENVIRONMEN
|
|
|
23
24
|
catch (e) {
|
|
24
25
|
const error = e;
|
|
25
26
|
if (error.response && error.response.body) {
|
|
26
|
-
(0, standardErrors_1.throwAuthErrorWithMessage)(
|
|
27
|
+
(0, standardErrors_1.throwAuthErrorWithMessage)(`${i18nKey}.errors.invalidPersonalAccessKey`, { errorMessage: error.response.body.message || '' }, error);
|
|
27
28
|
}
|
|
28
29
|
else {
|
|
29
30
|
(0, standardErrors_1.throwError)(e);
|
|
@@ -77,7 +78,7 @@ async function getNewAccessToken(accountId, personalAccessKey, expiresAt, env) {
|
|
|
77
78
|
async function accessTokenForPersonalAccessKey(accountId) {
|
|
78
79
|
const account = (0, config_1.getAccountConfig)(accountId);
|
|
79
80
|
if (!account) {
|
|
80
|
-
(0, standardErrors_1.throwErrorWithMessage)(
|
|
81
|
+
(0, standardErrors_1.throwErrorWithMessage)(`${i18nKey}.errors.accountNotFound`, { accountId });
|
|
81
82
|
}
|
|
82
83
|
const { auth, personalAccessKey, env } = account;
|
|
83
84
|
const authTokenInfo = auth && auth.tokenInfo;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare const BASE_URL: string;
|
|
2
|
+
export declare function startPortManagerServer(): Promise<void>;
|
|
3
|
+
export declare function stopPortManagerServer(): Promise<void>;
|
|
4
|
+
type RequestPortsData = {
|
|
5
|
+
instanceId: string;
|
|
6
|
+
port?: number;
|
|
7
|
+
};
|
|
8
|
+
export declare function requestPorts(portData: Array<RequestPortsData>): Promise<{
|
|
9
|
+
[instanceId: string]: number;
|
|
10
|
+
}>;
|
|
11
|
+
export declare function deleteServerInstance(serverInstanceId: string): Promise<void>;
|
|
12
|
+
export declare function portManagerHasActiveServers(): Promise<boolean>;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.portManagerHasActiveServers = exports.deleteServerInstance = exports.requestPorts = exports.stopPortManagerServer = exports.startPortManagerServer = exports.BASE_URL = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
const PortManagerServer_1 = __importDefault(require("../utils/PortManagerServer"));
|
|
9
|
+
const detectPort_1 = require("../utils/detectPort");
|
|
10
|
+
const ports_1 = require("../constants/ports");
|
|
11
|
+
exports.BASE_URL = `http://localhost:${ports_1.PORT_MANAGER_SERVER_PORT}`;
|
|
12
|
+
async function isPortManagerServerRunning() {
|
|
13
|
+
const port = await (0, detectPort_1.detectPort)(ports_1.PORT_MANAGER_SERVER_PORT);
|
|
14
|
+
return port !== ports_1.PORT_MANAGER_SERVER_PORT;
|
|
15
|
+
}
|
|
16
|
+
async function startPortManagerServer() {
|
|
17
|
+
const isRunning = await isPortManagerServerRunning();
|
|
18
|
+
if (!isRunning) {
|
|
19
|
+
await PortManagerServer_1.default.init();
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.startPortManagerServer = startPortManagerServer;
|
|
23
|
+
async function stopPortManagerServer() {
|
|
24
|
+
const isRunning = await isPortManagerServerRunning();
|
|
25
|
+
if (isRunning) {
|
|
26
|
+
await axios_1.default.post(`${exports.BASE_URL}/close`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.stopPortManagerServer = stopPortManagerServer;
|
|
30
|
+
async function requestPorts(portData) {
|
|
31
|
+
const { data } = await axios_1.default.post(`${exports.BASE_URL}/servers`, {
|
|
32
|
+
portData: portData,
|
|
33
|
+
});
|
|
34
|
+
return data.ports;
|
|
35
|
+
}
|
|
36
|
+
exports.requestPorts = requestPorts;
|
|
37
|
+
async function deleteServerInstance(serverInstanceId) {
|
|
38
|
+
await axios_1.default.delete(`${exports.BASE_URL}/servers/${serverInstanceId}`);
|
|
39
|
+
}
|
|
40
|
+
exports.deleteServerInstance = deleteServerInstance;
|
|
41
|
+
async function portManagerHasActiveServers() {
|
|
42
|
+
const { data } = await axios_1.default.get(`${exports.BASE_URL}/servers`);
|
|
43
|
+
return data.count > 0;
|
|
44
|
+
}
|
|
45
|
+
exports.portManagerHasActiveServers = portManagerHasActiveServers;
|
package/lib/sandboxes.js
CHANGED
|
@@ -4,7 +4,7 @@ exports.fetchTypes = exports.fetchTaskStatus = exports.initiateSync = exports.ge
|
|
|
4
4
|
const sandboxHubs_1 = require("../api/sandboxHubs");
|
|
5
5
|
const sandboxSync_1 = require("../api/sandboxSync");
|
|
6
6
|
const standardErrors_1 = require("../errors/standardErrors");
|
|
7
|
-
const i18nKey = 'sandboxes';
|
|
7
|
+
const i18nKey = 'lib.sandboxes';
|
|
8
8
|
async function createSandbox(accountId, name, type) {
|
|
9
9
|
try {
|
|
10
10
|
const resp = await (0, sandboxHubs_1.createSandbox)(accountId, name, type);
|
|
@@ -14,7 +14,7 @@ async function createSandbox(accountId, name, type) {
|
|
|
14
14
|
};
|
|
15
15
|
}
|
|
16
16
|
catch (err) {
|
|
17
|
-
(0, standardErrors_1.throwErrorWithMessage)(`${i18nKey}.createSandbox`, {}, err);
|
|
17
|
+
(0, standardErrors_1.throwErrorWithMessage)(`${i18nKey}.errors.createSandbox`, {}, err);
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
exports.createSandbox = createSandbox;
|
|
@@ -23,7 +23,7 @@ async function deleteSandbox(parentAccountId, sandboxAccountId) {
|
|
|
23
23
|
await (0, sandboxHubs_1.deleteSandbox)(parentAccountId, sandboxAccountId);
|
|
24
24
|
}
|
|
25
25
|
catch (err) {
|
|
26
|
-
(0, standardErrors_1.throwErrorWithMessage)(`${i18nKey}.deleteSandbox`, {}, err);
|
|
26
|
+
(0, standardErrors_1.throwErrorWithMessage)(`${i18nKey}.errors.deleteSandbox`, {}, err);
|
|
27
27
|
}
|
|
28
28
|
return {
|
|
29
29
|
parentAccountId,
|
|
@@ -37,7 +37,7 @@ async function getSandboxUsageLimits(parentAccountId) {
|
|
|
37
37
|
return resp && resp.usage;
|
|
38
38
|
}
|
|
39
39
|
catch (err) {
|
|
40
|
-
(0, standardErrors_1.throwErrorWithMessage)(`${i18nKey}.getSandboxUsageLimits`, {}, err);
|
|
40
|
+
(0, standardErrors_1.throwErrorWithMessage)(`${i18nKey}.errors.getSandboxUsageLimits`, {}, err);
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
exports.getSandboxUsageLimits = getSandboxUsageLimits;
|
|
@@ -46,7 +46,7 @@ async function initiateSync(fromHubId, toHubId, tasks, sandboxHubId) {
|
|
|
46
46
|
return await (0, sandboxSync_1.initiateSync)(fromHubId, toHubId, tasks, sandboxHubId);
|
|
47
47
|
}
|
|
48
48
|
catch (err) {
|
|
49
|
-
(0, standardErrors_1.throwErrorWithMessage)(`${i18nKey}.initiateSync`, {}, err);
|
|
49
|
+
(0, standardErrors_1.throwErrorWithMessage)(`${i18nKey}.errors.initiateSync`, {}, err);
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
52
|
exports.initiateSync = initiateSync;
|
|
@@ -55,7 +55,7 @@ async function fetchTaskStatus(accountId, taskId) {
|
|
|
55
55
|
return await (0, sandboxSync_1.fetchTaskStatus)(accountId, taskId);
|
|
56
56
|
}
|
|
57
57
|
catch (err) {
|
|
58
|
-
(0, standardErrors_1.throwErrorWithMessage)(`${i18nKey}.fetchTaskStatus`, {}, err);
|
|
58
|
+
(0, standardErrors_1.throwErrorWithMessage)(`${i18nKey}.errors.fetchTaskStatus`, {}, err);
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
exports.fetchTaskStatus = fetchTaskStatus;
|
|
@@ -65,7 +65,7 @@ async function fetchTypes(accountId, toHubId) {
|
|
|
65
65
|
return resp && resp.results;
|
|
66
66
|
}
|
|
67
67
|
catch (err) {
|
|
68
|
-
(0, standardErrors_1.throwErrorWithMessage)(`${i18nKey}.fetchTypes`, {}, err);
|
|
68
|
+
(0, standardErrors_1.throwErrorWithMessage)(`${i18nKey}.errors.fetchTypes`, {}, err);
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
71
|
exports.fetchTypes = fetchTypes;
|
package/lib/trackUsage.js
CHANGED
|
@@ -8,8 +8,8 @@ const logger_1 = require("../utils/logger");
|
|
|
8
8
|
const http_1 = __importDefault(require("../http"));
|
|
9
9
|
const config_1 = require("../config");
|
|
10
10
|
const fileMapper_1 = require("../api/fileMapper");
|
|
11
|
+
const i18nKey = 'lib.trackUsage';
|
|
11
12
|
async function trackUsage(eventName, eventClass, meta = {}, accountId) {
|
|
12
|
-
const i18nKey = 'api.filemapper.trackUsage';
|
|
13
13
|
const usageEvent = {
|
|
14
14
|
accountId,
|
|
15
15
|
eventName,
|
package/models/OAuth2Manager.js
CHANGED
|
@@ -24,7 +24,7 @@ class OAuth2Manager {
|
|
|
24
24
|
}
|
|
25
25
|
async accessToken() {
|
|
26
26
|
if (!this.account.auth.tokenInfo?.refreshToken) {
|
|
27
|
-
(0, standardErrors_1.throwErrorWithMessage)(`${i18nKey}.missingRefreshToken`, {
|
|
27
|
+
(0, standardErrors_1.throwErrorWithMessage)(`${i18nKey}.errors.missingRefreshToken`, {
|
|
28
28
|
accountId: (0, getAccountIdentifier_1.getAccountIdentifier)(this.account),
|
|
29
29
|
});
|
|
30
30
|
}
|
|
@@ -86,7 +86,7 @@ class OAuth2Manager {
|
|
|
86
86
|
catch (e) {
|
|
87
87
|
const error = e;
|
|
88
88
|
if (error.response) {
|
|
89
|
-
(0, standardErrors_1.throwAuthErrorWithMessage)(`${i18nKey}.auth`, {
|
|
89
|
+
(0, standardErrors_1.throwAuthErrorWithMessage)(`${i18nKey}.errors.auth`, {
|
|
90
90
|
token: error.response.body.message || '',
|
|
91
91
|
}, error);
|
|
92
92
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hubspot/local-dev-lib",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.9",
|
|
4
4
|
"description": "Provides library functionality for HubSpot local development tooling, including the HubSpot CLI",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"repository": {
|
|
@@ -29,7 +29,9 @@
|
|
|
29
29
|
"license": "Apache-2.0",
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@types/content-disposition": "^0.5.5",
|
|
32
|
+
"@types/cors": "^2.8.15",
|
|
32
33
|
"@types/debounce": "^1.2.1",
|
|
34
|
+
"@types/express": "^4.17.18",
|
|
33
35
|
"@types/findup-sync": "^4.0.2",
|
|
34
36
|
"@types/fs-extra": "^11.0.1",
|
|
35
37
|
"@types/jest": "^29.5.0",
|
|
@@ -55,9 +57,12 @@
|
|
|
55
57
|
"./logger": "./lib/logging/logger.js"
|
|
56
58
|
},
|
|
57
59
|
"dependencies": {
|
|
60
|
+
"address": "^2.0.1",
|
|
58
61
|
"axios": "^1.3.5",
|
|
59
62
|
"chokidar": "^3.5.3",
|
|
60
63
|
"content-disposition": "^0.5.4",
|
|
64
|
+
"cors": "^2.8.5",
|
|
65
|
+
"express": "^4.18.2",
|
|
61
66
|
"extract-zip": "^2.0.1",
|
|
62
67
|
"findup-sync": "^5.0.0",
|
|
63
68
|
"fs-extra": "^11.1.0",
|
package/types/Lang.d.ts
ADDED
package/types/Lang.js
ADDED
package/types/Schemas.d.ts
CHANGED
|
@@ -1,41 +1,26 @@
|
|
|
1
1
|
export type Schema = {
|
|
2
2
|
id: string;
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
calculated: boolean;
|
|
16
|
-
externalOptions: boolean;
|
|
17
|
-
archived: boolean;
|
|
18
|
-
hasUniqueValue: boolean;
|
|
19
|
-
}
|
|
20
|
-
];
|
|
21
|
-
associations?: [
|
|
22
|
-
{
|
|
23
|
-
id: string;
|
|
24
|
-
fromObjectTypeId: string;
|
|
25
|
-
toObjectTypeId: string;
|
|
26
|
-
name: string;
|
|
27
|
-
}
|
|
28
|
-
];
|
|
3
|
+
name: string;
|
|
4
|
+
createdAt?: string;
|
|
5
|
+
updatedAt?: string;
|
|
6
|
+
properties: Array<any>;
|
|
7
|
+
associations: Array<{
|
|
8
|
+
id: string;
|
|
9
|
+
fromObjectTypeId: string;
|
|
10
|
+
toObjectTypeId: string;
|
|
11
|
+
name?: string;
|
|
12
|
+
createdAt?: string | null;
|
|
13
|
+
updatedAt?: string | null;
|
|
14
|
+
}>;
|
|
29
15
|
labels: {
|
|
30
|
-
singular
|
|
31
|
-
plural
|
|
16
|
+
singular?: string;
|
|
17
|
+
plural?: string;
|
|
32
18
|
};
|
|
33
19
|
requiredProperties: Array<string>;
|
|
34
|
-
searchableProperties
|
|
35
|
-
primaryDisplayProperty
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
name: string;
|
|
20
|
+
searchableProperties?: Array<string>;
|
|
21
|
+
primaryDisplayProperty?: string;
|
|
22
|
+
fullyQualifiedName?: string;
|
|
23
|
+
[key: string]: any;
|
|
39
24
|
};
|
|
40
25
|
export type FetchSchemasResponse = {
|
|
41
26
|
results: Array<Schema>;
|
package/types/Utils.d.ts
CHANGED
|
@@ -1 +1,6 @@
|
|
|
1
1
|
export type ValueOf<T> = T[keyof T];
|
|
2
|
+
type Join<K, P> = K extends string | number ? P extends string | number ? `${K}${'' extends P ? '' : '.'}${P}` : never : never;
|
|
3
|
+
export type Leaves<T> = [10] extends [never] ? never : T extends object ? {
|
|
4
|
+
[K in keyof T]-?: Join<K, Leaves<T[K]>>;
|
|
5
|
+
}[keyof T] : '';
|
|
6
|
+
export {};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Express, Request, Response } from 'express';
|
|
3
|
+
import { Server } from 'http';
|
|
4
|
+
import { RequestPortsData } from '../types/PortManager';
|
|
5
|
+
type ServerPortMap = {
|
|
6
|
+
[instanceId: string]: number;
|
|
7
|
+
};
|
|
8
|
+
declare class PortManagerServer {
|
|
9
|
+
app?: Express;
|
|
10
|
+
server?: Server;
|
|
11
|
+
serverPortMap: ServerPortMap;
|
|
12
|
+
constructor();
|
|
13
|
+
init(): Promise<void>;
|
|
14
|
+
reset(): void;
|
|
15
|
+
listen(): Promise<Server>;
|
|
16
|
+
setupRoutes(): void;
|
|
17
|
+
setPort(instanceId: string, port: number): void;
|
|
18
|
+
deletePort(instanceId: string): void;
|
|
19
|
+
send404(res: Response, instanceId: string): void;
|
|
20
|
+
getServers: (req: Request, res: Response) => Promise<void>;
|
|
21
|
+
getServerPortByInstanceId: (req: Request, res: Response) => void;
|
|
22
|
+
assignPortsToServers: (req: Request<never, never, {
|
|
23
|
+
portData: Array<RequestPortsData>;
|
|
24
|
+
}>, res: Response) => Promise<void>;
|
|
25
|
+
deleteServerInstance: (req: Request, res: Response) => void;
|
|
26
|
+
closeServer: (req: Request, res: Response) => void;
|
|
27
|
+
}
|
|
28
|
+
declare const _default: PortManagerServer;
|
|
29
|
+
export default _default;
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const express_1 = __importDefault(require("express"));
|
|
7
|
+
const cors_1 = __importDefault(require("cors"));
|
|
8
|
+
const detectPort_1 = require("./detectPort");
|
|
9
|
+
const ports_1 = require("../constants/ports");
|
|
10
|
+
const standardErrors_1 = require("../errors/standardErrors");
|
|
11
|
+
const logger_1 = require("./logger");
|
|
12
|
+
const lang_1 = require("./lang");
|
|
13
|
+
const i18nKey = 'utils.PortManagerServer';
|
|
14
|
+
class PortManagerServer {
|
|
15
|
+
app;
|
|
16
|
+
server;
|
|
17
|
+
serverPortMap;
|
|
18
|
+
constructor() {
|
|
19
|
+
this.serverPortMap = {};
|
|
20
|
+
}
|
|
21
|
+
async init() {
|
|
22
|
+
if (this.app) {
|
|
23
|
+
(0, standardErrors_1.throwErrorWithMessage)(`${i18nKey}.errors.duplicateInstance`);
|
|
24
|
+
}
|
|
25
|
+
this.app = (0, express_1.default)();
|
|
26
|
+
this.app.use(express_1.default.json());
|
|
27
|
+
this.app.use((0, cors_1.default)());
|
|
28
|
+
this.setupRoutes();
|
|
29
|
+
try {
|
|
30
|
+
this.server = await this.listen();
|
|
31
|
+
}
|
|
32
|
+
catch (e) {
|
|
33
|
+
const error = e;
|
|
34
|
+
if (error.code === 'EADDRINUSE') {
|
|
35
|
+
(0, standardErrors_1.throwErrorWithMessage)(`${i18nKey}.errors.portInUse`, {
|
|
36
|
+
port: ports_1.PORT_MANAGER_SERVER_PORT,
|
|
37
|
+
}, error);
|
|
38
|
+
}
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
reset() {
|
|
43
|
+
this.app = undefined;
|
|
44
|
+
this.server = undefined;
|
|
45
|
+
this.serverPortMap = {};
|
|
46
|
+
}
|
|
47
|
+
listen() {
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
|
+
const server = this.app.listen(ports_1.PORT_MANAGER_SERVER_PORT, () => {
|
|
50
|
+
(0, logger_1.debug)(`${i18nKey}.started`, {
|
|
51
|
+
port: ports_1.PORT_MANAGER_SERVER_PORT,
|
|
52
|
+
});
|
|
53
|
+
resolve(server);
|
|
54
|
+
}).on('error', (err) => {
|
|
55
|
+
reject(err);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
setupRoutes() {
|
|
60
|
+
if (!this.app) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
this.app.get('/servers', this.getServers);
|
|
64
|
+
this.app.get('/servers/:instanceId', this.getServerPortByInstanceId);
|
|
65
|
+
this.app.post('/servers', this.assignPortsToServers);
|
|
66
|
+
this.app.delete('/servers/:instanceId', this.deleteServerInstance);
|
|
67
|
+
this.app.post('/close', this.closeServer);
|
|
68
|
+
}
|
|
69
|
+
setPort(instanceId, port) {
|
|
70
|
+
(0, logger_1.debug)(`${i18nKey}.setPort`, { instanceId, port });
|
|
71
|
+
this.serverPortMap[instanceId] = port;
|
|
72
|
+
}
|
|
73
|
+
deletePort(instanceId) {
|
|
74
|
+
(0, logger_1.debug)(`${i18nKey}.deletedPort`, {
|
|
75
|
+
instanceId,
|
|
76
|
+
port: this.serverPortMap[instanceId],
|
|
77
|
+
});
|
|
78
|
+
delete this.serverPortMap[instanceId];
|
|
79
|
+
}
|
|
80
|
+
send404(res, instanceId) {
|
|
81
|
+
res
|
|
82
|
+
.status(404)
|
|
83
|
+
.send((0, lang_1.i18n)(`${i18nKey}.errors.404`, { instanceId: instanceId }));
|
|
84
|
+
}
|
|
85
|
+
getServers = async (req, res) => {
|
|
86
|
+
res.send({
|
|
87
|
+
servers: this.serverPortMap,
|
|
88
|
+
count: Object.keys(this.serverPortMap).length,
|
|
89
|
+
});
|
|
90
|
+
};
|
|
91
|
+
getServerPortByInstanceId = (req, res) => {
|
|
92
|
+
const { instanceId } = req.params;
|
|
93
|
+
const port = this.serverPortMap[instanceId];
|
|
94
|
+
if (port) {
|
|
95
|
+
res.send({ port });
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
this.send404(res, instanceId);
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
assignPortsToServers = async (req, res) => {
|
|
102
|
+
const { portData } = req.body;
|
|
103
|
+
const portPromises = [];
|
|
104
|
+
for (let i = 0; i < portData.length; i++) {
|
|
105
|
+
const { port, instanceId } = portData[i];
|
|
106
|
+
if (this.serverPortMap[instanceId]) {
|
|
107
|
+
res.status(409).send((0, lang_1.i18n)(`${i18nKey}.errors.409`, {
|
|
108
|
+
instanceId,
|
|
109
|
+
port: this.serverPortMap[instanceId],
|
|
110
|
+
}));
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
else if (port && (port < ports_1.MIN_PORT_NUMBER || port > ports_1.MAX_PORT_NUMBER)) {
|
|
114
|
+
res.status(400).send((0, lang_1.i18n)(`${i18nKey}.errors.400`, {
|
|
115
|
+
minPort: ports_1.MIN_PORT_NUMBER,
|
|
116
|
+
maxPort: ports_1.MAX_PORT_NUMBER,
|
|
117
|
+
}));
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
const promise = new Promise(resolve => {
|
|
122
|
+
(0, detectPort_1.detectPort)(port, Object.values(this.serverPortMap)).then(resolvedPort => {
|
|
123
|
+
resolve({
|
|
124
|
+
[instanceId]: resolvedPort,
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
portPromises.push(promise);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
const portList = await Promise.all(portPromises);
|
|
132
|
+
const ports = portList.reduce((a, c) => Object.assign(a, c));
|
|
133
|
+
for (const instanceId in ports) {
|
|
134
|
+
this.setPort(instanceId, ports[instanceId]);
|
|
135
|
+
}
|
|
136
|
+
res.send({ ports });
|
|
137
|
+
};
|
|
138
|
+
deleteServerInstance = (req, res) => {
|
|
139
|
+
const { instanceId } = req.params;
|
|
140
|
+
const port = this.serverPortMap[instanceId];
|
|
141
|
+
if (port) {
|
|
142
|
+
this.deletePort(instanceId);
|
|
143
|
+
res.send(200);
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
this.send404(res, instanceId);
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
closeServer = (req, res) => {
|
|
150
|
+
if (this.server) {
|
|
151
|
+
(0, logger_1.debug)(`${i18nKey}.close`);
|
|
152
|
+
res.send(200);
|
|
153
|
+
this.server.close();
|
|
154
|
+
this.reset();
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
exports.default = new PortManagerServer();
|
package/utils/cms/modules.js
CHANGED
|
@@ -8,6 +8,7 @@ const path_1 = __importDefault(require("path"));
|
|
|
8
8
|
const path_2 = require("../../lib/path");
|
|
9
9
|
const extensions_1 = require("../../constants/extensions");
|
|
10
10
|
const standardErrors_1 = require("../../errors/standardErrors");
|
|
11
|
+
const i18nKey = 'utils.cms.modules';
|
|
11
12
|
const isBool = (x) => !!x === x;
|
|
12
13
|
function isPathInput(pathInput) {
|
|
13
14
|
return !!(pathInput &&
|
|
@@ -18,7 +19,7 @@ exports.isPathInput = isPathInput;
|
|
|
18
19
|
function throwInvalidPathInput(pathInput) {
|
|
19
20
|
if (isPathInput(pathInput))
|
|
20
21
|
return;
|
|
21
|
-
(0, standardErrors_1.throwTypeErrorWithMessage)(
|
|
22
|
+
(0, standardErrors_1.throwTypeErrorWithMessage)(`${i18nKey}.throwInvalidPathInput`);
|
|
22
23
|
}
|
|
23
24
|
function isModuleFolder(pathInput) {
|
|
24
25
|
throwInvalidPathInput(pathInput);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function detectPort(port?: number | null, exclude?: Array<number>): Promise<number>;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
From https://github.com/node-modules/detect-port/tree/master
|
|
4
|
+
|
|
5
|
+
The MIT License (MIT)
|
|
6
|
+
|
|
7
|
+
Copyright (c) 2014 - present node-modules and other contributors
|
|
8
|
+
|
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
10
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
11
|
+
the Software without restriction, including without limitation the rights to
|
|
12
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
13
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
14
|
+
subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
21
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
22
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
23
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
24
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
25
|
+
*/
|
|
26
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
27
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
28
|
+
};
|
|
29
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
+
exports.detectPort = void 0;
|
|
31
|
+
const net_1 = __importDefault(require("net"));
|
|
32
|
+
const address_1 = require("address");
|
|
33
|
+
const standardErrors_1 = require("../errors/standardErrors");
|
|
34
|
+
const ports_1 = require("../constants/ports");
|
|
35
|
+
const i18nKey = 'utils.detectPort';
|
|
36
|
+
function detectPort(port, exclude = []) {
|
|
37
|
+
if (port && (port < ports_1.MIN_PORT_NUMBER || port > ports_1.MAX_PORT_NUMBER)) {
|
|
38
|
+
(0, standardErrors_1.throwErrorWithMessage)(`${i18nKey}.errors.invalidPort`, {
|
|
39
|
+
minPort: ports_1.MIN_PORT_NUMBER,
|
|
40
|
+
maxPort: ports_1.MAX_PORT_NUMBER,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
const portToUse = port || 0;
|
|
44
|
+
const maxPort = Math.min(portToUse + 10, ports_1.MAX_PORT_NUMBER);
|
|
45
|
+
return new Promise(resolve => {
|
|
46
|
+
tryListen(portToUse, maxPort, exclude, (_, resolvedPort) => {
|
|
47
|
+
resolve(resolvedPort);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
exports.detectPort = detectPort;
|
|
52
|
+
function tryListen(port, maxPort, exclude, callback) {
|
|
53
|
+
const shouldGiveUp = port >= maxPort;
|
|
54
|
+
const nextPort = shouldGiveUp ? 0 : port + 1;
|
|
55
|
+
const nextMaxPort = shouldGiveUp ? 0 : maxPort;
|
|
56
|
+
listen(port, undefined, (err, realPort) => {
|
|
57
|
+
// ignore random listening
|
|
58
|
+
if (port === 0) {
|
|
59
|
+
return callback(err, realPort);
|
|
60
|
+
}
|
|
61
|
+
if (exclude.includes(port)) {
|
|
62
|
+
return tryListen(nextPort, nextMaxPort, exclude, callback);
|
|
63
|
+
}
|
|
64
|
+
if (err) {
|
|
65
|
+
return tryListen(nextPort, nextMaxPort, exclude, callback);
|
|
66
|
+
}
|
|
67
|
+
// 2. check 0.0.0.0
|
|
68
|
+
listen(port, '0.0.0.0', err => {
|
|
69
|
+
if (err) {
|
|
70
|
+
return tryListen(nextPort, nextMaxPort, exclude, callback);
|
|
71
|
+
}
|
|
72
|
+
// 3. check localhost
|
|
73
|
+
listen(port, 'localhost', err => {
|
|
74
|
+
if (err && err.code !== 'EADDRNOTAVAIL') {
|
|
75
|
+
return tryListen(nextPort, nextMaxPort, exclude, callback);
|
|
76
|
+
}
|
|
77
|
+
// 4. check current ip
|
|
78
|
+
listen(port, (0, address_1.ip)(), (err, realPort) => {
|
|
79
|
+
if (err) {
|
|
80
|
+
return tryListen(nextPort, nextMaxPort, exclude, callback);
|
|
81
|
+
}
|
|
82
|
+
callback(null, realPort);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
function listen(port, hostname, callback) {
|
|
89
|
+
const server = new net_1.default.Server();
|
|
90
|
+
server.on('error', (err) => {
|
|
91
|
+
server.close();
|
|
92
|
+
if (err.code === 'ENOTFOUND') {
|
|
93
|
+
return callback(null, port);
|
|
94
|
+
}
|
|
95
|
+
return callback(err, 0);
|
|
96
|
+
});
|
|
97
|
+
server.listen(port, hostname, () => {
|
|
98
|
+
const addressInfo = server.address();
|
|
99
|
+
server.close();
|
|
100
|
+
return callback(null, addressInfo.port);
|
|
101
|
+
});
|
|
102
|
+
}
|