@thirdweb-dev/service-utils 0.4.38 → 0.4.39

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.
Files changed (149) hide show
  1. package/dist/cjs/cf-worker/index.js +155 -0
  2. package/dist/cjs/cf-worker/index.js.map +1 -0
  3. package/dist/cjs/cf-worker/usage.js +55 -0
  4. package/dist/cjs/cf-worker/usage.js.map +1 -0
  5. package/dist/cjs/core/api.js +65 -0
  6. package/dist/cjs/core/api.js.map +1 -0
  7. package/dist/cjs/core/authorize/client.js +115 -0
  8. package/dist/cjs/core/authorize/client.js.map +1 -0
  9. package/dist/cjs/core/authorize/index.js +215 -0
  10. package/dist/cjs/core/authorize/index.js.map +1 -0
  11. package/dist/cjs/core/authorize/service.js +58 -0
  12. package/dist/cjs/core/authorize/service.js.map +1 -0
  13. package/dist/cjs/core/authorize/types.js +3 -0
  14. package/dist/cjs/core/authorize/types.js.map +1 -0
  15. package/dist/cjs/core/rateLimit/index.js +64 -0
  16. package/dist/cjs/core/rateLimit/index.js.map +1 -0
  17. package/dist/cjs/core/rateLimit/types.js +3 -0
  18. package/dist/cjs/core/rateLimit/types.js.map +1 -0
  19. package/dist/cjs/core/services.js +71 -0
  20. package/dist/cjs/core/services.js.map +1 -0
  21. package/dist/cjs/core/types.js +3 -0
  22. package/dist/cjs/core/types.js.map +1 -0
  23. package/dist/cjs/core/usage.js +93 -0
  24. package/dist/cjs/core/usage.js.map +1 -0
  25. package/dist/cjs/core/usageLimit/index.js +45 -0
  26. package/dist/cjs/core/usageLimit/index.js.map +1 -0
  27. package/dist/cjs/core/usageLimit/types.js +3 -0
  28. package/dist/cjs/core/usageLimit/types.js.map +1 -0
  29. package/dist/cjs/index.js +10 -0
  30. package/dist/cjs/index.js.map +1 -0
  31. package/dist/cjs/mocks.js +53 -0
  32. package/dist/cjs/mocks.js.map +1 -0
  33. package/dist/cjs/node/index.js +173 -0
  34. package/dist/cjs/node/index.js.map +1 -0
  35. package/dist/cjs/package.json +1 -0
  36. package/dist/esm/cf-worker/index.js +147 -0
  37. package/dist/esm/cf-worker/index.js.map +1 -0
  38. package/dist/esm/cf-worker/usage.js +54 -0
  39. package/dist/esm/cf-worker/usage.js.map +1 -0
  40. package/dist/esm/core/api.js +60 -0
  41. package/dist/esm/core/api.js.map +1 -0
  42. package/dist/esm/core/authorize/client.js +110 -0
  43. package/dist/esm/core/authorize/client.js.map +1 -0
  44. package/dist/esm/core/authorize/index.js +212 -0
  45. package/dist/esm/core/authorize/index.js.map +1 -0
  46. package/dist/esm/core/authorize/service.js +55 -0
  47. package/dist/esm/core/authorize/service.js.map +1 -0
  48. package/dist/esm/core/authorize/types.js +2 -0
  49. package/dist/esm/core/authorize/types.js.map +1 -0
  50. package/dist/esm/core/rateLimit/index.js +61 -0
  51. package/dist/esm/core/rateLimit/index.js.map +1 -0
  52. package/dist/esm/core/rateLimit/types.js +2 -0
  53. package/dist/esm/core/rateLimit/types.js.map +1 -0
  54. package/dist/esm/core/services.js +67 -0
  55. package/dist/esm/core/services.js.map +1 -0
  56. package/dist/esm/core/types.js +2 -0
  57. package/dist/esm/core/types.js.map +1 -0
  58. package/dist/esm/core/usage.js +90 -0
  59. package/dist/esm/core/usage.js.map +1 -0
  60. package/dist/esm/core/usageLimit/index.js +42 -0
  61. package/dist/esm/core/usageLimit/index.js.map +1 -0
  62. package/dist/esm/core/usageLimit/types.js +2 -0
  63. package/dist/esm/core/usageLimit/types.js.map +1 -0
  64. package/dist/esm/index.js +4 -0
  65. package/dist/esm/index.js.map +1 -0
  66. package/dist/esm/mocks.js +50 -0
  67. package/dist/esm/mocks.js.map +1 -0
  68. package/dist/esm/node/index.js +165 -0
  69. package/dist/esm/node/index.js.map +1 -0
  70. package/dist/esm/package.json +1 -0
  71. package/dist/{declarations/src → types}/cf-worker/index.d.ts +8 -8
  72. package/dist/types/cf-worker/index.d.ts.map +1 -0
  73. package/dist/{declarations/src → types}/cf-worker/usage.d.ts +3 -2
  74. package/dist/types/cf-worker/usage.d.ts.map +1 -0
  75. package/dist/{declarations/src → types}/core/api.d.ts +1 -1
  76. package/dist/types/core/api.d.ts.map +1 -0
  77. package/dist/{declarations/src → types}/core/authorize/client.d.ts +2 -2
  78. package/dist/types/core/authorize/client.d.ts.map +1 -0
  79. package/dist/{declarations/src → types}/core/authorize/index.d.ts +2 -2
  80. package/dist/types/core/authorize/index.d.ts.map +1 -0
  81. package/dist/{declarations/src → types}/core/authorize/service.d.ts +2 -2
  82. package/dist/types/core/authorize/service.d.ts.map +1 -0
  83. package/dist/{declarations/src → types}/core/authorize/types.d.ts +1 -1
  84. package/dist/types/core/authorize/types.d.ts.map +1 -0
  85. package/dist/{declarations/src → types}/core/rateLimit/index.d.ts +3 -3
  86. package/dist/types/core/rateLimit/index.d.ts.map +1 -0
  87. package/dist/types/core/rateLimit/types.d.ts.map +1 -0
  88. package/dist/types/core/services.d.ts.map +1 -0
  89. package/dist/types/core/types.d.ts.map +1 -0
  90. package/dist/{declarations/src → types}/core/usage.d.ts +2 -2
  91. package/dist/types/core/usage.d.ts.map +1 -0
  92. package/dist/types/core/usageLimit/index.d.ts +5 -0
  93. package/dist/types/core/usageLimit/index.d.ts.map +1 -0
  94. package/dist/types/core/usageLimit/types.d.ts.map +1 -0
  95. package/dist/types/index.d.ts +3 -0
  96. package/dist/types/index.d.ts.map +1 -0
  97. package/dist/types/mocks.d.ts +5 -0
  98. package/dist/types/mocks.d.ts.map +1 -0
  99. package/dist/{declarations/src → types}/node/index.d.ts +8 -8
  100. package/dist/types/node/index.d.ts.map +1 -0
  101. package/package.json +36 -29
  102. package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.cjs.d.ts +0 -2
  103. package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.cjs.d.ts.map +0 -1
  104. package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.cjs.dev.js +0 -226
  105. package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.cjs.js +0 -7
  106. package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.cjs.prod.js +0 -226
  107. package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.esm.js +0 -212
  108. package/cf-worker/package.json +0 -4
  109. package/dist/client-8440b8fb.esm.js +0 -195
  110. package/dist/client-84e46164.cjs.prod.js +0 -203
  111. package/dist/client-a5cc5822.cjs.dev.js +0 -203
  112. package/dist/declarations/src/cf-worker/index.d.ts.map +0 -1
  113. package/dist/declarations/src/cf-worker/usage.d.ts.map +0 -1
  114. package/dist/declarations/src/core/api.d.ts.map +0 -1
  115. package/dist/declarations/src/core/authorize/client.d.ts.map +0 -1
  116. package/dist/declarations/src/core/authorize/index.d.ts.map +0 -1
  117. package/dist/declarations/src/core/authorize/service.d.ts.map +0 -1
  118. package/dist/declarations/src/core/authorize/types.d.ts.map +0 -1
  119. package/dist/declarations/src/core/rateLimit/index.d.ts.map +0 -1
  120. package/dist/declarations/src/core/rateLimit/types.d.ts.map +0 -1
  121. package/dist/declarations/src/core/services.d.ts.map +0 -1
  122. package/dist/declarations/src/core/types.d.ts.map +0 -1
  123. package/dist/declarations/src/core/usage.d.ts.map +0 -1
  124. package/dist/declarations/src/core/usageLimit/index.d.ts +0 -5
  125. package/dist/declarations/src/core/usageLimit/index.d.ts.map +0 -1
  126. package/dist/declarations/src/core/usageLimit/types.d.ts.map +0 -1
  127. package/dist/declarations/src/index.d.ts +0 -3
  128. package/dist/declarations/src/index.d.ts.map +0 -1
  129. package/dist/declarations/src/node/index.d.ts.map +0 -1
  130. package/dist/index-23f268d8.cjs.prod.js +0 -540
  131. package/dist/index-5dc16842.esm.js +0 -535
  132. package/dist/index-88f1ffb6.cjs.dev.js +0 -540
  133. package/dist/thirdweb-dev-service-utils.cjs.d.ts +0 -2
  134. package/dist/thirdweb-dev-service-utils.cjs.d.ts.map +0 -1
  135. package/dist/thirdweb-dev-service-utils.cjs.dev.js +0 -14
  136. package/dist/thirdweb-dev-service-utils.cjs.js +0 -7
  137. package/dist/thirdweb-dev-service-utils.cjs.prod.js +0 -14
  138. package/dist/thirdweb-dev-service-utils.esm.js +0 -1
  139. package/node/dist/thirdweb-dev-service-utils-node.cjs.d.ts +0 -2
  140. package/node/dist/thirdweb-dev-service-utils-node.cjs.d.ts.map +0 -1
  141. package/node/dist/thirdweb-dev-service-utils-node.cjs.dev.js +0 -190
  142. package/node/dist/thirdweb-dev-service-utils-node.cjs.js +0 -7
  143. package/node/dist/thirdweb-dev-service-utils-node.cjs.prod.js +0 -190
  144. package/node/dist/thirdweb-dev-service-utils-node.esm.js +0 -176
  145. package/node/package.json +0 -4
  146. /package/dist/{declarations/src → types}/core/rateLimit/types.d.ts +0 -0
  147. /package/dist/{declarations/src → types}/core/services.d.ts +0 -0
  148. /package/dist/{declarations/src → types}/core/types.d.ts +0 -0
  149. /package/dist/{declarations/src → types}/core/usageLimit/types.d.ts +0 -0
@@ -1,540 +0,0 @@
1
- 'use strict';
2
-
3
- var client = require('./client-a5cc5822.cjs.dev.js');
4
- var zod = require('zod');
5
-
6
- async function fetchKeyMetadataFromApi(clientId, config) {
7
- const {
8
- apiUrl,
9
- serviceScope,
10
- serviceApiKey,
11
- checkPolicy,
12
- policyMetadata
13
- } = config;
14
- const policyQuery = checkPolicy && policyMetadata ? `&checkPolicy=true&policyMetadata=${encodeURIComponent(JSON.stringify(policyMetadata))}` : "";
15
- const url = `${apiUrl}/v1/keys/use?clientId=${clientId}&scope=${serviceScope}&includeUsage=true${policyQuery}`;
16
- const response = await fetch(url, {
17
- method: "GET",
18
- headers: {
19
- "x-service-api-key": serviceApiKey,
20
- "content-type": "application/json"
21
- }
22
- });
23
- let text = "";
24
- try {
25
- text = await response.text();
26
- return JSON.parse(text);
27
- } catch (e) {
28
- throw new Error(`Error fetching key metadata from API: ${response.status} - ${text}`);
29
- }
30
- }
31
- async function fetchAccountFromApi(jwt, config, useWalletAuth) {
32
- const {
33
- apiUrl,
34
- serviceApiKey
35
- } = config;
36
- const url = useWalletAuth ? `${apiUrl}/v1/wallet/me?includeUsage=true` : `${apiUrl}/v1/account/me?includeUsage=true`;
37
- const response = await fetch(url, {
38
- method: "GET",
39
- headers: {
40
- "x-service-api-key": serviceApiKey,
41
- "content-type": "application/json",
42
- authorization: `Bearer ${jwt}`
43
- }
44
- });
45
- let text = "";
46
- try {
47
- text = await response.text();
48
- return JSON.parse(text);
49
- } catch (e) {
50
- throw new Error(`Error fetching account from API: ${response.status} - ${text}`);
51
- }
52
- }
53
- async function updateRateLimitedAt(apiKeyId, config) {
54
- const {
55
- apiUrl,
56
- serviceScope: scope,
57
- serviceApiKey
58
- } = config;
59
- const url = `${apiUrl}/usage/rateLimit`;
60
- await fetch(url, {
61
- method: "PUT",
62
- headers: {
63
- "x-service-api-key": serviceApiKey,
64
- "content-type": "application/json"
65
- },
66
- body: JSON.stringify({
67
- apiKeyId,
68
- scope
69
- })
70
- });
71
- }
72
-
73
- function authorizeService(apiKeyMetadata, serviceConfig, authorizationPayload) {
74
- const {
75
- services
76
- } = apiKeyMetadata;
77
- // validate services
78
- const service = services.find(srv => srv.name === serviceConfig.serviceScope);
79
- if (!service) {
80
- return {
81
- authorized: false,
82
- errorMessage: `Invalid request: Unauthorized service: ${serviceConfig.serviceScope}. You can view the restrictions on this API key in your dashboard: https://thirdweb.com/create-api-key`,
83
- errorCode: "SERVICE_UNAUTHORIZED",
84
- status: 403
85
- };
86
- }
87
-
88
- // validate service actions
89
- if (serviceConfig.serviceAction) {
90
- const isActionAllowed = service.actions.includes(serviceConfig.serviceAction);
91
- if (!isActionAllowed) {
92
- return {
93
- authorized: false,
94
- errorMessage: `Invalid request: Unauthorized action: ${serviceConfig.serviceScope} ${serviceConfig.serviceAction}. You can view the restrictions on this API key in your dashboard: https://thirdweb.com/create-api-key`,
95
- errorCode: "SERVICE_ACTION_UNAUTHORIZED",
96
- status: 403
97
- };
98
- }
99
- }
100
-
101
- // validate service target addresses
102
- // the service has to pass in the target address for this to be validated
103
- if (authorizationPayload?.targetAddress) {
104
- const checkedAddresses = Array.isArray(authorizationPayload.targetAddress) ? authorizationPayload.targetAddress : [authorizationPayload.targetAddress];
105
- const allAllowed = service.targetAddresses.includes("*");
106
- if (!allAllowed && checkedAddresses.some(ta => !service.targetAddresses.includes(ta))) {
107
- return {
108
- authorized: false,
109
- errorMessage: `Invalid request: Unauthorized address: ${serviceConfig.serviceScope} ${checkedAddresses}. You can view the restrictions on this API key in your dashboard: https://thirdweb.com/create-api-key`,
110
- errorCode: "SERVICE_TARGET_ADDRESS_UNAUTHORIZED",
111
- status: 403
112
- };
113
- }
114
- }
115
- return {
116
- authorized: true,
117
- apiKeyMeta: apiKeyMetadata,
118
- accountMeta: {
119
- id: apiKeyMetadata.accountId,
120
- name: "",
121
- creatorWalletAddress: apiKeyMetadata.creatorWalletAddress,
122
- limits: apiKeyMetadata.limits,
123
- rateLimits: apiKeyMetadata.rateLimits,
124
- usage: apiKeyMetadata.usage
125
- }
126
- };
127
- }
128
-
129
- async function authorize(authData, serviceConfig, cacheOptions) {
130
- const {
131
- clientId,
132
- targetAddress,
133
- secretKeyHash,
134
- jwt,
135
- hashedJWT,
136
- useWalletAuth
137
- } = authData;
138
- const {
139
- enforceAuth
140
- } = serviceConfig;
141
-
142
- // BACKWARDS COMPAT: if auth not enforced and we don't have auth credentials bypass
143
- if (!enforceAuth && !clientId && !secretKeyHash) {
144
- return {
145
- authorized: true,
146
- apiKeyMeta: null,
147
- accountMeta: null
148
- };
149
- }
150
- // if we come in with a JWT then we only check the account is valid
151
- if (jwt && hashedJWT) {
152
- let accountMeta = null;
153
- if (cacheOptions) {
154
- try {
155
- const cachedAccountInfo = await cacheOptions.get(hashedJWT);
156
- if (cachedAccountInfo) {
157
- const parsed = JSON.parse(cachedAccountInfo);
158
- if ("updatedAt" in parsed) {
159
- // we want to compare the updatedAt time to the current time
160
- // if the difference is greater than the cacheTtl we want to ignore the cached data
161
- const now = Date.now();
162
- const diff = now - parsed.updatedAt;
163
- const cacheTtlMs = cacheOptions.cacheTtlSeconds * 1000;
164
- // only if the diff is less than the cacheTtl do we want to use the cached key
165
- if (diff < cacheTtlMs) {
166
- accountMeta = parsed.apiKeyMeta;
167
- }
168
- } else {
169
- accountMeta = parsed;
170
- }
171
- }
172
- } catch (err) {
173
- // ignore errors, proceed as if not in cache
174
- }
175
- }
176
- if (!accountMeta) {
177
- try {
178
- const {
179
- data,
180
- error
181
- } = await fetchAccountFromApi(jwt, serviceConfig, useWalletAuth?.toLowerCase() === "true");
182
- if (error) {
183
- return {
184
- authorized: false,
185
- errorCode: error.code,
186
- errorMessage: error.message,
187
- status: error.statusCode
188
- };
189
- }
190
- if (!data) {
191
- return {
192
- authorized: false,
193
- errorCode: "NO_ACCOUNT",
194
- errorMessage: "No error but also no account returned.",
195
- status: 500
196
- };
197
- }
198
- accountMeta = data;
199
- if (cacheOptions) {
200
- await cacheOptions.put(hashedJWT, accountMeta);
201
- }
202
- } catch (err) {
203
- console.warn("failed to fetch account from api", err);
204
- return {
205
- authorized: false,
206
- status: 500,
207
- errorMessage: "Failed to get account information.",
208
- errorCode: "FAILED_TO_LOAD_ACCOUNT"
209
- };
210
- }
211
- }
212
- // if we still don't have an accountMeta at this point we can't authorize
213
- if (!accountMeta) {
214
- return {
215
- authorized: false,
216
- status: 401,
217
- errorMessage: "Missing account information.",
218
- errorCode: "MISSING_ACCOUNT"
219
- };
220
- }
221
- // otherwise we want to return early with the accountMeta
222
- return {
223
- authorized: true,
224
- apiKeyMeta: null,
225
- accountMeta
226
- };
227
- }
228
-
229
- // if we don't have a client id at this point we can't authorize
230
- if (!clientId) {
231
- return {
232
- authorized: false,
233
- status: 401,
234
- errorMessage: "Missing clientId or secretKey.",
235
- errorCode: "MISSING_KEY"
236
- };
237
- }
238
- let apiKeyMeta = null;
239
- // if we have cache options we want to check the cache first
240
- if (cacheOptions) {
241
- try {
242
- const cachedKey = await cacheOptions.get(clientId);
243
- if (cachedKey) {
244
- const parsed = JSON.parse(cachedKey);
245
- if ("updatedAt" in parsed) {
246
- // we want to compare the updatedAt time to the current time
247
- // if the difference is greater than the cacheTtl we want to ignore the cached data
248
- const now = Date.now();
249
- const diff = now - parsed.updatedAt;
250
- const cacheTtlMs = cacheOptions.cacheTtlSeconds * 1000;
251
- // only if the diff is less than the cacheTtl do we want to use the cached key
252
- if (diff < cacheTtlMs) {
253
- apiKeyMeta = parsed.apiKeyMeta;
254
- }
255
- } else {
256
- apiKeyMeta = parsed;
257
- }
258
- }
259
- } catch (err) {
260
- // ignore errors, proceed as if not in cache
261
- }
262
- }
263
-
264
- // if we don't have a cached key, fetch from the API
265
- if (!apiKeyMeta) {
266
- try {
267
- const {
268
- data,
269
- error
270
- } = await fetchKeyMetadataFromApi(clientId, serviceConfig);
271
- if (error) {
272
- return {
273
- authorized: false,
274
- errorCode: error.code,
275
- errorMessage: error.message,
276
- status: error.statusCode
277
- };
278
- }
279
- if (!data) {
280
- return {
281
- authorized: false,
282
- errorCode: "NO_KEY",
283
- errorMessage: "No error but also no key returned.",
284
- status: 500
285
- };
286
- }
287
- // if we have a key for sure then assign it
288
- apiKeyMeta = data;
289
-
290
- // cache the retrieved key if we have cache options
291
- if (cacheOptions) {
292
- // we await this always because it can be a promise or not
293
- await cacheOptions.put(clientId, data);
294
- }
295
- } catch (err) {
296
- console.warn("failed to fetch key metadata from api", err);
297
- return {
298
- authorized: false,
299
- status: 500,
300
- errorMessage: "Failed to fetch key metadata. Please check your secret-key/clientId.",
301
- errorCode: "FAILED_TO_FETCH_KEY"
302
- };
303
- }
304
- }
305
- if (!apiKeyMeta) {
306
- return {
307
- authorized: false,
308
- status: 401,
309
- errorMessage: "Key is invalid. Please check your secret-key/clientId.",
310
- errorCode: "INVALID_KEY"
311
- };
312
- }
313
- // now we can validate the key itself
314
- const clientAuth = client.authorizeClient(authData, apiKeyMeta);
315
- if (!clientAuth.authorized) {
316
- return {
317
- errorCode: clientAuth.errorCode,
318
- authorized: false,
319
- status: 401,
320
- errorMessage: clientAuth.errorMessage
321
- };
322
- }
323
-
324
- // if we've made it this far we need to check service specific authorization
325
- const serviceAuth = authorizeService(apiKeyMeta, serviceConfig, {
326
- targetAddress
327
- });
328
- if (!serviceAuth.authorized) {
329
- return {
330
- errorCode: serviceAuth.errorCode,
331
- authorized: false,
332
- status: 403,
333
- errorMessage: serviceAuth.errorMessage
334
- };
335
- }
336
-
337
- // if we reach this point we are authorized!
338
- return {
339
- authorized: true,
340
- apiKeyMeta,
341
- accountMeta: {
342
- id: apiKeyMeta.accountId,
343
- // TODO update this later
344
- name: "",
345
- limits: apiKeyMeta.limits,
346
- rateLimits: apiKeyMeta.rateLimits,
347
- usage: apiKeyMeta.usage,
348
- creatorWalletAddress: apiKeyMeta.creatorWalletAddress
349
- }
350
- };
351
- }
352
-
353
- const usageEventSchema = zod.z.object({
354
- source: zod.z.enum(["ecosystemWallets", "embeddedWallets", "rpc", "storage", "bundler", "paymaster", "relayer", "connectWallet", "checkout", "engine", "pay", "rpcV2"]),
355
- action: zod.z.string(),
356
- /**
357
- * The following fields are optional.
358
- */
359
-
360
- accountId: zod.z.string().optional(),
361
- isClientEvent: zod.z.boolean().optional(),
362
- apiKeyId: zod.z.string().optional(),
363
- creatorWalletAddress: zod.z.string().optional(),
364
- clientId: zod.z.string().optional(),
365
- walletAddress: zod.z.string().optional(),
366
- walletType: zod.z.string().optional(),
367
- chainId: zod.z.number().int().positive().optional(),
368
- provider: zod.z.string().optional(),
369
- mimeType: zod.z.string().optional(),
370
- fileSize: zod.z.number().int().nonnegative().optional(),
371
- fileCid: zod.z.string().optional(),
372
- evmMethod: zod.z.string().optional(),
373
- userOpHash: zod.z.string().optional(),
374
- gasLimit: zod.z.number().nonnegative().optional(),
375
- gasPricePerUnit: zod.z.string().optional(),
376
- transactionFeeUsd: zod.z.number().optional(),
377
- transactionHash: zod.z.string().optional(),
378
- sdkName: zod.z.string().optional(),
379
- sdkVersion: zod.z.string().optional(),
380
- sdkPlatform: zod.z.string().optional(),
381
- sdkOS: zod.z.string().optional(),
382
- productName: zod.z.string().optional(),
383
- transactionValue: zod.z.string().optional(),
384
- pathname: zod.z.string().optional(),
385
- contractAddress: zod.z.string().optional(),
386
- errorCode: zod.z.string().optional(),
387
- httpStatusCode: zod.z.number().int().nonnegative().optional(),
388
- functionName: zod.z.string().optional(),
389
- extension: zod.z.string().optional(),
390
- retryCount: zod.z.number().int().nonnegative().optional(),
391
- policyId: zod.z.string().optional(),
392
- msSinceQueue: zod.z.number().nonnegative().optional(),
393
- msSinceSend: zod.z.number().nonnegative().optional(),
394
- msTotalDuration: zod.z.number().nonnegative().optional(),
395
- swapId: zod.z.string().optional(),
396
- tokenAddress: zod.z.string().optional(),
397
- amountWei: zod.z.string().optional(),
398
- amountUSDCents: zod.z.number().nonnegative().optional(),
399
- httpMethod: zod.z.enum(["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "CONNECT", "OPTIONS", "TRACE"]).optional(),
400
- // Used to identify the ecosystem that the an ecosystem wallet belongs too
401
- ecosystemId: zod.z.string().optional(),
402
- ecosystemPartnerId: zod.z.string().optional(),
403
- authenticationMethod: zod.z.string().optional(),
404
- chainName: zod.z.string().optional(),
405
- tokenSymbol: zod.z.string().optional(),
406
- dstChainId: zod.z.number().optional(),
407
- dstTokenAddress: zod.z.string().optional(),
408
- dstChainName: zod.z.string().optional(),
409
- dstTokenSymbol: zod.z.string().optional(),
410
- msLatency: zod.z.number().optional(),
411
- toAmountUSDCents: zod.z.number().optional(),
412
- secondaryProvider: zod.z.string().optional(),
413
- onRampId: zod.z.string().optional(),
414
- evmRequestParams: zod.z.string().optional(),
415
- providerIp: zod.z.string().optional()
416
- });
417
-
418
- const RATE_LIMIT_WINDOW_SECONDS = 10;
419
-
420
- // Redis interface compatible with ioredis (Node) and upstash (Cloudflare Workers).
421
-
422
- async function rateLimit(args) {
423
- const {
424
- authzResult,
425
- serviceConfig,
426
- redis,
427
- sampleRate = 1.0
428
- } = args;
429
- const shouldSampleRequest = Math.random() < sampleRate;
430
- if (!shouldSampleRequest || !authzResult.authorized) {
431
- return {
432
- rateLimited: false,
433
- requestCount: 0,
434
- rateLimit: 0
435
- };
436
- }
437
- const {
438
- apiKeyMeta,
439
- accountMeta
440
- } = authzResult;
441
- const accountId = apiKeyMeta?.accountId || accountMeta?.id;
442
- const {
443
- serviceScope
444
- } = serviceConfig;
445
- const limitPerSecond = apiKeyMeta?.rateLimits?.[serviceScope] ?? accountMeta?.rateLimits?.[serviceScope];
446
- if (!limitPerSecond) {
447
- // No rate limit is provided. Assume the request is not rate limited.
448
- return {
449
- rateLimited: false,
450
- requestCount: 0,
451
- rateLimit: 0
452
- };
453
- }
454
-
455
- // Gets the 10-second window for the current timestamp.
456
- const timestampWindow = Math.floor(Date.now() / (1000 * RATE_LIMIT_WINDOW_SECONDS)) * RATE_LIMIT_WINDOW_SECONDS;
457
- const key = `rate-limit:${serviceScope}:${accountId}:${timestampWindow}`;
458
-
459
- // Increment and get the current request count in this window.
460
- const requestCount = await redis.incr(key);
461
- if (requestCount === 1) {
462
- // For the first increment, set an expiration to clean up this key.
463
- await redis.expire(key, RATE_LIMIT_WINDOW_SECONDS);
464
- }
465
-
466
- // Get the limit for this window accounting for the sample rate.
467
- const limitPerWindow = limitPerSecond * sampleRate * RATE_LIMIT_WINDOW_SECONDS;
468
- if (requestCount > limitPerWindow) {
469
- // Report rate limit hits.
470
- if (apiKeyMeta?.id) {
471
- await updateRateLimitedAt(apiKeyMeta.id, serviceConfig);
472
- }
473
-
474
- // Reject requests when they've exceeded 2x the rate limit.
475
- if (requestCount > 2 * limitPerWindow) {
476
- return {
477
- rateLimited: true,
478
- requestCount,
479
- rateLimit: limitPerWindow,
480
- status: 429,
481
- errorMessage: `You've exceeded your ${serviceScope} rate limit at ${limitPerSecond} reqs/sec. To get higher rate limits, contact us at https://thirdweb.com/contact-us.`,
482
- errorCode: "RATE_LIMIT_EXCEEDED"
483
- };
484
- }
485
- }
486
- return {
487
- rateLimited: false,
488
- requestCount,
489
- rateLimit: limitPerWindow
490
- };
491
- }
492
-
493
- async function usageLimit(authzResult, serviceConfig) {
494
- if (!authzResult.authorized) {
495
- return {
496
- usageLimited: false
497
- };
498
- }
499
- const {
500
- apiKeyMeta,
501
- accountMeta
502
- } = authzResult;
503
- const {
504
- limits,
505
- usage
506
- } = apiKeyMeta || accountMeta || {};
507
- const {
508
- serviceScope
509
- } = serviceConfig;
510
- if (!usage || !(serviceScope in usage) || !limits || !(serviceScope in limits)) {
511
- // No usage limit is provided. Assume the request is not limited.
512
- return {
513
- usageLimited: false
514
- };
515
- }
516
- if (serviceScope === "storage" && (usage.storage?.sumFileSizeBytes ?? 0) > (limits.storage ?? 0)) {
517
- return {
518
- usageLimited: true,
519
- status: 403,
520
- errorMessage: `You've used all of your total usage credits for Storage Pinning. Please add your payment method at https://thirdweb.com/dashboard/settings/billing.`,
521
- errorCode: "PAYMENT_METHOD_REQUIRED"
522
- };
523
- }
524
- if (serviceScope === "embeddedWallets" && (usage.embeddedWallets?.countWalletAddresses ?? 0) > (limits.embeddedWallets ?? 0)) {
525
- return {
526
- usageLimited: true,
527
- status: 403,
528
- errorMessage: `You've used all of your total usage credits for Embedded Wallets. Please add your payment method at https://thirdweb.com/dashboard/settings/billing.`,
529
- errorCode: "PAYMENT_METHOD_REQUIRED"
530
- };
531
- }
532
- return {
533
- usageLimited: false
534
- };
535
- }
536
-
537
- exports.authorize = authorize;
538
- exports.rateLimit = rateLimit;
539
- exports.usageEventSchema = usageEventSchema;
540
- exports.usageLimit = usageLimit;
@@ -1,2 +0,0 @@
1
- export * from "./declarations/src/index";
2
- //# sourceMappingURL=thirdweb-dev-service-utils.cjs.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"thirdweb-dev-service-utils.cjs.d.ts","sourceRoot":"","sources":["./declarations/src/index.d.ts"],"names":[],"mappings":"AAAA"}
@@ -1,14 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var client = require('./client-a5cc5822.cjs.dev.js');
6
-
7
-
8
-
9
- exports.SERVICES = client.SERVICES;
10
- exports.SERVICE_DEFINITIONS = client.SERVICE_DEFINITIONS;
11
- exports.SERVICE_NAMES = client.SERVICE_NAMES;
12
- exports.authorizeBundleId = client.authorizeBundleId;
13
- exports.authorizeDomain = client.authorizeDomain;
14
- exports.getServiceByName = client.getServiceByName;
@@ -1,7 +0,0 @@
1
- 'use strict';
2
-
3
- if (process.env.NODE_ENV === "production") {
4
- module.exports = require("./thirdweb-dev-service-utils.cjs.prod.js");
5
- } else {
6
- module.exports = require("./thirdweb-dev-service-utils.cjs.dev.js");
7
- }
@@ -1,14 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var client = require('./client-84e46164.cjs.prod.js');
6
-
7
-
8
-
9
- exports.SERVICES = client.SERVICES;
10
- exports.SERVICE_DEFINITIONS = client.SERVICE_DEFINITIONS;
11
- exports.SERVICE_NAMES = client.SERVICE_NAMES;
12
- exports.authorizeBundleId = client.authorizeBundleId;
13
- exports.authorizeDomain = client.authorizeDomain;
14
- exports.getServiceByName = client.getServiceByName;
@@ -1 +0,0 @@
1
- export { d as SERVICES, S as SERVICE_DEFINITIONS, c as SERVICE_NAMES, a as authorizeBundleId, b as authorizeDomain, g as getServiceByName } from './client-8440b8fb.esm.js';
@@ -1,2 +0,0 @@
1
- export * from "../../dist/declarations/src/node/index";
2
- //# sourceMappingURL=thirdweb-dev-service-utils-node.cjs.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"thirdweb-dev-service-utils-node.cjs.d.ts","sourceRoot":"","sources":["../../dist/declarations/src/node/index.d.ts"],"names":[],"mappings":"AAAA"}