@rdfc/js-runner 2.0.0-alpha.6 → 2.0.0-alpha.7

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/src/writer.ts CHANGED
@@ -5,85 +5,86 @@ import { Any } from './reader'
5
5
 
6
6
  type Writable = (msg: OrchestratorMessage) => Promise<unknown>
7
7
  export interface Writer {
8
- buffer(buffer: Uint8Array): Promise<void>
8
+ readonly uri: string;
9
+ buffer(buffer: Uint8Array): Promise<void>
9
10
 
10
- stream(buffer: AsyncIterable<Uint8Array>): Promise<void>
11
- stream<T>(
12
- buffer: AsyncIterable<T>,
13
- tranform: (x: T) => Uint8Array,
14
- ): Promise<void>
11
+ stream(buffer: AsyncIterable<Uint8Array>): Promise<void>
12
+ stream<T>(
13
+ buffer: AsyncIterable<T>,
14
+ tranform: (x: T) => Uint8Array,
15
+ ): Promise<void>
15
16
 
16
- string(buffer: string): Promise<void>
17
- any(any: Any): Promise<void>
18
- close(): Promise<void>
17
+ string(buffer: string): Promise<void>
18
+ any(any: Any): Promise<void>
19
+ close(): Promise<void>
19
20
  }
20
21
  const encoder = new TextEncoder()
21
22
  export class WriterInstance implements Writer {
22
- private readonly uri: string
23
- private readonly client: RunnerClient
24
- private readonly write: Writable
25
- private readonly logger: Logger
23
+ readonly uri: string
24
+ private readonly client: RunnerClient
25
+ private readonly write: Writable
26
+ private readonly logger: Logger
26
27
 
27
- constructor(
28
- uri: string,
29
- client: RunnerClient,
30
- write: Writable,
31
- logger: Logger,
32
- ) {
33
- this.client = client
34
- this.write = write
35
- this.uri = uri
36
- this.logger = logger
37
- }
38
- async any(any: Any): Promise<void> {
39
- if ('stream' in any) {
40
- await this.stream(any.stream)
28
+ constructor(
29
+ uri: string,
30
+ client: RunnerClient,
31
+ write: Writable,
32
+ logger: Logger,
33
+ ) {
34
+ this.client = client
35
+ this.write = write
36
+ this.uri = uri
37
+ this.logger = logger
41
38
  }
42
- if ('buffer' in any) {
43
- await this.buffer(any.buffer)
39
+ async any(any: Any): Promise<void> {
40
+ if ('stream' in any) {
41
+ await this.stream(any.stream)
42
+ }
43
+ if ('buffer' in any) {
44
+ await this.buffer(any.buffer)
45
+ }
46
+ if ('string' in any) {
47
+ await this.string(any.string)
48
+ }
44
49
  }
45
- if ('string' in any) {
46
- await this.string(any.string)
50
+
51
+ async buffer(buffer: Uint8Array): Promise<void> {
52
+ this.logger.debug(`${this.uri} sends buffer ${buffer.length} bytes`)
53
+ await this.write({ msg: { data: buffer, channel: this.uri } })
47
54
  }
48
- }
49
55
 
50
- async buffer(buffer: Uint8Array): Promise<void> {
51
- this.logger.debug(`${this.uri} sends buffer ${buffer.length} bytes`)
52
- await this.write({ msg: { data: buffer, channel: this.uri } })
53
- }
56
+ async stream<T = Uint8Array>(
57
+ buffer: AsyncIterable<T>,
58
+ transform?: (x: T) => Uint8Array,
59
+ ) {
60
+ const t = transform || ((x: unknown) => <Uint8Array>x)
61
+ const stream = this.client.sendStreamMessage()
62
+ const id: Id = await new Promise((res) => stream.once('data', res))
63
+ this.logger.debug(`${this.uri} streams message with id ${id.id}`)
64
+ await this.write({ streamMsg: { id, channel: this.uri } })
54
65
 
55
- async stream<T = Uint8Array>(
56
- buffer: AsyncIterable<T>,
57
- transform?: (x: T) => Uint8Array,
58
- ) {
59
- const t = transform || ((x: unknown) => <Uint8Array>x)
60
- const stream = this.client.sendStreamMessage()
61
- const id: Id = await new Promise((res) => stream.once('data', res))
62
- this.logger.debug(`${this.uri} streams message with id ${id.id}`)
63
- await this.write({ streamMsg: { id, channel: this.uri } })
66
+ const write = promisify(stream.write.bind(stream))
67
+ for await (const msg of buffer) {
68
+ await write({ data: t(msg) })
69
+ }
64
70
 
65
- const write = promisify(stream.write.bind(stream))
66
- for await (const msg of buffer) {
67
- await write({ data: t(msg) })
71
+ this.logger.debug(`${this.uri} is done streaming message with id ${id.id}`)
72
+ stream.end()
68
73
  }
69
74
 
70
- this.logger.debug(`${this.uri} is done streaming message with id ${id.id}`)
71
- stream.end()
72
- }
73
-
74
- async string(msg: string): Promise<void> {
75
- this.logger.debug(`${this.uri} sends string ${msg.length} characters`)
76
- await this.write({
77
- msg: { data: encoder.encode(msg), channel: this.uri },
78
- })
79
- }
75
+ async string(msg: string): Promise<void> {
76
+ this.logger.debug(`${this.uri} sends string ${msg.length} characters`)
77
+ await this.write({
78
+ msg: { data: encoder.encode(msg), channel: this.uri },
79
+ })
80
+ }
80
81
 
81
- async close(issued = false): Promise<void> {
82
- this.logger.debug(`${this.uri} closes stream`)
83
- if (!issued) {
84
- await this.write({
85
- close: { channel: this.uri },
86
- })
82
+ async close(issued = false): Promise<void> {
83
+ this.logger.debug(`${this.uri} closes stream`)
84
+ if (!issued) {
85
+ await this.write({
86
+ close: { channel: this.uri },
87
+ })
88
+ }
87
89
  }
88
- }
89
90
  }