@skipruntime/core 0.0.1
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/binding.d.ts +70 -0
- package/dist/binding.d.ts.map +1 -0
- package/dist/binding.js +11 -0
- package/dist/binding.js.map +1 -0
- package/dist/errors.d.ts +3 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +3 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +206 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +707 -0
- package/dist/index.js.map +1 -0
- package/dist/internal.d.ts +38 -0
- package/dist/internal.d.ts.map +1 -0
- package/dist/internal.js +2 -0
- package/dist/internal.js.map +1 -0
- package/dist/remote.d.ts +22 -0
- package/dist/remote.d.ts.map +1 -0
- package/dist/remote.js +71 -0
- package/dist/remote.js.map +1 -0
- package/dist/utils.d.ts +22 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +40 -0
- package/dist/utils.js.map +1 -0
- package/eslint.config.js +3 -0
- package/package.json +18 -0
- package/src/binding.ts +258 -0
- package/src/errors.ts +1 -0
- package/src/index.ts +1134 -0
- package/src/internal.ts +54 -0
- package/src/remote.ts +94 -0
- package/src/utils.ts +48 -0
- package/tsconfig.json +3 -0
package/src/internal.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export type * from "@skiplang/json/internal.js";
|
|
2
|
+
import type { T, CJSON } from "@skiplang/json/internal.js";
|
|
3
|
+
|
|
4
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
5
|
+
|
|
6
|
+
declare const context: unique symbol;
|
|
7
|
+
export type Context = T<typeof context>;
|
|
8
|
+
|
|
9
|
+
declare const p: unique symbol;
|
|
10
|
+
|
|
11
|
+
declare const lhandle: unique symbol;
|
|
12
|
+
export type LHandle<K extends T<any> = CJSON, V extends T<any> = CJSON> = T<
|
|
13
|
+
typeof lhandle
|
|
14
|
+
> & { [p]: (_: V) => K };
|
|
15
|
+
|
|
16
|
+
declare const nonemptyiterator: unique symbol;
|
|
17
|
+
export type NonEmptyIterator<Ty extends T<any> = CJSON> = T<
|
|
18
|
+
typeof nonemptyiterator
|
|
19
|
+
> & { [p]: Ty };
|
|
20
|
+
|
|
21
|
+
declare const mapper: unique symbol;
|
|
22
|
+
export type Mapper = T<typeof mapper>;
|
|
23
|
+
|
|
24
|
+
declare const lazycompute: unique symbol;
|
|
25
|
+
export type LazyCompute = T<typeof lazycompute> & {
|
|
26
|
+
compute: (self: unknown, key: CJSON) => CJSON;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
declare const externalsupplier: unique symbol;
|
|
30
|
+
export type ExternalService = T<typeof externalsupplier>;
|
|
31
|
+
|
|
32
|
+
declare const resource: unique symbol;
|
|
33
|
+
export type Resource = T<typeof resource>;
|
|
34
|
+
|
|
35
|
+
declare const resourcebuilder: unique symbol;
|
|
36
|
+
export type ResourceBuilder = T<typeof resourcebuilder>;
|
|
37
|
+
|
|
38
|
+
declare const service: unique symbol;
|
|
39
|
+
export type Service = T<typeof service>;
|
|
40
|
+
|
|
41
|
+
declare const resourcebuildermap: unique symbol;
|
|
42
|
+
export type ResourceBuilderMap = T<typeof resourcebuildermap>;
|
|
43
|
+
|
|
44
|
+
declare const externalsuppliermap: unique symbol;
|
|
45
|
+
export type ExternalServiceMap = T<typeof externalsuppliermap>;
|
|
46
|
+
|
|
47
|
+
declare const reducer: unique symbol;
|
|
48
|
+
export type Reducer = T<typeof reducer>;
|
|
49
|
+
|
|
50
|
+
declare const notifier: unique symbol;
|
|
51
|
+
export type Notifier = T<typeof notifier>;
|
|
52
|
+
|
|
53
|
+
declare const request: unique symbol;
|
|
54
|
+
export type Request = T<typeof request>;
|
package/src/remote.ts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
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 "./index.js";
|
|
8
|
+
|
|
9
|
+
interface Closable {
|
|
10
|
+
close(): void;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class SkipExternalService implements ExternalService {
|
|
14
|
+
private readonly resources = new Map<string, Closable>();
|
|
15
|
+
|
|
16
|
+
constructor(
|
|
17
|
+
private readonly url: string,
|
|
18
|
+
private readonly 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
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
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
|
+
}
|
package/tsconfig.json
ADDED