@effectionx/process 0.6.1 → 0.7.0

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.
Files changed (90) hide show
  1. package/CHANGELOG.md +269 -0
  2. package/dist/mod.d.ts +3 -0
  3. package/dist/mod.d.ts.map +1 -0
  4. package/{esm → dist}/src/daemon.d.ts +1 -1
  5. package/dist/src/daemon.d.ts.map +1 -0
  6. package/{script → dist}/src/exec/api.d.ts +2 -4
  7. package/dist/src/exec/api.d.ts.map +1 -0
  8. package/{script → dist}/src/exec/error.d.ts +1 -1
  9. package/dist/src/exec/error.d.ts.map +1 -0
  10. package/dist/src/exec/error.js +29 -0
  11. package/dist/src/exec/posix.d.ts +3 -0
  12. package/dist/src/exec/posix.d.ts.map +1 -0
  13. package/{esm → dist}/src/exec/posix.js +12 -13
  14. package/{esm → dist}/src/exec/win32.d.ts +1 -1
  15. package/dist/src/exec/win32.d.ts.map +1 -0
  16. package/{esm → dist}/src/exec/win32.js +20 -13
  17. package/{script → dist}/src/exec.d.ts +3 -3
  18. package/dist/src/exec.d.ts.map +1 -0
  19. package/{esm → dist}/src/exec.js +3 -5
  20. package/dist/src/helpers.d.ts +4 -0
  21. package/dist/src/helpers.d.ts.map +1 -0
  22. package/dist/src/helpers.js +10 -0
  23. package/dist/tsconfig.tsbuildinfo +1 -0
  24. package/mod.ts +2 -0
  25. package/package.json +25 -19
  26. package/src/daemon.ts +34 -0
  27. package/src/exec/api.ts +83 -0
  28. package/src/exec/error.ts +43 -0
  29. package/src/exec/posix.ts +133 -0
  30. package/src/exec/win32.ts +217 -0
  31. package/src/exec.ts +100 -0
  32. package/src/helpers.ts +12 -0
  33. package/test/daemon.test.ts +106 -0
  34. package/test/eventemitter.test.ts +82 -0
  35. package/test/exec.test.ts +481 -0
  36. package/test/fixtures/dump-args.js +13 -0
  37. package/test/fixtures/echo-server.ts +49 -0
  38. package/test/fixtures/hello-world-failed.js +5 -0
  39. package/test/fixtures/hello-world.js +5 -0
  40. package/test/helpers.ts +69 -0
  41. package/test/output-stream.test.ts +73 -0
  42. package/tsconfig.json +20 -0
  43. package/esm/mod.d.ts +0 -3
  44. package/esm/mod.d.ts.map +0 -1
  45. package/esm/package.json +0 -3
  46. package/esm/src/daemon.d.ts.map +0 -1
  47. package/esm/src/eventemitter.d.ts +0 -22
  48. package/esm/src/eventemitter.d.ts.map +0 -1
  49. package/esm/src/eventemitter.js +0 -40
  50. package/esm/src/exec/api.d.ts +0 -70
  51. package/esm/src/exec/api.d.ts.map +0 -1
  52. package/esm/src/exec/error.d.ts +0 -14
  53. package/esm/src/exec/error.d.ts.map +0 -1
  54. package/esm/src/exec/error.js +0 -54
  55. package/esm/src/exec/posix.d.ts +0 -3
  56. package/esm/src/exec/posix.d.ts.map +0 -1
  57. package/esm/src/exec/win32.d.ts.map +0 -1
  58. package/esm/src/exec.d.ts +0 -16
  59. package/esm/src/exec.d.ts.map +0 -1
  60. package/esm/src/helpers.d.ts +0 -12
  61. package/esm/src/helpers.d.ts.map +0 -1
  62. package/esm/src/helpers.js +0 -71
  63. package/script/mod.d.ts +0 -3
  64. package/script/mod.d.ts.map +0 -1
  65. package/script/mod.js +0 -20
  66. package/script/package.json +0 -3
  67. package/script/src/daemon.d.ts +0 -11
  68. package/script/src/daemon.d.ts.map +0 -1
  69. package/script/src/daemon.js +0 -23
  70. package/script/src/eventemitter.d.ts +0 -22
  71. package/script/src/eventemitter.d.ts.map +0 -1
  72. package/script/src/eventemitter.js +0 -44
  73. package/script/src/exec/api.d.ts.map +0 -1
  74. package/script/src/exec/api.js +0 -2
  75. package/script/src/exec/error.d.ts.map +0 -1
  76. package/script/src/exec/error.js +0 -59
  77. package/script/src/exec/posix.d.ts +0 -3
  78. package/script/src/exec/posix.d.ts.map +0 -1
  79. package/script/src/exec/posix.js +0 -117
  80. package/script/src/exec/win32.d.ts +0 -4
  81. package/script/src/exec/win32.d.ts.map +0 -1
  82. package/script/src/exec/win32.js +0 -177
  83. package/script/src/exec.d.ts.map +0 -1
  84. package/script/src/exec.js +0 -92
  85. package/script/src/helpers.d.ts +0 -12
  86. package/script/src/helpers.d.ts.map +0 -1
  87. package/script/src/helpers.js +0 -76
  88. /package/{esm → dist}/mod.js +0 -0
  89. /package/{esm → dist}/src/daemon.js +0 -0
  90. /package/{esm → dist}/src/exec/api.js +0 -0
@@ -1,117 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.createPosixProcess = void 0;
7
- const node_child_process_1 = require("node:child_process");
8
- const effection_1 = require("effection");
9
- const node_process_1 = __importDefault(require("node:process"));
10
- const eventemitter_js_1 = require("../eventemitter.js");
11
- const helpers_js_1 = require("../helpers.js");
12
- const error_js_1 = require("./error.js");
13
- const createPosixProcess = (command, options) => {
14
- return (0, effection_1.resource)(function* (provide) {
15
- let processResult = (0, effection_1.withResolvers)();
16
- // Killing all child processes started by this command is surprisingly
17
- // tricky. If a process spawns another processes and we kill the parent,
18
- // then the child process is NOT automatically killed. Instead we're using
19
- // the `detached` option to force the child into its own process group,
20
- // which all of its children in turn will inherit. By sending the signal to
21
- // `-pid` rather than `pid`, we are sending it to the entire process group
22
- // instead. This will send the signal to all processes started by the child
23
- // process.
24
- //
25
- // More information here: https://unix.stackexchange.com/questions/14815/process-descendants
26
- let childProcess = (0, node_child_process_1.spawn)(command, options.arguments || [], {
27
- detached: true,
28
- shell: options.shell,
29
- env: options.env,
30
- cwd: options.cwd,
31
- stdio: "pipe",
32
- });
33
- let { pid } = childProcess;
34
- let io = {
35
- stdout: yield* (0, helpers_js_1.useReadable)(childProcess.stdout),
36
- stderr: yield* (0, helpers_js_1.useReadable)(childProcess.stderr),
37
- stdoutDone: (0, effection_1.withResolvers)(),
38
- stderrDone: (0, effection_1.withResolvers)(),
39
- };
40
- let stdout = (0, effection_1.createSignal)();
41
- let stderr = (0, effection_1.createSignal)();
42
- yield* (0, effection_1.spawn)(function* () {
43
- let next = yield* io.stdout.next();
44
- while (!next.done) {
45
- stdout.send(next.value);
46
- next = yield* io.stdout.next();
47
- }
48
- stdout.close();
49
- io.stdoutDone.resolve();
50
- });
51
- yield* (0, effection_1.spawn)(function* () {
52
- let next = yield* io.stderr.next();
53
- while (!next.done) {
54
- stderr.send(next.value);
55
- next = yield* io.stderr.next();
56
- }
57
- stderr.close();
58
- io.stderrDone.resolve();
59
- });
60
- let stdin = {
61
- send(data) {
62
- childProcess.stdin.write(data);
63
- },
64
- };
65
- yield* (0, effection_1.spawn)(function* trapError() {
66
- let [error] = yield* (0, eventemitter_js_1.once)(childProcess, "error");
67
- processResult.resolve((0, effection_1.Err)(error));
68
- });
69
- yield* (0, effection_1.spawn)(function* () {
70
- let value = yield* (0, eventemitter_js_1.once)(childProcess, "close");
71
- processResult.resolve((0, effection_1.Ok)(value));
72
- });
73
- function* join() {
74
- let result = yield* processResult.operation;
75
- if (result.ok) {
76
- let [code, signal] = result.value;
77
- return { command, options, code, signal };
78
- }
79
- else {
80
- throw result.error;
81
- }
82
- }
83
- function* expect() {
84
- let status = yield* join();
85
- if (status.code != 0) {
86
- throw new error_js_1.ExecError(status, command, options);
87
- }
88
- else {
89
- return status;
90
- }
91
- }
92
- try {
93
- yield* provide({
94
- pid: pid,
95
- stdin,
96
- stdout,
97
- stderr,
98
- join,
99
- expect,
100
- });
101
- }
102
- finally {
103
- try {
104
- if (typeof childProcess.pid === "undefined") {
105
- // deno-lint-ignore no-unsafe-finally
106
- throw new Error("no pid for childProcess");
107
- }
108
- node_process_1.default.kill(-childProcess.pid, "SIGTERM");
109
- yield* (0, effection_1.all)([io.stdoutDone.operation, io.stderrDone.operation]);
110
- }
111
- catch (_e) {
112
- // do nothing, process is probably already dead
113
- }
114
- }
115
- });
116
- };
117
- exports.createPosixProcess = createPosixProcess;
@@ -1,4 +0,0 @@
1
- import type { CreateOSProcess } from "./api.js";
2
- export declare const createWin32Process: CreateOSProcess;
3
- export declare const isWin32: () => boolean;
4
- //# sourceMappingURL=win32.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"win32.d.ts","sourceRoot":"","sources":["../../../src/src/exec/win32.ts"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAE,eAAe,EAAwB,MAAM,UAAU,CAAC;AAkBtE,eAAO,MAAM,kBAAkB,EAAE,eA4KhC,CAAC;AAEF,eAAO,MAAM,OAAO,QAAO,OAAiC,CAAC"}
@@ -1,177 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isWin32 = exports.createWin32Process = void 0;
4
- const node_os_1 = require("node:os");
5
- const effection_1 = require("effection");
6
- // @ts-types="npm:@types/cross-spawn@6.0.6"
7
- const cross_spawn_1 = require("cross-spawn");
8
- const ctrlc_windows_1 = require("ctrlc-windows");
9
- const eventemitter_js_1 = require("../eventemitter.js");
10
- const helpers_js_1 = require("../helpers.js");
11
- const error_js_1 = require("./error.js");
12
- function* killTree(pid) {
13
- try {
14
- const killer = (0, cross_spawn_1.spawn)("cmd.exe", ["/c", "taskkill", "/PID", String(pid), "/T", "/F"], { windowsHide: true, stdio: "ignore" });
15
- yield* (0, eventemitter_js_1.once)(killer, "close");
16
- }
17
- catch (_) {
18
- // best-effort; ignore errors
19
- }
20
- }
21
- const createWin32Process = (command, options) => {
22
- return (0, effection_1.resource)(function* (provide) {
23
- let processResult = (0, effection_1.withResolvers)();
24
- // Windows-specific process spawning with different options than POSIX
25
- let childProcess = (0, cross_spawn_1.spawn)(command, options.arguments || [], {
26
- // We lose exit information and events if this is detached in windows
27
- // and it opens a window in windows+powershell.
28
- detached: false,
29
- // The `shell` option is passed to `cross-spawn` to control whether a shell is used.
30
- // On Windows, `shell: true` is necessary to run command strings, as it uses
31
- // `cmd.exe` to parse the command and find executables in the PATH.
32
- // Using a boolean `true` was previously disabled, causing ENOENT errors for
33
- // commands that were not a direct path to an executable.
34
- shell: options.shell || false,
35
- // With stdio as pipe, windows gets stuck where neither the child nor the
36
- // parent wants to close the stream, so we call it ourselves in the exit event.
37
- stdio: "pipe",
38
- // Hide the child window so that killing it will not block the parent
39
- // with a Terminate Batch Process (Y/n)
40
- windowsHide: true,
41
- env: options.env,
42
- cwd: options.cwd,
43
- });
44
- let { pid } = childProcess;
45
- let io = {
46
- stdout: yield* (0, helpers_js_1.useReadable)(childProcess.stdout),
47
- stderr: yield* (0, helpers_js_1.useReadable)(childProcess.stderr),
48
- stdoutDone: (0, effection_1.withResolvers)(),
49
- stderrDone: (0, effection_1.withResolvers)(),
50
- };
51
- const stdout = (0, effection_1.createSignal)();
52
- const stderr = (0, effection_1.createSignal)();
53
- yield* (0, effection_1.spawn)(function* () {
54
- let next = yield* io.stdout.next();
55
- while (!next.done) {
56
- stdout.send(next.value);
57
- next = yield* io.stdout.next();
58
- }
59
- stdout.close();
60
- io.stdoutDone.resolve();
61
- });
62
- yield* (0, effection_1.spawn)(function* () {
63
- let next = yield* io.stderr.next();
64
- while (!next.done) {
65
- stderr.send(next.value);
66
- next = yield* io.stderr.next();
67
- }
68
- stderr.close();
69
- io.stderrDone.resolve();
70
- });
71
- let stdin = {
72
- send(data) {
73
- childProcess.stdin.write(data);
74
- },
75
- };
76
- yield* (0, effection_1.spawn)(function* trapError() {
77
- const [error] = yield* (0, eventemitter_js_1.once)(childProcess, "error");
78
- processResult.resolve((0, effection_1.Err)(error));
79
- });
80
- yield* (0, effection_1.spawn)(function* () {
81
- let value = yield* (0, eventemitter_js_1.once)(childProcess, "close");
82
- yield* (0, effection_1.all)([io.stdoutDone.operation, io.stderrDone.operation]);
83
- processResult.resolve((0, effection_1.Ok)(value));
84
- });
85
- function* join() {
86
- let result = yield* processResult.operation;
87
- if (result.ok) {
88
- let [code, signal] = result.value;
89
- return { command, options, code, signal };
90
- }
91
- else {
92
- throw result.error;
93
- }
94
- }
95
- function* expect() {
96
- let status = yield* join();
97
- if (status.code != 0) {
98
- throw new error_js_1.ExecError(status, command, options);
99
- }
100
- else {
101
- return status;
102
- }
103
- }
104
- try {
105
- yield* provide({
106
- pid: pid,
107
- stdin,
108
- stdout,
109
- stderr,
110
- join,
111
- expect,
112
- });
113
- }
114
- finally {
115
- try {
116
- // Only try to kill the process if it hasn't exited yet
117
- if (childProcess.exitCode === null &&
118
- childProcess.signalCode === null) {
119
- if (typeof childProcess.pid === "undefined") {
120
- // deno-lint-ignore no-unsafe-finally
121
- throw new Error("no pid for childProcess");
122
- }
123
- let stdinStream = childProcess.stdin;
124
- // Try graceful shutdown with ctrlc
125
- try {
126
- (0, ctrlc_windows_1.ctrlc)(childProcess.pid);
127
- if (stdinStream.writable) {
128
- try {
129
- // Terminate batch process (Y/N)
130
- stdinStream.write("Y\n");
131
- }
132
- catch (_err) {
133
- // not much we can do here
134
- }
135
- }
136
- }
137
- catch (_err) {
138
- // ctrlc might fail
139
- }
140
- // Close stdin to allow process to exit cleanly
141
- try {
142
- stdinStream.end();
143
- }
144
- catch (_err) {
145
- // stdin might already be closed
146
- }
147
- // If process still hasn't exited, escalate
148
- if (childProcess.exitCode === null &&
149
- childProcess.signalCode === null) {
150
- // Try regular kill first
151
- try {
152
- childProcess.kill();
153
- }
154
- catch (_err) {
155
- // process might already be dead
156
- }
157
- // If still alive after kill, force-kill entire process tree
158
- // This is necessary for bash on Windows where ctrlc doesn't work
159
- // and child.kill() only kills the shell, leaving grandchildren alive
160
- if (childProcess.exitCode === null &&
161
- childProcess.signalCode === null) {
162
- yield* killTree(childProcess.pid);
163
- }
164
- }
165
- // Wait for streams to finish
166
- yield* (0, effection_1.all)([io.stdoutDone.operation, io.stderrDone.operation]);
167
- }
168
- }
169
- catch (_e) {
170
- // do nothing, process is probably already dead
171
- }
172
- }
173
- });
174
- };
175
- exports.createWin32Process = createWin32Process;
176
- const isWin32 = () => (0, node_os_1.platform)() === "win32";
177
- exports.isWin32 = isWin32;
@@ -1 +0,0 @@
1
- {"version":3,"file":"exec.d.ts","sourceRoot":"","sources":["../../src/src/exec.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,SAAS,EAAS,MAAM,WAAW,CAAC;AAClD,OAAO,KAAK,EAEV,WAAW,EAEX,OAAO,EACP,aAAa,EACd,MAAM,eAAe,CAAC;AAIvB,cAAc,eAAe,CAAC;AAC9B,cAAc,iBAAiB,CAAC;AAEhC,MAAM,WAAW,IAAK,SAAQ,SAAS,CAAC,OAAO,CAAC;IAC9C,IAAI,IAAI,SAAS,CAAC,aAAa,CAAC,CAAC;IACjC,MAAM,IAAI,SAAS,CAAC,aAAa,CAAC,CAAC;CACpC;AAUD;;;;;GAKG;AACH,wBAAgB,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,GAAE,WAAgB,GAAG,IAAI,CAiErE"}
@@ -1,92 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.exec = exec;
18
- const shellwords_1 = require("shellwords");
19
- const effection_1 = require("effection");
20
- const posix_js_1 = require("./exec/posix.js");
21
- const win32_js_1 = require("./exec/win32.js");
22
- __exportStar(require("./exec/api.js"), exports);
23
- __exportStar(require("./exec/error.js"), exports);
24
- const createProcess = (cmd, opts) => {
25
- if ((0, win32_js_1.isWin32)()) {
26
- return (0, win32_js_1.createWin32Process)(cmd, opts);
27
- }
28
- else {
29
- return (0, posix_js_1.createPosixProcess)(cmd, opts);
30
- }
31
- };
32
- /**
33
- * Execute `command` with `options`. You should use this operation for processes
34
- * that have a finite lifetime and on which you may wish to synchronize on the
35
- * exit status. If you want to start a process like a server that spins up and runs
36
- * forever, consider using `daemon()`
37
- */
38
- function exec(command, options = {}) {
39
- let [cmd, ...args] = options.shell ? [command] : (0, shellwords_1.split)(command);
40
- let opts = { ...options, arguments: args.concat(options.arguments || []) };
41
- return {
42
- *[Symbol.iterator]() {
43
- return yield* createProcess(cmd, opts);
44
- },
45
- *join() {
46
- const process = yield* createProcess(cmd, opts);
47
- let stdout = "";
48
- let stderr = "";
49
- yield* (0, effection_1.spawn)(function* () {
50
- let subscription = yield* process.stdout;
51
- let next = yield* subscription.next();
52
- while (!next.done) {
53
- stdout += next.value;
54
- next = yield* subscription.next();
55
- }
56
- });
57
- yield* (0, effection_1.spawn)(function* () {
58
- let subscription = yield* process.stderr;
59
- let next = yield* subscription.next();
60
- while (!next.done) {
61
- stderr += next.value;
62
- next = yield* subscription.next();
63
- }
64
- });
65
- let status = yield* process.join();
66
- return { ...status, stdout, stderr };
67
- },
68
- *expect() {
69
- const process = yield* createProcess(cmd, opts);
70
- let stdout = "";
71
- let stderr = "";
72
- yield* (0, effection_1.spawn)(function* () {
73
- let subscription = yield* process.stdout;
74
- let next = yield* subscription.next();
75
- while (!next.done) {
76
- stdout += next.value;
77
- next = yield* subscription.next();
78
- }
79
- });
80
- yield* (0, effection_1.spawn)(function* () {
81
- let subscription = yield* process.stderr;
82
- let next = yield* subscription.next();
83
- while (!next.done) {
84
- stderr += next.value;
85
- next = yield* subscription.next();
86
- }
87
- });
88
- let status = yield* process.expect();
89
- return { ...status, stdout, stderr };
90
- },
91
- };
92
- }
@@ -1,12 +0,0 @@
1
- import { type Operation, type Result, type Stream } from "effection";
2
- import type { Readable } from "node:stream";
3
- export type OutputStream = Stream<Uint8Array, void>;
4
- export declare function useReadable(target: Readable | null): Stream<Uint8Array, void>;
5
- interface Remainder<T> {
6
- remainder: string;
7
- result: T;
8
- }
9
- export declare function lines(): <T extends Uint8Array, TReturn>(stream: Stream<T, TReturn>) => Stream<string, Remainder<TReturn>>;
10
- export declare function box<T>(op: () => Operation<T>): Operation<Result<T>>;
11
- export {};
12
- //# sourceMappingURL=helpers.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/src/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,SAAS,EAEd,KAAK,MAAM,EACX,KAAK,MAAM,EACZ,MAAM,WAAW,CAAC;AACnB,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAE5C,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;AAEpD,wBAAgB,WAAW,CACzB,MAAM,EAAE,QAAQ,GAAG,IAAI,GACtB,MAAM,CAAC,UAAU,EAAE,IAAI,CAAC,CAoB1B;AAED,UAAU,SAAS,CAAC,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,CAAC,CAAC;CACX;AAED,wBAAgB,KAAK,IAAI,CAAC,CAAC,SAAS,UAAU,EAAE,OAAO,EACrD,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,OAAO,CAAC,KACvB,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,OAAO,CAAC,CAAC,CAyCtC;AAED,wBAAiB,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,SAAS,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAOpE"}
@@ -1,76 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useReadable = useReadable;
4
- exports.lines = lines;
5
- exports.box = box;
6
- const effection_1 = require("effection");
7
- function useReadable(target) {
8
- return (0, effection_1.resource)(function* (provide) {
9
- let signal = (0, effection_1.createSignal)();
10
- let listener = (chunk) => {
11
- signal.send(chunk);
12
- };
13
- target?.on("data", listener);
14
- target?.on("end", signal.close);
15
- try {
16
- yield* provide(yield* signal);
17
- }
18
- finally {
19
- target?.off("data", listener);
20
- target?.off("end", signal.close);
21
- signal.close();
22
- }
23
- });
24
- }
25
- function lines() {
26
- const decoder = new TextDecoder();
27
- return function (stream) {
28
- return {
29
- *[Symbol.iterator]() {
30
- let subscription = yield* stream;
31
- let buffer = [];
32
- let remainder = "";
33
- return {
34
- *next() {
35
- while (buffer.length === 0) {
36
- let next = yield* subscription.next();
37
- if (next.done) {
38
- return {
39
- done: true,
40
- value: {
41
- remainder,
42
- result: next.value,
43
- },
44
- };
45
- }
46
- else {
47
- let current = remainder + decoder.decode(next.value);
48
- let lines = current.split("\n");
49
- if (lines.length > 0) {
50
- buffer.push(...lines.slice(0, -1));
51
- remainder = lines.slice(-1)[0];
52
- }
53
- else {
54
- remainder = current;
55
- }
56
- }
57
- }
58
- return {
59
- done: false,
60
- value: buffer.pop(),
61
- };
62
- },
63
- };
64
- },
65
- };
66
- };
67
- }
68
- function* box(op) {
69
- try {
70
- let value = yield* op();
71
- return (0, effection_1.Ok)(value);
72
- }
73
- catch (e) {
74
- return (0, effection_1.Err)(e);
75
- }
76
- }
File without changes
File without changes
File without changes