-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
37 changed files
with
636 additions
and
245 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
"keywords": [ | ||
"rdf-connect" | ||
], | ||
"type": "module", | ||
"author": "Jens Pots", | ||
"license": "MIT", | ||
"bugs": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,45 @@ | ||
import { Processor, Log } from "jvm-runner-ts"; | ||
import * as fs from "node:fs"; | ||
|
||
export default class FileWriter extends Processor { | ||
export default class FileReader extends Processor { | ||
private outgoing = this.args.get("outgoing", { | ||
type: "writer", | ||
list: "false", | ||
nullable: "false", | ||
}); | ||
|
||
private path = this.args.get("path", { | ||
private paths = this.args.get("paths", { | ||
type: "string", | ||
list: "false", | ||
list: "true", | ||
nullable: "false", | ||
}); | ||
|
||
async exec(): Promise<void> { | ||
Log.shared.debug(() => `Reading file: ${this.path}`); | ||
const data = fs.readFileSync(this.path); | ||
this.outgoing.write(data); | ||
for (const path of this.paths) { | ||
await this.readFile(path); | ||
} | ||
this.outgoing.close(); | ||
} | ||
|
||
async readFile(path: string) { | ||
Log.shared.debug(() => `Reading file: ${path}`); | ||
|
||
// Remove the file prefix, since that is not valid in Node.js. | ||
if (path.startsWith("file://")) { | ||
path = path.slice(7); | ||
} | ||
|
||
let data: Buffer; | ||
try { | ||
data = fs.readFileSync(path); | ||
} catch (e) { | ||
if (e instanceof Error) { | ||
Log.shared.fatal(`Failed to read file: ${e.message}`); | ||
} else { | ||
Log.shared.fatal("Failed to read file"); | ||
} | ||
} | ||
|
||
this.outgoing.write(data!); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
"keywords": [ | ||
"rdf-connect" | ||
], | ||
"type": "module", | ||
"author": "Jens Pots", | ||
"license": "MIT", | ||
"bugs": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
runners/nodejs/src/interfaces/buffered_callback_channel.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { Writer } from "./writer"; | ||
import { RunnerError } from "../error"; | ||
|
||
/** | ||
* A buffered callback channel is a simple implementation of a writer that calls | ||
* a callback whenever a value is written to it. The class does therefore not | ||
* implement the `Reader` interface, as it is not possible to read from the | ||
* channel. | ||
* | ||
* The class buffers all values that are written to the channel before | ||
* the callback is set. Once the callback is set, all buffered values are | ||
* written to the callback. | ||
* | ||
* Note that the callback cannot be overwritten once it is set, and if the | ||
* channel is closed before a callback is set, an error is thrown. | ||
*/ | ||
export class BufferedCallbackChannel<T> implements Writer<T> { | ||
/** | ||
* The buffer that stores the values written to the channel as long as there | ||
* is no callback set. | ||
* @private | ||
*/ | ||
private buffer: Array<T> = []; | ||
|
||
/** | ||
* The callback that is called whenever a value is written to the channel. If | ||
* it is not set, the values are buffered in the `buffer` array. | ||
* @private | ||
*/ | ||
private callback: null | ((value: T) => void | Promise<void>) = null; | ||
|
||
/** | ||
* Whether the channel has been closed or not. | ||
* @private | ||
*/ | ||
private closed = false; | ||
|
||
close(): void { | ||
// The channel was closed before a callback was set, which results in a loss of data. | ||
if (this.callback === null) { | ||
RunnerError.channelError(); | ||
} | ||
|
||
this.closed = true; | ||
} | ||
|
||
isClosed(): boolean { | ||
return this.closed; | ||
} | ||
|
||
write(data: T): void { | ||
if (this.callback === null) { | ||
this.buffer.push(data); | ||
} else { | ||
this.callback(data); | ||
} | ||
} | ||
|
||
/** | ||
* Set the callback that is called whenever a value is written to the | ||
* channel. All values that were written to the channel before the callback | ||
* was set are written to the callback as well. | ||
* @param callback The callback to call whenever a value is written to the | ||
*/ | ||
setCallback(callback: (value: T) => void | Promise<void>): void { | ||
// The callback cannot be overwritten. | ||
if (this.callback != null) { | ||
RunnerError.channelError(); | ||
} | ||
|
||
this.callback = callback; | ||
this.buffer.forEach((value) => callback(value)); | ||
this.buffer = []; | ||
} | ||
} |
Oops, something went wrong.