toolcraft-openapi 0.0.168 → 0.0.169

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.
@@ -33,12 +33,12 @@
33
33
  },
34
34
  {
35
35
  "name": "toolcraft-openapi",
36
- "version": "0.0.168",
36
+ "version": "0.0.169",
37
37
  "license": "MIT"
38
38
  },
39
39
  {
40
40
  "name": "toolcraft-schema",
41
- "version": "0.0.168",
41
+ "version": "0.0.169",
42
42
  "license": "MIT"
43
43
  },
44
44
  {
@@ -1,9 +1,12 @@
1
1
  import { EventEmitter } from "node:events";
2
2
  import * as readline from "node:readline";
3
+ import { Readable } from "node:stream";
4
+ import { StringDecoder } from "node:string_decoder";
3
5
  import { graphemes } from "../../dashboard/terminal-width.js";
4
6
  import { CANCEL } from "./cancel-symbol.js";
5
7
  import { mapKey } from "./keys.js";
6
8
  import { wrapFrame } from "./wrap.js";
9
+ const pendingCarriageReturns = new WeakMap();
7
10
  const cursor = {
8
11
  hide: "\x1b[?25l",
9
12
  show: "\x1b[?25h",
@@ -128,6 +131,88 @@ export class Prompt extends EventEmitter {
128
131
  this.state = "cancel";
129
132
  return Promise.resolve(CANCEL);
130
133
  }
134
+ const input = this.input;
135
+ if (input instanceof Readable) {
136
+ return new Promise((resolve, reject) => {
137
+ const decoder = new StringDecoder("utf8");
138
+ let line = "";
139
+ let settled = false;
140
+ const settle = (value, remainder) => {
141
+ if (settled)
142
+ return;
143
+ settled = true;
144
+ input.removeListener("data", onData);
145
+ input.removeListener("end", onEnd);
146
+ input.removeListener("close", onCancel);
147
+ input.removeListener("error", settle);
148
+ this.signal?.removeEventListener("abort", onCancel);
149
+ input.pause();
150
+ if (remainder?.length)
151
+ input.unshift(remainder, input.readableEncoding ?? undefined);
152
+ if (value instanceof Error)
153
+ reject(value);
154
+ else
155
+ resolve(value);
156
+ };
157
+ const onCancel = () => {
158
+ this.state = "cancel";
159
+ settle(CANCEL);
160
+ };
161
+ const onEnd = () => settle(line + decoder.end());
162
+ const onData = (chunk) => {
163
+ if (chunk.length === 0)
164
+ return;
165
+ if (settled) {
166
+ if (!input.destroyed)
167
+ input.unshift(chunk, input.readableEncoding ?? undefined);
168
+ return;
169
+ }
170
+ let offset = 0;
171
+ const previousCarriageReturn = pendingCarriageReturns.get(input);
172
+ if (previousCarriageReturn !== undefined) {
173
+ pendingCarriageReturns.delete(input);
174
+ const isLineFeed = typeof chunk === "string" ? chunk[0] === "\n" : chunk[0] === 10;
175
+ if (isLineFeed && Date.now() - previousCarriageReturn <= 100)
176
+ offset = 1;
177
+ }
178
+ for (; offset < chunk.length; offset += 1) {
179
+ const character = typeof chunk === "string" ? chunk[offset] : decoder.write(chunk.subarray(offset, offset + 1));
180
+ if (character.endsWith("\r") || character.endsWith("\n")) {
181
+ line += character.slice(0, -1);
182
+ offset += 1;
183
+ if (character.endsWith("\r")) {
184
+ if (offset === chunk.length) {
185
+ pendingCarriageReturns.set(input, Date.now());
186
+ }
187
+ else if (typeof chunk === "string" ? chunk[offset] === "\n" : chunk[offset] === 10) {
188
+ offset += 1;
189
+ }
190
+ }
191
+ settle(line, chunk.slice(offset));
192
+ return;
193
+ }
194
+ line += character;
195
+ }
196
+ };
197
+ input.once("end", onEnd);
198
+ input.once("close", onCancel);
199
+ input.once("error", settle);
200
+ this.signal?.addEventListener("abort", onCancel, { once: true });
201
+ if (this.signal?.aborted)
202
+ onCancel();
203
+ else if (input.readableEnded)
204
+ onEnd();
205
+ else if (input.destroyed)
206
+ onCancel();
207
+ else if (!settled) {
208
+ input.on("data", onData);
209
+ if (!settled)
210
+ input.resume();
211
+ if (settled)
212
+ input.pause();
213
+ }
214
+ });
215
+ }
131
216
  return new Promise((resolve, reject) => {
132
217
  let rl = null;
133
218
  let settled = false;
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "toolcraft-schema",
3
- "version": "0.0.168",
3
+ "version": "0.0.169",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "toolcraft-openapi",
3
- "version": "0.0.168",
3
+ "version": "0.0.169",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -30,7 +30,7 @@
30
30
  "toolcraft-openapi-generate": "dist/bin/generate.js"
31
31
  },
32
32
  "dependencies": {
33
- "toolcraft": "0.0.168",
33
+ "toolcraft": "0.0.169",
34
34
  "fast-string-width": "^3.0.2",
35
35
  "fast-wrap-ansi": "^0.2.0",
36
36
  "sisteransi": "^1.0.5",
@@ -45,7 +45,7 @@
45
45
  "directory": "packages/toolcraft-openapi"
46
46
  },
47
47
  "optionalDependencies": {
48
- "toolcraft-schema": "0.0.168",
48
+ "toolcraft-schema": "0.0.169",
49
49
  "toolcraft-design": "*",
50
50
  "@poe-code/frontmatter": "*"
51
51
  },