@dxos/functions 0.5.3-main.43e79dd → 0.5.3-main.4490e2f

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 (97) hide show
  1. package/dist/lib/browser/chunk-4D4I3YMJ.mjs +86 -0
  2. package/dist/lib/browser/chunk-4D4I3YMJ.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +915 -297
  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 +14 -0
  7. package/dist/lib/browser/types.mjs.map +7 -0
  8. package/dist/lib/node/chunk-3UYUR5N5.cjs +103 -0
  9. package/dist/lib/node/chunk-3UYUR5N5.cjs.map +7 -0
  10. package/dist/lib/node/index.cjs +901 -290
  11. package/dist/lib/node/index.cjs.map +4 -4
  12. package/dist/lib/node/meta.json +1 -1
  13. package/dist/lib/node/types.cjs +35 -0
  14. package/dist/lib/node/types.cjs.map +7 -0
  15. package/dist/types/src/browser/index.d.ts +2 -0
  16. package/dist/types/src/browser/index.d.ts.map +1 -0
  17. package/dist/types/src/function/function-registry.d.ts +24 -0
  18. package/dist/types/src/function/function-registry.d.ts.map +1 -0
  19. package/dist/types/src/function/function-registry.test.d.ts +2 -0
  20. package/dist/types/src/function/function-registry.test.d.ts.map +1 -0
  21. package/dist/types/src/function/index.d.ts +2 -0
  22. package/dist/types/src/function/index.d.ts.map +1 -0
  23. package/dist/types/src/handler.d.ts +33 -12
  24. package/dist/types/src/handler.d.ts.map +1 -1
  25. package/dist/types/src/index.d.ts +3 -1
  26. package/dist/types/src/index.d.ts.map +1 -1
  27. package/dist/types/src/runtime/dev-server.d.ts +15 -7
  28. package/dist/types/src/runtime/dev-server.d.ts.map +1 -1
  29. package/dist/types/src/runtime/dev-server.test.d.ts +2 -0
  30. package/dist/types/src/runtime/dev-server.test.d.ts.map +1 -0
  31. package/dist/types/src/runtime/scheduler.d.ts +15 -15
  32. package/dist/types/src/runtime/scheduler.d.ts.map +1 -1
  33. package/dist/types/src/testing/functions-integration.test.d.ts +2 -0
  34. package/dist/types/src/testing/functions-integration.test.d.ts.map +1 -0
  35. package/dist/types/src/testing/index.d.ts +4 -0
  36. package/dist/types/src/testing/index.d.ts.map +1 -0
  37. package/dist/types/src/testing/setup.d.ts +5 -0
  38. package/dist/types/src/testing/setup.d.ts.map +1 -0
  39. package/dist/types/src/testing/test/handler.d.ts +4 -0
  40. package/dist/types/src/testing/test/handler.d.ts.map +1 -0
  41. package/dist/types/src/testing/test/index.d.ts +3 -0
  42. package/dist/types/src/testing/test/index.d.ts.map +1 -0
  43. package/dist/types/src/testing/types.d.ts +9 -0
  44. package/dist/types/src/testing/types.d.ts.map +1 -0
  45. package/dist/types/src/testing/util.d.ts +3 -0
  46. package/dist/types/src/testing/util.d.ts.map +1 -0
  47. package/dist/types/src/trigger/index.d.ts +2 -0
  48. package/dist/types/src/trigger/index.d.ts.map +1 -0
  49. package/dist/types/src/trigger/trigger-registry.d.ts +37 -0
  50. package/dist/types/src/trigger/trigger-registry.d.ts.map +1 -0
  51. package/dist/types/src/trigger/trigger-registry.test.d.ts +2 -0
  52. package/dist/types/src/trigger/trigger-registry.test.d.ts.map +1 -0
  53. package/dist/types/src/trigger/type/index.d.ts +5 -0
  54. package/dist/types/src/trigger/type/index.d.ts.map +1 -0
  55. package/dist/types/src/trigger/type/subscription-trigger.d.ts +4 -0
  56. package/dist/types/src/trigger/type/subscription-trigger.d.ts.map +1 -0
  57. package/dist/types/src/trigger/type/timer-trigger.d.ts +4 -0
  58. package/dist/types/src/trigger/type/timer-trigger.d.ts.map +1 -0
  59. package/dist/types/src/trigger/type/webhook-trigger.d.ts +4 -0
  60. package/dist/types/src/trigger/type/webhook-trigger.d.ts.map +1 -0
  61. package/dist/types/src/trigger/type/websocket-trigger.d.ts +13 -0
  62. package/dist/types/src/trigger/type/websocket-trigger.d.ts.map +1 -0
  63. package/dist/types/src/types.d.ts +215 -0
  64. package/dist/types/src/types.d.ts.map +1 -0
  65. package/dist/types/tools/schema.d.ts +2 -0
  66. package/dist/types/tools/schema.d.ts.map +1 -0
  67. package/package.json +38 -13
  68. package/schema/functions.json +211 -0
  69. package/src/browser/index.ts +5 -0
  70. package/src/function/function-registry.test.ts +105 -0
  71. package/src/function/function-registry.ts +90 -0
  72. package/src/function/index.ts +5 -0
  73. package/src/handler.ts +56 -26
  74. package/src/index.ts +3 -1
  75. package/src/runtime/dev-server.test.ts +60 -0
  76. package/src/runtime/dev-server.ts +104 -53
  77. package/src/runtime/scheduler.test.ts +159 -21
  78. package/src/runtime/scheduler.ts +87 -150
  79. package/src/testing/functions-integration.test.ts +100 -0
  80. package/src/testing/index.ts +7 -0
  81. package/src/testing/setup.ts +43 -0
  82. package/src/testing/test/handler.ts +15 -0
  83. package/src/testing/test/index.ts +7 -0
  84. package/src/testing/types.ts +9 -0
  85. package/src/testing/util.ts +16 -0
  86. package/src/trigger/index.ts +5 -0
  87. package/src/trigger/trigger-registry.test.ts +272 -0
  88. package/src/trigger/trigger-registry.ts +211 -0
  89. package/src/trigger/type/index.ts +8 -0
  90. package/src/trigger/type/subscription-trigger.ts +86 -0
  91. package/src/trigger/type/timer-trigger.ts +45 -0
  92. package/src/trigger/type/webhook-trigger.ts +48 -0
  93. package/src/trigger/type/websocket-trigger.ts +92 -0
  94. package/src/types.ts +115 -0
  95. package/dist/types/src/manifest.d.ts +0 -26
  96. package/dist/types/src/manifest.d.ts.map +0 -1
  97. package/src/manifest.ts +0 -42
@@ -0,0 +1,92 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import WebSocket from 'ws';
6
+
7
+ import { sleep, Trigger } 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 WebsocketTrigger } from '../../types';
13
+ import { type TriggerCallback, type TriggerFactory } from '../trigger-registry';
14
+
15
+ interface WebsocketTriggerOptions {
16
+ retryDelay: number;
17
+ maxAttempts: number;
18
+ }
19
+
20
+ /**
21
+ * Websocket.
22
+ * NOTE: The port must be unique, so the same hook cannot be used for multiple spaces.
23
+ */
24
+ export const createWebsocketTrigger: TriggerFactory<WebsocketTrigger, WebsocketTriggerOptions> = async (
25
+ ctx: Context,
26
+ space: Space,
27
+ spec: WebsocketTrigger,
28
+ callback: TriggerCallback,
29
+ options: WebsocketTriggerOptions = { retryDelay: 2, maxAttempts: 5 },
30
+ ) => {
31
+ const { url, init } = spec;
32
+
33
+ let ws: WebSocket;
34
+ for (let attempt = 1; attempt <= options.maxAttempts; attempt++) {
35
+ const open = new Trigger<boolean>();
36
+
37
+ ws = new WebSocket(url);
38
+ Object.assign(ws, {
39
+ onopen: () => {
40
+ log.info('opened', { url });
41
+ if (spec.init) {
42
+ ws.send(new TextEncoder().encode(JSON.stringify(init)));
43
+ }
44
+
45
+ open.wake(true);
46
+ },
47
+
48
+ onclose: (event) => {
49
+ log.info('closed', { url, code: event.code });
50
+ // Reconnect if server closes (e.g., CF restart).
51
+ // https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent/code
52
+ if (event.code === 1006) {
53
+ setTimeout(async () => {
54
+ log.info(`reconnecting in ${options.retryDelay}s...`, { url });
55
+ await createWebsocketTrigger(ctx, space, spec, callback, options);
56
+ }, options.retryDelay * 1_000);
57
+ }
58
+
59
+ open.wake(false);
60
+ },
61
+
62
+ onerror: (event) => {
63
+ log.catch(event.error, { url });
64
+ },
65
+
66
+ onmessage: async (event) => {
67
+ try {
68
+ log.info('message');
69
+ const data = JSON.parse(new TextDecoder().decode(event.data as Uint8Array));
70
+ await callback({ data });
71
+ } catch (err) {
72
+ log.catch(err, { url });
73
+ }
74
+ },
75
+ } satisfies Partial<WebSocket>);
76
+
77
+ const isOpen = await open.wait();
78
+ if (isOpen) {
79
+ break;
80
+ } else {
81
+ const wait = Math.pow(attempt, 2) * options.retryDelay;
82
+ if (attempt < options.maxAttempts) {
83
+ log.warn(`failed to connect; trying again in ${wait}s`, { attempt });
84
+ await sleep(wait * 1_000);
85
+ }
86
+ }
87
+ }
88
+
89
+ ctx.onDispose(() => {
90
+ ws?.close();
91
+ });
92
+ };
package/src/types.ts ADDED
@@ -0,0 +1,115 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import { RawObject, S, TypedObject } from '@dxos/echo-schema';
6
+
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';
14
+
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
+ );
35
+
36
+ export type SubscriptionTrigger = S.Schema.Type<typeof SubscriptionTriggerSchema>;
37
+
38
+ const TimerTriggerSchema = S.mutable(
39
+ S.struct({
40
+ type: S.literal('timer'),
41
+ cron: S.string,
42
+ }),
43
+ );
44
+
45
+ export type TimerTrigger = S.Schema.Type<typeof TimerTriggerSchema>;
46
+
47
+ const WebhookTriggerSchema = S.mutable(
48
+ S.struct({
49
+ type: S.literal('webhook'),
50
+ method: S.string,
51
+ // Assigned port.
52
+ port: S.optional(S.number),
53
+ }),
54
+ );
55
+
56
+ export type WebhookTrigger = S.Schema.Type<typeof WebhookTriggerSchema>;
57
+
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
+ );
65
+
66
+ export type WebsocketTrigger = S.Schema.Type<typeof WebsocketTriggerSchema>;
67
+
68
+ const TriggerSpecSchema = S.union(
69
+ TimerTriggerSchema,
70
+ WebhookTriggerSchema,
71
+ WebsocketTriggerSchema,
72
+ SubscriptionTriggerSchema,
73
+ );
74
+
75
+ export type TriggerSpec = TimerTrigger | WebhookTrigger | WebsocketTrigger | SubscriptionTrigger;
76
+
77
+ /**
78
+ * Function definition.
79
+ */
80
+ export class FunctionDef extends TypedObject({
81
+ typename: 'dxos.org/type/FunctionDef',
82
+ version: '0.1.0',
83
+ })({
84
+ uri: S.string,
85
+ description: S.optional(S.string),
86
+ route: S.string,
87
+ handler: S.string,
88
+ }) {}
89
+
90
+ /**
91
+ * Function trigger.
92
+ */
93
+ export class FunctionTrigger extends TypedObject({
94
+ typename: 'dxos.org/type/FunctionTrigger',
95
+ version: '0.1.0',
96
+ })({
97
+ enabled: S.optional(S.boolean),
98
+ function: S.string.pipe(S.description('Function URI.')),
99
+ // The `meta` property is merged into the event data passed to the function.
100
+ meta: S.optional(S.mutable(S.any)),
101
+ spec: TriggerSpecSchema,
102
+ }) {}
103
+
104
+ /**
105
+ * Function manifest file.
106
+ */
107
+ export const FunctionManifestSchema = S.struct({
108
+ functions: S.optional(S.mutable(S.array(RawObject(FunctionDef)))),
109
+ triggers: S.optional(S.mutable(S.array(RawObject(FunctionTrigger)))),
110
+ });
111
+
112
+ export type FunctionManifest = S.Schema.Type<typeof FunctionManifestSchema>;
113
+
114
+ // TODO(burdon): Standards?
115
+ export const FUNCTION_SCHEMA = [FunctionDef, FunctionTrigger];
@@ -1,26 +0,0 @@
1
- export type FunctionDef = {
2
- id: string;
3
- name: string;
4
- handler: string;
5
- description?: string;
6
- };
7
- export type TriggerSubscription = {
8
- type: string;
9
- spaceKey: string;
10
- props?: Record<string, any>;
11
- deep?: boolean;
12
- delay?: number;
13
- };
14
- export type FunctionTrigger = {
15
- function: string;
16
- schedule?: string;
17
- subscriptions?: TriggerSubscription[];
18
- };
19
- /**
20
- * Function manifest file.
21
- */
22
- export type FunctionManifest = {
23
- functions: FunctionDef[];
24
- triggers: FunctionTrigger[];
25
- };
26
- //# sourceMappingURL=manifest.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"manifest.d.ts","sourceRoot":"","sources":["../../../src/manifest.ts"],"names":[],"mappings":"AAOA,MAAM,MAAM,WAAW,GAAG;IAExB,EAAE,EAAE,MAAM,CAAC;IAEX,IAAI,EAAE,MAAM,CAAC;IAEb,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAGF,MAAM,MAAM,mBAAmB,GAAG;IAChC,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC5B,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAKF,MAAM,MAAM,eAAe,GAAG;IAC5B,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,aAAa,CAAC,EAAE,mBAAmB,EAAE,CAAC;CACvC,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B,SAAS,EAAE,WAAW,EAAE,CAAC;IACzB,QAAQ,EAAE,eAAe,EAAE,CAAC;CAC7B,CAAC"}
package/src/manifest.ts DELETED
@@ -1,42 +0,0 @@
1
- //
2
- // Copyright 2023 DXOS.org
3
- //
4
-
5
- // Lambda-like function definitions.
6
- // See: https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml/#functions
7
-
8
- export type FunctionDef = {
9
- // FQ function name.
10
- id: string;
11
- // URL path.
12
- name: string;
13
- // File path of handler.
14
- handler: string;
15
- description?: string;
16
- };
17
-
18
- // TODO(burdon): Query DSL.
19
- export type TriggerSubscription = {
20
- type: string;
21
- spaceKey: string;
22
- props?: Record<string, any>;
23
- deep?: boolean; // Watch changes to object (not just creation).
24
- delay?: number;
25
- };
26
-
27
- // TODO(burdon): Generalize binding.
28
- // https://www.npmjs.com/package/aws-lambda
29
- // https://docs.aws.amazon.com/lambda/latest/dg/typescript-handler.html
30
- export type FunctionTrigger = {
31
- function: string;
32
- schedule?: string;
33
- subscriptions?: TriggerSubscription[];
34
- };
35
-
36
- /**
37
- * Function manifest file.
38
- */
39
- export type FunctionManifest = {
40
- functions: FunctionDef[];
41
- triggers: FunctionTrigger[];
42
- };