@positronic/cloudflare 0.0.71 → 0.0.72

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.
@@ -0,0 +1,712 @@
1
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
2
+ try {
3
+ var info = gen[key](arg);
4
+ var value = info.value;
5
+ } catch (error) {
6
+ reject(error);
7
+ return;
8
+ }
9
+ if (info.done) {
10
+ resolve(value);
11
+ } else {
12
+ Promise.resolve(value).then(_next, _throw);
13
+ }
14
+ }
15
+ function _async_to_generator(fn) {
16
+ return function() {
17
+ var self = this, args = arguments;
18
+ return new Promise(function(resolve, reject) {
19
+ var gen = fn.apply(self, args);
20
+ function _next(value) {
21
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
22
+ }
23
+ function _throw(err) {
24
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
25
+ }
26
+ _next(undefined);
27
+ });
28
+ };
29
+ }
30
+ function _define_property(obj, key, value) {
31
+ if (key in obj) {
32
+ Object.defineProperty(obj, key, {
33
+ value: value,
34
+ enumerable: true,
35
+ configurable: true,
36
+ writable: true
37
+ });
38
+ } else {
39
+ obj[key] = value;
40
+ }
41
+ return obj;
42
+ }
43
+ function _object_spread(target) {
44
+ for(var i = 1; i < arguments.length; i++){
45
+ var source = arguments[i] != null ? arguments[i] : {};
46
+ var ownKeys = Object.keys(source);
47
+ if (typeof Object.getOwnPropertySymbols === "function") {
48
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
49
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
50
+ }));
51
+ }
52
+ ownKeys.forEach(function(key) {
53
+ _define_property(target, key, source[key]);
54
+ });
55
+ }
56
+ return target;
57
+ }
58
+ function ownKeys(object, enumerableOnly) {
59
+ var keys = Object.keys(object);
60
+ if (Object.getOwnPropertySymbols) {
61
+ var symbols = Object.getOwnPropertySymbols(object);
62
+ if (enumerableOnly) {
63
+ symbols = symbols.filter(function(sym) {
64
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
65
+ });
66
+ }
67
+ keys.push.apply(keys, symbols);
68
+ }
69
+ return keys;
70
+ }
71
+ function _object_spread_props(target, source) {
72
+ source = source != null ? source : {};
73
+ if (Object.getOwnPropertyDescriptors) {
74
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
75
+ } else {
76
+ ownKeys(Object(source)).forEach(function(key) {
77
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
78
+ });
79
+ }
80
+ return target;
81
+ }
82
+ function _ts_generator(thisArg, body) {
83
+ var f, y, t, _ = {
84
+ label: 0,
85
+ sent: function() {
86
+ if (t[0] & 1) throw t[1];
87
+ return t[1];
88
+ },
89
+ trys: [],
90
+ ops: []
91
+ }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
92
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
93
+ return this;
94
+ }), g;
95
+ function verb(n) {
96
+ return function(v) {
97
+ return step([
98
+ n,
99
+ v
100
+ ]);
101
+ };
102
+ }
103
+ function step(op) {
104
+ if (f) throw new TypeError("Generator is already executing.");
105
+ while(g && (g = 0, op[0] && (_ = 0)), _)try {
106
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
107
+ if (y = 0, t) op = [
108
+ op[0] & 2,
109
+ t.value
110
+ ];
111
+ switch(op[0]){
112
+ case 0:
113
+ case 1:
114
+ t = op;
115
+ break;
116
+ case 4:
117
+ _.label++;
118
+ return {
119
+ value: op[1],
120
+ done: false
121
+ };
122
+ case 5:
123
+ _.label++;
124
+ y = op[1];
125
+ op = [
126
+ 0
127
+ ];
128
+ continue;
129
+ case 7:
130
+ op = _.ops.pop();
131
+ _.trys.pop();
132
+ continue;
133
+ default:
134
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
135
+ _ = 0;
136
+ continue;
137
+ }
138
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
139
+ _.label = op[1];
140
+ break;
141
+ }
142
+ if (op[0] === 6 && _.label < t[1]) {
143
+ _.label = t[1];
144
+ t = op;
145
+ break;
146
+ }
147
+ if (t && _.label < t[2]) {
148
+ _.label = t[2];
149
+ _.ops.push(op);
150
+ break;
151
+ }
152
+ if (t[2]) _.ops.pop();
153
+ _.trys.pop();
154
+ continue;
155
+ }
156
+ op = body.call(thisArg, _);
157
+ } catch (e) {
158
+ op = [
159
+ 6,
160
+ e
161
+ ];
162
+ y = 0;
163
+ } finally{
164
+ f = t = 0;
165
+ }
166
+ if (op[0] & 5) throw op[1];
167
+ return {
168
+ value: op[0] ? op[1] : void 0,
169
+ done: true
170
+ };
171
+ }
172
+ }
173
+ import { Hono } from 'hono';
174
+ var store = new Hono();
175
+ /**
176
+ * Get the userId for ownership filtering from the auth context.
177
+ * Root users get null (no filter — sees everything).
178
+ * Non-root users get their userId.
179
+ */ function scopeUserId(context) {
180
+ var auth = context.get('auth');
181
+ var _auth_userId;
182
+ return (auth === null || auth === void 0 ? void 0 : auth.isRoot) ? null : (_auth_userId = auth === null || auth === void 0 ? void 0 : auth.userId) !== null && _auth_userId !== void 0 ? _auth_userId : null;
183
+ }
184
+ /**
185
+ * Check if the authenticated user is root.
186
+ */ function isRoot(context) {
187
+ var auth = context.get('auth');
188
+ return (auth === null || auth === void 0 ? void 0 : auth.isRoot) === true;
189
+ }
190
+ /**
191
+ * Parse an R2 key from the store/ prefix into structured data.
192
+ *
193
+ * R2 key patterns:
194
+ * shared: store/{brainTitle}/{key}.json
195
+ * per-user: store/{brainTitle}/user/{userId}/{key}.json
196
+ */ function parseStoreKey(r2Key) {
197
+ // Remove "store/" prefix
198
+ var withoutPrefix = r2Key.slice('store/'.length);
199
+ // Check for per-user pattern: {brainTitle}/user/{userId}/{key}.json
200
+ var userMatch = withoutPrefix.match(/^([^/]+)\/user\/([^/]+)\/(.+)\.json$/);
201
+ if (userMatch) {
202
+ return {
203
+ brainTitle: userMatch[1],
204
+ key: userMatch[3],
205
+ scope: 'user',
206
+ userId: userMatch[2]
207
+ };
208
+ }
209
+ // Check for shared pattern: {brainTitle}/{key}.json
210
+ var sharedMatch = withoutPrefix.match(/^([^/]+)\/(.+)\.json$/);
211
+ if (sharedMatch) {
212
+ // Make sure it's not matching user/ prefix items
213
+ if (sharedMatch[2].startsWith('user/')) {
214
+ return null;
215
+ }
216
+ return {
217
+ brainTitle: sharedMatch[1],
218
+ key: sharedMatch[2],
219
+ scope: 'shared'
220
+ };
221
+ }
222
+ return null;
223
+ }
224
+ // GET /store/:brainTitle/shared/:key - Get shared key value (root only)
225
+ store.get('/:brainTitle/shared/:key', function(context) {
226
+ return _async_to_generator(function() {
227
+ var bucket, brainTitle, key, r2Key, object, value, _;
228
+ return _ts_generator(this, function(_state) {
229
+ switch(_state.label){
230
+ case 0:
231
+ if (!isRoot(context)) {
232
+ return [
233
+ 2,
234
+ context.json({
235
+ error: 'Root access required'
236
+ }, 403)
237
+ ];
238
+ }
239
+ bucket = context.env.RESOURCES_BUCKET;
240
+ brainTitle = decodeURIComponent(context.req.param('brainTitle'));
241
+ key = decodeURIComponent(context.req.param('key'));
242
+ r2Key = "store/".concat(brainTitle, "/").concat(key, ".json");
243
+ return [
244
+ 4,
245
+ bucket.get(r2Key)
246
+ ];
247
+ case 1:
248
+ object = _state.sent();
249
+ if (!object) {
250
+ return [
251
+ 2,
252
+ context.json({
253
+ error: "Key '".concat(key, "' not found")
254
+ }, 404)
255
+ ];
256
+ }
257
+ _ = JSON.parse;
258
+ return [
259
+ 4,
260
+ object.text()
261
+ ];
262
+ case 2:
263
+ value = _.apply(JSON, [
264
+ _state.sent()
265
+ ]);
266
+ return [
267
+ 2,
268
+ context.json({
269
+ key: key,
270
+ value: value,
271
+ scope: 'shared'
272
+ })
273
+ ];
274
+ }
275
+ });
276
+ })();
277
+ });
278
+ // GET /store/:brainTitle/user/:key - Get per-user key value
279
+ store.get('/:brainTitle/user/:key', function(context) {
280
+ return _async_to_generator(function() {
281
+ var userId, bucket, brainTitle, key, targetUserId, r2Key, object, value, _;
282
+ return _ts_generator(this, function(_state) {
283
+ switch(_state.label){
284
+ case 0:
285
+ userId = scopeUserId(context);
286
+ if (!userId && !isRoot(context)) {
287
+ return [
288
+ 2,
289
+ context.json({
290
+ error: 'Authentication required'
291
+ }, 401)
292
+ ];
293
+ }
294
+ bucket = context.env.RESOURCES_BUCKET;
295
+ brainTitle = decodeURIComponent(context.req.param('brainTitle'));
296
+ key = decodeURIComponent(context.req.param('key'));
297
+ // For root users, they could be looking at any user's key
298
+ // But for non-root, it's always their own
299
+ targetUserId = userId;
300
+ r2Key = "store/".concat(brainTitle, "/user/").concat(targetUserId, "/").concat(key, ".json");
301
+ return [
302
+ 4,
303
+ bucket.get(r2Key)
304
+ ];
305
+ case 1:
306
+ object = _state.sent();
307
+ if (!object) {
308
+ return [
309
+ 2,
310
+ context.json({
311
+ error: "Key '".concat(key, "' not found")
312
+ }, 404)
313
+ ];
314
+ }
315
+ _ = JSON.parse;
316
+ return [
317
+ 4,
318
+ object.text()
319
+ ];
320
+ case 2:
321
+ value = _.apply(JSON, [
322
+ _state.sent()
323
+ ]);
324
+ return [
325
+ 2,
326
+ context.json({
327
+ key: key,
328
+ value: value,
329
+ scope: 'user',
330
+ userId: targetUserId
331
+ })
332
+ ];
333
+ }
334
+ });
335
+ })();
336
+ });
337
+ // DELETE /store/:brainTitle/shared/:key - Delete shared key (root only)
338
+ store.delete('/:brainTitle/shared/:key', function(context) {
339
+ return _async_to_generator(function() {
340
+ var bucket, brainTitle, key, r2Key;
341
+ return _ts_generator(this, function(_state) {
342
+ switch(_state.label){
343
+ case 0:
344
+ if (!isRoot(context)) {
345
+ return [
346
+ 2,
347
+ context.json({
348
+ error: 'Root access required'
349
+ }, 403)
350
+ ];
351
+ }
352
+ bucket = context.env.RESOURCES_BUCKET;
353
+ brainTitle = decodeURIComponent(context.req.param('brainTitle'));
354
+ key = decodeURIComponent(context.req.param('key'));
355
+ r2Key = "store/".concat(brainTitle, "/").concat(key, ".json");
356
+ return [
357
+ 4,
358
+ bucket.delete(r2Key)
359
+ ];
360
+ case 1:
361
+ _state.sent();
362
+ return [
363
+ 2,
364
+ new Response(null, {
365
+ status: 204
366
+ })
367
+ ];
368
+ }
369
+ });
370
+ })();
371
+ });
372
+ // DELETE /store/:brainTitle/user/:key - Delete per-user key
373
+ store.delete('/:brainTitle/user/:key', function(context) {
374
+ return _async_to_generator(function() {
375
+ var userId, bucket, brainTitle, key, targetUserId, r2Key;
376
+ return _ts_generator(this, function(_state) {
377
+ switch(_state.label){
378
+ case 0:
379
+ userId = scopeUserId(context);
380
+ if (!userId && !isRoot(context)) {
381
+ return [
382
+ 2,
383
+ context.json({
384
+ error: 'Authentication required'
385
+ }, 401)
386
+ ];
387
+ }
388
+ bucket = context.env.RESOURCES_BUCKET;
389
+ brainTitle = decodeURIComponent(context.req.param('brainTitle'));
390
+ key = decodeURIComponent(context.req.param('key'));
391
+ targetUserId = userId;
392
+ r2Key = "store/".concat(brainTitle, "/user/").concat(targetUserId, "/").concat(key, ".json");
393
+ return [
394
+ 4,
395
+ bucket.delete(r2Key)
396
+ ];
397
+ case 1:
398
+ _state.sent();
399
+ return [
400
+ 2,
401
+ new Response(null, {
402
+ status: 204
403
+ })
404
+ ];
405
+ }
406
+ });
407
+ })();
408
+ });
409
+ // GET /store/:brainTitle - List keys for a brain
410
+ store.get('/:brainTitle', function(context) {
411
+ return _async_to_generator(function() {
412
+ var bucket, brainTitle, userId, rootUser, prefix, keys, cursor, listed, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, object, parsed;
413
+ return _ts_generator(this, function(_state) {
414
+ switch(_state.label){
415
+ case 0:
416
+ bucket = context.env.RESOURCES_BUCKET;
417
+ brainTitle = decodeURIComponent(context.req.param('brainTitle'));
418
+ userId = scopeUserId(context);
419
+ rootUser = isRoot(context);
420
+ prefix = "store/".concat(brainTitle, "/");
421
+ keys = [];
422
+ _state.label = 1;
423
+ case 1:
424
+ return [
425
+ 4,
426
+ bucket.list({
427
+ prefix: prefix,
428
+ cursor: cursor
429
+ })
430
+ ];
431
+ case 2:
432
+ listed = _state.sent();
433
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
434
+ try {
435
+ for(_iterator = listed.objects[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
436
+ object = _step.value;
437
+ parsed = parseStoreKey(object.key);
438
+ if (!parsed) continue;
439
+ // Access control: non-root only sees their own per-user keys
440
+ if (!rootUser) {
441
+ if (parsed.scope === 'shared') continue;
442
+ if (parsed.scope === 'user' && parsed.userId !== userId) continue;
443
+ }
444
+ keys.push(_object_spread_props(_object_spread({
445
+ key: parsed.key,
446
+ scope: parsed.scope
447
+ }, parsed.userId && {
448
+ userId: parsed.userId
449
+ }), {
450
+ size: object.size,
451
+ lastModified: object.uploaded.toISOString()
452
+ }));
453
+ }
454
+ } catch (err) {
455
+ _didIteratorError = true;
456
+ _iteratorError = err;
457
+ } finally{
458
+ try {
459
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
460
+ _iterator.return();
461
+ }
462
+ } finally{
463
+ if (_didIteratorError) {
464
+ throw _iteratorError;
465
+ }
466
+ }
467
+ }
468
+ cursor = listed.truncated ? listed.cursor : undefined;
469
+ _state.label = 3;
470
+ case 3:
471
+ if (cursor) return [
472
+ 3,
473
+ 1
474
+ ];
475
+ _state.label = 4;
476
+ case 4:
477
+ return [
478
+ 2,
479
+ context.json({
480
+ keys: keys,
481
+ count: keys.length
482
+ })
483
+ ];
484
+ }
485
+ });
486
+ })();
487
+ });
488
+ // DELETE /store/:brainTitle - Clear all accessible keys for a brain
489
+ store.delete('/:brainTitle', function(context) {
490
+ return _async_to_generator(function() {
491
+ var bucket, brainTitle, userId, rootUser, prefix, deleted, cursor, listed, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, object, parsed, err;
492
+ return _ts_generator(this, function(_state) {
493
+ switch(_state.label){
494
+ case 0:
495
+ bucket = context.env.RESOURCES_BUCKET;
496
+ brainTitle = decodeURIComponent(context.req.param('brainTitle'));
497
+ userId = scopeUserId(context);
498
+ rootUser = isRoot(context);
499
+ prefix = "store/".concat(brainTitle, "/");
500
+ deleted = 0;
501
+ _state.label = 1;
502
+ case 1:
503
+ return [
504
+ 4,
505
+ bucket.list({
506
+ prefix: prefix,
507
+ cursor: cursor
508
+ })
509
+ ];
510
+ case 2:
511
+ listed = _state.sent();
512
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
513
+ _state.label = 3;
514
+ case 3:
515
+ _state.trys.push([
516
+ 3,
517
+ 8,
518
+ 9,
519
+ 10
520
+ ]);
521
+ _iterator = listed.objects[Symbol.iterator]();
522
+ _state.label = 4;
523
+ case 4:
524
+ if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [
525
+ 3,
526
+ 7
527
+ ];
528
+ object = _step.value;
529
+ parsed = parseStoreKey(object.key);
530
+ if (!parsed) return [
531
+ 3,
532
+ 6
533
+ ];
534
+ // Access control: non-root only deletes their own per-user keys
535
+ if (!rootUser) {
536
+ if (parsed.scope === 'shared') return [
537
+ 3,
538
+ 6
539
+ ];
540
+ if (parsed.scope === 'user' && parsed.userId !== userId) return [
541
+ 3,
542
+ 6
543
+ ];
544
+ }
545
+ return [
546
+ 4,
547
+ bucket.delete(object.key)
548
+ ];
549
+ case 5:
550
+ _state.sent();
551
+ deleted++;
552
+ _state.label = 6;
553
+ case 6:
554
+ _iteratorNormalCompletion = true;
555
+ return [
556
+ 3,
557
+ 4
558
+ ];
559
+ case 7:
560
+ return [
561
+ 3,
562
+ 10
563
+ ];
564
+ case 8:
565
+ err = _state.sent();
566
+ _didIteratorError = true;
567
+ _iteratorError = err;
568
+ return [
569
+ 3,
570
+ 10
571
+ ];
572
+ case 9:
573
+ try {
574
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
575
+ _iterator.return();
576
+ }
577
+ } finally{
578
+ if (_didIteratorError) {
579
+ throw _iteratorError;
580
+ }
581
+ }
582
+ return [
583
+ 7
584
+ ];
585
+ case 10:
586
+ cursor = listed.truncated ? listed.cursor : undefined;
587
+ _state.label = 11;
588
+ case 11:
589
+ if (cursor) return [
590
+ 3,
591
+ 1
592
+ ];
593
+ _state.label = 12;
594
+ case 12:
595
+ return [
596
+ 2,
597
+ context.json({
598
+ deleted: deleted
599
+ })
600
+ ];
601
+ }
602
+ });
603
+ })();
604
+ });
605
+ // GET /store - List brains with store data
606
+ store.get('/', function(context) {
607
+ return _async_to_generator(function() {
608
+ var bucket, userId, rootUser, brainTitles, listed, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, prefix, title, cursor, listed1, _iteratorNormalCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step1, object, parsed, brains;
609
+ return _ts_generator(this, function(_state) {
610
+ switch(_state.label){
611
+ case 0:
612
+ bucket = context.env.RESOURCES_BUCKET;
613
+ userId = scopeUserId(context);
614
+ rootUser = isRoot(context);
615
+ brainTitles = new Set();
616
+ if (!rootUser) return [
617
+ 3,
618
+ 2
619
+ ];
620
+ return [
621
+ 4,
622
+ bucket.list({
623
+ prefix: 'store/',
624
+ delimiter: '/'
625
+ })
626
+ ];
627
+ case 1:
628
+ listed = _state.sent();
629
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
630
+ try {
631
+ for(_iterator = listed.delimitedPrefixes[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
632
+ prefix = _step.value;
633
+ // prefix is like "store/brain-name/"
634
+ title = prefix.slice('store/'.length, -1);
635
+ if (title) {
636
+ brainTitles.add(title);
637
+ }
638
+ }
639
+ } catch (err) {
640
+ _didIteratorError = true;
641
+ _iteratorError = err;
642
+ } finally{
643
+ try {
644
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
645
+ _iterator.return();
646
+ }
647
+ } finally{
648
+ if (_didIteratorError) {
649
+ throw _iteratorError;
650
+ }
651
+ }
652
+ }
653
+ return [
654
+ 3,
655
+ 5
656
+ ];
657
+ case 2:
658
+ return [
659
+ 4,
660
+ bucket.list({
661
+ prefix: 'store/',
662
+ cursor: cursor
663
+ })
664
+ ];
665
+ case 3:
666
+ listed1 = _state.sent();
667
+ _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined;
668
+ try {
669
+ for(_iterator1 = listed1.objects[Symbol.iterator](); !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true){
670
+ object = _step1.value;
671
+ parsed = parseStoreKey(object.key);
672
+ if (!parsed) continue;
673
+ if (parsed.scope === 'user' && parsed.userId === userId) {
674
+ brainTitles.add(parsed.brainTitle);
675
+ }
676
+ }
677
+ } catch (err) {
678
+ _didIteratorError1 = true;
679
+ _iteratorError1 = err;
680
+ } finally{
681
+ try {
682
+ if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
683
+ _iterator1.return();
684
+ }
685
+ } finally{
686
+ if (_didIteratorError1) {
687
+ throw _iteratorError1;
688
+ }
689
+ }
690
+ }
691
+ cursor = listed1.truncated ? listed1.cursor : undefined;
692
+ _state.label = 4;
693
+ case 4:
694
+ if (cursor) return [
695
+ 3,
696
+ 2
697
+ ];
698
+ _state.label = 5;
699
+ case 5:
700
+ brains = Array.from(brainTitles).sort();
701
+ return [
702
+ 2,
703
+ context.json({
704
+ brains: brains,
705
+ count: brains.length
706
+ })
707
+ ];
708
+ }
709
+ });
710
+ })();
711
+ });
712
+ export default store;