@positronic/cloudflare 0.0.52 → 0.0.53
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/src/api/brains.js +1003 -0
- package/dist/src/api/bundle.js +204 -0
- package/dist/src/api/index.js +149 -0
- package/dist/src/api/pages.js +681 -0
- package/dist/src/api/resources.js +778 -0
- package/dist/src/api/secrets.js +707 -0
- package/dist/src/api/types.js +1 -0
- package/dist/src/api/webhooks/coordination.js +267 -0
- package/dist/src/api/webhooks/index.js +277 -0
- package/dist/src/api/webhooks/system.js +248 -0
- package/dist/src/brain-runner-do.js +5 -5
- package/dist/src/dev-server.js +202 -9
- package/dist/src/index.js +2 -4
- package/dist/types/api/brains.d.ts +7 -0
- package/dist/types/api/brains.d.ts.map +1 -0
- package/dist/types/api/bundle.d.ts +7 -0
- package/dist/types/api/bundle.d.ts.map +1 -0
- package/dist/types/api/index.d.ts +8 -0
- package/dist/types/api/index.d.ts.map +1 -0
- package/dist/types/api/pages.d.ts +7 -0
- package/dist/types/api/pages.d.ts.map +1 -0
- package/dist/types/api/resources.d.ts +7 -0
- package/dist/types/api/resources.d.ts.map +1 -0
- package/dist/types/api/secrets.d.ts +7 -0
- package/dist/types/api/secrets.d.ts.map +1 -0
- package/dist/types/{api.d.ts → api/types.d.ts} +15 -9
- package/dist/types/api/types.d.ts.map +1 -0
- package/dist/types/api/webhooks/coordination.d.ts +32 -0
- package/dist/types/api/webhooks/coordination.d.ts.map +1 -0
- package/dist/types/api/webhooks/index.d.ts +7 -0
- package/dist/types/api/webhooks/index.d.ts.map +1 -0
- package/dist/types/api/webhooks/system.d.ts +7 -0
- package/dist/types/api/webhooks/system.d.ts.map +1 -0
- package/dist/types/dev-server.d.ts +5 -0
- package/dist/types/dev-server.d.ts.map +1 -1
- package/dist/types/index.d.ts +3 -5
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +4 -4
- package/dist/src/api.js +0 -2839
- package/dist/types/api.d.ts.map +0 -1
|
@@ -0,0 +1,778 @@
|
|
|
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 _instanceof(left, right) {
|
|
44
|
+
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
45
|
+
return !!right[Symbol.hasInstance](left);
|
|
46
|
+
} else {
|
|
47
|
+
return left instanceof right;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function _object_spread(target) {
|
|
51
|
+
for(var i = 1; i < arguments.length; i++){
|
|
52
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
|
53
|
+
var ownKeys = Object.keys(source);
|
|
54
|
+
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
55
|
+
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
56
|
+
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
57
|
+
}));
|
|
58
|
+
}
|
|
59
|
+
ownKeys.forEach(function(key) {
|
|
60
|
+
_define_property(target, key, source[key]);
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
return target;
|
|
64
|
+
}
|
|
65
|
+
function ownKeys(object, enumerableOnly) {
|
|
66
|
+
var keys = Object.keys(object);
|
|
67
|
+
if (Object.getOwnPropertySymbols) {
|
|
68
|
+
var symbols = Object.getOwnPropertySymbols(object);
|
|
69
|
+
if (enumerableOnly) {
|
|
70
|
+
symbols = symbols.filter(function(sym) {
|
|
71
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
keys.push.apply(keys, symbols);
|
|
75
|
+
}
|
|
76
|
+
return keys;
|
|
77
|
+
}
|
|
78
|
+
function _object_spread_props(target, source) {
|
|
79
|
+
source = source != null ? source : {};
|
|
80
|
+
if (Object.getOwnPropertyDescriptors) {
|
|
81
|
+
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
82
|
+
} else {
|
|
83
|
+
ownKeys(Object(source)).forEach(function(key) {
|
|
84
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
return target;
|
|
88
|
+
}
|
|
89
|
+
function _ts_generator(thisArg, body) {
|
|
90
|
+
var f, y, t, _ = {
|
|
91
|
+
label: 0,
|
|
92
|
+
sent: function() {
|
|
93
|
+
if (t[0] & 1) throw t[1];
|
|
94
|
+
return t[1];
|
|
95
|
+
},
|
|
96
|
+
trys: [],
|
|
97
|
+
ops: []
|
|
98
|
+
}, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
99
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
100
|
+
return this;
|
|
101
|
+
}), g;
|
|
102
|
+
function verb(n) {
|
|
103
|
+
return function(v) {
|
|
104
|
+
return step([
|
|
105
|
+
n,
|
|
106
|
+
v
|
|
107
|
+
]);
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
function step(op) {
|
|
111
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
112
|
+
while(g && (g = 0, op[0] && (_ = 0)), _)try {
|
|
113
|
+
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;
|
|
114
|
+
if (y = 0, t) op = [
|
|
115
|
+
op[0] & 2,
|
|
116
|
+
t.value
|
|
117
|
+
];
|
|
118
|
+
switch(op[0]){
|
|
119
|
+
case 0:
|
|
120
|
+
case 1:
|
|
121
|
+
t = op;
|
|
122
|
+
break;
|
|
123
|
+
case 4:
|
|
124
|
+
_.label++;
|
|
125
|
+
return {
|
|
126
|
+
value: op[1],
|
|
127
|
+
done: false
|
|
128
|
+
};
|
|
129
|
+
case 5:
|
|
130
|
+
_.label++;
|
|
131
|
+
y = op[1];
|
|
132
|
+
op = [
|
|
133
|
+
0
|
|
134
|
+
];
|
|
135
|
+
continue;
|
|
136
|
+
case 7:
|
|
137
|
+
op = _.ops.pop();
|
|
138
|
+
_.trys.pop();
|
|
139
|
+
continue;
|
|
140
|
+
default:
|
|
141
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
142
|
+
_ = 0;
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
146
|
+
_.label = op[1];
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
150
|
+
_.label = t[1];
|
|
151
|
+
t = op;
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
if (t && _.label < t[2]) {
|
|
155
|
+
_.label = t[2];
|
|
156
|
+
_.ops.push(op);
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
if (t[2]) _.ops.pop();
|
|
160
|
+
_.trys.pop();
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
op = body.call(thisArg, _);
|
|
164
|
+
} catch (e) {
|
|
165
|
+
op = [
|
|
166
|
+
6,
|
|
167
|
+
e
|
|
168
|
+
];
|
|
169
|
+
y = 0;
|
|
170
|
+
} finally{
|
|
171
|
+
f = t = 0;
|
|
172
|
+
}
|
|
173
|
+
if (op[0] & 5) throw op[1];
|
|
174
|
+
return {
|
|
175
|
+
value: op[0] ? op[1] : void 0,
|
|
176
|
+
done: true
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
import { Hono } from 'hono';
|
|
181
|
+
import { AwsClient } from 'aws4fetch';
|
|
182
|
+
import { RESOURCE_TYPES } from '@positronic/core';
|
|
183
|
+
var resources = new Hono();
|
|
184
|
+
resources.get('/', function(context) {
|
|
185
|
+
return _async_to_generator(function() {
|
|
186
|
+
var bucket, listed, resourceList, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, object, _r2Object_customMetadata, r2Object, err, error, errorMessage;
|
|
187
|
+
return _ts_generator(this, function(_state) {
|
|
188
|
+
switch(_state.label){
|
|
189
|
+
case 0:
|
|
190
|
+
bucket = context.env.RESOURCES_BUCKET;
|
|
191
|
+
_state.label = 1;
|
|
192
|
+
case 1:
|
|
193
|
+
_state.trys.push([
|
|
194
|
+
1,
|
|
195
|
+
11,
|
|
196
|
+
,
|
|
197
|
+
12
|
|
198
|
+
]);
|
|
199
|
+
return [
|
|
200
|
+
4,
|
|
201
|
+
bucket.list()
|
|
202
|
+
];
|
|
203
|
+
case 2:
|
|
204
|
+
listed = _state.sent();
|
|
205
|
+
resourceList = [];
|
|
206
|
+
_iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
207
|
+
_state.label = 3;
|
|
208
|
+
case 3:
|
|
209
|
+
_state.trys.push([
|
|
210
|
+
3,
|
|
211
|
+
8,
|
|
212
|
+
9,
|
|
213
|
+
10
|
|
214
|
+
]);
|
|
215
|
+
_iterator = listed.objects[Symbol.iterator]();
|
|
216
|
+
_state.label = 4;
|
|
217
|
+
case 4:
|
|
218
|
+
if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [
|
|
219
|
+
3,
|
|
220
|
+
7
|
|
221
|
+
];
|
|
222
|
+
object = _step.value;
|
|
223
|
+
return [
|
|
224
|
+
4,
|
|
225
|
+
bucket.head(object.key)
|
|
226
|
+
];
|
|
227
|
+
case 5:
|
|
228
|
+
r2Object = _state.sent();
|
|
229
|
+
// Skip objects without type metadata (e.g., pages or other non-resource data)
|
|
230
|
+
if (!r2Object || !((_r2Object_customMetadata = r2Object.customMetadata) === null || _r2Object_customMetadata === void 0 ? void 0 : _r2Object_customMetadata.type)) {
|
|
231
|
+
return [
|
|
232
|
+
3,
|
|
233
|
+
6
|
|
234
|
+
];
|
|
235
|
+
}
|
|
236
|
+
resourceList.push(_object_spread_props(_object_spread({
|
|
237
|
+
type: r2Object.customMetadata.type
|
|
238
|
+
}, r2Object.customMetadata.path && {
|
|
239
|
+
path: r2Object.customMetadata.path
|
|
240
|
+
}), {
|
|
241
|
+
key: object.key,
|
|
242
|
+
size: object.size,
|
|
243
|
+
lastModified: object.uploaded.toISOString(),
|
|
244
|
+
local: r2Object.customMetadata.local === 'true'
|
|
245
|
+
}));
|
|
246
|
+
_state.label = 6;
|
|
247
|
+
case 6:
|
|
248
|
+
_iteratorNormalCompletion = true;
|
|
249
|
+
return [
|
|
250
|
+
3,
|
|
251
|
+
4
|
|
252
|
+
];
|
|
253
|
+
case 7:
|
|
254
|
+
return [
|
|
255
|
+
3,
|
|
256
|
+
10
|
|
257
|
+
];
|
|
258
|
+
case 8:
|
|
259
|
+
err = _state.sent();
|
|
260
|
+
_didIteratorError = true;
|
|
261
|
+
_iteratorError = err;
|
|
262
|
+
return [
|
|
263
|
+
3,
|
|
264
|
+
10
|
|
265
|
+
];
|
|
266
|
+
case 9:
|
|
267
|
+
try {
|
|
268
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
269
|
+
_iterator.return();
|
|
270
|
+
}
|
|
271
|
+
} finally{
|
|
272
|
+
if (_didIteratorError) {
|
|
273
|
+
throw _iteratorError;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
return [
|
|
277
|
+
7
|
|
278
|
+
];
|
|
279
|
+
case 10:
|
|
280
|
+
return [
|
|
281
|
+
2,
|
|
282
|
+
context.json({
|
|
283
|
+
resources: resourceList,
|
|
284
|
+
truncated: listed.truncated,
|
|
285
|
+
count: resourceList.length
|
|
286
|
+
})
|
|
287
|
+
];
|
|
288
|
+
case 11:
|
|
289
|
+
error = _state.sent();
|
|
290
|
+
errorMessage = _instanceof(error, Error) ? error.message : 'Unknown error occurred';
|
|
291
|
+
return [
|
|
292
|
+
2,
|
|
293
|
+
context.json({
|
|
294
|
+
error: errorMessage
|
|
295
|
+
}, 500)
|
|
296
|
+
];
|
|
297
|
+
case 12:
|
|
298
|
+
return [
|
|
299
|
+
2
|
|
300
|
+
];
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
})();
|
|
304
|
+
});
|
|
305
|
+
resources.post('/', function(context) {
|
|
306
|
+
return _async_to_generator(function() {
|
|
307
|
+
var bucket, formData, file, type, path, key, local, objectKey, arrayBuffer, uploadedObject, resource, error, errorMessage;
|
|
308
|
+
return _ts_generator(this, function(_state) {
|
|
309
|
+
switch(_state.label){
|
|
310
|
+
case 0:
|
|
311
|
+
bucket = context.env.RESOURCES_BUCKET;
|
|
312
|
+
return [
|
|
313
|
+
4,
|
|
314
|
+
context.req.formData()
|
|
315
|
+
];
|
|
316
|
+
case 1:
|
|
317
|
+
formData = _state.sent();
|
|
318
|
+
file = formData.get('file');
|
|
319
|
+
type = formData.get('type');
|
|
320
|
+
path = formData.get('path');
|
|
321
|
+
key = formData.get('key');
|
|
322
|
+
local = formData.get('local');
|
|
323
|
+
if (!file) {
|
|
324
|
+
return [
|
|
325
|
+
2,
|
|
326
|
+
context.json({
|
|
327
|
+
error: 'Missing required field "file"'
|
|
328
|
+
}, 400)
|
|
329
|
+
];
|
|
330
|
+
}
|
|
331
|
+
if (!type) {
|
|
332
|
+
return [
|
|
333
|
+
2,
|
|
334
|
+
context.json({
|
|
335
|
+
error: 'Missing required field "type"'
|
|
336
|
+
}, 400)
|
|
337
|
+
];
|
|
338
|
+
}
|
|
339
|
+
if (!RESOURCE_TYPES.includes(type)) {
|
|
340
|
+
return [
|
|
341
|
+
2,
|
|
342
|
+
context.json({
|
|
343
|
+
error: 'Field "type" must be one of: '.concat(RESOURCE_TYPES.join(', '))
|
|
344
|
+
}, 400)
|
|
345
|
+
];
|
|
346
|
+
}
|
|
347
|
+
// Either key or path must be provided
|
|
348
|
+
if (!key && !path) {
|
|
349
|
+
return [
|
|
350
|
+
2,
|
|
351
|
+
context.json({
|
|
352
|
+
error: 'Either "key" or "path" must be provided'
|
|
353
|
+
}, 400)
|
|
354
|
+
];
|
|
355
|
+
}
|
|
356
|
+
// Use key if provided, otherwise use path
|
|
357
|
+
objectKey = key || path;
|
|
358
|
+
_state.label = 2;
|
|
359
|
+
case 2:
|
|
360
|
+
_state.trys.push([
|
|
361
|
+
2,
|
|
362
|
+
5,
|
|
363
|
+
,
|
|
364
|
+
6
|
|
365
|
+
]);
|
|
366
|
+
return [
|
|
367
|
+
4,
|
|
368
|
+
file.arrayBuffer()
|
|
369
|
+
];
|
|
370
|
+
case 3:
|
|
371
|
+
arrayBuffer = _state.sent();
|
|
372
|
+
return [
|
|
373
|
+
4,
|
|
374
|
+
bucket.put(objectKey, arrayBuffer, {
|
|
375
|
+
customMetadata: _object_spread_props(_object_spread({
|
|
376
|
+
type: type
|
|
377
|
+
}, path && {
|
|
378
|
+
path: path
|
|
379
|
+
}), {
|
|
380
|
+
local: local === 'true' ? 'true' : 'false'
|
|
381
|
+
})
|
|
382
|
+
})
|
|
383
|
+
];
|
|
384
|
+
case 4:
|
|
385
|
+
uploadedObject = _state.sent();
|
|
386
|
+
resource = _object_spread_props(_object_spread({
|
|
387
|
+
type: type
|
|
388
|
+
}, path && {
|
|
389
|
+
path: path
|
|
390
|
+
}), {
|
|
391
|
+
key: objectKey,
|
|
392
|
+
size: uploadedObject.size,
|
|
393
|
+
lastModified: uploadedObject.uploaded.toISOString(),
|
|
394
|
+
local: local === 'true'
|
|
395
|
+
});
|
|
396
|
+
return [
|
|
397
|
+
2,
|
|
398
|
+
context.json(resource, 201)
|
|
399
|
+
];
|
|
400
|
+
case 5:
|
|
401
|
+
error = _state.sent();
|
|
402
|
+
errorMessage = _instanceof(error, Error) ? error.message : 'Failed to upload resource';
|
|
403
|
+
return [
|
|
404
|
+
2,
|
|
405
|
+
context.json({
|
|
406
|
+
error: errorMessage
|
|
407
|
+
}, 500)
|
|
408
|
+
];
|
|
409
|
+
case 6:
|
|
410
|
+
return [
|
|
411
|
+
2
|
|
412
|
+
];
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
})();
|
|
416
|
+
});
|
|
417
|
+
// Delete a single resource by key
|
|
418
|
+
resources.delete('/:key', function(context) {
|
|
419
|
+
return _async_to_generator(function() {
|
|
420
|
+
var bucket, key, decodedKey, error;
|
|
421
|
+
return _ts_generator(this, function(_state) {
|
|
422
|
+
switch(_state.label){
|
|
423
|
+
case 0:
|
|
424
|
+
bucket = context.env.RESOURCES_BUCKET;
|
|
425
|
+
key = context.req.param('key');
|
|
426
|
+
// URL decode the key since it might contain slashes
|
|
427
|
+
decodedKey = decodeURIComponent(key);
|
|
428
|
+
_state.label = 1;
|
|
429
|
+
case 1:
|
|
430
|
+
_state.trys.push([
|
|
431
|
+
1,
|
|
432
|
+
3,
|
|
433
|
+
,
|
|
434
|
+
4
|
|
435
|
+
]);
|
|
436
|
+
// Delete the resource - R2 delete is idempotent, so it's safe to delete non-existent resources
|
|
437
|
+
return [
|
|
438
|
+
4,
|
|
439
|
+
bucket.delete(decodedKey)
|
|
440
|
+
];
|
|
441
|
+
case 2:
|
|
442
|
+
_state.sent();
|
|
443
|
+
return [
|
|
444
|
+
2,
|
|
445
|
+
new Response(null, {
|
|
446
|
+
status: 204
|
|
447
|
+
})
|
|
448
|
+
];
|
|
449
|
+
case 3:
|
|
450
|
+
error = _state.sent();
|
|
451
|
+
console.error('Failed to delete resource "'.concat(decodedKey, '":'), error);
|
|
452
|
+
return [
|
|
453
|
+
2,
|
|
454
|
+
context.json({
|
|
455
|
+
error: "Failed to delete resource: ".concat(_instanceof(error, Error) ? error.message : 'Unknown error')
|
|
456
|
+
}, 500)
|
|
457
|
+
];
|
|
458
|
+
case 4:
|
|
459
|
+
return [
|
|
460
|
+
2
|
|
461
|
+
];
|
|
462
|
+
}
|
|
463
|
+
});
|
|
464
|
+
})();
|
|
465
|
+
});
|
|
466
|
+
// Delete all resources (bulk delete) - only available in development mode
|
|
467
|
+
resources.delete('/', function(context) {
|
|
468
|
+
return _async_to_generator(function() {
|
|
469
|
+
var isDevelopment, bucket, listed, deletedCount, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, object, _r2Object_customMetadata, r2Object, err, cursor, nextBatch, _iteratorNormalCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step1, object1, _r2Object_customMetadata1, r2Object1, err;
|
|
470
|
+
return _ts_generator(this, function(_state) {
|
|
471
|
+
switch(_state.label){
|
|
472
|
+
case 0:
|
|
473
|
+
// Check if we're in development mode
|
|
474
|
+
isDevelopment = context.env.NODE_ENV === 'development';
|
|
475
|
+
if (!isDevelopment) {
|
|
476
|
+
return [
|
|
477
|
+
2,
|
|
478
|
+
context.json({
|
|
479
|
+
error: 'Bulk delete is only available in development mode'
|
|
480
|
+
}, 403)
|
|
481
|
+
];
|
|
482
|
+
}
|
|
483
|
+
bucket = context.env.RESOURCES_BUCKET;
|
|
484
|
+
return [
|
|
485
|
+
4,
|
|
486
|
+
bucket.list()
|
|
487
|
+
];
|
|
488
|
+
case 1:
|
|
489
|
+
listed = _state.sent();
|
|
490
|
+
deletedCount = 0;
|
|
491
|
+
_iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
492
|
+
_state.label = 2;
|
|
493
|
+
case 2:
|
|
494
|
+
_state.trys.push([
|
|
495
|
+
2,
|
|
496
|
+
8,
|
|
497
|
+
9,
|
|
498
|
+
10
|
|
499
|
+
]);
|
|
500
|
+
_iterator = listed.objects[Symbol.iterator]();
|
|
501
|
+
_state.label = 3;
|
|
502
|
+
case 3:
|
|
503
|
+
if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [
|
|
504
|
+
3,
|
|
505
|
+
7
|
|
506
|
+
];
|
|
507
|
+
object = _step.value;
|
|
508
|
+
return [
|
|
509
|
+
4,
|
|
510
|
+
bucket.head(object.key)
|
|
511
|
+
];
|
|
512
|
+
case 4:
|
|
513
|
+
r2Object = _state.sent();
|
|
514
|
+
if (!(r2Object === null || r2Object === void 0 ? void 0 : (_r2Object_customMetadata = r2Object.customMetadata) === null || _r2Object_customMetadata === void 0 ? void 0 : _r2Object_customMetadata.type)) return [
|
|
515
|
+
3,
|
|
516
|
+
6
|
|
517
|
+
];
|
|
518
|
+
return [
|
|
519
|
+
4,
|
|
520
|
+
bucket.delete(object.key)
|
|
521
|
+
];
|
|
522
|
+
case 5:
|
|
523
|
+
_state.sent();
|
|
524
|
+
deletedCount++;
|
|
525
|
+
_state.label = 6;
|
|
526
|
+
case 6:
|
|
527
|
+
_iteratorNormalCompletion = true;
|
|
528
|
+
return [
|
|
529
|
+
3,
|
|
530
|
+
3
|
|
531
|
+
];
|
|
532
|
+
case 7:
|
|
533
|
+
return [
|
|
534
|
+
3,
|
|
535
|
+
10
|
|
536
|
+
];
|
|
537
|
+
case 8:
|
|
538
|
+
err = _state.sent();
|
|
539
|
+
_didIteratorError = true;
|
|
540
|
+
_iteratorError = err;
|
|
541
|
+
return [
|
|
542
|
+
3,
|
|
543
|
+
10
|
|
544
|
+
];
|
|
545
|
+
case 9:
|
|
546
|
+
try {
|
|
547
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
548
|
+
_iterator.return();
|
|
549
|
+
}
|
|
550
|
+
} finally{
|
|
551
|
+
if (_didIteratorError) {
|
|
552
|
+
throw _iteratorError;
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
return [
|
|
556
|
+
7
|
|
557
|
+
];
|
|
558
|
+
case 10:
|
|
559
|
+
// Handle pagination if there are more than 1000 objects
|
|
560
|
+
cursor = listed.cursor;
|
|
561
|
+
_state.label = 11;
|
|
562
|
+
case 11:
|
|
563
|
+
if (!(listed.truncated && cursor)) return [
|
|
564
|
+
3,
|
|
565
|
+
22
|
|
566
|
+
];
|
|
567
|
+
return [
|
|
568
|
+
4,
|
|
569
|
+
bucket.list({
|
|
570
|
+
cursor: cursor
|
|
571
|
+
})
|
|
572
|
+
];
|
|
573
|
+
case 12:
|
|
574
|
+
nextBatch = _state.sent();
|
|
575
|
+
_iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined;
|
|
576
|
+
_state.label = 13;
|
|
577
|
+
case 13:
|
|
578
|
+
_state.trys.push([
|
|
579
|
+
13,
|
|
580
|
+
19,
|
|
581
|
+
20,
|
|
582
|
+
21
|
|
583
|
+
]);
|
|
584
|
+
_iterator1 = nextBatch.objects[Symbol.iterator]();
|
|
585
|
+
_state.label = 14;
|
|
586
|
+
case 14:
|
|
587
|
+
if (!!(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done)) return [
|
|
588
|
+
3,
|
|
589
|
+
18
|
|
590
|
+
];
|
|
591
|
+
object1 = _step1.value;
|
|
592
|
+
return [
|
|
593
|
+
4,
|
|
594
|
+
bucket.head(object1.key)
|
|
595
|
+
];
|
|
596
|
+
case 15:
|
|
597
|
+
r2Object1 = _state.sent();
|
|
598
|
+
if (!(r2Object1 === null || r2Object1 === void 0 ? void 0 : (_r2Object_customMetadata1 = r2Object1.customMetadata) === null || _r2Object_customMetadata1 === void 0 ? void 0 : _r2Object_customMetadata1.type)) return [
|
|
599
|
+
3,
|
|
600
|
+
17
|
|
601
|
+
];
|
|
602
|
+
return [
|
|
603
|
+
4,
|
|
604
|
+
bucket.delete(object1.key)
|
|
605
|
+
];
|
|
606
|
+
case 16:
|
|
607
|
+
_state.sent();
|
|
608
|
+
deletedCount++;
|
|
609
|
+
_state.label = 17;
|
|
610
|
+
case 17:
|
|
611
|
+
_iteratorNormalCompletion1 = true;
|
|
612
|
+
return [
|
|
613
|
+
3,
|
|
614
|
+
14
|
|
615
|
+
];
|
|
616
|
+
case 18:
|
|
617
|
+
return [
|
|
618
|
+
3,
|
|
619
|
+
21
|
|
620
|
+
];
|
|
621
|
+
case 19:
|
|
622
|
+
err = _state.sent();
|
|
623
|
+
_didIteratorError1 = true;
|
|
624
|
+
_iteratorError1 = err;
|
|
625
|
+
return [
|
|
626
|
+
3,
|
|
627
|
+
21
|
|
628
|
+
];
|
|
629
|
+
case 20:
|
|
630
|
+
try {
|
|
631
|
+
if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
|
|
632
|
+
_iterator1.return();
|
|
633
|
+
}
|
|
634
|
+
} finally{
|
|
635
|
+
if (_didIteratorError1) {
|
|
636
|
+
throw _iteratorError1;
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
return [
|
|
640
|
+
7
|
|
641
|
+
];
|
|
642
|
+
case 21:
|
|
643
|
+
cursor = nextBatch.cursor;
|
|
644
|
+
return [
|
|
645
|
+
3,
|
|
646
|
+
11
|
|
647
|
+
];
|
|
648
|
+
case 22:
|
|
649
|
+
return [
|
|
650
|
+
2,
|
|
651
|
+
context.json({
|
|
652
|
+
deletedCount: deletedCount
|
|
653
|
+
})
|
|
654
|
+
];
|
|
655
|
+
}
|
|
656
|
+
});
|
|
657
|
+
})();
|
|
658
|
+
});
|
|
659
|
+
// Generate presigned URL for large file uploads
|
|
660
|
+
resources.post('/presigned-link', function(context) {
|
|
661
|
+
return _async_to_generator(function() {
|
|
662
|
+
var body, key, type, size, _context_env, R2_ACCESS_KEY_ID, R2_SECRET_ACCESS_KEY, R2_ACCOUNT_ID, R2_BUCKET_NAME, client, url, expiresIn, requestToSign, signedRequest, error;
|
|
663
|
+
return _ts_generator(this, function(_state) {
|
|
664
|
+
switch(_state.label){
|
|
665
|
+
case 0:
|
|
666
|
+
_state.trys.push([
|
|
667
|
+
0,
|
|
668
|
+
3,
|
|
669
|
+
,
|
|
670
|
+
4
|
|
671
|
+
]);
|
|
672
|
+
return [
|
|
673
|
+
4,
|
|
674
|
+
context.req.json()
|
|
675
|
+
];
|
|
676
|
+
case 1:
|
|
677
|
+
body = _state.sent();
|
|
678
|
+
key = body.key, type = body.type, size = body.size;
|
|
679
|
+
// Validate required fields
|
|
680
|
+
if (!key) {
|
|
681
|
+
return [
|
|
682
|
+
2,
|
|
683
|
+
context.json({
|
|
684
|
+
error: 'Missing required field "key"'
|
|
685
|
+
}, 400)
|
|
686
|
+
];
|
|
687
|
+
}
|
|
688
|
+
if (!type) {
|
|
689
|
+
return [
|
|
690
|
+
2,
|
|
691
|
+
context.json({
|
|
692
|
+
error: 'Missing required field "type"'
|
|
693
|
+
}, 400)
|
|
694
|
+
];
|
|
695
|
+
}
|
|
696
|
+
if (size === undefined || size === null) {
|
|
697
|
+
return [
|
|
698
|
+
2,
|
|
699
|
+
context.json({
|
|
700
|
+
error: 'Missing required field "size"'
|
|
701
|
+
}, 400)
|
|
702
|
+
];
|
|
703
|
+
}
|
|
704
|
+
// Validate type
|
|
705
|
+
if (type !== 'text' && type !== 'binary') {
|
|
706
|
+
return [
|
|
707
|
+
2,
|
|
708
|
+
context.json({
|
|
709
|
+
error: 'type must be either "text" or "binary"'
|
|
710
|
+
}, 400)
|
|
711
|
+
];
|
|
712
|
+
}
|
|
713
|
+
// Check if R2 credentials are configured
|
|
714
|
+
_context_env = context.env, R2_ACCESS_KEY_ID = _context_env.R2_ACCESS_KEY_ID, R2_SECRET_ACCESS_KEY = _context_env.R2_SECRET_ACCESS_KEY, R2_ACCOUNT_ID = _context_env.R2_ACCOUNT_ID, R2_BUCKET_NAME = _context_env.R2_BUCKET_NAME;
|
|
715
|
+
if (!R2_ACCESS_KEY_ID || !R2_SECRET_ACCESS_KEY || !R2_ACCOUNT_ID || !R2_BUCKET_NAME) {
|
|
716
|
+
return [
|
|
717
|
+
2,
|
|
718
|
+
context.json({
|
|
719
|
+
error: 'R2 credentials not configured. Please set R2_ACCESS_KEY_ID, R2_SECRET_ACCESS_KEY, R2_ACCOUNT_ID, and R2_BUCKET_NAME environment variables.'
|
|
720
|
+
}, 400)
|
|
721
|
+
];
|
|
722
|
+
}
|
|
723
|
+
// Create AWS client with R2 credentials
|
|
724
|
+
client = new AwsClient({
|
|
725
|
+
accessKeyId: R2_ACCESS_KEY_ID,
|
|
726
|
+
secretAccessKey: R2_SECRET_ACCESS_KEY
|
|
727
|
+
});
|
|
728
|
+
// Construct the R2 URL
|
|
729
|
+
url = new URL("https://".concat(R2_BUCKET_NAME, ".").concat(R2_ACCOUNT_ID, ".r2.cloudflarestorage.com/").concat(key));
|
|
730
|
+
// Set expiration to 1 hour (3600 seconds)
|
|
731
|
+
expiresIn = 3600;
|
|
732
|
+
url.searchParams.set('X-Amz-Expires', expiresIn.toString());
|
|
733
|
+
// Create a request to sign
|
|
734
|
+
requestToSign = new Request(url, {
|
|
735
|
+
method: 'PUT',
|
|
736
|
+
headers: {
|
|
737
|
+
'x-amz-meta-type': type,
|
|
738
|
+
'x-amz-meta-local': 'false'
|
|
739
|
+
}
|
|
740
|
+
});
|
|
741
|
+
return [
|
|
742
|
+
4,
|
|
743
|
+
client.sign(requestToSign, {
|
|
744
|
+
aws: {
|
|
745
|
+
signQuery: true,
|
|
746
|
+
service: 's3'
|
|
747
|
+
}
|
|
748
|
+
})
|
|
749
|
+
];
|
|
750
|
+
case 2:
|
|
751
|
+
signedRequest = _state.sent();
|
|
752
|
+
// Return the presigned URL
|
|
753
|
+
return [
|
|
754
|
+
2,
|
|
755
|
+
context.json({
|
|
756
|
+
url: signedRequest.url,
|
|
757
|
+
method: 'PUT',
|
|
758
|
+
expiresIn: expiresIn
|
|
759
|
+
})
|
|
760
|
+
];
|
|
761
|
+
case 3:
|
|
762
|
+
error = _state.sent();
|
|
763
|
+
console.error('Error generating presigned URL:', error);
|
|
764
|
+
return [
|
|
765
|
+
2,
|
|
766
|
+
context.json({
|
|
767
|
+
error: "Failed to generate presigned URL: ".concat(_instanceof(error, Error) ? error.message : 'Unknown error')
|
|
768
|
+
}, 500)
|
|
769
|
+
];
|
|
770
|
+
case 4:
|
|
771
|
+
return [
|
|
772
|
+
2
|
|
773
|
+
];
|
|
774
|
+
}
|
|
775
|
+
});
|
|
776
|
+
})();
|
|
777
|
+
});
|
|
778
|
+
export default resources;
|