@windmill-labs/shared-utils 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib.es.js +1337 -0
  2. package/package.json +11 -0
package/lib.es.js ADDED
@@ -0,0 +1,1337 @@
1
+ var __create = Object.create, __defProp = Object.defineProperty, __getOwnPropDesc = Object.getOwnPropertyDescriptor, __getOwnPropNames = Object.getOwnPropertyNames, __getProtoOf = Object.getPrototypeOf, __hasOwnProp = Object.prototype.hasOwnProperty, __esmMin = (c, L) => () => (c && (L = c(c = 0)), L), __commonJSMin = (c, L) => () => (L || c((L = { exports: {} }).exports, L), L.exports), __export = (c) => {
2
+ let R = {};
3
+ for (var z in c) __defProp(R, z, {
4
+ get: c[z],
5
+ enumerable: !0
6
+ });
7
+ return R;
8
+ }, __copyProps = (c, B, H, U) => {
9
+ if (B && typeof B == "object" || typeof B == "function") for (var W = __getOwnPropNames(B), G = 0, K = W.length, q; G < K; G++) q = W[G], !__hasOwnProp.call(c, q) && q !== H && __defProp(c, q, {
10
+ get: ((c) => B[c]).bind(null, q),
11
+ enumerable: !(U = __getOwnPropDesc(B, q)) || U.enumerable
12
+ });
13
+ return c;
14
+ }, __toESM = (R, z, V) => (V = R == null ? {} : __create(__getProtoOf(R)), __copyProps(z || !R || !R.__esModule ? __defProp(V, "default", {
15
+ value: R,
16
+ enumerable: !0
17
+ }) : V, R)), __toCommonJS = (c) => __copyProps(__defProp({}, "__esModule", { value: !0 }), c), tslib_es6_exports = /* @__PURE__ */ __export({
18
+ __assign: () => __assign,
19
+ __asyncDelegator: () => __asyncDelegator,
20
+ __asyncGenerator: () => __asyncGenerator,
21
+ __asyncValues: () => __asyncValues,
22
+ __await: () => __await,
23
+ __awaiter: () => __awaiter,
24
+ __classPrivateFieldGet: () => __classPrivateFieldGet,
25
+ __classPrivateFieldSet: () => __classPrivateFieldSet,
26
+ __createBinding: () => __createBinding,
27
+ __decorate: () => __decorate,
28
+ __exportStar: () => __exportStar,
29
+ __extends: () => __extends,
30
+ __generator: () => __generator,
31
+ __importDefault: () => __importDefault,
32
+ __importStar: () => __importStar,
33
+ __makeTemplateObject: () => __makeTemplateObject,
34
+ __metadata: () => __metadata,
35
+ __param: () => __param,
36
+ __read: () => __read,
37
+ __rest: () => __rest,
38
+ __spread: () => __spread,
39
+ __spreadArrays: () => __spreadArrays,
40
+ __values: () => __values
41
+ });
42
+ function __extends(c, L) {
43
+ extendStatics(c, L);
44
+ function R() {
45
+ this.constructor = c;
46
+ }
47
+ c.prototype = L === null ? Object.create(L) : (R.prototype = L.prototype, new R());
48
+ }
49
+ function __rest(c, L) {
50
+ var R = {};
51
+ for (var z in c) Object.prototype.hasOwnProperty.call(c, z) && L.indexOf(z) < 0 && (R[z] = c[z]);
52
+ if (c != null && typeof Object.getOwnPropertySymbols == "function") for (var B = 0, z = Object.getOwnPropertySymbols(c); B < z.length; B++) L.indexOf(z[B]) < 0 && Object.prototype.propertyIsEnumerable.call(c, z[B]) && (R[z[B]] = c[z[B]]);
53
+ return R;
54
+ }
55
+ function __decorate(c, L, R, z) {
56
+ var B = arguments.length, V = B < 3 ? L : z === null ? z = Object.getOwnPropertyDescriptor(L, R) : z, H;
57
+ if (typeof Reflect == "object" && typeof Reflect.decorate == "function") V = Reflect.decorate(c, L, R, z);
58
+ else for (var U = c.length - 1; U >= 0; U--) (H = c[U]) && (V = (B < 3 ? H(V) : B > 3 ? H(L, R, V) : H(L, R)) || V);
59
+ return B > 3 && V && Object.defineProperty(L, R, V), V;
60
+ }
61
+ function __param(c, L) {
62
+ return function(R, z) {
63
+ L(R, z, c);
64
+ };
65
+ }
66
+ function __metadata(c, L) {
67
+ if (typeof Reflect == "object" && typeof Reflect.metadata == "function") return Reflect.metadata(c, L);
68
+ }
69
+ function __awaiter(c, L, R, z) {
70
+ function B(c) {
71
+ return c instanceof R ? c : new R(function(L) {
72
+ L(c);
73
+ });
74
+ }
75
+ return new (R ||= Promise)(function(R, V) {
76
+ function H(c) {
77
+ try {
78
+ W(z.next(c));
79
+ } catch (c) {
80
+ V(c);
81
+ }
82
+ }
83
+ function U(c) {
84
+ try {
85
+ W(z.throw(c));
86
+ } catch (c) {
87
+ V(c);
88
+ }
89
+ }
90
+ function W(c) {
91
+ c.done ? R(c.value) : B(c.value).then(H, U);
92
+ }
93
+ W((z = z.apply(c, L || [])).next());
94
+ });
95
+ }
96
+ function __generator(c, L) {
97
+ var R = {
98
+ label: 0,
99
+ sent: function() {
100
+ if (V[0] & 1) throw V[1];
101
+ return V[1];
102
+ },
103
+ trys: [],
104
+ ops: []
105
+ }, z, B, V, H;
106
+ return H = {
107
+ next: U(0),
108
+ throw: U(1),
109
+ return: U(2)
110
+ }, typeof Symbol == "function" && (H[Symbol.iterator] = function() {
111
+ return this;
112
+ }), H;
113
+ function U(c) {
114
+ return function(L) {
115
+ return W([c, L]);
116
+ };
117
+ }
118
+ function W(H) {
119
+ if (z) throw TypeError("Generator is already executing.");
120
+ for (; R;) try {
121
+ if (z = 1, B && (V = H[0] & 2 ? B.return : H[0] ? B.throw || ((V = B.return) && V.call(B), 0) : B.next) && !(V = V.call(B, H[1])).done) return V;
122
+ switch (B = 0, V && (H = [H[0] & 2, V.value]), H[0]) {
123
+ case 0:
124
+ case 1:
125
+ V = H;
126
+ break;
127
+ case 4: return R.label++, {
128
+ value: H[1],
129
+ done: !1
130
+ };
131
+ case 5:
132
+ R.label++, B = H[1], H = [0];
133
+ continue;
134
+ case 7:
135
+ H = R.ops.pop(), R.trys.pop();
136
+ continue;
137
+ default:
138
+ if ((V = R.trys, !(V = V.length > 0 && V[V.length - 1])) && (H[0] === 6 || H[0] === 2)) {
139
+ R = 0;
140
+ continue;
141
+ }
142
+ if (H[0] === 3 && (!V || H[1] > V[0] && H[1] < V[3])) {
143
+ R.label = H[1];
144
+ break;
145
+ }
146
+ if (H[0] === 6 && R.label < V[1]) {
147
+ R.label = V[1], V = H;
148
+ break;
149
+ }
150
+ if (V && R.label < V[2]) {
151
+ R.label = V[2], R.ops.push(H);
152
+ break;
153
+ }
154
+ V[2] && R.ops.pop(), R.trys.pop();
155
+ continue;
156
+ }
157
+ H = L.call(c, R);
158
+ } catch (c) {
159
+ H = [6, c], B = 0;
160
+ } finally {
161
+ z = V = 0;
162
+ }
163
+ if (H[0] & 5) throw H[1];
164
+ return {
165
+ value: H[0] ? H[1] : void 0,
166
+ done: !0
167
+ };
168
+ }
169
+ }
170
+ function __createBinding(c, L, R, z) {
171
+ z === void 0 && (z = R), c[z] = L[R];
172
+ }
173
+ function __exportStar(c, L) {
174
+ for (var R in c) R !== "default" && !L.hasOwnProperty(R) && (L[R] = c[R]);
175
+ }
176
+ function __values(c) {
177
+ var L = typeof Symbol == "function" && Symbol.iterator, R = L && c[L], z = 0;
178
+ if (R) return R.call(c);
179
+ if (c && typeof c.length == "number") return { next: function() {
180
+ return c && z >= c.length && (c = void 0), {
181
+ value: c && c[z++],
182
+ done: !c
183
+ };
184
+ } };
185
+ throw TypeError(L ? "Object is not iterable." : "Symbol.iterator is not defined.");
186
+ }
187
+ function __read(c, L) {
188
+ var R = typeof Symbol == "function" && c[Symbol.iterator];
189
+ if (!R) return c;
190
+ var z = R.call(c), B, V = [], H;
191
+ try {
192
+ for (; (L === void 0 || L-- > 0) && !(B = z.next()).done;) V.push(B.value);
193
+ } catch (c) {
194
+ H = { error: c };
195
+ } finally {
196
+ try {
197
+ B && !B.done && (R = z.return) && R.call(z);
198
+ } finally {
199
+ if (H) throw H.error;
200
+ }
201
+ }
202
+ return V;
203
+ }
204
+ function __spread() {
205
+ for (var c = [], L = 0; L < arguments.length; L++) c = c.concat(__read(arguments[L]));
206
+ return c;
207
+ }
208
+ function __spreadArrays() {
209
+ for (var c = 0, L = 0, R = arguments.length; L < R; L++) c += arguments[L].length;
210
+ for (var z = Array(c), B = 0, L = 0; L < R; L++) for (var V = arguments[L], H = 0, U = V.length; H < U; H++, B++) z[B] = V[H];
211
+ return z;
212
+ }
213
+ function __await(c) {
214
+ return this instanceof __await ? (this.v = c, this) : new __await(c);
215
+ }
216
+ function __asyncGenerator(c, L, R) {
217
+ if (!Symbol.asyncIterator) throw TypeError("Symbol.asyncIterator is not defined.");
218
+ var z = R.apply(c, L || []), B, V = [];
219
+ return B = {}, H("next"), H("throw"), H("return"), B[Symbol.asyncIterator] = function() {
220
+ return this;
221
+ }, B;
222
+ function H(c) {
223
+ z[c] && (B[c] = function(L) {
224
+ return new Promise(function(R, z) {
225
+ V.push([
226
+ c,
227
+ L,
228
+ R,
229
+ z
230
+ ]) > 1 || U(c, L);
231
+ });
232
+ });
233
+ }
234
+ function U(c, L) {
235
+ try {
236
+ W(z[c](L));
237
+ } catch (c) {
238
+ q(V[0][3], c);
239
+ }
240
+ }
241
+ function W(c) {
242
+ c.value instanceof __await ? Promise.resolve(c.value.v).then(G, K) : q(V[0][2], c);
243
+ }
244
+ function G(c) {
245
+ U("next", c);
246
+ }
247
+ function K(c) {
248
+ U("throw", c);
249
+ }
250
+ function q(c, L) {
251
+ c(L), V.shift(), V.length && U(V[0][0], V[0][1]);
252
+ }
253
+ }
254
+ function __asyncDelegator(c) {
255
+ var L, R;
256
+ return L = {}, z("next"), z("throw", function(c) {
257
+ throw c;
258
+ }), z("return"), L[Symbol.iterator] = function() {
259
+ return this;
260
+ }, L;
261
+ function z(z, B) {
262
+ L[z] = c[z] ? function(L) {
263
+ return (R = !R) ? {
264
+ value: __await(c[z](L)),
265
+ done: z === "return"
266
+ } : B ? B(L) : L;
267
+ } : B;
268
+ }
269
+ }
270
+ function __asyncValues(c) {
271
+ if (!Symbol.asyncIterator) throw TypeError("Symbol.asyncIterator is not defined.");
272
+ var L = c[Symbol.asyncIterator], R;
273
+ return L ? L.call(c) : (c = typeof __values == "function" ? __values(c) : c[Symbol.iterator](), R = {}, z("next"), z("throw"), z("return"), R[Symbol.asyncIterator] = function() {
274
+ return this;
275
+ }, R);
276
+ function z(L) {
277
+ R[L] = c[L] && function(R) {
278
+ return new Promise(function(z, V) {
279
+ R = c[L](R), B(z, V, R.done, R.value);
280
+ });
281
+ };
282
+ }
283
+ function B(c, L, R, z) {
284
+ Promise.resolve(z).then(function(L) {
285
+ c({
286
+ value: L,
287
+ done: R
288
+ });
289
+ }, L);
290
+ }
291
+ }
292
+ function __makeTemplateObject(c, L) {
293
+ return Object.defineProperty ? Object.defineProperty(c, "raw", { value: L }) : c.raw = L, c;
294
+ }
295
+ function __importStar(c) {
296
+ if (c && c.__esModule) return c;
297
+ var L = {};
298
+ if (c != null) for (var R in c) Object.hasOwnProperty.call(c, R) && (L[R] = c[R]);
299
+ return L.default = c, L;
300
+ }
301
+ function __importDefault(c) {
302
+ return c && c.__esModule ? c : { default: c };
303
+ }
304
+ function __classPrivateFieldGet(c, L) {
305
+ if (!L.has(c)) throw TypeError("attempted to get private field on non-instance");
306
+ return L.get(c);
307
+ }
308
+ function __classPrivateFieldSet(c, L, R) {
309
+ if (!L.has(c)) throw TypeError("attempted to set private field on non-instance");
310
+ return L.set(c, R), R;
311
+ }
312
+ var extendStatics, __assign, init_tslib_es6 = __esmMin((() => {
313
+ extendStatics = function(c, L) {
314
+ return extendStatics = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function(c, L) {
315
+ c.__proto__ = L;
316
+ } || function(c, L) {
317
+ for (var R in L) L.hasOwnProperty(R) && (c[R] = L[R]);
318
+ }, extendStatics(c, L);
319
+ }, __assign = function() {
320
+ return __assign = Object.assign || function(c) {
321
+ for (var L, R = 1, z = arguments.length; R < z; R++) for (var B in L = arguments[R], L) Object.prototype.hasOwnProperty.call(L, B) && (c[B] = L[B]);
322
+ return c;
323
+ }, __assign.apply(this, arguments);
324
+ };
325
+ })), require_constants = /* @__PURE__ */ __commonJSMin(((c) => {
326
+ Object.defineProperty(c, "__esModule", { value: !0 }), c.BLOCK_SIZE = 64, c.DIGEST_LENGTH = 32, c.KEY = new Uint32Array([
327
+ 1116352408,
328
+ 1899447441,
329
+ 3049323471,
330
+ 3921009573,
331
+ 961987163,
332
+ 1508970993,
333
+ 2453635748,
334
+ 2870763221,
335
+ 3624381080,
336
+ 310598401,
337
+ 607225278,
338
+ 1426881987,
339
+ 1925078388,
340
+ 2162078206,
341
+ 2614888103,
342
+ 3248222580,
343
+ 3835390401,
344
+ 4022224774,
345
+ 264347078,
346
+ 604807628,
347
+ 770255983,
348
+ 1249150122,
349
+ 1555081692,
350
+ 1996064986,
351
+ 2554220882,
352
+ 2821834349,
353
+ 2952996808,
354
+ 3210313671,
355
+ 3336571891,
356
+ 3584528711,
357
+ 113926993,
358
+ 338241895,
359
+ 666307205,
360
+ 773529912,
361
+ 1294757372,
362
+ 1396182291,
363
+ 1695183700,
364
+ 1986661051,
365
+ 2177026350,
366
+ 2456956037,
367
+ 2730485921,
368
+ 2820302411,
369
+ 3259730800,
370
+ 3345764771,
371
+ 3516065817,
372
+ 3600352804,
373
+ 4094571909,
374
+ 275423344,
375
+ 430227734,
376
+ 506948616,
377
+ 659060556,
378
+ 883997877,
379
+ 958139571,
380
+ 1322822218,
381
+ 1537002063,
382
+ 1747873779,
383
+ 1955562222,
384
+ 2024104815,
385
+ 2227730452,
386
+ 2361852424,
387
+ 2428436474,
388
+ 2756734187,
389
+ 3204031479,
390
+ 3329325298
391
+ ]), c.INIT = [
392
+ 1779033703,
393
+ 3144134277,
394
+ 1013904242,
395
+ 2773480762,
396
+ 1359893119,
397
+ 2600822924,
398
+ 528734635,
399
+ 1541459225
400
+ ], c.MAX_HASHABLE_LENGTH = 2 ** 53 - 1;
401
+ })), require_RawSha256 = /* @__PURE__ */ __commonJSMin(((c) => {
402
+ Object.defineProperty(c, "__esModule", { value: !0 });
403
+ var L = require_constants();
404
+ c.RawSha256 = function() {
405
+ function c() {
406
+ this.state = Int32Array.from(L.INIT), this.temp = new Int32Array(64), this.buffer = new Uint8Array(64), this.bufferLength = 0, this.bytesHashed = 0, this.finished = !1;
407
+ }
408
+ return c.prototype.update = function(c) {
409
+ if (this.finished) throw Error("Attempted to update an already finished hash.");
410
+ var R = 0, z = c.byteLength;
411
+ if (this.bytesHashed += z, this.bytesHashed * 8 > L.MAX_HASHABLE_LENGTH) throw Error("Cannot hash more than 2^53 - 1 bits");
412
+ for (; z > 0;) this.buffer[this.bufferLength++] = c[R++], z--, this.bufferLength === L.BLOCK_SIZE && (this.hashBuffer(), this.bufferLength = 0);
413
+ }, c.prototype.digest = function() {
414
+ if (!this.finished) {
415
+ var c = this.bytesHashed * 8, R = new DataView(this.buffer.buffer, this.buffer.byteOffset, this.buffer.byteLength), z = this.bufferLength;
416
+ if (R.setUint8(this.bufferLength++, 128), z % L.BLOCK_SIZE >= L.BLOCK_SIZE - 8) {
417
+ for (var B = this.bufferLength; B < L.BLOCK_SIZE; B++) R.setUint8(B, 0);
418
+ this.hashBuffer(), this.bufferLength = 0;
419
+ }
420
+ for (var B = this.bufferLength; B < L.BLOCK_SIZE - 8; B++) R.setUint8(B, 0);
421
+ R.setUint32(L.BLOCK_SIZE - 8, Math.floor(c / 4294967296), !0), R.setUint32(L.BLOCK_SIZE - 4, c), this.hashBuffer(), this.finished = !0;
422
+ }
423
+ for (var V = new Uint8Array(L.DIGEST_LENGTH), B = 0; B < 8; B++) V[B * 4] = this.state[B] >>> 24 & 255, V[B * 4 + 1] = this.state[B] >>> 16 & 255, V[B * 4 + 2] = this.state[B] >>> 8 & 255, V[B * 4 + 3] = this.state[B] >>> 0 & 255;
424
+ return V;
425
+ }, c.prototype.hashBuffer = function() {
426
+ for (var c = this, R = c.buffer, z = c.state, B = z[0], V = z[1], H = z[2], U = z[3], W = z[4], G = z[5], K = z[6], q = z[7], J = 0; J < L.BLOCK_SIZE; J++) {
427
+ if (J < 16) this.temp[J] = (R[J * 4] & 255) << 24 | (R[J * 4 + 1] & 255) << 16 | (R[J * 4 + 2] & 255) << 8 | R[J * 4 + 3] & 255;
428
+ else {
429
+ var Y = this.temp[J - 2], X = (Y >>> 17 | Y << 15) ^ (Y >>> 19 | Y << 13) ^ Y >>> 10;
430
+ Y = this.temp[J - 15];
431
+ var Z = (Y >>> 7 | Y << 25) ^ (Y >>> 18 | Y << 14) ^ Y >>> 3;
432
+ this.temp[J] = (X + this.temp[J - 7] | 0) + (Z + this.temp[J - 16] | 0);
433
+ }
434
+ var Q = (((W >>> 6 | W << 26) ^ (W >>> 11 | W << 21) ^ (W >>> 25 | W << 7)) + (W & G ^ ~W & K) | 0) + (q + (L.KEY[J] + this.temp[J] | 0) | 0) | 0, $ = ((B >>> 2 | B << 30) ^ (B >>> 13 | B << 19) ^ (B >>> 22 | B << 10)) + (B & V ^ B & H ^ V & H) | 0;
435
+ q = K, K = G, G = W, W = U + Q | 0, U = H, H = V, V = B, B = Q + $ | 0;
436
+ }
437
+ z[0] += B, z[1] += V, z[2] += H, z[3] += U, z[4] += W, z[5] += G, z[6] += K, z[7] += q;
438
+ }, c;
439
+ }();
440
+ })), fromUtf8$1, toUtf8, init_pureJs = __esmMin((() => {
441
+ fromUtf8$1 = (c) => {
442
+ let L = [];
443
+ for (let R = 0, z = c.length; R < z; R++) {
444
+ let z = c.charCodeAt(R);
445
+ if (z < 128) L.push(z);
446
+ else if (z < 2048) L.push(z >> 6 | 192, z & 63 | 128);
447
+ else if (R + 1 < c.length && (z & 64512) == 55296 && (c.charCodeAt(R + 1) & 64512) == 56320) {
448
+ let B = 65536 + ((z & 1023) << 10) + (c.charCodeAt(++R) & 1023);
449
+ L.push(B >> 18 | 240, B >> 12 & 63 | 128, B >> 6 & 63 | 128, B & 63 | 128);
450
+ } else L.push(z >> 12 | 224, z >> 6 & 63 | 128, z & 63 | 128);
451
+ }
452
+ return Uint8Array.from(L);
453
+ }, toUtf8 = (c) => {
454
+ let L = "";
455
+ for (let R = 0, z = c.length; R < z; R++) {
456
+ let z = c[R];
457
+ if (z < 128) L += String.fromCharCode(z);
458
+ else if (192 <= z && z < 224) {
459
+ let B = c[++R];
460
+ L += String.fromCharCode((z & 31) << 6 | B & 63);
461
+ } else if (240 <= z && z < 365) {
462
+ let B = "%" + [
463
+ z,
464
+ c[++R],
465
+ c[++R],
466
+ c[++R]
467
+ ].map((c) => c.toString(16)).join("%");
468
+ L += decodeURIComponent(B);
469
+ } else L += String.fromCharCode((z & 15) << 12 | (c[++R] & 63) << 6 | c[++R] & 63);
470
+ }
471
+ return L;
472
+ };
473
+ }));
474
+ function fromUtf8$2(c) {
475
+ return new TextEncoder().encode(c);
476
+ }
477
+ function toUtf8$1(c) {
478
+ return new TextDecoder("utf-8").decode(c);
479
+ }
480
+ var init_whatwgEncodingApi = __esmMin((() => {})), dist_es_exports = /* @__PURE__ */ __export({
481
+ fromUtf8: () => fromUtf8$3,
482
+ toUtf8: () => toUtf8$2
483
+ }), fromUtf8$3, toUtf8$2, init_dist_es = __esmMin((() => {
484
+ init_pureJs(), init_whatwgEncodingApi(), fromUtf8$3 = (c) => typeof TextEncoder == "function" ? fromUtf8$2(c) : fromUtf8$1(c), toUtf8$2 = (c) => typeof TextDecoder == "function" ? toUtf8$1(c) : toUtf8(c);
485
+ })), require_convertToBuffer = /* @__PURE__ */ __commonJSMin(((c) => {
486
+ Object.defineProperty(c, "__esModule", { value: !0 });
487
+ var L = (init_dist_es(), __toCommonJS(dist_es_exports)), R = typeof Buffer < "u" && Buffer.from ? function(c) {
488
+ return Buffer.from(c, "utf8");
489
+ } : L.fromUtf8;
490
+ function z(c) {
491
+ return c instanceof Uint8Array ? c : typeof c == "string" ? R(c) : ArrayBuffer.isView(c) ? new Uint8Array(c.buffer, c.byteOffset, c.byteLength / Uint8Array.BYTES_PER_ELEMENT) : new Uint8Array(c);
492
+ }
493
+ c.convertToBuffer = z;
494
+ })), require_isEmptyData = /* @__PURE__ */ __commonJSMin(((c) => {
495
+ Object.defineProperty(c, "__esModule", { value: !0 });
496
+ function L(c) {
497
+ return typeof c == "string" ? c.length === 0 : c.byteLength === 0;
498
+ }
499
+ c.isEmptyData = L;
500
+ })), require_numToUint8 = /* @__PURE__ */ __commonJSMin(((c) => {
501
+ Object.defineProperty(c, "__esModule", { value: !0 });
502
+ function L(c) {
503
+ return new Uint8Array([
504
+ (c & 4278190080) >> 24,
505
+ (c & 16711680) >> 16,
506
+ (c & 65280) >> 8,
507
+ c & 255
508
+ ]);
509
+ }
510
+ c.numToUint8 = L;
511
+ })), require_uint32ArrayFrom = /* @__PURE__ */ __commonJSMin(((c) => {
512
+ Object.defineProperty(c, "__esModule", { value: !0 });
513
+ function L(c) {
514
+ if (!Uint32Array.from) {
515
+ for (var L = new Uint32Array(c.length), R = 0; R < c.length;) L[R] = c[R], R += 1;
516
+ return L;
517
+ }
518
+ return Uint32Array.from(c);
519
+ }
520
+ c.uint32ArrayFrom = L;
521
+ })), require_build$1 = /* @__PURE__ */ __commonJSMin(((c) => {
522
+ Object.defineProperty(c, "__esModule", { value: !0 }), c.uint32ArrayFrom = c.numToUint8 = c.isEmptyData = c.convertToBuffer = void 0;
523
+ var L = require_convertToBuffer();
524
+ Object.defineProperty(c, "convertToBuffer", {
525
+ enumerable: !0,
526
+ get: function() {
527
+ return L.convertToBuffer;
528
+ }
529
+ });
530
+ var R = require_isEmptyData();
531
+ Object.defineProperty(c, "isEmptyData", {
532
+ enumerable: !0,
533
+ get: function() {
534
+ return R.isEmptyData;
535
+ }
536
+ });
537
+ var z = require_numToUint8();
538
+ Object.defineProperty(c, "numToUint8", {
539
+ enumerable: !0,
540
+ get: function() {
541
+ return z.numToUint8;
542
+ }
543
+ });
544
+ var B = require_uint32ArrayFrom();
545
+ Object.defineProperty(c, "uint32ArrayFrom", {
546
+ enumerable: !0,
547
+ get: function() {
548
+ return B.uint32ArrayFrom;
549
+ }
550
+ });
551
+ })), require_jsSha256 = /* @__PURE__ */ __commonJSMin(((c) => {
552
+ Object.defineProperty(c, "__esModule", { value: !0 });
553
+ var L = (init_tslib_es6(), __toCommonJS(tslib_es6_exports)), R = require_constants(), z = require_RawSha256(), B = require_build$1();
554
+ c.Sha256 = function() {
555
+ function c(c) {
556
+ this.secret = c, this.hash = new z.RawSha256(), this.reset();
557
+ }
558
+ return c.prototype.update = function(c) {
559
+ if (!((0, B.isEmptyData)(c) || this.error)) try {
560
+ this.hash.update((0, B.convertToBuffer)(c));
561
+ } catch (c) {
562
+ this.error = c;
563
+ }
564
+ }, c.prototype.digestSync = function() {
565
+ if (this.error) throw this.error;
566
+ return this.outer ? (this.outer.finished || this.outer.update(this.hash.digest()), this.outer.digest()) : this.hash.digest();
567
+ }, c.prototype.digest = function() {
568
+ return L.__awaiter(this, void 0, void 0, function() {
569
+ return L.__generator(this, function(c) {
570
+ return [2, this.digestSync()];
571
+ });
572
+ });
573
+ }, c.prototype.reset = function() {
574
+ if (this.hash = new z.RawSha256(), this.secret) {
575
+ this.outer = new z.RawSha256();
576
+ var c = V(this.secret), L = new Uint8Array(R.BLOCK_SIZE);
577
+ L.set(c);
578
+ for (var B = 0; B < R.BLOCK_SIZE; B++) c[B] ^= 54, L[B] ^= 92;
579
+ this.hash.update(c), this.outer.update(L);
580
+ for (var B = 0; B < c.byteLength; B++) c[B] = 0;
581
+ }
582
+ }, c;
583
+ }();
584
+ function V(c) {
585
+ var L = (0, B.convertToBuffer)(c);
586
+ if (L.byteLength > R.BLOCK_SIZE) {
587
+ var V = new z.RawSha256();
588
+ V.update(L), L = V.digest();
589
+ }
590
+ var H = new Uint8Array(R.BLOCK_SIZE);
591
+ return H.set(L), H;
592
+ }
593
+ })), require_build = /* @__PURE__ */ __commonJSMin(((c) => {
594
+ Object.defineProperty(c, "__esModule", { value: !0 }), (init_tslib_es6(), __toCommonJS(tslib_es6_exports)).__exportStar(require_jsSha256(), c);
595
+ }));
596
+ require_build();
597
+ function wrapDucklakeQuery(c, L) {
598
+ let R = `ATTACH 'ducklake://${L}' AS dl;USE dl;\n`;
599
+ return c.replace(/^(--.*\n)*/, (c) => c + R);
600
+ }
601
+ function getIntrospectionQuery(c) {
602
+ let L = {
603
+ descriptions: !0,
604
+ specifiedByUrl: !1,
605
+ directiveIsRepeatable: !1,
606
+ schemaDescription: !1,
607
+ inputValueDeprecation: !1,
608
+ oneOf: !1,
609
+ ...c
610
+ }, R = L.descriptions ? "description" : "", z = L.specifiedByUrl ? "specifiedByURL" : "", B = L.directiveIsRepeatable ? "isRepeatable" : "", V = L.schemaDescription ? R : "";
611
+ function H(c) {
612
+ return L.inputValueDeprecation ? c : "";
613
+ }
614
+ let U = L.oneOf ? "isOneOf" : "";
615
+ return `
616
+ query IntrospectionQuery {
617
+ __schema {
618
+ ${V}
619
+ queryType { name kind }
620
+ mutationType { name kind }
621
+ subscriptionType { name kind }
622
+ types {
623
+ ...FullType
624
+ }
625
+ directives {
626
+ name
627
+ ${R}
628
+ ${B}
629
+ locations
630
+ args${H("(includeDeprecated: true)")} {
631
+ ...InputValue
632
+ }
633
+ }
634
+ }
635
+ }
636
+
637
+ fragment FullType on __Type {
638
+ kind
639
+ name
640
+ ${R}
641
+ ${z}
642
+ ${U}
643
+ fields(includeDeprecated: true) {
644
+ name
645
+ ${R}
646
+ args${H("(includeDeprecated: true)")} {
647
+ ...InputValue
648
+ }
649
+ type {
650
+ ...TypeRef
651
+ }
652
+ isDeprecated
653
+ deprecationReason
654
+ }
655
+ inputFields${H("(includeDeprecated: true)")} {
656
+ ...InputValue
657
+ }
658
+ interfaces {
659
+ ...TypeRef
660
+ }
661
+ enumValues(includeDeprecated: true) {
662
+ name
663
+ ${R}
664
+ isDeprecated
665
+ deprecationReason
666
+ }
667
+ possibleTypes {
668
+ ...TypeRef
669
+ }
670
+ }
671
+
672
+ fragment InputValue on __InputValue {
673
+ name
674
+ ${R}
675
+ type { ...TypeRef }
676
+ defaultValue
677
+ ${H("isDeprecated")}
678
+ ${H("deprecationReason")}
679
+ }
680
+
681
+ fragment TypeRef on __Type {
682
+ kind
683
+ name
684
+ ofType {
685
+ kind
686
+ name
687
+ ofType {
688
+ kind
689
+ name
690
+ ofType {
691
+ kind
692
+ name
693
+ ofType {
694
+ kind
695
+ name
696
+ ofType {
697
+ kind
698
+ name
699
+ ofType {
700
+ kind
701
+ name
702
+ ofType {
703
+ kind
704
+ name
705
+ ofType {
706
+ kind
707
+ name
708
+ ofType {
709
+ kind
710
+ name
711
+ }
712
+ }
713
+ }
714
+ }
715
+ }
716
+ }
717
+ }
718
+ }
719
+ }
720
+ }
721
+ `;
722
+ }
723
+ let ColumnIdentity = /* @__PURE__ */ function(c) {
724
+ return c.ByDefault = "By Default", c.Always = "Always", c.No = "No", c;
725
+ }({});
726
+ getIntrospectionQuery();
727
+ function buildVisibleFieldList(c, L) {
728
+ return c.filter((c) => c && c.ignored !== !0).map((c) => {
729
+ switch (L) {
730
+ case "postgresql": return `"${c?.field}"`;
731
+ case "ms_sql_server": return `[${c?.field}]`;
732
+ case "mysql": return `\`${c?.field}\``;
733
+ case "snowflake": return `"${c?.field}"`;
734
+ case "bigquery": return `\`${c?.field}\``;
735
+ case "duckdb": return `"${c?.field}"`;
736
+ default: throw Error("Unsupported database type: " + L);
737
+ }
738
+ });
739
+ }
740
+ function getLanguageByResourceType(c) {
741
+ return {
742
+ postgresql: "postgresql",
743
+ mysql: "mysql",
744
+ ms_sql_server: "mssql",
745
+ snowflake: "snowflake",
746
+ snowflake_oauth: "snowflake",
747
+ bigquery: "bigquery",
748
+ duckdb: "duckdb"
749
+ }[c];
750
+ }
751
+ function buildParameters(c, L) {
752
+ return c.map((c, R) => {
753
+ switch (L) {
754
+ case "postgresql": return `-- $${R + 1} ${c.field}`;
755
+ case "mysql": return `-- :${c.field} (${c.datatype.split("(")[0]})`;
756
+ case "ms_sql_server": return `-- @p${R + 1} ${c.field} (${c.datatype.split("(")[0]})`;
757
+ case "snowflake": return `-- ? ${c.field} (${c.datatype.split("(")[0]})`;
758
+ case "bigquery": return `-- @${c.field} (${c.datatype.split("(")[0]})`;
759
+ case "duckdb": return `-- $${c.field} (${c.datatype.split("(")[0]})`;
760
+ }
761
+ }).join("\n");
762
+ }
763
+ function getPrimaryKeys(c) {
764
+ let L = c?.filter((c) => c.isprimarykey)?.map((c) => c.field) ?? [];
765
+ return L?.length === 0 && (L = c?.map((c) => c.field) ?? []), L ?? [];
766
+ }
767
+ function makeCountQuery(c, L, R = void 0, z) {
768
+ let B = " WHERE ", V = " AND ", H = "", U = buildParameters([{
769
+ field: "quicksearch",
770
+ datatype: c === "bigquery" ? "string" : "text"
771
+ }], c);
772
+ U += "\n", R && (H = ` ${R} AND `);
773
+ let W = buildVisibleFieldList(z, c);
774
+ switch (c) {
775
+ case "mysql":
776
+ W.length > 0 ? H += ` (:quicksearch = '' OR CONCAT_WS(' ', ${W.join(", ")}) LIKE CONCAT('%', :quicksearch, '%'))` : H += " (:quicksearch = '' OR 1 = 1)", U += `SELECT COUNT(*) as count FROM ${L}`;
777
+ break;
778
+ case "postgresql":
779
+ W.length > 0 ? H += `($1 = '' OR CONCAT(${W.join(", ")}) ILIKE '%' || $1 || '%')` : H += "($1 = '' OR 1 = 1)", U += `SELECT COUNT(*) as count FROM ${L}`;
780
+ break;
781
+ case "ms_sql_server":
782
+ W.length > 0 ? H += `(@p1 = '' OR CONCAT(${W.join(", +")}) LIKE '%' + @p1 + '%')` : H += "(@p1 = '' OR 1 = 1)", U += `SELECT COUNT(*) as count FROM [${L}]`;
783
+ break;
784
+ case "snowflake":
785
+ U = "", W.length > 0 ? (U += buildParameters([{
786
+ field: "quicksearch",
787
+ datatype: "text"
788
+ }, {
789
+ field: "quicksearch",
790
+ datatype: "text"
791
+ }], c), U += "\n", H += `(? = '' OR CONCAT(${W.join(", ")}) ILIKE '%' || ? || '%')`) : (U += buildParameters([{
792
+ field: "quicksearch",
793
+ datatype: "text"
794
+ }], c), U += "\n", H += "(? = '' OR 1 = 1)"), U += `SELECT COUNT(*) as count FROM ${L}`;
795
+ break;
796
+ case "bigquery":
797
+ if (W.length > 0) {
798
+ let c = W.map((c) => {
799
+ let L = z.find((L) => L.field === c.slice(1, -1));
800
+ return L?.datatype === "JSON" || L?.datatype.startsWith("STRUCT") || L?.datatype.startsWith("ARRAY") ? `TO_JSON_STRING(${c})` : `${c}`;
801
+ }).join(",");
802
+ H += `(@quicksearch = '' OR REGEXP_CONTAINS(CONCAT(${c}), '(?i)' || @quicksearch))`;
803
+ } else H += "(@quicksearch = '' OR 1 = 1)";
804
+ U += `SELECT COUNT(*) as count FROM \`${L}\``;
805
+ break;
806
+ case "duckdb":
807
+ W.length > 0 ? H += ` ($quicksearch = '' OR CONCAT(' ', ${W.join(", ")}) LIKE CONCAT('%', $quicksearch, '%'))` : H += " ($quicksearch = '' OR 1 = 1)", U += `SELECT COUNT(*) as count FROM ${L}`;
808
+ break;
809
+ default: throw Error("Unsupported database type:" + c);
810
+ }
811
+ return R ? U += `${B}${H}` : (U += c === "ms_sql_server" && !R ? B : V, U += H), !R && (c === "mysql" || c === "postgresql" || c === "snowflake" || c === "bigquery" || c === "duckdb") && (U = U.replace(`${V}`, B)), U;
812
+ }
813
+ function getCountInput(c, L, R, z) {
814
+ if (c.type == "ducklake" && !c.ducklake || c.type == "database" && !c.resourcePath || !L || !R?.length) return;
815
+ let B = c.type === "ducklake" ? "duckdb" : c.resourceType, V = makeCountQuery(B, L, z, R);
816
+ return c.type === "ducklake" && (V = wrapDucklakeQuery(V, c.ducklake)), {
817
+ runnable: {
818
+ name: "AppDbExplorer",
819
+ type: "runnableByName",
820
+ inlineScript: {
821
+ content: V,
822
+ language: getLanguageByResourceType(B),
823
+ schema: {
824
+ $schema: "https://json-schema.org/draft/2020-12/schema",
825
+ properties: c.type === "database" ? { database: {
826
+ description: "Database name",
827
+ type: "object",
828
+ format: `resource-${B}`
829
+ } } : {},
830
+ required: ["database"],
831
+ type: "object"
832
+ }
833
+ }
834
+ },
835
+ fields: c.type === "database" ? { database: {
836
+ type: "static",
837
+ value: `$res:${c.resourcePath}`,
838
+ fieldType: "object",
839
+ format: `resource-${B}`
840
+ } } : {},
841
+ type: "runnable",
842
+ fieldType: "object"
843
+ };
844
+ }
845
+ function makeDeleteQuery(c, L, R) {
846
+ let z = buildParameters(R === "snowflake" ? L.flatMap((c) => [c, c]) : L, R);
847
+ switch (R) {
848
+ case "postgresql": {
849
+ let R = L.map((c, L) => `($${L + 1}::text::${c.datatype} IS NULL AND ${c.field} IS NULL OR ${c.field} = $${L + 1}::text::${c.datatype})`).join("\n AND ");
850
+ return z += `\nDELETE FROM ${c} \nWHERE ${R} RETURNING 1;`, z;
851
+ }
852
+ case "mysql": {
853
+ let R = L.map((c) => `(:${c.field} IS NULL AND ${c.field} IS NULL OR ${c.field} = :${c.field})`).join("\n AND ");
854
+ return z += `\nDELETE FROM ${c} \nWHERE ${R}`, z;
855
+ }
856
+ case "ms_sql_server": {
857
+ let R = L.map((c, L) => `(@p${L + 1} IS NULL AND ${c.field} IS NULL OR ${c.field} = @p${L + 1})`).join("\n AND ");
858
+ return z += `\nDELETE FROM ${c} \nWHERE ${R}`, z;
859
+ }
860
+ case "snowflake": {
861
+ let R = L.map((c, L) => `(? = 'null' AND ${c.field} IS NULL OR ${c.field} = ?)`).join("\n AND ");
862
+ return z += `\nDELETE FROM ${c} \nWHERE ${R}`, z;
863
+ }
864
+ case "bigquery": {
865
+ let R = L.map((c, L) => `(CAST(@${c.field} AS STRING) = 'null' AND ${c.field} IS NULL OR ${c.field} = @${c.field})`).join("\n AND ");
866
+ return z += `\nDELETE FROM ${c} \nWHERE ${R}`, z;
867
+ }
868
+ case "duckdb": {
869
+ let R = L.map((c) => `($${c.field} IS NULL AND ${c.field} IS NULL OR ${c.field} = $${c.field})`).join("\n AND ");
870
+ return z += `\nDELETE FROM ${c} \nWHERE ${R}`, z;
871
+ }
872
+ default: throw Error("Unsupported database type");
873
+ }
874
+ }
875
+ function getDeleteInput(c, L, R) {
876
+ if (c.type == "ducklake" && !c.ducklake || c.type == "database" && !c.resourcePath || !L) return;
877
+ let z = c.type === "ducklake" ? "duckdb" : c.resourceType, B = makeDeleteQuery(L, R, z);
878
+ return c.type === "ducklake" && (B = wrapDucklakeQuery(B, c.ducklake)), {
879
+ runnable: {
880
+ name: "AppDbExplorer",
881
+ type: "runnableByName",
882
+ inlineScript: {
883
+ content: B,
884
+ language: getLanguageByResourceType(z),
885
+ schema: {
886
+ $schema: "https://json-schema.org/draft/2020-12/schema",
887
+ properties: {},
888
+ required: ["database"],
889
+ type: "object"
890
+ }
891
+ }
892
+ },
893
+ fields: c.type == "database" ? { database: {
894
+ type: "static",
895
+ value: `$res:${c.resourcePath}`,
896
+ fieldType: "object",
897
+ format: `resource-${z}`
898
+ } } : {},
899
+ type: "runnable",
900
+ fieldType: "object"
901
+ };
902
+ }
903
+ function formatInsertValues(c, L, R = 1) {
904
+ switch (L) {
905
+ case "mysql": return c.map((c) => `:${c.field}`).join(", ");
906
+ case "postgresql": return c.map((c, L) => `$${R + L}::${c.datatype}`).join(", ");
907
+ case "ms_sql_server": return c.map((c, L) => `@p${R + L}`).join(", ");
908
+ case "snowflake": return c.map(() => "?").join(", ");
909
+ case "bigquery": return c.map((c) => `@${c.field}`).join(", ");
910
+ case "duckdb": return c.map((c) => `$${c.field}`).join(", ");
911
+ default: throw Error("Unsupported database type");
912
+ }
913
+ }
914
+ function formatColumnNames(c) {
915
+ return c.map((c) => c.field).join(", ");
916
+ }
917
+ function getUserDefaultValue(c) {
918
+ if (c.defaultValueNull) return "NULL";
919
+ if (c.defaultUserValue) return typeof c.defaultUserValue == "string" ? `'${c.defaultUserValue}'` : c.defaultUserValue;
920
+ }
921
+ function formatDefaultValues(c) {
922
+ return c.map((c) => {
923
+ let L = getUserDefaultValue(c);
924
+ return c.overrideDefaultValue === !0 ? L : L ?? c.defaultvalue;
925
+ }).join(", ");
926
+ }
927
+ function shouldOmitColumnInInsert(c) {
928
+ if (!c.hideInsert || c.isidentity === ColumnIdentity.Always) return !0;
929
+ let L = c.defaultUserValue !== void 0 && c.defaultUserValue !== "" || c.defaultValueNull === !0, R = !!c.defaultvalue;
930
+ if (c.isnullable === "NO") {
931
+ if (!L && !R && c.isidentity === ColumnIdentity.No) throw Error(`Column ${c.field} is not nullable and has no default value`);
932
+ return !L && !R ? c.isidentity !== ColumnIdentity.No : !L && R;
933
+ } else if (c.isnullable === "YES") return !L;
934
+ return !1;
935
+ }
936
+ function makeInsertQuery(c, L, R) {
937
+ if (!c) throw Error("Table name is required");
938
+ let z = L.filter((c) => !c.hideInsert && !(R == "postgresql" && c.defaultvalue?.startsWith("nextval("))), B = L.filter((c) => !shouldOmitColumnInInsert(c)), V = z.concat(B), H = buildParameters(z, R);
939
+ H += "\n";
940
+ let U = B.length > 0, W = formatColumnNames(V), G = formatInsertValues(z, R), K = formatDefaultValues(B);
941
+ return H += `INSERT INTO ${c} (${W}) VALUES (${G}${U ? ", " : ""}${K})`, H;
942
+ }
943
+ function getInsertInput(c, L, R) {
944
+ let z = c.type === "ducklake" ? "duckdb" : c.resourceType, B = makeInsertQuery(L, R, z);
945
+ return c.type === "ducklake" && (B = wrapDucklakeQuery(B, c.ducklake)), {
946
+ runnable: {
947
+ name: "AppDbExplorer",
948
+ type: "runnableByName",
949
+ inlineScript: {
950
+ content: B,
951
+ language: getLanguageByResourceType(z),
952
+ schema: {
953
+ $schema: "https://json-schema.org/draft/2020-12/schema",
954
+ properties: {},
955
+ required: ["database"],
956
+ type: "object"
957
+ }
958
+ }
959
+ },
960
+ fields: c.type === "database" ? { database: {
961
+ type: "static",
962
+ value: `$res:${c.resourcePath}`,
963
+ fieldType: "object",
964
+ format: `resource-${z}`
965
+ } } : {},
966
+ type: "runnable",
967
+ fieldType: "object"
968
+ };
969
+ }
970
+ function makeSnowflakeSelectQuery(c, L, R, z) {
971
+ let B = coerceToNumber(z?.limit) || 100, V = coerceToNumber(z?.offset) || 0, H = [{
972
+ field: "quicksearch",
973
+ datatype: "text"
974
+ }], U = "";
975
+ U += "\n";
976
+ let W = buildVisibleFieldList(L, "snowflake"), G = W.join(", ");
977
+ U += `SELECT ${G} FROM ${c}`;
978
+ let K = ["LENGTH(?) = 0", ...W.map((c) => (H.push({
979
+ field: "quicksearch",
980
+ datatype: "text"
981
+ }), `CONCAT(${c}) ILIKE CONCAT('%', ?, '%')`))].join(" OR ");
982
+ R ? U += ` WHERE ${R} AND (${K})` : U += ` WHERE ${K}`;
983
+ let q = L.map((c) => (H.push({
984
+ field: "order_by",
985
+ datatype: "text"
986
+ }, {
987
+ field: "is_desc",
988
+ datatype: "boolean"
989
+ }, {
990
+ field: "order_by",
991
+ datatype: "text"
992
+ }, {
993
+ field: "is_desc",
994
+ datatype: "boolean"
995
+ }), `CASE WHEN ? = '${c.field}' AND ? = FALSE THEN "${c.field}" END ASC,
996
+ CASE WHEN ? = '${c.field}' AND ? = TRUE THEN "${c.field}" END DESC`));
997
+ return U += ` ORDER BY ${q.join(",\n")}`, U += ` LIMIT ${B} OFFSET ${V}`, U = buildParameters(H, "snowflake") + "\n" + U, U;
998
+ }
999
+ function makeSelectQuery(c, L, R, z, B) {
1000
+ if (!c) throw Error("Table name is required");
1001
+ let V = "", H = buildParameters([
1002
+ {
1003
+ field: "limit",
1004
+ datatype: z === "bigquery" ? "integer" : "int"
1005
+ },
1006
+ {
1007
+ field: "offset",
1008
+ datatype: z === "bigquery" ? "integer" : "int"
1009
+ },
1010
+ {
1011
+ field: "quicksearch",
1012
+ datatype: z === "bigquery" ? "string" : "text"
1013
+ },
1014
+ {
1015
+ field: "order_by",
1016
+ datatype: z === "bigquery" ? "string" : "text"
1017
+ },
1018
+ {
1019
+ field: "is_desc",
1020
+ datatype: z === "bigquery" ? "bool" : "boolean"
1021
+ }
1022
+ ], z);
1023
+ H += "\n";
1024
+ let U = buildVisibleFieldList(L, z), W = U.join(", ");
1025
+ switch (z) {
1026
+ case "mysql": {
1027
+ let z = L.map((c) => `
1028
+ CASE WHEN :order_by = '${c.field}' AND :is_desc IS false THEN \`${c.field}\` END,
1029
+ CASE WHEN :order_by = '${c.field}' AND :is_desc IS true THEN \`${c.field}\` END DESC`).join(",\n");
1030
+ V = ` (:quicksearch = '' OR CONCAT_WS(' ', ${U.join(", ")}) LIKE CONCAT('%', :quicksearch, '%'))`, H += `SELECT ${W} FROM ${c}`, H += ` WHERE ${R ? `${R} AND` : ""} ${V}`, H += ` ORDER BY ${z}`, H += " LIMIT :limit OFFSET :offset";
1031
+ break;
1032
+ }
1033
+ case "postgresql": {
1034
+ let z = `
1035
+ ${L.map((c) => `
1036
+ (CASE WHEN $4 = '${c.field}' AND $5 IS false THEN "${c.field}"::text END),
1037
+ (CASE WHEN $4 = '${c.field}' AND $5 IS true THEN "${c.field}"::text END) DESC`).join(",\n")}`;
1038
+ V = `($3 = '' OR CONCAT(${U.join(", ")}) ILIKE '%' || $3 || '%')`, H += `SELECT ${U.map((c) => `${c}::text`).join(", ")} FROM ${c}\n`, H += ` WHERE ${R ? `${R} AND` : ""} ${V}\n`, H += ` ORDER BY ${z}\n`, H += " LIMIT $1::INT OFFSET $2::INT";
1039
+ break;
1040
+ }
1041
+ case "ms_sql_server":
1042
+ let z = L.map((c) => `
1043
+ (CASE WHEN @p4 = '${c.field}' AND @p5 = 0 THEN ${c.field} END) ASC,
1044
+ (CASE WHEN @p4 = '${c.field}' AND @p5 = 1 THEN ${c.field} END) DESC`).join(",\n");
1045
+ V = ` (@p3 = '' OR CONCAT(${W}) LIKE '%' + @p3 + '%')`, H += `SELECT ${W} FROM ${c}`, H += ` WHERE ${R ? `${R} AND` : ""} ${V}`, H += ` ORDER BY ${z}`, H += " OFFSET @p2 ROWS FETCH NEXT @p1 ROWS ONLY";
1046
+ break;
1047
+ case "snowflake": return makeSnowflakeSelectQuery(c, L, R, B);
1048
+ case "bigquery": {
1049
+ let z = L.map((c) => c.datatype === "JSON" || c.datatype.startsWith("STRUCT") || c.datatype.startsWith("ARRAY") || c.datatype === "GEOGRAPHY" ? `
1050
+ (CASE WHEN @order_by = '${c.field}' AND @is_desc = false THEN TO_JSON_STRING(${c.field}) END) ASC,
1051
+ (CASE WHEN @order_by = '${c.field}' AND @is_desc = true THEN TO_JSON_STRING(${c.field}) END) DESC` : `
1052
+ (CASE WHEN @order_by = '${c.field}' AND @is_desc = false THEN ${c.field} END) ASC,
1053
+ (CASE WHEN @order_by = '${c.field}' AND @is_desc = true THEN ${c.field} END) DESC`).join(",\n");
1054
+ V = ` (@quicksearch = '' OR REGEXP_CONTAINS(CONCAT(${U.map((c) => {
1055
+ let R = L.find((L) => L.field === c.slice(1, -1));
1056
+ return R?.datatype === "JSON" || R?.datatype.startsWith("STRUCT") || R?.datatype.startsWith("ARRAY") || R?.datatype === "GEOGRAPHY" ? `TO_JSON_STRING(${c})` : `CAST(${c} AS STRING)`;
1057
+ }).join(",")}), '(?i)' || @quicksearch))`, H += `SELECT ${W} FROM ${c}`, H += ` WHERE ${R ? `${R} AND` : ""} ${V}`, H += ` ORDER BY ${z}`, H += " LIMIT @limit OFFSET @offset";
1058
+ break;
1059
+ }
1060
+ case "duckdb": {
1061
+ let z = `
1062
+ ${L.map((c) => `
1063
+ (CASE WHEN $order_by = '${c.field}' AND $is_desc IS false THEN "${c.field}"::text END),
1064
+ (CASE WHEN $order_by = '${c.field}' AND $is_desc IS true THEN "${c.field}"::text END) DESC`).join(",\n")}`;
1065
+ V = `($quicksearch = '' OR CONCAT(${U.join(", ")}) ILIKE '%' || $quicksearch || '%')`, H += `SELECT ${U.join(", ")} FROM ${c}\n`, H += ` WHERE ${R ? `${R} AND` : ""} ${V}\n`, H += ` ORDER BY ${z}\n`, H += " LIMIT $limit::INT OFFSET $offset::INT";
1066
+ break;
1067
+ }
1068
+ default: throw Error("Unsupported database type");
1069
+ }
1070
+ return H;
1071
+ }
1072
+ function coerceToNumber(c) {
1073
+ return typeof c == "number" ? c : typeof c == "string" ? parseInt(c, 10) : 0;
1074
+ }
1075
+ function getSelectInput(c, L, R, z, B) {
1076
+ if (c.type == "ducklake" && !c.ducklake || c.type == "database" && !c.resourcePath || !L || !R?.length) return;
1077
+ let V = c.type === "ducklake" ? "duckdb" : c.resourceType, H = makeSelectQuery(L, R, z, V, B);
1078
+ return c.type === "ducklake" && (H = wrapDucklakeQuery(H, c.ducklake)), {
1079
+ runnable: {
1080
+ name: "AppDbExplorer",
1081
+ type: "runnableByName",
1082
+ inlineScript: {
1083
+ content: H,
1084
+ language: getLanguageByResourceType(V)
1085
+ }
1086
+ },
1087
+ fields: c.type === "database" ? { database: {
1088
+ type: "static",
1089
+ value: `$res:${c.resourcePath}`,
1090
+ fieldType: "object",
1091
+ format: `resource-${V}`
1092
+ } } : {},
1093
+ type: "runnable",
1094
+ fieldType: "object",
1095
+ hideRefreshButton: !0
1096
+ };
1097
+ }
1098
+ function makeUpdateQuery(c, L, R, z) {
1099
+ let B = buildParameters([{
1100
+ field: "value_to_update",
1101
+ datatype: L.datatype
1102
+ }, ...z === "snowflake" ? R.flatMap((c) => [c, c]) : R], z);
1103
+ switch (B += "\n", z) {
1104
+ case "postgresql": {
1105
+ let z = R.map((c, L) => `($${L + 2}::text::${c.datatype} IS NULL AND ${c.field} IS NULL OR ${c.field} = $${L + 2}::text::${c.datatype})`).join("\n AND ");
1106
+ return B += `\nUPDATE ${c} SET ${L.field} = $1::text::${L.datatype} \nWHERE ${z} RETURNING 1`, B;
1107
+ }
1108
+ case "mysql": {
1109
+ let z = R.map((c) => `(:${c.field} IS NULL AND ${c.field} IS NULL OR ${c.field} = :${c.field})`).join("\n AND ");
1110
+ return B += `\nUPDATE ${c} SET ${L.field} = :value_to_update \nWHERE ${z}`, B;
1111
+ }
1112
+ case "ms_sql_server": {
1113
+ let z = R.map((c, L) => `(@p${L + 2} IS NULL AND ${c.field} IS NULL OR ${c.field} = @p${L + 2})`).join("\n AND ");
1114
+ return B += `\nUPDATE ${c} SET ${L.field} = @p1 \nWHERE ${z}`, B;
1115
+ }
1116
+ case "snowflake": {
1117
+ let z = R.map((c, L) => `(? = 'null' AND ${c.field} IS NULL OR ${c.field} = ?)`).join("\n AND ");
1118
+ return B += `\nUPDATE ${c} SET ${L.field} = ? \nWHERE ${z}`, B;
1119
+ }
1120
+ case "bigquery": {
1121
+ let z = R.map((c, L) => `(CAST(@${c.field} AS STRING) = 'null' AND ${c.field} IS NULL OR ${c.field} = @${c.field})`).join("\n AND ");
1122
+ return B += `\nUPDATE ${c} SET ${L.field} = @value_to_update \nWHERE ${z}`, B;
1123
+ }
1124
+ case "duckdb": {
1125
+ let z = R.map((c) => `($${c.field} IS NULL AND ${c.field} IS NULL OR ${c.field} = $${c.field})`).join("\n AND ");
1126
+ return B += `\nUPDATE ${c} SET ${L.field} = $value_to_update \nWHERE ${z}`, B;
1127
+ }
1128
+ default: throw Error("Unsupported database type");
1129
+ }
1130
+ }
1131
+ function getUpdateInput(c, L, R, z) {
1132
+ if (c.type == "ducklake" && !c.ducklake || c.type == "database" && !c.resourcePath || !L) return;
1133
+ let B = c.type === "ducklake" ? "duckdb" : c.resourceType, V = makeUpdateQuery(L, R, z, B);
1134
+ return c.type === "ducklake" && (V = wrapDucklakeQuery(V, c.ducklake)), {
1135
+ runnable: {
1136
+ name: "AppDbExplorer",
1137
+ type: "runnableByName",
1138
+ inlineScript: {
1139
+ content: V,
1140
+ language: getLanguageByResourceType(B),
1141
+ schema: {
1142
+ $schema: "https://json-schema.org/draft/2020-12/schema",
1143
+ properties: {},
1144
+ required: ["database"],
1145
+ type: "object"
1146
+ }
1147
+ }
1148
+ },
1149
+ fields: c.type === "database" ? { database: {
1150
+ type: "static",
1151
+ value: `$res:${c.resourcePath}`,
1152
+ fieldType: "object",
1153
+ format: `resource-${B}`
1154
+ } } : {},
1155
+ type: "runnable",
1156
+ fieldType: "object"
1157
+ };
1158
+ }
1159
+ function findGridItemById(c, L, R) {
1160
+ for (let z of allItems(c, L)) if (z.id === R) return z;
1161
+ }
1162
+ function allItems(c, L) {
1163
+ return L == null ? c ?? [] : [...c ?? [], ...Object.values(L).flat()];
1164
+ }
1165
+ function findGridItem(c, L) {
1166
+ return findGridItemById(c.grid, c.subgrids, L);
1167
+ }
1168
+ function collectOneOfFields(c, L) {
1169
+ return Object.fromEntries(Object.entries(c ?? {}).filter(([c, L]) => L.type == "evalv2").map(([c, R]) => {
1170
+ let z = R;
1171
+ if (!z.connections || z.connections.length !== 1) return [c, void 0];
1172
+ let B = z.connections[0], V = findGridItem(L, B.componentId);
1173
+ if (z.expr !== B.componentId + "." + B.id) return [c, void 0];
1174
+ if (V) {
1175
+ let L = V.data;
1176
+ if (L && (L.type === "resourceselectcomponent" || L.type === "selectcomponent" || L.type === "multiselectcomponent" || L.type === "multiselectcomponentv2")) {
1177
+ if ((L.type === "selectcomponent" || L.type === "multiselectcomponent" || L.type === "multiselectcomponentv2") && L.configuration?.create?.type === "static" && L.configuration?.create?.value === !0) return [c, void 0];
1178
+ if (L.configuration?.items?.type === "static") {
1179
+ let R = L.configuration.items.value;
1180
+ if (R && Array.isArray(R)) return L.type === "multiselectcomponent" || L.type === "multiselectcomponentv2" ? [c, R] : [c, R.filter((c) => c && typeof c == "object" && "value" in c && c.value).map((c) => c.value)];
1181
+ }
1182
+ }
1183
+ }
1184
+ return [c, void 0];
1185
+ }).filter(([c, L]) => L !== void 0));
1186
+ }
1187
+ function filenameExprToRegex(c) {
1188
+ return `^${c.replaceAll("${file.name}", "<file_name>").slice(1, -1).replace(/[.*+?^${}()|[\]\\]/g, "\\$&").replaceAll("<file_name>", "[^/]+")}$`;
1189
+ }
1190
+ function staticToRegex(c) {
1191
+ return `^${c.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}$`;
1192
+ }
1193
+ function checkIfExprIsString(c) {
1194
+ return /^(['"`])[^'"`]*\1$/g.test(c);
1195
+ }
1196
+ function checkIfEvalIsStringWithFilename(c) {
1197
+ return c.connections.length > 0 ? !1 : checkIfExprIsString(c.expr.replaceAll("${file.name}", ""));
1198
+ }
1199
+ function removeResourcePrefix(c) {
1200
+ return c.replace(/^\$res:/, "");
1201
+ }
1202
+ function computeWorkspaceS3FileInputPolicy() {
1203
+ return {
1204
+ allow_workspace_resource: !0,
1205
+ allowed_resources: [],
1206
+ allow_user_resources: !1,
1207
+ file_key_regex: ""
1208
+ };
1209
+ }
1210
+ function computeS3FileInputPolicy(c, L) {
1211
+ let R = c?.resource, z = c?.pathTemplate, B = !R || R.type === "static" && !R.value, V = R ? R.type === "static" ? R.value ? [removeResourcePrefix(R.value)] : [] : collectOneOfFields({ s3_resource: R }, L).s3_resource?.map((c) => removeResourcePrefix(c)) ?? [] : [], H = (R?.type === "evalv2" && R?.allowUserResources) ?? !1, U = "^.*$";
1212
+ return z && (z.type === "static" ? U = staticToRegex(z.value) : z.type === "evalv2" && checkIfEvalIsStringWithFilename(z) && (U = filenameExprToRegex(z.expr))), {
1213
+ allow_workspace_resource: B,
1214
+ allowed_resources: V,
1215
+ allow_user_resources: H,
1216
+ file_key_regex: U
1217
+ };
1218
+ }
1219
+ function isPartialS3Object(c) {
1220
+ return typeof c == "object" && !!c && typeof c.s3 == "string";
1221
+ }
1222
+ function computeS3ImageViewerPolicy(c) {
1223
+ if (c.source.type === "uploadS3" && isPartialS3Object(c.source.value)) return {
1224
+ s3_path: c.source.value.s3,
1225
+ storage: c.source.value.storage
1226
+ };
1227
+ if (c.source.type === "static" && typeof c.source.value == "string" && (c.sourceKind.type === "static" && c.sourceKind.value === "s3 (workspace storage)" || c.source.value.startsWith("s3://"))) return {
1228
+ s3_path: c.source.value.replace("s3://", ""),
1229
+ storage: void 0
1230
+ };
1231
+ }
1232
+ function collectStaticFields(c) {
1233
+ return Object.fromEntries(Object.entries(c ?? {}).filter(([c, L]) => L.type == "static").map(([c, L]) => [c, L.value]));
1234
+ }
1235
+ var import_build = /* @__PURE__ */ __toESM(require_build());
1236
+ async function updatePolicy(c, L) {
1237
+ let R = allItems(c.grid, c.subgrids), z = await Promise.all(R.flatMap((L) => {
1238
+ let R = L.data, z = [{
1239
+ input: R.componentInput,
1240
+ id: L.id
1241
+ }];
1242
+ if (R.type === "tablecomponent" && z.push(...R.actionButtons.map((c) => ({
1243
+ input: c.componentInput,
1244
+ id: c.id
1245
+ }))), (R.type === "aggridcomponent" || R.type === "aggridcomponentee" || R.type === "dbexplorercomponent" || R.type === "aggridinfinitecomponent" || R.type === "aggridinfinitecomponentee") && Array.isArray(R.actions) && z.push(...R.actions.map((c) => ({
1246
+ input: c.componentInput,
1247
+ id: c.id
1248
+ }))), R.type === "menucomponent" && z.push(...R.menuItems.map((c) => ({
1249
+ input: c.componentInput,
1250
+ id: c.id
1251
+ }))), R.type === "dbexplorercomponent") {
1252
+ let c = [], B = R.configuration, V = B?.type?.selected, H = B?.type?.configuration?.[V];
1253
+ if (H && V) {
1254
+ let { table: c, resource: U, ducklake: W } = H, G = c.value, K = U?.value.split("$res:")[1] ?? (W?.value)?.split("ducklake://")[1], q = R.configuration.columnDefs.value, J = R.configuration.whereClause.value;
1255
+ if (G && K && q) {
1256
+ let c = W ? {
1257
+ type: "ducklake",
1258
+ ducklake: K
1259
+ } : {
1260
+ type: "database",
1261
+ resourcePath: K,
1262
+ resourceType: V
1263
+ };
1264
+ z.push({
1265
+ input: getSelectInput(c, G, q, J),
1266
+ id: L.id
1267
+ }), z.push({
1268
+ input: getCountInput(c, G, q, J),
1269
+ id: L.id + "_count"
1270
+ }), z.push({
1271
+ input: getInsertInput(c, G, q),
1272
+ id: L.id + "_insert"
1273
+ });
1274
+ let R = getPrimaryKeys(q), H = q?.filter((c) => R.includes(c.field));
1275
+ z.push({
1276
+ input: getDeleteInput(c, G, H),
1277
+ id: L.id + "_delete"
1278
+ }), q.filter((c) => c.editable || B.allEditable.value).forEach((R) => {
1279
+ z.push({
1280
+ input: getUpdateInput(c, G, R, H),
1281
+ id: L.id + "_update"
1282
+ });
1283
+ });
1284
+ }
1285
+ }
1286
+ z.push(...c);
1287
+ }
1288
+ return z.filter((c) => c.input).map(async (L) => {
1289
+ if (L.input?.type == "runnable") return await processRunnable(L.id, L.input.runnable, L.input.fields, c);
1290
+ });
1291
+ }).concat(Object.values(c.hiddenInlineScripts ?? {}).map(async (L, R) => await processRunnable("bg_" + R, L, L.fields, c)))), B = Object.fromEntries(z.filter(Boolean)), V = R.filter((c) => c.data.type === "s3fileinputcomponent").map((L) => {
1292
+ let R = L.data.configuration;
1293
+ return computeS3FileInputPolicy(R?.type?.configuration?.s3, c);
1294
+ }).filter(Boolean);
1295
+ R.findIndex((c) => {
1296
+ let L = c.data;
1297
+ if (L.type === "schemaformcomponent" || L.type === "formbuttoncomponent" || L.type === "formcomponent") {
1298
+ let c = L.type === "schemaformcomponent" ? L.componentInput?.value?.properties : L.componentInput?.runnable?.type === "runnableByName" ? L.componentInput?.runnable?.inlineScript?.schema?.properties : L.componentInput?.runnable?.schema?.properties;
1299
+ return Object.values(c ?? {}).findIndex((c) => c?.type === "object" && c?.format === "resource-s3_object" || c?.type === "array" && (c?.items?.resourceType === "s3object" || c?.items?.resourceType === "s3_object")) !== -1;
1300
+ } else return !1;
1301
+ }) !== -1 && V.push(computeWorkspaceS3FileInputPolicy());
1302
+ let H = R.filter((c) => c.data.type === "imagecomponent").map((c) => {
1303
+ let L = c.data.configuration;
1304
+ return computeS3ImageViewerPolicy(L);
1305
+ }).filter(Boolean);
1306
+ return {
1307
+ ...L ?? {},
1308
+ allowed_s3_keys: H,
1309
+ s3_inputs: V,
1310
+ triggerables_v2: B
1311
+ };
1312
+ }
1313
+ async function processRunnable(c, L, R, z) {
1314
+ let B = collectStaticFields(R), V = collectOneOfFields(R, z), H = Object.entries(R).map(([c, L]) => L.allowUserResources ? c : void 0).filter(Boolean);
1315
+ if (L?.type == "runnableByName") {
1316
+ let R = await hash(L.inlineScript?.content);
1317
+ return console.log("hex", R, c), [`${c}:rawscript/${R}`, {
1318
+ static_inputs: B,
1319
+ one_of_inputs: V,
1320
+ allow_user_resources: H
1321
+ }];
1322
+ } else if (L?.type == "runnableByPath") return [`${c}:${L.runType === "hubscript" ? "script" : L.runType}/${L.path}`, {
1323
+ static_inputs: B,
1324
+ one_of_inputs: V,
1325
+ allow_user_resources: H
1326
+ }];
1327
+ }
1328
+ async function hash(c) {
1329
+ try {
1330
+ let L = new TextEncoder().encode(c), R = await crypto.subtle.digest("SHA-256", L);
1331
+ return Array.from(new Uint8Array(R)).map((c) => c.toString(16).padStart(2, "0")).join("");
1332
+ } catch {
1333
+ let L = new import_build.Sha256();
1334
+ return L.update(c ?? ""), Array.from(await L.digest()).map((c) => c.toString(16).padStart(2, "0")).join("");
1335
+ }
1336
+ }
1337
+ export { updatePolicy };
package/package.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "@windmill-labs/shared-utils",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "main": "./lib.es.js",
6
+ "module": "./lib.es.js",
7
+ "exports": {
8
+ ".": "./lib.es.js"
9
+ },
10
+ "types": "./lib.d.ts"
11
+ }