@rdfc/js-runner 1.0.0 → 2.0.0-alpha.10

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.
Files changed (89) hide show
  1. package/.husky/pre-commit +6 -0
  2. package/.idea/LNKD.tech Editor.xml +194 -0
  3. package/.idea/codeStyles/Project.xml +52 -0
  4. package/.idea/codeStyles/codeStyleConfig.xml +5 -0
  5. package/.idea/inspectionProfiles/Project_Default.xml +6 -0
  6. package/.idea/js-runner.iml +12 -0
  7. package/.idea/modules.xml +8 -0
  8. package/.idea/vcs.xml +6 -0
  9. package/.prettierrc +4 -0
  10. package/README.md +3 -38
  11. package/__tests__/channels.test.ts +96 -0
  12. package/bin/runner.js +8 -0
  13. package/dist/args.d.ts +3 -3
  14. package/dist/args.js +50 -51
  15. package/dist/connectors/file.d.ts +11 -11
  16. package/dist/connectors/file.js +79 -79
  17. package/dist/connectors/http.d.ts +10 -10
  18. package/dist/connectors/http.js +76 -76
  19. package/dist/connectors/kafka.d.ts +36 -36
  20. package/dist/connectors/kafka.js +66 -62
  21. package/dist/connectors/ws.d.ts +6 -6
  22. package/dist/connectors/ws.js +66 -63
  23. package/dist/connectors.d.ts +61 -42
  24. package/dist/connectors.js +155 -132
  25. package/dist/index.cjs +650 -595
  26. package/dist/index.d.ts +40 -31
  27. package/dist/index.js +72 -63
  28. package/dist/util.d.ts +63 -35
  29. package/dist/util.js +80 -63
  30. package/eslint.config.mjs +21 -0
  31. package/examples/echo/package-lock.json +80 -0
  32. package/examples/echo/package.json +18 -0
  33. package/examples/echo/pipeline.ttl +48 -0
  34. package/examples/echo/processors.ttl +82 -0
  35. package/examples/echo/src/processors.ts +74 -0
  36. package/examples/echo/tsconfig.json +114 -0
  37. package/index.ttl +71 -0
  38. package/jest.config.js +2 -0
  39. package/lib/client.d.ts +1 -0
  40. package/lib/client.js +43 -0
  41. package/lib/convertor.d.ts +9 -0
  42. package/lib/convertor.js +51 -0
  43. package/lib/index.d.ts +7 -0
  44. package/lib/index.js +8 -0
  45. package/lib/jsonld.d.ts +17 -0
  46. package/lib/jsonld.js +135 -0
  47. package/lib/logger.d.ts +17 -0
  48. package/lib/logger.js +49 -0
  49. package/lib/processor.d.ts +19 -0
  50. package/lib/processor.js +13 -0
  51. package/lib/reader.d.ts +30 -0
  52. package/lib/reader.js +101 -0
  53. package/lib/reexports.d.ts +3 -0
  54. package/lib/reexports.js +4 -0
  55. package/lib/runner.d.ts +26 -0
  56. package/lib/runner.js +121 -0
  57. package/lib/testUtils.d.ts +24 -0
  58. package/lib/testUtils.js +150 -0
  59. package/lib/tsconfig.tsbuildinfo +1 -0
  60. package/lib/util_processors.d.ts +11 -0
  61. package/lib/util_processors.js +13 -0
  62. package/lib/writer.d.ts +26 -0
  63. package/lib/writer.js +57 -0
  64. package/package.json +49 -51
  65. package/src/client.ts +52 -0
  66. package/src/convertor.ts +59 -0
  67. package/src/index.ts +8 -0
  68. package/src/jsonld.ts +220 -0
  69. package/src/logger.ts +64 -0
  70. package/src/processor.ts +39 -0
  71. package/src/reader.ts +142 -0
  72. package/src/reexports.ts +6 -0
  73. package/src/runner.ts +197 -0
  74. package/src/testUtils.ts +196 -0
  75. package/src/util_processors.ts +20 -0
  76. package/src/writer.ts +90 -0
  77. package/tsconfig.json +33 -0
  78. package/vite.config.ts +10 -0
  79. package/LICENSE +0 -21
  80. package/bin/js-runner.js +0 -4
  81. package/channels/file.ttl +0 -37
  82. package/channels/http.ttl +0 -59
  83. package/channels/kafka.ttl +0 -98
  84. package/channels/ws.ttl +0 -33
  85. package/ontology.ttl +0 -169
  86. package/processor/echo.ttl +0 -38
  87. package/processor/resc.ttl +0 -34
  88. package/processor/send.ttl +0 -40
  89. package/processor/test.js +0 -35
@@ -1,69 +1,72 @@
1
- import { SimpleStream, } from "../connectors.js";
2
- import { WebSocket } from "ws";
3
- import { WebSocketServer } from "ws";
1
+ import { SimpleStream } from '../connectors.js'
2
+ import { WebSocket } from 'ws'
3
+ import { WebSocketServer } from 'ws'
4
4
  function _connectWs(url, res) {
5
- const ws = new WebSocket(url, {});
6
- ws.on("error", () => {
7
- setTimeout(() => _connectWs(url, res), 300);
8
- });
9
- ws.on("ping", () => ws.pong());
10
- ws.on("open", () => {
11
- res(ws);
12
- });
5
+ const ws = new WebSocket(url, {})
6
+ ws.on('error', () => {
7
+ setTimeout(() => _connectWs(url, res), 300)
8
+ })
9
+ ws.on('ping', () => ws.pong())
10
+ ws.on('open', () => {
11
+ res(ws)
12
+ })
13
13
  }
14
14
  function connectWs(url) {
15
- return new Promise((res) => _connectWs(url, res));
15
+ return new Promise((res) => _connectWs(url, res))
16
16
  }
17
17
  export const startWsStreamReader = (config) => {
18
- const server = new WebSocketServer(config);
19
- server.on("error", (error) => {
20
- console.error("Ws server error:");
21
- console.error(error);
22
- });
23
- const connections = [];
24
- const interval = setInterval(() => {
25
- connections.forEach((instance, i) => {
26
- if (!instance) {
27
- return;
28
- }
29
- if (!instance.alive) {
30
- instance.socket.terminate();
31
- delete connections[i];
32
- return;
33
- }
34
- instance.socket.ping();
35
- instance.alive = false;
36
- });
37
- }, 30_000);
38
- const reader = new SimpleStream(() => new Promise((res) => {
39
- clearInterval(interval);
40
- server.close(() => res());
41
- }));
42
- server.on("connection", (ws) => {
43
- const instance = { socket: ws, alive: true };
44
- connections.push(instance);
45
- ws.on("message", async (msg) => {
46
- reader.push(msg.toString()).catch((error) => {
47
- throw error;
48
- });
49
- });
50
- ws.on("pong", () => {
51
- instance.alive = true;
52
- });
53
- });
54
- return { reader, init: async () => { } };
55
- };
18
+ const server = new WebSocketServer(config)
19
+ server.on('error', (error) => {
20
+ console.error('Ws server error:')
21
+ console.error(error)
22
+ })
23
+ const connections = []
24
+ const interval = setInterval(() => {
25
+ connections.forEach((instance, i) => {
26
+ if (!instance) {
27
+ return
28
+ }
29
+ if (!instance.alive) {
30
+ instance.socket.terminate()
31
+ delete connections[i]
32
+ return
33
+ }
34
+ instance.socket.ping()
35
+ instance.alive = false
36
+ })
37
+ }, 30_000)
38
+ const reader = new SimpleStream(
39
+ () =>
40
+ new Promise((res) => {
41
+ clearInterval(interval)
42
+ server.close(() => res())
43
+ }),
44
+ )
45
+ server.on('connection', (ws) => {
46
+ const instance = { socket: ws, alive: true }
47
+ connections.push(instance)
48
+ ws.on('message', async (msg) => {
49
+ reader.push(msg.toString()).catch((error) => {
50
+ throw error
51
+ })
52
+ })
53
+ ws.on('pong', () => {
54
+ instance.alive = true
55
+ })
56
+ })
57
+ return { reader, init: async () => {} }
58
+ }
56
59
  export const startWsStreamWriter = (config) => {
57
- let ws;
58
- const init = async () => {
59
- ws = await connectWs(config.url);
60
- ws.on("open", () => console.log("open"));
61
- };
62
- const writer = new SimpleStream(async () => {
63
- ws.close();
64
- });
65
- writer.push = async (item) => {
66
- ws.send(item);
67
- };
68
- return { writer, init };
69
- };
60
+ let ws
61
+ const init = async () => {
62
+ ws = await connectWs(config.url)
63
+ ws.on('open', () => console.log('open'))
64
+ }
65
+ const writer = new SimpleStream(async () => {
66
+ ws.close()
67
+ })
68
+ writer.push = async (item) => {
69
+ ws.send(item)
70
+ }
71
+ return { writer, init }
72
+ }
@@ -1,54 +1,73 @@
1
- import { NamedNode, Term } from "@rdfjs/types";
2
- export * from "./connectors/file.js";
3
- export * from "./connectors/ws.js";
4
- export * from "./connectors/kafka.js";
5
- export * from "./connectors/http.js";
6
- export declare const Conn: import("@treecg/types").Namespace<("FileReaderChannel" | "FileWriterChannel" | "HttpReaderChannel" | "HttpWriterChannel" | "KafkaReaderChannel" | "KafkaWriterChannel" | "WsReaderChannel" | "WsWriterChannel" | "WriterChannel" | "ReaderChannel")[], NamedNode<string>, string>;
1
+ import { NamedNode, Term } from '@rdfjs/types'
2
+ export * from './connectors/file.js'
3
+ export * from './connectors/ws.js'
4
+ export * from './connectors/kafka.js'
5
+ export * from './connectors/http.js'
6
+ export declare const Conn: import('@treecg/types').Namespace<
7
+ (
8
+ | 'FileReaderChannel'
9
+ | 'FileWriterChannel'
10
+ | 'HttpReaderChannel'
11
+ | 'HttpWriterChannel'
12
+ | 'KafkaReaderChannel'
13
+ | 'KafkaWriterChannel'
14
+ | 'WsReaderChannel'
15
+ | 'WsWriterChannel'
16
+ | 'WriterChannel'
17
+ | 'ReaderChannel'
18
+ )[],
19
+ NamedNode<string>,
20
+ string
21
+ >
7
22
  export interface Config<T> {
8
- id: Term;
9
- ty: NamedNode;
10
- config: T;
23
+ id: Term
24
+ ty: NamedNode
25
+ config: T
11
26
  }
12
27
  export type ReaderConstructor<C> = (config: C) => {
13
- reader: Stream<string | Buffer>;
14
- init: () => Promise<void>;
15
- };
28
+ reader: Stream<string | Buffer>
29
+ init: () => Promise<void>
30
+ }
16
31
  export type WriterConstructor<C> = (config: C) => {
17
- writer: Writer<string | Buffer>;
18
- init: () => Promise<void>;
19
- };
20
- export declare const JsOntology: import("@treecg/types").Namespace<("JsProcess" | "JsChannel" | "JsReaderChannel" | "JsWriterChannel")[], NamedNode<string>, string>;
32
+ writer: Writer<string | Buffer>
33
+ init: () => Promise<void>
34
+ }
35
+ export declare const JsOntology: import('@treecg/types').Namespace<
36
+ ('JsProcess' | 'JsChannel' | 'JsReaderChannel' | 'JsWriterChannel')[],
37
+ NamedNode<string>,
38
+ string
39
+ >
21
40
  export declare class ChannelFactory {
22
- private inits;
23
- private jsChannelsNamedNodes;
24
- private jsChannelsBlankNodes;
25
- createReader(config: Config<any>): Stream<string | Buffer>;
26
- createWriter(config: Config<any>): Writer<string | Buffer>;
27
- init(): Promise<void>;
41
+ private inits
42
+ private jsChannelsNamedNodes
43
+ private jsChannelsBlankNodes
44
+ createReader(config: Config<any>): Stream<string | Buffer>
45
+ createWriter(config: Config<any>): Writer<string | Buffer>
46
+ init(): Promise<void>
28
47
  }
29
48
  export interface Writer<T> {
30
- push(item: T): Promise<void>;
31
- end(): Promise<void>;
32
- on(event: "end", listener: Handler<void>): this;
49
+ push(item: T): Promise<void>
50
+ end(): Promise<void>
51
+ on(event: 'end', listener: Handler<void>): this
33
52
  }
34
53
  export interface Stream<T> {
35
- lastElement?: T;
36
- end(): Promise<void>;
37
- data(listener: (t: T) => PromiseLike<void> | void): this;
38
- on(event: "data", listener: (t: T) => PromiseLike<void> | void): this;
39
- on(event: "end", listener: () => PromiseLike<void> | void): this;
54
+ lastElement?: T
55
+ end(): Promise<void>
56
+ data(listener: (t: T) => PromiseLike<void> | void): this
57
+ on(event: 'data', listener: (t: T) => PromiseLike<void> | void): this
58
+ on(event: 'end', listener: () => PromiseLike<void> | void): this
40
59
  }
41
- export type Handler<T> = (item: T) => Promise<void> | void;
60
+ export type Handler<T> = (item: T) => Promise<void> | void
42
61
  export declare class SimpleStream<T> implements Stream<T> {
43
- private readonly dataHandlers;
44
- private readonly endHandlers;
45
- readonly disconnect: () => Promise<void>;
46
- lastElement?: T | undefined;
47
- private ended;
48
- constructor(onDisconnect?: () => Promise<void>);
49
- data(listener: Handler<T>): this;
50
- push(data: T): Promise<void>;
51
- end(): Promise<void>;
52
- on(event: "data", listener: Handler<T>): this;
53
- on(event: "end", listener: Handler<void>): this;
62
+ private readonly dataHandlers
63
+ private readonly endHandlers
64
+ readonly disconnect: () => Promise<void>
65
+ lastElement?: T | undefined
66
+ private ended
67
+ constructor(onDisconnect?: () => Promise<void>)
68
+ data(listener: Handler<T>): this
69
+ push(data: T): Promise<void>
70
+ end(): Promise<void>
71
+ on(event: 'data', listener: Handler<T>): this
72
+ on(event: 'end', listener: Handler<void>): this
54
73
  }
@@ -1,145 +1,168 @@
1
- import { createTermNamespace } from "@treecg/types";
2
- import { startFileStreamReader, startFileStreamWriter, } from "./connectors/file.js";
3
- export * from "./connectors/file.js";
4
- import { startWsStreamReader, startWsStreamWriter, } from "./connectors/ws.js";
5
- export * from "./connectors/ws.js";
6
- import { startKafkaStreamReader, startKafkaStreamWriter, } from "./connectors/kafka.js";
7
- export * from "./connectors/kafka.js";
8
- import { startHttpStreamReader, startHttpStreamWriter, } from "./connectors/http.js";
9
- import { LOG } from "./util.js";
10
- export * from "./connectors/http.js";
11
- export const Conn = createTermNamespace("https://w3id.org/conn#", "FileReaderChannel", "FileWriterChannel", "HttpReaderChannel", "HttpWriterChannel", "KafkaReaderChannel", "KafkaWriterChannel", "WsReaderChannel", "WsWriterChannel", "WriterChannel", "ReaderChannel");
12
- export const JsOntology = createTermNamespace("https://w3id.org/conn/js#", "JsProcess", "JsChannel", "JsReaderChannel", "JsWriterChannel");
1
+ import { createTermNamespace } from '@treecg/types'
2
+ import {
3
+ startFileStreamReader,
4
+ startFileStreamWriter,
5
+ } from './connectors/file.js'
6
+ export * from './connectors/file.js'
7
+ import { startWsStreamReader, startWsStreamWriter } from './connectors/ws.js'
8
+ export * from './connectors/ws.js'
9
+ import {
10
+ startKafkaStreamReader,
11
+ startKafkaStreamWriter,
12
+ } from './connectors/kafka.js'
13
+ export * from './connectors/kafka.js'
14
+ import {
15
+ startHttpStreamReader,
16
+ startHttpStreamWriter,
17
+ } from './connectors/http.js'
18
+ import { LOG } from './util.js'
19
+ export * from './connectors/http.js'
20
+ export const Conn = createTermNamespace(
21
+ 'https://w3id.org/conn#',
22
+ 'FileReaderChannel',
23
+ 'FileWriterChannel',
24
+ 'HttpReaderChannel',
25
+ 'HttpWriterChannel',
26
+ 'KafkaReaderChannel',
27
+ 'KafkaWriterChannel',
28
+ 'WsReaderChannel',
29
+ 'WsWriterChannel',
30
+ 'WriterChannel',
31
+ 'ReaderChannel',
32
+ )
33
+ export const JsOntology = createTermNamespace(
34
+ 'https://w3id.org/conn/js#',
35
+ 'JsProcess',
36
+ 'JsChannel',
37
+ 'JsReaderChannel',
38
+ 'JsWriterChannel',
39
+ )
13
40
  export class ChannelFactory {
14
- inits = [];
15
- jsChannelsNamedNodes = {};
16
- jsChannelsBlankNodes = {};
17
- createReader(config) {
18
- LOG.channel("Creating reader %s: a %s", config.id.value, config.ty.value);
19
- if (config.ty.equals(Conn.FileReaderChannel)) {
20
- const { reader, init } = startFileStreamReader(config.config);
21
- this.inits.push(init);
22
- return reader;
23
- }
24
- if (config.ty.equals(Conn.WsReaderChannel)) {
25
- const { reader, init } = startWsStreamReader(config.config);
26
- this.inits.push(init);
27
- return reader;
28
- }
29
- if (config.ty.equals(Conn.KafkaReaderChannel)) {
30
- const { reader, init } = startKafkaStreamReader(config.config);
31
- this.inits.push(init);
32
- return reader;
33
- }
34
- if (config.ty.equals(Conn.HttpReaderChannel)) {
35
- const { reader, init } = startHttpStreamReader(config.config);
36
- this.inits.push(init);
37
- return reader;
38
- }
39
- if (config.ty.equals(JsOntology.JsReaderChannel)) {
40
- const c = config.config;
41
- if (c.channel) {
42
- const id = c.channel.id.value;
43
- if (c.channel.id.termType === "NamedNode") {
44
- if (!this.jsChannelsNamedNodes[id]) {
45
- this.jsChannelsNamedNodes[id] =
46
- new SimpleStream();
47
- }
48
- return this.jsChannelsNamedNodes[id];
49
- }
50
- if (c.channel.id.termType === "BlankNode") {
51
- if (!this.jsChannelsBlankNodes[id]) {
52
- this.jsChannelsBlankNodes[id] =
53
- new SimpleStream();
54
- }
55
- return this.jsChannelsBlankNodes[id];
56
- }
57
- throw "Should have found a thing";
58
- }
59
- }
60
- throw "Unknown reader channel " + config.ty.value;
41
+ inits = []
42
+ jsChannelsNamedNodes = {}
43
+ jsChannelsBlankNodes = {}
44
+ createReader(config) {
45
+ LOG.channel('Creating reader %s: a %s', config.id.value, config.ty.value)
46
+ if (config.ty.equals(Conn.FileReaderChannel)) {
47
+ const { reader, init } = startFileStreamReader(config.config)
48
+ this.inits.push(init)
49
+ return reader
61
50
  }
62
- createWriter(config) {
63
- LOG.channel("Creating writer %s: a %s", config.id.value, config.ty.value);
64
- if (config.ty.equals(Conn.FileWriterChannel)) {
65
- const { writer, init } = startFileStreamWriter(config.config);
66
- this.inits.push(init);
67
- return writer;
68
- }
69
- if (config.ty.equals(Conn.WsWriterChannel)) {
70
- const { writer, init } = startWsStreamWriter(config.config);
71
- this.inits.push(init);
72
- return writer;
73
- }
74
- if (config.ty.equals(Conn.KafkaWriterChannel)) {
75
- const { writer, init } = startKafkaStreamWriter(config.config);
76
- this.inits.push(init);
77
- return writer;
78
- }
79
- if (config.ty.equals(Conn.HttpWriterChannel)) {
80
- const { writer, init } = startHttpStreamWriter(config.config);
81
- this.inits.push(init);
82
- return writer;
51
+ if (config.ty.equals(Conn.WsReaderChannel)) {
52
+ const { reader, init } = startWsStreamReader(config.config)
53
+ this.inits.push(init)
54
+ return reader
55
+ }
56
+ if (config.ty.equals(Conn.KafkaReaderChannel)) {
57
+ const { reader, init } = startKafkaStreamReader(config.config)
58
+ this.inits.push(init)
59
+ return reader
60
+ }
61
+ if (config.ty.equals(Conn.HttpReaderChannel)) {
62
+ const { reader, init } = startHttpStreamReader(config.config)
63
+ this.inits.push(init)
64
+ return reader
65
+ }
66
+ if (config.ty.equals(JsOntology.JsReaderChannel)) {
67
+ const c = config.config
68
+ if (c.channel) {
69
+ const id = c.channel.id.value
70
+ if (c.channel.id.termType === 'NamedNode') {
71
+ if (!this.jsChannelsNamedNodes[id]) {
72
+ this.jsChannelsNamedNodes[id] = new SimpleStream()
73
+ }
74
+ return this.jsChannelsNamedNodes[id]
83
75
  }
84
- if (config.ty.equals(JsOntology.JsWriterChannel)) {
85
- const c = config.config;
86
- if (c.channel) {
87
- const id = c.channel.id.value;
88
- if (c.channel.id.termType === "NamedNode") {
89
- if (!this.jsChannelsNamedNodes[id]) {
90
- this.jsChannelsNamedNodes[id] =
91
- new SimpleStream();
92
- }
93
- return this.jsChannelsNamedNodes[id];
94
- }
95
- if (c.channel.id.termType === "BlankNode") {
96
- if (!this.jsChannelsBlankNodes[id]) {
97
- this.jsChannelsBlankNodes[id] =
98
- new SimpleStream();
99
- }
100
- return this.jsChannelsBlankNodes[id];
101
- }
102
- throw "Should have found a thing";
103
- }
76
+ if (c.channel.id.termType === 'BlankNode') {
77
+ if (!this.jsChannelsBlankNodes[id]) {
78
+ this.jsChannelsBlankNodes[id] = new SimpleStream()
79
+ }
80
+ return this.jsChannelsBlankNodes[id]
104
81
  }
105
- throw "Unknown writer channel " + config.ty.value;
82
+ throw 'Should have found a thing'
83
+ }
106
84
  }
107
- async init() {
108
- await Promise.all(this.inits.map((x) => x()));
85
+ throw 'Unknown reader channel ' + config.ty.value
86
+ }
87
+ createWriter(config) {
88
+ LOG.channel('Creating writer %s: a %s', config.id.value, config.ty.value)
89
+ if (config.ty.equals(Conn.FileWriterChannel)) {
90
+ const { writer, init } = startFileStreamWriter(config.config)
91
+ this.inits.push(init)
92
+ return writer
109
93
  }
110
- }
111
- export class SimpleStream {
112
- dataHandlers = [];
113
- endHandlers = [];
114
- disconnect;
115
- lastElement;
116
- ended = false;
117
- constructor(onDisconnect) {
118
- this.disconnect = onDisconnect || (async () => { });
94
+ if (config.ty.equals(Conn.WsWriterChannel)) {
95
+ const { writer, init } = startWsStreamWriter(config.config)
96
+ this.inits.push(init)
97
+ return writer
119
98
  }
120
- data(listener) {
121
- this.dataHandlers.push(listener);
122
- return this;
123
- }
124
- async push(data) {
125
- if (this.ended) {
126
- throw new Error("Trying to push to a stream that has ended!");
127
- }
128
- this.lastElement = data;
129
- await Promise.all(this.dataHandlers.map((handler) => handler(data)));
99
+ if (config.ty.equals(Conn.KafkaWriterChannel)) {
100
+ const { writer, init } = startKafkaStreamWriter(config.config)
101
+ this.inits.push(init)
102
+ return writer
130
103
  }
131
- async end() {
132
- await this.disconnect();
133
- await Promise.all(this.endHandlers.map((handler) => handler()));
134
- this.ended = true;
104
+ if (config.ty.equals(Conn.HttpWriterChannel)) {
105
+ const { writer, init } = startHttpStreamWriter(config.config)
106
+ this.inits.push(init)
107
+ return writer
135
108
  }
136
- on(event, listener) {
137
- if (event === "data") {
138
- this.dataHandlers.push(listener);
109
+ if (config.ty.equals(JsOntology.JsWriterChannel)) {
110
+ const c = config.config
111
+ if (c.channel) {
112
+ const id = c.channel.id.value
113
+ if (c.channel.id.termType === 'NamedNode') {
114
+ if (!this.jsChannelsNamedNodes[id]) {
115
+ this.jsChannelsNamedNodes[id] = new SimpleStream()
116
+ }
117
+ return this.jsChannelsNamedNodes[id]
139
118
  }
140
- if (event === "end") {
141
- this.endHandlers.push(listener);
119
+ if (c.channel.id.termType === 'BlankNode') {
120
+ if (!this.jsChannelsBlankNodes[id]) {
121
+ this.jsChannelsBlankNodes[id] = new SimpleStream()
122
+ }
123
+ return this.jsChannelsBlankNodes[id]
142
124
  }
143
- return this;
125
+ throw 'Should have found a thing'
126
+ }
127
+ }
128
+ throw 'Unknown writer channel ' + config.ty.value
129
+ }
130
+ async init() {
131
+ await Promise.all(this.inits.map((x) => x()))
132
+ }
133
+ }
134
+ export class SimpleStream {
135
+ dataHandlers = []
136
+ endHandlers = []
137
+ disconnect
138
+ lastElement
139
+ ended = false
140
+ constructor(onDisconnect) {
141
+ this.disconnect = onDisconnect || (async () => {})
142
+ }
143
+ data(listener) {
144
+ this.dataHandlers.push(listener)
145
+ return this
146
+ }
147
+ async push(data) {
148
+ if (this.ended) {
149
+ throw new Error('Trying to push to a stream that has ended!')
150
+ }
151
+ this.lastElement = data
152
+ await Promise.all(this.dataHandlers.map((handler) => handler(data)))
153
+ }
154
+ async end() {
155
+ await this.disconnect()
156
+ await Promise.all(this.endHandlers.map((handler) => handler()))
157
+ this.ended = true
158
+ }
159
+ on(event, listener) {
160
+ if (event === 'data') {
161
+ this.dataHandlers.push(listener)
162
+ }
163
+ if (event === 'end') {
164
+ this.endHandlers.push(listener)
144
165
  }
166
+ return this
167
+ }
145
168
  }