@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.
Files changed (79) hide show
  1. package/.husky/pre-commit +6 -0
  2. package/.prettierrc +4 -0
  3. package/README.md +3 -38
  4. package/__tests__/channels.test.ts +133 -0
  5. package/bin/runner.js +8 -0
  6. package/eslint.config.mjs +21 -0
  7. package/examples/echo/package-lock.json +80 -0
  8. package/examples/echo/package.json +18 -0
  9. package/examples/echo/pipeline.ttl +30 -0
  10. package/examples/echo/processors.ttl +84 -0
  11. package/examples/echo/src/processors.ts +75 -0
  12. package/examples/echo/test.ttl +14 -0
  13. package/examples/echo/tsconfig.json +114 -0
  14. package/examples/echo/untitled:/types/MyType.ttl +0 -0
  15. package/index.ttl +63 -0
  16. package/jest.config.js +2 -0
  17. package/lib/client.d.ts +1 -0
  18. package/lib/client.js +43 -0
  19. package/lib/convertor.d.ts +9 -0
  20. package/lib/convertor.js +51 -0
  21. package/lib/index.d.ts +4 -0
  22. package/lib/index.js +5 -0
  23. package/lib/jsonld.d.ts +17 -0
  24. package/lib/jsonld.js +134 -0
  25. package/lib/logger.d.ts +15 -0
  26. package/lib/logger.js +27 -0
  27. package/lib/processor.d.ts +19 -0
  28. package/lib/processor.js +13 -0
  29. package/lib/reader.d.ts +29 -0
  30. package/lib/reader.js +110 -0
  31. package/lib/runner.d.ts +25 -0
  32. package/lib/runner.js +111 -0
  33. package/lib/tsconfig.tsbuildinfo +1 -0
  34. package/lib/util_processors.d.ts +11 -0
  35. package/lib/util_processors.js +13 -0
  36. package/lib/writer.d.ts +25 -0
  37. package/lib/writer.js +57 -0
  38. package/package.json +49 -51
  39. package/src/client.ts +52 -0
  40. package/src/convertor.ts +59 -0
  41. package/src/index.ts +4 -0
  42. package/src/jsonld.ts +217 -0
  43. package/src/logger.ts +38 -0
  44. package/src/processor.ts +39 -0
  45. package/src/reader.ts +148 -0
  46. package/src/runner.ts +186 -0
  47. package/src/util_processors.ts +20 -0
  48. package/src/writer.ts +89 -0
  49. package/tsconfig.json +33 -0
  50. package/vite.config.ts +10 -0
  51. package/LICENSE +0 -21
  52. package/bin/js-runner.js +0 -4
  53. package/channels/file.ttl +0 -37
  54. package/channels/http.ttl +0 -59
  55. package/channels/kafka.ttl +0 -98
  56. package/channels/ws.ttl +0 -33
  57. package/dist/args.d.ts +0 -4
  58. package/dist/args.js +0 -59
  59. package/dist/connectors/file.d.ts +0 -15
  60. package/dist/connectors/file.js +0 -89
  61. package/dist/connectors/http.d.ts +0 -14
  62. package/dist/connectors/http.js +0 -82
  63. package/dist/connectors/kafka.d.ts +0 -48
  64. package/dist/connectors/kafka.js +0 -64
  65. package/dist/connectors/ws.d.ts +0 -10
  66. package/dist/connectors/ws.js +0 -69
  67. package/dist/connectors.d.ts +0 -54
  68. package/dist/connectors.js +0 -145
  69. package/dist/index.cjs +0 -677
  70. package/dist/index.d.ts +0 -33
  71. package/dist/index.js +0 -74
  72. package/dist/tsconfig.tsbuildinfo +0 -1
  73. package/dist/util.d.ts +0 -43
  74. package/dist/util.js +0 -75
  75. package/ontology.ttl +0 -169
  76. package/processor/echo.ttl +0 -38
  77. package/processor/resc.ttl +0 -34
  78. package/processor/send.ttl +0 -40
  79. package/processor/test.js +0 -35
package/dist/index.cjs DELETED
@@ -1,677 +0,0 @@
1
- 'use strict';
2
-
3
- var n3 = require('n3');
4
- var commandLineArgs = require('command-line-args');
5
- var commandLineUsage = require('command-line-usage');
6
- var fs = require('fs');
7
- var http = require('http');
8
- var https = require('https');
9
- var types = require('@treecg/types');
10
- var debug = require('debug');
11
- var path = require('path');
12
- var promises = require('fs/promises');
13
- var node_fs = require('node:fs');
14
- var ws = require('ws');
15
- var kafkajs = require('kafkajs');
16
- var rdfLens = require('rdf-lens');
17
-
18
- function _interopNamespaceDefault(e) {
19
- var n = Object.create(null);
20
- if (e) {
21
- Object.keys(e).forEach(function (k) {
22
- if (k !== 'default') {
23
- var d = Object.getOwnPropertyDescriptor(e, k);
24
- Object.defineProperty(n, k, d.get ? d : {
25
- enumerable: true,
26
- get: function () { return e[k]; }
27
- });
28
- }
29
- });
30
- }
31
- n.default = e;
32
- return Object.freeze(n);
33
- }
34
-
35
- var http__namespace = /*#__PURE__*/_interopNamespaceDefault(http);
36
-
37
- const optionDefinitions = [
38
- {
39
- name: "input",
40
- type: String,
41
- defaultOption: true,
42
- summary: "Specify what input file to start up",
43
- },
44
- {
45
- name: "help",
46
- alias: "h",
47
- type: Boolean,
48
- description: "Display this help message",
49
- },
50
- ];
51
- const sections = [
52
- {
53
- header: "Js-runner",
54
- content: "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!",
55
- },
56
- {
57
- header: "Synopsis",
58
- content: "$ js-runner <input>",
59
- },
60
- {
61
- header: "Command List",
62
- content: [
63
- { name: "input", summary: "Specify what input file to start up" },
64
- ],
65
- },
66
- {
67
- optionList: [optionDefinitions[1]],
68
- },
69
- ];
70
- function validArgs(args) {
71
- if (!args.input)
72
- return false;
73
- return true;
74
- }
75
- function printUsage() {
76
- const usage = commandLineUsage(sections);
77
- console.log(usage);
78
- process.exit(0);
79
- }
80
- function getArgs() {
81
- let args;
82
- try {
83
- args = commandLineArgs(optionDefinitions);
84
- }
85
- catch (e) {
86
- console.error(e);
87
- printUsage();
88
- }
89
- if (args.help || !validArgs(args)) {
90
- printUsage();
91
- }
92
- return args;
93
- }
94
-
95
- const LOG = (function () {
96
- const main = debug("js-runner");
97
- const channel = main.extend("channel");
98
- const util = main.extend("util");
99
- return { main, channel, util };
100
- })();
101
- function toArray(stream) {
102
- const output = [];
103
- return new Promise((res, rej) => {
104
- stream.on("data", (x) => output.push(x));
105
- stream.on("end", () => res(output));
106
- stream.on("close", () => res(output));
107
- stream.on("error", rej);
108
- });
109
- }
110
- const OWL = types.createUriAndTermNamespace("http://www.w3.org/2002/07/owl#", "imports");
111
- types.createUriAndTermNamespace("https://w3id.org/conn#", "install", "build", "GitInstall", "LocalInstall", "url", "procFile", "path", "EnvVariable", "envKey", "envDefault");
112
- const { namedNode, literal } = n3.DataFactory;
113
- async function get_readstream(location) {
114
- if (location.startsWith("https")) {
115
- return new Promise((res) => {
116
- https.get(location, res);
117
- });
118
- }
119
- else if (location.startsWith("http")) {
120
- return new Promise((res) => {
121
- http.get(location, res);
122
- });
123
- }
124
- else {
125
- return fs.createReadStream(location);
126
- }
127
- }
128
- async function load_quads(location, baseIRI) {
129
- try {
130
- LOG.util("Loading quads %s", location);
131
- const parser = new n3.StreamParser({ baseIRI: baseIRI || location });
132
- const rdfStream = await get_readstream(location);
133
- rdfStream.pipe(parser);
134
- const quads = await toArray(parser);
135
- return quads;
136
- }
137
- catch (ex) {
138
- console.error("Failed to load_quads", location, baseIRI);
139
- console.error(ex);
140
- return [];
141
- }
142
- }
143
- function load_memory_quads(value, baseIRI) {
144
- const parser = new n3.Parser({ baseIRI });
145
- return parser.parse(value);
146
- }
147
- const loaded = new Set();
148
- async function load_store(location, store, recursive = true) {
149
- if (loaded.has(location))
150
- return;
151
- loaded.add(location);
152
- const quads = location.type === "remote"
153
- ? await load_quads(location.location)
154
- : load_memory_quads(location.value, location.baseIRI);
155
- store.addQuads(quads);
156
- if (recursive) {
157
- const loc = location.type === "remote" ? location.location : location.baseIRI;
158
- const other_imports = store.getObjects(namedNode(loc), OWL.terms.imports, null);
159
- for (const other of other_imports) {
160
- await load_store({ location: other.value, type: "remote" }, store, true);
161
- }
162
- }
163
- }
164
-
165
- async function getFileSize(path) {
166
- return (await promises.stat(path)).size;
167
- }
168
- function readPart(path, start, end, encoding) {
169
- return new Promise((res) => {
170
- const stream = fs.createReadStream(path, { encoding, start, end });
171
- let buffer = "";
172
- stream.on("data", (chunk) => {
173
- buffer += chunk;
174
- });
175
- stream.on("close", () => res(buffer));
176
- });
177
- }
178
- function debounce(func, timeout = 100) {
179
- let timer;
180
- return (...args) => {
181
- clearTimeout(timer);
182
- timer = setTimeout(() => {
183
- func(...args);
184
- }, timeout);
185
- };
186
- }
187
- const startFileStreamReader = (config) => {
188
- const path$1 = path.isAbsolute(config.path)
189
- ? config.path
190
- : `${process.cwd()}/${config.path}`;
191
- fs.openSync(path$1, "a+");
192
- const encoding = config.encoding || "utf-8";
193
- const reader = new SimpleStream();
194
- const init = async () => {
195
- let currentPos = await getFileSize(path$1);
196
- const watcher = node_fs.watch(path$1, { encoding: "utf-8" });
197
- watcher.on("change", debounce(async () => {
198
- try {
199
- let content;
200
- if (config.onReplace) {
201
- content = await promises.readFile(path$1, { encoding });
202
- }
203
- else {
204
- const newSize = await getFileSize(path$1);
205
- if (newSize <= currentPos) {
206
- currentPos = newSize;
207
- return;
208
- }
209
- content = await readPart(path$1, currentPos, newSize, encoding);
210
- currentPos = newSize;
211
- }
212
- await reader.push(content);
213
- }
214
- catch (error) {
215
- if (error.code === "ENOENT") {
216
- return;
217
- }
218
- throw error;
219
- }
220
- }));
221
- if (config.onReplace && config.readFirstContent) {
222
- const content = await promises.readFile(path$1, { encoding });
223
- await reader.push(content);
224
- }
225
- };
226
- return { reader, init };
227
- };
228
- const startFileStreamWriter = (config) => {
229
- const path$1 = path.isAbsolute(config.path)
230
- ? config.path
231
- : `${process.cwd()}/${config.path}`;
232
- const encoding = config.encoding || "utf-8";
233
- const writer = new SimpleStream();
234
- const init = async () => {
235
- if (!config.onReplace) {
236
- await promises.writeFile(path$1, "", { encoding });
237
- }
238
- };
239
- writer.push = async (item) => {
240
- if (config.onReplace) {
241
- await promises.writeFile(path$1, item, { encoding });
242
- }
243
- else {
244
- await promises.appendFile(path$1, item, { encoding });
245
- }
246
- };
247
- return { writer, init };
248
- };
249
-
250
- function _connectWs(url, res) {
251
- const ws$1 = new ws.WebSocket(url, {});
252
- ws$1.on("error", () => {
253
- setTimeout(() => _connectWs(url, res), 300);
254
- });
255
- ws$1.on("ping", () => ws$1.pong());
256
- ws$1.on("open", () => {
257
- res(ws$1);
258
- });
259
- }
260
- function connectWs(url) {
261
- return new Promise((res) => _connectWs(url, res));
262
- }
263
- const startWsStreamReader = (config) => {
264
- const server = new ws.WebSocketServer(config);
265
- server.on("error", (error) => {
266
- console.error("Ws server error:");
267
- console.error(error);
268
- });
269
- const connections = [];
270
- const interval = setInterval(() => {
271
- connections.forEach((instance, i) => {
272
- if (!instance) {
273
- return;
274
- }
275
- if (!instance.alive) {
276
- instance.socket.terminate();
277
- delete connections[i];
278
- return;
279
- }
280
- instance.socket.ping();
281
- instance.alive = false;
282
- });
283
- }, 30_000);
284
- const reader = new SimpleStream(() => new Promise((res) => {
285
- clearInterval(interval);
286
- server.close(() => res());
287
- }));
288
- server.on("connection", (ws) => {
289
- const instance = { socket: ws, alive: true };
290
- connections.push(instance);
291
- ws.on("message", async (msg) => {
292
- reader.push(msg.toString()).catch((error) => {
293
- throw error;
294
- });
295
- });
296
- ws.on("pong", () => {
297
- instance.alive = true;
298
- });
299
- });
300
- return { reader, init: async () => { } };
301
- };
302
- const startWsStreamWriter = (config) => {
303
- let ws;
304
- const init = async () => {
305
- ws = await connectWs(config.url);
306
- ws.on("open", () => console.log("open"));
307
- };
308
- const writer = new SimpleStream(async () => {
309
- ws.close();
310
- });
311
- writer.push = async (item) => {
312
- ws.send(item);
313
- };
314
- return { writer, init };
315
- };
316
-
317
- const startKafkaStreamReader = (config) => {
318
- const brokerConfig = {};
319
- if (typeof config.broker === "string" || config.broker instanceof String) {
320
- Object.assign(brokerConfig, JSON.parse(node_fs.readFileSync(config.broker, "utf-8")));
321
- }
322
- else {
323
- Object.assign(brokerConfig, config.broker);
324
- }
325
- if (brokerConfig && brokerConfig.hosts) {
326
- brokerConfig.brokers = (brokerConfig).hosts;
327
- }
328
- const kafka = new kafkajs.Kafka(brokerConfig);
329
- const consumer = kafka.consumer(config.consumer);
330
- const stream = new SimpleStream(async () => {
331
- await consumer.disconnect();
332
- await consumer.stop();
333
- });
334
- const init = async () => {
335
- await consumer.connect();
336
- await consumer.subscribe({
337
- topic: config.topic.name,
338
- fromBeginning: config.topic.fromBeginning,
339
- });
340
- consumer
341
- .run({
342
- async eachMessage({ topic, message, }) {
343
- if (topic === config.topic.name) {
344
- const element = message.value?.toString() ?? "";
345
- stream.push(element).catch((error) => {
346
- throw error;
347
- });
348
- }
349
- },
350
- })
351
- .catch((error) => {
352
- throw error;
353
- });
354
- };
355
- return { reader: stream, init };
356
- };
357
- const startKafkaStreamWriter = (config) => {
358
- const topic = config.topic.name;
359
- const brokerConfig = {};
360
- if (typeof config.broker === "string" || config.broker instanceof String) {
361
- Object.assign(brokerConfig, JSON.parse(node_fs.readFileSync(config.broker, "utf-8")));
362
- }
363
- else {
364
- Object.assign(brokerConfig, config.broker);
365
- }
366
- if (brokerConfig && brokerConfig.hosts) {
367
- brokerConfig.brokers = (brokerConfig).hosts;
368
- }
369
- const kafka = new kafkajs.Kafka(brokerConfig);
370
- const producer = kafka.producer(config.producer);
371
- const writer = new SimpleStream(async () => {
372
- await producer.disconnect();
373
- });
374
- const init = () => producer.connect();
375
- writer.push = async (item) => {
376
- await producer.send({ topic, messages: [{ value: item }] });
377
- };
378
- return { writer, init };
379
- };
380
-
381
- function streamToString(stream, binary) {
382
- const datas = [];
383
- return new Promise((res) => {
384
- stream.on("data", (data) => {
385
- datas.push(data);
386
- });
387
- stream.on("end", () => {
388
- const streamData = Buffer.concat(datas);
389
- res(binary ? streamData : streamData.toString());
390
- });
391
- });
392
- }
393
- const startHttpStreamReader = (config) => {
394
- let server = undefined;
395
- const stream = new SimpleStream(() => new Promise((res) => {
396
- if (server !== undefined) {
397
- server.close(() => {
398
- res();
399
- });
400
- }
401
- else {
402
- res();
403
- }
404
- }));
405
- const requestListener = async function (req, res) {
406
- try {
407
- const content = await streamToString(req, config.binary);
408
- const promise = stream.push(content).catch((error) => {
409
- throw error;
410
- });
411
- if (config.waitHandled) {
412
- await promise;
413
- }
414
- }
415
- catch (error) {
416
- console.error("Failed", error);
417
- }
418
- res.writeHead(config.responseCode || 200);
419
- res.end("OK");
420
- };
421
- server = http.createServer(requestListener);
422
- const init = () => {
423
- return new Promise((res) => {
424
- const cb = () => res(undefined);
425
- if (server) {
426
- server.listen(config.port, config.endpoint, cb);
427
- }
428
- else {
429
- cb();
430
- }
431
- });
432
- };
433
- return { reader: stream, init };
434
- };
435
- const startHttpStreamWriter = (config) => {
436
- const requestConfig = new URL(config.endpoint);
437
- const writer = new SimpleStream();
438
- writer.push = async (item) => {
439
- await new Promise((resolve) => {
440
- const options = {
441
- hostname: requestConfig.hostname,
442
- path: requestConfig.path,
443
- method: config.method,
444
- port: requestConfig.port,
445
- };
446
- const cb = (response) => {
447
- response.on("data", () => { });
448
- response.on("end", () => {
449
- resolve(null);
450
- });
451
- };
452
- const req = http__namespace.request(options, cb);
453
- req.write(item, () => {
454
- req.end();
455
- });
456
- });
457
- };
458
- return { writer, init: async () => { } };
459
- };
460
-
461
- const Conn = types.createTermNamespace("https://w3id.org/conn#", "FileReaderChannel", "FileWriterChannel", "HttpReaderChannel", "HttpWriterChannel", "KafkaReaderChannel", "KafkaWriterChannel", "WsReaderChannel", "WsWriterChannel", "WriterChannel", "ReaderChannel");
462
- const JsOntology = types.createTermNamespace("https://w3id.org/conn/js#", "JsProcess", "JsChannel", "JsReaderChannel", "JsWriterChannel");
463
- class ChannelFactory {
464
- inits = [];
465
- jsChannelsNamedNodes = {};
466
- jsChannelsBlankNodes = {};
467
- createReader(config) {
468
- LOG.channel("Creating reader %s: a %s", config.id.value, config.ty.value);
469
- if (config.ty.equals(Conn.FileReaderChannel)) {
470
- const { reader, init } = startFileStreamReader(config.config);
471
- this.inits.push(init);
472
- return reader;
473
- }
474
- if (config.ty.equals(Conn.WsReaderChannel)) {
475
- const { reader, init } = startWsStreamReader(config.config);
476
- this.inits.push(init);
477
- return reader;
478
- }
479
- if (config.ty.equals(Conn.KafkaReaderChannel)) {
480
- const { reader, init } = startKafkaStreamReader(config.config);
481
- this.inits.push(init);
482
- return reader;
483
- }
484
- if (config.ty.equals(Conn.HttpReaderChannel)) {
485
- const { reader, init } = startHttpStreamReader(config.config);
486
- this.inits.push(init);
487
- return reader;
488
- }
489
- if (config.ty.equals(JsOntology.JsReaderChannel)) {
490
- const c = config.config;
491
- if (c.channel) {
492
- const id = c.channel.id.value;
493
- if (c.channel.id.termType === "NamedNode") {
494
- if (!this.jsChannelsNamedNodes[id]) {
495
- this.jsChannelsNamedNodes[id] =
496
- new SimpleStream();
497
- }
498
- return this.jsChannelsNamedNodes[id];
499
- }
500
- if (c.channel.id.termType === "BlankNode") {
501
- if (!this.jsChannelsBlankNodes[id]) {
502
- this.jsChannelsBlankNodes[id] =
503
- new SimpleStream();
504
- }
505
- return this.jsChannelsBlankNodes[id];
506
- }
507
- throw "Should have found a thing";
508
- }
509
- }
510
- throw "Unknown reader channel " + config.ty.value;
511
- }
512
- createWriter(config) {
513
- LOG.channel("Creating writer %s: a %s", config.id.value, config.ty.value);
514
- if (config.ty.equals(Conn.FileWriterChannel)) {
515
- const { writer, init } = startFileStreamWriter(config.config);
516
- this.inits.push(init);
517
- return writer;
518
- }
519
- if (config.ty.equals(Conn.WsWriterChannel)) {
520
- const { writer, init } = startWsStreamWriter(config.config);
521
- this.inits.push(init);
522
- return writer;
523
- }
524
- if (config.ty.equals(Conn.KafkaWriterChannel)) {
525
- const { writer, init } = startKafkaStreamWriter(config.config);
526
- this.inits.push(init);
527
- return writer;
528
- }
529
- if (config.ty.equals(Conn.HttpWriterChannel)) {
530
- const { writer, init } = startHttpStreamWriter(config.config);
531
- this.inits.push(init);
532
- return writer;
533
- }
534
- if (config.ty.equals(JsOntology.JsWriterChannel)) {
535
- const c = config.config;
536
- if (c.channel) {
537
- const id = c.channel.id.value;
538
- if (c.channel.id.termType === "NamedNode") {
539
- if (!this.jsChannelsNamedNodes[id]) {
540
- this.jsChannelsNamedNodes[id] =
541
- new SimpleStream();
542
- }
543
- return this.jsChannelsNamedNodes[id];
544
- }
545
- if (c.channel.id.termType === "BlankNode") {
546
- if (!this.jsChannelsBlankNodes[id]) {
547
- this.jsChannelsBlankNodes[id] =
548
- new SimpleStream();
549
- }
550
- return this.jsChannelsBlankNodes[id];
551
- }
552
- throw "Should have found a thing";
553
- }
554
- }
555
- throw "Unknown writer channel " + config.ty.value;
556
- }
557
- async init() {
558
- await Promise.all(this.inits.map((x) => x()));
559
- }
560
- }
561
- class SimpleStream {
562
- dataHandlers = [];
563
- endHandlers = [];
564
- disconnect;
565
- lastElement;
566
- ended = false;
567
- constructor(onDisconnect) {
568
- this.disconnect = onDisconnect || (async () => { });
569
- }
570
- data(listener) {
571
- this.dataHandlers.push(listener);
572
- return this;
573
- }
574
- async push(data) {
575
- if (this.ended) {
576
- throw new Error("Trying to push to a stream that has ended!");
577
- }
578
- this.lastElement = data;
579
- await Promise.all(this.dataHandlers.map((handler) => handler(data)));
580
- }
581
- async end() {
582
- await this.disconnect();
583
- await Promise.all(this.endHandlers.map((handler) => handler()));
584
- this.ended = true;
585
- }
586
- on(event, listener) {
587
- if (event === "data") {
588
- this.dataHandlers.push(listener);
589
- }
590
- if (event === "end") {
591
- this.endHandlers.push(listener);
592
- }
593
- return this;
594
- }
595
- }
596
-
597
- function safeJoin(a, b) {
598
- if (b.startsWith("/")) {
599
- return b;
600
- }
601
- return path.join(a, b);
602
- }
603
- async function extractProcessors(source, apply) {
604
- const store = new n3.Store();
605
- await load_store(source, store);
606
- const quads = store.getQuads(null, null, null, null);
607
- const config = rdfLens.extractShapes(quads, apply);
608
- const subjects = quads
609
- .filter((x) => x.predicate.equals(types.RDF.terms.type) &&
610
- x.object.equals(JsOntology.JsProcess))
611
- .map((x) => x.subject);
612
- const processorLens = config.lenses[JsOntology.JsProcess.value];
613
- const processors = subjects.map((id) => processorLens.execute({ id, quads }));
614
- return { processors, quads, shapes: config };
615
- }
616
- function extractSteps(proc, quads, config) {
617
- const out = [];
618
- const subjects = quads
619
- .filter((x) => x.predicate.equals(types.RDF.terms.type) && x.object.equals(proc.ty))
620
- .map((x) => x.subject);
621
- const processorLens = config.lenses[proc.ty.value];
622
- const fields = proc.mapping.parameters;
623
- for (const id of subjects) {
624
- const obj = processorLens.execute({ id, quads });
625
- const functionArgs = new Array(fields.length);
626
- for (const field of fields) {
627
- functionArgs[field.position] = obj[field.parameter];
628
- }
629
- out.push(functionArgs);
630
- }
631
- return out;
632
- }
633
- async function jsRunner() {
634
- const args = getArgs();
635
- const cwd = process.cwd();
636
- const source = {
637
- location: safeJoin(cwd, args.input).replaceAll("\\", "/"),
638
- type: "remote",
639
- };
640
- const factory = new ChannelFactory();
641
- const apply = {};
642
- apply[Conn.ReaderChannel.value] = factory.createReader.bind(factory);
643
- apply[Conn.WriterChannel.value] = factory.createWriter.bind(factory);
644
- const { processors, quads, shapes: config, } = await extractProcessors(source, apply);
645
- LOG.main("Found %d processors", processors.length);
646
- const starts = [];
647
- for (const proc of processors) {
648
- const argss = extractSteps(proc, quads, config);
649
- const jsProgram = await import("file://" + proc.file);
650
- process.chdir(proc.location);
651
- for (const args of argss) {
652
- starts.push(await jsProgram[proc.func](...args));
653
- }
654
- }
655
- await factory.init();
656
- for (const s of starts) {
657
- if (s && typeof s === "function") {
658
- s();
659
- }
660
- }
661
- }
662
-
663
- exports.ChannelFactory = ChannelFactory;
664
- exports.Conn = Conn;
665
- exports.JsOntology = JsOntology;
666
- exports.SimpleStream = SimpleStream;
667
- exports.extractProcessors = extractProcessors;
668
- exports.extractSteps = extractSteps;
669
- exports.jsRunner = jsRunner;
670
- exports.startFileStreamReader = startFileStreamReader;
671
- exports.startFileStreamWriter = startFileStreamWriter;
672
- exports.startHttpStreamReader = startHttpStreamReader;
673
- exports.startHttpStreamWriter = startHttpStreamWriter;
674
- exports.startKafkaStreamReader = startKafkaStreamReader;
675
- exports.startKafkaStreamWriter = startKafkaStreamWriter;
676
- exports.startWsStreamReader = startWsStreamReader;
677
- exports.startWsStreamWriter = startWsStreamWriter;
package/dist/index.d.ts DELETED
@@ -1,33 +0,0 @@
1
- import { Quad, Term } from "@rdfjs/types";
2
- import { Shapes } from "rdf-lens";
3
- export * from "./connectors.js";
4
- type Processor = {
5
- ty: Term;
6
- file: string;
7
- location: string;
8
- func: string;
9
- mapping: {
10
- parameters: {
11
- parameter: string;
12
- position: number;
13
- }[];
14
- };
15
- };
16
- export type Source = {
17
- type: "remote";
18
- location: string;
19
- } | {
20
- type: "memory";
21
- value: string;
22
- baseIRI: string;
23
- };
24
- export type Extracted = {
25
- processors: Processor[];
26
- quads: Quad[];
27
- shapes: Shapes;
28
- };
29
- export declare function extractProcessors(source: Source, apply?: {
30
- [label: string]: (item: any) => any;
31
- }): Promise<Extracted>;
32
- export declare function extractSteps(proc: Processor, quads: Quad[], config: Shapes): any[][];
33
- export declare function jsRunner(): Promise<void>;