@@ -703,9 +703,18 @@ added: v17.0.0 |
703 | 703 | The `readlinePromises.createInterface()` method creates a new `readlinePromises.Interface` |
704 | 704 | instance. |
705 | 705 |
|
706 | | -```js |
707 | | -const readlinePromises = require('node:readline/promises'); |
708 | | -const rl = readlinePromises.createInterface({ |
| 706 | +```mjs |
| 707 | +import { createInterface } from 'node:readline/promises'; |
| 708 | +import { stdin, stdout } from 'node:process'; |
| 709 | +const rl = createInterface({ |
| 710 | + input: stdin, |
| 711 | + output: stdout, |
| 712 | +}); |
| 713 | +``` |
| 714 | + |
| 715 | +```cjs |
| 716 | +const { createInterface } = require('node:readline/promises'); |
| 717 | +const rl = createInterface({ |
709 | 718 | input: process.stdin, |
710 | 719 | output: process.stdout, |
711 | 720 | }); |
@@ -960,9 +969,18 @@ changes: |
960 | 969 | The `readline.createInterface()` method creates a new `readline.Interface` |
961 | 970 | instance. |
962 | 971 |
|
963 | | -```js |
964 | | -const readline = require('node:readline'); |
965 | | -const rl = readline.createInterface({ |
| 972 | +```mjs |
| 973 | +import { createInterface } from 'node:readline'; |
| 974 | +import { stdin, stdout } from 'node:process'; |
| 975 | +const rl = createInterface({ |
| 976 | + input: stdin, |
| 977 | + output: stdout, |
| 978 | +}); |
| 979 | +``` |
| 980 | + |
| 981 | +```cjs |
| 982 | +const { createInterface } = require('node:readline'); |
| 983 | +const rl = createInterface({ |
966 | 984 | input: process.stdin, |
967 | 985 | output: process.stdout, |
968 | 986 | }); |
@@ -1098,9 +1116,36 @@ if (process.stdin.isTTY) |
1098 | 1116 | The following example illustrates the use of `readline.Interface` class to |
1099 | 1117 | implement a small command-line interface: |
1100 | 1118 |
|
1101 | | -```js |
1102 | | -const readline = require('node:readline'); |
1103 | | -const rl = readline.createInterface({ |
| 1119 | +```mjs |
| 1120 | +import { createInterface } from 'node:readline'; |
| 1121 | +import { exit, stdin, stdout } from 'node:process'; |
| 1122 | +const rl = createInterface({ |
| 1123 | + input: stdin, |
| 1124 | + output: stdout, |
| 1125 | + prompt: 'OHAI> ', |
| 1126 | +}); |
| 1127 | + |
| 1128 | +rl.prompt(); |
| 1129 | + |
| 1130 | +rl.on('line', (line) => { |
| 1131 | + switch (line.trim()) { |
| 1132 | + case 'hello': |
| 1133 | + console.log('world!'); |
| 1134 | + break; |
| 1135 | + default: |
| 1136 | + console.log(`Say what? I might have heard '${line.trim()}'`); |
| 1137 | + break; |
| 1138 | + } |
| 1139 | + rl.prompt(); |
| 1140 | +}).on('close', () => { |
| 1141 | + console.log('Have a great day!'); |
| 1142 | + exit(0); |
| 1143 | +}); |
| 1144 | +``` |
| 1145 | + |
| 1146 | +```cjs |
| 1147 | +const { createInterface } = require('node:readline'); |
| 1148 | +const rl = createInterface({ |
1104 | 1149 | input: process.stdin, |
1105 | 1150 | output: process.stdout, |
1106 | 1151 | prompt: 'OHAI> ', |
@@ -1130,14 +1175,37 @@ A common use case for `readline` is to consume an input file one line at a |
1130 | 1175 | time. The easiest way to do so is leveraging the [`fs.ReadStream`][] API as |
1131 | 1176 | well as a `for await...of` loop: |
1132 | 1177 |
|
1133 | | -```js |
1134 | | -const fs = require('node:fs'); |
1135 | | -const readline = require('node:readline'); |
| 1178 | +```mjs |
| 1179 | +import { createReadStream } from 'node:fs'; |
| 1180 | +import { createInterface } from 'node:readline'; |
1136 | 1181 |
|
1137 | 1182 | async function processLineByLine() { |
1138 | | - const fileStream = fs.createReadStream('input.txt'); |
| 1183 | + const fileStream = createReadStream('input.txt'); |
1139 | 1184 |
|
1140 | | - const rl = readline.createInterface({ |
| 1185 | + const rl = createInterface({ |
| 1186 | + input: fileStream, |
| 1187 | + crlfDelay: Infinity, |
| 1188 | + }); |
| 1189 | + // Note: we use the crlfDelay option to recognize all instances of CR LF |
| 1190 | + // ('\r\n') in input.txt as a single line break. |
| 1191 | + |
| 1192 | + for await (const line of rl) { |
| 1193 | + // Each line in input.txt will be successively available here as `line`. |
| 1194 | + console.log(`Line from file: ${line}`); |
| 1195 | + } |
| 1196 | +} |
| 1197 | + |
| 1198 | +processLineByLine(); |
| 1199 | +``` |
| 1200 | + |
| 1201 | +```cjs |
| 1202 | +const { createReadStream } = require('node:fs'); |
| 1203 | +const { createInterface } = require('node:readline'); |
| 1204 | + |
| 1205 | +async function processLineByLine() { |
| 1206 | + const fileStream = createReadStream('input.txt'); |
| 1207 | + |
| 1208 | + const rl = createInterface({ |
1141 | 1209 | input: fileStream, |
1142 | 1210 | crlfDelay: Infinity, |
1143 | 1211 | }); |
@@ -1155,12 +1223,26 @@ processLineByLine(); |
1155 | 1223 |
|
1156 | 1224 | Alternatively, one could use the [`'line'`][] event: |
1157 | 1225 |
|
1158 | | -```js |
1159 | | -const fs = require('node:fs'); |
1160 | | -const readline = require('node:readline'); |
| 1226 | +```mjs |
| 1227 | +import { createReadStream } from 'node:fs'; |
| 1228 | +import { createInterface } from 'node:readline'; |
1161 | 1229 |
|
1162 | | -const rl = readline.createInterface({ |
1163 | | - input: fs.createReadStream('sample.txt'), |
| 1230 | +const rl = createInterface({ |
| 1231 | + input: createReadStream('sample.txt'), |
| 1232 | + crlfDelay: Infinity, |
| 1233 | +}); |
| 1234 | + |
| 1235 | +rl.on('line', (line) => { |
| 1236 | + console.log(`Line from file: ${line}`); |
| 1237 | +}); |
| 1238 | +``` |
| 1239 | + |
| 1240 | +```cjs |
| 1241 | +const { createReadStream } = require('node:fs'); |
| 1242 | +const { createInterface } = require('node:readline'); |
| 1243 | + |
| 1244 | +const rl = createInterface({ |
| 1245 | + input: createReadStream('sample.txt'), |
1164 | 1246 | crlfDelay: Infinity, |
1165 | 1247 | }); |
1166 | 1248 |
|
@@ -1172,7 +1254,32 @@ rl.on('line', (line) => { |
1172 | 1254 | Currently, `for await...of` loop can be a bit slower. If `async` / `await` |
1173 | 1255 | flow and speed are both essential, a mixed approach can be applied: |
1174 | 1256 |
|
1175 | | -```js |
| 1257 | +```mjs |
| 1258 | +import { once } from 'node:events'; |
| 1259 | +import { createReadStream } from 'node:fs'; |
| 1260 | +import { createInterface } from 'node:readline'; |
| 1261 | + |
| 1262 | +(async function processLineByLine() { |
| 1263 | + try { |
| 1264 | + const rl = createInterface({ |
| 1265 | + input: createReadStream('big-file.txt'), |
| 1266 | + crlfDelay: Infinity, |
| 1267 | + }); |
| 1268 | + |
| 1269 | + rl.on('line', (line) => { |
| 1270 | + // Process the line. |
| 1271 | + }); |
| 1272 | + |
| 1273 | + await once(rl, 'close'); |
| 1274 | + |
| 1275 | + console.log('File processed.'); |
| 1276 | + } catch (err) { |
| 1277 | + console.error(err); |
| 1278 | + } |
| 1279 | +})(); |
| 1280 | +``` |
| 1281 | + |
| 1282 | +```cjs |
1176 | 1283 | const { once } = require('node:events'); |
1177 | 1284 | const { createReadStream } = require('node:fs'); |
1178 | 1285 | const { createInterface } = require('node:readline'); |
|