@drawbridge/drawbridge-utils 0.0.65 → 0.0.67
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/dist/ai.cjs +45 -49
- package/dist/ai.d.cts +7 -161
- package/dist/ai.d.ts +7 -161
- package/dist/ai.js +44 -42
- package/dist/billing.cjs +373 -0
- package/dist/billing.d.cts +300 -0
- package/dist/billing.d.ts +300 -0
- package/dist/billing.js +343 -0
- package/dist/index.cjs +22 -61
- package/dist/index.d.cts +2 -122
- package/dist/index.d.ts +2 -122
- package/dist/index.js +22 -60
- package/dist/plans.d.cts +1 -0
- package/dist/plans.d.ts +1 -0
- package/dist/usage.cjs +91 -0
- package/dist/usage.d.cts +129 -0
- package/dist/usage.d.ts +129 -0
- package/dist/usage.js +65 -0
- package/package.json +6 -1
package/dist/usage.d.cts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// Usage helpers — the per-org `usage` doc totals and the append-only `action`
|
|
2
|
+
// ledger. These are the low-level usage primitives; billing.action.* (lib/
|
|
3
|
+
// billing.js) is the public surface. Kept separate from billing so the generic
|
|
4
|
+
// usage-totals $inc (used for non-billing totals like revenue/submissions/
|
|
5
|
+
// entries too) doesn't live under the billing namespace. Re-exported from
|
|
6
|
+
// index.js for backward compatibility with existing import sites.
|
|
7
|
+
|
|
8
|
+
// Centralized $inc helper for the per-org usage doc. Replaces inline
|
|
9
|
+
// $inc-with-bypass-validation patterns in drawbridge-api + drawbridge-sync.
|
|
10
|
+
//
|
|
11
|
+
// MongoDB's $inc with 0 / NaN / undefined / null writes the value into the
|
|
12
|
+
// field (zeroing or NaN-ing it) instead of no-op'ing — so we filter the
|
|
13
|
+
// $inc object to keep only finite, non-zero values and callers don't have
|
|
14
|
+
// to remember the guard.
|
|
15
|
+
//
|
|
16
|
+
// `session` is forwarded when present so callers inside a transaction get
|
|
17
|
+
// atomic writes; standalone callers omit it.
|
|
18
|
+
const incrementUsageTotals = async ({ controller, usageId, $inc, session }) => {
|
|
19
|
+
|
|
20
|
+
if( ! usageId || ! $inc ) return;
|
|
21
|
+
|
|
22
|
+
const safeInc = Object.fromEntries(
|
|
23
|
+
Object.entries( $inc ).filter( ([ , value ]) => {
|
|
24
|
+
|
|
25
|
+
const number = Number( value );
|
|
26
|
+
|
|
27
|
+
return Number.isFinite( number ) && number !== 0;
|
|
28
|
+
|
|
29
|
+
})
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
if( Object.keys( safeInc ).length === 0 ) return;
|
|
33
|
+
|
|
34
|
+
return controller.update({
|
|
35
|
+
collection : 'usage',
|
|
36
|
+
data : { $inc : safeInc },
|
|
37
|
+
options : {
|
|
38
|
+
bypassDocumentValidation : true,
|
|
39
|
+
...( session && { session })
|
|
40
|
+
},
|
|
41
|
+
query : { id : usageId }
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// True for a MongoDB duplicate-key error (unique-index violation).
|
|
47
|
+
const isDuplicateKey = ( error ) =>
|
|
48
|
+
error?.code === 11000
|
|
49
|
+
|| error?.code === '11000'
|
|
50
|
+
|| /E11000|duplicate key/i.test( error?.message || '' );
|
|
51
|
+
|
|
52
|
+
// Record a billable action as a row in the append-only `action` ledger — the
|
|
53
|
+
// source of truth for usage billing (summed per period at changeover).
|
|
54
|
+
//
|
|
55
|
+
// Idempotent via an UPSERT on the unique { usage, key }: a duplicate event (the
|
|
56
|
+
// segment realtime path AND the sweep firing for the same membership, or a retry)
|
|
57
|
+
// matches the existing row and no-ops — no duplicate-key error — so this is safe
|
|
58
|
+
// to call INSIDE a caller's transaction (a thrown E11000 would abort the whole
|
|
59
|
+
// transaction). The display cache `usage.totals.actions` is bumped ONLY when the
|
|
60
|
+
// upsert actually inserts a new row, so a duplicate never double-counts.
|
|
61
|
+
//
|
|
62
|
+
// Pass `session` to make the ledger row + cache bump atomic with the caller's own
|
|
63
|
+
// writes (e.g. the membership writes in a segment-sweep chunk, or the submission
|
|
64
|
+
// docs) — all-or-nothing. Without a session it runs standalone.
|
|
65
|
+
// key deterministic per source event, e.g. 'segment.join:<contact>:<segment>'
|
|
66
|
+
// units billable units (default 1; N for batch, e.g. accepted entries)
|
|
67
|
+
// meta attribution ids { campaign, page, submission, segment, workflow, step, ... }
|
|
68
|
+
const recordAction = async ({ controller, organization, usageId, type, units = 1, key, meta = {}, traceId, session }) => {
|
|
69
|
+
|
|
70
|
+
if( ! controller || ! organization || ! usageId || ! type || ! key ) return;
|
|
71
|
+
|
|
72
|
+
const amount = Number( units );
|
|
73
|
+
|
|
74
|
+
if( ! Number.isFinite( amount ) || amount === 0 ) return;
|
|
75
|
+
|
|
76
|
+
let inserted = false;
|
|
77
|
+
|
|
78
|
+
try {
|
|
79
|
+
|
|
80
|
+
const { lastErrorObject } = await controller.update({
|
|
81
|
+
collection : 'action',
|
|
82
|
+
data : {
|
|
83
|
+
$setOnInsert : {
|
|
84
|
+
organization,
|
|
85
|
+
usage : usageId,
|
|
86
|
+
type,
|
|
87
|
+
units : amount,
|
|
88
|
+
key,
|
|
89
|
+
meta,
|
|
90
|
+
...( traceId && { traceId })
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
options : {
|
|
94
|
+
bypassDocumentValidation : true,
|
|
95
|
+
includeResultMetadata : true,
|
|
96
|
+
upsert : true,
|
|
97
|
+
...( session && { session })
|
|
98
|
+
},
|
|
99
|
+
query : {
|
|
100
|
+
key,
|
|
101
|
+
usage : usageId
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
inserted = Boolean( lastErrorObject?.upserted );
|
|
106
|
+
|
|
107
|
+
} catch ( error ) {
|
|
108
|
+
|
|
109
|
+
// A concurrent upsert of the same { usage, key } can still race to E11000.
|
|
110
|
+
// Standalone: the other writer recorded it — skip. Inside a transaction:
|
|
111
|
+
// the txn is already aborted, so re-throw to let the caller retry the whole
|
|
112
|
+
// unit (which then sees the row and no-ops).
|
|
113
|
+
if( isDuplicateKey( error ) && ! session ) return;
|
|
114
|
+
|
|
115
|
+
throw error;
|
|
116
|
+
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Only a genuinely new row bumps the display cache (a matched duplicate must
|
|
120
|
+
// not double-count). Joined to the caller's transaction when a session is given.
|
|
121
|
+
if( inserted ){
|
|
122
|
+
|
|
123
|
+
await incrementUsageTotals({ controller, usageId, $inc : { 'totals.actions' : amount }, session });
|
|
124
|
+
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
export { incrementUsageTotals, isDuplicateKey, recordAction };
|
package/dist/usage.d.ts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// Usage helpers — the per-org `usage` doc totals and the append-only `action`
|
|
2
|
+
// ledger. These are the low-level usage primitives; billing.action.* (lib/
|
|
3
|
+
// billing.js) is the public surface. Kept separate from billing so the generic
|
|
4
|
+
// usage-totals $inc (used for non-billing totals like revenue/submissions/
|
|
5
|
+
// entries too) doesn't live under the billing namespace. Re-exported from
|
|
6
|
+
// index.js for backward compatibility with existing import sites.
|
|
7
|
+
|
|
8
|
+
// Centralized $inc helper for the per-org usage doc. Replaces inline
|
|
9
|
+
// $inc-with-bypass-validation patterns in drawbridge-api + drawbridge-sync.
|
|
10
|
+
//
|
|
11
|
+
// MongoDB's $inc with 0 / NaN / undefined / null writes the value into the
|
|
12
|
+
// field (zeroing or NaN-ing it) instead of no-op'ing — so we filter the
|
|
13
|
+
// $inc object to keep only finite, non-zero values and callers don't have
|
|
14
|
+
// to remember the guard.
|
|
15
|
+
//
|
|
16
|
+
// `session` is forwarded when present so callers inside a transaction get
|
|
17
|
+
// atomic writes; standalone callers omit it.
|
|
18
|
+
const incrementUsageTotals = async ({ controller, usageId, $inc, session }) => {
|
|
19
|
+
|
|
20
|
+
if( ! usageId || ! $inc ) return;
|
|
21
|
+
|
|
22
|
+
const safeInc = Object.fromEntries(
|
|
23
|
+
Object.entries( $inc ).filter( ([ , value ]) => {
|
|
24
|
+
|
|
25
|
+
const number = Number( value );
|
|
26
|
+
|
|
27
|
+
return Number.isFinite( number ) && number !== 0;
|
|
28
|
+
|
|
29
|
+
})
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
if( Object.keys( safeInc ).length === 0 ) return;
|
|
33
|
+
|
|
34
|
+
return controller.update({
|
|
35
|
+
collection : 'usage',
|
|
36
|
+
data : { $inc : safeInc },
|
|
37
|
+
options : {
|
|
38
|
+
bypassDocumentValidation : true,
|
|
39
|
+
...( session && { session })
|
|
40
|
+
},
|
|
41
|
+
query : { id : usageId }
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// True for a MongoDB duplicate-key error (unique-index violation).
|
|
47
|
+
const isDuplicateKey = ( error ) =>
|
|
48
|
+
error?.code === 11000
|
|
49
|
+
|| error?.code === '11000'
|
|
50
|
+
|| /E11000|duplicate key/i.test( error?.message || '' );
|
|
51
|
+
|
|
52
|
+
// Record a billable action as a row in the append-only `action` ledger — the
|
|
53
|
+
// source of truth for usage billing (summed per period at changeover).
|
|
54
|
+
//
|
|
55
|
+
// Idempotent via an UPSERT on the unique { usage, key }: a duplicate event (the
|
|
56
|
+
// segment realtime path AND the sweep firing for the same membership, or a retry)
|
|
57
|
+
// matches the existing row and no-ops — no duplicate-key error — so this is safe
|
|
58
|
+
// to call INSIDE a caller's transaction (a thrown E11000 would abort the whole
|
|
59
|
+
// transaction). The display cache `usage.totals.actions` is bumped ONLY when the
|
|
60
|
+
// upsert actually inserts a new row, so a duplicate never double-counts.
|
|
61
|
+
//
|
|
62
|
+
// Pass `session` to make the ledger row + cache bump atomic with the caller's own
|
|
63
|
+
// writes (e.g. the membership writes in a segment-sweep chunk, or the submission
|
|
64
|
+
// docs) — all-or-nothing. Without a session it runs standalone.
|
|
65
|
+
// key deterministic per source event, e.g. 'segment.join:<contact>:<segment>'
|
|
66
|
+
// units billable units (default 1; N for batch, e.g. accepted entries)
|
|
67
|
+
// meta attribution ids { campaign, page, submission, segment, workflow, step, ... }
|
|
68
|
+
const recordAction = async ({ controller, organization, usageId, type, units = 1, key, meta = {}, traceId, session }) => {
|
|
69
|
+
|
|
70
|
+
if( ! controller || ! organization || ! usageId || ! type || ! key ) return;
|
|
71
|
+
|
|
72
|
+
const amount = Number( units );
|
|
73
|
+
|
|
74
|
+
if( ! Number.isFinite( amount ) || amount === 0 ) return;
|
|
75
|
+
|
|
76
|
+
let inserted = false;
|
|
77
|
+
|
|
78
|
+
try {
|
|
79
|
+
|
|
80
|
+
const { lastErrorObject } = await controller.update({
|
|
81
|
+
collection : 'action',
|
|
82
|
+
data : {
|
|
83
|
+
$setOnInsert : {
|
|
84
|
+
organization,
|
|
85
|
+
usage : usageId,
|
|
86
|
+
type,
|
|
87
|
+
units : amount,
|
|
88
|
+
key,
|
|
89
|
+
meta,
|
|
90
|
+
...( traceId && { traceId })
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
options : {
|
|
94
|
+
bypassDocumentValidation : true,
|
|
95
|
+
includeResultMetadata : true,
|
|
96
|
+
upsert : true,
|
|
97
|
+
...( session && { session })
|
|
98
|
+
},
|
|
99
|
+
query : {
|
|
100
|
+
key,
|
|
101
|
+
usage : usageId
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
inserted = Boolean( lastErrorObject?.upserted );
|
|
106
|
+
|
|
107
|
+
} catch ( error ) {
|
|
108
|
+
|
|
109
|
+
// A concurrent upsert of the same { usage, key } can still race to E11000.
|
|
110
|
+
// Standalone: the other writer recorded it — skip. Inside a transaction:
|
|
111
|
+
// the txn is already aborted, so re-throw to let the caller retry the whole
|
|
112
|
+
// unit (which then sees the row and no-ops).
|
|
113
|
+
if( isDuplicateKey( error ) && ! session ) return;
|
|
114
|
+
|
|
115
|
+
throw error;
|
|
116
|
+
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Only a genuinely new row bumps the display cache (a matched duplicate must
|
|
120
|
+
// not double-count). Joined to the caller's transaction when a session is given.
|
|
121
|
+
if( inserted ){
|
|
122
|
+
|
|
123
|
+
await incrementUsageTotals({ controller, usageId, $inc : { 'totals.actions' : amount }, session });
|
|
124
|
+
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
export { incrementUsageTotals, isDuplicateKey, recordAction };
|
package/dist/usage.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// lib/usage.js
|
|
2
|
+
var incrementUsageTotals = async ({ controller, usageId, $inc, session }) => {
|
|
3
|
+
if (!usageId || !$inc) return;
|
|
4
|
+
const safeInc = Object.fromEntries(
|
|
5
|
+
Object.entries($inc).filter(([, value]) => {
|
|
6
|
+
const number = Number(value);
|
|
7
|
+
return Number.isFinite(number) && number !== 0;
|
|
8
|
+
})
|
|
9
|
+
);
|
|
10
|
+
if (Object.keys(safeInc).length === 0) return;
|
|
11
|
+
return controller.update({
|
|
12
|
+
collection: "usage",
|
|
13
|
+
data: { $inc: safeInc },
|
|
14
|
+
options: {
|
|
15
|
+
bypassDocumentValidation: true,
|
|
16
|
+
...session && { session }
|
|
17
|
+
},
|
|
18
|
+
query: { id: usageId }
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
var isDuplicateKey = (error) => (error == null ? void 0 : error.code) === 11e3 || (error == null ? void 0 : error.code) === "11000" || /E11000|duplicate key/i.test((error == null ? void 0 : error.message) || "");
|
|
22
|
+
var recordAction = async ({ controller, organization, usageId, type, units = 1, key, meta = {}, traceId, session }) => {
|
|
23
|
+
if (!controller || !organization || !usageId || !type || !key) return;
|
|
24
|
+
const amount = Number(units);
|
|
25
|
+
if (!Number.isFinite(amount) || amount === 0) return;
|
|
26
|
+
let inserted = false;
|
|
27
|
+
try {
|
|
28
|
+
const { lastErrorObject } = await controller.update({
|
|
29
|
+
collection: "action",
|
|
30
|
+
data: {
|
|
31
|
+
$setOnInsert: {
|
|
32
|
+
organization,
|
|
33
|
+
usage: usageId,
|
|
34
|
+
type,
|
|
35
|
+
units: amount,
|
|
36
|
+
key,
|
|
37
|
+
meta,
|
|
38
|
+
...traceId && { traceId }
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
options: {
|
|
42
|
+
bypassDocumentValidation: true,
|
|
43
|
+
includeResultMetadata: true,
|
|
44
|
+
upsert: true,
|
|
45
|
+
...session && { session }
|
|
46
|
+
},
|
|
47
|
+
query: {
|
|
48
|
+
key,
|
|
49
|
+
usage: usageId
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
inserted = Boolean(lastErrorObject == null ? void 0 : lastErrorObject.upserted);
|
|
53
|
+
} catch (error) {
|
|
54
|
+
if (isDuplicateKey(error) && !session) return;
|
|
55
|
+
throw error;
|
|
56
|
+
}
|
|
57
|
+
if (inserted) {
|
|
58
|
+
await incrementUsageTotals({ controller, usageId, $inc: { "totals.actions": amount }, session });
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
export {
|
|
62
|
+
incrementUsageTotals,
|
|
63
|
+
isDuplicateKey,
|
|
64
|
+
recordAction
|
|
65
|
+
};
|
package/package.json
CHANGED
|
@@ -37,6 +37,11 @@
|
|
|
37
37
|
"import": "./dist/ai.js",
|
|
38
38
|
"require": "./dist/ai.cjs"
|
|
39
39
|
},
|
|
40
|
+
"./billing": {
|
|
41
|
+
"types": "./dist/billing.d.ts",
|
|
42
|
+
"import": "./dist/billing.js",
|
|
43
|
+
"require": "./dist/billing.cjs"
|
|
44
|
+
},
|
|
40
45
|
"./color": {
|
|
41
46
|
"types": "./dist/color.d.ts",
|
|
42
47
|
"import": "./dist/color.js",
|
|
@@ -163,5 +168,5 @@
|
|
|
163
168
|
"build": "tsup && npm publish"
|
|
164
169
|
},
|
|
165
170
|
"types": "dist/index.d.ts",
|
|
166
|
-
"version": "0.0.
|
|
171
|
+
"version": "0.0.67"
|
|
167
172
|
}
|