| 1145 | // `next` skips unread body bytes; `read` consumes the current body. |
| 1146 | // Both preserve progress across `NeedInput` retries. |
| 1147 | class Decoder { |
| 1148 | private readonly queue = new ByteQueue() |
| 1149 | private ended = false |
| 1150 | private finished = false |
| 1151 | private closed = false |
| 1152 | // Incremented whenever the current body is invalidated |
| 1153 | generation = 0 |
| 1154 | private remaining = 0 |
| 1155 | private pad = 0 |
| 1156 | // Bytes to discard before the next header |
| 1157 | private skip = 0 |
| 1158 | private zeroBlock = false |
| 1159 | // Archive offset of the last header block read, for error messages |
| 1160 | private at = 0 |
| 1161 | // An extended header whose data is still to be read |
| 1162 | private meta: RawHeader | undefined |
| 1163 | private pax: Map<string, string> | undefined |
| 1164 | // Records of the PAX global headers read so far |
| 1165 | private global: Map<string, string> | undefined |
| 1166 | private longName = "" |
| 1167 | private longLink = "" |
| 1168 | |
| 1169 | push(chunk: Uint8Array): void { |
| 1170 | if (!this.finished) this.queue.push(chunk) |
| 1171 | } |
| 1172 | |
| 1173 | end(): void { |
| 1174 | this.ended = true |
| 1175 | } |
| 1176 | |
| 1177 | close(): void { |
| 1178 | this.closed = true |
| 1179 | this.generation++ |
| 1180 | this.queue.clear() |
| 1181 | } |
| 1182 | |
| 1183 | // Returns the next entry header, or `undefined` once the archive and the |
| 1184 | // input have ended. Input after the end of the archive is discarded. |
| 1185 | next(): Header | undefined | typeof NeedInput { |
| 1186 | try { |
| 1187 | return this.advance() |
| 1188 | } catch (error) { |
| 1189 | // Header errors are thrown where the archive offset is not known |
| 1190 | if (isTarError(error) && (error.kind === "InvalidHeader" || error.kind === "Unsupported")) { |
| 1191 | throw new TarError({ kind: error.kind, message: `${error.message} (header at byte ${this.at})` }) |
| 1192 | } |
| 1193 | throw error |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | private advance(): Header | undefined | typeof NeedInput { |
| 1198 | this.generation++ |
| 1199 | this.skip += this.remaining + this.pad |
| 1200 | this.remaining = 0 |
| 1201 | this.pad = 0 |
| 1202 | const queue = this.queue |
| 1203 | while (true) { |
| 1204 | if (this.finished) return this.finish() |
nothing calls this directly
no outgoing calls
no test coverage detected