npm package diff
Package: @forge/tunnel
Versions: 5.5.0-next.13 - 5.5.0-next.14
File: package/out/command/interactors/function-change-watcher.js
Index: package/out/command/interactors/function-change-watcher.js
===================================================================
--- package/out/command/interactors/function-change-watcher.js
+++ package/out/command/interactors/function-change-watcher.js
@@ -1,11 +1,10 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LocalFunctionHost = exports.MEMORY_LIMIT = void 0;
const tslib_1 = require("tslib");
-const path_1 = tslib_1.__importStar(require("path"));
-const fs_1 = require("fs");
-const tmp_1 = tslib_1.__importDefault(require("tmp"));
+const path_1 = tslib_1.__importDefault(require("path"));
+const promises_1 = require("fs/promises");
const cli_shared_1 = require("@forge/cli-shared");
const runtime_1 = require("@forge/runtime");
const util_1 = require("../../util");
exports.MEMORY_LIMIT = 256;
@@ -20,54 +19,37 @@
this.logger = logger;
this.snapshot = snapshot;
this.createSandbox = createSandbox;
}
- async generateBundleFiles(bundle, withSnapshot) {
- await fs_1.promises.mkdir((0, path_1.dirname)(bundle.bundleFilePath), { recursive: true });
- if (withSnapshot) {
- if (!this.snapshot) {
- throw new Error('Snapshot requested but snapshot creator not provided.');
- }
- const snapshotOutput = await this.snapshot({ isolateMemory: exports.MEMORY_LIMIT }, Buffer.from(bundle.bundleFileContent), bundle.bundleFilePath, bundle.bundleFileSourceMap ? Buffer.from(bundle.bundleFileSourceMap) : undefined);
- await fs_1.promises.writeFile(bundle.bundleFilePath + '.snapshot', Buffer.from(snapshotOutput.buffer));
- if (snapshotOutput.logs.length > 0) {
- this.snapshotLogs.push(snapshotOutput.logs);
- }
+ async generateSnapshot(filePath) {
+ if (!this.snapshot) {
+ throw new Error('Snapshot requested but snapshot creator not provided.');
}
- else {
- await fs_1.promises.writeFile(bundle.bundleFilePath, bundle.bundleFileContent);
+ const script = await (0, promises_1.readFile)(filePath);
+ const sourceMap = await (0, promises_1.readFile)(filePath + '.map').catch(() => undefined);
+ const snapshotOutput = await this.snapshot({ isolateMemory: exports.MEMORY_LIMIT }, script, filePath, sourceMap);
+ await (0, promises_1.writeFile)(filePath + '.snapshot', Buffer.from(snapshotOutput.buffer));
+ if (snapshotOutput.logs.length > 0) {
+ this.snapshotLogs.push(snapshotOutput.logs);
}
- if (bundle.bundleFileSourceMap) {
- await fs_1.promises.writeFile(bundle.bundleFileSourceMapPath, bundle.bundleFileSourceMap);
- }
}
async startWatching(bundledCode, tunnelOptions = cli_shared_1.defaultNoDebugTunnelOptions) {
const isNodeJsRuntime = (await this.configFile.runtimeType()) === cli_shared_1.RuntimeType.nodejs;
- let directory = tmp_1.default.dirSync({ prefix: 'tunnel' }).name;
- if (isNodeJsRuntime && tunnelOptions.debug) {
- directory = path_1.default.resolve(cli_shared_1.defaultDebugBundleOutputPath);
- }
+ const pathTo = (relativePath) => path_1.default.resolve(bundledCode.outputDir, relativePath);
const isSnapshotEnabled = (await this.configFile.snapshotsEnabled()) && !isNodeJsRuntime;
if (isSnapshotEnabled) {
this.logger.info('');
this.logger.info(cli_shared_1.Text.tunnel.preBundleTask(cli_shared_1.Text.tunnel.snapshotting));
this.snapshotLogs = [];
+ const modules = new Set((await this.configFile.getAppHandlers()).map(({ module }) => module));
+ for (const module of modules) {
+ const handlerPath = pathTo(`${module}.js`);
+ await this.generateSnapshot(handlerPath);
+ }
}
- const bundles = Object.keys(bundledCode.output).map((bundlePath) => {
- const sourceMapPath = `${bundlePath}.map`;
- return {
- bundleFilePath: (0, path_1.resolve)(directory, bundlePath),
- bundleFileContent: bundledCode.output[bundlePath],
- bundleFileSourceMapPath: (0, path_1.resolve)(directory, sourceMapPath),
- bundleFileSourceMap: bundledCode.sourceMap?.[sourceMapPath]
- };
- });
- const manifestFilePath = (0, path_1.resolve)(directory, cli_shared_1.manifestFileName);
+ const manifestFilePath = pathTo(cli_shared_1.manifestFileName);
const manifestContents = JSON.stringify(await this.configFile.readConfig());
- const allPromises = [];
- bundles.forEach((bundle) => allPromises.push(this.generateBundleFiles(bundle, isSnapshotEnabled)));
- allPromises.push(fs_1.promises.writeFile(manifestFilePath, manifestContents));
- await Promise.all(allPromises);
+ await (0, promises_1.writeFile)(manifestFilePath, manifestContents);
if (isSnapshotEnabled) {
if (this.snapshotLogs.length > 0) {
this.logger.info('');
this.snapshotLogs.forEach((snapshotLog) => {
@@ -81,9 +63,9 @@
this.logger.info(cli_shared_1.LogColor.trace(cli_shared_1.Text.tunnel.snapshottingNoLogs));
this.logger.info('');
}
}
- await this.initializeSandboxes(directory, tunnelOptions);
+ await this.initializeSandboxes(bundledCode.outputDir, tunnelOptions);
runtime_1.StaticInvocationEventEmitter.addListener(runtime_1.EVENT_P3_LOG, this.onRuntimeLog);
}
async stopWatching() {
runtime_1.StaticInvocationEventEmitter.removeAllListeners();