@prsm/entitle 2.1.0 → 2.1.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prsm/entitle",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "description": "Plan-based entitlements and feature gating, resolved at runtime, backed by postgres",
5
5
  "type": "module",
6
6
  "exports": {
@@ -49,9 +49,13 @@ function assertBoolean(scope, key, v) {
49
49
  }
50
50
 
51
51
  function assertLimitValue(scope, key, v) {
52
- if (v !== null && (typeof v !== "number" || !Number.isFinite(v))) {
52
+ if (v === null) return
53
+ if (typeof v !== "number" || !Number.isFinite(v)) {
53
54
  throw new Error(`${scope} limit "${key}" must be a finite number or null (null means unlimited)`)
54
55
  }
56
+ if (v < 0) {
57
+ throw new Error(`${scope} limit "${key}" must not be negative (use 0 to deny, null for unlimited)`)
58
+ }
55
59
  }
56
60
 
57
61
  /**
@@ -184,6 +188,12 @@ export function createEntitlements(options = {}) {
184
188
  return effective
185
189
  }
186
190
 
191
+ async function resolveLimit(subject, key) {
192
+ requireLimit(key)
193
+ const eff = await resolve(subject)
194
+ return key in eff.limits ? eff.limits[key] : 0
195
+ }
196
+
187
197
  return {
188
198
  /** Create the backing tables if they do not exist. Idempotent. */
189
199
  setup() {
@@ -282,10 +292,8 @@ export function createEntitlements(options = {}) {
282
292
  * @param {string} key
283
293
  * @returns {Promise<number|null>}
284
294
  */
285
- async limit(subject, key) {
286
- requireLimit(key)
287
- const eff = await resolve(subject)
288
- return key in eff.limits ? eff.limits[key] : 0
295
+ limit(subject, key) {
296
+ return resolveLimit(subject, key)
289
297
  },
290
298
 
291
299
  /**
@@ -303,7 +311,7 @@ export function createEntitlements(options = {}) {
303
311
  throw new Error("check requires a `meter`; pass it to createEntitlements, or use limit() for the static ceiling")
304
312
  }
305
313
  return traced(tracer, "entitle.check", { "entitle.subject": subject, "entitle.feature": key }, async () => {
306
- const limit = await this.limit(subject, key)
314
+ const limit = await resolveLimit(subject, key)
307
315
  const usage = await meter.usage({ ...usageQuery, subject, metric: key })
308
316
  const used = usage.quantity
309
317
  const allowed = limit === null || used < limit