@forge/tunnel 5.2.1-next.6 → 5.2.1-next.7
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/CHANGELOG.md +8 -0
- package/out/command/interactors/function-change-watcher.js +9 -6
- package/out/command/interactors/multi-compiler-watcher.js +13 -3
- package/out/command/interactors/tunnel-interactor.js +48 -47
- package/out/command/start-tunnel-command.js +61 -44
- package/out/graphql/tunnel-graphql-client.js +1 -0
- package/out/sandbox/node-sandbox.js +4 -2
- package/out/sandbox/sandbox-runner.js +1 -2
- package/out/servers/csp-reporter-server.js +7 -3
- package/out/servers/custom-ui-tunnel-server.js +8 -5
- package/out/servers/dev-server.js +44 -33
- package/out/servers/native-ui-tunnel-server.js +10 -3
- package/out/servers/resource-tunnel-server.js +35 -30
- package/out/services/create-tunnel-service.js +11 -4
- package/out/services/local-invocation-service.js +11 -9
- package/out/services/register-tunnel-service.js +4 -0
- package/out/util/sandboxes-container.js +2 -4
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
|
@@ -10,15 +10,16 @@ const runtime_1 = require("@forge/runtime");
|
|
|
10
10
|
const util_1 = require("../../util");
|
|
11
11
|
exports.MEMORY_LIMIT = 256;
|
|
12
12
|
class LocalFunctionHost {
|
|
13
|
+
configFile;
|
|
14
|
+
logger;
|
|
15
|
+
snapshot;
|
|
16
|
+
createSandbox;
|
|
17
|
+
snapshotLogs = [];
|
|
13
18
|
constructor(configFile, logger, snapshot, createSandbox) {
|
|
14
19
|
this.configFile = configFile;
|
|
15
20
|
this.logger = logger;
|
|
16
21
|
this.snapshot = snapshot;
|
|
17
22
|
this.createSandbox = createSandbox;
|
|
18
|
-
this.snapshotLogs = [];
|
|
19
|
-
this.onRuntimeLog = (event) => {
|
|
20
|
-
this.logger.info((0, util_1.formatRuntimeLog)(event));
|
|
21
|
-
};
|
|
22
23
|
}
|
|
23
24
|
async generateBundleFiles(bundle, withSnapshot) {
|
|
24
25
|
await fs_1.promises.mkdir((0, path_1.dirname)(bundle.bundleFilePath), { recursive: true });
|
|
@@ -52,13 +53,12 @@ class LocalFunctionHost {
|
|
|
52
53
|
this.snapshotLogs = [];
|
|
53
54
|
}
|
|
54
55
|
const bundles = Object.keys(bundledCode.output).map((bundlePath) => {
|
|
55
|
-
var _a;
|
|
56
56
|
const sourceMapPath = `${bundlePath}.map`;
|
|
57
57
|
return {
|
|
58
58
|
bundleFilePath: (0, path_1.resolve)(directory, bundlePath),
|
|
59
59
|
bundleFileContent: bundledCode.output[bundlePath],
|
|
60
60
|
bundleFileSourceMapPath: (0, path_1.resolve)(directory, sourceMapPath),
|
|
61
|
-
bundleFileSourceMap:
|
|
61
|
+
bundleFileSourceMap: bundledCode.sourceMap?.[sourceMapPath]
|
|
62
62
|
};
|
|
63
63
|
});
|
|
64
64
|
const manifestFilePath = (0, path_1.resolve)(directory, cli_shared_1.manifestFileName);
|
|
@@ -88,6 +88,9 @@ class LocalFunctionHost {
|
|
|
88
88
|
async stopWatching() {
|
|
89
89
|
runtime_1.StaticInvocationEventEmitter.removeAllListeners();
|
|
90
90
|
}
|
|
91
|
+
onRuntimeLog = (event) => {
|
|
92
|
+
this.logger.info((0, util_1.formatRuntimeLog)(event));
|
|
93
|
+
};
|
|
91
94
|
async initializeSandboxes(srcPath, tunnelOptions) {
|
|
92
95
|
try {
|
|
93
96
|
util_1.SandboxesContainer.stopSandboxes();
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.MultiCompilerWatcher = void 0;
|
|
4
4
|
const cli_shared_1 = require("@forge/cli-shared");
|
|
5
5
|
class MultiCompilerWatcher {
|
|
6
|
+
servers;
|
|
6
7
|
constructor(servers) {
|
|
7
8
|
this.servers = servers;
|
|
8
9
|
}
|
|
@@ -15,9 +16,18 @@ class MultiCompilerWatcher {
|
|
|
15
16
|
output: {}
|
|
16
17
|
};
|
|
17
18
|
for (const result of results) {
|
|
18
|
-
multiResult.output =
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
multiResult.output = {
|
|
20
|
+
...multiResult.output,
|
|
21
|
+
...result.output
|
|
22
|
+
};
|
|
23
|
+
multiResult.sourceMap = {
|
|
24
|
+
...multiResult.sourceMap,
|
|
25
|
+
...result.sourceMap
|
|
26
|
+
};
|
|
27
|
+
multiResult.metadata = {
|
|
28
|
+
...multiResult.metadata,
|
|
29
|
+
...result.metadata
|
|
30
|
+
};
|
|
21
31
|
}
|
|
22
32
|
return multiResult;
|
|
23
33
|
}
|
|
@@ -7,55 +7,9 @@ const bundler_1 = require("@forge/bundler");
|
|
|
7
7
|
const cli_shared_1 = require("@forge/cli-shared");
|
|
8
8
|
const multi_compiler_watcher_1 = require("./multi-compiler-watcher");
|
|
9
9
|
class TunnelInteractor {
|
|
10
|
+
logger;
|
|
10
11
|
constructor(logger) {
|
|
11
12
|
this.logger = logger;
|
|
12
|
-
this.watchApp = async (startTunnelResult, tunnelOptions = cli_shared_1.defaultNoDebugTunnelOptions) => {
|
|
13
|
-
const { localPort, inspectorAddress, reloadSandboxes, devServers } = startTunnelResult;
|
|
14
|
-
if (inspectorAddress) {
|
|
15
|
-
this.logger.info(cli_shared_1.Text.tunnel.startedInspector(inspectorAddress));
|
|
16
|
-
}
|
|
17
|
-
const onBundlingStart = async () => {
|
|
18
|
-
try {
|
|
19
|
-
this.logger.info('');
|
|
20
|
-
this.logger.info(cli_shared_1.Text.bundle.detectedChanges);
|
|
21
|
-
await (0, bundler_1.runLinter)();
|
|
22
|
-
this.logger.info(cli_shared_1.Text.tunnel.preBundleTask(cli_shared_1.Text.tunnel.bundlingHeader));
|
|
23
|
-
}
|
|
24
|
-
catch (err) {
|
|
25
|
-
throw err;
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
const onBundlingFinish = async (err, output) => {
|
|
29
|
-
if (err) {
|
|
30
|
-
this.logger.error(err);
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
if (output) {
|
|
34
|
-
await reloadSandboxes(output, tunnelOptions);
|
|
35
|
-
}
|
|
36
|
-
this.logger.info('');
|
|
37
|
-
this.logger.info(cli_shared_1.Text.tunnel.startedServer(localPort, this.logger.debugEnabled) + '\n');
|
|
38
|
-
};
|
|
39
|
-
if (devServers.length > 0) {
|
|
40
|
-
const multiCompiler = new multi_compiler_watcher_1.MultiCompilerWatcher(devServers);
|
|
41
|
-
await (0, bundler_1.runLinter)();
|
|
42
|
-
this.logger.info(cli_shared_1.Text.tunnel.preBundleTask(cli_shared_1.Text.tunnel.bundlingHeader));
|
|
43
|
-
try {
|
|
44
|
-
const output = await multiCompiler.compileAndWatch({
|
|
45
|
-
onChange: {
|
|
46
|
-
onBuildWillStart: onBundlingStart,
|
|
47
|
-
onBuildFinished: onBundlingFinish
|
|
48
|
-
}
|
|
49
|
-
}, tunnelOptions);
|
|
50
|
-
await reloadSandboxes(output, tunnelOptions);
|
|
51
|
-
this.logger.info('');
|
|
52
|
-
this.logger.info(cli_shared_1.Text.tunnel.startedServer(localPort, this.logger.debugEnabled) + '\n');
|
|
53
|
-
}
|
|
54
|
-
catch (_) {
|
|
55
|
-
}
|
|
56
|
-
return multiCompiler;
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
13
|
}
|
|
60
14
|
handleUserExitEvent(stopFunction, bundleMonitor) {
|
|
61
15
|
return new Promise((resolve, reject) => {
|
|
@@ -88,5 +42,52 @@ class TunnelInteractor {
|
|
|
88
42
|
process.on('SIGTERM', tunnelCleanupListener);
|
|
89
43
|
});
|
|
90
44
|
}
|
|
45
|
+
watchApp = async (startTunnelResult, tunnelOptions = cli_shared_1.defaultNoDebugTunnelOptions) => {
|
|
46
|
+
const { localPort, inspectorAddress, reloadSandboxes, devServers } = startTunnelResult;
|
|
47
|
+
if (inspectorAddress) {
|
|
48
|
+
this.logger.info(cli_shared_1.Text.tunnel.startedInspector(inspectorAddress));
|
|
49
|
+
}
|
|
50
|
+
const onBundlingStart = async () => {
|
|
51
|
+
try {
|
|
52
|
+
this.logger.info('');
|
|
53
|
+
this.logger.info(cli_shared_1.Text.bundle.detectedChanges);
|
|
54
|
+
await (0, bundler_1.runLinter)();
|
|
55
|
+
this.logger.info(cli_shared_1.Text.tunnel.preBundleTask(cli_shared_1.Text.tunnel.bundlingHeader));
|
|
56
|
+
}
|
|
57
|
+
catch (err) {
|
|
58
|
+
throw err;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
const onBundlingFinish = async (err, output) => {
|
|
62
|
+
if (err) {
|
|
63
|
+
this.logger.error(err);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (output) {
|
|
67
|
+
await reloadSandboxes(output, tunnelOptions);
|
|
68
|
+
}
|
|
69
|
+
this.logger.info('');
|
|
70
|
+
this.logger.info(cli_shared_1.Text.tunnel.startedServer(localPort, this.logger.debugEnabled) + '\n');
|
|
71
|
+
};
|
|
72
|
+
if (devServers.length > 0) {
|
|
73
|
+
const multiCompiler = new multi_compiler_watcher_1.MultiCompilerWatcher(devServers);
|
|
74
|
+
await (0, bundler_1.runLinter)();
|
|
75
|
+
this.logger.info(cli_shared_1.Text.tunnel.preBundleTask(cli_shared_1.Text.tunnel.bundlingHeader));
|
|
76
|
+
try {
|
|
77
|
+
const output = await multiCompiler.compileAndWatch({
|
|
78
|
+
onChange: {
|
|
79
|
+
onBuildWillStart: onBundlingStart,
|
|
80
|
+
onBuildFinished: onBundlingFinish
|
|
81
|
+
}
|
|
82
|
+
}, tunnelOptions);
|
|
83
|
+
await reloadSandboxes(output, tunnelOptions);
|
|
84
|
+
this.logger.info('');
|
|
85
|
+
this.logger.info(cli_shared_1.Text.tunnel.startedServer(localPort, this.logger.debugEnabled) + '\n');
|
|
86
|
+
}
|
|
87
|
+
catch (_) {
|
|
88
|
+
}
|
|
89
|
+
return multiCompiler;
|
|
90
|
+
}
|
|
91
|
+
};
|
|
91
92
|
}
|
|
92
93
|
exports.TunnelInteractor = TunnelInteractor;
|
|
@@ -8,6 +8,16 @@ const node_cache_1 = tslib_1.__importDefault(require("node-cache"));
|
|
|
8
8
|
const servers_1 = require("../servers");
|
|
9
9
|
const index_1 = require("../index");
|
|
10
10
|
class StartTunnelCommand {
|
|
11
|
+
getAppConfig;
|
|
12
|
+
devServer;
|
|
13
|
+
tunnelFactory;
|
|
14
|
+
tunnelClient;
|
|
15
|
+
functionHost;
|
|
16
|
+
inspector;
|
|
17
|
+
logger;
|
|
18
|
+
configFile;
|
|
19
|
+
tunnelServers = {};
|
|
20
|
+
cspReporterServer;
|
|
11
21
|
constructor(getAppConfig, devServer, tunnelFactory, tunnelClient, functionHost, inspector, logger, configFile) {
|
|
12
22
|
this.getAppConfig = getAppConfig;
|
|
13
23
|
this.devServer = devServer;
|
|
@@ -17,51 +27,58 @@ class StartTunnelCommand {
|
|
|
17
27
|
this.inspector = inspector;
|
|
18
28
|
this.logger = logger;
|
|
19
29
|
this.configFile = configFile;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
}
|
|
31
|
+
stopServices = async (appId, environmentKey) => {
|
|
32
|
+
await Promise.all([
|
|
33
|
+
this.functionHost.stopWatching(),
|
|
34
|
+
this.tunnelClient.unregisterTunnels(appId, environmentKey),
|
|
35
|
+
this.tunnelFactory.closeTunnel(),
|
|
36
|
+
this.devServer.stop(),
|
|
37
|
+
this.inspector.stopServer(),
|
|
38
|
+
...Object.values(this.tunnelServers).map((server) => server.stop()),
|
|
39
|
+
this.cspReporterServer?.stop()
|
|
40
|
+
]);
|
|
41
|
+
};
|
|
42
|
+
startFaaSTunnelServer = async ({ port, tunnelConfigPath, appId, environmentKey }) => {
|
|
43
|
+
const { permissions = {}, remotes = [] } = await this.configFile.readConfig();
|
|
44
|
+
const serverInfo = await this.devServer.start(port, permissions, remotes);
|
|
45
|
+
const { id, token, url } = await this.tunnelClient.setupTunnel(appId, environmentKey);
|
|
46
|
+
const faasTunnelUrl = await this.tunnelFactory.establishTunnel({
|
|
47
|
+
port: serverInfo.port,
|
|
48
|
+
tunnelConfigPath,
|
|
49
|
+
id,
|
|
50
|
+
token,
|
|
51
|
+
tunnelUrl: url
|
|
52
|
+
});
|
|
53
|
+
return {
|
|
54
|
+
...serverInfo,
|
|
55
|
+
tunnelUrl: faasTunnelUrl
|
|
32
56
|
};
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
57
|
+
};
|
|
58
|
+
startResourceBasedTunnelsServers = async (resourceDetails, options) => {
|
|
59
|
+
const portMap = options.resourcePortMap ?? JSON.parse(process.env.RESOURCE_PORT_MAP ?? '{}');
|
|
60
|
+
const { permissions = {}, remotes = [] } = await this.configFile.readConfig();
|
|
61
|
+
const cspReporterPort = parseInt(process.env.CSP_REPORTER_PORT ?? '4000', 10);
|
|
62
|
+
if (resourceDetails.length === 0)
|
|
63
|
+
return [];
|
|
64
|
+
this.cspReporterServer = new servers_1.CspReporterServer(cspReporterPort, this.logger, new node_cache_1.default());
|
|
65
|
+
await this.cspReporterServer.start();
|
|
66
|
+
return await Promise.all(resourceDetails.map(async (rd) => {
|
|
67
|
+
const { key, resourceType } = rd;
|
|
68
|
+
const port = portMap[key];
|
|
69
|
+
const tunnelServer = resourceType === 'nativeUI' ? servers_1.NativeUITunnelServer : servers_1.CustomUITunnelServer;
|
|
70
|
+
this.tunnelServers[key] = new tunnelServer({
|
|
71
|
+
...rd,
|
|
72
|
+
port,
|
|
73
|
+
host: options.host,
|
|
74
|
+
logger: this.logger,
|
|
75
|
+
cspReporterServerPort: cspReporterPort,
|
|
76
|
+
permissions,
|
|
77
|
+
remotes
|
|
43
78
|
});
|
|
44
|
-
return
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
var _a, _b, _c;
|
|
48
|
-
const portMap = (_a = options.resourcePortMap) !== null && _a !== void 0 ? _a : JSON.parse((_b = process.env.RESOURCE_PORT_MAP) !== null && _b !== void 0 ? _b : '{}');
|
|
49
|
-
const { permissions = {}, remotes = [] } = await this.configFile.readConfig();
|
|
50
|
-
const cspReporterPort = parseInt((_c = process.env.CSP_REPORTER_PORT) !== null && _c !== void 0 ? _c : '4000', 10);
|
|
51
|
-
if (resourceDetails.length === 0)
|
|
52
|
-
return [];
|
|
53
|
-
this.cspReporterServer = new servers_1.CspReporterServer(cspReporterPort, this.logger, new node_cache_1.default());
|
|
54
|
-
await this.cspReporterServer.start();
|
|
55
|
-
return await Promise.all(resourceDetails.map(async (rd) => {
|
|
56
|
-
const { key, resourceType } = rd;
|
|
57
|
-
const port = portMap[key];
|
|
58
|
-
const tunnelServer = resourceType === 'nativeUI' ? servers_1.NativeUITunnelServer : servers_1.CustomUITunnelServer;
|
|
59
|
-
this.tunnelServers[key] = new tunnelServer(Object.assign(Object.assign({}, rd), { port, host: options.host, logger: this.logger, cspReporterServerPort: cspReporterPort, permissions,
|
|
60
|
-
remotes }));
|
|
61
|
-
return this.tunnelServers[key].start();
|
|
62
|
-
}));
|
|
63
|
-
};
|
|
64
|
-
}
|
|
79
|
+
return this.tunnelServers[key].start();
|
|
80
|
+
}));
|
|
81
|
+
};
|
|
65
82
|
async execute(options) {
|
|
66
83
|
const { id: appId } = await this.getAppConfig();
|
|
67
84
|
const { port, environmentKey } = options;
|
|
@@ -111,7 +128,7 @@ class StartTunnelCommand {
|
|
|
111
128
|
try {
|
|
112
129
|
await this.stopServices(appId, environmentKey);
|
|
113
130
|
}
|
|
114
|
-
catch
|
|
131
|
+
catch { }
|
|
115
132
|
throw e;
|
|
116
133
|
}
|
|
117
134
|
}
|
|
@@ -9,6 +9,9 @@ const uuid_1 = require("uuid");
|
|
|
9
9
|
const runtime_1 = require("@forge/runtime");
|
|
10
10
|
const RUNNER = (0, path_1.join)(__dirname, '..', '..', 'out', 'sandbox', 'sandbox-runner.js');
|
|
11
11
|
class NodeSandbox {
|
|
12
|
+
name;
|
|
13
|
+
process;
|
|
14
|
+
callbacks;
|
|
12
15
|
constructor({ appPath, modName, handler, debugPort }) {
|
|
13
16
|
this.name = `${modName}.${handler}`;
|
|
14
17
|
const fileName = `${appPath}/${modName}.cjs`;
|
|
@@ -25,10 +28,9 @@ class NodeSandbox {
|
|
|
25
28
|
this.handleOutput(message);
|
|
26
29
|
});
|
|
27
30
|
this.process.on('message', (message) => {
|
|
28
|
-
var _a;
|
|
29
31
|
const requestId = message.requestId;
|
|
30
32
|
const result = message.result;
|
|
31
|
-
|
|
33
|
+
this.callbacks.get(requestId)?.(result);
|
|
32
34
|
});
|
|
33
35
|
}
|
|
34
36
|
handleOutput(output) {
|
|
@@ -3,8 +3,7 @@ const [_nodeBinary, _thisScript, fileName, handlerName] = process.argv;
|
|
|
3
3
|
global.__forge_tunnel__ = true;
|
|
4
4
|
const handler = require(fileName)[handlerName];
|
|
5
5
|
process.on('message', async ({ lambdaEvent, lambdaContext }) => {
|
|
6
|
-
var _a;
|
|
7
6
|
const requestId = lambdaContext.awsRequestId;
|
|
8
7
|
const result = await handler(lambdaEvent, lambdaContext);
|
|
9
|
-
|
|
8
|
+
process.send?.({ requestId, result });
|
|
10
9
|
});
|
|
@@ -5,6 +5,12 @@ const tslib_1 = require("tslib");
|
|
|
5
5
|
const express_1 = tslib_1.__importDefault(require("express"));
|
|
6
6
|
const cli_shared_1 = require("@forge/cli-shared");
|
|
7
7
|
class CspReporterServer {
|
|
8
|
+
port;
|
|
9
|
+
logger;
|
|
10
|
+
cache;
|
|
11
|
+
app;
|
|
12
|
+
server;
|
|
13
|
+
static TTL_IN_SECONDS = 30;
|
|
8
14
|
constructor(port, logger, cache) {
|
|
9
15
|
this.port = port;
|
|
10
16
|
this.logger = logger;
|
|
@@ -35,10 +41,8 @@ class CspReporterServer {
|
|
|
35
41
|
}
|
|
36
42
|
async stop() {
|
|
37
43
|
return new Promise((resolve, reject) => {
|
|
38
|
-
|
|
39
|
-
(_a = this.server) === null || _a === void 0 ? void 0 : _a.close((err) => (err ? reject(err) : resolve()));
|
|
44
|
+
this.server?.close((err) => (err ? reject(err) : resolve()));
|
|
40
45
|
});
|
|
41
46
|
}
|
|
42
47
|
}
|
|
43
48
|
exports.CspReporterServer = CspReporterServer;
|
|
44
|
-
CspReporterServer.TTL_IN_SECONDS = 30;
|
|
@@ -14,15 +14,19 @@ var ConnectionErrorCode;
|
|
|
14
14
|
ConnectionErrorCode["REFUSED"] = "ECONNREFUSED";
|
|
15
15
|
})(ConnectionErrorCode || (ConnectionErrorCode = {}));
|
|
16
16
|
class CustomUITunnelServer extends resource_tunnel_server_1.ResourceTunnelServer {
|
|
17
|
+
tunnelArgs;
|
|
18
|
+
app;
|
|
19
|
+
server;
|
|
20
|
+
proxy;
|
|
21
|
+
socket;
|
|
17
22
|
constructor(tunnelArgs) {
|
|
18
|
-
var _a, _b;
|
|
19
23
|
super(tunnelArgs);
|
|
20
24
|
this.tunnelArgs = tunnelArgs;
|
|
21
25
|
const { tunnel, permissions, remotes } = tunnelArgs;
|
|
22
26
|
this.app = (0, express_1.default)();
|
|
23
27
|
this.app.use(this.getCustomUIHtmlTransformMiddleware(permissions, remotes));
|
|
24
28
|
if (tunnel) {
|
|
25
|
-
|
|
29
|
+
net_1.default.setDefaultAutoSelectFamily?.(true);
|
|
26
30
|
const { port: tunnelPort } = tunnel;
|
|
27
31
|
this.proxy = (0, http_proxy_middleware_1.createProxyMiddleware)({
|
|
28
32
|
target: `http://${this.tunnelArgs.host}:${tunnelPort}`,
|
|
@@ -80,7 +84,7 @@ class CustomUITunnelServer extends resource_tunnel_server_1.ResourceTunnelServer
|
|
|
80
84
|
return this.app;
|
|
81
85
|
}
|
|
82
86
|
handleProxyDestinationNotStarted(err, reqUrl, res, tunnelPort) {
|
|
83
|
-
if (
|
|
87
|
+
if (err?.code !== ConnectionErrorCode.REFUSED || (reqUrl !== '/' && reqUrl !== 'index.html')) {
|
|
84
88
|
this.logger.error(err);
|
|
85
89
|
return;
|
|
86
90
|
}
|
|
@@ -91,9 +95,8 @@ class CustomUITunnelServer extends resource_tunnel_server_1.ResourceTunnelServer
|
|
|
91
95
|
res.end(cli_shared_1.Text.tunnel.error.serverNotStartedOnPort(tunnelPort));
|
|
92
96
|
}
|
|
93
97
|
handleWebsocketUpgrade(req, socket, head) {
|
|
94
|
-
var _a;
|
|
95
98
|
this.socket = socket;
|
|
96
|
-
if (
|
|
99
|
+
if (this.proxy?.upgrade) {
|
|
97
100
|
return this.proxy.upgrade(req, socket, head);
|
|
98
101
|
}
|
|
99
102
|
}
|
|
@@ -18,44 +18,19 @@ async function stopServer(server) {
|
|
|
18
18
|
}
|
|
19
19
|
exports.stopServer = stopServer;
|
|
20
20
|
class LocalDevelopmentServer {
|
|
21
|
+
invocationService;
|
|
22
|
+
logger;
|
|
23
|
+
configFile;
|
|
24
|
+
fileSystemReader;
|
|
25
|
+
app;
|
|
26
|
+
httpServer;
|
|
27
|
+
permissions;
|
|
28
|
+
remotes;
|
|
21
29
|
constructor(invocationService, logger, configFile, fileSystemReader) {
|
|
22
30
|
this.invocationService = invocationService;
|
|
23
31
|
this.logger = logger;
|
|
24
32
|
this.configFile = configFile;
|
|
25
33
|
this.fileSystemReader = fileSystemReader;
|
|
26
|
-
this.handleInvocation = async (req, res) => {
|
|
27
|
-
var _a, _b, _c, _d, _e, _f;
|
|
28
|
-
const request = req.body;
|
|
29
|
-
const envVars = (0, runtime_1.getUserVars)();
|
|
30
|
-
(_a = request.variables) === null || _a === void 0 ? void 0 : _a.forEach((item) => {
|
|
31
|
-
if (!envVars.find((i) => i.key === item.key)) {
|
|
32
|
-
envVars.push(item);
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
request.variables = [...envVars].sort((a, b) => a.key.localeCompare(b.key));
|
|
36
|
-
const allowList = (_f = (_e = (_d = (_c = (_b = this.permissions) === null || _b === void 0 ? void 0 : _b.external) === null || _c === void 0 ? void 0 : _c.fetch) === null || _d === void 0 ? void 0 : _d.backend) === null || _e === void 0 ? void 0 : _e.map((item) => {
|
|
37
|
-
var _a, _b;
|
|
38
|
-
if (typeof item === 'string')
|
|
39
|
-
return item;
|
|
40
|
-
return (_b = (_a = this.remotes) === null || _a === void 0 ? void 0 : _a.find((remote) => remote.key === item.remote)) === null || _b === void 0 ? void 0 : _b.baseUrl;
|
|
41
|
-
}).filter((item) => typeof item === 'string')) !== null && _f !== void 0 ? _f : undefined;
|
|
42
|
-
request._meta = Object.assign(Object.assign({}, request._meta), { fetchAllowList: allowList });
|
|
43
|
-
try {
|
|
44
|
-
const invokeResults = await this.invocationService.invoke(request.handler, request);
|
|
45
|
-
if ((await this.configFile.runtimeType()) === cli_shared_1.RuntimeType.nodejs && !invokeResults.success) {
|
|
46
|
-
return res.status(500).send(invokeResults.error);
|
|
47
|
-
}
|
|
48
|
-
res.json(invokeResults);
|
|
49
|
-
}
|
|
50
|
-
catch (error) {
|
|
51
|
-
try {
|
|
52
|
-
res.status(500).send((0, util_1.toLambdaError)(error));
|
|
53
|
-
}
|
|
54
|
-
catch (e) {
|
|
55
|
-
this.logger.error(e);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
34
|
this.app = (0, express_1.default)();
|
|
60
35
|
this.app.use(express_1.default.json({ limit: '6mb' }));
|
|
61
36
|
this.app.post(`/:fnKey`, this.handleInvocation);
|
|
@@ -120,5 +95,41 @@ class LocalDevelopmentServer {
|
|
|
120
95
|
getApp() {
|
|
121
96
|
return this.app;
|
|
122
97
|
}
|
|
98
|
+
handleInvocation = async (req, res) => {
|
|
99
|
+
const request = req.body;
|
|
100
|
+
const envVars = (0, runtime_1.getUserVars)();
|
|
101
|
+
request.variables?.forEach((item) => {
|
|
102
|
+
if (!envVars.find((i) => i.key === item.key)) {
|
|
103
|
+
envVars.push(item);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
request.variables = [...envVars].sort((a, b) => a.key.localeCompare(b.key));
|
|
107
|
+
const allowList = this.permissions?.external?.fetch?.backend
|
|
108
|
+
?.map((item) => {
|
|
109
|
+
if (typeof item === 'string')
|
|
110
|
+
return item;
|
|
111
|
+
return this.remotes?.find((remote) => remote.key === item.remote)?.baseUrl;
|
|
112
|
+
})
|
|
113
|
+
.filter((item) => typeof item === 'string') ?? undefined;
|
|
114
|
+
request._meta = {
|
|
115
|
+
...request._meta,
|
|
116
|
+
fetchAllowList: allowList
|
|
117
|
+
};
|
|
118
|
+
try {
|
|
119
|
+
const invokeResults = await this.invocationService.invoke(request.handler, request);
|
|
120
|
+
if ((await this.configFile.runtimeType()) === cli_shared_1.RuntimeType.nodejs && !invokeResults.success) {
|
|
121
|
+
return res.status(500).send(invokeResults.error);
|
|
122
|
+
}
|
|
123
|
+
res.json(invokeResults);
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
try {
|
|
127
|
+
res.status(500).send((0, util_1.toLambdaError)(error));
|
|
128
|
+
}
|
|
129
|
+
catch (e) {
|
|
130
|
+
this.logger.error(e);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
};
|
|
123
134
|
}
|
|
124
135
|
exports.LocalDevelopmentServer = LocalDevelopmentServer;
|
|
@@ -8,19 +8,26 @@ const resource_tunnel_server_1 = require("./resource-tunnel-server");
|
|
|
8
8
|
const cli_shared_1 = require("@forge/cli-shared");
|
|
9
9
|
const url_1 = require("url");
|
|
10
10
|
class NativeUITunnelServer extends resource_tunnel_server_1.ResourceTunnelServer {
|
|
11
|
+
tunnelArgs;
|
|
12
|
+
server;
|
|
11
13
|
constructor(tunnelArgs) {
|
|
12
14
|
super(tunnelArgs);
|
|
13
15
|
this.tunnelArgs = tunnelArgs;
|
|
14
16
|
const { key, path, port, permissions, remotes } = tunnelArgs;
|
|
15
17
|
const entrypoint = { name: key, path, functions: [] };
|
|
16
18
|
const config = (0, bundler_1.getNativeUiBuildConfig)([entrypoint]);
|
|
17
|
-
const compiler = (0, bundler_1.getCompiler)(
|
|
19
|
+
const compiler = (0, bundler_1.getCompiler)({
|
|
20
|
+
...config,
|
|
21
|
+
infrastructureLogging: {
|
|
18
22
|
level: 'error'
|
|
19
|
-
},
|
|
23
|
+
},
|
|
24
|
+
stats: 'errors-only',
|
|
25
|
+
watchOptions: {
|
|
20
26
|
aggregateTimeout: 200,
|
|
21
27
|
poll: 1000,
|
|
22
28
|
ignored: '**/node_modules'
|
|
23
|
-
}
|
|
29
|
+
}
|
|
30
|
+
});
|
|
24
31
|
this.server = new webpack_dev_server_1.default({
|
|
25
32
|
port,
|
|
26
33
|
host: process.env.FORGE_DEV_TUNNEL ? 'localhost' : '0.0.0.0',
|
|
@@ -5,31 +5,12 @@ const cli_shared_1 = require("@forge/cli-shared");
|
|
|
5
5
|
const csp_1 = require("@forge/csp");
|
|
6
6
|
const express_intercept_1 = require("express-intercept");
|
|
7
7
|
class ResourceTunnelServer {
|
|
8
|
+
port;
|
|
9
|
+
key;
|
|
10
|
+
path;
|
|
11
|
+
logger;
|
|
12
|
+
cspReporterServerPort;
|
|
8
13
|
constructor({ port, key, path, cspReporterServerPort, logger }) {
|
|
9
|
-
this.getCspHeader = (existingCsp) => new csp_1.CSPInjectionService()
|
|
10
|
-
.getInjectableCSP({
|
|
11
|
-
existingCSPDetails: existingCsp,
|
|
12
|
-
microsEnv: (0, cli_shared_1.getEnvironment)(cli_shared_1.CDNEnvironments),
|
|
13
|
-
tunnelCSPReporterUri: `http://localhost:${this.cspReporterServerPort}`
|
|
14
|
-
})
|
|
15
|
-
.join('; ');
|
|
16
|
-
this.injectGlobalBridgeScript = (htmlContent) => new cli_shared_1.BridgeScriptService().injectBridgeCore(htmlContent, () => {
|
|
17
|
-
throw new Error('Malformed HTML document');
|
|
18
|
-
});
|
|
19
|
-
this.injectIframeResizerScript = (htmlContent) => new cli_shared_1.IframeResizerScriptService().injectIframeResizer(htmlContent, () => {
|
|
20
|
-
throw new Error('Malformed HTML document');
|
|
21
|
-
});
|
|
22
|
-
this.getCustomUIHtmlTransformMiddleware = (permissions, remotes) => (0, express_intercept_1.responseHandler)()
|
|
23
|
-
.if((res) => /html/i.test(res.get('content-type')))
|
|
24
|
-
.replaceBuffer((body, _, res) => {
|
|
25
|
-
if (!res)
|
|
26
|
-
return body;
|
|
27
|
-
const htmlContentWithIframeResizerScript = this.injectIframeResizerScript(body);
|
|
28
|
-
const htmlContentWithBridgeScript = this.injectGlobalBridgeScript(htmlContentWithIframeResizerScript);
|
|
29
|
-
const cspDetails = new csp_1.CSPProcessingService({ info: () => { } }).getCspDetails(htmlContentWithBridgeScript, ResourceTunnelServer.transformPermissionsWithRemotes(permissions, remotes));
|
|
30
|
-
res.setHeader('Content-Security-Policy', this.getCspHeader(cspDetails));
|
|
31
|
-
return htmlContentWithBridgeScript;
|
|
32
|
-
});
|
|
33
14
|
this.port = port;
|
|
34
15
|
this.key = key;
|
|
35
16
|
this.path = path;
|
|
@@ -37,19 +18,43 @@ class ResourceTunnelServer {
|
|
|
37
18
|
this.cspReporterServerPort = cspReporterServerPort;
|
|
38
19
|
}
|
|
39
20
|
static transformPermissionsWithRemotes(permissions, remotes) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
var _a;
|
|
21
|
+
const client = permissions.external?.fetch?.client
|
|
22
|
+
?.map((item) => {
|
|
43
23
|
if (typeof item === 'string')
|
|
44
24
|
return item;
|
|
45
|
-
return
|
|
46
|
-
})
|
|
47
|
-
|
|
25
|
+
return remotes?.find((remote) => remote.key === item.remote)?.baseUrl;
|
|
26
|
+
})
|
|
27
|
+
.filter((item) => typeof item === 'string') ?? undefined;
|
|
28
|
+
return { ...permissions, external: { ...permissions.external, fetch: { ...permissions.external?.fetch, client } } };
|
|
48
29
|
}
|
|
49
30
|
logFileServed(reqUrl, textDecorator) {
|
|
50
31
|
if (reqUrl !== '/' && reqUrl !== '/index.html')
|
|
51
32
|
return;
|
|
52
33
|
this.logger.info(cli_shared_1.LogColor.trace(textDecorator('index.html')));
|
|
53
34
|
}
|
|
35
|
+
getCspHeader = (existingCsp) => new csp_1.CSPInjectionService()
|
|
36
|
+
.getInjectableCSP({
|
|
37
|
+
existingCSPDetails: existingCsp,
|
|
38
|
+
microsEnv: (0, cli_shared_1.getEnvironment)(cli_shared_1.CDNEnvironments),
|
|
39
|
+
tunnelCSPReporterUri: `http://localhost:${this.cspReporterServerPort}`
|
|
40
|
+
})
|
|
41
|
+
.join('; ');
|
|
42
|
+
injectGlobalBridgeScript = (htmlContent) => new cli_shared_1.BridgeScriptService().injectBridgeCore(htmlContent, () => {
|
|
43
|
+
throw new Error('Malformed HTML document');
|
|
44
|
+
});
|
|
45
|
+
injectIframeResizerScript = (htmlContent) => new cli_shared_1.IframeResizerScriptService().injectIframeResizer(htmlContent, () => {
|
|
46
|
+
throw new Error('Malformed HTML document');
|
|
47
|
+
});
|
|
48
|
+
getCustomUIHtmlTransformMiddleware = (permissions, remotes) => (0, express_intercept_1.responseHandler)()
|
|
49
|
+
.if((res) => /html/i.test(res.get('content-type')))
|
|
50
|
+
.replaceBuffer((body, _, res) => {
|
|
51
|
+
if (!res)
|
|
52
|
+
return body;
|
|
53
|
+
const htmlContentWithIframeResizerScript = this.injectIframeResizerScript(body);
|
|
54
|
+
const htmlContentWithBridgeScript = this.injectGlobalBridgeScript(htmlContentWithIframeResizerScript);
|
|
55
|
+
const cspDetails = new csp_1.CSPProcessingService({ info: () => { } }).getCspDetails(htmlContentWithBridgeScript, ResourceTunnelServer.transformPermissionsWithRemotes(permissions, remotes));
|
|
56
|
+
res.setHeader('Content-Security-Policy', this.getCspHeader(cspDetails));
|
|
57
|
+
return htmlContentWithBridgeScript;
|
|
58
|
+
});
|
|
54
59
|
}
|
|
55
60
|
exports.ResourceTunnelServer = ResourceTunnelServer;
|
|
@@ -38,6 +38,9 @@ class CloudflareError extends cli_shared_1.BaseError {
|
|
|
38
38
|
}
|
|
39
39
|
exports.CloudflareError = CloudflareError;
|
|
40
40
|
class TunnelServiceFacade {
|
|
41
|
+
featureFlagService;
|
|
42
|
+
logger;
|
|
43
|
+
instance;
|
|
41
44
|
constructor(featureFlagService, logger) {
|
|
42
45
|
this.featureFlagService = featureFlagService;
|
|
43
46
|
this.logger = logger;
|
|
@@ -59,6 +62,8 @@ class TunnelServiceFacade {
|
|
|
59
62
|
}
|
|
60
63
|
exports.TunnelServiceFacade = TunnelServiceFacade;
|
|
61
64
|
class CloudflareCreateTunnelService {
|
|
65
|
+
logger;
|
|
66
|
+
stopFunction;
|
|
62
67
|
constructor(logger) {
|
|
63
68
|
this.logger = logger;
|
|
64
69
|
}
|
|
@@ -83,13 +88,15 @@ class CloudflareCreateTunnelService {
|
|
|
83
88
|
try {
|
|
84
89
|
this.stopFunction('SIGKILL');
|
|
85
90
|
}
|
|
86
|
-
catch
|
|
91
|
+
catch { }
|
|
87
92
|
this.stopFunction = undefined;
|
|
88
93
|
}
|
|
89
94
|
}
|
|
90
95
|
}
|
|
91
96
|
exports.CloudflareCreateTunnelService = CloudflareCreateTunnelService;
|
|
92
97
|
class NgrokCreateTunnelService {
|
|
98
|
+
logger;
|
|
99
|
+
endpoint;
|
|
93
100
|
constructor(logger) {
|
|
94
101
|
this.logger = logger;
|
|
95
102
|
}
|
|
@@ -99,7 +106,7 @@ class NgrokCreateTunnelService {
|
|
|
99
106
|
try {
|
|
100
107
|
const options = {
|
|
101
108
|
addr: port,
|
|
102
|
-
configPath: tunnelConfigPath
|
|
109
|
+
configPath: tunnelConfigPath ?? defaultConfigPath,
|
|
103
110
|
onStatusChange: (status) => {
|
|
104
111
|
this.logger.debug(cli_shared_1.Text.tunnel.tunnelStatusChange(status));
|
|
105
112
|
},
|
|
@@ -110,7 +117,7 @@ class NgrokCreateTunnelService {
|
|
|
110
117
|
try {
|
|
111
118
|
ngrokErrorMessage = JSON.parse(ngrokErrorMessage);
|
|
112
119
|
}
|
|
113
|
-
catch
|
|
120
|
+
catch { }
|
|
114
121
|
}
|
|
115
122
|
this.logger.debug(data);
|
|
116
123
|
}
|
|
@@ -129,7 +136,7 @@ class NgrokCreateTunnelService {
|
|
|
129
136
|
await ngrok.disconnect();
|
|
130
137
|
await ngrok.kill();
|
|
131
138
|
}
|
|
132
|
-
catch
|
|
139
|
+
catch { }
|
|
133
140
|
this.endpoint = undefined;
|
|
134
141
|
}
|
|
135
142
|
}
|
|
@@ -8,13 +8,15 @@ const runtime_1 = require("@forge/runtime");
|
|
|
8
8
|
const cli_shared_1 = require("@forge/cli-shared");
|
|
9
9
|
const DEFAULT_INVOCATION_TIMEOUT = 25;
|
|
10
10
|
class LocalInvocationService {
|
|
11
|
+
configFile;
|
|
12
|
+
logger;
|
|
13
|
+
inspector;
|
|
11
14
|
constructor(configFile, logger, inspector) {
|
|
12
15
|
this.configFile = configFile;
|
|
13
16
|
this.logger = logger;
|
|
14
17
|
this.inspector = inspector;
|
|
15
18
|
}
|
|
16
19
|
async invoke(handler, request) {
|
|
17
|
-
var _a;
|
|
18
20
|
this.logger.info('');
|
|
19
21
|
this.logger.info((0, util_1.formatInvocation)(handler, request));
|
|
20
22
|
const sandbox = util_1.SandboxesContainer.get()[handler];
|
|
@@ -38,9 +40,9 @@ class LocalInvocationService {
|
|
|
38
40
|
variables.push({ key: 'FUNCTION_IS_SNAPSHOTTED', value: isSnapshotEnabled.toString(), secure: false });
|
|
39
41
|
}
|
|
40
42
|
const requestId = (0, util_1.getRequestId)(request, (0, uuid_1.v4)());
|
|
41
|
-
const timeout =
|
|
43
|
+
const timeout = request._meta.timeout ?? DEFAULT_INVOCATION_TIMEOUT;
|
|
42
44
|
const xenInvocationRequest = (0, runtime_1.xenInvocationRequestFactory)({
|
|
43
|
-
request:
|
|
45
|
+
request: { ...request, variables },
|
|
44
46
|
ctx: { requestId, traceId: requestId, timeout }
|
|
45
47
|
});
|
|
46
48
|
const invocationLimits = (0, runtime_1.perInvocationLimitsTrackerFactory)(xenInvocationRequest);
|
|
@@ -58,11 +60,11 @@ class LocalInvocationService {
|
|
|
58
60
|
LocalInvocationService.printMetrics(reportMetrics, metrics, this.logger);
|
|
59
61
|
return reportMetrics ? { body, metrics, success, error } : body;
|
|
60
62
|
}
|
|
63
|
+
static INTERNAL_METRICS = ['execute.setup-request-context', 'invoke.setup-isolate'];
|
|
64
|
+
static printMetrics = (shouldReportMetrics, metrics, logger) => {
|
|
65
|
+
if (shouldReportMetrics && metrics?.length) {
|
|
66
|
+
logger.debug(`Metrics: ${JSON.stringify(metrics.filter((m) => !LocalInvocationService.INTERNAL_METRICS.includes(m.name)), null, 2)}`);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
61
69
|
}
|
|
62
70
|
exports.LocalInvocationService = LocalInvocationService;
|
|
63
|
-
LocalInvocationService.INTERNAL_METRICS = ['execute.setup-request-context', 'invoke.setup-isolate'];
|
|
64
|
-
LocalInvocationService.printMetrics = (shouldReportMetrics, metrics, logger) => {
|
|
65
|
-
if (shouldReportMetrics && (metrics === null || metrics === void 0 ? void 0 : metrics.length)) {
|
|
66
|
-
logger.debug(`Metrics: ${JSON.stringify(metrics.filter((m) => !LocalInvocationService.INTERNAL_METRICS.includes(m.name)), null, 2)}`);
|
|
67
|
-
}
|
|
68
|
-
};
|
|
@@ -3,11 +3,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.RegisterTunnelServiceImpl = void 0;
|
|
4
4
|
const ONE_DAY = 1000 * 60 * 60 * 24;
|
|
5
5
|
class RegisterTunnelServiceImpl {
|
|
6
|
+
tunnelClient;
|
|
7
|
+
featureFlagService;
|
|
6
8
|
constructor(tunnelClient, featureFlagService) {
|
|
7
9
|
this.tunnelClient = tunnelClient;
|
|
8
10
|
this.featureFlagService = featureFlagService;
|
|
9
11
|
this.keepAliveActive = true;
|
|
10
12
|
}
|
|
13
|
+
keepAliveHandler;
|
|
14
|
+
keepAliveActive;
|
|
11
15
|
async registerTunnels(appId, environmentKey, tunnelDefinitions) {
|
|
12
16
|
if (!this.keepAliveActive) {
|
|
13
17
|
return;
|
|
@@ -2,9 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SandboxesContainer = void 0;
|
|
4
4
|
class SandboxesContainer {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
5
|
+
sandboxes = {};
|
|
6
|
+
static instance = new SandboxesContainer();
|
|
8
7
|
static get() {
|
|
9
8
|
return this.instance.sandboxes;
|
|
10
9
|
}
|
|
@@ -18,4 +17,3 @@ class SandboxesContainer {
|
|
|
18
17
|
}
|
|
19
18
|
}
|
|
20
19
|
exports.SandboxesContainer = SandboxesContainer;
|
|
21
|
-
SandboxesContainer.instance = new SandboxesContainer();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@forge/tunnel",
|
|
3
|
-
"version": "5.2.1-next.
|
|
3
|
+
"version": "5.2.1-next.7",
|
|
4
4
|
"description": "Tunnel functionality for Forge CLI",
|
|
5
5
|
"author": "Atlassian",
|
|
6
6
|
"license": "UNLICENSED",
|
|
@@ -11,8 +11,8 @@
|
|
|
11
11
|
"compile": "tsc -b -v"
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"@forge/bundler": "4.16.2-next.
|
|
15
|
-
"@forge/cli-shared": "5.0.0-next.
|
|
14
|
+
"@forge/bundler": "4.16.2-next.7",
|
|
15
|
+
"@forge/cli-shared": "5.0.0-next.6",
|
|
16
16
|
"@forge/csp": "3.2.1",
|
|
17
17
|
"@forge/runtime": "5.8.0",
|
|
18
18
|
"cloudflared": "^0.5.1",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@atlassian/xen-test-util": "^4.2.0",
|
|
31
|
-
"@forge/manifest": "7.3.0-next.
|
|
31
|
+
"@forge/manifest": "7.3.0-next.5",
|
|
32
32
|
"@types/express": "^4.17.21",
|
|
33
33
|
"@types/jest": "^29.5.12",
|
|
34
34
|
"@types/node": "14.18.63",
|