@resonatehq/sdk 0.3.2 → 0.3.4

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 (79) hide show
  1. package/dist/async.d.ts +135 -0
  2. package/dist/async.d.ts.map +1 -0
  3. package/dist/async.js +205 -0
  4. package/dist/core/encoder.d.ts +5 -0
  5. package/dist/core/encoder.d.ts.map +1 -0
  6. package/dist/core/encoder.js +1 -0
  7. package/dist/core/encoders/base64.d.ts +6 -0
  8. package/dist/core/encoders/base64.d.ts.map +1 -0
  9. package/dist/core/encoders/base64.js +8 -0
  10. package/dist/core/encoders/json.d.ts +6 -0
  11. package/dist/core/encoders/json.d.ts.map +1 -0
  12. package/dist/core/encoders/json.js +60 -0
  13. package/dist/core/errors.d.ts +21 -0
  14. package/dist/core/errors.d.ts.map +1 -0
  15. package/dist/core/errors.js +33 -0
  16. package/dist/core/execution.d.ts +44 -0
  17. package/dist/core/execution.d.ts.map +1 -0
  18. package/dist/core/execution.js +195 -0
  19. package/dist/core/future.d.ts +54 -0
  20. package/dist/core/future.d.ts.map +1 -0
  21. package/dist/core/future.js +95 -0
  22. package/dist/core/invocation.d.ts +43 -0
  23. package/dist/core/invocation.d.ts.map +1 -0
  24. package/dist/core/invocation.js +72 -0
  25. package/dist/core/logger.d.ts +10 -0
  26. package/dist/core/logger.d.ts.map +1 -0
  27. package/dist/core/logger.js +1 -0
  28. package/dist/core/loggers/logger.d.ts +13 -0
  29. package/dist/core/loggers/logger.d.ts.map +1 -0
  30. package/dist/core/loggers/logger.js +43 -0
  31. package/dist/core/options.d.ts +79 -0
  32. package/dist/core/options.d.ts.map +1 -0
  33. package/dist/core/options.js +3 -0
  34. package/dist/core/promises/promises.d.ts +47 -0
  35. package/dist/core/promises/promises.d.ts.map +1 -0
  36. package/dist/core/promises/promises.js +128 -0
  37. package/dist/core/promises/types.d.ts +99 -0
  38. package/dist/core/promises/types.d.ts.map +1 -0
  39. package/dist/core/promises/types.js +29 -0
  40. package/dist/core/retries/retry.d.ts +20 -0
  41. package/dist/core/retries/retry.d.ts.map +1 -0
  42. package/dist/core/retries/retry.js +36 -0
  43. package/dist/core/retry.d.ts +27 -0
  44. package/dist/core/retry.d.ts.map +1 -0
  45. package/dist/core/retry.js +17 -0
  46. package/dist/core/schedules/types.d.ts +19 -0
  47. package/dist/core/schedules/types.d.ts.map +1 -0
  48. package/dist/core/schedules/types.js +3 -0
  49. package/dist/core/storage.d.ts +6 -0
  50. package/dist/core/storage.d.ts.map +1 -0
  51. package/dist/core/storage.js +1 -0
  52. package/dist/core/storages/memory.d.ts +8 -0
  53. package/dist/core/storages/memory.d.ts.map +1 -0
  54. package/dist/core/storages/memory.js +22 -0
  55. package/dist/core/storages/withTimeout.d.ts +10 -0
  56. package/dist/core/storages/withTimeout.d.ts.map +1 -0
  57. package/dist/core/storages/withTimeout.js +38 -0
  58. package/dist/core/store.d.ts +139 -0
  59. package/dist/core/store.d.ts.map +1 -0
  60. package/dist/core/store.js +1 -0
  61. package/dist/core/stores/local.d.ts +54 -0
  62. package/dist/core/stores/local.d.ts.map +1 -0
  63. package/dist/core/stores/local.js +368 -0
  64. package/dist/core/stores/remote.d.ts +48 -0
  65. package/dist/core/stores/remote.d.ts.map +1 -0
  66. package/dist/core/stores/remote.js +399 -0
  67. package/dist/core/utils.d.ts +3 -0
  68. package/dist/core/utils.d.ts.map +1 -0
  69. package/dist/core/utils.js +13 -0
  70. package/dist/generator.d.ts +187 -0
  71. package/dist/generator.d.ts.map +1 -0
  72. package/dist/generator.js +396 -0
  73. package/dist/index.d.ts +4 -0
  74. package/dist/index.d.ts.map +1 -0
  75. package/dist/index.js +3 -0
  76. package/dist/resonate.d.ts +73 -0
  77. package/dist/resonate.d.ts.map +1 -0
  78. package/dist/resonate.js +142 -0
  79. package/package.json +1 -1
@@ -0,0 +1,195 @@
1
+ import { ErrorCodes, ResonateError } from "./errors";
2
+ import { ResonatePromise } from "./future";
3
+ import { DurablePromise } from "./promises/promises";
4
+ /////////////////////////////////////////////////////////////////////
5
+ // Execution
6
+ /////////////////////////////////////////////////////////////////////
7
+ export class Execution {
8
+ invocation;
9
+ promise = null;
10
+ /**
11
+ * Represents an execution of a Resonate function invocation.
12
+ *
13
+ * @constructor
14
+ * @param invocation - An invocation correpsonding to the Resonate function.
15
+ */
16
+ constructor(invocation) {
17
+ this.invocation = invocation;
18
+ }
19
+ execute() {
20
+ if (this.promise) {
21
+ return this.promise;
22
+ }
23
+ const forkPromise = this.fork();
24
+ const joinPromise = forkPromise.then((f) => this.join(f));
25
+ this.promise = new ResonatePromise(this.invocation.id, forkPromise, joinPromise);
26
+ return this.promise;
27
+ }
28
+ get killed() {
29
+ return this.invocation.root.killed;
30
+ }
31
+ kill(error) {
32
+ // this will mark the entire invocation tree as killed
33
+ this.invocation.root.killed = true;
34
+ // reject only the root invocation
35
+ this.invocation.root.reject(new ResonateError("Resonate function killed", ErrorCodes.KILLED, error, true));
36
+ }
37
+ tags() {
38
+ if (this.invocation.parent === null) {
39
+ return { ...this.invocation.opts.tags, "resonate:invocation": "true" };
40
+ }
41
+ return this.invocation.opts.tags;
42
+ }
43
+ }
44
+ export class OrdinaryExecution extends Execution {
45
+ func;
46
+ retry;
47
+ constructor(invocation, func, retry = invocation.opts.retry) {
48
+ super(invocation);
49
+ this.func = func;
50
+ this.retry = retry;
51
+ }
52
+ async fork() {
53
+ try {
54
+ // create a durable promise
55
+ const promise = await DurablePromise.create(this.invocation.opts.store.promises, this.invocation.opts.encoder, this.invocation.id, this.invocation.timeout, this.invocation.param, {
56
+ idempotencyKey: this.invocation.idempotencyKey,
57
+ headers: this.invocation.headers,
58
+ tags: this.tags(),
59
+ });
60
+ if (promise.pending) {
61
+ // if pending, invoke the function and resolve/reject the durable promise
62
+ await this.run().then((v) => promise.resolve(v, { idempotencyKey: this.invocation.idempotencyKey }), (e) => promise.reject(e, { idempotencyKey: this.invocation.idempotencyKey }));
63
+ }
64
+ // resolve/reject the invocation
65
+ if (promise.resolved) {
66
+ this.invocation.resolve(promise.value());
67
+ }
68
+ else if (promise.rejected || promise.canceled || promise.timedout) {
69
+ this.invocation.reject(promise.error());
70
+ }
71
+ }
72
+ catch (e) {
73
+ // if an error occurs, kill the execution
74
+ this.kill(e);
75
+ }
76
+ return this.invocation.future;
77
+ }
78
+ async join(future) {
79
+ return await future.promise;
80
+ }
81
+ async run() {
82
+ let error;
83
+ // invoke the function according to the retry policy
84
+ for (const delay of this.retry.iterator(this.invocation)) {
85
+ try {
86
+ await new Promise((resolve) => setTimeout(resolve, delay));
87
+ return await this.func();
88
+ }
89
+ catch (e) {
90
+ error = e;
91
+ // bump the attempt count
92
+ this.invocation.attempt++;
93
+ }
94
+ }
95
+ // if all attempts fail throw the last error
96
+ throw error;
97
+ }
98
+ }
99
+ export class DeferredExecution extends Execution {
100
+ constructor(invocation) {
101
+ super(invocation);
102
+ }
103
+ async fork() {
104
+ try {
105
+ // create a durable promise
106
+ const promise = await DurablePromise.create(this.invocation.opts.store.promises, this.invocation.opts.encoder, this.invocation.id, this.invocation.timeout, this.invocation.param, {
107
+ idempotencyKey: this.invocation.idempotencyKey,
108
+ headers: this.invocation.headers,
109
+ tags: this.tags(),
110
+ poll: true,
111
+ });
112
+ // poll the completion of the durable promise
113
+ promise.completed.then((p) => p.resolved ? this.invocation.resolve(p.value()) : this.invocation.reject(p.error()));
114
+ }
115
+ catch (e) {
116
+ // if an error occurs, kill the execution
117
+ this.kill(e);
118
+ }
119
+ return this.invocation.future;
120
+ }
121
+ async join(future) {
122
+ return await future.promise;
123
+ }
124
+ }
125
+ export class GeneratorExecution extends Execution {
126
+ generator;
127
+ constructor(invocation, generator) {
128
+ super(invocation);
129
+ this.generator = generator;
130
+ }
131
+ async create() {
132
+ try {
133
+ // create a durable promise
134
+ const promise = await DurablePromise.create(this.invocation.opts.store.promises, this.invocation.opts.encoder, this.invocation.id, this.invocation.timeout, this.invocation.param, {
135
+ idempotencyKey: this.invocation.idempotencyKey,
136
+ headers: this.invocation.headers,
137
+ tags: this.tags(),
138
+ });
139
+ // resolve/reject the invocation if already completed
140
+ if (promise.resolved) {
141
+ this.invocation.resolve(promise.value());
142
+ }
143
+ else if (promise.rejected || promise.canceled || promise.timedout) {
144
+ this.invocation.reject(promise.error());
145
+ }
146
+ return promise;
147
+ }
148
+ catch (e) {
149
+ // if an error occurs, kill the execution
150
+ this.kill(e);
151
+ }
152
+ }
153
+ async resolve(promise, value) {
154
+ try {
155
+ // resolve the durable promise
156
+ await promise.resolve(value, { idempotencyKey: this.invocation.idempotencyKey });
157
+ // resolve/reject the invocation
158
+ if (promise.resolved) {
159
+ this.invocation.resolve(promise.value());
160
+ }
161
+ else if (promise.rejected || promise.canceled || promise.timedout) {
162
+ this.invocation.reject(promise.error());
163
+ }
164
+ }
165
+ catch (e) {
166
+ // if an error occurs, kill the execution
167
+ this.kill(e);
168
+ }
169
+ return promise;
170
+ }
171
+ async reject(promise, error) {
172
+ try {
173
+ // reject the durable promise
174
+ await promise.reject(error, { idempotencyKey: this.invocation.idempotencyKey });
175
+ // resolve/reject the invocation
176
+ if (promise.resolved) {
177
+ this.invocation.resolve(promise.value());
178
+ }
179
+ else if (promise.rejected || promise.canceled || promise.timedout) {
180
+ this.invocation.reject(promise.error());
181
+ }
182
+ }
183
+ catch (e) {
184
+ // if an error occurs, kill the execution
185
+ this.kill(e);
186
+ }
187
+ return promise;
188
+ }
189
+ async fork() {
190
+ return this.invocation.future;
191
+ }
192
+ async join(future) {
193
+ return await future.promise;
194
+ }
195
+ }
@@ -0,0 +1,54 @@
1
+ import { Invocation } from "./invocation";
2
+ export declare class ResonatePromise<T> extends Promise<T> {
3
+ id: string;
4
+ created: Promise<string>;
5
+ static [Symbol.species]: PromiseConstructor;
6
+ /**
7
+ * Represents the eventual return value of a Resonate function. This is achieved by wrapping a Javscript promise.
8
+ *
9
+ * @constructor
10
+ * @param id - A unique identifier for this promise.
11
+ * @param created - A promise that resolves when the durable promise has been created.
12
+ * @param completed - A promise that resolves when the durable promise has been fulfilled.
13
+ */
14
+ constructor(id: string, created: Promise<any>, completed: Promise<T>);
15
+ }
16
+ export type Value<T> = {
17
+ kind: "pending";
18
+ } | {
19
+ kind: "resolved";
20
+ value: T;
21
+ } | {
22
+ kind: "rejected";
23
+ error: unknown;
24
+ };
25
+ export declare class Future<T> {
26
+ private invocation;
27
+ promise: Promise<T>;
28
+ private _resolve;
29
+ private _reject;
30
+ readonly kind = "future";
31
+ private _value;
32
+ /**
33
+ * Represents the eventual return value of a Resonate function.
34
+ *
35
+ * @constructor
36
+ * @param invocation - An invocation correpsonding to the Resonate function.
37
+ * @param promise - A promise that resolves to the return value of the Resonate function.
38
+ */
39
+ constructor(invocation: Invocation<T>, promise: Promise<T>, _resolve: (v: T) => void, _reject: (v?: unknown) => void);
40
+ static deferred<T>(invocation: Invocation<T>): {
41
+ future: Future<T>;
42
+ resolve: (value: T) => void;
43
+ reject: (error: unknown) => void;
44
+ };
45
+ get id(): string;
46
+ get idempotencyKey(): string | undefined;
47
+ get root(): Future<any>;
48
+ get parent(): Future<any> | null;
49
+ get children(): Future<any>[];
50
+ get value(): Value<T>;
51
+ private resolve;
52
+ private reject;
53
+ }
54
+ //# sourceMappingURL=future.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"future.d.ts","sourceRoot":"","sources":["../../lib/core/future.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAM1C,qBAAa,eAAe,CAAC,CAAC,CAAE,SAAQ,OAAO,CAAC,CAAC,CAAC;IAkBvC,EAAE,EAAE,MAAM;IAfnB,OAAO,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;IAIzB,MAAM,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,qBAAW;IAElC;;;;;;;OAOG;gBAEM,EAAE,EAAE,MAAM,EACjB,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,EACrB,SAAS,EAAE,OAAO,CAAC,CAAC,CAAC;CAUxB;AAMD,MAAM,MAAM,KAAK,CAAC,CAAC,IAAI;IAAE,IAAI,EAAE,SAAS,CAAA;CAAE,GAAG;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,KAAK,EAAE,CAAC,CAAA;CAAE,GAAG;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE,CAAC;AAEnH,qBAAa,MAAM,CAAC,CAAC;IAejB,OAAO,CAAC,UAAU;IACX,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC;IAC1B,OAAO,CAAC,QAAQ;IAChB,OAAO,CAAC,OAAO;IAhBjB,QAAQ,CAAC,IAAI,YAAY;IAGzB,OAAO,CAAC,MAAM,CAAiC;IAE/C;;;;;;OAMG;gBAEO,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,EAC1B,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,EAClB,QAAQ,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,IAAI,EACxB,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,OAAO,KAAK,IAAI;IAGxC,MAAM,CAAC,QAAQ,CAAC,CAAC,EAAE,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC;;;;;IAqB5C,IAAI,EAAE,WAEL;IAED,IAAI,cAAc,uBAEjB;IAED,IAAI,IAAI,gBAEP;IAED,IAAI,MAAM,uBAET;IAED,IAAI,QAAQ,kBAEX;IAED,IAAI,KAAK,aAER;IAED,OAAO,CAAC,OAAO;IAKf,OAAO,CAAC,MAAM;CAIf"}
@@ -0,0 +1,95 @@
1
+ /////////////////////////////////////////////////////////////////////
2
+ // Promise
3
+ /////////////////////////////////////////////////////////////////////
4
+ export class ResonatePromise extends Promise {
5
+ id;
6
+ // resolves to the id of the durable promise
7
+ // can be used to await durable promise creation
8
+ created;
9
+ // You are not expected to understand this (we don't either)
10
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/@@species
11
+ static [Symbol.species] = Promise;
12
+ /**
13
+ * Represents the eventual return value of a Resonate function. This is achieved by wrapping a Javscript promise.
14
+ *
15
+ * @constructor
16
+ * @param id - A unique identifier for this promise.
17
+ * @param created - A promise that resolves when the durable promise has been created.
18
+ * @param completed - A promise that resolves when the durable promise has been fulfilled.
19
+ */
20
+ constructor(id, created, completed) {
21
+ // bind the promise to the completed promise
22
+ super((resolve, reject) => {
23
+ completed.then(resolve, reject);
24
+ });
25
+ this.id = id;
26
+ // expose the id when the durable promise has been created
27
+ this.created = created.then(() => this.id);
28
+ }
29
+ }
30
+ export class Future {
31
+ invocation;
32
+ promise;
33
+ _resolve;
34
+ _reject;
35
+ // a discriminate property
36
+ kind = "future";
37
+ // initial value
38
+ _value = { kind: "pending" };
39
+ /**
40
+ * Represents the eventual return value of a Resonate function.
41
+ *
42
+ * @constructor
43
+ * @param invocation - An invocation correpsonding to the Resonate function.
44
+ * @param promise - A promise that resolves to the return value of the Resonate function.
45
+ */
46
+ constructor(invocation, promise, _resolve, _reject) {
47
+ this.invocation = invocation;
48
+ this.promise = promise;
49
+ this._resolve = _resolve;
50
+ this._reject = _reject;
51
+ }
52
+ static deferred(invocation) {
53
+ let resolve;
54
+ let reject;
55
+ // construct a javascript promise
56
+ const promise = new Promise((_resolve, _reject) => {
57
+ resolve = _resolve;
58
+ reject = _reject;
59
+ });
60
+ // construct a new future
61
+ const future = new Future(invocation, promise, resolve, reject);
62
+ // return future and resolvers
63
+ return {
64
+ future: future,
65
+ resolve: future.resolve.bind(future),
66
+ reject: future.reject.bind(future),
67
+ };
68
+ }
69
+ get id() {
70
+ return this.invocation.id;
71
+ }
72
+ get idempotencyKey() {
73
+ return this.invocation.idempotencyKey;
74
+ }
75
+ get root() {
76
+ return this.invocation.root.future;
77
+ }
78
+ get parent() {
79
+ return this.invocation.parent?.future ?? null;
80
+ }
81
+ get children() {
82
+ return this.invocation.children.map((i) => i.future);
83
+ }
84
+ get value() {
85
+ return this._value;
86
+ }
87
+ resolve(value) {
88
+ this._resolve(value);
89
+ this._value = { kind: "resolved", value };
90
+ }
91
+ reject(error) {
92
+ this._reject(error);
93
+ this._value = { kind: "rejected", error };
94
+ }
95
+ }
@@ -0,0 +1,43 @@
1
+ import { Future } from "./future";
2
+ import { Options, PartialOptions } from "./options";
3
+ export declare class Invocation<T> {
4
+ readonly name: string;
5
+ readonly version: number;
6
+ readonly id: string;
7
+ readonly idempotencyKey: string | undefined;
8
+ readonly headers: Record<string, string> | undefined;
9
+ readonly param: unknown;
10
+ readonly opts: Options;
11
+ future: Future<T>;
12
+ resolve: (v: T) => void;
13
+ reject: (e: unknown) => void;
14
+ killed: boolean;
15
+ createdAt: number;
16
+ counter: number;
17
+ attempt: number;
18
+ awaited: Future<any>[];
19
+ blocked: Future<any> | null;
20
+ readonly root: Invocation<any>;
21
+ readonly parent: Invocation<any> | null;
22
+ readonly children: Invocation<any>[];
23
+ /**
24
+ * Represents a Resonate function invocation.
25
+ *
26
+ * @constructor
27
+ * @param id - A unique id for this invocation.
28
+ * @param idempotencyKey - An idempotency key used to deduplicate invocations.
29
+ * @param opts - The invocation options.
30
+ * @param parent - The parent invocation.
31
+ */
32
+ constructor(name: string, version: number, id: string, idempotencyKey: string | undefined, headers: Record<string, string> | undefined, param: unknown, opts: Options, parent?: Invocation<any>);
33
+ get timeout(): number;
34
+ addChild(child: Invocation<any>): void;
35
+ await(future: Future<any>): void;
36
+ block(future: Future<any>): void;
37
+ unblock(): void;
38
+ split(args: [...any, PartialOptions?]): {
39
+ args: any[];
40
+ opts: Options;
41
+ };
42
+ }
43
+ //# sourceMappingURL=invocation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"invocation.d.ts","sourceRoot":"","sources":["../../lib/core/invocation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,OAAO,EAAE,cAAc,EAAoB,MAAM,WAAW,CAAC;AAMtE,qBAAa,UAAU,CAAC,CAAC;aA4BL,IAAI,EAAE,MAAM;aACZ,OAAO,EAAE,MAAM;aACf,EAAE,EAAE,MAAM;aACV,cAAc,EAAE,MAAM,GAAG,SAAS;aAClC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS;aAC3C,KAAK,EAAE,OAAO;aACd,IAAI,EAAE,OAAO;IAjC/B,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC;IAClB,OAAO,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,IAAI,CAAC;IACxB,MAAM,EAAE,CAAC,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;IAE7B,MAAM,EAAE,OAAO,CAAS;IAExB,SAAS,EAAE,MAAM,CAAc;IAC/B,OAAO,EAAE,MAAM,CAAK;IACpB,OAAO,EAAE,MAAM,CAAK;IAEpB,OAAO,EAAE,MAAM,CAAC,GAAG,CAAC,EAAE,CAAM;IAC5B,OAAO,EAAE,MAAM,CAAC,GAAG,CAAC,GAAG,IAAI,CAAQ;IAEnC,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAC,GAAG,CAAC,CAAC;IAC/B,QAAQ,CAAC,MAAM,EAAE,UAAU,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC;IACxC,QAAQ,CAAC,QAAQ,EAAE,UAAU,CAAC,GAAG,CAAC,EAAE,CAAM;IAE1C;;;;;;;;OAQG;gBAEe,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,EACf,EAAE,EAAE,MAAM,EACV,cAAc,EAAE,MAAM,GAAG,SAAS,EAClC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,EAC3C,KAAK,EAAE,OAAO,EACd,IAAI,EAAE,OAAO,EAC7B,MAAM,CAAC,EAAE,UAAU,CAAC,GAAG,CAAC;IAW1B,IAAI,OAAO,IAAI,MAAM,CAEpB;IAED,QAAQ,CAAC,KAAK,EAAE,UAAU,CAAC,GAAG,CAAC;IAI/B,KAAK,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,CAAC;IAIzB,KAAK,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,CAAC;IAIzB,OAAO;IAIP,KAAK,CAAC,IAAI,EAAE,CAAC,GAAG,GAAG,EAAE,cAAc,CAAC,CAAC,GAAG;QAAE,IAAI,EAAE,GAAG,EAAE,CAAC;QAAC,IAAI,EAAE,OAAO,CAAA;KAAE;CAQvE"}
@@ -0,0 +1,72 @@
1
+ import { Future } from "./future";
2
+ import { isPartialOptions } from "./options";
3
+ /////////////////////////////////////////////////////////////////////
4
+ // Invocation
5
+ /////////////////////////////////////////////////////////////////////
6
+ export class Invocation {
7
+ name;
8
+ version;
9
+ id;
10
+ idempotencyKey;
11
+ headers;
12
+ param;
13
+ opts;
14
+ future;
15
+ resolve;
16
+ reject;
17
+ killed = false;
18
+ createdAt = Date.now();
19
+ counter = 0;
20
+ attempt = 0;
21
+ awaited = [];
22
+ blocked = null;
23
+ root;
24
+ parent;
25
+ children = [];
26
+ /**
27
+ * Represents a Resonate function invocation.
28
+ *
29
+ * @constructor
30
+ * @param id - A unique id for this invocation.
31
+ * @param idempotencyKey - An idempotency key used to deduplicate invocations.
32
+ * @param opts - The invocation options.
33
+ * @param parent - The parent invocation.
34
+ */
35
+ constructor(name, version, id, idempotencyKey, headers, param, opts, parent) {
36
+ this.name = name;
37
+ this.version = version;
38
+ this.id = id;
39
+ this.idempotencyKey = idempotencyKey;
40
+ this.headers = headers;
41
+ this.param = param;
42
+ this.opts = opts;
43
+ const { future, resolve, reject } = Future.deferred(this);
44
+ this.future = future;
45
+ this.resolve = resolve;
46
+ this.reject = reject;
47
+ this.root = parent?.root ?? this;
48
+ this.parent = parent ?? null;
49
+ }
50
+ get timeout() {
51
+ return Math.min(this.createdAt + this.opts.timeout, this.parent?.timeout ?? Infinity);
52
+ }
53
+ addChild(child) {
54
+ this.children.push(child);
55
+ }
56
+ await(future) {
57
+ this.awaited.push(future);
58
+ }
59
+ block(future) {
60
+ this.blocked = future;
61
+ }
62
+ unblock() {
63
+ this.blocked = null;
64
+ }
65
+ split(args) {
66
+ const opts = args[args.length - 1];
67
+ const parentOpts = this.parent?.opts ?? this.root.opts;
68
+ return isPartialOptions(opts)
69
+ ? { args: args.slice(0, -1), opts: { ...parentOpts, ...opts, tags: { ...parentOpts.tags, ...opts.tags } } }
70
+ : { args, opts: parentOpts };
71
+ }
72
+ }
@@ -0,0 +1,10 @@
1
+ export type LogLevel = "debug" | "info" | "warn" | "error";
2
+ export interface ILogger {
3
+ level: LogLevel;
4
+ debug(...args: any[]): void;
5
+ info(...args: any[]): void;
6
+ warn(...args: any[]): void;
7
+ error(...args: any[]): void;
8
+ table(...args: any[]): void;
9
+ }
10
+ //# sourceMappingURL=logger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../lib/core/logger.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,QAAQ,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;AAE3D,MAAM,WAAW,OAAO;IACtB,KAAK,EAAE,QAAQ,CAAC;IAChB,KAAK,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;IAC5B,IAAI,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;IAC3B,IAAI,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;IAC3B,KAAK,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;IAC5B,KAAK,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;CAC7B"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,13 @@
1
+ import { ILogger, LogLevel } from "../logger";
2
+ export declare class Logger implements ILogger {
3
+ level: LogLevel;
4
+ private _level;
5
+ constructor(level?: LogLevel);
6
+ debug(...args: any[]): void;
7
+ info(...args: any[]): void;
8
+ warn(...args: any[]): void;
9
+ error(...args: any[]): void;
10
+ table(...args: any[]): void;
11
+ private log;
12
+ }
13
+ //# sourceMappingURL=logger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../../lib/core/loggers/logger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAE9C,qBAAa,MAAO,YAAW,OAAO;IAC7B,KAAK,EAAE,QAAQ,CAAC;IACvB,OAAO,CAAC,MAAM,CAAS;gBAEX,KAAK,CAAC,EAAE,QAAQ;IAmB5B,KAAK,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,IAAI;IAI3B,IAAI,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,IAAI;IAI1B,IAAI,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,IAAI;IAI1B,KAAK,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,IAAI;IAI3B,KAAK,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,IAAI;IAM3B,OAAO,CAAC,GAAG;CAKZ"}
@@ -0,0 +1,43 @@
1
+ export class Logger {
2
+ level;
3
+ _level;
4
+ constructor(level) {
5
+ this.level = level ?? process.env.LOG_LEVEL ?? "info";
6
+ switch (this.level) {
7
+ case "debug":
8
+ this._level = 0;
9
+ break;
10
+ case "info":
11
+ this._level = 1;
12
+ break;
13
+ case "warn":
14
+ this._level = 2;
15
+ break;
16
+ case "error":
17
+ this._level = 3;
18
+ break;
19
+ }
20
+ }
21
+ debug(...args) {
22
+ this.log(0, args);
23
+ }
24
+ info(...args) {
25
+ this.log(1, args);
26
+ }
27
+ warn(...args) {
28
+ this.log(2, args);
29
+ }
30
+ error(...args) {
31
+ this.log(3, args);
32
+ }
33
+ table(...args) {
34
+ if (this._level <= 0) {
35
+ console.table(...args);
36
+ }
37
+ }
38
+ log(level, args) {
39
+ if (this._level <= level) {
40
+ console.log(...args);
41
+ }
42
+ }
43
+ }
@@ -0,0 +1,79 @@
1
+ import { IEncoder } from "./encoder";
2
+ import { ILogger } from "./logger";
3
+ import { IRetry } from "./retry";
4
+ import { IStore } from "./store";
5
+ /**
6
+ * Resonate configuration options.
7
+ */
8
+ export interface ResonateOptions {
9
+ /**
10
+ * An encoder instance used for encoding and decoding values
11
+ * returned (or thrown) by registered functions. If not provided,
12
+ * a default JSON encoder will be used.
13
+ */
14
+ encoder: IEncoder<unknown, string | undefined>;
15
+ /**
16
+ * A process id that can be used to uniquely identify this Resonate
17
+ * instance. If not provided a default value will be generated.
18
+ */
19
+ pid: string;
20
+ /**
21
+ * A logger instance, if not provided a default logger will be
22
+ * used.
23
+ */
24
+ logger: ILogger;
25
+ /**
26
+ * A retry instance, defaults to exponential backoff.
27
+ */
28
+ retry: IRetry;
29
+ /**
30
+ * Tags to add to all durable promises.
31
+ */
32
+ tags: Record<string, string>;
33
+ /**
34
+ * A store instance, if provided this will take precedence over a
35
+ * remote store.
36
+ */
37
+ store: IStore;
38
+ /**
39
+ * The default promise timeout in ms, used for every function
40
+ * executed by calling run. Defaults to 1000.
41
+ */
42
+ timeout: number;
43
+ /**
44
+ * The remote promise store url. If not provided, an in-memory
45
+ * promise store will be used.
46
+ */
47
+ url: string;
48
+ }
49
+ /**
50
+ * Resonate function invocation options.
51
+ */
52
+ export interface Options {
53
+ __resonate: true;
54
+ /**
55
+ * Overrides the default encoder.
56
+ */
57
+ encoder: IEncoder<unknown, string | undefined>;
58
+ /**
59
+ * Overrides the default retry policy.
60
+ */
61
+ retry: IRetry;
62
+ /**
63
+ * Overrides the default store.
64
+ */
65
+ store: IStore;
66
+ /**
67
+ * Additional tags to add to the durable promise.
68
+ */
69
+ tags: Record<string, string>;
70
+ /**
71
+ * Overrides the default timeout.
72
+ */
73
+ timeout: number;
74
+ }
75
+ export type PartialOptions = Partial<Options> & {
76
+ __resonate: true;
77
+ };
78
+ export declare function isPartialOptions(o: unknown): o is PartialOptions;
79
+ //# sourceMappingURL=options.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"options.d.ts","sourceRoot":"","sources":["../../lib/core/options.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AACrC,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AACnC,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAEjC;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B;;;;OAIG;IACH,OAAO,EAAE,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC;IAE/C;;;OAGG;IACH,GAAG,EAAE,MAAM,CAAC;IAEZ;;;OAGG;IACH,MAAM,EAAE,OAAO,CAAC;IAEhB;;OAEG;IACH,KAAK,EAAE,MAAM,CAAC;IAEd;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAE7B;;;OAGG;IACH,KAAK,EAAE,MAAM,CAAC;IAEd;;;OAGG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB;;;OAGG;IACH,GAAG,EAAE,MAAM,CAAC;CACb;AAED;;GAEG;AACH,MAAM,WAAW,OAAO;IACtB,UAAU,EAAE,IAAI,CAAC;IAEjB;;OAEG;IACH,OAAO,EAAE,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC;IAE/C;;OAEG;IACH,KAAK,EAAE,MAAM,CAAC;IAEd;;OAEG;IACH,KAAK,EAAE,MAAM,CAAC;IAEd;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAE7B;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,MAAM,cAAc,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG;IAAE,UAAU,EAAE,IAAI,CAAA;CAAE,CAAC;AAErE,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,cAAc,CAEhE"}
@@ -0,0 +1,3 @@
1
+ export function isPartialOptions(o) {
2
+ return typeof o === "object" && o !== null && o.__resonate === true;
3
+ }