@thirdweb-dev/service-utils 0.0.0-dev-bb06abb-20230915121226 → 0.0.0-dev-79896a1-20230919190912
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.
- package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.cjs.dev.js +26 -22
- package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.cjs.prod.js +26 -22
- package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.esm.js +27 -23
- package/dist/declarations/src/cf-worker/index.d.ts +2 -2
- package/dist/declarations/src/cf-worker/index.d.ts.map +1 -1
- package/dist/declarations/src/core/api.d.ts +13 -12
- package/dist/declarations/src/core/api.d.ts.map +1 -1
- package/dist/declarations/src/core/authorize/client.d.ts.map +1 -1
- package/dist/declarations/src/core/authorize/index.d.ts.map +1 -1
- package/dist/declarations/src/core/authorize/service.d.ts.map +1 -1
- package/dist/declarations/src/core/rateLimit/index.d.ts +3 -2
- package/dist/declarations/src/core/rateLimit/index.d.ts.map +1 -1
- package/dist/declarations/src/core/usageLimit/index.d.ts +3 -2
- package/dist/declarations/src/core/usageLimit/index.d.ts.map +1 -1
- package/dist/declarations/src/node/index.d.ts.map +1 -1
- package/dist/{index-2a4bc395.cjs.prod.js → index-807f6a60.cjs.dev.js} +46 -18
- package/dist/{index-79984a5a.esm.js → index-bcf68113.esm.js} +46 -18
- package/dist/{index-8be280f0.cjs.dev.js → index-cfc8027b.cjs.prod.js} +46 -18
- package/node/dist/thirdweb-dev-service-utils-node.cjs.dev.js +26 -22
- package/node/dist/thirdweb-dev-service-utils-node.cjs.prod.js +26 -22
- package/node/dist/thirdweb-dev-service-utils-node.esm.js +27 -23
- package/package.json +1 -1
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
4
4
|
|
5
|
-
var index = require('../../dist/index-
|
5
|
+
var index = require('../../dist/index-807f6a60.cjs.dev.js');
|
6
6
|
var aws4fetch = require('aws4fetch');
|
7
7
|
var zod = require('zod');
|
8
8
|
var services = require('../../dist/services-a3f36057.cjs.dev.js');
|
@@ -120,8 +120,8 @@ async function authorizeWorker(authInput, serviceConfig) {
|
|
120
120
|
cacheTtlSeconds: serviceConfig.cacheTtlSeconds ?? DEFAULT_CACHE_TTL_SECONDS
|
121
121
|
});
|
122
122
|
}
|
123
|
-
async function rateLimitWorker(
|
124
|
-
return await index.rateLimit(
|
123
|
+
async function rateLimitWorker(authzResult, serviceConfig) {
|
124
|
+
return await index.rateLimit(authzResult, serviceConfig, {
|
125
125
|
get: async bucketId => serviceConfig.kvStore.get(bucketId),
|
126
126
|
put: (bucketId, count) => serviceConfig.kvStore.put(bucketId, count, {
|
127
127
|
expirationTtl: DEFAULT_RATE_LIMIT_CACHE_TTL_SECONDS
|
@@ -218,25 +218,29 @@ async function logHttpRequest(_ref) {
|
|
218
218
|
isAuthed,
|
219
219
|
statusMessage
|
220
220
|
} = _ref;
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
221
|
+
try {
|
222
|
+
const authorizationData = await extractAuthorizationData({
|
223
|
+
req,
|
224
|
+
clientId
|
225
|
+
});
|
226
|
+
const headers = req.headers;
|
227
|
+
console.log(JSON.stringify({
|
228
|
+
source,
|
229
|
+
pathname: req.url,
|
230
|
+
hasSecretKey: !!authorizationData.secretKey,
|
231
|
+
hasClientId: !!authorizationData.clientId,
|
232
|
+
hasJwt: !!authorizationData.jwt,
|
233
|
+
clientId: authorizationData.clientId,
|
234
|
+
isAuthed: !!isAuthed ?? null,
|
235
|
+
status: res.status,
|
236
|
+
sdkName: headers.get("x-sdk-name") ?? "unknown",
|
237
|
+
sdkVersion: headers.get("x-sdk-version") ?? "unknown",
|
238
|
+
platform: headers.get("x-sdk-platform") ?? "unknown"
|
239
|
+
}));
|
240
|
+
console.log(`statusMessage=${statusMessage ?? res.statusText}`);
|
241
|
+
} catch (err) {
|
242
|
+
console.error("Failed to log HTTP request:", err);
|
243
|
+
}
|
240
244
|
}
|
241
245
|
|
242
246
|
exports.rateLimit = index.rateLimit;
|
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
4
4
|
|
5
|
-
var index = require('../../dist/index-
|
5
|
+
var index = require('../../dist/index-cfc8027b.cjs.prod.js');
|
6
6
|
var aws4fetch = require('aws4fetch');
|
7
7
|
var zod = require('zod');
|
8
8
|
var services = require('../../dist/services-9e185105.cjs.prod.js');
|
@@ -120,8 +120,8 @@ async function authorizeWorker(authInput, serviceConfig) {
|
|
120
120
|
cacheTtlSeconds: serviceConfig.cacheTtlSeconds ?? DEFAULT_CACHE_TTL_SECONDS
|
121
121
|
});
|
122
122
|
}
|
123
|
-
async function rateLimitWorker(
|
124
|
-
return await index.rateLimit(
|
123
|
+
async function rateLimitWorker(authzResult, serviceConfig) {
|
124
|
+
return await index.rateLimit(authzResult, serviceConfig, {
|
125
125
|
get: async bucketId => serviceConfig.kvStore.get(bucketId),
|
126
126
|
put: (bucketId, count) => serviceConfig.kvStore.put(bucketId, count, {
|
127
127
|
expirationTtl: DEFAULT_RATE_LIMIT_CACHE_TTL_SECONDS
|
@@ -218,25 +218,29 @@ async function logHttpRequest(_ref) {
|
|
218
218
|
isAuthed,
|
219
219
|
statusMessage
|
220
220
|
} = _ref;
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
221
|
+
try {
|
222
|
+
const authorizationData = await extractAuthorizationData({
|
223
|
+
req,
|
224
|
+
clientId
|
225
|
+
});
|
226
|
+
const headers = req.headers;
|
227
|
+
console.log(JSON.stringify({
|
228
|
+
source,
|
229
|
+
pathname: req.url,
|
230
|
+
hasSecretKey: !!authorizationData.secretKey,
|
231
|
+
hasClientId: !!authorizationData.clientId,
|
232
|
+
hasJwt: !!authorizationData.jwt,
|
233
|
+
clientId: authorizationData.clientId,
|
234
|
+
isAuthed: !!isAuthed ?? null,
|
235
|
+
status: res.status,
|
236
|
+
sdkName: headers.get("x-sdk-name") ?? "unknown",
|
237
|
+
sdkVersion: headers.get("x-sdk-version") ?? "unknown",
|
238
|
+
platform: headers.get("x-sdk-platform") ?? "unknown"
|
239
|
+
}));
|
240
|
+
console.log(`statusMessage=${statusMessage ?? res.statusText}`);
|
241
|
+
} catch (err) {
|
242
|
+
console.error("Failed to log HTTP request:", err);
|
243
|
+
}
|
240
244
|
}
|
241
245
|
|
242
246
|
exports.rateLimit = index.rateLimit;
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import { a as authorize, r as rateLimit } from '../../dist/index-
|
2
|
-
export { r as rateLimit, u as usageLimit } from '../../dist/index-
|
1
|
+
import { a as authorize, r as rateLimit } from '../../dist/index-bcf68113.esm.js';
|
2
|
+
export { r as rateLimit, u as usageLimit } from '../../dist/index-bcf68113.esm.js';
|
3
3
|
import { AwsClient } from 'aws4fetch';
|
4
4
|
import { z } from 'zod';
|
5
5
|
export { b as SERVICES, S as SERVICE_DEFINITIONS, a as SERVICE_NAMES, g as getServiceByName } from '../../dist/services-86283509.esm.js';
|
@@ -117,8 +117,8 @@ async function authorizeWorker(authInput, serviceConfig) {
|
|
117
117
|
cacheTtlSeconds: serviceConfig.cacheTtlSeconds ?? DEFAULT_CACHE_TTL_SECONDS
|
118
118
|
});
|
119
119
|
}
|
120
|
-
async function rateLimitWorker(
|
121
|
-
return await rateLimit(
|
120
|
+
async function rateLimitWorker(authzResult, serviceConfig) {
|
121
|
+
return await rateLimit(authzResult, serviceConfig, {
|
122
122
|
get: async bucketId => serviceConfig.kvStore.get(bucketId),
|
123
123
|
put: (bucketId, count) => serviceConfig.kvStore.put(bucketId, count, {
|
124
124
|
expirationTtl: DEFAULT_RATE_LIMIT_CACHE_TTL_SECONDS
|
@@ -215,25 +215,29 @@ async function logHttpRequest(_ref) {
|
|
215
215
|
isAuthed,
|
216
216
|
statusMessage
|
217
217
|
} = _ref;
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
218
|
+
try {
|
219
|
+
const authorizationData = await extractAuthorizationData({
|
220
|
+
req,
|
221
|
+
clientId
|
222
|
+
});
|
223
|
+
const headers = req.headers;
|
224
|
+
console.log(JSON.stringify({
|
225
|
+
source,
|
226
|
+
pathname: req.url,
|
227
|
+
hasSecretKey: !!authorizationData.secretKey,
|
228
|
+
hasClientId: !!authorizationData.clientId,
|
229
|
+
hasJwt: !!authorizationData.jwt,
|
230
|
+
clientId: authorizationData.clientId,
|
231
|
+
isAuthed: !!isAuthed ?? null,
|
232
|
+
status: res.status,
|
233
|
+
sdkName: headers.get("x-sdk-name") ?? "unknown",
|
234
|
+
sdkVersion: headers.get("x-sdk-version") ?? "unknown",
|
235
|
+
platform: headers.get("x-sdk-platform") ?? "unknown"
|
236
|
+
}));
|
237
|
+
console.log(`statusMessage=${statusMessage ?? res.statusText}`);
|
238
|
+
} catch (err) {
|
239
|
+
console.error("Failed to log HTTP request:", err);
|
240
|
+
}
|
237
241
|
}
|
238
242
|
|
239
243
|
export { authorizeWorker, deriveClientIdFromSecretKeyHash, extractAuthorizationData, hashSecretKey, logHttpRequest, publishUsageEvents, rateLimitWorker };
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import type { ExecutionContext, KVNamespace, Response } from "@cloudflare/workers-types";
|
2
|
-
import type {
|
2
|
+
import type { CoreServiceConfig } from "../core/api";
|
3
3
|
import type { Request } from "@cloudflare/workers-types";
|
4
4
|
import type { AuthorizationInput } from "../core/authorize";
|
5
5
|
import type { AuthorizationResult } from "../core/authorize/types";
|
@@ -18,7 +18,7 @@ type AuthInput = CoreAuthInput & {
|
|
18
18
|
req: Request;
|
19
19
|
};
|
20
20
|
export declare function authorizeWorker(authInput: AuthInput, serviceConfig: WorkerServiceConfig): Promise<AuthorizationResult>;
|
21
|
-
export declare function rateLimitWorker(
|
21
|
+
export declare function rateLimitWorker(authzResult: AuthorizationResult, serviceConfig: WorkerServiceConfig): Promise<RateLimitResult>;
|
22
22
|
export declare function extractAuthorizationData(authInput: AuthInput): Promise<AuthorizationInput>;
|
23
23
|
export declare function hashSecretKey(secretKey: string): Promise<string>;
|
24
24
|
export declare function deriveClientIdFromSecretKeyHash(secretKeyHash: string): string;
|
@@ -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,
|
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,EAGV,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,SAAS,CAAC;AACxB,cAAc,kBAAkB,CAAC;AACjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AAEnC,MAAM,MAAM,mBAAmB,GAAG,iBAAiB,GAAG;IACpD,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,WAAW,EAAE,mBAAmB,EAChC,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,iBAwBA"}
|
@@ -7,6 +7,14 @@ export type CoreServiceConfig = {
|
|
7
7
|
serviceAction?: string;
|
8
8
|
useWalletAuth?: boolean;
|
9
9
|
};
|
10
|
+
type Usage = {
|
11
|
+
storage?: {
|
12
|
+
sumFileSizeBytes: number;
|
13
|
+
};
|
14
|
+
embeddedWallets?: {
|
15
|
+
countWalletAddresses: number;
|
16
|
+
};
|
17
|
+
};
|
10
18
|
export type ApiKeyMetadata = {
|
11
19
|
id: string;
|
12
20
|
key: string;
|
@@ -23,18 +31,7 @@ export type ApiKeyMetadata = {
|
|
23
31
|
targetAddresses: string[];
|
24
32
|
actions: string[];
|
25
33
|
}[];
|
26
|
-
usage?:
|
27
|
-
bundler?: {
|
28
|
-
chainId: number;
|
29
|
-
sumTransactionFee: number;
|
30
|
-
}[];
|
31
|
-
storage?: {
|
32
|
-
sumFileSizeBytes: number;
|
33
|
-
};
|
34
|
-
embeddedWallets?: {
|
35
|
-
countWalletAddresses: number;
|
36
|
-
};
|
37
|
-
};
|
34
|
+
usage?: Usage;
|
38
35
|
limits: Partial<Record<ServiceName, number>>;
|
39
36
|
rateLimits: Partial<Record<ServiceName, number>>;
|
40
37
|
};
|
@@ -42,6 +39,9 @@ export type AccountMetadata = {
|
|
42
39
|
id: string;
|
43
40
|
name: string;
|
44
41
|
creatorWalletAddress: string;
|
42
|
+
usage?: Usage;
|
43
|
+
limits: Partial<Record<ServiceName, number>>;
|
44
|
+
rateLimits: Partial<Record<ServiceName, number>>;
|
45
45
|
};
|
46
46
|
export type ApiResponse = {
|
47
47
|
data: ApiKeyMetadata | null;
|
@@ -62,4 +62,5 @@ export type ApiAccountResponse = {
|
|
62
62
|
export declare function fetchKeyMetadataFromApi(clientId: string, config: CoreServiceConfig): Promise<ApiResponse>;
|
63
63
|
export declare function fetchAccountFromApi(jwt: string, config: CoreServiceConfig, useWalletAuth: boolean): Promise<ApiAccountResponse>;
|
64
64
|
export declare function updateRateLimitedAt(apiKeyId: string, config: CoreServiceConfig): Promise<void>;
|
65
|
+
export {};
|
65
66
|
//# sourceMappingURL=api.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"api.d.ts","sourceRoot":"../../../../src/core","sources":["api.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;A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"file":"client.d.ts","sourceRoot":"../../../../../src/core/authorize","sources":["client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,QAAQ,CAAC;AAC7C,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAEnD,MAAM,MAAM,0BAA0B,GAAG;IACvC,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;CACvB,CAAC;AAEF,wBAAgB,eAAe,CAC7B,WAAW,EAAE,0BAA0B,EACvC,UAAU,EAAE,cAAc,GACzB,mBAAmB,CA2GrB"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"../../../../../src/core/authorize","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,cAAc,EACd,iBAAiB,EAGlB,MAAM,QAAQ,CAAC;AAGhB,OAAO,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAE9C,MAAM,MAAM,kBAAkB,GAAG;IAC/B,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IACnB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,aAAa,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAElC,aAAa,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAC/B,CAAC;AAEF,KAAK,YAAY,GAAG;IAClB,GAAG,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,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E,MAAM,CAAC;CACzB,CAAC;AAgBF,wBAAsB,SAAS,CAC7B,QAAQ,EAAE,kBAAkB,EAC5B,aAAa,EAAE,iBAAiB,EAChC,YAAY,CAAC,EAAE,YAAY,GAC1B,OAAO,CAAC,mBAAmB,CAAC,CAsN9B"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"service.d.ts","sourceRoot":"../../../../../src/core/authorize","sources":["service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,QAAQ,CAAC;AAC3D,OAAO,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAE9C,MAAM,MAAM,2BAA2B,GAAG;IAAE,aAAa,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;CAAE,CAAC;AAEhF,wBAAgB,gBAAgB,CAC9B,cAAc,EAAE,cAAc,EAC9B,aAAa,EAAE,iBAAiB,EAChC,oBAAoB,CAAC,EAAE,2BAA2B,GACjD,mBAAmB,
|
1
|
+
{"version":3,"file":"service.d.ts","sourceRoot":"../../../../../src/core/authorize","sources":["service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,QAAQ,CAAC;AAC3D,OAAO,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAE9C,MAAM,MAAM,2BAA2B,GAAG;IAAE,aAAa,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;CAAE,CAAC;AAEhF,wBAAgB,gBAAgB,CAC9B,cAAc,EAAE,cAAc,EAC9B,aAAa,EAAE,iBAAiB,EAChC,oBAAoB,CAAC,EAAE,2BAA2B,GACjD,mBAAmB,CAgErB"}
|
@@ -1,9 +1,10 @@
|
|
1
|
-
import {
|
1
|
+
import { CoreServiceConfig } from "../api";
|
2
|
+
import { AuthorizationResult } from "../authorize/types";
|
2
3
|
import { RateLimitResult } from "./types";
|
3
4
|
type CacheOptions = {
|
4
5
|
get: (bucketId: string) => Promise<string | null>;
|
5
6
|
put: (bucketId: string, count: string) => Promise<void> | void;
|
6
7
|
};
|
7
|
-
export declare function rateLimit(
|
8
|
+
export declare function rateLimit(authzResult: AuthorizationResult, serviceConfig: CoreServiceConfig, cacheOptions: CacheOptions): Promise<RateLimitResult>;
|
8
9
|
export {};
|
9
10
|
//# sourceMappingURL=index.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"../../../../../src/core/rateLimit","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"../../../../../src/core/rateLimit","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAuB,MAAM,QAAQ,CAAC;AAChE,OAAO,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAEzD,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAK1C,KAAK,YAAY,GAAG;IAClB,GAAG,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IAClD,GAAG,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;CAChE,CAAC;AAEF,wBAAsB,SAAS,CAC7B,WAAW,EAAE,mBAAmB,EAChC,aAAa,EAAE,iBAAiB,EAChC,YAAY,EAAE,YAAY,GACzB,OAAO,CAAC,eAAe,CAAC,CAuD1B"}
|
@@ -1,4 +1,5 @@
|
|
1
|
-
import {
|
1
|
+
import { CoreServiceConfig } from "../api";
|
2
|
+
import { AuthorizationResult } from "../authorize/types";
|
2
3
|
import { UsageLimitResult } from "./types";
|
3
|
-
export declare function usageLimit(
|
4
|
+
export declare function usageLimit(authzResult: AuthorizationResult, serviceConfig: CoreServiceConfig): Promise<UsageLimitResult>;
|
4
5
|
//# sourceMappingURL=index.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"../../../../../src/core/usageLimit","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"../../../../../src/core/usageLimit","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,QAAQ,CAAC;AAC3C,OAAO,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAEzD,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAE3C,wBAAsB,UAAU,CAC9B,WAAW,EAAE,mBAAmB,EAChC,aAAa,EAAE,iBAAiB,GAC/B,OAAO,CAAC,gBAAgB,CAAC,CAwC3B"}
|
@@ -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,CAAC;AAC3C,OAAO,KAAK,EAAuB,eAAe,EAAE,MAAM,WAAW,CAAC;AACtE,OAAO,KAAK,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,G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B,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,QA0BA"}
|
@@ -6,7 +6,7 @@ async function fetchKeyMetadataFromApi(clientId, config) {
|
|
6
6
|
serviceScope,
|
7
7
|
serviceApiKey
|
8
8
|
} = config;
|
9
|
-
const url = `${apiUrl}/v1/keys/use?clientId=${clientId}&scope=${serviceScope}`;
|
9
|
+
const url = `${apiUrl}/v1/keys/use?clientId=${clientId}&scope=${serviceScope}&includeUsage=true`;
|
10
10
|
const response = await fetch(url, {
|
11
11
|
method: "GET",
|
12
12
|
headers: {
|
@@ -27,7 +27,7 @@ async function fetchAccountFromApi(jwt, config, useWalletAuth) {
|
|
27
27
|
apiUrl,
|
28
28
|
serviceApiKey
|
29
29
|
} = config;
|
30
|
-
const url = useWalletAuth ? `${apiUrl}/v1/wallet/me` : `${apiUrl}/v1/account/me`;
|
30
|
+
const url = useWalletAuth ? `${apiUrl}/v1/wallet/me?includeUsage=true` : `${apiUrl}/v1/account/me?includeUsage=true`;
|
31
31
|
const response = await fetch(url, {
|
32
32
|
method: "GET",
|
33
33
|
headers: {
|
@@ -82,7 +82,10 @@ function authorizeClient(authOptions, apiKeyMeta) {
|
|
82
82
|
id: apiKeyMeta.accountId,
|
83
83
|
// TODO update this later
|
84
84
|
name: "",
|
85
|
-
creatorWalletAddress: apiKeyMeta.creatorWalletAddress
|
85
|
+
creatorWalletAddress: apiKeyMeta.creatorWalletAddress,
|
86
|
+
limits: apiKeyMeta.limits,
|
87
|
+
rateLimits: apiKeyMeta.rateLimits,
|
88
|
+
usage: apiKeyMeta.usage
|
86
89
|
}
|
87
90
|
};
|
88
91
|
|
@@ -212,12 +215,15 @@ function authorizeService(apiKeyMetadata, serviceConfig, authorizationPayload) {
|
|
212
215
|
}
|
213
216
|
return {
|
214
217
|
authorized: true,
|
218
|
+
apiKeyMeta: apiKeyMetadata,
|
215
219
|
accountMeta: {
|
216
220
|
id: apiKeyMetadata.accountId,
|
217
221
|
name: "",
|
218
|
-
creatorWalletAddress: apiKeyMetadata.creatorWalletAddress
|
219
|
-
|
220
|
-
|
222
|
+
creatorWalletAddress: apiKeyMetadata.creatorWalletAddress,
|
223
|
+
limits: apiKeyMetadata.limits,
|
224
|
+
rateLimits: apiKeyMetadata.rateLimits,
|
225
|
+
usage: apiKeyMetadata.usage
|
226
|
+
}
|
221
227
|
};
|
222
228
|
}
|
223
229
|
|
@@ -435,6 +441,9 @@ async function authorize(authData, serviceConfig, cacheOptions) {
|
|
435
441
|
id: apiKeyMeta.accountId,
|
436
442
|
// TODO update this later
|
437
443
|
name: "",
|
444
|
+
limits: apiKeyMeta.limits,
|
445
|
+
rateLimits: apiKeyMeta.rateLimits,
|
446
|
+
usage: apiKeyMeta.usage,
|
438
447
|
creatorWalletAddress: apiKeyMeta.creatorWalletAddress
|
439
448
|
}
|
440
449
|
};
|
@@ -443,22 +452,30 @@ async function authorize(authData, serviceConfig, cacheOptions) {
|
|
443
452
|
const DEFAULT_RATE_LIMIT_WINDOW_SECONDS = 10;
|
444
453
|
const HARD_LIMIT_MULTIPLE = 2; // 2x of allowed limit
|
445
454
|
|
446
|
-
async function rateLimit(
|
455
|
+
async function rateLimit(authzResult, serviceConfig, cacheOptions) {
|
456
|
+
if (!authzResult.authorized) {
|
457
|
+
return {
|
458
|
+
rateLimited: false
|
459
|
+
};
|
460
|
+
}
|
447
461
|
const {
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
462
|
+
apiKeyMeta,
|
463
|
+
accountMeta
|
464
|
+
} = authzResult;
|
465
|
+
const {
|
466
|
+
rateLimits
|
467
|
+
} = apiKeyMeta || accountMeta || {};
|
468
|
+
const accountId = apiKeyMeta?.accountId || accountMeta?.id;
|
452
469
|
const {
|
453
470
|
serviceScope
|
454
471
|
} = serviceConfig;
|
455
|
-
|
456
|
-
if (limit === undefined) {
|
472
|
+
if (!rateLimits || !(serviceScope in rateLimits)) {
|
457
473
|
// No rate limit is provided. Assume the request is not rate limited.
|
458
474
|
return {
|
459
475
|
rateLimited: false
|
460
476
|
};
|
461
477
|
}
|
478
|
+
const limit = rateLimits[serviceScope];
|
462
479
|
|
463
480
|
// Floors the current time to the nearest DEFAULT_RATE_LIMIT_WINDOW_SECONDS.
|
464
481
|
const bucketId = Math.floor(Date.now() / (1000 * DEFAULT_RATE_LIMIT_WINDOW_SECONDS)) * DEFAULT_RATE_LIMIT_WINDOW_SECONDS;
|
@@ -470,7 +487,9 @@ async function rateLimit(apiKeyMeta, serviceConfig, cacheOptions) {
|
|
470
487
|
const limitWindow = limit * DEFAULT_RATE_LIMIT_WINDOW_SECONDS;
|
471
488
|
if (current > limitWindow) {
|
472
489
|
// report rate limit hits
|
473
|
-
|
490
|
+
if (apiKeyMeta?.id) {
|
491
|
+
await updateRateLimitedAt(apiKeyMeta.id, serviceConfig);
|
492
|
+
}
|
474
493
|
|
475
494
|
// actually rate limit only when reached hard limit
|
476
495
|
if (current > limitWindow * HARD_LIMIT_MULTIPLE) {
|
@@ -489,21 +508,30 @@ async function rateLimit(apiKeyMeta, serviceConfig, cacheOptions) {
|
|
489
508
|
};
|
490
509
|
}
|
491
510
|
|
492
|
-
async function usageLimit(
|
511
|
+
async function usageLimit(authzResult, serviceConfig) {
|
512
|
+
if (!authzResult.authorized) {
|
513
|
+
return {
|
514
|
+
usageLimited: false
|
515
|
+
};
|
516
|
+
}
|
517
|
+
const {
|
518
|
+
apiKeyMeta,
|
519
|
+
accountMeta
|
520
|
+
} = authzResult;
|
493
521
|
const {
|
494
522
|
limits,
|
495
523
|
usage
|
496
|
-
} = apiKeyMeta;
|
524
|
+
} = apiKeyMeta || accountMeta || {};
|
497
525
|
const {
|
498
526
|
serviceScope
|
499
527
|
} = serviceConfig;
|
500
|
-
|
501
|
-
if (!usage || !(serviceScope in usage) || limit === undefined) {
|
528
|
+
if (!usage || !(serviceScope in usage) || !limits || !(serviceScope in limits)) {
|
502
529
|
// No usage limit is provided. Assume the request is not limited.
|
503
530
|
return {
|
504
531
|
usageLimited: false
|
505
532
|
};
|
506
533
|
}
|
534
|
+
const limit = limits.storage;
|
507
535
|
if (serviceScope === "storage" && (usage.storage?.sumFileSizeBytes || 0) > limit) {
|
508
536
|
return {
|
509
537
|
usageLimited: true,
|
@@ -4,7 +4,7 @@ async function fetchKeyMetadataFromApi(clientId, config) {
|
|
4
4
|
serviceScope,
|
5
5
|
serviceApiKey
|
6
6
|
} = config;
|
7
|
-
const url = `${apiUrl}/v1/keys/use?clientId=${clientId}&scope=${serviceScope}`;
|
7
|
+
const url = `${apiUrl}/v1/keys/use?clientId=${clientId}&scope=${serviceScope}&includeUsage=true`;
|
8
8
|
const response = await fetch(url, {
|
9
9
|
method: "GET",
|
10
10
|
headers: {
|
@@ -25,7 +25,7 @@ async function fetchAccountFromApi(jwt, config, useWalletAuth) {
|
|
25
25
|
apiUrl,
|
26
26
|
serviceApiKey
|
27
27
|
} = config;
|
28
|
-
const url = useWalletAuth ? `${apiUrl}/v1/wallet/me` : `${apiUrl}/v1/account/me`;
|
28
|
+
const url = useWalletAuth ? `${apiUrl}/v1/wallet/me?includeUsage=true` : `${apiUrl}/v1/account/me?includeUsage=true`;
|
29
29
|
const response = await fetch(url, {
|
30
30
|
method: "GET",
|
31
31
|
headers: {
|
@@ -80,7 +80,10 @@ function authorizeClient(authOptions, apiKeyMeta) {
|
|
80
80
|
id: apiKeyMeta.accountId,
|
81
81
|
// TODO update this later
|
82
82
|
name: "",
|
83
|
-
creatorWalletAddress: apiKeyMeta.creatorWalletAddress
|
83
|
+
creatorWalletAddress: apiKeyMeta.creatorWalletAddress,
|
84
|
+
limits: apiKeyMeta.limits,
|
85
|
+
rateLimits: apiKeyMeta.rateLimits,
|
86
|
+
usage: apiKeyMeta.usage
|
84
87
|
}
|
85
88
|
};
|
86
89
|
|
@@ -210,12 +213,15 @@ function authorizeService(apiKeyMetadata, serviceConfig, authorizationPayload) {
|
|
210
213
|
}
|
211
214
|
return {
|
212
215
|
authorized: true,
|
216
|
+
apiKeyMeta: apiKeyMetadata,
|
213
217
|
accountMeta: {
|
214
218
|
id: apiKeyMetadata.accountId,
|
215
219
|
name: "",
|
216
|
-
creatorWalletAddress: apiKeyMetadata.creatorWalletAddress
|
217
|
-
|
218
|
-
|
220
|
+
creatorWalletAddress: apiKeyMetadata.creatorWalletAddress,
|
221
|
+
limits: apiKeyMetadata.limits,
|
222
|
+
rateLimits: apiKeyMetadata.rateLimits,
|
223
|
+
usage: apiKeyMetadata.usage
|
224
|
+
}
|
219
225
|
};
|
220
226
|
}
|
221
227
|
|
@@ -433,6 +439,9 @@ async function authorize(authData, serviceConfig, cacheOptions) {
|
|
433
439
|
id: apiKeyMeta.accountId,
|
434
440
|
// TODO update this later
|
435
441
|
name: "",
|
442
|
+
limits: apiKeyMeta.limits,
|
443
|
+
rateLimits: apiKeyMeta.rateLimits,
|
444
|
+
usage: apiKeyMeta.usage,
|
436
445
|
creatorWalletAddress: apiKeyMeta.creatorWalletAddress
|
437
446
|
}
|
438
447
|
};
|
@@ -441,22 +450,30 @@ async function authorize(authData, serviceConfig, cacheOptions) {
|
|
441
450
|
const DEFAULT_RATE_LIMIT_WINDOW_SECONDS = 10;
|
442
451
|
const HARD_LIMIT_MULTIPLE = 2; // 2x of allowed limit
|
443
452
|
|
444
|
-
async function rateLimit(
|
453
|
+
async function rateLimit(authzResult, serviceConfig, cacheOptions) {
|
454
|
+
if (!authzResult.authorized) {
|
455
|
+
return {
|
456
|
+
rateLimited: false
|
457
|
+
};
|
458
|
+
}
|
445
459
|
const {
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
460
|
+
apiKeyMeta,
|
461
|
+
accountMeta
|
462
|
+
} = authzResult;
|
463
|
+
const {
|
464
|
+
rateLimits
|
465
|
+
} = apiKeyMeta || accountMeta || {};
|
466
|
+
const accountId = apiKeyMeta?.accountId || accountMeta?.id;
|
450
467
|
const {
|
451
468
|
serviceScope
|
452
469
|
} = serviceConfig;
|
453
|
-
|
454
|
-
if (limit === undefined) {
|
470
|
+
if (!rateLimits || !(serviceScope in rateLimits)) {
|
455
471
|
// No rate limit is provided. Assume the request is not rate limited.
|
456
472
|
return {
|
457
473
|
rateLimited: false
|
458
474
|
};
|
459
475
|
}
|
476
|
+
const limit = rateLimits[serviceScope];
|
460
477
|
|
461
478
|
// Floors the current time to the nearest DEFAULT_RATE_LIMIT_WINDOW_SECONDS.
|
462
479
|
const bucketId = Math.floor(Date.now() / (1000 * DEFAULT_RATE_LIMIT_WINDOW_SECONDS)) * DEFAULT_RATE_LIMIT_WINDOW_SECONDS;
|
@@ -468,7 +485,9 @@ async function rateLimit(apiKeyMeta, serviceConfig, cacheOptions) {
|
|
468
485
|
const limitWindow = limit * DEFAULT_RATE_LIMIT_WINDOW_SECONDS;
|
469
486
|
if (current > limitWindow) {
|
470
487
|
// report rate limit hits
|
471
|
-
|
488
|
+
if (apiKeyMeta?.id) {
|
489
|
+
await updateRateLimitedAt(apiKeyMeta.id, serviceConfig);
|
490
|
+
}
|
472
491
|
|
473
492
|
// actually rate limit only when reached hard limit
|
474
493
|
if (current > limitWindow * HARD_LIMIT_MULTIPLE) {
|
@@ -487,21 +506,30 @@ async function rateLimit(apiKeyMeta, serviceConfig, cacheOptions) {
|
|
487
506
|
};
|
488
507
|
}
|
489
508
|
|
490
|
-
async function usageLimit(
|
509
|
+
async function usageLimit(authzResult, serviceConfig) {
|
510
|
+
if (!authzResult.authorized) {
|
511
|
+
return {
|
512
|
+
usageLimited: false
|
513
|
+
};
|
514
|
+
}
|
515
|
+
const {
|
516
|
+
apiKeyMeta,
|
517
|
+
accountMeta
|
518
|
+
} = authzResult;
|
491
519
|
const {
|
492
520
|
limits,
|
493
521
|
usage
|
494
|
-
} = apiKeyMeta;
|
522
|
+
} = apiKeyMeta || accountMeta || {};
|
495
523
|
const {
|
496
524
|
serviceScope
|
497
525
|
} = serviceConfig;
|
498
|
-
|
499
|
-
if (!usage || !(serviceScope in usage) || limit === undefined) {
|
526
|
+
if (!usage || !(serviceScope in usage) || !limits || !(serviceScope in limits)) {
|
500
527
|
// No usage limit is provided. Assume the request is not limited.
|
501
528
|
return {
|
502
529
|
usageLimited: false
|
503
530
|
};
|
504
531
|
}
|
532
|
+
const limit = limits.storage;
|
505
533
|
if (serviceScope === "storage" && (usage.storage?.sumFileSizeBytes || 0) > limit) {
|
506
534
|
return {
|
507
535
|
usageLimited: true,
|
@@ -6,7 +6,7 @@ async function fetchKeyMetadataFromApi(clientId, config) {
|
|
6
6
|
serviceScope,
|
7
7
|
serviceApiKey
|
8
8
|
} = config;
|
9
|
-
const url = `${apiUrl}/v1/keys/use?clientId=${clientId}&scope=${serviceScope}`;
|
9
|
+
const url = `${apiUrl}/v1/keys/use?clientId=${clientId}&scope=${serviceScope}&includeUsage=true`;
|
10
10
|
const response = await fetch(url, {
|
11
11
|
method: "GET",
|
12
12
|
headers: {
|
@@ -27,7 +27,7 @@ async function fetchAccountFromApi(jwt, config, useWalletAuth) {
|
|
27
27
|
apiUrl,
|
28
28
|
serviceApiKey
|
29
29
|
} = config;
|
30
|
-
const url = useWalletAuth ? `${apiUrl}/v1/wallet/me` : `${apiUrl}/v1/account/me`;
|
30
|
+
const url = useWalletAuth ? `${apiUrl}/v1/wallet/me?includeUsage=true` : `${apiUrl}/v1/account/me?includeUsage=true`;
|
31
31
|
const response = await fetch(url, {
|
32
32
|
method: "GET",
|
33
33
|
headers: {
|
@@ -82,7 +82,10 @@ function authorizeClient(authOptions, apiKeyMeta) {
|
|
82
82
|
id: apiKeyMeta.accountId,
|
83
83
|
// TODO update this later
|
84
84
|
name: "",
|
85
|
-
creatorWalletAddress: apiKeyMeta.creatorWalletAddress
|
85
|
+
creatorWalletAddress: apiKeyMeta.creatorWalletAddress,
|
86
|
+
limits: apiKeyMeta.limits,
|
87
|
+
rateLimits: apiKeyMeta.rateLimits,
|
88
|
+
usage: apiKeyMeta.usage
|
86
89
|
}
|
87
90
|
};
|
88
91
|
|
@@ -212,12 +215,15 @@ function authorizeService(apiKeyMetadata, serviceConfig, authorizationPayload) {
|
|
212
215
|
}
|
213
216
|
return {
|
214
217
|
authorized: true,
|
218
|
+
apiKeyMeta: apiKeyMetadata,
|
215
219
|
accountMeta: {
|
216
220
|
id: apiKeyMetadata.accountId,
|
217
221
|
name: "",
|
218
|
-
creatorWalletAddress: apiKeyMetadata.creatorWalletAddress
|
219
|
-
|
220
|
-
|
222
|
+
creatorWalletAddress: apiKeyMetadata.creatorWalletAddress,
|
223
|
+
limits: apiKeyMetadata.limits,
|
224
|
+
rateLimits: apiKeyMetadata.rateLimits,
|
225
|
+
usage: apiKeyMetadata.usage
|
226
|
+
}
|
221
227
|
};
|
222
228
|
}
|
223
229
|
|
@@ -435,6 +441,9 @@ async function authorize(authData, serviceConfig, cacheOptions) {
|
|
435
441
|
id: apiKeyMeta.accountId,
|
436
442
|
// TODO update this later
|
437
443
|
name: "",
|
444
|
+
limits: apiKeyMeta.limits,
|
445
|
+
rateLimits: apiKeyMeta.rateLimits,
|
446
|
+
usage: apiKeyMeta.usage,
|
438
447
|
creatorWalletAddress: apiKeyMeta.creatorWalletAddress
|
439
448
|
}
|
440
449
|
};
|
@@ -443,22 +452,30 @@ async function authorize(authData, serviceConfig, cacheOptions) {
|
|
443
452
|
const DEFAULT_RATE_LIMIT_WINDOW_SECONDS = 10;
|
444
453
|
const HARD_LIMIT_MULTIPLE = 2; // 2x of allowed limit
|
445
454
|
|
446
|
-
async function rateLimit(
|
455
|
+
async function rateLimit(authzResult, serviceConfig, cacheOptions) {
|
456
|
+
if (!authzResult.authorized) {
|
457
|
+
return {
|
458
|
+
rateLimited: false
|
459
|
+
};
|
460
|
+
}
|
447
461
|
const {
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
462
|
+
apiKeyMeta,
|
463
|
+
accountMeta
|
464
|
+
} = authzResult;
|
465
|
+
const {
|
466
|
+
rateLimits
|
467
|
+
} = apiKeyMeta || accountMeta || {};
|
468
|
+
const accountId = apiKeyMeta?.accountId || accountMeta?.id;
|
452
469
|
const {
|
453
470
|
serviceScope
|
454
471
|
} = serviceConfig;
|
455
|
-
|
456
|
-
if (limit === undefined) {
|
472
|
+
if (!rateLimits || !(serviceScope in rateLimits)) {
|
457
473
|
// No rate limit is provided. Assume the request is not rate limited.
|
458
474
|
return {
|
459
475
|
rateLimited: false
|
460
476
|
};
|
461
477
|
}
|
478
|
+
const limit = rateLimits[serviceScope];
|
462
479
|
|
463
480
|
// Floors the current time to the nearest DEFAULT_RATE_LIMIT_WINDOW_SECONDS.
|
464
481
|
const bucketId = Math.floor(Date.now() / (1000 * DEFAULT_RATE_LIMIT_WINDOW_SECONDS)) * DEFAULT_RATE_LIMIT_WINDOW_SECONDS;
|
@@ -470,7 +487,9 @@ async function rateLimit(apiKeyMeta, serviceConfig, cacheOptions) {
|
|
470
487
|
const limitWindow = limit * DEFAULT_RATE_LIMIT_WINDOW_SECONDS;
|
471
488
|
if (current > limitWindow) {
|
472
489
|
// report rate limit hits
|
473
|
-
|
490
|
+
if (apiKeyMeta?.id) {
|
491
|
+
await updateRateLimitedAt(apiKeyMeta.id, serviceConfig);
|
492
|
+
}
|
474
493
|
|
475
494
|
// actually rate limit only when reached hard limit
|
476
495
|
if (current > limitWindow * HARD_LIMIT_MULTIPLE) {
|
@@ -489,21 +508,30 @@ async function rateLimit(apiKeyMeta, serviceConfig, cacheOptions) {
|
|
489
508
|
};
|
490
509
|
}
|
491
510
|
|
492
|
-
async function usageLimit(
|
511
|
+
async function usageLimit(authzResult, serviceConfig) {
|
512
|
+
if (!authzResult.authorized) {
|
513
|
+
return {
|
514
|
+
usageLimited: false
|
515
|
+
};
|
516
|
+
}
|
517
|
+
const {
|
518
|
+
apiKeyMeta,
|
519
|
+
accountMeta
|
520
|
+
} = authzResult;
|
493
521
|
const {
|
494
522
|
limits,
|
495
523
|
usage
|
496
|
-
} = apiKeyMeta;
|
524
|
+
} = apiKeyMeta || accountMeta || {};
|
497
525
|
const {
|
498
526
|
serviceScope
|
499
527
|
} = serviceConfig;
|
500
|
-
|
501
|
-
if (!usage || !(serviceScope in usage) || limit === undefined) {
|
528
|
+
if (!usage || !(serviceScope in usage) || !limits || !(serviceScope in limits)) {
|
502
529
|
// No usage limit is provided. Assume the request is not limited.
|
503
530
|
return {
|
504
531
|
usageLimited: false
|
505
532
|
};
|
506
533
|
}
|
534
|
+
const limit = limits.storage;
|
507
535
|
if (serviceScope === "storage" && (usage.storage?.sumFileSizeBytes || 0) > limit) {
|
508
536
|
return {
|
509
537
|
usageLimited: true,
|
@@ -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-
|
6
|
+
var index = require('../../dist/index-807f6a60.cjs.dev.js');
|
7
7
|
var services = require('../../dist/services-a3f36057.cjs.dev.js');
|
8
8
|
|
9
9
|
/**
|
@@ -150,27 +150,31 @@ function logHttpRequest(_ref) {
|
|
150
150
|
isAuthed,
|
151
151
|
statusMessage
|
152
152
|
} = _ref;
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
153
|
+
try {
|
154
|
+
const authorizationData = extractAuthorizationData({
|
155
|
+
req,
|
156
|
+
clientId
|
157
|
+
});
|
158
|
+
const headers = req.headers;
|
159
|
+
const _statusMessage = statusMessage ?? res.statusMessage;
|
160
|
+
console.log(JSON.stringify({
|
161
|
+
source,
|
162
|
+
pathname: req.url,
|
163
|
+
hasSecretKey: !!authorizationData.secretKey,
|
164
|
+
hasClientId: !!authorizationData.clientId,
|
165
|
+
hasJwt: !!authorizationData.jwt,
|
166
|
+
clientId: authorizationData.clientId,
|
167
|
+
isAuthed: !!isAuthed ?? null,
|
168
|
+
status: res.statusCode,
|
169
|
+
statusMessage: _statusMessage,
|
170
|
+
sdkName: headers["x-sdk-name"] ?? "unknown",
|
171
|
+
sdkVersion: headers["x-sdk-version"] ?? "unknown",
|
172
|
+
platform: headers["x-sdk-platform"] ?? "unknown"
|
173
|
+
}));
|
174
|
+
console.log(`statusMessage=${_statusMessage}`);
|
175
|
+
} catch (err) {
|
176
|
+
console.error("Failed to log HTTP request:", err);
|
177
|
+
}
|
174
178
|
}
|
175
179
|
|
176
180
|
exports.rateLimit = index.rateLimit;
|
@@ -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-
|
6
|
+
var index = require('../../dist/index-cfc8027b.cjs.prod.js');
|
7
7
|
var services = require('../../dist/services-9e185105.cjs.prod.js');
|
8
8
|
|
9
9
|
/**
|
@@ -150,27 +150,31 @@ function logHttpRequest(_ref) {
|
|
150
150
|
isAuthed,
|
151
151
|
statusMessage
|
152
152
|
} = _ref;
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
153
|
+
try {
|
154
|
+
const authorizationData = extractAuthorizationData({
|
155
|
+
req,
|
156
|
+
clientId
|
157
|
+
});
|
158
|
+
const headers = req.headers;
|
159
|
+
const _statusMessage = statusMessage ?? res.statusMessage;
|
160
|
+
console.log(JSON.stringify({
|
161
|
+
source,
|
162
|
+
pathname: req.url,
|
163
|
+
hasSecretKey: !!authorizationData.secretKey,
|
164
|
+
hasClientId: !!authorizationData.clientId,
|
165
|
+
hasJwt: !!authorizationData.jwt,
|
166
|
+
clientId: authorizationData.clientId,
|
167
|
+
isAuthed: !!isAuthed ?? null,
|
168
|
+
status: res.statusCode,
|
169
|
+
statusMessage: _statusMessage,
|
170
|
+
sdkName: headers["x-sdk-name"] ?? "unknown",
|
171
|
+
sdkVersion: headers["x-sdk-version"] ?? "unknown",
|
172
|
+
platform: headers["x-sdk-platform"] ?? "unknown"
|
173
|
+
}));
|
174
|
+
console.log(`statusMessage=${_statusMessage}`);
|
175
|
+
} catch (err) {
|
176
|
+
console.error("Failed to log HTTP request:", err);
|
177
|
+
}
|
174
178
|
}
|
175
179
|
|
176
180
|
exports.rateLimit = index.rateLimit;
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { createHash } from 'node:crypto';
|
2
|
-
import { a as authorize } from '../../dist/index-
|
3
|
-
export { r as rateLimit, u as usageLimit } from '../../dist/index-
|
2
|
+
import { a as authorize } from '../../dist/index-bcf68113.esm.js';
|
3
|
+
export { r as rateLimit, u as usageLimit } from '../../dist/index-bcf68113.esm.js';
|
4
4
|
export { b as SERVICES, S as SERVICE_DEFINITIONS, a as SERVICE_NAMES, g as getServiceByName } from '../../dist/services-86283509.esm.js';
|
5
5
|
|
6
6
|
/**
|
@@ -147,27 +147,31 @@ function logHttpRequest(_ref) {
|
|
147
147
|
isAuthed,
|
148
148
|
statusMessage
|
149
149
|
} = _ref;
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
150
|
+
try {
|
151
|
+
const authorizationData = extractAuthorizationData({
|
152
|
+
req,
|
153
|
+
clientId
|
154
|
+
});
|
155
|
+
const headers = req.headers;
|
156
|
+
const _statusMessage = statusMessage ?? res.statusMessage;
|
157
|
+
console.log(JSON.stringify({
|
158
|
+
source,
|
159
|
+
pathname: req.url,
|
160
|
+
hasSecretKey: !!authorizationData.secretKey,
|
161
|
+
hasClientId: !!authorizationData.clientId,
|
162
|
+
hasJwt: !!authorizationData.jwt,
|
163
|
+
clientId: authorizationData.clientId,
|
164
|
+
isAuthed: !!isAuthed ?? null,
|
165
|
+
status: res.statusCode,
|
166
|
+
statusMessage: _statusMessage,
|
167
|
+
sdkName: headers["x-sdk-name"] ?? "unknown",
|
168
|
+
sdkVersion: headers["x-sdk-version"] ?? "unknown",
|
169
|
+
platform: headers["x-sdk-platform"] ?? "unknown"
|
170
|
+
}));
|
171
|
+
console.log(`statusMessage=${_statusMessage}`);
|
172
|
+
} catch (err) {
|
173
|
+
console.error("Failed to log HTTP request:", err);
|
174
|
+
}
|
171
175
|
}
|
172
176
|
|
173
177
|
export { authorizeNode, deriveClientIdFromSecretKeyHash, extractAuthorizationData, hashSecretKey, logHttpRequest };
|
package/package.json
CHANGED