@midsummer-ai/sdk 0.1.0-staging.3650 → 0.1.0-staging.3656

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.
Files changed (44) hide show
  1. package/THIRD-PARTY-NOTICES +1279 -0
  2. package/auth/client.d.mts +222 -18
  3. package/auth/client.mjs +321 -40
  4. package/auth/contract.d.mts +10 -1
  5. package/auth/contract.mjs +347 -30
  6. package/auth/files.d.mts +2 -28
  7. package/auth/files.mjs +2 -231
  8. package/auth/impersonate.d.mts +33 -0
  9. package/auth/impersonate.mjs +42 -0
  10. package/auth/import.d.mts +71 -0
  11. package/auth/import.mjs +195 -0
  12. package/auth/index.d.mts +5 -198
  13. package/auth/index.mjs +3 -2
  14. package/auth/mail.d.mts +23 -5
  15. package/auth/mail.mjs +93 -39
  16. package/auth/password.d.mts +10 -2
  17. package/auth/password.mjs +14 -2
  18. package/auth/react.d.mts +86 -0
  19. package/auth/react.mjs +865 -0
  20. package/auth/stand-in.d.mts +14 -3
  21. package/auth/stand-in.mjs +162 -29
  22. package/auth-CND9RW17.mjs +1819 -0
  23. package/authorize-BzDrbYtI-0gOzDrp2.mjs +18783 -0
  24. package/{client-C7QtAX5u.mjs → connection-C_JSWUrt.mjs} +3 -668
  25. package/database/client.mjs +665 -1
  26. package/dist-DXtY-DJA.mjs +343 -0
  27. package/engine-BhA0XgI7.mjs +48423 -0
  28. package/esm-E3EvzsGX.mjs +1518 -0
  29. package/factory-9jfDGyc1.mjs +1138 -0
  30. package/files-C-FvBxT4.d.mts +75 -0
  31. package/files-DAFOuID_.mjs +692 -0
  32. package/get-id-field-vhOArqXN.mjs +1455 -0
  33. package/index-CsgQ_ouh.d.mts +246 -0
  34. package/package.json +24 -7
  35. package/qr-C1mfuCT9.mjs +1163 -0
  36. package/request-state-BaLB2Hrt.mjs +85 -0
  37. package/rolldown-runtime-CL6CfywC.mjs +36 -0
  38. package/schemas-DTJTcCdK.mjs +6046 -0
  39. package/settings-si8iBtwc.d.mts +47 -0
  40. package/store-CeIGY-xT.d.mts +16 -0
  41. package/{store-dsVD9VnM.mjs → store-CsBhnabU.mjs} +1 -1
  42. package/{tabs-C85sXTwS.mjs → tabs-gHJHkhm2.mjs} +1 -1
  43. package/auth-BM6vB3XD.mjs +0 -754
  44. package/store-gCz6XVaG.d.mts +0 -15
@@ -1,4 +1,4 @@
1
- import { t as AuthStore } from "../store-gCz6XVaG.mjs";
1
+ import { n as AuthStore } from "../store-CeIGY-xT.mjs";
2
2
  import { SignInMail } from "./mail.mjs";
3
3
  //#region src/auth/stand-in.d.ts
4
4
  interface StandIn {
@@ -7,9 +7,20 @@ interface StandIn {
7
7
  readonly outbox: ReadonlyArray<SignInMail>;
8
8
  /** How many calls reached the rows, of any kind. */
9
9
  calls(): number;
10
- /** The rows of a table, as kept. */
10
+ /** The rows of a table, as kept, oldest first. */
11
11
  rows(table: string): ReadonlyArray<Record<string, unknown>>;
12
12
  }
13
+ interface Clause {
14
+ readonly field: string;
15
+ readonly value: unknown;
16
+ readonly operator?: string;
17
+ readonly connector?: string;
18
+ readonly mode?: string;
19
+ }
20
+ /** Fields kept in lower case, so a match that ignores case reads their index, as `db/auth.ts` keeps them. */
21
+ declare const LOWER: Readonly<Record<string, ReadonlyArray<string>>>;
22
+ /** Whether `db/auth.ts` reads a clause through an index, as its `readOf` decides. */
23
+ declare function indexed(lookups: ReadonlyArray<string>, model: string, clause: Clause): boolean;
13
24
  declare function standIn(): StandIn;
14
25
  //#endregion
15
- export { StandIn, standIn };
26
+ export { LOWER, StandIn, indexed, standIn };
package/auth/stand-in.mjs CHANGED
@@ -1,41 +1,174 @@
1
- import { memoryAdapter } from "better-auth/adapters/memory";
2
- import { getAuthTables } from "better-auth/db";
1
+ import { r as authTables } from "../files-DAFOuID_.mjs";
3
2
  //#region src/auth/stand-in.ts
4
- /**
5
- * Sign-in's Stand-in: its rows in memory and its mail kept, for `midsummer dev` without a database and for
6
- * tests. It is better-auth's own memory store, so sign-in behaves as it does over the Project's Database,
7
- * and passes the contract (`@midsummer-ai/sdk/auth/contract`) except durability.
8
- *
9
- * const stand = standIn()
10
- * const auth = createAuth({ store: stand.store, mail: stand.mail })
11
- * stand.outbox // every sign-in mail, newest last
12
- * stand.reads() // how many reads and writes reached the rows
13
- */
3
+ const text = (value) => typeof value === "string" ? value.toLowerCase() : value;
4
+ /** Whether one clause holds for a row, as `db/auth.ts` decides it. */
5
+ function holds(row, clause) {
6
+ const { field, value, operator = "eq", mode } = clause;
7
+ const have = row[field], loose = mode === "insensitive";
8
+ const same = (a, b) => loose ? text(a) === text(b) : a === b;
9
+ const ordered = value != null && have != null;
10
+ switch (operator) {
11
+ case "eq": return value === null ? have == null : same(have, value);
12
+ case "ne": return value === null ? have != null : !same(have, value);
13
+ case "in": return Array.isArray(value) && value.some((one) => same(have, one));
14
+ case "not_in": return Array.isArray(value) && !value.some((one) => same(have, one));
15
+ case "lt": return ordered && have < value;
16
+ case "lte": return ordered && have <= value;
17
+ case "gt": return ordered && have > value;
18
+ case "gte": return ordered && have >= value;
19
+ case "contains": return typeof have === "string" && typeof value === "string" && (loose ? have.toLowerCase().includes(value.toLowerCase()) : have.includes(value));
20
+ case "starts_with": return typeof have === "string" && typeof value === "string" && (loose ? have.toLowerCase().startsWith(value.toLowerCase()) : have.startsWith(value));
21
+ case "ends_with": return typeof have === "string" && typeof value === "string" && (loose ? have.toLowerCase().endsWith(value.toLowerCase()) : have.endsWith(value));
22
+ default: throw new Error(`sign-in asked with an operator the Database's sign-in tables don't take: ${operator}`);
23
+ }
24
+ }
25
+ function matches(row, clauses) {
26
+ if (!clauses?.length) return true;
27
+ let result = holds(row, clauses[0]);
28
+ for (const clause of clauses.slice(1)) result = clause.connector === "OR" ? result || holds(row, clause) : result && holds(row, clause);
29
+ return result;
30
+ }
31
+ function ordered(rows, sortBy) {
32
+ if (!sortBy) return rows;
33
+ const sign = sortBy.direction === "desc" ? -1 : 1;
34
+ return [...rows].sort((a, b) => {
35
+ const x = a[sortBy.field], y = b[sortBy.field];
36
+ return x == null && y == null ? 0 : x == null ? -sign : y == null ? sign : x < y ? -sign : x > y ? sign : 0;
37
+ });
38
+ }
39
+ /** Fields kept in lower case, so a match that ignores case reads their index, as `db/auth.ts` keeps them. */
40
+ const LOWER = {
41
+ users: ["email"],
42
+ authInvitations: ["email"],
43
+ authWaitlist: ["email"]
44
+ };
45
+ const RANGES = /* @__PURE__ */ new Set([
46
+ "lt",
47
+ "lte",
48
+ "gt",
49
+ "gte"
50
+ ]);
51
+ /** Whether `db/auth.ts` reads a clause through an index, as its `readOf` decides. */
52
+ function indexed(lookups, model, clause) {
53
+ const operator = clause.operator ?? "eq", loose = clause.mode === "insensitive";
54
+ if (loose && !(LOWER[model] ?? []).includes(clause.field)) return false;
55
+ if (clause.field === "id") return operator === "eq" || operator === "in" && Array.isArray(clause.value);
56
+ if (!lookups.includes(clause.field)) return false;
57
+ if (operator === "eq") return clause.value !== null && clause.value !== void 0;
58
+ if (operator === "in") return Array.isArray(clause.value);
59
+ if (RANGES.has(operator)) return clause.value !== null && clause.value !== void 0;
60
+ return operator === "starts_with" && typeof clause.value === "string" && !loose;
61
+ }
14
62
  function standIn() {
15
- const tables = {};
63
+ const tables = authTables();
64
+ const kept = /* @__PURE__ */ new Map();
16
65
  const outbox = [];
17
- let count = 0;
18
- const counted = (store) => new Proxy(store, { get(target, key, receiver) {
19
- const value = Reflect.get(target, key, receiver);
20
- if (typeof value !== "function" || key === "transaction" || key === "createSchema") return value;
21
- return (...args) => {
22
- count++;
23
- return value.apply(target, args);
24
- };
25
- } });
26
- const memory = memoryAdapter(tables);
66
+ let count = 0, seq = 0;
67
+ const lookupsOf = (model) => tables[model] ? [...tables[model].unique, ...tables[model].indexed] : [];
68
+ const table = (model) => {
69
+ if (typeof model !== "string" || !Object.hasOwn(tables, model)) throw new Error(`${String(model)} is not one of sign-in's tables`);
70
+ let found = kept.get(model);
71
+ if (!found) kept.set(model, found = /* @__PURE__ */ new Map());
72
+ return found;
73
+ };
74
+ /** The rows a where holds, refused as `db/auth.ts` refuses a where no index serves. */
75
+ const found = (model, where, whole = false) => {
76
+ const clauses = where ?? [];
77
+ if (!clauses.length && !whole) throw new Error(`sign-in asked for every row of ${String(model)} at once: name a field, or read a page with auth.page.`);
78
+ if (clauses.length) {
79
+ const reads = clauses.map((clause) => indexed(lookupsOf(model), model, clause));
80
+ if (clauses.some((clause, index) => index > 0 && clause.connector === "OR") ? reads.includes(false) : !reads.includes(true)) throw new Error(`sign-in asked for ${String(model)} by a field no index serves: ${clauses.map((clause) => clause.field).join(", ")}`);
81
+ }
82
+ return [...table(model).values()].map((one) => one.row).filter((row) => matches(row, clauses));
83
+ };
84
+ /** A row's fields as written: the id is the store's own, `undefined` takes a field out, and emails are lowered. */
85
+ const fieldsOf = (model, data) => Object.fromEntries(Object.entries(data ?? {}).filter(([field, value]) => field !== "id" && field !== "_id" && value !== void 0).map(([field, value]) => [field, (LOWER[model] ?? []).includes(field) && typeof value === "string" ? value.toLowerCase() : value]));
86
+ const copy = (row) => row ? structuredClone(row) : null;
87
+ /** Refuses a write that gives a unique field a value another row has, as the Database's unique index does. */
88
+ const unique = (model, row) => {
89
+ for (const field of tables[model].unique) {
90
+ if (row[field] === void 0) continue;
91
+ for (const other of table(model).values()) if (other.row.id !== row.id && other.row[field] === row[field]) throw new Error(`${model} has a row with this ${field} already`);
92
+ }
93
+ };
94
+ const run = {
95
+ create({ model, data }) {
96
+ const id = crypto.randomUUID().replaceAll("-", "");
97
+ const row = {
98
+ id,
99
+ ...fieldsOf(model, data)
100
+ };
101
+ unique(model, row);
102
+ table(model).set(id, {
103
+ seq: ++seq,
104
+ row
105
+ });
106
+ return copy(row);
107
+ },
108
+ findOne: ({ model, where }) => copy(found(model, where, true).at(-1)),
109
+ findMany({ model, where, limit, offset, sortBy }) {
110
+ const rows = ordered(where && where.length ? found(model, where) : found(model, where, true).reverse().slice(0, 1e3), sortBy);
111
+ const from = offset ?? 0;
112
+ return rows.slice(from, limit === void 0 ? void 0 : from + limit).map((row) => copy(row));
113
+ },
114
+ count: ({ model, where }) => found(model, where).length,
115
+ update({ model, where, update }) {
116
+ const row = found(model, where)[0];
117
+ if (!row) return null;
118
+ const next = {
119
+ ...row,
120
+ ...fieldsOf(model, update)
121
+ };
122
+ unique(model, next);
123
+ Object.assign(row, fieldsOf(model, update));
124
+ return copy(row);
125
+ },
126
+ updateMany({ model, where, update }) {
127
+ const rows = found(model, where);
128
+ for (const row of rows) Object.assign(row, fieldsOf(model, update));
129
+ return rows.length;
130
+ },
131
+ deleteMany({ model, where }) {
132
+ const rows = found(model, where);
133
+ for (const row of rows) table(model).delete(row.id);
134
+ return rows.length;
135
+ },
136
+ increment({ model, where, increment, set }) {
137
+ const row = found(model, where)[0];
138
+ if (!row) return null;
139
+ Object.assign(row, fieldsOf(model, set));
140
+ for (const [field, delta] of Object.entries(increment ?? {})) row[field] = (row[field] ?? 0) + delta;
141
+ return copy(row);
142
+ },
143
+ consume({ model, where }) {
144
+ const row = found(model, where)[0];
145
+ if (!row) return null;
146
+ table(model).delete(row.id);
147
+ return copy(row);
148
+ },
149
+ page({ model, field, value, numItems, cursor }) {
150
+ if (!Number.isInteger(numItems) || numItems < 1 || numItems > 200) throw new Error("A page is 1 to 200 rows.");
151
+ const after = cursor === null || cursor === void 0 ? Infinity : Number(cursor);
152
+ const newest = [...table(model).values()].filter((one) => one.seq < after && (field === void 0 || one.row[field] === value)).sort((a, b) => b.seq - a.seq);
153
+ const page = newest.slice(0, numItems);
154
+ return {
155
+ page: page.map((one) => copy(one.row)),
156
+ next: newest.length > page.length ? String(page.at(-1).seq) : null
157
+ };
158
+ }
159
+ };
27
160
  return {
28
- store: ((options) => {
29
- for (const table of Object.values(getAuthTables(options))) tables[table.modelName] ??= [];
30
- return counted(memory(options));
31
- }),
161
+ store: { async call(fn, args) {
162
+ count++;
163
+ return run[fn](args);
164
+ } },
32
165
  mail: async (mail) => {
33
166
  outbox.push(mail);
34
167
  },
35
168
  outbox,
36
169
  calls: () => count,
37
- rows: (table) => tables[table] ?? []
170
+ rows: (name) => [...kept.get(name)?.values() ?? []].map((one) => one.row)
38
171
  };
39
172
  }
40
173
  //#endregion
41
- export { standIn };
174
+ export { LOWER, indexed, standIn };