@nehorai/credits 1.0.0
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/LICENSE +21 -0
- package/dist/adapters/generic.d.ts +18 -0
- package/dist/adapters/generic.d.ts.map +1 -0
- package/dist/adapters/generic.js +147 -0
- package/dist/adapters/generic.js.map +1 -0
- package/dist/adapters/index.d.ts +3 -0
- package/dist/adapters/index.d.ts.map +1 -0
- package/dist/adapters/index.js +2 -0
- package/dist/adapters/index.js.map +1 -0
- package/dist/adapters/types.d.ts +45 -0
- package/dist/adapters/types.d.ts.map +1 -0
- package/dist/adapters/types.js +8 -0
- package/dist/adapters/types.js.map +1 -0
- package/dist/auth/api-key.d.ts +37 -0
- package/dist/auth/api-key.d.ts.map +1 -0
- package/dist/auth/api-key.js +94 -0
- package/dist/auth/api-key.js.map +1 -0
- package/dist/auth/index.d.ts +8 -0
- package/dist/auth/index.d.ts.map +1 -0
- package/dist/auth/index.js +8 -0
- package/dist/auth/index.js.map +1 -0
- package/dist/auth/types.d.ts +31 -0
- package/dist/auth/types.d.ts.map +1 -0
- package/dist/auth/types.js +2 -0
- package/dist/auth/types.js.map +1 -0
- package/dist/config/costs.d.ts +61 -0
- package/dist/config/costs.d.ts.map +1 -0
- package/dist/config/costs.js +86 -0
- package/dist/config/costs.js.map +1 -0
- package/dist/config/index.d.ts +165 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +286 -0
- package/dist/config/index.js.map +1 -0
- package/dist/core/deferred.d.ts +65 -0
- package/dist/core/deferred.d.ts.map +1 -0
- package/dist/core/deferred.js +72 -0
- package/dist/core/deferred.js.map +1 -0
- package/dist/core/errors.d.ts +78 -0
- package/dist/core/errors.d.ts.map +1 -0
- package/dist/core/errors.js +110 -0
- package/dist/core/errors.js.map +1 -0
- package/dist/core/index.d.ts +29 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +28 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/operations.d.ts +36 -0
- package/dist/core/operations.d.ts.map +1 -0
- package/dist/core/operations.js +87 -0
- package/dist/core/operations.js.map +1 -0
- package/dist/core/types.d.ts +287 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +93 -0
- package/dist/core/types.js.map +1 -0
- package/dist/index.d.ts +57 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +63 -0
- package/dist/index.js.map +1 -0
- package/dist/notifications/index.d.ts +76 -0
- package/dist/notifications/index.d.ts.map +1 -0
- package/dist/notifications/index.js +182 -0
- package/dist/notifications/index.js.map +1 -0
- package/dist/repository/index.d.ts +10 -0
- package/dist/repository/index.d.ts.map +1 -0
- package/dist/repository/index.js +11 -0
- package/dist/repository/index.js.map +1 -0
- package/dist/repository/memory/index.d.ts +70 -0
- package/dist/repository/memory/index.d.ts.map +1 -0
- package/dist/repository/memory/index.js +518 -0
- package/dist/repository/memory/index.js.map +1 -0
- package/dist/repository/types.d.ts +283 -0
- package/dist/repository/types.d.ts.map +1 -0
- package/dist/repository/types.js +39 -0
- package/dist/repository/types.js.map +1 -0
- package/dist/repository/utils.d.ts +22 -0
- package/dist/repository/utils.d.ts.map +1 -0
- package/dist/repository/utils.js +40 -0
- package/dist/repository/utils.js.map +1 -0
- package/dist/sdk/admin-client.d.ts +146 -0
- package/dist/sdk/admin-client.d.ts.map +1 -0
- package/dist/sdk/admin-client.js +196 -0
- package/dist/sdk/admin-client.js.map +1 -0
- package/dist/sdk/client.d.ts +144 -0
- package/dist/sdk/client.d.ts.map +1 -0
- package/dist/sdk/client.js +247 -0
- package/dist/sdk/client.js.map +1 -0
- package/dist/sdk/errors.d.ts +91 -0
- package/dist/sdk/errors.d.ts.map +1 -0
- package/dist/sdk/errors.js +185 -0
- package/dist/sdk/errors.js.map +1 -0
- package/dist/sdk/index.d.ts +13 -0
- package/dist/sdk/index.d.ts.map +1 -0
- package/dist/sdk/index.js +13 -0
- package/dist/sdk/index.js.map +1 -0
- package/dist/sdk/types.d.ts +110 -0
- package/dist/sdk/types.d.ts.map +1 -0
- package/dist/sdk/types.js +20 -0
- package/dist/sdk/types.js.map +1 -0
- package/dist/service/credits-service.d.ts +139 -0
- package/dist/service/credits-service.d.ts.map +1 -0
- package/dist/service/credits-service.js +372 -0
- package/dist/service/credits-service.js.map +1 -0
- package/dist/service/index.d.ts +3 -0
- package/dist/service/index.d.ts.map +1 -0
- package/dist/service/index.js +2 -0
- package/dist/service/index.js.map +1 -0
- package/dist/utils/error-utils.d.ts +48 -0
- package/dist/utils/error-utils.d.ts.map +1 -0
- package/dist/utils/error-utils.js +57 -0
- package/dist/utils/error-utils.js.map +1 -0
- package/dist/utils/index.d.ts +54 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +72 -0
- package/dist/utils/index.js.map +1 -0
- package/package.json +69 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Nehorai Hadad
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic credits adapter - framework agnostic
|
|
3
|
+
*
|
|
4
|
+
* This adapter uses standard JavaScript APIs and works in any
|
|
5
|
+
* JavaScript environment (Node.js, browser, etc.).
|
|
6
|
+
*/
|
|
7
|
+
import type { CreditsAdapter, CreditsAdapterConfig } from "./types";
|
|
8
|
+
/**
|
|
9
|
+
* Create a generic credits adapter
|
|
10
|
+
*
|
|
11
|
+
* This adapter:
|
|
12
|
+
* - Authenticates users via the provided auth provider
|
|
13
|
+
* - Reserves credits before action execution
|
|
14
|
+
* - Commits credits on success, releases on failure
|
|
15
|
+
* - Logs usage asynchronously via the deferred executor
|
|
16
|
+
*/
|
|
17
|
+
export declare function createGenericAdapter(config: CreditsAdapterConfig): CreditsAdapter;
|
|
18
|
+
//# sourceMappingURL=generic.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generic.d.ts","sourceRoot":"","sources":["../../src/adapters/generic.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAgB,cAAc,EAAE,oBAAoB,EAAuB,MAAM,SAAS,CAAC;AAgBvG;;;;;;;;GAQG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,oBAAoB,GAAG,cAAc,CAgIjF"}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic credits adapter - framework agnostic
|
|
3
|
+
*
|
|
4
|
+
* This adapter uses standard JavaScript APIs and works in any
|
|
5
|
+
* JavaScript environment (Node.js, browser, etc.).
|
|
6
|
+
*/
|
|
7
|
+
import { commitReservationWithJournal, releaseReservationWithJournal, reserveCreditsForOperation, } from "../core/operations";
|
|
8
|
+
/**
|
|
9
|
+
* Generate a unique request ID
|
|
10
|
+
*/
|
|
11
|
+
function generateRequestId() {
|
|
12
|
+
const timestamp = Date.now();
|
|
13
|
+
const randomPart = Math.random().toString(36).substring(2, 14);
|
|
14
|
+
return `req_${timestamp}_${randomPart}`;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Create a generic credits adapter
|
|
18
|
+
*
|
|
19
|
+
* This adapter:
|
|
20
|
+
* - Authenticates users via the provided auth provider
|
|
21
|
+
* - Reserves credits before action execution
|
|
22
|
+
* - Commits credits on success, releases on failure
|
|
23
|
+
* - Logs usage asynchronously via the deferred executor
|
|
24
|
+
*/
|
|
25
|
+
export function createGenericAdapter(config) {
|
|
26
|
+
const { repository, authProvider, deferred, operationCosts } = config;
|
|
27
|
+
function getOperationCost(operationType) {
|
|
28
|
+
return operationCosts[operationType] ?? 0;
|
|
29
|
+
}
|
|
30
|
+
function logUsage(params) {
|
|
31
|
+
deferred.defer(async () => {
|
|
32
|
+
await repository.logUsage({
|
|
33
|
+
userId: params.userId,
|
|
34
|
+
operationType: params.operationType,
|
|
35
|
+
provider: "gemini",
|
|
36
|
+
creditsUsed: params.creditsUsed,
|
|
37
|
+
success: params.success,
|
|
38
|
+
errorMessage: params.errorMessage,
|
|
39
|
+
resourceId: params.resourceId,
|
|
40
|
+
resourceType: params.resourceType,
|
|
41
|
+
requestId: params.requestId,
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
return {
|
|
46
|
+
withCredits(options, handler) {
|
|
47
|
+
return async (data) => {
|
|
48
|
+
const requestId = generateRequestId();
|
|
49
|
+
// Authenticate
|
|
50
|
+
const user = await authProvider.getCurrentUser();
|
|
51
|
+
if (!user?.id) {
|
|
52
|
+
return { success: false, error: "Authentication required" };
|
|
53
|
+
}
|
|
54
|
+
// Handle preview mode - skip credit handling entirely
|
|
55
|
+
if (isPreviewMode(data)) {
|
|
56
|
+
const dummyReservation = createDummyReservation(user.id, options.operationType);
|
|
57
|
+
return handler(user, data, dummyReservation);
|
|
58
|
+
}
|
|
59
|
+
// Calculate cost
|
|
60
|
+
const cost = options.customCost ?? getOperationCost(options.operationType);
|
|
61
|
+
// Reserve credits
|
|
62
|
+
let reservation;
|
|
63
|
+
try {
|
|
64
|
+
reservation = await reserveCreditsForOperation(repository, user.id, cost, options.operationType);
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
const message = error instanceof Error ? error.message : "Failed to reserve credits";
|
|
68
|
+
logUsage({
|
|
69
|
+
userId: user.id,
|
|
70
|
+
operationType: options.operationType,
|
|
71
|
+
creditsUsed: 0,
|
|
72
|
+
success: false,
|
|
73
|
+
errorMessage: message,
|
|
74
|
+
resourceId: options.resourceId,
|
|
75
|
+
resourceType: options.resourceType,
|
|
76
|
+
requestId,
|
|
77
|
+
});
|
|
78
|
+
return { success: false, error: message };
|
|
79
|
+
}
|
|
80
|
+
// Execute action
|
|
81
|
+
try {
|
|
82
|
+
const result = await handler(user, data, reservation);
|
|
83
|
+
if (result.success) {
|
|
84
|
+
await commitReservationWithJournal(repository, user.id, reservation.id);
|
|
85
|
+
logUsage({
|
|
86
|
+
userId: user.id,
|
|
87
|
+
operationType: options.operationType,
|
|
88
|
+
creditsUsed: cost,
|
|
89
|
+
success: true,
|
|
90
|
+
resourceId: options.resourceId,
|
|
91
|
+
resourceType: options.resourceType,
|
|
92
|
+
requestId,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
await releaseReservationWithJournal(repository, user.id, reservation.id);
|
|
97
|
+
logUsage({
|
|
98
|
+
userId: user.id,
|
|
99
|
+
operationType: options.operationType,
|
|
100
|
+
creditsUsed: 0,
|
|
101
|
+
success: false,
|
|
102
|
+
errorMessage: result.error,
|
|
103
|
+
resourceId: options.resourceId,
|
|
104
|
+
resourceType: options.resourceType,
|
|
105
|
+
requestId,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
return result;
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
const message = error instanceof Error ? error.message : "An unexpected error occurred";
|
|
112
|
+
await releaseReservationWithJournal(repository, user.id, reservation.id);
|
|
113
|
+
logUsage({
|
|
114
|
+
userId: user.id,
|
|
115
|
+
operationType: options.operationType,
|
|
116
|
+
creditsUsed: 0,
|
|
117
|
+
success: false,
|
|
118
|
+
errorMessage: message,
|
|
119
|
+
resourceId: options.resourceId,
|
|
120
|
+
resourceType: options.resourceType,
|
|
121
|
+
requestId,
|
|
122
|
+
});
|
|
123
|
+
console.error("Action error:", error);
|
|
124
|
+
return { success: false, error: message };
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
function isPreviewMode(data) {
|
|
131
|
+
return (typeof data === "object" &&
|
|
132
|
+
data !== null &&
|
|
133
|
+
"preview" in data &&
|
|
134
|
+
data.preview === true);
|
|
135
|
+
}
|
|
136
|
+
function createDummyReservation(userId, operationType) {
|
|
137
|
+
return {
|
|
138
|
+
id: "preview-mode",
|
|
139
|
+
userId,
|
|
140
|
+
amount: 0,
|
|
141
|
+
operationType,
|
|
142
|
+
status: "released",
|
|
143
|
+
createdAt: new Date().toISOString(),
|
|
144
|
+
expiresAt: new Date().toISOString(),
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
//# sourceMappingURL=generic.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generic.js","sourceRoot":"","sources":["../../src/adapters/generic.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,OAAO,EACL,4BAA4B,EAC5B,6BAA6B,EAC7B,0BAA0B,GAC3B,MAAM,oBAAoB,CAAC;AAE5B;;GAEG;AACH,SAAS,iBAAiB;IACxB,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;IAC7B,MAAM,UAAU,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AAO,CAAC,UAAU;wBAC9B,YAAY,EAAE,OAAO,CAAC,YAAY;wBAClC,SAAS;qBACV,CAAC,CAAC;oBACH,OAAO,CAAC,KAAK,CAAC,eAAe,EAAE,KAAK,CAAC,CAAC;oBACtC,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC;gBAC5C,CAAC;YACH,CAAC,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC;AAED,SAAS,aAAa,CAAC,IAAa;IAClC,OAAO,CACL,OAAO,IAAI,KAAK,QAAQ;QACxB,IAAI,KAAK,IAAI;QACb,SAAS,IAAI,IAAI;QAChB,IAAgC,CAAC,OAAO,KAAK,IAAI,CACnD,CAAC;AACJ,CAAC;AAED,SAAS,sBAAsB,CAAC,MAAc,EAAE,aAAqB;IACnE,OAAO;QACL,EAAE,EAAE,cAAc;QAClB,MAAM;QACN,MAAM,EAAE,CAAC;QACT,aAAa;QACb,MAAM,EAAE,UAAU;QAClB,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;QACnC,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;KACpC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/adapters/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,YAAY,EACZ,mBAAmB,EACnB,oBAAoB,EACpB,cAAc,GACf,MAAM,SAAS,CAAC;AAEjB,OAAO,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/adapters/index.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adapter types for the credits system
|
|
3
|
+
*
|
|
4
|
+
* Defines the interface for framework-specific adapters that wrap
|
|
5
|
+
* credit operations with different execution strategies.
|
|
6
|
+
*/
|
|
7
|
+
import type { PortableReservation, WithCreditsOptions } from "../core/types";
|
|
8
|
+
import type { CreditsUser, ICreditsAuthProvider } from "../auth/types";
|
|
9
|
+
import type { ICreditRepository } from "../repository/types";
|
|
10
|
+
import type { DeferredExecutor } from "../core";
|
|
11
|
+
/**
|
|
12
|
+
* Generic action result type
|
|
13
|
+
*/
|
|
14
|
+
export interface ActionResult<T> {
|
|
15
|
+
success: boolean;
|
|
16
|
+
data?: T;
|
|
17
|
+
error?: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Handler function signature for credit-wrapped actions
|
|
21
|
+
*/
|
|
22
|
+
export type CreditActionHandler<TInput, TOutput> = (user: CreditsUser, data: TInput, reservation: PortableReservation) => Promise<ActionResult<TOutput>>;
|
|
23
|
+
/**
|
|
24
|
+
* Configuration for creating a credits adapter
|
|
25
|
+
*/
|
|
26
|
+
export interface CreditsAdapterConfig {
|
|
27
|
+
/** Repository for credit operations */
|
|
28
|
+
repository: ICreditRepository;
|
|
29
|
+
/** Auth provider for user authentication */
|
|
30
|
+
authProvider: ICreditsAuthProvider;
|
|
31
|
+
/** Deferred executor for background tasks */
|
|
32
|
+
deferred: DeferredExecutor;
|
|
33
|
+
/** Operation cost lookup table */
|
|
34
|
+
operationCosts: Record<string, number>;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Interface for credits adapters
|
|
38
|
+
*/
|
|
39
|
+
export interface CreditsAdapter {
|
|
40
|
+
/**
|
|
41
|
+
* Wrap an action function with credit handling
|
|
42
|
+
*/
|
|
43
|
+
withCredits<TInput, TOutput>(options: WithCreditsOptions, handler: CreditActionHandler<TInput, TOutput>): (data: TInput) => Promise<ActionResult<TOutput>>;
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,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+1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/adapters/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { ICreditsAuthProvider, CreditsUser } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Verify a Bearer token from an Authorization header
|
|
4
|
+
*
|
|
5
|
+
* @param header - The Authorization header value (e.g., "Bearer secret123")
|
|
6
|
+
* @param secret - The expected secret value
|
|
7
|
+
* @returns True if token is valid
|
|
8
|
+
*/
|
|
9
|
+
export declare function verifyBearerToken(header: string | null, secret: string): boolean;
|
|
10
|
+
/**
|
|
11
|
+
* API Key auth provider for admin routes
|
|
12
|
+
*
|
|
13
|
+
* Uses Bearer token authentication for external API access
|
|
14
|
+
* The API key is stored in CREDITS_ADMIN_API_KEY environment variable
|
|
15
|
+
*/
|
|
16
|
+
export declare class ApiKeyCreditsProvider implements ICreditsAuthProvider {
|
|
17
|
+
private readonly apiKey;
|
|
18
|
+
private isValidated;
|
|
19
|
+
constructor(authHeader?: string | null, apiKey?: string);
|
|
20
|
+
getCurrentUser(): Promise<CreditsUser | null>;
|
|
21
|
+
verifyAdminAccess(): Promise<boolean>;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Verify an API key from a request header
|
|
25
|
+
* @param authHeader - Authorization header value
|
|
26
|
+
* @param apiKey - Optional API key to use (defaults to CREDITS_ADMIN_API_KEY env var)
|
|
27
|
+
* @returns True if API key is valid
|
|
28
|
+
*/
|
|
29
|
+
export declare function verifyAdminApiKey(authHeader: string | null, apiKey?: string): boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Create an API key auth provider from a request
|
|
32
|
+
* @param request - Request object with headers
|
|
33
|
+
* @param apiKey - Optional API key to use
|
|
34
|
+
* @returns API key auth provider
|
|
35
|
+
*/
|
|
36
|
+
export declare function createApiKeyProvider(request: Request, apiKey?: string): ApiKeyCreditsProvider;
|
|
37
|
+
//# sourceMappingURL=api-key.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api-key.d.ts","sourceRoot":"","sources":["../../src/auth/api-key.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAwBjE;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAUhF;AAED;;;;;GAKG;AACH,qBAAa,qBAAsB,YAAW,oBAAoB;IAChE,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAqB;IAC5C,OAAO,CAAC,WAAW,CAAS;gBAEhB,UAAU,CAAC,EAAE,MAAM,GAAG,IAAI,EAAE,MAAM,CAAC,EAAE,MAAM;IASjD,cAAc,IAAI,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC;IAa7C,iBAAiB,IAAI,OAAO,CAAC,OAAO,CAAC;CAG5C;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAarF;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,qBAAqB,CAG7F"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Timing-safe string comparison to prevent timing attacks
|
|
3
|
+
*/
|
|
4
|
+
function timingSafeEqual(a, b) {
|
|
5
|
+
if (!a || !b) {
|
|
6
|
+
return false;
|
|
7
|
+
}
|
|
8
|
+
// Pad strings to same length to prevent length-based timing attacks
|
|
9
|
+
const maxLength = Math.max(a.length, b.length);
|
|
10
|
+
const paddedA = a.padEnd(maxLength);
|
|
11
|
+
const paddedB = b.padEnd(maxLength);
|
|
12
|
+
let result = 0;
|
|
13
|
+
for (let i = 0; i < maxLength; i++) {
|
|
14
|
+
result |= paddedA.charCodeAt(i) ^ paddedB.charCodeAt(i);
|
|
15
|
+
}
|
|
16
|
+
// Also check that lengths were originally equal
|
|
17
|
+
return result === 0 && a.length === b.length;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Verify a Bearer token from an Authorization header
|
|
21
|
+
*
|
|
22
|
+
* @param header - The Authorization header value (e.g., "Bearer secret123")
|
|
23
|
+
* @param secret - The expected secret value
|
|
24
|
+
* @returns True if token is valid
|
|
25
|
+
*/
|
|
26
|
+
export function verifyBearerToken(header, secret) {
|
|
27
|
+
if (!header || !secret) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
// Extract token from "Bearer {token}" format
|
|
31
|
+
const token = header.replace(/^Bearer\s+/i, "");
|
|
32
|
+
// Use timing-safe comparison
|
|
33
|
+
return timingSafeEqual(token, secret);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* API Key auth provider for admin routes
|
|
37
|
+
*
|
|
38
|
+
* Uses Bearer token authentication for external API access
|
|
39
|
+
* The API key is stored in CREDITS_ADMIN_API_KEY environment variable
|
|
40
|
+
*/
|
|
41
|
+
export class ApiKeyCreditsProvider {
|
|
42
|
+
apiKey;
|
|
43
|
+
isValidated = false;
|
|
44
|
+
constructor(authHeader, apiKey) {
|
|
45
|
+
this.apiKey = apiKey ?? process.env.CREDITS_ADMIN_API_KEY;
|
|
46
|
+
// Validate the provided auth header using timing-safe comparison
|
|
47
|
+
if (authHeader && this.apiKey) {
|
|
48
|
+
this.isValidated = verifyBearerToken(authHeader, this.apiKey);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
async getCurrentUser() {
|
|
52
|
+
if (!this.isValidated) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
// API key auth returns a special "admin" user
|
|
56
|
+
return {
|
|
57
|
+
id: "api-admin",
|
|
58
|
+
email: "api@admin.internal",
|
|
59
|
+
name: "API Admin",
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
async verifyAdminAccess() {
|
|
63
|
+
return this.isValidated;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Verify an API key from a request header
|
|
68
|
+
* @param authHeader - Authorization header value
|
|
69
|
+
* @param apiKey - Optional API key to use (defaults to CREDITS_ADMIN_API_KEY env var)
|
|
70
|
+
* @returns True if API key is valid
|
|
71
|
+
*/
|
|
72
|
+
export function verifyAdminApiKey(authHeader, apiKey) {
|
|
73
|
+
if (!authHeader) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
const expectedKey = apiKey ?? process.env.CREDITS_ADMIN_API_KEY;
|
|
77
|
+
if (!expectedKey) {
|
|
78
|
+
console.warn("CREDITS_ADMIN_API_KEY not configured");
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
// Use timing-safe comparison to prevent timing attacks
|
|
82
|
+
return verifyBearerToken(authHeader, expectedKey);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Create an API key auth provider from a request
|
|
86
|
+
* @param request - Request object with headers
|
|
87
|
+
* @param apiKey - Optional API key to use
|
|
88
|
+
* @returns API key auth provider
|
|
89
|
+
*/
|
|
90
|
+
export function createApiKeyProvider(request, apiKey) {
|
|
91
|
+
const authHeader = request.headers.get("Authorization");
|
|
92
|
+
return new ApiKeyCreditsProvider(authHeader, apiKey);
|
|
93
|
+
}
|
|
94
|
+
//# sourceMappingURL=api-key.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api-key.js","sourceRoot":"","sources":["../../src/auth/api-key.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,SAAS,eAAe,CAAC,CAAS,EAAE,CAAS;IAC3C,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC;QACb,OAAO,KAAK,CAAC;IACf,CAAC;IAED,oEAAoE;IACpE,MAAM,SAAS,GAAG,IAAI,CAA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M,CAAC,CAAC;QAChE,CAAC;IACH,CAAC;IAED,KAAK,CAAC,cAAc;QAClB,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC;YACtB,OAAO,IAAI,CAAC;QACd,CAAC;QAED,8CAA8C;QAC9C,OAAO;YACL,EAAE,EAAE,WAAW;YACf,KAAK,EAAE,oBAAoB;YAC3B,IAAI,EAAE,WAAW;SAClB,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,iBAAiB;QACrB,OAAO,IAAI,CAAC,WAAW,CAAC;IAC1B,CAAC;CACF;AAED;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,UAAyB,EAAE,MAAe;IAC1E,IAAI,CAAC,UAAU,EAAE,CAAC;QAChB,OAAO,KAAK,CAAC;IACf,CAAC;IAED,MAAM,WAAW,GAAG,MAAM,IAAI,OAAO,CAAC,GAAG,CAAC,qBAAqB,CAAC;IAChE,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,OAAO,CAAC,IAAI,CAAC,sCAAsC,CAAC,CAAC;QACrD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,uDAAuD;IACvD,OAAO,iBAAiB,CAAC,UAAU,EAAE,WAAW,CAAC,CAAC;AACpD,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,oBAAoB,CAAC,OAAgB,EAAE,MAAe;IACpE,MAAM,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC;IACxD,OAAO,IAAI,qBAAqB,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;AACvD,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Credits auth providers - barrel exports
|
|
3
|
+
*
|
|
4
|
+
* Provides authentication abstraction for credits system
|
|
5
|
+
*/
|
|
6
|
+
export type { ICreditsAuthProvider, CreditsAuthProviderFactory, CreditsUser, } from "./types";
|
|
7
|
+
export { ApiKeyCreditsProvider, verifyAdminApiKey, createApiKeyProvider, verifyBearerToken, } from "./api-key";
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/auth/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,YAAY,EACV,oBAAoB,EACpB,0BAA0B,EAC1B,WAAW,GACZ,MAAM,SAAS,CAAC;AAGjB,OAAO,EACL,qBAAqB,EACrB,iBAAiB,EACjB,oBAAoB,EACpB,iBAAiB,GAClB,MAAM,WAAW,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Credits auth providers - barrel exports
|
|
3
|
+
*
|
|
4
|
+
* Provides authentication abstraction for credits system
|
|
5
|
+
*/
|
|
6
|
+
// API Key implementation (for admin routes)
|
|
7
|
+
export { ApiKeyCreditsProvider, verifyAdminApiKey, createApiKeyProvider, verifyBearerToken, } from "./api-key";
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/auth/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AASH,4CAA4C;AAC5C,OAAO,EACL,qBAAqB,EACrB,iBAAiB,EACjB,oBAAoB,EACpB,iBAAiB,GAClB,MAAM,WAAW,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Authenticated user information for credits operations
|
|
3
|
+
*/
|
|
4
|
+
export interface CreditsUser {
|
|
5
|
+
id: string;
|
|
6
|
+
email?: string | null;
|
|
7
|
+
name?: string | null;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Auth provider interface for credits system
|
|
11
|
+
*
|
|
12
|
+
* Implementations can use any auth system (NextAuth, JWT, API keys, etc.)
|
|
13
|
+
*/
|
|
14
|
+
export interface ICreditsAuthProvider {
|
|
15
|
+
/**
|
|
16
|
+
* Get the currently authenticated user
|
|
17
|
+
* @returns User information or null if not authenticated
|
|
18
|
+
*/
|
|
19
|
+
getCurrentUser(): Promise<CreditsUser | null>;
|
|
20
|
+
/**
|
|
21
|
+
* Verify if a user has admin access for credits management
|
|
22
|
+
* @param userId - User ID to check
|
|
23
|
+
* @returns True if user has admin access
|
|
24
|
+
*/
|
|
25
|
+
verifyAdminAccess(userId: string): Promise<boolean>;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Factory type for creating auth provider instances
|
|
29
|
+
*/
|
|
30
|
+
export type CreditsAuthProviderFactory = () => ICreditsAuthProvider;
|
|
31
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/auth/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CACtB;AAED;;;;GAIG;AACH,MAAM,WAAW,oBAAoB;IACnC;;;OAGG;IACH,cAAc,IAAI,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC,CAAC;IAE9C;;;;OAIG;IACH,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CACrD;AAED;;GAEG;AACH,MAAM,MAAM,0BAA0B,GAAG,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/auth/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { SubscriptionTier, TierConfig } from "../core/types";
|
|
2
|
+
import { getValidOperationTypes, isValidOperationType } from "./index";
|
|
3
|
+
/**
|
|
4
|
+
* Get operation costs as a record
|
|
5
|
+
* Returns all configured operation costs from the current config
|
|
6
|
+
*/
|
|
7
|
+
export declare function getOperationCosts(): Record<string, number>;
|
|
8
|
+
/**
|
|
9
|
+
* Get the credit cost for an operation type
|
|
10
|
+
* Uses the configuration system for dynamic costs
|
|
11
|
+
*
|
|
12
|
+
* @param type - Operation type (must be configured in the system)
|
|
13
|
+
* @returns Credit cost for the operation
|
|
14
|
+
* @throws Error if the operation type is not configured
|
|
15
|
+
*/
|
|
16
|
+
export declare function getOperationCost(type: string): number;
|
|
17
|
+
export { getValidOperationTypes, isValidOperationType };
|
|
18
|
+
/**
|
|
19
|
+
* Tier configurations with monthly limits and pricing
|
|
20
|
+
*
|
|
21
|
+
* NOTE: This is kept for backward compatibility.
|
|
22
|
+
* The actual configs are loaded from configuration.
|
|
23
|
+
* Use getTierConfig() instead of accessing this directly.
|
|
24
|
+
*/
|
|
25
|
+
export declare const TIER_CONFIGS: Record<SubscriptionTier, TierConfig>;
|
|
26
|
+
/**
|
|
27
|
+
* Get tier configuration
|
|
28
|
+
* Uses the configuration system for dynamic configs
|
|
29
|
+
*
|
|
30
|
+
* @param tier - Subscription tier
|
|
31
|
+
* @returns Tier configuration
|
|
32
|
+
*/
|
|
33
|
+
export declare function getTierConfig(tier: SubscriptionTier): TierConfig;
|
|
34
|
+
/**
|
|
35
|
+
* Get monthly credit limit for a tier
|
|
36
|
+
* Returns Infinity for unlimited tier
|
|
37
|
+
* Uses the configuration system for dynamic limits
|
|
38
|
+
*
|
|
39
|
+
* @param tier - Subscription tier
|
|
40
|
+
* @returns Monthly credit limit
|
|
41
|
+
*/
|
|
42
|
+
export declare function getMonthlyLimit(tier: SubscriptionTier): number;
|
|
43
|
+
/**
|
|
44
|
+
* Default credits for new users (free tier)
|
|
45
|
+
* Uses the configuration system
|
|
46
|
+
*/
|
|
47
|
+
export declare const DEFAULT_FREE_CREDITS = 25;
|
|
48
|
+
/**
|
|
49
|
+
* Get default free credits from configuration
|
|
50
|
+
*/
|
|
51
|
+
export declare function getDefaultFreeCredits(): number;
|
|
52
|
+
/**
|
|
53
|
+
* Reservation expiry time in milliseconds
|
|
54
|
+
* Uses the configuration system
|
|
55
|
+
*/
|
|
56
|
+
export declare const RESERVATION_EXPIRY_MS: number;
|
|
57
|
+
/**
|
|
58
|
+
* Get reservation expiry time from configuration
|
|
59
|
+
*/
|
|
60
|
+
export declare function getReservationExpiryMs(): number;
|
|
61
|
+
//# sourceMappingURL=costs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"costs.d.ts","sourceRoot":"","sources":["../../src/config/costs.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAClE,OAAO,EAKL,sBAAsB,EACtB,oBAAoB,EACrB,MAAM,SAAS,CAAC;AAEjB;;;GAGG;AACH,wBAAgB,iBAAiB,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAE1D;AAED;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAErD;AAGD,OAAO,EAAE,sBAAsB,EAAE,oBAAoB,EAAE,CAAC;AAExD;;;;;;GAMG;AACH,eAAO,MAAM,YAAY,EAAE,MAAM,CAAC,gBAAgB,EAAE,UAAU,CAa7D,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,gBAAgB,GAAG,UAAU,CAEhE;AAED;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,gBAAgB,GAAG,MAAM,CAE9D;AAED;;;GAGG;AACH,eAAO,MAAM,oBAAoB,KAAK,CAAC;AAEvC;;GAEG;AACH,wBAAgB,qBAAqB,IAAI,MAAM,CAE9C;AAED;;;GAGG;AACH,eAAO,MAAM,qBAAqB,QAAgB,CAAC;AAEnD;;GAEG;AACH,wBAAgB,sBAAsB,IAAI,MAAM,CAE/C"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { getConfig, getConfigOperationCost, getConfigTierConfig, getConfigMonthlyLimit, getValidOperationTypes, isValidOperationType, } from "./index";
|
|
2
|
+
/**
|
|
3
|
+
* Get operation costs as a record
|
|
4
|
+
* Returns all configured operation costs from the current config
|
|
5
|
+
*/
|
|
6
|
+
export function getOperationCosts() {
|
|
7
|
+
return { ...getConfig().operationCosts };
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Get the credit cost for an operation type
|
|
11
|
+
* Uses the configuration system for dynamic costs
|
|
12
|
+
*
|
|
13
|
+
* @param type - Operation type (must be configured in the system)
|
|
14
|
+
* @returns Credit cost for the operation
|
|
15
|
+
* @throws Error if the operation type is not configured
|
|
16
|
+
*/
|
|
17
|
+
export function getOperationCost(type) {
|
|
18
|
+
return getConfigOperationCost(type);
|
|
19
|
+
}
|
|
20
|
+
// Re-export validation helpers for convenience
|
|
21
|
+
export { getValidOperationTypes, isValidOperationType };
|
|
22
|
+
/**
|
|
23
|
+
* Tier configurations with monthly limits and pricing
|
|
24
|
+
*
|
|
25
|
+
* NOTE: This is kept for backward compatibility.
|
|
26
|
+
* The actual configs are loaded from configuration.
|
|
27
|
+
* Use getTierConfig() instead of accessing this directly.
|
|
28
|
+
*/
|
|
29
|
+
export const TIER_CONFIGS = {
|
|
30
|
+
get free() {
|
|
31
|
+
return getConfig().tierConfigs.free;
|
|
32
|
+
},
|
|
33
|
+
get basic() {
|
|
34
|
+
return getConfig().tierConfigs.basic;
|
|
35
|
+
},
|
|
36
|
+
get premium() {
|
|
37
|
+
return getConfig().tierConfigs.premium;
|
|
38
|
+
},
|
|
39
|
+
get unlimited() {
|
|
40
|
+
return getConfig().tierConfigs.unlimited;
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Get tier configuration
|
|
45
|
+
* Uses the configuration system for dynamic configs
|
|
46
|
+
*
|
|
47
|
+
* @param tier - Subscription tier
|
|
48
|
+
* @returns Tier configuration
|
|
49
|
+
*/
|
|
50
|
+
export function getTierConfig(tier) {
|
|
51
|
+
return getConfigTierConfig(tier);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Get monthly credit limit for a tier
|
|
55
|
+
* Returns Infinity for unlimited tier
|
|
56
|
+
* Uses the configuration system for dynamic limits
|
|
57
|
+
*
|
|
58
|
+
* @param tier - Subscription tier
|
|
59
|
+
* @returns Monthly credit limit
|
|
60
|
+
*/
|
|
61
|
+
export function getMonthlyLimit(tier) {
|
|
62
|
+
return getConfigMonthlyLimit(tier);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Default credits for new users (free tier)
|
|
66
|
+
* Uses the configuration system
|
|
67
|
+
*/
|
|
68
|
+
export const DEFAULT_FREE_CREDITS = 25; // Static fallback, actual value from getConfig().defaultFreeCredits
|
|
69
|
+
/**
|
|
70
|
+
* Get default free credits from configuration
|
|
71
|
+
*/
|
|
72
|
+
export function getDefaultFreeCredits() {
|
|
73
|
+
return getConfig().defaultFreeCredits;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Reservation expiry time in milliseconds
|
|
77
|
+
* Uses the configuration system
|
|
78
|
+
*/
|
|
79
|
+
export const RESERVATION_EXPIRY_MS = 5 * 60 * 1000; // Static fallback
|
|
80
|
+
/**
|
|
81
|
+
* Get reservation expiry time from configuration
|
|
82
|
+
*/
|
|
83
|
+
export function getReservationExpiryMs() {
|
|
84
|
+
return getConfig().reservationExpiryMs;
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=costs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"costs.js","sourceRoot":"","sources":["../../src/config/costs.ts"],"names":[],"mappings":"AACA,OAAO,EACL,SAAS,EACT,sBAAsB,EACtB,mBAAmB,EACnB,qBAAqB,EACrB,sBAAsB,EACtB,oBAAoB,GACrB,MAAM,SAAS,CAAC;AAEjB;;;GAGG;AACH,MAAM,UAAU,iBAAiB;IAC/B,OAAO,EAAE,GAAG,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