@rdfc/js-runner 1.0.0 → 2.0.0-alpha.2
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/.husky/pre-commit +6 -0
- package/.prettierrc +4 -0
- package/README.md +3 -38
- package/__tests__/channels.test.ts +133 -0
- package/bin/runner.js +8 -0
- package/eslint.config.mjs +21 -0
- package/examples/echo/package-lock.json +80 -0
- package/examples/echo/package.json +18 -0
- package/examples/echo/pipeline.ttl +30 -0
- package/examples/echo/processors.ttl +84 -0
- package/examples/echo/src/processors.ts +75 -0
- package/examples/echo/test.ttl +14 -0
- package/examples/echo/tsconfig.json +114 -0
- package/examples/echo/untitled:/types/MyType.ttl +0 -0
- package/index.ttl +63 -0
- package/jest.config.js +2 -0
- package/lib/client.d.ts +1 -0
- package/lib/client.js +43 -0
- package/lib/convertor.d.ts +9 -0
- package/lib/convertor.js +51 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +5 -0
- package/lib/jsonld.d.ts +17 -0
- package/lib/jsonld.js +134 -0
- package/lib/logger.d.ts +15 -0
- package/lib/logger.js +27 -0
- package/lib/processor.d.ts +19 -0
- package/lib/processor.js +13 -0
- package/lib/reader.d.ts +29 -0
- package/lib/reader.js +110 -0
- package/lib/runner.d.ts +25 -0
- package/lib/runner.js +111 -0
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/lib/util_processors.d.ts +11 -0
- package/lib/util_processors.js +13 -0
- package/lib/writer.d.ts +25 -0
- package/lib/writer.js +57 -0
- package/package.json +49 -51
- package/src/client.ts +52 -0
- package/src/convertor.ts +59 -0
- package/src/index.ts +4 -0
- package/src/jsonld.ts +217 -0
- package/src/logger.ts +38 -0
- package/src/processor.ts +39 -0
- package/src/reader.ts +148 -0
- package/src/runner.ts +186 -0
- package/src/util_processors.ts +20 -0
- package/src/writer.ts +89 -0
- package/tsconfig.json +33 -0
- package/vite.config.ts +10 -0
- package/LICENSE +0 -21
- package/bin/js-runner.js +0 -4
- package/channels/file.ttl +0 -37
- package/channels/http.ttl +0 -59
- package/channels/kafka.ttl +0 -98
- package/channels/ws.ttl +0 -33
- package/dist/args.d.ts +0 -4
- package/dist/args.js +0 -59
- package/dist/connectors/file.d.ts +0 -15
- package/dist/connectors/file.js +0 -89
- package/dist/connectors/http.d.ts +0 -14
- package/dist/connectors/http.js +0 -82
- package/dist/connectors/kafka.d.ts +0 -48
- package/dist/connectors/kafka.js +0 -64
- package/dist/connectors/ws.d.ts +0 -10
- package/dist/connectors/ws.js +0 -69
- package/dist/connectors.d.ts +0 -54
- package/dist/connectors.js +0 -145
- package/dist/index.cjs +0 -677
- package/dist/index.d.ts +0 -33
- package/dist/index.js +0 -74
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/dist/util.d.ts +0 -43
- package/dist/util.js +0 -75
- package/ontology.ttl +0 -169
- package/processor/echo.ttl +0 -38
- package/processor/resc.ttl +0 -34
- package/processor/send.ttl +0 -40
- package/processor/test.js +0 -35
package/dist/connectors/file.js
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import { createReadStream, openSync } from "fs";
|
|
2
|
-
import { appendFile, readFile, stat, writeFile } from "fs/promises";
|
|
3
|
-
import { isAbsolute } from "path";
|
|
4
|
-
import { watch } from "node:fs";
|
|
5
|
-
import { SimpleStream, } from "../connectors.js";
|
|
6
|
-
async function getFileSize(path) {
|
|
7
|
-
return (await stat(path)).size;
|
|
8
|
-
}
|
|
9
|
-
function readPart(path, start, end, encoding) {
|
|
10
|
-
return new Promise((res) => {
|
|
11
|
-
const stream = createReadStream(path, { encoding, start, end });
|
|
12
|
-
let buffer = "";
|
|
13
|
-
stream.on("data", (chunk) => {
|
|
14
|
-
buffer += chunk;
|
|
15
|
-
});
|
|
16
|
-
stream.on("close", () => res(buffer));
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
function debounce(func, timeout = 100) {
|
|
20
|
-
let timer;
|
|
21
|
-
return (...args) => {
|
|
22
|
-
clearTimeout(timer);
|
|
23
|
-
timer = setTimeout(() => {
|
|
24
|
-
func(...args);
|
|
25
|
-
}, timeout);
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
export const startFileStreamReader = (config) => {
|
|
29
|
-
const path = isAbsolute(config.path)
|
|
30
|
-
? config.path
|
|
31
|
-
: `${process.cwd()}/${config.path}`;
|
|
32
|
-
openSync(path, "a+");
|
|
33
|
-
const encoding = config.encoding || "utf-8";
|
|
34
|
-
const reader = new SimpleStream();
|
|
35
|
-
const init = async () => {
|
|
36
|
-
let currentPos = await getFileSize(path);
|
|
37
|
-
const watcher = watch(path, { encoding: "utf-8" });
|
|
38
|
-
watcher.on("change", debounce(async () => {
|
|
39
|
-
try {
|
|
40
|
-
let content;
|
|
41
|
-
if (config.onReplace) {
|
|
42
|
-
content = await readFile(path, { encoding });
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
const newSize = await getFileSize(path);
|
|
46
|
-
if (newSize <= currentPos) {
|
|
47
|
-
currentPos = newSize;
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
content = await readPart(path, currentPos, newSize, encoding);
|
|
51
|
-
currentPos = newSize;
|
|
52
|
-
}
|
|
53
|
-
await reader.push(content);
|
|
54
|
-
}
|
|
55
|
-
catch (error) {
|
|
56
|
-
if (error.code === "ENOENT") {
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
throw error;
|
|
60
|
-
}
|
|
61
|
-
}));
|
|
62
|
-
if (config.onReplace && config.readFirstContent) {
|
|
63
|
-
const content = await readFile(path, { encoding });
|
|
64
|
-
await reader.push(content);
|
|
65
|
-
}
|
|
66
|
-
};
|
|
67
|
-
return { reader, init };
|
|
68
|
-
};
|
|
69
|
-
export const startFileStreamWriter = (config) => {
|
|
70
|
-
const path = isAbsolute(config.path)
|
|
71
|
-
? config.path
|
|
72
|
-
: `${process.cwd()}/${config.path}`;
|
|
73
|
-
const encoding = config.encoding || "utf-8";
|
|
74
|
-
const writer = new SimpleStream();
|
|
75
|
-
const init = async () => {
|
|
76
|
-
if (!config.onReplace) {
|
|
77
|
-
await writeFile(path, "", { encoding });
|
|
78
|
-
}
|
|
79
|
-
};
|
|
80
|
-
writer.push = async (item) => {
|
|
81
|
-
if (config.onReplace) {
|
|
82
|
-
await writeFile(path, item, { encoding });
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
await appendFile(path, item, { encoding });
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
return { writer, init };
|
|
89
|
-
};
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { ReaderConstructor, WriterConstructor } from "../connectors.js";
|
|
2
|
-
export interface HttpReaderConfig {
|
|
3
|
-
endpoint: string;
|
|
4
|
-
port: number;
|
|
5
|
-
binary: boolean;
|
|
6
|
-
waitHandled?: boolean;
|
|
7
|
-
responseCode?: number;
|
|
8
|
-
}
|
|
9
|
-
export declare const startHttpStreamReader: ReaderConstructor<HttpReaderConfig>;
|
|
10
|
-
export interface HttpWriterConfig {
|
|
11
|
-
endpoint: string;
|
|
12
|
-
method: string;
|
|
13
|
-
}
|
|
14
|
-
export declare const startHttpStreamWriter: WriterConstructor<HttpWriterConfig>;
|
package/dist/connectors/http.js
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import * as http from "http";
|
|
2
|
-
import { createServer } from "http";
|
|
3
|
-
import { SimpleStream, } from "../connectors.js";
|
|
4
|
-
function streamToString(stream, binary) {
|
|
5
|
-
const datas = [];
|
|
6
|
-
return new Promise((res) => {
|
|
7
|
-
stream.on("data", (data) => {
|
|
8
|
-
datas.push(data);
|
|
9
|
-
});
|
|
10
|
-
stream.on("end", () => {
|
|
11
|
-
const streamData = Buffer.concat(datas);
|
|
12
|
-
res(binary ? streamData : streamData.toString());
|
|
13
|
-
});
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
export const startHttpStreamReader = (config) => {
|
|
17
|
-
let server = undefined;
|
|
18
|
-
const stream = new SimpleStream(() => new Promise((res) => {
|
|
19
|
-
if (server !== undefined) {
|
|
20
|
-
server.close(() => {
|
|
21
|
-
res();
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
else {
|
|
25
|
-
res();
|
|
26
|
-
}
|
|
27
|
-
}));
|
|
28
|
-
const requestListener = async function (req, res) {
|
|
29
|
-
try {
|
|
30
|
-
const content = await streamToString(req, config.binary);
|
|
31
|
-
const promise = stream.push(content).catch((error) => {
|
|
32
|
-
throw error;
|
|
33
|
-
});
|
|
34
|
-
if (config.waitHandled) {
|
|
35
|
-
await promise;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
catch (error) {
|
|
39
|
-
console.error("Failed", error);
|
|
40
|
-
}
|
|
41
|
-
res.writeHead(config.responseCode || 200);
|
|
42
|
-
res.end("OK");
|
|
43
|
-
};
|
|
44
|
-
server = createServer(requestListener);
|
|
45
|
-
const init = () => {
|
|
46
|
-
return new Promise((res) => {
|
|
47
|
-
const cb = () => res(undefined);
|
|
48
|
-
if (server) {
|
|
49
|
-
server.listen(config.port, config.endpoint, cb);
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
cb();
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
};
|
|
56
|
-
return { reader: stream, init };
|
|
57
|
-
};
|
|
58
|
-
export const startHttpStreamWriter = (config) => {
|
|
59
|
-
const requestConfig = new URL(config.endpoint);
|
|
60
|
-
const writer = new SimpleStream();
|
|
61
|
-
writer.push = async (item) => {
|
|
62
|
-
await new Promise((resolve) => {
|
|
63
|
-
const options = {
|
|
64
|
-
hostname: requestConfig.hostname,
|
|
65
|
-
path: requestConfig.path,
|
|
66
|
-
method: config.method,
|
|
67
|
-
port: requestConfig.port,
|
|
68
|
-
};
|
|
69
|
-
const cb = (response) => {
|
|
70
|
-
response.on("data", () => { });
|
|
71
|
-
response.on("end", () => {
|
|
72
|
-
resolve(null);
|
|
73
|
-
});
|
|
74
|
-
};
|
|
75
|
-
const req = http.request(options, cb);
|
|
76
|
-
req.write(item, () => {
|
|
77
|
-
req.end();
|
|
78
|
-
});
|
|
79
|
-
});
|
|
80
|
-
};
|
|
81
|
-
return { writer, init: async () => { } };
|
|
82
|
-
};
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import type { ProducerConfig } from "kafkajs";
|
|
2
|
-
import { ReaderConstructor, WriterConstructor } from "../connectors.js";
|
|
3
|
-
export interface SASLOptions {
|
|
4
|
-
mechanism: "plain";
|
|
5
|
-
username: string;
|
|
6
|
-
password: string;
|
|
7
|
-
}
|
|
8
|
-
export interface BrokerConfig {
|
|
9
|
-
hosts: string[];
|
|
10
|
-
ssl?: boolean;
|
|
11
|
-
sasl?: SASLOptions;
|
|
12
|
-
}
|
|
13
|
-
export interface ConsumerConfig {
|
|
14
|
-
groupId: string;
|
|
15
|
-
metadataMaxAge?: number;
|
|
16
|
-
sessionTimeout?: number;
|
|
17
|
-
rebalanceTimeout?: number;
|
|
18
|
-
heartbeatInterval?: number;
|
|
19
|
-
maxBytesPerPartition?: number;
|
|
20
|
-
minBytes?: number;
|
|
21
|
-
maxBytes?: number;
|
|
22
|
-
maxWaitTimeInMs?: number;
|
|
23
|
-
allowAutoTopicCreation?: boolean;
|
|
24
|
-
maxInFlightRequests?: number;
|
|
25
|
-
readUncommitted?: boolean;
|
|
26
|
-
rackId?: string;
|
|
27
|
-
}
|
|
28
|
-
export interface CSTopic {
|
|
29
|
-
topic: string;
|
|
30
|
-
fromBeginning?: boolean;
|
|
31
|
-
}
|
|
32
|
-
export interface KafkaReaderConfig {
|
|
33
|
-
topic: {
|
|
34
|
-
name: string;
|
|
35
|
-
fromBeginning?: boolean;
|
|
36
|
-
};
|
|
37
|
-
consumer: ConsumerConfig;
|
|
38
|
-
broker: string | BrokerConfig;
|
|
39
|
-
}
|
|
40
|
-
export declare const startKafkaStreamReader: ReaderConstructor<KafkaReaderConfig>;
|
|
41
|
-
export interface KafkaWriterConfig {
|
|
42
|
-
topic: {
|
|
43
|
-
name: string;
|
|
44
|
-
};
|
|
45
|
-
producer: ProducerConfig;
|
|
46
|
-
broker: BrokerConfig | string;
|
|
47
|
-
}
|
|
48
|
-
export declare const startKafkaStreamWriter: WriterConstructor<KafkaWriterConfig>;
|
package/dist/connectors/kafka.js
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { readFileSync } from "node:fs";
|
|
2
|
-
import { Kafka } from "kafkajs";
|
|
3
|
-
import { SimpleStream, } from "../connectors.js";
|
|
4
|
-
export const startKafkaStreamReader = (config) => {
|
|
5
|
-
const brokerConfig = {};
|
|
6
|
-
if (typeof config.broker === "string" || config.broker instanceof String) {
|
|
7
|
-
Object.assign(brokerConfig, JSON.parse(readFileSync(config.broker, "utf-8")));
|
|
8
|
-
}
|
|
9
|
-
else {
|
|
10
|
-
Object.assign(brokerConfig, config.broker);
|
|
11
|
-
}
|
|
12
|
-
if (brokerConfig && brokerConfig.hosts) {
|
|
13
|
-
brokerConfig.brokers = (brokerConfig).hosts;
|
|
14
|
-
}
|
|
15
|
-
const kafka = new Kafka(brokerConfig);
|
|
16
|
-
const consumer = kafka.consumer(config.consumer);
|
|
17
|
-
const stream = new SimpleStream(async () => {
|
|
18
|
-
await consumer.disconnect();
|
|
19
|
-
await consumer.stop();
|
|
20
|
-
});
|
|
21
|
-
const init = async () => {
|
|
22
|
-
await consumer.connect();
|
|
23
|
-
await consumer.subscribe({
|
|
24
|
-
topic: config.topic.name,
|
|
25
|
-
fromBeginning: config.topic.fromBeginning,
|
|
26
|
-
});
|
|
27
|
-
consumer
|
|
28
|
-
.run({
|
|
29
|
-
async eachMessage({ topic, message, }) {
|
|
30
|
-
if (topic === config.topic.name) {
|
|
31
|
-
const element = message.value?.toString() ?? "";
|
|
32
|
-
await stream.push(element);
|
|
33
|
-
}
|
|
34
|
-
},
|
|
35
|
-
})
|
|
36
|
-
.catch((error) => {
|
|
37
|
-
throw error;
|
|
38
|
-
});
|
|
39
|
-
};
|
|
40
|
-
return { reader: stream, init };
|
|
41
|
-
};
|
|
42
|
-
export const startKafkaStreamWriter = (config) => {
|
|
43
|
-
const topic = config.topic.name;
|
|
44
|
-
const brokerConfig = {};
|
|
45
|
-
if (typeof config.broker === "string" || config.broker instanceof String) {
|
|
46
|
-
Object.assign(brokerConfig, JSON.parse(readFileSync(config.broker, "utf-8")));
|
|
47
|
-
}
|
|
48
|
-
else {
|
|
49
|
-
Object.assign(brokerConfig, config.broker);
|
|
50
|
-
}
|
|
51
|
-
if (brokerConfig && brokerConfig.hosts) {
|
|
52
|
-
brokerConfig.brokers = (brokerConfig).hosts;
|
|
53
|
-
}
|
|
54
|
-
const kafka = new Kafka(brokerConfig);
|
|
55
|
-
const producer = kafka.producer(config.producer);
|
|
56
|
-
const writer = new SimpleStream(async () => {
|
|
57
|
-
await producer.disconnect();
|
|
58
|
-
});
|
|
59
|
-
const init = () => producer.connect();
|
|
60
|
-
writer.push = async (item) => {
|
|
61
|
-
await producer.send({ topic, messages: [{ value: item }] });
|
|
62
|
-
};
|
|
63
|
-
return { writer, init };
|
|
64
|
-
};
|
package/dist/connectors/ws.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { ReaderConstructor, WriterConstructor } from "../connectors.js";
|
|
2
|
-
export interface WsWriterConfig {
|
|
3
|
-
url: string;
|
|
4
|
-
}
|
|
5
|
-
export interface WsReaderConfig {
|
|
6
|
-
host: string;
|
|
7
|
-
port: number;
|
|
8
|
-
}
|
|
9
|
-
export declare const startWsStreamReader: ReaderConstructor<WsReaderConfig>;
|
|
10
|
-
export declare const startWsStreamWriter: WriterConstructor<WsWriterConfig>;
|
package/dist/connectors/ws.js
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import { SimpleStream, } from "../connectors.js";
|
|
2
|
-
import { WebSocket } from "ws";
|
|
3
|
-
import { WebSocketServer } from "ws";
|
|
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
|
-
});
|
|
13
|
-
}
|
|
14
|
-
function connectWs(url) {
|
|
15
|
-
return new Promise((res) => _connectWs(url, res));
|
|
16
|
-
}
|
|
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
|
-
};
|
|
56
|
-
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
|
-
};
|
package/dist/connectors.d.ts
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
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>;
|
|
7
|
-
export interface Config<T> {
|
|
8
|
-
id: Term;
|
|
9
|
-
ty: NamedNode;
|
|
10
|
-
config: T;
|
|
11
|
-
}
|
|
12
|
-
export type ReaderConstructor<C> = (config: C) => {
|
|
13
|
-
reader: Stream<string | Buffer>;
|
|
14
|
-
init: () => Promise<void>;
|
|
15
|
-
};
|
|
16
|
-
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>;
|
|
21
|
-
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>;
|
|
28
|
-
}
|
|
29
|
-
export interface Writer<T> {
|
|
30
|
-
push(item: T): Promise<void>;
|
|
31
|
-
end(): Promise<void>;
|
|
32
|
-
on(event: "end", listener: Handler<void>): this;
|
|
33
|
-
}
|
|
34
|
-
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;
|
|
40
|
-
}
|
|
41
|
-
export type Handler<T> = (item: T) => Promise<void> | void;
|
|
42
|
-
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;
|
|
54
|
-
}
|
package/dist/connectors.js
DELETED
|
@@ -1,145 +0,0 @@
|
|
|
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");
|
|
13
|
-
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;
|
|
61
|
-
}
|
|
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;
|
|
83
|
-
}
|
|
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
|
-
}
|
|
104
|
-
}
|
|
105
|
-
throw "Unknown writer channel " + config.ty.value;
|
|
106
|
-
}
|
|
107
|
-
async init() {
|
|
108
|
-
await Promise.all(this.inits.map((x) => x()));
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
export class SimpleStream {
|
|
112
|
-
dataHandlers = [];
|
|
113
|
-
endHandlers = [];
|
|
114
|
-
disconnect;
|
|
115
|
-
lastElement;
|
|
116
|
-
ended = false;
|
|
117
|
-
constructor(onDisconnect) {
|
|
118
|
-
this.disconnect = onDisconnect || (async () => { });
|
|
119
|
-
}
|
|
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)));
|
|
130
|
-
}
|
|
131
|
-
async end() {
|
|
132
|
-
await this.disconnect();
|
|
133
|
-
await Promise.all(this.endHandlers.map((handler) => handler()));
|
|
134
|
-
this.ended = true;
|
|
135
|
-
}
|
|
136
|
-
on(event, listener) {
|
|
137
|
-
if (event === "data") {
|
|
138
|
-
this.dataHandlers.push(listener);
|
|
139
|
-
}
|
|
140
|
-
if (event === "end") {
|
|
141
|
-
this.endHandlers.push(listener);
|
|
142
|
-
}
|
|
143
|
-
return this;
|
|
144
|
-
}
|
|
145
|
-
}
|