@rebasepro/server-postgres 0.10.0 → 0.10.1-canary.14e53ae
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/PostgresBootstrapper.d.ts +7 -3
- package/dist/auth/services.d.ts +43 -4
- package/dist/backup/backup-logic.d.ts +23 -0
- package/dist/backup/backup-service.d.ts +44 -2
- package/dist/backup/pg-tools.d.ts +41 -1
- package/dist/chunk-DSJWtz9O.js +40 -0
- package/dist/cli-helpers.d.ts +33 -1
- package/dist/ensure-collection-tables-CNlIONzj.js +304 -0
- package/dist/ensure-collection-tables-CNlIONzj.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.es.js +1472 -4640
- package/dist/index.es.js.map +1 -1
- package/dist/schema/auth-schema.d.ts +170 -0
- package/dist/schema/destructive-sql.d.ts +49 -0
- package/dist/schema/ensure-collection-tables.d.ts +79 -0
- package/dist/schema/generate-postgres-ddl-logic.d.ts +4 -1
- package/dist/services/cdc/CdcListener.d.ts +7 -14
- package/dist/services/channel-bus/ChannelBus.d.ts +29 -0
- package/dist/services/channel-bus/PostgresChannelBus.d.ts +111 -0
- package/dist/services/channel-bus/index.d.ts +55 -0
- package/dist/services/channel-history.d.ts +11 -0
- package/dist/services/channel-presence.d.ts +66 -0
- package/dist/services/pg-notify-listener.d.ts +47 -0
- package/dist/services/realtimeService.d.ts +114 -6
- package/dist/src-B0v4IKaI.js +329 -0
- package/dist/src-B0v4IKaI.js.map +1 -0
- package/dist/src-DmsRg8MR.js +4056 -0
- package/dist/src-DmsRg8MR.js.map +1 -0
- package/package.json +6 -6
- package/src/PostgresBootstrapper.ts +72 -3
- package/src/auth/ensure-tables.ts +91 -3
- package/src/auth/services.ts +186 -48
- package/src/backup/backup-cli.ts +60 -1
- package/src/backup/backup-cron.ts +24 -1
- package/src/backup/backup-logic.ts +62 -0
- package/src/backup/backup-service.ts +132 -13
- package/src/backup/pg-tools.ts +70 -2
- package/src/cli-helpers.ts +82 -27
- package/src/cli.ts +152 -6
- package/src/index.ts +4 -0
- package/src/schema/auth-schema.ts +41 -3
- package/src/schema/destructive-sql.ts +94 -0
- package/src/schema/ensure-collection-tables.test.ts +156 -0
- package/src/schema/ensure-collection-tables.ts +297 -0
- package/src/schema/generate-postgres-ddl-logic.ts +3 -3
- package/src/services/cdc/CdcListener.ts +27 -91
- package/src/services/channel-bus/ChannelBus.ts +44 -0
- package/src/services/channel-bus/PostgresChannelBus.ts +299 -0
- package/src/services/channel-bus/index.ts +123 -0
- package/src/services/channel-history.ts +35 -0
- package/src/services/channel-presence.ts +148 -0
- package/src/services/pg-notify-listener.ts +137 -0
- package/src/services/realtimeService.ts +383 -14
|
@@ -0,0 +1,4056 @@
|
|
|
1
|
+
import { createRequire as __createRequire } from "module";
|
|
2
|
+
import "process";
|
|
3
|
+
__createRequire(import.meta.url);
|
|
4
|
+
import { r as __require, t as __commonJSMin } from "./chunk-DSJWtz9O.js";
|
|
5
|
+
import { c as getDeclaredSubcollections, d as REST_TO_CANONICAL, f as toCanonicalOp, l as isPostgresCollectionConfig, n as getDataSourceCapabilities, o as ANONYMOUS_USER_ID, p as EntityRelation, s as policy, u as NULL_OPS } from "./src-B0v4IKaI.js";
|
|
6
|
+
//#region ../common/src/util/common.ts
|
|
7
|
+
var DEFAULT_ONE_OF_TYPE = "type";
|
|
8
|
+
var DEFAULT_ONE_OF_VALUE = "value";
|
|
9
|
+
var snakeCaseRegex = /[A-Z]{2,}(?=[A-Z][a-z]|\b)|[A-Z]?[a-z]+|[0-9]+(?:[a-z](?![a-z]))?|[A-Z]/g;
|
|
10
|
+
var toSnakeCase = (str) => {
|
|
11
|
+
if (!str || typeof str !== "string") return "";
|
|
12
|
+
const regExpMatchArray = str.match(snakeCaseRegex);
|
|
13
|
+
if (!regExpMatchArray) return "";
|
|
14
|
+
return regExpMatchArray.map((x) => x.toLowerCase()).join("_");
|
|
15
|
+
};
|
|
16
|
+
function camelCase(str) {
|
|
17
|
+
if (!str) return "";
|
|
18
|
+
if (str.length === 1) return str.toLowerCase();
|
|
19
|
+
const parts = str.split(/[-_ ]+/).filter(Boolean);
|
|
20
|
+
if (parts.length === 0) return "";
|
|
21
|
+
return parts[0].toLowerCase() + parts.slice(1).map((part) => part.charAt(0).toUpperCase() + part.substring(1).toLowerCase()).join("");
|
|
22
|
+
}
|
|
23
|
+
(/* @__PURE__ */ __commonJSMin(((exports, module) => {
|
|
24
|
+
(function(e) {
|
|
25
|
+
var t;
|
|
26
|
+
"object" == typeof exports ? module.exports = e() : "function" == typeof define && define.amd ? define(e) : ("undefined" != typeof window ? t = window : "undefined" != typeof global ? t = global : "undefined" != typeof self && (t = self), t.objectHash = e());
|
|
27
|
+
})(function() {
|
|
28
|
+
return function r(o, i, u) {
|
|
29
|
+
function s(n, e) {
|
|
30
|
+
if (!i[n]) {
|
|
31
|
+
if (!o[n]) {
|
|
32
|
+
var t = "function" == typeof __require && __require;
|
|
33
|
+
if (!e && t) return t(n, !0);
|
|
34
|
+
if (a) return a(n, !0);
|
|
35
|
+
throw new Error("Cannot find module '" + n + "'");
|
|
36
|
+
}
|
|
37
|
+
e = i[n] = { exports: {} };
|
|
38
|
+
o[n][0].call(e.exports, function(e) {
|
|
39
|
+
var t = o[n][1][e];
|
|
40
|
+
return s(t || e);
|
|
41
|
+
}, e, e.exports, r, o, i, u);
|
|
42
|
+
}
|
|
43
|
+
return i[n].exports;
|
|
44
|
+
}
|
|
45
|
+
for (var a = "function" == typeof __require && __require, e = 0; e < u.length; e++) s(u[e]);
|
|
46
|
+
return s;
|
|
47
|
+
}({
|
|
48
|
+
1: [function(w, b, m) {
|
|
49
|
+
(function(e, n, s, c, d, h, p, g, y) {
|
|
50
|
+
"use strict";
|
|
51
|
+
var r = w("crypto");
|
|
52
|
+
function t(e, t) {
|
|
53
|
+
t = u(e, t);
|
|
54
|
+
var n;
|
|
55
|
+
return void 0 === (n = "passthrough" !== t.algorithm ? r.createHash(t.algorithm) : new l()).write && (n.write = n.update, n.end = n.update), f(t, n).dispatch(e), n.update || n.end(""), n.digest ? n.digest("buffer" === t.encoding ? void 0 : t.encoding) : (e = n.read(), "buffer" !== t.encoding ? e.toString(t.encoding) : e);
|
|
56
|
+
}
|
|
57
|
+
(m = b.exports = t).sha1 = function(e) {
|
|
58
|
+
return t(e);
|
|
59
|
+
}, m.keys = function(e) {
|
|
60
|
+
return t(e, {
|
|
61
|
+
excludeValues: !0,
|
|
62
|
+
algorithm: "sha1",
|
|
63
|
+
encoding: "hex"
|
|
64
|
+
});
|
|
65
|
+
}, m.MD5 = function(e) {
|
|
66
|
+
return t(e, {
|
|
67
|
+
algorithm: "md5",
|
|
68
|
+
encoding: "hex"
|
|
69
|
+
});
|
|
70
|
+
}, m.keysMD5 = function(e) {
|
|
71
|
+
return t(e, {
|
|
72
|
+
algorithm: "md5",
|
|
73
|
+
encoding: "hex",
|
|
74
|
+
excludeValues: !0
|
|
75
|
+
});
|
|
76
|
+
};
|
|
77
|
+
var o = r.getHashes ? r.getHashes().slice() : ["sha1", "md5"], i = (o.push("passthrough"), [
|
|
78
|
+
"buffer",
|
|
79
|
+
"hex",
|
|
80
|
+
"binary",
|
|
81
|
+
"base64"
|
|
82
|
+
]);
|
|
83
|
+
function u(e, t) {
|
|
84
|
+
var n = {};
|
|
85
|
+
if (n.algorithm = (t = t || {}).algorithm || "sha1", n.encoding = t.encoding || "hex", n.excludeValues = !!t.excludeValues, n.algorithm = n.algorithm.toLowerCase(), n.encoding = n.encoding.toLowerCase(), n.ignoreUnknown = !0 === t.ignoreUnknown, n.respectType = !1 !== t.respectType, n.respectFunctionNames = !1 !== t.respectFunctionNames, n.respectFunctionProperties = !1 !== t.respectFunctionProperties, n.unorderedArrays = !0 === t.unorderedArrays, n.unorderedSets = !1 !== t.unorderedSets, n.unorderedObjects = !1 !== t.unorderedObjects, n.replacer = t.replacer || void 0, n.excludeKeys = t.excludeKeys || void 0, void 0 === e) throw new Error("Object argument required.");
|
|
86
|
+
for (var r = 0; r < o.length; ++r) o[r].toLowerCase() === n.algorithm.toLowerCase() && (n.algorithm = o[r]);
|
|
87
|
+
if (-1 === o.indexOf(n.algorithm)) throw new Error("Algorithm \"" + n.algorithm + "\" not supported. supported values: " + o.join(", "));
|
|
88
|
+
if (-1 === i.indexOf(n.encoding) && "passthrough" !== n.algorithm) throw new Error("Encoding \"" + n.encoding + "\" not supported. supported values: " + i.join(", "));
|
|
89
|
+
return n;
|
|
90
|
+
}
|
|
91
|
+
function a(e) {
|
|
92
|
+
if ("function" == typeof e) return null != /^function\s+\w*\s*\(\s*\)\s*{\s+\[native code\]\s+}$/i.exec(Function.prototype.toString.call(e));
|
|
93
|
+
}
|
|
94
|
+
function f(o, t, i) {
|
|
95
|
+
i = i || [];
|
|
96
|
+
function u(e) {
|
|
97
|
+
return t.update ? t.update(e, "utf8") : t.write(e, "utf8");
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
dispatch: function(e) {
|
|
101
|
+
return this["_" + (null === (e = o.replacer ? o.replacer(e) : e) ? "null" : typeof e)](e);
|
|
102
|
+
},
|
|
103
|
+
_object: function(t) {
|
|
104
|
+
var n, e = Object.prototype.toString.call(t), r = /\[object (.*)\]/i.exec(e);
|
|
105
|
+
r = (r = r ? r[1] : "unknown:[" + e + "]").toLowerCase();
|
|
106
|
+
if (0 <= (e = i.indexOf(t))) return this.dispatch("[CIRCULAR:" + e + "]");
|
|
107
|
+
if (i.push(t), void 0 !== s && s.isBuffer && s.isBuffer(t)) return u("buffer:"), u(t);
|
|
108
|
+
if ("object" === r || "function" === r || "asyncfunction" === r) return e = Object.keys(t), o.unorderedObjects && (e = e.sort()), !1 === o.respectType || a(t) || e.splice(0, 0, "prototype", "__proto__", "constructor"), o.excludeKeys && (e = e.filter(function(e) {
|
|
109
|
+
return !o.excludeKeys(e);
|
|
110
|
+
})), u("object:" + e.length + ":"), n = this, e.forEach(function(e) {
|
|
111
|
+
n.dispatch(e), u(":"), o.excludeValues || n.dispatch(t[e]), u(",");
|
|
112
|
+
});
|
|
113
|
+
if (!this["_" + r]) {
|
|
114
|
+
if (o.ignoreUnknown) return u("[" + r + "]");
|
|
115
|
+
throw new Error("Unknown object type \"" + r + "\"");
|
|
116
|
+
}
|
|
117
|
+
this["_" + r](t);
|
|
118
|
+
},
|
|
119
|
+
_array: function(e, t) {
|
|
120
|
+
t = void 0 !== t ? t : !1 !== o.unorderedArrays;
|
|
121
|
+
var n = this;
|
|
122
|
+
if (u("array:" + e.length + ":"), !t || e.length <= 1) return e.forEach(function(e) {
|
|
123
|
+
return n.dispatch(e);
|
|
124
|
+
});
|
|
125
|
+
var r = [], t = e.map(function(e) {
|
|
126
|
+
var t = new l(), n = i.slice();
|
|
127
|
+
return f(o, t, n).dispatch(e), r = r.concat(n.slice(i.length)), t.read().toString();
|
|
128
|
+
});
|
|
129
|
+
return i = i.concat(r), t.sort(), this._array(t, !1);
|
|
130
|
+
},
|
|
131
|
+
_date: function(e) {
|
|
132
|
+
return u("date:" + e.toJSON());
|
|
133
|
+
},
|
|
134
|
+
_symbol: function(e) {
|
|
135
|
+
return u("symbol:" + e.toString());
|
|
136
|
+
},
|
|
137
|
+
_error: function(e) {
|
|
138
|
+
return u("error:" + e.toString());
|
|
139
|
+
},
|
|
140
|
+
_boolean: function(e) {
|
|
141
|
+
return u("bool:" + e.toString());
|
|
142
|
+
},
|
|
143
|
+
_string: function(e) {
|
|
144
|
+
u("string:" + e.length + ":"), u(e.toString());
|
|
145
|
+
},
|
|
146
|
+
_function: function(e) {
|
|
147
|
+
u("fn:"), a(e) ? this.dispatch("[native]") : this.dispatch(e.toString()), !1 !== o.respectFunctionNames && this.dispatch("function-name:" + String(e.name)), o.respectFunctionProperties && this._object(e);
|
|
148
|
+
},
|
|
149
|
+
_number: function(e) {
|
|
150
|
+
return u("number:" + e.toString());
|
|
151
|
+
},
|
|
152
|
+
_xml: function(e) {
|
|
153
|
+
return u("xml:" + e.toString());
|
|
154
|
+
},
|
|
155
|
+
_null: function() {
|
|
156
|
+
return u("Null");
|
|
157
|
+
},
|
|
158
|
+
_undefined: function() {
|
|
159
|
+
return u("Undefined");
|
|
160
|
+
},
|
|
161
|
+
_regexp: function(e) {
|
|
162
|
+
return u("regex:" + e.toString());
|
|
163
|
+
},
|
|
164
|
+
_uint8array: function(e) {
|
|
165
|
+
return u("uint8array:"), this.dispatch(Array.prototype.slice.call(e));
|
|
166
|
+
},
|
|
167
|
+
_uint8clampedarray: function(e) {
|
|
168
|
+
return u("uint8clampedarray:"), this.dispatch(Array.prototype.slice.call(e));
|
|
169
|
+
},
|
|
170
|
+
_int8array: function(e) {
|
|
171
|
+
return u("int8array:"), this.dispatch(Array.prototype.slice.call(e));
|
|
172
|
+
},
|
|
173
|
+
_uint16array: function(e) {
|
|
174
|
+
return u("uint16array:"), this.dispatch(Array.prototype.slice.call(e));
|
|
175
|
+
},
|
|
176
|
+
_int16array: function(e) {
|
|
177
|
+
return u("int16array:"), this.dispatch(Array.prototype.slice.call(e));
|
|
178
|
+
},
|
|
179
|
+
_uint32array: function(e) {
|
|
180
|
+
return u("uint32array:"), this.dispatch(Array.prototype.slice.call(e));
|
|
181
|
+
},
|
|
182
|
+
_int32array: function(e) {
|
|
183
|
+
return u("int32array:"), this.dispatch(Array.prototype.slice.call(e));
|
|
184
|
+
},
|
|
185
|
+
_float32array: function(e) {
|
|
186
|
+
return u("float32array:"), this.dispatch(Array.prototype.slice.call(e));
|
|
187
|
+
},
|
|
188
|
+
_float64array: function(e) {
|
|
189
|
+
return u("float64array:"), this.dispatch(Array.prototype.slice.call(e));
|
|
190
|
+
},
|
|
191
|
+
_arraybuffer: function(e) {
|
|
192
|
+
return u("arraybuffer:"), this.dispatch(new Uint8Array(e));
|
|
193
|
+
},
|
|
194
|
+
_url: function(e) {
|
|
195
|
+
return u("url:" + e.toString());
|
|
196
|
+
},
|
|
197
|
+
_map: function(e) {
|
|
198
|
+
u("map:");
|
|
199
|
+
e = Array.from(e);
|
|
200
|
+
return this._array(e, !1 !== o.unorderedSets);
|
|
201
|
+
},
|
|
202
|
+
_set: function(e) {
|
|
203
|
+
u("set:");
|
|
204
|
+
e = Array.from(e);
|
|
205
|
+
return this._array(e, !1 !== o.unorderedSets);
|
|
206
|
+
},
|
|
207
|
+
_file: function(e) {
|
|
208
|
+
return u("file:"), this.dispatch([
|
|
209
|
+
e.name,
|
|
210
|
+
e.size,
|
|
211
|
+
e.type,
|
|
212
|
+
e.lastModfied
|
|
213
|
+
]);
|
|
214
|
+
},
|
|
215
|
+
_blob: function() {
|
|
216
|
+
if (o.ignoreUnknown) return u("[blob]");
|
|
217
|
+
throw Error("Hashing Blob objects is currently not supported\n(see https://github.com/puleos/object-hash/issues/26)\nUse \"options.replacer\" or \"options.ignoreUnknown\"\n");
|
|
218
|
+
},
|
|
219
|
+
_domwindow: function() {
|
|
220
|
+
return u("domwindow");
|
|
221
|
+
},
|
|
222
|
+
_bigint: function(e) {
|
|
223
|
+
return u("bigint:" + e.toString());
|
|
224
|
+
},
|
|
225
|
+
_process: function() {
|
|
226
|
+
return u("process");
|
|
227
|
+
},
|
|
228
|
+
_timer: function() {
|
|
229
|
+
return u("timer");
|
|
230
|
+
},
|
|
231
|
+
_pipe: function() {
|
|
232
|
+
return u("pipe");
|
|
233
|
+
},
|
|
234
|
+
_tcp: function() {
|
|
235
|
+
return u("tcp");
|
|
236
|
+
},
|
|
237
|
+
_udp: function() {
|
|
238
|
+
return u("udp");
|
|
239
|
+
},
|
|
240
|
+
_tty: function() {
|
|
241
|
+
return u("tty");
|
|
242
|
+
},
|
|
243
|
+
_statwatcher: function() {
|
|
244
|
+
return u("statwatcher");
|
|
245
|
+
},
|
|
246
|
+
_securecontext: function() {
|
|
247
|
+
return u("securecontext");
|
|
248
|
+
},
|
|
249
|
+
_connection: function() {
|
|
250
|
+
return u("connection");
|
|
251
|
+
},
|
|
252
|
+
_zlib: function() {
|
|
253
|
+
return u("zlib");
|
|
254
|
+
},
|
|
255
|
+
_context: function() {
|
|
256
|
+
return u("context");
|
|
257
|
+
},
|
|
258
|
+
_nodescript: function() {
|
|
259
|
+
return u("nodescript");
|
|
260
|
+
},
|
|
261
|
+
_httpparser: function() {
|
|
262
|
+
return u("httpparser");
|
|
263
|
+
},
|
|
264
|
+
_dataview: function() {
|
|
265
|
+
return u("dataview");
|
|
266
|
+
},
|
|
267
|
+
_signal: function() {
|
|
268
|
+
return u("signal");
|
|
269
|
+
},
|
|
270
|
+
_fsevent: function() {
|
|
271
|
+
return u("fsevent");
|
|
272
|
+
},
|
|
273
|
+
_tlswrap: function() {
|
|
274
|
+
return u("tlswrap");
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
function l() {
|
|
279
|
+
return {
|
|
280
|
+
buf: "",
|
|
281
|
+
write: function(e) {
|
|
282
|
+
this.buf += e;
|
|
283
|
+
},
|
|
284
|
+
end: function(e) {
|
|
285
|
+
this.buf += e;
|
|
286
|
+
},
|
|
287
|
+
read: function() {
|
|
288
|
+
return this.buf;
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
m.writeToStream = function(e, t, n) {
|
|
293
|
+
return void 0 === n && (n = t, t = {}), f(t = u(e, t), n).dispatch(e);
|
|
294
|
+
};
|
|
295
|
+
}).call(this, w("lYpoI2"), "undefined" != typeof self ? self : "undefined" != typeof window ? window : {}, w("buffer").Buffer, arguments[3], arguments[4], arguments[5], arguments[6], "/fake_9a5aa49d.js", "/");
|
|
296
|
+
}, {
|
|
297
|
+
buffer: 3,
|
|
298
|
+
crypto: 5,
|
|
299
|
+
lYpoI2: 11
|
|
300
|
+
}],
|
|
301
|
+
2: [function(e, t, f) {
|
|
302
|
+
(function(e, t, n, r, o, i, u, s, a) {
|
|
303
|
+
(function(e) {
|
|
304
|
+
"use strict";
|
|
305
|
+
var a = "undefined" != typeof Uint8Array ? Uint8Array : Array, t = "+".charCodeAt(0), n = "/".charCodeAt(0), r = "0".charCodeAt(0), o = "a".charCodeAt(0), i = "A".charCodeAt(0), u = "-".charCodeAt(0), s = "_".charCodeAt(0);
|
|
306
|
+
function f(e) {
|
|
307
|
+
e = e.charCodeAt(0);
|
|
308
|
+
return e === t || e === u ? 62 : e === n || e === s ? 63 : e < r ? -1 : e < r + 10 ? e - r + 26 + 26 : e < i + 26 ? e - i : e < o + 26 ? e - o + 26 : void 0;
|
|
309
|
+
}
|
|
310
|
+
e.toByteArray = function(e) {
|
|
311
|
+
var t, n;
|
|
312
|
+
if (0 < e.length % 4) throw new Error("Invalid string. Length must be a multiple of 4");
|
|
313
|
+
var r = e.length, r = "=" === e.charAt(r - 2) ? 2 : "=" === e.charAt(r - 1) ? 1 : 0, o = new a(3 * e.length / 4 - r), i = 0 < r ? e.length - 4 : e.length, u = 0;
|
|
314
|
+
function s(e) {
|
|
315
|
+
o[u++] = e;
|
|
316
|
+
}
|
|
317
|
+
for (t = 0; t < i; t += 4) s((16711680 & (n = f(e.charAt(t)) << 18 | f(e.charAt(t + 1)) << 12 | f(e.charAt(t + 2)) << 6 | f(e.charAt(t + 3)))) >> 16), s((65280 & n) >> 8), s(255 & n);
|
|
318
|
+
return 2 == r ? s(255 & (n = f(e.charAt(t)) << 2 | f(e.charAt(t + 1)) >> 4)) : 1 == r && (s((n = f(e.charAt(t)) << 10 | f(e.charAt(t + 1)) << 4 | f(e.charAt(t + 2)) >> 2) >> 8 & 255), s(255 & n)), o;
|
|
319
|
+
}, e.fromByteArray = function(e) {
|
|
320
|
+
var t, n, r, o, i = e.length % 3, u = "";
|
|
321
|
+
function s(e) {
|
|
322
|
+
return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(e);
|
|
323
|
+
}
|
|
324
|
+
for (t = 0, r = e.length - i; t < r; t += 3) n = (e[t] << 16) + (e[t + 1] << 8) + e[t + 2], u += s((o = n) >> 18 & 63) + s(o >> 12 & 63) + s(o >> 6 & 63) + s(63 & o);
|
|
325
|
+
switch (i) {
|
|
326
|
+
case 1:
|
|
327
|
+
u = (u += s((n = e[e.length - 1]) >> 2)) + s(n << 4 & 63) + "==";
|
|
328
|
+
break;
|
|
329
|
+
case 2: u = (u = (u += s((n = (e[e.length - 2] << 8) + e[e.length - 1]) >> 10)) + s(n >> 4 & 63)) + s(n << 2 & 63) + "=";
|
|
330
|
+
}
|
|
331
|
+
return u;
|
|
332
|
+
};
|
|
333
|
+
})(void 0 === f ? this.base64js = {} : f);
|
|
334
|
+
}).call(this, e("lYpoI2"), "undefined" != typeof self ? self : "undefined" != typeof window ? window : {}, e("buffer").Buffer, arguments[3], arguments[4], arguments[5], arguments[6], "/node_modules/gulp-browserify/node_modules/base64-js/lib/b64.js", "/node_modules/gulp-browserify/node_modules/base64-js/lib");
|
|
335
|
+
}, {
|
|
336
|
+
buffer: 3,
|
|
337
|
+
lYpoI2: 11
|
|
338
|
+
}],
|
|
339
|
+
3: [function(O, e, H) {
|
|
340
|
+
(function(e, n, f, r, h, p, g, y, w) {
|
|
341
|
+
var a = O("base64-js"), i = O("ieee754");
|
|
342
|
+
function f(e, t, n) {
|
|
343
|
+
if (!(this instanceof f)) return new f(e, t, n);
|
|
344
|
+
var r, o, i, u, s = typeof e;
|
|
345
|
+
if ("base64" === t && "string" == s) for (e = (u = e).trim ? u.trim() : u.replace(/^\s+|\s+$/g, ""); e.length % 4 != 0;) e += "=";
|
|
346
|
+
if ("number" == s) r = j(e);
|
|
347
|
+
else if ("string" == s) r = f.byteLength(e, t);
|
|
348
|
+
else {
|
|
349
|
+
if ("object" != s) throw new Error("First argument needs to be a number, array or string.");
|
|
350
|
+
r = j(e.length);
|
|
351
|
+
}
|
|
352
|
+
if (f._useTypedArrays ? o = f._augment(new Uint8Array(r)) : ((o = this).length = r, o._isBuffer = !0), f._useTypedArrays && "number" == typeof e.byteLength) o._set(e);
|
|
353
|
+
else if (C(u = e) || f.isBuffer(u) || u && "object" == typeof u && "number" == typeof u.length) for (i = 0; i < r; i++) f.isBuffer(e) ? o[i] = e.readUInt8(i) : o[i] = e[i];
|
|
354
|
+
else if ("string" == s) o.write(e, 0, t);
|
|
355
|
+
else if ("number" == s && !f._useTypedArrays && !n) for (i = 0; i < r; i++) o[i] = 0;
|
|
356
|
+
return o;
|
|
357
|
+
}
|
|
358
|
+
function b(e, t, n, r) {
|
|
359
|
+
return f._charsWritten = c(function(e) {
|
|
360
|
+
for (var t = [], n = 0; n < e.length; n++) t.push(255 & e.charCodeAt(n));
|
|
361
|
+
return t;
|
|
362
|
+
}(t), e, n, r);
|
|
363
|
+
}
|
|
364
|
+
function m(e, t, n, r) {
|
|
365
|
+
return f._charsWritten = c(function(e) {
|
|
366
|
+
for (var t, n, r = [], o = 0; o < e.length; o++) n = e.charCodeAt(o), t = n >> 8, n = n % 256, r.push(n), r.push(t);
|
|
367
|
+
return r;
|
|
368
|
+
}(t), e, n, r);
|
|
369
|
+
}
|
|
370
|
+
function v(e, t, n) {
|
|
371
|
+
var r = "";
|
|
372
|
+
n = Math.min(e.length, n);
|
|
373
|
+
for (var o = t; o < n; o++) r += String.fromCharCode(e[o]);
|
|
374
|
+
return r;
|
|
375
|
+
}
|
|
376
|
+
function o(e, t, n, r) {
|
|
377
|
+
r || (d("boolean" == typeof n, "missing or invalid endian"), d(null != t, "missing offset"), d(t + 1 < e.length, "Trying to read beyond buffer length"));
|
|
378
|
+
var o, r = e.length;
|
|
379
|
+
if (!(r <= t)) return n ? (o = e[t], t + 1 < r && (o |= e[t + 1] << 8)) : (o = e[t] << 8, t + 1 < r && (o |= e[t + 1])), o;
|
|
380
|
+
}
|
|
381
|
+
function u(e, t, n, r) {
|
|
382
|
+
r || (d("boolean" == typeof n, "missing or invalid endian"), d(null != t, "missing offset"), d(t + 3 < e.length, "Trying to read beyond buffer length"));
|
|
383
|
+
var o, r = e.length;
|
|
384
|
+
if (!(r <= t)) return n ? (t + 2 < r && (o = e[t + 2] << 16), t + 1 < r && (o |= e[t + 1] << 8), o |= e[t], t + 3 < r && (o += e[t + 3] << 24 >>> 0)) : (t + 1 < r && (o = e[t + 1] << 16), t + 2 < r && (o |= e[t + 2] << 8), t + 3 < r && (o |= e[t + 3]), o += e[t] << 24 >>> 0), o;
|
|
385
|
+
}
|
|
386
|
+
function _(e, t, n, r) {
|
|
387
|
+
if (r || (d("boolean" == typeof n, "missing or invalid endian"), d(null != t, "missing offset"), d(t + 1 < e.length, "Trying to read beyond buffer length")), !(e.length <= t)) return r = o(e, t, n, !0), 32768 & r ? -1 * (65535 - r + 1) : r;
|
|
388
|
+
}
|
|
389
|
+
function E(e, t, n, r) {
|
|
390
|
+
if (r || (d("boolean" == typeof n, "missing or invalid endian"), d(null != t, "missing offset"), d(t + 3 < e.length, "Trying to read beyond buffer length")), !(e.length <= t)) return r = u(e, t, n, !0), 2147483648 & r ? -1 * (4294967295 - r + 1) : r;
|
|
391
|
+
}
|
|
392
|
+
function I(e, t, n, r) {
|
|
393
|
+
return r || (d("boolean" == typeof n, "missing or invalid endian"), d(t + 3 < e.length, "Trying to read beyond buffer length")), i.read(e, t, n, 23, 4);
|
|
394
|
+
}
|
|
395
|
+
function A(e, t, n, r) {
|
|
396
|
+
return r || (d("boolean" == typeof n, "missing or invalid endian"), d(t + 7 < e.length, "Trying to read beyond buffer length")), i.read(e, t, n, 52, 8);
|
|
397
|
+
}
|
|
398
|
+
function s(e, t, n, r, o) {
|
|
399
|
+
o || (d(null != t, "missing value"), d("boolean" == typeof r, "missing or invalid endian"), d(null != n, "missing offset"), d(n + 1 < e.length, "trying to write beyond buffer length"), Y(t, 65535));
|
|
400
|
+
o = e.length;
|
|
401
|
+
if (!(o <= n)) for (var i = 0, u = Math.min(o - n, 2); i < u; i++) e[n + i] = (t & 255 << 8 * (r ? i : 1 - i)) >>> 8 * (r ? i : 1 - i);
|
|
402
|
+
}
|
|
403
|
+
function l(e, t, n, r, o) {
|
|
404
|
+
o || (d(null != t, "missing value"), d("boolean" == typeof r, "missing or invalid endian"), d(null != n, "missing offset"), d(n + 3 < e.length, "trying to write beyond buffer length"), Y(t, 4294967295));
|
|
405
|
+
o = e.length;
|
|
406
|
+
if (!(o <= n)) for (var i = 0, u = Math.min(o - n, 4); i < u; i++) e[n + i] = t >>> 8 * (r ? i : 3 - i) & 255;
|
|
407
|
+
}
|
|
408
|
+
function B(e, t, n, r, o) {
|
|
409
|
+
o || (d(null != t, "missing value"), d("boolean" == typeof r, "missing or invalid endian"), d(null != n, "missing offset"), d(n + 1 < e.length, "Trying to write beyond buffer length"), F(t, 32767, -32768)), e.length <= n || s(e, 0 <= t ? t : 65535 + t + 1, n, r, o);
|
|
410
|
+
}
|
|
411
|
+
function L(e, t, n, r, o) {
|
|
412
|
+
o || (d(null != t, "missing value"), d("boolean" == typeof r, "missing or invalid endian"), d(null != n, "missing offset"), d(n + 3 < e.length, "Trying to write beyond buffer length"), F(t, 2147483647, -2147483648)), e.length <= n || l(e, 0 <= t ? t : 4294967295 + t + 1, n, r, o);
|
|
413
|
+
}
|
|
414
|
+
function U(e, t, n, r, o) {
|
|
415
|
+
o || (d(null != t, "missing value"), d("boolean" == typeof r, "missing or invalid endian"), d(null != n, "missing offset"), d(n + 3 < e.length, "Trying to write beyond buffer length"), D(t, 34028234663852886e22, -34028234663852886e22)), e.length <= n || i.write(e, t, n, r, 23, 4);
|
|
416
|
+
}
|
|
417
|
+
function x(e, t, n, r, o) {
|
|
418
|
+
o || (d(null != t, "missing value"), d("boolean" == typeof r, "missing or invalid endian"), d(null != n, "missing offset"), d(n + 7 < e.length, "Trying to write beyond buffer length"), D(t, 17976931348623157e292, -17976931348623157e292)), e.length <= n || i.write(e, t, n, r, 52, 8);
|
|
419
|
+
}
|
|
420
|
+
H.Buffer = f, H.SlowBuffer = f, H.INSPECT_MAX_BYTES = 50, f.poolSize = 8192, f._useTypedArrays = function() {
|
|
421
|
+
try {
|
|
422
|
+
var t = new Uint8Array(/* @__PURE__ */ new ArrayBuffer(0));
|
|
423
|
+
return t.foo = function() {
|
|
424
|
+
return 42;
|
|
425
|
+
}, 42 === t.foo() && "function" == typeof t.subarray;
|
|
426
|
+
} catch (e) {
|
|
427
|
+
return !1;
|
|
428
|
+
}
|
|
429
|
+
}(), f.isEncoding = function(e) {
|
|
430
|
+
switch (String(e).toLowerCase()) {
|
|
431
|
+
case "hex":
|
|
432
|
+
case "utf8":
|
|
433
|
+
case "utf-8":
|
|
434
|
+
case "ascii":
|
|
435
|
+
case "binary":
|
|
436
|
+
case "base64":
|
|
437
|
+
case "raw":
|
|
438
|
+
case "ucs2":
|
|
439
|
+
case "ucs-2":
|
|
440
|
+
case "utf16le":
|
|
441
|
+
case "utf-16le": return !0;
|
|
442
|
+
default: return !1;
|
|
443
|
+
}
|
|
444
|
+
}, f.isBuffer = function(e) {
|
|
445
|
+
return !(null == e || !e._isBuffer);
|
|
446
|
+
}, f.byteLength = function(e, t) {
|
|
447
|
+
var n;
|
|
448
|
+
switch (e += "", t || "utf8") {
|
|
449
|
+
case "hex":
|
|
450
|
+
n = e.length / 2;
|
|
451
|
+
break;
|
|
452
|
+
case "utf8":
|
|
453
|
+
case "utf-8":
|
|
454
|
+
n = T(e).length;
|
|
455
|
+
break;
|
|
456
|
+
case "ascii":
|
|
457
|
+
case "binary":
|
|
458
|
+
case "raw":
|
|
459
|
+
n = e.length;
|
|
460
|
+
break;
|
|
461
|
+
case "base64":
|
|
462
|
+
n = M(e).length;
|
|
463
|
+
break;
|
|
464
|
+
case "ucs2":
|
|
465
|
+
case "ucs-2":
|
|
466
|
+
case "utf16le":
|
|
467
|
+
case "utf-16le":
|
|
468
|
+
n = 2 * e.length;
|
|
469
|
+
break;
|
|
470
|
+
default: throw new Error("Unknown encoding");
|
|
471
|
+
}
|
|
472
|
+
return n;
|
|
473
|
+
}, f.concat = function(e, t) {
|
|
474
|
+
if (d(C(e), "Usage: Buffer.concat(list, [totalLength])\nlist should be an Array."), 0 === e.length) return new f(0);
|
|
475
|
+
if (1 === e.length) return e[0];
|
|
476
|
+
if ("number" != typeof t) for (o = t = 0; o < e.length; o++) t += e[o].length;
|
|
477
|
+
for (var n = new f(t), r = 0, o = 0; o < e.length; o++) {
|
|
478
|
+
var i = e[o];
|
|
479
|
+
i.copy(n, r), r += i.length;
|
|
480
|
+
}
|
|
481
|
+
return n;
|
|
482
|
+
}, f.prototype.write = function(e, t, n, r) {
|
|
483
|
+
isFinite(t) ? isFinite(n) || (r = n, n = void 0) : (a = r, r = t, t = n, n = a), t = Number(t) || 0;
|
|
484
|
+
var o, i, u, s, a = this.length - t;
|
|
485
|
+
switch ((!n || a < (n = Number(n))) && (n = a), r = String(r || "utf8").toLowerCase()) {
|
|
486
|
+
case "hex":
|
|
487
|
+
o = function(e, t, n, r) {
|
|
488
|
+
n = Number(n) || 0;
|
|
489
|
+
var o = e.length - n;
|
|
490
|
+
(!r || o < (r = Number(r))) && (r = o), d((o = t.length) % 2 == 0, "Invalid hex string"), o / 2 < r && (r = o / 2);
|
|
491
|
+
for (var i = 0; i < r; i++) {
|
|
492
|
+
var u = parseInt(t.substr(2 * i, 2), 16);
|
|
493
|
+
d(!isNaN(u), "Invalid hex string"), e[n + i] = u;
|
|
494
|
+
}
|
|
495
|
+
return f._charsWritten = 2 * i, i;
|
|
496
|
+
}(this, e, t, n);
|
|
497
|
+
break;
|
|
498
|
+
case "utf8":
|
|
499
|
+
case "utf-8":
|
|
500
|
+
i = this, u = t, s = n, o = f._charsWritten = c(T(e), i, u, s);
|
|
501
|
+
break;
|
|
502
|
+
case "ascii":
|
|
503
|
+
case "binary":
|
|
504
|
+
o = b(this, e, t, n);
|
|
505
|
+
break;
|
|
506
|
+
case "base64":
|
|
507
|
+
i = this, u = t, s = n, o = f._charsWritten = c(M(e), i, u, s);
|
|
508
|
+
break;
|
|
509
|
+
case "ucs2":
|
|
510
|
+
case "ucs-2":
|
|
511
|
+
case "utf16le":
|
|
512
|
+
case "utf-16le":
|
|
513
|
+
o = m(this, e, t, n);
|
|
514
|
+
break;
|
|
515
|
+
default: throw new Error("Unknown encoding");
|
|
516
|
+
}
|
|
517
|
+
return o;
|
|
518
|
+
}, f.prototype.toString = function(e, t, n) {
|
|
519
|
+
var r, o, i, u, s = this;
|
|
520
|
+
if (e = String(e || "utf8").toLowerCase(), t = Number(t) || 0, (n = void 0 !== n ? Number(n) : s.length) === t) return "";
|
|
521
|
+
switch (e) {
|
|
522
|
+
case "hex":
|
|
523
|
+
r = function(e, t, n) {
|
|
524
|
+
var r = e.length;
|
|
525
|
+
(!t || t < 0) && (t = 0);
|
|
526
|
+
(!n || n < 0 || r < n) && (n = r);
|
|
527
|
+
for (var o = "", i = t; i < n; i++) o += k(e[i]);
|
|
528
|
+
return o;
|
|
529
|
+
}(s, t, n);
|
|
530
|
+
break;
|
|
531
|
+
case "utf8":
|
|
532
|
+
case "utf-8":
|
|
533
|
+
r = function(e, t, n) {
|
|
534
|
+
var r = "", o = "";
|
|
535
|
+
n = Math.min(e.length, n);
|
|
536
|
+
for (var i = t; i < n; i++) e[i] <= 127 ? (r += N(o) + String.fromCharCode(e[i]), o = "") : o += "%" + e[i].toString(16);
|
|
537
|
+
return r + N(o);
|
|
538
|
+
}(s, t, n);
|
|
539
|
+
break;
|
|
540
|
+
case "ascii":
|
|
541
|
+
case "binary":
|
|
542
|
+
r = v(s, t, n);
|
|
543
|
+
break;
|
|
544
|
+
case "base64":
|
|
545
|
+
o = s, u = n, r = 0 === (i = t) && u === o.length ? a.fromByteArray(o) : a.fromByteArray(o.slice(i, u));
|
|
546
|
+
break;
|
|
547
|
+
case "ucs2":
|
|
548
|
+
case "ucs-2":
|
|
549
|
+
case "utf16le":
|
|
550
|
+
case "utf-16le":
|
|
551
|
+
r = function(e, t, n) {
|
|
552
|
+
for (var r = e.slice(t, n), o = "", i = 0; i < r.length; i += 2) o += String.fromCharCode(r[i] + 256 * r[i + 1]);
|
|
553
|
+
return o;
|
|
554
|
+
}(s, t, n);
|
|
555
|
+
break;
|
|
556
|
+
default: throw new Error("Unknown encoding");
|
|
557
|
+
}
|
|
558
|
+
return r;
|
|
559
|
+
}, f.prototype.toJSON = function() {
|
|
560
|
+
return {
|
|
561
|
+
type: "Buffer",
|
|
562
|
+
data: Array.prototype.slice.call(this._arr || this, 0)
|
|
563
|
+
};
|
|
564
|
+
}, f.prototype.copy = function(e, t, n, r) {
|
|
565
|
+
if (t = t || 0, (r = r || 0 === r ? r : this.length) !== (n = n || 0) && 0 !== e.length && 0 !== this.length) {
|
|
566
|
+
d(n <= r, "sourceEnd < sourceStart"), d(0 <= t && t < e.length, "targetStart out of bounds"), d(0 <= n && n < this.length, "sourceStart out of bounds"), d(0 <= r && r <= this.length, "sourceEnd out of bounds"), r > this.length && (r = this.length);
|
|
567
|
+
var o = (r = e.length - t < r - n ? e.length - t + n : r) - n;
|
|
568
|
+
if (o < 100 || !f._useTypedArrays) for (var i = 0; i < o; i++) e[i + t] = this[i + n];
|
|
569
|
+
else e._set(this.subarray(n, n + o), t);
|
|
570
|
+
}
|
|
571
|
+
}, f.prototype.slice = function(e, t) {
|
|
572
|
+
var n = this.length;
|
|
573
|
+
if (e = S(e, n, 0), t = S(t, n, n), f._useTypedArrays) return f._augment(this.subarray(e, t));
|
|
574
|
+
for (var r = t - e, o = new f(r, void 0, !0), i = 0; i < r; i++) o[i] = this[i + e];
|
|
575
|
+
return o;
|
|
576
|
+
}, f.prototype.get = function(e) {
|
|
577
|
+
return console.log(".get() is deprecated. Access using array indexes instead."), this.readUInt8(e);
|
|
578
|
+
}, f.prototype.set = function(e, t) {
|
|
579
|
+
return console.log(".set() is deprecated. Access using array indexes instead."), this.writeUInt8(e, t);
|
|
580
|
+
}, f.prototype.readUInt8 = function(e, t) {
|
|
581
|
+
if (t || (d(null != e, "missing offset"), d(e < this.length, "Trying to read beyond buffer length")), !(e >= this.length)) return this[e];
|
|
582
|
+
}, f.prototype.readUInt16LE = function(e, t) {
|
|
583
|
+
return o(this, e, !0, t);
|
|
584
|
+
}, f.prototype.readUInt16BE = function(e, t) {
|
|
585
|
+
return o(this, e, !1, t);
|
|
586
|
+
}, f.prototype.readUInt32LE = function(e, t) {
|
|
587
|
+
return u(this, e, !0, t);
|
|
588
|
+
}, f.prototype.readUInt32BE = function(e, t) {
|
|
589
|
+
return u(this, e, !1, t);
|
|
590
|
+
}, f.prototype.readInt8 = function(e, t) {
|
|
591
|
+
if (t || (d(null != e, "missing offset"), d(e < this.length, "Trying to read beyond buffer length")), !(e >= this.length)) return 128 & this[e] ? -1 * (255 - this[e] + 1) : this[e];
|
|
592
|
+
}, f.prototype.readInt16LE = function(e, t) {
|
|
593
|
+
return _(this, e, !0, t);
|
|
594
|
+
}, f.prototype.readInt16BE = function(e, t) {
|
|
595
|
+
return _(this, e, !1, t);
|
|
596
|
+
}, f.prototype.readInt32LE = function(e, t) {
|
|
597
|
+
return E(this, e, !0, t);
|
|
598
|
+
}, f.prototype.readInt32BE = function(e, t) {
|
|
599
|
+
return E(this, e, !1, t);
|
|
600
|
+
}, f.prototype.readFloatLE = function(e, t) {
|
|
601
|
+
return I(this, e, !0, t);
|
|
602
|
+
}, f.prototype.readFloatBE = function(e, t) {
|
|
603
|
+
return I(this, e, !1, t);
|
|
604
|
+
}, f.prototype.readDoubleLE = function(e, t) {
|
|
605
|
+
return A(this, e, !0, t);
|
|
606
|
+
}, f.prototype.readDoubleBE = function(e, t) {
|
|
607
|
+
return A(this, e, !1, t);
|
|
608
|
+
}, f.prototype.writeUInt8 = function(e, t, n) {
|
|
609
|
+
n || (d(null != e, "missing value"), d(null != t, "missing offset"), d(t < this.length, "trying to write beyond buffer length"), Y(e, 255)), t >= this.length || (this[t] = e);
|
|
610
|
+
}, f.prototype.writeUInt16LE = function(e, t, n) {
|
|
611
|
+
s(this, e, t, !0, n);
|
|
612
|
+
}, f.prototype.writeUInt16BE = function(e, t, n) {
|
|
613
|
+
s(this, e, t, !1, n);
|
|
614
|
+
}, f.prototype.writeUInt32LE = function(e, t, n) {
|
|
615
|
+
l(this, e, t, !0, n);
|
|
616
|
+
}, f.prototype.writeUInt32BE = function(e, t, n) {
|
|
617
|
+
l(this, e, t, !1, n);
|
|
618
|
+
}, f.prototype.writeInt8 = function(e, t, n) {
|
|
619
|
+
n || (d(null != e, "missing value"), d(null != t, "missing offset"), d(t < this.length, "Trying to write beyond buffer length"), F(e, 127, -128)), t >= this.length || (0 <= e ? this.writeUInt8(e, t, n) : this.writeUInt8(255 + e + 1, t, n));
|
|
620
|
+
}, f.prototype.writeInt16LE = function(e, t, n) {
|
|
621
|
+
B(this, e, t, !0, n);
|
|
622
|
+
}, f.prototype.writeInt16BE = function(e, t, n) {
|
|
623
|
+
B(this, e, t, !1, n);
|
|
624
|
+
}, f.prototype.writeInt32LE = function(e, t, n) {
|
|
625
|
+
L(this, e, t, !0, n);
|
|
626
|
+
}, f.prototype.writeInt32BE = function(e, t, n) {
|
|
627
|
+
L(this, e, t, !1, n);
|
|
628
|
+
}, f.prototype.writeFloatLE = function(e, t, n) {
|
|
629
|
+
U(this, e, t, !0, n);
|
|
630
|
+
}, f.prototype.writeFloatBE = function(e, t, n) {
|
|
631
|
+
U(this, e, t, !1, n);
|
|
632
|
+
}, f.prototype.writeDoubleLE = function(e, t, n) {
|
|
633
|
+
x(this, e, t, !0, n);
|
|
634
|
+
}, f.prototype.writeDoubleBE = function(e, t, n) {
|
|
635
|
+
x(this, e, t, !1, n);
|
|
636
|
+
}, f.prototype.fill = function(e, t, n) {
|
|
637
|
+
if (t = t || 0, n = n || this.length, d("number" == typeof (e = "string" == typeof (e = e || 0) ? e.charCodeAt(0) : e) && !isNaN(e), "value is not a number"), d(t <= n, "end < start"), n !== t && 0 !== this.length) {
|
|
638
|
+
d(0 <= t && t < this.length, "start out of bounds"), d(0 <= n && n <= this.length, "end out of bounds");
|
|
639
|
+
for (var r = t; r < n; r++) this[r] = e;
|
|
640
|
+
}
|
|
641
|
+
}, f.prototype.inspect = function() {
|
|
642
|
+
for (var e = [], t = this.length, n = 0; n < t; n++) if (e[n] = k(this[n]), n === H.INSPECT_MAX_BYTES) {
|
|
643
|
+
e[n + 1] = "...";
|
|
644
|
+
break;
|
|
645
|
+
}
|
|
646
|
+
return "<Buffer " + e.join(" ") + ">";
|
|
647
|
+
}, f.prototype.toArrayBuffer = function() {
|
|
648
|
+
if ("undefined" == typeof Uint8Array) throw new Error("Buffer.toArrayBuffer not supported in this browser");
|
|
649
|
+
if (f._useTypedArrays) return new f(this).buffer;
|
|
650
|
+
for (var e = new Uint8Array(this.length), t = 0, n = e.length; t < n; t += 1) e[t] = this[t];
|
|
651
|
+
return e.buffer;
|
|
652
|
+
};
|
|
653
|
+
var t = f.prototype;
|
|
654
|
+
function S(e, t, n) {
|
|
655
|
+
return "number" != typeof e ? n : t <= (e = ~~e) ? t : 0 <= e || 0 <= (e += t) ? e : 0;
|
|
656
|
+
}
|
|
657
|
+
function j(e) {
|
|
658
|
+
return (e = ~~Math.ceil(+e)) < 0 ? 0 : e;
|
|
659
|
+
}
|
|
660
|
+
function C(e) {
|
|
661
|
+
return (Array.isArray || function(e) {
|
|
662
|
+
return "[object Array]" === Object.prototype.toString.call(e);
|
|
663
|
+
})(e);
|
|
664
|
+
}
|
|
665
|
+
function k(e) {
|
|
666
|
+
return e < 16 ? "0" + e.toString(16) : e.toString(16);
|
|
667
|
+
}
|
|
668
|
+
function T(e) {
|
|
669
|
+
for (var t = [], n = 0; n < e.length; n++) {
|
|
670
|
+
var r = e.charCodeAt(n);
|
|
671
|
+
if (r <= 127) t.push(e.charCodeAt(n));
|
|
672
|
+
else for (var o = n, i = (55296 <= r && r <= 57343 && n++, encodeURIComponent(e.slice(o, n + 1)).substr(1).split("%")), u = 0; u < i.length; u++) t.push(parseInt(i[u], 16));
|
|
673
|
+
}
|
|
674
|
+
return t;
|
|
675
|
+
}
|
|
676
|
+
function M(e) {
|
|
677
|
+
return a.toByteArray(e);
|
|
678
|
+
}
|
|
679
|
+
function c(e, t, n, r) {
|
|
680
|
+
for (var o = 0; o < r && !(o + n >= t.length || o >= e.length); o++) t[o + n] = e[o];
|
|
681
|
+
return o;
|
|
682
|
+
}
|
|
683
|
+
function N(e) {
|
|
684
|
+
try {
|
|
685
|
+
return decodeURIComponent(e);
|
|
686
|
+
} catch (e) {
|
|
687
|
+
return String.fromCharCode(65533);
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
function Y(e, t) {
|
|
691
|
+
d("number" == typeof e, "cannot write a non-number as a number"), d(0 <= e, "specified a negative value for writing an unsigned value"), d(e <= t, "value is larger than maximum value for type"), d(Math.floor(e) === e, "value has a fractional component");
|
|
692
|
+
}
|
|
693
|
+
function F(e, t, n) {
|
|
694
|
+
d("number" == typeof e, "cannot write a non-number as a number"), d(e <= t, "value larger than maximum allowed value"), d(n <= e, "value smaller than minimum allowed value"), d(Math.floor(e) === e, "value has a fractional component");
|
|
695
|
+
}
|
|
696
|
+
function D(e, t, n) {
|
|
697
|
+
d("number" == typeof e, "cannot write a non-number as a number"), d(e <= t, "value larger than maximum allowed value"), d(n <= e, "value smaller than minimum allowed value");
|
|
698
|
+
}
|
|
699
|
+
function d(e, t) {
|
|
700
|
+
if (!e) throw new Error(t || "Failed assertion");
|
|
701
|
+
}
|
|
702
|
+
f._augment = function(e) {
|
|
703
|
+
return e._isBuffer = !0, e._get = e.get, e._set = e.set, e.get = t.get, e.set = t.set, e.write = t.write, e.toString = t.toString, e.toLocaleString = t.toString, e.toJSON = t.toJSON, e.copy = t.copy, e.slice = t.slice, e.readUInt8 = t.readUInt8, e.readUInt16LE = t.readUInt16LE, e.readUInt16BE = t.readUInt16BE, e.readUInt32LE = t.readUInt32LE, e.readUInt32BE = t.readUInt32BE, e.readInt8 = t.readInt8, e.readInt16LE = t.readInt16LE, e.readInt16BE = t.readInt16BE, e.readInt32LE = t.readInt32LE, e.readInt32BE = t.readInt32BE, e.readFloatLE = t.readFloatLE, e.readFloatBE = t.readFloatBE, e.readDoubleLE = t.readDoubleLE, e.readDoubleBE = t.readDoubleBE, e.writeUInt8 = t.writeUInt8, e.writeUInt16LE = t.writeUInt16LE, e.writeUInt16BE = t.writeUInt16BE, e.writeUInt32LE = t.writeUInt32LE, e.writeUInt32BE = t.writeUInt32BE, e.writeInt8 = t.writeInt8, e.writeInt16LE = t.writeInt16LE, e.writeInt16BE = t.writeInt16BE, e.writeInt32LE = t.writeInt32LE, e.writeInt32BE = t.writeInt32BE, e.writeFloatLE = t.writeFloatLE, e.writeFloatBE = t.writeFloatBE, e.writeDoubleLE = t.writeDoubleLE, e.writeDoubleBE = t.writeDoubleBE, e.fill = t.fill, e.inspect = t.inspect, e.toArrayBuffer = t.toArrayBuffer, e;
|
|
704
|
+
};
|
|
705
|
+
}).call(this, O("lYpoI2"), "undefined" != typeof self ? self : "undefined" != typeof window ? window : {}, O("buffer").Buffer, arguments[3], arguments[4], arguments[5], arguments[6], "/node_modules/gulp-browserify/node_modules/buffer/index.js", "/node_modules/gulp-browserify/node_modules/buffer");
|
|
706
|
+
}, {
|
|
707
|
+
"base64-js": 2,
|
|
708
|
+
buffer: 3,
|
|
709
|
+
ieee754: 10,
|
|
710
|
+
lYpoI2: 11
|
|
711
|
+
}],
|
|
712
|
+
4: [function(c, d, e) {
|
|
713
|
+
(function(e, t, a, n, r, o, i, u, s) {
|
|
714
|
+
var a = c("buffer").Buffer, f = 4, l = new a(f);
|
|
715
|
+
l.fill(0);
|
|
716
|
+
d.exports = { hash: function(e, t, n, r) {
|
|
717
|
+
for (var o = t(function(e, t) {
|
|
718
|
+
e.length % f != 0 && (n = e.length + (f - e.length % f), e = a.concat([e, l], n));
|
|
719
|
+
for (var n, r = [], o = t ? e.readInt32BE : e.readInt32LE, i = 0; i < e.length; i += f) r.push(o.call(e, i));
|
|
720
|
+
return r;
|
|
721
|
+
}(e = a.isBuffer(e) ? e : new a(e), r), 8 * e.length), t = r, i = new a(n), u = t ? i.writeInt32BE : i.writeInt32LE, s = 0; s < o.length; s++) u.call(i, o[s], 4 * s, !0);
|
|
722
|
+
return i;
|
|
723
|
+
} };
|
|
724
|
+
}).call(this, c("lYpoI2"), "undefined" != typeof self ? self : "undefined" != typeof window ? window : {}, c("buffer").Buffer, arguments[3], arguments[4], arguments[5], arguments[6], "/node_modules/gulp-browserify/node_modules/crypto-browserify/helpers.js", "/node_modules/gulp-browserify/node_modules/crypto-browserify");
|
|
725
|
+
}, {
|
|
726
|
+
buffer: 3,
|
|
727
|
+
lYpoI2: 11
|
|
728
|
+
}],
|
|
729
|
+
5: [function(v, e, _) {
|
|
730
|
+
(function(l, c, u, d, h, p, g, y, w) {
|
|
731
|
+
var u = v("buffer").Buffer, e = v("./sha"), t = v("./sha256"), n = v("./rng"), b = {
|
|
732
|
+
sha1: e,
|
|
733
|
+
sha256: t,
|
|
734
|
+
md5: v("./md5")
|
|
735
|
+
}, s = 64, a = new u(s);
|
|
736
|
+
function r(e, n) {
|
|
737
|
+
var r = b[e = e || "sha1"], o = [];
|
|
738
|
+
return r || i("algorithm:", e, "is not yet supported"), {
|
|
739
|
+
update: function(e) {
|
|
740
|
+
return u.isBuffer(e) || (e = new u(e)), o.push(e), e.length, this;
|
|
741
|
+
},
|
|
742
|
+
digest: function(e) {
|
|
743
|
+
var t = u.concat(o), t = n ? function(e, t, n) {
|
|
744
|
+
u.isBuffer(t) || (t = new u(t)), u.isBuffer(n) || (n = new u(n)), t.length > s ? t = e(t) : t.length < s && (t = u.concat([t, a], s));
|
|
745
|
+
for (var r = new u(s), o = new u(s), i = 0; i < s; i++) r[i] = 54 ^ t[i], o[i] = 92 ^ t[i];
|
|
746
|
+
return n = e(u.concat([r, n])), e(u.concat([o, n]));
|
|
747
|
+
}(r, n, t) : r(t);
|
|
748
|
+
return o = null, e ? t.toString(e) : t;
|
|
749
|
+
}
|
|
750
|
+
};
|
|
751
|
+
}
|
|
752
|
+
function i() {
|
|
753
|
+
var e = [].slice.call(arguments).join(" ");
|
|
754
|
+
throw new Error([
|
|
755
|
+
e,
|
|
756
|
+
"we accept pull requests",
|
|
757
|
+
"http://github.com/dominictarr/crypto-browserify"
|
|
758
|
+
].join("\n"));
|
|
759
|
+
}
|
|
760
|
+
a.fill(0), _.createHash = function(e) {
|
|
761
|
+
return r(e);
|
|
762
|
+
}, _.createHmac = r, _.randomBytes = function(e, t) {
|
|
763
|
+
if (!t || !t.call) return new u(n(e));
|
|
764
|
+
try {
|
|
765
|
+
t.call(this, void 0, new u(n(e)));
|
|
766
|
+
} catch (e) {
|
|
767
|
+
t(e);
|
|
768
|
+
}
|
|
769
|
+
};
|
|
770
|
+
var o, f = [
|
|
771
|
+
"createCredentials",
|
|
772
|
+
"createCipher",
|
|
773
|
+
"createCipheriv",
|
|
774
|
+
"createDecipher",
|
|
775
|
+
"createDecipheriv",
|
|
776
|
+
"createSign",
|
|
777
|
+
"createVerify",
|
|
778
|
+
"createDiffieHellman",
|
|
779
|
+
"pbkdf2"
|
|
780
|
+
], m = function(e) {
|
|
781
|
+
_[e] = function() {
|
|
782
|
+
i("sorry,", e, "is not implemented yet");
|
|
783
|
+
};
|
|
784
|
+
};
|
|
785
|
+
for (o in f) m(f[o], o);
|
|
786
|
+
}).call(this, v("lYpoI2"), "undefined" != typeof self ? self : "undefined" != typeof window ? window : {}, v("buffer").Buffer, arguments[3], arguments[4], arguments[5], arguments[6], "/node_modules/gulp-browserify/node_modules/crypto-browserify/index.js", "/node_modules/gulp-browserify/node_modules/crypto-browserify");
|
|
787
|
+
}, {
|
|
788
|
+
"./md5": 6,
|
|
789
|
+
"./rng": 7,
|
|
790
|
+
"./sha": 8,
|
|
791
|
+
"./sha256": 9,
|
|
792
|
+
buffer: 3,
|
|
793
|
+
lYpoI2: 11
|
|
794
|
+
}],
|
|
795
|
+
6: [function(w, b, e) {
|
|
796
|
+
(function(e, r, o, i, u, a, f, l, y) {
|
|
797
|
+
var t = w("./helpers");
|
|
798
|
+
function n(e, t) {
|
|
799
|
+
e[t >> 5] |= 128 << t % 32, e[14 + (t + 64 >>> 9 << 4)] = t;
|
|
800
|
+
for (var n = 1732584193, r = -271733879, o = -1732584194, i = 271733878, u = 0; u < e.length; u += 16) {
|
|
801
|
+
var s = n, a = r, f = o, l = i, n = c(n, r, o, i, e[u + 0], 7, -680876936), i = c(i, n, r, o, e[u + 1], 12, -389564586), o = c(o, i, n, r, e[u + 2], 17, 606105819), r = c(r, o, i, n, e[u + 3], 22, -1044525330);
|
|
802
|
+
n = c(n, r, o, i, e[u + 4], 7, -176418897), i = c(i, n, r, o, e[u + 5], 12, 1200080426), o = c(o, i, n, r, e[u + 6], 17, -1473231341), r = c(r, o, i, n, e[u + 7], 22, -45705983), n = c(n, r, o, i, e[u + 8], 7, 1770035416), i = c(i, n, r, o, e[u + 9], 12, -1958414417), o = c(o, i, n, r, e[u + 10], 17, -42063), r = c(r, o, i, n, e[u + 11], 22, -1990404162), n = c(n, r, o, i, e[u + 12], 7, 1804603682), i = c(i, n, r, o, e[u + 13], 12, -40341101), o = c(o, i, n, r, e[u + 14], 17, -1502002290), n = d(n, r = c(r, o, i, n, e[u + 15], 22, 1236535329), o, i, e[u + 1], 5, -165796510), i = d(i, n, r, o, e[u + 6], 9, -1069501632), o = d(o, i, n, r, e[u + 11], 14, 643717713), r = d(r, o, i, n, e[u + 0], 20, -373897302), n = d(n, r, o, i, e[u + 5], 5, -701558691), i = d(i, n, r, o, e[u + 10], 9, 38016083), o = d(o, i, n, r, e[u + 15], 14, -660478335), r = d(r, o, i, n, e[u + 4], 20, -405537848), n = d(n, r, o, i, e[u + 9], 5, 568446438), i = d(i, n, r, o, e[u + 14], 9, -1019803690), o = d(o, i, n, r, e[u + 3], 14, -187363961), r = d(r, o, i, n, e[u + 8], 20, 1163531501), n = d(n, r, o, i, e[u + 13], 5, -1444681467), i = d(i, n, r, o, e[u + 2], 9, -51403784), o = d(o, i, n, r, e[u + 7], 14, 1735328473), n = h(n, r = d(r, o, i, n, e[u + 12], 20, -1926607734), o, i, e[u + 5], 4, -378558), i = h(i, n, r, o, e[u + 8], 11, -2022574463), o = h(o, i, n, r, e[u + 11], 16, 1839030562), r = h(r, o, i, n, e[u + 14], 23, -35309556), n = h(n, r, o, i, e[u + 1], 4, -1530992060), i = h(i, n, r, o, e[u + 4], 11, 1272893353), o = h(o, i, n, r, e[u + 7], 16, -155497632), r = h(r, o, i, n, e[u + 10], 23, -1094730640), n = h(n, r, o, i, e[u + 13], 4, 681279174), i = h(i, n, r, o, e[u + 0], 11, -358537222), o = h(o, i, n, r, e[u + 3], 16, -722521979), r = h(r, o, i, n, e[u + 6], 23, 76029189), n = h(n, r, o, i, e[u + 9], 4, -640364487), i = h(i, n, r, o, e[u + 12], 11, -421815835), o = h(o, i, n, r, e[u + 15], 16, 530742520), n = p(n, r = h(r, o, i, n, e[u + 2], 23, -995338651), o, i, e[u + 0], 6, -198630844), i = p(i, n, r, o, e[u + 7], 10, 1126891415), o = p(o, i, n, r, e[u + 14], 15, -1416354905), r = p(r, o, i, n, e[u + 5], 21, -57434055), n = p(n, r, o, i, e[u + 12], 6, 1700485571), i = p(i, n, r, o, e[u + 3], 10, -1894986606), o = p(o, i, n, r, e[u + 10], 15, -1051523), r = p(r, o, i, n, e[u + 1], 21, -2054922799), n = p(n, r, o, i, e[u + 8], 6, 1873313359), i = p(i, n, r, o, e[u + 15], 10, -30611744), o = p(o, i, n, r, e[u + 6], 15, -1560198380), r = p(r, o, i, n, e[u + 13], 21, 1309151649), n = p(n, r, o, i, e[u + 4], 6, -145523070), i = p(i, n, r, o, e[u + 11], 10, -1120210379), o = p(o, i, n, r, e[u + 2], 15, 718787259), r = p(r, o, i, n, e[u + 9], 21, -343485551), n = g(n, s), r = g(r, a), o = g(o, f), i = g(i, l);
|
|
803
|
+
}
|
|
804
|
+
return Array(n, r, o, i);
|
|
805
|
+
}
|
|
806
|
+
function s(e, t, n, r, o, i) {
|
|
807
|
+
return g((t = g(g(t, e), g(r, i))) << o | t >>> 32 - o, n);
|
|
808
|
+
}
|
|
809
|
+
function c(e, t, n, r, o, i, u) {
|
|
810
|
+
return s(t & n | ~t & r, e, t, o, i, u);
|
|
811
|
+
}
|
|
812
|
+
function d(e, t, n, r, o, i, u) {
|
|
813
|
+
return s(t & r | n & ~r, e, t, o, i, u);
|
|
814
|
+
}
|
|
815
|
+
function h(e, t, n, r, o, i, u) {
|
|
816
|
+
return s(t ^ n ^ r, e, t, o, i, u);
|
|
817
|
+
}
|
|
818
|
+
function p(e, t, n, r, o, i, u) {
|
|
819
|
+
return s(n ^ (t | ~r), e, t, o, i, u);
|
|
820
|
+
}
|
|
821
|
+
function g(e, t) {
|
|
822
|
+
var n = (65535 & e) + (65535 & t);
|
|
823
|
+
return (e >> 16) + (t >> 16) + (n >> 16) << 16 | 65535 & n;
|
|
824
|
+
}
|
|
825
|
+
b.exports = function(e) {
|
|
826
|
+
return t.hash(e, n, 16);
|
|
827
|
+
};
|
|
828
|
+
}).call(this, w("lYpoI2"), "undefined" != typeof self ? self : "undefined" != typeof window ? window : {}, w("buffer").Buffer, arguments[3], arguments[4], arguments[5], arguments[6], "/node_modules/gulp-browserify/node_modules/crypto-browserify/md5.js", "/node_modules/gulp-browserify/node_modules/crypto-browserify");
|
|
829
|
+
}, {
|
|
830
|
+
"./helpers": 4,
|
|
831
|
+
buffer: 3,
|
|
832
|
+
lYpoI2: 11
|
|
833
|
+
}],
|
|
834
|
+
7: [function(e, l, t) {
|
|
835
|
+
(function(e, t, n, r, o, i, u, s, f) {
|
|
836
|
+
var a;
|
|
837
|
+
l.exports = a || function(e) {
|
|
838
|
+
for (var t, n = new Array(e), r = 0; r < e; r++) 0 == (3 & r) && (t = 4294967296 * Math.random()), n[r] = t >>> ((3 & r) << 3) & 255;
|
|
839
|
+
return n;
|
|
840
|
+
};
|
|
841
|
+
}).call(this, e("lYpoI2"), "undefined" != typeof self ? self : "undefined" != typeof window ? window : {}, e("buffer").Buffer, arguments[3], arguments[4], arguments[5], arguments[6], "/node_modules/gulp-browserify/node_modules/crypto-browserify/rng.js", "/node_modules/gulp-browserify/node_modules/crypto-browserify");
|
|
842
|
+
}, {
|
|
843
|
+
buffer: 3,
|
|
844
|
+
lYpoI2: 11
|
|
845
|
+
}],
|
|
846
|
+
8: [function(c, d, e) {
|
|
847
|
+
(function(e, t, n, r, o, s, a, f, l) {
|
|
848
|
+
var i = c("./helpers");
|
|
849
|
+
function u(l, c) {
|
|
850
|
+
l[c >> 5] |= 128 << 24 - c % 32, l[15 + (c + 64 >> 9 << 4)] = c;
|
|
851
|
+
for (var e, t, n, r = Array(80), o = 1732584193, i = -271733879, u = -1732584194, s = 271733878, d = -1009589776, h = 0; h < l.length; h += 16) {
|
|
852
|
+
for (var p = o, g = i, y = u, w = s, b = d, a = 0; a < 80; a++) {
|
|
853
|
+
r[a] = a < 16 ? l[h + a] : v(r[a - 3] ^ r[a - 8] ^ r[a - 14] ^ r[a - 16], 1);
|
|
854
|
+
var f = m(m(v(o, 5), (f = i, t = u, n = s, (e = a) < 20 ? f & t | ~f & n : !(e < 40) && e < 60 ? f & t | f & n | t & n : f ^ t ^ n)), m(m(d, r[a]), (e = a) < 20 ? 1518500249 : e < 40 ? 1859775393 : e < 60 ? -1894007588 : -899497514)), d = s, s = u, u = v(i, 30), i = o, o = f;
|
|
855
|
+
}
|
|
856
|
+
o = m(o, p), i = m(i, g), u = m(u, y), s = m(s, w), d = m(d, b);
|
|
857
|
+
}
|
|
858
|
+
return Array(o, i, u, s, d);
|
|
859
|
+
}
|
|
860
|
+
function m(e, t) {
|
|
861
|
+
var n = (65535 & e) + (65535 & t);
|
|
862
|
+
return (e >> 16) + (t >> 16) + (n >> 16) << 16 | 65535 & n;
|
|
863
|
+
}
|
|
864
|
+
function v(e, t) {
|
|
865
|
+
return e << t | e >>> 32 - t;
|
|
866
|
+
}
|
|
867
|
+
d.exports = function(e) {
|
|
868
|
+
return i.hash(e, u, 20, !0);
|
|
869
|
+
};
|
|
870
|
+
}).call(this, c("lYpoI2"), "undefined" != typeof self ? self : "undefined" != typeof window ? window : {}, c("buffer").Buffer, arguments[3], arguments[4], arguments[5], arguments[6], "/node_modules/gulp-browserify/node_modules/crypto-browserify/sha.js", "/node_modules/gulp-browserify/node_modules/crypto-browserify");
|
|
871
|
+
}, {
|
|
872
|
+
"./helpers": 4,
|
|
873
|
+
buffer: 3,
|
|
874
|
+
lYpoI2: 11
|
|
875
|
+
}],
|
|
876
|
+
9: [function(c, d, e) {
|
|
877
|
+
(function(e, t, n, r, u, s, a, f, l) {
|
|
878
|
+
function b(e, t) {
|
|
879
|
+
var n = (65535 & e) + (65535 & t);
|
|
880
|
+
return (e >> 16) + (t >> 16) + (n >> 16) << 16 | 65535 & n;
|
|
881
|
+
}
|
|
882
|
+
function o(e, l) {
|
|
883
|
+
var c, d = new Array(1116352408, 1899447441, 3049323471, 3921009573, 961987163, 1508970993, 2453635748, 2870763221, 3624381080, 310598401, 607225278, 1426881987, 1925078388, 2162078206, 2614888103, 3248222580, 3835390401, 4022224774, 264347078, 604807628, 770255983, 1249150122, 1555081692, 1996064986, 2554220882, 2821834349, 2952996808, 3210313671, 3336571891, 3584528711, 113926993, 338241895, 666307205, 773529912, 1294757372, 1396182291, 1695183700, 1986661051, 2177026350, 2456956037, 2730485921, 2820302411, 3259730800, 3345764771, 3516065817, 3600352804, 4094571909, 275423344, 430227734, 506948616, 659060556, 883997877, 958139571, 1322822218, 1537002063, 1747873779, 1955562222, 2024104815, 2227730452, 2361852424, 2428436474, 2756734187, 3204031479, 3329325298), t = new Array(1779033703, 3144134277, 1013904242, 2773480762, 1359893119, 2600822924, 528734635, 1541459225), n = new Array(64);
|
|
884
|
+
e[l >> 5] |= 128 << 24 - l % 32, e[15 + (l + 64 >> 9 << 4)] = l;
|
|
885
|
+
for (var r, o, h = 0; h < e.length; h += 16) {
|
|
886
|
+
for (var i = t[0], u = t[1], s = t[2], p = t[3], a = t[4], g = t[5], y = t[6], w = t[7], f = 0; f < 64; f++) n[f] = f < 16 ? e[f + h] : b(b(b((o = n[f - 2], m(o, 17) ^ m(o, 19) ^ v(o, 10)), n[f - 7]), (o = n[f - 15], m(o, 7) ^ m(o, 18) ^ v(o, 3))), n[f - 16]), c = b(b(b(b(w, m(o = a, 6) ^ m(o, 11) ^ m(o, 25)), a & g ^ ~a & y), d[f]), n[f]), r = b(m(r = i, 2) ^ m(r, 13) ^ m(r, 22), i & u ^ i & s ^ u & s), w = y, y = g, g = a, a = b(p, c), p = s, s = u, u = i, i = b(c, r);
|
|
887
|
+
t[0] = b(i, t[0]), t[1] = b(u, t[1]), t[2] = b(s, t[2]), t[3] = b(p, t[3]), t[4] = b(a, t[4]), t[5] = b(g, t[5]), t[6] = b(y, t[6]), t[7] = b(w, t[7]);
|
|
888
|
+
}
|
|
889
|
+
return t;
|
|
890
|
+
}
|
|
891
|
+
var i = c("./helpers"), m = function(e, t) {
|
|
892
|
+
return e >>> t | e << 32 - t;
|
|
893
|
+
}, v = function(e, t) {
|
|
894
|
+
return e >>> t;
|
|
895
|
+
};
|
|
896
|
+
d.exports = function(e) {
|
|
897
|
+
return i.hash(e, o, 32, !0);
|
|
898
|
+
};
|
|
899
|
+
}).call(this, c("lYpoI2"), "undefined" != typeof self ? self : "undefined" != typeof window ? window : {}, c("buffer").Buffer, arguments[3], arguments[4], arguments[5], arguments[6], "/node_modules/gulp-browserify/node_modules/crypto-browserify/sha256.js", "/node_modules/gulp-browserify/node_modules/crypto-browserify");
|
|
900
|
+
}, {
|
|
901
|
+
"./helpers": 4,
|
|
902
|
+
buffer: 3,
|
|
903
|
+
lYpoI2: 11
|
|
904
|
+
}],
|
|
905
|
+
10: [function(e, t, f) {
|
|
906
|
+
(function(e, t, n, r, o, i, u, s, a) {
|
|
907
|
+
f.read = function(e, t, n, r, o) {
|
|
908
|
+
var i, u, l = 8 * o - r - 1, c = (1 << l) - 1, d = c >> 1, s = -7, a = n ? o - 1 : 0, f = n ? -1 : 1, o = e[t + a];
|
|
909
|
+
for (a += f, i = o & (1 << -s) - 1, o >>= -s, s += l; 0 < s; i = 256 * i + e[t + a], a += f, s -= 8);
|
|
910
|
+
for (u = i & (1 << -s) - 1, i >>= -s, s += r; 0 < s; u = 256 * u + e[t + a], a += f, s -= 8);
|
|
911
|
+
if (0 === i) i = 1 - d;
|
|
912
|
+
else {
|
|
913
|
+
if (i === c) return u ? NaN : Infinity * (o ? -1 : 1);
|
|
914
|
+
u += Math.pow(2, r), i -= d;
|
|
915
|
+
}
|
|
916
|
+
return (o ? -1 : 1) * u * Math.pow(2, i - r);
|
|
917
|
+
}, f.write = function(e, t, l, n, r, c) {
|
|
918
|
+
var o, i, u = 8 * c - r - 1, s = (1 << u) - 1, a = s >> 1, d = 23 === r ? Math.pow(2, -24) - Math.pow(2, -77) : 0, f = n ? 0 : c - 1, h = n ? 1 : -1, c = t < 0 || 0 === t && 1 / t < 0 ? 1 : 0;
|
|
919
|
+
for (t = Math.abs(t), isNaN(t) || t === Infinity ? (i = isNaN(t) ? 1 : 0, o = s) : (o = Math.floor(Math.log(t) / Math.LN2), t * (n = Math.pow(2, -o)) < 1 && (o--, n *= 2), 2 <= (t += 1 <= o + a ? d / n : d * Math.pow(2, 1 - a)) * n && (o++, n /= 2), s <= o + a ? (i = 0, o = s) : 1 <= o + a ? (i = (t * n - 1) * Math.pow(2, r), o += a) : (i = t * Math.pow(2, a - 1) * Math.pow(2, r), o = 0)); 8 <= r; e[l + f] = 255 & i, f += h, i /= 256, r -= 8);
|
|
920
|
+
for (o = o << r | i, u += r; 0 < u; e[l + f] = 255 & o, f += h, o /= 256, u -= 8);
|
|
921
|
+
e[l + f - h] |= 128 * c;
|
|
922
|
+
};
|
|
923
|
+
}).call(this, e("lYpoI2"), "undefined" != typeof self ? self : "undefined" != typeof window ? window : {}, e("buffer").Buffer, arguments[3], arguments[4], arguments[5], arguments[6], "/node_modules/gulp-browserify/node_modules/ieee754/index.js", "/node_modules/gulp-browserify/node_modules/ieee754");
|
|
924
|
+
}, {
|
|
925
|
+
buffer: 3,
|
|
926
|
+
lYpoI2: 11
|
|
927
|
+
}],
|
|
928
|
+
11: [function(e, h, t) {
|
|
929
|
+
(function(e, t, n, r, o, f, l, c, d) {
|
|
930
|
+
var i, u, s;
|
|
931
|
+
function a() {}
|
|
932
|
+
(e = h.exports = {}).nextTick = (u = "undefined" != typeof window && window.setImmediate, s = "undefined" != typeof window && window.postMessage && window.addEventListener, u ? function(e) {
|
|
933
|
+
return window.setImmediate(e);
|
|
934
|
+
} : s ? (i = [], window.addEventListener("message", function(e) {
|
|
935
|
+
var t = e.source;
|
|
936
|
+
t !== window && null !== t || "process-tick" !== e.data || (e.stopPropagation(), 0 < i.length && i.shift()());
|
|
937
|
+
}, !0), function(e) {
|
|
938
|
+
i.push(e), window.postMessage("process-tick", "*");
|
|
939
|
+
}) : function(e) {
|
|
940
|
+
setTimeout(e, 0);
|
|
941
|
+
}), e.title = "browser", e.browser = !0, e.env = {}, e.argv = [], e.on = a, e.addListener = a, e.once = a, e.off = a, e.removeListener = a, e.removeAllListeners = a, e.emit = a, e.binding = function(e) {
|
|
942
|
+
throw new Error("process.binding is not supported");
|
|
943
|
+
}, e.cwd = function() {
|
|
944
|
+
return "/";
|
|
945
|
+
}, e.chdir = function(e) {
|
|
946
|
+
throw new Error("process.chdir is not supported");
|
|
947
|
+
};
|
|
948
|
+
}).call(this, e("lYpoI2"), "undefined" != typeof self ? self : "undefined" != typeof window ? window : {}, e("buffer").Buffer, arguments[3], arguments[4], arguments[5], arguments[6], "/node_modules/gulp-browserify/node_modules/process/browser.js", "/node_modules/gulp-browserify/node_modules/process");
|
|
949
|
+
}, {
|
|
950
|
+
buffer: 3,
|
|
951
|
+
lYpoI2: 11
|
|
952
|
+
}]
|
|
953
|
+
}, {}, [1])(1);
|
|
954
|
+
});
|
|
955
|
+
})))();
|
|
956
|
+
/**
|
|
957
|
+
* Deep clone a value, preserving function references and class instances.
|
|
958
|
+
* Unlike structuredClone, this handles objects that contain functions
|
|
959
|
+
* (e.g. CollectionConfig with target(), childCollections(), callbacks).
|
|
960
|
+
*/
|
|
961
|
+
function deepClone(value) {
|
|
962
|
+
if (value === null || value === void 0) return value;
|
|
963
|
+
if (typeof value === "function") return value;
|
|
964
|
+
if (typeof value !== "object") return value;
|
|
965
|
+
if (Array.isArray(value)) return value.map((item) => deepClone(item));
|
|
966
|
+
if (Object.getPrototypeOf(value) !== Object.prototype) return value;
|
|
967
|
+
const result = {};
|
|
968
|
+
for (const key of Object.keys(value)) result[key] = deepClone(value[key]);
|
|
969
|
+
return result;
|
|
970
|
+
}
|
|
971
|
+
function isObject(item) {
|
|
972
|
+
return !!item && typeof item === "object" && !Array.isArray(item);
|
|
973
|
+
}
|
|
974
|
+
function isPlainObject(obj) {
|
|
975
|
+
if (typeof obj !== "object" || obj === null || Array.isArray(obj)) return false;
|
|
976
|
+
return Object.getPrototypeOf(obj) === Object.prototype;
|
|
977
|
+
}
|
|
978
|
+
function mergeDeep(target, source, ignoreUndefined = false) {
|
|
979
|
+
if (!isObject(target)) return target;
|
|
980
|
+
const output = { ...target };
|
|
981
|
+
if (!isObject(source)) return output;
|
|
982
|
+
for (const key in source) {
|
|
983
|
+
if (key === "__proto__" || key === "constructor" || key === "prototype") continue;
|
|
984
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
985
|
+
const sourceValue = source[key];
|
|
986
|
+
const outputValue = output[key];
|
|
987
|
+
if (ignoreUndefined && sourceValue === void 0) continue;
|
|
988
|
+
if (sourceValue instanceof Date) output[key] = new Date(sourceValue.getTime());
|
|
989
|
+
else if (Array.isArray(sourceValue)) if (Array.isArray(outputValue)) if (!(sourceValue.some(isPlainObject) || outputValue.some(isPlainObject))) output[key] = [...sourceValue];
|
|
990
|
+
else {
|
|
991
|
+
const newArray = [];
|
|
992
|
+
const maxLength = Math.max(outputValue.length, sourceValue.length);
|
|
993
|
+
for (let i = 0; i < maxLength; i++) {
|
|
994
|
+
const sourceItem = sourceValue[i];
|
|
995
|
+
const targetItem = outputValue[i];
|
|
996
|
+
if (i >= sourceValue.length) newArray[i] = targetItem;
|
|
997
|
+
else if (i >= outputValue.length) newArray[i] = sourceItem;
|
|
998
|
+
else if (sourceItem === null) newArray[i] = targetItem;
|
|
999
|
+
else if (isPlainObject(sourceItem) && isPlainObject(targetItem)) newArray[i] = mergeDeep(targetItem, sourceItem, ignoreUndefined);
|
|
1000
|
+
else newArray[i] = sourceItem;
|
|
1001
|
+
}
|
|
1002
|
+
output[key] = newArray;
|
|
1003
|
+
}
|
|
1004
|
+
else output[key] = [...sourceValue];
|
|
1005
|
+
else if (isPlainObject(sourceValue)) if (isPlainObject(outputValue)) output[key] = mergeDeep(outputValue, sourceValue, ignoreUndefined);
|
|
1006
|
+
else output[key] = sourceValue;
|
|
1007
|
+
else if (isObject(sourceValue)) output[key] = sourceValue;
|
|
1008
|
+
else output[key] = sourceValue;
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
return output;
|
|
1012
|
+
}
|
|
1013
|
+
function removeFunctions(o) {
|
|
1014
|
+
if (o === void 0) return void 0;
|
|
1015
|
+
if (o === null) return null;
|
|
1016
|
+
if (typeof o === "object") {
|
|
1017
|
+
if (Array.isArray(o)) return o.map((v) => removeFunctions(v));
|
|
1018
|
+
if (!isPlainObject(o)) return o;
|
|
1019
|
+
return Object.entries(o).filter(([_, value]) => typeof value !== "function").map(([key, value]) => {
|
|
1020
|
+
if (Array.isArray(value)) return { [key]: value.map((v) => removeFunctions(v)) };
|
|
1021
|
+
else if (typeof value === "object") return { [key]: removeFunctions(value) };
|
|
1022
|
+
else return { [key]: value };
|
|
1023
|
+
}).reduce((a, b) => ({
|
|
1024
|
+
...a,
|
|
1025
|
+
...b
|
|
1026
|
+
}), {});
|
|
1027
|
+
}
|
|
1028
|
+
return o;
|
|
1029
|
+
}
|
|
1030
|
+
//#endregion
|
|
1031
|
+
//#region ../utils/src/sha1.ts
|
|
1032
|
+
/**
|
|
1033
|
+
* Minimal SHA-1 implementation that runs in both Node and the browser.
|
|
1034
|
+
*
|
|
1035
|
+
* This exists because generated Postgres policy names embed a SHA-1 digest of
|
|
1036
|
+
* the security rule. The DDL generator runs on the server (where `node:crypto`
|
|
1037
|
+
* is available) but the Studio has to derive the same names in the browser to
|
|
1038
|
+
* tell a policy it generated apart from one it did not. `node:crypto` cannot be
|
|
1039
|
+
* bundled for the browser, so the shared derivation needs a portable digest.
|
|
1040
|
+
*
|
|
1041
|
+
* SHA-1 is used purely to name things deterministically — never for security.
|
|
1042
|
+
* The output is byte-identical to `createHash("sha1").update(str).digest("hex")`,
|
|
1043
|
+
* which `sha1.test.ts` pins against `node:crypto` directly.
|
|
1044
|
+
*/
|
|
1045
|
+
/** Rotate a 32-bit word left by `n` bits. */
|
|
1046
|
+
function rotl(value, n) {
|
|
1047
|
+
return value << n | value >>> 32 - n;
|
|
1048
|
+
}
|
|
1049
|
+
/**
|
|
1050
|
+
* SHA-1 digest of a string, hex-encoded.
|
|
1051
|
+
*
|
|
1052
|
+
* The input is encoded as UTF-8, matching Node's default handling of strings
|
|
1053
|
+
* passed to `hash.update(str)`.
|
|
1054
|
+
*/
|
|
1055
|
+
function sha1Hex(input) {
|
|
1056
|
+
const bytes = Array.from(new TextEncoder().encode(input));
|
|
1057
|
+
const bitLength = bytes.length * 8;
|
|
1058
|
+
bytes.push(128);
|
|
1059
|
+
while (bytes.length % 64 !== 56) bytes.push(0);
|
|
1060
|
+
const hi = Math.floor(bitLength / 4294967296);
|
|
1061
|
+
const lo = bitLength >>> 0;
|
|
1062
|
+
bytes.push(hi >>> 24 & 255, hi >>> 16 & 255, hi >>> 8 & 255, hi & 255);
|
|
1063
|
+
bytes.push(lo >>> 24 & 255, lo >>> 16 & 255, lo >>> 8 & 255, lo & 255);
|
|
1064
|
+
let h0 = 1732584193;
|
|
1065
|
+
let h1 = 4023233417;
|
|
1066
|
+
let h2 = 2562383102;
|
|
1067
|
+
let h3 = 271733878;
|
|
1068
|
+
let h4 = 3285377520;
|
|
1069
|
+
const w = new Array(80);
|
|
1070
|
+
for (let offset = 0; offset < bytes.length; offset += 64) {
|
|
1071
|
+
for (let i = 0; i < 16; i++) {
|
|
1072
|
+
const j = offset + i * 4;
|
|
1073
|
+
w[i] = bytes[j] << 24 | bytes[j + 1] << 16 | bytes[j + 2] << 8 | bytes[j + 3] | 0;
|
|
1074
|
+
}
|
|
1075
|
+
for (let i = 16; i < 80; i++) w[i] = rotl(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
|
|
1076
|
+
let a = h0;
|
|
1077
|
+
let b = h1;
|
|
1078
|
+
let c = h2;
|
|
1079
|
+
let d = h3;
|
|
1080
|
+
let e = h4;
|
|
1081
|
+
for (let i = 0; i < 80; i++) {
|
|
1082
|
+
let f;
|
|
1083
|
+
let k;
|
|
1084
|
+
if (i < 20) {
|
|
1085
|
+
f = b & c | ~b & d;
|
|
1086
|
+
k = 1518500249;
|
|
1087
|
+
} else if (i < 40) {
|
|
1088
|
+
f = b ^ c ^ d;
|
|
1089
|
+
k = 1859775393;
|
|
1090
|
+
} else if (i < 60) {
|
|
1091
|
+
f = b & c | b & d | c & d;
|
|
1092
|
+
k = 2400959708;
|
|
1093
|
+
} else {
|
|
1094
|
+
f = b ^ c ^ d;
|
|
1095
|
+
k = 3395469782;
|
|
1096
|
+
}
|
|
1097
|
+
const temp = rotl(a, 5) + f + e + k + w[i] | 0;
|
|
1098
|
+
e = d;
|
|
1099
|
+
d = c;
|
|
1100
|
+
c = rotl(b, 30);
|
|
1101
|
+
b = a;
|
|
1102
|
+
a = temp;
|
|
1103
|
+
}
|
|
1104
|
+
h0 = h0 + a | 0;
|
|
1105
|
+
h1 = h1 + b | 0;
|
|
1106
|
+
h2 = h2 + c | 0;
|
|
1107
|
+
h3 = h3 + d | 0;
|
|
1108
|
+
h4 = h4 + e | 0;
|
|
1109
|
+
}
|
|
1110
|
+
return [
|
|
1111
|
+
h0,
|
|
1112
|
+
h1,
|
|
1113
|
+
h2,
|
|
1114
|
+
h3,
|
|
1115
|
+
h4
|
|
1116
|
+
].map((word) => (word >>> 0).toString(16).padStart(8, "0")).join("");
|
|
1117
|
+
}
|
|
1118
|
+
//#endregion
|
|
1119
|
+
//#region ../utils/src/policy-names.ts
|
|
1120
|
+
/**
|
|
1121
|
+
* Naming of the Postgres policies generated from a collection's security rules.
|
|
1122
|
+
*
|
|
1123
|
+
* A rule without an explicit `name` is compiled to `<table>_<op>_<hash>`, where
|
|
1124
|
+
* the hash covers the rule's semantics. The Studio needs the same names to tell
|
|
1125
|
+
* "this policy came from your code" apart from "someone wrote this in SQL" —
|
|
1126
|
+
* without them it treats generated policies as foreign and offers to import
|
|
1127
|
+
* them back into the codebase they came from.
|
|
1128
|
+
*
|
|
1129
|
+
* This is the single definition of that naming. The DDL and Drizzle generators
|
|
1130
|
+
* both derive names from here, so a change cannot silently rename every policy
|
|
1131
|
+
* in every deployed database while the UI keeps matching the old ones.
|
|
1132
|
+
*/
|
|
1133
|
+
/** Stable digest of the parts of a rule that determine what the policy does. */
|
|
1134
|
+
function getPolicyNameHash(rule) {
|
|
1135
|
+
return sha1Hex(JSON.stringify({
|
|
1136
|
+
a: rule.access,
|
|
1137
|
+
m: rule.mode,
|
|
1138
|
+
op: rule.operation,
|
|
1139
|
+
ops: rule.operations?.slice().sort(),
|
|
1140
|
+
own: rule.ownerField,
|
|
1141
|
+
rol: rule.roles?.slice().sort(),
|
|
1142
|
+
pg: rule.pgRoles?.slice().sort(),
|
|
1143
|
+
u: rule.using,
|
|
1144
|
+
w: rule.withCheck,
|
|
1145
|
+
c: rule.condition,
|
|
1146
|
+
ch: rule.check
|
|
1147
|
+
})).substring(0, 7);
|
|
1148
|
+
}
|
|
1149
|
+
/** The operations a rule expands to — `operations` wins over `operation`. */
|
|
1150
|
+
function getPolicyOperations(rule) {
|
|
1151
|
+
return rule.operations && rule.operations.length > 0 ? rule.operations : [rule.operation ?? "all"];
|
|
1152
|
+
}
|
|
1153
|
+
/**
|
|
1154
|
+
* Every Postgres policy name a single rule compiles to — one per operation.
|
|
1155
|
+
*
|
|
1156
|
+
* @param rule The security rule as written in the collection config.
|
|
1157
|
+
* @param tableName The rule's table (see `getTableName` in `@rebasepro/common`).
|
|
1158
|
+
*/
|
|
1159
|
+
function getPolicyNamesForRule(rule, tableName) {
|
|
1160
|
+
const ops = getPolicyOperations(rule);
|
|
1161
|
+
const ruleHash = getPolicyNameHash(rule);
|
|
1162
|
+
return ops.map((op, opIdx) => rule.name ? ops.length > 1 ? `${rule.name}_${op}` : rule.name : `${tableName}_${op}_${ruleHash}${ops.length > 1 ? `_${opIdx}` : ""}`);
|
|
1163
|
+
}
|
|
1164
|
+
//#endregion
|
|
1165
|
+
//#region ../utils/src/names.ts
|
|
1166
|
+
/**
|
|
1167
|
+
* Generates a foreign key column name from a given string, typically a collection slug or name.
|
|
1168
|
+
* It converts the name to snake_case, attempts to singularize it by removing a trailing 's'
|
|
1169
|
+
* (a common convention for collection names), and appends '_id'.
|
|
1170
|
+
*
|
|
1171
|
+
* @param name The base name to convert to a foreign key.
|
|
1172
|
+
* @returns A foreign key name in the format 'singular_name_id'.
|
|
1173
|
+
*
|
|
1174
|
+
* @example
|
|
1175
|
+
* // returns "user_id"
|
|
1176
|
+
* generateForeignKeyName("users")
|
|
1177
|
+
*
|
|
1178
|
+
* @example
|
|
1179
|
+
* // returns "post_id"
|
|
1180
|
+
* generateForeignKeyName("posts")
|
|
1181
|
+
*
|
|
1182
|
+
* @example
|
|
1183
|
+
* // returns "product_id"
|
|
1184
|
+
* generateForeignKeyName("Product")
|
|
1185
|
+
*
|
|
1186
|
+
*/
|
|
1187
|
+
function generateForeignKeyName(name) {
|
|
1188
|
+
const snakeCaseName = toSnakeCase(name);
|
|
1189
|
+
return `${snakeCaseName.endsWith("s") ? snakeCaseName.slice(0, -1) : snakeCaseName}_id`;
|
|
1190
|
+
}
|
|
1191
|
+
//#endregion
|
|
1192
|
+
//#region ../common/src/util/entities.ts
|
|
1193
|
+
/**
|
|
1194
|
+
* Update the automatic values in a entity before save
|
|
1195
|
+
* @group Driver
|
|
1196
|
+
*/
|
|
1197
|
+
function updateDateAutoValues({ inputValues, properties, status, timestampNowValue }) {
|
|
1198
|
+
return traverseValuesProperties(inputValues, properties, (inputValue, property) => {
|
|
1199
|
+
if (property.type === "date") if (status === "existing" && property.autoValue === "on_update") return timestampNowValue;
|
|
1200
|
+
else if ((status === "new" || status === "copy") && (property.autoValue === "on_update" || property.autoValue === "on_create")) return timestampNowValue;
|
|
1201
|
+
else return inputValue;
|
|
1202
|
+
else return inputValue;
|
|
1203
|
+
}) ?? {};
|
|
1204
|
+
}
|
|
1205
|
+
/**
|
|
1206
|
+
* Normalize a value into a proper EntityRelation instance.
|
|
1207
|
+
* Handles EntityRelation class instances, and plain objects
|
|
1208
|
+
* with `__type === "relation"` or an `isEntityRelation()` method.
|
|
1209
|
+
*
|
|
1210
|
+
* When `propertyType` is `"relation"`, also accepts plain objects that
|
|
1211
|
+
* have `id` and `path` fields — these are relation-shaped objects from
|
|
1212
|
+
* edge cases in the data pipeline (REST fallback, stale cache, custom data source).
|
|
1213
|
+
*
|
|
1214
|
+
* Returns null if the value cannot be coerced.
|
|
1215
|
+
*/
|
|
1216
|
+
function normalizeToEntityRelation(value, propertyType) {
|
|
1217
|
+
if (value instanceof EntityRelation) return value;
|
|
1218
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
|
1219
|
+
const obj = value;
|
|
1220
|
+
if (!(obj.__type === "relation" || obj.__type === "reference" || typeof obj.isEntityRelation === "function" && obj.isEntityRelation() || typeof obj.isEntityReference === "function" && obj.isEntityReference() || propertyType === "relation" && typeof obj.id !== "undefined" && typeof obj.path === "string")) return null;
|
|
1221
|
+
return new EntityRelation(obj.id, obj.path, obj.data);
|
|
1222
|
+
}
|
|
1223
|
+
function traverseValuesProperties(inputValues, properties, operation) {
|
|
1224
|
+
const safeInputValues = inputValues ?? {};
|
|
1225
|
+
const result = mergeDeep(safeInputValues, Object.entries(properties).map(([key, property]) => {
|
|
1226
|
+
const updatedValue = traverseValueProperty(safeInputValues && safeInputValues[key], property, operation);
|
|
1227
|
+
if (updatedValue === null) return null;
|
|
1228
|
+
if (updatedValue === void 0) return void 0;
|
|
1229
|
+
return { [key]: updatedValue };
|
|
1230
|
+
}).reduce((a, b) => ({
|
|
1231
|
+
...a,
|
|
1232
|
+
...b
|
|
1233
|
+
}), {}));
|
|
1234
|
+
if (!result || Object.keys(result).length === 0) return void 0;
|
|
1235
|
+
return result;
|
|
1236
|
+
}
|
|
1237
|
+
function traverseValueProperty(inputValue, property, operation) {
|
|
1238
|
+
let value;
|
|
1239
|
+
if (property.type === "map" && property.properties) value = traverseValuesProperties(inputValue, property.properties, operation);
|
|
1240
|
+
else if (property.type === "array") {
|
|
1241
|
+
const of = property.of;
|
|
1242
|
+
if (of && Array.isArray(inputValue) && !Array.isArray(of)) value = inputValue.map((e) => traverseValueProperty(e, of, operation));
|
|
1243
|
+
else if (of && Array.isArray(inputValue) && Array.isArray(of)) value = inputValue.map((e, i) => {
|
|
1244
|
+
if (i < of.length) return traverseValueProperty(e, of[i], operation);
|
|
1245
|
+
return null;
|
|
1246
|
+
}).filter(Boolean);
|
|
1247
|
+
else if (property.oneOf && Array.isArray(inputValue)) {
|
|
1248
|
+
const typeField = property.oneOf?.typeField ?? "type";
|
|
1249
|
+
const valueField = property.oneOf?.valueField ?? "value";
|
|
1250
|
+
value = inputValue.map((e) => {
|
|
1251
|
+
if (e === null) return null;
|
|
1252
|
+
if (typeof e !== "object") return e;
|
|
1253
|
+
const rec = e;
|
|
1254
|
+
const type = rec[typeField];
|
|
1255
|
+
const childProperty = property.oneOf?.properties[type];
|
|
1256
|
+
if (!type || !childProperty) return e;
|
|
1257
|
+
return {
|
|
1258
|
+
[typeField]: type,
|
|
1259
|
+
[valueField]: traverseValueProperty(rec[valueField], childProperty, operation)
|
|
1260
|
+
};
|
|
1261
|
+
});
|
|
1262
|
+
} else value = inputValue;
|
|
1263
|
+
} else value = operation(inputValue, property);
|
|
1264
|
+
return value;
|
|
1265
|
+
}
|
|
1266
|
+
/**
|
|
1267
|
+
* Create a lightweight relation stub for CMS views.
|
|
1268
|
+
* Replaces inline `{ id, path, __type: "relation" }` object literals.
|
|
1269
|
+
*/
|
|
1270
|
+
function createRelationRef(id, path) {
|
|
1271
|
+
return {
|
|
1272
|
+
id,
|
|
1273
|
+
path,
|
|
1274
|
+
__type: "relation"
|
|
1275
|
+
};
|
|
1276
|
+
}
|
|
1277
|
+
/**
|
|
1278
|
+
* Create a hydrated relation reference that includes the full entity data.
|
|
1279
|
+
* Used when entity data has been pre-fetched (e.g., via batch loading or JOINs).
|
|
1280
|
+
*/
|
|
1281
|
+
function createRelationRefWithData(id, path, data) {
|
|
1282
|
+
return {
|
|
1283
|
+
id,
|
|
1284
|
+
path,
|
|
1285
|
+
__type: "relation",
|
|
1286
|
+
data
|
|
1287
|
+
};
|
|
1288
|
+
}
|
|
1289
|
+
/**
|
|
1290
|
+
* Derive a row's address from its key columns.
|
|
1291
|
+
*
|
|
1292
|
+
* Single key → the value as a string. Composite → each part joined by
|
|
1293
|
+
* {@link COMPOSITE_ID_SEPARATOR}, in primary-key order, which is what
|
|
1294
|
+
* {@link parseIdValues} expects to invert.
|
|
1295
|
+
*/
|
|
1296
|
+
function buildCompositeId(values, primaryKeys) {
|
|
1297
|
+
if (primaryKeys.length === 0) return "";
|
|
1298
|
+
if (primaryKeys.length === 1) return String(values[primaryKeys[0].fieldName] ?? "");
|
|
1299
|
+
return primaryKeys.map((pk) => String(values[pk.fieldName] ?? "")).join(":::");
|
|
1300
|
+
}
|
|
1301
|
+
/**
|
|
1302
|
+
* Invert {@link buildCompositeId}: turn an address back into key columns, each
|
|
1303
|
+
* coerced to the type its column actually round-trips as.
|
|
1304
|
+
*
|
|
1305
|
+
* This is the boundary where a URL segment becomes a query parameter, so a
|
|
1306
|
+
* malformed address must throw rather than silently produce a query that
|
|
1307
|
+
* matches the wrong row (or none).
|
|
1308
|
+
*/
|
|
1309
|
+
function parseIdValues(idValue, primaryKeys) {
|
|
1310
|
+
const result = {};
|
|
1311
|
+
if (primaryKeys.length === 0) return result;
|
|
1312
|
+
if (primaryKeys.length === 1) {
|
|
1313
|
+
const pk = primaryKeys[0];
|
|
1314
|
+
if (pk.type === "number" && !pk.isUUID) {
|
|
1315
|
+
const parsed = typeof idValue === "number" ? idValue : parseInt(String(idValue), 10);
|
|
1316
|
+
if (isNaN(parsed)) throw new Error(`Invalid numeric ID: ${idValue}`);
|
|
1317
|
+
result[pk.fieldName] = parsed;
|
|
1318
|
+
} else result[pk.fieldName] = String(idValue);
|
|
1319
|
+
return result;
|
|
1320
|
+
}
|
|
1321
|
+
const parts = String(idValue).split(":::");
|
|
1322
|
+
if (parts.length !== primaryKeys.length) throw new Error(`Composite ID parts mismatch. Expected ${primaryKeys.length}, got ${parts.length} for ID: ${idValue}`);
|
|
1323
|
+
for (let i = 0; i < primaryKeys.length; i++) {
|
|
1324
|
+
const pk = primaryKeys[i];
|
|
1325
|
+
const val = parts[i];
|
|
1326
|
+
if (pk.type === "number" && !pk.isUUID) {
|
|
1327
|
+
const parsed = parseInt(val, 10);
|
|
1328
|
+
if (isNaN(parsed)) throw new Error(`Invalid numeric ID component: ${val}`);
|
|
1329
|
+
result[pk.fieldName] = parsed;
|
|
1330
|
+
} else result[pk.fieldName] = val;
|
|
1331
|
+
}
|
|
1332
|
+
return result;
|
|
1333
|
+
}
|
|
1334
|
+
/**
|
|
1335
|
+
* The primary keys of a collection, as declared by its properties.
|
|
1336
|
+
*
|
|
1337
|
+
* This is the only tier both sides can read, because it is the only one written
|
|
1338
|
+
* in the config: the postgres driver can also infer keys from the Drizzle
|
|
1339
|
+
* schema, which the browser never sees and is never sent — the admin compiles
|
|
1340
|
+
* the collection files into its own bundle rather than being served them. A key
|
|
1341
|
+
* that lives only in the Drizzle schema is therefore invisible here, and the
|
|
1342
|
+
* server says so at boot (`warnOnKeysTheAdminCannotResolve`) naming the `isId`
|
|
1343
|
+
* to add.
|
|
1344
|
+
*
|
|
1345
|
+
* Returns an empty array when a collection declares none, which callers must
|
|
1346
|
+
* treat as "not addressable" rather than defaulting to `id`: guessing a key
|
|
1347
|
+
* that is not the real one produces confidently wrong addresses.
|
|
1348
|
+
*/
|
|
1349
|
+
function getDeclaredPrimaryKeys(collection) {
|
|
1350
|
+
const properties = collection.properties;
|
|
1351
|
+
if (!properties) return [];
|
|
1352
|
+
const keys = [];
|
|
1353
|
+
for (const [fieldName, propRaw] of Object.entries(properties)) {
|
|
1354
|
+
const prop = propRaw;
|
|
1355
|
+
if (!prop || typeof prop !== "object") continue;
|
|
1356
|
+
if (!("isId" in prop) || !prop.isId) continue;
|
|
1357
|
+
keys.push({
|
|
1358
|
+
fieldName,
|
|
1359
|
+
type: prop.type === "number" ? "number" : "string",
|
|
1360
|
+
isUUID: prop.isId === "uuid"
|
|
1361
|
+
});
|
|
1362
|
+
}
|
|
1363
|
+
return keys;
|
|
1364
|
+
}
|
|
1365
|
+
/**
|
|
1366
|
+
* The keys to address a collection's rows with, resolved the way the driver
|
|
1367
|
+
* resolves them — minus the tier the browser cannot reach.
|
|
1368
|
+
*
|
|
1369
|
+
* The postgres driver tries, in order: properties marked `isId`; the primary
|
|
1370
|
+
* keys of the Drizzle schema; and finally a column literally named `id`. Only
|
|
1371
|
+
* the first and last are visible in a `CollectionConfig`, which is what both
|
|
1372
|
+
* sides share.
|
|
1373
|
+
*
|
|
1374
|
+
* So the two agree except on a collection that declares no `isId` and whose key
|
|
1375
|
+
* is known only to Drizzle. There, the driver reads the real key, and this
|
|
1376
|
+
* either resolves nothing (reported to the console by the caller) or — if the
|
|
1377
|
+
* table happens to have an unrelated `id` property — resolves `id`, which is
|
|
1378
|
+
* the wrong key and cannot be detected from here: the addresses look right and
|
|
1379
|
+
* route wrong. Only the config can settle it, so the server names both cases
|
|
1380
|
+
* at boot (`warnOnKeysTheAdminCannotResolve`) with the `isId` to add.
|
|
1381
|
+
*/
|
|
1382
|
+
function resolvePrimaryKeys(collection) {
|
|
1383
|
+
const declared = getDeclaredPrimaryKeys(collection);
|
|
1384
|
+
if (declared.length > 0) return declared;
|
|
1385
|
+
const idProp = collection.properties?.id;
|
|
1386
|
+
if (idProp && typeof idProp === "object") return [{
|
|
1387
|
+
fieldName: "id",
|
|
1388
|
+
type: idProp.type === "number" ? "number" : "string"
|
|
1389
|
+
}];
|
|
1390
|
+
return [];
|
|
1391
|
+
}
|
|
1392
|
+
//#endregion
|
|
1393
|
+
//#region ../common/src/util/enums.ts
|
|
1394
|
+
function enumToObjectEntries(enumValues) {
|
|
1395
|
+
if (Array.isArray(enumValues)) return enumValues;
|
|
1396
|
+
else return Object.entries(enumValues).map(([id, value]) => {
|
|
1397
|
+
if (typeof value === "string") return {
|
|
1398
|
+
id,
|
|
1399
|
+
label: value
|
|
1400
|
+
};
|
|
1401
|
+
else return {
|
|
1402
|
+
...value,
|
|
1403
|
+
id
|
|
1404
|
+
};
|
|
1405
|
+
});
|
|
1406
|
+
}
|
|
1407
|
+
//#endregion
|
|
1408
|
+
//#region ../common/src/util/relations.ts
|
|
1409
|
+
function sanitizeRelation(relation, sourceCollection, resolveCollection) {
|
|
1410
|
+
if (!relation.target) throw new Error("Relation is missing a `target` collection.");
|
|
1411
|
+
const rawTarget = relation.target;
|
|
1412
|
+
let targetCollection;
|
|
1413
|
+
if (typeof rawTarget === "string") {
|
|
1414
|
+
if (resolveCollection) targetCollection = resolveCollection(rawTarget);
|
|
1415
|
+
if (!targetCollection) targetCollection = {
|
|
1416
|
+
slug: rawTarget,
|
|
1417
|
+
name: rawTarget
|
|
1418
|
+
};
|
|
1419
|
+
} else if (typeof rawTarget === "function") {
|
|
1420
|
+
const evaluated = rawTarget();
|
|
1421
|
+
if (typeof evaluated === "string") {
|
|
1422
|
+
if (resolveCollection) targetCollection = resolveCollection(evaluated);
|
|
1423
|
+
if (!targetCollection) targetCollection = {
|
|
1424
|
+
slug: evaluated,
|
|
1425
|
+
name: evaluated
|
|
1426
|
+
};
|
|
1427
|
+
} else targetCollection = evaluated;
|
|
1428
|
+
} else if (rawTarget && typeof rawTarget === "object") targetCollection = rawTarget;
|
|
1429
|
+
if (!targetCollection) throw new Error("Relation is missing a valid `target` collection.");
|
|
1430
|
+
const newRelation = { ...relation };
|
|
1431
|
+
newRelation.target = () => {
|
|
1432
|
+
if (typeof rawTarget === "string") return resolveCollection && resolveCollection(rawTarget) || targetCollection;
|
|
1433
|
+
else if (typeof rawTarget === "function") {
|
|
1434
|
+
const evaluated = rawTarget();
|
|
1435
|
+
if (typeof evaluated === "string") return resolveCollection && resolveCollection(evaluated) || targetCollection;
|
|
1436
|
+
return evaluated;
|
|
1437
|
+
}
|
|
1438
|
+
return targetCollection;
|
|
1439
|
+
};
|
|
1440
|
+
if (!newRelation.relationName) newRelation.relationName = toSnakeCase(targetCollection.slug);
|
|
1441
|
+
if (!newRelation.direction) if (newRelation.foreignKeyOnTarget) newRelation.direction = "inverse";
|
|
1442
|
+
else if (newRelation.through) newRelation.direction = "owning";
|
|
1443
|
+
else if (newRelation.cardinality === "many") newRelation.direction = "inverse";
|
|
1444
|
+
else newRelation.direction = "owning";
|
|
1445
|
+
if (!newRelation.joinPath) {
|
|
1446
|
+
const sourceName = toSnakeCase(sourceCollection.slug ?? sourceCollection.name);
|
|
1447
|
+
if (newRelation.cardinality === "one" && newRelation.direction === "owning") {
|
|
1448
|
+
if (!newRelation.localKey) newRelation.localKey = generateForeignKeyName(newRelation.relationName);
|
|
1449
|
+
} else if (newRelation.cardinality === "one" && newRelation.direction === "inverse") {
|
|
1450
|
+
if (!newRelation.foreignKeyOnTarget) {
|
|
1451
|
+
let foundForeignKey = false;
|
|
1452
|
+
try {
|
|
1453
|
+
const targetRelations = getDataSourceCapabilities(targetCollection.engine).supportsRelations ? targetCollection.relations || [] : [];
|
|
1454
|
+
for (const targetRel of targetRelations) if (targetRel.direction === "owning" && targetRel.cardinality === "one" && targetRel.localKey) try {
|
|
1455
|
+
if (targetRel.target().slug === sourceCollection.slug) {
|
|
1456
|
+
newRelation.foreignKeyOnTarget = targetRel.localKey;
|
|
1457
|
+
foundForeignKey = true;
|
|
1458
|
+
break;
|
|
1459
|
+
}
|
|
1460
|
+
} catch (e) {
|
|
1461
|
+
continue;
|
|
1462
|
+
}
|
|
1463
|
+
} catch (e) {}
|
|
1464
|
+
if (!foundForeignKey) newRelation.foreignKeyOnTarget = generateForeignKeyName(newRelation.inverseRelationName ? toSnakeCase(newRelation.inverseRelationName) : sourceName);
|
|
1465
|
+
}
|
|
1466
|
+
} else if (newRelation.cardinality === "many" && newRelation.direction === "inverse") {
|
|
1467
|
+
let isManyToManyInverse = false;
|
|
1468
|
+
if (newRelation.inverseRelationName && !newRelation.foreignKeyOnTarget) try {
|
|
1469
|
+
const targetRelations = getDataSourceCapabilities(targetCollection.engine).supportsRelations ? targetCollection.relations || [] : [];
|
|
1470
|
+
for (const targetRel of targetRelations) if (targetRel.cardinality === "many" && (targetRel.direction === "owning" || !targetRel.direction) && targetRel.relationName === newRelation.inverseRelationName) {
|
|
1471
|
+
isManyToManyInverse = true;
|
|
1472
|
+
break;
|
|
1473
|
+
}
|
|
1474
|
+
if (!isManyToManyInverse && targetCollection.properties) for (const [propKey, prop] of Object.entries(targetCollection.properties)) {
|
|
1475
|
+
if (prop.type !== "relation") continue;
|
|
1476
|
+
const relProp = prop;
|
|
1477
|
+
if ((relProp.relationName || propKey) === newRelation.inverseRelationName && relProp.cardinality === "many" && (relProp.direction === "owning" || !relProp.direction)) {
|
|
1478
|
+
isManyToManyInverse = true;
|
|
1479
|
+
break;
|
|
1480
|
+
}
|
|
1481
|
+
}
|
|
1482
|
+
} catch (e) {}
|
|
1483
|
+
if (!isManyToManyInverse && !newRelation.foreignKeyOnTarget) newRelation.foreignKeyOnTarget = generateForeignKeyName(sourceName);
|
|
1484
|
+
} else if (newRelation.cardinality === "many" && newRelation.direction === "owning") {
|
|
1485
|
+
const sourceTableName = getTableName(sourceCollection);
|
|
1486
|
+
const targetTableName = getTableName(targetCollection);
|
|
1487
|
+
newRelation.through = {
|
|
1488
|
+
table: newRelation.through?.table ?? [sourceTableName, targetTableName].sort().join("_"),
|
|
1489
|
+
sourceColumn: newRelation.through?.sourceColumn ?? generateForeignKeyName(sourceName),
|
|
1490
|
+
targetColumn: newRelation.through?.targetColumn ?? generateForeignKeyName(newRelation.relationName)
|
|
1491
|
+
};
|
|
1492
|
+
}
|
|
1493
|
+
}
|
|
1494
|
+
if (newRelation.cardinality === "one" && newRelation.direction === "owning" && !newRelation.localKey && !newRelation.joinPath) throw new Error(`Configuration Error in relation from '${sourceCollection.name}': An 'owning' one-to-one relation requires a 'localKey'. Check the relation config for '${newRelation.relationName}'`);
|
|
1495
|
+
if (newRelation.cardinality === "one" && newRelation.direction === "inverse" && !newRelation.foreignKeyOnTarget && !newRelation.joinPath) throw new Error(`Configuration Error in relation from '${sourceCollection.name}': An 'inverse' one-to-one relation requires a 'foreignKeyOnTarget'. Check the relation config for '${newRelation.relationName}'`);
|
|
1496
|
+
if (newRelation.cardinality === "many" && newRelation.direction === "inverse" && !newRelation.foreignKeyOnTarget && !newRelation.joinPath && !newRelation.inverseRelationName) throw new Error(`Configuration Error in relation from '${sourceCollection.name}': An 'inverse' one-to-many relation requires a 'foreignKeyOnTarget'. Check the relation config for '${newRelation.relationName}'`);
|
|
1497
|
+
return newRelation;
|
|
1498
|
+
}
|
|
1499
|
+
/** WeakMap cache — same collection instance always yields the same relation map. */
|
|
1500
|
+
var _resolvedRelationsCache = /* @__PURE__ */ new WeakMap();
|
|
1501
|
+
function resolveCollectionRelations(collection) {
|
|
1502
|
+
const cached = _resolvedRelationsCache.get(collection);
|
|
1503
|
+
if (cached) return cached;
|
|
1504
|
+
if (!getDataSourceCapabilities(collection.engine).supportsRelations) return {};
|
|
1505
|
+
const relations = {};
|
|
1506
|
+
const registeredRelationNames = /* @__PURE__ */ new Set();
|
|
1507
|
+
if (collection.relations) collection.relations.forEach((relation) => {
|
|
1508
|
+
try {
|
|
1509
|
+
const normalizedRelation = sanitizeRelation(relation, collection);
|
|
1510
|
+
const relationKey = normalizedRelation.relationName;
|
|
1511
|
+
if (relationKey) {
|
|
1512
|
+
relations[relationKey] = normalizedRelation;
|
|
1513
|
+
registeredRelationNames.add(relationKey);
|
|
1514
|
+
}
|
|
1515
|
+
} catch (e) {}
|
|
1516
|
+
});
|
|
1517
|
+
if (collection.properties) Object.entries(collection.properties).forEach(([propKey, prop]) => {
|
|
1518
|
+
const relation = resolvePropertyRelation({
|
|
1519
|
+
propertyKey: propKey,
|
|
1520
|
+
property: prop,
|
|
1521
|
+
sourceCollection: collection
|
|
1522
|
+
});
|
|
1523
|
+
if (relation) {
|
|
1524
|
+
if (relations[propKey]) return;
|
|
1525
|
+
if (!relation.relationName) relation.relationName = propKey;
|
|
1526
|
+
const normalizedRelation = sanitizeRelation(relation, collection);
|
|
1527
|
+
relations[propKey] = normalizedRelation;
|
|
1528
|
+
registeredRelationNames.add(normalizedRelation.relationName ?? propKey);
|
|
1529
|
+
}
|
|
1530
|
+
});
|
|
1531
|
+
_resolvedRelationsCache.set(collection, relations);
|
|
1532
|
+
return relations;
|
|
1533
|
+
}
|
|
1534
|
+
function resolvePropertyRelation({ propertyKey, property, sourceCollection }) {
|
|
1535
|
+
if (property.type !== "relation") return void 0;
|
|
1536
|
+
const relProp = property;
|
|
1537
|
+
if (relProp.target) return {
|
|
1538
|
+
relationName: relProp.relationName || propertyKey,
|
|
1539
|
+
target: relProp.target,
|
|
1540
|
+
cardinality: relProp.cardinality || "one",
|
|
1541
|
+
direction: relProp.direction || "owning",
|
|
1542
|
+
inverseRelationName: relProp.inverseRelationName,
|
|
1543
|
+
localKey: relProp.localKey,
|
|
1544
|
+
foreignKeyOnTarget: relProp.foreignKeyOnTarget,
|
|
1545
|
+
through: relProp.through,
|
|
1546
|
+
joinPath: relProp.joinPath,
|
|
1547
|
+
onUpdate: relProp.onUpdate,
|
|
1548
|
+
onDelete: relProp.onDelete,
|
|
1549
|
+
overrides: relProp.overrides
|
|
1550
|
+
};
|
|
1551
|
+
console.warn(`Unrecognized or missing relation target for property '${propertyKey}' in collection '${sourceCollection.slug}'`);
|
|
1552
|
+
}
|
|
1553
|
+
function getTableName(collection) {
|
|
1554
|
+
if (getDataSourceCapabilities(collection.engine).supportsRelations) return collection.table ?? toSnakeCase(collection.slug) ?? toSnakeCase(collection.name);
|
|
1555
|
+
return toSnakeCase(collection.slug) ?? toSnakeCase(collection.name);
|
|
1556
|
+
}
|
|
1557
|
+
function getTableVarName(tableName) {
|
|
1558
|
+
return tableName.replace(/_([a-z])/g, (_, char) => char.toUpperCase());
|
|
1559
|
+
}
|
|
1560
|
+
function getEnumVarName(tableName, propName) {
|
|
1561
|
+
return `${getTableVarName(tableName)}${propName.charAt(0).toUpperCase() + propName.slice(1)}`;
|
|
1562
|
+
}
|
|
1563
|
+
function getColumnName(fullColumn) {
|
|
1564
|
+
return fullColumn.includes(".") ? fullColumn.split(".").pop() : fullColumn;
|
|
1565
|
+
}
|
|
1566
|
+
/**
|
|
1567
|
+
* Look up a relation by key with forgiving normalization.
|
|
1568
|
+
*
|
|
1569
|
+
* `resolveCollectionRelations` stores each relation under a single canonical
|
|
1570
|
+
* key (no aliases). This helper tries the given key as-is, then falls back to
|
|
1571
|
+
* slug form (underscores → hyphens) and snake_case form (hyphens → underscores)
|
|
1572
|
+
* so that callers that receive a key from external input (URL path segments,
|
|
1573
|
+
* user-provided config, etc.) can still find the right entry.
|
|
1574
|
+
*/
|
|
1575
|
+
function findRelation(resolvedRelations, key) {
|
|
1576
|
+
if (resolvedRelations[key]) return resolvedRelations[key];
|
|
1577
|
+
const slugKey = key.replace(/_/g, "-");
|
|
1578
|
+
if (slugKey !== key && resolvedRelations[slugKey]) return resolvedRelations[slugKey];
|
|
1579
|
+
const snakeKey = key.replace(/-/g, "_");
|
|
1580
|
+
if (snakeKey !== key && resolvedRelations[snakeKey]) return resolvedRelations[snakeKey];
|
|
1581
|
+
}
|
|
1582
|
+
//#endregion
|
|
1583
|
+
//#region ../common/src/util/resolutions.ts
|
|
1584
|
+
function getSubcollections(collection) {
|
|
1585
|
+
if (collection.childCollections) return collection.childCollections() ?? [];
|
|
1586
|
+
const declaredSubcollections = getDeclaredSubcollections(collection);
|
|
1587
|
+
if (getDataSourceCapabilities(collection.engine).supportsSubcollections && declaredSubcollections) return declaredSubcollections() ?? [];
|
|
1588
|
+
if (getDataSourceCapabilities(collection.engine).supportsRelations) {
|
|
1589
|
+
const resolvedRelations = resolveCollectionRelations(collection);
|
|
1590
|
+
return Object.values(resolvedRelations).filter((r) => r.cardinality === "many").map((r) => {
|
|
1591
|
+
const target = r.target();
|
|
1592
|
+
if (!target) return void 0;
|
|
1593
|
+
const relationKey = r.relationName || target.slug;
|
|
1594
|
+
let customName;
|
|
1595
|
+
if (collection.properties) {
|
|
1596
|
+
const prop = Object.entries(collection.properties).find(([_, p]) => p.type === "relation" && p.relationName === relationKey);
|
|
1597
|
+
if (prop && prop[1].name) customName = prop[1].name;
|
|
1598
|
+
}
|
|
1599
|
+
const baseOverrides = { slug: relationKey };
|
|
1600
|
+
if (customName) {
|
|
1601
|
+
baseOverrides.name = customName;
|
|
1602
|
+
baseOverrides.singularName = customName;
|
|
1603
|
+
}
|
|
1604
|
+
const targetWithOverrides = {
|
|
1605
|
+
...target,
|
|
1606
|
+
...baseOverrides
|
|
1607
|
+
};
|
|
1608
|
+
return r.overrides ? mergeDeep(targetWithOverrides, r.overrides) : targetWithOverrides;
|
|
1609
|
+
}).filter((c) => Boolean(c));
|
|
1610
|
+
}
|
|
1611
|
+
return [];
|
|
1612
|
+
}
|
|
1613
|
+
//#endregion
|
|
1614
|
+
//#region ../common/src/util/policy/sqlToPolicy.ts
|
|
1615
|
+
/**
|
|
1616
|
+
* A tiny, regex-based SQL "parser" for security rules.
|
|
1617
|
+
*
|
|
1618
|
+
* This is NOT a full SQL parser. It is designed to handle the subset of SQL
|
|
1619
|
+
* commonly used in `USING` and `WITH CHECK` clauses, enough to drive the
|
|
1620
|
+
* optimistic client-side UI decision.
|
|
1621
|
+
*
|
|
1622
|
+
* It handles:
|
|
1623
|
+
* - `field = 'literal'`
|
|
1624
|
+
* - `field != 'literal'`
|
|
1625
|
+
* - `field = current_setting('app.uid')` (or the legacy `app.user_id`)
|
|
1626
|
+
* - `A AND B`, `A OR B` — only where the keyword is at the top level
|
|
1627
|
+
* - `true`
|
|
1628
|
+
* - `IN (...)` (as optimistic true)
|
|
1629
|
+
*
|
|
1630
|
+
* For anything it doesn't understand, it returns a `raw` expression, which
|
|
1631
|
+
* the evaluator treats as "unknown" (and usually optimistic true).
|
|
1632
|
+
*
|
|
1633
|
+
* **This output also round-trips back into DDL** via `policyToPostgres` (the
|
|
1634
|
+
* schema/policy generators), so decomposing a clause the parser only partly
|
|
1635
|
+
* understands is not a cosmetic mistake — it emits invalid SQL. When in doubt,
|
|
1636
|
+
* prefer `raw`: it is reproduced verbatim.
|
|
1637
|
+
*/
|
|
1638
|
+
/** True when `keyword` starts at `i` as a standalone word. */
|
|
1639
|
+
function isKeywordAt(upper, i, keyword) {
|
|
1640
|
+
if (!upper.startsWith(keyword, i)) return false;
|
|
1641
|
+
const before = i === 0 ? " " : upper[i - 1];
|
|
1642
|
+
const after = upper[i + keyword.length] ?? " ";
|
|
1643
|
+
return /[\s()]/.test(before) && /[\s()]/.test(after);
|
|
1644
|
+
}
|
|
1645
|
+
/**
|
|
1646
|
+
* Split `sql` on a boolean keyword, but only where it sits at paren depth 0 and
|
|
1647
|
+
* outside a string literal. Returns null when it never does, so the caller
|
|
1648
|
+
* leaves the clause alone.
|
|
1649
|
+
*
|
|
1650
|
+
* This used to be `sql.split(/ AND /i)`, which tore subqueries in half: the
|
|
1651
|
+
* `AND` inside
|
|
1652
|
+
* `EXISTS (SELECT 1 FROM organization_members m WHERE m.org = t.org AND m.user_id = auth.uid())`
|
|
1653
|
+
* split the expression, and re-emitting the halves produced
|
|
1654
|
+
* `(EXISTS (...) AND m.user_id = auth.uid())`
|
|
1655
|
+
* where `m` is no longer in scope — SQL that Postgres rejects outright with
|
|
1656
|
+
* "missing FROM-clause entry for table". Returning null instead keeps such a
|
|
1657
|
+
* clause as a `raw` expression, which round-trips verbatim.
|
|
1658
|
+
*/
|
|
1659
|
+
function splitTopLevel(sql, keyword) {
|
|
1660
|
+
const upper = sql.toUpperCase();
|
|
1661
|
+
const parts = [];
|
|
1662
|
+
let depth = 0;
|
|
1663
|
+
let inString = false;
|
|
1664
|
+
let start = 0;
|
|
1665
|
+
for (let i = 0; i < sql.length; i++) {
|
|
1666
|
+
const ch = sql[i];
|
|
1667
|
+
if (inString) {
|
|
1668
|
+
if (ch === "'") if (sql[i + 1] === "'") i++;
|
|
1669
|
+
else inString = false;
|
|
1670
|
+
continue;
|
|
1671
|
+
}
|
|
1672
|
+
if (ch === "'") {
|
|
1673
|
+
inString = true;
|
|
1674
|
+
continue;
|
|
1675
|
+
}
|
|
1676
|
+
if (ch === "(") {
|
|
1677
|
+
depth++;
|
|
1678
|
+
continue;
|
|
1679
|
+
}
|
|
1680
|
+
if (ch === ")") {
|
|
1681
|
+
depth--;
|
|
1682
|
+
continue;
|
|
1683
|
+
}
|
|
1684
|
+
if (depth === 0 && isKeywordAt(upper, i, keyword)) {
|
|
1685
|
+
parts.push(sql.slice(start, i));
|
|
1686
|
+
i += keyword.length - 1;
|
|
1687
|
+
start = i + 1;
|
|
1688
|
+
}
|
|
1689
|
+
}
|
|
1690
|
+
if (parts.length === 0) return null;
|
|
1691
|
+
parts.push(sql.slice(start));
|
|
1692
|
+
const trimmedParts = parts.map((p) => p.trim()).filter((p) => p.length > 0);
|
|
1693
|
+
return trimmedParts.length > 1 ? trimmedParts : null;
|
|
1694
|
+
}
|
|
1695
|
+
/** Drop redundant wrapping parens (`(a AND b)` → `a AND b`), never `(a) AND (b)`. */
|
|
1696
|
+
function stripOuterParens(sql) {
|
|
1697
|
+
let s = sql.trim();
|
|
1698
|
+
for (;;) {
|
|
1699
|
+
if (!s.startsWith("(") || !s.endsWith(")")) return s;
|
|
1700
|
+
let depth = 0;
|
|
1701
|
+
let inString = false;
|
|
1702
|
+
let wraps = true;
|
|
1703
|
+
for (let i = 0; i < s.length; i++) {
|
|
1704
|
+
const ch = s[i];
|
|
1705
|
+
if (inString) {
|
|
1706
|
+
if (ch === "'") if (s[i + 1] === "'") i++;
|
|
1707
|
+
else inString = false;
|
|
1708
|
+
continue;
|
|
1709
|
+
}
|
|
1710
|
+
if (ch === "'") {
|
|
1711
|
+
inString = true;
|
|
1712
|
+
continue;
|
|
1713
|
+
}
|
|
1714
|
+
if (ch === "(") depth++;
|
|
1715
|
+
else if (ch === ")") {
|
|
1716
|
+
depth--;
|
|
1717
|
+
if (depth === 0 && i < s.length - 1) {
|
|
1718
|
+
wraps = false;
|
|
1719
|
+
break;
|
|
1720
|
+
}
|
|
1721
|
+
}
|
|
1722
|
+
}
|
|
1723
|
+
if (!wraps) return s;
|
|
1724
|
+
s = s.slice(1, -1).trim();
|
|
1725
|
+
}
|
|
1726
|
+
}
|
|
1727
|
+
function sqlToPolicy(sql) {
|
|
1728
|
+
const trimmed = stripOuterParens(sql.trim());
|
|
1729
|
+
if (trimmed.toLowerCase() === "true") return policy.true();
|
|
1730
|
+
if (trimmed.toLowerCase() === "false") return policy.false();
|
|
1731
|
+
const overlapMatch = trimmed.match(/^string_to_array\s*\(\s*auth\.roles\(\)\s*,\s*','\s*\)\s*&&\s*ARRAY\s*\[(.+)\]$/i);
|
|
1732
|
+
if (overlapMatch) {
|
|
1733
|
+
const roles = overlapMatch[1].split(",").map((s) => s.trim().replace(/^'|'$/g, ""));
|
|
1734
|
+
return policy.rolesOverlap(roles);
|
|
1735
|
+
}
|
|
1736
|
+
const containMatch = trimmed.match(/^string_to_array\s*\(\s*auth\.roles\(\)\s*,\s*','\s*\)\s*@>\s*ARRAY\s*\[(.+)\]$/i);
|
|
1737
|
+
if (containMatch) {
|
|
1738
|
+
const roles = containMatch[1].split(",").map((s) => s.trim().replace(/^'|'$/g, ""));
|
|
1739
|
+
return policy.rolesContain(roles);
|
|
1740
|
+
}
|
|
1741
|
+
const orParts = splitTopLevel(trimmed, "OR");
|
|
1742
|
+
if (orParts) return policy.or(...orParts.map(sqlToPolicy));
|
|
1743
|
+
const andParts = splitTopLevel(trimmed, "AND");
|
|
1744
|
+
if (andParts) return policy.and(...andParts.map(sqlToPolicy));
|
|
1745
|
+
const match = trimmed.match(/^(.+?)\s*(!?=)\s*(.+)$/);
|
|
1746
|
+
if (match) {
|
|
1747
|
+
const [, leftStr, op, rightStr] = match;
|
|
1748
|
+
const left = parseOperand(leftStr.trim());
|
|
1749
|
+
const right = parseOperand(rightStr.trim());
|
|
1750
|
+
if (left && right) return policy.compare(left, op === "=" ? "eq" : "neq", right);
|
|
1751
|
+
}
|
|
1752
|
+
return policy.raw(sql);
|
|
1753
|
+
}
|
|
1754
|
+
/**
|
|
1755
|
+
* Literals from other BaaS platforms that people compare `auth.uid()` against
|
|
1756
|
+
* out of habit. Mirrors the driver's `FOREIGN_CONVENTION_ROLES` guard on
|
|
1757
|
+
* `pgRoles`, one surface over: the same muscle memory inside a `using:` string
|
|
1758
|
+
* is the more dangerous spelling, because it inverts a rule instead of
|
|
1759
|
+
* emptying a table.
|
|
1760
|
+
*/
|
|
1761
|
+
var FOREIGN_CONVENTION_UIDS = {
|
|
1762
|
+
anon: "Supabase",
|
|
1763
|
+
authenticated: "Supabase",
|
|
1764
|
+
service_role: "Supabase"
|
|
1765
|
+
};
|
|
1766
|
+
/** `auth.uid() IS NOT NULL` in raw SQL, the clause that is always true. */
|
|
1767
|
+
var UID_NOT_NULL = /auth\.uid\(\)\s+IS\s+NOT\s+NULL/i;
|
|
1768
|
+
/**
|
|
1769
|
+
* Find clauses that read as "signed-in users only" but admit anonymous callers.
|
|
1770
|
+
*
|
|
1771
|
+
* Both spellings come from the same place — Supabase, where `auth.uid()` really
|
|
1772
|
+
* is NULL for an anonymous request. Rebase substitutes
|
|
1773
|
+
* {@link ANONYMOUS_USER_ID} instead (a blank id would read back as NULL, which
|
|
1774
|
+
* is how the trusted *server* context is recognised), so:
|
|
1775
|
+
*
|
|
1776
|
+
* - `auth.uid() IS NOT NULL` is a tautology on the user path, and
|
|
1777
|
+
* - `auth.uid() != 'anon'` compares against a string no caller ever has.
|
|
1778
|
+
*
|
|
1779
|
+
* Either one turns a lockdown into a full grant, and neither looks wrong. No
|
|
1780
|
+
* real user id is ever one of these literals, and a user-context request is
|
|
1781
|
+
* never NULL, so a match is always a mistake rather than a deliberate check.
|
|
1782
|
+
*
|
|
1783
|
+
* Structured expressions are checked too, not just parsed SQL: `policy.compare`
|
|
1784
|
+
* can spell the same mistake.
|
|
1785
|
+
*/
|
|
1786
|
+
function findAnonymousGrants(expr) {
|
|
1787
|
+
const found = [];
|
|
1788
|
+
const visit = (e) => {
|
|
1789
|
+
switch (e.kind) {
|
|
1790
|
+
case "and":
|
|
1791
|
+
case "or":
|
|
1792
|
+
e.operands.forEach(visit);
|
|
1793
|
+
return;
|
|
1794
|
+
case "not":
|
|
1795
|
+
visit(e.operand);
|
|
1796
|
+
return;
|
|
1797
|
+
case "existsIn":
|
|
1798
|
+
visit(e.where);
|
|
1799
|
+
return;
|
|
1800
|
+
case "raw":
|
|
1801
|
+
if (UID_NOT_NULL.test(e.sql)) found.push({
|
|
1802
|
+
pattern: "uid-not-null",
|
|
1803
|
+
detail: e.sql,
|
|
1804
|
+
explanation: `\`auth.uid() IS NOT NULL\` is true for every request that came from a client, including anonymous ones — they carry '${ANONYMOUS_USER_ID}', not NULL. Use \`condition: policy.authenticated()\` to mean "signed in".`
|
|
1805
|
+
});
|
|
1806
|
+
return;
|
|
1807
|
+
case "compare": {
|
|
1808
|
+
const literal = [e.left, e.right].find((o) => o.kind === "literal");
|
|
1809
|
+
if (!(e.left.kind === "authUid" || e.right.kind === "authUid") || typeof literal?.value !== "string") return;
|
|
1810
|
+
const platform = FOREIGN_CONVENTION_UIDS[literal.value];
|
|
1811
|
+
if (!platform) return;
|
|
1812
|
+
found.push({
|
|
1813
|
+
pattern: "foreign-uid-literal",
|
|
1814
|
+
detail: literal.value,
|
|
1815
|
+
explanation: `'${literal.value}' is a ${platform} convention. Rebase reports an anonymous request as '${ANONYMOUS_USER_ID}', so comparing against '${literal.value}' passes for every caller. Use \`condition: policy.authenticated()\` to mean "signed in".`
|
|
1816
|
+
});
|
|
1817
|
+
return;
|
|
1818
|
+
}
|
|
1819
|
+
default: return;
|
|
1820
|
+
}
|
|
1821
|
+
};
|
|
1822
|
+
visit(expr);
|
|
1823
|
+
return found;
|
|
1824
|
+
}
|
|
1825
|
+
function parseOperand(str) {
|
|
1826
|
+
if (/current_setting\s*\(\s*'app\.(uid|user_id)'\s*\)/i.test(str) || /auth\.uid\(\)/i.test(str)) return policy.authUid();
|
|
1827
|
+
const stringMatch = str.match(/^'(.+)'$/);
|
|
1828
|
+
if (stringMatch) return policy.literal(stringMatch[1]);
|
|
1829
|
+
if (/^\w+$/.test(str)) return policy.field(str);
|
|
1830
|
+
return null;
|
|
1831
|
+
}
|
|
1832
|
+
//#endregion
|
|
1833
|
+
//#region ../common/src/util/policy/securityRuleToConditions.ts
|
|
1834
|
+
/**
|
|
1835
|
+
* Desugars a {@link SecurityRule} — its `access`/`ownerField`/`roles` shortcuts,
|
|
1836
|
+
* structured `condition`/`check`, and raw `using`/`withCheck` — into a single
|
|
1837
|
+
* normalized {@link PolicyExpression} pair.
|
|
1838
|
+
*
|
|
1839
|
+
* **This is the linchpin against drift:** both the Postgres DDL generators and
|
|
1840
|
+
* the client-side evaluator consume this one function, so there is exactly one
|
|
1841
|
+
* definition of what a rule means. In particular, application `roles` are folded
|
|
1842
|
+
* into the expression here (AND'd with the base condition, matching how Postgres
|
|
1843
|
+
* generates the clause) rather than being handled separately by each consumer.
|
|
1844
|
+
*/
|
|
1845
|
+
function securityRuleToConditions(rule) {
|
|
1846
|
+
return {
|
|
1847
|
+
usingExpr: withRoles(baseUsing(rule), rule),
|
|
1848
|
+
withCheckExpr: withRoles(baseWithCheck(rule), rule)
|
|
1849
|
+
};
|
|
1850
|
+
}
|
|
1851
|
+
function baseUsing(rule) {
|
|
1852
|
+
if (rule.condition) return rule.condition;
|
|
1853
|
+
if (rule.using != null) return sqlToPolicy(rule.using);
|
|
1854
|
+
if (rule.access === "public") return policy.true();
|
|
1855
|
+
if (rule.ownerField) return policy.compare(policy.field(rule.ownerField), "eq", policy.authUid());
|
|
1856
|
+
return null;
|
|
1857
|
+
}
|
|
1858
|
+
function baseWithCheck(rule) {
|
|
1859
|
+
if (rule.check) return rule.check;
|
|
1860
|
+
if (rule.withCheck != null) return sqlToPolicy(rule.withCheck);
|
|
1861
|
+
return baseUsing(rule);
|
|
1862
|
+
}
|
|
1863
|
+
/**
|
|
1864
|
+
* AND the base condition with an application-role check, or produce a roles-only
|
|
1865
|
+
* condition when there is no base. Mirrors the Postgres generator so that a
|
|
1866
|
+
* role-scoped restrictive rule denies exactly the same set of users on both
|
|
1867
|
+
* sides.
|
|
1868
|
+
*/
|
|
1869
|
+
function withRoles(base, rule) {
|
|
1870
|
+
if (!rule.roles || rule.roles.length === 0) return base;
|
|
1871
|
+
const rolesExpr = policy.rolesOverlap(rule.roles);
|
|
1872
|
+
if (rule.mode === "restrictive") return base ? policy.or(policy.not(rolesExpr), base) : policy.not(rolesExpr);
|
|
1873
|
+
return base ? policy.and(base, rolesExpr) : rolesExpr;
|
|
1874
|
+
}
|
|
1875
|
+
//#endregion
|
|
1876
|
+
//#region ../common/src/util/policy/policyToPostgres.ts
|
|
1877
|
+
/**
|
|
1878
|
+
* Compiles a {@link PolicyExpression} to a PostgreSQL boolean SQL string,
|
|
1879
|
+
* suitable for a `USING (...)` / `WITH CHECK (...)` clause.
|
|
1880
|
+
*
|
|
1881
|
+
* This is one of the two consumers of the shared policy model (the other being
|
|
1882
|
+
* {@link evaluatePolicy}); the Postgres schema generators call it so that DDL
|
|
1883
|
+
* and the admin UI derive from the exact same expression.
|
|
1884
|
+
*/
|
|
1885
|
+
function policyToPostgres(expr, collection, options) {
|
|
1886
|
+
return compile(expr, {
|
|
1887
|
+
fieldCollection: collection,
|
|
1888
|
+
fieldPrefix: "",
|
|
1889
|
+
outerCollection: collection,
|
|
1890
|
+
outerPrefix: "",
|
|
1891
|
+
resolveCollection: options?.resolveCollection,
|
|
1892
|
+
alias: { n: 0 }
|
|
1893
|
+
});
|
|
1894
|
+
}
|
|
1895
|
+
function compile(expr, scope) {
|
|
1896
|
+
switch (expr.kind) {
|
|
1897
|
+
case "true": return "true";
|
|
1898
|
+
case "false": return "false";
|
|
1899
|
+
case "and": return expr.operands.length === 0 ? "true" : expr.operands.map((o) => `(${compile(o, scope)})`).join(" AND ");
|
|
1900
|
+
case "or": return expr.operands.length === 0 ? "false" : expr.operands.map((o) => `(${compile(o, scope)})`).join(" OR ");
|
|
1901
|
+
case "not": return `NOT (${compile(expr.operand, scope)})`;
|
|
1902
|
+
case "compare": {
|
|
1903
|
+
const castForAuthUid = (operand, sqlText, other) => other.kind === "authUid" && (operand.kind === "field" || operand.kind === "outerField") ? `(${sqlText})::text` : sqlText;
|
|
1904
|
+
const leftSql = castForAuthUid(expr.left, operandToSql(expr.left, scope), expr.right);
|
|
1905
|
+
const rightSql = castForAuthUid(expr.right, operandToSql(expr.right, scope), expr.left);
|
|
1906
|
+
return `${leftSql} ${COMPARE_SQL[expr.op]} ${rightSql}`;
|
|
1907
|
+
}
|
|
1908
|
+
case "rolesOverlap": return `string_to_array(auth.roles(), ',') && ${rolesArraySql(expr.roles)}`;
|
|
1909
|
+
case "rolesContain": return `string_to_array(auth.roles(), ',') @> ${rolesArraySql(expr.roles)}`;
|
|
1910
|
+
case "authenticated": return `auth.uid() IS NOT NULL AND auth.uid() <> ${quoteLiteral(ANONYMOUS_USER_ID)}`;
|
|
1911
|
+
case "serverContext": return "auth.uid() IS NULL";
|
|
1912
|
+
case "existsIn": return compileExistsIn(expr, scope);
|
|
1913
|
+
case "raw": return expr.sql.replace(/\{(\w+)\}/g, (_, col) => `${outerQualifier(scope)}${resolveColumnName(col, scope.outerCollection)}`);
|
|
1914
|
+
}
|
|
1915
|
+
}
|
|
1916
|
+
/**
|
|
1917
|
+
* Compiles `existsIn` to a correlated `EXISTS (SELECT 1 FROM <join> WHERE ...)`.
|
|
1918
|
+
* Inside the subquery, `field` operands bind to the aliased join table and
|
|
1919
|
+
* `outerField` operands bind to the (table-qualified) outer RLS row.
|
|
1920
|
+
*/
|
|
1921
|
+
function compileExistsIn(expr, scope) {
|
|
1922
|
+
const join = scope.resolveCollection?.(expr.collection);
|
|
1923
|
+
const joinTable = join ? getTableName(join) : toSnakeCase(expr.collection);
|
|
1924
|
+
const joinSchema = schemaOf(join) ?? schemaOf(scope.outerCollection) ?? "public";
|
|
1925
|
+
const alias = `_ex${scope.alias.n++}`;
|
|
1926
|
+
const outerPrefix = outerQualifier(scope);
|
|
1927
|
+
const innerScope = {
|
|
1928
|
+
fieldCollection: join,
|
|
1929
|
+
fieldPrefix: `"${alias}".`,
|
|
1930
|
+
outerCollection: scope.outerCollection,
|
|
1931
|
+
outerPrefix,
|
|
1932
|
+
resolveCollection: scope.resolveCollection,
|
|
1933
|
+
alias: scope.alias
|
|
1934
|
+
};
|
|
1935
|
+
return `EXISTS (SELECT 1 FROM "${joinSchema}"."${joinTable}" "${alias}" WHERE ${compile(expr.where, innerScope)})`;
|
|
1936
|
+
}
|
|
1937
|
+
var COMPARE_SQL = {
|
|
1938
|
+
eq: "=",
|
|
1939
|
+
neq: "!=",
|
|
1940
|
+
lt: "<",
|
|
1941
|
+
lte: "<=",
|
|
1942
|
+
gt: ">",
|
|
1943
|
+
gte: ">="
|
|
1944
|
+
};
|
|
1945
|
+
function operandToSql(operand, scope) {
|
|
1946
|
+
switch (operand.kind) {
|
|
1947
|
+
case "field": return `${scope.fieldPrefix}${resolveColumnName(operand.name, scope.fieldCollection)}`;
|
|
1948
|
+
case "outerField": return `${scope.outerPrefix}${resolveColumnName(operand.name, scope.outerCollection)}`;
|
|
1949
|
+
case "literal": return quoteLiteral(operand.value);
|
|
1950
|
+
case "authUid": return "auth.uid()";
|
|
1951
|
+
case "authRoles": return "string_to_array(auth.roles(), ',')";
|
|
1952
|
+
}
|
|
1953
|
+
}
|
|
1954
|
+
/**
|
|
1955
|
+
* SQL prefix that qualifies a column of the outer RLS row (`"schema"."table".`),
|
|
1956
|
+
* or `""` when the collection is unknown.
|
|
1957
|
+
*/
|
|
1958
|
+
function outerQualifier(scope) {
|
|
1959
|
+
const table = scope.outerCollection ? getTableName(scope.outerCollection) : void 0;
|
|
1960
|
+
if (!table) return "";
|
|
1961
|
+
return `"${schemaOf(scope.outerCollection) ?? "public"}"."${table}".`;
|
|
1962
|
+
}
|
|
1963
|
+
function schemaOf(collection) {
|
|
1964
|
+
return collection?.schema || void 0;
|
|
1965
|
+
}
|
|
1966
|
+
function resolveColumnName(propName, collection) {
|
|
1967
|
+
const prop = collection?.properties?.[propName];
|
|
1968
|
+
if (prop && "columnName" in prop && typeof prop.columnName === "string") return prop.columnName;
|
|
1969
|
+
return toSnakeCase(propName);
|
|
1970
|
+
}
|
|
1971
|
+
function quoteLiteral(value) {
|
|
1972
|
+
if (value === null) return "NULL";
|
|
1973
|
+
if (typeof value === "boolean") return value ? "true" : "false";
|
|
1974
|
+
if (typeof value === "number") return String(value);
|
|
1975
|
+
return `'${value.replace(/'/g, "''")}'`;
|
|
1976
|
+
}
|
|
1977
|
+
/** Sorted, single-quoted `ARRAY['a','b']` — matches the generators' output. */
|
|
1978
|
+
function rolesArraySql(roles) {
|
|
1979
|
+
return `ARRAY[${[...roles].sort().map((r) => `'${r}'`).join(",")}]`;
|
|
1980
|
+
}
|
|
1981
|
+
//#endregion
|
|
1982
|
+
//#region ../common/src/util/callbacks.ts
|
|
1983
|
+
/**
|
|
1984
|
+
* Helper function to recursively check if there are any callbacks in the properties.
|
|
1985
|
+
*/
|
|
1986
|
+
function hasPropertyCallbacks(properties, callbackName) {
|
|
1987
|
+
if (!properties) return false;
|
|
1988
|
+
for (const property of Object.values(properties)) {
|
|
1989
|
+
if (property.callbacks?.[callbackName]) return true;
|
|
1990
|
+
if (property.type === "map" && property.properties) {
|
|
1991
|
+
if (hasPropertyCallbacks(property.properties, callbackName)) return true;
|
|
1992
|
+
} else if (property.type === "array" && property.of) {
|
|
1993
|
+
const ofs = Array.isArray(property.of) ? property.of : [property.of];
|
|
1994
|
+
for (const of of ofs) {
|
|
1995
|
+
if (of.callbacks?.[callbackName]) return true;
|
|
1996
|
+
if (of.type === "map" && of.properties && hasPropertyCallbacks(of.properties, callbackName)) return true;
|
|
1997
|
+
}
|
|
1998
|
+
}
|
|
1999
|
+
}
|
|
2000
|
+
return false;
|
|
2001
|
+
}
|
|
2002
|
+
/**
|
|
2003
|
+
* Recursively process properties to apply field-level hooks.
|
|
2004
|
+
*/
|
|
2005
|
+
async function processProperties(properties, values, previousValues, propsContext, callbackName) {
|
|
2006
|
+
if (!values || typeof values !== "object") return values;
|
|
2007
|
+
const result = { ...values };
|
|
2008
|
+
for (const [key, property] of Object.entries(properties)) {
|
|
2009
|
+
if (result[key] === void 0) continue;
|
|
2010
|
+
let currentValue = result[key];
|
|
2011
|
+
const previousValue = previousValues?.[key];
|
|
2012
|
+
if (property.type === "array" && Array.isArray(currentValue)) {
|
|
2013
|
+
if (property.of && !Array.isArray(property.of)) currentValue = await Promise.all(currentValue.map(async (item, index) => {
|
|
2014
|
+
const prevItem = Array.isArray(previousValue) ? previousValue[index] : void 0;
|
|
2015
|
+
return (await processProperties({ "_tmp": property.of }, { "_tmp": item }, { "_tmp": prevItem }, propsContext, callbackName))["_tmp"];
|
|
2016
|
+
}));
|
|
2017
|
+
} else if (property.type === "map" && property.properties && typeof currentValue === "object") currentValue = await processProperties(property.properties, currentValue, previousValue ?? {}, propsContext, callbackName);
|
|
2018
|
+
if (property.callbacks?.[callbackName]) {
|
|
2019
|
+
const cbRes = await Promise.resolve(property.callbacks[callbackName]({
|
|
2020
|
+
...propsContext,
|
|
2021
|
+
value: currentValue,
|
|
2022
|
+
previousValue
|
|
2023
|
+
}));
|
|
2024
|
+
if (cbRes !== void 0) currentValue = cbRes;
|
|
2025
|
+
}
|
|
2026
|
+
result[key] = currentValue;
|
|
2027
|
+
}
|
|
2028
|
+
return result;
|
|
2029
|
+
}
|
|
2030
|
+
/**
|
|
2031
|
+
* Helper function to extract field-level PropertyCallbacks from a properties schema
|
|
2032
|
+
* and wrap them into an CollectionCallbacks object recursively.
|
|
2033
|
+
*/
|
|
2034
|
+
var buildPropertyCallbacks = (properties) => {
|
|
2035
|
+
if (!properties) return void 0;
|
|
2036
|
+
const propertyCallbacks = {};
|
|
2037
|
+
if (hasPropertyCallbacks(properties, "afterRead")) propertyCallbacks.afterRead = async (props) => {
|
|
2038
|
+
const row = props.row;
|
|
2039
|
+
const processedValues = await processProperties(properties, row, row, props, "afterRead");
|
|
2040
|
+
return {
|
|
2041
|
+
...props.row,
|
|
2042
|
+
...processedValues
|
|
2043
|
+
};
|
|
2044
|
+
};
|
|
2045
|
+
if (hasPropertyCallbacks(properties, "beforeSave")) propertyCallbacks.beforeSave = async (props) => {
|
|
2046
|
+
return await processProperties(properties, props.values, props.previousValues ?? {}, props, "beforeSave");
|
|
2047
|
+
};
|
|
2048
|
+
return Object.keys(propertyCallbacks).length > 0 ? propertyCallbacks : void 0;
|
|
2049
|
+
};
|
|
2050
|
+
//#endregion
|
|
2051
|
+
//#region ../common/src/util/auth-default-policies.ts
|
|
2052
|
+
/**
|
|
2053
|
+
* Default RLS policies injected by the schema generator.
|
|
2054
|
+
*
|
|
2055
|
+
* Rebase's enforcement model is unified: authenticated (user-context) requests
|
|
2056
|
+
* run under the restricted `rebase_user` role, so Postgres RLS binds *every*
|
|
2057
|
+
* statement — reads and writes. A collection's `securityRules` are the whole
|
|
2058
|
+
* authorization model. The server context (auth flows, migrations,
|
|
2059
|
+
* `dataAsAdmin`) runs as the owner and bypasses RLS.
|
|
2060
|
+
*
|
|
2061
|
+
* Because RLS default-denies, every collection is **locked by default**: with
|
|
2062
|
+
* no rules, only the server context and admins can touch it. The generator
|
|
2063
|
+
* injects that safe baseline:
|
|
2064
|
+
*
|
|
2065
|
+
* **For every collection**
|
|
2066
|
+
* 1. A permissive **server-or-admin SELECT** grant.
|
|
2067
|
+
* 2. A permissive **server-or-admin write** grant (insert/update/delete).
|
|
2068
|
+
*
|
|
2069
|
+
* Author `securityRules` are permissive and OR together, so explicit rules only
|
|
2070
|
+
* *broaden* access from this locked baseline (e.g. "users read/write their own
|
|
2071
|
+
* rows").
|
|
2072
|
+
*
|
|
2073
|
+
* **For auth collections additionally**
|
|
2074
|
+
* 3. A permissive **self SELECT** grant (`id = auth.uid()`), so users can read
|
|
2075
|
+
* their own row (profile, session bootstrap) without every app re-declaring
|
|
2076
|
+
* it.
|
|
2077
|
+
* 4. A **restrictive** admin write gate. Restrictive policies are AND'd with
|
|
2078
|
+
* every other policy, so a write is rejected unless the caller is an admin
|
|
2079
|
+
* (or the server context) — even if the author also wrote a permissive rule
|
|
2080
|
+
* such as "a user may edit their own row". Without this, a permissive owner
|
|
2081
|
+
* rule would let a user change their own `roles`.
|
|
2082
|
+
*
|
|
2083
|
+
* The server context is recognised as `auth.uid() IS NULL` (`policy.serverContext()`)
|
|
2084
|
+
* — the built-in flows that run without a user (signup, migrations) set no user
|
|
2085
|
+
* GUC — which also lets the owner connection satisfy these policies even under
|
|
2086
|
+
* FORCE RLS. A *user* request never reaches that state: an anonymous one carries
|
|
2087
|
+
* `ANONYMOUS_USER_ID`, precisely so it cannot pass for the server here.
|
|
2088
|
+
*
|
|
2089
|
+
* Opt out with `disableDefaultPolicies: true` to take full responsibility for
|
|
2090
|
+
* the collection's RLS.
|
|
2091
|
+
*/
|
|
2092
|
+
var SERVER_OR_ADMIN_EXPR$1 = policy.or(policy.serverContext(), policy.rolesOverlap(["admin"]));
|
|
2093
|
+
/** Write operations that must be admin-gated by default on auth collections. */
|
|
2094
|
+
var DEFAULT_GUARDED_OPS = [
|
|
2095
|
+
"insert",
|
|
2096
|
+
"update",
|
|
2097
|
+
"delete"
|
|
2098
|
+
];
|
|
2099
|
+
/** Whether a collection is flagged as an authentication collection. */
|
|
2100
|
+
function isAuthCollection(collection) {
|
|
2101
|
+
const auth = collection.auth;
|
|
2102
|
+
return auth === true || typeof auth === "object" && auth?.enabled === true;
|
|
2103
|
+
}
|
|
2104
|
+
/** The property marked as the row id (falls back to `id`). */
|
|
2105
|
+
function getIdPropertyName$1(collection) {
|
|
2106
|
+
for (const [name, prop] of Object.entries(collection.properties ?? {})) if (prop && typeof prop === "object" && "isId" in prop && prop.isId) return name;
|
|
2107
|
+
return "id";
|
|
2108
|
+
}
|
|
2109
|
+
/**
|
|
2110
|
+
* Returns the security rules that should be applied to a collection: the
|
|
2111
|
+
* author's explicit `securityRules` plus the framework defaults described in
|
|
2112
|
+
* the module doc (baseline server/admin read for all collections; self-read
|
|
2113
|
+
* and the admin write gate for auth collections).
|
|
2114
|
+
*
|
|
2115
|
+
* Collections that opt out via `disableDefaultPolicies` are returned unchanged.
|
|
2116
|
+
*/
|
|
2117
|
+
function getEffectiveSecurityRules(collection) {
|
|
2118
|
+
const explicit = [...(isPostgresCollectionConfig(collection) ? collection.securityRules : void 0) ?? []];
|
|
2119
|
+
if (collection.disableDefaultPolicies) return explicit;
|
|
2120
|
+
const tableName = getTableName(collection);
|
|
2121
|
+
const injected = [];
|
|
2122
|
+
injected.push({
|
|
2123
|
+
name: `${tableName}_default_admin_read`,
|
|
2124
|
+
operations: ["select"],
|
|
2125
|
+
condition: SERVER_OR_ADMIN_EXPR$1
|
|
2126
|
+
});
|
|
2127
|
+
injected.push({
|
|
2128
|
+
name: `${tableName}_default_admin_write`,
|
|
2129
|
+
operations: [...DEFAULT_GUARDED_OPS],
|
|
2130
|
+
condition: SERVER_OR_ADMIN_EXPR$1,
|
|
2131
|
+
check: SERVER_OR_ADMIN_EXPR$1
|
|
2132
|
+
});
|
|
2133
|
+
if (isAuthCollection(collection)) {
|
|
2134
|
+
injected.push({
|
|
2135
|
+
name: `${tableName}_default_self_read`,
|
|
2136
|
+
operations: ["select"],
|
|
2137
|
+
condition: policy.compare(policy.field(getIdPropertyName$1(collection)), "eq", policy.authUid())
|
|
2138
|
+
});
|
|
2139
|
+
injected.push({
|
|
2140
|
+
name: `${tableName}_require_admin_write`,
|
|
2141
|
+
mode: "restrictive",
|
|
2142
|
+
operations: [...DEFAULT_GUARDED_OPS],
|
|
2143
|
+
condition: SERVER_OR_ADMIN_EXPR$1,
|
|
2144
|
+
check: SERVER_OR_ADMIN_EXPR$1
|
|
2145
|
+
});
|
|
2146
|
+
}
|
|
2147
|
+
return [...explicit, ...injected];
|
|
2148
|
+
}
|
|
2149
|
+
//#endregion
|
|
2150
|
+
//#region ../common/src/util/junction-policies.ts
|
|
2151
|
+
var SERVER_OR_ADMIN_EXPR = policy.or(policy.serverContext(), policy.rolesOverlap(["admin"]));
|
|
2152
|
+
/**
|
|
2153
|
+
* Walk every collection's resolved relations and aggregate the junction tables
|
|
2154
|
+
* they declare. Two collections may declare the same junction from opposite
|
|
2155
|
+
* sides (posts→tags and tags→posts through `posts_tags`); both become
|
|
2156
|
+
* `declaringSides` of one spec, so derived write grants consider both.
|
|
2157
|
+
*/
|
|
2158
|
+
function resolveJunctionSpecs(collections) {
|
|
2159
|
+
const specs = /* @__PURE__ */ new Map();
|
|
2160
|
+
for (const collection of collections) {
|
|
2161
|
+
const resolved = resolveCollectionRelations(collection);
|
|
2162
|
+
for (const relation of Object.values(resolved)) {
|
|
2163
|
+
if (!relation.through) continue;
|
|
2164
|
+
const targetCollection = typeof relation.target === "function" ? relation.target() : void 0;
|
|
2165
|
+
if (!targetCollection) continue;
|
|
2166
|
+
const rawName = relation.through.table;
|
|
2167
|
+
const table = rawName.includes(".") ? rawName.split(".").pop() : rawName;
|
|
2168
|
+
const schema = "public";
|
|
2169
|
+
const source = {
|
|
2170
|
+
collection,
|
|
2171
|
+
junctionColumn: relation.through.sourceColumn,
|
|
2172
|
+
relation
|
|
2173
|
+
};
|
|
2174
|
+
const target = {
|
|
2175
|
+
collection: targetCollection,
|
|
2176
|
+
junctionColumn: relation.through.targetColumn
|
|
2177
|
+
};
|
|
2178
|
+
const existing = specs.get(table);
|
|
2179
|
+
if (!existing) specs.set(table, {
|
|
2180
|
+
table,
|
|
2181
|
+
schema,
|
|
2182
|
+
endpoints: [source, target],
|
|
2183
|
+
declaringSides: [source]
|
|
2184
|
+
});
|
|
2185
|
+
else if (!existing.declaringSides.some((s) => s.collection === collection)) existing.declaringSides.push(source);
|
|
2186
|
+
}
|
|
2187
|
+
}
|
|
2188
|
+
return specs;
|
|
2189
|
+
}
|
|
2190
|
+
/**
|
|
2191
|
+
* A synthetic CollectionConfig standing in for the junction during policy
|
|
2192
|
+
* compilation and naming. Its two FK columns carry explicit `columnName`s so
|
|
2193
|
+
* `outerField` operands resolve to the exact columns the CREATE TABLE emitted,
|
|
2194
|
+
* whatever their casing.
|
|
2195
|
+
*/
|
|
2196
|
+
function getJunctionCollectionConfig(spec) {
|
|
2197
|
+
const properties = {};
|
|
2198
|
+
for (const endpoint of spec.endpoints) properties[endpoint.junctionColumn] = {
|
|
2199
|
+
type: "string",
|
|
2200
|
+
columnName: endpoint.junctionColumn
|
|
2201
|
+
};
|
|
2202
|
+
return {
|
|
2203
|
+
slug: spec.table,
|
|
2204
|
+
name: spec.table,
|
|
2205
|
+
table: spec.table,
|
|
2206
|
+
schema: spec.schema,
|
|
2207
|
+
properties
|
|
2208
|
+
};
|
|
2209
|
+
}
|
|
2210
|
+
/** The property marked as the row id (falls back to `id`). */
|
|
2211
|
+
function getIdPropertyName(collection) {
|
|
2212
|
+
for (const [name, prop] of Object.entries(collection.properties ?? {})) if (prop && typeof prop === "object" && "isId" in prop && prop.isId) return name;
|
|
2213
|
+
return "id";
|
|
2214
|
+
}
|
|
2215
|
+
/** `EXISTS (SELECT 1 FROM endpoint WHERE endpoint.pk = junction.fk [AND extra])`. */
|
|
2216
|
+
function existsEndpoint(endpoint, extra) {
|
|
2217
|
+
const correlation = policy.compare(policy.field(getIdPropertyName(endpoint.collection)), "eq", policy.outerField(endpoint.junctionColumn));
|
|
2218
|
+
return policy.existsIn({
|
|
2219
|
+
collection: endpoint.collection.slug,
|
|
2220
|
+
where: extra ? policy.and(correlation, extra) : correlation
|
|
2221
|
+
});
|
|
2222
|
+
}
|
|
2223
|
+
/**
|
|
2224
|
+
* Whether a parent-rule expression keeps its meaning when moved inside the
|
|
2225
|
+
* junction's `EXISTS` subquery — and the re-scoped copy if it does.
|
|
2226
|
+
*
|
|
2227
|
+
* Returns `null` when the rule cannot be embedded faithfully: `raw` SQL
|
|
2228
|
+
* anywhere (its `{column}` placeholders would bind to the junction), or an
|
|
2229
|
+
* `outerField` inside a nested `existsIn` (it would bind to the junction while
|
|
2230
|
+
* the author meant the parent, and no operand can express "the middle scope").
|
|
2231
|
+
* Top-level `outerField`s are rewritten to `field`, which is what they meant.
|
|
2232
|
+
*/
|
|
2233
|
+
function embedParentExpression(expr, depth = 0) {
|
|
2234
|
+
switch (expr.kind) {
|
|
2235
|
+
case "raw": return null;
|
|
2236
|
+
case "and":
|
|
2237
|
+
case "or": {
|
|
2238
|
+
const parts = [];
|
|
2239
|
+
for (const child of expr.operands) {
|
|
2240
|
+
const embedded = embedParentExpression(child, depth);
|
|
2241
|
+
if (!embedded) return null;
|
|
2242
|
+
parts.push(embedded);
|
|
2243
|
+
}
|
|
2244
|
+
return expr.kind === "and" ? policy.and(...parts) : policy.or(...parts);
|
|
2245
|
+
}
|
|
2246
|
+
case "not": {
|
|
2247
|
+
const embedded = embedParentExpression(expr.operand, depth);
|
|
2248
|
+
return embedded ? policy.not(embedded) : null;
|
|
2249
|
+
}
|
|
2250
|
+
case "existsIn": {
|
|
2251
|
+
const where = embedParentExpression(expr.where, depth + 1);
|
|
2252
|
+
return where ? policy.existsIn({
|
|
2253
|
+
collection: expr.collection,
|
|
2254
|
+
where
|
|
2255
|
+
}) : null;
|
|
2256
|
+
}
|
|
2257
|
+
case "compare": {
|
|
2258
|
+
const left = embedOperand(expr.left, depth);
|
|
2259
|
+
const right = embedOperand(expr.right, depth);
|
|
2260
|
+
if (!left || !right) return null;
|
|
2261
|
+
return {
|
|
2262
|
+
...expr,
|
|
2263
|
+
left,
|
|
2264
|
+
right
|
|
2265
|
+
};
|
|
2266
|
+
}
|
|
2267
|
+
default: return expr;
|
|
2268
|
+
}
|
|
2269
|
+
}
|
|
2270
|
+
/** Re-scope an operand, or return `null` if its binding cannot be preserved. */
|
|
2271
|
+
function embedOperand(operand, depth) {
|
|
2272
|
+
if (operand.kind === "outerField") {
|
|
2273
|
+
if (depth === 0) return policy.field(operand.name);
|
|
2274
|
+
return null;
|
|
2275
|
+
}
|
|
2276
|
+
return operand;
|
|
2277
|
+
}
|
|
2278
|
+
/** Does the rule cover the `update` operation? */
|
|
2279
|
+
function coversUpdate(rule) {
|
|
2280
|
+
return getPolicyOperations(rule).some((op) => op === "update" || op === "all");
|
|
2281
|
+
}
|
|
2282
|
+
/**
|
|
2283
|
+
* The full derived policy set for a junction table: the locked server/admin
|
|
2284
|
+
* baseline, the endpoint-visibility read grant, inherited write grants, and
|
|
2285
|
+
* inherited restrictive gates. Returns `[]` when every declaring collection set
|
|
2286
|
+
* `disableDefaultPolicies` — the junction is then the author's to police, and
|
|
2287
|
+
* stays locked (RLS is still enabled) until they write policies for it.
|
|
2288
|
+
*/
|
|
2289
|
+
function getJunctionSecurityRules(spec) {
|
|
2290
|
+
if (spec.declaringSides.every((side) => side.collection.disableDefaultPolicies)) return [];
|
|
2291
|
+
const rules = [];
|
|
2292
|
+
rules.push({
|
|
2293
|
+
name: `${spec.table}_default_admin_read`,
|
|
2294
|
+
operations: ["select"],
|
|
2295
|
+
condition: SERVER_OR_ADMIN_EXPR
|
|
2296
|
+
});
|
|
2297
|
+
rules.push({
|
|
2298
|
+
name: `${spec.table}_default_admin_write`,
|
|
2299
|
+
operations: [
|
|
2300
|
+
"insert",
|
|
2301
|
+
"update",
|
|
2302
|
+
"delete"
|
|
2303
|
+
],
|
|
2304
|
+
condition: SERVER_OR_ADMIN_EXPR,
|
|
2305
|
+
check: SERVER_OR_ADMIN_EXPR
|
|
2306
|
+
});
|
|
2307
|
+
rules.push({
|
|
2308
|
+
name: `${spec.table}_default_edge_read`,
|
|
2309
|
+
operations: ["select"],
|
|
2310
|
+
condition: policy.and(existsEndpoint(spec.endpoints[0]), existsEndpoint(spec.endpoints[1]))
|
|
2311
|
+
});
|
|
2312
|
+
const writeGrants = [];
|
|
2313
|
+
for (const side of spec.declaringSides) {
|
|
2314
|
+
const updateRules = ((isPostgresCollectionConfig(side.collection) ? side.collection.securityRules : void 0) ?? []).filter(coversUpdate);
|
|
2315
|
+
const permissive = updateRules.filter((r) => r.mode !== "restrictive");
|
|
2316
|
+
const restrictive = updateRules.filter((r) => r.mode === "restrictive");
|
|
2317
|
+
const embeddedGates = [];
|
|
2318
|
+
let gatesEmbeddable = true;
|
|
2319
|
+
for (const gate of restrictive) {
|
|
2320
|
+
const using = securityRuleToConditions(gate).usingExpr;
|
|
2321
|
+
const embedded = using ? embedParentExpression(using) : null;
|
|
2322
|
+
if (!embedded) {
|
|
2323
|
+
gatesEmbeddable = false;
|
|
2324
|
+
break;
|
|
2325
|
+
}
|
|
2326
|
+
embeddedGates.push(embedded);
|
|
2327
|
+
}
|
|
2328
|
+
if (!gatesEmbeddable) continue;
|
|
2329
|
+
const grants = [];
|
|
2330
|
+
for (const rule of permissive) {
|
|
2331
|
+
const using = securityRuleToConditions(rule).usingExpr;
|
|
2332
|
+
const embedded = using ? embedParentExpression(using) : null;
|
|
2333
|
+
if (embedded) grants.push(embedded);
|
|
2334
|
+
}
|
|
2335
|
+
if (grants.length === 0) continue;
|
|
2336
|
+
const condition = embeddedGates.length > 0 ? policy.and(policy.or(...grants), ...embeddedGates) : policy.or(...grants);
|
|
2337
|
+
writeGrants.push(existsEndpoint(side, condition));
|
|
2338
|
+
}
|
|
2339
|
+
if (writeGrants.length > 0) rules.push({
|
|
2340
|
+
name: `${spec.table}_default_edge_write`,
|
|
2341
|
+
operations: [
|
|
2342
|
+
"insert",
|
|
2343
|
+
"update",
|
|
2344
|
+
"delete"
|
|
2345
|
+
],
|
|
2346
|
+
condition: writeGrants.length === 1 ? writeGrants[0] : policy.or(...writeGrants),
|
|
2347
|
+
check: writeGrants.length === 1 ? writeGrants[0] : policy.or(...writeGrants)
|
|
2348
|
+
});
|
|
2349
|
+
return rules;
|
|
2350
|
+
}
|
|
2351
|
+
(/* @__PURE__ */ __commonJSMin(((exports, module) => {
|
|
2352
|
+
(function(root, factory) {
|
|
2353
|
+
if (typeof define === "function" && define.amd) define(factory);
|
|
2354
|
+
else if (typeof exports === "object") module.exports = factory();
|
|
2355
|
+
else root.jsonLogic = factory();
|
|
2356
|
+
})(exports, function() {
|
|
2357
|
+
"use strict";
|
|
2358
|
+
if (!Array.isArray) Array.isArray = function(arg) {
|
|
2359
|
+
return Object.prototype.toString.call(arg) === "[object Array]";
|
|
2360
|
+
};
|
|
2361
|
+
/**
|
|
2362
|
+
* Return an array that contains no duplicates (original not modified)
|
|
2363
|
+
* @param {array} array Original reference array
|
|
2364
|
+
* @return {array} New array with no duplicates
|
|
2365
|
+
*/
|
|
2366
|
+
function arrayUnique(array) {
|
|
2367
|
+
var a = [];
|
|
2368
|
+
for (var i = 0, l = array.length; i < l; i++) if (a.indexOf(array[i]) === -1) a.push(array[i]);
|
|
2369
|
+
return a;
|
|
2370
|
+
}
|
|
2371
|
+
var jsonLogic = {};
|
|
2372
|
+
var operations = {
|
|
2373
|
+
"==": function(a, b) {
|
|
2374
|
+
return a == b;
|
|
2375
|
+
},
|
|
2376
|
+
"===": function(a, b) {
|
|
2377
|
+
return a === b;
|
|
2378
|
+
},
|
|
2379
|
+
"!=": function(a, b) {
|
|
2380
|
+
return a != b;
|
|
2381
|
+
},
|
|
2382
|
+
"!==": function(a, b) {
|
|
2383
|
+
return a !== b;
|
|
2384
|
+
},
|
|
2385
|
+
">": function(a, b) {
|
|
2386
|
+
return a > b;
|
|
2387
|
+
},
|
|
2388
|
+
">=": function(a, b) {
|
|
2389
|
+
return a >= b;
|
|
2390
|
+
},
|
|
2391
|
+
"<": function(a, b, c) {
|
|
2392
|
+
return c === void 0 ? a < b : a < b && b < c;
|
|
2393
|
+
},
|
|
2394
|
+
"<=": function(a, b, c) {
|
|
2395
|
+
return c === void 0 ? a <= b : a <= b && b <= c;
|
|
2396
|
+
},
|
|
2397
|
+
"!!": function(a) {
|
|
2398
|
+
return jsonLogic.truthy(a);
|
|
2399
|
+
},
|
|
2400
|
+
"!": function(a) {
|
|
2401
|
+
return !jsonLogic.truthy(a);
|
|
2402
|
+
},
|
|
2403
|
+
"%": function(a, b) {
|
|
2404
|
+
return a % b;
|
|
2405
|
+
},
|
|
2406
|
+
"log": function(a) {
|
|
2407
|
+
console.log(a);
|
|
2408
|
+
return a;
|
|
2409
|
+
},
|
|
2410
|
+
"in": function(a, b) {
|
|
2411
|
+
if (!b || typeof b.indexOf === "undefined") return false;
|
|
2412
|
+
return b.indexOf(a) !== -1;
|
|
2413
|
+
},
|
|
2414
|
+
"cat": function() {
|
|
2415
|
+
return Array.prototype.join.call(arguments, "");
|
|
2416
|
+
},
|
|
2417
|
+
"substr": function(source, start, end) {
|
|
2418
|
+
if (end < 0) {
|
|
2419
|
+
var temp = String(source).substr(start);
|
|
2420
|
+
return temp.substr(0, temp.length + end);
|
|
2421
|
+
}
|
|
2422
|
+
return String(source).substr(start, end);
|
|
2423
|
+
},
|
|
2424
|
+
"+": function() {
|
|
2425
|
+
return Array.prototype.reduce.call(arguments, function(a, b) {
|
|
2426
|
+
return parseFloat(a, 10) + parseFloat(b, 10);
|
|
2427
|
+
}, 0);
|
|
2428
|
+
},
|
|
2429
|
+
"*": function() {
|
|
2430
|
+
return Array.prototype.reduce.call(arguments, function(a, b) {
|
|
2431
|
+
return parseFloat(a, 10) * parseFloat(b, 10);
|
|
2432
|
+
});
|
|
2433
|
+
},
|
|
2434
|
+
"-": function(a, b) {
|
|
2435
|
+
if (b === void 0) return -a;
|
|
2436
|
+
else return a - b;
|
|
2437
|
+
},
|
|
2438
|
+
"/": function(a, b) {
|
|
2439
|
+
return a / b;
|
|
2440
|
+
},
|
|
2441
|
+
"min": function() {
|
|
2442
|
+
return Math.min.apply(this, arguments);
|
|
2443
|
+
},
|
|
2444
|
+
"max": function() {
|
|
2445
|
+
return Math.max.apply(this, arguments);
|
|
2446
|
+
},
|
|
2447
|
+
"merge": function() {
|
|
2448
|
+
return Array.prototype.reduce.call(arguments, function(a, b) {
|
|
2449
|
+
return a.concat(b);
|
|
2450
|
+
}, []);
|
|
2451
|
+
},
|
|
2452
|
+
"var": function(a, b) {
|
|
2453
|
+
var not_found = b === void 0 ? null : b;
|
|
2454
|
+
var data = this;
|
|
2455
|
+
if (typeof a === "undefined" || a === "" || a === null) return data;
|
|
2456
|
+
var sub_props = String(a).split(".");
|
|
2457
|
+
for (var i = 0; i < sub_props.length; i++) {
|
|
2458
|
+
if (data === null || data === void 0) return not_found;
|
|
2459
|
+
data = data[sub_props[i]];
|
|
2460
|
+
if (data === void 0) return not_found;
|
|
2461
|
+
}
|
|
2462
|
+
return data;
|
|
2463
|
+
},
|
|
2464
|
+
"missing": function() {
|
|
2465
|
+
var missing = [];
|
|
2466
|
+
var keys = Array.isArray(arguments[0]) ? arguments[0] : arguments;
|
|
2467
|
+
for (var i = 0; i < keys.length; i++) {
|
|
2468
|
+
var key = keys[i];
|
|
2469
|
+
var value = jsonLogic.apply({ "var": key }, this);
|
|
2470
|
+
if (value === null || value === "") missing.push(key);
|
|
2471
|
+
}
|
|
2472
|
+
return missing;
|
|
2473
|
+
},
|
|
2474
|
+
"missing_some": function(need_count, options) {
|
|
2475
|
+
var are_missing = jsonLogic.apply({ "missing": options }, this);
|
|
2476
|
+
if (options.length - are_missing.length >= need_count) return [];
|
|
2477
|
+
else return are_missing;
|
|
2478
|
+
}
|
|
2479
|
+
};
|
|
2480
|
+
jsonLogic.is_logic = function(logic) {
|
|
2481
|
+
return typeof logic === "object" && logic !== null && !Array.isArray(logic) && Object.keys(logic).length === 1;
|
|
2482
|
+
};
|
|
2483
|
+
jsonLogic.truthy = function(value) {
|
|
2484
|
+
if (Array.isArray(value) && value.length === 0) return false;
|
|
2485
|
+
return !!value;
|
|
2486
|
+
};
|
|
2487
|
+
jsonLogic.get_operator = function(logic) {
|
|
2488
|
+
return Object.keys(logic)[0];
|
|
2489
|
+
};
|
|
2490
|
+
jsonLogic.get_values = function(logic) {
|
|
2491
|
+
return logic[jsonLogic.get_operator(logic)];
|
|
2492
|
+
};
|
|
2493
|
+
jsonLogic.apply = function(logic, data) {
|
|
2494
|
+
if (Array.isArray(logic)) return logic.map(function(l) {
|
|
2495
|
+
return jsonLogic.apply(l, data);
|
|
2496
|
+
});
|
|
2497
|
+
if (!jsonLogic.is_logic(logic)) return logic;
|
|
2498
|
+
var op = jsonLogic.get_operator(logic);
|
|
2499
|
+
var values = logic[op];
|
|
2500
|
+
var i;
|
|
2501
|
+
var current;
|
|
2502
|
+
var scopedLogic;
|
|
2503
|
+
var scopedData;
|
|
2504
|
+
var initial;
|
|
2505
|
+
if (!Array.isArray(values)) values = [values];
|
|
2506
|
+
if (op === "if" || op == "?:") {
|
|
2507
|
+
for (i = 0; i < values.length - 1; i += 2) if (jsonLogic.truthy(jsonLogic.apply(values[i], data))) return jsonLogic.apply(values[i + 1], data);
|
|
2508
|
+
if (values.length === i + 1) return jsonLogic.apply(values[i], data);
|
|
2509
|
+
return null;
|
|
2510
|
+
} else if (op === "and") {
|
|
2511
|
+
for (i = 0; i < values.length; i += 1) {
|
|
2512
|
+
current = jsonLogic.apply(values[i], data);
|
|
2513
|
+
if (!jsonLogic.truthy(current)) return current;
|
|
2514
|
+
}
|
|
2515
|
+
return current;
|
|
2516
|
+
} else if (op === "or") {
|
|
2517
|
+
for (i = 0; i < values.length; i += 1) {
|
|
2518
|
+
current = jsonLogic.apply(values[i], data);
|
|
2519
|
+
if (jsonLogic.truthy(current)) return current;
|
|
2520
|
+
}
|
|
2521
|
+
return current;
|
|
2522
|
+
} else if (op === "filter") {
|
|
2523
|
+
scopedData = jsonLogic.apply(values[0], data);
|
|
2524
|
+
scopedLogic = values[1];
|
|
2525
|
+
if (!Array.isArray(scopedData)) return [];
|
|
2526
|
+
return scopedData.filter(function(datum) {
|
|
2527
|
+
return jsonLogic.truthy(jsonLogic.apply(scopedLogic, datum));
|
|
2528
|
+
});
|
|
2529
|
+
} else if (op === "map") {
|
|
2530
|
+
scopedData = jsonLogic.apply(values[0], data);
|
|
2531
|
+
scopedLogic = values[1];
|
|
2532
|
+
if (!Array.isArray(scopedData)) return [];
|
|
2533
|
+
return scopedData.map(function(datum) {
|
|
2534
|
+
return jsonLogic.apply(scopedLogic, datum);
|
|
2535
|
+
});
|
|
2536
|
+
} else if (op === "reduce") {
|
|
2537
|
+
scopedData = jsonLogic.apply(values[0], data);
|
|
2538
|
+
scopedLogic = values[1];
|
|
2539
|
+
initial = typeof values[2] !== "undefined" ? jsonLogic.apply(values[2], data) : null;
|
|
2540
|
+
if (!Array.isArray(scopedData)) return initial;
|
|
2541
|
+
return scopedData.reduce(function(accumulator, current) {
|
|
2542
|
+
return jsonLogic.apply(scopedLogic, {
|
|
2543
|
+
current,
|
|
2544
|
+
accumulator
|
|
2545
|
+
});
|
|
2546
|
+
}, initial);
|
|
2547
|
+
} else if (op === "all") {
|
|
2548
|
+
scopedData = jsonLogic.apply(values[0], data);
|
|
2549
|
+
scopedLogic = values[1];
|
|
2550
|
+
if (!Array.isArray(scopedData) || !scopedData.length) return false;
|
|
2551
|
+
for (i = 0; i < scopedData.length; i += 1) if (!jsonLogic.truthy(jsonLogic.apply(scopedLogic, scopedData[i]))) return false;
|
|
2552
|
+
return true;
|
|
2553
|
+
} else if (op === "none") {
|
|
2554
|
+
scopedData = jsonLogic.apply(values[0], data);
|
|
2555
|
+
scopedLogic = values[1];
|
|
2556
|
+
if (!Array.isArray(scopedData) || !scopedData.length) return true;
|
|
2557
|
+
for (i = 0; i < scopedData.length; i += 1) if (jsonLogic.truthy(jsonLogic.apply(scopedLogic, scopedData[i]))) return false;
|
|
2558
|
+
return true;
|
|
2559
|
+
} else if (op === "some") {
|
|
2560
|
+
scopedData = jsonLogic.apply(values[0], data);
|
|
2561
|
+
scopedLogic = values[1];
|
|
2562
|
+
if (!Array.isArray(scopedData) || !scopedData.length) return false;
|
|
2563
|
+
for (i = 0; i < scopedData.length; i += 1) if (jsonLogic.truthy(jsonLogic.apply(scopedLogic, scopedData[i]))) return true;
|
|
2564
|
+
return false;
|
|
2565
|
+
}
|
|
2566
|
+
values = values.map(function(val) {
|
|
2567
|
+
return jsonLogic.apply(val, data);
|
|
2568
|
+
});
|
|
2569
|
+
if (operations.hasOwnProperty(op) && typeof operations[op] === "function") return operations[op].apply(data, values);
|
|
2570
|
+
else if (op.indexOf(".") > 0) {
|
|
2571
|
+
var sub_ops = String(op).split(".");
|
|
2572
|
+
var operation = operations;
|
|
2573
|
+
for (i = 0; i < sub_ops.length; i++) {
|
|
2574
|
+
if (!operation.hasOwnProperty(sub_ops[i])) throw new Error("Unrecognized operation " + op + " (failed at " + sub_ops.slice(0, i + 1).join(".") + ")");
|
|
2575
|
+
operation = operation[sub_ops[i]];
|
|
2576
|
+
}
|
|
2577
|
+
return operation.apply(data, values);
|
|
2578
|
+
}
|
|
2579
|
+
throw new Error("Unrecognized operation " + op);
|
|
2580
|
+
};
|
|
2581
|
+
jsonLogic.uses_data = function(logic) {
|
|
2582
|
+
var collection = [];
|
|
2583
|
+
if (jsonLogic.is_logic(logic)) {
|
|
2584
|
+
var op = jsonLogic.get_operator(logic);
|
|
2585
|
+
var values = logic[op];
|
|
2586
|
+
if (!Array.isArray(values)) values = [values];
|
|
2587
|
+
if (op === "var") collection.push(values[0]);
|
|
2588
|
+
else values.forEach(function(val) {
|
|
2589
|
+
collection.push.apply(collection, jsonLogic.uses_data(val));
|
|
2590
|
+
});
|
|
2591
|
+
}
|
|
2592
|
+
return arrayUnique(collection);
|
|
2593
|
+
};
|
|
2594
|
+
jsonLogic.add_operation = function(name, code) {
|
|
2595
|
+
operations[name] = code;
|
|
2596
|
+
};
|
|
2597
|
+
jsonLogic.rm_operation = function(name) {
|
|
2598
|
+
delete operations[name];
|
|
2599
|
+
};
|
|
2600
|
+
jsonLogic.rule_like = function(rule, pattern) {
|
|
2601
|
+
if (pattern === rule) return true;
|
|
2602
|
+
if (pattern === "@") return true;
|
|
2603
|
+
if (pattern === "number") return typeof rule === "number";
|
|
2604
|
+
if (pattern === "string") return typeof rule === "string";
|
|
2605
|
+
if (pattern === "array") return Array.isArray(rule) && !jsonLogic.is_logic(rule);
|
|
2606
|
+
if (jsonLogic.is_logic(pattern)) {
|
|
2607
|
+
if (jsonLogic.is_logic(rule)) {
|
|
2608
|
+
var pattern_op = jsonLogic.get_operator(pattern);
|
|
2609
|
+
var rule_op = jsonLogic.get_operator(rule);
|
|
2610
|
+
if (pattern_op === "@" || pattern_op === rule_op) return jsonLogic.rule_like(jsonLogic.get_values(rule, false), jsonLogic.get_values(pattern, false));
|
|
2611
|
+
}
|
|
2612
|
+
return false;
|
|
2613
|
+
}
|
|
2614
|
+
if (Array.isArray(pattern)) if (Array.isArray(rule)) {
|
|
2615
|
+
if (pattern.length !== rule.length) return false;
|
|
2616
|
+
for (var i = 0; i < pattern.length; i += 1) if (!jsonLogic.rule_like(rule[i], pattern[i])) return false;
|
|
2617
|
+
return true;
|
|
2618
|
+
} else return false;
|
|
2619
|
+
return false;
|
|
2620
|
+
};
|
|
2621
|
+
return jsonLogic;
|
|
2622
|
+
});
|
|
2623
|
+
})))();
|
|
2624
|
+
//#endregion
|
|
2625
|
+
//#region ../common/src/util/filter-operator-resolution.ts
|
|
2626
|
+
/**
|
|
2627
|
+
* Default operators offered per property type, before engine capabilities and
|
|
2628
|
+
* per-property narrowing are applied. These mirror what the built-in filter
|
|
2629
|
+
* fields can render.
|
|
2630
|
+
*/
|
|
2631
|
+
var COMPARISON_OPS = [
|
|
2632
|
+
"==",
|
|
2633
|
+
"!=",
|
|
2634
|
+
">",
|
|
2635
|
+
">=",
|
|
2636
|
+
"<",
|
|
2637
|
+
"<="
|
|
2638
|
+
];
|
|
2639
|
+
var NULL_CHECK_OPS = ["is-null", "is-not-null"];
|
|
2640
|
+
var MEMBERSHIP_OPS = ["in", "not-in"];
|
|
2641
|
+
var PATTERN_OPS = [
|
|
2642
|
+
"like",
|
|
2643
|
+
"ilike",
|
|
2644
|
+
"not-like",
|
|
2645
|
+
"not-ilike"
|
|
2646
|
+
];
|
|
2647
|
+
[
|
|
2648
|
+
...COMPARISON_OPS,
|
|
2649
|
+
...MEMBERSHIP_OPS,
|
|
2650
|
+
...PATTERN_OPS,
|
|
2651
|
+
...NULL_CHECK_OPS
|
|
2652
|
+
], [
|
|
2653
|
+
...COMPARISON_OPS,
|
|
2654
|
+
...MEMBERSHIP_OPS,
|
|
2655
|
+
...NULL_CHECK_OPS
|
|
2656
|
+
], [...COMPARISON_OPS, ...NULL_CHECK_OPS], [...NULL_CHECK_OPS], [...MEMBERSHIP_OPS, ...NULL_CHECK_OPS], [...MEMBERSHIP_OPS, ...NULL_CHECK_OPS];
|
|
2657
|
+
//#endregion
|
|
2658
|
+
//#region ../../node_modules/.pnpm/fast-equals@6.0.0/node_modules/fast-equals/dist/es/index.mjs
|
|
2659
|
+
var { getOwnPropertyNames, getOwnPropertySymbols } = Object;
|
|
2660
|
+
var { hasOwnProperty } = Object.prototype;
|
|
2661
|
+
/**
|
|
2662
|
+
* Combine two comparators into a single comparators.
|
|
2663
|
+
*/
|
|
2664
|
+
function combineComparators(comparatorA, comparatorB) {
|
|
2665
|
+
return function isEqual(a, b, state) {
|
|
2666
|
+
return comparatorA(a, b, state) && comparatorB(a, b, state);
|
|
2667
|
+
};
|
|
2668
|
+
}
|
|
2669
|
+
/**
|
|
2670
|
+
* Wrap the provided `areItemsEqual` method to manage the circular state, allowing
|
|
2671
|
+
* for circular references to be safely included in the comparison without creating
|
|
2672
|
+
* stack overflows.
|
|
2673
|
+
*/
|
|
2674
|
+
function createIsCircular(areItemsEqual) {
|
|
2675
|
+
return function isCircular(a, b, state) {
|
|
2676
|
+
if (!a || !b || typeof a !== "object" || typeof b !== "object") return areItemsEqual(a, b, state);
|
|
2677
|
+
const { cache } = state;
|
|
2678
|
+
const cachedA = cache.get(a);
|
|
2679
|
+
const cachedB = cache.get(b);
|
|
2680
|
+
if (cachedA && cachedB) return cachedA === b && cachedB === a;
|
|
2681
|
+
cache.set(a, b);
|
|
2682
|
+
cache.set(b, a);
|
|
2683
|
+
const result = areItemsEqual(a, b, state);
|
|
2684
|
+
cache.delete(a);
|
|
2685
|
+
cache.delete(b);
|
|
2686
|
+
return result;
|
|
2687
|
+
};
|
|
2688
|
+
}
|
|
2689
|
+
/**
|
|
2690
|
+
* Get the properties to strictly examine, which include both own properties that are
|
|
2691
|
+
* not enumerable and symbol properties.
|
|
2692
|
+
*/
|
|
2693
|
+
function getStrictProperties(object) {
|
|
2694
|
+
return getOwnPropertyNames(object).concat(getOwnPropertySymbols(object));
|
|
2695
|
+
}
|
|
2696
|
+
/**
|
|
2697
|
+
* Whether the object contains the property passed as an own property.
|
|
2698
|
+
*/
|
|
2699
|
+
var hasOwn = Object.hasOwn || ((object, property) => hasOwnProperty.call(object, property));
|
|
2700
|
+
var PREACT_VNODE = "__v";
|
|
2701
|
+
var PREACT_OWNER = "__o";
|
|
2702
|
+
var REACT_OWNER = "_owner";
|
|
2703
|
+
var { getOwnPropertyDescriptor, keys } = Object;
|
|
2704
|
+
/**
|
|
2705
|
+
* Whether the values passed are equal based on a [SameValue](https://262.ecma-international.org/7.0/#sec-samevalue) basis.
|
|
2706
|
+
* Simplified, this maps to if the two values are referentially equal to one another (`a === b`) or both are `NaN`.
|
|
2707
|
+
*
|
|
2708
|
+
* @note
|
|
2709
|
+
* When available in the environment, this is just a re-export of the global
|
|
2710
|
+
* [`Object.is`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) method.
|
|
2711
|
+
*/
|
|
2712
|
+
var sameValueEqual = Object.is || function sameValueEqual(a, b) {
|
|
2713
|
+
return a === b ? a !== 0 || 1 / a === 1 / b : a !== a && b !== b;
|
|
2714
|
+
};
|
|
2715
|
+
/**
|
|
2716
|
+
* Whether the values passed are equal based on a
|
|
2717
|
+
* [Strict Equality Comparison](https://262.ecma-international.org/7.0/#sec-strict-equality-comparison) basis.
|
|
2718
|
+
* Simplified, this maps to if the two values are referentially equal to one another (`a === b`).
|
|
2719
|
+
*
|
|
2720
|
+
* @note
|
|
2721
|
+
* This is mainly available as a convenience function, such as being a default when a function to determine equality between
|
|
2722
|
+
* two objects is used.
|
|
2723
|
+
*/
|
|
2724
|
+
function strictEqual(a, b) {
|
|
2725
|
+
return a === b;
|
|
2726
|
+
}
|
|
2727
|
+
/**
|
|
2728
|
+
* Whether the array buffers are equal in value.
|
|
2729
|
+
*/
|
|
2730
|
+
function areArrayBuffersEqual(a, b) {
|
|
2731
|
+
return a.byteLength === b.byteLength && areTypedArraysEqual(new Uint8Array(a), new Uint8Array(b));
|
|
2732
|
+
}
|
|
2733
|
+
/**
|
|
2734
|
+
* Whether the arrays are equal in value.
|
|
2735
|
+
*/
|
|
2736
|
+
function areArraysEqual(a, b, state) {
|
|
2737
|
+
let index = a.length;
|
|
2738
|
+
if (b.length !== index) return false;
|
|
2739
|
+
while (index-- > 0) if (!state.equals(a[index], b[index], index, index, a, b, state)) return false;
|
|
2740
|
+
return true;
|
|
2741
|
+
}
|
|
2742
|
+
/**
|
|
2743
|
+
* Whether the dataviews are equal in value.
|
|
2744
|
+
*/
|
|
2745
|
+
function areDataViewsEqual(a, b) {
|
|
2746
|
+
return a.byteLength === b.byteLength && areTypedArraysEqual(new Uint8Array(a.buffer, a.byteOffset, a.byteLength), new Uint8Array(b.buffer, b.byteOffset, b.byteLength));
|
|
2747
|
+
}
|
|
2748
|
+
/**
|
|
2749
|
+
* Whether the dates passed are equal in value.
|
|
2750
|
+
*/
|
|
2751
|
+
function areDatesEqual(a, b) {
|
|
2752
|
+
return sameValueEqual(a.getTime(), b.getTime());
|
|
2753
|
+
}
|
|
2754
|
+
/**
|
|
2755
|
+
* Whether the errors passed are equal in value.
|
|
2756
|
+
*/
|
|
2757
|
+
function areErrorsEqual(a, b) {
|
|
2758
|
+
return a.name === b.name && a.message === b.message && a.cause === b.cause && a.stack === b.stack;
|
|
2759
|
+
}
|
|
2760
|
+
/**
|
|
2761
|
+
* Whether the `Map`s are equal in value.
|
|
2762
|
+
*/
|
|
2763
|
+
function areMapsEqual(a, b, state) {
|
|
2764
|
+
const size = a.size;
|
|
2765
|
+
if (size !== b.size) return false;
|
|
2766
|
+
if (!size) return true;
|
|
2767
|
+
const matchedIndices = new Array(size);
|
|
2768
|
+
const aIterable = a.entries();
|
|
2769
|
+
let aResult;
|
|
2770
|
+
let bResult;
|
|
2771
|
+
let index = 0;
|
|
2772
|
+
while (aResult = aIterable.next()) {
|
|
2773
|
+
if (aResult.done) break;
|
|
2774
|
+
const bIterable = b.entries();
|
|
2775
|
+
let hasMatch = false;
|
|
2776
|
+
let matchIndex = 0;
|
|
2777
|
+
while (bResult = bIterable.next()) {
|
|
2778
|
+
if (bResult.done) break;
|
|
2779
|
+
if (matchedIndices[matchIndex]) {
|
|
2780
|
+
matchIndex++;
|
|
2781
|
+
continue;
|
|
2782
|
+
}
|
|
2783
|
+
const aEntry = aResult.value;
|
|
2784
|
+
const bEntry = bResult.value;
|
|
2785
|
+
if (state.equals(aEntry[0], bEntry[0], index, matchIndex, a, b, state) && state.equals(aEntry[1], bEntry[1], aEntry[0], bEntry[0], a, b, state)) {
|
|
2786
|
+
hasMatch = matchedIndices[matchIndex] = true;
|
|
2787
|
+
break;
|
|
2788
|
+
}
|
|
2789
|
+
matchIndex++;
|
|
2790
|
+
}
|
|
2791
|
+
if (!hasMatch) return false;
|
|
2792
|
+
index++;
|
|
2793
|
+
}
|
|
2794
|
+
return true;
|
|
2795
|
+
}
|
|
2796
|
+
/**
|
|
2797
|
+
* Whether the objects are equal in value.
|
|
2798
|
+
*/
|
|
2799
|
+
function areObjectsEqual(a, b, state) {
|
|
2800
|
+
const properties = keys(a);
|
|
2801
|
+
let index = properties.length;
|
|
2802
|
+
if (keys(b).length !== index) return false;
|
|
2803
|
+
while (index-- > 0) if (!isPropertyEqual(a, b, state, properties[index])) return false;
|
|
2804
|
+
return true;
|
|
2805
|
+
}
|
|
2806
|
+
/**
|
|
2807
|
+
* Whether the objects are equal in value with strict property checking.
|
|
2808
|
+
*/
|
|
2809
|
+
function areObjectsEqualStrict(a, b, state) {
|
|
2810
|
+
const properties = getStrictProperties(a);
|
|
2811
|
+
let index = properties.length;
|
|
2812
|
+
if (getStrictProperties(b).length !== index) return false;
|
|
2813
|
+
let property;
|
|
2814
|
+
let descriptorA;
|
|
2815
|
+
let descriptorB;
|
|
2816
|
+
while (index-- > 0) {
|
|
2817
|
+
property = properties[index];
|
|
2818
|
+
if (!isPropertyEqual(a, b, state, property)) return false;
|
|
2819
|
+
descriptorA = getOwnPropertyDescriptor(a, property);
|
|
2820
|
+
descriptorB = getOwnPropertyDescriptor(b, property);
|
|
2821
|
+
if ((descriptorA || descriptorB) && (!descriptorA || !descriptorB || descriptorA.configurable !== descriptorB.configurable || descriptorA.enumerable !== descriptorB.enumerable || descriptorA.writable !== descriptorB.writable)) return false;
|
|
2822
|
+
}
|
|
2823
|
+
return true;
|
|
2824
|
+
}
|
|
2825
|
+
/**
|
|
2826
|
+
* Whether the primitive wrappers passed are equal in value.
|
|
2827
|
+
*/
|
|
2828
|
+
function arePrimitiveWrappersEqual(a, b) {
|
|
2829
|
+
return sameValueEqual(a.valueOf(), b.valueOf());
|
|
2830
|
+
}
|
|
2831
|
+
/**
|
|
2832
|
+
* Whether the regexps passed are equal in value.
|
|
2833
|
+
*/
|
|
2834
|
+
function areRegExpsEqual(a, b) {
|
|
2835
|
+
return a.source === b.source && a.flags === b.flags;
|
|
2836
|
+
}
|
|
2837
|
+
/**
|
|
2838
|
+
* Whether the `Set`s are equal in value.
|
|
2839
|
+
*/
|
|
2840
|
+
function areSetsEqual(a, b, state) {
|
|
2841
|
+
const size = a.size;
|
|
2842
|
+
if (size !== b.size) return false;
|
|
2843
|
+
if (!size) return true;
|
|
2844
|
+
const matchedIndices = new Array(size);
|
|
2845
|
+
const aIterable = a.values();
|
|
2846
|
+
let aResult;
|
|
2847
|
+
let bResult;
|
|
2848
|
+
while (aResult = aIterable.next()) {
|
|
2849
|
+
if (aResult.done) break;
|
|
2850
|
+
const bIterable = b.values();
|
|
2851
|
+
let hasMatch = false;
|
|
2852
|
+
let matchIndex = 0;
|
|
2853
|
+
while (bResult = bIterable.next()) {
|
|
2854
|
+
if (bResult.done) break;
|
|
2855
|
+
if (!matchedIndices[matchIndex] && state.equals(aResult.value, bResult.value, aResult.value, bResult.value, a, b, state)) {
|
|
2856
|
+
hasMatch = matchedIndices[matchIndex] = true;
|
|
2857
|
+
break;
|
|
2858
|
+
}
|
|
2859
|
+
matchIndex++;
|
|
2860
|
+
}
|
|
2861
|
+
if (!hasMatch) return false;
|
|
2862
|
+
}
|
|
2863
|
+
return true;
|
|
2864
|
+
}
|
|
2865
|
+
/**
|
|
2866
|
+
* Whether the TypedArray instances are equal in value.
|
|
2867
|
+
*/
|
|
2868
|
+
function areTypedArraysEqual(a, b) {
|
|
2869
|
+
let index = a.byteLength;
|
|
2870
|
+
if (b.byteLength !== index || a.byteOffset !== b.byteOffset) return false;
|
|
2871
|
+
while (index-- > 0) if (a[index] !== b[index]) return false;
|
|
2872
|
+
return true;
|
|
2873
|
+
}
|
|
2874
|
+
/**
|
|
2875
|
+
* Whether the URL instances are equal in value.
|
|
2876
|
+
*/
|
|
2877
|
+
function areUrlsEqual(a, b) {
|
|
2878
|
+
return a.hostname === b.hostname && a.pathname === b.pathname && a.protocol === b.protocol && a.port === b.port && a.hash === b.hash && a.username === b.username && a.password === b.password;
|
|
2879
|
+
}
|
|
2880
|
+
function isPropertyEqual(a, b, state, property) {
|
|
2881
|
+
if ((property === REACT_OWNER || property === PREACT_OWNER || property === PREACT_VNODE) && (a.$$typeof || b.$$typeof)) return true;
|
|
2882
|
+
return hasOwn(b, property) && state.equals(a[property], b[property], property, property, a, b, state);
|
|
2883
|
+
}
|
|
2884
|
+
var toString = Object.prototype.toString;
|
|
2885
|
+
/**
|
|
2886
|
+
* Create a comparator method based on the type-specific equality comparators passed.
|
|
2887
|
+
*/
|
|
2888
|
+
function createEqualityComparator(config) {
|
|
2889
|
+
const supportedComparatorMap = createSupportedComparatorMap(config);
|
|
2890
|
+
const { areArraysEqual, areDatesEqual, areFunctionsEqual, areMapsEqual, areNumbersEqual, areObjectsEqual, areRegExpsEqual, areSetsEqual, getUnsupportedCustomComparator } = config;
|
|
2891
|
+
/**
|
|
2892
|
+
* compare the value of the two objects and return true if they are equivalent in values
|
|
2893
|
+
*/
|
|
2894
|
+
return function comparator(a, b, state) {
|
|
2895
|
+
if (a === b) return true;
|
|
2896
|
+
if (a == null || b == null) return false;
|
|
2897
|
+
const type = typeof a;
|
|
2898
|
+
if (type !== typeof b) return false;
|
|
2899
|
+
if (type !== "object") {
|
|
2900
|
+
if (type === "number" || type === "bigint") return areNumbersEqual(a, b, state);
|
|
2901
|
+
if (type === "function") return areFunctionsEqual(a, b, state);
|
|
2902
|
+
return false;
|
|
2903
|
+
}
|
|
2904
|
+
const constructor = a.constructor;
|
|
2905
|
+
if (constructor !== b.constructor) return false;
|
|
2906
|
+
if (constructor === Object) return areObjectsEqual(a, b, state);
|
|
2907
|
+
if (constructor === Array) return areArraysEqual(a, b, state);
|
|
2908
|
+
if (constructor === Date) return areDatesEqual(a, b, state);
|
|
2909
|
+
if (constructor === RegExp) return areRegExpsEqual(a, b, state);
|
|
2910
|
+
if (constructor === Map) return areMapsEqual(a, b, state);
|
|
2911
|
+
if (constructor === Set) return areSetsEqual(a, b, state);
|
|
2912
|
+
if (constructor === Promise) return false;
|
|
2913
|
+
if (Array.isArray(a)) return areArraysEqual(a, b, state);
|
|
2914
|
+
const tag = toString.call(a);
|
|
2915
|
+
const supportedComparator = supportedComparatorMap[tag];
|
|
2916
|
+
if (supportedComparator) return supportedComparator(a, b, state);
|
|
2917
|
+
const unsupportedCustomComparator = getUnsupportedCustomComparator && getUnsupportedCustomComparator(a, b, state, tag);
|
|
2918
|
+
if (unsupportedCustomComparator) return unsupportedCustomComparator(a, b, state);
|
|
2919
|
+
return false;
|
|
2920
|
+
};
|
|
2921
|
+
}
|
|
2922
|
+
/**
|
|
2923
|
+
* Create the configuration object used for building comparators.
|
|
2924
|
+
*/
|
|
2925
|
+
function createEqualityComparatorConfig({ circular, createCustomConfig, strict }) {
|
|
2926
|
+
let config = {
|
|
2927
|
+
areArrayBuffersEqual,
|
|
2928
|
+
areArraysEqual: strict ? areObjectsEqualStrict : areArraysEqual,
|
|
2929
|
+
areDataViewsEqual,
|
|
2930
|
+
areDatesEqual,
|
|
2931
|
+
areErrorsEqual,
|
|
2932
|
+
areFunctionsEqual: strictEqual,
|
|
2933
|
+
areMapsEqual: strict ? combineComparators(areMapsEqual, areObjectsEqualStrict) : areMapsEqual,
|
|
2934
|
+
areNumbersEqual: sameValueEqual,
|
|
2935
|
+
areObjectsEqual: strict ? areObjectsEqualStrict : areObjectsEqual,
|
|
2936
|
+
arePrimitiveWrappersEqual,
|
|
2937
|
+
areRegExpsEqual,
|
|
2938
|
+
areSetsEqual: strict ? combineComparators(areSetsEqual, areObjectsEqualStrict) : areSetsEqual,
|
|
2939
|
+
areTypedArraysEqual: strict ? combineComparators(areTypedArraysEqual, areObjectsEqualStrict) : areTypedArraysEqual,
|
|
2940
|
+
areUrlsEqual,
|
|
2941
|
+
getUnsupportedCustomComparator: void 0
|
|
2942
|
+
};
|
|
2943
|
+
if (createCustomConfig) config = Object.assign({}, config, createCustomConfig(config));
|
|
2944
|
+
if (circular) {
|
|
2945
|
+
const areArraysEqual = createIsCircular(config.areArraysEqual);
|
|
2946
|
+
const areMapsEqual = createIsCircular(config.areMapsEqual);
|
|
2947
|
+
const areObjectsEqual = createIsCircular(config.areObjectsEqual);
|
|
2948
|
+
const areSetsEqual = createIsCircular(config.areSetsEqual);
|
|
2949
|
+
config = Object.assign({}, config, {
|
|
2950
|
+
areArraysEqual,
|
|
2951
|
+
areMapsEqual,
|
|
2952
|
+
areObjectsEqual,
|
|
2953
|
+
areSetsEqual
|
|
2954
|
+
});
|
|
2955
|
+
}
|
|
2956
|
+
return config;
|
|
2957
|
+
}
|
|
2958
|
+
/**
|
|
2959
|
+
* Default equality comparator pass-through, used as the standard `isEqual` creator for
|
|
2960
|
+
* use inside the built comparator.
|
|
2961
|
+
*/
|
|
2962
|
+
function createInternalEqualityComparator(compare) {
|
|
2963
|
+
return function(a, b, _indexOrKeyA, _indexOrKeyB, _parentA, _parentB, state) {
|
|
2964
|
+
return compare(a, b, state);
|
|
2965
|
+
};
|
|
2966
|
+
}
|
|
2967
|
+
/**
|
|
2968
|
+
* Create the `isEqual` function used by the consuming application.
|
|
2969
|
+
*/
|
|
2970
|
+
function createIsEqual({ circular, comparator, createState, equals, strict }) {
|
|
2971
|
+
if (createState) return function isEqual(a, b) {
|
|
2972
|
+
const { cache = circular ? /* @__PURE__ */ new WeakMap() : void 0, meta } = createState();
|
|
2973
|
+
return comparator(a, b, {
|
|
2974
|
+
cache,
|
|
2975
|
+
equals,
|
|
2976
|
+
meta,
|
|
2977
|
+
strict
|
|
2978
|
+
});
|
|
2979
|
+
};
|
|
2980
|
+
if (circular) return function isEqual(a, b) {
|
|
2981
|
+
return comparator(a, b, {
|
|
2982
|
+
cache: /* @__PURE__ */ new WeakMap(),
|
|
2983
|
+
equals,
|
|
2984
|
+
meta: void 0,
|
|
2985
|
+
strict
|
|
2986
|
+
});
|
|
2987
|
+
};
|
|
2988
|
+
const state = {
|
|
2989
|
+
cache: void 0,
|
|
2990
|
+
equals,
|
|
2991
|
+
meta: void 0,
|
|
2992
|
+
strict
|
|
2993
|
+
};
|
|
2994
|
+
return function isEqual(a, b) {
|
|
2995
|
+
return comparator(a, b, state);
|
|
2996
|
+
};
|
|
2997
|
+
}
|
|
2998
|
+
/**
|
|
2999
|
+
* Create a map of `toString()` values to their respective handlers for `tag`-based lookups.
|
|
3000
|
+
*/
|
|
3001
|
+
function createSupportedComparatorMap({ areArrayBuffersEqual, areArraysEqual, areDataViewsEqual, areDatesEqual, areErrorsEqual, areFunctionsEqual, areMapsEqual, areNumbersEqual, areObjectsEqual, arePrimitiveWrappersEqual, areRegExpsEqual, areSetsEqual, areTypedArraysEqual, areUrlsEqual }) {
|
|
3002
|
+
return {
|
|
3003
|
+
"[object Arguments]": areObjectsEqual,
|
|
3004
|
+
"[object Array]": areArraysEqual,
|
|
3005
|
+
"[object ArrayBuffer]": areArrayBuffersEqual,
|
|
3006
|
+
"[object AsyncGeneratorFunction]": areFunctionsEqual,
|
|
3007
|
+
"[object BigInt]": areNumbersEqual,
|
|
3008
|
+
"[object BigInt64Array]": areTypedArraysEqual,
|
|
3009
|
+
"[object BigUint64Array]": areTypedArraysEqual,
|
|
3010
|
+
"[object Boolean]": arePrimitiveWrappersEqual,
|
|
3011
|
+
"[object DataView]": areDataViewsEqual,
|
|
3012
|
+
"[object Date]": areDatesEqual,
|
|
3013
|
+
"[object Error]": areErrorsEqual,
|
|
3014
|
+
"[object Float16Array]": areTypedArraysEqual,
|
|
3015
|
+
"[object Float32Array]": areTypedArraysEqual,
|
|
3016
|
+
"[object Float64Array]": areTypedArraysEqual,
|
|
3017
|
+
"[object Function]": areFunctionsEqual,
|
|
3018
|
+
"[object GeneratorFunction]": areFunctionsEqual,
|
|
3019
|
+
"[object Int8Array]": areTypedArraysEqual,
|
|
3020
|
+
"[object Int16Array]": areTypedArraysEqual,
|
|
3021
|
+
"[object Int32Array]": areTypedArraysEqual,
|
|
3022
|
+
"[object Map]": areMapsEqual,
|
|
3023
|
+
"[object Number]": arePrimitiveWrappersEqual,
|
|
3024
|
+
"[object Object]": (a, b, state) => typeof a.then !== "function" && typeof b.then !== "function" && areObjectsEqual(a, b, state),
|
|
3025
|
+
"[object RegExp]": areRegExpsEqual,
|
|
3026
|
+
"[object Set]": areSetsEqual,
|
|
3027
|
+
"[object String]": arePrimitiveWrappersEqual,
|
|
3028
|
+
"[object URL]": areUrlsEqual,
|
|
3029
|
+
"[object Uint8Array]": areTypedArraysEqual,
|
|
3030
|
+
"[object Uint8ClampedArray]": areTypedArraysEqual,
|
|
3031
|
+
"[object Uint16Array]": areTypedArraysEqual,
|
|
3032
|
+
"[object Uint32Array]": areTypedArraysEqual
|
|
3033
|
+
};
|
|
3034
|
+
}
|
|
3035
|
+
/**
|
|
3036
|
+
* Whether the items passed are deeply-equal in value.
|
|
3037
|
+
*/
|
|
3038
|
+
var deepEqual = createCustomEqual();
|
|
3039
|
+
createCustomEqual({ strict: true });
|
|
3040
|
+
createCustomEqual({ circular: true });
|
|
3041
|
+
createCustomEqual({
|
|
3042
|
+
circular: true,
|
|
3043
|
+
strict: true
|
|
3044
|
+
});
|
|
3045
|
+
createCustomEqual({ createInternalComparator: () => sameValueEqual });
|
|
3046
|
+
createCustomEqual({
|
|
3047
|
+
strict: true,
|
|
3048
|
+
createInternalComparator: () => sameValueEqual
|
|
3049
|
+
});
|
|
3050
|
+
createCustomEqual({
|
|
3051
|
+
circular: true,
|
|
3052
|
+
createInternalComparator: () => sameValueEqual
|
|
3053
|
+
});
|
|
3054
|
+
createCustomEqual({
|
|
3055
|
+
circular: true,
|
|
3056
|
+
createInternalComparator: () => sameValueEqual,
|
|
3057
|
+
strict: true
|
|
3058
|
+
});
|
|
3059
|
+
/**
|
|
3060
|
+
* Create a custom equality comparison method.
|
|
3061
|
+
*
|
|
3062
|
+
* This can be done to create very targeted comparisons in extreme hot-path scenarios
|
|
3063
|
+
* where the standard methods are not performant enough, but can also be used to provide
|
|
3064
|
+
* support for legacy environments that do not support expected features like
|
|
3065
|
+
* `RegExp.prototype.flags` out of the box.
|
|
3066
|
+
*/
|
|
3067
|
+
function createCustomEqual(options = {}) {
|
|
3068
|
+
const { circular = false, createInternalComparator: createCustomInternalComparator, createState, strict = false } = options;
|
|
3069
|
+
const comparator = createEqualityComparator(createEqualityComparatorConfig(options));
|
|
3070
|
+
return createIsEqual({
|
|
3071
|
+
circular,
|
|
3072
|
+
comparator,
|
|
3073
|
+
createState,
|
|
3074
|
+
equals: createCustomInternalComparator ? createCustomInternalComparator(comparator) : createInternalEqualityComparator(comparator),
|
|
3075
|
+
strict
|
|
3076
|
+
});
|
|
3077
|
+
}
|
|
3078
|
+
//#endregion
|
|
3079
|
+
//#region ../common/src/data/resolveDataSource.ts
|
|
3080
|
+
/**
|
|
3081
|
+
* Resolve the effective data source for a collection — the single source of
|
|
3082
|
+
* truth shared by the frontend router, the backend driver registry, and the
|
|
3083
|
+
* editor's capability lookups.
|
|
3084
|
+
*
|
|
3085
|
+
* Resolution order:
|
|
3086
|
+
* 1. The routing **key** is `collection.dataSource`, else
|
|
3087
|
+
* {@link DEFAULT_DATA_SOURCE_KEY}.
|
|
3088
|
+
* 2. If a definition is registered for that key, it provides `engine`,
|
|
3089
|
+
* `transport`, and `databaseId`.
|
|
3090
|
+
* 3. Otherwise values are synthesized: `engine` from `collection.engine`
|
|
3091
|
+
* (or the key, or `"postgres"`), `transport` defaults to `"server"`,
|
|
3092
|
+
* and `databaseId` from the collection.
|
|
3093
|
+
*
|
|
3094
|
+
* `capabilities` are always derived from the resolved `engine`, so two
|
|
3095
|
+
* data sources sharing an engine share capabilities.
|
|
3096
|
+
*
|
|
3097
|
+
* @param collection the collection (or any object carrying the routing fields)
|
|
3098
|
+
* @param registry optional registry of declared data sources
|
|
3099
|
+
*/
|
|
3100
|
+
function resolveDataSource(collection, registry) {
|
|
3101
|
+
const key = collection?.dataSource ?? "(default)";
|
|
3102
|
+
const def = registry?.[key];
|
|
3103
|
+
const engine = def?.engine ?? collection?.engine ?? (key !== "(default)" ? key : "postgres");
|
|
3104
|
+
return {
|
|
3105
|
+
key,
|
|
3106
|
+
engine,
|
|
3107
|
+
transport: def?.transport ?? "server",
|
|
3108
|
+
databaseId: collection?.databaseId ?? def?.databaseId,
|
|
3109
|
+
capabilities: getDataSourceCapabilities(engine)
|
|
3110
|
+
};
|
|
3111
|
+
}
|
|
3112
|
+
//#endregion
|
|
3113
|
+
//#region ../common/src/collections/CollectionRegistry.ts
|
|
3114
|
+
var CollectionRegistry = class {
|
|
3115
|
+
/**
|
|
3116
|
+
* Declared data sources, used during normalization to resolve each
|
|
3117
|
+
* collection's engine (so `dataSource`-only collections get the right
|
|
3118
|
+
* capabilities). Empty by default.
|
|
3119
|
+
*/
|
|
3120
|
+
dataSources = {};
|
|
3121
|
+
/**
|
|
3122
|
+
* Global lifecycle callbacks applied to every collection.
|
|
3123
|
+
* Runs on all data paths (REST, WebSocket, `rebase.data`).
|
|
3124
|
+
* Execution order: global → collection → property callbacks.
|
|
3125
|
+
*/
|
|
3126
|
+
_globalCallbacks;
|
|
3127
|
+
/**
|
|
3128
|
+
* Set global lifecycle callbacks that apply to every collection.
|
|
3129
|
+
* Typically called once during backend initialization.
|
|
3130
|
+
*/
|
|
3131
|
+
setGlobalCallbacks(callbacks) {
|
|
3132
|
+
this._globalCallbacks = callbacks;
|
|
3133
|
+
}
|
|
3134
|
+
/**
|
|
3135
|
+
* Get the currently registered global callbacks, if any.
|
|
3136
|
+
*/
|
|
3137
|
+
getGlobalCallbacks() {
|
|
3138
|
+
return this._globalCallbacks;
|
|
3139
|
+
}
|
|
3140
|
+
collectionsByTableName = /* @__PURE__ */ new Map();
|
|
3141
|
+
collectionsBySlug = /* @__PURE__ */ new Map();
|
|
3142
|
+
rootCollections = [];
|
|
3143
|
+
cachedCollectionsList = null;
|
|
3144
|
+
rawCollectionsByTableName = /* @__PURE__ */ new Map();
|
|
3145
|
+
rawCollectionsBySlug = /* @__PURE__ */ new Map();
|
|
3146
|
+
rawRootCollections = [];
|
|
3147
|
+
cachedRawCollectionsList = null;
|
|
3148
|
+
lastRawInputEntity = null;
|
|
3149
|
+
constructor(collections, dataSources) {
|
|
3150
|
+
if (dataSources) this.dataSources = dataSources;
|
|
3151
|
+
if (collections) this.registerMultiple(collections);
|
|
3152
|
+
}
|
|
3153
|
+
/**
|
|
3154
|
+
* Provide the declared data sources used to resolve each collection's
|
|
3155
|
+
* engine during normalization. Set this before registering collections.
|
|
3156
|
+
* Returns true if the registry changed (callers may re-register).
|
|
3157
|
+
*/
|
|
3158
|
+
setDataSources(dataSources) {
|
|
3159
|
+
if (deepEqual(this.dataSources, dataSources)) return false;
|
|
3160
|
+
this.dataSources = dataSources ?? {};
|
|
3161
|
+
return true;
|
|
3162
|
+
}
|
|
3163
|
+
reset() {
|
|
3164
|
+
this.collectionsByTableName.clear();
|
|
3165
|
+
this.collectionsBySlug.clear();
|
|
3166
|
+
this.rootCollections = [];
|
|
3167
|
+
this.cachedCollectionsList = null;
|
|
3168
|
+
this.rawCollectionsByTableName.clear();
|
|
3169
|
+
this.rawCollectionsBySlug.clear();
|
|
3170
|
+
this.rawRootCollections = [];
|
|
3171
|
+
this.cachedRawCollectionsList = null;
|
|
3172
|
+
}
|
|
3173
|
+
/**
|
|
3174
|
+
* Registers a collection and its subcollections recursively.
|
|
3175
|
+
* Returns true if the collections have changed, false otherwise.
|
|
3176
|
+
*
|
|
3177
|
+
* Idempotent: compares the raw input (before normalization) against a stored
|
|
3178
|
+
* entity. Only re-normalizes and re-registers when the raw input actually changed.
|
|
3179
|
+
* @param collections
|
|
3180
|
+
*/
|
|
3181
|
+
registerMultiple(collections) {
|
|
3182
|
+
const rawEntity = collections.map((c) => removeFunctions(c));
|
|
3183
|
+
if (this.lastRawInputEntity && deepEqual(this.lastRawInputEntity, rawEntity)) return false;
|
|
3184
|
+
this.reset();
|
|
3185
|
+
collections.forEach((c) => {
|
|
3186
|
+
if (c.slug) this.collectionsBySlug.set(c.slug, c);
|
|
3187
|
+
this.collectionsByTableName.set(getTableName(c), c);
|
|
3188
|
+
});
|
|
3189
|
+
const normalizedCollections = collections.map((c) => this.normalizeCollection({ ...c }));
|
|
3190
|
+
normalizedCollections.forEach((c, index) => {
|
|
3191
|
+
const raw = deepClone(collections[index]);
|
|
3192
|
+
this.rootCollections.push(c);
|
|
3193
|
+
this.rawRootCollections.push(raw);
|
|
3194
|
+
const normalized = this.normalizeCollection(c);
|
|
3195
|
+
this.collectionsByTableName.set(getTableName(normalized), normalized);
|
|
3196
|
+
this.rawCollectionsByTableName.set(getTableName(raw), raw);
|
|
3197
|
+
if (normalized.slug) this.collectionsBySlug.set(normalized.slug, normalized);
|
|
3198
|
+
if (raw.slug) this.rawCollectionsBySlug.set(raw.slug, raw);
|
|
3199
|
+
});
|
|
3200
|
+
normalizedCollections.forEach((c) => {
|
|
3201
|
+
const subcollections = getSubcollections(c);
|
|
3202
|
+
if (subcollections && subcollections.length > 0) subcollections.forEach((subCollection) => {
|
|
3203
|
+
if (!subCollection) return;
|
|
3204
|
+
this._registerRecursively(this.normalizeCollection({ ...subCollection }), deepClone(subCollection));
|
|
3205
|
+
});
|
|
3206
|
+
});
|
|
3207
|
+
this.lastRawInputEntity = rawEntity;
|
|
3208
|
+
return true;
|
|
3209
|
+
}
|
|
3210
|
+
register(collection, rawCollection) {
|
|
3211
|
+
const raw = rawCollection ? deepClone(rawCollection) : deepClone(collection);
|
|
3212
|
+
this.rootCollections.push(collection);
|
|
3213
|
+
this.rawRootCollections.push(raw);
|
|
3214
|
+
this._registerRecursively(collection, raw);
|
|
3215
|
+
}
|
|
3216
|
+
_registerRecursively(collection, rawCollection) {
|
|
3217
|
+
if (this.collectionsByTableName.has(getTableName(collection))) return;
|
|
3218
|
+
const normalizedCollection = this.normalizeCollection(collection);
|
|
3219
|
+
this.collectionsByTableName.set(getTableName(normalizedCollection), normalizedCollection);
|
|
3220
|
+
this.rawCollectionsByTableName.set(getTableName(rawCollection), rawCollection);
|
|
3221
|
+
if (normalizedCollection.slug) this.collectionsBySlug.set(normalizedCollection.slug, normalizedCollection);
|
|
3222
|
+
if (rawCollection.slug) this.rawCollectionsBySlug.set(rawCollection.slug, rawCollection);
|
|
3223
|
+
const subcollections = getSubcollections(normalizedCollection);
|
|
3224
|
+
if (subcollections && subcollections.length > 0) subcollections.forEach((subCollection) => {
|
|
3225
|
+
if (!subCollection) return;
|
|
3226
|
+
this._registerRecursively(this.normalizeCollection({ ...subCollection }), deepClone(subCollection));
|
|
3227
|
+
});
|
|
3228
|
+
}
|
|
3229
|
+
normalizeCollection(collection) {
|
|
3230
|
+
const result = { ...collection };
|
|
3231
|
+
{
|
|
3232
|
+
const resolved = resolveDataSource(result, this.dataSources);
|
|
3233
|
+
if (!result.dataSource) result.dataSource = resolved.key;
|
|
3234
|
+
if (!result.engine) result.engine = resolved.engine;
|
|
3235
|
+
}
|
|
3236
|
+
const extractedRelations = this.extractRelationsFromProperties(result.properties);
|
|
3237
|
+
const relResult = result;
|
|
3238
|
+
const manualRelations = getDataSourceCapabilities(result.engine).supportsRelations ? relResult.relations ?? [] : [];
|
|
3239
|
+
const mergedRelationsRaw = [...extractedRelations];
|
|
3240
|
+
for (const manual of manualRelations) {
|
|
3241
|
+
const name = manual.relationName;
|
|
3242
|
+
if (!name) mergedRelationsRaw.push(manual);
|
|
3243
|
+
else {
|
|
3244
|
+
const existingIndex = mergedRelationsRaw.findIndex((r) => r.relationName === name);
|
|
3245
|
+
if (existingIndex === -1) mergedRelationsRaw.push(manual);
|
|
3246
|
+
else mergedRelationsRaw[existingIndex] = {
|
|
3247
|
+
...manual,
|
|
3248
|
+
...mergedRelationsRaw[existingIndex]
|
|
3249
|
+
};
|
|
3250
|
+
}
|
|
3251
|
+
}
|
|
3252
|
+
let mergedRelations = mergedRelationsRaw;
|
|
3253
|
+
if (getDataSourceCapabilities(result.engine).supportsRelations) {
|
|
3254
|
+
mergedRelations = mergedRelationsRaw.map((r) => {
|
|
3255
|
+
try {
|
|
3256
|
+
return sanitizeRelation(r, result, (slug) => this.get(slug));
|
|
3257
|
+
} catch {
|
|
3258
|
+
return r;
|
|
3259
|
+
}
|
|
3260
|
+
});
|
|
3261
|
+
relResult.relations = mergedRelations;
|
|
3262
|
+
}
|
|
3263
|
+
result.properties = this.normalizeProperties(result.properties, mergedRelations);
|
|
3264
|
+
if (!result.childCollections) {
|
|
3265
|
+
const capabilities = getDataSourceCapabilities(result.engine);
|
|
3266
|
+
const declaredSubcollections = getDeclaredSubcollections(result);
|
|
3267
|
+
if (capabilities.supportsSubcollections && declaredSubcollections) result.childCollections = declaredSubcollections;
|
|
3268
|
+
else if (capabilities.supportsRelations && relResult.relations) {
|
|
3269
|
+
const manyRelations = relResult.relations.filter((r) => r.cardinality === "many");
|
|
3270
|
+
if (manyRelations.length > 0) result.childCollections = () => manyRelations.map((r) => {
|
|
3271
|
+
const target = r.target();
|
|
3272
|
+
return r.overrides ? mergeDeep(target, r.overrides) : target;
|
|
3273
|
+
});
|
|
3274
|
+
}
|
|
3275
|
+
}
|
|
3276
|
+
return result;
|
|
3277
|
+
}
|
|
3278
|
+
/**
|
|
3279
|
+
* Extract Relation[] from properties that have inline relation config (i.e. `target` is set).
|
|
3280
|
+
* This allows developers to define relations directly on properties without a separate
|
|
3281
|
+
* `relations[]` entry on the collection.
|
|
3282
|
+
*/
|
|
3283
|
+
extractRelationsFromProperties(properties) {
|
|
3284
|
+
const relations = [];
|
|
3285
|
+
for (const [key, property] of Object.entries(properties)) if (property.type === "relation") {
|
|
3286
|
+
const relProp = property;
|
|
3287
|
+
const target = relProp.target ?? relProp.relation?.target;
|
|
3288
|
+
if (target) {
|
|
3289
|
+
const relationName = relProp.relationName ?? relProp.relation?.relationName ?? key;
|
|
3290
|
+
relations.push({
|
|
3291
|
+
relationName,
|
|
3292
|
+
target,
|
|
3293
|
+
cardinality: relProp.cardinality ?? relProp.relation?.cardinality ?? "one",
|
|
3294
|
+
direction: relProp.direction ?? relProp.relation?.direction ?? "owning",
|
|
3295
|
+
inverseRelationName: relProp.inverseRelationName ?? relProp.relation?.inverseRelationName,
|
|
3296
|
+
localKey: relProp.localKey ?? relProp.relation?.localKey,
|
|
3297
|
+
foreignKeyOnTarget: relProp.foreignKeyOnTarget ?? relProp.relation?.foreignKeyOnTarget,
|
|
3298
|
+
through: relProp.through ?? relProp.relation?.through,
|
|
3299
|
+
joinPath: relProp.joinPath ?? relProp.relation?.joinPath,
|
|
3300
|
+
onUpdate: relProp.onUpdate ?? relProp.relation?.onUpdate,
|
|
3301
|
+
onDelete: relProp.onDelete ?? relProp.relation?.onDelete,
|
|
3302
|
+
overrides: relProp.overrides ?? relProp.relation?.overrides
|
|
3303
|
+
});
|
|
3304
|
+
}
|
|
3305
|
+
} else if (property.type === "map" && property.properties) relations.push(...this.extractRelationsFromProperties(property.properties));
|
|
3306
|
+
return relations;
|
|
3307
|
+
}
|
|
3308
|
+
normalizeProperties(properties, relations) {
|
|
3309
|
+
const newProperties = {};
|
|
3310
|
+
for (const key in properties) newProperties[key] = this.normalizeProperty(key, properties[key], relations);
|
|
3311
|
+
return newProperties;
|
|
3312
|
+
}
|
|
3313
|
+
normalizeProperty(key, property, relations) {
|
|
3314
|
+
const newProperty = { ...property };
|
|
3315
|
+
if (newProperty.type === "map" && newProperty.properties) newProperty.properties = this.normalizeProperties(newProperty.properties, relations);
|
|
3316
|
+
else if (newProperty.type === "array") {
|
|
3317
|
+
const arrayProp = newProperty;
|
|
3318
|
+
if (arrayProp.of) if (Array.isArray(arrayProp.of)) arrayProp.of = arrayProp.of.map((p, i) => this.normalizeProperty(`${key}[${i}]`, p, relations));
|
|
3319
|
+
else arrayProp.of = this.normalizeProperty(`${key}.of`, arrayProp.of, relations);
|
|
3320
|
+
else if (arrayProp.oneOf && arrayProp.oneOf.properties) arrayProp.oneOf.properties = this.normalizeProperties(arrayProp.oneOf.properties, relations);
|
|
3321
|
+
} else if ((newProperty.type === "string" || newProperty.type === "number") && newProperty.enum) {
|
|
3322
|
+
const stringOrNumberProperty = newProperty;
|
|
3323
|
+
if (typeof stringOrNumberProperty.enum === "object" && !Array.isArray(stringOrNumberProperty.enum)) stringOrNumberProperty.enum = enumToObjectEntries(stringOrNumberProperty.enum)?.filter((value) => value && (value.id || value.id === 0) && value.label) ?? [];
|
|
3324
|
+
} else if (newProperty.type === "relation") {
|
|
3325
|
+
const relationProperty = newProperty;
|
|
3326
|
+
const name = relationProperty.relationName || key;
|
|
3327
|
+
const relation = relations.find((r) => r.relationName === name);
|
|
3328
|
+
if (relation) relationProperty.relation = relation;
|
|
3329
|
+
else console.warn(`Could not find relation for property '${key}' with relationName: ${name}`);
|
|
3330
|
+
}
|
|
3331
|
+
return newProperty;
|
|
3332
|
+
}
|
|
3333
|
+
get(path) {
|
|
3334
|
+
const bySlug = this.collectionsBySlug.get(path);
|
|
3335
|
+
if (bySlug) return bySlug;
|
|
3336
|
+
if (path.includes("-")) {
|
|
3337
|
+
const normalized = path.replace(/-/g, "_");
|
|
3338
|
+
const byNormalized = this.collectionsBySlug.get(normalized);
|
|
3339
|
+
if (byNormalized) return byNormalized;
|
|
3340
|
+
}
|
|
3341
|
+
return this.collectionsByTableName.get(path);
|
|
3342
|
+
}
|
|
3343
|
+
/**
|
|
3344
|
+
* Gets the pristine, un-normalized collection exactly as it was provided.
|
|
3345
|
+
* Useful for the AST editor so it doesn't accidentally serialize injected metadata back to disk.
|
|
3346
|
+
*/
|
|
3347
|
+
getRaw(path) {
|
|
3348
|
+
const bySlug = this.rawCollectionsBySlug.get(path);
|
|
3349
|
+
if (bySlug) return bySlug;
|
|
3350
|
+
if (path.includes("-")) {
|
|
3351
|
+
const normalized = path.replace(/-/g, "_");
|
|
3352
|
+
const byNormalized = this.rawCollectionsBySlug.get(normalized);
|
|
3353
|
+
if (byNormalized) return byNormalized;
|
|
3354
|
+
}
|
|
3355
|
+
return this.rawCollectionsByTableName.get(path);
|
|
3356
|
+
}
|
|
3357
|
+
/**
|
|
3358
|
+
* Get collection by resolving multi-segment paths through relations
|
|
3359
|
+
* e.g., "authors/70/posts" resolves to the posts collection
|
|
3360
|
+
*/
|
|
3361
|
+
getCollectionByPath(collectionPath) {
|
|
3362
|
+
if (!collectionPath.includes("/")) return this.get(collectionPath);
|
|
3363
|
+
const pathSegments = collectionPath.split("/").filter((p) => p);
|
|
3364
|
+
if (pathSegments.length < 3 || pathSegments.length % 2 === 0) throw new Error(`Invalid relation path: ${collectionPath}. Expected format: collection/id/relation or collection/id/relation/id/relation`);
|
|
3365
|
+
const rootCollectionPath = pathSegments[0];
|
|
3366
|
+
let currentCollection = this.get(rootCollectionPath);
|
|
3367
|
+
if (!currentCollection) throw new Error(`Root collection not found: ${rootCollectionPath}`);
|
|
3368
|
+
for (let i = 2; i < pathSegments.length; i += 2) {
|
|
3369
|
+
const relationKey = pathSegments[i];
|
|
3370
|
+
if (!getDataSourceCapabilities(currentCollection.engine).supportsRelations) throw new Error(`Relation path navigation requires a collection that supports relations, but '${currentCollection.slug}' uses engine '${currentCollection.engine}'`);
|
|
3371
|
+
const relation = findRelation(resolveCollectionRelations(currentCollection), relationKey);
|
|
3372
|
+
if (!relation) throw new Error(`Relation '${relationKey}' not found in collection '${currentCollection.slug}'`);
|
|
3373
|
+
const target = relation.target();
|
|
3374
|
+
const targetRelationKey = relation.relationName || target.slug;
|
|
3375
|
+
const targetSlug = relation.overrides?.slug ?? targetRelationKey;
|
|
3376
|
+
currentCollection = this.get(targetSlug) || this.normalizeCollection(target);
|
|
3377
|
+
if (i + 1 < pathSegments.length) {}
|
|
3378
|
+
}
|
|
3379
|
+
return currentCollection;
|
|
3380
|
+
}
|
|
3381
|
+
getCollections() {
|
|
3382
|
+
if (!this.cachedCollectionsList) this.cachedCollectionsList = Array.from(this.collectionsByTableName.values());
|
|
3383
|
+
return this.cachedCollectionsList;
|
|
3384
|
+
}
|
|
3385
|
+
getRawCollections() {
|
|
3386
|
+
if (!this.cachedRawCollectionsList) this.cachedRawCollectionsList = Array.from(this.rawCollectionsByTableName.values());
|
|
3387
|
+
return this.cachedRawCollectionsList;
|
|
3388
|
+
}
|
|
3389
|
+
/**
|
|
3390
|
+
* Resolves a multi-segment path like "products/123/locales" and returns
|
|
3391
|
+
* information about the collections and entity IDs along the path
|
|
3392
|
+
*/
|
|
3393
|
+
resolvePathToCollections(path) {
|
|
3394
|
+
const pathSegments = path.split("/").filter((p) => p);
|
|
3395
|
+
if (pathSegments.length === 0) throw new Error(`Invalid path: ${path}`);
|
|
3396
|
+
if (pathSegments.length % 2 !== 1) throw new Error(`Invalid collection path: ${path}. It must have an odd number of segments.`);
|
|
3397
|
+
const collections = [];
|
|
3398
|
+
const entityIds = [];
|
|
3399
|
+
let currentCollection = this.get(pathSegments[0]);
|
|
3400
|
+
if (!currentCollection) throw new Error(`Unknown collection path or slug: ${pathSegments[0]}`);
|
|
3401
|
+
collections.push(currentCollection);
|
|
3402
|
+
for (let i = 1; i < pathSegments.length; i += 2) {
|
|
3403
|
+
const entityId = pathSegments[i];
|
|
3404
|
+
entityIds.push(entityId);
|
|
3405
|
+
if (i + 1 < pathSegments.length) {
|
|
3406
|
+
const subcollectionSlug = pathSegments[i + 1];
|
|
3407
|
+
const subcollections = getSubcollections(currentCollection);
|
|
3408
|
+
if (!subcollections || subcollections.length === 0) throw new Error(`No subcollections found for ${currentCollection.slug} in path: ${path}`);
|
|
3409
|
+
const subcollection = subcollections.find((c) => c.slug === subcollectionSlug);
|
|
3410
|
+
if (!subcollection) throw new Error(`Subcollection '${subcollectionSlug}' not found in ${currentCollection.slug}`);
|
|
3411
|
+
currentCollection = this.get(subcollection.slug) || this.normalizeCollection(subcollection);
|
|
3412
|
+
collections.push(currentCollection);
|
|
3413
|
+
}
|
|
3414
|
+
}
|
|
3415
|
+
return {
|
|
3416
|
+
collections,
|
|
3417
|
+
entityIds,
|
|
3418
|
+
finalCollection: currentCollection
|
|
3419
|
+
};
|
|
3420
|
+
}
|
|
3421
|
+
};
|
|
3422
|
+
//#endregion
|
|
3423
|
+
//#region ../common/src/data/query_builder.ts
|
|
3424
|
+
var QueryBuilder = class {
|
|
3425
|
+
collection;
|
|
3426
|
+
params = { where: {} };
|
|
3427
|
+
constructor(collection) {
|
|
3428
|
+
this.collection = collection;
|
|
3429
|
+
}
|
|
3430
|
+
where(columnOrCondition, operator, value) {
|
|
3431
|
+
if (typeof columnOrCondition === "object" && columnOrCondition !== null && "type" in columnOrCondition) {
|
|
3432
|
+
this.params.logical = columnOrCondition;
|
|
3433
|
+
return this;
|
|
3434
|
+
}
|
|
3435
|
+
if (!this.params.where) this.params.where = {};
|
|
3436
|
+
const column = columnOrCondition;
|
|
3437
|
+
const condition = [operator, value];
|
|
3438
|
+
const existing = this.params.where[column];
|
|
3439
|
+
if (existing === void 0) this.params.where[column] = condition;
|
|
3440
|
+
else if (Array.isArray(existing) && existing.length > 0 && Array.isArray(existing[0])) this.params.where[column].push(condition);
|
|
3441
|
+
else {
|
|
3442
|
+
let firstCondition;
|
|
3443
|
+
if (Array.isArray(existing) && existing.length === 2 && typeof existing[0] === "string") firstCondition = existing;
|
|
3444
|
+
else firstCondition = ["==", existing];
|
|
3445
|
+
this.params.where[column] = [firstCondition, condition];
|
|
3446
|
+
}
|
|
3447
|
+
return this;
|
|
3448
|
+
}
|
|
3449
|
+
/**
|
|
3450
|
+
* Order the results by a specific column.
|
|
3451
|
+
* @example
|
|
3452
|
+
* client.collection('users').orderBy('createdAt', 'desc').find()
|
|
3453
|
+
*/
|
|
3454
|
+
orderBy(column, direction = "asc") {
|
|
3455
|
+
this.params.orderBy = [column, direction];
|
|
3456
|
+
return this;
|
|
3457
|
+
}
|
|
3458
|
+
/**
|
|
3459
|
+
* Limit the number of results returned.
|
|
3460
|
+
*/
|
|
3461
|
+
limit(count) {
|
|
3462
|
+
this.params.limit = count;
|
|
3463
|
+
return this;
|
|
3464
|
+
}
|
|
3465
|
+
/**
|
|
3466
|
+
* Skip the first N results.
|
|
3467
|
+
*/
|
|
3468
|
+
offset(count) {
|
|
3469
|
+
this.params.offset = count;
|
|
3470
|
+
return this;
|
|
3471
|
+
}
|
|
3472
|
+
/**
|
|
3473
|
+
* Set a free-text search string if supported by the backend.
|
|
3474
|
+
*/
|
|
3475
|
+
search(searchString) {
|
|
3476
|
+
this.params.searchString = searchString;
|
|
3477
|
+
return this;
|
|
3478
|
+
}
|
|
3479
|
+
/**
|
|
3480
|
+
* Include related entities in the response.
|
|
3481
|
+
* Relations will be populated with full entity data instead of just IDs.
|
|
3482
|
+
*
|
|
3483
|
+
* @param relations - Relation names to include, or "*" for all.
|
|
3484
|
+
* @example
|
|
3485
|
+
* // Include specific relations
|
|
3486
|
+
* client.data.posts.include("tags", "author").find()
|
|
3487
|
+
*
|
|
3488
|
+
* // Include all relations
|
|
3489
|
+
* client.data.posts.include("*").find()
|
|
3490
|
+
*/
|
|
3491
|
+
include(...relations) {
|
|
3492
|
+
this.params.include = relations;
|
|
3493
|
+
return this;
|
|
3494
|
+
}
|
|
3495
|
+
/**
|
|
3496
|
+
* Execute the find query and return the results.
|
|
3497
|
+
*/
|
|
3498
|
+
async find() {
|
|
3499
|
+
return this.collection.find(this.params);
|
|
3500
|
+
}
|
|
3501
|
+
/**
|
|
3502
|
+
* Listen to realtime updates matching this query.
|
|
3503
|
+
*/
|
|
3504
|
+
listen(onUpdate, onError) {
|
|
3505
|
+
if (!this.collection.listen) throw new Error("Listen is only available when RebaseClient is configured with a websocketUrl.");
|
|
3506
|
+
return this.collection.listen(this.params, onUpdate, onError);
|
|
3507
|
+
}
|
|
3508
|
+
};
|
|
3509
|
+
//#endregion
|
|
3510
|
+
//#region ../common/src/data/filter-dialect.ts
|
|
3511
|
+
/**
|
|
3512
|
+
* REST wire-format adapter for the unified filter system.
|
|
3513
|
+
*
|
|
3514
|
+
* This module is the ONLY code in the entire codebase that knows about
|
|
3515
|
+
* PostgREST-style dot-syntax strings (`eq.active`, `gt.18`, `in.(a,b)`).
|
|
3516
|
+
* Everything else speaks `FilterValues` exclusively.
|
|
3517
|
+
*
|
|
3518
|
+
* Wire-format values are always strings — the wire format carries no type
|
|
3519
|
+
* metadata, so type coercion is the responsibility of the server-side data
|
|
3520
|
+
* driver which has access to the collection schema.
|
|
3521
|
+
*
|
|
3522
|
+
* Commas inside list values are backslash-escaped (`\,`), and literal
|
|
3523
|
+
* backslashes are escaped as `\\`.
|
|
3524
|
+
*
|
|
3525
|
+
* @module
|
|
3526
|
+
*/
|
|
3527
|
+
/**
|
|
3528
|
+
* Unescape a single list item from the wire format.
|
|
3529
|
+
* `\\` → `\`, `\,` → `,`
|
|
3530
|
+
*/
|
|
3531
|
+
function unescapeListItem(value) {
|
|
3532
|
+
let result = "";
|
|
3533
|
+
for (let i = 0; i < value.length; i++) if (value[i] === "\\" && i + 1 < value.length) {
|
|
3534
|
+
result += value[i + 1];
|
|
3535
|
+
i++;
|
|
3536
|
+
} else result += value[i];
|
|
3537
|
+
return result;
|
|
3538
|
+
}
|
|
3539
|
+
/**
|
|
3540
|
+
* Split a parenthesized list string on unescaped commas.
|
|
3541
|
+
* Input is the content between `(` and `)`.
|
|
3542
|
+
*
|
|
3543
|
+
* @example
|
|
3544
|
+
* splitListItems("admin,editor") // ["admin", "editor"]
|
|
3545
|
+
* splitListItems("hello\\, world,foo") // ["hello, world", "foo"]
|
|
3546
|
+
*/
|
|
3547
|
+
function splitListItems(inner) {
|
|
3548
|
+
const items = [];
|
|
3549
|
+
let current = "";
|
|
3550
|
+
for (let i = 0; i < inner.length; i++) if (inner[i] === "\\" && i + 1 < inner.length) {
|
|
3551
|
+
current += inner[i] + inner[i + 1];
|
|
3552
|
+
i++;
|
|
3553
|
+
} else if (inner[i] === ",") {
|
|
3554
|
+
items.push(unescapeListItem(current));
|
|
3555
|
+
current = "";
|
|
3556
|
+
} else current += inner[i];
|
|
3557
|
+
items.push(unescapeListItem(current));
|
|
3558
|
+
return items;
|
|
3559
|
+
}
|
|
3560
|
+
var REST_OP_LOOKUP = REST_TO_CANONICAL;
|
|
3561
|
+
/**
|
|
3562
|
+
* Parse a single PostgREST dot-string into a `[WhereFilterOp, unknown]` tuple.
|
|
3563
|
+
*
|
|
3564
|
+
* All values are returned as strings — the wire format carries no type
|
|
3565
|
+
* metadata, so coercion is the data driver's responsibility.
|
|
3566
|
+
*
|
|
3567
|
+
* If the string doesn't match a known operator prefix, it falls back to
|
|
3568
|
+
* `["==", originalString]` (treating the whole string as an equality value).
|
|
3569
|
+
* This intentional defense handles values like `"user@host.com"` or
|
|
3570
|
+
* `"1.2.3"` that happen to contain dots.
|
|
3571
|
+
*/
|
|
3572
|
+
function deserializeSingle(raw) {
|
|
3573
|
+
const dotIndex = raw.indexOf(".");
|
|
3574
|
+
if (dotIndex === -1) return ["==", raw];
|
|
3575
|
+
const prefix = raw.substring(0, dotIndex);
|
|
3576
|
+
const rest = raw.substring(dotIndex + 1);
|
|
3577
|
+
const canonicalOp = REST_OP_LOOKUP[prefix];
|
|
3578
|
+
if (!canonicalOp) return ["==", raw];
|
|
3579
|
+
if (NULL_OPS.has(canonicalOp)) return [canonicalOp, null];
|
|
3580
|
+
if (rest.startsWith("(") && rest.endsWith(")")) return [canonicalOp, splitListItems(rest.slice(1, -1))];
|
|
3581
|
+
return [canonicalOp, rest];
|
|
3582
|
+
}
|
|
3583
|
+
/**
|
|
3584
|
+
* Convert a PostgREST-style querystring record to `FilterValues`.
|
|
3585
|
+
*
|
|
3586
|
+
* - String values are parsed as single conditions.
|
|
3587
|
+
* - String arrays (repeated query params) become multiple conditions on the same field.
|
|
3588
|
+
*
|
|
3589
|
+
* @example
|
|
3590
|
+
* deserializeFilter({ status: "eq.active" })
|
|
3591
|
+
* // → { status: ["==", "active"] }
|
|
3592
|
+
*
|
|
3593
|
+
* deserializeFilter({ age: ["gte.18", "lt.65"] })
|
|
3594
|
+
* // → { age: [[">=", "18"], ["<", "65"]] }
|
|
3595
|
+
*/
|
|
3596
|
+
function deserializeFilter(query) {
|
|
3597
|
+
const result = {};
|
|
3598
|
+
for (const [field, raw] of Object.entries(query)) {
|
|
3599
|
+
if (raw === void 0) continue;
|
|
3600
|
+
if (Array.isArray(raw) && raw.length === 2 && typeof raw[0] === "string" && toCanonicalOp(raw[0]) === raw[0]) {
|
|
3601
|
+
result[field] = raw;
|
|
3602
|
+
continue;
|
|
3603
|
+
}
|
|
3604
|
+
if (Array.isArray(raw)) {
|
|
3605
|
+
if (raw.length === 0) continue;
|
|
3606
|
+
if (Array.isArray(raw[0]) && raw[0].length === 2 && typeof raw[0][0] === "string" && toCanonicalOp(raw[0][0]) === raw[0][0]) {
|
|
3607
|
+
result[field] = raw;
|
|
3608
|
+
continue;
|
|
3609
|
+
}
|
|
3610
|
+
if (raw.length === 1) result[field] = typeof raw[0] === "string" ? deserializeSingle(raw[0]) : ["==", raw[0]];
|
|
3611
|
+
else if (typeof raw[0] === "string" && raw[0].includes(".")) result[field] = raw.map((r) => typeof r === "string" ? deserializeSingle(r) : ["==", r]);
|
|
3612
|
+
else result[field] = ["in", raw];
|
|
3613
|
+
} else if (typeof raw === "string") result[field] = deserializeSingle(raw);
|
|
3614
|
+
else result[field] = ["==", raw];
|
|
3615
|
+
}
|
|
3616
|
+
return result;
|
|
3617
|
+
}
|
|
3618
|
+
//#endregion
|
|
3619
|
+
//#region ../common/src/data/buildRebaseData.ts
|
|
3620
|
+
function createPrimaryKeyResolver(options) {
|
|
3621
|
+
const cache = /* @__PURE__ */ new Map();
|
|
3622
|
+
const warned = /* @__PURE__ */ new Set();
|
|
3623
|
+
return function primaryKeysFor(slug) {
|
|
3624
|
+
const cached = cache.get(slug);
|
|
3625
|
+
if (cached) return cached;
|
|
3626
|
+
const collection = options?.resolveCollection?.(slug);
|
|
3627
|
+
if (!collection) return [];
|
|
3628
|
+
const keys = resolvePrimaryKeys(collection);
|
|
3629
|
+
if (keys.length > 0) {
|
|
3630
|
+
cache.set(slug, keys);
|
|
3631
|
+
return keys;
|
|
3632
|
+
}
|
|
3633
|
+
if (!warned.has(slug)) {
|
|
3634
|
+
warned.add(slug);
|
|
3635
|
+
console.warn(`[rebase] Collection '${slug}' declares no primary key, so its rows have no address: detail links, caching and relations will not work for it. Mark the key property with \`isId\` in its collection config — the server logs which column to mark at boot, if its schema knows the key.`);
|
|
3636
|
+
}
|
|
3637
|
+
return keys;
|
|
3638
|
+
};
|
|
3639
|
+
}
|
|
3640
|
+
/**
|
|
3641
|
+
* Give a flat row the Entity view-model the admin renders.
|
|
3642
|
+
*
|
|
3643
|
+
* The address is *derived here* — it is not a column, and the row it came from
|
|
3644
|
+
* does not contain one. Rows carry exactly what the table has, with the types
|
|
3645
|
+
* Postgres returned; the id is this layer's invention, and this is the only
|
|
3646
|
+
* place it is minted.
|
|
3647
|
+
*
|
|
3648
|
+
* `primaryKeys` empty falls back to a literal `id` on the row: drivers other
|
|
3649
|
+
* than postgres still serve rows with one, and this keeps them working.
|
|
3650
|
+
*/
|
|
3651
|
+
function rowToEntity(row, slug, primaryKeys = []) {
|
|
3652
|
+
return {
|
|
3653
|
+
id: primaryKeys.length > 0 ? buildCompositeId(row, primaryKeys) : row.id,
|
|
3654
|
+
path: slug,
|
|
3655
|
+
values: row
|
|
3656
|
+
};
|
|
3657
|
+
}
|
|
3658
|
+
function createDriverAccessor(driver, slug, getPks = () => []) {
|
|
3659
|
+
const accessor = {
|
|
3660
|
+
async find(params) {
|
|
3661
|
+
const filter = params?.where ? deserializeFilter(params.where) : void 0;
|
|
3662
|
+
const limit = params?.limit ?? 20;
|
|
3663
|
+
const offset = params?.offset ?? 0;
|
|
3664
|
+
const fetchService = driver.restFetchService;
|
|
3665
|
+
const rows = fetchService && params?.include && params.include.length > 0 ? await fetchService.fetchCollectionForRest(slug, {
|
|
3666
|
+
filter,
|
|
3667
|
+
limit: params?.limit,
|
|
3668
|
+
offset: params?.offset,
|
|
3669
|
+
orderBy: params?.orderBy?.[0],
|
|
3670
|
+
order: params?.orderBy?.[1],
|
|
3671
|
+
searchString: params?.searchString
|
|
3672
|
+
}, params.include) : await driver.fetchCollection({
|
|
3673
|
+
path: slug,
|
|
3674
|
+
limit: params?.limit,
|
|
3675
|
+
offset: params?.offset,
|
|
3676
|
+
filter,
|
|
3677
|
+
orderBy: params?.orderBy?.[0],
|
|
3678
|
+
order: params?.orderBy?.[1],
|
|
3679
|
+
searchString: params?.searchString
|
|
3680
|
+
});
|
|
3681
|
+
let total = rows.length + offset;
|
|
3682
|
+
let hasMore = rows.length >= limit;
|
|
3683
|
+
if (driver.count) {
|
|
3684
|
+
total = await driver.count({
|
|
3685
|
+
path: slug,
|
|
3686
|
+
filter
|
|
3687
|
+
});
|
|
3688
|
+
hasMore = offset + rows.length < total;
|
|
3689
|
+
}
|
|
3690
|
+
return {
|
|
3691
|
+
data: rows.map((row) => rowToEntity(row, slug, getPks())),
|
|
3692
|
+
meta: {
|
|
3693
|
+
total,
|
|
3694
|
+
limit,
|
|
3695
|
+
offset,
|
|
3696
|
+
hasMore
|
|
3697
|
+
}
|
|
3698
|
+
};
|
|
3699
|
+
},
|
|
3700
|
+
async findById(id) {
|
|
3701
|
+
const row = await driver.fetchOne({
|
|
3702
|
+
path: slug,
|
|
3703
|
+
id
|
|
3704
|
+
});
|
|
3705
|
+
return row ? rowToEntity(row, slug, getPks()) : void 0;
|
|
3706
|
+
},
|
|
3707
|
+
async create(data, id) {
|
|
3708
|
+
return rowToEntity(await driver.save({
|
|
3709
|
+
path: slug,
|
|
3710
|
+
values: data,
|
|
3711
|
+
id,
|
|
3712
|
+
status: "new"
|
|
3713
|
+
}), slug, getPks());
|
|
3714
|
+
},
|
|
3715
|
+
createMany: driver.saveMany ? async (data, options) => {
|
|
3716
|
+
return (await driver.saveMany({
|
|
3717
|
+
path: slug,
|
|
3718
|
+
rows: data,
|
|
3719
|
+
upsert: options?.upsert
|
|
3720
|
+
})).map((row) => rowToEntity(row, slug, getPks()));
|
|
3721
|
+
} : void 0,
|
|
3722
|
+
async update(id, data) {
|
|
3723
|
+
return rowToEntity(await driver.save({
|
|
3724
|
+
path: slug,
|
|
3725
|
+
values: data,
|
|
3726
|
+
id,
|
|
3727
|
+
status: "existing"
|
|
3728
|
+
}), slug, getPks());
|
|
3729
|
+
},
|
|
3730
|
+
async delete(id) {
|
|
3731
|
+
return driver.delete({ row: {
|
|
3732
|
+
id,
|
|
3733
|
+
path: slug,
|
|
3734
|
+
values: {}
|
|
3735
|
+
} });
|
|
3736
|
+
},
|
|
3737
|
+
count: driver.count ? async (params) => {
|
|
3738
|
+
const filter = params?.where ? deserializeFilter(params.where) : void 0;
|
|
3739
|
+
return driver.count({
|
|
3740
|
+
path: slug,
|
|
3741
|
+
filter
|
|
3742
|
+
});
|
|
3743
|
+
} : void 0,
|
|
3744
|
+
listen: driver.listenCollection ? (params, onUpdate, onError) => {
|
|
3745
|
+
const limit = params?.limit ?? 20;
|
|
3746
|
+
const offset = params?.offset ?? 0;
|
|
3747
|
+
return driver.listenCollection({
|
|
3748
|
+
path: slug,
|
|
3749
|
+
limit: params?.limit,
|
|
3750
|
+
offset: params?.offset,
|
|
3751
|
+
filter: params?.where,
|
|
3752
|
+
orderBy: params?.orderBy?.[0],
|
|
3753
|
+
order: params?.orderBy?.[1],
|
|
3754
|
+
searchString: params?.searchString,
|
|
3755
|
+
onUpdate: (entities) => {
|
|
3756
|
+
onUpdate({
|
|
3757
|
+
data: entities.map((row) => rowToEntity(row, slug, getPks())),
|
|
3758
|
+
meta: {
|
|
3759
|
+
total: entities.length,
|
|
3760
|
+
limit,
|
|
3761
|
+
offset,
|
|
3762
|
+
hasMore: entities.length >= limit
|
|
3763
|
+
}
|
|
3764
|
+
});
|
|
3765
|
+
},
|
|
3766
|
+
onError
|
|
3767
|
+
});
|
|
3768
|
+
} : void 0,
|
|
3769
|
+
listenById: driver.listenOne ? (id, onUpdate, onError) => {
|
|
3770
|
+
return driver.listenOne({
|
|
3771
|
+
path: slug,
|
|
3772
|
+
id,
|
|
3773
|
+
onUpdate: (entity) => onUpdate(entity ? rowToEntity(entity, slug, getPks()) : void 0),
|
|
3774
|
+
onError
|
|
3775
|
+
});
|
|
3776
|
+
} : void 0,
|
|
3777
|
+
where(columnOrCondition, operator, value) {
|
|
3778
|
+
const builder = new QueryBuilder(accessor);
|
|
3779
|
+
if (typeof columnOrCondition === "object") return builder.where(columnOrCondition);
|
|
3780
|
+
return builder.where(columnOrCondition, operator, value);
|
|
3781
|
+
},
|
|
3782
|
+
orderBy(column, ascending) {
|
|
3783
|
+
return new QueryBuilder(accessor).orderBy(column, ascending);
|
|
3784
|
+
},
|
|
3785
|
+
limit(count) {
|
|
3786
|
+
return new QueryBuilder(accessor).limit(count);
|
|
3787
|
+
},
|
|
3788
|
+
offset(count) {
|
|
3789
|
+
return new QueryBuilder(accessor).offset(count);
|
|
3790
|
+
},
|
|
3791
|
+
search(searchString) {
|
|
3792
|
+
return new QueryBuilder(accessor).search(searchString);
|
|
3793
|
+
},
|
|
3794
|
+
include(...relations) {
|
|
3795
|
+
return new QueryBuilder(accessor).include(...relations);
|
|
3796
|
+
}
|
|
3797
|
+
};
|
|
3798
|
+
return accessor;
|
|
3799
|
+
}
|
|
3800
|
+
/**
|
|
3801
|
+
* Build a `RebaseData` object from a `DataDriver` using JavaScript Proxy.
|
|
3802
|
+
*
|
|
3803
|
+
* This is the key bridge: any property access like `data.products` returns
|
|
3804
|
+
* a `CollectionAccessor` backed by the underlying DataDriver, without
|
|
3805
|
+
* needing per-collection code generation.
|
|
3806
|
+
*
|
|
3807
|
+
* @example
|
|
3808
|
+
* const data = buildRebaseData(driver);
|
|
3809
|
+
* await data.products.create({ name: "Camera", price: 299 });
|
|
3810
|
+
* const { data: items } = await data.products.find({ where: { status: ["==", "published"] } });
|
|
3811
|
+
*/
|
|
3812
|
+
function buildRebaseData(driver, options) {
|
|
3813
|
+
const cache = /* @__PURE__ */ new Map();
|
|
3814
|
+
const primaryKeysFor = createPrimaryKeyResolver(options);
|
|
3815
|
+
function getAccessor(slug) {
|
|
3816
|
+
let accessor = cache.get(slug);
|
|
3817
|
+
if (!accessor) {
|
|
3818
|
+
accessor = createDriverAccessor(driver, slug, () => primaryKeysFor(slug));
|
|
3819
|
+
cache.set(slug, accessor);
|
|
3820
|
+
}
|
|
3821
|
+
return accessor;
|
|
3822
|
+
}
|
|
3823
|
+
return new Proxy({ collection: getAccessor }, { get(_target, prop) {
|
|
3824
|
+
if (prop === "collection") return getAccessor;
|
|
3825
|
+
if (typeof prop === "symbol") return void 0;
|
|
3826
|
+
if (prop === "then" || prop === "toJSON" || prop === "$$typeof") return void 0;
|
|
3827
|
+
return getAccessor(toSnakeCase(prop));
|
|
3828
|
+
} });
|
|
3829
|
+
}
|
|
3830
|
+
/**
|
|
3831
|
+
* Unwrap a Entity back into the flat row it was built from. `rowToEntity` keeps
|
|
3832
|
+
* the row untouched under `.values` and derives `.id` alongside it, so dropping
|
|
3833
|
+
* the wrapper is the whole operation — the address was never part of the row.
|
|
3834
|
+
*/
|
|
3835
|
+
function entityToRow(entity) {
|
|
3836
|
+
return entity.values;
|
|
3837
|
+
}
|
|
3838
|
+
/**
|
|
3839
|
+
* Fluent query builder for the flat SDK data layer. Mirrors {@link QueryBuilder}
|
|
3840
|
+
* but resolves to `FindResult<M>` (flat rows) instead of Entity-wrapped
|
|
3841
|
+
* `FindResponse<M>`.
|
|
3842
|
+
*/
|
|
3843
|
+
var SdkQueryBuilder = class {
|
|
3844
|
+
client;
|
|
3845
|
+
params = { where: {} };
|
|
3846
|
+
constructor(client) {
|
|
3847
|
+
this.client = client;
|
|
3848
|
+
}
|
|
3849
|
+
where(columnOrCondition, operator, value) {
|
|
3850
|
+
if (typeof columnOrCondition === "object" && columnOrCondition !== null && "type" in columnOrCondition) {
|
|
3851
|
+
this.params.logical = columnOrCondition;
|
|
3852
|
+
return this;
|
|
3853
|
+
}
|
|
3854
|
+
if (!this.params.where) this.params.where = {};
|
|
3855
|
+
const column = columnOrCondition;
|
|
3856
|
+
const condition = [operator, value];
|
|
3857
|
+
const existing = this.params.where[column];
|
|
3858
|
+
if (existing === void 0) this.params.where[column] = condition;
|
|
3859
|
+
else if (Array.isArray(existing) && existing.length > 0 && Array.isArray(existing[0])) this.params.where[column].push(condition);
|
|
3860
|
+
else {
|
|
3861
|
+
let firstCondition;
|
|
3862
|
+
if (Array.isArray(existing) && existing.length === 2 && typeof existing[0] === "string") firstCondition = existing;
|
|
3863
|
+
else firstCondition = ["==", existing];
|
|
3864
|
+
this.params.where[column] = [firstCondition, condition];
|
|
3865
|
+
}
|
|
3866
|
+
return this;
|
|
3867
|
+
}
|
|
3868
|
+
orderBy(column, direction = "asc") {
|
|
3869
|
+
this.params.orderBy = [column, direction];
|
|
3870
|
+
return this;
|
|
3871
|
+
}
|
|
3872
|
+
limit(count) {
|
|
3873
|
+
this.params.limit = count;
|
|
3874
|
+
return this;
|
|
3875
|
+
}
|
|
3876
|
+
offset(count) {
|
|
3877
|
+
this.params.offset = count;
|
|
3878
|
+
return this;
|
|
3879
|
+
}
|
|
3880
|
+
search(searchString) {
|
|
3881
|
+
this.params.searchString = searchString;
|
|
3882
|
+
return this;
|
|
3883
|
+
}
|
|
3884
|
+
include(...relations) {
|
|
3885
|
+
this.params.include = relations;
|
|
3886
|
+
return this;
|
|
3887
|
+
}
|
|
3888
|
+
async find() {
|
|
3889
|
+
return this.client.find(this.params);
|
|
3890
|
+
}
|
|
3891
|
+
async count() {
|
|
3892
|
+
return this.client.count ? this.client.count(this.params) : 0;
|
|
3893
|
+
}
|
|
3894
|
+
listen(onUpdate, onError) {
|
|
3895
|
+
if (!this.client.listen) throw new Error("Listen is only available when the driver supports realtime.");
|
|
3896
|
+
return this.client.listen(this.params, onUpdate, onError);
|
|
3897
|
+
}
|
|
3898
|
+
};
|
|
3899
|
+
/**
|
|
3900
|
+
* Wrap a Entity-shaped {@link CollectionAccessor} into a flat
|
|
3901
|
+
* {@link SDKCollectionClient}. Every returned record is unwrapped to a flat row
|
|
3902
|
+
* so the backend SDK is byte-for-byte the same shape as the frontend client.
|
|
3903
|
+
*/
|
|
3904
|
+
function toSdkCollectionClient(snap) {
|
|
3905
|
+
const client = {
|
|
3906
|
+
async find(params) {
|
|
3907
|
+
const res = await snap.find(params);
|
|
3908
|
+
return {
|
|
3909
|
+
data: res.data.map(entityToRow),
|
|
3910
|
+
meta: res.meta
|
|
3911
|
+
};
|
|
3912
|
+
},
|
|
3913
|
+
async findById(id) {
|
|
3914
|
+
const s = await snap.findById(id);
|
|
3915
|
+
return s ? entityToRow(s) : void 0;
|
|
3916
|
+
},
|
|
3917
|
+
async create(data, id) {
|
|
3918
|
+
return entityToRow(await snap.create(data, id));
|
|
3919
|
+
},
|
|
3920
|
+
async createMany(data, options) {
|
|
3921
|
+
if (!Array.isArray(data)) throw new TypeError("createMany expects an array of records.");
|
|
3922
|
+
if (data.length === 0) return [];
|
|
3923
|
+
if (!snap.createMany) throw new Error("Bulk writes are not supported by this collection's data source. Fall back to create() per record.");
|
|
3924
|
+
return (await snap.createMany(data, options)).map(entityToRow);
|
|
3925
|
+
},
|
|
3926
|
+
async update(id, data) {
|
|
3927
|
+
return entityToRow(await snap.update(id, data));
|
|
3928
|
+
},
|
|
3929
|
+
delete(id) {
|
|
3930
|
+
return snap.delete(id);
|
|
3931
|
+
},
|
|
3932
|
+
count: snap.count ? (params) => snap.count(params) : void 0,
|
|
3933
|
+
listen: snap.listen ? (params, onUpdate, onError) => snap.listen(params, (res) => onUpdate({
|
|
3934
|
+
data: res.data.map(entityToRow),
|
|
3935
|
+
meta: res.meta
|
|
3936
|
+
}), onError) : void 0,
|
|
3937
|
+
listenById: snap.listenById ? (id, onUpdate, onError) => snap.listenById(id, (s) => onUpdate(s ? entityToRow(s) : void 0), onError) : void 0,
|
|
3938
|
+
where(columnOrCondition, operator, value) {
|
|
3939
|
+
const builder = new SdkQueryBuilder(client);
|
|
3940
|
+
if (typeof columnOrCondition === "object") return builder.where(columnOrCondition);
|
|
3941
|
+
return builder.where(columnOrCondition, operator, value);
|
|
3942
|
+
},
|
|
3943
|
+
orderBy: (column, direction) => new SdkQueryBuilder(client).orderBy(column, direction),
|
|
3944
|
+
limit: (count) => new SdkQueryBuilder(client).limit(count),
|
|
3945
|
+
offset: (count) => new SdkQueryBuilder(client).offset(count),
|
|
3946
|
+
search: (searchString) => new SdkQueryBuilder(client).search(searchString),
|
|
3947
|
+
include: (...relations) => new SdkQueryBuilder(client).include(...relations)
|
|
3948
|
+
};
|
|
3949
|
+
return client;
|
|
3950
|
+
}
|
|
3951
|
+
/**
|
|
3952
|
+
* Wrap a Entity-shaped {@link RebaseData} into a flat {@link RebaseSdkData}.
|
|
3953
|
+
*
|
|
3954
|
+
* Every collection accessor is adapted to return flat rows. Use this to derive
|
|
3955
|
+
* the flat SDK data layer (`context.data`) from an existing Entity data layer
|
|
3956
|
+
* — e.g. the admin routes its Entity data via `useData()` and exposes the
|
|
3957
|
+
* same routing as flat `context.data` for callbacks by wrapping it here.
|
|
3958
|
+
*/
|
|
3959
|
+
function wrapAsSdkData(entityData) {
|
|
3960
|
+
const cache = /* @__PURE__ */ new Map();
|
|
3961
|
+
function getAccessor(slug) {
|
|
3962
|
+
let accessor = cache.get(slug);
|
|
3963
|
+
if (!accessor) {
|
|
3964
|
+
accessor = toSdkCollectionClient(entityData.collection(slug));
|
|
3965
|
+
cache.set(slug, accessor);
|
|
3966
|
+
}
|
|
3967
|
+
return accessor;
|
|
3968
|
+
}
|
|
3969
|
+
return new Proxy({ collection: getAccessor }, { get(_target, prop) {
|
|
3970
|
+
if (prop === "collection") return getAccessor;
|
|
3971
|
+
if (typeof prop === "symbol") return void 0;
|
|
3972
|
+
if (prop === "then" || prop === "toJSON" || prop === "$$typeof") return void 0;
|
|
3973
|
+
return getAccessor(toSnakeCase(prop));
|
|
3974
|
+
} });
|
|
3975
|
+
}
|
|
3976
|
+
/**
|
|
3977
|
+
* Build a flat {@link RebaseSdkData} from a `DataDriver`.
|
|
3978
|
+
*
|
|
3979
|
+
* This is the developer-facing SDK data layer used by backend framework
|
|
3980
|
+
* callbacks & scripts (`context.data` / `rebase.data`). It returns flat rows —
|
|
3981
|
+
* identical in shape to the frontend SDK client — so the API is symmetric
|
|
3982
|
+
* across front and back. The admin CMS uses {@link buildRebaseData} (Entity).
|
|
3983
|
+
*/
|
|
3984
|
+
function buildSdkData(driver) {
|
|
3985
|
+
return wrapAsSdkData(buildRebaseData(driver));
|
|
3986
|
+
}
|
|
3987
|
+
//#endregion
|
|
3988
|
+
//#region ../common/src/table-classification.ts
|
|
3989
|
+
/** Schemas that are always considered Rebase-internal. */
|
|
3990
|
+
var REBASE_INTERNAL_SCHEMAS = ["rebase", "auth"];
|
|
3991
|
+
/** Table-name prefixes that mark a table as Rebase-internal regardless of schema. */
|
|
3992
|
+
var REBASE_INTERNAL_PREFIXES = [
|
|
3993
|
+
"_rebase_",
|
|
3994
|
+
"_auth_",
|
|
3995
|
+
"drizzle_"
|
|
3996
|
+
];
|
|
3997
|
+
/**
|
|
3998
|
+
* Synchronously classify a table based on naming conventions.
|
|
3999
|
+
*
|
|
4000
|
+
* @param tableName - The unqualified name of the table.
|
|
4001
|
+
* @param schemaName - The schema the table belongs to (e.g. `"public"`, `"rebase"`).
|
|
4002
|
+
* @returns `"rebase-internal"` when the table belongs to a reserved schema or
|
|
4003
|
+
* carries a reserved prefix; `"user"` otherwise.
|
|
4004
|
+
*
|
|
4005
|
+
* @remarks
|
|
4006
|
+
* Junction-table detection requires an async database query and is therefore
|
|
4007
|
+
* **not** handled by this function. Use {@link detectJunctionTables} to obtain
|
|
4008
|
+
* the set of junction tables, then reclassify as needed.
|
|
4009
|
+
*/
|
|
4010
|
+
function classifyTable(tableName, schemaName) {
|
|
4011
|
+
if (REBASE_INTERNAL_SCHEMAS.includes(schemaName) || REBASE_INTERNAL_PREFIXES.some((prefix) => tableName.startsWith(prefix))) return "rebase-internal";
|
|
4012
|
+
return "user";
|
|
4013
|
+
}
|
|
4014
|
+
/** SQL query that detects junction tables in the `public` schema. */
|
|
4015
|
+
var JUNCTION_TABLES_SQL = `
|
|
4016
|
+
SELECT t.table_name
|
|
4017
|
+
FROM information_schema.tables t
|
|
4018
|
+
WHERE t.table_schema = 'public'
|
|
4019
|
+
AND t.table_type = 'BASE TABLE'
|
|
4020
|
+
AND NOT EXISTS (
|
|
4021
|
+
SELECT 1
|
|
4022
|
+
FROM information_schema.columns c
|
|
4023
|
+
WHERE c.table_schema = t.table_schema
|
|
4024
|
+
AND c.table_name = t.table_name
|
|
4025
|
+
AND c.column_name NOT IN (
|
|
4026
|
+
SELECT kcu.column_name
|
|
4027
|
+
FROM information_schema.key_column_usage kcu
|
|
4028
|
+
JOIN information_schema.table_constraints tc
|
|
4029
|
+
ON tc.constraint_name = kcu.constraint_name
|
|
4030
|
+
AND tc.table_schema = kcu.table_schema
|
|
4031
|
+
WHERE tc.constraint_type = 'FOREIGN KEY'
|
|
4032
|
+
AND kcu.table_schema = t.table_schema
|
|
4033
|
+
AND kcu.table_name = t.table_name
|
|
4034
|
+
)
|
|
4035
|
+
)
|
|
4036
|
+
`;
|
|
4037
|
+
/**
|
|
4038
|
+
* Asynchronously detect junction (link) tables in the `public` schema.
|
|
4039
|
+
*
|
|
4040
|
+
* A junction table is defined as a table where **every** column participates in
|
|
4041
|
+
* at least one foreign-key constraint.
|
|
4042
|
+
*
|
|
4043
|
+
* @param executeSql - A callback that executes a raw SQL string and returns the
|
|
4044
|
+
* resulting rows.
|
|
4045
|
+
* @returns A `Set` containing the names of all detected junction tables.
|
|
4046
|
+
*/
|
|
4047
|
+
async function detectJunctionTables(executeSql) {
|
|
4048
|
+
const rows = await executeSql(JUNCTION_TABLES_SQL);
|
|
4049
|
+
const junctionTables = /* @__PURE__ */ new Set();
|
|
4050
|
+
for (const row of rows) if (typeof row.table_name === "string") junctionTables.add(row.table_name);
|
|
4051
|
+
return junctionTables;
|
|
4052
|
+
}
|
|
4053
|
+
//#endregion
|
|
4054
|
+
export { DEFAULT_ONE_OF_TYPE as A, createRelationRefWithData as C, mergeDeep as D, getPolicyNamesForRule as E, camelCase as O, createRelationRef as S, updateDateAutoValues as T, getTableVarName as _, getJunctionCollectionConfig as a, getDeclaredPrimaryKeys as b, getEffectiveSecurityRules as c, securityRuleToConditions as d, findAnonymousGrants as f, getTableName as g, getEnumVarName as h, CollectionRegistry as i, DEFAULT_ONE_OF_VALUE as j, toSnakeCase as k, buildPropertyCallbacks as l, getColumnName as m, detectJunctionTables as n, getJunctionSecurityRules as o, findRelation as p, buildSdkData as r, resolveJunctionSpecs as s, classifyTable as t, policyToPostgres as u, resolveCollectionRelations as v, normalizeToEntityRelation as w, parseIdValues as x, buildCompositeId as y };
|
|
4055
|
+
|
|
4056
|
+
//# sourceMappingURL=src-DmsRg8MR.js.map
|