@skipruntime/core 0.0.2 → 0.0.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.
package/src/utils.ts CHANGED
@@ -1,48 +1,63 @@
1
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
- }
2
+ import { type NativeStub, sknative } from "@skiplang/std";
3
+ import type { Reducer, Json } from "@skipruntime/api";
4
+
5
+ /**
6
+ * `Reducer` to maintain the sum of input values.
7
+ *
8
+ * A `Reducer` that maintains the sum of values as they are added and removed from a collection.
9
+ */
10
+ export class Sum implements NativeStub, Reducer<number, number> {
11
+ [sknative] = "sum";
12
+
13
+ // Lie to TypeScript that this implements Reducer, but leave out any implementations
14
+ // since we provide a native implementation.
15
+ initial!: number;
16
+ add!: (accum: number) => number;
17
+ remove!: (accum: number) => Nullable<number>;
15
18
  }
16
19
 
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
- }
20
+ /**
21
+ * `Reducer` to maintain the minimum of input values.
22
+ *
23
+ * A `Reducer` that maintains the minimum of values as they are added and removed from a collection.
24
+ */
25
+ export class Min implements NativeStub, Reducer<number, number> {
26
+ [sknative] = "min";
27
+
28
+ // Lie to TypeScript that this implements Reducer, but leave out any implementations
29
+ // since we provide a native implementation.
30
+ initial!: number;
31
+ add!: (accum: number) => number;
32
+ remove!: (accum: number) => Nullable<number>;
27
33
  }
28
34
 
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
- }
35
+ /**
36
+ * `Reducer` to maintain the maximum of input values.
37
+ *
38
+ * A `Reducer` that maintains the maximum of values as they are added and removed from a collection.
39
+ */
40
+ export class Max implements NativeStub, Reducer<number, number> {
41
+ [sknative] = "max";
42
+
43
+ // Lie to TypeScript that this implements Reducer, but leave out any implementations
44
+ // since we provide a native implementation.
45
+ initial!: number;
46
+ add!: (accum: number) => number;
47
+ remove!: (accum: number) => Nullable<number>;
39
48
  }
40
49
 
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
- }
50
+ /**
51
+ * `Reducer` to maintain the count of input values.
52
+ *
53
+ * A `Reducer` that maintains the number of values as they are added and removed from a collection.
54
+ */
55
+ export class Count<T extends Json> implements Reducer<T, number>, NativeStub {
56
+ [sknative] = "count";
57
+
58
+ // Lie to TypeScript that this implements Reducer, but leave out any implementations
59
+ // since we provide a native implementation.
60
+ initial!: number;
61
+ add!: (accum: number) => number;
62
+ remove!: (accum: number) => Nullable<number>;
48
63
  }
package/dist/remote.d.ts DELETED
@@ -1,22 +0,0 @@
1
- import type { Entry, ExternalService, Json } from "@skipruntime/api";
2
- import type { Entrypoint } from "./index.js";
3
- export declare class SkipExternalService implements ExternalService {
4
- private readonly url;
5
- private readonly control_url;
6
- private readonly resources;
7
- constructor(url: string, control_url: string);
8
- static direct(entrypoint: Entrypoint): SkipExternalService;
9
- subscribe(resource: string, params: {
10
- [param: string]: string;
11
- }, callbacks: {
12
- update: (updates: Entry<Json, Json>[], isInitial: boolean) => void;
13
- error: (error: Json) => void;
14
- loading: () => void;
15
- }): void;
16
- unsubscribe(resource: string, params: {
17
- [param: string]: string;
18
- }): void;
19
- shutdown(): void;
20
- private toId;
21
- }
22
- //# sourceMappingURL=remote.d.ts.map
@@ -1 +0,0 @@
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,kBAAkB,CAAC;AAErE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAM7C,qBAAa,mBAAoB,YAAW,eAAe;IAIvD,OAAO,CAAC,QAAQ,CAAC,GAAG;IACpB,OAAO,CAAC,QAAQ,CAAC,WAAW;IAJ9B,OAAO,CAAC,QAAQ,CAAC,SAAS,CAA+B;gBAGtC,GAAG,EAAE,MAAM,EACX,WAAW,EAAE,MAAM;IAItC,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,UAAU,GAAG,mBAAmB;IAU1D,SAAS,CACP,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE;QAAE,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,EACnC,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;IAiCP,WAAW,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE;QAAE,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE;IAKjE,QAAQ,IAAI,IAAI;IAMhB,OAAO,CAAC,IAAI;CAQb"}
package/dist/remote.js DELETED
@@ -1,71 +0,0 @@
1
- // TODO: Remove once global `EventSource` makes it out of experimental
2
- // in nodejs LTS.
3
- import EventSource from "eventsource";
4
- export class SkipExternalService {
5
- constructor(url, control_url) {
6
- this.url = url;
7
- this.control_url = control_url;
8
- this.resources = new Map();
9
- }
10
- // TODO: Support Skip external services going through a gateway.
11
- static direct(entrypoint) {
12
- let url = `http://${entrypoint.host}:${entrypoint.streaming_port.toString()}`;
13
- let control_url = `http://${entrypoint.host}:${entrypoint.control_port.toString()}`;
14
- if (entrypoint.secured) {
15
- url = `https://${entrypoint.host}:${entrypoint.streaming_port.toString()}`;
16
- control_url = `https://${entrypoint.host}:${entrypoint.control_port.toString()}`;
17
- }
18
- return new SkipExternalService(url, control_url);
19
- }
20
- subscribe(resource, params, callbacks) {
21
- // TODO Manage Status
22
- fetch(`${this.control_url}/v1/streams`, {
23
- method: "POST",
24
- headers: {
25
- "Content-Type": "application/json",
26
- },
27
- body: JSON.stringify({
28
- resource,
29
- params,
30
- }),
31
- })
32
- .then((resp) => resp.text())
33
- .then((uuid) => {
34
- const evSource = new EventSource(`${this.url}/v1/streams/${uuid}`);
35
- evSource.addEventListener("init", (e) => {
36
- const updates = JSON.parse(e.data);
37
- callbacks.update(updates, true);
38
- });
39
- evSource.addEventListener("update", (e) => {
40
- const updates = JSON.parse(e.data);
41
- callbacks.update(updates, false);
42
- });
43
- evSource.onerror = (e) => {
44
- console.log(e);
45
- };
46
- this.resources.set(this.toId(resource, params), evSource);
47
- })
48
- .catch((e) => {
49
- console.log(e);
50
- });
51
- }
52
- unsubscribe(resource, params) {
53
- const closable = this.resources.get(this.toId(resource, params));
54
- if (closable)
55
- closable.close();
56
- }
57
- shutdown() {
58
- for (const res of this.resources.values()) {
59
- res.close();
60
- }
61
- }
62
- toId(resource, params) {
63
- // TODO: This is equivalent to `querystring.encode(params, ',', ':')`.
64
- const strparams = [];
65
- for (const key of Object.keys(params).sort()) {
66
- strparams.push(`${key}:${params[key]}`);
67
- }
68
- return `${resource}[${strparams.join(",")}]`;
69
- }
70
- }
71
- //# sourceMappingURL=remote.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"remote.js","sourceRoot":"","sources":["../src/remote.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,iBAAi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AAE,CAAC;YAC1C,GAAG,CAAC,KAAK,EAAE,CAAC;QACd,CAAC;IACH,CAAC;IAEO,IAAI,CAAC,QAAgB,EAAE,MAAmC;QAChE,sEAAsE;QACtE,MAAM,SAAS,GAAa,EAAE,CAAC;QAC/B,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC;YAC7C,SAAS,CAAC,IAAI,CAAC,GAAG,GAAG,IAAI,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QAC1C,CAAC;QACD,OAAO,GAAG,QAAQ,IAAI,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC;IAC/C,CAAC;CACF"}
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 "./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
- }