| 145 | ]; |
| 146 | |
| 147 | export class AdbAuthenticationProcessor implements Disposable { |
| 148 | readonly authenticators: readonly AdbAuthenticator[]; |
| 149 | |
| 150 | readonly #credentialStore: AdbCredentialStore; |
| 151 | |
| 152 | #pendingRequest = new PromiseResolver<AdbPacketData>(); |
| 153 | #iterator: AsyncIterator<AdbPacketData, void, void> | undefined; |
| 154 | |
| 155 | constructor( |
| 156 | authenticators: readonly AdbAuthenticator[], |
| 157 | credentialStore: AdbCredentialStore, |
| 158 | ) { |
| 159 | this.authenticators = authenticators; |
| 160 | this.#credentialStore = credentialStore; |
| 161 | } |
| 162 | |
| 163 | #getNextRequest = (): Promise<AdbPacketData> => { |
| 164 | return this.#pendingRequest.promise; |
| 165 | }; |
| 166 | |
| 167 | async *#invokeAuthenticator(): AsyncGenerator<AdbPacketData, void, void> { |
| 168 | for (const authenticator of this.authenticators) { |
| 169 | for await (const packet of authenticator( |
| 170 | this.#credentialStore, |
| 171 | this.#getNextRequest, |
| 172 | )) { |
| 173 | // If the authenticator yielded a response |
| 174 | // Prepare `nextRequest` for next authentication request |
| 175 | this.#pendingRequest = new PromiseResolver(); |
| 176 | |
| 177 | // Yield the response to outer layer |
| 178 | yield packet; |
| 179 | } |
| 180 | |
| 181 | // If the authenticator returned, |
| 182 | // Next authenticator will be given the same `pendingRequest` |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | async process(packet: AdbPacketData): Promise<AdbPacketData> { |
| 187 | if (!this.#iterator) { |
| 188 | this.#iterator = this.#invokeAuthenticator(); |
| 189 | } |
| 190 | |
| 191 | this.#pendingRequest.resolve(packet); |
| 192 | |
| 193 | const result = await this.#iterator.next(); |
| 194 | if (result.done) { |
| 195 | throw new Error("No authenticator can handle the request"); |
| 196 | } |
| 197 | |
| 198 | return result.value; |
| 199 | } |
| 200 | |
| 201 | dispose() { |
| 202 | void this.#iterator?.return?.(); |
| 203 | } |
| 204 | } |
nothing calls this directly
no outgoing calls
no test coverage detected