Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions packages/client-core/__tests__/observe-requests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,55 @@ describe('watching what the transport did', () => {
expect(refreshed.filter((event) => !(event as { joined: boolean }).joined)).toHaveLength(1);
});

/**
* The refresh has a duration and nothing reported it. Without a closing
* event the waterfall can say a request hit the credential refresh but not
* how long it sat there, which is the only number that makes an auth stall
* distinguishable from a slow server.
*/
it('closes the refresh so its duration is measurable', async () => {
const seen = recorder();
let token = 't0';
const client = fakeClient((config) => {
if (config.headers?.['Authorization'] === 'Bearer t0') throw new HttpFailure(401);

return { ok: true };
});
const auth: AuthProvider = {
credentials: () => ({ Authorization: `Bearer ${token}` }),
refresh: () => {
token = 't1';

return Promise.resolve();
},
};
const rest = new RestTransport({ client, auth, sleep: () => Promise.resolve(), observer: seen.observe });

await rest.execute({ meta: list, args: {} });

const kinds = seen.events.map((event) => event.type);

expect(kinds).toContain('refresh');
expect(kinds).toContain('refreshed');
expect(kinds.indexOf('refreshed')).toBeGreaterThan(kinds.indexOf('refresh'));
});

it('closes the refresh even when it fails', async () => {
const seen = recorder();
const client = fakeClient(() => {
throw new HttpFailure(401);
});
const auth: AuthProvider = {
credentials: () => ({ Authorization: 'Bearer t0' }),
refresh: () => Promise.reject(new Error('refresh is down')),
};
const rest = new RestTransport({ client, auth, sleep: () => Promise.resolve(), observer: seen.observe });

await expect(rest.execute({ meta: list, args: {} })).rejects.toThrow();

expect(seen.events.map((event) => event.type)).toContain('refreshed');
});

it('costs an unwatched transport nothing but an undefined field', async () => {
const client = fakeClient(() => ({ ok: true }));
const rest = new RestTransport({ client });
Expand Down
11 changes: 11 additions & 0 deletions packages/client-core/src/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,14 @@ export type RequestEvent =
| { readonly type: 'attempt'; readonly attempt: number }
/** A 401 sent this request to the credential refresh. */
| { readonly type: 'refresh'; readonly joined: boolean }
/**
* The refresh this request was waiting on has finished, either way.
*
* Paired with `refresh` so the wait has a duration. Without it a watcher can
* say a request hit the credential refresh but not how long it sat there,
* which is the only number that tells an auth stall apart from a slow server.
*/
| { readonly type: 'refreshed'; readonly ok: boolean }
| {
readonly type: 'retry';
readonly attempt: number;
Expand Down Expand Up @@ -484,7 +492,10 @@ export class RestTransport implements Transport {
if (generation === this.generation) {
try {
await this.refresh(watch);
watch?.({ type: 'refreshed', ok: true });
} catch {
watch?.({ type: 'refreshed', ok: false });

// The refresh failed, so the 401 stands. Reporting the refresh's own
// error here would replace "you are not authorized" with whatever
// the token endpoint said, which is not what the caller asked for.
Expand Down
Loading
Loading