@streamr/cli-tools 6.0.0-alpha.0 → 6.0.0-alpha.4

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 (154) hide show
  1. package/.eslintignore +0 -1
  2. package/CHANGELOG.md +18 -2
  3. package/README.md +22 -12
  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 -28
  7. package/bin/streamr-storage-node-list-stream-parts.ts +18 -34
  8. package/bin/streamr-storage-node-list.ts +14 -39
  9. package/bin/streamr-storage-node-remove-stream.ts +8 -28
  10. package/bin/streamr-storage-node.ts +1 -1
  11. package/bin/streamr-stream-create.ts +16 -28
  12. package/bin/streamr-stream-grant-permission.ts +9 -68
  13. package/bin/streamr-stream-publish.ts +54 -22
  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 -95
  18. package/bin/streamr-stream-revoke-permission.ts +8 -26
  19. package/bin/streamr-stream-search.ts +17 -0
  20. package/bin/streamr-stream-show.ts +16 -17
  21. package/bin/streamr-stream-subscribe.ts +16 -17
  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 +34 -8
  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 -25
  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 -32
  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 -25
  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 +13 -22
  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 -64
  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 -24
  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 -90
  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 -22
  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 -16
  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 +16 -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-whoami.d.ts +2 -0
  79. package/dist/bin/streamr-whoami.js +11 -0
  80. package/dist/bin/streamr-whoami.js.map +1 -0
  81. package/dist/bin/streamr.js +1 -2
  82. package/dist/bin/streamr.js.map +1 -1
  83. package/dist/package.json +14 -11
  84. package/dist/src/client.d.ts +3 -0
  85. package/dist/src/client.js +33 -0
  86. package/dist/src/client.js.map +1 -0
  87. package/dist/src/command.d.ts +8 -0
  88. package/dist/src/command.js +45 -0
  89. package/dist/src/command.js.map +1 -0
  90. package/dist/src/common.d.ts +6 -0
  91. package/dist/src/common.js +15 -0
  92. package/dist/src/common.js.map +1 -0
  93. package/dist/src/config.d.ts +6 -0
  94. package/dist/src/config.js +58 -0
  95. package/dist/src/config.js.map +1 -0
  96. package/dist/src/logLevel.d.ts +0 -0
  97. package/dist/src/logLevel.js +11 -0
  98. package/dist/src/logLevel.js.map +1 -0
  99. package/dist/src/permission.d.ts +4 -0
  100. package/dist/src/permission.js +48 -0
  101. package/dist/src/permission.js.map +1 -0
  102. package/dist/src/resend.d.ts +3 -4
  103. package/dist/src/resend.js +10 -6
  104. package/dist/src/resend.js.map +1 -1
  105. package/dist/tsconfig.tsbuildinfo +1 -1
  106. package/package.json +14 -11
  107. package/src/client.ts +32 -0
  108. package/src/command.ts +45 -0
  109. package/src/common.ts +16 -0
  110. package/src/config.ts +56 -0
  111. package/src/logLevel.ts +9 -0
  112. package/src/permission.ts +49 -0
  113. package/src/resend.ts +17 -8
  114. package/tsconfig.json +13 -5
  115. package/bin/common.ts +0 -94
  116. package/bin/streamr-misc-get-session-token.ts +0 -33
  117. package/bin/streamr-misc.ts +0 -10
  118. package/bin/streamr-stream-list.ts +0 -33
  119. package/dist/bin/common.d.ts +0 -16
  120. package/dist/bin/common.js +0 -80
  121. package/dist/bin/common.js.map +0 -1
  122. package/dist/bin/streamr-misc-get-session-token.d.ts +0 -2
  123. package/dist/bin/streamr-misc-get-session-token.js +0 -33
  124. package/dist/bin/streamr-misc-get-session-token.js.map +0 -1
  125. package/dist/bin/streamr-misc.d.ts +0 -2
  126. package/dist/bin/streamr-misc.js +0 -15
  127. package/dist/bin/streamr-misc.js.map +0 -1
  128. package/dist/bin/streamr-stream-list.d.ts +0 -2
  129. package/dist/bin/streamr-stream-list.js +0 -37
  130. package/dist/bin/streamr-stream-list.js.map +0 -1
  131. package/dist/src/create.d.ts +0 -2
  132. package/dist/src/create.js +0 -20
  133. package/dist/src/create.js.map +0 -1
  134. package/dist/src/generate.d.ts +0 -1
  135. package/dist/src/generate.js +0 -32
  136. package/dist/src/generate.js.map +0 -1
  137. package/dist/src/list.d.ts +0 -2
  138. package/dist/src/list.js +0 -29
  139. package/dist/src/list.js.map +0 -1
  140. package/dist/src/publish.d.ts +0 -3
  141. package/dist/src/publish.js +0 -35
  142. package/dist/src/publish.js.map +0 -1
  143. package/dist/src/show.d.ts +0 -2
  144. package/dist/src/show.js +0 -23
  145. package/dist/src/show.js.map +0 -1
  146. package/dist/src/subscribe.d.ts +0 -2
  147. package/dist/src/subscribe.js +0 -13
  148. package/dist/src/subscribe.js.map +0 -1
  149. package/src/create.ts +0 -19
  150. package/src/generate.ts +0 -33
  151. package/src/list.ts +0 -22
  152. package/src/publish.ts +0 -41
  153. package/src/show.ts +0 -23
  154. 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,9 +7,25 @@ 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`
11
- - (Breaking) `streamr stream create` argument is a stream ID, not a name
10
+ - (Breaking) Remove `--msg-chain-id` parameter from `stream resend from`
11
+ - (Breaking) `stream create` argument is a stream ID, not a name
12
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
+ - (Breaking) Rename `--partition-key` argument to `--partition-key-field` in `stream publish`
27
+ - Bump dependency streamr-client to 6.1.0
28
+ - Bump dependency commander to 8.3.0
13
29
 
14
30
  ## [5.0.0] - 2021-05-05
15
31
  ### Added
package/README.md CHANGED
@@ -50,9 +50,6 @@ To subscribe to a private stream and authenticate with an Ethereum private key:
50
50
  streamr stream subscribe streamId --private-key <key>
51
51
  ```
52
52
 
53
- 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.
54
-
55
-
56
53
  ### publish
57
54
  Used to publish events to a stream from stdin line-by-line. Each line should be a valid JSON object.
58
55
 
@@ -61,8 +58,6 @@ Example of use:
61
58
  streamr stream publish <streamId> --private-key <key>
62
59
  ```
63
60
 
64
- Flag `--dev` or `--stg` can be enabled for the command to operate on pre-defined development or staging environment.
65
-
66
61
 
67
62
  ### generate
68
63
  Generate random JSON objects to stdout line-by-line.
@@ -116,8 +111,28 @@ To fetch data between two date-times
116
111
  streamr stream resend range 2019-05-10T17:00:00 2019-05-11T21:00:00 <streamId> --private-key <key>
117
112
  ```
118
113
 
119
- Flag `--dev` or `--stg` can be enabled for the command to operate on pre-defined development or staging environment.
114
+ #### Configuration
120
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
+ ```
121
136
 
122
137
  ### Examples: Piping with subscribe and publish
123
138
 
@@ -157,13 +172,8 @@ the real-time events.
157
172
  streamr stream subscribe streamr.eth/demos/helsinki-trams | streamr stream publish --dev <streamId> --private-key <key>
158
173
  ```
159
174
 
160
- And the same for staging environment:
161
- ```
162
- streamr stream subscribe streamr.eth/demos/helsinki-trams | streamr stream publish --stg <streamId> --private-key <key>
163
- ```
164
-
165
175
  ## Develop
166
- 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/),
167
177
  which does the heavy lifting, while this project concentrates on CLI concerns: parsing and
168
178
  passing arguments, stdin/stdout, errors, and so forth.
169
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,32 +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
- getStreamId,
10
- } from './common'
11
- import pkg from '../package.json'
2
+ import '../src/logLevel'
3
+ import StreamrClient from 'streamr-client'
4
+ import { createClientCommand } from '../src/command'
12
5
 
13
- const program = new Command()
14
- program
6
+ createClientCommand(async (client: StreamrClient, storageNodeAddress: string, streamId: string) => {
7
+ const stream = await client.getStream(streamId)
8
+ await stream.addToStorageNode(storageNodeAddress)
9
+ })
15
10
  .arguments('<storageNodeAddress> <streamId>')
16
11
  .description('add stream to a storage node')
17
- authOptions(program)
18
- envOptions(program)
19
- .version(pkg.version)
20
- .action((storageNodeAddress: string, streamIdOrPath: string, options: any) => {
21
- const client = new StreamrClient(formStreamrOptionsWithEnv(options))
22
- const streamId = getStreamId(streamIdOrPath, options)!
23
- client.getStream(streamId)
24
- .then((stream: Stream) => stream.addToStorageNode(storageNodeAddress))
25
- .catch((err) => {
26
- console.error(err)
27
- process.exit(1)
28
- })
29
- })
30
- .parse(process.argv)
31
-
32
- exitWithHelpIfArgsNotBetween(program, 2, 2)
12
+ .parseAsync()
@@ -1,38 +1,22 @@
1
1
  #!/usr/bin/env node
2
- import { Command } from 'commander'
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'
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) => {
24
- if (streamParts.length > 0) {
25
- console.info(EasyTable.print(streamParts.map(({ streamId, streamPartition }) => ({
26
- streamId,
27
- streamPartition,
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,52 +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
- getStreamId,
10
- } from './common'
11
- import pkg from '../package.json'
12
4
  import EasyTable from 'easy-table'
5
+ import { createClientCommand } from '../src/command'
13
6
 
14
7
  const getStorageNodes = async (streamId: string | undefined, client: StreamrClient): Promise<string[]> => {
15
8
  if (streamId !== undefined) {
16
9
  const stream = await client.getStream(streamId)
17
- const storageNodes = await stream.getStorageNodes()
18
- return storageNodes.map((storageNode) => storageNode.address)
10
+ return stream.getStorageNodes()
19
11
  } else {
20
- // all storage nodes (currently there is only one)
21
- const nodes = await client.getNodes()
22
- return nodes.map((n) => n.address)
12
+ return client.getAllStorageNodes()
23
13
  }
24
14
  }
25
15
 
26
- const program = new Command()
27
- 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
+ })
28
25
  .description('fetch a list of storage nodes')
29
26
  .option('-s, --stream <streamId>', 'only storage nodes which store the given stream (needs authentication)')
30
-
31
- authOptions(program)
32
-
33
- envOptions(program)
34
- .version(pkg.version)
35
- .action((options: any) => {
36
- const client = new StreamrClient(formStreamrOptionsWithEnv(options))
37
- const streamId = getStreamId(options.stream, options)
38
- getStorageNodes(streamId, client).then((addresses: string[]) => {
39
- if (addresses.length > 0) {
40
- console.info(EasyTable.print(addresses.map((address: string) => ({
41
- address
42
- }))))
43
- }
44
- return true
45
- }).catch((err) => {
46
- console.error(err)
47
- process.exit(1)
48
- })
49
- })
50
- .parse(process.argv)
51
-
52
- exitWithHelpIfArgsNotBetween(program, 0, 0)
27
+ .parseAsync()
@@ -1,32 +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
- getStreamId,
10
- } from './common'
11
- import pkg from '../package.json'
2
+ import '../src/logLevel'
3
+ import StreamrClient from 'streamr-client'
4
+ import { createClientCommand } from '../src/command'
12
5
 
13
- const program = new Command()
14
- program
6
+ createClientCommand(async (client: StreamrClient, storageNodeAddress: string, streamId: string) => {
7
+ const stream = await client.getStream(streamId)
8
+ await stream.removeFromStorageNode(storageNodeAddress)
9
+ })
15
10
  .arguments('<storageNodeAddress> <streamId>')
16
11
  .description('remove stream from a storage node')
17
- authOptions(program)
18
- envOptions(program)
19
- .version(pkg.version)
20
- .action((storageNodeAddress: string, streamIdOrPath: string, options: any) => {
21
- const client = new StreamrClient(formStreamrOptionsWithEnv(options))
22
- const streamId = getStreamId(streamIdOrPath, options)!
23
- client.getStream(streamId)
24
- .then((stream: Stream) => stream.removeFromStorageNode(storageNodeAddress))
25
- .catch((err) => {
26
- console.error(err)
27
- process.exit(1)
28
- })
29
- })
30
- .parse(process.argv)
31
-
32
- 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
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
+ })
15
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((streamIdOrPath: string, options: any) => {
25
- const body: any = {
26
- id: streamIdOrPath,
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,69 +1,10 @@
1
1
  #!/usr/bin/env node
2
- import { Command } from 'commander'
3
- import {
4
- envOptions,
5
- authOptions,
6
- formStreamrOptionsWithEnv,
7
- getStreamId
8
- } from './common'
9
- import pkg from '../package.json'
10
- import { AnonymousStreamPermisson, StreamOperation, StreamrClient, UserStreamPermission } from 'streamr-client'
11
- import EasyTable from 'easy-table'
12
-
13
- const PUBLIC_PERMISSION_ID = 'public'
14
- const OPERATION_PREFIX = 'stream_'
15
-
16
- const getOperation = (id: string) => {
17
- // we support both short ids (e.g. "publish"), and long ids (e.g. "stream_publish")
18
- // the actual StreamOperation constant is the long id string
19
- // backend does the validation of invalid constants
20
- if (!id.startsWith(OPERATION_PREFIX)) {
21
- return (OPERATION_PREFIX + id) as StreamOperation
22
- } else {
23
- return id as StreamOperation
24
- }
25
- }
26
-
27
- const getShortOperationId = (operation: StreamOperation) => {
28
- const longOperationId = operation as string
29
- if (longOperationId.startsWith(OPERATION_PREFIX)) {
30
- return longOperationId.substring(OPERATION_PREFIX.length)
31
- } else {
32
- throw new Error(`Assertion failed: unknown prefix for in ${longOperationId}`)
33
- }
34
- }
35
-
36
- const getTarget = (user: string): string|undefined => {
37
- if (user === PUBLIC_PERMISSION_ID) {
38
- return undefined
39
- } else {
40
- return user
41
- }
42
- }
43
-
44
- const program = new Command()
45
- program
46
- .arguments('<streamId> <user> <operations...>')
47
- .description('grant permission: use keyword "public" as a user to grant a public permission')
48
- authOptions(program)
49
- envOptions(program)
50
- .version(pkg.version)
51
- .action(async (streamIdOrPath: string, user: string, operationIds: string[], options: any) => {
52
- const operations = operationIds.map((o: string) => getOperation(o))
53
- const target = getTarget(user)
54
- const client = new StreamrClient(formStreamrOptionsWithEnv(options))
55
- const streamId = getStreamId(streamIdOrPath, options)!
56
- const stream = await client.getStream(streamId)
57
- const tasks = operations.map((operation: StreamOperation) => stream.grantPermission(operation, target))
58
- const permissions = await Promise.all(tasks)
59
- console.info(EasyTable.print(permissions.map((permission: UserStreamPermission|AnonymousStreamPermisson) => ({
60
- id: permission.id,
61
- operation: getShortOperationId(permission.operation),
62
- user: (permission as AnonymousStreamPermisson).anonymous ? PUBLIC_PERMISSION_ID : (permission as UserStreamPermission).user
63
- }))))
64
- })
65
- .parseAsync(process.argv)
66
- .catch((e) => {
67
- console.error(e)
68
- process.exit(1)
69
- })
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,30 +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, getStreamId } 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((streamIdOrPath: string, options: any) => {
17
- const streamId = getStreamId(streamIdOrPath, options)!
18
- const ps = publishStream(streamId, options.partitionKey, formStreamrOptionsWithEnv(options))
9
+ const publishStream = (
10
+ stream: string,
11
+ partitionKeyField: 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
+ const partitionKey = (partitionKeyField !== undefined) ? json[partitionKeyField] : undefined
31
+ client.publish(stream, json, Date.now(), 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.partitionKeyField, client)
42
+ return new Promise((resolve, reject) => {
19
43
  process.stdin
20
44
  .pipe(es.split())
21
45
  .pipe(ps)
22
- .on('error', (err: any) => {
23
- console.error(err)
24
- process.exit(1)
25
- // 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)
26
55
  })
56
+ .once('error', (err: any) => reject(err) )
27
57
  })
28
- .parse(process.argv)
29
-
30
- 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-field <string>', 'field name in each message to use for assigning the message to a stream partition')
62
+ .parseAsync()
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env node
2
+ import '../src/logLevel'
3
+ import StreamrClient from 'streamr-client'
4
+ import { createClientCommand } from '../src/command'
5
+ import { resend } from '../src/resend'
6
+
7
+ createClientCommand(async (client: StreamrClient, from: string, streamId: string, options: any) => {
8
+ const resendOptions = {
9
+ from: {
10
+ timestamp: Date.parse(from),
11
+ sequenceNumber: 0
12
+ },
13
+ publisherId: options.publisherId
14
+ }
15
+ await resend(streamId, resendOptions, client, options.subscribe)
16
+ }, {
17
+ clientOptionsFactory: (options) => ({
18
+ orderMessages: !options.disableOrdering
19
+ })
20
+ })
21
+ .arguments('<from> <streamId>')
22
+ .description('request messages starting from given date-time (format: "YYYY-MM-DDTHH:mm:ss.sssZ")')
23
+ .option('--publisher-id <string>', 'filter results by publisher')
24
+ .option('-d, --disable-ordering', 'disable ordering of messages by OrderingUtil', false)
25
+ .option('-s, --subscribe', 'subscribe in addition to resend', false)
26
+ .parseAsync()
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env node
2
+ import '../src/logLevel'
3
+ import StreamrClient from 'streamr-client'
4
+ import { createClientCommand } from '../src/command'
5
+ import { resend } from '../src/resend'
6
+
7
+ createClientCommand(async (client: StreamrClient, n: string, streamId: string, options: any) => {
8
+ if (isNaN(n as any)) {
9
+ console.error('argument n is not a number')
10
+ process.exit(1)
11
+ }
12
+ const resendOptions = {
13
+ last: parseInt(n)
14
+ }
15
+ await resend(streamId, resendOptions, client, options.subscribe)
16
+ }, {
17
+ clientOptionsFactory: (options) => ({
18
+ orderMessages: !options.disableOrdering
19
+ })
20
+ })
21
+ .arguments('<n> <streamId>')
22
+ .description('request last N messages')
23
+ .option('-d, --disable-ordering', 'disable ordering of messages by OrderingUtil', false)
24
+ .option('-s, --subscribe', 'subscribe in addition to resend', false)
25
+ .parseAsync()