@toa.io/extensions.realtime 1.0.0-alpha.21 → 1.0.0-alpha.212
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/components/streams/manifest.toa.yaml +12 -1
- package/components/streams/operations/create.d.ts +6 -2
- package/components/streams/operations/create.js +47 -14
- package/components/streams/operations/create.js.map +1 -1
- package/components/streams/operations/lib/Stash.d.ts +20 -0
- package/components/streams/operations/lib/Stash.js +71 -0
- package/components/streams/operations/lib/Stash.js.map +1 -0
- package/components/streams/operations/lib/Stream.d.ts +11 -0
- package/components/streams/operations/lib/Stream.js +34 -0
- package/components/streams/operations/lib/Stream.js.map +1 -0
- package/components/streams/operations/lib/index.d.ts +2 -0
- package/components/streams/operations/lib/index.js +8 -0
- package/components/streams/operations/lib/index.js.map +1 -0
- package/components/streams/operations/lib/types.d.ts +22 -0
- package/components/streams/operations/lib/types.js.map +1 -0
- package/components/streams/operations/push.d.ts +1 -1
- package/components/streams/operations/push.js +6 -0
- package/components/streams/operations/push.js.map +1 -1
- package/components/streams/operations/tsconfig.tsbuildinfo +1 -1
- package/components/streams/source/create.ts +62 -12
- package/components/streams/source/lib/Stash.ts +105 -0
- package/components/streams/source/lib/Stream.ts +40 -0
- package/components/streams/source/lib/index.ts +2 -0
- package/components/streams/source/lib/types.ts +24 -0
- package/components/streams/source/push.ts +8 -1
- package/features/static.feature +89 -0
- package/features/steps/Realtime.ts +2 -2
- package/features/steps/Streams.ts +44 -9
- package/features/steps/components/messages/manifest.toa.yaml +2 -0
- package/package.json +6 -2
- package/readme.md +32 -6
- package/source/Composition.ts +1 -7
- package/source/Realtime.ts +4 -2
- package/source/Receiver.ts +49 -0
- package/source/Routes.ts +7 -27
- package/source/extension.ts +69 -0
- package/source/index.ts +1 -0
- package/transpiled/Composition.d.ts +1 -1
- package/transpiled/Composition.js +2 -5
- package/transpiled/Composition.js.map +1 -1
- package/transpiled/Realtime.js +5 -3
- package/transpiled/Realtime.js.map +1 -1
- package/transpiled/Receiver.d.ts +11 -0
- package/transpiled/Receiver.js +41 -0
- package/transpiled/Receiver.js.map +1 -0
- package/transpiled/Routes.d.ts +1 -2
- package/transpiled/Routes.js +5 -17
- package/transpiled/Routes.js.map +1 -1
- package/transpiled/extension.d.ts +8 -0
- package/transpiled/extension.js +46 -0
- package/transpiled/extension.js.map +1 -0
- package/transpiled/index.d.ts +1 -0
- package/transpiled/index.js +15 -0
- package/transpiled/index.js.map +1 -1
- package/transpiled/tsconfig.tsbuildinfo +1 -1
- package/components/streams/operations/lib/stream.d.ts +0 -10
- package/components/streams/operations/lib/stream.js +0 -31
- package/components/streams/operations/lib/stream.js.map +0 -1
- package/components/streams/operations/types.d.ts +0 -11
- package/components/streams/operations/types.js.map +0 -1
- package/components/streams/source/lib/stream.ts +0 -37
- package/components/streams/source/types.ts +0 -13
- package/stage/streams.test.ts +0 -128
- /package/components/streams/operations/{types.js → lib/types.js} +0 -0
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
prototype: null
|
|
2
1
|
namespace: realtime
|
|
3
2
|
name: streams
|
|
4
3
|
|
|
@@ -6,6 +5,7 @@ operations:
|
|
|
6
5
|
create:
|
|
7
6
|
input:
|
|
8
7
|
key: string
|
|
8
|
+
token: string
|
|
9
9
|
push:
|
|
10
10
|
bindings: ~ # only for internal use
|
|
11
11
|
input:
|
|
@@ -13,7 +13,18 @@ operations:
|
|
|
13
13
|
event*: string
|
|
14
14
|
data: ~
|
|
15
15
|
|
|
16
|
+
configuration:
|
|
17
|
+
schema:
|
|
18
|
+
properties:
|
|
19
|
+
maxlen:
|
|
20
|
+
type: number
|
|
21
|
+
default: 1000
|
|
22
|
+
expire:
|
|
23
|
+
type: number
|
|
24
|
+
default: 300
|
|
25
|
+
|
|
16
26
|
state: ~
|
|
27
|
+
stash: ~
|
|
17
28
|
|
|
18
29
|
exposition:
|
|
19
30
|
/:key:
|
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { type Readable } from 'node:stream';
|
|
3
3
|
import { type Operation } from '@toa.io/types';
|
|
4
|
-
import { type Context } from './types';
|
|
4
|
+
import { type Context } from './lib/types';
|
|
5
5
|
export declare class Effect implements Operation {
|
|
6
6
|
private readonly streams;
|
|
7
|
+
private stash;
|
|
8
|
+
private logs;
|
|
7
9
|
mount(context: Context): void;
|
|
8
|
-
|
|
10
|
+
unmount(): void;
|
|
11
|
+
execute(input: Input): Promise<Readable>;
|
|
9
12
|
private createStream;
|
|
10
13
|
}
|
|
11
14
|
interface Input {
|
|
12
15
|
key: string;
|
|
16
|
+
token?: string;
|
|
13
17
|
}
|
|
14
18
|
export {};
|
|
@@ -1,28 +1,61 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.Effect = void 0;
|
|
7
|
-
const
|
|
8
|
-
const stream_1 = require("./lib/stream");
|
|
4
|
+
const lib_1 = require("./lib");
|
|
9
5
|
class Effect {
|
|
10
6
|
streams = new Map();
|
|
7
|
+
stash;
|
|
8
|
+
logs;
|
|
11
9
|
mount(context) {
|
|
12
10
|
context.state.streams = this.streams;
|
|
11
|
+
context.state.stash = new lib_1.Stash(context.stash, context.configuration);
|
|
12
|
+
this.logs = context.logs;
|
|
13
|
+
this.stash = context.state.stash;
|
|
13
14
|
}
|
|
14
|
-
|
|
15
|
+
unmount() {
|
|
16
|
+
this.logs.info('Destroying streams', { count: this.streams.size });
|
|
17
|
+
for (const stream of this.streams.values())
|
|
18
|
+
stream.destroy();
|
|
19
|
+
}
|
|
20
|
+
async execute(input) {
|
|
15
21
|
const key = input.key;
|
|
16
|
-
if (!this.streams.has(key))
|
|
17
|
-
this.createStream(key);
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
22
|
+
if (!this.streams.has(key)) {
|
|
23
|
+
const stream = this.createStream(key);
|
|
24
|
+
this.streams.set(key, stream);
|
|
25
|
+
this.logs.debug('Stream created', { key });
|
|
26
|
+
}
|
|
27
|
+
// welcome
|
|
28
|
+
setTimeout(() => this.streams.get(key)?.heartbeat(), 1000);
|
|
29
|
+
if (input.token === undefined)
|
|
30
|
+
void this.stash.connect(key).then((token) => {
|
|
31
|
+
if (token instanceof Error)
|
|
32
|
+
this.logs.error('Failed to connect to stash', { key, error: token });
|
|
33
|
+
else
|
|
34
|
+
this.streams.get(key)?.push({ event: 'token', data: token });
|
|
35
|
+
});
|
|
36
|
+
else
|
|
37
|
+
void this.stash.pop(key, input.token).then((result) => {
|
|
38
|
+
if (result instanceof Error)
|
|
39
|
+
this.logs.error('Failed to pop from stash', { key, error: result });
|
|
40
|
+
else if (result !== null) {
|
|
41
|
+
const stream = this.streams.get(key);
|
|
42
|
+
if (stream === undefined)
|
|
43
|
+
return;
|
|
44
|
+
const [token, events] = result;
|
|
45
|
+
for (const event of events)
|
|
46
|
+
stream.push({ event: event.event, data: event.data });
|
|
47
|
+
stream.push({ event: 'token', data: token });
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
return this.streams.get(key);
|
|
21
51
|
}
|
|
22
52
|
createStream(key) {
|
|
23
|
-
const stream = new
|
|
24
|
-
|
|
25
|
-
|
|
53
|
+
const stream = new lib_1.Stream();
|
|
54
|
+
stream.events.once('destroy', () => {
|
|
55
|
+
this.logs.debug('Stream destroyed', { key });
|
|
56
|
+
this.streams.delete(key);
|
|
57
|
+
});
|
|
58
|
+
return stream;
|
|
26
59
|
}
|
|
27
60
|
}
|
|
28
61
|
exports.Effect = Effect;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create.js","sourceRoot":"","sources":["../source/create.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"create.js","sourceRoot":"","sources":["../source/create.ts"],"names":[],"mappings":";;;AAGA,+BAAqC;AAErC,MA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CAAA;QAEJ,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAE,CAAA;IAC/B,CAAC;IAEO,YAAY,CAAE,GAAW;QAC/B,MAAM,MAAM,GAAG,IAAI,YAAM,EAAE,CAAA;QAE3B,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,EAAE,GAAG,EAAE;YACjC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,kBAAkB,EAAE,EAAE,GAAG,EAAE,CAAC,CAAA;YAC5C,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;QAC1B,CAAC,CAAC,CAAA;QAEF,OAAO,MAAM,CAAA;IACf,CAAC;CACF;AAxED,wBAwEC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export declare class Stash {
|
|
2
|
+
private readonly stash;
|
|
3
|
+
private readonly configuration;
|
|
4
|
+
constructor(stash: any, configuration: Configuration);
|
|
5
|
+
connect(key: string): Promise<string | Error>;
|
|
6
|
+
push(key: string, event: string, data: unknown): Promise<string | Error>;
|
|
7
|
+
pop(key: string, token: string): Promise<[string, Event[]] | null | Error>;
|
|
8
|
+
private xadd;
|
|
9
|
+
private encode;
|
|
10
|
+
private decode;
|
|
11
|
+
}
|
|
12
|
+
interface Configuration {
|
|
13
|
+
maxlen: number;
|
|
14
|
+
expire: number;
|
|
15
|
+
}
|
|
16
|
+
interface Event {
|
|
17
|
+
event: string;
|
|
18
|
+
data: unknown;
|
|
19
|
+
}
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Stash = void 0;
|
|
4
|
+
class Stash {
|
|
5
|
+
stash;
|
|
6
|
+
configuration;
|
|
7
|
+
constructor(stash, configuration) {
|
|
8
|
+
this.stash = stash;
|
|
9
|
+
this.configuration = configuration;
|
|
10
|
+
}
|
|
11
|
+
async connect(key) {
|
|
12
|
+
return await this.xadd(key, 'connect');
|
|
13
|
+
}
|
|
14
|
+
async push(key, event, data) {
|
|
15
|
+
return await this.xadd(key, event, data);
|
|
16
|
+
}
|
|
17
|
+
async pop(key, token) {
|
|
18
|
+
const stamp = this.decode(token);
|
|
19
|
+
const results = await this.stash
|
|
20
|
+
.xread('STREAMS', key, stamp)
|
|
21
|
+
.catch((error) => error);
|
|
22
|
+
if (results === null)
|
|
23
|
+
return ERR_NO_RESULTS;
|
|
24
|
+
if (results instanceof Error)
|
|
25
|
+
return results;
|
|
26
|
+
if (results.length === 0)
|
|
27
|
+
return null;
|
|
28
|
+
const [, items] = results[0];
|
|
29
|
+
const events = [];
|
|
30
|
+
let lastStamp = null;
|
|
31
|
+
for (const item of items) {
|
|
32
|
+
const [, event, , json] = item[1];
|
|
33
|
+
lastStamp = item[0];
|
|
34
|
+
events.push({ event, data: JSON.parse(json) });
|
|
35
|
+
}
|
|
36
|
+
if (lastStamp === null)
|
|
37
|
+
return null;
|
|
38
|
+
return [this.encode(lastStamp), events];
|
|
39
|
+
}
|
|
40
|
+
async xadd(key, event, data) {
|
|
41
|
+
const args = ['MAXLEN', '~', this.configuration.maxlen, '*', 'type', event];
|
|
42
|
+
if (data !== undefined)
|
|
43
|
+
args.push('data', JSON.stringify(data));
|
|
44
|
+
const results = await this.stash
|
|
45
|
+
.multi()
|
|
46
|
+
.xadd(key, ...args)
|
|
47
|
+
.expire(key, this.configuration.expire)
|
|
48
|
+
.exec()
|
|
49
|
+
.catch((error) => error);
|
|
50
|
+
if (results === null)
|
|
51
|
+
return ERR_NO_RESULTS;
|
|
52
|
+
if (results instanceof Error)
|
|
53
|
+
return results;
|
|
54
|
+
const [[error, stamp]] = results;
|
|
55
|
+
if (error !== null)
|
|
56
|
+
return error;
|
|
57
|
+
return this.encode(stamp);
|
|
58
|
+
}
|
|
59
|
+
encode(token) {
|
|
60
|
+
return Buffer.from(token).toString('base64url');
|
|
61
|
+
}
|
|
62
|
+
decode(token) {
|
|
63
|
+
return Buffer.from(token, 'base64url').toString();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
exports.Stash = Stash;
|
|
67
|
+
class NoResultsError extends Error {
|
|
68
|
+
code = 'NO_RESULTS';
|
|
69
|
+
}
|
|
70
|
+
const ERR_NO_RESULTS = new NoResultsError();
|
|
71
|
+
//# sourceMappingURL=Stash.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Stash.js","sourceRoot":"","sources":["../../source/lib/Stash.ts"],"names":[],"mappings":";;;AAEA,MAAa,KAAK;IACC,KAAK,CAAO;IACZ,aAAa,CAAe;IAE7C,YAAoB,KAAU,EAAE,aAA4B;QAC1D,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;QAClB,IAAI,CAAC,aAAa,GAAG,aAAa,CAAA;IACpC,CAAC;IAEM,KAAK,CAAC,OAAO,CAAE,GAAW;QAC/B,OAAO,MAAM,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,SAAS,CAAC,CAAA;IACxC,CAAC;IAEM,KAAK,CAAC,IAAI,CAAE,GAAW,EAAE,KAAa,EAAE,IAAa;QAC1D,OAAO,MAAM,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,KAAK,EAAE,IAAI,CAAC,CAAA;IAC1C,CAAC;IAEM,KAAK,CAAC,GAAG,CAAE,GAAW,EAAE,KAAa;QAC1C,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;QAEhC,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,KAAK;aAC7B,KAAK,CAAC,SAAS,EAAE,GAAG,EAAE,KAAK,CAAC;aAC5B,KAAK,CAAC,CAAC,KAAY,EAAE,EAAE,CAAC,KAAK,CAAC,CAAA;QAEjC,IAAI,OAAO,KAAK,IAAI;YAClB,OAAO,cAAc,CAAA;QAEvB,IAAI,OAAO,YAAY,KAAK;YAC1B,OAAO,OAAO,CAAA;QAEhB,IAAI,OAAO,CAAC,MAA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;QAEjC,IAAI,OAAO,KAAK,IAAI;YAClB,OAAO,cAAc,CAAA;QAEvB,IAAI,OAAO,YAAY,KAAK;YAC1B,OAAO,OAAO,CAAA;QAEhB,MAAM,CAAC,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,GAAG,OAAO,CAAA;QAEhC,IAAI,KAAK,KAAK,IAAI;YAChB,OAAO,KAAK,CAAA;QAEd,OAAO,IAAI,CAAC,MAAM,CAAC,KAAe,CAAC,CAAA;IACrC,CAAC;IAEO,MAAM,CAAE,KAAa;QAC3B,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAA;IACjD,CAAC;IAEO,MAAM,CAAE,KAAa;QAC3B,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC,QAAQ,EAAE,CAAA;IACnD,CAAC;CACF;AAtFD,sBAsFC;AAYD,MAAM,cAAe,SAAQ,KAAK;IAChB,IAAI,GAAG,YAAY,CAAA;CACpC;AAED,MAAM,cAAc,GAAG,IAAI,cAAc,EAAE,CAAA"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import { EventEmitter, Readable } from 'node:stream';
|
|
4
|
+
export declare class Stream extends Readable {
|
|
5
|
+
events: EventEmitter<[never]>;
|
|
6
|
+
private interval;
|
|
7
|
+
constructor();
|
|
8
|
+
_read(): void;
|
|
9
|
+
_destroy(error: Error | null, callback: (error?: (Error | null)) => void): void;
|
|
10
|
+
heartbeat(stream?: Readable): boolean;
|
|
11
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Stream = void 0;
|
|
4
|
+
const node_stream_1 = require("node:stream");
|
|
5
|
+
class Stream extends node_stream_1.Readable {
|
|
6
|
+
events = new node_stream_1.EventEmitter();
|
|
7
|
+
interval = null;
|
|
8
|
+
constructor() {
|
|
9
|
+
super(objectMode);
|
|
10
|
+
}
|
|
11
|
+
// has to be here
|
|
12
|
+
_read() {
|
|
13
|
+
if (this.interval === null)
|
|
14
|
+
this.interval = setInterval(() => this.heartbeat(), HEARTBEAT_INTERVAL);
|
|
15
|
+
}
|
|
16
|
+
_destroy(error, callback) {
|
|
17
|
+
if (this.interval !== null)
|
|
18
|
+
clearInterval(this.interval);
|
|
19
|
+
this.events.emit('destroy');
|
|
20
|
+
super._destroy(error, callback);
|
|
21
|
+
}
|
|
22
|
+
heartbeat(stream = this) {
|
|
23
|
+
const resume = stream.push('heartbeat ' + Date.now());
|
|
24
|
+
if (!resume && this.interval !== null) {
|
|
25
|
+
clearInterval(this.interval);
|
|
26
|
+
this.interval = null;
|
|
27
|
+
}
|
|
28
|
+
return resume;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.Stream = Stream;
|
|
32
|
+
const HEARTBEAT_INTERVAL = 16_000; // why?
|
|
33
|
+
const objectMode = { objectMode: true };
|
|
34
|
+
//# sourceMappingURL=Stream.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Stream.js","sourceRoot":"","sources":["../../source/lib/Stream.ts"],"names":[],"mappings":";;;AAAA,6CAAoD;AAEpD,MAAa,MAAO,SAAQ,sBAAQ;IAC3B,MAAM,GAAG,IAAI,0BAAY,EAAE,CAAA;IAE1B,QAAQ,GAA0B,IAAI,CAAA;IAE9C;QACE,KAAK,CAAC,UAAU,CAAC,CAAA;IACnB,CAAC;IAED,iBAAiB;IACD,KAAK;QACnB,IAAI,IAAI,CAAC,QAAQ,KAAK,IAAI;YACxB,IAAI,CAAC,QAAQ,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,SAAS,EAAE,EAAE,kBAAkB,CAAC,CAAA;IAC3E,CAAC;IAEe,QAAQ,CAAE,KAAmB,EAAE,QAA0C;QACvF,IAAI,IAAI,CAAC,QAAQ,KAAK,IAAI;YACxB,aAAa,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;QAE9B,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA;QAE3B,KAAK,CAAC,QAAQ,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAA;IACjC,CAAC;IAEM,SAAS,CAAE,SAAmB,IAAI;QACvC,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC,CAAA;QAErD,IAAI,CAAC,MAAM,IAAI,IAAI,CAAC,QAAQ,KAAK,IAAI,EAAE,CAAC;YACtC,aAAa,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;YAC5B,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAA;QACtB,CAAC;QAED,OAAO,MAAM,CAAA;IACf,CAAC;CACF;AAlCD,wBAkCC;AAED,MAAM,kBAAkB,GAAG,MAAM,CAAA,CAAC,OAAO;AACzC,MAAM,UAAU,GAAG,EAAE,UAAU,EAAE,IAAI,EAAE,CAAA"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Stream = exports.Stash = void 0;
|
|
4
|
+
var Stash_1 = require("./Stash");
|
|
5
|
+
Object.defineProperty(exports, "Stash", { enumerable: true, get: function () { return Stash_1.Stash; } });
|
|
6
|
+
var Stream_1 = require("./Stream");
|
|
7
|
+
Object.defineProperty(exports, "Stream", { enumerable: true, get: function () { return Stream_1.Stream; } });
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/lib/index.ts"],"names":[],"mappings":";;;AAAA,iCAA+B;AAAtB,8FAAA,KAAK,OAAA;AACd,mCAAiC;AAAxB,gGAAA,MAAM,OAAA"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type Stream } from './Stream';
|
|
2
|
+
import { type Stash } from './Stash';
|
|
3
|
+
export interface Context {
|
|
4
|
+
stash: any;
|
|
5
|
+
state: {
|
|
6
|
+
streams: Map<string, Stream>;
|
|
7
|
+
stash: Stash;
|
|
8
|
+
};
|
|
9
|
+
logs: {
|
|
10
|
+
info: (m: string, att?: object) => void;
|
|
11
|
+
error: (m: string, att?: object) => void;
|
|
12
|
+
};
|
|
13
|
+
configuration: {
|
|
14
|
+
maxlen: number;
|
|
15
|
+
expire: number;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
export interface PushInput {
|
|
19
|
+
key: string;
|
|
20
|
+
event: string;
|
|
21
|
+
data: unknown;
|
|
22
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../source/lib/types.ts"],"names":[],"mappings":""}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { type Context, type PushInput } from './types';
|
|
1
|
+
import { type Context, type PushInput } from './lib/types';
|
|
2
2
|
export declare function effect({ key, event, data }: PushInput, context: Context): Promise<void>;
|
|
@@ -3,6 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.effect = void 0;
|
|
4
4
|
async function effect({ key, event, data }, context) {
|
|
5
5
|
context.state.streams.get(key)?.push({ event, data });
|
|
6
|
+
void context.state.stash.push(key, event, data).then((token) => {
|
|
7
|
+
if (token instanceof Error)
|
|
8
|
+
context.logs.error('Failed to push to stash', { key, error: token });
|
|
9
|
+
else
|
|
10
|
+
context.state.streams.get(key)?.push({ event: 'token', data: token });
|
|
11
|
+
});
|
|
6
12
|
}
|
|
7
13
|
exports.effect = effect;
|
|
8
14
|
//# sourceMappingURL=push.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"push.js","sourceRoot":"","sources":["../source/push.ts"],"names":[],"mappings":";;;AAEO,KAAK,UAAU,MAAM,CAAE,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,EAAa,EAAE,OAAgB;IAC7E,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAA;
|
|
1
|
+
{"version":3,"file":"push.js","sourceRoot":"","sources":["../source/push.ts"],"names":[],"mappings":";;;AAEO,KAAK,UAAU,MAAM,CAAE,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,EAAa,EAAE,OAAgB;IAC7E,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAA;IAErD,KAAK,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE;QAC7D,IAAI,KAAK,YAAY,KAAK;YACxB,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,yBAAyB,EAAE,EAAE,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAA;;YAEpE,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAA;IACzE,CAAC,CAAC,CAAA;AACJ,CAAC;AATD,wBASC"}
|