File
Blob: src/node/internal/internal_readline_promises.ts
| 1 | // Copyright (c) 2017-2022 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | // |
| 5 | // Copyright Joyent, Inc. and other Node contributors. |
| 6 | // |
| 7 | // Permission is hereby granted, free of charge, to any person obtaining a |
| 8 | // copy of this software and associated documentation files (the |
| 9 | // "Software"), to deal in the Software without restriction, including |
| 10 | // without limitation the rights to use, copy, modify, merge, publish, |
| 11 | // distribute, sublicense, and/or sell copies of the Software, and to permit |
| 12 | // persons to whom the Software is furnished to do so, subject to the |
| 13 | // following conditions: |
| 14 | // |
| 15 | // The above copyright notice and this permission notice shall be included |
| 16 | // in all copies or substantial portions of the Software. |
| 17 | // |
| 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 19 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 20 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 21 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 22 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 23 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 24 | // USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 25 | |
| 26 | import { EventEmitter } from 'node-internal:events'; |
| 27 | import type { Abortable } from 'node:events'; |
| 28 | import type ReadlineType from 'node:readline/promises'; |
| 29 | import type { CursorPos, Direction } from 'node:readline'; |
| 30 | |
| 31 | // This class provides a no-op stub implementation that matches unenv's behavior. |
| 32 | // See: https://github.com/unjs/unenv/blob/main/src/runtime/node/internal/readline/promises/interface.ts |
| 33 | // Methods are no-ops or return sensible defaults rather than throwing errors, |
| 34 | // which allows code that depends on readline to work without crashing. |
| 35 | export class Interface extends EventEmitter implements ReadlineType.Interface { |
| 36 | terminal = false; |
| 37 | line = ''; |
| 38 | cursor = 0; |
| 39 | |
| 40 | getPrompt(): string { |
| 41 | return ''; |
| 42 | } |
| 43 | |
| 44 | setPrompt(_prompt: string): void { |
| 45 | // No-op |
| 46 | } |
| 47 | |
| 48 | prompt(_preserveCursor?: boolean): void { |
| 49 | // No-op |
| 50 | } |
| 51 | |
| 52 | question(query: string): Promise<string>; |
| 53 | question(query: string, options: Abortable): Promise<string>; |
| 54 | question(_query: string, _options?: Abortable): Promise<string> { |
| 55 | return Promise.resolve(''); |
| 56 | } |
| 57 | |
| 58 | pause(): this { |
| 59 | return this; |
| 60 | } |
| 61 | |
| 62 | resume(): this { |
| 63 | return this; |
| 64 | } |
| 65 | |
| 66 | close(): void { |
| 67 | // No-op |
| 68 | } |
| 69 | |
| 70 | write(_data: unknown, _key?: unknown): void { |
| 71 | // No-op |
| 72 | } |
| 73 | |
| 74 | getCursorPos(): CursorPos { |
| 75 | return { |
| 76 | rows: 0, |
| 77 | cols: 0, |
| 78 | }; |
| 79 | } |
| 80 | |
| 81 | [Symbol.dispose](): void { |
| 82 | this.close(); |
| 83 | } |
| 84 | |
| 85 | // eslint-disable-next-line @typescript-eslint/require-await |
| 86 | async [Symbol.asyncDispose](): Promise<void> { |
| 87 | this.close(); |
| 88 | } |
| 89 | |
| 90 | // Yield a single empty string so that `for await...of` loops complete |
| 91 | // immediately without blocking, consistent with no-op stub behavior. |
| 92 | // eslint-disable-next-line @typescript-eslint/require-await |
| 93 | async *[Symbol.asyncIterator](): NodeJS.AsyncIterator<string> { |
| 94 | yield ''; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // This class provides a no-op stub implementation that matches unenv's behavior. |
| 99 | // See: https://github.com/unjs/unenv/blob/main/src/runtime/node/internal/readline/promises/readline.ts |
| 100 | export class Readline implements ReadlineType.Readline { |
| 101 | clearLine(_dir: Direction): this { |
| 102 | return this; |
| 103 | } |
| 104 | |
| 105 | clearScreenDown(): this { |
| 106 | return this; |
| 107 | } |
| 108 | |
| 109 | commit(): Promise<void> { |
| 110 | return Promise.resolve(); |
| 111 | } |
| 112 | |
| 113 | cursorTo(_x: number, _y?: number): this { |
| 114 | return this; |
| 115 | } |
| 116 | |
| 117 | moveCursor(_dx: number, _dy: number): this { |
| 118 | return this; |
| 119 | } |
| 120 | |
| 121 | rollback(): this { |
| 122 | return this; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | export function createInterface(): ReadlineType.Interface { |
| 127 | return new Interface(); |
| 128 | } |