From 0485b2de25aad9780894dc45c0c11c24a415c089 Mon Sep 17 00:00:00 2001 From: Mikael Finstad Date: Wed, 26 Feb 2025 12:58:26 +0900 Subject: [PATCH 1/3] support HTTP 429 rate limiting the initial implementation of the rate limiting based on http 413 was based on https://transloadit.com/blog/2012/04/introducing-rate-limiting/ but some time after that, the api seems to have changed to instead reply 429, and now rate limiting seems to be broken, as evident by some tests failing. this code adds support for rate limiting 429 (in addition to 413) --- src/Transloadit.ts | 8 +++++--- test/unit/mock-http.test.ts | 6 +++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Transloadit.ts b/src/Transloadit.ts index da314a2..0f0bb7d 100644 --- a/src/Transloadit.ts +++ b/src/Transloadit.ts @@ -746,18 +746,20 @@ export class Transloadit { // https://transloadit.com/blog/2012/04/introducing-rate-limiting/ if ( !( - statusCode === 413 && typeof body === 'object' && body != null && 'error' in body && - body.error === 'RATE_LIMIT_REACHED' && 'info' in body && typeof body.info === 'object' && body.info != null && 'retryIn' in body.info && typeof body.info.retryIn === 'number' && Boolean(body.info.retryIn) && - retryCount < this._maxRetries + retryCount < this._maxRetries && + (// 413 taken from https://transloadit.com/blog/2012/04/introducing-rate-limiting/ + // todo can it be removed? + (statusCode === 413 && body.error === 'RATE_LIMIT_REACHED') || + statusCode === 429) ) ) { throw new ApiError({ diff --git a/test/unit/mock-http.test.ts b/test/unit/mock-http.test.ts index be640cc..b59d6e6 100644 --- a/test/unit/mock-http.test.ts +++ b/test/unit/mock-http.test.ts @@ -178,7 +178,7 @@ describe('Mocked API tests', () => { const scope = nock('http://localhost') .post(createAssemblyRegex) - .reply(413, { error: 'RATE_LIMIT_REACHED', info: { retryIn: 0.01 } }) + .reply(429, { error: 'ASSEMBLY_STATUS_FETCHING_RATE_LIMIT_REACHED', info: { retryIn: 0.01 } }) .post(createAssemblyRegex) .reply(200, { ok: 'ASSEMBLY_EXECUTING' }) @@ -191,7 +191,7 @@ describe('Mocked API tests', () => { const scope = nock('http://localhost') .post(createAssemblyRegex) - .reply(413, { + .reply(429, { error: 'RATE_LIMIT_REACHED', message: 'Request limit reached', info: { retryIn: 0.01 }, @@ -199,7 +199,7 @@ describe('Mocked API tests', () => { await expect(client.createAssembly()).rejects.toThrow( expect.objectContaining({ - message: 'API error (HTTP 413) RATE_LIMIT_REACHED: Request limit reached', + message: 'API error (HTTP 429) RATE_LIMIT_REACHED: Request limit reached', code: 'RATE_LIMIT_REACHED', }) ) From 5db4177f35adee738951e0fc6baea527a325733c Mon Sep 17 00:00:00 2001 From: Mikael Finstad Date: Wed, 26 Feb 2025 12:58:38 +0900 Subject: [PATCH 2/3] fix broken npm scripts --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 963f7af..be07d11 100644 --- a/package.json +++ b/package.json @@ -72,8 +72,8 @@ "prepack": "tsc --build", "test-unit": "vitest run --coverage ./test/unit", "test-integration": "vitest run ./test/integration", - "test-all": "npm run tsd && vitest run --coverage", - "test": "npm run tsd && npm run test-unit", + "test-all": "vitest run --coverage", + "test": "yarn test-unit", "fix:formatting": "prettier --write .", "lint:formatting": "prettier --check ." }, From 1b78e49fbaf440a3dbd8378c9c4b5c3f8e7ebb4a Mon Sep 17 00:00:00 2001 From: Mikael Finstad Date: Wed, 26 Feb 2025 13:00:55 +0900 Subject: [PATCH 3/3] fix format --- src/Transloadit.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Transloadit.ts b/src/Transloadit.ts index 0f0bb7d..ed14b1b 100644 --- a/src/Transloadit.ts +++ b/src/Transloadit.ts @@ -755,11 +755,9 @@ export class Transloadit { 'retryIn' in body.info && typeof body.info.retryIn === 'number' && Boolean(body.info.retryIn) && - retryCount < this._maxRetries && - (// 413 taken from https://transloadit.com/blog/2012/04/introducing-rate-limiting/ + retryCount < this._maxRetries && // 413 taken from https://transloadit.com/blog/2012/04/introducing-rate-limiting/ // todo can it be removed? - (statusCode === 413 && body.error === 'RATE_LIMIT_REACHED') || - statusCode === 429) + ((statusCode === 413 && body.error === 'RATE_LIMIT_REACHED') || statusCode === 429) ) ) { throw new ApiError({