Skip to content

Commit 4027c9b

Browse files
authored
feat: add retry-after-limit (#6)
* feat: add throttled-retry and onStatusChange * fix: throttledRetry * chore: format * chore: retry-after more second * revert: remove onStatusChange * revert: remove SignAddonStatus * chore: rename to retry-after-limit * chore: remove AMOClientExtra * chore: comment
1 parent d429b20 commit 4027c9b

4 files changed

Lines changed: 26 additions & 3 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ $ npx amo-upload \
3939
--addon-version $VERSION \
4040
--channel listed \
4141
--dist-file path/to/dist.zip \
42-
--source-file path/to/source.zip
42+
--source-file path/to/source.zip \
43+
--retry-after-limit 120 \
4344
--output path/to/my-ext-v1.2.3.xpi
4445
```
4546

@@ -75,6 +76,7 @@ try {
7576
channel: 'listed',
7677
distFile: 'path/to/dist.zip',
7778
sourceFile: 'path/to/source.zip',
79+
retryAfterLimit: 120,
7880
output: 'path/to/my-ext-v1.2.3.xpi',
7981
});
8082
console.info('The signed file is stored at:', output);

src/bin.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ This command could be run multiple times to check the status, and the version wi
5959
'--compatibility <jsonString>',
6060
'the compatibility info as a JSON string, e.g. `["android","firefox"]`',
6161
)
62+
.option(
63+
'--retry-after-limit <maxTime>',
64+
'when a throttled error occurs, if a retry is possible within this parameter, the request will wait for a certain period before being re-requested. e.g. 120',
65+
)
6266
.option('--output <output>', 'the file path to save the signed XPI file')
6367
.option('--verbose', 'show verbose logs')
6468
.action(
@@ -68,6 +72,10 @@ This command could be run multiple times to check the status, and the version wi
6872
...command.optsWithGlobals(),
6973
};
7074
verifyKeys(options, ['apiKey', 'apiSecret', 'addonId', 'addonVersion']);
75+
let retryAfterLimit = Number(options.retryAfterLimit);
76+
if (Number.isNaN(retryAfterLimit)) {
77+
retryAfterLimit = 0;
78+
}
7179
const downloadedFile = await signAddon({
7280
apiKey: options.apiKey as string,
7381
apiSecret: options.apiSecret as string,
@@ -84,6 +92,7 @@ This command could be run multiple times to check the status, and the version wi
8492
compatibility: options.compatibility
8593
? (JSON.parse(options.compatibility) as CompatibilityInfo)
8694
: undefined,
95+
retryAfterLimit,
8796
output: options.output as string,
8897
onDebug: getLogHandler(options.verbose as boolean),
8998
});

src/index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,25 @@ export class AMOClient {
7979
});
8080
}
8181

82-
private async request<T = unknown>(url: string, opts?: RequestInit) {
82+
private async request<T = unknown>(url: string, opts?: RequestInit): Promise<T> {
8383
this.options.onDebug?.('request-start', {
8484
method: opts?.method || 'GET',
8585
url,
8686
});
8787
const res = await this.fetch(this.apiUrlPrefix + url, opts);
8888
const data = (await res.json()) as T;
8989
this.options.onDebug?.('request-end', { url, status: res.status });
90-
if (!res.ok) throw { res, data };
90+
if (!res.ok) {
91+
if (res.headers.has('retry-after') && this.options.retryAfterLimit && this.options.retryAfterLimit > 0) {
92+
const after = Number(res.headers.get('retry-after'));
93+
if (!Number.isNaN(after) && after <= this.options.retryAfterLimit) {
94+
// wait one more second
95+
await setTimeout(after * 1000 + 1000);
96+
return this.request<T>(url, opts);
97+
}
98+
}
99+
throw { res, data };
100+
}
91101
return data;
92102
}
93103

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ export type AMOClientActions =
3535

3636
export interface AMOClientOptions {
3737
onDebug?: (type: AMOClientActions, payload?: unknown) => void;
38+
/** When a throttled error occurs, if a retry is possible within this parameter, the system will wait until that time before retrying. */
39+
retryAfterLimit?: number;
3840
}
3941

4042
export interface FileInfo {

0 commit comments

Comments
 (0)