@dipswitch/sdk-node 0.1.0 → 0.2.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/README.md CHANGED
@@ -4,10 +4,8 @@ Dipswitch feature flags for Node. It keeps your application's rules in memory, s
4
4
 
5
5
  ## Install
6
6
 
7
- Until the packages are on npm, install the tarball:
8
-
9
7
  ```sh
10
- npm install ./dipswitch-sdk-node-0.1.0.tgz
8
+ npm install @dipswitch/sdk-node
11
9
  ```
12
10
 
13
11
  ## First use
package/dist/index.cjs CHANGED
@@ -19,6 +19,7 @@ function compile(ruleset) {
19
19
  flags.set(key, {
20
20
  enabled: flag.enabled,
21
21
  defaultOn: flag.defaultOn,
22
+ rollout: flag.rollout,
22
23
  users,
23
24
  segmentsOn: flag.segments.on.map(members),
24
25
  segmentsOff: flag.segments.off.map(members)
@@ -49,8 +50,12 @@ const defaultOn = {
49
50
  value: true,
50
51
  reason: "STATIC"
51
52
  };
53
+ const rolledOut = {
54
+ value: true,
55
+ reason: "SPLIT"
56
+ };
52
57
  /** The same evaluation, with the rule that decided the value. */
53
- function evaluateWithReason(ruleset, flagKey, userId) {
58
+ function evaluateWithReason(ruleset, flagKey, userId, workspaceId) {
54
59
  const flag = ruleset.flags.get(flagKey);
55
60
  if (flag === void 0) return void 0;
56
61
  if (!flag.enabled) return disabled;
@@ -60,7 +65,19 @@ function evaluateWithReason(ruleset, flagKey, userId) {
60
65
  if (flag.segmentsOff.some((segment) => segment.has(userId))) return targetedOff;
61
66
  if (flag.segmentsOn.some((segment) => segment.has(userId))) return targetedOn;
62
67
  }
63
- return flag.defaultOn ? defaultOn : defaultOff;
68
+ const fallback = flag.defaultOn ? defaultOn : defaultOff;
69
+ const rollout = flag.rollout;
70
+ if (!rollout) return fallback;
71
+ const key = rollout.key === "userId" ? userId : workspaceId;
72
+ if (typeof key !== "string" || !key || key === "." || key === ".." || key.includes("\0") || /\p{Surrogate}/u.test(key) || key.length > 256 && [...key].length > 256) return {
73
+ ...fallback,
74
+ metadata: { missingRolloutKey: rollout.key }
75
+ };
76
+ return (0, node_crypto.createHash)("sha256").update(JSON.stringify([
77
+ flagKey,
78
+ rollout.key,
79
+ key
80
+ ])).digest().readUInt32BE(0) * 100 < rollout.percentage * 2 ** 32 ? rolledOut : fallback;
64
81
  }
65
82
  // src/sse.ts
66
83
  var SseParser = class {
@@ -141,18 +158,18 @@ var Client = class {
141
158
  this.connect();
142
159
  });
143
160
  }
144
- isEnabled(flagKey, userId, defaultValue) {
145
- return this.getFlagDetails(flagKey, userId).value ?? defaultValue;
161
+ isEnabled(flagKey, userId, defaultValue, workspaceId) {
162
+ return this.getFlagDetails(flagKey, userId, workspaceId).value ?? defaultValue;
146
163
  }
147
164
  get snapshotVersion() {
148
165
  return this.held;
149
166
  }
150
- getFlagDetails(flagKey, userId) {
167
+ getFlagDetails(flagKey, userId, workspaceId) {
151
168
  if (!this.compiled) return {
152
169
  value: void 0,
153
170
  reason: "NOT_READY"
154
171
  };
155
- return evaluateWithReason(this.compiled, flagKey, userId ?? void 0) ?? {
172
+ return evaluateWithReason(this.compiled, flagKey, userId ?? void 0, workspaceId) ?? {
156
173
  value: void 0,
157
174
  reason: "FLAG_NOT_FOUND"
158
175
  };
@@ -351,6 +368,8 @@ function isRuleset(value) {
351
368
  for (const flag of Object.values(value.flags)) {
352
369
  if (!isRecord(flag) || typeof flag.enabled !== "boolean" || typeof flag.defaultOn !== "boolean") return false;
353
370
  if (!isOverrides(flag.users) || !isOverrides(flag.segments)) return false;
371
+ const rollout = flag.rollout;
372
+ if (rollout !== void 0 && (!isRecord(rollout) || rollout.key !== "userId" && rollout.key !== "workspaceId" || typeof rollout.percentage !== "number" || !Number.isInteger(rollout.percentage) || rollout.percentage < 0 || rollout.percentage > 100)) return false;
354
373
  }
355
374
  return Object.values(value.segments).every(isStrings);
356
375
  }
@@ -366,13 +385,14 @@ function retryAfterMs(value) {
366
385
  }
367
386
  // ../eval/dist/keys.js
368
387
  /** HMAC-SHA256 for the exact UTF-8 user id, shared by the signer and verifier. */
369
- function identityDigest(userId, identitySecret) {
370
- return (0, node_crypto.createHmac)("sha256", identitySecret).update(userId).digest();
388
+ function identityDigest(userId, identitySecret, workspaceId) {
389
+ const identity = workspaceId === void 0 ? userId ?? "" : `\0${JSON.stringify([userId ?? null, workspaceId])}`;
390
+ return (0, node_crypto.createHmac)("sha256", identitySecret).update(identity).digest();
371
391
  }
372
392
  // src/identity.ts
373
393
  /** Signs a user id on your backend with a client key's identity secret. */
374
- function signIdentity(userId, identitySecret) {
375
- return identityDigest(userId, identitySecret).toString("hex");
394
+ function signIdentity(userId, identitySecret, workspaceId) {
395
+ return identityDigest(userId, identitySecret, workspaceId).toString("hex");
376
396
  }
377
397
  exports.createClient = createClient;
378
398
  exports.signIdentity = signIdentity;
package/dist/index.d.cts CHANGED
@@ -1,7 +1,11 @@
1
+ type RolloutKey = 'userId' | 'workspaceId';
1
2
  /** A local SDK lookup, which can distinguish an empty cache from a missing key. */
2
3
  type FlagDetails<Reason extends string> = {
3
4
  readonly value: boolean;
4
5
  readonly reason: Reason;
6
+ readonly metadata?: {
7
+ readonly missingRolloutKey: RolloutKey;
8
+ };
5
9
  } | {
6
10
  readonly value: undefined;
7
11
  readonly reason: 'NOT_READY' | 'FLAG_NOT_FOUND';
@@ -25,9 +29,9 @@ interface DipswitchClient {
25
29
  * SDK has no data for the flag: not loaded yet, deleted, or not assigned to
26
30
  * this application. Without a user id (`undefined`), no override applies.
27
31
  */
28
- isEnabled(flagKey: string, userId: string | undefined, defaultValue: boolean): boolean;
32
+ isEnabled(flagKey: string, userId: string | undefined, defaultValue: boolean, workspaceId?: string): boolean;
29
33
  /** The current value and its reason, or why no value can be read. */
30
- getFlagDetails(flagKey: string, userId?: string): FlagDetails<'STATIC' | 'TARGETING_MATCH' | 'DISABLED'>;
34
+ getFlagDetails(flagKey: string, userId?: string, workspaceId?: string): FlagDetails<'STATIC' | 'TARGETING_MATCH' | 'DISABLED' | 'SPLIT'>;
31
35
  /** Calls `listener` whenever newer flags have arrived after `ready`. Returns what removes it. */
32
36
  onChange(listener: () => void): () => void;
33
37
  /** Stops every request and timer. The values held stay readable. */
@@ -35,5 +39,5 @@ interface DipswitchClient {
35
39
  }
36
40
  export declare function createClient(options: ClientOptions): DipswitchClient;
37
41
  /** Signs a user id on your backend with a client key's identity secret. */
38
- export declare function signIdentity(userId: string, identitySecret: string): string;
42
+ export declare function signIdentity(userId: string | undefined, identitySecret: string, workspaceId?: string): string;
39
43
  export type { ClientOptions, DipswitchClient };
package/dist/index.d.mts CHANGED
@@ -1,7 +1,11 @@
1
+ type RolloutKey = 'userId' | 'workspaceId';
1
2
  /** A local SDK lookup, which can distinguish an empty cache from a missing key. */
2
3
  type FlagDetails<Reason extends string> = {
3
4
  readonly value: boolean;
4
5
  readonly reason: Reason;
6
+ readonly metadata?: {
7
+ readonly missingRolloutKey: RolloutKey;
8
+ };
5
9
  } | {
6
10
  readonly value: undefined;
7
11
  readonly reason: 'NOT_READY' | 'FLAG_NOT_FOUND';
@@ -25,9 +29,9 @@ interface DipswitchClient {
25
29
  * SDK has no data for the flag: not loaded yet, deleted, or not assigned to
26
30
  * this application. Without a user id (`undefined`), no override applies.
27
31
  */
28
- isEnabled(flagKey: string, userId: string | undefined, defaultValue: boolean): boolean;
32
+ isEnabled(flagKey: string, userId: string | undefined, defaultValue: boolean, workspaceId?: string): boolean;
29
33
  /** The current value and its reason, or why no value can be read. */
30
- getFlagDetails(flagKey: string, userId?: string): FlagDetails<'STATIC' | 'TARGETING_MATCH' | 'DISABLED'>;
34
+ getFlagDetails(flagKey: string, userId?: string, workspaceId?: string): FlagDetails<'STATIC' | 'TARGETING_MATCH' | 'DISABLED' | 'SPLIT'>;
31
35
  /** Calls `listener` whenever newer flags have arrived after `ready`. Returns what removes it. */
32
36
  onChange(listener: () => void): () => void;
33
37
  /** Stops every request and timer. The values held stay readable. */
@@ -35,5 +39,5 @@ interface DipswitchClient {
35
39
  }
36
40
  export declare function createClient(options: ClientOptions): DipswitchClient;
37
41
  /** Signs a user id on your backend with a client key's identity secret. */
38
- export declare function signIdentity(userId: string, identitySecret: string): string;
42
+ export declare function signIdentity(userId: string | undefined, identitySecret: string, workspaceId?: string): string;
39
43
  export type { ClientOptions, DipswitchClient };
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { createHmac } from "node:crypto";
1
+ import { createHash, createHmac } from "node:crypto";
2
2
  // ../eval/dist/evaluate.js
3
3
  /**
4
4
  * Prepares a ruleset for `evaluate`. Expects a ruleset the schema accepts;
@@ -18,6 +18,7 @@ function compile(ruleset) {
18
18
  flags.set(key, {
19
19
  enabled: flag.enabled,
20
20
  defaultOn: flag.defaultOn,
21
+ rollout: flag.rollout,
21
22
  users,
22
23
  segmentsOn: flag.segments.on.map(members),
23
24
  segmentsOff: flag.segments.off.map(members)
@@ -48,8 +49,12 @@ const defaultOn = {
48
49
  value: true,
49
50
  reason: "STATIC"
50
51
  };
52
+ const rolledOut = {
53
+ value: true,
54
+ reason: "SPLIT"
55
+ };
51
56
  /** The same evaluation, with the rule that decided the value. */
52
- function evaluateWithReason(ruleset, flagKey, userId) {
57
+ function evaluateWithReason(ruleset, flagKey, userId, workspaceId) {
53
58
  const flag = ruleset.flags.get(flagKey);
54
59
  if (flag === void 0) return void 0;
55
60
  if (!flag.enabled) return disabled;
@@ -59,7 +64,19 @@ function evaluateWithReason(ruleset, flagKey, userId) {
59
64
  if (flag.segmentsOff.some((segment) => segment.has(userId))) return targetedOff;
60
65
  if (flag.segmentsOn.some((segment) => segment.has(userId))) return targetedOn;
61
66
  }
62
- return flag.defaultOn ? defaultOn : defaultOff;
67
+ const fallback = flag.defaultOn ? defaultOn : defaultOff;
68
+ const rollout = flag.rollout;
69
+ if (!rollout) return fallback;
70
+ const key = rollout.key === "userId" ? userId : workspaceId;
71
+ if (typeof key !== "string" || !key || key === "." || key === ".." || key.includes("\0") || /\p{Surrogate}/u.test(key) || key.length > 256 && [...key].length > 256) return {
72
+ ...fallback,
73
+ metadata: { missingRolloutKey: rollout.key }
74
+ };
75
+ return createHash("sha256").update(JSON.stringify([
76
+ flagKey,
77
+ rollout.key,
78
+ key
79
+ ])).digest().readUInt32BE(0) * 100 < rollout.percentage * 2 ** 32 ? rolledOut : fallback;
63
80
  }
64
81
  // src/sse.ts
65
82
  var SseParser = class {
@@ -140,18 +157,18 @@ var Client = class {
140
157
  this.connect();
141
158
  });
142
159
  }
143
- isEnabled(flagKey, userId, defaultValue) {
144
- return this.getFlagDetails(flagKey, userId).value ?? defaultValue;
160
+ isEnabled(flagKey, userId, defaultValue, workspaceId) {
161
+ return this.getFlagDetails(flagKey, userId, workspaceId).value ?? defaultValue;
145
162
  }
146
163
  get snapshotVersion() {
147
164
  return this.held;
148
165
  }
149
- getFlagDetails(flagKey, userId) {
166
+ getFlagDetails(flagKey, userId, workspaceId) {
150
167
  if (!this.compiled) return {
151
168
  value: void 0,
152
169
  reason: "NOT_READY"
153
170
  };
154
- return evaluateWithReason(this.compiled, flagKey, userId ?? void 0) ?? {
171
+ return evaluateWithReason(this.compiled, flagKey, userId ?? void 0, workspaceId) ?? {
155
172
  value: void 0,
156
173
  reason: "FLAG_NOT_FOUND"
157
174
  };
@@ -350,6 +367,8 @@ function isRuleset(value) {
350
367
  for (const flag of Object.values(value.flags)) {
351
368
  if (!isRecord(flag) || typeof flag.enabled !== "boolean" || typeof flag.defaultOn !== "boolean") return false;
352
369
  if (!isOverrides(flag.users) || !isOverrides(flag.segments)) return false;
370
+ const rollout = flag.rollout;
371
+ if (rollout !== void 0 && (!isRecord(rollout) || rollout.key !== "userId" && rollout.key !== "workspaceId" || typeof rollout.percentage !== "number" || !Number.isInteger(rollout.percentage) || rollout.percentage < 0 || rollout.percentage > 100)) return false;
353
372
  }
354
373
  return Object.values(value.segments).every(isStrings);
355
374
  }
@@ -365,12 +384,13 @@ function retryAfterMs(value) {
365
384
  }
366
385
  // ../eval/dist/keys.js
367
386
  /** HMAC-SHA256 for the exact UTF-8 user id, shared by the signer and verifier. */
368
- function identityDigest(userId, identitySecret) {
369
- return createHmac("sha256", identitySecret).update(userId).digest();
387
+ function identityDigest(userId, identitySecret, workspaceId) {
388
+ const identity = workspaceId === void 0 ? userId ?? "" : `\0${JSON.stringify([userId ?? null, workspaceId])}`;
389
+ return createHmac("sha256", identitySecret).update(identity).digest();
370
390
  }
371
391
  // src/identity.ts
372
392
  /** Signs a user id on your backend with a client key's identity secret. */
373
- function signIdentity(userId, identitySecret) {
374
- return identityDigest(userId, identitySecret).toString("hex");
393
+ function signIdentity(userId, identitySecret, workspaceId) {
394
+ return identityDigest(userId, identitySecret, workspaceId).toString("hex");
375
395
  }
376
396
  export { createClient, signIdentity };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dipswitch/sdk-node",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Dipswitch feature flags for Node: the rules in memory, checks with no network call, changes within seconds.",
5
5
  "license": "MIT",
6
6
  "type": "module",