@skipruntime/helpers 0.0.8 → 0.0.10
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/README.md +18 -0
- package/dist/external.d.ts +27 -10
- package/dist/external.d.ts.map +1 -1
- package/dist/external.js +37 -33
- package/dist/external.js.map +1 -1
- package/dist/remote.d.ts +3 -4
- package/dist/remote.d.ts.map +1 -1
- package/dist/remote.js +9 -20
- package/dist/remote.js.map +1 -1
- package/dist/rest.d.ts +3 -14
- package/dist/rest.d.ts.map +1 -1
- package/dist/rest.js +7 -20
- package/dist/rest.js.map +1 -1
- package/package.json +2 -3
- package/src/external.ts +49 -35
- package/src/remote.ts +11 -19
- package/src/rest.ts +8 -26
package/README.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# @skipruntime/helpers
|
|
2
|
+
|
|
3
|
+
This package contains items that may be useful for working with the Skip framework, but are not strictly necessary.
|
|
4
|
+
|
|
5
|
+
See the [docs](https://skiplabs.io/docs/api/helpers) for more details.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Install directly using npm (`npm i @skipruntime/helpers`), or along with other
|
|
10
|
+
components of the runtime by installing the
|
|
11
|
+
[`@skiplabs/skip`](https://www.npmjs.com/package/@skiplabs/skip) meta-package
|
|
12
|
+
(`npm i @skiplabs/skip`)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
## Support
|
|
16
|
+
|
|
17
|
+
Join the [Discord](https://discord.gg/ss4zxfgUBH) to talk to other community
|
|
18
|
+
members and developers or ask any questions.
|
package/dist/external.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import type { Entry, ExternalService, Json } from "@skipruntime/
|
|
1
|
+
import type { Entry, ExternalService, Json } from "@skipruntime/core";
|
|
2
2
|
/**
|
|
3
3
|
* Interface required by `GenericExternalService` for external resources.
|
|
4
4
|
*/
|
|
5
5
|
export interface ExternalResource {
|
|
6
|
-
open(params: Json, callbacks: {
|
|
6
|
+
open(instance: string, params: Json, callbacks: {
|
|
7
7
|
update: (updates: Entry<Json, Json>[], isInit: boolean) => void;
|
|
8
8
|
error: (error: Json) => void;
|
|
9
9
|
loading: () => void;
|
|
10
10
|
}): void;
|
|
11
|
-
close(
|
|
11
|
+
close(instance: string): void;
|
|
12
12
|
}
|
|
13
13
|
/**
|
|
14
14
|
* A generic external service providing external resources.
|
|
@@ -17,28 +17,29 @@ export interface ExternalResource {
|
|
|
17
17
|
*/
|
|
18
18
|
export declare class GenericExternalService implements ExternalService {
|
|
19
19
|
private readonly resources;
|
|
20
|
+
private readonly instances;
|
|
20
21
|
/**
|
|
21
22
|
* @param resources - Association of resource names to `ExternalResource`s.
|
|
22
23
|
*/
|
|
23
24
|
constructor(resources: {
|
|
24
25
|
[name: string]: ExternalResource;
|
|
25
26
|
});
|
|
26
|
-
subscribe(resourceName: string, params: Json, callbacks: {
|
|
27
|
+
subscribe(instance: string, resourceName: string, params: Json, callbacks: {
|
|
27
28
|
update: (updates: Entry<Json, Json>[], isInit: boolean) => void;
|
|
28
29
|
error: (error: Json) => void;
|
|
29
30
|
loading: () => void;
|
|
30
31
|
}): void;
|
|
31
|
-
unsubscribe(
|
|
32
|
+
unsubscribe(instance: string): void;
|
|
32
33
|
shutdown(): void;
|
|
33
34
|
}
|
|
34
35
|
export declare class TimerResource implements ExternalResource {
|
|
35
36
|
private readonly intervals;
|
|
36
|
-
open(params: Json, callbacks: {
|
|
37
|
+
open(instance: string, params: Json, callbacks: {
|
|
37
38
|
update: (updates: Entry<Json, Json>[], isInit: boolean) => void;
|
|
38
39
|
error: (error: Json) => void;
|
|
39
40
|
loading: () => void;
|
|
40
41
|
}): void;
|
|
41
|
-
close(
|
|
42
|
+
close(instance: string): void;
|
|
42
43
|
}
|
|
43
44
|
/**
|
|
44
45
|
* An external resource that is refreshed at some polling interval.
|
|
@@ -52,19 +53,35 @@ export declare class Polled<S extends Json, K extends Json, V extends Json> impl
|
|
|
52
53
|
private readonly duration;
|
|
53
54
|
private readonly conv;
|
|
54
55
|
private readonly encodeParams;
|
|
56
|
+
private readonly options?;
|
|
55
57
|
private readonly intervals;
|
|
56
58
|
/**
|
|
59
|
+
* Construct a `Polled` external resource.
|
|
60
|
+
*
|
|
61
|
+
* The URL of the external resource is formed by appending the given base `url` and the result of `encodeParams(params)` where `params` are the parameters provided when instantiating the resource.
|
|
62
|
+
* The default `encodeParams` function produces a query string consisting of a leading `?` followed by the encoding of the `params` given by [URLSearchParams.toString](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/toString) when `params` is an object and otherwise a single key-value pair `params=JSON.stringify(params)`.
|
|
63
|
+
*
|
|
64
|
+
* Note that the result of `encodeParams` contains the `?` separator, but it need not be at the beginning of the returned string, so some parameters can be used in part of the URL preceding the `?`.
|
|
65
|
+
*
|
|
57
66
|
* @param url - HTTP endpoint of external resource to poll.
|
|
58
67
|
* @param duration - Refresh interval, in milliseconds.
|
|
59
68
|
* @param conv - Function to convert data of type `S` received from external resource to `key`-`value` entries.
|
|
60
69
|
* @param encodeParams - Function to use to encode params of type `Json` for external resource request.
|
|
70
|
+
* @param options - Optional parameters.
|
|
71
|
+
* @param options.headers - Additional headers to add to request.
|
|
72
|
+
* @param options.timeout - Timeout for request, in milliseconds. Defaults to 1000ms.
|
|
61
73
|
*/
|
|
62
|
-
constructor(url: string, duration: number, conv: (data: S) => Entry<K, V>[], encodeParams?: (params: Json) => string
|
|
63
|
-
|
|
74
|
+
constructor(url: string, duration: number, conv: (data: S) => Entry<K, V>[], encodeParams?: (params: Json) => string, options?: {
|
|
75
|
+
headers?: {
|
|
76
|
+
[header: string]: string;
|
|
77
|
+
};
|
|
78
|
+
timeout?: number;
|
|
79
|
+
} | undefined);
|
|
80
|
+
open(instance: string, params: Json, callbacks: {
|
|
64
81
|
update: (updates: Entry<Json, Json>[], isInit: boolean) => void;
|
|
65
82
|
error: (error: Json) => void;
|
|
66
83
|
loading: () => void;
|
|
67
84
|
}): void;
|
|
68
|
-
close(
|
|
85
|
+
close(instance: string): void;
|
|
69
86
|
}
|
|
70
87
|
//# sourceMappingURL=external.d.ts.map
|
package/dist/external.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"external.d.ts","sourceRoot":"","sources":["../src/external.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,eAAe,EAAE,IAAI,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"external.d.ts","sourceRoot":"","sources":["../src/external.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,eAAe,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAItE;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,IAAI,CACF,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,IAAI,EACZ,SAAS,EAAE;QACT,MAAM,EAAE,CAAC,OAAO,EAAE,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,KAAK,IAAI,CAAC;QAChE,KAAK,EAAE,CAAC,KAAK,EAAE,IAAI,KAAK,IAAI,CAAC;QAC7B,OAAO,EAAE,MAAM,IAAI,CAAC;KACrB,GACA,IAAI,CAAC;IAER,KAAK,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;CAC/B;AAED;;;;GAIG;AACH,qBAAa,sBAAuB,YAAW,eAAe;IAO1D,OAAO,CAAC,QAAQ,CAAC,SAAS;IAN5B,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAuC;IAEjE;;OAEG;gBAEgB,SAAS,EAAE;QAAE,CAAC,IAAI,EAAE,MAAM,GAAG,gBAAgB,CAAA;KAAE;IAGlE,SAAS,CACP,QAAQ,EAAE,MAAM,EAChB,YAAY,EAAE,MAAM,EACpB,MAAM,EAAE,IAAI,EACZ,SAAS,EAAE;QACT,MAAM,EAAE,CAAC,OAAO,EAAE,KAAK,CAAC,IAAI,E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,GAAE,CAC7B,MAAM,EAAE,IAAI,KACT,MAA4B,EAChB,OAAO,CAAC,EAAE;QACzB,OAAO,CAAC,EAAE;YAAE,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAA;SAAE,CAAC;QACvC,OAAO,CAAC,EAAE,MAAM,CAAC;KAClB,YAAA;IAGH,IAAI,CACF,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,IAAI,EACZ,SAAS,EAAE;QACT,MAAM,EAAE,CAAC,OAAO,EAAE,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,KAAK,IAAI,CAAC;QAChE,KAAK,EAAE,CAAC,KAAK,EAAE,IAAI,KAAK,IAAI,CAAC;QAC7B,OAAO,EAAE,MAAM,IAAI,CAAC;KACrB;IAkBH,KAAK,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;CAO9B"}
|
package/dist/external.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { SkipUnknownResourceError } from "@skipruntime/core";
|
|
1
2
|
import { fetchJSON } from "./rest.js";
|
|
2
3
|
/**
|
|
3
4
|
* A generic external service providing external resources.
|
|
@@ -10,20 +11,22 @@ export class GenericExternalService {
|
|
|
10
11
|
*/
|
|
11
12
|
constructor(resources) {
|
|
12
13
|
this.resources = resources;
|
|
14
|
+
this.instances = new Map();
|
|
13
15
|
}
|
|
14
|
-
subscribe(resourceName, params, callbacks) {
|
|
16
|
+
subscribe(instance, resourceName, params, callbacks) {
|
|
15
17
|
const resource = this.resources[resourceName];
|
|
16
18
|
if (!resource) {
|
|
17
|
-
throw new
|
|
19
|
+
throw new SkipUnknownResourceError(`Unknown resource named '${resourceName}'`);
|
|
18
20
|
}
|
|
19
|
-
|
|
21
|
+
this.instances.set(instance, resource);
|
|
22
|
+
resource.open(instance, params, callbacks);
|
|
20
23
|
}
|
|
21
|
-
unsubscribe(
|
|
22
|
-
const resource = this.
|
|
23
|
-
if (
|
|
24
|
-
|
|
24
|
+
unsubscribe(instance) {
|
|
25
|
+
const resource = this.instances.get(instance);
|
|
26
|
+
if (resource) {
|
|
27
|
+
resource.close(instance);
|
|
28
|
+
this.instances.delete(instance);
|
|
25
29
|
}
|
|
26
|
-
resource.close(params);
|
|
27
30
|
}
|
|
28
31
|
shutdown() {
|
|
29
32
|
return;
|
|
@@ -33,14 +36,13 @@ export class TimerResource {
|
|
|
33
36
|
constructor() {
|
|
34
37
|
this.intervals = new Map();
|
|
35
38
|
}
|
|
36
|
-
open(params, callbacks) {
|
|
39
|
+
open(instance, params, callbacks) {
|
|
37
40
|
const time = new Date().getTime();
|
|
38
41
|
const values = [];
|
|
39
42
|
for (const name of Object.keys(params)) {
|
|
40
43
|
values.push([name, [time]]);
|
|
41
44
|
}
|
|
42
45
|
callbacks.update(values, true);
|
|
43
|
-
const id = toId(params);
|
|
44
46
|
const intervals = {};
|
|
45
47
|
for (const [name, duration] of Object.entries(params)) {
|
|
46
48
|
const ms = Number(duration);
|
|
@@ -51,14 +53,15 @@ export class TimerResource {
|
|
|
51
53
|
}, ms);
|
|
52
54
|
}
|
|
53
55
|
}
|
|
54
|
-
this.intervals.set(
|
|
56
|
+
this.intervals.set(instance, intervals);
|
|
55
57
|
}
|
|
56
|
-
close(
|
|
57
|
-
const intervals = this.intervals.get(
|
|
58
|
+
close(instance) {
|
|
59
|
+
const intervals = this.intervals.get(instance);
|
|
58
60
|
if (intervals != null) {
|
|
59
61
|
for (const interval of Object.values(intervals)) {
|
|
60
62
|
clearInterval(interval);
|
|
61
63
|
}
|
|
64
|
+
this.intervals.delete(instance);
|
|
62
65
|
}
|
|
63
66
|
}
|
|
64
67
|
}
|
|
@@ -71,10 +74,10 @@ function defaultParamEncoder(params) {
|
|
|
71
74
|
else
|
|
72
75
|
queryParams[key] = value.toString();
|
|
73
76
|
}
|
|
74
|
-
return new URLSearchParams(queryParams).toString()
|
|
77
|
+
return `?${new URLSearchParams(queryParams).toString()}`;
|
|
75
78
|
}
|
|
76
79
|
else
|
|
77
|
-
return
|
|
80
|
+
return `?params=${JSON.stringify(params)}`;
|
|
78
81
|
}
|
|
79
82
|
/**
|
|
80
83
|
* An external resource that is refreshed at some polling interval.
|
|
@@ -85,24 +88,34 @@ function defaultParamEncoder(params) {
|
|
|
85
88
|
*/
|
|
86
89
|
export class Polled {
|
|
87
90
|
/**
|
|
91
|
+
* Construct a `Polled` external resource.
|
|
92
|
+
*
|
|
93
|
+
* The URL of the external resource is formed by appending the given base `url` and the result of `encodeParams(params)` where `params` are the parameters provided when instantiating the resource.
|
|
94
|
+
* The default `encodeParams` function produces a query string consisting of a leading `?` followed by the encoding of the `params` given by [URLSearchParams.toString](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/toString) when `params` is an object and otherwise a single key-value pair `params=JSON.stringify(params)`.
|
|
95
|
+
*
|
|
96
|
+
* Note that the result of `encodeParams` contains the `?` separator, but it need not be at the beginning of the returned string, so some parameters can be used in part of the URL preceding the `?`.
|
|
97
|
+
*
|
|
88
98
|
* @param url - HTTP endpoint of external resource to poll.
|
|
89
99
|
* @param duration - Refresh interval, in milliseconds.
|
|
90
100
|
* @param conv - Function to convert data of type `S` received from external resource to `key`-`value` entries.
|
|
91
101
|
* @param encodeParams - Function to use to encode params of type `Json` for external resource request.
|
|
102
|
+
* @param options - Optional parameters.
|
|
103
|
+
* @param options.headers - Additional headers to add to request.
|
|
104
|
+
* @param options.timeout - Timeout for request, in milliseconds. Defaults to 1000ms.
|
|
92
105
|
*/
|
|
93
|
-
constructor(url, duration, conv, encodeParams = defaultParamEncoder) {
|
|
106
|
+
constructor(url, duration, conv, encodeParams = defaultParamEncoder, options) {
|
|
94
107
|
this.url = url;
|
|
95
108
|
this.duration = duration;
|
|
96
109
|
this.conv = conv;
|
|
97
110
|
this.encodeParams = encodeParams;
|
|
111
|
+
this.options = options;
|
|
98
112
|
this.intervals = new Map();
|
|
99
113
|
}
|
|
100
|
-
open(params, callbacks) {
|
|
101
|
-
this.
|
|
102
|
-
const url = `${this.url}?${this.encodeParams(params)}`;
|
|
114
|
+
open(instance, params, callbacks) {
|
|
115
|
+
const url = `${this.url}${this.encodeParams(params)}`;
|
|
103
116
|
const call = () => {
|
|
104
117
|
callbacks.loading();
|
|
105
|
-
fetchJSON(url, "GET",
|
|
118
|
+
fetchJSON(url, "GET", this.options)
|
|
106
119
|
.then((r) => {
|
|
107
120
|
callbacks.update(this.conv(r[0]), true);
|
|
108
121
|
})
|
|
@@ -112,23 +125,14 @@ export class Polled {
|
|
|
112
125
|
});
|
|
113
126
|
};
|
|
114
127
|
call();
|
|
115
|
-
this.intervals.set(
|
|
128
|
+
this.intervals.set(instance, setInterval(call, this.duration));
|
|
116
129
|
}
|
|
117
|
-
close(
|
|
118
|
-
const interval = this.intervals.get(
|
|
130
|
+
close(instance) {
|
|
131
|
+
const interval = this.intervals.get(instance);
|
|
119
132
|
if (interval) {
|
|
120
133
|
clearInterval(interval);
|
|
134
|
+
this.intervals.delete(instance);
|
|
121
135
|
}
|
|
122
136
|
}
|
|
123
137
|
}
|
|
124
|
-
function toId(params) {
|
|
125
|
-
if (typeof params == "object") {
|
|
126
|
-
const strparams = Object.entries(params)
|
|
127
|
-
.map(([key, value]) => `${key}:${btoa(JSON.stringify(value))}`)
|
|
128
|
-
.sort();
|
|
129
|
-
return `[${strparams.join(",")}]`;
|
|
130
|
-
}
|
|
131
|
-
else
|
|
132
|
-
return btoa(JSON.stringify(params));
|
|
133
|
-
}
|
|
134
138
|
//# sourceMappingURL=external.js.map
|
package/dist/external.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"external.js","sourceRoot":"","sources":["../src/external.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"external.js","sourceRoot":"","sources":["../src/external.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,wBAAwB,EAAE,MAAM,mBAAmB,CAAC;AAC7D,OAAO,E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AAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAM,CAAC,EAAE,IAAI,CAAC,CAAC;YAC/C,CAAC,CAAC;iBACD,KAAK,CAAC,CAAC,CAAU,EAAE,EAAE;gBACpB,SAAS,CAAC,KAAK,CAAC,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;gBACpE,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YACnB,CAAC,CAAC,CAAC;QACP,CAAC,CAAC;QACF,IAAI,EAAE,CAAC;QACP,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,QAAQ,EAAE,WAAW,CAAC,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC;IACjE,CAAC;IAED,KAAK,CAAC,QAAgB;QACpB,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QAC9C,IAAI,QAAQ,EAAE,CAAC;YACb,aAAa,CAAC,QAAQ,CAAC,CAAC;YACxB,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QAClC,CAAC;IACH,CAAC;CACF"}
|
package/dist/remote.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Entry, ExternalService, Json } from "@skipruntime/
|
|
1
|
+
import type { Entry, ExternalService, Json } from "@skipruntime/core";
|
|
2
2
|
import type { Entrypoint } from "./rest.js";
|
|
3
3
|
/**
|
|
4
4
|
* An external Skip reactive service.
|
|
@@ -21,13 +21,12 @@ export declare class SkipExternalService implements ExternalService {
|
|
|
21
21
|
* @returns An `ExternalService` to interact with the service running at `entrypoint`.
|
|
22
22
|
*/
|
|
23
23
|
static direct(entrypoint: Entrypoint): SkipExternalService;
|
|
24
|
-
subscribe(resource: string, params: Json, callbacks: {
|
|
24
|
+
subscribe(instance: string, resource: string, params: Json, callbacks: {
|
|
25
25
|
update: (updates: Entry<Json, Json>[], isInitial: boolean) => void;
|
|
26
26
|
error: (error: Json) => void;
|
|
27
27
|
loading: () => void;
|
|
28
28
|
}): void;
|
|
29
|
-
unsubscribe(
|
|
29
|
+
unsubscribe(instance: string): void;
|
|
30
30
|
shutdown(): void;
|
|
31
|
-
private toId;
|
|
32
31
|
}
|
|
33
32
|
//# sourceMappingURL=remote.d.ts.map
|
package/dist/remote.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remote.d.ts","sourceRoot":"","sources":["../src/remote.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,KAAK,EAAE,eAAe,EAAE,IAAI,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"remote.d.ts","sourceRoot":"","sources":["../src/remote.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,KAAK,EAAE,eAAe,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAEtE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAM5C;;;;GAIG;AACH,qBAAa,mBAAoB,YAAW,eAAe;IAQvD,OAAO,CAAC,QAAQ,CAAC,GAAG;IACpB,OAAO,CAAC,QAAQ,CAAC,WAAW;IAR9B,OAAO,CAAC,QAAQ,CAAC,SAAS,CAA+B;IAEzD;;;OAGG;gBAEgB,GAAG,EAAE,MAAM,EACX,WAAW,EAAE,MAAM;IAGtC;;;;;OAKG;IAEH,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,UAAU,GAAG,mBAAmB;IAU1D,SAAS,CACP,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,IAAI,EACZ,SAAS,EAAE;QACT,MAAM,EAAE,CAAC,OAAO,EAAE,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,EAAE,SAAS,EAAE,OAAO,KAAK,IAAI,CAAC;QAEnE,KAAK,EAAE,CAAC,KAAK,EAAE,IAAI,KAAK,IAAI,CAAC;QAE7B,OAAO,EAAE,MAAM,IAAI,CAAC;KACrB,GACA,IAAI;IA8BP,WAAW,CAAC,QAAQ,EAAE,MAAM;IAQ5B,QAAQ,IAAI,IAAI;CAKjB"}
|
package/dist/remote.js
CHANGED
|
@@ -32,17 +32,14 @@ export class SkipExternalService {
|
|
|
32
32
|
}
|
|
33
33
|
return new SkipExternalService(url, control_url);
|
|
34
34
|
}
|
|
35
|
-
subscribe(resource, params, callbacks) {
|
|
35
|
+
subscribe(instance, resource, params, callbacks) {
|
|
36
36
|
// TODO Manage Status
|
|
37
|
-
fetch(`${this.control_url}/v1/streams`, {
|
|
37
|
+
fetch(`${this.control_url}/v1/streams/${resource}`, {
|
|
38
38
|
method: "POST",
|
|
39
39
|
headers: {
|
|
40
40
|
"Content-Type": "application/json",
|
|
41
41
|
},
|
|
42
|
-
body: JSON.stringify(
|
|
43
|
-
resource,
|
|
44
|
-
params,
|
|
45
|
-
}),
|
|
42
|
+
body: JSON.stringify(params),
|
|
46
43
|
})
|
|
47
44
|
.then((resp) => resp.text())
|
|
48
45
|
.then((uuid) => {
|
|
@@ -58,31 +55,23 @@ export class SkipExternalService {
|
|
|
58
55
|
evSource.onerror = (e) => {
|
|
59
56
|
console.log(e);
|
|
60
57
|
};
|
|
61
|
-
this.resources.set(
|
|
58
|
+
this.resources.set(instance, evSource);
|
|
62
59
|
})
|
|
63
60
|
.catch((e) => {
|
|
64
61
|
console.log(e);
|
|
65
62
|
});
|
|
66
63
|
}
|
|
67
|
-
unsubscribe(
|
|
68
|
-
const closable = this.resources.get(
|
|
69
|
-
if (closable)
|
|
64
|
+
unsubscribe(instance) {
|
|
65
|
+
const closable = this.resources.get(instance);
|
|
66
|
+
if (closable) {
|
|
70
67
|
closable.close();
|
|
68
|
+
this.resources.delete(instance);
|
|
69
|
+
}
|
|
71
70
|
}
|
|
72
71
|
shutdown() {
|
|
73
72
|
for (const res of this.resources.values()) {
|
|
74
73
|
res.close();
|
|
75
74
|
}
|
|
76
75
|
}
|
|
77
|
-
toId(resource, params) {
|
|
78
|
-
if (typeof params == "object") {
|
|
79
|
-
const strparams = Object.entries(params)
|
|
80
|
-
.map(([key, value]) => `${key}:${btoa(JSON.stringify(value))}`)
|
|
81
|
-
.sort();
|
|
82
|
-
return `${resource}[${strparams.join(",")}]`;
|
|
83
|
-
}
|
|
84
|
-
else
|
|
85
|
-
return `${resource}[${btoa(JSON.stringify(params))}]`;
|
|
86
|
-
}
|
|
87
76
|
}
|
|
88
77
|
//# sourceMappingURL=remote.js.map
|
package/dist/remote.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remote.js","sourceRoot":"","sources":["../src/remote.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,iBAAiB;AACjB,OAAO,WAAW,MAAM,aAAa,CAAC;AAUtC;;;;GAIG;AACH,MAAM,OAAO,mBAAmB;IAG9B;;;OAGG;IACH,YACmB,GAAW,EACX,WAAmB;QADnB,QAAG,GAAH,GAAG,CAAQ;QACX,gBAAW,GAAX,WAAW,CAAQ;QARrB,cAAS,GAAG,IAAI,GAAG,EAAoB,CAAC;IAStD,CAAC;IAEJ;;;;;OAKG;IACH,gEAAgE;IAChE,MAAM,CAAC,MAAM,CAAC,UAAsB;QAClC,IAAI,GAAG,GAAG,UAAU,UAAU,CAAC,IAAI,IAAI,UAAU,CAAC,cAAc,CAAC,QAAQ,EAAE,EAAE,CAAC;QAC9E,IAAI,WAAW,GAAG,UAAU,UAAU,CAAC,IAAI,IAAI,UAAU,CAAC,YAAY,CAAC,QAAQ,EAAE,EAAE,CAAC;QACpF,IAAI,UAAU,CAAC,OAAO,EAAE,CAAC;YACvB,GAAG,GAAG,WAAW,UAAU,CAAC,IAAI,IAAI,UAAU,CAAC,cAAc,CAAC,QAAQ,EAAE,EAAE,CAAC;YAC3E,WAAW,GAAG,WAAW,UAAU,CAAC,IAAI,IAAI,UAAU,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AAC,CAAC;IACP,CAAC;IAED,WAAW,CAAC,QAAgB;QAC1B,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QAC9C,IAAI,QAAQ,EAAE,CAAC;YACb,QAAQ,CAAC,KAAK,EAAE,CAAC;YACjB,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QAClC,CAAC;IACH,CAAC;IAED,QAAQ;QACN,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,EAAE,CAAC;YAC1C,GAAG,CAAC,KAAK,EAAE,CAAC;QACd,CAAC;IACH,CAAC;CACF"}
|
package/dist/rest.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Json, Entry } from "@skipruntime/
|
|
1
|
+
import type { Json, Entry } from "@skipruntime/core";
|
|
2
2
|
/**
|
|
3
3
|
* An entry point of a Skip reactive service.
|
|
4
4
|
*
|
|
@@ -86,20 +86,9 @@ export declare class SkipServiceBroker {
|
|
|
86
86
|
* @param params - Resource instance parameters.
|
|
87
87
|
* @param key - Key to read.
|
|
88
88
|
* @returns The value associated to the key.
|
|
89
|
-
* @throws `
|
|
89
|
+
* @throws `SkipNonUniqueValueError` when the key is associated to either zero or multiple values.
|
|
90
90
|
*/
|
|
91
91
|
getUnique<K extends Json, V extends Json>(resource: string, params: Json, key: K): Promise<V>;
|
|
92
|
-
/**
|
|
93
|
-
* Write the values for a single key in a collection.
|
|
94
|
-
*
|
|
95
|
-
* @typeParam K - Type of keys.
|
|
96
|
-
* @typeParam V - Type of values.
|
|
97
|
-
* @param collection - Name of the input collection to update, must be a key of the `Inputs` type parameter of the `SkipService` running at `entrypoint`.
|
|
98
|
-
* @param key - Key of entry to write.
|
|
99
|
-
* @param values - Values of entry to write.
|
|
100
|
-
* @returns {void}
|
|
101
|
-
*/
|
|
102
|
-
put<K extends Json, V extends Json>(collection: string, key: K, values: V[]): Promise<void>;
|
|
103
92
|
/**
|
|
104
93
|
* Write multiple entries to a collection.
|
|
105
94
|
*
|
|
@@ -109,7 +98,7 @@ export declare class SkipServiceBroker {
|
|
|
109
98
|
* @param entries - Entries to write.
|
|
110
99
|
* @returns {void}
|
|
111
100
|
*/
|
|
112
|
-
|
|
101
|
+
update<K extends Json, V extends Json>(collection: string, entries: Entry<K, V>[]): Promise<void>;
|
|
113
102
|
/**
|
|
114
103
|
* Remove all values associated with a key in a collection.
|
|
115
104
|
*
|
package/dist/rest.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rest.d.ts","sourceRoot":"","sources":["../src/rest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"rest.d.ts","sourceRoot":"","sources":["../src/rest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAGrD;;;;GAIG;AACH,MAAM,MAAM,UAAU,GAAG;IACvB;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,cAAc,EAAE,MAAM,CAAC;IAEvB;;OAEG;IACH,YAAY,EAAE,MAAM,CAAC;IAErB;;OAEG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB,CAAC;AAQF;;;;;;;;;;;GAWG;AACH,wBAAsB,SAAS,CAAC,CAAC,SAAS,IAAI,EAC5C,GAAG,EAAE,MAAM,EACX,MAAM,GAAE,MAAM,GAAG,KAAK,GAAG,KAAK,GAAG,OAAO,GAAG,MAAM,GAAG,QAAgB,EACpE,OAAO,GAAE;IACP,OAAO,CAAC,EAAE;QAAE,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAC;IACvC,IAAI,CAAC,EAAE,IAAI,CAAC;IACZ,OAAO,CAAC,EAAE,MAAM,CAAC;CAIlB,GACA,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AC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAG9C"}
|
package/dist/rest.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SkipNonUniqueValueError, SkipFetchError } from "@skipruntime/core";
|
|
2
2
|
function toHttp(entrypoint) {
|
|
3
3
|
if (entrypoint.secured)
|
|
4
4
|
return `https://${entrypoint.host}:${entrypoint.control_port}`;
|
|
@@ -29,10 +29,10 @@ export async function fetchJSON(url, method = "GET", options = {
|
|
|
29
29
|
Accept: "application/json",
|
|
30
30
|
...options.headers,
|
|
31
31
|
},
|
|
32
|
-
signal: AbortSignal.timeout(1000),
|
|
32
|
+
signal: AbortSignal.timeout(options.timeout ?? 1000),
|
|
33
33
|
});
|
|
34
34
|
if (!response.ok) {
|
|
35
|
-
throw new
|
|
35
|
+
throw new SkipFetchError(`${response.status}: ${response.statusText}`);
|
|
36
36
|
}
|
|
37
37
|
const responseText = await response.text();
|
|
38
38
|
const responseJSON = responseText.length > 0 && responseText != response.statusText
|
|
@@ -96,28 +96,15 @@ export class SkipServiceBroker {
|
|
|
96
96
|
* @param params - Resource instance parameters.
|
|
97
97
|
* @param key - Key to read.
|
|
98
98
|
* @returns The value associated to the key.
|
|
99
|
-
* @throws `
|
|
99
|
+
* @throws `SkipNonUniqueValueError` when the key is associated to either zero or multiple values.
|
|
100
100
|
*/
|
|
101
101
|
async getUnique(resource, params, key) {
|
|
102
102
|
return this.getArray(resource, params, key).then((values) => {
|
|
103
103
|
if (values.length !== 1 || values[0] === undefined)
|
|
104
|
-
throw new
|
|
104
|
+
throw new SkipNonUniqueValueError();
|
|
105
105
|
return values[0];
|
|
106
106
|
});
|
|
107
107
|
}
|
|
108
|
-
/**
|
|
109
|
-
* Write the values for a single key in a collection.
|
|
110
|
-
*
|
|
111
|
-
* @typeParam K - Type of keys.
|
|
112
|
-
* @typeParam V - Type of values.
|
|
113
|
-
* @param collection - Name of the input collection to update, must be a key of the `Inputs` type parameter of the `SkipService` running at `entrypoint`.
|
|
114
|
-
* @param key - Key of entry to write.
|
|
115
|
-
* @param values - Values of entry to write.
|
|
116
|
-
* @returns {void}
|
|
117
|
-
*/
|
|
118
|
-
async put(collection, key, values) {
|
|
119
|
-
return await this.patch(collection, [[key, values]]);
|
|
120
|
-
}
|
|
121
108
|
/**
|
|
122
109
|
* Write multiple entries to a collection.
|
|
123
110
|
*
|
|
@@ -127,7 +114,7 @@ export class SkipServiceBroker {
|
|
|
127
114
|
* @param entries - Entries to write.
|
|
128
115
|
* @returns {void}
|
|
129
116
|
*/
|
|
130
|
-
async
|
|
117
|
+
async update(collection, entries) {
|
|
131
118
|
await fetchJSON(`${this.entrypoint}/v1/inputs/${collection}`, "PATCH", {
|
|
132
119
|
body: entries,
|
|
133
120
|
});
|
|
@@ -141,7 +128,7 @@ export class SkipServiceBroker {
|
|
|
141
128
|
* @returns {void}
|
|
142
129
|
*/
|
|
143
130
|
async deleteKey(collection, key) {
|
|
144
|
-
return await this.
|
|
131
|
+
return await this.update(collection, [[key, []]]);
|
|
145
132
|
}
|
|
146
133
|
/**
|
|
147
134
|
* Create a resource instance UUID.
|
package/dist/rest.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rest.js","sourceRoot":"","sources":["../src/rest.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,uBAAuB,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"rest.js","sourceRoot":"","sources":["../src/rest.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,uBAAuB,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AA6B5E,SAAS,MAAM,CAAC,UAAsB;IACpC,IAAI,UAAU,CAAC,OAAO;QACpB,OAAO,WAAW,UAAU,CAAC,IAAI,IAAI,UAAU,CAAC,YAAY,EAAE,CAAC;IACjE,OAAO,UAAU,UAAU,CAAC,IAAI,IAAI,UAAU,CAAC,YAAY,EAAE,CAAC;AAChE,CAAC;AAED;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IACL,CAAC;IAED;;;;;;;OAOG;IACH,KAAK,CAAC,SAAS,CAAiB,UAAkB,EAAE,GAAM;QACxD,OAAO,MAAM,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC;IACpD,CAAC;IAED;;;;;;;;OAQG;IACH,KAAK,CAAC,aAAa,CAAC,QAAgB,EAAE,SAAe,EAAE;QACrD,OAAO,KAAK,CAAC,GAAG,IAAI,CAAC,UAAU,eAAe,QAAQ,EAAE,EAAE;YACxD,MAAM,EAAE,MAAM;YACd,OAAO,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE;YAC/C,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC;SAC7B,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC;IAC/B,CAAC;IAED;;;;;;;OAOG;IACH,KAAK,CAAC,UAAU,CAAC,IAAY;QAC3B,MAAM,SAAS,CAAC,GAAG,IAAI,CAAC,UAAU,eAAe,IAAI,EAAE,EAAE,QAAQ,CAAC,CAAC;IACrE,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@skipruntime/helpers",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.10",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./dist/index.js",
|
|
@@ -21,8 +21,7 @@
|
|
|
21
21
|
"dependencies": {
|
|
22
22
|
"eventsource": "^2.0.2",
|
|
23
23
|
"express": "^4.21.1",
|
|
24
|
-
"@skipruntime/core": "
|
|
25
|
-
"@skipruntime/api": "^0.0.6"
|
|
24
|
+
"@skipruntime/core": "0.0.10"
|
|
26
25
|
},
|
|
27
26
|
"devDependencies": {
|
|
28
27
|
"@types/eventsource": "^1.1.15"
|
package/src/external.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { Entry, ExternalService, Json } from "@skipruntime/
|
|
1
|
+
import type { Entry, ExternalService, Json } from "@skipruntime/core";
|
|
2
|
+
import { SkipUnknownResourceError } from "@skipruntime/core";
|
|
2
3
|
import { fetchJSON } from "./rest.js";
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -6,6 +7,7 @@ import { fetchJSON } from "./rest.js";
|
|
|
6
7
|
*/
|
|
7
8
|
export interface ExternalResource {
|
|
8
9
|
open(
|
|
10
|
+
instance: string,
|
|
9
11
|
params: Json,
|
|
10
12
|
callbacks: {
|
|
11
13
|
update: (updates: Entry<Json, Json>[], isInit: boolean) => void;
|
|
@@ -14,7 +16,7 @@ export interface ExternalResource {
|
|
|
14
16
|
},
|
|
15
17
|
): void;
|
|
16
18
|
|
|
17
|
-
close(
|
|
19
|
+
close(instance: string): void;
|
|
18
20
|
}
|
|
19
21
|
|
|
20
22
|
/**
|
|
@@ -23,6 +25,8 @@ export interface ExternalResource {
|
|
|
23
25
|
* `GenericExternalService` provides an implementation of `ExternalService` for external resources by lifting the `open` and `close` operations from `ExternalResource` to the `subscribe` and `unsubscribe` operations required by `ExternalService`.
|
|
24
26
|
*/
|
|
25
27
|
export class GenericExternalService implements ExternalService {
|
|
28
|
+
private readonly instances = new Map<string, ExternalResource>();
|
|
29
|
+
|
|
26
30
|
/**
|
|
27
31
|
* @param resources - Association of resource names to `ExternalResource`s.
|
|
28
32
|
*/
|
|
@@ -31,6 +35,7 @@ export class GenericExternalService implements ExternalService {
|
|
|
31
35
|
) {}
|
|
32
36
|
|
|
33
37
|
subscribe(
|
|
38
|
+
instance: string,
|
|
34
39
|
resourceName: string,
|
|
35
40
|
params: Json,
|
|
36
41
|
callbacks: {
|
|
@@ -38,24 +43,25 @@ export class GenericExternalService implements ExternalService {
|
|
|
38
43
|
error: (error: Json) => void;
|
|
39
44
|
loading: () => void;
|
|
40
45
|
},
|
|
41
|
-
)
|
|
46
|
+
) {
|
|
42
47
|
const resource = this.resources[resourceName] as
|
|
43
48
|
| ExternalResource
|
|
44
49
|
| undefined;
|
|
45
50
|
if (!resource) {
|
|
46
|
-
throw new
|
|
51
|
+
throw new SkipUnknownResourceError(
|
|
52
|
+
`Unknown resource named '${resourceName}'`,
|
|
53
|
+
);
|
|
47
54
|
}
|
|
48
|
-
|
|
55
|
+
this.instances.set(instance, resource);
|
|
56
|
+
resource.open(instance, params, callbacks);
|
|
49
57
|
}
|
|
50
58
|
|
|
51
|
-
unsubscribe(
|
|
52
|
-
const resource = this.
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
throw new Error(`Unkonwn resource named '${resourceName}'`);
|
|
59
|
+
unsubscribe(instance: string) {
|
|
60
|
+
const resource = this.instances.get(instance);
|
|
61
|
+
if (resource) {
|
|
62
|
+
resource.close(instance);
|
|
63
|
+
this.instances.delete(instance);
|
|
57
64
|
}
|
|
58
|
-
resource.close(params);
|
|
59
65
|
}
|
|
60
66
|
|
|
61
67
|
shutdown(): void {
|
|
@@ -64,11 +70,13 @@ export class GenericExternalService implements ExternalService {
|
|
|
64
70
|
}
|
|
65
71
|
|
|
66
72
|
type Timeout = ReturnType<typeof setInterval>;
|
|
73
|
+
type Timeouts = { [name: string]: Timeout };
|
|
67
74
|
|
|
68
75
|
export class TimerResource implements ExternalResource {
|
|
69
76
|
private readonly intervals = new Map<string, { [name: string]: Timeout }>();
|
|
70
77
|
|
|
71
78
|
open(
|
|
79
|
+
instance: string,
|
|
72
80
|
params: Json,
|
|
73
81
|
callbacks: {
|
|
74
82
|
update: (updates: Entry<Json, Json>[], isInit: boolean) => void;
|
|
@@ -82,8 +90,7 @@ export class TimerResource implements ExternalResource {
|
|
|
82
90
|
values.push([name, [time]]);
|
|
83
91
|
}
|
|
84
92
|
callbacks.update(values, true);
|
|
85
|
-
const
|
|
86
|
-
const intervals: { [name: string]: Timeout } = {};
|
|
93
|
+
const intervals: Timeouts = {};
|
|
87
94
|
for (const [name, duration] of Object.entries(params)) {
|
|
88
95
|
const ms = Number(duration);
|
|
89
96
|
if (ms > 0) {
|
|
@@ -93,15 +100,16 @@ export class TimerResource implements ExternalResource {
|
|
|
93
100
|
}, ms);
|
|
94
101
|
}
|
|
95
102
|
}
|
|
96
|
-
this.intervals.set(
|
|
103
|
+
this.intervals.set(instance, intervals);
|
|
97
104
|
}
|
|
98
105
|
|
|
99
|
-
close(
|
|
100
|
-
const intervals = this.intervals.get(
|
|
106
|
+
close(instance: string): void {
|
|
107
|
+
const intervals = this.intervals.get(instance);
|
|
101
108
|
if (intervals != null) {
|
|
102
109
|
for (const interval of Object.values(intervals)) {
|
|
103
110
|
clearInterval(interval);
|
|
104
111
|
}
|
|
112
|
+
this.intervals.delete(instance);
|
|
105
113
|
}
|
|
106
114
|
}
|
|
107
115
|
}
|
|
@@ -113,8 +121,8 @@ function defaultParamEncoder(params: Json): string {
|
|
|
113
121
|
if (typeof value == "object") queryParams[key] = JSON.stringify(value);
|
|
114
122
|
else queryParams[key] = value.toString();
|
|
115
123
|
}
|
|
116
|
-
return new URLSearchParams(queryParams).toString()
|
|
117
|
-
} else return
|
|
124
|
+
return `?${new URLSearchParams(queryParams).toString()}`;
|
|
125
|
+
} else return `?params=${JSON.stringify(params)}`;
|
|
118
126
|
}
|
|
119
127
|
|
|
120
128
|
/**
|
|
@@ -130,10 +138,20 @@ export class Polled<S extends Json, K extends Json, V extends Json>
|
|
|
130
138
|
private readonly intervals = new Map<string, Timeout>();
|
|
131
139
|
|
|
132
140
|
/**
|
|
141
|
+
* Construct a `Polled` external resource.
|
|
142
|
+
*
|
|
143
|
+
* The URL of the external resource is formed by appending the given base `url` and the result of `encodeParams(params)` where `params` are the parameters provided when instantiating the resource.
|
|
144
|
+
* The default `encodeParams` function produces a query string consisting of a leading `?` followed by the encoding of the `params` given by [URLSearchParams.toString](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/toString) when `params` is an object and otherwise a single key-value pair `params=JSON.stringify(params)`.
|
|
145
|
+
*
|
|
146
|
+
* Note that the result of `encodeParams` contains the `?` separator, but it need not be at the beginning of the returned string, so some parameters can be used in part of the URL preceding the `?`.
|
|
147
|
+
*
|
|
133
148
|
* @param url - HTTP endpoint of external resource to poll.
|
|
134
149
|
* @param duration - Refresh interval, in milliseconds.
|
|
135
150
|
* @param conv - Function to convert data of type `S` received from external resource to `key`-`value` entries.
|
|
136
151
|
* @param encodeParams - Function to use to encode params of type `Json` for external resource request.
|
|
152
|
+
* @param options - Optional parameters.
|
|
153
|
+
* @param options.headers - Additional headers to add to request.
|
|
154
|
+
* @param options.timeout - Timeout for request, in milliseconds. Defaults to 1000ms.
|
|
137
155
|
*/
|
|
138
156
|
constructor(
|
|
139
157
|
private readonly url: string,
|
|
@@ -142,21 +160,25 @@ export class Polled<S extends Json, K extends Json, V extends Json>
|
|
|
142
160
|
private readonly encodeParams: (
|
|
143
161
|
params: Json,
|
|
144
162
|
) => string = defaultParamEncoder,
|
|
163
|
+
private readonly options?: {
|
|
164
|
+
headers?: { [header: string]: string };
|
|
165
|
+
timeout?: number;
|
|
166
|
+
},
|
|
145
167
|
) {}
|
|
146
168
|
|
|
147
169
|
open(
|
|
170
|
+
instance: string,
|
|
148
171
|
params: Json,
|
|
149
172
|
callbacks: {
|
|
150
173
|
update: (updates: Entry<Json, Json>[], isInit: boolean) => void;
|
|
151
174
|
error: (error: Json) => void;
|
|
152
175
|
loading: () => void;
|
|
153
176
|
},
|
|
154
|
-
)
|
|
155
|
-
this.
|
|
156
|
-
const url = `${this.url}?${this.encodeParams(params)}`;
|
|
177
|
+
) {
|
|
178
|
+
const url = `${this.url}${this.encodeParams(params)}`;
|
|
157
179
|
const call = () => {
|
|
158
180
|
callbacks.loading();
|
|
159
|
-
fetchJSON(url, "GET",
|
|
181
|
+
fetchJSON(url, "GET", this.options)
|
|
160
182
|
.then((r) => {
|
|
161
183
|
callbacks.update(this.conv(r[0] as S), true);
|
|
162
184
|
})
|
|
@@ -166,22 +188,14 @@ export class Polled<S extends Json, K extends Json, V extends Json>
|
|
|
166
188
|
});
|
|
167
189
|
};
|
|
168
190
|
call();
|
|
169
|
-
this.intervals.set(
|
|
191
|
+
this.intervals.set(instance, setInterval(call, this.duration));
|
|
170
192
|
}
|
|
171
193
|
|
|
172
|
-
close(
|
|
173
|
-
const interval = this.intervals.get(
|
|
194
|
+
close(instance: string): void {
|
|
195
|
+
const interval = this.intervals.get(instance);
|
|
174
196
|
if (interval) {
|
|
175
197
|
clearInterval(interval);
|
|
198
|
+
this.intervals.delete(instance);
|
|
176
199
|
}
|
|
177
200
|
}
|
|
178
201
|
}
|
|
179
|
-
|
|
180
|
-
function toId(params: Json): string {
|
|
181
|
-
if (typeof params == "object") {
|
|
182
|
-
const strparams = Object.entries(params)
|
|
183
|
-
.map(([key, value]) => `${key}:${btoa(JSON.stringify(value))}`)
|
|
184
|
-
.sort();
|
|
185
|
-
return `[${strparams.join(",")}]`;
|
|
186
|
-
} else return btoa(JSON.stringify(params));
|
|
187
|
-
}
|
package/src/remote.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// in nodejs LTS.
|
|
3
3
|
import EventSource from "eventsource";
|
|
4
4
|
|
|
5
|
-
import type { Entry, ExternalService, Json } from "@skipruntime/
|
|
5
|
+
import type { Entry, ExternalService, Json } from "@skipruntime/core";
|
|
6
6
|
|
|
7
7
|
import type { Entrypoint } from "./rest.js";
|
|
8
8
|
|
|
@@ -45,6 +45,7 @@ export class SkipExternalService implements ExternalService {
|
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
subscribe(
|
|
48
|
+
instance: string,
|
|
48
49
|
resource: string,
|
|
49
50
|
params: Json,
|
|
50
51
|
callbacks: {
|
|
@@ -56,15 +57,12 @@ export class SkipExternalService implements ExternalService {
|
|
|
56
57
|
},
|
|
57
58
|
): void {
|
|
58
59
|
// TODO Manage Status
|
|
59
|
-
fetch(`${this.control_url}/v1/streams`, {
|
|
60
|
+
fetch(`${this.control_url}/v1/streams/${resource}`, {
|
|
60
61
|
method: "POST",
|
|
61
62
|
headers: {
|
|
62
63
|
"Content-Type": "application/json",
|
|
63
64
|
},
|
|
64
|
-
body: JSON.stringify(
|
|
65
|
-
resource,
|
|
66
|
-
params,
|
|
67
|
-
}),
|
|
65
|
+
body: JSON.stringify(params),
|
|
68
66
|
})
|
|
69
67
|
.then((resp) => resp.text())
|
|
70
68
|
.then((uuid) => {
|
|
@@ -80,16 +78,19 @@ export class SkipExternalService implements ExternalService {
|
|
|
80
78
|
evSource.onerror = (e) => {
|
|
81
79
|
console.log(e);
|
|
82
80
|
};
|
|
83
|
-
this.resources.set(
|
|
81
|
+
this.resources.set(instance, evSource);
|
|
84
82
|
})
|
|
85
83
|
.catch((e: unknown) => {
|
|
86
84
|
console.log(e);
|
|
87
85
|
});
|
|
88
86
|
}
|
|
89
87
|
|
|
90
|
-
unsubscribe(
|
|
91
|
-
const closable = this.resources.get(
|
|
92
|
-
if (closable)
|
|
88
|
+
unsubscribe(instance: string) {
|
|
89
|
+
const closable = this.resources.get(instance);
|
|
90
|
+
if (closable) {
|
|
91
|
+
closable.close();
|
|
92
|
+
this.resources.delete(instance);
|
|
93
|
+
}
|
|
93
94
|
}
|
|
94
95
|
|
|
95
96
|
shutdown(): void {
|
|
@@ -97,13 +98,4 @@ export class SkipExternalService implements ExternalService {
|
|
|
97
98
|
res.close();
|
|
98
99
|
}
|
|
99
100
|
}
|
|
100
|
-
|
|
101
|
-
private toId(resource: string, params: Json): string {
|
|
102
|
-
if (typeof params == "object") {
|
|
103
|
-
const strparams = Object.entries(params)
|
|
104
|
-
.map(([key, value]) => `${key}:${btoa(JSON.stringify(value))}`)
|
|
105
|
-
.sort();
|
|
106
|
-
return `${resource}[${strparams.join(",")}]`;
|
|
107
|
-
} else return `${resource}[${btoa(JSON.stringify(params))}]`;
|
|
108
|
-
}
|
|
109
101
|
}
|
package/src/rest.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Json, Entry } from "@skipruntime/
|
|
2
|
-
import {
|
|
1
|
+
import type { Json, Entry } from "@skipruntime/core";
|
|
2
|
+
import { SkipNonUniqueValueError, SkipFetchError } from "@skipruntime/core";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* An entry point of a Skip reactive service.
|
|
@@ -67,10 +67,10 @@ export async function fetchJSON<V extends Json>(
|
|
|
67
67
|
Accept: "application/json",
|
|
68
68
|
...options.headers,
|
|
69
69
|
},
|
|
70
|
-
signal: AbortSignal.timeout(1000),
|
|
70
|
+
signal: AbortSignal.timeout(options.timeout ?? 1000),
|
|
71
71
|
});
|
|
72
72
|
if (!response.ok) {
|
|
73
|
-
throw new
|
|
73
|
+
throw new SkipFetchError(`${response.status}: ${response.statusText}`);
|
|
74
74
|
}
|
|
75
75
|
const responseText = await response.text();
|
|
76
76
|
const responseJSON =
|
|
@@ -158,7 +158,7 @@ export class SkipServiceBroker {
|
|
|
158
158
|
* @param params - Resource instance parameters.
|
|
159
159
|
* @param key - Key to read.
|
|
160
160
|
* @returns The value associated to the key.
|
|
161
|
-
* @throws `
|
|
161
|
+
* @throws `SkipNonUniqueValueError` when the key is associated to either zero or multiple values.
|
|
162
162
|
*/
|
|
163
163
|
async getUnique<K extends Json, V extends Json>(
|
|
164
164
|
resource: string,
|
|
@@ -167,29 +167,11 @@ export class SkipServiceBroker {
|
|
|
167
167
|
): Promise<V> {
|
|
168
168
|
return this.getArray<K, V>(resource, params, key).then((values) => {
|
|
169
169
|
if (values.length !== 1 || values[0] === undefined)
|
|
170
|
-
throw new
|
|
170
|
+
throw new SkipNonUniqueValueError();
|
|
171
171
|
return values[0];
|
|
172
172
|
});
|
|
173
173
|
}
|
|
174
174
|
|
|
175
|
-
/**
|
|
176
|
-
* Write the values for a single key in a collection.
|
|
177
|
-
*
|
|
178
|
-
* @typeParam K - Type of keys.
|
|
179
|
-
* @typeParam V - Type of values.
|
|
180
|
-
* @param collection - Name of the input collection to update, must be a key of the `Inputs` type parameter of the `SkipService` running at `entrypoint`.
|
|
181
|
-
* @param key - Key of entry to write.
|
|
182
|
-
* @param values - Values of entry to write.
|
|
183
|
-
* @returns {void}
|
|
184
|
-
*/
|
|
185
|
-
async put<K extends Json, V extends Json>(
|
|
186
|
-
collection: string,
|
|
187
|
-
key: K,
|
|
188
|
-
values: V[],
|
|
189
|
-
): Promise<void> {
|
|
190
|
-
return await this.patch(collection, [[key, values]]);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
175
|
/**
|
|
194
176
|
* Write multiple entries to a collection.
|
|
195
177
|
*
|
|
@@ -199,7 +181,7 @@ export class SkipServiceBroker {
|
|
|
199
181
|
* @param entries - Entries to write.
|
|
200
182
|
* @returns {void}
|
|
201
183
|
*/
|
|
202
|
-
async
|
|
184
|
+
async update<K extends Json, V extends Json>(
|
|
203
185
|
collection: string,
|
|
204
186
|
entries: Entry<K, V>[],
|
|
205
187
|
): Promise<void> {
|
|
@@ -217,7 +199,7 @@ export class SkipServiceBroker {
|
|
|
217
199
|
* @returns {void}
|
|
218
200
|
*/
|
|
219
201
|
async deleteKey<K extends Json>(collection: string, key: K): Promise<void> {
|
|
220
|
-
return await this.
|
|
202
|
+
return await this.update(collection, [[key, []]]);
|
|
221
203
|
}
|
|
222
204
|
|
|
223
205
|
/**
|