Skip to content

Commit

Permalink
fix(cmd-api-server): enable version selection in plugins
Browse files Browse the repository at this point in the history
cactus-cmd-api-server can now import plugins specifying
the npm package version as a plugin option

cmd-api-server: add missing dependency bluebird

closes hyperledger-cacti#839 and hyperledger-cacti#840

Signed-off-by: Elena Izaguirre <e.izaguirre.equiza@accenture.com>
  • Loading branch information
elenaizaguirre committed Oct 1, 2021
1 parent 6deed6d commit d5855a6
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/cactus-cmd-api-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"@hyperledger/cactus-core-api": "0.10.0",
"@thream/socketio-jwt": "2.1.1",
"axios": "0.21.4",
"bluebird": "3.7.2",
"body-parser": "1.19.0",
"compression": "1.7.4",
"convict": "6.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ export class ApiServer {

const instanceId = pluginImport.options.instanceId;
const pluginPackageDir = path.join(this.pluginsPath, instanceId);
// version of the npm package
const pluginVersion = pluginImport.options.version
? "@".concat(pluginImport.options.version)
: "";
try {
await fs.mkdirp(pluginPackageDir);
this.log.debug(`${pkgName} plugin package dir: %o`, pluginPackageDir);
Expand All @@ -341,12 +345,13 @@ export class ApiServer {
lmify.setRootDir(pluginPackageDir);
this.log.debug(`Installing ${pkgName} for plugin import`, pluginImport);
const out = await lmify.install([
pkgName,
pkgName.concat(pluginVersion), // empty if no version was specified
"--production",
"--audit=false",
"--progress=false",
"--fund=false",
`--prefix=${pluginPackageDir}`,
"--save-exact",
// "--ignore-workspace-root-check",
]);
this.log.debug("%o install result: %o", pkgName, out);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import path from "path";
import test, { Test } from "tape-promise/tape";
import { v4 as uuidv4 } from "uuid";
import { LogLevelDesc } from "@hyperledger/cactus-common";
import { PluginImportType } from "@hyperledger/cactus-core-api";
import {
ApiServer,
AuthorizationProtocol,
ConfigService,
} from "@hyperledger/cactus-cmd-api-server";

const logLevel: LogLevelDesc = "TRACE";

test("can install plugins at runtime with specified version based on imports", async (t: Test) => {
const pluginsPath = path.join(
__dirname,
"../../../../../../", // walk back up to the project root
".tmp/test/test-cmd-api-server/plugin-import-with-npm-install_test/", // the dir path from the root
uuidv4(), // then a random directory to ensure proper isolation
);
const pluginManagerOptionsJson = JSON.stringify({ pluginsPath });

const configService = new ConfigService();

const apiServerOptions = configService.newExampleConfig();
apiServerOptions.pluginManagerOptionsJson = pluginManagerOptionsJson;
apiServerOptions.authorizationProtocol = AuthorizationProtocol.NONE;
apiServerOptions.configFile = "";
apiServerOptions.apiCorsDomainCsv = "*";
apiServerOptions.apiPort = 0;
apiServerOptions.cockpitPort = 0;
apiServerOptions.grpcPort = 0;
apiServerOptions.apiTlsEnabled = false;
apiServerOptions.plugins = [
{
packageName: "@hyperledger/cactus-plugin-keychain-memory",
type: PluginImportType.Local,
options: {
instanceId: uuidv4(),
keychainId: uuidv4(),
logLevel,
version: "0.9.0",
},
},
];
const config = configService.newExampleConfigConvict(apiServerOptions);

const apiServer = new ApiServer({
config: config.getProperties(),
});

const startResponse = apiServer.start();
await t.doesNotReject(
startResponse,
"failed to start API server with dynamic plugin imports configured for it...",
);
t.ok(startResponse, "startResponse truthy OK");

const packageFilePath = path.join(
pluginsPath,
apiServerOptions.plugins[0].options.instanceId,
"package.json",
);
const { dependencies } = await import(packageFilePath);
t.strictEquals(
dependencies[`${apiServerOptions.plugins[0].packageName}`],
apiServerOptions.plugins[0].options.version,
);

test.onFinish(() => apiServer.shutdown());
});
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6275,7 +6275,7 @@ blocking-proxy@^1.0.0:
dependencies:
minimist "^1.2.0"

bluebird@^3.5.0:
bluebird@3.7.2, bluebird@^3.5.0:
version "3.7.2"
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.7.2.tgz#9f229c15be272454ffa973ace0dbee79a1b0c36f"
integrity sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg==
Expand Down

0 comments on commit d5855a6

Please sign in to comment.