Skip to content

Commit f875745

Browse files
committed
fix(core): retry after clock skew correction
1 parent 3b37740 commit f875745

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

packages/core/src/middleware-http-signing/httpSigningMiddleware.ts

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,18 @@ const defaultSuccessHandler: SuccessHandler = (
3535
signingProperties: Record<string, unknown>
3636
): void => {};
3737

38+
/**
39+
* @internal
40+
*/
41+
interface HttpSigningMiddlewarePreviouslyResolved {
42+
systemClockOffset?: number;
43+
}
44+
3845
/**
3946
* @internal
4047
*/
4148
export const httpSigningMiddleware = <Input extends object, Output extends object>(
42-
config: object
49+
config: HttpSigningMiddlewarePreviouslyResolved
4350
): FinalizeRequestMiddleware<Input, Output> => (
4451
next: FinalizeHandler<Input, Output>,
4552
context: HttpSigningMiddlewareHandlerExecutionContext
@@ -60,10 +67,35 @@ export const httpSigningMiddleware = <Input extends object, Output extends objec
6067
identity,
6168
signer,
6269
} = scheme;
63-
const output = await next({
64-
...args,
65-
request: await signer.sign(args.request, identity, signingProperties),
66-
}).catch((signer.errorHandler || defaultErrorHandler)(signingProperties));
67-
(signer.successHandler || defaultSuccessHandler)(output.response, signingProperties);
70+
const lastSystemClockOffset = config.systemClockOffset | 0;
71+
72+
const makeSignedRequest = async () =>
73+
next({
74+
...args,
75+
request: await signer.sign(args.request as HttpRequest, identity, signingProperties),
76+
});
77+
78+
const onError = (signer.errorHandler || defaultErrorHandler)(signingProperties);
79+
const onSuccess = signer.successHandler || defaultSuccessHandler;
80+
81+
const output = await makeSignedRequest().catch(async (error: unknown) => {
82+
let thrownError: unknown;
83+
try {
84+
onError(error as Error);
85+
} catch (e) {
86+
thrownError = e;
87+
}
88+
const latestSystemClockOffset = config.systemClockOffset | 0;
89+
const systemClockOffsetModified = lastSystemClockOffset !== latestSystemClockOffset;
90+
91+
if (systemClockOffsetModified) {
92+
return makeSignedRequest().catch(onError);
93+
} else {
94+
if (thrownError) {
95+
throw thrownError;
96+
}
97+
}
98+
});
99+
onSuccess(output.response, signingProperties);
68100
return output;
69101
};

0 commit comments

Comments
 (0)