@dxos/functions 0.5.3-main.a3ea5f1 → 0.5.3-main.ac34611

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.
Files changed (36) hide show
  1. package/dist/lib/browser/{chunk-366QG6IX.mjs → chunk-HRU7VDYL.mjs} +14 -8
  2. package/dist/lib/browser/chunk-HRU7VDYL.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +85 -49
  4. package/dist/lib/browser/index.mjs.map +4 -4
  5. package/dist/lib/browser/meta.json +1 -1
  6. package/dist/lib/browser/types.mjs +3 -1
  7. package/dist/lib/node/{chunk-3VSJ57ZZ.cjs → chunk-R5JUDLSN.cjs} +17 -10
  8. package/dist/lib/node/chunk-R5JUDLSN.cjs.map +7 -0
  9. package/dist/lib/node/index.cjs +100 -60
  10. package/dist/lib/node/index.cjs.map +4 -4
  11. package/dist/lib/node/meta.json +1 -1
  12. package/dist/lib/node/types.cjs +6 -4
  13. package/dist/lib/node/types.cjs.map +2 -2
  14. package/dist/types/src/browser/index.d.ts +2 -0
  15. package/dist/types/src/browser/index.d.ts.map +1 -0
  16. package/dist/types/src/function/function-registry.d.ts.map +1 -1
  17. package/dist/types/src/handler.d.ts.map +1 -1
  18. package/dist/types/src/runtime/scheduler.d.ts +1 -1
  19. package/dist/types/src/runtime/scheduler.d.ts.map +1 -1
  20. package/dist/types/src/trigger/trigger-registry.d.ts.map +1 -1
  21. package/dist/types/src/types.d.ts +34 -30
  22. package/dist/types/src/types.d.ts.map +1 -1
  23. package/package.json +14 -14
  24. package/src/browser/index.ts +5 -0
  25. package/src/function/function-registry.ts +5 -5
  26. package/src/runtime/scheduler.ts +7 -2
  27. package/src/trigger/trigger-registry.ts +23 -11
  28. package/src/types.ts +37 -27
  29. package/dist/lib/browser/chunk-366QG6IX.mjs.map +0 -7
  30. package/dist/lib/node/chunk-3VSJ57ZZ.cjs.map +0 -7
  31. package/dist/types/src/util.d.ts +0 -15
  32. package/dist/types/src/util.d.ts.map +0 -1
  33. package/dist/types/src/util.test.d.ts +0 -2
  34. package/dist/types/src/util.test.d.ts.map +0 -1
  35. package/src/util.test.ts +0 -43
  36. package/src/util.ts +0 -48
package/src/types.ts CHANGED
@@ -12,31 +12,35 @@ import { RawObject, S, TypedObject } from '@dxos/echo-schema';
12
12
  */
13
13
  export type FunctionTriggerType = 'subscription' | 'timer' | 'webhook' | 'websocket';
14
14
 
15
- const SubscriptionTriggerSchema = S.struct({
16
- type: S.literal('subscription'),
17
- // TODO(burdon): Define query DSL (from ECHO).
18
- filter: S.array(
19
- S.struct({
20
- type: S.string,
21
- props: S.optional(S.record(S.string, S.any)),
22
- }),
23
- ),
24
- options: S.optional(
25
- S.struct({
26
- // Watch changes to object (not just creation).
27
- deep: S.optional(S.boolean),
28
- // Debounce changes (delay in ms).
29
- delay: S.optional(S.number),
30
- }),
31
- ),
32
- });
15
+ const SubscriptionTriggerSchema = S.mutable(
16
+ S.struct({
17
+ type: S.literal('subscription'),
18
+ // TODO(burdon): Define query DSL (from ECHO).
19
+ filter: S.array(
20
+ S.struct({
21
+ type: S.string,
22
+ props: S.optional(S.record(S.string, S.any)),
23
+ }),
24
+ ),
25
+ options: S.optional(
26
+ S.struct({
27
+ // Watch changes to object (not just creation).
28
+ deep: S.optional(S.boolean),
29
+ // Debounce changes (delay in ms).
30
+ delay: S.optional(S.number),
31
+ }),
32
+ ),
33
+ }),
34
+ );
33
35
 
34
36
  export type SubscriptionTrigger = S.Schema.Type<typeof SubscriptionTriggerSchema>;
35
37
 
36
- const TimerTriggerSchema = S.struct({
37
- type: S.literal('timer'),
38
- cron: S.string,
39
- });
38
+ const TimerTriggerSchema = S.mutable(
39
+ S.struct({
40
+ type: S.literal('timer'),
41
+ cron: S.string,
42
+ }),
43
+ );
40
44
 
41
45
  export type TimerTrigger = S.Schema.Type<typeof TimerTriggerSchema>;
42
46
 
@@ -51,11 +55,13 @@ const WebhookTriggerSchema = S.mutable(
51
55
 
52
56
  export type WebhookTrigger = S.Schema.Type<typeof WebhookTriggerSchema>;
53
57
 
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
- });
58
+ const WebsocketTriggerSchema = S.mutable(
59
+ S.struct({
60
+ type: S.literal('websocket'),
61
+ url: S.string,
62
+ init: S.optional(S.record(S.string, S.any)),
63
+ }),
64
+ );
59
65
 
60
66
  export type WebsocketTrigger = S.Schema.Type<typeof WebsocketTriggerSchema>;
61
67
 
@@ -86,6 +92,7 @@ export class FunctionTrigger extends TypedObject({
86
92
  typename: 'dxos.org/type/FunctionTrigger',
87
93
  version: '0.1.0',
88
94
  })({
95
+ enabled: S.optional(S.boolean),
89
96
  function: S.string.pipe(S.description('Function URI.')),
90
97
  // Context is merged into the event data passed to the function.
91
98
  meta: S.optional(S.object),
@@ -101,3 +108,6 @@ export const FunctionManifestSchema = S.struct({
101
108
  });
102
109
 
103
110
  export type FunctionManifest = S.Schema.Type<typeof FunctionManifestSchema>;
111
+
112
+ // TODO(burdon): Standards?
113
+ export const FUNCTION_SCHEMA = [FunctionDef, FunctionTrigger];
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../../src/types.ts"],
4
- "sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { RawObject, S, TypedObject } from '@dxos/echo-schema';\n\n/**\n * Type discriminator for TriggerSpec.\n * Every spec has a type field of type FunctionTriggerType that we can use to understand which\n * type we're working with.\n * https://www.typescriptlang.org/docs/handbook/2/narrowing.html#discriminated-unions\n */\nexport type FunctionTriggerType = 'subscription' | 'timer' | 'webhook' | 'websocket';\n\nconst SubscriptionTriggerSchema = S.struct({\n type: S.literal('subscription'),\n // TODO(burdon): Define query DSL (from ECHO).\n filter: S.array(\n S.struct({\n type: S.string,\n props: S.optional(S.record(S.string, S.any)),\n }),\n ),\n options: S.optional(\n S.struct({\n // Watch changes to object (not just creation).\n deep: S.optional(S.boolean),\n // Debounce changes (delay in ms).\n delay: S.optional(S.number),\n }),\n ),\n});\n\nexport type SubscriptionTrigger = S.Schema.Type<typeof SubscriptionTriggerSchema>;\n\nconst TimerTriggerSchema = S.struct({\n type: S.literal('timer'),\n cron: S.string,\n});\n\nexport type TimerTrigger = S.Schema.Type<typeof TimerTriggerSchema>;\n\nconst WebhookTriggerSchema = S.mutable(\n S.struct({\n type: S.literal('webhook'),\n method: S.string,\n // Assigned port.\n port: S.optional(S.number),\n }),\n);\n\nexport type WebhookTrigger = S.Schema.Type<typeof WebhookTriggerSchema>;\n\nconst WebsocketTriggerSchema = S.struct({\n type: S.literal('websocket'),\n url: S.string,\n init: S.optional(S.record(S.string, S.any)),\n});\n\nexport type WebsocketTrigger = S.Schema.Type<typeof WebsocketTriggerSchema>;\n\nconst TriggerSpecSchema = S.union(\n TimerTriggerSchema,\n WebhookTriggerSchema,\n WebsocketTriggerSchema,\n SubscriptionTriggerSchema,\n);\n\nexport type TriggerSpec = TimerTrigger | WebhookTrigger | WebsocketTrigger | SubscriptionTrigger;\n\n/**\n * Function definition.\n */\nexport class FunctionDef extends TypedObject({\n typename: 'dxos.org/type/FunctionDef',\n version: '0.1.0',\n})({\n uri: S.string,\n description: S.optional(S.string),\n route: S.string,\n // TODO(burdon): NPM/GitHub/Docker/CF URL?\n handler: S.string,\n}) {}\n\nexport class FunctionTrigger extends TypedObject({\n typename: 'dxos.org/type/FunctionTrigger',\n version: '0.1.0',\n})({\n function: S.string.pipe(S.description('Function URI.')),\n // Context is merged into the event data passed to the function.\n meta: S.optional(S.object),\n spec: TriggerSpecSchema,\n}) {}\n\n/**\n * Function manifest file.\n */\nexport const FunctionManifestSchema = S.struct({\n functions: S.optional(S.mutable(S.array(RawObject(FunctionDef)))),\n triggers: S.optional(S.mutable(S.array(RawObject(FunctionTrigger)))),\n});\n\nexport type FunctionManifest = S.Schema.Type<typeof FunctionManifestSchema>;\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;AAIA,SAASA,WA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X,CAAA,EAAG;EACDM,UAAUpC,EAAEM,OAAO+B,KAAKrC,EAAEgC,YAAY,eAAA,CAAA;;EAEtCM,MAAMtC,EAAEQ,SAASR,EAAEuC,MAAM;EACzBC,MAAMf;AACR,CAAA,EAAA;AAAI;AAKG,IAAMgB,yBAAyBzC,EAAEC,OAAO;EAC7CyC,WAAW1C,EAAEQ,SAASR,EAAEmB,QAAQnB,EAAEK,MAAMsC,UAAUhB,WAAAA,CAAAA,CAAAA,CAAAA;EAClDiB,UAAU5C,EAAEQ,SAASR,EAAEmB,QAAQnB,EAAEK,MAAMsC,UAAUR,eAAAA,CAAAA,CAAAA,CAAAA;AACnD,CAAA;",
6
- "names": ["RawObject", "S", "TypedObject", "SubscriptionTriggerSchema", "S", "struct", "type", "literal", "filter", "array", "string", "props", "optional", "record", "any", "options", "deep", "boolean", "delay", "number", "TimerTriggerSchema", "cron", "WebhookTriggerSchema", "mutable", "method", "port", "WebsocketTriggerSchema", "url", "init", "TriggerSpecSchema", "union", "FunctionDef", "TypedObject", "typename", "version", "uri", "description", "route", "handler", "FunctionTrigger", "function", "pipe", "meta", "object", "spec", "FunctionManifestSchema", "functions", "RawObject", "triggers"]
7
- }
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../../src/types.ts"],
4
- "sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { RawObject, S, TypedObject } from '@dxos/echo-schema';\n\n/**\n * Type discriminator for TriggerSpec.\n * Every spec has a type field of type FunctionTriggerType that we can use to understand which\n * type we're working with.\n * https://www.typescriptlang.org/docs/handbook/2/narrowing.html#discriminated-unions\n */\nexport type FunctionTriggerType = 'subscription' | 'timer' | 'webhook' | 'websocket';\n\nconst SubscriptionTriggerSchema = S.struct({\n type: S.literal('subscription'),\n // TODO(burdon): Define query DSL (from ECHO).\n filter: S.array(\n S.struct({\n type: S.string,\n props: S.optional(S.record(S.string, S.any)),\n }),\n ),\n options: S.optional(\n S.struct({\n // Watch changes to object (not just creation).\n deep: S.optional(S.boolean),\n // Debounce changes (delay in ms).\n delay: S.optional(S.number),\n }),\n ),\n});\n\nexport type SubscriptionTrigger = S.Schema.Type<typeof SubscriptionTriggerSchema>;\n\nconst TimerTriggerSchema = S.struct({\n type: S.literal('timer'),\n cron: S.string,\n});\n\nexport type TimerTrigger = S.Schema.Type<typeof TimerTriggerSchema>;\n\nconst WebhookTriggerSchema = S.mutable(\n S.struct({\n type: S.literal('webhook'),\n method: S.string,\n // Assigned port.\n port: S.optional(S.number),\n }),\n);\n\nexport type WebhookTrigger = S.Schema.Type<typeof WebhookTriggerSchema>;\n\nconst WebsocketTriggerSchema = S.struct({\n type: S.literal('websocket'),\n url: S.string,\n init: S.optional(S.record(S.string, S.any)),\n});\n\nexport type WebsocketTrigger = S.Schema.Type<typeof WebsocketTriggerSchema>;\n\nconst TriggerSpecSchema = S.union(\n TimerTriggerSchema,\n WebhookTriggerSchema,\n WebsocketTriggerSchema,\n SubscriptionTriggerSchema,\n);\n\nexport type TriggerSpec = TimerTrigger | WebhookTrigger | WebsocketTrigger | SubscriptionTrigger;\n\n/**\n * Function definition.\n */\nexport class FunctionDef extends TypedObject({\n typename: 'dxos.org/type/FunctionDef',\n version: '0.1.0',\n})({\n uri: S.string,\n description: S.optional(S.string),\n route: S.string,\n // TODO(burdon): NPM/GitHub/Docker/CF URL?\n handler: S.string,\n}) {}\n\nexport class FunctionTrigger extends TypedObject({\n typename: 'dxos.org/type/FunctionTrigger',\n version: '0.1.0',\n})({\n function: S.string.pipe(S.description('Function URI.')),\n // Context is merged into the event data passed to the function.\n meta: S.optional(S.object),\n spec: TriggerSpecSchema,\n}) {}\n\n/**\n * Function manifest file.\n */\nexport const FunctionManifestSchema = S.struct({\n functions: S.optional(S.mutable(S.array(RawObject(FunctionDef)))),\n triggers: S.optional(S.mutable(S.array(RawObject(FunctionTrigger)))),\n});\n\nexport type FunctionManifest = S.Schema.Type<typeof FunctionManifestSchema>;\n"],
5
- "mappings": ";;;;;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AA;AAAI;AAEG,IAAM6B,kBAAN,kBAA8BP,gCAAY;EAC/CC,UAAU;EACVC,SAAS;AACX,CAAA,EAAG;EACDM,UAAUpC,qBAAEM,OAAO+B,KAAKrC,qBAAEgC,YAAY,eAAA,CAAA;;EAEtCM,MAAMtC,qBAAEQ,SAASR,qBAAEuC,MAAM;EACzBC,MAAMf;AACR,CAAA,EAAA;AAAI;AAKG,IAAMgB,yBAAyBzC,qBAAEC,OAAO;EAC7CyC,WAAW1C,qBAAEQ,SAASR,qBAAEmB,QAAQnB,qBAAEK,UAAMsC,8BAAUhB,WAAAA,CAAAA,CAAAA,CAAAA;EAClDiB,UAAU5C,qBAAEQ,SAASR,qBAAEmB,QAAQnB,qBAAEK,UAAMsC,8BAAUR,eAAAA,CAAAA,CAAAA,CAAAA;AACnD,CAAA;",
6
- "names": ["SubscriptionTriggerSchema", "S", "struct", "type", "literal", "filter", "array", "string", "props", "optional", "record", "any", "options", "deep", "boolean", "delay", "number", "TimerTriggerSchema", "cron", "WebhookTriggerSchema", "mutable", "method", "port", "WebsocketTriggerSchema", "url", "init", "TriggerSpecSchema", "union", "FunctionDef", "TypedObject", "typename", "version", "uri", "description", "route", "handler", "FunctionTrigger", "function", "pipe", "meta", "object", "spec", "FunctionManifestSchema", "functions", "RawObject", "triggers"]
7
- }
@@ -1,15 +0,0 @@
1
- export type Comparator<A, B = A> = (a: A, b: B) => boolean;
2
- export type DiffResult<A, B = A> = {
3
- added: B[];
4
- updated: A[];
5
- removed: A[];
6
- };
7
- /**
8
- *
9
- * @param previous
10
- * @param next
11
- * @param comparator
12
- */
13
- export declare const diff: <A, B = A>(previous: readonly A[], next: readonly B[], comparator: Comparator<A, B>) => DiffResult<A, B>;
14
- export declare const intersection: <A, B = A>(a: A[], b: B[], comparator: Comparator<A, B>) => A[];
15
- //# sourceMappingURL=util.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../../src/util.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,UAAU,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,OAAO,CAAC;AAE3D,MAAM,MAAM,UAAU,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,IAAI;IACjC,KAAK,EAAE,CAAC,EAAE,CAAC;IACX,OAAO,EAAE,CAAC,EAAE,CAAC;IACb,OAAO,EAAE,CAAC,EAAE,CAAC;CACd,CAAC;AAEF;;;;;GAKG;AAEH,eAAO,MAAM,IAAI,0GAwBhB,CAAC;AAGF,eAAO,MAAM,YAAY,iEACuC,CAAC"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=util.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"util.test.d.ts","sourceRoot":"","sources":["../../../src/util.test.ts"],"names":[],"mappings":""}
package/src/util.test.ts DELETED
@@ -1,43 +0,0 @@
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 DELETED
@@ -1,48 +0,0 @@
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);