the-moby-effect
Version:
Moby/Docker API client built using effect-ts
69 lines (68 loc) • 4.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.fan = void 0;
var Channel = _interopRequireWildcard(require("effect/Channel"));
var Effect = _interopRequireWildcard(require("effect/Effect"));
var Either = _interopRequireWildcard(require("effect/Either"));
var Exit = _interopRequireWildcard(require("effect/Exit"));
var Function = _interopRequireWildcard(require("effect/Function"));
var Option = _interopRequireWildcard(require("effect/Option"));
var Queue = _interopRequireWildcard(require("effect/Queue"));
var Sink = _interopRequireWildcard(require("effect/Sink"));
var Stream = _interopRequireWildcard(require("effect/Stream"));
var _multiplexed = require("./multiplexed.js");
var _raw = require("./raw.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 fan = exports.fan = /*#__PURE__*/Function.dual(arguments_ => (0, _multiplexed.isMultiplexedChannel)(arguments_[0]) || (0, _multiplexed.isMultiplexedSocket)(arguments_[0]), /*#__PURE__*/Effect.fnUntraced(function* (multiplexedInput, options) {
const mutex = yield* Effect.makeSemaphore(1);
const context = yield* Effect.context();
// Internal buffers
const stdoutConsumerQueue = yield* Queue.bounded(options.requestedCapacity);
const stderrConsumerQueue = yield* Queue.bounded(options.requestedCapacity);
const stdinProducerQueue = yield* Queue.bounded(options.requestedCapacity);
// We MUST only touch the underlying once!!! Very important!!!
// Demux everything to and fro the correct places. We can touch
// this more than once because it is wrapped with a mutex.
const motherChannel = (0, _multiplexed.demuxMultiplexedToSeparateSinks)(multiplexedInput, Function.pipe(Stream.fromQueue(stdinProducerQueue, {
shutdown: true
}), Stream.map(Either.match({
onRight: Exit.succeed,
onLeft: Function.flow(Exit.mapBoth({
onFailure: Option.some,
onSuccess: Function.compose(Option.none, Exit.fail)
}), Exit.flatten)
})), Stream.flattenExitOption, Stream.flattenChunks), Sink.fromQueue(stdoutConsumerQueue, {
shutdown: true
}), Sink.fromQueue(stderrConsumerQueue, {
shutdown: true
}), {
bufferSize: 32,
encoding: options.encoding
}).pipe(mutex.withPermitsIfAvailable(1)).pipe(Effect.asVoid).pipe(Channel.fromEffect).pipe(Channel.provideContext(context));
// Any one of these will kick off the mother demux, but only one will
const independentStdinChannel = Channel.toQueue(stdinProducerQueue).pipe(Channel.mapInput(Function.constVoid)).pipe(Channel.zipLeft(motherChannel, {
concurrent: true
})).pipe(_raw.makeRawChannel);
// Any one of these will kick off the mother demux, but only one will
const independentStdoutChannel = Stream.fromQueue(stdoutConsumerQueue).pipe(Stream.encodeText).pipe((0, _raw.rawFromStreamWith)()).pipe(({
underlying
}) => underlying).pipe(Channel.zipLeft(motherChannel, {
concurrent: true
})).pipe(_raw.makeRawChannel);
// Any one of these will kick off the mother demux, but only one will
const independentStderrChannel = Stream.fromQueue(stderrConsumerQueue).pipe(Stream.encodeText).pipe((0, _raw.rawFromStreamWith)()).pipe(({
underlying
}) => underlying).pipe(Channel.zipLeft(motherChannel, {
concurrent: true
})).pipe(_raw.makeRawChannel);
return {
stdin: independentStdinChannel,
stdout: independentStdoutChannel,
stderr: independentStderrChannel
};
}));
//# sourceMappingURL=fan.js.map