@dxos/functions 0.3.9-main.c2ac8a5 → 0.3.9-main.c7cd0ec

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.
@@ -1,7 +1,10 @@
1
1
  import { type Client } from '@dxos/client';
2
+ import { type FunctionSubscriptionEvent } from '../handler';
2
3
  import { type FunctionManifest } from '../manifest';
4
+ type Callback = (data: FunctionSubscriptionEvent) => Promise<number>;
3
5
  type SchedulerOptions = {
4
- endpoint: string;
6
+ endpoint?: string;
7
+ callback?: Callback;
5
8
  };
6
9
  /**
7
10
  * Functions scheduler.
@@ -11,12 +14,14 @@ export declare class Scheduler {
11
14
  private readonly _manifest;
12
15
  private readonly _options;
13
16
  private readonly _mounts;
14
- constructor(_client: Client, _manifest: FunctionManifest, _options: SchedulerOptions);
17
+ constructor(_client: Client, _manifest: FunctionManifest, _options?: SchedulerOptions);
15
18
  start(): Promise<void>;
16
19
  stop(): Promise<void>;
17
20
  private mount;
18
21
  private unmount;
19
- private execFunction;
22
+ private _createTimer;
23
+ private _createSubscription;
24
+ private _execFunction;
20
25
  }
21
26
  export {};
22
27
  //# sourceMappingURL=scheduler.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"scheduler.d.ts","sourceRoot":"","sources":["../../../../src/runtime/scheduler.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,KAAK,MAAM,EAAkB,MAAM,cAAc,CAAC;AAQ3D,OAAO,EAAoB,KAAK,gBAAgB,EAAwB,MAAM,aAAa,CAAC;AAE5F,KAAK,gBAAgB,GAAG;IACtB,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF;;GAEG;AAEH,qBAAa,SAAS;IAQlB,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAR3B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAG6B;gBAGlC,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,gBAAgB,EAC3B,QAAQ,EAAE,gBAAgB;IAGvC,KAAK;IAWL,IAAI;YAMI,KAAK;YAgFL,OAAO;YASP,YAAY;CAiB3B"}
1
+ {"version":3,"file":"scheduler.d.ts","sourceRoot":"","sources":["../../../../src/runtime/scheduler.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,KAAK,MAAM,EAAkB,MAAM,cAAc,CAAC;AAQ3D,OAAO,EAAE,KAAK,yBAAyB,EAAE,MAAM,YAAY,CAAC;AAC5D,OAAO,EAAoB,KAAK,gBAAgB,EAAkD,MAAM,aAAa,CAAC;AAEtH,KAAK,QAAQ,GAAG,CAAC,IAAI,EAAE,yBAAyB,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;AAErE,KAAK,gBAAgB,GAAG;IACtB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,QAAQ,CAAC;CACrB,CAAC;AAEF;;GAEG;AAEH,qBAAa,SAAS;IAQlB,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAR3B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAG6B;gBAGlC,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,gBAAgB,EAC3B,QAAQ,GAAE,gBAAqB;IAG5C,KAAK;IAWL,IAAI;YAMI,KAAK;YA0BL,OAAO;IASrB,OAAO,CAAC,YAAY;IAepB,OAAO,CAAC,mBAAmB;YAoDb,aAAa;CA0B5B"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=scheduler.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scheduler.test.d.ts","sourceRoot":"","sources":["../../../../src/runtime/scheduler.test.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/functions",
3
- "version": "0.3.9-main.c2ac8a5",
3
+ "version": "0.3.9-main.c7cd0ec",
4
4
  "description": "Functions SDK and runtime.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -24,14 +24,14 @@
24
24
  "deepsignal": "1.4.0-shallow.0",
25
25
  "express": "^4.17.1",
26
26
  "get-port-please": "^3.1.1",
27
- "@dxos/async": "0.3.9-main.c2ac8a5",
28
- "@dxos/client": "0.3.9-main.c2ac8a5",
29
- "@dxos/echo-schema": "0.3.9-main.c2ac8a5",
30
- "@dxos/context": "0.3.9-main.c2ac8a5",
31
- "@dxos/invariant": "0.3.9-main.c2ac8a5",
32
- "@dxos/log": "0.3.9-main.c2ac8a5",
33
- "@dxos/node-std": "0.3.9-main.c2ac8a5",
34
- "@dxos/util": "0.3.9-main.c2ac8a5"
27
+ "@dxos/async": "0.3.9-main.c7cd0ec",
28
+ "@dxos/context": "0.3.9-main.c7cd0ec",
29
+ "@dxos/client": "0.3.9-main.c7cd0ec",
30
+ "@dxos/echo-schema": "0.3.9-main.c7cd0ec",
31
+ "@dxos/invariant": "0.3.9-main.c7cd0ec",
32
+ "@dxos/log": "0.3.9-main.c7cd0ec",
33
+ "@dxos/node-std": "0.3.9-main.c7cd0ec",
34
+ "@dxos/util": "0.3.9-main.c7cd0ec"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@types/express": "^4.17.17"
package/src/handler.ts CHANGED
@@ -2,7 +2,11 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { type Client } from '@dxos/client';
5
+ import { type Client, PublicKey } from '@dxos/client';
6
+ import { type Space } from '@dxos/client/echo';
7
+ import { isTypedObject, type TypedObject } from '@dxos/echo-schema';
8
+ import { log } from '@dxos/log';
9
+ import { nonNullable } from '@dxos/util';
6
10
 
7
11
  // TODO(burdon): No response?
8
12
  export interface Response {
@@ -24,6 +28,44 @@ export type FunctionHandler<T extends {}> = (params: {
24
28
  }) => Promise<Response | void>;
25
29
 
26
30
  export type FunctionSubscriptionEvent = {
27
- space: string; // TODO(burdon): Convert to PublicKey.
28
- objects: string[];
31
+ space?: string; // TODO(burdon): Convert to PublicKey.
32
+ objects?: string[];
33
+ };
34
+
35
+ export type FunctionSubscriptionEvent2 = {
36
+ space?: Space;
37
+ objects?: TypedObject[];
38
+ };
39
+
40
+ /**
41
+ * Handler wrapper for subscription events; extracts space and objects.
42
+ *
43
+ * To test:
44
+ * ```
45
+ * curl -s -X POST -H "Content-Type: application/json" --data '{"space": "0446...1cbb"}' http://localhost:7100/dev/email-extractor
46
+ * ```
47
+ *
48
+ * NOTE: Get space key from devtools or `dx space list --json`
49
+ */
50
+ export const subscriptionHandler = (
51
+ handler: FunctionHandler<FunctionSubscriptionEvent2>,
52
+ ): FunctionHandler<FunctionSubscriptionEvent> => {
53
+ return ({ event, context, ...rest }) => {
54
+ const { client } = context;
55
+ const space = event.space ? client.spaces.get(PublicKey.from(event.space)) : undefined;
56
+ const objects =
57
+ space &&
58
+ event.objects
59
+ ?.map<TypedObject | undefined>((id) => space!.db.getObjectById(id))
60
+ .filter(nonNullable)
61
+ .filter(isTypedObject);
62
+
63
+ if (!!event.space && !space) {
64
+ log.warn('invalid space', { event });
65
+ } else {
66
+ log.info('handler', { space: space?.key.truncate(), objects: objects?.length });
67
+ }
68
+
69
+ return handler({ event: { space, objects }, context, ...rest });
70
+ };
29
71
  };
package/src/manifest.ts CHANGED
@@ -20,7 +20,7 @@ export type TriggerSubscription = {
20
20
  type: string;
21
21
  spaceKey: string;
22
22
  props?: Record<string, any>;
23
- deep?: boolean;
23
+ deep?: boolean; // Watch changes to object (not just creation).
24
24
  delay?: number;
25
25
  };
26
26
 
@@ -30,7 +30,7 @@ export type TriggerSubscription = {
30
30
  export type FunctionTrigger = {
31
31
  function: string;
32
32
  schedule?: string;
33
- subscription?: TriggerSubscription;
33
+ subscriptions?: TriggerSubscription[];
34
34
  };
35
35
 
36
36
  /**
@@ -80,7 +80,7 @@ export class DevServer {
80
80
  res.statusCode = await this._invoke(name, req.body);
81
81
  res.end();
82
82
  } catch (err: any) {
83
- log.catch(err);
83
+ log.error(`Function failed: ${name}`, err);
84
84
  res.statusCode = 500;
85
85
  res.end();
86
86
  }
@@ -0,0 +1,57 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import { expect } from 'chai';
6
+
7
+ import { Trigger } from '@dxos/async';
8
+ import { Client } from '@dxos/client';
9
+ import { TestBuilder } from '@dxos/client/testing';
10
+ import { describe, test } from '@dxos/test';
11
+
12
+ import { Scheduler } from './scheduler';
13
+ import { type FunctionManifest } from '../manifest';
14
+
15
+ describe('scheduler', () => {
16
+ test('basic', async () => {
17
+ const testBuilder = new TestBuilder();
18
+ const client = new Client({ services: testBuilder.createLocal() });
19
+ await client.initialize();
20
+ await client.halo.createIdentity();
21
+
22
+ const manifest: FunctionManifest = {
23
+ functions: [
24
+ {
25
+ id: 'example.com/function/test',
26
+ name: 'test',
27
+ handler: 'test',
28
+ },
29
+ ],
30
+ triggers: [
31
+ {
32
+ function: 'example.com/function/test',
33
+ schedule: '* * * * * *', // Every second.
34
+ },
35
+ ],
36
+ };
37
+
38
+ let count = 0;
39
+ const done = new Trigger();
40
+ const scheduler = new Scheduler(client, manifest, {
41
+ callback: async () => {
42
+ if (++count === 3) {
43
+ done.wake();
44
+ }
45
+
46
+ return 200;
47
+ },
48
+ });
49
+
50
+ await scheduler.start();
51
+ await done.wait();
52
+ expect(count).to.equal(3);
53
+
54
+ await scheduler.stop();
55
+ await client.destroy();
56
+ });
57
+ });
@@ -13,10 +13,14 @@ import { invariant } from '@dxos/invariant';
13
13
  import { log } from '@dxos/log';
14
14
  import { ComplexMap } from '@dxos/util';
15
15
 
16
- import { type FunctionDef, type FunctionManifest, type FunctionTrigger } from '../manifest';
16
+ import { type FunctionSubscriptionEvent } from '../handler';
17
+ import { type FunctionDef, type FunctionManifest, type FunctionTrigger, type TriggerSubscription } from '../manifest';
18
+
19
+ type Callback = (data: FunctionSubscriptionEvent) => Promise<number>;
17
20
 
18
21
  type SchedulerOptions = {
19
- endpoint: string;
22
+ endpoint?: string;
23
+ callback?: Callback;
20
24
  };
21
25
 
22
26
  /**
@@ -33,7 +37,7 @@ export class Scheduler {
33
37
  constructor(
34
38
  private readonly _client: Client,
35
39
  private readonly _manifest: FunctionManifest,
36
- private readonly _options: SchedulerOptions,
40
+ private readonly _options: SchedulerOptions = {},
37
41
  ) {}
38
42
 
39
43
  async start() {
@@ -58,6 +62,7 @@ export class Scheduler {
58
62
  const def = this._manifest.functions.find((config) => config.id === trigger.function);
59
63
  invariant(def, `Function not found: ${trigger.function}`);
60
64
 
65
+ // Currently supports only one trigger declaration per function.
61
66
  const exists = this._mounts.get(key);
62
67
  if (!exists) {
63
68
  this._mounts.set(key, { ctx, trigger });
@@ -66,69 +71,14 @@ export class Scheduler {
66
71
  return;
67
72
  }
68
73
 
69
- // Cron schedule.
74
+ // Timer.
70
75
  if (trigger.schedule) {
71
- const task = new DeferredTask(ctx, async () => {
72
- await this.execFunction(def, {
73
- space: space.key,
74
- });
75
- });
76
-
77
- // TODO(burdon): Check greater than 30s min (use cron-parser).
78
- const job = new CronJob(trigger.schedule, () => task.schedule());
79
-
80
- job.start();
81
- ctx.onDispose(() => job.stop());
76
+ this._createTimer(ctx, space, def, trigger);
82
77
  }
83
78
 
84
- // ECHO subscription.
85
- if (trigger.subscription) {
86
- const objectIds = new Set<string>();
87
- const task = new DeferredTask(ctx, async () => {
88
- await this.execFunction(def, {
89
- space: space.key,
90
- objects: Array.from(objectIds),
91
- });
92
- });
93
-
94
- // TODO(burdon): Standardize subscription handles.
95
- const subscriptions: (() => void)[] = [];
96
- const subscription = createSubscription(({ added, updated }) => {
97
- for (const object of added) {
98
- objectIds.add(object.id);
99
- }
100
- for (const object of updated) {
101
- objectIds.add(object.id);
102
- }
103
-
104
- task.schedule();
105
- });
106
- subscriptions.push(() => subscription.unsubscribe());
107
-
108
- const { type, props, deep, delay } = trigger.subscription;
109
- const update = ({ objects }: Query) => {
110
- subscription.update(objects);
111
-
112
- // TODO(burdon): Hack to monitor changes to Document's text object.
113
- if (deep) {
114
- log.info('update', { type, deep, objects: objects.length });
115
- for (const object of objects) {
116
- const content = object.content;
117
- if (content instanceof TextObject) {
118
- subscriptions.push(content[subscribe](debounce(() => subscription.update([object]), 1_000)));
119
- }
120
- }
121
- }
122
- };
123
-
124
- // TODO(burdon): [Bug]: all callbacks are fired on the first mutation.
125
- // TODO(burdon): [Bug]: not updated when document is deleted (either top or hierarchically).
126
- const query = space.db.query(Filter.typename(type, props));
127
- subscriptions.push(query.subscribe(delay ? debounce(update, delay * 1_000) : update));
128
-
129
- ctx.onDispose(() => {
130
- subscriptions.forEach((unsubscribe) => unsubscribe());
131
- });
79
+ // Subscription.
80
+ for (const triggerSubscription of trigger.subscriptions ?? []) {
81
+ this._createSubscription(ctx, space, def, triggerSubscription);
132
82
  }
133
83
  }
134
84
  }
@@ -142,19 +92,95 @@ export class Scheduler {
142
92
  }
143
93
  }
144
94
 
145
- private async execFunction(def: FunctionDef, data: any) {
95
+ private _createTimer(ctx: Context, space: Space, def: FunctionDef, trigger: FunctionTrigger) {
96
+ const task = new DeferredTask(ctx, async () => {
97
+ await this._execFunction(def, {
98
+ space: space.key,
99
+ });
100
+ });
101
+
102
+ // TODO(burdon): Check greater than 30s min (use cron-parser).
103
+ invariant(trigger.schedule);
104
+ const job = new CronJob(trigger.schedule, () => task.schedule());
105
+
106
+ job.start();
107
+ ctx.onDispose(() => job.stop());
108
+ }
109
+
110
+ private _createSubscription(ctx: Context, space: Space, def: FunctionDef, triggerSubscription: TriggerSubscription) {
111
+ const objectIds = new Set<string>();
112
+ const task = new DeferredTask(ctx, async () => {
113
+ await this._execFunction(def, {
114
+ space: space.key,
115
+ objects: Array.from(objectIds),
116
+ });
117
+ });
118
+
119
+ // TODO(burdon): Don't fire initially.
120
+ // TODO(burdon): Standardize subscription handles.
121
+ const subscriptions: (() => void)[] = [];
122
+ const subscription = createSubscription(({ added, updated }) => {
123
+ for (const object of added) {
124
+ objectIds.add(object.id);
125
+ }
126
+ for (const object of updated) {
127
+ objectIds.add(object.id);
128
+ }
129
+
130
+ task.schedule();
131
+ });
132
+ subscriptions.push(() => subscription.unsubscribe());
133
+
134
+ // TODO(burdon): Create queue. Only allow one invocation per trigger at a time?
135
+ // TODO(burdon): Disable trigger if keeps failing.
136
+ const { type, props, deep, delay } = triggerSubscription;
137
+ const update = ({ objects }: Query) => {
138
+ subscription.update(objects);
139
+
140
+ // TODO(burdon): Hack to monitor changes to Document's text object.
141
+ if (deep) {
142
+ log.info('update', { type, deep, objects: objects.length });
143
+ for (const object of objects) {
144
+ const content = object.content;
145
+ if (content instanceof TextObject) {
146
+ subscriptions.push(content[subscribe](debounce(() => subscription.update([object]), 1_000)));
147
+ }
148
+ }
149
+ }
150
+ };
151
+
152
+ // TODO(burdon): [Bug]: all callbacks are fired on the first mutation.
153
+ // TODO(burdon): [Bug]: not updated when document is deleted (either top or hierarchically).
154
+ const query = space.db.query(Filter.typename(type, props));
155
+ subscriptions.push(query.subscribe(delay ? debounce(update, delay * 1_000) : update));
156
+
157
+ ctx.onDispose(() => {
158
+ subscriptions.forEach((unsubscribe) => unsubscribe());
159
+ });
160
+ }
161
+
162
+ private async _execFunction(def: FunctionDef, data: any) {
146
163
  try {
147
164
  log('request', { function: def.id });
148
- const response = await fetch(`${this._options.endpoint}/${def.name}`, {
149
- method: 'POST',
150
- headers: {
151
- 'Content-Type': 'application/json',
152
- },
153
- body: JSON.stringify(data),
154
- });
165
+ const { endpoint, callback } = this._options;
166
+ let status = 0;
167
+ if (endpoint) {
168
+ // TODO(burdon): Move out of scheduler (generalize as callback).
169
+ const response = await fetch(`${this._options.endpoint}/${def.name}`, {
170
+ method: 'POST',
171
+ headers: {
172
+ 'Content-Type': 'application/json',
173
+ },
174
+ body: JSON.stringify(data),
175
+ });
176
+
177
+ status = response.status;
178
+ } else if (callback) {
179
+ status = await callback(data);
180
+ }
155
181
 
156
182
  // const result = await response.json();
157
- log('result', { function: def.id, result: response.status });
183
+ log('result', { function: def.id, result: status });
158
184
  } catch (err: any) {
159
185
  log.error('error', { function: def.id, error: err.message });
160
186
  }
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=function.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"function.test.d.ts","sourceRoot":"","sources":["../../../src/function.test.ts"],"names":[],"mappings":""}
@@ -1,7 +0,0 @@
1
- //
2
- // Copyright 2023 DXOS.org
3
- //
4
-
5
- import { test } from '@dxos/test';
6
-
7
- test('works', () => {});