@dxos/functions-runtime-cloudflare 0.8.4-main.70d3990 → 0.8.4-main.765dc60934
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/LICENSE +102 -5
- package/README.md +1 -1
- package/dist/lib/browser/index.mjs +741 -251
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +741 -251
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/functions-client.d.ts +1 -5
- package/dist/types/src/functions-client.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +2 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/internal/data-service-impl.d.ts +8 -7
- package/dist/types/src/internal/data-service-impl.d.ts.map +1 -1
- package/dist/types/src/internal/query-service-impl.d.ts +4 -9
- package/dist/types/src/internal/query-service-impl.d.ts.map +1 -1
- package/dist/types/src/internal/queue-service-impl.d.ts +8 -9
- package/dist/types/src/internal/queue-service-impl.d.ts.map +1 -1
- package/dist/types/src/internal/service-container.d.ts +9 -9
- package/dist/types/src/internal/service-container.d.ts.map +1 -1
- package/dist/types/src/internal/utils.d.ts +2 -0
- package/dist/types/src/internal/utils.d.ts.map +1 -0
- package/dist/types/src/logger.d.ts +2 -0
- package/dist/types/src/logger.d.ts.map +1 -0
- package/dist/types/src/queues-api.d.ts +10 -6
- package/dist/types/src/queues-api.d.ts.map +1 -1
- package/dist/types/src/space-proxy.d.ts +3 -2
- package/dist/types/src/space-proxy.d.ts.map +1 -1
- package/dist/types/src/wrap-handler-for-cloudflare.d.ts +6 -0
- package/dist/types/src/wrap-handler-for-cloudflare.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +21 -16
- package/src/functions-client.ts +9 -8
- package/src/index.ts +2 -0
- package/src/internal/data-service-impl.ts +73 -18
- package/src/internal/query-service-impl.ts +23 -61
- package/src/internal/queue-service-impl.ts +54 -11
- package/src/internal/service-container.ts +50 -12
- package/src/internal/utils.ts +5 -0
- package/src/logger.ts +44 -0
- package/src/queues-api.ts +26 -7
- package/src/space-proxy.ts +5 -4
- package/src/wrap-handler-for-cloudflare.ts +4 -4
- package/dist/types/src/internal/adapter.d.ts +0 -12
- package/dist/types/src/internal/adapter.d.ts.map +0 -1
- package/src/internal/adapter.ts +0 -48
|
@@ -1,10 +1,86 @@
|
|
|
1
|
+
// src/functions-client.ts
|
|
2
|
+
import { Resource as Resource2 } from "@dxos/context";
|
|
3
|
+
import { EchoClient } from "@dxos/echo-db";
|
|
4
|
+
import { invariant as invariant3 } from "@dxos/invariant";
|
|
5
|
+
|
|
1
6
|
// src/internal/data-service-impl.ts
|
|
2
7
|
import { Stream } from "@dxos/codec-protobuf/stream";
|
|
3
8
|
import { raise } from "@dxos/debug";
|
|
9
|
+
import { NotImplementedError, RuntimeServiceError } from "@dxos/errors";
|
|
4
10
|
import { invariant } from "@dxos/invariant";
|
|
5
11
|
import { SpaceId } from "@dxos/keys";
|
|
6
12
|
import { log } from "@dxos/log";
|
|
7
|
-
|
|
13
|
+
|
|
14
|
+
// src/internal/utils.ts
|
|
15
|
+
var copyUint8Array = (value) => new Uint8Array(value);
|
|
16
|
+
|
|
17
|
+
// src/internal/data-service-impl.ts
|
|
18
|
+
var __dxlog_file = "/__w/dxos/dxos/packages/core/compute/functions-runtime-cloudflare/src/internal/data-service-impl.ts";
|
|
19
|
+
function _ts_add_disposable_resource(env, value, async) {
|
|
20
|
+
if (value !== null && value !== void 0) {
|
|
21
|
+
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
|
|
22
|
+
var dispose, inner;
|
|
23
|
+
if (async) {
|
|
24
|
+
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
|
|
25
|
+
dispose = value[Symbol.asyncDispose];
|
|
26
|
+
}
|
|
27
|
+
if (dispose === void 0) {
|
|
28
|
+
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
|
|
29
|
+
dispose = value[Symbol.dispose];
|
|
30
|
+
if (async) inner = dispose;
|
|
31
|
+
}
|
|
32
|
+
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
|
|
33
|
+
if (inner) dispose = function() {
|
|
34
|
+
try {
|
|
35
|
+
inner.call(this);
|
|
36
|
+
} catch (e) {
|
|
37
|
+
return Promise.reject(e);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
env.stack.push({
|
|
41
|
+
value,
|
|
42
|
+
dispose,
|
|
43
|
+
async
|
|
44
|
+
});
|
|
45
|
+
} else if (async) {
|
|
46
|
+
env.stack.push({
|
|
47
|
+
async: true
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
52
|
+
function _ts_dispose_resources(env) {
|
|
53
|
+
var _SuppressedError = typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
|
|
54
|
+
var e = new Error(message);
|
|
55
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
56
|
+
};
|
|
57
|
+
return (_ts_dispose_resources = function _ts_dispose_resources5(env2) {
|
|
58
|
+
function fail(e) {
|
|
59
|
+
env2.error = env2.hasError ? new _SuppressedError(e, env2.error, "An error was suppressed during disposal.") : e;
|
|
60
|
+
env2.hasError = true;
|
|
61
|
+
}
|
|
62
|
+
var r, s = 0;
|
|
63
|
+
function next() {
|
|
64
|
+
while (r = env2.stack.pop()) {
|
|
65
|
+
try {
|
|
66
|
+
if (!r.async && s === 1) return s = 0, env2.stack.push(r), Promise.resolve().then(next);
|
|
67
|
+
if (r.dispose) {
|
|
68
|
+
var result = r.dispose.call(r.value);
|
|
69
|
+
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) {
|
|
70
|
+
fail(e);
|
|
71
|
+
return next();
|
|
72
|
+
});
|
|
73
|
+
} else s |= 1;
|
|
74
|
+
} catch (e) {
|
|
75
|
+
fail(e);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (s === 1) return env2.hasError ? Promise.reject(env2.error) : Promise.resolve();
|
|
79
|
+
if (env2.hasError) throw env2.error;
|
|
80
|
+
}
|
|
81
|
+
return next();
|
|
82
|
+
})(env);
|
|
83
|
+
}
|
|
8
84
|
var DataServiceImpl = class {
|
|
9
85
|
_executionContext;
|
|
10
86
|
_dataService;
|
|
@@ -15,15 +91,7 @@ var DataServiceImpl = class {
|
|
|
15
91
|
}
|
|
16
92
|
subscribe({ subscriptionId, spaceId }) {
|
|
17
93
|
return new Stream(({ next }) => {
|
|
18
|
-
invariant(SpaceId.isValid(spaceId), void 0, {
|
|
19
|
-
F: __dxlog_file,
|
|
20
|
-
L: 34,
|
|
21
|
-
S: this,
|
|
22
|
-
A: [
|
|
23
|
-
"SpaceId.isValid(spaceId)",
|
|
24
|
-
""
|
|
25
|
-
]
|
|
26
|
-
});
|
|
94
|
+
invariant(SpaceId.isValid(spaceId), void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 86, S: this, A: ["SpaceId.isValid(spaceId)", ""] });
|
|
27
95
|
this.dataSubscriptions.set(subscriptionId, {
|
|
28
96
|
spaceId,
|
|
29
97
|
next
|
|
@@ -33,158 +101,199 @@ var DataServiceImpl = class {
|
|
|
33
101
|
};
|
|
34
102
|
});
|
|
35
103
|
}
|
|
36
|
-
async updateSubscription({ subscriptionId, addIds
|
|
37
|
-
const sub = this.dataSubscriptions.get(subscriptionId) ?? raise(new
|
|
104
|
+
async updateSubscription({ subscriptionId, addIds }) {
|
|
105
|
+
const sub = this.dataSubscriptions.get(subscriptionId) ?? raise(new RuntimeServiceError({
|
|
106
|
+
message: "Subscription not found.",
|
|
107
|
+
context: {
|
|
108
|
+
subscriptionId
|
|
109
|
+
}
|
|
110
|
+
}));
|
|
38
111
|
if (addIds) {
|
|
39
112
|
log.info("request documents", {
|
|
40
113
|
count: addIds.length
|
|
41
|
-
}, {
|
|
42
|
-
F: __dxlog_file,
|
|
43
|
-
L: 47,
|
|
44
|
-
S: this,
|
|
45
|
-
C: (f, a) => f(...a)
|
|
46
|
-
});
|
|
114
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 104, S: this });
|
|
47
115
|
for (const documentId of addIds) {
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
F: __dxlog_file,
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
116
|
+
const env = {
|
|
117
|
+
stack: [],
|
|
118
|
+
error: void 0,
|
|
119
|
+
hasError: false
|
|
120
|
+
};
|
|
121
|
+
try {
|
|
122
|
+
const document = _ts_add_disposable_resource(env, await this._dataService.getDocument(this._executionContext, sub.spaceId, documentId), false);
|
|
123
|
+
log.info("document loaded", {
|
|
124
|
+
documentId,
|
|
125
|
+
spaceId: sub.spaceId,
|
|
126
|
+
found: !!document
|
|
127
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 116, S: this });
|
|
128
|
+
if (!document) {
|
|
129
|
+
log.warn("not found", {
|
|
130
|
+
documentId
|
|
131
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 122, S: this });
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
sub.next({
|
|
135
|
+
updates: [
|
|
136
|
+
{
|
|
137
|
+
documentId,
|
|
138
|
+
// Copy returned object to avoid hanging RPC stub
|
|
139
|
+
// See https://developers.cloudflare.com/workers/runtime-apis/rpc/lifecycle/
|
|
140
|
+
mutation: copyUint8Array(document.data)
|
|
141
|
+
}
|
|
142
|
+
]
|
|
67
143
|
});
|
|
68
|
-
|
|
144
|
+
} catch (e) {
|
|
145
|
+
env.error = e;
|
|
146
|
+
env.hasError = true;
|
|
147
|
+
} finally {
|
|
148
|
+
_ts_dispose_resources(env);
|
|
69
149
|
}
|
|
70
|
-
sub.next({
|
|
71
|
-
updates: [
|
|
72
|
-
{
|
|
73
|
-
documentId,
|
|
74
|
-
mutation: document.data
|
|
75
|
-
}
|
|
76
|
-
]
|
|
77
|
-
});
|
|
78
150
|
}
|
|
79
151
|
}
|
|
80
152
|
}
|
|
153
|
+
async createDocument({ spaceId, initialValue }) {
|
|
154
|
+
const env = {
|
|
155
|
+
stack: [],
|
|
156
|
+
error: void 0,
|
|
157
|
+
hasError: false
|
|
158
|
+
};
|
|
159
|
+
try {
|
|
160
|
+
invariant(SpaceId.isValid(spaceId), void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 153, S: this, A: ["SpaceId.isValid(spaceId)", ""] });
|
|
161
|
+
const response = _ts_add_disposable_resource(env, await this._dataService.createDocument(this._executionContext, spaceId, initialValue), false);
|
|
162
|
+
return {
|
|
163
|
+
documentId: response.documentId
|
|
164
|
+
};
|
|
165
|
+
} catch (e) {
|
|
166
|
+
env.error = e;
|
|
167
|
+
env.hasError = true;
|
|
168
|
+
} finally {
|
|
169
|
+
_ts_dispose_resources(env);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
81
172
|
async update({ updates, subscriptionId }) {
|
|
82
|
-
const sub = this.dataSubscriptions.get(subscriptionId) ?? raise(new
|
|
83
|
-
|
|
84
|
-
|
|
173
|
+
const sub = this.dataSubscriptions.get(subscriptionId) ?? raise(new RuntimeServiceError({
|
|
174
|
+
message: "Subscription not found.",
|
|
175
|
+
context: {
|
|
176
|
+
subscriptionId
|
|
177
|
+
}
|
|
178
|
+
}));
|
|
179
|
+
try {
|
|
180
|
+
for (const update of updates ?? []) {
|
|
181
|
+
if (!update.mutation) {
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
await this._dataService.changeDocument(this._executionContext, sub.spaceId, update.documentId, update.mutation);
|
|
185
|
+
}
|
|
186
|
+
} catch (error) {
|
|
187
|
+
throw RuntimeServiceError.wrap({
|
|
188
|
+
message: "Failed to apply document updates.",
|
|
189
|
+
context: {
|
|
190
|
+
subscriptionId
|
|
191
|
+
},
|
|
192
|
+
ifTypeDiffers: true
|
|
193
|
+
})(error);
|
|
85
194
|
}
|
|
86
|
-
throw new Error("Method not implemented.");
|
|
87
195
|
}
|
|
88
196
|
async flush() {
|
|
89
197
|
}
|
|
90
|
-
subscribeSpaceSyncState(
|
|
91
|
-
throw new
|
|
198
|
+
subscribeSpaceSyncState(_request, _options) {
|
|
199
|
+
throw new NotImplementedError({
|
|
200
|
+
message: "subscribeSpaceSyncState is not implemented."
|
|
201
|
+
});
|
|
92
202
|
}
|
|
93
|
-
async getDocumentHeads({ documentIds }) {
|
|
94
|
-
throw new
|
|
203
|
+
async getDocumentHeads({ documentIds: _documentIds }) {
|
|
204
|
+
throw new NotImplementedError({
|
|
205
|
+
message: "getDocumentHeads is not implemented."
|
|
206
|
+
});
|
|
95
207
|
}
|
|
96
|
-
async reIndexHeads({ documentIds }) {
|
|
97
|
-
throw new
|
|
208
|
+
async reIndexHeads({ documentIds: _documentIds }) {
|
|
209
|
+
throw new NotImplementedError({
|
|
210
|
+
message: "reIndexHeads is not implemented."
|
|
211
|
+
});
|
|
98
212
|
}
|
|
99
213
|
async updateIndexes() {
|
|
100
|
-
|
|
214
|
+
log.verbose("updateIndexes called, but it is a no-op in EDGE env.", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 211, S: this });
|
|
101
215
|
}
|
|
102
|
-
async waitUntilHeadsReplicated({ heads }) {
|
|
103
|
-
throw new
|
|
216
|
+
async waitUntilHeadsReplicated({ heads: _heads }) {
|
|
217
|
+
throw new NotImplementedError({
|
|
218
|
+
message: "waitUntilHeadsReplicated is not implemented."
|
|
219
|
+
});
|
|
104
220
|
}
|
|
105
221
|
};
|
|
106
222
|
|
|
107
223
|
// src/internal/query-service-impl.ts
|
|
108
|
-
import * as Schema from "effect/Schema";
|
|
109
224
|
import { Stream as Stream2 } from "@dxos/codec-protobuf/stream";
|
|
110
|
-
import {
|
|
111
|
-
import { invariant as invariant3 } from "@dxos/invariant";
|
|
112
|
-
import { PublicKey } from "@dxos/keys";
|
|
113
|
-
import { SpaceId as SpaceId3 } from "@dxos/keys";
|
|
225
|
+
import { NotImplementedError as NotImplementedError2, RuntimeServiceError as RuntimeServiceError2 } from "@dxos/errors";
|
|
114
226
|
import { log as log2 } from "@dxos/log";
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
invariant2(options?.spaceIds?.length === 1, "Only one space is supported", {
|
|
124
|
-
F: __dxlog_file2,
|
|
125
|
-
L: 13,
|
|
126
|
-
S: void 0,
|
|
127
|
-
A: [
|
|
128
|
-
"options?.spaceIds?.length === 1",
|
|
129
|
-
"'Only one space is supported'"
|
|
130
|
-
]
|
|
131
|
-
});
|
|
132
|
-
invariant2(filter.type === "object", "Only object filters are supported", {
|
|
133
|
-
F: __dxlog_file2,
|
|
134
|
-
L: 14,
|
|
135
|
-
S: void 0,
|
|
136
|
-
A: [
|
|
137
|
-
"filter.type === 'object'",
|
|
138
|
-
"'Only object filters are supported'"
|
|
139
|
-
]
|
|
140
|
-
});
|
|
141
|
-
const spaceId = options.spaceIds[0];
|
|
142
|
-
invariant2(SpaceId2.isValid(spaceId), void 0, {
|
|
143
|
-
F: __dxlog_file2,
|
|
144
|
-
L: 17,
|
|
145
|
-
S: void 0,
|
|
146
|
-
A: [
|
|
147
|
-
"SpaceId.isValid(spaceId)",
|
|
148
|
-
""
|
|
149
|
-
]
|
|
150
|
-
});
|
|
151
|
-
return {
|
|
152
|
-
spaceId,
|
|
153
|
-
type: filter.typename ?? void 0,
|
|
154
|
-
objectIds: [
|
|
155
|
-
...filter.id ?? []
|
|
156
|
-
]
|
|
157
|
-
};
|
|
158
|
-
};
|
|
159
|
-
var isSimpleSelectionQuery = (query) => {
|
|
160
|
-
switch (query.type) {
|
|
161
|
-
case "options": {
|
|
162
|
-
const maybeFilter = isSimpleSelectionQuery(query.query);
|
|
163
|
-
if (!maybeFilter) {
|
|
164
|
-
return null;
|
|
165
|
-
}
|
|
166
|
-
return {
|
|
167
|
-
filter: maybeFilter.filter,
|
|
168
|
-
options: query.options
|
|
169
|
-
};
|
|
227
|
+
var __dxlog_file2 = "/__w/dxos/dxos/packages/core/compute/functions-runtime-cloudflare/src/internal/query-service-impl.ts";
|
|
228
|
+
function _ts_add_disposable_resource2(env, value, async) {
|
|
229
|
+
if (value !== null && value !== void 0) {
|
|
230
|
+
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
|
|
231
|
+
var dispose, inner;
|
|
232
|
+
if (async) {
|
|
233
|
+
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
|
|
234
|
+
dispose = value[Symbol.asyncDispose];
|
|
170
235
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
default: {
|
|
178
|
-
return null;
|
|
236
|
+
if (dispose === void 0) {
|
|
237
|
+
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
|
|
238
|
+
dispose = value[Symbol.dispose];
|
|
239
|
+
if (async) inner = dispose;
|
|
179
240
|
}
|
|
241
|
+
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
|
|
242
|
+
if (inner) dispose = function() {
|
|
243
|
+
try {
|
|
244
|
+
inner.call(this);
|
|
245
|
+
} catch (e) {
|
|
246
|
+
return Promise.reject(e);
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
env.stack.push({
|
|
250
|
+
value,
|
|
251
|
+
dispose,
|
|
252
|
+
async
|
|
253
|
+
});
|
|
254
|
+
} else if (async) {
|
|
255
|
+
env.stack.push({
|
|
256
|
+
async: true
|
|
257
|
+
});
|
|
180
258
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
var
|
|
259
|
+
return value;
|
|
260
|
+
}
|
|
261
|
+
function _ts_dispose_resources2(env) {
|
|
262
|
+
var _SuppressedError = typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
|
|
263
|
+
var e = new Error(message);
|
|
264
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
265
|
+
};
|
|
266
|
+
return (_ts_dispose_resources2 = function _ts_dispose_resources5(env2) {
|
|
267
|
+
function fail(e) {
|
|
268
|
+
env2.error = env2.hasError ? new _SuppressedError(e, env2.error, "An error was suppressed during disposal.") : e;
|
|
269
|
+
env2.hasError = true;
|
|
270
|
+
}
|
|
271
|
+
var r, s = 0;
|
|
272
|
+
function next() {
|
|
273
|
+
while (r = env2.stack.pop()) {
|
|
274
|
+
try {
|
|
275
|
+
if (!r.async && s === 1) return s = 0, env2.stack.push(r), Promise.resolve().then(next);
|
|
276
|
+
if (r.dispose) {
|
|
277
|
+
var result = r.dispose.call(r.value);
|
|
278
|
+
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) {
|
|
279
|
+
fail(e);
|
|
280
|
+
return next();
|
|
281
|
+
});
|
|
282
|
+
} else s |= 1;
|
|
283
|
+
} catch (e) {
|
|
284
|
+
fail(e);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
if (s === 1) return env2.hasError ? Promise.reject(env2.error) : Promise.resolve();
|
|
288
|
+
if (env2.hasError) throw env2.error;
|
|
289
|
+
}
|
|
290
|
+
return next();
|
|
291
|
+
})(env);
|
|
292
|
+
}
|
|
185
293
|
var QueryServiceImpl = class {
|
|
186
294
|
_executionContext;
|
|
187
295
|
_dataService;
|
|
296
|
+
_queryCount = 0;
|
|
188
297
|
constructor(_executionContext, _dataService) {
|
|
189
298
|
this._executionContext = _executionContext;
|
|
190
299
|
this._dataService = _dataService;
|
|
@@ -192,93 +301,123 @@ var QueryServiceImpl = class {
|
|
|
192
301
|
execQuery(request) {
|
|
193
302
|
log2.info("execQuery", {
|
|
194
303
|
request
|
|
195
|
-
}, {
|
|
196
|
-
F: __dxlog_file3,
|
|
197
|
-
L: 30,
|
|
198
|
-
S: this,
|
|
199
|
-
C: (f, a) => f(...a)
|
|
200
|
-
});
|
|
201
|
-
const query = QueryAST.Query.pipe(Schema.decodeUnknownSync)(JSON.parse(request.query));
|
|
202
|
-
const requestedSpaceIds = getTargetSpacesForQuery(query);
|
|
203
|
-
invariant3(requestedSpaceIds.length === 1, "Only one space is supported", {
|
|
204
|
-
F: __dxlog_file3,
|
|
205
|
-
L: 33,
|
|
206
|
-
S: this,
|
|
207
|
-
A: [
|
|
208
|
-
"requestedSpaceIds.length === 1",
|
|
209
|
-
"'Only one space is supported'"
|
|
210
|
-
]
|
|
211
|
-
});
|
|
212
|
-
const spaceId = requestedSpaceIds[0];
|
|
304
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 81, S: this });
|
|
213
305
|
return Stream2.fromPromise((async () => {
|
|
214
306
|
try {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
L: 39,
|
|
220
|
-
S: this,
|
|
221
|
-
C: (f, a) => f(...a)
|
|
222
|
-
});
|
|
223
|
-
const queryResponse = await this._dataService.queryDocuments(this._executionContext, queryToDataServiceRequest(query));
|
|
224
|
-
log2.info("query response", {
|
|
225
|
-
spaceId,
|
|
226
|
-
filter: request.filter,
|
|
227
|
-
resultCount: queryResponse.results.length
|
|
228
|
-
}, {
|
|
229
|
-
F: __dxlog_file3,
|
|
230
|
-
L: 44,
|
|
231
|
-
S: this,
|
|
232
|
-
C: (f, a) => f(...a)
|
|
233
|
-
});
|
|
234
|
-
return {
|
|
235
|
-
results: queryResponse.results.map((object) => ({
|
|
236
|
-
id: object.objectId,
|
|
237
|
-
spaceId,
|
|
238
|
-
spaceKey: PublicKey.ZERO,
|
|
239
|
-
documentId: object.document.documentId,
|
|
240
|
-
rank: 0,
|
|
241
|
-
documentAutomerge: object.document.data
|
|
242
|
-
}))
|
|
307
|
+
const env = {
|
|
308
|
+
stack: [],
|
|
309
|
+
error: void 0,
|
|
310
|
+
hasError: false
|
|
243
311
|
};
|
|
244
|
-
|
|
312
|
+
try {
|
|
313
|
+
this._queryCount++;
|
|
314
|
+
log2.info("begin query", {
|
|
315
|
+
request
|
|
316
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 93, S: this });
|
|
317
|
+
const queryResponse = _ts_add_disposable_resource2(env, await this._dataService.execQuery(this._executionContext, request), false);
|
|
318
|
+
log2.info("query response", {
|
|
319
|
+
resultCount: queryResponse.results?.length
|
|
320
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 97, S: this });
|
|
321
|
+
return structuredClone(queryResponse);
|
|
322
|
+
} catch (e) {
|
|
323
|
+
env.error = e;
|
|
324
|
+
env.hasError = true;
|
|
325
|
+
} finally {
|
|
326
|
+
_ts_dispose_resources2(env);
|
|
327
|
+
}
|
|
328
|
+
} catch (error) {
|
|
245
329
|
log2.error("query failed", {
|
|
246
|
-
err
|
|
247
|
-
}, {
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
330
|
+
err: error
|
|
331
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 108, S: this });
|
|
332
|
+
throw new RuntimeServiceError2({
|
|
333
|
+
message: `Query execution failed (queryCount=${this._queryCount})`,
|
|
334
|
+
context: {
|
|
335
|
+
queryCount: this._queryCount
|
|
336
|
+
},
|
|
337
|
+
cause: error
|
|
252
338
|
});
|
|
253
|
-
throw err;
|
|
254
339
|
}
|
|
255
340
|
})());
|
|
256
341
|
}
|
|
257
342
|
async reindex() {
|
|
258
|
-
throw new
|
|
343
|
+
throw new NotImplementedError2({
|
|
344
|
+
message: "Reindex is not implemented."
|
|
345
|
+
});
|
|
259
346
|
}
|
|
260
347
|
async setConfig() {
|
|
261
|
-
throw new
|
|
348
|
+
throw new NotImplementedError2({
|
|
349
|
+
message: "SetConfig is not implemented."
|
|
350
|
+
});
|
|
262
351
|
}
|
|
263
352
|
};
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
353
|
+
|
|
354
|
+
// src/internal/queue-service-impl.ts
|
|
355
|
+
import { RuntimeServiceError as RuntimeServiceError3 } from "@dxos/errors";
|
|
356
|
+
function _ts_add_disposable_resource3(env, value, async) {
|
|
357
|
+
if (value !== null && value !== void 0) {
|
|
358
|
+
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
|
|
359
|
+
var dispose, inner;
|
|
360
|
+
if (async) {
|
|
361
|
+
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
|
|
362
|
+
dispose = value[Symbol.asyncDispose];
|
|
363
|
+
}
|
|
364
|
+
if (dispose === void 0) {
|
|
365
|
+
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
|
|
366
|
+
dispose = value[Symbol.dispose];
|
|
367
|
+
if (async) inner = dispose;
|
|
368
|
+
}
|
|
369
|
+
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
|
|
370
|
+
if (inner) dispose = function() {
|
|
371
|
+
try {
|
|
372
|
+
inner.call(this);
|
|
373
|
+
} catch (e) {
|
|
374
|
+
return Promise.reject(e);
|
|
375
|
+
}
|
|
376
|
+
};
|
|
377
|
+
env.stack.push({
|
|
378
|
+
value,
|
|
379
|
+
dispose,
|
|
380
|
+
async
|
|
381
|
+
});
|
|
382
|
+
} else if (async) {
|
|
383
|
+
env.stack.push({
|
|
384
|
+
async: true
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
return value;
|
|
388
|
+
}
|
|
389
|
+
function _ts_dispose_resources3(env) {
|
|
390
|
+
var _SuppressedError = typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
|
|
391
|
+
var e = new Error(message);
|
|
392
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
393
|
+
};
|
|
394
|
+
return (_ts_dispose_resources3 = function _ts_dispose_resources5(env2) {
|
|
395
|
+
function fail(e) {
|
|
396
|
+
env2.error = env2.hasError ? new _SuppressedError(e, env2.error, "An error was suppressed during disposal.") : e;
|
|
397
|
+
env2.hasError = true;
|
|
398
|
+
}
|
|
399
|
+
var r, s = 0;
|
|
400
|
+
function next() {
|
|
401
|
+
while (r = env2.stack.pop()) {
|
|
402
|
+
try {
|
|
403
|
+
if (!r.async && s === 1) return s = 0, env2.stack.push(r), Promise.resolve().then(next);
|
|
404
|
+
if (r.dispose) {
|
|
405
|
+
var result = r.dispose.call(r.value);
|
|
406
|
+
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) {
|
|
407
|
+
fail(e);
|
|
408
|
+
return next();
|
|
409
|
+
});
|
|
410
|
+
} else s |= 1;
|
|
411
|
+
} catch (e) {
|
|
412
|
+
fail(e);
|
|
271
413
|
}
|
|
272
414
|
}
|
|
415
|
+
if (s === 1) return env2.hasError ? Promise.reject(env2.error) : Promise.resolve();
|
|
416
|
+
if (env2.hasError) throw env2.error;
|
|
273
417
|
}
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
...spaces
|
|
278
|
-
];
|
|
279
|
-
};
|
|
280
|
-
|
|
281
|
-
// src/internal/queue-service-impl.ts
|
|
418
|
+
return next();
|
|
419
|
+
})(env);
|
|
420
|
+
}
|
|
282
421
|
var QueueServiceImpl = class {
|
|
283
422
|
_ctx;
|
|
284
423
|
_queueService;
|
|
@@ -286,29 +425,194 @@ var QueueServiceImpl = class {
|
|
|
286
425
|
this._ctx = _ctx;
|
|
287
426
|
this._queueService = _queueService;
|
|
288
427
|
}
|
|
289
|
-
queryQueue(
|
|
290
|
-
|
|
428
|
+
async queryQueue(request) {
|
|
429
|
+
try {
|
|
430
|
+
const env = {
|
|
431
|
+
stack: [],
|
|
432
|
+
error: void 0,
|
|
433
|
+
hasError: false
|
|
434
|
+
};
|
|
435
|
+
try {
|
|
436
|
+
const result = _ts_add_disposable_resource3(env, await this._queueService.queryQueue(this._ctx, request), false);
|
|
437
|
+
return {
|
|
438
|
+
objects: structuredClone(result.objects),
|
|
439
|
+
nextCursor: result.nextCursor,
|
|
440
|
+
prevCursor: result.prevCursor
|
|
441
|
+
};
|
|
442
|
+
} catch (e) {
|
|
443
|
+
env.error = e;
|
|
444
|
+
env.hasError = true;
|
|
445
|
+
} finally {
|
|
446
|
+
_ts_dispose_resources3(env);
|
|
447
|
+
}
|
|
448
|
+
} catch (error) {
|
|
449
|
+
const { query } = request;
|
|
450
|
+
throw RuntimeServiceError3.wrap({
|
|
451
|
+
message: "Queue query failed.",
|
|
452
|
+
context: {
|
|
453
|
+
subspaceTag: query?.queuesNamespace,
|
|
454
|
+
spaceId: query?.spaceId,
|
|
455
|
+
queueId: query?.queueIds?.[0]
|
|
456
|
+
},
|
|
457
|
+
ifTypeDiffers: true
|
|
458
|
+
})(error);
|
|
459
|
+
}
|
|
291
460
|
}
|
|
292
|
-
insertIntoQueue(
|
|
293
|
-
|
|
461
|
+
async insertIntoQueue(request) {
|
|
462
|
+
try {
|
|
463
|
+
const env = {
|
|
464
|
+
stack: [],
|
|
465
|
+
error: void 0,
|
|
466
|
+
hasError: false
|
|
467
|
+
};
|
|
468
|
+
try {
|
|
469
|
+
const _ = _ts_add_disposable_resource3(env, await this._queueService.insertIntoQueue(this._ctx, request), false);
|
|
470
|
+
} catch (e) {
|
|
471
|
+
env.error = e;
|
|
472
|
+
env.hasError = true;
|
|
473
|
+
} finally {
|
|
474
|
+
_ts_dispose_resources3(env);
|
|
475
|
+
}
|
|
476
|
+
} catch (error) {
|
|
477
|
+
const { subspaceTag, spaceId, queueId } = request;
|
|
478
|
+
throw RuntimeServiceError3.wrap({
|
|
479
|
+
message: "Queue append failed.",
|
|
480
|
+
context: {
|
|
481
|
+
subspaceTag,
|
|
482
|
+
spaceId,
|
|
483
|
+
queueId
|
|
484
|
+
},
|
|
485
|
+
ifTypeDiffers: true
|
|
486
|
+
})(error);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
async deleteFromQueue(request) {
|
|
490
|
+
try {
|
|
491
|
+
const env = {
|
|
492
|
+
stack: [],
|
|
493
|
+
error: void 0,
|
|
494
|
+
hasError: false
|
|
495
|
+
};
|
|
496
|
+
try {
|
|
497
|
+
const _ = _ts_add_disposable_resource3(env, await this._queueService.deleteFromQueue(this._ctx, request), false);
|
|
498
|
+
} catch (e) {
|
|
499
|
+
env.error = e;
|
|
500
|
+
env.hasError = true;
|
|
501
|
+
} finally {
|
|
502
|
+
_ts_dispose_resources3(env);
|
|
503
|
+
}
|
|
504
|
+
} catch (error) {
|
|
505
|
+
const { subspaceTag, spaceId, queueId } = request;
|
|
506
|
+
throw RuntimeServiceError3.wrap({
|
|
507
|
+
message: "Queue delete failed.",
|
|
508
|
+
context: {
|
|
509
|
+
subspaceTag,
|
|
510
|
+
spaceId,
|
|
511
|
+
queueId
|
|
512
|
+
},
|
|
513
|
+
ifTypeDiffers: true
|
|
514
|
+
})(error);
|
|
515
|
+
}
|
|
294
516
|
}
|
|
295
|
-
|
|
296
|
-
throw new Error("Deleting from queue is not supported.");
|
|
517
|
+
async syncQueue(_) {
|
|
297
518
|
}
|
|
298
519
|
};
|
|
299
520
|
|
|
300
521
|
// src/internal/service-container.ts
|
|
522
|
+
function _ts_add_disposable_resource4(env, value, async) {
|
|
523
|
+
if (value !== null && value !== void 0) {
|
|
524
|
+
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
|
|
525
|
+
var dispose, inner;
|
|
526
|
+
if (async) {
|
|
527
|
+
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
|
|
528
|
+
dispose = value[Symbol.asyncDispose];
|
|
529
|
+
}
|
|
530
|
+
if (dispose === void 0) {
|
|
531
|
+
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
|
|
532
|
+
dispose = value[Symbol.dispose];
|
|
533
|
+
if (async) inner = dispose;
|
|
534
|
+
}
|
|
535
|
+
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
|
|
536
|
+
if (inner) dispose = function() {
|
|
537
|
+
try {
|
|
538
|
+
inner.call(this);
|
|
539
|
+
} catch (e) {
|
|
540
|
+
return Promise.reject(e);
|
|
541
|
+
}
|
|
542
|
+
};
|
|
543
|
+
env.stack.push({
|
|
544
|
+
value,
|
|
545
|
+
dispose,
|
|
546
|
+
async
|
|
547
|
+
});
|
|
548
|
+
} else if (async) {
|
|
549
|
+
env.stack.push({
|
|
550
|
+
async: true
|
|
551
|
+
});
|
|
552
|
+
}
|
|
553
|
+
return value;
|
|
554
|
+
}
|
|
555
|
+
function _ts_dispose_resources4(env) {
|
|
556
|
+
var _SuppressedError = typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
|
|
557
|
+
var e = new Error(message);
|
|
558
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
559
|
+
};
|
|
560
|
+
return (_ts_dispose_resources4 = function _ts_dispose_resources5(env2) {
|
|
561
|
+
function fail(e) {
|
|
562
|
+
env2.error = env2.hasError ? new _SuppressedError(e, env2.error, "An error was suppressed during disposal.") : e;
|
|
563
|
+
env2.hasError = true;
|
|
564
|
+
}
|
|
565
|
+
var r, s = 0;
|
|
566
|
+
function next() {
|
|
567
|
+
while (r = env2.stack.pop()) {
|
|
568
|
+
try {
|
|
569
|
+
if (!r.async && s === 1) return s = 0, env2.stack.push(r), Promise.resolve().then(next);
|
|
570
|
+
if (r.dispose) {
|
|
571
|
+
var result = r.dispose.call(r.value);
|
|
572
|
+
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) {
|
|
573
|
+
fail(e);
|
|
574
|
+
return next();
|
|
575
|
+
});
|
|
576
|
+
} else s |= 1;
|
|
577
|
+
} catch (e) {
|
|
578
|
+
fail(e);
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
if (s === 1) return env2.hasError ? Promise.reject(env2.error) : Promise.resolve();
|
|
582
|
+
if (env2.hasError) throw env2.error;
|
|
583
|
+
}
|
|
584
|
+
return next();
|
|
585
|
+
})(env);
|
|
586
|
+
}
|
|
301
587
|
var ServiceContainer = class {
|
|
302
588
|
_executionContext;
|
|
303
589
|
_dataService;
|
|
304
590
|
_queueService;
|
|
305
|
-
|
|
591
|
+
_functionsService;
|
|
592
|
+
constructor(_executionContext, _dataService, _queueService, _functionsService) {
|
|
306
593
|
this._executionContext = _executionContext;
|
|
307
594
|
this._dataService = _dataService;
|
|
308
595
|
this._queueService = _queueService;
|
|
596
|
+
this._functionsService = _functionsService;
|
|
309
597
|
}
|
|
310
598
|
async getSpaceMeta(spaceId) {
|
|
311
|
-
|
|
599
|
+
const env = {
|
|
600
|
+
stack: [],
|
|
601
|
+
error: void 0,
|
|
602
|
+
hasError: false
|
|
603
|
+
};
|
|
604
|
+
try {
|
|
605
|
+
const result = _ts_add_disposable_resource4(env, await this._dataService.getSpaceMeta(this._executionContext, spaceId), false);
|
|
606
|
+
return result ? {
|
|
607
|
+
spaceKey: result.spaceKey,
|
|
608
|
+
rootDocumentId: result.rootDocumentId
|
|
609
|
+
} : void 0;
|
|
610
|
+
} catch (e) {
|
|
611
|
+
env.error = e;
|
|
612
|
+
env.hasError = true;
|
|
613
|
+
} finally {
|
|
614
|
+
_ts_dispose_resources4(env);
|
|
615
|
+
}
|
|
312
616
|
}
|
|
313
617
|
async createServices() {
|
|
314
618
|
const dataService = new DataServiceImpl(this._executionContext, this._dataService);
|
|
@@ -317,17 +621,185 @@ var ServiceContainer = class {
|
|
|
317
621
|
return {
|
|
318
622
|
dataService,
|
|
319
623
|
queryService,
|
|
320
|
-
queueService
|
|
624
|
+
queueService,
|
|
625
|
+
functionsAiService: this._functionsService
|
|
626
|
+
};
|
|
627
|
+
}
|
|
628
|
+
async queryQueue(queue) {
|
|
629
|
+
const parts = queue.asQueueDXN();
|
|
630
|
+
if (!parts) {
|
|
631
|
+
throw new Error("Invalid queue DXN");
|
|
632
|
+
}
|
|
633
|
+
const { subspaceTag, spaceId, queueId } = parts;
|
|
634
|
+
const result = await this._queueService.queryQueue(this._executionContext, {
|
|
635
|
+
query: {
|
|
636
|
+
spaceId,
|
|
637
|
+
queuesNamespace: subspaceTag,
|
|
638
|
+
queueIds: [
|
|
639
|
+
queueId
|
|
640
|
+
]
|
|
641
|
+
}
|
|
642
|
+
});
|
|
643
|
+
return {
|
|
644
|
+
objects: structuredClone(result.objects),
|
|
645
|
+
nextCursor: result.nextCursor ?? null,
|
|
646
|
+
prevCursor: result.prevCursor ?? null
|
|
321
647
|
};
|
|
322
648
|
}
|
|
323
|
-
|
|
324
|
-
|
|
649
|
+
async insertIntoQueue(queue, objects) {
|
|
650
|
+
const parts = queue.asQueueDXN();
|
|
651
|
+
if (!parts) {
|
|
652
|
+
throw new Error("Invalid queue DXN");
|
|
653
|
+
}
|
|
654
|
+
const { subspaceTag, spaceId, queueId } = parts;
|
|
655
|
+
await this._queueService.insertIntoQueue(this._executionContext, {
|
|
656
|
+
subspaceTag,
|
|
657
|
+
spaceId,
|
|
658
|
+
queueId,
|
|
659
|
+
objects: objects.map((obj) => JSON.stringify(obj))
|
|
660
|
+
});
|
|
661
|
+
}
|
|
662
|
+
};
|
|
663
|
+
|
|
664
|
+
// src/space-proxy.ts
|
|
665
|
+
import { Resource } from "@dxos/context";
|
|
666
|
+
import { invariant as invariant2 } from "@dxos/invariant";
|
|
667
|
+
import { PublicKey } from "@dxos/keys";
|
|
668
|
+
|
|
669
|
+
// src/queues-api.ts
|
|
670
|
+
import { log as log3 } from "@dxos/log";
|
|
671
|
+
var __dxlog_file3 = "/__w/dxos/dxos/packages/core/compute/functions-runtime-cloudflare/src/queues-api.ts";
|
|
672
|
+
var QueuesAPIImpl = class {
|
|
673
|
+
_serviceContainer;
|
|
674
|
+
_spaceId;
|
|
675
|
+
constructor(_serviceContainer, _spaceId) {
|
|
676
|
+
this._serviceContainer = _serviceContainer;
|
|
677
|
+
this._spaceId = _spaceId;
|
|
678
|
+
}
|
|
679
|
+
async queryQueue(queue, options) {
|
|
680
|
+
const result = await this._serviceContainer.queryQueue(queue);
|
|
681
|
+
const objects = (result.objects ?? []).flatMap((encoded) => {
|
|
682
|
+
try {
|
|
683
|
+
return [
|
|
684
|
+
JSON.parse(encoded)
|
|
685
|
+
];
|
|
686
|
+
} catch (err) {
|
|
687
|
+
log3.verbose("queue object JSON parse failed; object ignored", {
|
|
688
|
+
encoded,
|
|
689
|
+
error: err
|
|
690
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 22, S: this });
|
|
691
|
+
return [];
|
|
692
|
+
}
|
|
693
|
+
});
|
|
694
|
+
return {
|
|
695
|
+
objects,
|
|
696
|
+
nextCursor: result.nextCursor ?? null,
|
|
697
|
+
prevCursor: result.prevCursor ?? null
|
|
698
|
+
};
|
|
325
699
|
}
|
|
326
700
|
insertIntoQueue(queue, objects) {
|
|
327
|
-
return this.
|
|
701
|
+
return this._serviceContainer.insertIntoQueue(queue, JSON.parse(JSON.stringify(objects)));
|
|
702
|
+
}
|
|
703
|
+
};
|
|
704
|
+
|
|
705
|
+
// src/space-proxy.ts
|
|
706
|
+
var __dxlog_file4 = "/__w/dxos/dxos/packages/core/compute/functions-runtime-cloudflare/src/space-proxy.ts";
|
|
707
|
+
var SpaceProxy = class extends Resource {
|
|
708
|
+
_serviceContainer;
|
|
709
|
+
_echoClient;
|
|
710
|
+
_id;
|
|
711
|
+
_db = void 0;
|
|
712
|
+
_queuesApi;
|
|
713
|
+
constructor(_serviceContainer, _echoClient, _id) {
|
|
714
|
+
super(), this._serviceContainer = _serviceContainer, this._echoClient = _echoClient, this._id = _id;
|
|
715
|
+
this._queuesApi = new QueuesAPIImpl(this._serviceContainer, this._id);
|
|
716
|
+
}
|
|
717
|
+
get id() {
|
|
718
|
+
return this._id;
|
|
719
|
+
}
|
|
720
|
+
get db() {
|
|
721
|
+
invariant2(this._db, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 24, S: this, A: ["this._db", ""] });
|
|
722
|
+
return this._db;
|
|
723
|
+
}
|
|
724
|
+
/**
|
|
725
|
+
* @deprecated Use db API.
|
|
726
|
+
*/
|
|
727
|
+
get crud() {
|
|
728
|
+
invariant2(this._db, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 30, S: this, A: ["this._db", ""] });
|
|
729
|
+
return this._db.coreDatabase;
|
|
730
|
+
}
|
|
731
|
+
get queues() {
|
|
732
|
+
return this._queuesApi;
|
|
733
|
+
}
|
|
734
|
+
async _open() {
|
|
735
|
+
const meta = await this._serviceContainer.getSpaceMeta(this._id);
|
|
736
|
+
if (!meta) {
|
|
737
|
+
throw new Error(`Space not found: ${this._id}`);
|
|
738
|
+
}
|
|
739
|
+
this._db = this._echoClient.constructDatabase({
|
|
740
|
+
spaceId: this._id,
|
|
741
|
+
spaceKey: PublicKey.from(meta.spaceKey),
|
|
742
|
+
reactiveSchemaQuery: false,
|
|
743
|
+
owningObject: this
|
|
744
|
+
});
|
|
745
|
+
await this._db.coreDatabase.open(this._ctx, {
|
|
746
|
+
rootUrl: meta.rootDocumentId
|
|
747
|
+
});
|
|
328
748
|
}
|
|
329
749
|
};
|
|
330
750
|
|
|
751
|
+
// src/functions-client.ts
|
|
752
|
+
var __dxlog_file5 = "/__w/dxos/dxos/packages/core/compute/functions-runtime-cloudflare/src/functions-client.ts";
|
|
753
|
+
var FunctionsClient = class extends Resource2 {
|
|
754
|
+
_serviceContainer;
|
|
755
|
+
_echoClient;
|
|
756
|
+
_executionContext = {};
|
|
757
|
+
_spaces = /* @__PURE__ */ new Map();
|
|
758
|
+
constructor(services) {
|
|
759
|
+
super();
|
|
760
|
+
invariant3(typeof services.dataService !== "undefined", "DataService is required", { "~LogMeta": "~LogMeta", F: __dxlog_file5, L: 19, S: this, A: ["typeof services.dataService !== 'undefined'", "'DataService is required'"] });
|
|
761
|
+
invariant3(typeof services.queueService !== "undefined", "QueueService is required", { "~LogMeta": "~LogMeta", F: __dxlog_file5, L: 20, S: this, A: ["typeof services.queueService !== 'undefined'", "'QueueService is required'"] });
|
|
762
|
+
this._serviceContainer = new ServiceContainer(this._executionContext, services.dataService, services.queueService, services.functionsAiService);
|
|
763
|
+
this._echoClient = new EchoClient({});
|
|
764
|
+
}
|
|
765
|
+
get echo() {
|
|
766
|
+
return this._echoClient;
|
|
767
|
+
}
|
|
768
|
+
async _open() {
|
|
769
|
+
const { dataService, queryService } = await this._serviceContainer.createServices();
|
|
770
|
+
this._echoClient.connectToService({
|
|
771
|
+
dataService,
|
|
772
|
+
queryService
|
|
773
|
+
});
|
|
774
|
+
await this._echoClient.open();
|
|
775
|
+
}
|
|
776
|
+
async _close() {
|
|
777
|
+
for (const space of this._spaces.values()) {
|
|
778
|
+
await space.close();
|
|
779
|
+
}
|
|
780
|
+
this._spaces.clear();
|
|
781
|
+
await this._echoClient.close();
|
|
782
|
+
}
|
|
783
|
+
async getSpace(spaceId) {
|
|
784
|
+
if (!this._spaces.has(spaceId)) {
|
|
785
|
+
const space2 = new SpaceProxy(this._serviceContainer, this._echoClient, spaceId);
|
|
786
|
+
this._spaces.set(spaceId, space2);
|
|
787
|
+
}
|
|
788
|
+
const space = this._spaces.get(spaceId);
|
|
789
|
+
await space.open();
|
|
790
|
+
return space;
|
|
791
|
+
}
|
|
792
|
+
};
|
|
793
|
+
var createClientFromEnv = async (env) => {
|
|
794
|
+
const client = new FunctionsClient({
|
|
795
|
+
dataService: env.DATA_SERVICE,
|
|
796
|
+
queueService: env.QUEUE_SERVICE,
|
|
797
|
+
functionsAiService: env.FUNCTIONS_AI_SERVICE
|
|
798
|
+
});
|
|
799
|
+
await client.open();
|
|
800
|
+
return client;
|
|
801
|
+
};
|
|
802
|
+
|
|
331
803
|
// src/types.ts
|
|
332
804
|
var FUNCTION_ROUTE_HEADER = "X-DXOS-Function-Route";
|
|
333
805
|
var FunctionRouteValue = /* @__PURE__ */ (function(FunctionRouteValue2) {
|
|
@@ -337,48 +809,35 @@ var FunctionRouteValue = /* @__PURE__ */ (function(FunctionRouteValue2) {
|
|
|
337
809
|
|
|
338
810
|
// src/wrap-handler-for-cloudflare.ts
|
|
339
811
|
import { invariant as invariant4 } from "@dxos/invariant";
|
|
340
|
-
import { SpaceId as
|
|
341
|
-
import { log as
|
|
812
|
+
import { SpaceId as SpaceId2 } from "@dxos/keys";
|
|
813
|
+
import { log as log4 } from "@dxos/log";
|
|
342
814
|
import { EdgeResponse } from "@dxos/protocols";
|
|
343
|
-
var
|
|
815
|
+
var __dxlog_file6 = "/__w/dxos/dxos/packages/core/compute/functions-runtime-cloudflare/src/wrap-handler-for-cloudflare.ts";
|
|
344
816
|
var wrapHandlerForCloudflare = (func) => {
|
|
345
817
|
return async (request, env) => {
|
|
346
818
|
if (request.headers.get(FUNCTION_ROUTE_HEADER) === FunctionRouteValue.Meta) {
|
|
347
|
-
log3.info(">>> meta", {
|
|
348
|
-
func
|
|
349
|
-
}, {
|
|
350
|
-
F: __dxlog_file4,
|
|
351
|
-
L: 25,
|
|
352
|
-
S: void 0,
|
|
353
|
-
C: (f, a) => f(...a)
|
|
354
|
-
});
|
|
355
819
|
return handleFunctionMetaCall(func, request);
|
|
356
820
|
}
|
|
357
821
|
try {
|
|
358
822
|
const spaceId = new URL(request.url).searchParams.get("spaceId");
|
|
359
823
|
if (spaceId) {
|
|
360
|
-
if (!
|
|
824
|
+
if (!SpaceId2.isValid(spaceId)) {
|
|
361
825
|
return new Response("Invalid spaceId", {
|
|
362
826
|
status: 400
|
|
363
827
|
});
|
|
364
828
|
}
|
|
365
829
|
}
|
|
366
|
-
const serviceContainer = new ServiceContainer({}, env.DATA_SERVICE, env.QUEUE_SERVICE);
|
|
830
|
+
const serviceContainer = new ServiceContainer({}, env.DATA_SERVICE, env.QUEUE_SERVICE, env.FUNCTIONS_AI_SERVICE);
|
|
367
831
|
const context = await createFunctionContext({
|
|
368
832
|
serviceContainer,
|
|
369
833
|
contextSpaceId: spaceId
|
|
370
834
|
});
|
|
371
835
|
return EdgeResponse.success(await invokeFunction(func, context, request));
|
|
372
836
|
} catch (error) {
|
|
373
|
-
|
|
837
|
+
log4.error("error invoking function", {
|
|
374
838
|
error,
|
|
375
839
|
stack: error.stack
|
|
376
|
-
}, {
|
|
377
|
-
F: __dxlog_file4,
|
|
378
|
-
L: 45,
|
|
379
|
-
S: void 0,
|
|
380
|
-
C: (f, a) => f(...a)
|
|
381
|
-
});
|
|
840
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file6, L: 37, S: void 0 });
|
|
382
841
|
return EdgeResponse.failure({
|
|
383
842
|
message: error?.message ?? "Internal error",
|
|
384
843
|
error
|
|
@@ -411,12 +870,7 @@ var decodeRequest = async (request) => {
|
|
|
411
870
|
}
|
|
412
871
|
};
|
|
413
872
|
} catch (err) {
|
|
414
|
-
|
|
415
|
-
F: __dxlog_file4,
|
|
416
|
-
L: 80,
|
|
417
|
-
S: void 0,
|
|
418
|
-
C: (f, a) => f(...a)
|
|
419
|
-
});
|
|
873
|
+
log4.catch(err, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file6, L: 76, S: void 0 });
|
|
420
874
|
return {
|
|
421
875
|
data: {
|
|
422
876
|
bodyText,
|
|
@@ -440,7 +894,7 @@ var handleFunctionMetaCall = (functionDefinition, request) => {
|
|
|
440
894
|
});
|
|
441
895
|
};
|
|
442
896
|
var createFunctionContext = async ({ serviceContainer, contextSpaceId }) => {
|
|
443
|
-
const { dataService, queryService, queueService } = await serviceContainer.createServices();
|
|
897
|
+
const { dataService, queryService, queueService, functionsAiService } = await serviceContainer.createServices();
|
|
444
898
|
let spaceKey;
|
|
445
899
|
let rootUrl;
|
|
446
900
|
if (contextSpaceId) {
|
|
@@ -449,32 +903,68 @@ var createFunctionContext = async ({ serviceContainer, contextSpaceId }) => {
|
|
|
449
903
|
throw new Error(`Space not found: ${contextSpaceId}`);
|
|
450
904
|
}
|
|
451
905
|
spaceKey = meta.spaceKey;
|
|
452
|
-
invariant4(!meta.rootDocumentId.startsWith("automerge:"), void 0, {
|
|
453
|
-
F: __dxlog_file4,
|
|
454
|
-
L: 118,
|
|
455
|
-
S: void 0,
|
|
456
|
-
A: [
|
|
457
|
-
"!meta.rootDocumentId.startsWith('automerge:')",
|
|
458
|
-
""
|
|
459
|
-
]
|
|
460
|
-
});
|
|
906
|
+
invariant4(!meta.rootDocumentId.startsWith("automerge:"), void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file6, L: 109, S: void 0, A: ["!meta.rootDocumentId.startsWith('automerge:')", ""] });
|
|
461
907
|
rootUrl = `automerge:${meta.rootDocumentId}`;
|
|
462
908
|
}
|
|
463
909
|
return {
|
|
464
910
|
services: {
|
|
465
911
|
dataService,
|
|
466
912
|
queryService,
|
|
467
|
-
queueService
|
|
913
|
+
queueService,
|
|
914
|
+
functionsAiService
|
|
468
915
|
},
|
|
469
916
|
spaceId: contextSpaceId,
|
|
470
917
|
spaceKey,
|
|
471
918
|
spaceRootUrl: rootUrl
|
|
472
919
|
};
|
|
473
920
|
};
|
|
921
|
+
|
|
922
|
+
// src/logger.ts
|
|
923
|
+
import { LogLevel, log as log5, shouldLog } from "@dxos/log";
|
|
924
|
+
var setupFunctionsLogger = () => {
|
|
925
|
+
log5.runtimeConfig.processors.length = 0;
|
|
926
|
+
log5.runtimeConfig.processors.push(functionLogProcessor);
|
|
927
|
+
};
|
|
928
|
+
var functionLogProcessor = (config, entry) => {
|
|
929
|
+
if (!shouldLog(entry, config.filters)) {
|
|
930
|
+
return;
|
|
931
|
+
}
|
|
932
|
+
const context = entry.computedContext;
|
|
933
|
+
const error = entry.computedError;
|
|
934
|
+
const extras = [
|
|
935
|
+
Object.keys(context).length > 0 ? context : void 0,
|
|
936
|
+
error
|
|
937
|
+
].filter((value) => value !== void 0);
|
|
938
|
+
switch (entry.level) {
|
|
939
|
+
case LogLevel.DEBUG:
|
|
940
|
+
case LogLevel.TRACE:
|
|
941
|
+
console.debug(entry.message, ...extras);
|
|
942
|
+
break;
|
|
943
|
+
case LogLevel.VERBOSE:
|
|
944
|
+
console.log(entry.message, ...extras);
|
|
945
|
+
break;
|
|
946
|
+
case LogLevel.INFO:
|
|
947
|
+
console.info(entry.message, ...extras);
|
|
948
|
+
break;
|
|
949
|
+
case LogLevel.WARN:
|
|
950
|
+
console.warn(entry.message, ...extras);
|
|
951
|
+
break;
|
|
952
|
+
case LogLevel.ERROR:
|
|
953
|
+
console.error(entry.message, ...extras);
|
|
954
|
+
break;
|
|
955
|
+
default:
|
|
956
|
+
console.log(entry.message, ...extras);
|
|
957
|
+
break;
|
|
958
|
+
}
|
|
959
|
+
};
|
|
474
960
|
export {
|
|
475
961
|
FUNCTION_ROUTE_HEADER,
|
|
476
962
|
FunctionRouteValue,
|
|
963
|
+
FunctionsClient,
|
|
477
964
|
ServiceContainer,
|
|
965
|
+
createClientFromEnv,
|
|
966
|
+
createFunctionContext,
|
|
967
|
+
setupFunctionsLogger,
|
|
478
968
|
wrapHandlerForCloudflare
|
|
479
969
|
};
|
|
480
970
|
//# sourceMappingURL=index.mjs.map
|