| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 30060e1 commit f5ea06c
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -822,43 +822,52 @@ For example, the following can be added to a `.bashrc` file: | |||
| 822 | 822 | alias node="env NODE_NO_READLINE=1 rlwrap node" | |
| 823 | 823 | ``` | |
| 824 | 824 | ||
| 825 | - ### Starting multiple REPL instances against a single running instance | ||
| 825 | + ### Starting multiple REPL instances in the same process | ||
| 826 | 826 | ||
| 827 | 827 | It is possible to create and run multiple REPL instances against a single | |
| 828 | - running instance of Node.js that share a single `global` object but have | ||
| 829 | - separate I/O interfaces. | ||
| 828 | + running instance of Node.js that share a single `global` object (by setting | ||
| 829 | + the `useGlobal` option to `true`) but have separate I/O interfaces. | ||
| 830 | 830 | ||
| 831 | 831 | The following example, for instance, provides separate REPLs on `stdin`, a Unix | |
| 832 | - socket, and a TCP socket: | ||
| 832 | + socket, and a TCP socket, all sharing the same `global` object: | ||
| 833 | 833 | ||
| 834 | 834 | ```mjs | |
| 835 | 835 | import net from 'node:net'; | |
| 836 | 836 | import repl from 'node:repl'; | |
| 837 | 837 | import process from 'node:process'; | |
| 838 | + import fs from 'node:fs'; | ||
| 838 | 839 | ||
| 839 | 840 | let connections = 0; | |
| 840 | 841 | ||
| 841 | 842 | repl.start({ | |
| 842 | 843 | prompt: 'Node.js via stdin> ', | |
| 844 | + useGlobal: true, | ||
| 843 | 845 | input: process.stdin, | |
| 844 | 846 | output: process.stdout, | |
| 845 | 847 | }); | |
| 846 | 848 | ||
| 849 | + const unixSocketPath = '/tmp/node-repl-sock'; | ||
| 850 | + | ||
| 851 | + // If the socket file already exists let's remove it | ||
| 852 | + fs.rmSync(unixSocketPath, { force: true }); | ||
| 853 | + | ||
| 847 | 854 | net.createServer((socket) => { | |
| 848 | 855 | connections += 1; | |
| 849 | 856 | repl.start({ | |
| 850 | 857 | prompt: 'Node.js via Unix socket> ', | |
| 858 | + useGlobal: true, | ||
| 851 | 859 | input: socket, | |
| 852 | 860 | output: socket, | |
| 853 | 861 | }).on('exit', () => { | |
| 854 | 862 | socket.end(); | |
| 855 | 863 | }); | |
| 856 | - }).listen('/tmp/node-repl-sock'); | ||
| 864 | + }).listen(unixSocketPath); | ||
| 857 | 865 | ||
| 858 | 866 | net.createServer((socket) => { | |
| 859 | 867 | connections += 1; | |
| 860 | 868 | repl.start({ | |
| 861 | 869 | prompt: 'Node.js via TCP socket> ', | |
| 870 | + useGlobal: true, | ||
| 862 | 871 | input: socket, | |
| 863 | 872 | output: socket, | |
| 864 | 873 | }).on('exit', () => { | |
@@ -870,29 +879,39 @@ net.createServer((socket) => { | |||
| 870 | 879 | ```cjs | |
| 871 | 880 | const net = require('node:net'); | |
| 872 | 881 | const repl = require('node:repl'); | |
| 882 | + const fs = require('node:fs'); | ||
| 883 | + | ||
| 873 | 884 | let connections = 0; | |
| 874 | 885 | ||
| 875 | 886 | repl.start({ | |
| 876 | 887 | prompt: 'Node.js via stdin> ', | |
| 888 | + useGlobal: true, | ||
| 877 | 889 | input: process.stdin, | |
| 878 | 890 | output: process.stdout, | |
| 879 | 891 | }); | |
| 880 | 892 | ||
| 893 | + const unixSocketPath = '/tmp/node-repl-sock'; | ||
| 894 | + | ||
| 895 | + // If the socket file already exists let's remove it | ||
| 896 | + fs.rmSync(unixSocketPath, { force: true }); | ||
| 897 | + | ||
| 881 | 898 | net.createServer((socket) => { | |
| 882 | 899 | connections += 1; | |
| 883 | 900 | repl.start({ | |
| 884 | 901 | prompt: 'Node.js via Unix socket> ', | |
| 902 | + useGlobal: true, | ||
| 885 | 903 | input: socket, | |
| 886 | 904 | output: socket, | |
| 887 | 905 | }).on('exit', () => { | |
| 888 | 906 | socket.end(); | |
| 889 | 907 | }); | |
| 890 | - }).listen('/tmp/node-repl-sock'); | ||
| 908 | + }).listen(unixSocketPath); | ||
| 891 | 909 | ||
| 892 | 910 | net.createServer((socket) => { | |
| 893 | 911 | connections += 1; | |
| 894 | 912 | repl.start({ | |
| 895 | 913 | prompt: 'Node.js via TCP socket> ', | |
| 914 | + useGlobal: true, | ||
| 896 | 915 | input: socket, | |
| 897 | 916 | output: socket, | |
| 898 | 917 | }).on('exit', () => { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments