@compilr-dev/sdk 0.9.19 → 0.9.21
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.
|
@@ -11,7 +11,36 @@
|
|
|
11
11
|
* - fetchFn: how to call the server endpoint
|
|
12
12
|
* - store (optional): encrypted persistence for offline grace (IEntitlementStore)
|
|
13
13
|
*/
|
|
14
|
+
import { createPublicKey, verify } from 'node:crypto';
|
|
14
15
|
import { UNLIMITED, OFFLINE_FALLBACK_LIMITS } from './types.js';
|
|
16
|
+
/**
|
|
17
|
+
* Ed25519 public key for verifying entitlement response signatures.
|
|
18
|
+
* The corresponding private key is stored in Netlify env var ENTITLEMENT_SIGNING_KEY.
|
|
19
|
+
* This public key cannot forge signatures — it can only verify them.
|
|
20
|
+
*/
|
|
21
|
+
const ENTITLEMENT_PUBLIC_KEY = 'MCowBQYDK2VwAyEAMgDyiGWQVSnZKV6NF1UrDIkWtfH9BAyv93PyEg7AH40=';
|
|
22
|
+
/**
|
|
23
|
+
* Verify the Ed25519 signature on an entitlement response.
|
|
24
|
+
* Returns true if valid or if signature verification is not available.
|
|
25
|
+
*/
|
|
26
|
+
function verifySignature(response) {
|
|
27
|
+
if (!response.signature)
|
|
28
|
+
return true; // No signature — allow (beta/dev)
|
|
29
|
+
try {
|
|
30
|
+
const { signature, ...payload } = response;
|
|
31
|
+
const publicKey = createPublicKey({
|
|
32
|
+
key: Buffer.from(ENTITLEMENT_PUBLIC_KEY, 'base64'),
|
|
33
|
+
format: 'der',
|
|
34
|
+
type: 'spki',
|
|
35
|
+
});
|
|
36
|
+
const payloadBuffer = Buffer.from(JSON.stringify(payload));
|
|
37
|
+
const signatureBuffer = Buffer.from(signature, 'base64');
|
|
38
|
+
return verify(null, payloadBuffer, publicKey, signatureBuffer);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return true; // Verification unavailable — fail open (don't break during migration)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
15
44
|
// =============================================================================
|
|
16
45
|
// Cache Implementation
|
|
17
46
|
// =============================================================================
|
|
@@ -146,6 +175,10 @@ export class EntitlementCache {
|
|
|
146
175
|
async fetchAndCache() {
|
|
147
176
|
try {
|
|
148
177
|
const response = await this.fetchFn();
|
|
178
|
+
// Verify Ed25519 signature (prevents tampered responses)
|
|
179
|
+
if (!verifySignature(response)) {
|
|
180
|
+
throw new Error('Entitlement signature verification failed');
|
|
181
|
+
}
|
|
149
182
|
this.entitlements = response;
|
|
150
183
|
this.fetchedAtMonotonic = process.hrtime.bigint();
|
|
151
184
|
this.fetchedAtWall = Date.now();
|
|
@@ -174,7 +207,7 @@ export class EntitlementCache {
|
|
|
174
207
|
if (stored) {
|
|
175
208
|
const parsed = JSON.parse(stored);
|
|
176
209
|
const age = Date.now() - parsed.fetchedAtWall;
|
|
177
|
-
if (age < this.offlineGraceMs) {
|
|
210
|
+
if (age < this.offlineGraceMs && verifySignature(parsed.response)) {
|
|
178
211
|
this.entitlements = parsed.response;
|
|
179
212
|
// Compute monotonic offset from wall-clock age so isWithinOfflineGrace()
|
|
180
213
|
// returns the correct remaining time (not a fresh 24h window on every restart)
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gating Helpers — Shared utilities for limit enforcement.
|
|
3
|
+
*
|
|
4
|
+
* DailyCounter: In-memory counter that resets at midnight UTC.
|
|
5
|
+
* Format helpers: Human-readable limit messages and time-until-reset.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* In-memory counter that resets daily at midnight UTC.
|
|
9
|
+
*
|
|
10
|
+
* Used for maxConversationsPerDay and maxAgentMessagesPerDay.
|
|
11
|
+
* Can be seeded from server-reported usage on startup.
|
|
12
|
+
*/
|
|
13
|
+
export declare class DailyCounter {
|
|
14
|
+
private count;
|
|
15
|
+
private resetDate;
|
|
16
|
+
/**
|
|
17
|
+
* Increment the counter and return the new value.
|
|
18
|
+
* Auto-resets if the UTC date has changed.
|
|
19
|
+
*/
|
|
20
|
+
increment(): number;
|
|
21
|
+
/**
|
|
22
|
+
* Get the current count without incrementing.
|
|
23
|
+
*/
|
|
24
|
+
getCount(): number;
|
|
25
|
+
/**
|
|
26
|
+
* Seed the counter with a server-reported value.
|
|
27
|
+
* Called on startup to prevent restart-based abuse.
|
|
28
|
+
*/
|
|
29
|
+
seed(count: number, date?: string): void;
|
|
30
|
+
/**
|
|
31
|
+
* Reset the counter to zero (e.g., for testing).
|
|
32
|
+
*/
|
|
33
|
+
reset(): void;
|
|
34
|
+
private ensureCurrentDay;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Format a human-readable limit message.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* formatLimitMessage('projects', 2, 2)
|
|
41
|
+
* // "You've reached your project limit (2/2)"
|
|
42
|
+
*
|
|
43
|
+
* formatLimitMessage('daily messages', 100, 100)
|
|
44
|
+
* // "You've reached your daily message limit (100/100)"
|
|
45
|
+
*/
|
|
46
|
+
export declare function formatLimitMessage(limitName: string, current: number, limit: number): string;
|
|
47
|
+
/**
|
|
48
|
+
* Format the time remaining until daily counters reset (midnight UTC).
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* formatTimeUntilReset()
|
|
52
|
+
* // "Resets in 4h 23m" or "Resets in 59m"
|
|
53
|
+
*/
|
|
54
|
+
export declare function formatTimeUntilReset(): string;
|
|
55
|
+
/**
|
|
56
|
+
* Format an upgrade hint with the limit context.
|
|
57
|
+
*/
|
|
58
|
+
export declare function formatUpgradeHint(limitName: string): string;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gating Helpers — Shared utilities for limit enforcement.
|
|
3
|
+
*
|
|
4
|
+
* DailyCounter: In-memory counter that resets at midnight UTC.
|
|
5
|
+
* Format helpers: Human-readable limit messages and time-until-reset.
|
|
6
|
+
*/
|
|
7
|
+
// =============================================================================
|
|
8
|
+
// Daily Counter
|
|
9
|
+
// =============================================================================
|
|
10
|
+
/**
|
|
11
|
+
* In-memory counter that resets daily at midnight UTC.
|
|
12
|
+
*
|
|
13
|
+
* Used for maxConversationsPerDay and maxAgentMessagesPerDay.
|
|
14
|
+
* Can be seeded from server-reported usage on startup.
|
|
15
|
+
*/
|
|
16
|
+
export class DailyCounter {
|
|
17
|
+
count = 0;
|
|
18
|
+
resetDate = ''; // 'YYYY-MM-DD' in UTC
|
|
19
|
+
/**
|
|
20
|
+
* Increment the counter and return the new value.
|
|
21
|
+
* Auto-resets if the UTC date has changed.
|
|
22
|
+
*/
|
|
23
|
+
increment() {
|
|
24
|
+
this.ensureCurrentDay();
|
|
25
|
+
this.count++;
|
|
26
|
+
return this.count;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Get the current count without incrementing.
|
|
30
|
+
*/
|
|
31
|
+
getCount() {
|
|
32
|
+
this.ensureCurrentDay();
|
|
33
|
+
return this.count;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Seed the counter with a server-reported value.
|
|
37
|
+
* Called on startup to prevent restart-based abuse.
|
|
38
|
+
*/
|
|
39
|
+
seed(count, date) {
|
|
40
|
+
const today = new Date().toISOString().slice(0, 10);
|
|
41
|
+
const seedDate = date ?? today;
|
|
42
|
+
if (seedDate === today) {
|
|
43
|
+
this.count = count;
|
|
44
|
+
this.resetDate = today;
|
|
45
|
+
}
|
|
46
|
+
// If seed date is not today, ignore (stale data)
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Reset the counter to zero (e.g., for testing).
|
|
50
|
+
*/
|
|
51
|
+
reset() {
|
|
52
|
+
this.count = 0;
|
|
53
|
+
this.resetDate = '';
|
|
54
|
+
}
|
|
55
|
+
ensureCurrentDay() {
|
|
56
|
+
const today = new Date().toISOString().slice(0, 10);
|
|
57
|
+
if (today !== this.resetDate) {
|
|
58
|
+
this.count = 0;
|
|
59
|
+
this.resetDate = today;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
// =============================================================================
|
|
64
|
+
// Format Helpers
|
|
65
|
+
// =============================================================================
|
|
66
|
+
/**
|
|
67
|
+
* Format a human-readable limit message.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* formatLimitMessage('projects', 2, 2)
|
|
71
|
+
* // "You've reached your project limit (2/2)"
|
|
72
|
+
*
|
|
73
|
+
* formatLimitMessage('daily messages', 100, 100)
|
|
74
|
+
* // "You've reached your daily message limit (100/100)"
|
|
75
|
+
*/
|
|
76
|
+
export function formatLimitMessage(limitName, current, limit) {
|
|
77
|
+
return `You've reached your ${limitName} limit (${String(current)}/${String(limit)})`;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Format the time remaining until daily counters reset (midnight UTC).
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* formatTimeUntilReset()
|
|
84
|
+
* // "Resets in 4h 23m" or "Resets in 59m"
|
|
85
|
+
*/
|
|
86
|
+
export function formatTimeUntilReset() {
|
|
87
|
+
const now = new Date();
|
|
88
|
+
const midnight = new Date(now);
|
|
89
|
+
midnight.setUTCDate(midnight.getUTCDate() + 1);
|
|
90
|
+
midnight.setUTCHours(0, 0, 0, 0);
|
|
91
|
+
const diffMs = midnight.getTime() - now.getTime();
|
|
92
|
+
const hours = Math.floor(diffMs / (1000 * 60 * 60));
|
|
93
|
+
const minutes = Math.floor((diffMs % (1000 * 60 * 60)) / (1000 * 60));
|
|
94
|
+
if (hours > 0) {
|
|
95
|
+
return `Resets in ${String(hours)}h ${String(minutes)}m`;
|
|
96
|
+
}
|
|
97
|
+
return `Resets in ${String(minutes)}m`;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Format an upgrade hint with the limit context.
|
|
101
|
+
*/
|
|
102
|
+
export function formatUpgradeHint(limitName) {
|
|
103
|
+
return `Upgrade to Pro for unlimited ${limitName}`;
|
|
104
|
+
}
|
|
@@ -5,3 +5,4 @@ export type { TierLimits, EntitlementResponse, LimitCheckResult, } from './types
|
|
|
5
5
|
export { UNLIMITED, OFFLINE_FALLBACK_LIMITS, } from './types.js';
|
|
6
6
|
export type { IEntitlementStore, EntitlementCacheConfig, } from './cache.js';
|
|
7
7
|
export { EntitlementCache } from './cache.js';
|
|
8
|
+
export { DailyCounter, formatLimitMessage, formatTimeUntilReset, formatUpgradeHint, } from './gating.js';
|
package/dist/index.d.ts
CHANGED
|
@@ -58,7 +58,7 @@ export { createSQLiteRepositories, SQLiteProjectRepository, SQLiteWorkItemReposi
|
|
|
58
58
|
export type { SQLiteRepositories, CreateSQLiteRepositoriesOptions, ProjectDeleteHooks, ProjectRecord, WorkItemRecord, ProjectDocumentRecord, WorkItemCommentRecord, } from './platform/index.js';
|
|
59
59
|
export { createAskUserTool, createAskUserSimpleTool, createProposeAlternativesTool } from './tools/index.js';
|
|
60
60
|
export type { AskUserQuestion, AskUserInput, AskUserResult, AskUserHandler, AskUserSimpleInput, AskUserSimpleResult, AskUserSimpleHandler, Alternative, ProposeAlternativesInput, ProposeAlternativesResult, ProposeAlternativesHandler, } from './tools/index.js';
|
|
61
|
-
export { EntitlementCache, UNLIMITED, OFFLINE_FALLBACK_LIMITS } from './entitlements/index.js';
|
|
61
|
+
export { EntitlementCache, UNLIMITED, OFFLINE_FALLBACK_LIMITS, DailyCounter, formatLimitMessage, formatTimeUntilReset, formatUpgradeHint, } from './entitlements/index.js';
|
|
62
62
|
export type { TierLimits, EntitlementResponse, LimitCheckResult, IEntitlementStore, EntitlementCacheConfig, } from './entitlements/index.js';
|
|
63
63
|
export { detectProject, suggestProjectType, detectCommon } from './detection/index.js';
|
|
64
64
|
export type { DetectProjectOptions, DetectionResult, ContentSummary } from './detection/index.js';
|
package/dist/index.js
CHANGED
|
@@ -135,7 +135,7 @@ export { createAskUserTool, createAskUserSimpleTool, createProposeAlternativesTo
|
|
|
135
135
|
// =============================================================================
|
|
136
136
|
// Entitlements (server-driven tier management)
|
|
137
137
|
// =============================================================================
|
|
138
|
-
export { EntitlementCache, UNLIMITED, OFFLINE_FALLBACK_LIMITS } from './entitlements/index.js';
|
|
138
|
+
export { EntitlementCache, UNLIMITED, OFFLINE_FALLBACK_LIMITS, DailyCounter, formatLimitMessage, formatTimeUntilReset, formatUpgradeHint, } from './entitlements/index.js';
|
|
139
139
|
// =============================================================================
|
|
140
140
|
// Project Detection (universal project content detection)
|
|
141
141
|
// =============================================================================
|