@dxos/functions 0.3.9-main.3ced312 → 0.3.9-main.3fcc0fa

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/functions",
3
- "version": "0.3.9-main.3ced312",
3
+ "version": "0.3.9-main.3fcc0fa",
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.3ced312",
28
- "@dxos/client": "0.3.9-main.3ced312",
29
- "@dxos/echo-schema": "0.3.9-main.3ced312",
30
- "@dxos/log": "0.3.9-main.3ced312",
31
- "@dxos/invariant": "0.3.9-main.3ced312",
32
- "@dxos/context": "0.3.9-main.3ced312",
33
- "@dxos/node-std": "0.3.9-main.3ced312",
34
- "@dxos/util": "0.3.9-main.3ced312"
27
+ "@dxos/client": "0.3.9-main.3fcc0fa",
28
+ "@dxos/async": "0.3.9-main.3fcc0fa",
29
+ "@dxos/context": "0.3.9-main.3fcc0fa",
30
+ "@dxos/echo-schema": "0.3.9-main.3fcc0fa",
31
+ "@dxos/log": "0.3.9-main.3fcc0fa",
32
+ "@dxos/invariant": "0.3.9-main.3fcc0fa",
33
+ "@dxos/node-std": "0.3.9-main.3fcc0fa",
34
+ "@dxos/util": "0.3.9-main.3fcc0fa"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@types/express": "^4.17.17"
package/src/handler.ts CHANGED
@@ -2,7 +2,10 @@
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 { nonNullable } from '@dxos/util';
6
9
 
7
10
  // TODO(burdon): No response?
8
11
  export interface Response {
@@ -12,6 +15,7 @@ export interface Response {
12
15
  // TODO(burdon): Limit access to individual space?
13
16
  export interface FunctionContext {
14
17
  client: Client;
18
+ dataDir?: string;
15
19
  }
16
20
 
17
21
  // TODO(burdon): Model after http request. Ref Lambda/OpenFaaS.
@@ -23,6 +27,28 @@ export type FunctionHandler<T extends {}> = (params: {
23
27
  }) => Promise<Response | void>;
24
28
 
25
29
  export type FunctionSubscriptionEvent = {
26
- space: string; // TODO(burdon): Convert to PublicKey.
27
- objects: string[];
30
+ space?: string; // TODO(burdon): Convert to PublicKey.
31
+ objects?: string[];
32
+ };
33
+
34
+ export type FunctionSubscriptionEvent2 = {
35
+ space?: Space;
36
+ objects?: TypedObject[];
37
+ };
38
+
39
+ export const subscriptionHandler = (
40
+ handler: FunctionHandler<FunctionSubscriptionEvent2>,
41
+ ): FunctionHandler<FunctionSubscriptionEvent> => {
42
+ return ({ event, context, ...rest }) => {
43
+ const { client } = context;
44
+ const space = event.space ? client.spaces.get(PublicKey.from(event.space)) : undefined;
45
+ const objects =
46
+ space &&
47
+ event.objects
48
+ ?.map<TypedObject | undefined>((id) => space!.db.getObjectById(id))
49
+ .filter(nonNullable)
50
+ .filter(isTypedObject);
51
+
52
+ return handler({ event: { space, objects }, context, ...rest });
53
+ };
28
54
  };
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
  /**
@@ -20,6 +20,7 @@ export type DevServerOptions = {
20
20
  directory: string;
21
21
  manifest: FunctionManifest;
22
22
  reload?: boolean;
23
+ dataDir?: string;
23
24
  };
24
25
 
25
26
  /**
@@ -85,7 +86,7 @@ export class DevServer {
85
86
  }
86
87
  });
87
88
 
88
- this._port = await getPort({ port: 7200, portRange: [7200, 7299] });
89
+ this._port = await getPort({ host: 'localhost', port: 7200, portRange: [7200, 7299] });
89
90
  this._server = app.listen(this._port);
90
91
 
91
92
  try {
@@ -161,6 +162,7 @@ export class DevServer {
161
162
 
162
163
  const context: FunctionContext = {
163
164
  client: this._client,
165
+ dataDir: this._options.dataDir,
164
166
  };
165
167
 
166
168
  let statusCode = 200;
@@ -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,77 +62,23 @@ 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 });
64
- log.info('mount', { space: space.key, trigger });
69
+ log('mount', { space: space.key, trigger });
65
70
  if (ctx.disposed) {
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,92 @@ 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): Standardize subscription handles.
120
+ const subscriptions: (() => void)[] = [];
121
+ const subscription = createSubscription(({ added, updated }) => {
122
+ for (const object of added) {
123
+ objectIds.add(object.id);
124
+ }
125
+ for (const object of updated) {
126
+ objectIds.add(object.id);
127
+ }
128
+
129
+ task.schedule();
130
+ });
131
+ subscriptions.push(() => subscription.unsubscribe());
132
+
133
+ const { type, props, deep, delay } = triggerSubscription;
134
+ const update = ({ objects }: Query) => {
135
+ subscription.update(objects);
136
+
137
+ // TODO(burdon): Hack to monitor changes to Document's text object.
138
+ if (deep) {
139
+ log.info('update', { type, deep, objects: objects.length });
140
+ for (const object of objects) {
141
+ const content = object.content;
142
+ if (content instanceof TextObject) {
143
+ subscriptions.push(content[subscribe](debounce(() => subscription.update([object]), 1_000)));
144
+ }
145
+ }
146
+ }
147
+ };
148
+
149
+ // TODO(burdon): [Bug]: all callbacks are fired on the first mutation.
150
+ // TODO(burdon): [Bug]: not updated when document is deleted (either top or hierarchically).
151
+ const query = space.db.query(Filter.typename(type, props));
152
+ subscriptions.push(query.subscribe(delay ? debounce(update, delay * 1_000) : update));
153
+
154
+ ctx.onDispose(() => {
155
+ subscriptions.forEach((unsubscribe) => unsubscribe());
156
+ });
157
+ }
158
+
159
+ private async _execFunction(def: FunctionDef, data: any) {
146
160
  try {
147
161
  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
- });
162
+ const { endpoint, callback } = this._options;
163
+ let status = 0;
164
+ if (endpoint) {
165
+ // TODO(burdon): Move out of scheduler (generalize as callback).
166
+ const response = await fetch(`${this._options.endpoint}/${def.name}`, {
167
+ method: 'POST',
168
+ headers: {
169
+ 'Content-Type': 'application/json',
170
+ },
171
+ body: JSON.stringify(data),
172
+ });
173
+
174
+ status = response.status;
175
+ } else if (callback) {
176
+ status = await callback(data);
177
+ }
155
178
 
156
179
  // const result = await response.json();
157
- log('result', { function: def.id, result: response.status });
180
+ log('result', { function: def.id, result: status });
158
181
  } catch (err: any) {
159
182
  log.error('error', { function: def.id, error: err.message });
160
183
  }
@@ -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', () => {});