|
1 | | -import type { REPLServer } from 'repl'; |
| 1 | +import type { Interface as ReadlineInterface } from 'readline'; |
| 2 | +import { start as replStart, type REPLServer } from 'repl'; |
| 3 | +import { PassThrough } from 'stream'; |
2 | 4 |
|
3 | 5 | // https://github.com/nodejs/node/blob/d9786109b2a0982677135f0c146f6b591a0e4961/lib/internal/readline/utils.js#L90 |
4 | 6 | // https://nodejs.org/api/readline.html#readlineemitkeypresseventsstream-interface |
@@ -65,3 +67,67 @@ export function installPasteSupport(repl: REPLServer): string { |
65 | 67 | }); |
66 | 68 | return onEnd; |
67 | 69 | } |
| 70 | + |
| 71 | +// Bug: https://github.com/nodejs/node/issues/60446 |
| 72 | +// Introduced by: https://github.com/nodejs/node/pull/59857 |
| 73 | +// Fixed by: https://github.com/nodejs/node/pull/60470 |
| 74 | +function _hasNode60446(): boolean { |
| 75 | + const input = new PassThrough(); |
| 76 | + const output = new PassThrough(); |
| 77 | + const repl = replStart({ terminal: true, input, output, useColors: false }); |
| 78 | + repl.input.emit('data', '{}'); |
| 79 | + repl.input.emit('keypress', '', { name: 'left' }); |
| 80 | + repl.input.emit('data', 'node'); |
| 81 | + const { line } = repl; |
| 82 | + repl.close(); |
| 83 | + return line === '{}noed'; |
| 84 | +} |
| 85 | +_hasNode60446(); |
| 86 | +let hasNode60446: boolean | undefined = undefined; |
| 87 | + |
| 88 | +export function fixNode60446(repl: ReadlineInterface): void { |
| 89 | + hasNode60446 ??= _hasNode60446(); |
| 90 | + if (!hasNode60446) return; |
| 91 | + const symbols = [...prototypeChain(repl)].flatMap((proto) => |
| 92 | + Object.getOwnPropertySymbols(proto) |
| 93 | + ); |
| 94 | + const insertStringKey = symbols.find((s) => |
| 95 | + String(s).includes('(_insertString)') |
| 96 | + ); |
| 97 | + const writeToOutputKey = symbols.find((s) => |
| 98 | + String(s).includes('(_writeToOutput)') |
| 99 | + ); |
| 100 | + if (!insertStringKey || !writeToOutputKey) return; |
| 101 | + const original = (repl as any)[insertStringKey]; |
| 102 | + |
| 103 | + // Monkey-patch in the fix linked above |
| 104 | + let fixupOnWriteToOutput: undefined | (() => void); |
| 105 | + Object.defineProperty(repl as any, insertStringKey, { |
| 106 | + configurable: true, |
| 107 | + writable: true, |
| 108 | + enumerable: false, |
| 109 | + value: function (this: ReadlineInterface, s: string) { |
| 110 | + if (!(this as any).isCompletionEnabled) { |
| 111 | + const origLine = this.line; |
| 112 | + const origCursor = this.cursor; |
| 113 | + fixupOnWriteToOutput = () => { |
| 114 | + const beg = origLine.slice(0, origCursor); |
| 115 | + const end = origLine.slice(origCursor); |
| 116 | + (this as any).line = beg + s + end; |
| 117 | + }; |
| 118 | + } |
| 119 | + return original.call(this, s); |
| 120 | + }, |
| 121 | + }); |
| 122 | + const originalWriteToOutput = (repl as any)[writeToOutputKey]; |
| 123 | + Object.defineProperty(repl as any, writeToOutputKey, { |
| 124 | + configurable: true, |
| 125 | + writable: true, |
| 126 | + enumerable: false, |
| 127 | + value: function (this: ReadlineInterface, s: string) { |
| 128 | + fixupOnWriteToOutput?.(); |
| 129 | + fixupOnWriteToOutput = undefined; |
| 130 | + return originalWriteToOutput.call(this, s); |
| 131 | + }, |
| 132 | + }); |
| 133 | +} |
0 commit comments