@rdfc/js-runner 2.0.0-alpha.9 → 3.0.0
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/.editorconfig +9 -0
- package/.github/renovate.json +3 -0
- package/README.md +127 -3
- package/__tests__/channels.test.ts +131 -74
- package/__tests__/echoProcessor.test.ts +131 -0
- package/__tests__/testProcessor.test.ts +69 -0
- package/eslint.config.mjs +1 -1
- package/examples/echo/.idea/echo.iml +9 -0
- package/examples/echo/.idea/misc.xml +6 -0
- package/{.idea → examples/echo/.idea}/modules.xml +1 -1
- package/examples/echo/.idea/vcs.xml +7 -0
- package/examples/echo/.swls/config.json +1 -0
- package/examples/echo/index.ttl +3 -0
- package/examples/echo/minimal.ttl +90 -0
- package/examples/echo/shacl.ttl +9 -0
- package/examples/echo/shape.ttl +1339 -0
- package/examples/echo/test.ttl +11 -0
- package/examples/echo/untitled:/types/MyType.ttl +0 -0
- package/file:/home/silvius/Projects/mumo-pipeline/ldes/http_3A_2F_2Fdata.mumo.be_2Fstreams_2Fnodes_2Fdefault/root/index.trig +3 -0
- package/index.ttl +3 -31
- package/ldes/http_3A_2F_2Fdata.mumo.be_2Fstreams_2Fnodes_2Fdefault/root/index.trig +3 -0
- package/lib/client.js +7 -10
- package/lib/logger.d.ts +2 -2
- package/lib/logger.js +3 -3
- package/lib/reader.d.ts +8 -6
- package/lib/reader.js +135 -26
- package/lib/runner.d.ts +10 -5
- package/lib/runner.js +86 -46
- package/lib/testUtils/duplex.d.ts +25 -0
- package/lib/testUtils/duplex.js +70 -0
- package/lib/testUtils/index.d.ts +51 -0
- package/lib/testUtils/index.js +243 -0
- package/lib/testUtils.d.ts +6 -0
- package/lib/testUtils.js +92 -2
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/writer.d.ts +12 -5
- package/lib/writer.js +66 -13
- package/minimal.ttl +99 -0
- package/package.json +5 -5
- package/src/client.ts +9 -12
- package/src/logger.ts +3 -3
- package/src/reader.ts +207 -30
- package/src/runner.ts +128 -65
- package/src/testUtils/duplex.ts +112 -0
- package/src/testUtils/index.ts +430 -0
- package/src/writer.ts +106 -16
- package/.idea/LNKD.tech Editor.xml +0 -194
- package/.idea/codeStyles/Project.xml +0 -52
- package/.idea/codeStyles/codeStyleConfig.xml +0 -5
- package/.idea/inspectionProfiles/Project_Default.xml +0 -6
- package/.idea/js-runner.iml +0 -12
- package/.idea/vcs.xml +0 -6
- package/dist/args.d.ts +0 -4
- package/dist/args.js +0 -58
- 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 -68
- package/dist/connectors/ws.d.ts +0 -10
- package/dist/connectors/ws.js +0 -72
- package/dist/connectors.d.ts +0 -73
- package/dist/connectors.js +0 -168
- package/dist/index.cjs +0 -732
- package/dist/index.d.ts +0 -42
- package/dist/index.js +0 -83
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/dist/util.d.ts +0 -71
- package/dist/util.js +0 -92
- package/src/jsonld.ts +0 -220
- package/src/testUtils.ts +0 -77
package/.editorconfig
ADDED
package/README.md
CHANGED
|
@@ -1,5 +1,129 @@
|
|
|
1
|
-
#
|
|
1
|
+
# JavaScript runner for RDF-Connect
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
## Usage
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
To use the JavaScript (Node or Bun) runner for RDF-Connect, you need to have a pipeline configuration that includes JavaScript/TypeScript processors.
|
|
6
|
+
The runner can be added to your RDF-Connect pipeline as follows:
|
|
7
|
+
|
|
8
|
+
```turtle
|
|
9
|
+
@prefix rdfc: <https://w3id.org/rdf-connect#>.
|
|
10
|
+
@prefix owl: <http://www.w3.org/2002/07/owl#>.
|
|
11
|
+
|
|
12
|
+
### Import the runner
|
|
13
|
+
<> owl:imports <./node_modules/@rdfc/js-runner/index.ttl>.
|
|
14
|
+
|
|
15
|
+
### Define the pipeline and add the Node runner
|
|
16
|
+
<> a rdfc:Pipeline;
|
|
17
|
+
rdfc:consistsOf [
|
|
18
|
+
rdfc:instantiates rdfc:NodeRunner; # Use rdfc:BunRunner for Bun
|
|
19
|
+
rdfc:processor <log>, <send>; # List of JavaScript processors to be used in the pipeline. You should define and configure these processors separately.
|
|
20
|
+
].
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
This example assumes that the js-runner is installed in your project via npm or yarn in the current working directory.
|
|
24
|
+
|
|
25
|
+
You can install the js-runner package using the following command:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
npm install @rdfc/js-runner
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Logging
|
|
32
|
+
|
|
33
|
+
The JavaScript runner and processors use the `winston` logging library for logging.
|
|
34
|
+
The JavasScript runner initiates a logger that is passed to each processor, allowing them to log messages at various levels (info, warn, error, debug).
|
|
35
|
+
You can access this logger in your processor class code on the `this.logger` property.
|
|
36
|
+
Here's an example of how to use the logger in a processor:
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import { Processor } from '@rdfc/js-runner'
|
|
40
|
+
|
|
41
|
+
class MyProcessor extends Processor<MyProcessorArgs> {
|
|
42
|
+
async init(this: MyProcessorArgs & this): Promise<void> {
|
|
43
|
+
this.logger.info('I am initializing my processor!')
|
|
44
|
+
}
|
|
45
|
+
// ...
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
This logger is configured to forward log messages to the RDF-Connect logging system.
|
|
50
|
+
This means you can view and manage these logs in the RDF-Connect logging interface, allowing for consistent log management across different components of your RDF-Connect pipeline.
|
|
51
|
+
|
|
52
|
+
If you want to create a child logger for a subclass or submethod, you can do so using the `extendLogger` method.
|
|
53
|
+
Here's an example:
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
import { Processor, extendLogger } from '@rdfc/js-runner'
|
|
57
|
+
|
|
58
|
+
class MyProcessor extends Processor<MyProcessorArgs> {
|
|
59
|
+
async init(this: MyProcessorArgs & this): Promise<void> {
|
|
60
|
+
const childLogger = extendLogger(this.logger, 'init')
|
|
61
|
+
childLogger.debug('This is a debug message from init.')
|
|
62
|
+
}
|
|
63
|
+
// ...
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Develop a processor for this runner
|
|
68
|
+
|
|
69
|
+
The simplest way to start developing a processor for the JavaScript runner, is to start from the [template-processor-ts](https://github.com/rdf-connect/template-processor-ts) template repository.
|
|
70
|
+
It has everything set up to get you started quickly and let you focus on the actual processor logic.
|
|
71
|
+
|
|
72
|
+
At the very least, a JavaScript/TypeScript processor should consist of a class that inherits from the `Processor` abstract base class provided by the `@rdfc/js-runner` package.
|
|
73
|
+
This class should implement the `init` method, which is called when the processor is initialized.
|
|
74
|
+
This method is where you can set up any necessary configuration or state for your processor like opening a database connection or reading a file.
|
|
75
|
+
Additionally, you should implement the `transform` method, which is called before the `produce` method.
|
|
76
|
+
In this `transform` method, you should put any logic that handles incoming data by consuming readers, possibly transforming it, and passing it to the next processor in the pipeline.
|
|
77
|
+
This method should only write to writers as reply to the data it receives from readers, not produce new data, as it is important that it does not write data to channels before all readers have been initialized and are ready to consume data.
|
|
78
|
+
Finally, you should implement the `produce` method, which is called after all readers have been initialized and are ready to consume data.
|
|
79
|
+
It is called after the `transform` method.
|
|
80
|
+
This method is where you can produce (new) output data by writing to writers to send the data to the next step in the pipeline.
|
|
81
|
+
|
|
82
|
+
Next to the class, you should define a configuration for the processor in the `processor.ttl` file of your package.
|
|
83
|
+
JavaScript/TypeScript processors must include the JavaScript specific configuration parameters `rdfc:file`, `rdfc:class`, and `rdfc:entrypoint`, which specify the file, class, and entrypoint path of the processor, respectively.
|
|
84
|
+
|
|
85
|
+
## Development of the js-runner
|
|
86
|
+
|
|
87
|
+
The JavaScript runner is implemented in TypeScript.
|
|
88
|
+
The source code is contained in the `src` folder.
|
|
89
|
+
The main cli entry point is the `bin/runner.js` file, which you can also run after installation of the package using `npx js-runner`.
|
|
90
|
+
|
|
91
|
+
If you want to get started with the development of the js-runner, you can clone the repository and install the dependencies using the following commands.
|
|
92
|
+
|
|
93
|
+
Clone the repository:
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
git clone git@github.com:rdf-connect/js-runner.git
|
|
97
|
+
cd js-runner
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Install the dependencies:
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
npm install
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
You can run the formatter and linter using:
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
npm run format
|
|
110
|
+
npm run lint
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
You can run the tests using:
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
npm test
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
You can then build the project using:
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
npm run build
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Lastly, you can publish the package to npm using:
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
npm publish --access=public
|
|
129
|
+
```
|
|
@@ -1,96 +1,153 @@
|
|
|
1
|
-
import { describe, expect, test } from 'vitest'
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { describe, expect, test, vi } from 'vitest'
|
|
2
|
+
import { StreamMsgMock } from '../src/testUtils'
|
|
3
|
+
import { WriterInstance } from '../src/writer'
|
|
4
|
+
import { FromRunner, StreamIdentify } from '@rdfc/proto'
|
|
5
|
+
import { createLogger, transports } from 'winston'
|
|
4
6
|
|
|
5
7
|
const encoder = new TextEncoder()
|
|
8
|
+
const decoder = new TextDecoder()
|
|
6
9
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
reader.handleMsg({ data: encoder.encode('Hello world'), channel: uri })
|
|
13
|
-
const out = await promise
|
|
10
|
+
const logger = createLogger({
|
|
11
|
+
transports: new transports.Console({
|
|
12
|
+
level: process.env['DEBUG'] || 'info',
|
|
13
|
+
}),
|
|
14
|
+
})
|
|
14
15
|
|
|
15
|
-
|
|
16
|
+
describe('Writer', async () => {
|
|
17
|
+
test('sends strings', async () => {
|
|
18
|
+
const uri = 'someUri'
|
|
19
|
+
const runner = 'myRunner'
|
|
20
|
+
const fn = vi.fn((id: StreamIdentify) => {
|
|
21
|
+
expect(id.channel).toBe(uri)
|
|
22
|
+
expect(id.runner).toBe(runner)
|
|
23
|
+
return 1
|
|
24
|
+
})
|
|
25
|
+
const client = new StreamMsgMock(fn)
|
|
26
|
+
const msgs: FromRunner[] = []
|
|
27
|
+
const write = async (msg: FromRunner) => msgs.push(msg)
|
|
28
|
+
const writer = new WriterInstance(uri, client as any, write, runner, logger)
|
|
29
|
+
|
|
30
|
+
const send = writer.string('hello world')
|
|
31
|
+
writer.handled()
|
|
32
|
+
await send
|
|
33
|
+
|
|
34
|
+
expect(msgs.length).toBe(1)
|
|
35
|
+
expect(msgs.map((x) => decoder.decode(x.msg!.data))).toEqual([
|
|
36
|
+
'hello world',
|
|
37
|
+
])
|
|
38
|
+
|
|
39
|
+
expect(fn).toBeCalledTimes(0)
|
|
16
40
|
})
|
|
17
41
|
|
|
18
|
-
test('
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
42
|
+
test('sends binary', async () => {
|
|
43
|
+
const uri = 'someUri'
|
|
44
|
+
const runner = 'myRunner'
|
|
45
|
+
const fn = vi.fn((id: StreamIdentify) => {
|
|
46
|
+
expect(id.channel).toBe(uri)
|
|
47
|
+
expect(id.runner).toBe(runner)
|
|
48
|
+
return 1
|
|
49
|
+
})
|
|
50
|
+
const client = new StreamMsgMock(fn)
|
|
51
|
+
const msgs: FromRunner[] = []
|
|
52
|
+
const write = async (msg: FromRunner) => msgs.push(msg)
|
|
53
|
+
const writer = new WriterInstance(uri, client as any, write, runner, logger)
|
|
54
|
+
|
|
55
|
+
const send = writer.buffer(encoder.encode('hello world'))
|
|
56
|
+
writer.handled()
|
|
57
|
+
await send
|
|
58
|
+
|
|
59
|
+
expect(msgs.length).toBe(1)
|
|
60
|
+
expect(msgs.map((x) => decoder.decode(x.msg!.data))).toEqual([
|
|
61
|
+
'hello world',
|
|
62
|
+
])
|
|
63
|
+
|
|
64
|
+
expect(fn).toBeCalledTimes(0)
|
|
26
65
|
})
|
|
27
66
|
|
|
28
|
-
test('
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
67
|
+
test('streams data', async () => {
|
|
68
|
+
const uri = 'someUri'
|
|
69
|
+
const runner = 'myRunner'
|
|
70
|
+
const fn = vi.fn((id: StreamIdentify) => {
|
|
71
|
+
expect(id.channel).toBe(uri)
|
|
72
|
+
expect(id.runner).toBe(runner)
|
|
73
|
+
return 1
|
|
74
|
+
})
|
|
75
|
+
const client = new StreamMsgMock(fn)
|
|
76
|
+
const msgs: FromRunner[] = []
|
|
77
|
+
const write = async (msg: FromRunner) => msgs.push(msg)
|
|
78
|
+
const writer = new WriterInstance(uri, client as any, write, runner, logger)
|
|
79
|
+
|
|
80
|
+
async function* gen() {
|
|
81
|
+
yield encoder.encode('hello')
|
|
82
|
+
yield encoder.encode('world')
|
|
83
|
+
|
|
84
|
+
setTimeout(() => writer.handled(), 20)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
await writer.stream(gen())
|
|
88
|
+
|
|
89
|
+
expect(client.data.length).toBe(2)
|
|
90
|
+
expect(client.data.map((x) => decoder.decode(x.data))).toEqual([
|
|
91
|
+
'hello',
|
|
92
|
+
'world',
|
|
93
|
+
])
|
|
94
|
+
expect(fn).toBeCalled()
|
|
45
95
|
})
|
|
46
96
|
|
|
47
|
-
test('
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
97
|
+
test('closes', async () => {
|
|
98
|
+
const uri = 'someUri'
|
|
99
|
+
const runner = 'myRunner'
|
|
100
|
+
const fn = vi.fn((id: StreamIdentify) => {
|
|
101
|
+
expect(id.channel).toBe(uri)
|
|
102
|
+
expect(id.runner).toBe(runner)
|
|
103
|
+
return 1
|
|
104
|
+
})
|
|
105
|
+
const client = new StreamMsgMock(fn)
|
|
106
|
+
const msgs: FromRunner[] = []
|
|
107
|
+
const write = async (msg: FromRunner) => msgs.push(msg)
|
|
108
|
+
const writer = new WriterInstance(uri, client as any, write, runner, logger)
|
|
109
|
+
|
|
110
|
+
await writer.close()
|
|
111
|
+
|
|
112
|
+
expect(msgs.length).toBe(1)
|
|
113
|
+
expect(msgs.map((x) => x.close!.channel)).toEqual([uri])
|
|
114
|
+
|
|
115
|
+
expect(fn).toBeCalledTimes(0)
|
|
56
116
|
})
|
|
57
117
|
|
|
58
|
-
test('
|
|
59
|
-
const
|
|
118
|
+
test('wait to close after stream is finished', async () => {
|
|
119
|
+
const uri = 'someUri'
|
|
120
|
+
const runner = 'myRunner'
|
|
121
|
+
const client = new StreamMsgMock(() => 1)
|
|
60
122
|
|
|
61
|
-
const
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
const out = await promise
|
|
123
|
+
const msgs: FromRunner[] = []
|
|
124
|
+
const write = async (msg: FromRunner) => msgs.push(msg)
|
|
125
|
+
const writer = new WriterInstance(uri, client as any, write, runner, logger)
|
|
65
126
|
|
|
66
|
-
|
|
67
|
-
|
|
127
|
+
let closingPromise: Promise<void> | undefined = undefined
|
|
128
|
+
async function* gen() {
|
|
129
|
+
yield encoder.encode('hello')
|
|
68
130
|
|
|
69
|
-
|
|
70
|
-
|
|
131
|
+
// initiate close but the channel cannot close yet, as it has an open stream message
|
|
132
|
+
closingPromise = writer.close()
|
|
71
133
|
|
|
72
|
-
|
|
73
|
-
const promise = one(reader.streams())
|
|
74
|
-
reader.handleStreamingMessage({ id: { id: 5 }, channel: uri })
|
|
75
|
-
const stream = await nextStream
|
|
134
|
+
await new Promise((res) => setTimeout(res, 20))
|
|
76
135
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
console.log(stream.eventNames())
|
|
136
|
+
expect(msgs.filter((x) => !!x.close)).toEqual([])
|
|
137
|
+
yield encoder.encode('world')
|
|
80
138
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
for await (const msg of out!) {
|
|
85
|
-
msgs.push(msg)
|
|
86
|
-
}
|
|
87
|
-
return msgs
|
|
88
|
-
})()
|
|
139
|
+
// we 'handled' the message
|
|
140
|
+
setTimeout(() => writer.handled(), 20)
|
|
141
|
+
}
|
|
89
142
|
|
|
90
|
-
stream
|
|
91
|
-
|
|
143
|
+
await writer.stream(gen())
|
|
144
|
+
await closingPromise!
|
|
145
|
+
expect(msgs.map((x) => x.close!.channel)).toEqual([uri])
|
|
92
146
|
|
|
93
|
-
|
|
94
|
-
|
|
147
|
+
expect(client.data.length).toBe(2)
|
|
148
|
+
expect(client.data.map((x) => decoder.decode(x.data))).toEqual([
|
|
149
|
+
'hello',
|
|
150
|
+
'world',
|
|
151
|
+
])
|
|
95
152
|
})
|
|
96
153
|
})
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { describe, expect, test } from 'vitest'
|
|
2
|
+
import { Reader } from '../src/reader'
|
|
3
|
+
import { createRunner, channel } from '../src/testUtils'
|
|
4
|
+
import { Processor } from '../src/processor'
|
|
5
|
+
import { Writer } from '../src/writer'
|
|
6
|
+
import { FullProc } from '../src/runner'
|
|
7
|
+
import { createLogger, transports } from 'winston'
|
|
8
|
+
|
|
9
|
+
const encoder = new TextEncoder()
|
|
10
|
+
|
|
11
|
+
const logger = createLogger({
|
|
12
|
+
transports: new transports.Console({
|
|
13
|
+
level: process.env['DEBUG'] || 'info',
|
|
14
|
+
}),
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
class EchoProcessor extends Processor<{
|
|
18
|
+
reader: Reader
|
|
19
|
+
writer: Writer
|
|
20
|
+
streams: boolean
|
|
21
|
+
}> {
|
|
22
|
+
async init(this: { reader: Reader; writer: Writer } & this): Promise<void> {
|
|
23
|
+
this.logger.info('EchoProcessor initialized')
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async transform(
|
|
27
|
+
this: { reader: Reader; writer: Writer; streams: boolean } & this,
|
|
28
|
+
): Promise<void> {
|
|
29
|
+
this.logger.info('EchoProcessor transforming')
|
|
30
|
+
|
|
31
|
+
if (this.streams) {
|
|
32
|
+
for await (const msg of this.reader.streams()) {
|
|
33
|
+
this.logger.info(`EchoProcessor received stream`)
|
|
34
|
+
await this.writer.stream(msg)
|
|
35
|
+
}
|
|
36
|
+
} else {
|
|
37
|
+
for await (const msg of this.reader.strings()) {
|
|
38
|
+
this.logger.info(`EchoProcessor received: ${msg}`)
|
|
39
|
+
await this.writer.string(msg)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
this.logger.info('EchoProcessor transformed')
|
|
44
|
+
await this.writer.close()
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async produce(
|
|
48
|
+
this: { reader: Reader; writer: Writer } & this,
|
|
49
|
+
): Promise<void> {
|
|
50
|
+
// Nothing to produce, transform handles everything
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
describe('EchoProcessor', () => {
|
|
55
|
+
test('echoes string messages correctly', async () => {
|
|
56
|
+
const runner = createRunner()
|
|
57
|
+
|
|
58
|
+
const [inputWriter, inputReader] = channel(runner, 'input')
|
|
59
|
+
const [outputWriter, outputReader] = channel(runner, 'output')
|
|
60
|
+
|
|
61
|
+
const proc = <FullProc<EchoProcessor>>(
|
|
62
|
+
new EchoProcessor(
|
|
63
|
+
{ reader: inputReader, writer: outputWriter, streams: false },
|
|
64
|
+
logger,
|
|
65
|
+
)
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
await proc.init()
|
|
69
|
+
const transformPromise = proc.transform()
|
|
70
|
+
|
|
71
|
+
const msgs: string[] = []
|
|
72
|
+
|
|
73
|
+
;(async () => {
|
|
74
|
+
for await (const m of outputReader.strings()) {
|
|
75
|
+
msgs.push(m)
|
|
76
|
+
}
|
|
77
|
+
})()
|
|
78
|
+
|
|
79
|
+
await inputWriter.string('Hello')
|
|
80
|
+
expect(msgs).toEqual(['Hello'])
|
|
81
|
+
await inputWriter.string('world')
|
|
82
|
+
|
|
83
|
+
await inputWriter.close()
|
|
84
|
+
|
|
85
|
+
// Wait for processing to complete
|
|
86
|
+
await transformPromise
|
|
87
|
+
|
|
88
|
+
expect(msgs).toEqual(['Hello', 'world'])
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
test('echoes stream messages correctly', async () => {
|
|
92
|
+
const runner = createRunner()
|
|
93
|
+
|
|
94
|
+
const [inputWriter, inputReader] = channel(runner, 'input')
|
|
95
|
+
const [outputWriter, outputReader] = channel(runner, 'output')
|
|
96
|
+
|
|
97
|
+
const proc = <FullProc<EchoProcessor>>(
|
|
98
|
+
new EchoProcessor(
|
|
99
|
+
{ reader: inputReader, writer: outputWriter, streams: true },
|
|
100
|
+
logger,
|
|
101
|
+
)
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
await proc.init()
|
|
105
|
+
const transformPromise = proc.transform()
|
|
106
|
+
|
|
107
|
+
const msgs: string[] = []
|
|
108
|
+
|
|
109
|
+
;(async () => {
|
|
110
|
+
for await (const m of outputReader.strings()) {
|
|
111
|
+
msgs.push(m)
|
|
112
|
+
}
|
|
113
|
+
})()
|
|
114
|
+
|
|
115
|
+
const gen = async function* () {
|
|
116
|
+
yield encoder.encode('Hello')
|
|
117
|
+
yield encoder.encode('World')
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
await inputWriter.stream(gen())
|
|
121
|
+
expect(msgs).toEqual(['HelloWorld'])
|
|
122
|
+
await inputWriter.stream(gen())
|
|
123
|
+
|
|
124
|
+
await inputWriter.close()
|
|
125
|
+
|
|
126
|
+
// Wait for processing to complete
|
|
127
|
+
await transformPromise
|
|
128
|
+
|
|
129
|
+
expect(msgs).toEqual(['HelloWorld', 'HelloWorld'])
|
|
130
|
+
})
|
|
131
|
+
})
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { describe, expect, test, vi } from 'vitest'
|
|
2
|
+
import { ProcHelper } from '../src/testUtils'
|
|
3
|
+
import { Processor } from '../src/processor'
|
|
4
|
+
import path from 'path/posix'
|
|
5
|
+
|
|
6
|
+
// Example test processor
|
|
7
|
+
export class TestProcessor extends Processor<{ message: string }> {
|
|
8
|
+
async init(): Promise<void> {
|
|
9
|
+
this.logger.info('Test processor initialized')
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async transform(): Promise<void> {
|
|
13
|
+
// Mock transform - just wait
|
|
14
|
+
await new Promise((resolve) => setTimeout(resolve, 10))
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async produce(): Promise<void> {
|
|
18
|
+
// Mock produce - do nothing
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
describe('Test processor', async () => {
|
|
23
|
+
test('Is well defined', async () => {
|
|
24
|
+
const helper = new ProcHelper<TestProcessor>()
|
|
25
|
+
|
|
26
|
+
await helper.importFile(path.resolve('./index.ttl'))
|
|
27
|
+
await helper.importInline(
|
|
28
|
+
path.resolve('config.ttl'),
|
|
29
|
+
`@prefix ex: <http://example.org/>.
|
|
30
|
+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
|
|
31
|
+
@prefix sh: <http://www.w3.org/ns/shacl#>.
|
|
32
|
+
@prefix owl: <http://www.w3.org/2002/07/owl#>.
|
|
33
|
+
@prefix rdfc: <https://w3id.org/rdf-connect#>.
|
|
34
|
+
|
|
35
|
+
rdfc:Test a owl:Class;
|
|
36
|
+
rdfc:jsImplementationOf rdfc:Processor;
|
|
37
|
+
rdfc:file <./__tests__/testProcessor.test.ts>;
|
|
38
|
+
rdfc:class "TestProcessor";
|
|
39
|
+
rdfc:entrypoint <./>.
|
|
40
|
+
|
|
41
|
+
[ ] a sh:NodeShape;
|
|
42
|
+
sh:targetClass rdfc:Test;
|
|
43
|
+
sh:property [
|
|
44
|
+
sh:datatype xsd:string;
|
|
45
|
+
sh:path rdfc:message;
|
|
46
|
+
sh:name "message";
|
|
47
|
+
sh:maxCount 1;
|
|
48
|
+
sh:minCount 1;
|
|
49
|
+
].`,
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
const config = helper.getConfig('Test')
|
|
53
|
+
expect(config.clazz).toEqual('TestProcessor')
|
|
54
|
+
expect(config.location).toBeDefined()
|
|
55
|
+
expect(config.file).toBeDefined()
|
|
56
|
+
|
|
57
|
+
await helper.importInline(
|
|
58
|
+
path.resolve('pipeline.ttl'),
|
|
59
|
+
`@prefix rdfc: <https://w3id.org/rdf-connect#>.
|
|
60
|
+
|
|
61
|
+
<http://example.org/proc> a rdfc:Test;
|
|
62
|
+
rdfc:message "Hello world".`,
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
const proc = await helper.getProcessor('http://example.org/proc')
|
|
66
|
+
|
|
67
|
+
expect(proc.message).toEqual('Hello world')
|
|
68
|
+
})
|
|
69
|
+
})
|
package/eslint.config.mjs
CHANGED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<module type="JAVA_MODULE" version="4">
|
|
3
|
+
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
|
4
|
+
<exclude-output />
|
|
5
|
+
<content url="file://$MODULE_DIR$" />
|
|
6
|
+
<orderEntry type="inheritedJdk" />
|
|
7
|
+
<orderEntry type="sourceFolder" forTests="false" />
|
|
8
|
+
</component>
|
|
9
|
+
</module>
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
<project version="4">
|
|
3
3
|
<component name="ProjectModuleManager">
|
|
4
4
|
<modules>
|
|
5
|
-
<module fileurl="file://$PROJECT_DIR$/.idea/
|
|
5
|
+
<module fileurl="file://$PROJECT_DIR$/.idea/echo.iml" filepath="$PROJECT_DIR$/.idea/echo.iml" />
|
|
6
6
|
</modules>
|
|
7
7
|
</component>
|
|
8
8
|
</project>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "disabled": ["shapes"] }
|