@drawbridge/drawbridge-utils 0.0.42 → 0.0.44

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.
@@ -1,7 +1,111 @@
1
- import {
2
- credit,
3
- debit
4
- } from "./chunk-JT2PAZAZ.js";
1
+ // lib/transactions.js
2
+ import { currentTraceId } from "@drawbridge/drawbridge-telemetry";
3
+ var insertTransaction = async ({
4
+ db,
5
+ user,
6
+ type,
7
+ category,
8
+ source,
9
+ amount,
10
+ _id,
11
+ stripeInvoiceId,
12
+ stripeEventId,
13
+ session,
14
+ ...rest
15
+ }) => {
16
+ var _a;
17
+ const beforeRaw = (_a = user == null ? void 0 : user.balance) == null ? void 0 : _a[type];
18
+ const balance = typeof beforeRaw === "number" ? { before: beforeRaw, after: beforeRaw + amount } : void 0;
19
+ const trace = currentTraceId();
20
+ await db.create({
21
+ authenticated: user,
22
+ collection: "transaction",
23
+ data: {
24
+ ..._id && { _id },
25
+ user: user == null ? void 0 : user.id,
26
+ type,
27
+ category,
28
+ source,
29
+ amount,
30
+ ...balance && { balance },
31
+ ...trace && { trace },
32
+ ...rest,
33
+ ...stripeInvoiceId && { stripeInvoiceId },
34
+ ...stripeEventId && { stripeEventId }
35
+ },
36
+ ...session && { options: { session } }
37
+ });
38
+ };
39
+ var credit = async ({
40
+ db,
41
+ user,
42
+ amount,
43
+ type,
44
+ category,
45
+ source,
46
+ _id,
47
+ stripeInvoiceId,
48
+ stripeEventId,
49
+ session,
50
+ ...rest
51
+ }) => {
52
+ if (!Number.isInteger(amount) || amount <= 0) {
53
+ throw new Error(`credit() requires a positive integer amount, got ${amount}`);
54
+ }
55
+ if (!type) {
56
+ throw new Error('credit() requires a type (e.g., "ai")');
57
+ }
58
+ if (!source) {
59
+ throw new Error('credit() requires a source ("system" | "admin" | "user")');
60
+ }
61
+ await insertTransaction({
62
+ db,
63
+ user,
64
+ type,
65
+ category,
66
+ source,
67
+ amount,
68
+ _id,
69
+ stripeInvoiceId,
70
+ stripeEventId,
71
+ session,
72
+ ...rest
73
+ });
74
+ };
75
+ var debit = async ({
76
+ db,
77
+ user,
78
+ amount,
79
+ type,
80
+ category,
81
+ source,
82
+ stripeInvoiceId,
83
+ stripeEventId,
84
+ session,
85
+ ...rest
86
+ }) => {
87
+ if (!Number.isInteger(amount) || amount <= 0) {
88
+ throw new Error(`debit() requires a positive integer amount, got ${amount}`);
89
+ }
90
+ if (!type) {
91
+ throw new Error('debit() requires a type (e.g., "ai")');
92
+ }
93
+ if (!source) {
94
+ throw new Error('debit() requires a source ("system" | "admin" | "user")');
95
+ }
96
+ await insertTransaction({
97
+ db,
98
+ user,
99
+ type,
100
+ category,
101
+ source,
102
+ amount: -amount,
103
+ stripeInvoiceId,
104
+ stripeEventId,
105
+ session,
106
+ ...rest
107
+ });
108
+ };
5
109
  export {
6
110
  credit,
7
111
  debit
package/dist/upload.cjs CHANGED
@@ -16,7 +16,7 @@ var __copyProps = (to, from, except, desc) => {
16
16
  };
17
17
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
18
 
19
- // upload.js
19
+ // lib/upload.js
20
20
  var upload_exports = {};
21
21
  __export(upload_exports, {
22
22
  allowedMimes: () => allowedMimes,
package/dist/upload.js CHANGED
@@ -1,4 +1,4 @@
1
- // upload.js
1
+ // lib/upload.js
2
2
  var allowedUploadTypes = {
3
3
  application: {
4
4
  pdf: "application/pdf"
package/package.json CHANGED
@@ -12,6 +12,17 @@
12
12
  "tsup": "8.5.1",
13
13
  "typescript": "5.9.3"
14
14
  },
15
+ "devDependencies": {
16
+ "@node-oauth/oauth2-server": "5.3.0"
17
+ },
18
+ "peerDependencies": {
19
+ "@node-oauth/oauth2-server": "^5.3.0"
20
+ },
21
+ "peerDependenciesMeta": {
22
+ "@node-oauth/oauth2-server": {
23
+ "optional": true
24
+ }
25
+ },
15
26
  "exports": {
16
27
  ".": {
17
28
  "types": "./dist/index.d.ts",
@@ -49,9 +60,14 @@
49
60
  "require": "./dist/nanoid.cjs"
50
61
  },
51
62
  "./oauth": {
52
- "types": "./dist/oauth.d.ts",
53
- "import": "./dist/oauth.js",
54
- "require": "./dist/oauth.cjs"
63
+ "types": "./dist/oauth/index.d.ts",
64
+ "import": "./dist/oauth/index.js",
65
+ "require": "./dist/oauth/index.cjs"
66
+ },
67
+ "./oauth/server": {
68
+ "types": "./dist/oauth/server.d.ts",
69
+ "import": "./dist/oauth/server.js",
70
+ "require": "./dist/oauth/server.cjs"
55
71
  },
56
72
  "./orders": {
57
73
  "types": "./dist/orders.d.ts",
@@ -114,5 +130,5 @@
114
130
  "build": "tsup && npm publish"
115
131
  },
116
132
  "types": "dist/index.d.ts",
117
- "version": "0.0.42"
133
+ "version": "0.0.44"
118
134
  }
@@ -1,38 +0,0 @@
1
- import {
2
- generate
3
- } from "./chunk-OS2AHUWX.js";
4
-
5
- // encrypt.js
6
- import crypto from "crypto";
7
- var ALGORITHM = "aes-256-gcm";
8
- var getKey = () => Buffer.from(process.env.ENCRYPT_CONNECTION_SECRET, "hex");
9
- var encrypt = (value) => {
10
- const iv = generate(12, null);
11
- const cipher = crypto.createCipheriv(ALGORITHM, getKey(), iv);
12
- const data = Buffer.concat([
13
- cipher.update(JSON.stringify(value), "utf8"),
14
- cipher.final()
15
- ]);
16
- const tag = cipher.getAuthTag();
17
- return [iv, tag, data].map((b) => b.toString("hex")).join(":");
18
- };
19
- var decrypt = (value) => {
20
- if (typeof value !== "string") return value;
21
- const [ivHex, tagHex, dataHex] = value.split(":");
22
- const decipher = crypto.createDecipheriv(
23
- ALGORITHM,
24
- getKey(),
25
- Buffer.from(ivHex, "hex")
26
- );
27
- decipher.setAuthTag(Buffer.from(tagHex, "hex"));
28
- const result = Buffer.concat([
29
- decipher.update(Buffer.from(dataHex, "hex")),
30
- decipher.final()
31
- ]);
32
- return JSON.parse(result.toString("utf8"));
33
- };
34
-
35
- export {
36
- encrypt,
37
- decrypt
38
- };
@@ -1,54 +0,0 @@
1
- // circuit.js
2
- var CLOSED = "CLOSED";
3
- var OPEN = "OPEN";
4
- var HALF_OPEN = "HALF_OPEN";
5
- var circuit = ({
6
- name,
7
- threshold = 5,
8
- timeout = 3e4
9
- }) => {
10
- let state = CLOSED;
11
- let failures = 0;
12
- let openedAt = null;
13
- const trip = () => {
14
- state = OPEN;
15
- openedAt = Date.now();
16
- console.error(`Circuit breaker OPEN: ${name}`);
17
- };
18
- const reset = () => {
19
- state = CLOSED;
20
- failures = 0;
21
- openedAt = null;
22
- console.log(`Circuit breaker CLOSED: ${name}`);
23
- };
24
- return async (fn) => {
25
- if (state === OPEN) {
26
- if (Date.now() - openedAt >= timeout) {
27
- state = HALF_OPEN;
28
- } else {
29
- const error = new Error(`${name} is temporarily unavailable`);
30
- error.status = 503;
31
- throw error;
32
- }
33
- }
34
- try {
35
- const result = await fn();
36
- if (state === HALF_OPEN) {
37
- reset();
38
- } else {
39
- failures = 0;
40
- }
41
- return result;
42
- } catch (error) {
43
- failures++;
44
- if (state === HALF_OPEN || failures >= threshold) {
45
- trip();
46
- }
47
- throw error;
48
- }
49
- };
50
- };
51
-
52
- export {
53
- circuit
54
- };
@@ -1,113 +0,0 @@
1
- // transactions.js
2
- import { currentTraceId } from "@drawbridge/drawbridge-telemetry";
3
- var insertTransaction = async ({
4
- db,
5
- user,
6
- type,
7
- category,
8
- source,
9
- amount,
10
- _id,
11
- stripeInvoiceId,
12
- stripeEventId,
13
- session,
14
- ...rest
15
- }) => {
16
- var _a;
17
- const beforeRaw = (_a = user == null ? void 0 : user.balance) == null ? void 0 : _a[type];
18
- const balance = typeof beforeRaw === "number" ? { before: beforeRaw, after: beforeRaw + amount } : void 0;
19
- const trace = currentTraceId();
20
- await db.create({
21
- authenticated: user,
22
- collection: "transaction",
23
- data: {
24
- ..._id && { _id },
25
- user: user == null ? void 0 : user.id,
26
- type,
27
- category,
28
- source,
29
- amount,
30
- ...balance && { balance },
31
- ...trace && { trace },
32
- ...rest,
33
- ...stripeInvoiceId && { stripeInvoiceId },
34
- ...stripeEventId && { stripeEventId }
35
- },
36
- ...session && { options: { session } }
37
- });
38
- };
39
- var credit = async ({
40
- db,
41
- user,
42
- amount,
43
- type,
44
- category,
45
- source,
46
- _id,
47
- stripeInvoiceId,
48
- stripeEventId,
49
- session,
50
- ...rest
51
- }) => {
52
- if (!Number.isInteger(amount) || amount <= 0) {
53
- throw new Error(`credit() requires a positive integer amount, got ${amount}`);
54
- }
55
- if (!type) {
56
- throw new Error('credit() requires a type (e.g., "ai")');
57
- }
58
- if (!source) {
59
- throw new Error('credit() requires a source ("system" | "admin" | "user")');
60
- }
61
- await insertTransaction({
62
- db,
63
- user,
64
- type,
65
- category,
66
- source,
67
- amount,
68
- _id,
69
- stripeInvoiceId,
70
- stripeEventId,
71
- session,
72
- ...rest
73
- });
74
- };
75
- var debit = async ({
76
- db,
77
- user,
78
- amount,
79
- type,
80
- category,
81
- source,
82
- stripeInvoiceId,
83
- stripeEventId,
84
- session,
85
- ...rest
86
- }) => {
87
- if (!Number.isInteger(amount) || amount <= 0) {
88
- throw new Error(`debit() requires a positive integer amount, got ${amount}`);
89
- }
90
- if (!type) {
91
- throw new Error('debit() requires a type (e.g., "ai")');
92
- }
93
- if (!source) {
94
- throw new Error('debit() requires a source ("system" | "admin" | "user")');
95
- }
96
- await insertTransaction({
97
- db,
98
- user,
99
- type,
100
- category,
101
- source,
102
- amount: -amount,
103
- stripeInvoiceId,
104
- stripeEventId,
105
- session,
106
- ...rest
107
- });
108
- };
109
-
110
- export {
111
- credit,
112
- debit
113
- };
@@ -1,27 +0,0 @@
1
- // token.js
2
- import crypto from "crypto";
3
- var generate = (bytes = 32, encoding = "base64url") => {
4
- const buf = crypto.randomBytes(bytes);
5
- return encoding ? buf.toString(encoding) : buf;
6
- };
7
- var hash = (value, key) => {
8
- if (!value) return null;
9
- if (key) {
10
- return crypto.createHmac("sha256", key).update(String(value)).digest("base64url");
11
- }
12
- return crypto.createHash("sha256").update(String(value)).digest("base64url");
13
- };
14
- var compare = (a, b) => {
15
- if (typeof a !== "string" || typeof b !== "string") return false;
16
- if (a.length !== b.length) return false;
17
- return crypto.timingSafeEqual(
18
- Buffer.from(a),
19
- Buffer.from(b)
20
- );
21
- };
22
-
23
- export {
24
- generate,
25
- hash,
26
- compare
27
- };