|
3 | 3 | * Licensed under the MIT License. See License.txt in the project root for license information. |
4 | 4 | *--------------------------------------------------------------------------------------------*/ |
5 | 5 |
|
6 | | -import * as fs from 'fs'; |
7 | 6 | import { VSBufferReadableStream, VSBufferReadable, VSBuffer } from 'vs/base/common/buffer'; |
8 | 7 | import { Readable } from 'stream'; |
9 | 8 | import { isUndefinedOrNull } from 'vs/base/common/types'; |
10 | 9 | import { UTF8, UTF8_with_bom, UTF8_BOM, UTF16be, UTF16le_BOM, UTF16be_BOM, UTF16le, UTF_ENCODING } from 'vs/base/node/encoding'; |
11 | 10 |
|
12 | | -/** |
13 | | - * Reads a file until a matching string is found. |
14 | | - * |
15 | | - * @param file The file to read. |
16 | | - * @param matchingString The string to search for. |
17 | | - * @param chunkBytes The number of bytes to read each iteration. |
18 | | - * @param maximumBytesToRead The maximum number of bytes to read before giving up. |
19 | | - * @param callback The finished callback. |
20 | | - */ |
21 | | -export function readToMatchingString(file: string, matchingString: string, chunkBytes: number, maximumBytesToRead: number): Promise<string | null> { |
22 | | - return new Promise<string | null>((resolve, reject) => |
23 | | - fs.open(file, 'r', null, (err, fd) => { |
24 | | - if (err) { |
25 | | - return reject(err); |
26 | | - } |
27 | | - |
28 | | - function end(err: Error | null, result: string | null): void { |
29 | | - fs.close(fd, closeError => { |
30 | | - if (closeError) { |
31 | | - return reject(closeError); |
32 | | - } |
33 | | - |
34 | | - if (err && (<any>err).code === 'EISDIR') { |
35 | | - return reject(err); // we want to bubble this error up (file is actually a folder) |
36 | | - } |
37 | | - |
38 | | - return resolve(result); |
39 | | - }); |
40 | | - } |
41 | | - |
42 | | - const buffer = Buffer.allocUnsafe(maximumBytesToRead); |
43 | | - let offset = 0; |
44 | | - |
45 | | - function readChunk(): void { |
46 | | - fs.read(fd, buffer, offset, chunkBytes, null, (err, bytesRead) => { |
47 | | - if (err) { |
48 | | - return end(err, null); |
49 | | - } |
50 | | - |
51 | | - if (bytesRead === 0) { |
52 | | - return end(null, null); |
53 | | - } |
54 | | - |
55 | | - offset += bytesRead; |
56 | | - |
57 | | - const newLineIndex = buffer.indexOf(matchingString); |
58 | | - if (newLineIndex >= 0) { |
59 | | - return end(null, buffer.toString('utf8').substr(0, newLineIndex)); |
60 | | - } |
61 | | - |
62 | | - if (offset >= maximumBytesToRead) { |
63 | | - return end(new Error(`Could not find ${matchingString} in first ${maximumBytesToRead} bytes of ${file}`), null); |
64 | | - } |
65 | | - |
66 | | - return readChunk(); |
67 | | - }); |
68 | | - } |
69 | | - |
70 | | - readChunk(); |
71 | | - }) |
72 | | - ); |
73 | | -} |
74 | | - |
75 | 11 | export function streamToNodeReadable(stream: VSBufferReadableStream): Readable { |
76 | 12 | return new class extends Readable { |
77 | 13 | private listening = false; |
|
0 commit comments