Skip to content
File

Blob: src/node/repl.ts

typescript131 lines
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 
26import { ERR_METHOD_NOT_IMPLEMENTED } from 'node-internal:internal_errors';
27import { builtinModules as _builtinModules } from 'node-internal:internal_module';
28import { Interface } from 'node-internal:internal_readline_promises';
29import type { InspectOptions } from 'node:util';
30import type { Context } from 'node:vm';
31import type ReplType from 'node:repl';
32import type { Completer, AsyncCompleter } from 'readline';
33 
34export function writer(): ReplType.REPLWriter & { options: InspectOptions } {
35 throw new ERR_METHOD_NOT_IMPLEMENTED('writer');
36}
37 
38const 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};
52Object.assign(writer, { options: writerOptions });
53 
54export function start(): ReplType.REPLServer {
55 throw new ERR_METHOD_NOT_IMPLEMENTED('start');
56}
57 
58export class Recoverable extends SyntaxError implements ReplType.Recoverable {
59 err: Error;
60 constructor(err: Error) {
61 super();
62 this.err = err;
63 }
64}
65 
66export 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 
111export const REPL_MODE_SLOPPY: unique symbol = Symbol('repl-sloppy');
112 
113export const REPL_MODE_STRICT: unique symbol = Symbol('repl-strict');
114 
115export const builtinModules: string[] = _builtinModules.filter(
116 (m) => m[0] !== '_'
117);
118 
119export const _builtinLibs = builtinModules;
120 
121export default {
122 writer,
123 start,
124 Recoverable,
125 REPLServer,
126 builtinModules,
127 _builtinLibs,
128 REPL_MODE_SLOPPY,
129 REPL_MODE_STRICT,
130};