UNPKG

the-moby-effect

Version:
284 lines 10.5 kB
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 Stream from "effect/Stream"; 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 { JSONMessage, TypesPlugin as Plugin, RuntimePluginPrivilege as PluginPrivilege } from "../generated/index.js"; import { WithRegistryAuthHeader } from "./auth.js"; import { DockerError } from "./circular.js"; import { InternalServerError, NotFound } from "./errors.js"; import { BooleanFilter } from "./filters.js"; /** @since 1.0.0 */ export const ListFilters = /*#__PURE__*/Schema.fromJsonString(/*#__PURE__*/Schema.Struct({ capability: /*#__PURE__*/Schema.optional(/*#__PURE__*/Schema.Array(Schema.String)), enabled: /*#__PURE__*/BooleanFilter("enabled").pipe(Schema.optional) })); /** @see https://docs.docker.com/reference/api/engine/latest/#tag/Plugin/operation/PluginList */ const listPluginsEndpoint = /*#__PURE__*/HttpApiEndpoint.get("list", "/", { query: { filters: /*#__PURE__*/Schema.optional(ListFilters) }, success: /*#__PURE__*/Schema.Array(Plugin), // 200 OK error: [InternalServerError] }); /** @see https://docs.docker.com/reference/api/engine/latest/#tag/Plugin/operation/GetPluginPrivileges */ const getPrivilegesEndpoint = /*#__PURE__*/HttpApiEndpoint.get("getPrivileges", "/privileges", { query: { remote: Schema.String }, success: /*#__PURE__*/Schema.Array(PluginPrivilege), // 200 OK error: [InternalServerError] }); /** @see https://docs.docker.com/reference/api/engine/latest/#tag/Plugin/operation/PluginPull */ const pullPluginEndpoint = /*#__PURE__*/HttpApiEndpoint.post("pull", "/pull", { query: { remote: Schema.String, name: /*#__PURE__*/Schema.optional(Schema.String) }, headers: { "X-Registry-Auth": /*#__PURE__*/Schema.optional(Schema.String) }, payload: /*#__PURE__*/Schema.Array(PluginPrivilege), success: /*#__PURE__*/HttpApiSchema.StreamUint8Array(), // 200 OK (streaming response) error: [InternalServerError] }); /** @see https://docs.docker.com/reference/api/engine/latest/#tag/Plugin/operation/PluginInspect */ const inspectPluginEndpoint = /*#__PURE__*/HttpApiEndpoint.get("inspect", "/:name/json", { params: { name: Schema.String }, success: Plugin, // 200 OK error: [NotFound, // 404 No such plugin InternalServerError] }); /** @see https://docs.docker.com/reference/api/engine/latest/#tag/Plugin/operation/PluginDelete */ const deletePluginEndpoint = /*#__PURE__*/HttpApiEndpoint.delete("delete", "/:name", { params: { name: Schema.String }, query: { force: /*#__PURE__*/Schema.optional(Schema.Boolean) }, success: /*#__PURE__*/HttpApiSchema.StreamUint8Array(), // 200 OK (streaming response) error: [NotFound, // 404 No such plugin InternalServerError] }); /** @see https://docs.docker.com/reference/api/engine/latest/#tag/Plugin/operation/PluginEnable */ const enablePluginEndpoint = /*#__PURE__*/HttpApiEndpoint.post("enable", "/:name/enable", { params: { name: Schema.String }, query: { timeout: /*#__PURE__*/Schema.optional(Schema.Number) }, success: /*#__PURE__*/HttpApiSchema.Empty(200), // 200 OK error: [NotFound, // 404 No such plugin InternalServerError] }); /** @see https://docs.docker.com/reference/api/engine/latest/#tag/Plugin/operation/PluginDisable */ const disablePluginEndpoint = /*#__PURE__*/HttpApiEndpoint.post("disable", "/:name/disable", { params: { name: Schema.String }, query: { force: /*#__PURE__*/Schema.optional(Schema.Boolean) }, success: /*#__PURE__*/HttpApiSchema.Empty(200), // 200 OK error: [NotFound, // 404 No such plugin InternalServerError] }); /** @see https://docs.docker.com/reference/api/engine/latest/#tag/Plugin/operation/PluginUpgrade */ const upgradePluginEndpoint = /*#__PURE__*/HttpApiEndpoint.post("upgrade", "/:name/upgrade", { params: { name: Schema.String }, query: { remote: Schema.String }, headers: { "X-Registry-Auth": /*#__PURE__*/Schema.optional(Schema.String) }, payload: /*#__PURE__*/Schema.Array(PluginPrivilege), success: /*#__PURE__*/HttpApiSchema.StreamUint8Array(), // 200 OK (streaming json progress response) error: [NotFound, // 404 No such plugin InternalServerError] }); /** @see https://docs.docker.com/reference/api/engine/latest/#tag/Plugin/operation/PluginCreate */ const createPluginEndpoint = /*#__PURE__*/HttpApiEndpoint.post("create", "/create", { query: { name: Schema.String }, payload: /*#__PURE__*/HttpApiSchema.StreamUint8Array(), success: HttpApiSchema.NoContent, // 204 No Content error: [InternalServerError] }); /** @see https://docs.docker.com/reference/api/engine/latest/#tag/Plugin/operation/PluginPush */ const pushPluginEndpoint = /*#__PURE__*/HttpApiEndpoint.post("push", "/:name/push", { params: { name: Schema.String }, success: /*#__PURE__*/HttpApiSchema.Empty(200), // 200 OK error: [NotFound, // 404 No such plugin InternalServerError] }); /** @see https://docs.docker.com/reference/api/engine/latest/#tag/Plugin/operation/PluginSet */ const setPluginEndpoint = /*#__PURE__*/HttpApiEndpoint.post("set", "/:name/set", { params: { name: Schema.String }, payload: /*#__PURE__*/Schema.Array(Schema.String), success: HttpApiSchema.NoContent, // 204 No Content error: [NotFound, // 404 No such plugin InternalServerError] }); /** @see https://docs.docker.com/reference/api/engine/latest/#tag/Plugin */ const PluginsGroup = /*#__PURE__*/HttpApiGroup.make("plugins").add(listPluginsEndpoint, getPrivilegesEndpoint, pullPluginEndpoint, inspectPluginEndpoint, deletePluginEndpoint, enablePluginEndpoint, disablePluginEndpoint, upgradePluginEndpoint, createPluginEndpoint, pushPluginEndpoint, setPluginEndpoint).prefix("/plugins"); /** * @since 1.0.0 * @category HttpApi * @see https://docs.docker.com/reference/api/engine/latest/#tag/Plugin */ export const PluginsApi = /*#__PURE__*/HttpApi.make("PluginsApi").add(PluginsGroup); /** * @since 1.0.0 * @category Services * @see https://docs.docker.com/reference/api/engine/latest/#tag/Plugin */ export class Plugins extends /*#__PURE__*/Context.Service()("@the-moby-effect/endpoints/Plugins", { make: /*#__PURE__*/Effect.gen(function* () { const httpClient = yield* Effect.map(HttpClient.HttpClient, WithRegistryAuthHeader(pullPluginEndpoint, upgradePluginEndpoint)); const PluginsError = DockerError.WrapForModule("plugins"); const client = yield* HttpApiClient.group(PluginsApi, { group: "plugins", httpClient }); const list_ = filters => client.list({ query: { filters } }).pipe(Effect.mapError(PluginsError("list"))); const getPrivileges_ = remote => client.getPrivileges({ query: { remote } }).pipe(Effect.mapError(PluginsError("getPrivileges"))); const pull_ = (remote, options) => Effect.forEach(options?.privileges ?? [], privilege => PluginPrivilege.makeEffect(privilege)).pipe(Effect.flatMap(payload => client.pull({ headers: {}, query: { remote, name: options?.name }, payload })), Stream.unwrap, Stream.decodeText(), Stream.splitLines, Stream.mapEffect(line => Schema.decodeEffect(Schema.fromJsonString(JSONMessage))(line)), Stream.mapError(PluginsError("pull"))); const inspect_ = name => client.inspect({ params: { name } }).pipe(Effect.mapError(PluginsError("inspect"))); const delete_ = (name, options) => client.delete({ params: { name }, query: { ...options } }).pipe(Effect.mapError(PluginsError("delete"))); const enable_ = (name, options) => client.enable({ params: { name }, query: { timeout: options?.timeout ?? 0 } }).pipe(Effect.mapError(PluginsError("enable"))); const disable_ = (name, options) => client.disable({ params: { name }, query: { ...options } }).pipe(Effect.mapError(PluginsError("disable"))); const upgrade_ = (name, remote, privileges) => Effect.forEach(privileges ?? [], privilege => PluginPrivilege.makeEffect(privilege)).pipe(Effect.flatMap(payload => client.upgrade({ headers: {}, params: { name }, query: { remote }, payload })), Stream.unwrap, Stream.decodeText(), Stream.splitLines, Stream.mapEffect(line => Schema.decodeEffect(Schema.fromJsonString(JSONMessage))(line)), Stream.runDrain, Effect.mapError(PluginsError("upgrade"))); const create_ = (name, tar) => Effect.contextWith(context => client.create({ query: { name }, payload: Stream.provideContext(tar, context) })).pipe(Effect.mapError(PluginsError("create"))); const push_ = name => client.push({ params: { name } }).pipe(Effect.mapError(PluginsError("push"))); const set_ = (name, body) => client.set({ params: { name }, payload: body }).pipe(Effect.mapError(PluginsError("set"))); return { list: list_, getPrivileges: getPrivileges_, pull: pull_, inspect: inspect_, delete: delete_, enable: enable_, disable: disable_, upgrade: upgrade_, create: create_, push: push_, set: set_ }; }) }) {} /** * @since 1.0.0 * @category Layers * @see https://docs.docker.com/reference/api/engine/latest/#tag/Plugin */ export const PluginsLayer = /*#__PURE__*/Layer.effect(Plugins, Plugins.make); /** * @since 1.0.0 * @category Layers * @see https://docs.docker.com/reference/api/engine/latest/#tag/Plugin */ export const PluginsLayerLocalSocket = /*#__PURE__*/PluginsLayer.pipe(/*#__PURE__*/Layer.provide(/*#__PURE__*/makeAgnosticHttpClientLayer(/*#__PURE__*/MobyConnectionOptions.socket({ socketPath: "/var/run/docker.sock" })))); //# sourceMappingURL=plugins.js.map