@dxos/functions 0.5.3-main.f752aaa → 0.5.3-next.57eca40
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/chunk-366QG6IX.mjs +81 -0
- package/dist/lib/browser/chunk-366QG6IX.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +825 -471
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/types.mjs +12 -0
- package/dist/lib/browser/types.mjs.map +7 -0
- package/dist/lib/node/chunk-3VSJ57ZZ.cjs +97 -0
- package/dist/lib/node/chunk-3VSJ57ZZ.cjs.map +7 -0
- package/dist/lib/node/index.cjs +805 -461
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/types.cjs +33 -0
- package/dist/lib/node/types.cjs.map +7 -0
- package/dist/types/src/function/function-registry.d.ts +24 -0
- package/dist/types/src/function/function-registry.d.ts.map +1 -0
- package/dist/types/src/function/function-registry.test.d.ts +2 -0
- package/dist/types/src/function/function-registry.test.d.ts.map +1 -0
- package/dist/types/src/function/index.d.ts +2 -0
- package/dist/types/src/function/index.d.ts.map +1 -0
- package/dist/types/src/handler.d.ts +33 -12
- package/dist/types/src/handler.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/runtime/dev-server.d.ts +16 -13
- package/dist/types/src/runtime/dev-server.d.ts.map +1 -1
- package/dist/types/src/runtime/dev-server.test.d.ts +2 -0
- package/dist/types/src/runtime/dev-server.test.d.ts.map +1 -0
- package/dist/types/src/runtime/scheduler.d.ts +13 -27
- package/dist/types/src/runtime/scheduler.d.ts.map +1 -1
- package/dist/types/src/testing/functions-integration.test.d.ts +2 -0
- package/dist/types/src/testing/functions-integration.test.d.ts.map +1 -0
- package/dist/types/src/testing/index.d.ts +4 -0
- package/dist/types/src/testing/index.d.ts.map +1 -0
- package/dist/types/src/testing/setup.d.ts +5 -0
- package/dist/types/src/testing/setup.d.ts.map +1 -0
- package/dist/types/src/testing/test/handler.d.ts +4 -0
- package/dist/types/src/testing/test/handler.d.ts.map +1 -0
- package/dist/types/src/testing/test/index.d.ts +3 -0
- package/dist/types/src/testing/test/index.d.ts.map +1 -0
- package/dist/types/src/testing/types.d.ts +9 -0
- package/dist/types/src/testing/types.d.ts.map +1 -0
- package/dist/types/src/testing/util.d.ts +3 -0
- package/dist/types/src/testing/util.d.ts.map +1 -0
- package/dist/types/src/trigger/index.d.ts +2 -0
- package/dist/types/src/trigger/index.d.ts.map +1 -0
- package/dist/types/src/trigger/trigger-registry.d.ts +40 -0
- package/dist/types/src/trigger/trigger-registry.d.ts.map +1 -0
- package/dist/types/src/trigger/trigger-registry.test.d.ts +2 -0
- package/dist/types/src/trigger/trigger-registry.test.d.ts.map +1 -0
- package/dist/types/src/trigger/type/index.d.ts +5 -0
- package/dist/types/src/trigger/type/index.d.ts.map +1 -0
- package/dist/types/src/trigger/type/subscription-trigger.d.ts +4 -0
- package/dist/types/src/trigger/type/subscription-trigger.d.ts.map +1 -0
- package/dist/types/src/trigger/type/timer-trigger.d.ts +4 -0
- package/dist/types/src/trigger/type/timer-trigger.d.ts.map +1 -0
- package/dist/types/src/trigger/type/webhook-trigger.d.ts +4 -0
- package/dist/types/src/trigger/type/webhook-trigger.d.ts.map +1 -0
- package/dist/types/src/trigger/type/websocket-trigger.d.ts +13 -0
- package/dist/types/src/trigger/type/websocket-trigger.d.ts.map +1 -0
- package/dist/types/src/types.d.ts +143 -101
- package/dist/types/src/types.d.ts.map +1 -1
- package/dist/types/src/util.d.ts +15 -0
- package/dist/types/src/util.d.ts.map +1 -0
- package/dist/types/src/util.test.d.ts +2 -0
- package/dist/types/src/util.test.d.ts.map +1 -0
- package/package.json +33 -15
- package/schema/functions.json +140 -104
- package/src/function/function-registry.test.ts +105 -0
- package/src/function/function-registry.ts +90 -0
- package/src/function/index.ts +5 -0
- package/src/handler.ts +54 -31
- package/src/index.ts +2 -0
- package/src/runtime/dev-server.test.ts +60 -0
- package/src/runtime/dev-server.ts +104 -53
- package/src/runtime/scheduler.test.ts +56 -73
- package/src/runtime/scheduler.ts +87 -271
- package/src/testing/functions-integration.test.ts +99 -0
- package/src/testing/index.ts +7 -0
- package/src/testing/setup.ts +45 -0
- package/src/testing/test/handler.ts +15 -0
- package/src/testing/test/index.ts +7 -0
- package/src/testing/types.ts +9 -0
- package/src/testing/util.ts +16 -0
- package/src/trigger/index.ts +5 -0
- package/src/trigger/trigger-registry.test.ts +255 -0
- package/src/trigger/trigger-registry.ts +189 -0
- package/src/trigger/type/index.ts +8 -0
- package/src/trigger/type/subscription-trigger.ts +80 -0
- package/src/trigger/type/timer-trigger.ts +44 -0
- package/src/trigger/type/webhook-trigger.ts +47 -0
- package/src/trigger/type/websocket-trigger.ts +91 -0
- package/src/types.ts +59 -32
- package/src/util.test.ts +43 -0
- package/src/util.ts +48 -0
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { TextV0Type } from '@braneframe/types';
|
|
6
|
+
import { debounce, UpdateScheduler } from '@dxos/async';
|
|
7
|
+
import { type Context } from '@dxos/context';
|
|
8
|
+
import { createSubscription, Filter, getAutomergeObjectCore, type Query } from '@dxos/echo-db';
|
|
9
|
+
import { log } from '@dxos/log';
|
|
10
|
+
|
|
11
|
+
import type { SubscriptionTrigger } from '../../types';
|
|
12
|
+
import { type TriggerCallback, type TriggerContext, type TriggerFactory } from '../trigger-registry';
|
|
13
|
+
|
|
14
|
+
export const createSubscriptionTrigger: TriggerFactory<SubscriptionTrigger> = async (
|
|
15
|
+
ctx: Context,
|
|
16
|
+
triggerCtx: TriggerContext,
|
|
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): Don't fire initially?
|
|
34
|
+
// TODO(burdon): Create queue. Only allow one invocation per trigger at a time?
|
|
35
|
+
const subscriptions: (() => void)[] = [];
|
|
36
|
+
const subscription = createSubscription(({ added, updated }) => {
|
|
37
|
+
const sizeBefore = objectIds.size;
|
|
38
|
+
for (const object of added) {
|
|
39
|
+
objectIds.add(object.id);
|
|
40
|
+
}
|
|
41
|
+
for (const object of updated) {
|
|
42
|
+
objectIds.add(object.id);
|
|
43
|
+
}
|
|
44
|
+
if (objectIds.size > sizeBefore) {
|
|
45
|
+
log.info('updated', { added: added.length, updated: updated.length });
|
|
46
|
+
task.trigger();
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
subscriptions.push(() => subscription.unsubscribe());
|
|
51
|
+
|
|
52
|
+
// TODO(burdon): Disable trigger if keeps failing.
|
|
53
|
+
const { filter, options: { deep, delay } = {} } = spec;
|
|
54
|
+
const update = ({ objects }: Query) => {
|
|
55
|
+
subscription.update(objects);
|
|
56
|
+
|
|
57
|
+
// TODO(burdon): Hack to monitor changes to Document's text object.
|
|
58
|
+
if (deep) {
|
|
59
|
+
log.info('update', { objects: objects.length });
|
|
60
|
+
for (const object of objects) {
|
|
61
|
+
const content = object.content;
|
|
62
|
+
if (content instanceof TextV0Type) {
|
|
63
|
+
subscriptions.push(
|
|
64
|
+
getAutomergeObjectCore(content).updates.on(debounce(() => subscription.update([object]), 1_000)),
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
// TODO(burdon): Is Filter.or implemented?
|
|
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
|
+
const query = triggerCtx.space.db.query(Filter.or(filter.map(({ type, props }) => Filter.typename(type, props))));
|
|
75
|
+
subscriptions.push(query.subscribe(delay ? debounce(update, delay) : update));
|
|
76
|
+
|
|
77
|
+
ctx.onDispose(() => {
|
|
78
|
+
subscriptions.forEach((unsubscribe) => unsubscribe());
|
|
79
|
+
});
|
|
80
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { CronJob } from 'cron';
|
|
6
|
+
|
|
7
|
+
import { DeferredTask } from '@dxos/async';
|
|
8
|
+
import { type Context } from '@dxos/context';
|
|
9
|
+
import { log } from '@dxos/log';
|
|
10
|
+
|
|
11
|
+
import type { TimerTrigger } from '../../types';
|
|
12
|
+
import { type TriggerCallback, type TriggerContext, type TriggerFactory } from '../trigger-registry';
|
|
13
|
+
|
|
14
|
+
export const createTimerTrigger: TriggerFactory<TimerTrigger> = async (
|
|
15
|
+
ctx: Context,
|
|
16
|
+
triggerContext: TriggerContext,
|
|
17
|
+
spec: TimerTrigger,
|
|
18
|
+
callback: TriggerCallback,
|
|
19
|
+
) => {
|
|
20
|
+
const task = new DeferredTask(ctx, async () => {
|
|
21
|
+
await callback({});
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
let last = 0;
|
|
25
|
+
let run = 0;
|
|
26
|
+
// https://www.npmjs.com/package/cron#constructor
|
|
27
|
+
const job = CronJob.from({
|
|
28
|
+
cronTime: spec.cron,
|
|
29
|
+
runOnInit: false,
|
|
30
|
+
onTick: () => {
|
|
31
|
+
// TODO(burdon): Check greater than 30s (use cron-parser).
|
|
32
|
+
const now = Date.now();
|
|
33
|
+
const delta = last ? now - last : 0;
|
|
34
|
+
last = now;
|
|
35
|
+
|
|
36
|
+
run++;
|
|
37
|
+
log.info('tick', { space: triggerContext.space.key.truncate(), count: run, delta });
|
|
38
|
+
task.schedule();
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
job.start();
|
|
43
|
+
ctx.onDispose(() => job.stop());
|
|
44
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
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 Context } from '@dxos/context';
|
|
9
|
+
import { log } from '@dxos/log';
|
|
10
|
+
|
|
11
|
+
import type { WebhookTrigger } from '../../types';
|
|
12
|
+
import { type TriggerCallback, type TriggerContext, type TriggerFactory } from '../trigger-registry';
|
|
13
|
+
|
|
14
|
+
export const createWebhookTrigger: TriggerFactory<WebhookTrigger> = async (
|
|
15
|
+
ctx: Context,
|
|
16
|
+
_: TriggerContext,
|
|
17
|
+
spec: WebhookTrigger,
|
|
18
|
+
callback: TriggerCallback,
|
|
19
|
+
) => {
|
|
20
|
+
// TODO(burdon): Enable POST hook with payload.
|
|
21
|
+
const server = http.createServer(async (req, res) => {
|
|
22
|
+
if (req.method !== spec.method) {
|
|
23
|
+
res.statusCode = 405;
|
|
24
|
+
return res.end();
|
|
25
|
+
}
|
|
26
|
+
res.statusCode = await callback({});
|
|
27
|
+
res.end();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
// TODO(burdon): Not used.
|
|
31
|
+
// const DEF_PORT_RANGE = { min: 7500, max: 7599 };
|
|
32
|
+
// const portRange = Object.assign({}, trigger.port, DEF_PORT_RANGE) as WebhookTrigger['port'];
|
|
33
|
+
const port = await getPort({
|
|
34
|
+
random: true,
|
|
35
|
+
// portRange: [portRange!.min, portRange!.max],
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// TODO(burdon): Update trigger object with actual port.
|
|
39
|
+
server.listen(port, () => {
|
|
40
|
+
log.info('started webhook', { port });
|
|
41
|
+
spec.port = port;
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
ctx.onDispose(() => {
|
|
45
|
+
server.close();
|
|
46
|
+
});
|
|
47
|
+
};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import WebSocket from 'ws';
|
|
6
|
+
|
|
7
|
+
import { sleep, Trigger } from '@dxos/async';
|
|
8
|
+
import { type Context } from '@dxos/context';
|
|
9
|
+
import { log } from '@dxos/log';
|
|
10
|
+
|
|
11
|
+
import { type WebsocketTrigger } from '../../types';
|
|
12
|
+
import { type TriggerCallback, type TriggerContext, type TriggerFactory } from '../trigger-registry';
|
|
13
|
+
|
|
14
|
+
interface WebsocketTriggerOptions {
|
|
15
|
+
retryDelay: number;
|
|
16
|
+
maxAttempts: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Websocket.
|
|
21
|
+
* NOTE: The port must be unique, so the same hook cannot be used for multiple spaces.
|
|
22
|
+
*/
|
|
23
|
+
export const createWebsocketTrigger: TriggerFactory<WebsocketTrigger, WebsocketTriggerOptions> = async (
|
|
24
|
+
ctx: Context,
|
|
25
|
+
triggerCtx: TriggerContext,
|
|
26
|
+
spec: WebsocketTrigger,
|
|
27
|
+
callback: TriggerCallback,
|
|
28
|
+
options: WebsocketTriggerOptions = { retryDelay: 2, maxAttempts: 5 },
|
|
29
|
+
) => {
|
|
30
|
+
const { url, init } = spec;
|
|
31
|
+
|
|
32
|
+
let ws: WebSocket;
|
|
33
|
+
for (let attempt = 1; attempt <= options.maxAttempts; attempt++) {
|
|
34
|
+
const open = new Trigger<boolean>();
|
|
35
|
+
|
|
36
|
+
ws = new WebSocket(url);
|
|
37
|
+
Object.assign(ws, {
|
|
38
|
+
onopen: () => {
|
|
39
|
+
log.info('opened', { url });
|
|
40
|
+
if (spec.init) {
|
|
41
|
+
ws.send(new TextEncoder().encode(JSON.stringify(init)));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
open.wake(true);
|
|
45
|
+
},
|
|
46
|
+
|
|
47
|
+
onclose: (event) => {
|
|
48
|
+
log.info('closed', { url, code: event.code });
|
|
49
|
+
// Reconnect if server closes (e.g., CF restart).
|
|
50
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent/code
|
|
51
|
+
if (event.code === 1006) {
|
|
52
|
+
setTimeout(async () => {
|
|
53
|
+
log.info(`reconnecting in ${options.retryDelay}s...`, { url });
|
|
54
|
+
await createWebsocketTrigger(ctx, triggerCtx, spec, callback, options);
|
|
55
|
+
}, options.retryDelay * 1_000);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
open.wake(false);
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
onerror: (event) => {
|
|
62
|
+
log.catch(event.error, { url });
|
|
63
|
+
},
|
|
64
|
+
|
|
65
|
+
onmessage: async (event) => {
|
|
66
|
+
try {
|
|
67
|
+
log.info('message');
|
|
68
|
+
const data = JSON.parse(new TextDecoder().decode(event.data as Uint8Array));
|
|
69
|
+
await callback({ data });
|
|
70
|
+
} catch (err) {
|
|
71
|
+
log.catch(err, { url });
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
} satisfies Partial<WebSocket>);
|
|
75
|
+
|
|
76
|
+
const isOpen = await open.wait();
|
|
77
|
+
if (isOpen) {
|
|
78
|
+
break;
|
|
79
|
+
} else {
|
|
80
|
+
const wait = Math.pow(attempt, 2) * options.retryDelay;
|
|
81
|
+
if (attempt < options.maxAttempts) {
|
|
82
|
+
log.warn(`failed to connect; trying again in ${wait}s`, { attempt });
|
|
83
|
+
await sleep(wait * 1_000);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
ctx.onDispose(() => {
|
|
89
|
+
ws?.close();
|
|
90
|
+
});
|
|
91
|
+
};
|
package/src/types.ts
CHANGED
|
@@ -2,24 +2,19 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import { RawObject, S, TypedObject } from '@dxos/echo-schema';
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const WebsocketTriggerSchema = S.struct({
|
|
16
|
-
url: S.string,
|
|
17
|
-
init: S.optional(S.record(S.string, S.any)),
|
|
18
|
-
});
|
|
7
|
+
/**
|
|
8
|
+
* Type discriminator for TriggerSpec.
|
|
9
|
+
* Every spec has a type field of type FunctionTriggerType that we can use to understand which
|
|
10
|
+
* type we're working with.
|
|
11
|
+
* https://www.typescriptlang.org/docs/handbook/2/narrowing.html#discriminated-unions
|
|
12
|
+
*/
|
|
13
|
+
export type FunctionTriggerType = 'subscription' | 'timer' | 'webhook' | 'websocket';
|
|
19
14
|
|
|
20
15
|
const SubscriptionTriggerSchema = S.struct({
|
|
21
|
-
|
|
22
|
-
// TODO(burdon): Define query DSL.
|
|
16
|
+
type: S.literal('subscription'),
|
|
17
|
+
// TODO(burdon): Define query DSL (from ECHO).
|
|
23
18
|
filter: S.array(
|
|
24
19
|
S.struct({
|
|
25
20
|
type: S.string,
|
|
@@ -36,41 +31,73 @@ const SubscriptionTriggerSchema = S.struct({
|
|
|
36
31
|
),
|
|
37
32
|
});
|
|
38
33
|
|
|
39
|
-
|
|
40
|
-
function: S.string.pipe(S.description('Function ID/URI.')),
|
|
34
|
+
export type SubscriptionTrigger = S.Schema.Type<typeof SubscriptionTriggerSchema>;
|
|
41
35
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
subscription: S.optional(SubscriptionTriggerSchema),
|
|
36
|
+
const TimerTriggerSchema = S.struct({
|
|
37
|
+
type: S.literal('timer'),
|
|
38
|
+
cron: S.string,
|
|
46
39
|
});
|
|
47
40
|
|
|
48
41
|
export type TimerTrigger = S.Schema.Type<typeof TimerTriggerSchema>;
|
|
42
|
+
|
|
43
|
+
const WebhookTriggerSchema = S.mutable(
|
|
44
|
+
S.struct({
|
|
45
|
+
type: S.literal('webhook'),
|
|
46
|
+
method: S.string,
|
|
47
|
+
// Assigned port.
|
|
48
|
+
port: S.optional(S.number),
|
|
49
|
+
}),
|
|
50
|
+
);
|
|
51
|
+
|
|
49
52
|
export type WebhookTrigger = S.Schema.Type<typeof WebhookTriggerSchema>;
|
|
53
|
+
|
|
54
|
+
const WebsocketTriggerSchema = S.struct({
|
|
55
|
+
type: S.literal('websocket'),
|
|
56
|
+
url: S.string,
|
|
57
|
+
init: S.optional(S.record(S.string, S.any)),
|
|
58
|
+
});
|
|
59
|
+
|
|
50
60
|
export type WebsocketTrigger = S.Schema.Type<typeof WebsocketTriggerSchema>;
|
|
51
|
-
|
|
52
|
-
|
|
61
|
+
|
|
62
|
+
const TriggerSpecSchema = S.union(
|
|
63
|
+
TimerTriggerSchema,
|
|
64
|
+
WebhookTriggerSchema,
|
|
65
|
+
WebsocketTriggerSchema,
|
|
66
|
+
SubscriptionTriggerSchema,
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
export type TriggerSpec = TimerTrigger | WebhookTrigger | WebsocketTrigger | SubscriptionTrigger;
|
|
53
70
|
|
|
54
71
|
/**
|
|
55
72
|
* Function definition.
|
|
56
73
|
*/
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
74
|
+
export class FunctionDef extends TypedObject({
|
|
75
|
+
typename: 'dxos.org/type/FunctionDef',
|
|
76
|
+
version: '0.1.0',
|
|
77
|
+
})({
|
|
78
|
+
uri: S.string,
|
|
60
79
|
description: S.optional(S.string),
|
|
61
|
-
|
|
62
|
-
// TODO(burdon): NPM/GitHub URL?
|
|
80
|
+
route: S.string,
|
|
81
|
+
// TODO(burdon): NPM/GitHub/Docker/CF URL?
|
|
63
82
|
handler: S.string,
|
|
64
|
-
})
|
|
83
|
+
}) {}
|
|
65
84
|
|
|
66
|
-
export
|
|
85
|
+
export class FunctionTrigger extends TypedObject({
|
|
86
|
+
typename: 'dxos.org/type/FunctionTrigger',
|
|
87
|
+
version: '0.1.0',
|
|
88
|
+
})({
|
|
89
|
+
function: S.string.pipe(S.description('Function URI.')),
|
|
90
|
+
// Context is merged into the event data passed to the function.
|
|
91
|
+
meta: S.optional(S.object),
|
|
92
|
+
spec: TriggerSpecSchema,
|
|
93
|
+
}) {}
|
|
67
94
|
|
|
68
95
|
/**
|
|
69
96
|
* Function manifest file.
|
|
70
97
|
*/
|
|
71
98
|
export const FunctionManifestSchema = S.struct({
|
|
72
|
-
functions: S.mutable(S.array(
|
|
73
|
-
triggers: S.mutable(S.array(
|
|
99
|
+
functions: S.optional(S.mutable(S.array(RawObject(FunctionDef)))),
|
|
100
|
+
triggers: S.optional(S.mutable(S.array(RawObject(FunctionTrigger)))),
|
|
74
101
|
});
|
|
75
102
|
|
|
76
103
|
export type FunctionManifest = S.Schema.Type<typeof FunctionManifestSchema>;
|
package/src/util.test.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { expect } from 'chai';
|
|
6
|
+
|
|
7
|
+
import { describe, test } from '@dxos/test';
|
|
8
|
+
|
|
9
|
+
import { diff, intersection } from './util';
|
|
10
|
+
|
|
11
|
+
describe('diff', () => {
|
|
12
|
+
test('returns the difference between two sets', () => {
|
|
13
|
+
{
|
|
14
|
+
const { added, updated, removed } = diff<number>([], [], (a, b) => a === b);
|
|
15
|
+
expect(added).to.deep.eq([]);
|
|
16
|
+
expect(updated).to.deep.eq([]);
|
|
17
|
+
expect(removed).to.deep.eq([]);
|
|
18
|
+
}
|
|
19
|
+
{
|
|
20
|
+
const previous = [1, 2, 3];
|
|
21
|
+
const next = [2, 3, 4];
|
|
22
|
+
const { added, updated, removed } = diff(previous, next, (a, b) => a === b);
|
|
23
|
+
expect(added).to.deep.eq([4]);
|
|
24
|
+
expect(updated).to.deep.eq([2, 3]);
|
|
25
|
+
expect(removed).to.deep.eq([1]);
|
|
26
|
+
}
|
|
27
|
+
{
|
|
28
|
+
const previous = [{ x: 1 }, { x: 2 }, { x: 3 }];
|
|
29
|
+
const next = [{ x: 2 }, { x: 3 }, { x: 4 }];
|
|
30
|
+
const { added, updated, removed } = diff(previous, next, (a, b) => a.x === b.x);
|
|
31
|
+
expect(added).to.deep.eq([{ x: 4 }]);
|
|
32
|
+
expect(updated).to.deep.eq([{ x: 2 }, { x: 3 }]);
|
|
33
|
+
expect(removed).to.deep.eq([{ x: 1 }]);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test('intersection', () => {
|
|
38
|
+
expect(intersection([1, 2, 3], [2, 3, 4], (a, b) => a === b)).to.deep.eq([2, 3]);
|
|
39
|
+
expect(
|
|
40
|
+
intersection([{ x: 1 }, { x: 2 }, { x: 3 }], [{ x: 2 }, { x: 3 }, { x: 4 }], (a, b) => a.x === b.x),
|
|
41
|
+
).to.deep.eq([{ x: 2 }, { x: 3 }]);
|
|
42
|
+
});
|
|
43
|
+
});
|
package/src/util.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
export type Comparator<A, B = A> = (a: A, b: B) => boolean;
|
|
6
|
+
|
|
7
|
+
export type DiffResult<A, B = A> = {
|
|
8
|
+
added: B[];
|
|
9
|
+
updated: A[];
|
|
10
|
+
removed: A[];
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
*
|
|
15
|
+
* @param previous
|
|
16
|
+
* @param next
|
|
17
|
+
* @param comparator
|
|
18
|
+
*/
|
|
19
|
+
// TODO(burdon): Factor out.
|
|
20
|
+
export const diff = <A, B = A>(
|
|
21
|
+
previous: readonly A[],
|
|
22
|
+
next: readonly B[],
|
|
23
|
+
comparator: Comparator<A, B>,
|
|
24
|
+
): DiffResult<A, B> => {
|
|
25
|
+
const remaining = [...previous];
|
|
26
|
+
const result: DiffResult<A, B> = {
|
|
27
|
+
added: [],
|
|
28
|
+
updated: [],
|
|
29
|
+
removed: remaining,
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// TODO(burdon): Mark and sweep.
|
|
33
|
+
for (const object of next) {
|
|
34
|
+
const index = remaining.findIndex((item) => comparator(item, object));
|
|
35
|
+
if (index === -1) {
|
|
36
|
+
result.added.push(object);
|
|
37
|
+
} else {
|
|
38
|
+
result.updated.push(remaining[index]);
|
|
39
|
+
remaining.splice(index, 1);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return result;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// TODO(burdon): Factor out.
|
|
47
|
+
export const intersection = <A, B = A>(a: A[], b: B[], comparator: Comparator<A, B>): A[] =>
|
|
48
|
+
a.filter((a) => b.find((b) => comparator(a, b)) !== undefined);
|