@skipruntime/helpers 0.0.4 → 0.0.6
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/dist/external.d.ts +40 -31
- package/dist/external.d.ts.map +1 -1
- package/dist/external.js +46 -11
- package/dist/external.js.map +1 -1
- package/dist/index.d.ts +7 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -3
- package/dist/index.js.map +1 -1
- package/dist/rest.d.ts +108 -20
- package/dist/rest.d.ts.map +1 -1
- package/dist/rest.js +117 -13
- package/dist/rest.js.map +1 -1
- package/package.json +4 -4
- package/src/external.ts +62 -25
- package/src/index.ts +15 -3
- package/src/rest.ts +139 -33
- package/dist/errors.d.ts +0 -3
- package/dist/errors.d.ts.map +0 -1
- package/dist/errors.js +0 -3
- package/dist/errors.js.map +0 -1
- package/dist/remote.d.ts +0 -22
- package/dist/remote.d.ts.map +0 -1
- package/dist/remote.js +0 -71
- package/dist/remote.js.map +0 -1
- package/dist/utils.d.ts +0 -22
- package/dist/utils.d.ts.map +0 -1
- package/dist/utils.js +0 -40
- package/dist/utils.js.map +0 -1
- package/src/errors.ts +0 -1
- package/src/remote.ts +0 -94
- package/src/utils.ts +0 -48
package/src/remote.ts
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
// TODO: Remove once global `EventSource` makes it out of experimental
|
|
2
|
-
// in nodejs LTS.
|
|
3
|
-
import EventSource from "eventsource";
|
|
4
|
-
|
|
5
|
-
import type { Entry, ExternalService, Json } from "@skipruntime/api";
|
|
6
|
-
|
|
7
|
-
import type { Entrypoint } from "./rest.js";
|
|
8
|
-
|
|
9
|
-
interface Closable {
|
|
10
|
-
close(): void;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export class SkipExternalService implements ExternalService {
|
|
14
|
-
private resources = new Map<string, Closable>();
|
|
15
|
-
|
|
16
|
-
constructor(
|
|
17
|
-
private url: string,
|
|
18
|
-
private control_url: string,
|
|
19
|
-
) {}
|
|
20
|
-
|
|
21
|
-
// TODO: Support Skip external services going through a gateway.
|
|
22
|
-
static direct(entrypoint: Entrypoint): SkipExternalService {
|
|
23
|
-
let url = `http://${entrypoint.host}:${entrypoint.streaming_port.toString()}`;
|
|
24
|
-
let control_url = `http://${entrypoint.host}:${entrypoint.control_port.toString()}`;
|
|
25
|
-
if (entrypoint.secured) {
|
|
26
|
-
url = `https://${entrypoint.host}:${entrypoint.streaming_port.toString()}`;
|
|
27
|
-
control_url = `https://${entrypoint.host}:${entrypoint.control_port.toString()}`;
|
|
28
|
-
}
|
|
29
|
-
return new SkipExternalService(url, control_url);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
subscribe(
|
|
33
|
-
resource: string,
|
|
34
|
-
params: { [param: string]: string },
|
|
35
|
-
callbacks: {
|
|
36
|
-
update: (updates: Entry<Json, Json>[], isInitial: boolean) => void;
|
|
37
|
-
// FIXME: What is `error()` used for?
|
|
38
|
-
error: (error: Json) => void;
|
|
39
|
-
// FIXME: What is `loading()` used for?
|
|
40
|
-
loading: () => void;
|
|
41
|
-
},
|
|
42
|
-
): void {
|
|
43
|
-
// TODO Manage Status
|
|
44
|
-
fetch(`${this.control_url}/v1/streams`, {
|
|
45
|
-
method: "POST",
|
|
46
|
-
headers: {
|
|
47
|
-
"Content-Type": "application/json",
|
|
48
|
-
},
|
|
49
|
-
body: JSON.stringify({
|
|
50
|
-
resource,
|
|
51
|
-
params,
|
|
52
|
-
}),
|
|
53
|
-
})
|
|
54
|
-
.then((resp) => resp.text())
|
|
55
|
-
.then((uuid) => {
|
|
56
|
-
const evSource = new EventSource(`${this.url}/v1/streams/${uuid}`);
|
|
57
|
-
evSource.addEventListener("init", (e: MessageEvent<string>) => {
|
|
58
|
-
const updates = JSON.parse(e.data) as Entry<Json, Json>[];
|
|
59
|
-
callbacks.update(updates, true);
|
|
60
|
-
});
|
|
61
|
-
evSource.addEventListener("update", (e: MessageEvent<string>) => {
|
|
62
|
-
const updates = JSON.parse(e.data) as Entry<Json, Json>[];
|
|
63
|
-
callbacks.update(updates, false);
|
|
64
|
-
});
|
|
65
|
-
evSource.onerror = (e) => {
|
|
66
|
-
console.log(e);
|
|
67
|
-
};
|
|
68
|
-
this.resources.set(this.toId(resource, params), evSource);
|
|
69
|
-
})
|
|
70
|
-
.catch((e: unknown) => {
|
|
71
|
-
console.log(e);
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
unsubscribe(resource: string, params: { [param: string]: string }) {
|
|
76
|
-
const closable = this.resources.get(this.toId(resource, params));
|
|
77
|
-
if (closable) closable.close();
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
shutdown(): void {
|
|
81
|
-
for (const res of this.resources.values()) {
|
|
82
|
-
res.close();
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
private toId(resource: string, params: { [param: string]: string }): string {
|
|
87
|
-
// TODO: This is equivalent to `querystring.encode(params, ',', ':')`.
|
|
88
|
-
const strparams: string[] = [];
|
|
89
|
-
for (const key of Object.keys(params).sort()) {
|
|
90
|
-
strparams.push(`${key}:${params[key]}`);
|
|
91
|
-
}
|
|
92
|
-
return `${resource}[${strparams.join(",")}]`;
|
|
93
|
-
}
|
|
94
|
-
}
|
package/src/utils.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import type { Nullable } from "@skip-wasm/std";
|
|
2
|
-
import { ManyToOneMapper } from "@skipruntime/api";
|
|
3
|
-
import type { Reducer, NonEmptyIterator, Json } from "@skipruntime/api";
|
|
4
|
-
|
|
5
|
-
export class Sum implements Reducer<number, number> {
|
|
6
|
-
default = 0;
|
|
7
|
-
|
|
8
|
-
add(acc: number, value: number): number {
|
|
9
|
-
return acc + value;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
remove(acc: number, value: number): Nullable<number> {
|
|
13
|
-
return acc - value;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export class Min implements Reducer<number, number> {
|
|
18
|
-
default = null;
|
|
19
|
-
|
|
20
|
-
add(acc: Nullable<number>, value: number): number {
|
|
21
|
-
return acc === null ? value : Math.min(acc, value);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
remove(acc: number, value: number): Nullable<number> {
|
|
25
|
-
return value > acc ? acc : null;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export class Max implements Reducer<number, number> {
|
|
30
|
-
default = null;
|
|
31
|
-
|
|
32
|
-
add(acc: Nullable<number>, value: number): number {
|
|
33
|
-
return acc === null ? value : Math.max(acc, value);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
remove(acc: number, value: number): Nullable<number> {
|
|
37
|
-
return value < acc ? acc : null;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export class CountMapper<
|
|
42
|
-
K extends Json,
|
|
43
|
-
V extends Json,
|
|
44
|
-
> extends ManyToOneMapper<K, V, number> {
|
|
45
|
-
mapValues(values: NonEmptyIterator<V>): number {
|
|
46
|
-
return values.toArray().length;
|
|
47
|
-
}
|
|
48
|
-
}
|