the-moby-effect
Version:
Moby/Docker API client built using effect-ts
118 lines (117 loc) • 6.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isExecsError = exports.ExecsLayer = exports.ExecsErrorTypeId = exports.ExecsError = exports.Execs = void 0;
var PlatformError = _interopRequireWildcard(require("@effect/platform/Error"));
var HttpClient = _interopRequireWildcard(require("@effect/platform/HttpClient"));
var HttpClientRequest = _interopRequireWildcard(require("@effect/platform/HttpClientRequest"));
var HttpClientResponse = _interopRequireWildcard(require("@effect/platform/HttpClientResponse"));
var Effect = _interopRequireWildcard(require("effect/Effect"));
var Function = _interopRequireWildcard(require("effect/Function"));
var Option = _interopRequireWildcard(require("effect/Option"));
var Predicate = _interopRequireWildcard(require("effect/Predicate"));
var Schema = _interopRequireWildcard(require("effect/Schema"));
var Tuple = _interopRequireWildcard(require("effect/Tuple"));
var MobyDemux = _interopRequireWildcard(require("../../MobyDemux.js"));
var _index = require("../generated/index.js");
var _common = require("./common.js");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
/**
* @since 1.0.0
* @category Errors
*/
const ExecsErrorTypeId = exports.ExecsErrorTypeId = /*#__PURE__*/Symbol.for("@the-moby-effect/endpoints/ExecsError");
/**
* @since 1.0.0
* @category Errors
*/
const isExecsError = u => Predicate.hasProperty(u, ExecsErrorTypeId);
/**
* @since 1.0.0
* @category Errors
*/
exports.isExecsError = isExecsError;
class ExecsError extends /*#__PURE__*/PlatformError.TypeIdError(ExecsErrorTypeId, "ExecsError") {
get message() {
return `${this.method}`;
}
}
/**
* Execs service
*
* @since 1.0.0
* @category Tags
* @see https://docs.docker.com/reference/api/engine/latest/#tag/Exec
*/
exports.ExecsError = ExecsError;
class Execs extends /*#__PURE__*/Effect.Service()("@the-moby-effect/endpoints/Execs", {
accessors: false,
dependencies: [],
effect: /*#__PURE__*/Effect.gen(function* () {
const contextClient = yield* HttpClient.HttpClient;
const client = contextClient.pipe(HttpClient.filterStatusOk);
const maybeUpgradedClient = contextClient.pipe(HttpClient.filterStatus(status => status >= 200 && status < 300 || status === 101));
/**
* Create an exec instance
*
* @param execConfig - Exec configuration
* @param id - ID or name of container
* @see https://docs.docker.com/reference/api/engine/latest/#tag/Exec/operation/ContainerExec
*/
const container_ = (id, execConfig) => Function.pipe(Schema.decode(_index.ContainerExecOptions)(execConfig), Effect.map(body => Tuple.make(HttpClientRequest.post(`/containers/${encodeURIComponent(id)}/exec`), body)), Effect.flatMap(Function.tupled(HttpClientRequest.schemaBodyJson(_index.ContainerExecOptions))), Effect.flatMap(client.execute), Effect.flatMap(HttpClientResponse.schemaBodyJson(_index.IDResponse)), Effect.mapError(cause => new ExecsError({
method: "container",
cause
})));
/**
* Start an exec instance
*
* @param execStartConfig -
* @param id - Exec instance ID
* @see https://docs.docker.com/reference/api/engine/latest/#tag/Exec/operation/ExecStart
*/
const start_ = (id, execStartConfig) => {
const response = Function.pipe(Schema.decode(_index.ContainerExecOptions)(execStartConfig), Effect.map(body => Tuple.make(HttpClientRequest.post(`/exec/${encodeURIComponent(id)}/start`), body)), Effect.flatMap(Function.tupled(HttpClientRequest.schemaBodyJson(_index.ContainerExecOptions))), Effect.map(HttpClientRequest.setHeader("Upgrade", "tcp")), Effect.map(HttpClientRequest.setHeader("Connection", "Upgrade")), Effect.flatMap(maybeUpgradedClient.execute), Effect.mapError(cause => new ExecsError({
method: "start",
cause
})));
const toStreamingSock = Function.compose(MobyDemux.responseToStreamingSocketOrFailUnsafe, Effect.mapError(cause => new ExecsError({
method: "start",
cause
})));
return execStartConfig.Detach ? Function.pipe(response, Effect.asVoid) : Function.pipe(response, Effect.flatMap(toStreamingSock));
};
/**
* Resize an exec instance
*
* @param id - Exec instance ID
* @param h - Height of the TTY session in characters
* @param w - Width of the TTY session in characters
* @see https://docs.docker.com/reference/api/engine/latest/#tag/Exec/operation/ExecResize
*/
const resize_ = (id, options) => Function.pipe(HttpClientRequest.post(`/exec/${encodeURIComponent(id)}/resize`), (0, _common.maybeAddQueryParameter)("h", Option.fromNullable(options?.h)), (0, _common.maybeAddQueryParameter)("w", Option.fromNullable(options?.w)), client.execute, Effect.asVoid, Effect.mapError(cause => new ExecsError({
method: "resize",
cause
})));
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Exec/operation/ExecInspect */
const inspect_ = id => Function.pipe(HttpClientRequest.get(`/exec/${encodeURIComponent(id)}/json`), client.execute, Effect.flatMap(HttpClientResponse.schemaBodyJson(_index.ContainerExecInspect)), Effect.mapError(cause => new ExecsError({
method: "inspect",
cause
})));
return {
container: container_,
start: start_,
resize: resize_,
inspect: inspect_
};
})
}) {}
/**
* @since 1.0.0
* @category Layers
* @see https://docs.docker.com/reference/api/engine/latest/#tag/Exec
*/
exports.Execs = Execs;
const ExecsLayer = exports.ExecsLayer = Execs.Default;
//# sourceMappingURL=execs.js.map