@skipruntime/core 0.0.3 → 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/dist/binding.d.ts +2 -0
- package/dist/binding.d.ts.map +1 -1
- package/dist/index.d.ts +1 -25
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +16 -10
- package/dist/index.js.map +1 -1
- package/dist/utils.d.ts +20 -27
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +10 -42
- package/dist/utils.js.map +1 -1
- package/package.json +1 -1
- package/src/binding.js +14 -0
- package/src/binding.ts +9 -0
- package/src/errors.js +26 -0
- package/src/index.js +742 -0
- package/src/index.ts +45 -53
- package/src/internal.js +2 -0
- package/src/utils.js +57 -0
- package/src/utils.ts +34 -59
- package/dist/remote.d.ts +0 -33
- package/dist/remote.d.ts.map +0 -1
- package/dist/remote.js +0 -88
- package/dist/remote.js.map +0 -1
- package/src/remote.ts +0 -109
package/src/index.ts
CHANGED
|
@@ -18,6 +18,9 @@ import {
|
|
|
18
18
|
SkManaged,
|
|
19
19
|
checkOrCloneParam,
|
|
20
20
|
} from "@skiplang/json";
|
|
21
|
+
|
|
22
|
+
import { sknative } from "@skiplang/std";
|
|
23
|
+
|
|
21
24
|
import type * as Internal from "./internal.js";
|
|
22
25
|
import {
|
|
23
26
|
NonUniqueValueException,
|
|
@@ -48,39 +51,11 @@ import {
|
|
|
48
51
|
} from "./binding.js";
|
|
49
52
|
|
|
50
53
|
export { UnknownCollectionError, sk_freeze, isSkManaged };
|
|
51
|
-
export {
|
|
52
|
-
export { Sum, Min, Max, Count, CountMapper } from "./utils.js";
|
|
54
|
+
export { Sum, Min, Max, Count } from "./utils.js";
|
|
53
55
|
|
|
54
56
|
export type JSONMapper = Mapper<Json, Json, Json, Json>;
|
|
55
57
|
export type JSONLazyCompute = LazyCompute<Json, Json>;
|
|
56
58
|
|
|
57
|
-
/**
|
|
58
|
-
* An entry point of a Skip reactive service.
|
|
59
|
-
*
|
|
60
|
-
* URLs for the service's control and streaming APIs can be constructed from an `Entrypoint`.
|
|
61
|
-
*/
|
|
62
|
-
export type Entrypoint = {
|
|
63
|
-
/**
|
|
64
|
-
* Hostname of the service.
|
|
65
|
-
*/
|
|
66
|
-
host: string;
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Port to use for the service's streaming interface.
|
|
70
|
-
*/
|
|
71
|
-
streaming_port: number;
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Port to use for the service's control interface.
|
|
75
|
-
*/
|
|
76
|
-
control_port: number;
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Flag that when set indicates that https should be used instead of http.
|
|
80
|
-
*/
|
|
81
|
-
secured?: boolean;
|
|
82
|
-
};
|
|
83
|
-
|
|
84
59
|
class Handles {
|
|
85
60
|
private nextID: number = 1;
|
|
86
61
|
private readonly objects: any[] = [];
|
|
@@ -275,16 +250,28 @@ class EagerCollectionImpl<K extends Json, V extends Json>
|
|
|
275
250
|
const skmapper = this.refs.binding.SkipRuntime_createMapper(
|
|
276
251
|
this.refs.handles.register(mapperObj),
|
|
277
252
|
);
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
this.
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
253
|
+
|
|
254
|
+
if (sknative in reducerObj && typeof reducerObj[sknative] == "string") {
|
|
255
|
+
return this.derive<K2, Accum>(
|
|
256
|
+
this.refs.binding.SkipRuntime_Collection__nativeMapReduce(
|
|
257
|
+
this.collection,
|
|
258
|
+
skmapper,
|
|
259
|
+
reducerObj[sknative],
|
|
260
|
+
),
|
|
261
|
+
);
|
|
262
|
+
} else {
|
|
263
|
+
const skreducer = this.refs.binding.SkipRuntime_createReducer(
|
|
264
|
+
this.refs.handles.register(reducerObj),
|
|
265
|
+
this.refs.skjson.exportJSON(reducerObj.initial),
|
|
266
|
+
);
|
|
267
|
+
return this.derive<K2, Accum>(
|
|
268
|
+
this.refs.binding.SkipRuntime_Collection__mapReduce(
|
|
269
|
+
this.collection,
|
|
270
|
+
skmapper,
|
|
271
|
+
skreducer,
|
|
272
|
+
),
|
|
273
|
+
);
|
|
274
|
+
}
|
|
288
275
|
};
|
|
289
276
|
}
|
|
290
277
|
|
|
@@ -298,16 +285,25 @@ class EagerCollectionImpl<K extends Json, V extends Json>
|
|
|
298
285
|
if (!reducerObj.constructor.name) {
|
|
299
286
|
throw new Error("Reducer classes must be defined at top-level.");
|
|
300
287
|
}
|
|
301
|
-
|
|
302
|
-
this.
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
288
|
+
if (sknative in reducerObj && typeof reducerObj[sknative] == "string") {
|
|
289
|
+
return this.derive<K, Accum>(
|
|
290
|
+
this.refs.binding.SkipRuntime_Collection__nativeReduce(
|
|
291
|
+
this.collection,
|
|
292
|
+
reducerObj[sknative],
|
|
293
|
+
),
|
|
294
|
+
);
|
|
295
|
+
} else {
|
|
296
|
+
const skreducer = this.refs.binding.SkipRuntime_createReducer(
|
|
297
|
+
this.refs.handles.register(reducerObj),
|
|
298
|
+
this.refs.skjson.exportJSON(reducerObj.initial),
|
|
299
|
+
);
|
|
300
|
+
return this.derive<K, Accum>(
|
|
301
|
+
this.refs.binding.SkipRuntime_Collection__reduce(
|
|
302
|
+
this.collection,
|
|
303
|
+
skreducer,
|
|
304
|
+
),
|
|
305
|
+
);
|
|
306
|
+
}
|
|
311
307
|
}
|
|
312
308
|
|
|
313
309
|
merge(...others: EagerCollection<K, V>[]): EagerCollection<K, V> {
|
|
@@ -734,10 +730,6 @@ class ValuesImpl<T> implements Values<T> {
|
|
|
734
730
|
},
|
|
735
731
|
};
|
|
736
732
|
}
|
|
737
|
-
|
|
738
|
-
map<U>(f: (value: T & DepSafe, index: number) => U, thisObj?: any): U[] {
|
|
739
|
-
return this.toArray().map(f, thisObj);
|
|
740
|
-
}
|
|
741
733
|
}
|
|
742
734
|
|
|
743
735
|
export class ToBinding {
|
package/src/internal.js
ADDED
package/src/utils.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var _a, _b, _c, _d;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.Count = exports.Max = exports.Min = exports.Sum = void 0;
|
|
5
|
+
var std_1 = require("@skiplang/std");
|
|
6
|
+
/**
|
|
7
|
+
* `Reducer` to maintain the sum of input values.
|
|
8
|
+
*
|
|
9
|
+
* A `Reducer` that maintains the sum of values as they are added and removed from a collection.
|
|
10
|
+
*/
|
|
11
|
+
var Sum = /** @class */ (function () {
|
|
12
|
+
function Sum() {
|
|
13
|
+
this[_a] = "sum";
|
|
14
|
+
}
|
|
15
|
+
return Sum;
|
|
16
|
+
}());
|
|
17
|
+
exports.Sum = Sum;
|
|
18
|
+
_a = std_1.sknative;
|
|
19
|
+
/**
|
|
20
|
+
* `Reducer` to maintain the minimum of input values.
|
|
21
|
+
*
|
|
22
|
+
* A `Reducer` that maintains the minimum of values as they are added and removed from a collection.
|
|
23
|
+
*/
|
|
24
|
+
var Min = /** @class */ (function () {
|
|
25
|
+
function Min() {
|
|
26
|
+
this[_b] = "min";
|
|
27
|
+
}
|
|
28
|
+
return Min;
|
|
29
|
+
}());
|
|
30
|
+
exports.Min = Min;
|
|
31
|
+
_b = std_1.sknative;
|
|
32
|
+
/**
|
|
33
|
+
* `Reducer` to maintain the maximum of input values.
|
|
34
|
+
*
|
|
35
|
+
* A `Reducer` that maintains the maximum of values as they are added and removed from a collection.
|
|
36
|
+
*/
|
|
37
|
+
var Max = /** @class */ (function () {
|
|
38
|
+
function Max() {
|
|
39
|
+
this[_c] = "max";
|
|
40
|
+
}
|
|
41
|
+
return Max;
|
|
42
|
+
}());
|
|
43
|
+
exports.Max = Max;
|
|
44
|
+
_c = std_1.sknative;
|
|
45
|
+
/**
|
|
46
|
+
* `Reducer` to maintain the count of input values.
|
|
47
|
+
*
|
|
48
|
+
* A `Reducer` that maintains the number of values as they are added and removed from a collection.
|
|
49
|
+
*/
|
|
50
|
+
var Count = /** @class */ (function () {
|
|
51
|
+
function Count() {
|
|
52
|
+
this[_d] = "count";
|
|
53
|
+
}
|
|
54
|
+
return Count;
|
|
55
|
+
}());
|
|
56
|
+
exports.Count = Count;
|
|
57
|
+
_d = std_1.sknative;
|
package/src/utils.ts
CHANGED
|
@@ -1,22 +1,20 @@
|
|
|
1
1
|
import type { Nullable } from "@skip-wasm/std";
|
|
2
|
-
import {
|
|
3
|
-
import type { Reducer,
|
|
2
|
+
import { type NativeStub, sknative } from "@skiplang/std";
|
|
3
|
+
import type { Reducer, Json } from "@skipruntime/api";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* `Reducer` to maintain the sum of input values.
|
|
7
7
|
*
|
|
8
8
|
* A `Reducer` that maintains the sum of values as they are added and removed from a collection.
|
|
9
9
|
*/
|
|
10
|
-
export class Sum implements Reducer<number, number> {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
remove(accum: number
|
|
18
|
-
return accum - value;
|
|
19
|
-
}
|
|
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>;
|
|
20
18
|
}
|
|
21
19
|
|
|
22
20
|
/**
|
|
@@ -24,16 +22,14 @@ export class Sum implements Reducer<number, number> {
|
|
|
24
22
|
*
|
|
25
23
|
* A `Reducer` that maintains the minimum of values as they are added and removed from a collection.
|
|
26
24
|
*/
|
|
27
|
-
export class Min implements Reducer<number, number> {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
remove(accum: number
|
|
35
|
-
return value > accum ? accum : null;
|
|
36
|
-
}
|
|
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>;
|
|
37
33
|
}
|
|
38
34
|
|
|
39
35
|
/**
|
|
@@ -41,16 +37,14 @@ export class Min implements Reducer<number, number> {
|
|
|
41
37
|
*
|
|
42
38
|
* A `Reducer` that maintains the maximum of values as they are added and removed from a collection.
|
|
43
39
|
*/
|
|
44
|
-
export class Max implements Reducer<number, number> {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
remove(accum: number
|
|
52
|
-
return value < accum ? accum : null;
|
|
53
|
-
}
|
|
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>;
|
|
54
48
|
}
|
|
55
49
|
|
|
56
50
|
/**
|
|
@@ -58,31 +52,12 @@ export class Max implements Reducer<number, number> {
|
|
|
58
52
|
*
|
|
59
53
|
* A `Reducer` that maintains the number of values as they are added and removed from a collection.
|
|
60
54
|
*/
|
|
61
|
-
export class Count<T extends Json> implements Reducer<T, number
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
remove(accum: number)
|
|
69
|
-
return accum - 1;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* `Mapper` to count input values.
|
|
75
|
-
*
|
|
76
|
-
* A `Mapper` that associates each key in the output with the number of values it is associated with in the input.
|
|
77
|
-
*
|
|
78
|
-
* @remarks
|
|
79
|
-
* If there are many values associated to keys in a collection, and they are updated frequently, using the `Count` `Reducer` instead could be more efficient.
|
|
80
|
-
*/
|
|
81
|
-
export class CountMapper<
|
|
82
|
-
K extends Json,
|
|
83
|
-
V extends Json,
|
|
84
|
-
> extends ManyToOneMapper<K, V, number> {
|
|
85
|
-
mapValues(values: Values<V>): number {
|
|
86
|
-
return values.toArray().length;
|
|
87
|
-
}
|
|
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>;
|
|
88
63
|
}
|
package/dist/remote.d.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import type { Entry, ExternalService, Json } from "@skipruntime/api";
|
|
2
|
-
import type { Entrypoint } from "./index.js";
|
|
3
|
-
/**
|
|
4
|
-
* An external Skip reactive service.
|
|
5
|
-
*
|
|
6
|
-
* `SkipExternalService` provides an implementation of `ExternalService` for an external Skip service.
|
|
7
|
-
*/
|
|
8
|
-
export declare class SkipExternalService implements ExternalService {
|
|
9
|
-
private readonly url;
|
|
10
|
-
private readonly control_url;
|
|
11
|
-
private readonly resources;
|
|
12
|
-
/**
|
|
13
|
-
* @param url - URL to use for the service's streaming interface.
|
|
14
|
-
* @param control_url - URL to use for the service's control interface.
|
|
15
|
-
*/
|
|
16
|
-
constructor(url: string, control_url: string);
|
|
17
|
-
/**
|
|
18
|
-
* Constructor accepting an `Entrypoint`.
|
|
19
|
-
*
|
|
20
|
-
* @param entrypoint - The entry point for the external Skip service.
|
|
21
|
-
* @returns An `ExternalService` to interact with the service running at `entrypoint`.
|
|
22
|
-
*/
|
|
23
|
-
static direct(entrypoint: Entrypoint): SkipExternalService;
|
|
24
|
-
subscribe(resource: string, params: Json, callbacks: {
|
|
25
|
-
update: (updates: Entry<Json, Json>[], isInitial: boolean) => void;
|
|
26
|
-
error: (error: Json) => void;
|
|
27
|
-
loading: () => void;
|
|
28
|
-
}): void;
|
|
29
|
-
unsubscribe(resource: string, params: Json): void;
|
|
30
|
-
shutdown(): void;
|
|
31
|
-
private toId;
|
|
32
|
-
}
|
|
33
|
-
//# sourceMappingURL=remote.d.ts.map
|
package/dist/remote.d.ts.map
DELETED
|
@@ -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;;;;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,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;IAiCP,WAAW,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI;IAK1C,QAAQ,IAAI,IAAI;IAMhB,OAAO,CAAC,IAAI;CAQb"}
|
package/dist/remote.js
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
// TODO: Remove once global `EventSource` makes it out of experimental
|
|
2
|
-
// in nodejs LTS.
|
|
3
|
-
import EventSource from "eventsource";
|
|
4
|
-
/**
|
|
5
|
-
* An external Skip reactive service.
|
|
6
|
-
*
|
|
7
|
-
* `SkipExternalService` provides an implementation of `ExternalService` for an external Skip service.
|
|
8
|
-
*/
|
|
9
|
-
export class SkipExternalService {
|
|
10
|
-
/**
|
|
11
|
-
* @param url - URL to use for the service's streaming interface.
|
|
12
|
-
* @param control_url - URL to use for the service's control interface.
|
|
13
|
-
*/
|
|
14
|
-
constructor(url, control_url) {
|
|
15
|
-
this.url = url;
|
|
16
|
-
this.control_url = control_url;
|
|
17
|
-
this.resources = new Map();
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Constructor accepting an `Entrypoint`.
|
|
21
|
-
*
|
|
22
|
-
* @param entrypoint - The entry point for the external Skip service.
|
|
23
|
-
* @returns An `ExternalService` to interact with the service running at `entrypoint`.
|
|
24
|
-
*/
|
|
25
|
-
// TODO: Support Skip external services going through a gateway.
|
|
26
|
-
static direct(entrypoint) {
|
|
27
|
-
let url = `http://${entrypoint.host}:${entrypoint.streaming_port.toString()}`;
|
|
28
|
-
let control_url = `http://${entrypoint.host}:${entrypoint.control_port.toString()}`;
|
|
29
|
-
if (entrypoint.secured) {
|
|
30
|
-
url = `https://${entrypoint.host}:${entrypoint.streaming_port.toString()}`;
|
|
31
|
-
control_url = `https://${entrypoint.host}:${entrypoint.control_port.toString()}`;
|
|
32
|
-
}
|
|
33
|
-
return new SkipExternalService(url, control_url);
|
|
34
|
-
}
|
|
35
|
-
subscribe(resource, params, callbacks) {
|
|
36
|
-
// TODO Manage Status
|
|
37
|
-
fetch(`${this.control_url}/v1/streams`, {
|
|
38
|
-
method: "POST",
|
|
39
|
-
headers: {
|
|
40
|
-
"Content-Type": "application/json",
|
|
41
|
-
},
|
|
42
|
-
body: JSON.stringify({
|
|
43
|
-
resource,
|
|
44
|
-
params,
|
|
45
|
-
}),
|
|
46
|
-
})
|
|
47
|
-
.then((resp) => resp.text())
|
|
48
|
-
.then((uuid) => {
|
|
49
|
-
const evSource = new EventSource(`${this.url}/v1/streams/${uuid}`);
|
|
50
|
-
evSource.addEventListener("init", (e) => {
|
|
51
|
-
const updates = JSON.parse(e.data);
|
|
52
|
-
callbacks.update(updates, true);
|
|
53
|
-
});
|
|
54
|
-
evSource.addEventListener("update", (e) => {
|
|
55
|
-
const updates = JSON.parse(e.data);
|
|
56
|
-
callbacks.update(updates, false);
|
|
57
|
-
});
|
|
58
|
-
evSource.onerror = (e) => {
|
|
59
|
-
console.log(e);
|
|
60
|
-
};
|
|
61
|
-
this.resources.set(this.toId(resource, params), evSource);
|
|
62
|
-
})
|
|
63
|
-
.catch((e) => {
|
|
64
|
-
console.log(e);
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
unsubscribe(resource, params) {
|
|
68
|
-
const closable = this.resources.get(this.toId(resource, params));
|
|
69
|
-
if (closable)
|
|
70
|
-
closable.close();
|
|
71
|
-
}
|
|
72
|
-
shutdown() {
|
|
73
|
-
for (const res of this.resources.values()) {
|
|
74
|
-
res.close();
|
|
75
|
-
}
|
|
76
|
-
}
|
|
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
|
-
}
|
|
88
|
-
//# sourceMappingURL=remote.js.map
|
package/dist/remote.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"remote.js","sourceRoot":"","sources":["../src/remote.ts"],"names":[],"mappings":"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"}
|
package/src/remote.ts
DELETED
|
@@ -1,109 +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
|
-
/**
|
|
14
|
-
* An external Skip reactive service.
|
|
15
|
-
*
|
|
16
|
-
* `SkipExternalService` provides an implementation of `ExternalService` for an external Skip service.
|
|
17
|
-
*/
|
|
18
|
-
export class SkipExternalService implements ExternalService {
|
|
19
|
-
private readonly resources = new Map<string, Closable>();
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* @param url - URL to use for the service's streaming interface.
|
|
23
|
-
* @param control_url - URL to use for the service's control interface.
|
|
24
|
-
*/
|
|
25
|
-
constructor(
|
|
26
|
-
private readonly url: string,
|
|
27
|
-
private readonly control_url: string,
|
|
28
|
-
) {}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Constructor accepting an `Entrypoint`.
|
|
32
|
-
*
|
|
33
|
-
* @param entrypoint - The entry point for the external Skip service.
|
|
34
|
-
* @returns An `ExternalService` to interact with the service running at `entrypoint`.
|
|
35
|
-
*/
|
|
36
|
-
// TODO: Support Skip external services going through a gateway.
|
|
37
|
-
static direct(entrypoint: Entrypoint): SkipExternalService {
|
|
38
|
-
let url = `http://${entrypoint.host}:${entrypoint.streaming_port.toString()}`;
|
|
39
|
-
let control_url = `http://${entrypoint.host}:${entrypoint.control_port.toString()}`;
|
|
40
|
-
if (entrypoint.secured) {
|
|
41
|
-
url = `https://${entrypoint.host}:${entrypoint.streaming_port.toString()}`;
|
|
42
|
-
control_url = `https://${entrypoint.host}:${entrypoint.control_port.toString()}`;
|
|
43
|
-
}
|
|
44
|
-
return new SkipExternalService(url, control_url);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
subscribe(
|
|
48
|
-
resource: string,
|
|
49
|
-
params: Json,
|
|
50
|
-
callbacks: {
|
|
51
|
-
update: (updates: Entry<Json, Json>[], isInitial: boolean) => void;
|
|
52
|
-
// FIXME: What is `error()` used for?
|
|
53
|
-
error: (error: Json) => void;
|
|
54
|
-
// FIXME: What is `loading()` used for?
|
|
55
|
-
loading: () => void;
|
|
56
|
-
},
|
|
57
|
-
): void {
|
|
58
|
-
// TODO Manage Status
|
|
59
|
-
fetch(`${this.control_url}/v1/streams`, {
|
|
60
|
-
method: "POST",
|
|
61
|
-
headers: {
|
|
62
|
-
"Content-Type": "application/json",
|
|
63
|
-
},
|
|
64
|
-
body: JSON.stringify({
|
|
65
|
-
resource,
|
|
66
|
-
params,
|
|
67
|
-
}),
|
|
68
|
-
})
|
|
69
|
-
.then((resp) => resp.text())
|
|
70
|
-
.then((uuid) => {
|
|
71
|
-
const evSource = new EventSource(`${this.url}/v1/streams/${uuid}`);
|
|
72
|
-
evSource.addEventListener("init", (e: MessageEvent<string>) => {
|
|
73
|
-
const updates = JSON.parse(e.data) as Entry<Json, Json>[];
|
|
74
|
-
callbacks.update(updates, true);
|
|
75
|
-
});
|
|
76
|
-
evSource.addEventListener("update", (e: MessageEvent<string>) => {
|
|
77
|
-
const updates = JSON.parse(e.data) as Entry<Json, Json>[];
|
|
78
|
-
callbacks.update(updates, false);
|
|
79
|
-
});
|
|
80
|
-
evSource.onerror = (e) => {
|
|
81
|
-
console.log(e);
|
|
82
|
-
};
|
|
83
|
-
this.resources.set(this.toId(resource, params), evSource);
|
|
84
|
-
})
|
|
85
|
-
.catch((e: unknown) => {
|
|
86
|
-
console.log(e);
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
unsubscribe(resource: string, params: Json) {
|
|
91
|
-
const closable = this.resources.get(this.toId(resource, params));
|
|
92
|
-
if (closable) closable.close();
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
shutdown(): void {
|
|
96
|
-
for (const res of this.resources.values()) {
|
|
97
|
-
res.close();
|
|
98
|
-
}
|
|
99
|
-
}
|
|
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
|
-
}
|