@lunora/flags 1.0.0-alpha.41 → 1.0.0-alpha.43

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/README.md CHANGED
@@ -79,7 +79,11 @@ export const listPosts = query(async (ctx) => {
79
79
  ```
80
80
 
81
81
  `ctx.flags` never throws: a missing flag, type mismatch, or provider error resolves
82
- with the `defaultValue`. The default `targetingKey` (from `identify`) is merged under
82
+ with the `defaultValue`. It does not do so silently — a provider that fails to BIND
83
+ (a missing binding, an unset token, a throwing `initialize`) is logged through
84
+ `defineFlags({ logger })`, or on `console.error` when none is configured, since
85
+ otherwise a misconfigured deployment serves every flag at its checked-in default
86
+ with nothing to notice. The default `targetingKey` (from `identify`) is merged under
83
87
  any per-call context, and identical evaluations within one request are memoized.
84
88
 
85
89
  ## Exports
package/dist/index.d.mts CHANGED
@@ -54,7 +54,9 @@ interface CreateFlagsOptions {
54
54
  * (`definition`, `env`) pair — both stable object identities for the isolate's
55
55
  * lifetime — and each pair owns its own OpenFeature domain, so a pair always
56
56
  * evaluates against the provider it was configured with instead of whichever
57
- * bind happened first. Evaluations resolve through the
57
+ * bind happened first. The bind is additionally attempted at most ONCE per
58
+ * request, so a broken deployment costs one `initialize` and one log line
59
+ * rather than one per flag read. Evaluations resolve through the
58
60
  * OpenFeature client (the SDK applies hooks and never throws — provider errors
59
61
  * surface as the default value with an `errorCode`). The default `targetingKey`
60
62
  * is merged under any per-call context, and identical evaluations within the
package/dist/index.d.ts CHANGED
@@ -54,7 +54,9 @@ interface CreateFlagsOptions {
54
54
  * (`definition`, `env`) pair — both stable object identities for the isolate's
55
55
  * lifetime — and each pair owns its own OpenFeature domain, so a pair always
56
56
  * evaluates against the provider it was configured with instead of whichever
57
- * bind happened first. Evaluations resolve through the
57
+ * bind happened first. The bind is additionally attempted at most ONCE per
58
+ * request, so a broken deployment costs one `initialize` and one log line
59
+ * rather than one per flag read. Evaluations resolve through the
58
60
  * OpenFeature client (the SDK applies hooks and never throws — provider errors
59
61
  * surface as the default value with an `errorCode`). The default `targetingKey`
60
62
  * is merged under any per-call context, and identical evaluations within the
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import{defineFlags as o,isFlagsDefinition as r}from"./packem_shared/defineFlags-BZhoR_h7.mjs";import{createFlags as f}from"./packem_shared/createFlags-BuOXqhnI.mjs";export{f as createFlags,o as defineFlags,r as isFlagsDefinition};
1
+ import{defineFlags as o,isFlagsDefinition as r}from"./packem_shared/defineFlags-BZhoR_h7.mjs";import{createFlags as f}from"./packem_shared/createFlags-D8e_hTbh.mjs";export{f as createFlags,o as defineFlags,r as isFlagsDefinition};
@@ -0,0 +1 @@
1
+ import{LunoraError as D}from"@lunora/errors";import{OpenFeature as E,ErrorCode as $}from"@openfeature/server-sdk";const A=/["\\\u0000-\u001F\uD800-\uDFFF]/,j=e=>A.test(e)?JSON.stringify(e):`"${e}"`,h=e=>{if(e===void 0)return"null";if(typeof e=="bigint")throw new TypeError("stableStringify: cannot use a bigint in a stable JSON cache key — pass it as a string, or use stableWireKey");if(typeof e=="number"){if(Number.isNaN(e))return"nan";if(e===1/0)return"inf";if(e===-1/0)return"-inf";if(Object.is(e,-0))return"-0"}if(typeof e=="string")return j(e);if(e===null||typeof e!="object")return JSON.stringify(e);if(Array.isArray(e)){let i="[";for(let l=0;l<e.length;l++)l>0&&(i+=","),i+=h(e[l]);return i+"]"}const c=Object.getPrototypeOf(e);if(c!==null&&c!==Object.prototype){const i=e.constructor?.name??"value";throw new TypeError(`stableStringify: cannot use a ${i} in a stable JSON cache key — only plain objects, arrays, and JSON primitives are supported (wire-typed values key via stableWireKey)`)}const a=e,s=Object.keys(a).sort();let n="{",u=!0;for(const i of s){const l=a[i];l!==void 0&&(u?u=!1:n+=",",n+=j(i),n+=":",n+=h(l))}return n+"}"},S="lunora";let O=new WeakMap,p=0;const C={},M=e=>Object.keys(e).length===0?C:e,T=e=>typeof e?.then=="function",W=(e,c)=>{const a=`@lunora/flags: could not bind the OpenFeature provider — every flag read falls back to its caller-supplied default until this is fixed: ${c instanceof Error?c.message:String(c)}`;if(e)try{const s=e.error(a);T(s)&&s.catch(()=>{});return}catch{}console.error(a)},J=(e,c,a)=>{const s=M(c);let n=O.get(e);n===void 0&&(n=new WeakMap,O.set(e,n));let u=n.get(s);if(u===void 0&&(p+=1,u={domain:p===1?S:`${S}-${String(p)}`},n.set(s,u)),u.client!==void 0)return u.client;const i=u,{domain:l}=i,{hooks:b,logger:d}=e,g=(async()=>{await E.setProviderAndWait(l,a());const t=E.getClient(l);return d&&t.setLogger(d),b&&b.length>0&&t.addHooks(...b),t})();return i.client=g,g.catch(t=>{i.client===g&&(i.client=void 0),W(d,t)}).catch(()=>{}),g},B=async()=>{O=new WeakMap,p=0,await E.clearProviders()},L=(e,c,a,s)=>{const n=h([e,c,a]);return Object.keys(s).length===0?`${n}:{}`:`${n}:${h(s)}`},P=(e,c,a,s,n)=>{switch(c){case"boolean":return e.getBooleanDetails(a,s,n);case"number":return e.getNumberDetails(a,s,n);case"object":return e.getObjectDetails(a,s,n);case"string":return e.getStringDetails(a,s,n);default:throw new D("INTERNAL",`createFlags: unknown flag type "${c}"`)}},_=(e,c,a={})=>{const{provider:s,targetingKey:n}=a,u=()=>s?.()??e.provider(c);let i;try{i=typeof n=="function"?n():n}catch{i=void 0}const l=new Map;let b;const d=()=>(b??=J(e,c,u),b),g=(t,r,o,f)=>{const k=i===void 0?{...f}:{targetingKey:i,...f},F=y=>({errorCode:$.GENERAL,errorMessage:y instanceof Error?y.message:String(y),flagKey:r,flagMetadata:{},reason:"ERROR",value:o}),w=()=>d().then(y=>P(y,t,r,o,k)).catch(F);let m;try{m=L(t,r,o,k)}catch{return w()}const v=l.get(m);if(v)return v;const N=w();return l.set(m,N),N};return{boolean:(t,r,o)=>g("boolean",t,r,o).then(f=>f.value),details:{boolean:(t,r,o)=>g("boolean",t,r,o),number:(t,r,o)=>g("number",t,r,o),object:(t,r,o)=>g("object",t,r,o),string:(t,r,o)=>g("string",t,r,o)},number:(t,r,o)=>g("number",t,r,o).then(f=>f.value),object:(t,r,o)=>g("object",t,r,o).then(f=>f.value),string:(t,r,o)=>g("string",t,r,o).then(f=>f.value)}};export{_ as createFlags,B as resetFlags};
@@ -32,8 +32,9 @@ interface FlagshipHttpOptions {
32
32
  * Either the literal token, or a thunk resolved against the Worker `env` at
33
33
  * construction (`(env) => env.FLAGSHIP_TOKEN`) so the secret never has to be
34
34
  * inlined in source. A thunk resolving to anything but a non-empty string
35
- * throws: `Bearer undefined` would otherwise make every evaluation fall
36
- * silently back to its default.
35
+ * throws from the bind (`createFlags` logs it and evaluations fall back to
36
+ * their caller-supplied defaults): `Bearer undefined` would otherwise make
37
+ * every evaluation fall back with nothing said.
37
38
  */
38
39
  authToken?: ((env: Record<string, unknown>) => unknown) | string;
39
40
  /** Base URL override (only used with `appId`). */
@@ -32,8 +32,9 @@ interface FlagshipHttpOptions {
32
32
  * Either the literal token, or a thunk resolved against the Worker `env` at
33
33
  * construction (`(env) => env.FLAGSHIP_TOKEN`) so the secret never has to be
34
34
  * inlined in source. A thunk resolving to anything but a non-empty string
35
- * throws: `Bearer undefined` would otherwise make every evaluation fall
36
- * silently back to its default.
35
+ * throws from the bind (`createFlags` logs it and evaluations fall back to
36
+ * their caller-supplied defaults): `Bearer undefined` would otherwise make
37
+ * every evaluation fall back with nothing said.
37
38
  */
38
39
  authToken?: ((env: Record<string, unknown>) => unknown) | string;
39
40
  /** Base URL override (only used with `appId`). */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lunora/flags",
3
- "version": "1.0.0-alpha.41",
3
+ "version": "1.0.0-alpha.43",
4
4
  "description": "OpenFeature-based feature flags for Lunora — ctx.flags, useFlag, and a first-class Cloudflare Flagship provider with any OpenFeature provider pluggable",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -1 +0,0 @@
1
- import{LunoraError as v}from"@lunora/errors";import{OpenFeature as m,ErrorCode as D}from"@openfeature/server-sdk";const F=/["\\\u0000-\u001F\uD800-\uDFFF]/,N=e=>F.test(e)?JSON.stringify(e):`"${e}"`,d=e=>{if(e===void 0)return"null";if(typeof e=="bigint")throw new TypeError("stableStringify: cannot use a bigint in a stable JSON cache key — pass it as a string, or use stableWireKey");if(typeof e=="number"){if(Number.isNaN(e))return"nan";if(e===1/0)return"inf";if(e===-1/0)return"-inf";if(Object.is(e,-0))return"-0"}if(typeof e=="string")return N(e);if(e===null||typeof e!="object")return JSON.stringify(e);if(Array.isArray(e)){let i="[";for(let a=0;a<e.length;a++)a>0&&(i+=","),i+=d(e[a]);return i+"]"}const g=Object.getPrototypeOf(e);if(g!==null&&g!==Object.prototype){const i=e.constructor?.name??"value";throw new TypeError(`stableStringify: cannot use a ${i} in a stable JSON cache key — only plain objects, arrays, and JSON primitives are supported (wire-typed values key via stableWireKey)`)}const c=e,s=Object.keys(c).sort();let r="{",f=!0;for(const i of s){const a=c[i];a!==void 0&&(f?f=!1:r+=",",r+=N(i),r+=":",r+=d(a))}return r+"}"},j="lunora";let h=new WeakMap,y=0;const A={},C=e=>Object.keys(e).length===0?A:e,M=(e,g,c)=>{const s=C(g);let r=h.get(e);r===void 0&&(r=new WeakMap,h.set(e,r));let f=r.get(s);if(f===void 0&&(y+=1,f={domain:y===1?j:`${j}-${String(y)}`},r.set(s,f)),f.client!==void 0)return f.client;const i=f,{domain:a}=i,{hooks:b,logger:o}=e,t=(async()=>{await m.setProviderAndWait(a,c());const n=m.getClient(a);return o&&n.setLogger(o),b&&b.length>0&&n.addHooks(...b),n})();return i.client=t,t.catch(()=>{i.client===t&&(i.client=void 0)}),t},L=async()=>{h=new WeakMap,y=0,await m.clearProviders()},$=(e,g,c,s)=>{const r=d([e,g,c]);return Object.keys(s).length===0?`${r}:{}`:`${r}:${d(s)}`},T=(e,g,c,s,r)=>{switch(g){case"boolean":return e.getBooleanDetails(c,s,r);case"number":return e.getNumberDetails(c,s,r);case"object":return e.getObjectDetails(c,s,r);case"string":return e.getStringDetails(c,s,r);default:throw new v("INTERNAL",`createFlags: unknown flag type "${g}"`)}},P=(e,g,c={})=>{const{provider:s,targetingKey:r}=c,f=()=>s?.()??e.provider(g);let i;try{i=typeof r=="function"?r():r}catch{i=void 0}const a=new Map,b=(o,t,n,l)=>{const E=i===void 0?{...l}:{targetingKey:i,...l},S=u=>({errorCode:D.GENERAL,errorMessage:u instanceof Error?u.message:String(u),flagKey:t,flagMetadata:{},reason:"ERROR",value:n}),O=()=>M(e,g,f).then(u=>T(u,o,t,n,E)).catch(S);let p;try{p=$(o,t,n,E)}catch{return O()}const w=a.get(p);if(w)return w;const k=O();return a.set(p,k),k};return{boolean:(o,t,n)=>b("boolean",o,t,n).then(l=>l.value),details:{boolean:(o,t,n)=>b("boolean",o,t,n),number:(o,t,n)=>b("number",o,t,n),object:(o,t,n)=>b("object",o,t,n),string:(o,t,n)=>b("string",o,t,n)},number:(o,t,n)=>b("number",o,t,n).then(l=>l.value),object:(o,t,n)=>b("object",o,t,n).then(l=>l.value),string:(o,t,n)=>b("string",o,t,n).then(l=>l.value)}};export{P as createFlags,L as resetFlags};