@thirdweb-dev/service-utils 0.0.0-dev-c57f450-20230913125903 → 0.0.0-dev-92be883-20230913130717

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,52 +2,10 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var index = require('../../dist/index-e1d8f989.cjs.dev.js');
6
- var services = require('../../dist/services-a3f36057.cjs.dev.js');
5
+ var index = require('../../dist/index-4e1ffe00.cjs.dev.js');
7
6
  var aws4fetch = require('aws4fetch');
8
7
  var zod = require('zod');
9
-
10
- const DEFAULT_RATE_LIMIT_WINDOW_SECONDS = 10;
11
- async function rateLimit(apiKeyMeta, serviceConfig, cacheOptions) {
12
- const {
13
- id,
14
- rateLimits,
15
- accountId
16
- } = apiKeyMeta;
17
- const {
18
- serviceScope
19
- } = serviceConfig;
20
- const limit = rateLimits[serviceScope];
21
- if (limit === undefined) {
22
- // No rate limit is provided. Assume the request is not rate limited.
23
- return {
24
- rateLimited: false
25
- };
26
- }
27
-
28
- // Floors the current time to the nearest DEFAULT_RATE_LIMIT_WINDOW_SECONDS.
29
- const bucketId = Math.floor(Date.now() / (1000 * DEFAULT_RATE_LIMIT_WINDOW_SECONDS)) * DEFAULT_RATE_LIMIT_WINDOW_SECONDS;
30
- const key = [serviceScope, accountId, bucketId].join(":");
31
- const value = parseInt((await cacheOptions.get(key)) || "0");
32
- const current = value + 1;
33
-
34
- // limit is in seconds, but we need in DEFAULT_RATE_LIMIT_WINDOW_SECONDS
35
- if (current > limit * DEFAULT_RATE_LIMIT_WINDOW_SECONDS) {
36
- // report rate limit hits
37
- await index.updateRateLimitedAt(id, serviceConfig);
38
- return {
39
- rateLimited: true,
40
- status: 429,
41
- errorMessage: `You've exceeded your ${serviceScope} rate limit at ${limit} reqs/sec. To get higher rate limits, contact us at https://thirdweb.com/contact-us.`,
42
- errorCode: "RATE_LIMIT_EXCEEDED"
43
- };
44
- } else {
45
- await cacheOptions.put(key, current.toString());
46
- }
47
- return {
48
- rateLimited: false
49
- };
50
- }
8
+ var services = require('../../dist/services-a3f36057.cjs.dev.js');
51
9
 
52
10
  // Initialize a singleton for aws usage.
53
11
  let _aws;
@@ -162,7 +120,7 @@ async function authorizeWorker(authInput, serviceConfig) {
162
120
  });
163
121
  }
164
122
  async function rateLimitWorker(apiKeyMeta, serviceConfig) {
165
- return await rateLimit(apiKeyMeta, serviceConfig, {
123
+ return await index.rateLimit(apiKeyMeta, serviceConfig, {
166
124
  get: async bucketId => serviceConfig.kvStore.get(bucketId),
167
125
  put: (bucketId, count) => serviceConfig.kvStore.put(bucketId, count, {
168
126
  expirationTtl: DEFAULT_RATE_LIMIT_CACHE_TTL_SECONDS
@@ -280,6 +238,8 @@ async function logHttpRequest(_ref) {
280
238
  console.log(`statusMessage=${statusMessage ?? res.statusText}`);
281
239
  }
282
240
 
241
+ exports.rateLimit = index.rateLimit;
242
+ exports.usageLimit = index.usageLimit;
283
243
  exports.SERVICES = services.SERVICES;
284
244
  exports.SERVICE_DEFINITIONS = services.SERVICE_DEFINITIONS;
285
245
  exports.SERVICE_NAMES = services.SERVICE_NAMES;
@@ -2,52 +2,10 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var index = require('../../dist/index-4b7b94ff.cjs.prod.js');
6
- var services = require('../../dist/services-9e185105.cjs.prod.js');
5
+ var index = require('../../dist/index-21ba31a2.cjs.prod.js');
7
6
  var aws4fetch = require('aws4fetch');
8
7
  var zod = require('zod');
9
-
10
- const DEFAULT_RATE_LIMIT_WINDOW_SECONDS = 10;
11
- async function rateLimit(apiKeyMeta, serviceConfig, cacheOptions) {
12
- const {
13
- id,
14
- rateLimits,
15
- accountId
16
- } = apiKeyMeta;
17
- const {
18
- serviceScope
19
- } = serviceConfig;
20
- const limit = rateLimits[serviceScope];
21
- if (limit === undefined) {
22
- // No rate limit is provided. Assume the request is not rate limited.
23
- return {
24
- rateLimited: false
25
- };
26
- }
27
-
28
- // Floors the current time to the nearest DEFAULT_RATE_LIMIT_WINDOW_SECONDS.
29
- const bucketId = Math.floor(Date.now() / (1000 * DEFAULT_RATE_LIMIT_WINDOW_SECONDS)) * DEFAULT_RATE_LIMIT_WINDOW_SECONDS;
30
- const key = [serviceScope, accountId, bucketId].join(":");
31
- const value = parseInt((await cacheOptions.get(key)) || "0");
32
- const current = value + 1;
33
-
34
- // limit is in seconds, but we need in DEFAULT_RATE_LIMIT_WINDOW_SECONDS
35
- if (current > limit * DEFAULT_RATE_LIMIT_WINDOW_SECONDS) {
36
- // report rate limit hits
37
- await index.updateRateLimitedAt(id, serviceConfig);
38
- return {
39
- rateLimited: true,
40
- status: 429,
41
- errorMessage: `You've exceeded your ${serviceScope} rate limit at ${limit} reqs/sec. To get higher rate limits, contact us at https://thirdweb.com/contact-us.`,
42
- errorCode: "RATE_LIMIT_EXCEEDED"
43
- };
44
- } else {
45
- await cacheOptions.put(key, current.toString());
46
- }
47
- return {
48
- rateLimited: false
49
- };
50
- }
8
+ var services = require('../../dist/services-9e185105.cjs.prod.js');
51
9
 
52
10
  // Initialize a singleton for aws usage.
53
11
  let _aws;
@@ -162,7 +120,7 @@ async function authorizeWorker(authInput, serviceConfig) {
162
120
  });
163
121
  }
164
122
  async function rateLimitWorker(apiKeyMeta, serviceConfig) {
165
- return await rateLimit(apiKeyMeta, serviceConfig, {
123
+ return await index.rateLimit(apiKeyMeta, serviceConfig, {
166
124
  get: async bucketId => serviceConfig.kvStore.get(bucketId),
167
125
  put: (bucketId, count) => serviceConfig.kvStore.put(bucketId, count, {
168
126
  expirationTtl: DEFAULT_RATE_LIMIT_CACHE_TTL_SECONDS
@@ -280,6 +238,8 @@ async function logHttpRequest(_ref) {
280
238
  console.log(`statusMessage=${statusMessage ?? res.statusText}`);
281
239
  }
282
240
 
241
+ exports.rateLimit = index.rateLimit;
242
+ exports.usageLimit = index.usageLimit;
283
243
  exports.SERVICES = services.SERVICES;
284
244
  exports.SERVICE_DEFINITIONS = services.SERVICE_DEFINITIONS;
285
245
  exports.SERVICE_NAMES = services.SERVICE_NAMES;
@@ -1,49 +1,8 @@
1
- import { u as updateRateLimitedAt, a as authorize } from '../../dist/index-3f3d50f4.esm.js';
2
- export { b as SERVICES, S as SERVICE_DEFINITIONS, a as SERVICE_NAMES, g as getServiceByName } from '../../dist/services-86283509.esm.js';
1
+ import { a as authorize, r as rateLimit } from '../../dist/index-524baaec.esm.js';
2
+ export { r as rateLimit, u as usageLimit } from '../../dist/index-524baaec.esm.js';
3
3
  import { AwsClient } from 'aws4fetch';
4
4
  import { z } from 'zod';
5
-
6
- const DEFAULT_RATE_LIMIT_WINDOW_SECONDS = 10;
7
- async function rateLimit(apiKeyMeta, serviceConfig, cacheOptions) {
8
- const {
9
- id,
10
- rateLimits,
11
- accountId
12
- } = apiKeyMeta;
13
- const {
14
- serviceScope
15
- } = serviceConfig;
16
- const limit = rateLimits[serviceScope];
17
- if (limit === undefined) {
18
- // No rate limit is provided. Assume the request is not rate limited.
19
- return {
20
- rateLimited: false
21
- };
22
- }
23
-
24
- // Floors the current time to the nearest DEFAULT_RATE_LIMIT_WINDOW_SECONDS.
25
- const bucketId = Math.floor(Date.now() / (1000 * DEFAULT_RATE_LIMIT_WINDOW_SECONDS)) * DEFAULT_RATE_LIMIT_WINDOW_SECONDS;
26
- const key = [serviceScope, accountId, bucketId].join(":");
27
- const value = parseInt((await cacheOptions.get(key)) || "0");
28
- const current = value + 1;
29
-
30
- // limit is in seconds, but we need in DEFAULT_RATE_LIMIT_WINDOW_SECONDS
31
- if (current > limit * DEFAULT_RATE_LIMIT_WINDOW_SECONDS) {
32
- // report rate limit hits
33
- await updateRateLimitedAt(id, serviceConfig);
34
- return {
35
- rateLimited: true,
36
- status: 429,
37
- errorMessage: `You've exceeded your ${serviceScope} rate limit at ${limit} reqs/sec. To get higher rate limits, contact us at https://thirdweb.com/contact-us.`,
38
- errorCode: "RATE_LIMIT_EXCEEDED"
39
- };
40
- } else {
41
- await cacheOptions.put(key, current.toString());
42
- }
43
- return {
44
- rateLimited: false
45
- };
46
- }
5
+ export { b as SERVICES, S as SERVICE_DEFINITIONS, a as SERVICE_NAMES, g as getServiceByName } from '../../dist/services-86283509.esm.js';
47
6
 
48
7
  // Initialize a singleton for aws usage.
49
8
  let _aws;
@@ -5,8 +5,10 @@ import type { AuthorizationInput } from "../core/authorize";
5
5
  import type { AuthorizationResult } from "../core/authorize/types";
6
6
  import type { RateLimitResult } from "../core/rateLimit/types";
7
7
  import type { CoreAuthInput } from "../core/types";
8
- export * from "../core/services";
9
8
  export * from "./usage";
9
+ export * from "../core/services";
10
+ export * from "../core/rateLimit";
11
+ export * from "../core/usageLimit";
10
12
  type WorkerServiceConfig = CoreServiceConfig & {
11
13
  kvStore: KVNamespace;
12
14
  ctx: ExecutionContext;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"../../../../src/cf-worker","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,gBAAgB,EAChB,WAAW,EACX,QAAQ,EACT,MAAM,2BAA2B,CAAC;AACnC,OAAO,KAAK,EACV,cAAc,EAEd,iBAAiB,EAClB,MAAM,aAAa,CAAC;AAGrB,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AACnE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC/D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAGnD,cAAc,kBAAkB,CAAC;AACjC,cAAc,SAAS,CAAC;AAExB,KAAK,mBAAmB,GAAG,iBAAiB,GAAG;IAC7C,OAAO,EAAE,WAAW,CAAC;IACrB,GAAG,EAAE,gBAAgB,CAAC;IACtB,eAAe,CAAC,EAAE,MAAM,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BAAyB,CAAC;AAC/D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAGnD,cAAc,SAAS,CAAC;AACxB,cAAc,kBAAkB,CAAC;AACjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AAEnC,KAAK,mBAAmB,GAAG,iBAAiB,GAAG;IAC7C,OAAO,EAAE,WAAW,CAAC;IACrB,GAAG,EAAE,gBAAgB,CAAC;IACtB,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B,CAAC;AAMF,KAAK,SAAS,GAAG,aAAa,GAAG;IAC/B,GAAG,EAAE,OAAO,CAAC;CACd,CAAC;AAEF,wBAAsB,eAAe,CACnC,SAAS,EAAE,SAAS,EACpB,aAAa,EAAE,mBAAmB,GACjC,OAAO,CAAC,mBAAmB,CAAC,CA0C9B;AAED,wBAAsB,eAAe,CACnC,UAAU,EAAE,cAAc,EAC1B,aAAa,EAAE,mBAAmB,GACjC,OAAO,CAAC,eAAe,CAAC,CAQ1B;AAED,wBAAsB,wBAAwB,CAC5C,SAAS,EAAE,SAAS,GACnB,OAAO,CAAC,kBAAkB,CAAC,CA2E7B;AAED,wBAAsB,aAAa,CAAC,SAAS,EAAE,MAAM,mBAIpD;AAED,wBAAgB,+BAA+B,CAAC,aAAa,EAAE,MAAM,UAEpE;AAQD,wBAAsB,cAAc,CAAC,EACnC,MAAM,EACN,QAAQ,EACR,GAAG,EACH,GAAG,EACH,QAAQ,EACR,aAAa,GACd,EAAE,SAAS,GAAG;IACb,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,EAAE,QAAQ,CAAC;IACd,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,aAAa,CAAC,EAAE,KAAK,GAAG,MAAM,CAAC;CAChC,iBAoBA"}
@@ -0,0 +1,4 @@
1
+ import { ApiKeyMetadata, CoreServiceConfig } from "../api";
2
+ import { UsageLimitResult } from "./types";
3
+ export declare function usageLimit(apiKeyMeta: ApiKeyMetadata, serviceConfig: CoreServiceConfig): Promise<UsageLimitResult>;
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"../../../../../src/core/usageLimit","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,QAAQ,CAAC;AAE3D,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAE3C,wBAAsB,UAAU,CAC9B,UAAU,EAAE,cAAc,EAC1B,aAAa,EAAE,iBAAiB,GAC/B,OAAO,CAAC,gBAAgB,CAAC,CA4B3B"}
@@ -0,0 +1,9 @@
1
+ export type UsageLimitResult = {
2
+ usageLimited: false;
3
+ } | {
4
+ usageLimited: true;
5
+ status: number;
6
+ errorMessage: string;
7
+ errorCode: string;
8
+ };
9
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"../../../../../src/core/usageLimit","sources":["types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,gBAAgB,GACxB;IACE,YAAY,EAAE,KAAK,CAAC;CACrB,GACD;IACE,YAAY,EAAE,IAAI,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC"}
@@ -6,6 +6,8 @@ import type { AuthorizationInput } from "../core/authorize";
6
6
  import type { AuthorizationResult } from "../core/authorize/types";
7
7
  import type { CoreAuthInput } from "../core/types";
8
8
  export * from "../core/services";
9
+ export * from "../core/rateLimit";
10
+ export * from "../core/usageLimit";
9
11
  type NodeServiceConfig = CoreServiceConfig;
10
12
  export type AuthInput = CoreAuthInput & {
11
13
  req: IncomingMessage;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"../../../../src/node","sources":["index.ts"],"names":[],"mappings":";AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,MAAM,CA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AAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AACnE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAEnD,cAAc,kBAAkB,CAAC;AACjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AAEnC,KAAK,iBAAiB,GAAG,iBAAiB,CAAC;AAE3C,MAAM,MAAM,SAAS,GAAG,aAAa,GAAG;IACtC,GAAG,EAAE,eAAe,CAAC;CACtB,CAAC;AAEF;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,aAAa,CACjC,SAAS,EAAE,SAAS,EACpB,aAAa,EAAE,iBAAiB,GAC/B,OAAO,CAAC,mBAAmB,CAAC,CAsB9B;AAaD,wBAAgB,wBAAwB,CACtC,SAAS,EAAE,SAAS,GACnB,kBAAkB,CA2FpB;AAED,wBAAgB,aAAa,CAAC,SAAS,EAAE,MAAM,UAE9C;AAED,wBAAgB,+BAA+B,CAAC,aAAa,EAAE,MAAM,UAEpE;AAED,wBAAgB,cAAc,CAAC,EAC7B,MAAM,EACN,QAAQ,EACR,GAAG,EACH,GAAG,EACH,QAAQ,EACR,aAAa,GACd,EAAE,SAAS,GAAG;IACb,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,EAAE,cAAc,CAAC;IACpB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,aAAa,CAAC,EAAE,KAAK,GAAG,MAAM,CAAC;CAChC,QAsBA"}
@@ -440,5 +440,76 @@ async function authorize(authData, serviceConfig, cacheOptions) {
440
440
  };
441
441
  }
442
442
 
443
+ const DEFAULT_RATE_LIMIT_WINDOW_SECONDS = 10;
444
+ async function rateLimit(apiKeyMeta, serviceConfig, cacheOptions) {
445
+ const {
446
+ id,
447
+ rateLimits,
448
+ accountId
449
+ } = apiKeyMeta;
450
+ const {
451
+ serviceScope
452
+ } = serviceConfig;
453
+ const limit = rateLimits[serviceScope];
454
+ if (limit === undefined) {
455
+ // No rate limit is provided. Assume the request is not rate limited.
456
+ return {
457
+ rateLimited: false
458
+ };
459
+ }
460
+
461
+ // Floors the current time to the nearest DEFAULT_RATE_LIMIT_WINDOW_SECONDS.
462
+ const bucketId = Math.floor(Date.now() / (1000 * DEFAULT_RATE_LIMIT_WINDOW_SECONDS)) * DEFAULT_RATE_LIMIT_WINDOW_SECONDS;
463
+ const key = [serviceScope, accountId, bucketId].join(":");
464
+ const value = parseInt((await cacheOptions.get(key)) || "0");
465
+ const current = value + 1;
466
+
467
+ // limit is in seconds, but we need in DEFAULT_RATE_LIMIT_WINDOW_SECONDS
468
+ if (current > limit * DEFAULT_RATE_LIMIT_WINDOW_SECONDS) {
469
+ // report rate limit hits
470
+ await updateRateLimitedAt(id, serviceConfig);
471
+ return {
472
+ rateLimited: true,
473
+ status: 429,
474
+ errorMessage: `You've exceeded your ${serviceScope} rate limit at ${limit} reqs/sec. To get higher rate limits, contact us at https://thirdweb.com/contact-us.`,
475
+ errorCode: "RATE_LIMIT_EXCEEDED"
476
+ };
477
+ } else {
478
+ await cacheOptions.put(key, current.toString());
479
+ }
480
+ return {
481
+ rateLimited: false
482
+ };
483
+ }
484
+
485
+ async function usageLimit(apiKeyMeta, serviceConfig) {
486
+ const {
487
+ limits,
488
+ usage
489
+ } = apiKeyMeta;
490
+ const {
491
+ serviceScope
492
+ } = serviceConfig;
493
+ const limit = limits[serviceScope];
494
+ if (!usage || !(serviceScope in usage) || limit === undefined) {
495
+ // No usage limit is provided. Assume the request is not limited.
496
+ return {
497
+ usageLimited: false
498
+ };
499
+ }
500
+ if (serviceScope === "storage" && (usage.storage?.sumFileSizeBytes || 0) > limit) {
501
+ return {
502
+ usageLimited: true,
503
+ status: 402,
504
+ errorMessage: `You've used all of your total usage limit for Storage Pinning. Please add your payment method at https://thirdweb.com/dashboard/settings/billing.`,
505
+ errorCode: "PAYMENT_METHOD_REQUIRED"
506
+ };
507
+ }
508
+ return {
509
+ usageLimited: false
510
+ };
511
+ }
512
+
443
513
  exports.authorize = authorize;
444
- exports.updateRateLimitedAt = updateRateLimitedAt;
514
+ exports.rateLimit = rateLimit;
515
+ exports.usageLimit = usageLimit;
@@ -440,5 +440,76 @@ async function authorize(authData, serviceConfig, cacheOptions) {
440
440
  };
441
441
  }
442
442
 
443
+ const DEFAULT_RATE_LIMIT_WINDOW_SECONDS = 10;
444
+ async function rateLimit(apiKeyMeta, serviceConfig, cacheOptions) {
445
+ const {
446
+ id,
447
+ rateLimits,
448
+ accountId
449
+ } = apiKeyMeta;
450
+ const {
451
+ serviceScope
452
+ } = serviceConfig;
453
+ const limit = rateLimits[serviceScope];
454
+ if (limit === undefined) {
455
+ // No rate limit is provided. Assume the request is not rate limited.
456
+ return {
457
+ rateLimited: false
458
+ };
459
+ }
460
+
461
+ // Floors the current time to the nearest DEFAULT_RATE_LIMIT_WINDOW_SECONDS.
462
+ const bucketId = Math.floor(Date.now() / (1000 * DEFAULT_RATE_LIMIT_WINDOW_SECONDS)) * DEFAULT_RATE_LIMIT_WINDOW_SECONDS;
463
+ const key = [serviceScope, accountId, bucketId].join(":");
464
+ const value = parseInt((await cacheOptions.get(key)) || "0");
465
+ const current = value + 1;
466
+
467
+ // limit is in seconds, but we need in DEFAULT_RATE_LIMIT_WINDOW_SECONDS
468
+ if (current > limit * DEFAULT_RATE_LIMIT_WINDOW_SECONDS) {
469
+ // report rate limit hits
470
+ await updateRateLimitedAt(id, serviceConfig);
471
+ return {
472
+ rateLimited: true,
473
+ status: 429,
474
+ errorMessage: `You've exceeded your ${serviceScope} rate limit at ${limit} reqs/sec. To get higher rate limits, contact us at https://thirdweb.com/contact-us.`,
475
+ errorCode: "RATE_LIMIT_EXCEEDED"
476
+ };
477
+ } else {
478
+ await cacheOptions.put(key, current.toString());
479
+ }
480
+ return {
481
+ rateLimited: false
482
+ };
483
+ }
484
+
485
+ async function usageLimit(apiKeyMeta, serviceConfig) {
486
+ const {
487
+ limits,
488
+ usage
489
+ } = apiKeyMeta;
490
+ const {
491
+ serviceScope
492
+ } = serviceConfig;
493
+ const limit = limits[serviceScope];
494
+ if (!usage || !(serviceScope in usage) || limit === undefined) {
495
+ // No usage limit is provided. Assume the request is not limited.
496
+ return {
497
+ usageLimited: false
498
+ };
499
+ }
500
+ if (serviceScope === "storage" && (usage.storage?.sumFileSizeBytes || 0) > limit) {
501
+ return {
502
+ usageLimited: true,
503
+ status: 402,
504
+ errorMessage: `You've used all of your total usage limit for Storage Pinning. Please add your payment method at https://thirdweb.com/dashboard/settings/billing.`,
505
+ errorCode: "PAYMENT_METHOD_REQUIRED"
506
+ };
507
+ }
508
+ return {
509
+ usageLimited: false
510
+ };
511
+ }
512
+
443
513
  exports.authorize = authorize;
444
- exports.updateRateLimitedAt = updateRateLimitedAt;
514
+ exports.rateLimit = rateLimit;
515
+ exports.usageLimit = usageLimit;
@@ -438,4 +438,74 @@ async function authorize(authData, serviceConfig, cacheOptions) {
438
438
  };
439
439
  }
440
440
 
441
- export { authorize as a, updateRateLimitedAt as u };
441
+ const DEFAULT_RATE_LIMIT_WINDOW_SECONDS = 10;
442
+ async function rateLimit(apiKeyMeta, serviceConfig, cacheOptions) {
443
+ const {
444
+ id,
445
+ rateLimits,
446
+ accountId
447
+ } = apiKeyMeta;
448
+ const {
449
+ serviceScope
450
+ } = serviceConfig;
451
+ const limit = rateLimits[serviceScope];
452
+ if (limit === undefined) {
453
+ // No rate limit is provided. Assume the request is not rate limited.
454
+ return {
455
+ rateLimited: false
456
+ };
457
+ }
458
+
459
+ // Floors the current time to the nearest DEFAULT_RATE_LIMIT_WINDOW_SECONDS.
460
+ const bucketId = Math.floor(Date.now() / (1000 * DEFAULT_RATE_LIMIT_WINDOW_SECONDS)) * DEFAULT_RATE_LIMIT_WINDOW_SECONDS;
461
+ const key = [serviceScope, accountId, bucketId].join(":");
462
+ const value = parseInt((await cacheOptions.get(key)) || "0");
463
+ const current = value + 1;
464
+
465
+ // limit is in seconds, but we need in DEFAULT_RATE_LIMIT_WINDOW_SECONDS
466
+ if (current > limit * DEFAULT_RATE_LIMIT_WINDOW_SECONDS) {
467
+ // report rate limit hits
468
+ await updateRateLimitedAt(id, serviceConfig);
469
+ return {
470
+ rateLimited: true,
471
+ status: 429,
472
+ errorMessage: `You've exceeded your ${serviceScope} rate limit at ${limit} reqs/sec. To get higher rate limits, contact us at https://thirdweb.com/contact-us.`,
473
+ errorCode: "RATE_LIMIT_EXCEEDED"
474
+ };
475
+ } else {
476
+ await cacheOptions.put(key, current.toString());
477
+ }
478
+ return {
479
+ rateLimited: false
480
+ };
481
+ }
482
+
483
+ async function usageLimit(apiKeyMeta, serviceConfig) {
484
+ const {
485
+ limits,
486
+ usage
487
+ } = apiKeyMeta;
488
+ const {
489
+ serviceScope
490
+ } = serviceConfig;
491
+ const limit = limits[serviceScope];
492
+ if (!usage || !(serviceScope in usage) || limit === undefined) {
493
+ // No usage limit is provided. Assume the request is not limited.
494
+ return {
495
+ usageLimited: false
496
+ };
497
+ }
498
+ if (serviceScope === "storage" && (usage.storage?.sumFileSizeBytes || 0) > limit) {
499
+ return {
500
+ usageLimited: true,
501
+ status: 402,
502
+ errorMessage: `You've used all of your total usage limit for Storage Pinning. Please add your payment method at https://thirdweb.com/dashboard/settings/billing.`,
503
+ errorCode: "PAYMENT_METHOD_REQUIRED"
504
+ };
505
+ }
506
+ return {
507
+ usageLimited: false
508
+ };
509
+ }
510
+
511
+ export { authorize as a, rateLimit as r, usageLimit as u };
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var node_crypto = require('node:crypto');
6
- var index = require('../../dist/index-e1d8f989.cjs.dev.js');
6
+ var index = require('../../dist/index-4e1ffe00.cjs.dev.js');
7
7
  var services = require('../../dist/services-a3f36057.cjs.dev.js');
8
8
 
9
9
  /**
@@ -173,6 +173,8 @@ function logHttpRequest(_ref) {
173
173
  console.log(`statusMessage=${_statusMessage}`);
174
174
  }
175
175
 
176
+ exports.rateLimit = index.rateLimit;
177
+ exports.usageLimit = index.usageLimit;
176
178
  exports.SERVICES = services.SERVICES;
177
179
  exports.SERVICE_DEFINITIONS = services.SERVICE_DEFINITIONS;
178
180
  exports.SERVICE_NAMES = services.SERVICE_NAMES;
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var node_crypto = require('node:crypto');
6
- var index = require('../../dist/index-4b7b94ff.cjs.prod.js');
6
+ var index = require('../../dist/index-21ba31a2.cjs.prod.js');
7
7
  var services = require('../../dist/services-9e185105.cjs.prod.js');
8
8
 
9
9
  /**
@@ -173,6 +173,8 @@ function logHttpRequest(_ref) {
173
173
  console.log(`statusMessage=${_statusMessage}`);
174
174
  }
175
175
 
176
+ exports.rateLimit = index.rateLimit;
177
+ exports.usageLimit = index.usageLimit;
176
178
  exports.SERVICES = services.SERVICES;
177
179
  exports.SERVICE_DEFINITIONS = services.SERVICE_DEFINITIONS;
178
180
  exports.SERVICE_NAMES = services.SERVICE_NAMES;
@@ -1,5 +1,6 @@
1
1
  import { createHash } from 'node:crypto';
2
- import { a as authorize } from '../../dist/index-3f3d50f4.esm.js';
2
+ import { a as authorize } from '../../dist/index-524baaec.esm.js';
3
+ export { r as rateLimit, u as usageLimit } from '../../dist/index-524baaec.esm.js';
3
4
  export { b as SERVICES, S as SERVICE_DEFINITIONS, a as SERVICE_NAMES, g as getServiceByName } from '../../dist/services-86283509.esm.js';
4
5
 
5
6
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thirdweb-dev/service-utils",
3
- "version": "0.0.0-dev-c57f450-20230913125903",
3
+ "version": "0.0.0-dev-92be883-20230913130717",
4
4
  "main": "dist/thirdweb-dev-service-utils.cjs.js",
5
5
  "module": "dist/thirdweb-dev-service-utils.esm.js",
6
6
  "exports": {