@dxos/functions 0.7.1-staging.8709ba7 → 0.7.2-main.f1adc9f

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 (53) hide show
  1. package/dist/lib/browser/{chunk-QX2SGAWR.mjs → chunk-L7APL3RG.mjs} +28 -159
  2. package/dist/lib/browser/chunk-L7APL3RG.mjs.map +7 -0
  3. package/dist/lib/browser/{chunk-FWIFI2MH.mjs → chunk-N5D5R4Q4.mjs} +29 -16
  4. package/dist/lib/browser/chunk-N5D5R4Q4.mjs.map +7 -0
  5. package/dist/lib/browser/index.mjs +5 -7
  6. package/dist/lib/browser/index.mjs.map +2 -2
  7. package/dist/lib/browser/meta.json +1 -1
  8. package/dist/lib/browser/testing/index.mjs +5 -3
  9. package/dist/lib/browser/testing/index.mjs.map +3 -3
  10. package/dist/lib/browser/types.mjs +3 -1
  11. package/dist/lib/node/{chunk-OKV7IWWB.cjs → chunk-5ZUMVNC3.cjs} +45 -176
  12. package/dist/lib/node/chunk-5ZUMVNC3.cjs.map +7 -0
  13. package/dist/lib/node/{chunk-GKDHAWCP.cjs → chunk-JMJCJ3SA.cjs} +32 -18
  14. package/dist/lib/node/chunk-JMJCJ3SA.cjs.map +7 -0
  15. package/dist/lib/node/index.cjs +14 -16
  16. package/dist/lib/node/index.cjs.map +2 -2
  17. package/dist/lib/node/meta.json +1 -1
  18. package/dist/lib/node/testing/index.cjs +13 -11
  19. package/dist/lib/node/testing/index.cjs.map +3 -3
  20. package/dist/lib/node/types.cjs +9 -7
  21. package/dist/lib/node/types.cjs.map +2 -2
  22. package/dist/lib/node-esm/{chunk-YSKH6PBN.mjs → chunk-2NACE6MJ.mjs} +29 -16
  23. package/dist/lib/node-esm/chunk-2NACE6MJ.mjs.map +7 -0
  24. package/dist/lib/node-esm/{chunk-SPHZ5SOR.mjs → chunk-EREF4EDR.mjs} +28 -159
  25. package/dist/lib/node-esm/chunk-EREF4EDR.mjs.map +7 -0
  26. package/dist/lib/node-esm/index.mjs +5 -7
  27. package/dist/lib/node-esm/index.mjs.map +2 -2
  28. package/dist/lib/node-esm/meta.json +1 -1
  29. package/dist/lib/node-esm/testing/index.mjs +5 -3
  30. package/dist/lib/node-esm/testing/index.mjs.map +3 -3
  31. package/dist/lib/node-esm/types.mjs +3 -1
  32. package/dist/types/src/handler.d.ts +2 -2
  33. package/dist/types/src/trigger/type/index.d.ts +0 -1
  34. package/dist/types/src/trigger/type/index.d.ts.map +1 -1
  35. package/dist/types/src/types.d.ts +49 -83
  36. package/dist/types/src/types.d.ts.map +1 -1
  37. package/package.json +15 -15
  38. package/src/handler.ts +3 -3
  39. package/src/runtime/scheduler.test.ts +4 -55
  40. package/src/runtime/scheduler.ts +1 -1
  41. package/src/testing/functions-integration.test.ts +2 -2
  42. package/src/trigger/trigger-registry.test.ts +4 -4
  43. package/src/trigger/type/index.ts +0 -1
  44. package/src/types.ts +28 -25
  45. package/dist/lib/browser/chunk-FWIFI2MH.mjs.map +0 -7
  46. package/dist/lib/browser/chunk-QX2SGAWR.mjs.map +0 -7
  47. package/dist/lib/node/chunk-GKDHAWCP.cjs.map +0 -7
  48. package/dist/lib/node/chunk-OKV7IWWB.cjs.map +0 -7
  49. package/dist/lib/node-esm/chunk-SPHZ5SOR.mjs.map +0 -7
  50. package/dist/lib/node-esm/chunk-YSKH6PBN.mjs.map +0 -7
  51. package/dist/types/src/trigger/type/websocket-trigger.d.ts +0 -14
  52. package/dist/types/src/trigger/type/websocket-trigger.d.ts.map +0 -1
  53. package/src/trigger/type/websocket-trigger.ts +0 -112
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../../src/types.ts"],
4
- "sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { AST, RawObject, S, TypedObject } from '@dxos/echo-schema';\n\n/**\n * Type discriminator for TriggerType.\n * Every spec has a type field of type TriggerKind that we can use to understand which type we're working with.\n * https://www.typescriptlang.org/docs/handbook/2/narrowing.html#discriminated-unions\n */\nexport type TriggerKind = 'timer' | 'webhook' | 'websocket' | 'subscription';\n\n// TODO(burdon): Rename prop kind.\nconst typeLiteralAnnotations = { [AST.TitleAnnotationId]: 'Type' };\n\n/**\n * Cron timer.\n */\nconst TimerTriggerSchema = S.Struct({\n type: S.Literal('timer').annotations(typeLiteralAnnotations),\n cron: S.NonEmptyString.annotations({\n [AST.TitleAnnotationId]: 'Cron',\n [AST.ExamplesAnnotationId]: '0 0 * * *',\n }),\n}).pipe(S.mutable);\n\nexport type TimerTrigger = S.Schema.Type<typeof TimerTriggerSchema>;\n\n/**\n * Webhook.\n */\nconst WebhookTriggerSchema = S.Struct({\n type: S.Literal('webhook').annotations(typeLiteralAnnotations),\n method: S.String,\n // Assigned port.\n port: S.optional(S.Number),\n}).pipe(S.mutable);\n\nexport type WebhookTrigger = S.Schema.Type<typeof WebhookTriggerSchema>;\n\n/**\n * Websocket.\n * @deprecated\n */\nconst WebsocketTriggerSchema = S.Struct({\n type: S.Literal('websocket').annotations(typeLiteralAnnotations),\n url: S.String,\n init: S.optional(S.Record({ key: S.String, value: S.Any })),\n}).pipe(S.mutable);\n\nexport type WebsocketTrigger = S.Schema.Type<typeof WebsocketTriggerSchema>;\n\n// TODO(burdon): Use ECHO definition (from https://github.com/dxos/dxos/pull/8233).\nconst QuerySchema = S.Struct({\n type: S.optional(S.String.annotations({ [AST.TitleAnnotationId]: 'Type' })),\n props: S.optional(S.Record({ key: S.String, value: S.Any })),\n}).annotations({ [AST.TitleAnnotationId]: 'Query' });\n\n/**\n * Subscription.\n */\nconst SubscriptionTriggerSchema = S.Struct({\n type: S.Literal('subscription').annotations(typeLiteralAnnotations),\n // TODO(burdon): Define query DSL (from ECHO). Reconcile with Table.Query.\n filter: QuerySchema,\n options: S.optional(\n S.Struct({\n // Watch changes to object (not just creation).\n deep: S.optional(S.Boolean.annotations({ [AST.TitleAnnotationId]: 'Nested' })),\n // Debounce changes (delay in ms).\n delay: S.optional(S.Number.annotations({ [AST.TitleAnnotationId]: 'Delay' })),\n }).annotations({ [AST.TitleAnnotationId]: 'Options' }),\n ),\n}).pipe(S.mutable);\n\nexport type SubscriptionTrigger = S.Schema.Type<typeof SubscriptionTriggerSchema>;\n\n/**\n * Trigger schema (discriminated union).\n */\nexport const TriggerSchema = S.Union(\n TimerTriggerSchema,\n WebhookTriggerSchema,\n WebsocketTriggerSchema,\n SubscriptionTriggerSchema,\n).annotations({ [AST.TitleAnnotationId]: 'Trigger' });\n\nexport type TriggerType = S.Schema.Type<typeof TriggerSchema>;\n\n/**\n * Function trigger.\n */\nexport const FunctionTriggerSchema = S.Struct({\n // TODO(burdon): What type does this reference.\n function: S.optional(S.String.annotations({ [AST.TitleAnnotationId]: 'Function' })),\n enabled: S.optional(S.Boolean.annotations({ [AST.TitleAnnotationId]: 'Enabled' })),\n\n // TODO(burdon): Flatten?\n spec: S.optional(TriggerSchema),\n\n // TODO(burdon): Get meta from function.\n // The `meta` property is merged into the event data passed to the function.\n meta: S.optional(S.mutable(S.Record({ key: S.String, value: S.Any }))),\n});\n\nexport type FunctionTriggerType = S.Schema.Type<typeof FunctionTriggerSchema>;\n\n/**\n * Function trigger.\n */\nexport class FunctionTrigger extends TypedObject({\n typename: 'dxos.org/type/FunctionTrigger',\n version: '0.1.0',\n})(FunctionTriggerSchema.fields) {}\n\n/**\n * Function definition.\n * @deprecated (Use dxos.org/type/Function)\n */\n// TODO(burdon): Reconcile with FunctionType.\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 handler: S.String,\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\nexport const FUNCTION_TYPES = [Fun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AASd,EAAEY,MAAM;EAChCkC,OAAO9C,EAAEY;EACTmC,SAAS/C,EAAEY;AACb,CAAA,EAAA;AAAI;AAKG,IAAMoC,yBAAyBhD,EAAEC,OAAO;EAC7CgD,WAAWjD,EAAEc,SAASd,EAAES,QAAQT,EAAEkD,MAAMC,UAAUR,WAAAA,CAAAA,CAAAA,CAAAA;EAClDS,UAAUpD,EAAEc,SAASd,EAAES,QAAQT,EAAEkD,MAAMC,UAAUb,eAAAA,CAAAA,CAAAA,CAAAA;AACnD,CAAA;AAIO,IAAMe,iBAAiB;EAACV;EAAaL;;",
6
- "names": ["AST", "RawObject", "S", "TypedObject", "typeLiteralAnnotations", "AST", "TitleAnnotationId", "TimerTriggerSchema", "S", "Struct", "type", "Literal", "annotations", "cron", "NonEmptyString", "ExamplesAnnotationId", "pipe", "mutable", "WebhookTriggerSchema", "method", "String", "port", "optional", "Number", "WebsocketTriggerSchema", "url", "init", "Record", "key", "value", "Any", "QuerySchema", "props", "SubscriptionTriggerSchema", "filter", "options", "deep", "Boolean", "delay", "TriggerSchema", "Union", "FunctionTriggerSchema", "function", "enabled", "spec", "meta", "FunctionTrigger", "TypedObject", "typename", "version", "fields", "FunctionDef", "uri", "description", "route", "handler", "FunctionManifestSchema", "functions", "Array", "RawObject", "triggers", "FUNCTION_TYPES"]
7
- }
@@ -1,14 +0,0 @@
1
- import { type WebsocketTrigger } from '../../types';
2
- import { type TriggerFactory } from '../trigger-registry';
3
- interface WebsocketTriggerOptions {
4
- retryDelay: number;
5
- maxAttempts: number;
6
- }
7
- /**
8
- * Websocket.
9
- * NOTE: The port must be unique, so the same hook cannot be used for multiple spaces.
10
- */
11
- export declare const createWebsocketTrigger: TriggerFactory<WebsocketTrigger, WebsocketTriggerOptions>;
12
- export declare const createWebSocket: (url: string) => Promise<WebSocket>;
13
- export {};
14
- //# sourceMappingURL=websocket-trigger.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"websocket-trigger.d.ts","sourceRoot":"","sources":["../../../../../src/trigger/type/websocket-trigger.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,KAAK,gBAAgB,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAwB,KAAK,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAEhF,UAAU,uBAAuB;IAC/B,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;CACrB;AAED;;;GAGG;AACH,eAAO,MAAM,sBAAsB,EAAE,cAAc,CAAC,gBAAgB,EAAE,uBAAuB,CA6E5F,CAAC;AAWF,eAAO,MAAM,eAAe,QAAe,MAAM,KAAG,OAAO,CAAC,SAAS,CAEpE,CAAC"}
@@ -1,112 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import { sleep, Trigger } from '@dxos/async';
6
- import { type Space } from '@dxos/client/echo';
7
- import { type Context } from '@dxos/context';
8
- import { log } from '@dxos/log';
9
-
10
- import { type WebsocketTrigger } from '../../types';
11
- import { type TriggerCallback, type TriggerFactory } from '../trigger-registry';
12
-
13
- interface WebsocketTriggerOptions {
14
- retryDelay: number;
15
- maxAttempts: number;
16
- }
17
-
18
- /**
19
- * Websocket.
20
- * NOTE: The port must be unique, so the same hook cannot be used for multiple spaces.
21
- */
22
- export const createWebsocketTrigger: TriggerFactory<WebsocketTrigger, WebsocketTriggerOptions> = async (
23
- ctx: Context,
24
- space: Space,
25
- spec: WebsocketTrigger,
26
- callback: TriggerCallback,
27
- options: WebsocketTriggerOptions = { retryDelay: 2, maxAttempts: 5 },
28
- ) => {
29
- const { url, init } = spec;
30
-
31
- let wasOpen = false;
32
- let ws: WebSocket;
33
- for (let attempt = 1; attempt <= options.maxAttempts; attempt++) {
34
- const open = new Trigger<boolean>();
35
-
36
- ws = await createWebSocket(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 && wasOpen && !ctx.disposed) {
52
- setTimeout(async () => {
53
- log.info(`reconnecting in ${options.retryDelay}s...`, { url });
54
- await createWebsocketTrigger(ctx, space, spec, callback, options);
55
- }, options.retryDelay * 1_000);
56
- }
57
- open.wake(false);
58
- },
59
-
60
- onerror: (event) => {
61
- log.catch((event as any).error ?? new Error('Unspecified ws error.'), { url });
62
- open.wake(false);
63
- },
64
-
65
- onmessage: async (event) => {
66
- try {
67
- log.info('message');
68
- let data;
69
- if (event.data && 'text' in event.data) {
70
- data = JSON.parse(await (event.data as Blob).text());
71
- } else {
72
- data = JSON.parse(new TextDecoder().decode(event.data as Uint8Array));
73
- }
74
- await callback({ data });
75
- } catch (err) {
76
- log.catch(err, { url, data: event.data });
77
- }
78
- },
79
- } satisfies Partial<WebSocket>);
80
-
81
- const isOpen = await open.wait();
82
- if (ctx.disposed) {
83
- break;
84
- }
85
- if (isOpen) {
86
- wasOpen = true;
87
- break;
88
- }
89
- const wait = Math.pow(attempt, 2) * options.retryDelay;
90
- if (attempt < options.maxAttempts) {
91
- log.warn(`failed to connect; trying again in ${wait}s`, { attempt });
92
- await sleep(wait * 1_000);
93
- }
94
- }
95
-
96
- ctx.onDispose(() => {
97
- ws?.close();
98
- });
99
- };
100
-
101
- const createNodeWebSocket = async (url: string) => {
102
- // eslint-disable-next-line no-new-func
103
- const importESM = Function('path', 'return import(path)');
104
- const {
105
- default: { WebSocket },
106
- } = await importESM('ws');
107
- return new WebSocket(url);
108
- };
109
-
110
- export const createWebSocket = async (url: string): Promise<WebSocket> => {
111
- return typeof (globalThis as any).WebSocket === 'undefined' ? await createNodeWebSocket(url) : new WebSocket(url);
112
- };