File
Blob: src/node/repl.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 { ERR_METHOD_NOT_IMPLEMENTED } from 'node-internal:internal_errors'; |
| 27 | import { builtinModules as _builtinModules } from 'node-internal:internal_module'; |
| 28 | import { Interface } from 'node-internal:internal_readline_promises'; |
| 29 | import type { InspectOptions } from 'node:util'; |
| 30 | import type { Context } from 'node:vm'; |
| 31 | import type ReplType from 'node:repl'; |
| 32 | import type { Completer, AsyncCompleter } from 'readline'; |
| 33 | |
| 34 | export function writer(): ReplType.REPLWriter & { options: InspectOptions } { |
| 35 | throw new ERR_METHOD_NOT_IMPLEMENTED('writer'); |
| 36 | } |
| 37 | |
| 38 | const writerOptions: InspectOptions = { |
| 39 | showHidden: false, |
| 40 | depth: 2, |
| 41 | colors: true, |
| 42 | customInspect: true, |
| 43 | showProxy: true, |
| 44 | maxArrayLength: 100, |
| 45 | maxStringLength: 10000, |
| 46 | breakLength: 80, |
| 47 | compact: 3, |
| 48 | sorted: false, |
| 49 | getters: false, |
| 50 | numericSeparator: false, |
| 51 | }; |
| 52 | Object.assign(writer, { options: writerOptions }); |
| 53 | |
| 54 | export function start(): ReplType.REPLServer { |
| 55 | throw new ERR_METHOD_NOT_IMPLEMENTED('start'); |
| 56 | } |
| 57 | |
| 58 | export class Recoverable extends SyntaxError implements ReplType.Recoverable { |
| 59 | err: Error; |
| 60 | constructor(err: Error) { |
| 61 | super(); |
| 62 | this.err = err; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | export class REPLServer extends Interface implements ReplType.REPLServer { |
| 67 | context: Context; |
| 68 | inputStream: NodeJS.ReadableStream; |
| 69 | input: NodeJS.ReadableStream; |
| 70 | outputStream: NodeJS.WritableStream; |
| 71 | output: NodeJS.WritableStream; |
| 72 | commands: NodeJS.ReadOnlyDict<ReplType.REPLCommand>; |
| 73 | editorMode: boolean; |
| 74 | underscoreAssigned: boolean; |
| 75 | underscoreErrAssigned: boolean; |
| 76 | last: unknown; |
| 77 | lastError: unknown; |
| 78 | eval: ReplType.REPLEval; |
| 79 | useColors: boolean; |
| 80 | useGlobal: boolean; |
| 81 | ignoreUndefined: boolean; |
| 82 | writer: ReplType.REPLWriter; |
| 83 | completer: Completer | AsyncCompleter; |
| 84 | replMode: typeof ReplType.REPL_MODE_SLOPPY | typeof ReplType.REPL_MODE_STRICT; |
| 85 | |
| 86 | constructor() { |
| 87 | super(); |
| 88 | throw new ERR_METHOD_NOT_IMPLEMENTED('REPLServer'); |
| 89 | } |
| 90 | |
| 91 | setupHistory(_historyConfig?: unknown, _callback?: unknown): void { |
| 92 | throw new ERR_METHOD_NOT_IMPLEMENTED('setupHistory'); |
| 93 | } |
| 94 | |
| 95 | defineCommand( |
| 96 | _keyword: string, |
| 97 | _cmd: ReplType.REPLCommandAction | ReplType.REPLCommand |
| 98 | ): void { |
| 99 | throw new ERR_METHOD_NOT_IMPLEMENTED('defineCommand'); |
| 100 | } |
| 101 | |
| 102 | displayPrompt(_preserveCursor?: boolean): void { |
| 103 | throw new ERR_METHOD_NOT_IMPLEMENTED('displayPrompt'); |
| 104 | } |
| 105 | |
| 106 | clearBufferedCommand(): void { |
| 107 | throw new ERR_METHOD_NOT_IMPLEMENTED('clearBufferedCommand'); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | export const REPL_MODE_SLOPPY: unique symbol = Symbol('repl-sloppy'); |
| 112 | |
| 113 | export const REPL_MODE_STRICT: unique symbol = Symbol('repl-strict'); |
| 114 | |
| 115 | export const builtinModules: string[] = _builtinModules.filter( |
| 116 | (m) => m[0] !== '_' |
| 117 | ); |
| 118 | |
| 119 | export const _builtinLibs = builtinModules; |
| 120 | |
| 121 | export default { |
| 122 | writer, |
| 123 | start, |
| 124 | Recoverable, |
| 125 | REPLServer, |
| 126 | builtinModules, |
| 127 | _builtinLibs, |
| 128 | REPL_MODE_SLOPPY, |
| 129 | REPL_MODE_STRICT, |
| 130 | }; |