the-moby-effect
Version:
Moby/Docker API client built using effect-ts
76 lines • 3.16 kB
JavaScript
import * as Effect from "effect/Effect";
import * as Function from "effect/Function";
import * as Layer from "effect/Layer";
import * as internalAgnostic from "./agnostic.js";
import * as internalConnection from "./connection.js";
/** @internal */
export const makeUndiciSshConnector = connectionOptions => Effect.Do.pipe(Effect.bind("ssh2Lazy", () => Effect.promise(() => import("ssh2"))), Effect.let("acquire", ({
ssh2Lazy
}) => Effect.sync(() => new ssh2Lazy.Client())), Effect.let("release", () => client => Effect.sync(() => {
client.end();
client.destroy();
})), Effect.flatMap(({
acquire,
release
}) => Effect.acquireRelease(acquire, release)), Effect.flatMap(sshClient => Effect.async(resume => {
sshClient.once("ready", () => {
sshClient.removeAllListeners("error");
sshClient.removeAllListeners("ready");
resume(Effect.succeed(sshClient));
}).once("error", error => {
sshClient.removeAllListeners("error");
sshClient.removeAllListeners("ready");
resume(Effect.fail(error));
}).connect(connectionOptions);
})), Effect.map(sshClient => (_opts, callback) => {
sshClient.openssh_forwardOutStreamLocal(connectionOptions.remoteSocketPath, (error, socket) => {
if (error) {
return callback(error, null);
}
socket.once("close", () => {
socket.end();
socket.destroy();
});
return callback(null, socket);
});
}), Effect.orDie);
/** @internal */
export const getUndiciDispatcher = connectionOptions => Effect.flatMap(Effect.promise(() => import("undici")), undiciLazy => {
const AcquireUndiciDispatcher = internalConnection.MobyConnectionOptions.$match({
socket: options => Effect.succeed(new undiciLazy.Agent({
connect: {
socketPath: options.socketPath
}
})),
http: options => Effect.succeed(new undiciLazy.Agent({
connect: {
host: options.host,
port: options.port,
path: options.path
}
})),
https: options => Effect.succeed(new undiciLazy.Agent({
connect: {
ca: options.ca,
key: options.key,
cert: options.cert,
passphrase: options.passphrase,
host: options.host,
port: options.port,
path: options.path
}
})),
ssh: options => Effect.map(makeUndiciSshConnector(options), connector => new undiciLazy.Agent({
connect: connector
}))
});
const releaseUndiciDispatcher = dispatcher => Effect.promise(() => dispatcher.destroy());
return Effect.acquireRelease(AcquireUndiciDispatcher(connectionOptions), releaseUndiciDispatcher);
});
/** @internal */
export const makeUndiciHttpClientLayer = connectionOptions => {
const undiciLayer = Function.pipe(Effect.promise(() => import("@effect/platform-node/NodeHttpClient")), Effect.map(nodeHttpClientLazy => Layer.provide(nodeHttpClientLazy.layerUndiciWithoutDispatcher, Layer.scoped(nodeHttpClientLazy.Dispatcher, getUndiciDispatcher(connectionOptions)))), Layer.unwrapEffect);
const agnosticHttpClientLayer = internalAgnostic.makeAgnosticLayer(connectionOptions);
return Layer.provide(agnosticHttpClientLayer, undiciLayer);
};
//# sourceMappingURL=undici.js.map