@adpt/cloud
Version:
AdaptJS cloud component library
112 lines • 4.29 kB
JavaScript
"use strict";
/*
* Copyright 2019 Unbounded Systems, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = tslib_1.__importStar(require("@adpt/core"));
const ConnectTo_1 = require("../ConnectTo");
const Container_1 = require("../Container");
const env_1 = require("../env");
const NetworkService_1 = require("../NetworkService");
const Service_1 = require("../Service");
const LocalNodeImage_1 = require("./LocalNodeImage");
const defaultBuildOptions = {
runNpmScripts: "build"
};
const defaultProps = {
buildOptions: defaultBuildOptions,
connectTo: [],
deps: [],
env: {},
port: 8080,
scope: "cluster-internal",
};
/**
* A partially abstract component that builds Node.js source code into a Container
* and exposes a NetworkService.
*
* @remarks
* To use this component, the `srcDir` prop must be the path to the root of
* a Node.js project, which contains a package.json file. The component will
* build a Docker container image by:
*
* - starting with an official Node.js base image
*
* - copying `srcDir` into the container image
*
* - executing `npm run build`
*
* - setting the container CMD to execute the `main` file specified in
* package.json
*
* Abstract components:
*
* This component uses the following abstract components which must be
* replaced via style sheet rules:
*
* - {@link Service}
*
* - {@link NetworkService}
*
* - {@link Container}
*
* The {@link NetworkService} and {@link Container} components are both
* children of the {@link Service} component.
*
* Instance methods:
*
* - `hostname(): string | undefined`
*
* Returns the hostname of the NetworkService, once it is known.
*
* - `port(): number | undefined`
*
* Returns the port number of the NetworkService, once it is known.
*
* - `image():` {@link docker.ImageInfo} | `undefined`
*
* Information about the successfully built image, once it has been built.
*
* @param props - See {@link nodejs.NodeServiceProps}
* @public
*/
function NodeService(props) {
const { buildOptions, connectTo, deps, env, externalPort, port: targetPort, scope, srcDir } = props;
const buildOpts = Object.assign({}, defaultBuildOptions, buildOptions);
const netSvc = core_1.handle();
const nodeCtr = core_1.handle();
const connectEnvs = ConnectTo_1.useConnectTo(connectTo);
const finalEnv = env_1.mergeEnvPairs({ HTTP_PORT: `${targetPort}` }, connectEnvs, env);
const img = core_1.handle();
const image = core_1.useMethod(img, "latestImage");
core_1.useMethodFrom(netSvc, "hostname");
core_1.useMethodFrom(netSvc, "port");
core_1.useImperativeMethods(() => ({
image: () => image,
}));
return core_1.default.createElement(core_1.Sequence, { key: props.key },
deps,
core_1.default.createElement(LocalNodeImage_1.LocalNodeImage, { key: props.key + "-img", handle: img, srcDir: srcDir, options: buildOpts }),
connectTo,
core_1.default.createElement(Service_1.Service, { key: props.key },
core_1.default.createElement(NetworkService_1.NetworkService, { key: props.key + "-netsvc", handle: netSvc, endpoint: nodeCtr, port: externalPort || targetPort, targetPort: targetPort, scope: scope }),
core_1.default.createElement(Container_1.Container, { key: props.key, name: "node-service", handle: nodeCtr, environment: finalEnv, image: img, ports: [targetPort], imagePullPolicy: "Never" })));
}
exports.NodeService = NodeService;
exports.default = NodeService;
// TODO: The "as any" is a workaround for an api-extractor bug. See issue #185.
NodeService.defaultProps = defaultProps;
//# sourceMappingURL=NodeService.js.map