@streamr/cli-tools 5.0.1 → 6.0.0-alpha.3

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 (169) hide show
  1. package/.eslintignore +0 -1
  2. package/CHANGELOG.md +18 -1
  3. package/README.md +42 -17
  4. package/bin/streamr-mock-data-generate.ts +38 -8
  5. package/bin/streamr-mock-data.ts +1 -1
  6. package/bin/streamr-storage-node-add-stream.ts +8 -26
  7. package/bin/streamr-storage-node-list-stream-parts.ts +18 -34
  8. package/bin/streamr-storage-node-list.ts +14 -34
  9. package/bin/streamr-storage-node-remove-stream.ts +8 -26
  10. package/bin/streamr-storage-node.ts +1 -1
  11. package/bin/streamr-stream-create.ts +17 -29
  12. package/bin/streamr-stream-grant-permission.ts +9 -66
  13. package/bin/streamr-stream-publish.ts +54 -21
  14. package/bin/streamr-stream-resend-from.ts +26 -0
  15. package/bin/streamr-stream-resend-last.ts +25 -0
  16. package/bin/streamr-stream-resend-range.ts +32 -0
  17. package/bin/streamr-stream-resend.ts +7 -92
  18. package/bin/streamr-stream-revoke-permission.ts +8 -24
  19. package/bin/streamr-stream-search.ts +17 -0
  20. package/bin/streamr-stream-show.ts +16 -16
  21. package/bin/streamr-stream-subscribe.ts +16 -16
  22. package/bin/streamr-stream.ts +2 -2
  23. package/bin/streamr.ts +1 -2
  24. package/dist/bin/streamr-mock-data-generate.d.ts +1 -1
  25. package/dist/bin/streamr-mock-data-generate.js +35 -9
  26. package/dist/bin/streamr-mock-data-generate.js.map +1 -1
  27. package/dist/bin/streamr-mock-data.js +1 -1
  28. package/dist/bin/streamr-mock-data.js.map +1 -1
  29. package/dist/bin/streamr-storage-node-add-stream.d.ts +1 -1
  30. package/dist/bin/streamr-storage-node-add-stream.js +8 -24
  31. package/dist/bin/streamr-storage-node-add-stream.js.map +1 -1
  32. package/dist/bin/streamr-storage-node-list-stream-parts.d.ts +1 -1
  33. package/dist/bin/streamr-storage-node-list-stream-parts.js +17 -28
  34. package/dist/bin/streamr-storage-node-list-stream-parts.js.map +1 -1
  35. package/dist/bin/streamr-storage-node-list.d.ts +1 -1
  36. package/dist/bin/streamr-storage-node-list.js +15 -30
  37. package/dist/bin/streamr-storage-node-list.js.map +1 -1
  38. package/dist/bin/streamr-storage-node-remove-stream.d.ts +1 -1
  39. package/dist/bin/streamr-storage-node-remove-stream.js +8 -24
  40. package/dist/bin/streamr-storage-node-remove-stream.js.map +1 -1
  41. package/dist/bin/streamr-storage-node.js +1 -1
  42. package/dist/bin/streamr-storage-node.js.map +1 -1
  43. package/dist/bin/streamr-stream-create.d.ts +1 -1
  44. package/dist/bin/streamr-stream-create.js +14 -23
  45. package/dist/bin/streamr-stream-create.js.map +1 -1
  46. package/dist/bin/streamr-stream-grant-permission.d.ts +1 -1
  47. package/dist/bin/streamr-stream-grant-permission.js +3 -63
  48. package/dist/bin/streamr-stream-grant-permission.js.map +1 -1
  49. package/dist/bin/streamr-stream-publish.d.ts +1 -1
  50. package/dist/bin/streamr-stream-publish.js +49 -23
  51. package/dist/bin/streamr-stream-publish.js.map +1 -1
  52. package/dist/bin/streamr-stream-resend-from.d.ts +2 -0
  53. package/dist/bin/streamr-stream-resend-from.js +27 -0
  54. package/dist/bin/streamr-stream-resend-from.js.map +1 -0
  55. package/dist/bin/streamr-stream-resend-last.d.ts +2 -0
  56. package/dist/bin/streamr-stream-resend-last.js +26 -0
  57. package/dist/bin/streamr-stream-resend-last.js.map +1 -0
  58. package/dist/bin/streamr-stream-resend-range.d.ts +2 -0
  59. package/dist/bin/streamr-stream-resend-range.js +33 -0
  60. package/dist/bin/streamr-stream-resend-range.js.map +1 -0
  61. package/dist/bin/streamr-stream-resend.d.ts +1 -1
  62. package/dist/bin/streamr-stream-resend.js +8 -87
  63. package/dist/bin/streamr-stream-resend.js.map +1 -1
  64. package/dist/bin/streamr-stream-revoke-permission.d.ts +1 -1
  65. package/dist/bin/streamr-stream-revoke-permission.js +3 -21
  66. package/dist/bin/streamr-stream-revoke-permission.js.map +1 -1
  67. package/dist/bin/streamr-stream-search.d.ts +2 -0
  68. package/dist/bin/streamr-stream-search.js +21 -0
  69. package/dist/bin/streamr-stream-search.js.map +1 -0
  70. package/dist/bin/streamr-stream-show.d.ts +1 -1
  71. package/dist/bin/streamr-stream-show.js +15 -15
  72. package/dist/bin/streamr-stream-show.js.map +1 -1
  73. package/dist/bin/streamr-stream-subscribe.d.ts +1 -1
  74. package/dist/bin/streamr-stream-subscribe.js +17 -20
  75. package/dist/bin/streamr-stream-subscribe.js.map +1 -1
  76. package/dist/bin/streamr-stream.js +2 -2
  77. package/dist/bin/streamr-stream.js.map +1 -1
  78. package/dist/bin/streamr.js +1 -2
  79. package/dist/bin/streamr.js.map +1 -1
  80. package/dist/package.json +18 -13
  81. package/dist/src/client.d.ts +3 -0
  82. package/dist/src/client.js +33 -0
  83. package/dist/src/client.js.map +1 -0
  84. package/dist/src/command.d.ts +8 -0
  85. package/dist/src/command.js +45 -0
  86. package/dist/src/command.js.map +1 -0
  87. package/dist/src/common.d.ts +6 -0
  88. package/dist/src/common.js +15 -0
  89. package/dist/src/common.js.map +1 -0
  90. package/dist/src/config.d.ts +6 -0
  91. package/dist/src/config.js +58 -0
  92. package/dist/src/config.js.map +1 -0
  93. package/dist/src/logLevel.d.ts +0 -0
  94. package/dist/src/logLevel.js +11 -0
  95. package/dist/src/logLevel.js.map +1 -0
  96. package/dist/src/permission.d.ts +4 -0
  97. package/dist/src/permission.js +48 -0
  98. package/dist/src/permission.js.map +1 -0
  99. package/dist/src/resend.d.ts +3 -4
  100. package/dist/src/resend.js +12 -23
  101. package/dist/src/resend.js.map +1 -1
  102. package/dist/tsconfig.tsbuildinfo +1 -8525
  103. package/package.json +18 -13
  104. package/src/client.ts +32 -0
  105. package/src/command.ts +45 -0
  106. package/src/common.ts +16 -0
  107. package/src/config.ts +56 -0
  108. package/src/logLevel.ts +9 -0
  109. package/src/permission.ts +49 -0
  110. package/src/resend.ts +19 -28
  111. package/tsconfig.json +13 -5
  112. package/bin/common.ts +0 -93
  113. package/bin/streamr-misc-get-session-token.ts +0 -33
  114. package/bin/streamr-misc.ts +0 -10
  115. package/bin/streamr-stream-list.ts +0 -33
  116. package/dist/bin/common.d.ts +0 -17
  117. package/dist/bin/common.js +0 -77
  118. package/dist/bin/common.js.map +0 -1
  119. package/dist/bin/streamr-misc-get-session-token.d.ts +0 -2
  120. package/dist/bin/streamr-misc-get-session-token.js +0 -33
  121. package/dist/bin/streamr-misc-get-session-token.js.map +0 -1
  122. package/dist/bin/streamr-misc.d.ts +0 -2
  123. package/dist/bin/streamr-misc.js +0 -15
  124. package/dist/bin/streamr-misc.js.map +0 -1
  125. package/dist/bin/streamr-stream-list.d.ts +0 -2
  126. package/dist/bin/streamr-stream-list.js +0 -37
  127. package/dist/bin/streamr-stream-list.js.map +0 -1
  128. package/dist/commands/common.js +0 -76
  129. package/dist/commands/streamor.js +0 -16
  130. package/dist/commands/streamr-mock-data-generate.js +0 -20
  131. package/dist/commands/streamr-mock-data.js +0 -14
  132. package/dist/commands/streamr-storage-node-add-stream.js +0 -24
  133. package/dist/commands/streamr-storage-node-list-stream-parts.js +0 -32
  134. package/dist/commands/streamr-storage-node-list.js +0 -44
  135. package/dist/commands/streamr-storage-node-remove-stream.js +0 -24
  136. package/dist/commands/streamr-storage-node.js +0 -17
  137. package/dist/commands/streamr-stream-create.js +0 -31
  138. package/dist/commands/streamr-stream-grant-permission.js +0 -62
  139. package/dist/commands/streamr-stream-list.js +0 -33
  140. package/dist/commands/streamr-stream-publish.js +0 -32
  141. package/dist/commands/streamr-stream-resend.js +0 -101
  142. package/dist/commands/streamr-stream-revoke-permission.js +0 -24
  143. package/dist/commands/streamr-stream-show.js +0 -23
  144. package/dist/commands/streamr-stream-subscribe.js +0 -25
  145. package/dist/commands/streamr-stream.js +0 -21
  146. package/dist/src/create.d.ts +0 -2
  147. package/dist/src/create.js +0 -19
  148. package/dist/src/create.js.map +0 -1
  149. package/dist/src/generate.d.ts +0 -1
  150. package/dist/src/generate.js +0 -32
  151. package/dist/src/generate.js.map +0 -1
  152. package/dist/src/list.d.ts +0 -2
  153. package/dist/src/list.js +0 -29
  154. package/dist/src/list.js.map +0 -1
  155. package/dist/src/publish.d.ts +0 -3
  156. package/dist/src/publish.js +0 -36
  157. package/dist/src/publish.js.map +0 -1
  158. package/dist/src/show.d.ts +0 -2
  159. package/dist/src/show.js +0 -23
  160. package/dist/src/show.js.map +0 -1
  161. package/dist/src/subscribe.d.ts +0 -2
  162. package/dist/src/subscribe.js +0 -13
  163. package/dist/src/subscribe.js.map +0 -1
  164. package/src/create.ts +0 -16
  165. package/src/generate.ts +0 -33
  166. package/src/list.ts +0 -22
  167. package/src/publish.ts +0 -42
  168. package/src/show.ts +0 -23
  169. package/src/subscribe.ts +0 -9
package/.eslintignore CHANGED
@@ -1,3 +1,2 @@
1
1
  node_modules/**
2
2
  dist/**
3
-
package/CHANGELOG.md CHANGED
@@ -7,7 +7,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
  ## [Unreleased]
8
8
  - Add permission commands: `stream grant-permission` and `stream revoke-permission`
9
9
  - Remove `typescript` and `ts-node` as run-time dependencies
10
- - Remove `--msg-chain-id` parameter from `stream resend from`
10
+ - (Breaking) Remove `--msg-chain-id` parameter from `stream resend from`
11
+ - (Breaking) `stream create` argument is a stream ID, not a name
12
+ - Support path notation when defining a stream ID
13
+ - (Breaking) Remove `misc get-session-token` command
14
+ - (Breaking) Change permission names and arguments:
15
+ - renamed `--operation` to `--permission` in `stream list`
16
+ - the data format of `permissions` returned by `--include-permissions` of `stream show`
17
+ - enum values updated for all commands: `subscribe`, `publish`, `edit`, `delete` and `grant`
18
+ - (Breaking) Remove `lastUpdated` and `name` columns from `stream list` output
19
+ - (Breaking) Modify configuration management:
20
+ - can read config from `~/.streamr/config/default.json`
21
+ - `--config` argument added
22
+ - `--stg`, `--http-url` and `--ws-url` arguments removed
23
+ - (Breaking) Rename `--config` argument to `--stream-config` in `stream create`
24
+ - (Breaking) Remove `stream list` command
25
+ - Add `stream search` command
26
+ - Bump dependency streamr-client to 6.1.0
27
+ - Bump dependency commander to 8.3.0
11
28
 
12
29
  ## [5.0.0] - 2021-05-05
13
30
  ### Added
package/README.md CHANGED
@@ -1,3 +1,9 @@
1
+ <p align="center">
2
+ <a href="https://streamr.network">
3
+ <img alt="Streamr" src="https://raw.githubusercontent.com/streamr-dev/network-monorepo/main/packages/client/readme-header-img.png" width="1320" />
4
+ </a>
5
+ </p>
6
+
1
7
  # @streamr/cli-tools
2
8
 
3
9
  Command line tool for interacting with [Streamr](https://streamr.network).
@@ -28,12 +34,14 @@ To get a list of all commands simply run `streamr`. To list subcommands run e.g.
28
34
 
29
35
  Run `streamr <command> <subcommand> --help` to get more information about a a command, its options, and so forth.
30
36
 
37
+ If there is a stream parameter in a command, it can be defined as a full id (e.g. `0x1234567890123456789012345678901234567890/foo/bar`) or a path (e.g. `/foo/bar`). If path notation is used, the stream ID is made by prefixing the authenticated Ethereum address (`--private-key <key>`) to the path.
38
+
31
39
  ### subscribe
32
40
  Used to subscribe to a stream and output real-time JSON objects to stdout line-by-line.
33
41
 
34
42
  For example, to subscribe to a public stream such as the tram demo do
35
43
  ```
36
- streamr stream subscribe 7wa7APtlTq6EC5iTCBy6dw
44
+ streamr stream subscribe streamr.eth/demos/helsinki-trams
37
45
  ```
38
46
 
39
47
  To subscribe to a private stream and authenticate with an Ethereum private key:
@@ -42,9 +50,6 @@ To subscribe to a private stream and authenticate with an Ethereum private key:
42
50
  streamr stream subscribe streamId --private-key <key>
43
51
  ```
44
52
 
45
- Flag `--dev` or `--stg` can be enabled for the command to operate on pre-defined development or staging environment. Alternatively, you can give `--ws-url <url>` and `--http-url <url>` to connect to any custom network.
46
-
47
-
48
53
  ### publish
49
54
  Used to publish events to a stream from stdin line-by-line. Each line should be a valid JSON object.
50
55
 
@@ -53,8 +58,6 @@ Example of use:
53
58
  streamr stream publish <streamId> --private-key <key>
54
59
  ```
55
60
 
56
- Flag `--dev` or `--stg` can be enabled for the command to operate on pre-defined development or staging environment.
57
-
58
61
 
59
62
  ### generate
60
63
  Generate random JSON objects to stdout line-by-line.
@@ -79,15 +82,22 @@ streamr stream show <streamId> --private-key <key>
79
82
  ### create
80
83
  Create a new stream
81
84
  ```
82
- streamr stream create <name> --private-key <key>
85
+ streamr stream create <streamId> --private-key <key>
86
+ ```
87
+ E.g.
83
88
  ```
89
+ streamr stream create /foo/bar
90
+ streamr stream create 0x1234567890123456789012345678901234567890/foobar
91
+ streamr stream create yourdomain.ens/foobar
92
+ ```
93
+
84
94
 
85
95
  ### resend
86
96
  Request a resend of historical data printed as JSON objects to stdout line-by-line.
87
97
 
88
98
  For example, to fetch the 10 latest messages of a public stream such as the tram demo do
89
99
  ```
90
- streamr stream resend last 10 7wa7APtlTq6EC5iTCBy6dw
100
+ streamr stream resend last 10 streamr.eth/demos/helsinki-trams
91
101
  ```
92
102
 
93
103
 
@@ -101,8 +111,28 @@ To fetch data between two date-times
101
111
  streamr stream resend range 2019-05-10T17:00:00 2019-05-11T21:00:00 <streamId> --private-key <key>
102
112
  ```
103
113
 
104
- Flag `--dev` or `--stg` can be enabled for the command to operate on pre-defined development or staging environment.
114
+ #### Configuration
105
115
 
116
+ User can specify environment and authentication details with the following command line arguments:
117
+ - `--private-key <key>`, e.g. `--private-key 0x1234567890123456789012345678901234567890123456789012345678901234`
118
+ - `--config <file>`, e.g. `--config foobar.json`
119
+ - `--dev` use the pre-defined [development environment](https://github.com/streamr-dev/streamr-docker-dev)
120
+
121
+ The `--config` argument tries to read a configuration file from the current working directory (either without a file extension, or with `.json` extension added). It also tries to read it from `~/.streamr/config/${id}.json` dotfile.
122
+
123
+ If no `--config` argument is specified, default settings are read from `~/.streamr/config/default.json`, if that file exists.
124
+
125
+ The configuration file is a JSON. It has one root-level property `client`, which contains any configuration properties for the [streamr-client-javascript](https://github.com/streamr-dev/network-monorepo/blob/main/packages/client/) client. Example:
126
+ ```
127
+ {
128
+ "client": {
129
+ "auth": {
130
+ "privateKey": ...
131
+ },
132
+ "publishWithSignature": "always"
133
+ }
134
+ }
135
+ ```
106
136
 
107
137
  ### Examples: Piping with subscribe and publish
108
138
 
@@ -114,7 +144,7 @@ You can pipe the line-by-line JSON objects output by `subscribe` to
114
144
  your program written in any language. Just make the program read JSON objects
115
145
  from stdin.
116
146
  ```
117
- streamr stream subscribe 7wa7APtlTq6EC5iTCBy6dw | ruby calculate-average-speed.rb
147
+ streamr stream subscribe streamr.eth/demos/helsinki-trams | ruby calculate-average-speed.rb
118
148
  ```
119
149
 
120
150
  #### Publishing to a stream from any programming language
@@ -139,16 +169,11 @@ If you have a working stream in production that you'd also like to use in your
139
169
  development environment, you can combine the `subscribe` and `publish` commands to effectively copy
140
170
  the real-time events.
141
171
  ```
142
- streamr stream subscribe 7wa7APtlTq6EC5iTCBy6dw | streamr stream publish --dev <streamId> --private-key <key>
143
- ```
144
-
145
- And the same for staging environment:
146
- ```
147
- streamr stream subscribe 7wa7APtlTq6EC5iTCBy6dw | streamr stream publish --stg <streamId> --private-key <key>
172
+ streamr stream subscribe streamr.eth/demos/helsinki-trams | streamr stream publish --dev <streamId> --private-key <key>
148
173
  ```
149
174
 
150
175
  ## Develop
151
- This project is a thin wrapper around [streamr-client-javascript](https://github.com/streamr-dev/streamr-client-javascript),
176
+ This project is a thin wrapper around [streamr-client-javascript](https://github.com/streamr-dev/network-monorepo/blob/main/packages/client/),
152
177
  which does the heavy lifting, while this project concentrates on CLI concerns: parsing and
153
178
  passing arguments, stdin/stdout, errors, and so forth.
154
179
 
@@ -1,17 +1,47 @@
1
1
  #!/usr/bin/env node
2
- import { Command } from 'commander'
3
- import { generate } from '../src/generate'
4
- import { exitWithHelpIfArgsNotBetween, createFnParseInt } from './common'
5
2
  import pkg from '../package.json'
3
+ import { createFnParseInt } from '../src/common'
4
+ import { createCommand } from '../src/command'
6
5
 
7
- const program = new Command()
8
- program
6
+ // From: https://stackoverflow.com/questions/10726909/random-alpha-numeric-string-in-javascript
7
+ function randomString(
8
+ length: number,
9
+ chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
10
+ ): string {
11
+ let result = ''
12
+ for (let i = length; i > 0; --i) {
13
+ result += chars[Math.floor(Math.random() * chars.length)]
14
+ }
15
+ return result
16
+ }
17
+
18
+ function genArray<T>(size: number, elementFn: () => T): T[] {
19
+ const arr = []
20
+ for (let i=0; i < size; ++i) {
21
+ arr.push(elementFn())
22
+ }
23
+ return arr
24
+ }
25
+
26
+ export const generate = (rate: number): void => {
27
+ setInterval(() => {
28
+ console.info(JSON.stringify({
29
+ someText: randomString(64),
30
+ aNumber: Math.random() * 10000,
31
+ bNumber: Math.random(),
32
+ yesOrNo: Math.random() > 0.5,
33
+ arrayOfStrings: genArray(Math.floor(Math.random() * 20), () => randomString(8)),
34
+ arrayOfIntegers: genArray(Math.floor(Math.random() * 10), () => Math.floor(Math.random() * 100))
35
+
36
+ }))
37
+ }, rate)
38
+ }
39
+
40
+ createCommand()
9
41
  .description('generate and print semi-random JSON data to stdout')
10
42
  .option('-r, --rate <n>', 'rate in milliseconds', createFnParseInt('--rate'), 500)
11
43
  .version(pkg.version)
12
44
  .action((options: any) => {
13
45
  generate(options.rate)
14
46
  })
15
- .parse(process.argv)
16
-
17
- exitWithHelpIfArgsNotBetween(program, 0, 0)
47
+ .parse()
@@ -7,4 +7,4 @@ program
7
7
  .usage('<command> [<args>]')
8
8
  .description('mock-data subcommands')
9
9
  .command('generate', 'generate JSON data')
10
- .parse(process.argv)
10
+ .parse()
@@ -1,30 +1,12 @@
1
1
  #!/usr/bin/env node
2
- import { Command } from 'commander'
3
- import { StreamrClient, Stream } from 'streamr-client'
4
- import {
5
- envOptions,
6
- authOptions,
7
- exitWithHelpIfArgsNotBetween,
8
- formStreamrOptionsWithEnv,
9
- } from './common'
10
- import pkg from '../package.json'
2
+ import '../src/logLevel'
3
+ import StreamrClient from 'streamr-client'
4
+ import { createClientCommand } from '../src/command'
11
5
 
12
- const program = new Command()
13
- program
6
+ createClientCommand(async (client: StreamrClient, storageNodeAddress: string, streamId: string) => {
7
+ const stream = await client.getStream(streamId)
8
+ await stream.addToStorageNode(storageNodeAddress)
9
+ })
14
10
  .arguments('<storageNodeAddress> <streamId>')
15
11
  .description('add stream to a storage node')
16
- authOptions(program)
17
- envOptions(program)
18
- .version(pkg.version)
19
- .action((storageNodeAddress: string, streamId: string, options: any) => {
20
- const client = new StreamrClient(formStreamrOptionsWithEnv(options))
21
- client.getStream(streamId)
22
- .then((stream: Stream) => stream.addToStorageNode(storageNodeAddress))
23
- .catch((err) => {
24
- console.error(err)
25
- process.exit(1)
26
- })
27
- })
28
- .parse(process.argv)
29
-
30
- exitWithHelpIfArgsNotBetween(program, 2, 2)
12
+ .parseAsync()
@@ -1,38 +1,22 @@
1
1
  #!/usr/bin/env node
2
- import { Command } from 'commander'
3
- import { StreamrClient, StreamPart } from 'streamr-client'
4
- import {
5
- envOptions,
6
- authOptions,
7
- exitWithHelpIfArgsNotBetween,
8
- formStreamrOptionsWithEnv,
9
- } from './common'
10
- import pkg from '../package.json'
2
+ import '../src/logLevel'
11
3
  import EasyTable from 'easy-table'
4
+ import StreamrClient from 'streamr-client'
5
+ import { createClientCommand } from '../src/command'
6
+ import { StreamPartIDUtils } from 'streamr-client-protocol'
12
7
 
13
- const program = new Command()
14
- program
8
+ createClientCommand((async (client: StreamrClient, storageNodeAddress: string) => {
9
+ const streamParts = await client.getStreamPartsByStorageNode(storageNodeAddress)
10
+ if (streamParts.length > 0) {
11
+ console.info(EasyTable.print(streamParts.map((streamPartId) => {
12
+ const [streamId, streamPartition] = StreamPartIDUtils.getStreamIDAndPartition(streamPartId)
13
+ return {
14
+ streamId,
15
+ streamPartition
16
+ }
17
+ })))
18
+ }
19
+ }))
15
20
  .arguments('<storageNodeAddress>')
16
- .description('list streams parts in a storage node')
17
- authOptions(program)
18
- envOptions(program)
19
- .version(pkg.version)
20
- .action((storageNodeAddress: string, options: any) => {
21
- const client = new StreamrClient(formStreamrOptionsWithEnv(options))
22
- client.getStreamPartsByStorageNode(storageNodeAddress)
23
- .then((streamParts: StreamPart[]) => {
24
- if (streamParts.length > 0) {
25
- console.info(EasyTable.print(streamParts.map((streamPart: StreamPart) => ({
26
- streamId: streamPart.getStreamId(),
27
- streamPartition: streamPart.getStreamPartition()
28
- }))))
29
- }
30
- return true
31
- }).catch((err) => {
32
- console.error(err)
33
- process.exit(1)
34
- })
35
- })
36
- .parse(process.argv)
37
-
38
- exitWithHelpIfArgsNotBetween(program, 1, 1)
21
+ .description('list stream parts in a storage node')
22
+ .parseAsync()
@@ -1,47 +1,27 @@
1
1
  #!/usr/bin/env node
2
- import { Command } from 'commander'
2
+ import '../src/logLevel'
3
3
  import { StreamrClient } from 'streamr-client'
4
- import {
5
- envOptions,
6
- authOptions,
7
- exitWithHelpIfArgsNotBetween,
8
- formStreamrOptionsWithEnv,
9
- } from './common'
10
- import pkg from '../package.json'
11
4
  import EasyTable from 'easy-table'
5
+ import { createClientCommand } from '../src/command'
12
6
 
13
7
  const getStorageNodes = async (streamId: string | undefined, client: StreamrClient): Promise<string[]> => {
14
8
  if (streamId !== undefined) {
15
9
  const stream = await client.getStream(streamId)
16
- const storageNodes = await stream.getStorageNodes()
17
- return storageNodes.map((storageNode) => storageNode.getAddress())
10
+ return stream.getStorageNodes()
18
11
  } else {
19
- // all storage nodes (currently there is only one)
20
- return [client.options.storageNode.address]
12
+ return client.getAllStorageNodes()
21
13
  }
22
14
  }
23
15
 
24
- const program = new Command()
25
- program
16
+ createClientCommand(async (client: StreamrClient, options: any) => {
17
+ const streamId = options.stream
18
+ const addresses = await getStorageNodes(streamId, client)
19
+ if (addresses.length > 0) {
20
+ console.info(EasyTable.print(addresses.map((address: string) => ({
21
+ address
22
+ }))))
23
+ }
24
+ })
26
25
  .description('fetch a list of storage nodes')
27
26
  .option('-s, --stream <streamId>', 'only storage nodes which store the given stream (needs authentication)')
28
- authOptions(program)
29
- envOptions(program)
30
- .version(pkg.version)
31
- .action((options: any) => {
32
- const client = new StreamrClient(formStreamrOptionsWithEnv(options))
33
- getStorageNodes(options.stream, client).then((addresses: string[]) => {
34
- if (addresses.length > 0) {
35
- console.info(EasyTable.print(addresses.map((address: string) => ({
36
- address
37
- }))))
38
- }
39
- return true
40
- }).catch((err) => {
41
- console.error(err)
42
- process.exit(1)
43
- })
44
- })
45
- .parse(process.argv)
46
-
47
- exitWithHelpIfArgsNotBetween(program, 0, 0)
27
+ .parseAsync()
@@ -1,30 +1,12 @@
1
1
  #!/usr/bin/env node
2
- import { Command } from 'commander'
3
- import { StreamrClient, Stream } from 'streamr-client'
4
- import {
5
- envOptions,
6
- authOptions,
7
- exitWithHelpIfArgsNotBetween,
8
- formStreamrOptionsWithEnv,
9
- } from './common'
10
- import pkg from '../package.json'
2
+ import '../src/logLevel'
3
+ import StreamrClient from 'streamr-client'
4
+ import { createClientCommand } from '../src/command'
11
5
 
12
- const program = new Command()
13
- program
6
+ createClientCommand(async (client: StreamrClient, storageNodeAddress: string, streamId: string) => {
7
+ const stream = await client.getStream(streamId)
8
+ await stream.removeFromStorageNode(storageNodeAddress)
9
+ })
14
10
  .arguments('<storageNodeAddress> <streamId>')
15
11
  .description('remove stream from a storage node')
16
- authOptions(program)
17
- envOptions(program)
18
- .version(pkg.version)
19
- .action((storageNodeAddress: string, streamId: string, options: any) => {
20
- const client = new StreamrClient(formStreamrOptionsWithEnv(options))
21
- client.getStream(streamId)
22
- .then((stream: Stream) => stream.removeFromStorageNode(storageNodeAddress))
23
- .catch((err) => {
24
- console.error(err)
25
- process.exit(1)
26
- })
27
- })
28
- .parse(process.argv)
29
-
30
- exitWithHelpIfArgsNotBetween(program, 2, 2)
12
+ .parseAsync()
@@ -10,4 +10,4 @@ program
10
10
  .command('add-stream', 'add stream')
11
11
  .command('remove-stream', 'remove stream')
12
12
  .command('list-stream-parts', 'list stream parts in a storage node')
13
- .parse(process.argv)
13
+ .parse()
@@ -1,35 +1,23 @@
1
1
  #!/usr/bin/env node
2
- import { Command } from 'commander'
3
- import { create } from '../src/create'
4
- import {
5
- envOptions,
6
- authOptions,
7
- exitWithHelpIfArgsNotBetween,
8
- formStreamrOptionsWithEnv,
9
- createFnParseInt
10
- } from './common'
11
- import pkg from '../package.json'
2
+ import '../src/logLevel'
3
+ import StreamrClient from 'streamr-client'
4
+ import { createFnParseInt } from '../src/common'
5
+ import { createClientCommand } from '../src/command'
12
6
 
13
- const program = new Command()
14
- program
15
- .arguments('<name>')
7
+ createClientCommand(async (client: StreamrClient, streamIdOrPath: string, options: any) => {
8
+ const body: any = {
9
+ id: streamIdOrPath,
10
+ description: options.description,
11
+ config: options.streamConfig,
12
+ partitions: options.partitions
13
+ }
14
+ const stream = await client.createStream(body)
15
+ console.info(JSON.stringify(stream.toObject(), null, 2))
16
+ })
17
+ .arguments('<streamId>')
16
18
  .description('create a new stream')
17
19
  .option('-d, --description <description>', 'define a description')
18
- .option('-c, --config <config>', 'define a configuration as JSON', (s: string) => JSON.parse(s))
20
+ .option('-c, --stream-config <config>', 'define a configuration as JSON', (s: string) => JSON.parse(s))
19
21
  .option('-p, --partitions <count>', 'define a partition count',
20
22
  createFnParseInt('--partitions'))
21
- authOptions(program)
22
- envOptions(program)
23
- .version(pkg.version)
24
- .action((name: string, options: any) => {
25
- const body: any = {
26
- name,
27
- description: options.description,
28
- config: options.config,
29
- partitions: options.partitions
30
- }
31
- create(body, formStreamrOptionsWithEnv(options))
32
- })
33
- .parse(process.argv)
34
-
35
- exitWithHelpIfArgsNotBetween(program, 1, 1)
23
+ .parseAsync()
@@ -1,67 +1,10 @@
1
1
  #!/usr/bin/env node
2
- import { Command } from 'commander'
3
- import {
4
- envOptions,
5
- authOptions,
6
- formStreamrOptionsWithEnv
7
- } from './common'
8
- import pkg from '../package.json'
9
- import { AnonymousStreamPermisson, StreamOperation, StreamrClient, UserStreamPermission } from 'streamr-client'
10
- import EasyTable from 'easy-table'
11
-
12
- const PUBLIC_PERMISSION_ID = 'public'
13
- const OPERATION_PREFIX = 'stream_'
14
-
15
- const getOperation = (id: string) => {
16
- // we support both short ids (e.g. "publish"), and long ids (e.g. "stream_publish")
17
- // the actual StreamOperation constant is the long id string
18
- // backend does the validation of invalid constants
19
- if (!id.startsWith(OPERATION_PREFIX)) {
20
- return (OPERATION_PREFIX + id) as StreamOperation
21
- } else {
22
- return id as StreamOperation
23
- }
24
- }
25
-
26
- const getShortOperationId = (operation: StreamOperation) => {
27
- const longOperationId = operation as string
28
- if (longOperationId.startsWith(OPERATION_PREFIX)) {
29
- return longOperationId.substring(OPERATION_PREFIX.length)
30
- } else {
31
- throw new Error(`Assertion failed: unknown prefix for in ${longOperationId}`)
32
- }
33
- }
34
-
35
- const getTarget = (user: string): string|undefined => {
36
- if (user === PUBLIC_PERMISSION_ID) {
37
- return undefined
38
- } else {
39
- return user
40
- }
41
- }
42
-
43
- const program = new Command()
44
- program
45
- .arguments('<streamId> <user> <operations...>')
46
- .description('grant permission: use keyword "public" as a user to grant a public permission')
47
- authOptions(program)
48
- envOptions(program)
49
- .version(pkg.version)
50
- .action(async (streamId: string, user: string, operationIds: string[], options: any) => {
51
- const operations = operationIds.map((o: string) => getOperation(o))
52
- const target = getTarget(user)
53
- const client = new StreamrClient(formStreamrOptionsWithEnv(options))
54
- const stream = await client.getStream(streamId)
55
- const tasks = operations.map((operation: StreamOperation) => stream.grantPermission(operation, target))
56
- const permissions = await Promise.all(tasks)
57
- console.info(EasyTable.print(permissions.map((permission: UserStreamPermission|AnonymousStreamPermisson) => ({
58
- id: permission.id,
59
- operation: getShortOperationId(permission.operation),
60
- user: (permission as AnonymousStreamPermisson).anonymous ? PUBLIC_PERMISSION_ID : (permission as UserStreamPermission).user
61
- }))))
62
- })
63
- .parseAsync(process.argv)
64
- .catch((e) => {
65
- console.error(e)
66
- process.exit(1)
67
- })
2
+ import '../src/logLevel'
3
+ import { Stream, StreamPermission } from 'streamr-client'
4
+ import { runModifyPermissionsCommand } from '../src/permission'
5
+
6
+ runModifyPermissionsCommand(
7
+ (stream: Stream, permission: StreamPermission, target: string) => stream.grantUserPermission(permission, target),
8
+ (stream: Stream, permission: StreamPermission) => stream.grantPublicPermission(permission),
9
+ 'grant'
10
+ )
@@ -1,29 +1,62 @@
1
1
  #!/usr/bin/env node
2
- import { Command } from 'commander'
2
+ import '../src/logLevel'
3
+ import { Writable } from 'stream'
4
+ import { StreamrClient } from 'streamr-client'
5
+ import { wait } from 'streamr-test-utils'
3
6
  import es from 'event-stream'
4
- import { publishStream } from '../src/publish'
5
- import { envOptions, authOptions, exitWithHelpIfArgsNotBetween, formStreamrOptionsWithEnv } from './common'
6
- import pkg from '../package.json'
7
+ import { createClientCommand } from '../src/command'
7
8
 
8
- const program = new Command()
9
- program
10
- .arguments('<streamId>')
11
- .description('publish to a stream by reading JSON messages from stdin line-by-line')
12
- .option('-k, --partition-key <string>', 'field name in each message to use for assigning the message to a stream partition')
13
- authOptions(program)
14
- envOptions(program)
15
- .version(pkg.version)
16
- .action((streamId: string, options: any) => {
17
- const ps = publishStream(streamId, options.partitionKey, formStreamrOptionsWithEnv(options))
9
+ const publishStream = (
10
+ stream: string,
11
+ partitionKey: string | undefined,
12
+ client: StreamrClient
13
+ ): Writable => {
14
+ const writable = new Writable({
15
+ objectMode: true,
16
+ write: (data: any, _: any, done: any) => {
17
+ let json = null
18
+ // ignore newlines, etc
19
+ if (!data || String(data).trim() === '') {
20
+ done()
21
+ return
22
+ }
23
+ try {
24
+ json = JSON.parse(data)
25
+ } catch (e) {
26
+ console.error(data.toString())
27
+ done(e)
28
+ return
29
+ }
30
+ // @ts-expect-error TODO: the last argument here looks wrong, should be just `partitionKey`?
31
+ client.publish(stream, json, Date.now(), json[partitionKey]).then(
32
+ () => done(),
33
+ (err) => done(err)
34
+ )
35
+ }
36
+ })
37
+ return writable
38
+ }
39
+
40
+ createClientCommand(async (client: StreamrClient, streamId: string, options: any) => {
41
+ const ps = publishStream(streamId, options.partitionKey, client)
42
+ return new Promise((resolve, reject) => {
18
43
  process.stdin
19
44
  .pipe(es.split())
20
45
  .pipe(ps)
21
- .on('error', (err: any) => {
22
- console.error(err)
23
- process.exit(1)
24
- // process.stdin.pipe(ps) recover pipe to continue execution
46
+ .once('finish', async () => {
47
+ // We need to wait some time because the client.publish() may resolve the promise
48
+ // before the node has propagated the message. That may happend if the node
49
+ // has not yet connected to Tracker when client.publish() is called. In that case
50
+ // the message is put to the propagation queue (activeTaskStore.add call in
51
+ // network Propagation.ts:59) and the client.publish() promise resolves immeditatelly.
52
+ // TODO Remove this wait when NET-604 has been resolved
53
+ await wait(2000)
54
+ resolve(undefined)
25
55
  })
56
+ .once('error', (err: any) => reject(err) )
26
57
  })
27
- .parse(process.argv)
28
-
29
- exitWithHelpIfArgsNotBetween(program, 1, 1)
58
+ })
59
+ .arguments('<streamId>')
60
+ .description('publish to a stream by reading JSON messages from stdin line-by-line')
61
+ .option('-k, --partition-key <string>', 'field name in each message to use for assigning the message to a stream partition')
62
+ .parseAsync()