the-moby-effect
Version:
Moby/Docker API client built using effect-ts
99 lines (98 loc) • 4.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.makeNodeSshAgent = exports.makeNodeHttpClientLayer = exports.getNodeAgent = void 0;
var Effect = _interopRequireWildcard(require("effect/Effect"));
var Function = _interopRequireWildcard(require("effect/Function"));
var Layer = _interopRequireWildcard(require("effect/Layer"));
var internalAgnostic = _interopRequireWildcard(require("./agnostic.js"));
var internalConnection = _interopRequireWildcard(require("./connection.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; }
/** @internal */
const makeNodeSshAgent = (httpLazy, ssh2Lazy, connectionOptions) => new class extends httpLazy.Agent {
// The ssh client that will be connecting to the server
sshClient;
// How to connect to the remote server and where the moby socket is located.
connectConfig;
constructor(ssh2ConnectConfig, agentOptions) {
super(agentOptions);
this.sshClient = new ssh2Lazy.Client();
this.connectConfig = ssh2ConnectConfig;
}
/**
* When creating a new connection, first start by trying to connect to
* the remote server over ssh. Then, if that was successful, we send a
* forwardOurStreamLocal request which is an OpenSSH extension that
* opens a connection to the unix domain socket at socketPath on the
* remote server and forwards traffic.
*
* @since 1.0.0
* @see https://nodejs.org/api/http.html#agentcreateconnectionoptions-callback
*/
createConnection(_options, callback) {
this.sshClient.on("ready", () => {
this.sshClient.openssh_forwardOutStreamLocal(this.connectConfig.remoteSocketPath, (error, stream) => {
if (error) {
this.sshClient.end();
return callback(error);
}
stream.once("close", () => {
stream.end();
stream.destroy();
this.sshClient.end();
});
callback(undefined, stream);
});
}).on("error", error => callback(error, undefined)).connect(this.connectConfig);
}
}(connectionOptions);
/** @internal */
exports.makeNodeSshAgent = makeNodeSshAgent;
const getNodeAgent = connectionOptions => Function.pipe(Effect.all({
httpLazy: Effect.promise(() => import("node:http")),
httpsLazy: Effect.promise(() => import("node:https")),
nodeHttpClientLazy: Effect.promise(() => import("@effect/platform-node/NodeHttpClient"))
}, {
concurrency: 3
}), Effect.flatMap(({
httpLazy,
httpsLazy,
nodeHttpClientLazy
}) => {
const AcquireNodeHttpAgent = internalConnection.MobyConnectionOptions.$match({
http: options => Effect.succeed(new httpLazy.Agent({
host: options.host,
port: options.port
})),
socket: options => Effect.succeed(new httpLazy.Agent({
socketPath: options.socketPath
})),
ssh: options => Effect.map(Effect.promise(() => import("ssh2")), ssh2Lazy => makeNodeSshAgent(httpLazy, ssh2Lazy, options)),
https: options => Effect.succeed(new httpsLazy.Agent({
ca: options.ca,
key: options.key,
cert: options.cert,
host: options.host,
port: options.port,
passphrase: options.passphrase
}))
});
const releaseNodeHttpAgent = agent => Effect.sync(() => agent.destroy());
const resource = Effect.acquireRelease(AcquireNodeHttpAgent(connectionOptions), releaseNodeHttpAgent);
return Effect.map(resource, agent => ({
http: agent,
https: agent,
[nodeHttpClientLazy.HttpAgentTypeId]: nodeHttpClientLazy.HttpAgentTypeId
}));
}));
/** @internal */
exports.getNodeAgent = getNodeAgent;
const makeNodeHttpClientLayer = connectionOptions => {
const nodeHttpClientLayer = Function.pipe(Effect.promise(() => import("@effect/platform-node/NodeHttpClient")), Effect.map(nodeHttpClientLazy => Layer.provide(nodeHttpClientLazy.layerWithoutAgent, Layer.scoped(nodeHttpClientLazy.HttpAgent, getNodeAgent(connectionOptions)))), Layer.unwrapEffect);
const agnosticHttpClientLayer = internalAgnostic.makeAgnosticLayer(connectionOptions);
return Layer.provide(agnosticHttpClientLayer, nodeHttpClientLayer);
};
exports.makeNodeHttpClientLayer = makeNodeHttpClientLayer;
//# sourceMappingURL=node.js.map