@rdfc/js-runner 3.0.2 → 3.0.4-remote

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 (84) hide show
  1. package/.claude/settings.local.json +12 -0
  2. package/README.md +41 -1
  3. package/__tests__/echoProcessor.test.ts +11 -12
  4. package/bin/runner.js +7 -1
  5. package/bin/server.js +13 -0
  6. package/bun.lock +820 -0
  7. package/examples/echo/.idea/echo.iml +9 -0
  8. package/examples/echo/.idea/misc.xml +6 -0
  9. package/{.idea → examples/echo/.idea}/modules.xml +1 -1
  10. package/examples/echo/.idea/vcs.xml +7 -0
  11. package/examples/echo/.swls/config.json +1 -0
  12. package/examples/echo/package-lock.json +27 -29
  13. package/examples/echo/pipeline.ttl +0 -1
  14. package/examples/echo/processors.ttl +1 -1
  15. package/examples/echo/remote_pipeline.ttl +18 -0
  16. package/examples/echo/server.ttl +5 -0
  17. package/examples/echo/untitled:/types/MyType.ttl +0 -0
  18. package/file:/home/silvius/Projects/mumo-pipeline/ldes/http_3A_2F_2Fdata.mumo.be_2Fstreams_2Fnodes_2Fdefault/root/index.trig +3 -0
  19. package/ldes/http_3A_2F_2Fdata.mumo.be_2Fstreams_2Fnodes_2Fdefault/root/index.trig +3 -0
  20. package/lib/client.d.ts +2 -1
  21. package/lib/client.js +70 -22
  22. package/lib/index.d.ts +2 -0
  23. package/lib/index.js +3 -1
  24. package/lib/jsonld.d.ts +17 -0
  25. package/lib/jsonld.js +135 -0
  26. package/lib/logger.d.ts +1 -0
  27. package/lib/logger.js +19 -9
  28. package/lib/reader.d.ts +4 -1
  29. package/lib/reader.js +11 -3
  30. package/lib/runner.d.ts +6 -1
  31. package/lib/runner.js +43 -15
  32. package/lib/server.d.ts +9 -0
  33. package/lib/server.js +459 -0
  34. package/lib/state.d.ts +32 -0
  35. package/lib/state.js +71 -0
  36. package/lib/testUtils/duplex.js +1 -1
  37. package/lib/testUtils/index.d.ts +9 -9
  38. package/lib/testUtils/index.js +9 -9
  39. package/lib/testUtils.d.ts +24 -0
  40. package/lib/testUtils.js +150 -0
  41. package/lib/tsconfig.tsbuildinfo +1 -1
  42. package/lib/writer.d.ts +5 -1
  43. package/lib/writer.js +26 -10
  44. package/minimal.ttl +99 -0
  45. package/package.json +21 -19
  46. package/src/client.ts +99 -24
  47. package/src/index.ts +2 -0
  48. package/src/logger.ts +15 -7
  49. package/src/reader.ts +19 -9
  50. package/src/runner.ts +61 -12
  51. package/src/server.ts +545 -0
  52. package/src/state.ts +105 -0
  53. package/src/testUtils/duplex.ts +1 -4
  54. package/src/testUtils/index.ts +25 -21
  55. package/src/writer.ts +36 -12
  56. package/.idea/LNKD.tech Editor.xml +0 -194
  57. package/.idea/codeStyles/Project.xml +0 -52
  58. package/.idea/codeStyles/codeStyleConfig.xml +0 -5
  59. package/.idea/copilot.data.migration.agent.xml +0 -6
  60. package/.idea/copilot.data.migration.ask.xml +0 -6
  61. package/.idea/copilot.data.migration.ask2agent.xml +0 -6
  62. package/.idea/copilot.data.migration.edit.xml +0 -6
  63. package/.idea/inspectionProfiles/Project_Default.xml +0 -6
  64. package/.idea/js-runner.iml +0 -12
  65. package/.idea/vcs.xml +0 -6
  66. package/dist/args.d.ts +0 -4
  67. package/dist/args.js +0 -58
  68. package/dist/connectors/file.d.ts +0 -15
  69. package/dist/connectors/file.js +0 -89
  70. package/dist/connectors/http.d.ts +0 -14
  71. package/dist/connectors/http.js +0 -82
  72. package/dist/connectors/kafka.d.ts +0 -48
  73. package/dist/connectors/kafka.js +0 -68
  74. package/dist/connectors/ws.d.ts +0 -10
  75. package/dist/connectors/ws.js +0 -72
  76. package/dist/connectors.d.ts +0 -73
  77. package/dist/connectors.js +0 -168
  78. package/dist/index.cjs +0 -732
  79. package/dist/index.d.ts +0 -42
  80. package/dist/index.js +0 -83
  81. package/dist/tsconfig.tsbuildinfo +0 -1
  82. package/dist/util.d.ts +0 -71
  83. package/dist/util.js +0 -92
  84. package/jest.config.js +0 -2
package/dist/args.js DELETED
@@ -1,58 +0,0 @@
1
- import commandLineArgs from 'command-line-args'
2
- import commandLineUsage from 'command-line-usage'
3
- const optionDefinitions = [
4
- {
5
- name: 'input',
6
- type: String,
7
- defaultOption: true,
8
- summary: 'Specify what input file to start up',
9
- },
10
- {
11
- name: 'help',
12
- alias: 'h',
13
- type: Boolean,
14
- description: 'Display this help message',
15
- },
16
- ]
17
- const sections = [
18
- {
19
- header: 'Js-runner',
20
- content:
21
- 'JS-runner is part of the {italic connector architecture}. Starting from an input file start up all JsProcessors that are defined. Please do not use blank nodes, skolemize your data somewhere else!',
22
- },
23
- {
24
- header: 'Synopsis',
25
- content: '$ js-runner <input>',
26
- },
27
- {
28
- header: 'Command List',
29
- content: [
30
- { name: 'input', summary: 'Specify what input file to start up' },
31
- ],
32
- },
33
- {
34
- optionList: [optionDefinitions[1]],
35
- },
36
- ]
37
- function validArgs(args) {
38
- if (!args.input) return false
39
- return true
40
- }
41
- function printUsage() {
42
- const usage = commandLineUsage(sections)
43
- console.log(usage)
44
- process.exit(0)
45
- }
46
- export function getArgs() {
47
- let args
48
- try {
49
- args = commandLineArgs(optionDefinitions)
50
- } catch (e) {
51
- console.error(e)
52
- printUsage()
53
- }
54
- if (args.help || !validArgs(args)) {
55
- printUsage()
56
- }
57
- return args
58
- }
@@ -1,15 +0,0 @@
1
- import { ReaderConstructor, WriterConstructor } from '../connectors.js'
2
- export interface FileReaderConfig {
3
- path: string
4
- onReplace: boolean
5
- readFirstContent?: boolean
6
- encoding?: string
7
- }
8
- export interface FileWriterConfig {
9
- path: string
10
- onReplace: boolean
11
- readFirstContent?: boolean
12
- encoding?: string
13
- }
14
- export declare const startFileStreamReader: ReaderConstructor<FileReaderConfig>
15
- export declare const startFileStreamWriter: WriterConstructor<FileWriterConfig>
@@ -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(
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
50
- }
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
60
- }
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
- }
70
- export const startFileStreamWriter = (config) => {
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 +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>
@@ -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(
19
- () =>
20
- new Promise((res) => {
21
- if (server !== undefined) {
22
- server.close(() => {
23
- res()
24
- })
25
- } else {
26
- res()
27
- }
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
- 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>
@@ -1,68 +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(
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
- }
44
- export const startKafkaStreamWriter = (config) => {
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 +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>
@@ -1,72 +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(
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
- }
59
- export const startWsStreamWriter = (config) => {
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,73 +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<
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
- >
22
- export interface Config<T> {
23
- id: Term
24
- ty: NamedNode
25
- config: T
26
- }
27
- export type ReaderConstructor<C> = (config: C) => {
28
- reader: Stream<string | Buffer>
29
- init: () => Promise<void>
30
- }
31
- export type WriterConstructor<C> = (config: C) => {
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
- >
40
- export declare class ChannelFactory {
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>
47
- }
48
- export interface Writer<T> {
49
- push(item: T): Promise<void>
50
- end(): Promise<void>
51
- on(event: 'end', listener: Handler<void>): this
52
- }
53
- export interface Stream<T> {
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
59
- }
60
- export type Handler<T> = (item: T) => Promise<void> | void
61
- export declare class SimpleStream<T> implements Stream<T> {
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
73
- }
@@ -1,168 +0,0 @@
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
- )
40
- export class ChannelFactory {
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
50
- }
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]
75
- }
76
- if (c.channel.id.termType === 'BlankNode') {
77
- if (!this.jsChannelsBlankNodes[id]) {
78
- this.jsChannelsBlankNodes[id] = new SimpleStream()
79
- }
80
- return this.jsChannelsBlankNodes[id]
81
- }
82
- throw 'Should have found a thing'
83
- }
84
- }
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
93
- }
94
- if (config.ty.equals(Conn.WsWriterChannel)) {
95
- const { writer, init } = startWsStreamWriter(config.config)
96
- this.inits.push(init)
97
- return writer
98
- }
99
- if (config.ty.equals(Conn.KafkaWriterChannel)) {
100
- const { writer, init } = startKafkaStreamWriter(config.config)
101
- this.inits.push(init)
102
- return writer
103
- }
104
- if (config.ty.equals(Conn.HttpWriterChannel)) {
105
- const { writer, init } = startHttpStreamWriter(config.config)
106
- this.inits.push(init)
107
- return writer
108
- }
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]
118
- }
119
- if (c.channel.id.termType === 'BlankNode') {
120
- if (!this.jsChannelsBlankNodes[id]) {
121
- this.jsChannelsBlankNodes[id] = new SimpleStream()
122
- }
123
- return this.jsChannelsBlankNodes[id]
124
- }
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)
165
- }
166
- return this
167
- }
168
- }