@dxos/functions 0.8.1 → 0.8.2-main.2f9c567
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/lib/browser/bundler/index.mjs +0 -3
- package/dist/lib/browser/bundler/index.mjs.map +1 -1
- package/dist/lib/browser/edge/index.mjs +63 -7
- package/dist/lib/browser/edge/index.mjs.map +4 -4
- package/dist/lib/browser/index.mjs +368 -98
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/bundler/index.cjs +0 -1
- package/dist/lib/node/bundler/index.cjs.map +1 -1
- package/dist/lib/node/edge/index.cjs +65 -5
- package/dist/lib/node/edge/index.cjs.map +4 -4
- package/dist/lib/node/index.cjs +383 -93
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/bundler/index.mjs +0 -1
- package/dist/lib/node-esm/bundler/index.mjs.map +1 -1
- package/dist/lib/node-esm/edge/index.mjs +64 -6
- package/dist/lib/node-esm/edge/index.mjs.map +4 -4
- package/dist/lib/node-esm/index.mjs +368 -96
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/edge/index.d.ts.map +1 -1
- package/dist/types/src/handler.d.ts +15 -63
- package/dist/types/src/handler.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +3 -3
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/schema.d.ts +57 -0
- package/dist/types/src/schema.d.ts.map +1 -0
- package/dist/types/src/trace.d.ts +148 -0
- package/dist/types/src/trace.d.ts.map +1 -0
- package/dist/types/src/{types/types.d.ts → types.d.ts} +191 -49
- package/dist/types/src/types.d.ts.map +1 -0
- package/dist/types/src/{types/url.d.ts → url.d.ts} +6 -0
- package/dist/types/src/url.d.ts.map +1 -0
- package/package.json +20 -35
- package/src/edge/index.ts +4 -0
- package/src/handler.ts +17 -120
- package/src/index.ts +4 -5
- package/src/schema.ts +53 -0
- package/src/{types/trace.ts → trace.ts} +33 -31
- package/src/translations.ts +1 -1
- package/src/types.ts +214 -0
- package/src/{types/url.ts → url.ts} +5 -0
- package/dist/lib/browser/chunk-HI7YZO2K.mjs +0 -482
- package/dist/lib/browser/chunk-HI7YZO2K.mjs.map +0 -7
- package/dist/lib/browser/chunk-LT4LR4VU.mjs +0 -72
- package/dist/lib/browser/chunk-LT4LR4VU.mjs.map +0 -7
- package/dist/lib/browser/chunk-RVSG6WTL.mjs +0 -358
- package/dist/lib/browser/chunk-RVSG6WTL.mjs.map +0 -7
- package/dist/lib/browser/chunk-XRCXIG74.mjs +0 -12
- package/dist/lib/browser/chunk-XRCXIG74.mjs.map +0 -7
- package/dist/lib/browser/testing/index.mjs +0 -670
- package/dist/lib/browser/testing/index.mjs.map +0 -7
- package/dist/lib/browser/types/index.mjs +0 -49
- package/dist/lib/browser/types/index.mjs.map +0 -7
- package/dist/lib/node/chunk-DSUGRAAL.cjs +0 -392
- package/dist/lib/node/chunk-DSUGRAAL.cjs.map +0 -7
- package/dist/lib/node/chunk-JEQ2X3Z6.cjs +0 -34
- package/dist/lib/node/chunk-JEQ2X3Z6.cjs.map +0 -7
- package/dist/lib/node/chunk-NXZNXVT3.cjs +0 -94
- package/dist/lib/node/chunk-NXZNXVT3.cjs.map +0 -7
- package/dist/lib/node/chunk-RXMCVAMJ.cjs +0 -496
- package/dist/lib/node/chunk-RXMCVAMJ.cjs.map +0 -7
- package/dist/lib/node/testing/index.cjs +0 -687
- package/dist/lib/node/testing/index.cjs.map +0 -7
- package/dist/lib/node/types/index.cjs +0 -70
- package/dist/lib/node/types/index.cjs.map +0 -7
- package/dist/lib/node-esm/chunk-DHGBFXSZ.mjs +0 -12
- package/dist/lib/node-esm/chunk-DHGBFXSZ.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-HBD2FZXO.mjs +0 -358
- package/dist/lib/node-esm/chunk-HBD2FZXO.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-O2SXVYU5.mjs +0 -72
- package/dist/lib/node-esm/chunk-O2SXVYU5.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-SQSJO5HI.mjs +0 -482
- package/dist/lib/node-esm/chunk-SQSJO5HI.mjs.map +0 -7
- package/dist/lib/node-esm/testing/index.mjs +0 -670
- package/dist/lib/node-esm/testing/index.mjs.map +0 -7
- package/dist/lib/node-esm/types/index.mjs +0 -49
- package/dist/lib/node-esm/types/index.mjs.map +0 -7
- package/dist/types/src/browser/index.d.ts +0 -2
- package/dist/types/src/browser/index.d.ts.map +0 -1
- package/dist/types/src/function/function-registry.d.ts +0 -25
- package/dist/types/src/function/function-registry.d.ts.map +0 -1
- package/dist/types/src/function/function-registry.test.d.ts +0 -2
- package/dist/types/src/function/function-registry.test.d.ts.map +0 -1
- package/dist/types/src/function/index.d.ts +0 -2
- package/dist/types/src/function/index.d.ts.map +0 -1
- package/dist/types/src/runtime/dev-server.d.ts +0 -52
- package/dist/types/src/runtime/dev-server.d.ts.map +0 -1
- package/dist/types/src/runtime/dev-server.test.d.ts +0 -2
- package/dist/types/src/runtime/dev-server.test.d.ts.map +0 -1
- package/dist/types/src/runtime/index.d.ts +0 -3
- package/dist/types/src/runtime/index.d.ts.map +0 -1
- package/dist/types/src/runtime/scheduler.d.ts +0 -34
- package/dist/types/src/runtime/scheduler.d.ts.map +0 -1
- package/dist/types/src/runtime/scheduler.test.d.ts +0 -2
- package/dist/types/src/runtime/scheduler.test.d.ts.map +0 -1
- package/dist/types/src/testing/functions-integration.test.d.ts +0 -2
- package/dist/types/src/testing/functions-integration.test.d.ts.map +0 -1
- package/dist/types/src/testing/index.d.ts +0 -5
- package/dist/types/src/testing/index.d.ts.map +0 -1
- package/dist/types/src/testing/manifest.d.ts +0 -3
- package/dist/types/src/testing/manifest.d.ts.map +0 -1
- package/dist/types/src/testing/plugin-init.d.ts +0 -6
- package/dist/types/src/testing/plugin-init.d.ts.map +0 -1
- package/dist/types/src/testing/setup.d.ts +0 -15
- package/dist/types/src/testing/setup.d.ts.map +0 -1
- package/dist/types/src/testing/test/handler.d.ts +0 -4
- package/dist/types/src/testing/test/handler.d.ts.map +0 -1
- package/dist/types/src/testing/test/index.d.ts +0 -3
- package/dist/types/src/testing/test/index.d.ts.map +0 -1
- package/dist/types/src/testing/types.d.ts +0 -10
- package/dist/types/src/testing/types.d.ts.map +0 -1
- package/dist/types/src/testing/util.d.ts +0 -5
- package/dist/types/src/testing/util.d.ts.map +0 -1
- package/dist/types/src/trigger/index.d.ts +0 -3
- package/dist/types/src/trigger/index.d.ts.map +0 -1
- package/dist/types/src/trigger/trigger-registry.d.ts +0 -38
- package/dist/types/src/trigger/trigger-registry.d.ts.map +0 -1
- package/dist/types/src/trigger/trigger-registry.test.d.ts +0 -2
- package/dist/types/src/trigger/trigger-registry.test.d.ts.map +0 -1
- package/dist/types/src/trigger/type/index.d.ts +0 -3
- package/dist/types/src/trigger/type/index.d.ts.map +0 -1
- package/dist/types/src/trigger/type/subscription-trigger.d.ts +0 -4
- package/dist/types/src/trigger/type/subscription-trigger.d.ts.map +0 -1
- package/dist/types/src/trigger/type/timer-trigger.d.ts +0 -4
- package/dist/types/src/trigger/type/timer-trigger.d.ts.map +0 -1
- package/dist/types/src/trigger/type/webhook-trigger.d.ts +0 -4
- package/dist/types/src/trigger/type/webhook-trigger.d.ts.map +0 -1
- package/dist/types/src/types/index.d.ts +0 -5
- package/dist/types/src/types/index.d.ts.map +0 -1
- package/dist/types/src/types/schema.d.ts +0 -53
- package/dist/types/src/types/schema.d.ts.map +0 -1
- package/dist/types/src/types/trace.d.ts +0 -146
- package/dist/types/src/types/trace.d.ts.map +0 -1
- package/dist/types/src/types/types.d.ts.map +0 -1
- package/dist/types/src/types/url.d.ts.map +0 -1
- package/dist/types/tools/schema.d.ts +0 -2
- package/dist/types/tools/schema.d.ts.map +0 -1
- package/schema/functions.json +0 -211
- package/src/browser/index.ts +0 -5
- package/src/function/function-registry.test.ts +0 -118
- package/src/function/function-registry.ts +0 -104
- package/src/function/index.ts +0 -5
- package/src/runtime/dev-server.test.ts +0 -79
- package/src/runtime/dev-server.ts +0 -240
- package/src/runtime/index.ts +0 -6
- package/src/runtime/scheduler.test.ts +0 -152
- package/src/runtime/scheduler.ts +0 -170
- package/src/testing/functions-integration.test.ts +0 -65
- package/src/testing/index.ts +0 -8
- package/src/testing/manifest.ts +0 -15
- package/src/testing/plugin-init.ts +0 -20
- package/src/testing/setup.ts +0 -109
- package/src/testing/test/handler.ts +0 -15
- package/src/testing/test/index.ts +0 -7
- package/src/testing/types.ts +0 -9
- package/src/testing/util.ts +0 -26
- package/src/trigger/index.ts +0 -6
- package/src/trigger/trigger-registry.test.ts +0 -278
- package/src/trigger/trigger-registry.ts +0 -218
- package/src/trigger/type/index.ts +0 -7
- package/src/trigger/type/subscription-trigger.ts +0 -84
- package/src/trigger/type/timer-trigger.ts +0 -48
- package/src/trigger/type/webhook-trigger.ts +0 -48
- package/src/types/index.ts +0 -8
- package/src/types/schema.ts +0 -46
- package/src/types/types.ts +0 -163
|
@@ -1,218 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { Event } from '@dxos/async';
|
|
6
|
-
import { type Client } from '@dxos/client';
|
|
7
|
-
import { create, Filter, getMeta, type Space, compareForeignKeys } from '@dxos/client/echo';
|
|
8
|
-
import { Context, Resource } from '@dxos/context';
|
|
9
|
-
import { ECHO_ATTR_META, foreignKey } from '@dxos/echo-schema';
|
|
10
|
-
import { invariant } from '@dxos/invariant';
|
|
11
|
-
import { PublicKey } from '@dxos/keys';
|
|
12
|
-
import { log } from '@dxos/log';
|
|
13
|
-
import { ComplexMap, diff } from '@dxos/util';
|
|
14
|
-
|
|
15
|
-
import { createSubscriptionTrigger, createTimerTrigger } from './type';
|
|
16
|
-
import { type FunctionManifest, FunctionTrigger, type TriggerKind, type TriggerType } from '../types';
|
|
17
|
-
|
|
18
|
-
type ResponseCode = number;
|
|
19
|
-
|
|
20
|
-
export type TriggerCallback = (args: object) => Promise<ResponseCode>;
|
|
21
|
-
|
|
22
|
-
// TODO(burdon): Make object?
|
|
23
|
-
export type TriggerFactory<Spec extends TriggerType, Options = any> = (
|
|
24
|
-
ctx: Context,
|
|
25
|
-
space: Space,
|
|
26
|
-
spec: Spec,
|
|
27
|
-
callback: TriggerCallback,
|
|
28
|
-
options?: Options,
|
|
29
|
-
) => Promise<void>;
|
|
30
|
-
|
|
31
|
-
export type TriggerFactoryMap = Partial<Record<TriggerKind, TriggerFactory<any>>>;
|
|
32
|
-
|
|
33
|
-
const triggerFactory: TriggerFactoryMap = {
|
|
34
|
-
timer: createTimerTrigger,
|
|
35
|
-
// TODO(burdon): Cannot use in browser.
|
|
36
|
-
// webhook: createWebhookTrigger,
|
|
37
|
-
subscription: createSubscriptionTrigger,
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
export type TriggerEvent = {
|
|
41
|
-
space: Space;
|
|
42
|
-
triggers: FunctionTrigger[];
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
type RegisteredTrigger = {
|
|
46
|
-
activationCtx?: Context;
|
|
47
|
-
trigger: FunctionTrigger;
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
export class TriggerRegistry extends Resource {
|
|
51
|
-
private readonly _triggersBySpaceKey = new ComplexMap<PublicKey, RegisteredTrigger[]>(PublicKey.hash);
|
|
52
|
-
|
|
53
|
-
public readonly registered = new Event<TriggerEvent>();
|
|
54
|
-
public readonly removed = new Event<TriggerEvent>();
|
|
55
|
-
|
|
56
|
-
constructor(
|
|
57
|
-
private readonly _client: Client,
|
|
58
|
-
private readonly _options?: TriggerFactoryMap,
|
|
59
|
-
) {
|
|
60
|
-
super();
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
public getActiveTriggers(space: Space): FunctionTrigger[] {
|
|
64
|
-
return this._getTriggers(space, (t) => t.activationCtx != null);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
public getInactiveTriggers(space: Space): FunctionTrigger[] {
|
|
68
|
-
return this._getTriggers(space, (t) => t.activationCtx == null);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Set callback for trigger.
|
|
73
|
-
*/
|
|
74
|
-
public async activate(space: Space, trigger: FunctionTrigger, callback: TriggerCallback): Promise<void> {
|
|
75
|
-
log('activate', { space: space.key, trigger });
|
|
76
|
-
|
|
77
|
-
const activationCtx = new Context({ name: `FunctionTrigger-${trigger.function}` });
|
|
78
|
-
this._ctx.onDispose(() => activationCtx.dispose());
|
|
79
|
-
const registeredTrigger = this._triggersBySpaceKey.get(space.key)?.find((reg) => reg.trigger.id === trigger.id);
|
|
80
|
-
invariant(registeredTrigger, `Trigger is not registered: ${trigger.function}`);
|
|
81
|
-
registeredTrigger.activationCtx = activationCtx;
|
|
82
|
-
|
|
83
|
-
try {
|
|
84
|
-
// Create trigger.
|
|
85
|
-
invariant(trigger.spec);
|
|
86
|
-
const options = this._options?.[trigger.spec.type];
|
|
87
|
-
const createTrigger = triggerFactory[trigger.spec.type];
|
|
88
|
-
invariant(createTrigger, `Trigger factory not found: ${trigger.spec.type}`);
|
|
89
|
-
await createTrigger(activationCtx, space, trigger.spec, callback, options);
|
|
90
|
-
} catch (err) {
|
|
91
|
-
delete registeredTrigger.activationCtx;
|
|
92
|
-
throw err;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Loads triggers from the manifest into the space.
|
|
98
|
-
*/
|
|
99
|
-
public async register(space: Space, manifest: FunctionManifest): Promise<void> {
|
|
100
|
-
log('register', { space: space.key });
|
|
101
|
-
if (!manifest.triggers?.length) {
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
if (!space.db.graph.schemaRegistry.hasSchema(FunctionTrigger)) {
|
|
106
|
-
space.db.graph.schemaRegistry.addSchema([FunctionTrigger]);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// Create FK to enable syncing if none are set (NOTE: Possible collision).
|
|
110
|
-
const manifestTriggers = manifest.triggers.map((trigger) => {
|
|
111
|
-
let keys = trigger[ECHO_ATTR_META]?.keys;
|
|
112
|
-
delete trigger[ECHO_ATTR_META];
|
|
113
|
-
if (!keys?.length) {
|
|
114
|
-
keys = [foreignKey('manifest', [trigger.function, trigger.spec?.type].join(':'))];
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
return create(FunctionTrigger, trigger, { keys });
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
// Sync triggers.
|
|
121
|
-
const { objects: existing } = await space.db.query(Filter.schema(FunctionTrigger)).run();
|
|
122
|
-
const { added } = diff(existing, manifestTriggers, compareForeignKeys);
|
|
123
|
-
|
|
124
|
-
// TODO(burdon): Update existing.
|
|
125
|
-
added.forEach((trigger) => {
|
|
126
|
-
space.db.add(trigger);
|
|
127
|
-
log.info('added', { meta: getMeta(trigger) });
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
if (added.length > 0) {
|
|
131
|
-
await space.db.flush();
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
protected override async _open(): Promise<void> {
|
|
136
|
-
log.info('open...');
|
|
137
|
-
const spaceListSubscription = this._client.spaces.subscribe(async (spaces) => {
|
|
138
|
-
for (const space of spaces) {
|
|
139
|
-
if (this._triggersBySpaceKey.has(space.key)) {
|
|
140
|
-
continue;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
const registered: RegisteredTrigger[] = [];
|
|
144
|
-
this._triggersBySpaceKey.set(space.key, registered);
|
|
145
|
-
await space.waitUntilReady();
|
|
146
|
-
if (this._ctx.disposed) {
|
|
147
|
-
break;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
// Subscribe to updates.
|
|
151
|
-
this._ctx.onDispose(
|
|
152
|
-
space.db.query(Filter.schema(FunctionTrigger)).subscribe(async ({ objects: current }) => {
|
|
153
|
-
log.info('update', { space: space.key, registered: registered.length, current: current.length });
|
|
154
|
-
await this._handleRemovedTriggers(space, current, registered);
|
|
155
|
-
this._handleNewTriggers(space, current, registered);
|
|
156
|
-
}),
|
|
157
|
-
);
|
|
158
|
-
}
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
this._ctx.onDispose(() => spaceListSubscription.unsubscribe());
|
|
162
|
-
log.info('opened');
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
protected override async _close(_: Context): Promise<void> {
|
|
166
|
-
log.info('close...');
|
|
167
|
-
this._triggersBySpaceKey.clear();
|
|
168
|
-
log.info('closed');
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
private _handleNewTriggers(space: Space, current: FunctionTrigger[], registered: RegisteredTrigger[]) {
|
|
172
|
-
const added = current.filter((candidate) => {
|
|
173
|
-
return candidate.enabled && registered.find((reg) => reg.trigger.id === candidate.id) == null;
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
if (added.length > 0) {
|
|
177
|
-
const newRegisteredTriggers: RegisteredTrigger[] = added.map((trigger) => ({ trigger }));
|
|
178
|
-
registered.push(...newRegisteredTriggers);
|
|
179
|
-
log.info('added', () => ({
|
|
180
|
-
spaceKey: space.key,
|
|
181
|
-
triggers: added.map((trigger) => trigger.function),
|
|
182
|
-
}));
|
|
183
|
-
|
|
184
|
-
this.registered.emit({ space, triggers: added });
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
private async _handleRemovedTriggers(
|
|
189
|
-
space: Space,
|
|
190
|
-
current: FunctionTrigger[],
|
|
191
|
-
registered: RegisteredTrigger[],
|
|
192
|
-
): Promise<void> {
|
|
193
|
-
const removed: FunctionTrigger[] = [];
|
|
194
|
-
for (let i = registered.length - 1; i >= 0; i--) {
|
|
195
|
-
const wasRemoved =
|
|
196
|
-
current.filter((trigger) => trigger.enabled).find((trigger) => trigger.id === registered[i].trigger.id) == null;
|
|
197
|
-
if (wasRemoved) {
|
|
198
|
-
const unregistered = registered.splice(i, 1)[0];
|
|
199
|
-
await unregistered.activationCtx?.dispose();
|
|
200
|
-
removed.push(unregistered.trigger);
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
if (removed.length > 0) {
|
|
205
|
-
log.info('removed', () => ({
|
|
206
|
-
spaceKey: space.key,
|
|
207
|
-
triggers: removed.map((trigger) => trigger.function),
|
|
208
|
-
}));
|
|
209
|
-
|
|
210
|
-
this.removed.emit({ space, triggers: removed });
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
private _getTriggers(space: Space, predicate: (trigger: RegisteredTrigger) => boolean): FunctionTrigger[] {
|
|
215
|
-
const allSpaceTriggers = this._triggersBySpaceKey.get(space.key) ?? [];
|
|
216
|
-
return allSpaceTriggers.filter(predicate).map((trigger) => trigger.trigger);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { debounce, UpdateScheduler } from '@dxos/async';
|
|
6
|
-
import { Filter, type Space } from '@dxos/client/echo';
|
|
7
|
-
import { type Context } from '@dxos/context';
|
|
8
|
-
import { createSubscription, type Query } from '@dxos/echo-db';
|
|
9
|
-
import { log } from '@dxos/log';
|
|
10
|
-
|
|
11
|
-
import type { SubscriptionTrigger } from '../../types';
|
|
12
|
-
import { type TriggerCallback, type TriggerFactory } from '../trigger-registry';
|
|
13
|
-
|
|
14
|
-
export const createSubscriptionTrigger: TriggerFactory<SubscriptionTrigger> = async (
|
|
15
|
-
ctx: Context,
|
|
16
|
-
space: Space,
|
|
17
|
-
spec: SubscriptionTrigger,
|
|
18
|
-
callback: TriggerCallback,
|
|
19
|
-
) => {
|
|
20
|
-
const objectIds = new Set<string>();
|
|
21
|
-
const task = new UpdateScheduler(
|
|
22
|
-
ctx,
|
|
23
|
-
async () => {
|
|
24
|
-
if (objectIds.size > 0) {
|
|
25
|
-
const objects = Array.from(objectIds);
|
|
26
|
-
objectIds.clear();
|
|
27
|
-
await callback({ objects });
|
|
28
|
-
}
|
|
29
|
-
},
|
|
30
|
-
{ maxFrequency: 4 },
|
|
31
|
-
);
|
|
32
|
-
|
|
33
|
-
// TODO(burdon): Factor out diff.
|
|
34
|
-
// TODO(burdon): Don't fire initially?
|
|
35
|
-
// TODO(burdon): Create queue. Only allow one invocation per trigger at a time?
|
|
36
|
-
const subscriptions: (() => void)[] = [];
|
|
37
|
-
const subscription = createSubscription(({ added, updated }) => {
|
|
38
|
-
const sizeBefore = objectIds.size;
|
|
39
|
-
for (const object of added) {
|
|
40
|
-
objectIds.add(object.id);
|
|
41
|
-
}
|
|
42
|
-
for (const object of updated) {
|
|
43
|
-
objectIds.add(object.id);
|
|
44
|
-
}
|
|
45
|
-
if (objectIds.size > sizeBefore) {
|
|
46
|
-
log.info('updated', { added: added.length, updated: updated.length });
|
|
47
|
-
task.trigger();
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
subscriptions.push(() => subscription.unsubscribe());
|
|
52
|
-
|
|
53
|
-
// TODO(burdon): Disable trigger if keeps failing.
|
|
54
|
-
const { filter, options: { deep, delay } = {} } = spec;
|
|
55
|
-
const update = ({ objects }: Query) => {
|
|
56
|
-
log.info('update', { objects: objects.length });
|
|
57
|
-
subscription.update(objects);
|
|
58
|
-
|
|
59
|
-
// TODO(burdon): Hack to monitor changes to Document's text object.
|
|
60
|
-
if (deep) {
|
|
61
|
-
// TODO(dmaretskyi): Removed to not have dependency on markdown-plugin.
|
|
62
|
-
// for (const object of objects) {
|
|
63
|
-
// const content = object.content;
|
|
64
|
-
// if (content instanceof TextType) {
|
|
65
|
-
// subscriptions.push(getObjectCore(content).updates.on(debounce(() => subscription.update([object]), 1_000)));
|
|
66
|
-
// }
|
|
67
|
-
// }
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
// TODO(burdon): OR not working.
|
|
72
|
-
// TODO(burdon): [Bug]: all callbacks are fired on the first mutation.
|
|
73
|
-
// TODO(burdon): [Bug]: not updated when document is deleted (either top or hierarchically).
|
|
74
|
-
log.info('subscription', { filter });
|
|
75
|
-
// const query = triggerCtx.space.db.query(Filter.or(filter.map(({ type, props }) => Filter.typename(type, props))));
|
|
76
|
-
if (filter.type) {
|
|
77
|
-
const query = space.db.query(Filter.typename(filter.type, filter.props));
|
|
78
|
-
subscriptions.push(query.subscribe(delay ? debounce(update, delay) : update));
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
ctx.onDispose(() => {
|
|
82
|
-
subscriptions.forEach((unsubscribe) => unsubscribe());
|
|
83
|
-
});
|
|
84
|
-
};
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { parseCronExpression } from 'cron-schedule';
|
|
6
|
-
|
|
7
|
-
import { DeferredTask } from '@dxos/async';
|
|
8
|
-
import { type Space } from '@dxos/client/echo';
|
|
9
|
-
import { type Context } from '@dxos/context';
|
|
10
|
-
import { log } from '@dxos/log';
|
|
11
|
-
|
|
12
|
-
import type { TimerTrigger } from '../../types';
|
|
13
|
-
import { type TriggerCallback, type TriggerFactory } from '../trigger-registry';
|
|
14
|
-
|
|
15
|
-
export const createTimerTrigger: TriggerFactory<TimerTrigger> = async (
|
|
16
|
-
ctx: Context,
|
|
17
|
-
space: Space,
|
|
18
|
-
spec: TimerTrigger,
|
|
19
|
-
callback: TriggerCallback,
|
|
20
|
-
) => {
|
|
21
|
-
const task = new DeferredTask(ctx, async () => {
|
|
22
|
-
await callback({});
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
let last = 0;
|
|
26
|
-
let run = 0;
|
|
27
|
-
const schedule = parseCronExpression(spec.cron);
|
|
28
|
-
const getRunTimeout = () => Date.now() - schedule.getNextDate().getTime();
|
|
29
|
-
const runCron = () => {
|
|
30
|
-
if (ctx.disposed) {
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
// TODO(burdon): Check greater than 30s (use cron-parser).
|
|
34
|
-
const now = Date.now();
|
|
35
|
-
const delta = last ? now - last : 0;
|
|
36
|
-
last = now;
|
|
37
|
-
|
|
38
|
-
run++;
|
|
39
|
-
log.info('tick', { space: space.key.truncate(), count: run, delta });
|
|
40
|
-
task.schedule();
|
|
41
|
-
|
|
42
|
-
timeout = setTimeout(runCron, getRunTimeout());
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
let timeout = setTimeout(runCron, getRunTimeout());
|
|
46
|
-
|
|
47
|
-
ctx.onDispose(() => clearTimeout(timeout));
|
|
48
|
-
};
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { getPort } from 'get-port-please';
|
|
6
|
-
import http from 'node:http';
|
|
7
|
-
|
|
8
|
-
import { type Space } from '@dxos/client/echo';
|
|
9
|
-
import { type Context } from '@dxos/context';
|
|
10
|
-
import { log } from '@dxos/log';
|
|
11
|
-
|
|
12
|
-
import type { WebhookTrigger } from '../../types';
|
|
13
|
-
import { type TriggerCallback, type TriggerFactory } from '../trigger-registry';
|
|
14
|
-
|
|
15
|
-
export const createWebhookTrigger: TriggerFactory<WebhookTrigger> = async (
|
|
16
|
-
ctx: Context,
|
|
17
|
-
space: Space,
|
|
18
|
-
spec: WebhookTrigger,
|
|
19
|
-
callback: TriggerCallback,
|
|
20
|
-
) => {
|
|
21
|
-
// TODO(burdon): Enable POST hook with payload.
|
|
22
|
-
const server = http.createServer(async (req, res) => {
|
|
23
|
-
if (req.method !== spec.method) {
|
|
24
|
-
res.statusCode = 405;
|
|
25
|
-
return res.end();
|
|
26
|
-
}
|
|
27
|
-
res.statusCode = await callback({});
|
|
28
|
-
res.end();
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
// TODO(burdon): Not used.
|
|
32
|
-
// const DEF_PORT_RANGE = { min: 7500, max: 7599 };
|
|
33
|
-
// const portRange = Object.assign({}, trigger.port, DEF_PORT_RANGE) as WebhookTrigger['port'];
|
|
34
|
-
const port = await getPort({
|
|
35
|
-
random: true,
|
|
36
|
-
// portRange: [portRange!.min, portRange!.max],
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
// TODO(burdon): Update trigger object with actual port.
|
|
40
|
-
server.listen(port, () => {
|
|
41
|
-
log.info('started webhook', { port });
|
|
42
|
-
spec.port = port;
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
ctx.onDispose(() => {
|
|
46
|
-
server.close();
|
|
47
|
-
});
|
|
48
|
-
};
|
package/src/types/index.ts
DELETED
package/src/types/schema.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { EchoObject, JsonSchemaType, LabelAnnotationId, Ref, S, TypedObject } from '@dxos/echo-schema';
|
|
6
|
-
import { TextType } from '@dxos/schema';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Source script.
|
|
10
|
-
*/
|
|
11
|
-
export const ScriptType = S.Struct({
|
|
12
|
-
name: S.optional(S.String),
|
|
13
|
-
description: S.optional(S.String),
|
|
14
|
-
// TODO(burdon): Change to hash of deployed content.
|
|
15
|
-
// Whether source has changed since last deploy.
|
|
16
|
-
changed: S.optional(S.Boolean),
|
|
17
|
-
source: Ref(TextType),
|
|
18
|
-
})
|
|
19
|
-
.annotations({ [LabelAnnotationId]: 'name' })
|
|
20
|
-
.pipe(EchoObject({ typename: 'dxos.org/type/Script', version: '0.1.0' }));
|
|
21
|
-
|
|
22
|
-
export type ScriptType = S.Schema.Type<typeof ScriptType>;
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Function deployment.
|
|
26
|
-
*/
|
|
27
|
-
export class FunctionType extends TypedObject({
|
|
28
|
-
typename: 'dxos.org/type/Function',
|
|
29
|
-
version: '0.1.0',
|
|
30
|
-
})({
|
|
31
|
-
// TODO(burdon): Rename to id/uri?
|
|
32
|
-
name: S.NonEmptyString,
|
|
33
|
-
version: S.String,
|
|
34
|
-
|
|
35
|
-
description: S.optional(S.String),
|
|
36
|
-
|
|
37
|
-
// Reference to a source script if it exists within ECHO.
|
|
38
|
-
// TODO(burdon): Don't ref ScriptType directly (core).
|
|
39
|
-
source: S.optional(Ref(ScriptType)),
|
|
40
|
-
|
|
41
|
-
inputSchema: S.optional(JsonSchemaType),
|
|
42
|
-
outputSchema: S.optional(JsonSchemaType),
|
|
43
|
-
|
|
44
|
-
// Local binding to a function name.
|
|
45
|
-
binding: S.optional(S.String),
|
|
46
|
-
}) {}
|
package/src/types/types.ts
DELETED
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2023 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { AST, OptionsAnnotationId, RawObject, S, TypedObject, DXN } from '@dxos/echo-schema';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Type discriminator for TriggerType.
|
|
9
|
-
* Every spec has a type field of type TriggerKind that we can use to understand which type we're working with.
|
|
10
|
-
* https://www.typescriptlang.org/docs/handbook/2/narrowing.html#discriminated-unions
|
|
11
|
-
*/
|
|
12
|
-
export enum TriggerKind {
|
|
13
|
-
Timer = 'timer',
|
|
14
|
-
Webhook = 'webhook',
|
|
15
|
-
Subscription = 'subscription',
|
|
16
|
-
Email = 'email',
|
|
17
|
-
Queue = 'queue',
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// TODO(burdon): Rename prop kind.
|
|
21
|
-
const typeLiteralAnnotations = { [AST.TitleAnnotationId]: 'Type' };
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Cron timer.
|
|
25
|
-
*/
|
|
26
|
-
const TimerTriggerSchema = S.Struct({
|
|
27
|
-
type: S.Literal(TriggerKind.Timer).annotations(typeLiteralAnnotations),
|
|
28
|
-
cron: S.String.annotations({
|
|
29
|
-
[AST.TitleAnnotationId]: 'Cron',
|
|
30
|
-
[AST.ExamplesAnnotationId]: ['0 0 * * *'],
|
|
31
|
-
}),
|
|
32
|
-
}).pipe(S.mutable);
|
|
33
|
-
|
|
34
|
-
export type TimerTrigger = S.Schema.Type<typeof TimerTriggerSchema>;
|
|
35
|
-
|
|
36
|
-
const EmailTriggerSchema = S.Struct({
|
|
37
|
-
type: S.Literal(TriggerKind.Email).annotations(typeLiteralAnnotations),
|
|
38
|
-
}).pipe(S.mutable);
|
|
39
|
-
|
|
40
|
-
export type EmailTrigger = S.Schema.Type<typeof EmailTriggerSchema>;
|
|
41
|
-
|
|
42
|
-
const QueueTriggerSchema = S.Struct({
|
|
43
|
-
type: S.Literal(TriggerKind.Queue).annotations(typeLiteralAnnotations),
|
|
44
|
-
queue: DXN,
|
|
45
|
-
}).pipe(S.mutable);
|
|
46
|
-
|
|
47
|
-
export type QueueTrigger = S.Schema.Type<typeof QueueTriggerSchema>;
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Webhook.
|
|
51
|
-
*/
|
|
52
|
-
const WebhookTriggerSchema = S.Struct({
|
|
53
|
-
type: S.Literal(TriggerKind.Webhook).annotations(typeLiteralAnnotations),
|
|
54
|
-
method: S.optional(
|
|
55
|
-
S.String.annotations({
|
|
56
|
-
[AST.TitleAnnotationId]: 'Method',
|
|
57
|
-
[OptionsAnnotationId]: ['GET', 'POST'],
|
|
58
|
-
}),
|
|
59
|
-
),
|
|
60
|
-
port: S.optional(
|
|
61
|
-
S.Number.annotations({
|
|
62
|
-
[AST.TitleAnnotationId]: 'Port',
|
|
63
|
-
}),
|
|
64
|
-
),
|
|
65
|
-
}).pipe(S.mutable);
|
|
66
|
-
|
|
67
|
-
export type WebhookTrigger = S.Schema.Type<typeof WebhookTriggerSchema>;
|
|
68
|
-
|
|
69
|
-
// TODO(burdon): Use ECHO definition (from https://github.com/dxos/dxos/pull/8233).
|
|
70
|
-
const QuerySchema = S.Struct({
|
|
71
|
-
type: S.optional(S.String.annotations({ [AST.TitleAnnotationId]: 'Type' })),
|
|
72
|
-
props: S.optional(S.Record({ key: S.String, value: S.Any })),
|
|
73
|
-
}).annotations({ [AST.TitleAnnotationId]: 'Query' });
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Subscription.
|
|
77
|
-
*/
|
|
78
|
-
const SubscriptionTriggerSchema = S.Struct({
|
|
79
|
-
type: S.Literal(TriggerKind.Subscription).annotations(typeLiteralAnnotations),
|
|
80
|
-
// TODO(burdon): Define query DSL (from ECHO). Reconcile with Table.Query.
|
|
81
|
-
filter: QuerySchema,
|
|
82
|
-
options: S.optional(
|
|
83
|
-
S.Struct({
|
|
84
|
-
// Watch changes to object (not just creation).
|
|
85
|
-
deep: S.optional(S.Boolean.annotations({ [AST.TitleAnnotationId]: 'Nested' })),
|
|
86
|
-
// Debounce changes (delay in ms).
|
|
87
|
-
delay: S.optional(S.Number.annotations({ [AST.TitleAnnotationId]: 'Delay' })),
|
|
88
|
-
}).annotations({ [AST.TitleAnnotationId]: 'Options' }),
|
|
89
|
-
),
|
|
90
|
-
}).pipe(S.mutable);
|
|
91
|
-
|
|
92
|
-
export type SubscriptionTrigger = S.Schema.Type<typeof SubscriptionTriggerSchema>;
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Trigger schema (discriminated union).
|
|
96
|
-
*/
|
|
97
|
-
export const TriggerSchema = S.Union(
|
|
98
|
-
TimerTriggerSchema,
|
|
99
|
-
WebhookTriggerSchema,
|
|
100
|
-
SubscriptionTriggerSchema,
|
|
101
|
-
EmailTriggerSchema,
|
|
102
|
-
QueueTriggerSchema,
|
|
103
|
-
).annotations({
|
|
104
|
-
[AST.TitleAnnotationId]: 'Trigger',
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
export type TriggerType = S.Schema.Type<typeof TriggerSchema>;
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Function trigger.
|
|
111
|
-
*/
|
|
112
|
-
export const FunctionTriggerSchema = S.Struct({
|
|
113
|
-
// TODO(burdon): What type does this reference.
|
|
114
|
-
// TODO(wittjosiah): This should probably be a Ref?
|
|
115
|
-
function: S.optional(S.String.annotations({ [AST.TitleAnnotationId]: 'Function' })),
|
|
116
|
-
|
|
117
|
-
enabled: S.optional(S.Boolean.annotations({ [AST.TitleAnnotationId]: 'Enabled' })),
|
|
118
|
-
|
|
119
|
-
// TODO(burdon): Flatten entire schema.
|
|
120
|
-
spec: S.optional(TriggerSchema),
|
|
121
|
-
|
|
122
|
-
// TODO(burdon): Get schema as partial from function.
|
|
123
|
-
// TODO(wittjosiah): Rename to payload.
|
|
124
|
-
// The `meta` property is merged into the event data passed to the function.
|
|
125
|
-
meta: S.optional(S.mutable(S.Record({ key: S.String, value: S.Any }))),
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
export type FunctionTriggerType = S.Schema.Type<typeof FunctionTriggerSchema>;
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Function trigger.
|
|
132
|
-
*/
|
|
133
|
-
export class FunctionTrigger extends TypedObject({
|
|
134
|
-
typename: 'dxos.org/type/FunctionTrigger',
|
|
135
|
-
version: '0.1.0',
|
|
136
|
-
})(FunctionTriggerSchema.fields) {}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Function definition.
|
|
140
|
-
* @deprecated (Use dxos.org/type/Function)
|
|
141
|
-
*/
|
|
142
|
-
// TODO(burdon): Reconcile with FunctionType.
|
|
143
|
-
export class FunctionDef extends TypedObject({
|
|
144
|
-
typename: 'dxos.org/type/FunctionDef',
|
|
145
|
-
version: '0.1.0',
|
|
146
|
-
})({
|
|
147
|
-
uri: S.String,
|
|
148
|
-
description: S.optional(S.String),
|
|
149
|
-
route: S.String,
|
|
150
|
-
handler: S.String,
|
|
151
|
-
}) {}
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Function manifest file.
|
|
155
|
-
*/
|
|
156
|
-
export const FunctionManifestSchema = S.Struct({
|
|
157
|
-
functions: S.optional(S.mutable(S.Array(RawObject(FunctionDef)))),
|
|
158
|
-
triggers: S.optional(S.mutable(S.Array(RawObject(FunctionTrigger)))),
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
export type FunctionManifest = S.Schema.Type<typeof FunctionManifestSchema>;
|
|
162
|
-
|
|
163
|
-
export const FUNCTION_TYPES = [FunctionDef, FunctionTrigger];
|