the-moby-effect
Version:
Moby/Docker API client built using effect-ts
157 lines • 5.71 kB
JavaScript
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Schema from "effect/Schema";
import * as HttpClient from "effect/unstable/http/HttpClient";
import * as HttpApi from "effect/unstable/httpapi/HttpApi";
import * as HttpApiClient from "effect/unstable/httpapi/HttpApiClient";
import * as HttpApiEndpoint from "effect/unstable/httpapi/HttpApiEndpoint";
import * as HttpApiGroup from "effect/unstable/httpapi/HttpApiGroup";
import * as HttpApiSchema from "effect/unstable/httpapi/HttpApiSchema";
import { MobyConnectionOptions } from "../../MobyConnection.js";
import { makeAgnosticHttpClientLayer } from "../../MobyPlatforms.js";
import { responseToStreamingSocketOrFailUnsafe } from "../demux/hijack.js";
import { ContainerExecStartOptions, ContainerExecOptions as ExecConfig, ContainerExecInspect as ExecInspectResponse } from "../generated/index.js";
import { ExecIdentifier } from "../schemas/id.js";
import { DockerError } from "./circular.js";
import { BadRequest, Conflict, InternalServerError, NotFound } from "./errors.js";
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Exec/operation/ContainerExec */
const createExecEndpoint = /*#__PURE__*/HttpApiEndpoint.post("container", "/containers/:id/exec", {
params: {
id: Schema.String
},
payload: ExecConfig,
success: /*#__PURE__*/Schema.Struct({
Id: ExecIdentifier
}).pipe(/*#__PURE__*/HttpApiSchema.status(201)),
// 201 Created
error: [NotFound,
// 404 No such container
Conflict,
// 409 Container is not running
InternalServerError]
});
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Exec/operation/ExecStart */
const startExecEndpoint = /*#__PURE__*/HttpApiEndpoint.post("start", "/exec/:id/start", {
params: {
id: ExecIdentifier
},
headers: {
Upgrade: /*#__PURE__*/Schema.Literal("tcp"),
Connection: /*#__PURE__*/Schema.Literal("Upgrade")
},
payload: ContainerExecStartOptions,
success: [/*#__PURE__*/HttpApiSchema.Empty(101),
/*#__PURE__*/
// 101 Switching Protocols
HttpApiSchema.Empty(200) // 200 OK
],
error: [NotFound,
// 404 No such Exec instance
Conflict,
// 409 Container is not running
InternalServerError]
});
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Exec/operation/ExecResize */
const resizeExecEndpoint = /*#__PURE__*/HttpApiEndpoint.post("resize", "/exec/:id/resize", {
params: {
id: ExecIdentifier
},
query: {
h: Schema.Number,
w: Schema.Number
},
success: /*#__PURE__*/HttpApiSchema.Empty(200),
// 200 OK
error: [BadRequest,
// 400 Invalid parameters
NotFound,
// 404 No such Exec instance
InternalServerError]
});
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Exec/operation/ExecInspect */
const inspectExecEndpoint = /*#__PURE__*/HttpApiEndpoint.get("inspect", "/exec/:id/json", {
params: {
id: ExecIdentifier
},
success: ExecInspectResponse,
// 200 OK
error: [NotFound,
// 404 No such Exec instance
InternalServerError]
});
/** @see https://docs.docker.com/reference/api/engine/latest/#tag/Exec */
const ExecsGroup = /*#__PURE__*/HttpApiGroup.make("exec").add(createExecEndpoint, startExecEndpoint, resizeExecEndpoint, inspectExecEndpoint);
/**
* @since 1.0.0
* @category HttpApi
* @see https://docs.docker.com/reference/api/engine/latest/#tag/Exec
*/
export const ExecsApi = /*#__PURE__*/HttpApi.make("ExecsApi").add(ExecsGroup);
/**
* @since 1.0.0
* @category Services
* @see https://docs.docker.com/reference/api/engine/latest/#tag/Exec
*/
export class Execs extends /*#__PURE__*/Context.Service()("@the-moby-effect/endpoints/Execs", {
make: /*#__PURE__*/Effect.gen(function* () {
const httpClient = yield* HttpClient.HttpClient;
const ExecsError = DockerError.WrapForModule("execs");
const client = yield* HttpApiClient.group(ExecsApi, {
group: "exec",
httpClient
});
const container_ = (id, payload) => ExecConfig.makeEffect(payload).pipe(Effect.flatMap(payload => client.container({
params: {
id
},
payload
})), Effect.mapError(ExecsError("container")));
const start_ = (id, payload) => ContainerExecStartOptions.makeEffect(payload).pipe(Effect.flatMap(startOptions => client.start({
params: {
id
},
payload: startOptions,
headers: {
Connection: "Upgrade",
Upgrade: "tcp"
},
// FIXME: Broken on undici
responseMode: "response-only"
})), Effect.flatMap(responseToStreamingSocketOrFailUnsafe), Effect.map(socket => payload.Detach === true ? void 0 : socket), Effect.mapError(ExecsError("start")));
const resize_ = (id, params) => client.resize({
params: {
id
},
query: {
...params
}
}).pipe(Effect.mapError(ExecsError("resize")));
const inspect_ = id => client.inspect({
params: {
id
}
}).pipe(Effect.mapError(ExecsError("inspect")));
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
*/
export const ExecsLayer = /*#__PURE__*/Layer.effect(Execs, Execs.make);
/**
* @since 1.0.0
* @category Layers
* @see https://docs.docker.com/reference/api/engine/latest/#tag/Exec
*/
export const ExecsLayerLocalSocket = /*#__PURE__*/ExecsLayer.pipe(/*#__PURE__*/Layer.provide(/*#__PURE__*/makeAgnosticHttpClientLayer(/*#__PURE__*/MobyConnectionOptions.socket({
socketPath: "/var/run/docker.sock"
}))));
//# sourceMappingURL=execs.js.map