()
| 84 | }; |
| 85 | |
| 86 | async function* iterator(): AsyncGenerator<string> { |
| 87 | let initialized = false; |
| 88 | let offset = 0; |
| 89 | let carry = ""; |
| 90 | let decoder = new StringDecoder("utf8"); |
| 91 | |
| 92 | while (!closed) { |
| 93 | let fileSize: number; |
| 94 | try { |
| 95 | const stats = await stat(filePath); |
| 96 | fileSize = Number(stats.size); |
| 97 | } catch (error) { |
| 98 | if (isNodeErrorWithCode(error) && error.code === "ENOENT") { |
| 99 | await sleep(pollMs); |
| 100 | continue; |
| 101 | } |
| 102 | throw error; |
| 103 | } |
| 104 | |
| 105 | if (!initialized) { |
| 106 | initialized = true; |
| 107 | offset = fromEnd ? fileSize : 0; |
| 108 | } |
| 109 | |
| 110 | if (fileSize < offset) { |
| 111 | // File was truncated/rotated. |
| 112 | offset = 0; |
| 113 | carry = ""; |
| 114 | decoder = new StringDecoder("utf8"); |
| 115 | } |
| 116 | |
| 117 | if (fileSize > offset) { |
| 118 | const delta = await readUtf8Slice(filePath, offset, fileSize, decoder); |
| 119 | offset = fileSize; |
| 120 | const segments = `${carry}${delta}`.split(/\r?\n/); |
| 121 | carry = segments.pop() ?? ""; |
| 122 | for (const line of segments) { |
| 123 | if (closed) return; |
| 124 | yield line; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | await sleep(pollMs); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return { |
| 133 | [Symbol.asyncIterator]: iterator, |
nothing calls this directly
no test coverage detected