UNPKG

@k8ts/instruments

Version:

A collection of utilities and core components for k8ts.

82 lines 2.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PortSet = void 0; const metadata_1 = require("@k8ts/metadata"); const immutable_1 = require("immutable"); const error_1 = require("./error"); const map_1 = require("./map"); const entry_1 = require("./tools/entry"); class PortSet { _map; constructor(_map = (0, immutable_1.Map)()) { this._map = _map; for (const entry of _map.values()) { metadata_1.Meta._checkNameValue(`container port '${entry.name}' (${entry.port})`, entry.name); } } _apply(f) { return new PortSet(f(this._map)); } union(other) { return new PortSet(this._map.merge(other._map)); } add(a, b, c) { if (!a) { return this; } if (c) { return this._apply(map => map.set(a, { name: a, port: +b, protocol: c.toUpperCase() })); } if (b) { return this._apply(map => map.set(a, (0, entry_1.parsePortInput)(a, b))); } return this._apply(map => { const processed = (0, entry_1.portRecordInput)(a); return map.merge(processed); }); } pick(...name) { return this._apply(map => map.filter((_, key) => name.includes(key))); } get names() { return this._map.keySeq().toArray(); } get(name) { if (!this._map.has(name)) { throw new error_1.PortError(`Port ${name} not found`); } return this._map.get(name); } get values() { return this._map; } map(mapping) { return new map_1.PortMap(this._map.map(entry => { if (!(entry.name in mapping)) { throw new error_1.PortError(`Port ${entry.name} not found in mapping`); } const portIn = mapping[entry.name]; let portVal; if (typeof portIn === "boolean") { portVal = entry.port; } else if (typeof portIn === "number") { portVal = portIn; } else { throw new error_1.PortError(`Port ${entry.name} mapping value must be a number or boolean`); } return { name: entry.name, protocol: entry.protocol, backend: entry.name, frontend: portVal }; })); } static make(input) { return new PortSet().add(input); } } exports.PortSet = PortSet; //# sourceMappingURL=set.js.map