UNPKG

the-moby-effect

Version:
86 lines (85 loc) 5.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.pack = void 0; var Socket = _interopRequireWildcard(require("@effect/platform/Socket")); var Channel = _interopRequireWildcard(require("effect/Channel")); var Chunk = _interopRequireWildcard(require("effect/Chunk")); 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 Match = _interopRequireWildcard(require("effect/Match")); var Option = _interopRequireWildcard(require("effect/Option")); var Predicate = _interopRequireWildcard(require("effect/Predicate")); 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 pack = exports.pack = /*#__PURE__*/Function.dual(arguments_ => "stdin" in arguments_[0] || "stdout" in arguments_[0] || "stderr" in arguments_[0], /*#__PURE__*/Effect.fnUntraced(function* (stdio, options) { const mutex = yield* Effect.makeSemaphore(1); const context = yield* Effect.context(); const capacity = options.requestedCapacity; const stdoutConsumerQueue = yield* Queue.bounded(capacity); const stderrConsumerQueue = yield* Queue.bounded(capacity); const stdinProducerQueue = yield* Queue.bounded(capacity); // Demux everything to and fro the correct places. We can touch // this more than once because it is wrapped in the mutex. const mother = (0, _raw.demuxStdioRawToSeparateSinks)(stdio, { stdin: 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), stdout: Sink.fromQueue(stdoutConsumerQueue, { shutdown: true }), stderr: Sink.fromQueue(stderrConsumerQueue, { shutdown: true }) }, { encoding: options?.encoding }).pipe(mutex.withPermitsIfAvailable(1)).pipe(Effect.asVoid).pipe(Channel.fromEffect).pipe(Channel.provideContext(context)); // Convert the streams to the multiplexed streams const textEncoder = new TextEncoder(); const encode = Function.pipe(Match.type(), Match.when(Predicate.isUint8Array, data => data), Match.when(Predicate.isString, data => textEncoder.encode(data)), Match.when(Socket.isCloseEvent, () => new Uint8Array()), Match.exhaustive); const mapOutEntry = type => data => { const encoded = encode(data); const size = encoded.length; const result = new Uint8Array(8 + encoded.length); // Set the header result[0] = type; result[1] = 0; result[2] = 0; result[3] = 0; // Set the size bytes in big-endian order result[4] = size >>> 24 & 0xff; result[5] = size >>> 16 & 0xff; result[6] = size >>> 8 & 0xff; result[7] = size & 0xff; result.set(encoded, 8); return result; }; const concurrency = { concurrent: true }; const independentStdinChannel = Channel.toQueue(stdinProducerQueue).pipe(Channel.mapInput(Function.constVoid)).pipe(Channel.mapInputError(Function.unsafeCoerce)).pipe(Channel.mapInputIn(input => Chunk.map(input, encode))).pipe(Channel.zipLeft(mother, concurrency)); const { underlying: independentStdoutChannel } = Stream.fromQueue(stdoutConsumerQueue).pipe(Stream.encodeText).pipe(Stream.map(mapOutEntry(_multiplexed.MultiplexedHeaderType.Stdout))).pipe((0, _multiplexed.multiplexedFromStreamWith)()); const { underlying: independentStderrChannel } = Stream.fromQueue(stderrConsumerQueue).pipe(Stream.encodeText).pipe(Stream.map(mapOutEntry(_multiplexed.MultiplexedHeaderType.Stderr))).pipe((0, _multiplexed.multiplexedFromStreamWith)()); const mixedOutputChannel = Channel.zip(independentStdoutChannel, independentStderrChannel, concurrency); return (0, _multiplexed.makeMultiplexedChannel)(Channel.zip(independentStdinChannel, mixedOutputChannel, concurrency)); })); //# sourceMappingURL=pack.js.map