@adpt/cloud
Version:
AdaptJS cloud component library
73 lines • 2.82 kB
JavaScript
;
/*
* 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 core_1 = require("@adpt/core");
const NetworkService_1 = require("../NetworkService");
const defaultProps = NetworkService_1.NetworkService.defaultProps;
/**
* Docker network service component, compatible with the abstract
* {@link NetworkService} component.
*
* @remarks
*
* Implements the {@link NetworkServiceInstance} interface.
* In a Docker deployment, there is no actual network service object to deploy.
* So this is a "virtual" component that simply implements the required
* instance methods for a NetworkService, but renders to null.
*
* This component is typically used by {@link docker.ServiceContainerSet}. The
* {@link docker.ServiceContainerSet} component can be used to ensure the proper
* network port configuration is applied to the `props.endpoint` container.
*
* @public
*/
function NetworkService(props) {
const { endpoint = null, ip, port, scope, targetPort } = props;
if (ip) {
throw new Error(`Setting IP address not supported for docker.NetworkService`);
}
const portNum = toPortNum(port);
if (scope !== "external" && targetPort != null) {
if (toPortNum(targetPort) !== portNum) {
throw new Error(`Port number translation currently only supported ` +
`by docker.NetworkService when scope is 'external'. ` +
`(scope=${scope} port=${port}, targetPort=${targetPort})`);
}
}
core_1.useImperativeMethods(() => ({
hostname: (_scope) => {
if (!endpoint)
return undefined;
return core_1.callFirstInstanceWithMethod(endpoint, undefined, "dockerIP");
},
port: () => portNum,
}));
return null;
}
exports.NetworkService = NetworkService;
// TODO: The "as any" is a workaround for an api-extractor bug. See issue #185.
NetworkService.defaultProps = defaultProps;
function toPortNum(input) {
const num = Number(input);
if (isNaN(num) ||
!Number.isInteger(num) ||
num <= 0 ||
num >= 65536)
throw new Error(`Invalid port number ${input}`);
return num;
}
//# sourceMappingURL=NetworkService.js.map