@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,89 +1,89 @@
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";
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
6
  async function getFileSize(path) {
7
- return (await stat(path)).size;
7
+ return (await stat(path)).size
8
8
  }
9
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
- });
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
18
  }
19
19
  function debounce(func, timeout = 100) {
20
- let timer;
21
- return (...args) => {
22
- clearTimeout(timer);
23
- timer = setTimeout(() => {
24
- func(...args);
25
- }, timeout);
26
- };
20
+ let timer
21
+ return (...args) => {
22
+ clearTimeout(timer)
23
+ timer = setTimeout(() => {
24
+ func(...args)
25
+ }, timeout)
26
+ }
27
27
  }
28
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);
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(
39
+ 'change',
40
+ debounce(async () => {
41
+ try {
42
+ let content
43
+ if (config.onReplace) {
44
+ content = await readFile(path, { encoding })
45
+ } else {
46
+ const newSize = await getFileSize(path)
47
+ if (newSize <= currentPos) {
48
+ currentPos = newSize
49
+ return
54
50
  }
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);
51
+ content = await readPart(path, currentPos, newSize, encoding)
52
+ currentPos = newSize
53
+ }
54
+ await reader.push(content)
55
+ } catch (error) {
56
+ if (error.code === 'ENOENT') {
57
+ return
58
+ }
59
+ throw error
65
60
  }
66
- };
67
- return { reader, init };
68
- };
61
+ }),
62
+ )
63
+ if (config.onReplace && config.readFirstContent) {
64
+ const content = await readFile(path, { encoding })
65
+ await reader.push(content)
66
+ }
67
+ }
68
+ return { reader, init }
69
+ }
69
70
  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
- };
71
+ const path = isAbsolute(config.path)
72
+ ? config.path
73
+ : `${process.cwd()}/${config.path}`
74
+ const encoding = config.encoding || 'utf-8'
75
+ const writer = new SimpleStream()
76
+ const init = async () => {
77
+ if (!config.onReplace) {
78
+ await writeFile(path, '', { encoding })
79
+ }
80
+ }
81
+ writer.push = async (item) => {
82
+ if (config.onReplace) {
83
+ await writeFile(path, item, { encoding })
84
+ } else {
85
+ await appendFile(path, item, { encoding })
86
+ }
87
+ }
88
+ return { writer, init }
89
+ }
@@ -1,14 +1,14 @@
1
- import { ReaderConstructor, WriterConstructor } from "../connectors.js";
1
+ import { ReaderConstructor, WriterConstructor } from '../connectors.js'
2
2
  export interface HttpReaderConfig {
3
- endpoint: string;
4
- port: number;
5
- binary: boolean;
6
- waitHandled?: boolean;
7
- responseCode?: number;
3
+ endpoint: string
4
+ port: number
5
+ binary: boolean
6
+ waitHandled?: boolean
7
+ responseCode?: number
8
8
  }
9
- export declare const startHttpStreamReader: ReaderConstructor<HttpReaderConfig>;
9
+ export declare const startHttpStreamReader: ReaderConstructor<HttpReaderConfig>
10
10
  export interface HttpWriterConfig {
11
- endpoint: string;
12
- method: string;
11
+ endpoint: string
12
+ method: string
13
13
  }
14
- export declare const startHttpStreamWriter: WriterConstructor<HttpWriterConfig>;
14
+ export declare const startHttpStreamWriter: WriterConstructor<HttpWriterConfig>
@@ -1,82 +1,82 @@
1
- import * as http from "http";
2
- import { createServer } from "http";
3
- import { SimpleStream, } from "../connectors.js";
1
+ import * as http from 'http'
2
+ import { createServer } from 'http'
3
+ import { SimpleStream } from '../connectors.js'
4
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
- });
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
15
  }
16
16
  export const startHttpStreamReader = (config) => {
17
- let server = undefined;
18
- const stream = new SimpleStream(() => new Promise((res) => {
17
+ let server = undefined
18
+ const stream = new SimpleStream(
19
+ () =>
20
+ new Promise((res) => {
19
21
  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);
22
+ server.close(() => {
23
+ res()
24
+ })
25
+ } else {
26
+ res()
40
27
  }
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
- };
28
+ }),
29
+ )
30
+ const requestListener = async function (req, res) {
31
+ try {
32
+ const content = await streamToString(req, config.binary)
33
+ const promise = stream.push(content).catch((error) => {
34
+ throw error
35
+ })
36
+ if (config.waitHandled) {
37
+ await promise
38
+ }
39
+ } catch (error) {
40
+ console.error('Failed', error)
41
+ }
42
+ res.writeHead(config.responseCode || 200)
43
+ res.end('OK')
44
+ }
45
+ server = createServer(requestListener)
46
+ const init = () => {
47
+ return new Promise((res) => {
48
+ const cb = () => res(undefined)
49
+ if (server) {
50
+ server.listen(config.port, config.endpoint, cb)
51
+ } else {
52
+ cb()
53
+ }
54
+ })
55
+ }
56
+ return { reader: stream, init }
57
+ }
58
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
- };
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 +1,48 @@
1
- import type { ProducerConfig } from "kafkajs";
2
- import { ReaderConstructor, WriterConstructor } from "../connectors.js";
1
+ import type { ProducerConfig } from 'kafkajs'
2
+ import { ReaderConstructor, WriterConstructor } from '../connectors.js'
3
3
  export interface SASLOptions {
4
- mechanism: "plain";
5
- username: string;
6
- password: string;
4
+ mechanism: 'plain'
5
+ username: string
6
+ password: string
7
7
  }
8
8
  export interface BrokerConfig {
9
- hosts: string[];
10
- ssl?: boolean;
11
- sasl?: SASLOptions;
9
+ hosts: string[]
10
+ ssl?: boolean
11
+ sasl?: SASLOptions
12
12
  }
13
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;
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
27
  }
28
28
  export interface CSTopic {
29
- topic: string;
30
- fromBeginning?: boolean;
29
+ topic: string
30
+ fromBeginning?: boolean
31
31
  }
32
32
  export interface KafkaReaderConfig {
33
- topic: {
34
- name: string;
35
- fromBeginning?: boolean;
36
- };
37
- consumer: ConsumerConfig;
38
- broker: string | BrokerConfig;
33
+ topic: {
34
+ name: string
35
+ fromBeginning?: boolean
36
+ }
37
+ consumer: ConsumerConfig
38
+ broker: string | BrokerConfig
39
39
  }
40
- export declare const startKafkaStreamReader: ReaderConstructor<KafkaReaderConfig>;
40
+ export declare const startKafkaStreamReader: ReaderConstructor<KafkaReaderConfig>
41
41
  export interface KafkaWriterConfig {
42
- topic: {
43
- name: string;
44
- };
45
- producer: ProducerConfig;
46
- broker: BrokerConfig | string;
42
+ topic: {
43
+ name: string
44
+ }
45
+ producer: ProducerConfig
46
+ broker: BrokerConfig | string
47
47
  }
48
- export declare const startKafkaStreamWriter: WriterConstructor<KafkaWriterConfig>;
48
+ export declare const startKafkaStreamWriter: WriterConstructor<KafkaWriterConfig>
@@ -1,64 +1,68 @@
1
- import { readFileSync } from "node:fs";
2
- import { Kafka } from "kafkajs";
3
- import { SimpleStream, } from "../connectors.js";
1
+ import { readFileSync } from 'node:fs'
2
+ import { Kafka } from 'kafkajs'
3
+ import { SimpleStream } from '../connectors.js'
4
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
- };
5
+ const brokerConfig = {}
6
+ if (typeof config.broker === 'string' || config.broker instanceof String) {
7
+ Object.assign(
8
+ brokerConfig,
9
+ JSON.parse(readFileSync(config.broker, 'utf-8')),
10
+ )
11
+ } else {
12
+ Object.assign(brokerConfig, config.broker)
13
+ }
14
+ if (brokerConfig && brokerConfig.hosts) {
15
+ brokerConfig.brokers = brokerConfig.hosts
16
+ }
17
+ const kafka = new Kafka(brokerConfig)
18
+ const consumer = kafka.consumer(config.consumer)
19
+ const stream = new SimpleStream(async () => {
20
+ await consumer.disconnect()
21
+ await consumer.stop()
22
+ })
23
+ const init = async () => {
24
+ await consumer.connect()
25
+ await consumer.subscribe({
26
+ topic: config.topic.name,
27
+ fromBeginning: config.topic.fromBeginning,
28
+ })
29
+ consumer
30
+ .run({
31
+ async eachMessage({ topic, message }) {
32
+ if (topic === config.topic.name) {
33
+ const element = message.value?.toString() ?? ''
34
+ await stream.push(element)
35
+ }
36
+ },
37
+ })
38
+ .catch((error) => {
39
+ throw error
40
+ })
41
+ }
42
+ return { reader: stream, init }
43
+ }
42
44
  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
- };
45
+ const topic = config.topic.name
46
+ const brokerConfig = {}
47
+ if (typeof config.broker === 'string' || config.broker instanceof String) {
48
+ Object.assign(
49
+ brokerConfig,
50
+ JSON.parse(readFileSync(config.broker, 'utf-8')),
51
+ )
52
+ } else {
53
+ Object.assign(brokerConfig, config.broker)
54
+ }
55
+ if (brokerConfig && brokerConfig.hosts) {
56
+ brokerConfig.brokers = brokerConfig.hosts
57
+ }
58
+ const kafka = new Kafka(brokerConfig)
59
+ const producer = kafka.producer(config.producer)
60
+ const writer = new SimpleStream(async () => {
61
+ await producer.disconnect()
62
+ })
63
+ const init = () => producer.connect()
64
+ writer.push = async (item) => {
65
+ await producer.send({ topic, messages: [{ value: item }] })
66
+ }
67
+ return { writer, init }
68
+ }
@@ -1,10 +1,10 @@
1
- import { ReaderConstructor, WriterConstructor } from "../connectors.js";
1
+ import { ReaderConstructor, WriterConstructor } from '../connectors.js'
2
2
  export interface WsWriterConfig {
3
- url: string;
3
+ url: string
4
4
  }
5
5
  export interface WsReaderConfig {
6
- host: string;
7
- port: number;
6
+ host: string
7
+ port: number
8
8
  }
9
- export declare const startWsStreamReader: ReaderConstructor<WsReaderConfig>;
10
- export declare const startWsStreamWriter: WriterConstructor<WsWriterConfig>;
9
+ export declare const startWsStreamReader: ReaderConstructor<WsReaderConfig>
10
+ export declare const startWsStreamWriter: WriterConstructor<WsWriterConfig>