@positronic/cloudflare 0.0.2 → 0.0.4
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.js +1270 -0
- package/dist/src/brain-runner-do.js +654 -0
- package/dist/src/dev-server.js +1357 -0
- package/{src/index.ts → dist/src/index.js} +1 -6
- package/dist/src/manifest.js +278 -0
- package/dist/src/monitor-do.js +408 -0
- package/{src/node-index.ts → dist/src/node-index.js} +3 -7
- package/dist/src/r2-loader.js +207 -0
- package/dist/src/schedule-do.js +705 -0
- package/dist/src/sqlite-adapter.js +69 -0
- package/dist/types/api.d.ts +21 -0
- package/dist/types/api.d.ts.map +1 -0
- package/dist/types/brain-runner-do.d.ts +25 -0
- package/dist/types/brain-runner-do.d.ts.map +1 -0
- package/dist/types/dev-server.d.ts +45 -0
- package/dist/types/dev-server.d.ts.map +1 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/manifest.d.ts +11 -0
- package/dist/types/manifest.d.ts.map +1 -0
- package/dist/types/monitor-do.d.ts +16 -0
- package/dist/types/monitor-do.d.ts.map +1 -0
- package/dist/types/node-index.d.ts +10 -0
- package/dist/types/node-index.d.ts.map +1 -0
- package/dist/types/r2-loader.d.ts +10 -0
- package/dist/types/r2-loader.d.ts.map +1 -0
- package/dist/types/schedule-do.d.ts +47 -0
- package/dist/types/schedule-do.d.ts.map +1 -0
- package/dist/types/sqlite-adapter.d.ts +10 -0
- package/dist/types/sqlite-adapter.d.ts.map +1 -0
- package/package.json +8 -4
- package/src/api.ts +0 -579
- package/src/brain-runner-do.ts +0 -309
- package/src/dev-server.ts +0 -776
- package/src/manifest.ts +0 -69
- package/src/monitor-do.ts +0 -268
- package/src/r2-loader.ts +0 -27
- package/src/schedule-do.ts +0 -377
- package/src/sqlite-adapter.ts +0 -50
- package/test-project/package-lock.json +0 -3010
- package/test-project/package.json +0 -21
- package/test-project/src/index.ts +0 -70
- package/test-project/src/runner.ts +0 -24
- package/test-project/tests/api.test.ts +0 -1005
- package/test-project/tests/r2loader.test.ts +0 -73
- package/test-project/tests/resources-api.test.ts +0 -671
- package/test-project/tests/spec.test.ts +0 -135
- package/test-project/tests/tsconfig.json +0 -7
- package/test-project/tsconfig.json +0 -20
- package/test-project/vitest.config.ts +0 -12
- package/test-project/wrangler.jsonc +0 -53
- package/tsconfig.json +0 -11
|
@@ -0,0 +1,654 @@
|
|
|
1
|
+
function _assert_this_initialized(self) {
|
|
2
|
+
if (self === void 0) {
|
|
3
|
+
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
|
4
|
+
}
|
|
5
|
+
return self;
|
|
6
|
+
}
|
|
7
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
8
|
+
try {
|
|
9
|
+
var info = gen[key](arg);
|
|
10
|
+
var value = info.value;
|
|
11
|
+
} catch (error) {
|
|
12
|
+
reject(error);
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
if (info.done) {
|
|
16
|
+
resolve(value);
|
|
17
|
+
} else {
|
|
18
|
+
Promise.resolve(value).then(_next, _throw);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function _async_to_generator(fn) {
|
|
22
|
+
return function() {
|
|
23
|
+
var self = this, args = arguments;
|
|
24
|
+
return new Promise(function(resolve, reject) {
|
|
25
|
+
var gen = fn.apply(self, args);
|
|
26
|
+
function _next(value) {
|
|
27
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
28
|
+
}
|
|
29
|
+
function _throw(err) {
|
|
30
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
31
|
+
}
|
|
32
|
+
_next(undefined);
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function _call_super(_this, derived, args) {
|
|
37
|
+
derived = _get_prototype_of(derived);
|
|
38
|
+
return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args));
|
|
39
|
+
}
|
|
40
|
+
function _class_call_check(instance, Constructor) {
|
|
41
|
+
if (!(instance instanceof Constructor)) {
|
|
42
|
+
throw new TypeError("Cannot call a class as a function");
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function _defineProperties(target, props) {
|
|
46
|
+
for(var i = 0; i < props.length; i++){
|
|
47
|
+
var descriptor = props[i];
|
|
48
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
49
|
+
descriptor.configurable = true;
|
|
50
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
51
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function _create_class(Constructor, protoProps, staticProps) {
|
|
55
|
+
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
56
|
+
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
57
|
+
return Constructor;
|
|
58
|
+
}
|
|
59
|
+
function _define_property(obj, key, value) {
|
|
60
|
+
if (key in obj) {
|
|
61
|
+
Object.defineProperty(obj, key, {
|
|
62
|
+
value: value,
|
|
63
|
+
enumerable: true,
|
|
64
|
+
configurable: true,
|
|
65
|
+
writable: true
|
|
66
|
+
});
|
|
67
|
+
} else {
|
|
68
|
+
obj[key] = value;
|
|
69
|
+
}
|
|
70
|
+
return obj;
|
|
71
|
+
}
|
|
72
|
+
function _get_prototype_of(o) {
|
|
73
|
+
_get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
|
|
74
|
+
return o.__proto__ || Object.getPrototypeOf(o);
|
|
75
|
+
};
|
|
76
|
+
return _get_prototype_of(o);
|
|
77
|
+
}
|
|
78
|
+
function _inherits(subClass, superClass) {
|
|
79
|
+
if (typeof superClass !== "function" && superClass !== null) {
|
|
80
|
+
throw new TypeError("Super expression must either be null or a function");
|
|
81
|
+
}
|
|
82
|
+
subClass.prototype = Object.create(superClass && superClass.prototype, {
|
|
83
|
+
constructor: {
|
|
84
|
+
value: subClass,
|
|
85
|
+
writable: true,
|
|
86
|
+
configurable: true
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
if (superClass) _set_prototype_of(subClass, superClass);
|
|
90
|
+
}
|
|
91
|
+
function _possible_constructor_return(self, call) {
|
|
92
|
+
if (call && (_type_of(call) === "object" || typeof call === "function")) {
|
|
93
|
+
return call;
|
|
94
|
+
}
|
|
95
|
+
return _assert_this_initialized(self);
|
|
96
|
+
}
|
|
97
|
+
function _set_prototype_of(o, p) {
|
|
98
|
+
_set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
|
|
99
|
+
o.__proto__ = p;
|
|
100
|
+
return o;
|
|
101
|
+
};
|
|
102
|
+
return _set_prototype_of(o, p);
|
|
103
|
+
}
|
|
104
|
+
function _type_of(obj) {
|
|
105
|
+
"@swc/helpers - typeof";
|
|
106
|
+
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
|
|
107
|
+
}
|
|
108
|
+
function _is_native_reflect_construct() {
|
|
109
|
+
try {
|
|
110
|
+
var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
|
|
111
|
+
} catch (_) {}
|
|
112
|
+
return (_is_native_reflect_construct = function() {
|
|
113
|
+
return !!result;
|
|
114
|
+
})();
|
|
115
|
+
}
|
|
116
|
+
function _ts_generator(thisArg, body) {
|
|
117
|
+
var f, y, t, _ = {
|
|
118
|
+
label: 0,
|
|
119
|
+
sent: function() {
|
|
120
|
+
if (t[0] & 1) throw t[1];
|
|
121
|
+
return t[1];
|
|
122
|
+
},
|
|
123
|
+
trys: [],
|
|
124
|
+
ops: []
|
|
125
|
+
}, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
126
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
127
|
+
return this;
|
|
128
|
+
}), g;
|
|
129
|
+
function verb(n) {
|
|
130
|
+
return function(v) {
|
|
131
|
+
return step([
|
|
132
|
+
n,
|
|
133
|
+
v
|
|
134
|
+
]);
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
function step(op) {
|
|
138
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
139
|
+
while(g && (g = 0, op[0] && (_ = 0)), _)try {
|
|
140
|
+
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;
|
|
141
|
+
if (y = 0, t) op = [
|
|
142
|
+
op[0] & 2,
|
|
143
|
+
t.value
|
|
144
|
+
];
|
|
145
|
+
switch(op[0]){
|
|
146
|
+
case 0:
|
|
147
|
+
case 1:
|
|
148
|
+
t = op;
|
|
149
|
+
break;
|
|
150
|
+
case 4:
|
|
151
|
+
_.label++;
|
|
152
|
+
return {
|
|
153
|
+
value: op[1],
|
|
154
|
+
done: false
|
|
155
|
+
};
|
|
156
|
+
case 5:
|
|
157
|
+
_.label++;
|
|
158
|
+
y = op[1];
|
|
159
|
+
op = [
|
|
160
|
+
0
|
|
161
|
+
];
|
|
162
|
+
continue;
|
|
163
|
+
case 7:
|
|
164
|
+
op = _.ops.pop();
|
|
165
|
+
_.trys.pop();
|
|
166
|
+
continue;
|
|
167
|
+
default:
|
|
168
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
169
|
+
_ = 0;
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
173
|
+
_.label = op[1];
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
177
|
+
_.label = t[1];
|
|
178
|
+
t = op;
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
if (t && _.label < t[2]) {
|
|
182
|
+
_.label = t[2];
|
|
183
|
+
_.ops.push(op);
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
if (t[2]) _.ops.pop();
|
|
187
|
+
_.trys.pop();
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
op = body.call(thisArg, _);
|
|
191
|
+
} catch (e) {
|
|
192
|
+
op = [
|
|
193
|
+
6,
|
|
194
|
+
e
|
|
195
|
+
];
|
|
196
|
+
y = 0;
|
|
197
|
+
} finally{
|
|
198
|
+
f = t = 0;
|
|
199
|
+
}
|
|
200
|
+
if (op[0] & 5) throw op[1];
|
|
201
|
+
return {
|
|
202
|
+
value: op[0] ? op[1] : void 0,
|
|
203
|
+
done: true
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
import { DurableObject } from 'cloudflare:workers';
|
|
208
|
+
import { BrainRunSQLiteAdapter } from './sqlite-adapter.js';
|
|
209
|
+
import { CloudflareR2Loader } from './r2-loader.js';
|
|
210
|
+
import { createResources } from '@positronic/core';
|
|
211
|
+
var manifest = null;
|
|
212
|
+
export function setManifest(generatedManifest) {
|
|
213
|
+
manifest = generatedManifest;
|
|
214
|
+
}
|
|
215
|
+
export function getManifest() {
|
|
216
|
+
return manifest;
|
|
217
|
+
}
|
|
218
|
+
var brainRunner = null;
|
|
219
|
+
export function setBrainRunner(runner) {
|
|
220
|
+
brainRunner = runner;
|
|
221
|
+
}
|
|
222
|
+
var EventStreamAdapter = /*#__PURE__*/ function() {
|
|
223
|
+
"use strict";
|
|
224
|
+
function EventStreamAdapter() {
|
|
225
|
+
_class_call_check(this, EventStreamAdapter);
|
|
226
|
+
_define_property(this, "subscribers", new Set());
|
|
227
|
+
_define_property(this, "encoder", new TextEncoder());
|
|
228
|
+
}
|
|
229
|
+
_create_class(EventStreamAdapter, [
|
|
230
|
+
{
|
|
231
|
+
key: "subscribe",
|
|
232
|
+
value: function subscribe(controller) {
|
|
233
|
+
this.subscribers.add(controller);
|
|
234
|
+
}
|
|
235
|
+
},
|
|
236
|
+
{
|
|
237
|
+
key: "unsubscribe",
|
|
238
|
+
value: function unsubscribe(controller) {
|
|
239
|
+
this.subscribers.delete(controller);
|
|
240
|
+
}
|
|
241
|
+
},
|
|
242
|
+
{
|
|
243
|
+
key: "broadcast",
|
|
244
|
+
value: function broadcast(event) {
|
|
245
|
+
var _this = this;
|
|
246
|
+
var message = "data: ".concat(JSON.stringify(event), "\n\n");
|
|
247
|
+
var encodedMessage = this.encoder.encode(message);
|
|
248
|
+
this.subscribers.forEach(function(controller) {
|
|
249
|
+
try {
|
|
250
|
+
controller.enqueue(encodedMessage);
|
|
251
|
+
} catch (e) {
|
|
252
|
+
console.error('[DO_SSE_ADAPTER] Failed to send message to subscriber, removing.', e);
|
|
253
|
+
_this.unsubscribe(controller);
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
},
|
|
258
|
+
{
|
|
259
|
+
key: "dispatch",
|
|
260
|
+
value: function dispatch(event) {
|
|
261
|
+
return _async_to_generator(function() {
|
|
262
|
+
return _ts_generator(this, function(_state) {
|
|
263
|
+
try {
|
|
264
|
+
this.broadcast(event);
|
|
265
|
+
} catch (e) {
|
|
266
|
+
console.error('[DO_SSE_ADAPTER] Error dispatching event:', e);
|
|
267
|
+
throw e;
|
|
268
|
+
}
|
|
269
|
+
return [
|
|
270
|
+
2
|
|
271
|
+
];
|
|
272
|
+
});
|
|
273
|
+
}).call(this);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
]);
|
|
277
|
+
return EventStreamAdapter;
|
|
278
|
+
}();
|
|
279
|
+
var MonitorAdapter = /*#__PURE__*/ function() {
|
|
280
|
+
"use strict";
|
|
281
|
+
function MonitorAdapter(monitorStub) {
|
|
282
|
+
_class_call_check(this, MonitorAdapter);
|
|
283
|
+
_define_property(this, "monitorStub", void 0);
|
|
284
|
+
this.monitorStub = monitorStub;
|
|
285
|
+
}
|
|
286
|
+
_create_class(MonitorAdapter, [
|
|
287
|
+
{
|
|
288
|
+
key: "dispatch",
|
|
289
|
+
value: function dispatch(event) {
|
|
290
|
+
return _async_to_generator(function() {
|
|
291
|
+
return _ts_generator(this, function(_state) {
|
|
292
|
+
switch(_state.label){
|
|
293
|
+
case 0:
|
|
294
|
+
return [
|
|
295
|
+
4,
|
|
296
|
+
this.monitorStub.handleBrainEvent(event)
|
|
297
|
+
];
|
|
298
|
+
case 1:
|
|
299
|
+
_state.sent();
|
|
300
|
+
return [
|
|
301
|
+
2
|
|
302
|
+
];
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
}).call(this);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
]);
|
|
309
|
+
return MonitorAdapter;
|
|
310
|
+
}();
|
|
311
|
+
var ScheduleAdapter = /*#__PURE__*/ function() {
|
|
312
|
+
"use strict";
|
|
313
|
+
function ScheduleAdapter(scheduleStub) {
|
|
314
|
+
_class_call_check(this, ScheduleAdapter);
|
|
315
|
+
_define_property(this, "scheduleStub", void 0);
|
|
316
|
+
this.scheduleStub = scheduleStub;
|
|
317
|
+
}
|
|
318
|
+
_create_class(ScheduleAdapter, [
|
|
319
|
+
{
|
|
320
|
+
key: "dispatch",
|
|
321
|
+
value: function dispatch(event) {
|
|
322
|
+
return _async_to_generator(function() {
|
|
323
|
+
return _ts_generator(this, function(_state) {
|
|
324
|
+
switch(_state.label){
|
|
325
|
+
case 0:
|
|
326
|
+
return [
|
|
327
|
+
4,
|
|
328
|
+
this.scheduleStub.handleBrainEvent(event)
|
|
329
|
+
];
|
|
330
|
+
case 1:
|
|
331
|
+
_state.sent();
|
|
332
|
+
return [
|
|
333
|
+
2
|
|
334
|
+
];
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
}).call(this);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
]);
|
|
341
|
+
return ScheduleAdapter;
|
|
342
|
+
}();
|
|
343
|
+
export var BrainRunnerDO = /*#__PURE__*/ function(DurableObject) {
|
|
344
|
+
"use strict";
|
|
345
|
+
_inherits(BrainRunnerDO, DurableObject);
|
|
346
|
+
function BrainRunnerDO(state, env) {
|
|
347
|
+
_class_call_check(this, BrainRunnerDO);
|
|
348
|
+
var _this;
|
|
349
|
+
_this = _call_super(this, BrainRunnerDO, [
|
|
350
|
+
state,
|
|
351
|
+
env
|
|
352
|
+
]), _define_property(_this, "sql", void 0), _define_property(_this, "brainRunId", void 0), _define_property(_this, "eventStreamAdapter", new EventStreamAdapter());
|
|
353
|
+
_this.sql = state.storage.sql;
|
|
354
|
+
_this.brainRunId = state.id.toString();
|
|
355
|
+
_this.env = env;
|
|
356
|
+
return _this;
|
|
357
|
+
}
|
|
358
|
+
_create_class(BrainRunnerDO, [
|
|
359
|
+
{
|
|
360
|
+
key: "loadResourcesFromR2",
|
|
361
|
+
value: function loadResourcesFromR2() {
|
|
362
|
+
return _async_to_generator(function() {
|
|
363
|
+
var bucket, listed, manifest, resourceCount, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, object, _r2Object_customMetadata, r2Object, keyParts, fileName, current, i, part, err, loader, resources;
|
|
364
|
+
return _ts_generator(this, function(_state) {
|
|
365
|
+
switch(_state.label){
|
|
366
|
+
case 0:
|
|
367
|
+
bucket = this.env.RESOURCES_BUCKET;
|
|
368
|
+
return [
|
|
369
|
+
4,
|
|
370
|
+
bucket.list()
|
|
371
|
+
];
|
|
372
|
+
case 1:
|
|
373
|
+
listed = _state.sent();
|
|
374
|
+
// Check if results are truncated
|
|
375
|
+
if (listed.truncated) {
|
|
376
|
+
throw new Error("Too many resources in R2 bucket (more than 1000). " + "Resource pagination is not yet supported. " + "Please reduce the number of resources.");
|
|
377
|
+
}
|
|
378
|
+
if (listed.objects.length === 0) {
|
|
379
|
+
return [
|
|
380
|
+
2,
|
|
381
|
+
null
|
|
382
|
+
];
|
|
383
|
+
}
|
|
384
|
+
// Build the manifest structure
|
|
385
|
+
manifest = {};
|
|
386
|
+
resourceCount = 0;
|
|
387
|
+
_iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
388
|
+
_state.label = 2;
|
|
389
|
+
case 2:
|
|
390
|
+
_state.trys.push([
|
|
391
|
+
2,
|
|
392
|
+
7,
|
|
393
|
+
8,
|
|
394
|
+
9
|
|
395
|
+
]);
|
|
396
|
+
_iterator = listed.objects[Symbol.iterator]();
|
|
397
|
+
_state.label = 3;
|
|
398
|
+
case 3:
|
|
399
|
+
if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [
|
|
400
|
+
3,
|
|
401
|
+
6
|
|
402
|
+
];
|
|
403
|
+
object = _step.value;
|
|
404
|
+
return [
|
|
405
|
+
4,
|
|
406
|
+
bucket.head(object.key)
|
|
407
|
+
];
|
|
408
|
+
case 4:
|
|
409
|
+
r2Object = _state.sent();
|
|
410
|
+
if (!r2Object || !((_r2Object_customMetadata = r2Object.customMetadata) === null || _r2Object_customMetadata === void 0 ? void 0 : _r2Object_customMetadata.type)) {
|
|
411
|
+
console.warn("[DO ".concat(this.brainRunId, "] Skipping resource ").concat(object.key, " - ") + "missing metadata.type (found: ".concat(JSON.stringify((r2Object === null || r2Object === void 0 ? void 0 : r2Object.customMetadata) || {}), ")"));
|
|
412
|
+
return [
|
|
413
|
+
3,
|
|
414
|
+
5
|
|
415
|
+
];
|
|
416
|
+
}
|
|
417
|
+
// Parse the key to create nested structure
|
|
418
|
+
// e.g., "folder/file.txt" becomes manifest.folder["file.txt"]
|
|
419
|
+
keyParts = object.key.split('/');
|
|
420
|
+
// Get the file name (with extension preserved)
|
|
421
|
+
fileName = keyParts[keyParts.length - 1];
|
|
422
|
+
// Navigate/create nested structure
|
|
423
|
+
current = manifest;
|
|
424
|
+
for(i = 0; i < keyParts.length - 1; i++){
|
|
425
|
+
part = keyParts[i];
|
|
426
|
+
if (!current[part]) {
|
|
427
|
+
current[part] = {};
|
|
428
|
+
}
|
|
429
|
+
current = current[part];
|
|
430
|
+
}
|
|
431
|
+
// Add the resource entry with full filename
|
|
432
|
+
current[fileName] = {
|
|
433
|
+
type: r2Object.customMetadata.type,
|
|
434
|
+
key: object.key,
|
|
435
|
+
path: r2Object.customMetadata.path || object.key
|
|
436
|
+
};
|
|
437
|
+
resourceCount++;
|
|
438
|
+
_state.label = 5;
|
|
439
|
+
case 5:
|
|
440
|
+
_iteratorNormalCompletion = true;
|
|
441
|
+
return [
|
|
442
|
+
3,
|
|
443
|
+
3
|
|
444
|
+
];
|
|
445
|
+
case 6:
|
|
446
|
+
return [
|
|
447
|
+
3,
|
|
448
|
+
9
|
|
449
|
+
];
|
|
450
|
+
case 7:
|
|
451
|
+
err = _state.sent();
|
|
452
|
+
_didIteratorError = true;
|
|
453
|
+
_iteratorError = err;
|
|
454
|
+
return [
|
|
455
|
+
3,
|
|
456
|
+
9
|
|
457
|
+
];
|
|
458
|
+
case 8:
|
|
459
|
+
try {
|
|
460
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
461
|
+
_iterator.return();
|
|
462
|
+
}
|
|
463
|
+
} finally{
|
|
464
|
+
if (_didIteratorError) {
|
|
465
|
+
throw _iteratorError;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
return [
|
|
469
|
+
7
|
|
470
|
+
];
|
|
471
|
+
case 9:
|
|
472
|
+
if (resourceCount === 0) {
|
|
473
|
+
return [
|
|
474
|
+
2,
|
|
475
|
+
null
|
|
476
|
+
];
|
|
477
|
+
}
|
|
478
|
+
// Create the loader and resources
|
|
479
|
+
loader = new CloudflareR2Loader(bucket);
|
|
480
|
+
resources = createResources(loader, manifest);
|
|
481
|
+
return [
|
|
482
|
+
2,
|
|
483
|
+
resources
|
|
484
|
+
];
|
|
485
|
+
}
|
|
486
|
+
});
|
|
487
|
+
}).call(this);
|
|
488
|
+
}
|
|
489
|
+
},
|
|
490
|
+
{
|
|
491
|
+
key: "start",
|
|
492
|
+
value: function start(brainName, brainRunId, initialData) {
|
|
493
|
+
return _async_to_generator(function() {
|
|
494
|
+
var sql, brainToRun, sqliteAdapter, eventStreamAdapter, monitorAdapter, scheduleAdapter, r2Resources, runnerWithResources;
|
|
495
|
+
return _ts_generator(this, function(_state) {
|
|
496
|
+
switch(_state.label){
|
|
497
|
+
case 0:
|
|
498
|
+
sql = this.sql;
|
|
499
|
+
if (!manifest) {
|
|
500
|
+
throw new Error('Runtime manifest not initialized');
|
|
501
|
+
}
|
|
502
|
+
return [
|
|
503
|
+
4,
|
|
504
|
+
manifest.import(brainName)
|
|
505
|
+
];
|
|
506
|
+
case 1:
|
|
507
|
+
brainToRun = _state.sent();
|
|
508
|
+
if (!brainToRun) {
|
|
509
|
+
console.error("[DO ".concat(brainRunId, "] Brain ").concat(brainName, " not found in manifest."));
|
|
510
|
+
console.error(JSON.stringify(manifest, null, 2));
|
|
511
|
+
throw new Error("Brain ".concat(brainName, " not found"));
|
|
512
|
+
}
|
|
513
|
+
sqliteAdapter = new BrainRunSQLiteAdapter(sql);
|
|
514
|
+
eventStreamAdapter = this.eventStreamAdapter;
|
|
515
|
+
monitorAdapter = new MonitorAdapter(this.env.MONITOR_DO.get(this.env.MONITOR_DO.idFromName('singleton')));
|
|
516
|
+
scheduleAdapter = new ScheduleAdapter(this.env.SCHEDULE_DO.get(this.env.SCHEDULE_DO.idFromName('singleton')));
|
|
517
|
+
if (!brainRunner) {
|
|
518
|
+
throw new Error('BrainRunner not initialized');
|
|
519
|
+
}
|
|
520
|
+
return [
|
|
521
|
+
4,
|
|
522
|
+
this.loadResourcesFromR2()
|
|
523
|
+
];
|
|
524
|
+
case 2:
|
|
525
|
+
r2Resources = _state.sent();
|
|
526
|
+
// Create an enhanced runner with resources if available
|
|
527
|
+
runnerWithResources = brainRunner;
|
|
528
|
+
// Use R2 resources if available
|
|
529
|
+
if (r2Resources) {
|
|
530
|
+
runnerWithResources = brainRunner.withResources(r2Resources);
|
|
531
|
+
}
|
|
532
|
+
runnerWithResources.withAdapters([
|
|
533
|
+
sqliteAdapter,
|
|
534
|
+
eventStreamAdapter,
|
|
535
|
+
monitorAdapter,
|
|
536
|
+
scheduleAdapter
|
|
537
|
+
]).run(brainToRun, {
|
|
538
|
+
initialState: initialData || {},
|
|
539
|
+
brainRunId: brainRunId
|
|
540
|
+
}).catch(function(err) {
|
|
541
|
+
console.error("[DO ".concat(brainRunId, "] BrainRunner run failed:"), err);
|
|
542
|
+
});
|
|
543
|
+
return [
|
|
544
|
+
2
|
|
545
|
+
];
|
|
546
|
+
}
|
|
547
|
+
});
|
|
548
|
+
}).call(this);
|
|
549
|
+
}
|
|
550
|
+
},
|
|
551
|
+
{
|
|
552
|
+
key: "fetch",
|
|
553
|
+
value: function fetch(request) {
|
|
554
|
+
return _async_to_generator(function() {
|
|
555
|
+
var _this, sql, eventStreamAdapter, brainRunId, url, encoder, sendEvent, streamController, stream;
|
|
556
|
+
return _ts_generator(this, function(_state) {
|
|
557
|
+
_this = this, sql = _this.sql, eventStreamAdapter = _this.eventStreamAdapter, brainRunId = _this.brainRunId;
|
|
558
|
+
url = new URL(request.url);
|
|
559
|
+
encoder = new TextEncoder();
|
|
560
|
+
sendEvent = function(controller, data) {
|
|
561
|
+
controller.enqueue(encoder.encode("data: ".concat(JSON.stringify(data), "\n\n")));
|
|
562
|
+
};
|
|
563
|
+
streamController = null;
|
|
564
|
+
try {
|
|
565
|
+
if (url.pathname === '/watch') {
|
|
566
|
+
stream = new ReadableStream({
|
|
567
|
+
start: function start(controller) {
|
|
568
|
+
return _async_to_generator(function() {
|
|
569
|
+
var existingEventsSql, existingEventsResult, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, row, event;
|
|
570
|
+
return _ts_generator(this, function(_state) {
|
|
571
|
+
streamController = controller;
|
|
572
|
+
try {
|
|
573
|
+
streamController = controller;
|
|
574
|
+
existingEventsSql = "\n SELECT serialized_event\n FROM brain_events\n ORDER BY event_id ASC;\n ";
|
|
575
|
+
existingEventsResult = sql.exec(existingEventsSql).toArray();
|
|
576
|
+
_iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
577
|
+
try {
|
|
578
|
+
for(_iterator = existingEventsResult[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
579
|
+
row = _step.value;
|
|
580
|
+
try {
|
|
581
|
+
event = JSON.parse(row.serialized_event);
|
|
582
|
+
sendEvent(controller, event);
|
|
583
|
+
} catch (parseError) {
|
|
584
|
+
console.error("[DO ".concat(brainRunId, " WATCH] Failed to parse historical event JSON: ").concat(row.serialized_event), parseError);
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
} catch (err) {
|
|
588
|
+
_didIteratorError = true;
|
|
589
|
+
_iteratorError = err;
|
|
590
|
+
} finally{
|
|
591
|
+
try {
|
|
592
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
593
|
+
_iterator.return();
|
|
594
|
+
}
|
|
595
|
+
} finally{
|
|
596
|
+
if (_didIteratorError) {
|
|
597
|
+
throw _iteratorError;
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
eventStreamAdapter.subscribe(controller);
|
|
602
|
+
} catch (err) {
|
|
603
|
+
console.error("[DO ".concat(brainRunId, " WATCH] Error during stream start:"), err);
|
|
604
|
+
controller.close();
|
|
605
|
+
eventStreamAdapter.unsubscribe(streamController);
|
|
606
|
+
throw err;
|
|
607
|
+
}
|
|
608
|
+
return [
|
|
609
|
+
2
|
|
610
|
+
];
|
|
611
|
+
});
|
|
612
|
+
})();
|
|
613
|
+
},
|
|
614
|
+
cancel: function cancel(reason) {
|
|
615
|
+
if (streamController) eventStreamAdapter.unsubscribe(streamController);
|
|
616
|
+
}
|
|
617
|
+
});
|
|
618
|
+
return [
|
|
619
|
+
2,
|
|
620
|
+
new Response(stream, {
|
|
621
|
+
headers: {
|
|
622
|
+
'Content-Type': 'text/event-stream',
|
|
623
|
+
'Cache-Control': 'no-cache',
|
|
624
|
+
Connection: 'keep-alive'
|
|
625
|
+
}
|
|
626
|
+
})
|
|
627
|
+
];
|
|
628
|
+
}
|
|
629
|
+
console.warn("[DO ".concat(brainRunId, "] fetch() called with unhandled path: ").concat(url.pathname));
|
|
630
|
+
return [
|
|
631
|
+
2,
|
|
632
|
+
new Response('Not found', {
|
|
633
|
+
status: 404
|
|
634
|
+
})
|
|
635
|
+
];
|
|
636
|
+
} catch (error) {
|
|
637
|
+
console.error("[DO ".concat(brainRunId, "] Error in fetch():"), error);
|
|
638
|
+
return [
|
|
639
|
+
2,
|
|
640
|
+
new Response('Internal server error', {
|
|
641
|
+
status: 500
|
|
642
|
+
})
|
|
643
|
+
];
|
|
644
|
+
}
|
|
645
|
+
return [
|
|
646
|
+
2
|
|
647
|
+
];
|
|
648
|
+
});
|
|
649
|
+
}).call(this);
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
]);
|
|
653
|
+
return BrainRunnerDO;
|
|
654
|
+
}(DurableObject);
|