@effect/platform-node 0.22.0 → 0.22.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CommandExecutor/dist/effect-platform-node-CommandExecutor.cjs.dev.js +7 -2
- package/CommandExecutor/dist/effect-platform-node-CommandExecutor.cjs.prod.js +7 -2
- package/CommandExecutor/dist/effect-platform-node-CommandExecutor.esm.js +7 -2
- package/Http/FormData/dist/effect-platform-node-Http-FormData.cjs.dev.js +8 -3
- package/Http/FormData/dist/effect-platform-node-Http-FormData.cjs.prod.js +8 -3
- package/Http/FormData/dist/effect-platform-node-Http-FormData.esm.js +8 -3
- package/Http/NodeClient/dist/effect-platform-node-Http-NodeClient.cjs.dev.js +10 -4
- package/Http/NodeClient/dist/effect-platform-node-Http-NodeClient.cjs.prod.js +10 -4
- package/Http/NodeClient/dist/effect-platform-node-Http-NodeClient.esm.js +10 -4
- package/Http/Server/dist/effect-platform-node-Http-Server.cjs.dev.js +12 -8
- package/Http/Server/dist/effect-platform-node-Http-Server.cjs.prod.js +12 -8
- package/Http/Server/dist/effect-platform-node-Http-Server.esm.js +12 -8
- package/HttpClient/dist/effect-platform-node-HttpClient.cjs.dev.js +10 -4
- package/HttpClient/dist/effect-platform-node-HttpClient.cjs.prod.js +10 -4
- package/HttpClient/dist/effect-platform-node-HttpClient.esm.js +10 -4
- package/HttpServer/dist/effect-platform-node-HttpServer.cjs.dev.js +11 -7
- package/HttpServer/dist/effect-platform-node-HttpServer.cjs.prod.js +11 -7
- package/HttpServer/dist/effect-platform-node-HttpServer.esm.js +11 -7
- package/NodeContext/dist/effect-platform-node-NodeContext.cjs.dev.js +7 -2
- package/NodeContext/dist/effect-platform-node-NodeContext.cjs.prod.js +7 -2
- package/NodeContext/dist/effect-platform-node-NodeContext.esm.js +7 -2
- package/Sink/dist/effect-platform-node-Sink.cjs.dev.js +10 -8
- package/Sink/dist/effect-platform-node-Sink.cjs.prod.js +10 -8
- package/Sink/dist/effect-platform-node-Sink.esm.js +10 -8
- package/Stream/dist/effect-platform-node-Stream.cjs.dev.js +33 -2
- package/Stream/dist/effect-platform-node-Stream.cjs.mjs +3 -0
- package/Stream/dist/effect-platform-node-Stream.cjs.prod.js +33 -2
- package/Stream/dist/effect-platform-node-Stream.esm.js +31 -3
- package/dist/{FormData-d91e8016.cjs.prod.js → FormData-33600671.cjs.prod.js} +1 -1
- package/dist/{FormData-5ea8a8b6.esm.js → FormData-aba95773.esm.js} +1 -1
- package/dist/{FormData-15af6672.cjs.dev.js → FormData-b905ea51.cjs.dev.js} +1 -1
- package/dist/{NodeClient-3432a6a8.cjs.prod.js → NodeClient-174ebaf2.cjs.prod.js} +1 -1
- package/dist/{NodeClient-ebd71893.cjs.dev.js → NodeClient-86d70074.cjs.dev.js} +1 -1
- package/dist/{NodeClient-cd56cae8.esm.js → NodeClient-f1038dc6.esm.js} +1 -1
- package/dist/{Server-5dbcee7b.cjs.prod.js → Server-015ebdb2.cjs.prod.js} +2 -2
- package/dist/{Server-c08c825c.esm.js → Server-07db176b.esm.js} +2 -2
- package/dist/{Server-5f055bfd.cjs.dev.js → Server-d00836e3.cjs.dev.js} +2 -2
- package/dist/declarations/src/Sink.d.ts +1 -9
- package/dist/declarations/src/Sink.d.ts.map +1 -1
- package/dist/declarations/src/Stream.d.ts +34 -2
- package/dist/declarations/src/Stream.d.ts.map +1 -1
- package/dist/sink-5526bd6c.cjs.prod.js +52 -0
- package/dist/sink-a2069d86.cjs.dev.js +52 -0
- package/dist/sink-f7795af1.esm.js +28 -0
- package/dist/stream-36ca9d0a.esm.js +176 -0
- package/dist/stream-c01737c4.cjs.prod.js +210 -0
- package/dist/stream-c0c7bc20.cjs.dev.js +210 -0
- package/package.json +1 -1
- package/src/Sink.ts +1 -9
- package/src/Stream.ts +52 -3
- package/src/internal/sink.ts +28 -48
- package/src/internal/stream.ts +230 -49
- package/dist/sink-bd7ef408.esm.js +0 -34
- package/dist/sink-da49e187.cjs.prod.js +0 -57
- package/dist/sink-daf9e0e5.cjs.dev.js +0 -57
- package/dist/stream-1456ece0.cjs.dev.js +0 -120
- package/dist/stream-860139d3.esm.js +0 -94
- package/dist/stream-ef8b6a66.cjs.prod.js +0 -120
- /package/dist/{formData-632b1146.cjs.dev.js → formData-00b767b9.cjs.dev.js} +0 -0
- /package/dist/{formData-dd75bbe1.esm.js → formData-3a02d09f.esm.js} +0 -0
- /package/dist/{formData-ecf6742b.cjs.prod.js → formData-59e5f494.cjs.prod.js} +0 -0
- /package/dist/{incomingMessage-86bcf94d.cjs.dev.js → incomingMessage-587c8285.cjs.dev.js} +0 -0
- /package/dist/{incomingMessage-11c9bea6.esm.js → incomingMessage-6970f455.esm.js} +0 -0
- /package/dist/{incomingMessage-f56be93e.cjs.prod.js → incomingMessage-890fef7a.cjs.prod.js} +0 -0
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import * as Channel from 'effect/Channel';
|
|
2
|
+
import * as Chunk from 'effect/Chunk';
|
|
3
|
+
import * as Effect from 'effect/Effect';
|
|
4
|
+
import * as Either from 'effect/Either';
|
|
5
|
+
import * as Exit from 'effect/Exit';
|
|
6
|
+
import { dual, pipe } from 'effect/Function';
|
|
7
|
+
import * as Queue from 'effect/Queue';
|
|
8
|
+
import * as Stream from 'effect/Stream';
|
|
9
|
+
import { SystemError } from '@effect/platform/Error';
|
|
10
|
+
|
|
11
|
+
/** @internal */
|
|
12
|
+
const fromReadable = (evaluate, onError, {
|
|
13
|
+
chunkSize
|
|
14
|
+
} = {}) => Stream.fromChannel(readChannel(evaluate, onError, chunkSize ? Number(chunkSize) : undefined));
|
|
15
|
+
|
|
16
|
+
/** @internal */
|
|
17
|
+
const toString = options => {
|
|
18
|
+
const maxBytesNumber = options.maxBytes ? Number(options.maxBytes) : undefined;
|
|
19
|
+
return Effect.acquireUseRelease(Effect.sync(() => {
|
|
20
|
+
const stream = options.readable();
|
|
21
|
+
stream.setEncoding(options.encoding ?? "utf8");
|
|
22
|
+
return stream;
|
|
23
|
+
}), stream => Effect.async(resume => {
|
|
24
|
+
let string = "";
|
|
25
|
+
let bytes = 0;
|
|
26
|
+
stream.once("error", err => {
|
|
27
|
+
resume(Effect.fail(options.onFailure(err)));
|
|
28
|
+
});
|
|
29
|
+
stream.once("end", () => {
|
|
30
|
+
resume(Effect.succeed(string));
|
|
31
|
+
});
|
|
32
|
+
stream.on("data", chunk => {
|
|
33
|
+
string += chunk;
|
|
34
|
+
bytes += Buffer.byteLength(chunk);
|
|
35
|
+
if (maxBytesNumber && bytes > maxBytesNumber) {
|
|
36
|
+
resume(Effect.fail(options.onFailure(new Error("maxBytes exceeded"))));
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}), stream => Effect.sync(() => {
|
|
40
|
+
stream.removeAllListeners();
|
|
41
|
+
if (!stream.closed) {
|
|
42
|
+
stream.destroy();
|
|
43
|
+
}
|
|
44
|
+
}));
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** @internal */
|
|
48
|
+
const toUint8Array = options => {
|
|
49
|
+
const maxBytesNumber = options.maxBytes ? Number(options.maxBytes) : undefined;
|
|
50
|
+
return Effect.acquireUseRelease(Effect.sync(options.readable), stream => Effect.async(resume => {
|
|
51
|
+
let buffer = Buffer.alloc(0);
|
|
52
|
+
let bytes = 0;
|
|
53
|
+
stream.once("error", err => {
|
|
54
|
+
resume(Effect.fail(options.onFailure(err)));
|
|
55
|
+
});
|
|
56
|
+
stream.once("end", () => {
|
|
57
|
+
resume(Effect.succeed(buffer));
|
|
58
|
+
});
|
|
59
|
+
stream.on("data", chunk => {
|
|
60
|
+
buffer = Buffer.concat([buffer, chunk]);
|
|
61
|
+
bytes += chunk.length;
|
|
62
|
+
if (maxBytesNumber && bytes > maxBytesNumber) {
|
|
63
|
+
resume(Effect.fail(options.onFailure(new Error("maxBytes exceeded"))));
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}), stream => Effect.sync(() => {
|
|
67
|
+
stream.removeAllListeners();
|
|
68
|
+
if (!stream.closed) {
|
|
69
|
+
stream.destroy();
|
|
70
|
+
}
|
|
71
|
+
}));
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/** @internal */
|
|
75
|
+
const fromDuplex = (evaluate, onError, options = {}) => Channel.acquireUseRelease(Effect.tap(Effect.zip(Effect.sync(evaluate), Queue.unbounded()), ([duplex, queue]) => readableOffer(duplex, queue, onError)), ([duplex, queue]) => Channel.embedInput(readableTake(duplex, queue, options.chunkSize ? Number(options.chunkSize) : undefined), writeInput(duplex, queue, onError, options)), ([duplex, queue]) => Effect.zipRight(Effect.sync(() => {
|
|
76
|
+
duplex.removeAllListeners();
|
|
77
|
+
if (!duplex.closed) {
|
|
78
|
+
duplex.destroy();
|
|
79
|
+
}
|
|
80
|
+
}), Queue.shutdown(queue)));
|
|
81
|
+
|
|
82
|
+
/** @internal */
|
|
83
|
+
const pipeThroughDuplex = /*#__PURE__*/dual(args => Stream.StreamTypeId in args[0], (self, duplex, onError, options) => Stream.pipeThroughChannelOrFail(self, fromDuplex(duplex, onError, options)));
|
|
84
|
+
|
|
85
|
+
/** @internal */
|
|
86
|
+
const pipeThroughSimple = /*#__PURE__*/dual(2, (self, duplex) => Stream.pipeThroughChannelOrFail(self, fromDuplex(duplex, error => SystemError({
|
|
87
|
+
module: "Stream",
|
|
88
|
+
method: "pipeThroughSimple",
|
|
89
|
+
pathOrDescriptor: "",
|
|
90
|
+
reason: "Unknown",
|
|
91
|
+
message: String(error)
|
|
92
|
+
}))));
|
|
93
|
+
const readChannel = (evaluate, onError, chunkSize) => Channel.acquireUseRelease(Effect.tap(Effect.zip(Effect.sync(evaluate), Queue.unbounded()), ([readable, queue]) => readableOffer(readable, queue, onError)), ([readable, queue]) => readableTake(readable, queue, chunkSize), ([readable, queue]) => Effect.zipRight(Effect.sync(() => {
|
|
94
|
+
readable.removeAllListeners();
|
|
95
|
+
if (!readable.closed) {
|
|
96
|
+
readable.destroy();
|
|
97
|
+
}
|
|
98
|
+
}), Queue.shutdown(queue)));
|
|
99
|
+
const writeInput = (writable, queue, onError, {
|
|
100
|
+
encoding,
|
|
101
|
+
endOnDone = true
|
|
102
|
+
} = {}) => {
|
|
103
|
+
const write = writeEffect(writable, onError, encoding);
|
|
104
|
+
const close = endOnDone ? Effect.async(resume => {
|
|
105
|
+
if (writable.closed) {
|
|
106
|
+
resume(Effect.unit);
|
|
107
|
+
} else {
|
|
108
|
+
writable.end(() => resume(Effect.unit));
|
|
109
|
+
}
|
|
110
|
+
}) : Effect.unit;
|
|
111
|
+
return {
|
|
112
|
+
awaitRead: () => Effect.unit,
|
|
113
|
+
emit: chunk => Effect.catchAllCause(write(chunk), cause => Queue.offer(queue, Either.left(Exit.failCause(cause)))),
|
|
114
|
+
error: cause => Effect.zipRight(close, Queue.offer(queue, Either.left(Exit.failCause(cause)))),
|
|
115
|
+
done: _ => close
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/** @internal */
|
|
120
|
+
const writeEffect = (writable, onError, encoding) => chunk => Effect.async(resume => {
|
|
121
|
+
const iterator = chunk[Symbol.iterator]();
|
|
122
|
+
function loop() {
|
|
123
|
+
const item = iterator.next();
|
|
124
|
+
if (item.done) {
|
|
125
|
+
resume(Effect.unit);
|
|
126
|
+
} else if (encoding) {
|
|
127
|
+
writable.write(item.value, encoding, onDone);
|
|
128
|
+
} else {
|
|
129
|
+
writable.write(item.value, onDone);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function onDone(err) {
|
|
133
|
+
if (err) {
|
|
134
|
+
resume(Effect.fail(onError(err)));
|
|
135
|
+
} else {
|
|
136
|
+
loop();
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
loop();
|
|
140
|
+
});
|
|
141
|
+
const readableOffer = (readable, queue, onError) => Effect.sync(() => {
|
|
142
|
+
readable.on("readable", () => {
|
|
143
|
+
const size = queue.unsafeSize();
|
|
144
|
+
if (size._tag === "Some" && size.value <= 0) {
|
|
145
|
+
queue.unsafeOffer(Either.right(void 0));
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
readable.on("error", err => {
|
|
149
|
+
queue.unsafeOffer(Either.left(Exit.fail(onError(err))));
|
|
150
|
+
});
|
|
151
|
+
readable.on("end", () => {
|
|
152
|
+
queue.unsafeOffer(Either.left(Exit.unit));
|
|
153
|
+
});
|
|
154
|
+
if (readable.readable) {
|
|
155
|
+
queue.unsafeOffer(Either.right(void 0));
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
const readableTake = (readable, queue, chunkSize) => {
|
|
159
|
+
const read = readChunkChannel(readable, chunkSize);
|
|
160
|
+
const loop = pipe(Channel.fromEffect(Queue.take(queue)), Channel.flatMap(Either.match({
|
|
161
|
+
onLeft: Channel.fromEffect,
|
|
162
|
+
onRight: _ => Channel.flatMap(read, () => loop)
|
|
163
|
+
})));
|
|
164
|
+
return loop;
|
|
165
|
+
};
|
|
166
|
+
const readChunkChannel = (readable, chunkSize) => Channel.flatMap(Channel.sync(() => {
|
|
167
|
+
const arr = [];
|
|
168
|
+
let chunk = readable.read(chunkSize);
|
|
169
|
+
while (chunk !== null) {
|
|
170
|
+
arr.push(chunk);
|
|
171
|
+
chunk = readable.read(chunkSize);
|
|
172
|
+
}
|
|
173
|
+
return Chunk.unsafeFromArray(arr);
|
|
174
|
+
}), Channel.write);
|
|
175
|
+
|
|
176
|
+
export { fromDuplex as a, pipeThroughSimple as b, toUint8Array as c, fromReadable as f, pipeThroughDuplex as p, toString as t, writeEffect as w };
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var Channel = require('effect/Channel');
|
|
4
|
+
var Chunk = require('effect/Chunk');
|
|
5
|
+
var Effect = require('effect/Effect');
|
|
6
|
+
var Either = require('effect/Either');
|
|
7
|
+
var Exit = require('effect/Exit');
|
|
8
|
+
var Function = require('effect/Function');
|
|
9
|
+
var Queue = require('effect/Queue');
|
|
10
|
+
var Stream = require('effect/Stream');
|
|
11
|
+
var Error$1 = require('@effect/platform/Error');
|
|
12
|
+
|
|
13
|
+
function _interopNamespace(e) {
|
|
14
|
+
if (e && e.__esModule) return e;
|
|
15
|
+
var n = Object.create(null);
|
|
16
|
+
if (e) {
|
|
17
|
+
Object.keys(e).forEach(function (k) {
|
|
18
|
+
if (k !== 'default') {
|
|
19
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
20
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
21
|
+
enumerable: true,
|
|
22
|
+
get: function () { return e[k]; }
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
n["default"] = e;
|
|
28
|
+
return Object.freeze(n);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
var Channel__namespace = /*#__PURE__*/_interopNamespace(Channel);
|
|
32
|
+
var Chunk__namespace = /*#__PURE__*/_interopNamespace(Chunk);
|
|
33
|
+
var Effect__namespace = /*#__PURE__*/_interopNamespace(Effect);
|
|
34
|
+
var Either__namespace = /*#__PURE__*/_interopNamespace(Either);
|
|
35
|
+
var Exit__namespace = /*#__PURE__*/_interopNamespace(Exit);
|
|
36
|
+
var Queue__namespace = /*#__PURE__*/_interopNamespace(Queue);
|
|
37
|
+
var Stream__namespace = /*#__PURE__*/_interopNamespace(Stream);
|
|
38
|
+
|
|
39
|
+
/** @internal */
|
|
40
|
+
const fromReadable = (evaluate, onError, {
|
|
41
|
+
chunkSize
|
|
42
|
+
} = {}) => Stream__namespace.fromChannel(readChannel(evaluate, onError, chunkSize ? Number(chunkSize) : undefined));
|
|
43
|
+
|
|
44
|
+
/** @internal */
|
|
45
|
+
const toString = options => {
|
|
46
|
+
const maxBytesNumber = options.maxBytes ? Number(options.maxBytes) : undefined;
|
|
47
|
+
return Effect__namespace.acquireUseRelease(Effect__namespace.sync(() => {
|
|
48
|
+
const stream = options.readable();
|
|
49
|
+
stream.setEncoding(options.encoding ?? "utf8");
|
|
50
|
+
return stream;
|
|
51
|
+
}), stream => Effect__namespace.async(resume => {
|
|
52
|
+
let string = "";
|
|
53
|
+
let bytes = 0;
|
|
54
|
+
stream.once("error", err => {
|
|
55
|
+
resume(Effect__namespace.fail(options.onFailure(err)));
|
|
56
|
+
});
|
|
57
|
+
stream.once("end", () => {
|
|
58
|
+
resume(Effect__namespace.succeed(string));
|
|
59
|
+
});
|
|
60
|
+
stream.on("data", chunk => {
|
|
61
|
+
string += chunk;
|
|
62
|
+
bytes += Buffer.byteLength(chunk);
|
|
63
|
+
if (maxBytesNumber && bytes > maxBytesNumber) {
|
|
64
|
+
resume(Effect__namespace.fail(options.onFailure(new Error("maxBytes exceeded"))));
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}), stream => Effect__namespace.sync(() => {
|
|
68
|
+
stream.removeAllListeners();
|
|
69
|
+
if (!stream.closed) {
|
|
70
|
+
stream.destroy();
|
|
71
|
+
}
|
|
72
|
+
}));
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/** @internal */
|
|
76
|
+
const toUint8Array = options => {
|
|
77
|
+
const maxBytesNumber = options.maxBytes ? Number(options.maxBytes) : undefined;
|
|
78
|
+
return Effect__namespace.acquireUseRelease(Effect__namespace.sync(options.readable), stream => Effect__namespace.async(resume => {
|
|
79
|
+
let buffer = Buffer.alloc(0);
|
|
80
|
+
let bytes = 0;
|
|
81
|
+
stream.once("error", err => {
|
|
82
|
+
resume(Effect__namespace.fail(options.onFailure(err)));
|
|
83
|
+
});
|
|
84
|
+
stream.once("end", () => {
|
|
85
|
+
resume(Effect__namespace.succeed(buffer));
|
|
86
|
+
});
|
|
87
|
+
stream.on("data", chunk => {
|
|
88
|
+
buffer = Buffer.concat([buffer, chunk]);
|
|
89
|
+
bytes += chunk.length;
|
|
90
|
+
if (maxBytesNumber && bytes > maxBytesNumber) {
|
|
91
|
+
resume(Effect__namespace.fail(options.onFailure(new Error("maxBytes exceeded"))));
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}), stream => Effect__namespace.sync(() => {
|
|
95
|
+
stream.removeAllListeners();
|
|
96
|
+
if (!stream.closed) {
|
|
97
|
+
stream.destroy();
|
|
98
|
+
}
|
|
99
|
+
}));
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
/** @internal */
|
|
103
|
+
const fromDuplex = (evaluate, onError, options = {}) => Channel__namespace.acquireUseRelease(Effect__namespace.tap(Effect__namespace.zip(Effect__namespace.sync(evaluate), Queue__namespace.unbounded()), ([duplex, queue]) => readableOffer(duplex, queue, onError)), ([duplex, queue]) => Channel__namespace.embedInput(readableTake(duplex, queue, options.chunkSize ? Number(options.chunkSize) : undefined), writeInput(duplex, queue, onError, options)), ([duplex, queue]) => Effect__namespace.zipRight(Effect__namespace.sync(() => {
|
|
104
|
+
duplex.removeAllListeners();
|
|
105
|
+
if (!duplex.closed) {
|
|
106
|
+
duplex.destroy();
|
|
107
|
+
}
|
|
108
|
+
}), Queue__namespace.shutdown(queue)));
|
|
109
|
+
|
|
110
|
+
/** @internal */
|
|
111
|
+
const pipeThroughDuplex = /*#__PURE__*/Function.dual(args => Stream__namespace.StreamTypeId in args[0], (self, duplex, onError, options) => Stream__namespace.pipeThroughChannelOrFail(self, fromDuplex(duplex, onError, options)));
|
|
112
|
+
|
|
113
|
+
/** @internal */
|
|
114
|
+
const pipeThroughSimple = /*#__PURE__*/Function.dual(2, (self, duplex) => Stream__namespace.pipeThroughChannelOrFail(self, fromDuplex(duplex, error => Error$1.SystemError({
|
|
115
|
+
module: "Stream",
|
|
116
|
+
method: "pipeThroughSimple",
|
|
117
|
+
pathOrDescriptor: "",
|
|
118
|
+
reason: "Unknown",
|
|
119
|
+
message: String(error)
|
|
120
|
+
}))));
|
|
121
|
+
const readChannel = (evaluate, onError, chunkSize) => Channel__namespace.acquireUseRelease(Effect__namespace.tap(Effect__namespace.zip(Effect__namespace.sync(evaluate), Queue__namespace.unbounded()), ([readable, queue]) => readableOffer(readable, queue, onError)), ([readable, queue]) => readableTake(readable, queue, chunkSize), ([readable, queue]) => Effect__namespace.zipRight(Effect__namespace.sync(() => {
|
|
122
|
+
readable.removeAllListeners();
|
|
123
|
+
if (!readable.closed) {
|
|
124
|
+
readable.destroy();
|
|
125
|
+
}
|
|
126
|
+
}), Queue__namespace.shutdown(queue)));
|
|
127
|
+
const writeInput = (writable, queue, onError, {
|
|
128
|
+
encoding,
|
|
129
|
+
endOnDone = true
|
|
130
|
+
} = {}) => {
|
|
131
|
+
const write = writeEffect(writable, onError, encoding);
|
|
132
|
+
const close = endOnDone ? Effect__namespace.async(resume => {
|
|
133
|
+
if (writable.closed) {
|
|
134
|
+
resume(Effect__namespace.unit);
|
|
135
|
+
} else {
|
|
136
|
+
writable.end(() => resume(Effect__namespace.unit));
|
|
137
|
+
}
|
|
138
|
+
}) : Effect__namespace.unit;
|
|
139
|
+
return {
|
|
140
|
+
awaitRead: () => Effect__namespace.unit,
|
|
141
|
+
emit: chunk => Effect__namespace.catchAllCause(write(chunk), cause => Queue__namespace.offer(queue, Either__namespace.left(Exit__namespace.failCause(cause)))),
|
|
142
|
+
error: cause => Effect__namespace.zipRight(close, Queue__namespace.offer(queue, Either__namespace.left(Exit__namespace.failCause(cause)))),
|
|
143
|
+
done: _ => close
|
|
144
|
+
};
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
/** @internal */
|
|
148
|
+
const writeEffect = (writable, onError, encoding) => chunk => Effect__namespace.async(resume => {
|
|
149
|
+
const iterator = chunk[Symbol.iterator]();
|
|
150
|
+
function loop() {
|
|
151
|
+
const item = iterator.next();
|
|
152
|
+
if (item.done) {
|
|
153
|
+
resume(Effect__namespace.unit);
|
|
154
|
+
} else if (encoding) {
|
|
155
|
+
writable.write(item.value, encoding, onDone);
|
|
156
|
+
} else {
|
|
157
|
+
writable.write(item.value, onDone);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
function onDone(err) {
|
|
161
|
+
if (err) {
|
|
162
|
+
resume(Effect__namespace.fail(onError(err)));
|
|
163
|
+
} else {
|
|
164
|
+
loop();
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
loop();
|
|
168
|
+
});
|
|
169
|
+
const readableOffer = (readable, queue, onError) => Effect__namespace.sync(() => {
|
|
170
|
+
readable.on("readable", () => {
|
|
171
|
+
const size = queue.unsafeSize();
|
|
172
|
+
if (size._tag === "Some" && size.value <= 0) {
|
|
173
|
+
queue.unsafeOffer(Either__namespace.right(void 0));
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
readable.on("error", err => {
|
|
177
|
+
queue.unsafeOffer(Either__namespace.left(Exit__namespace.fail(onError(err))));
|
|
178
|
+
});
|
|
179
|
+
readable.on("end", () => {
|
|
180
|
+
queue.unsafeOffer(Either__namespace.left(Exit__namespace.unit));
|
|
181
|
+
});
|
|
182
|
+
if (readable.readable) {
|
|
183
|
+
queue.unsafeOffer(Either__namespace.right(void 0));
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
const readableTake = (readable, queue, chunkSize) => {
|
|
187
|
+
const read = readChunkChannel(readable, chunkSize);
|
|
188
|
+
const loop = Function.pipe(Channel__namespace.fromEffect(Queue__namespace.take(queue)), Channel__namespace.flatMap(Either__namespace.match({
|
|
189
|
+
onLeft: Channel__namespace.fromEffect,
|
|
190
|
+
onRight: _ => Channel__namespace.flatMap(read, () => loop)
|
|
191
|
+
})));
|
|
192
|
+
return loop;
|
|
193
|
+
};
|
|
194
|
+
const readChunkChannel = (readable, chunkSize) => Channel__namespace.flatMap(Channel__namespace.sync(() => {
|
|
195
|
+
const arr = [];
|
|
196
|
+
let chunk = readable.read(chunkSize);
|
|
197
|
+
while (chunk !== null) {
|
|
198
|
+
arr.push(chunk);
|
|
199
|
+
chunk = readable.read(chunkSize);
|
|
200
|
+
}
|
|
201
|
+
return Chunk__namespace.unsafeFromArray(arr);
|
|
202
|
+
}), Channel__namespace.write);
|
|
203
|
+
|
|
204
|
+
exports.fromDuplex = fromDuplex;
|
|
205
|
+
exports.fromReadable = fromReadable;
|
|
206
|
+
exports.pipeThroughDuplex = pipeThroughDuplex;
|
|
207
|
+
exports.pipeThroughSimple = pipeThroughSimple;
|
|
208
|
+
exports.toString = toString;
|
|
209
|
+
exports.toUint8Array = toUint8Array;
|
|
210
|
+
exports.writeEffect = writeEffect;
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var Channel = require('effect/Channel');
|
|
4
|
+
var Chunk = require('effect/Chunk');
|
|
5
|
+
var Effect = require('effect/Effect');
|
|
6
|
+
var Either = require('effect/Either');
|
|
7
|
+
var Exit = require('effect/Exit');
|
|
8
|
+
var Function = require('effect/Function');
|
|
9
|
+
var Queue = require('effect/Queue');
|
|
10
|
+
var Stream = require('effect/Stream');
|
|
11
|
+
var Error$1 = require('@effect/platform/Error');
|
|
12
|
+
|
|
13
|
+
function _interopNamespace(e) {
|
|
14
|
+
if (e && e.__esModule) return e;
|
|
15
|
+
var n = Object.create(null);
|
|
16
|
+
if (e) {
|
|
17
|
+
Object.keys(e).forEach(function (k) {
|
|
18
|
+
if (k !== 'default') {
|
|
19
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
20
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
21
|
+
enumerable: true,
|
|
22
|
+
get: function () { return e[k]; }
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
n["default"] = e;
|
|
28
|
+
return Object.freeze(n);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
var Channel__namespace = /*#__PURE__*/_interopNamespace(Channel);
|
|
32
|
+
var Chunk__namespace = /*#__PURE__*/_interopNamespace(Chunk);
|
|
33
|
+
var Effect__namespace = /*#__PURE__*/_interopNamespace(Effect);
|
|
34
|
+
var Either__namespace = /*#__PURE__*/_interopNamespace(Either);
|
|
35
|
+
var Exit__namespace = /*#__PURE__*/_interopNamespace(Exit);
|
|
36
|
+
var Queue__namespace = /*#__PURE__*/_interopNamespace(Queue);
|
|
37
|
+
var Stream__namespace = /*#__PURE__*/_interopNamespace(Stream);
|
|
38
|
+
|
|
39
|
+
/** @internal */
|
|
40
|
+
const fromReadable = (evaluate, onError, {
|
|
41
|
+
chunkSize
|
|
42
|
+
} = {}) => Stream__namespace.fromChannel(readChannel(evaluate, onError, chunkSize ? Number(chunkSize) : undefined));
|
|
43
|
+
|
|
44
|
+
/** @internal */
|
|
45
|
+
const toString = options => {
|
|
46
|
+
const maxBytesNumber = options.maxBytes ? Number(options.maxBytes) : undefined;
|
|
47
|
+
return Effect__namespace.acquireUseRelease(Effect__namespace.sync(() => {
|
|
48
|
+
const stream = options.readable();
|
|
49
|
+
stream.setEncoding(options.encoding ?? "utf8");
|
|
50
|
+
return stream;
|
|
51
|
+
}), stream => Effect__namespace.async(resume => {
|
|
52
|
+
let string = "";
|
|
53
|
+
let bytes = 0;
|
|
54
|
+
stream.once("error", err => {
|
|
55
|
+
resume(Effect__namespace.fail(options.onFailure(err)));
|
|
56
|
+
});
|
|
57
|
+
stream.once("end", () => {
|
|
58
|
+
resume(Effect__namespace.succeed(string));
|
|
59
|
+
});
|
|
60
|
+
stream.on("data", chunk => {
|
|
61
|
+
string += chunk;
|
|
62
|
+
bytes += Buffer.byteLength(chunk);
|
|
63
|
+
if (maxBytesNumber && bytes > maxBytesNumber) {
|
|
64
|
+
resume(Effect__namespace.fail(options.onFailure(new Error("maxBytes exceeded"))));
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}), stream => Effect__namespace.sync(() => {
|
|
68
|
+
stream.removeAllListeners();
|
|
69
|
+
if (!stream.closed) {
|
|
70
|
+
stream.destroy();
|
|
71
|
+
}
|
|
72
|
+
}));
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/** @internal */
|
|
76
|
+
const toUint8Array = options => {
|
|
77
|
+
const maxBytesNumber = options.maxBytes ? Number(options.maxBytes) : undefined;
|
|
78
|
+
return Effect__namespace.acquireUseRelease(Effect__namespace.sync(options.readable), stream => Effect__namespace.async(resume => {
|
|
79
|
+
let buffer = Buffer.alloc(0);
|
|
80
|
+
let bytes = 0;
|
|
81
|
+
stream.once("error", err => {
|
|
82
|
+
resume(Effect__namespace.fail(options.onFailure(err)));
|
|
83
|
+
});
|
|
84
|
+
stream.once("end", () => {
|
|
85
|
+
resume(Effect__namespace.succeed(buffer));
|
|
86
|
+
});
|
|
87
|
+
stream.on("data", chunk => {
|
|
88
|
+
buffer = Buffer.concat([buffer, chunk]);
|
|
89
|
+
bytes += chunk.length;
|
|
90
|
+
if (maxBytesNumber && bytes > maxBytesNumber) {
|
|
91
|
+
resume(Effect__namespace.fail(options.onFailure(new Error("maxBytes exceeded"))));
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}), stream => Effect__namespace.sync(() => {
|
|
95
|
+
stream.removeAllListeners();
|
|
96
|
+
if (!stream.closed) {
|
|
97
|
+
stream.destroy();
|
|
98
|
+
}
|
|
99
|
+
}));
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
/** @internal */
|
|
103
|
+
const fromDuplex = (evaluate, onError, options = {}) => Channel__namespace.acquireUseRelease(Effect__namespace.tap(Effect__namespace.zip(Effect__namespace.sync(evaluate), Queue__namespace.unbounded()), ([duplex, queue]) => readableOffer(duplex, queue, onError)), ([duplex, queue]) => Channel__namespace.embedInput(readableTake(duplex, queue, options.chunkSize ? Number(options.chunkSize) : undefined), writeInput(duplex, queue, onError, options)), ([duplex, queue]) => Effect__namespace.zipRight(Effect__namespace.sync(() => {
|
|
104
|
+
duplex.removeAllListeners();
|
|
105
|
+
if (!duplex.closed) {
|
|
106
|
+
duplex.destroy();
|
|
107
|
+
}
|
|
108
|
+
}), Queue__namespace.shutdown(queue)));
|
|
109
|
+
|
|
110
|
+
/** @internal */
|
|
111
|
+
const pipeThroughDuplex = /*#__PURE__*/Function.dual(args => Stream__namespace.StreamTypeId in args[0], (self, duplex, onError, options) => Stream__namespace.pipeThroughChannelOrFail(self, fromDuplex(duplex, onError, options)));
|
|
112
|
+
|
|
113
|
+
/** @internal */
|
|
114
|
+
const pipeThroughSimple = /*#__PURE__*/Function.dual(2, (self, duplex) => Stream__namespace.pipeThroughChannelOrFail(self, fromDuplex(duplex, error => Error$1.SystemError({
|
|
115
|
+
module: "Stream",
|
|
116
|
+
method: "pipeThroughSimple",
|
|
117
|
+
pathOrDescriptor: "",
|
|
118
|
+
reason: "Unknown",
|
|
119
|
+
message: String(error)
|
|
120
|
+
}))));
|
|
121
|
+
const readChannel = (evaluate, onError, chunkSize) => Channel__namespace.acquireUseRelease(Effect__namespace.tap(Effect__namespace.zip(Effect__namespace.sync(evaluate), Queue__namespace.unbounded()), ([readable, queue]) => readableOffer(readable, queue, onError)), ([readable, queue]) => readableTake(readable, queue, chunkSize), ([readable, queue]) => Effect__namespace.zipRight(Effect__namespace.sync(() => {
|
|
122
|
+
readable.removeAllListeners();
|
|
123
|
+
if (!readable.closed) {
|
|
124
|
+
readable.destroy();
|
|
125
|
+
}
|
|
126
|
+
}), Queue__namespace.shutdown(queue)));
|
|
127
|
+
const writeInput = (writable, queue, onError, {
|
|
128
|
+
encoding,
|
|
129
|
+
endOnDone = true
|
|
130
|
+
} = {}) => {
|
|
131
|
+
const write = writeEffect(writable, onError, encoding);
|
|
132
|
+
const close = endOnDone ? Effect__namespace.async(resume => {
|
|
133
|
+
if (writable.closed) {
|
|
134
|
+
resume(Effect__namespace.unit);
|
|
135
|
+
} else {
|
|
136
|
+
writable.end(() => resume(Effect__namespace.unit));
|
|
137
|
+
}
|
|
138
|
+
}) : Effect__namespace.unit;
|
|
139
|
+
return {
|
|
140
|
+
awaitRead: () => Effect__namespace.unit,
|
|
141
|
+
emit: chunk => Effect__namespace.catchAllCause(write(chunk), cause => Queue__namespace.offer(queue, Either__namespace.left(Exit__namespace.failCause(cause)))),
|
|
142
|
+
error: cause => Effect__namespace.zipRight(close, Queue__namespace.offer(queue, Either__namespace.left(Exit__namespace.failCause(cause)))),
|
|
143
|
+
done: _ => close
|
|
144
|
+
};
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
/** @internal */
|
|
148
|
+
const writeEffect = (writable, onError, encoding) => chunk => Effect__namespace.async(resume => {
|
|
149
|
+
const iterator = chunk[Symbol.iterator]();
|
|
150
|
+
function loop() {
|
|
151
|
+
const item = iterator.next();
|
|
152
|
+
if (item.done) {
|
|
153
|
+
resume(Effect__namespace.unit);
|
|
154
|
+
} else if (encoding) {
|
|
155
|
+
writable.write(item.value, encoding, onDone);
|
|
156
|
+
} else {
|
|
157
|
+
writable.write(item.value, onDone);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
function onDone(err) {
|
|
161
|
+
if (err) {
|
|
162
|
+
resume(Effect__namespace.fail(onError(err)));
|
|
163
|
+
} else {
|
|
164
|
+
loop();
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
loop();
|
|
168
|
+
});
|
|
169
|
+
const readableOffer = (readable, queue, onError) => Effect__namespace.sync(() => {
|
|
170
|
+
readable.on("readable", () => {
|
|
171
|
+
const size = queue.unsafeSize();
|
|
172
|
+
if (size._tag === "Some" && size.value <= 0) {
|
|
173
|
+
queue.unsafeOffer(Either__namespace.right(void 0));
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
readable.on("error", err => {
|
|
177
|
+
queue.unsafeOffer(Either__namespace.left(Exit__namespace.fail(onError(err))));
|
|
178
|
+
});
|
|
179
|
+
readable.on("end", () => {
|
|
180
|
+
queue.unsafeOffer(Either__namespace.left(Exit__namespace.unit));
|
|
181
|
+
});
|
|
182
|
+
if (readable.readable) {
|
|
183
|
+
queue.unsafeOffer(Either__namespace.right(void 0));
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
const readableTake = (readable, queue, chunkSize) => {
|
|
187
|
+
const read = readChunkChannel(readable, chunkSize);
|
|
188
|
+
const loop = Function.pipe(Channel__namespace.fromEffect(Queue__namespace.take(queue)), Channel__namespace.flatMap(Either__namespace.match({
|
|
189
|
+
onLeft: Channel__namespace.fromEffect,
|
|
190
|
+
onRight: _ => Channel__namespace.flatMap(read, () => loop)
|
|
191
|
+
})));
|
|
192
|
+
return loop;
|
|
193
|
+
};
|
|
194
|
+
const readChunkChannel = (readable, chunkSize) => Channel__namespace.flatMap(Channel__namespace.sync(() => {
|
|
195
|
+
const arr = [];
|
|
196
|
+
let chunk = readable.read(chunkSize);
|
|
197
|
+
while (chunk !== null) {
|
|
198
|
+
arr.push(chunk);
|
|
199
|
+
chunk = readable.read(chunkSize);
|
|
200
|
+
}
|
|
201
|
+
return Chunk__namespace.unsafeFromArray(arr);
|
|
202
|
+
}), Channel__namespace.write);
|
|
203
|
+
|
|
204
|
+
exports.fromDuplex = fromDuplex;
|
|
205
|
+
exports.fromReadable = fromReadable;
|
|
206
|
+
exports.pipeThroughDuplex = pipeThroughDuplex;
|
|
207
|
+
exports.pipeThroughSimple = pipeThroughSimple;
|
|
208
|
+
exports.toString = toString;
|
|
209
|
+
exports.toUint8Array = toUint8Array;
|
|
210
|
+
exports.writeEffect = writeEffect;
|
package/package.json
CHANGED
package/src/Sink.ts
CHANGED
|
@@ -6,15 +6,7 @@ import type { LazyArg } from "effect/Function"
|
|
|
6
6
|
import type { Sink } from "effect/Sink"
|
|
7
7
|
import type { Writable } from "stream"
|
|
8
8
|
import * as internal from "./internal/sink"
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* @category model
|
|
12
|
-
* @since 1.0.0
|
|
13
|
-
*/
|
|
14
|
-
export interface FromWritableOptions {
|
|
15
|
-
readonly endOnClose?: boolean
|
|
16
|
-
readonly encoding?: BufferEncoding
|
|
17
|
-
}
|
|
9
|
+
import type { FromWritableOptions } from "./Stream"
|
|
18
10
|
|
|
19
11
|
/**
|
|
20
12
|
* @category constructor
|