Skip to content
This repository was archived by the owner on Dec 21, 2021. It is now read-only.

Commit e780937

Browse files
authored
Merge pull request #201 from streamr-dev/typescript-tweaks
Typescript tweaks
2 parents aaed908 + 219a596 commit e780937

27 files changed

+2163
-2948
lines changed

.eslintrc.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ module.exports = {
66
extends: [
77
'streamr-nodejs'
88
],
9+
parserOptions: {
10+
ecmaVersion: 2020,
11+
ecmaFeatures: {
12+
modules: true
13+
}
14+
},
15+
env: {
16+
browser: true,
17+
es6: true
18+
},
919
rules: {
1020
'max-len': ['warn', {
1121
code: 150
@@ -43,8 +53,6 @@ module.exports = {
4353
'@typescript-eslint/no-shadow': 'error',
4454
'no-unused-vars': 'off',
4555
'@typescript-eslint/no-unused-vars': ['error'],
46-
'no-else-return': 'off',
47-
'no-return-await': 'off'
4856
},
4957
settings: {
5058
'import/resolver': {

jest.config.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ module.exports = {
7676
// moduleNameMapper: {},
7777

7878
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
79-
// modulePathIgnorePatterns: [],
79+
modulePathIgnorePatterns: [
80+
'<rootDir>/dist',
81+
],
8082

8183
// Activates notifications for test results
8284
// notify: false,
@@ -85,7 +87,7 @@ module.exports = {
8587
// notifyMode: "always",
8688

8789
// A preset that is used as a base for Jest's configuration
88-
// preset: null,
90+
preset: 'ts-jest',
8991

9092
// Run tests from one or more projects
9193
// projects: null,
@@ -170,9 +172,9 @@ module.exports = {
170172
},
171173

172174
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
173-
// transformIgnorePatterns: [
174-
// "/node_modules/"
175-
// ],
175+
transformIgnorePatterns: [
176+
'/node_modules/(?!quick-lru)', // quick-lru is esm
177+
],
176178

177179
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
178180
// unmockedModulePathPatterns: undefined,

package-lock.json

Lines changed: 1430 additions & 2500 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,30 @@
66
"type": "git",
77
"url": "git://github.com/streamr-dev/streamr-client.git"
88
},
9-
"main": "dist/streamr-client.nodejs.js",
10-
"browser": "dist/streamr-client.web.min.js",
119
"types": "dist/types/src/StreamrClient.d.ts",
10+
"main": "dist/node/src/StreamrClient.js",
11+
"browser": "dist/streamr-client.web.min.js",
1212
"directories": {
1313
"example": "examples",
1414
"test": "test"
1515
},
1616
"scripts": {
17-
"build": "rm -rf dist; NODE_ENV=production webpack --mode=production --progress && npm run build:types",
18-
"build:types": "tsc --emitDeclarationOnly",
17+
"build": "rm -rf dist; NODE_ENV=production webpack --mode=production --progress && npm run build-node && npm run build:types",
18+
"build-node": "tsc --incremental --project ./tsconfig.node.json",
19+
"watch-node": "tsc --incremental --watch --project ./tsconfig.node.json",
20+
"build:types": "tsc --incremental --emitDeclarationOnly",
1921
"benchmarks": "node test/benchmarks/publish.js && node test/benchmarks/subscribe.js",
2022
"prebuild-benchmark": "npm run build -- --config-name=node-lib",
2123
"build-benchmark": "npm run benchmarks",
2224
"benchmark-build": "npm run build-benchmark",
2325
"benchmark": "npm run benchmarks",
2426
"prepack": "npm run build",
2527
"prebuild": "npm run eslint -- --cache",
26-
"dev": "webpack --progress --colors --watch --mode=development",
28+
"watch": "webpack --progress --watch --mode=development",
2729
"eslint": "eslint --cache-location=node_modules/.cache/.eslintcache/ '*/**/*.{js,ts}'",
2830
"test": "jest --detectOpenHandles",
2931
"test-unit": "jest test/unit --detectOpenHandles",
30-
"test-types": "tsc --noEmit --project ./tsconfig.test.json",
32+
"test-types": "tsc --noEmit --incremental --project ./tsconfig.test.json",
3133
"coverage": "jest --coverage",
3234
"test-integration": "jest --forceExit test/integration",
3335
"test-integration-no-resend": "jest --forceExit --testTimeout=10000 --testPathIgnorePatterns='resend|Resend' --testNamePattern='^((?!(resend|Resend|resent|Resent|gap|Gap)).)*$' test/integration/*.test.js",
@@ -45,62 +47,73 @@
4547
"author": "Streamr",
4648
"license": "Apache-2.0",
4749
"devDependencies": {
48-
"@babel/cli": "^7.12.10",
49-
"@babel/core": "^7.12.10",
50-
"@babel/plugin-proposal-class-properties": "^7.12.1",
51-
"@babel/plugin-transform-classes": "^7.12.1",
52-
"@babel/plugin-transform-modules-commonjs": "^7.12.1",
53-
"@babel/plugin-transform-runtime": "^7.12.15",
54-
"@babel/preset-env": "^7.12.11",
55-
"@babel/preset-typescript": "^7.12.13",
50+
"@babel/cli": "^7.12.17",
51+
"@babel/core": "^7.12.17",
52+
"@babel/helper-call-delegate": "^7.12.13",
53+
"@babel/plugin-proposal-class-properties": "^7.12.13",
54+
"@babel/plugin-transform-classes": "^7.12.13",
55+
"@babel/plugin-transform-modules-commonjs": "^7.12.13",
56+
"@babel/plugin-transform-runtime": "^7.12.17",
57+
"@babel/preset-env": "^7.12.17",
58+
"@babel/preset-typescript": "^7.12.17",
59+
"@tsconfig/node14": "^1.0.0",
5660
"@types/debug": "^4.1.5",
5761
"@types/jest": "^26.0.20",
62+
"@types/lodash.uniqueid": "^4.0.6",
63+
"@types/node": "^14.14.31",
5864
"@types/qs": "^6.9.5",
65+
"@types/uuid": "^8.3.0",
5966
"@typescript-eslint/eslint-plugin": "^4.15.1",
6067
"@typescript-eslint/parser": "^4.15.1",
61-
"async-mutex": "^0.2.6",
68+
"async-mutex": "^0.3.0",
6269
"babel-loader": "^8.2.2",
6370
"babel-plugin-add-module-exports": "^1.0.4",
6471
"babel-plugin-transform-class-properties": "^6.24.1",
6572
"benchmark": "^2.1.4",
6673
"buffer": "^6.0.3",
6774
"chromedriver": "^88.0.0",
68-
"core-js": "^3.8.3",
75+
"core-js": "^3.9.0",
76+
"crypto-browserify": "^3.12.0",
6977
"eslint": "^7.20.0",
7078
"eslint-config-airbnb": "^18.2.1",
7179
"eslint-config-streamr-nodejs": "^1.3.0",
7280
"eslint-loader": "^4.0.2",
7381
"eslint-plugin-import": "^2.22.1",
74-
"eslint-plugin-promise": "^4.2.1",
75-
"ethers": "^5.0.26",
82+
"eslint-plugin-promise": "^4.3.1",
83+
"ethers": "^5.0.31",
7684
"express": "^4.17.1",
7785
"geckodriver": "^1.22.1",
7886
"git-revision-webpack-plugin": "^3.0.6",
7987
"jest": "^26.6.3",
8088
"jest-circus": "^26.6.3",
8189
"nightwatch": "^1.5.1",
90+
"process": "^0.11.10",
8291
"sinon": "^9.2.4",
83-
"streamr-test-utils": "^1.3.0",
84-
"terser-webpack-plugin": "^4.2.3",
85-
"webpack": "^4.46.0",
92+
"streamr-test-utils": "^1.3.1",
93+
"terser-webpack-plugin": "^5.1.1",
94+
"ts-jest": "^26.5.1",
95+
"ts-loader": "^8.0.17",
96+
"typescript": "^4.1.5",
97+
"util": "^0.12.3",
98+
"webpack": "^5.23.0",
8699
"webpack-bundle-analyzer": "^4.4.0",
87-
"webpack-cli": "^4.4.0",
100+
"webpack-cli": "^4.5.0",
88101
"webpack-merge": "^5.7.3"
89102
},
90103
"#IMPORTANT": "babel-runtime must be in dependencies, not devDependencies",
91104
"dependencies": {
105+
"@babel/runtime": "^7.12.18",
106+
"@babel/runtime-corejs3": "^7.12.18",
92107
"@ethersproject/abi": "^5.0.12",
93-
"@babel/runtime": "^7.12.13",
94-
"@babel/runtime-corejs3": "^7.12.13",
95-
"@ethersproject/address": "^5.0.9",
96-
"@ethersproject/bignumber": "^5.0.13",
97-
"@ethersproject/bytes": "^5.0.9",
98-
"@ethersproject/contracts": "^5.0.9",
99-
"@ethersproject/keccak256": "^5.0.7",
100-
"@ethersproject/providers": "^5.0.19",
101-
"@ethersproject/sha2": "^5.0.7",
102-
"@ethersproject/transactions": "^5.0.9",
103-
"@ethersproject/wallet": "^5.0.10",
108+
"@ethersproject/address": "^5.0.10",
109+
"@ethersproject/bignumber": "^5.0.14",
110+
"@ethersproject/bytes": "^5.0.10",
111+
"@ethersproject/contracts": "^5.0.11",
112+
"@ethersproject/keccak256": "^5.0.8",
113+
"@ethersproject/providers": "^5.0.23",
114+
"@ethersproject/sha2": "^5.0.8",
115+
"@ethersproject/transactions": "^5.0.10",
116+
"@ethersproject/wallet": "^5.0.11",
104117
"debug": "^4.3.2",
105118
"eventemitter3": "^4.0.7",
106119
"lodash.uniqueid": "^4.0.1",
@@ -114,13 +127,13 @@
114127
"p-queue": "^6.6.2",
115128
"promise-memoize": "^1.2.1",
116129
"qs": "^6.9.6",
117-
"quick-lru": "^5.1.1",
130+
"quick-lru": "^6.0.0",
118131
"readable-stream": "^3.6.0",
119132
"streamr-client-protocol": "^8.0.0-beta.2",
120-
"typescript": "^4.1.4",
133+
"ts-toolbelt": "^9.3.12",
121134
"uuid": "^8.3.2",
122135
"webpack-node-externals": "^2.5.2",
123-
"ws": "^7.4.2"
136+
"ws": "^7.4.3"
124137
},
125138
"optionalDependencies": {
126139
"bufferutil": "^4.0.3",

src/Config.ts

Lines changed: 71 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,64 @@
11
import qs from 'qs'
2-
import { ControlLayer, MessageLayer } from 'streamr-client-protocol'
32
import Debug from 'debug'
4-
3+
import { ControlLayer, MessageLayer } from 'streamr-client-protocol'
4+
import { ExternalProvider, JsonRpcFetchFunc } from '@ethersproject/providers'
5+
import { BigNumber } from '@ethersproject/bignumber'
6+
import { O } from 'ts-toolbelt'
57
import { getVersionString, counterId } from './utils'
6-
import { StreamrClientOptions } from './StreamrClient'
8+
import { Todo } from './types'
9+
10+
export type EthereumConfig = ExternalProvider|JsonRpcFetchFunc
11+
12+
export type StreamrClientOptions = {
13+
id?: string
14+
debug?: Debug.Debugger,
15+
auth?: {
16+
privateKey?: string
17+
ethereum?: EthereumConfig
18+
apiKey?: string
19+
username?: string
20+
password?: string
21+
}
22+
url?: string
23+
restUrl?: string
24+
streamrNodeAddress?: string
25+
autoConnect?: boolean
26+
autoDisconnect?: boolean
27+
orderMessages?: boolean,
28+
retryResendAfter?: number,
29+
gapFillTimeout?: number,
30+
maxGapRequests?: number,
31+
maxPublishQueueSize?: number,
32+
publishWithSignature?: Todo,
33+
verifySignatures?: Todo,
34+
publisherStoreKeyHistory?: boolean,
35+
groupKeys?: Todo
36+
keyExchange?: Todo
37+
mainnet?: Todo
38+
sidechain?: {
39+
url?: string
40+
},
41+
dataUnion?: string
42+
tokenAddress?: string,
43+
minimumWithdrawTokenWei?: BigNumber|number|string,
44+
sidechainTokenAddress?: string
45+
factoryMainnetAddress?: string
46+
factorySidechainAddress?: string
47+
payForSignatureTransport?: boolean
48+
cache?: {
49+
maxSize?: number,
50+
maxAge?: number
51+
}
52+
}
753

54+
export type StreamrClientConfig = O.Compulsory<StreamrClientOptions, 'url' | 'restUrl'>
855
const { ControlMessage } = ControlLayer
956
const { StreamMessage } = MessageLayer
1057

1158
export default function ClientConfig(opts: Partial<StreamrClientOptions> = {}) {
1259
const { id = counterId('StreamrClient') } = opts
1360

14-
const options: StreamrClientOptions = {
61+
const defaults = {
1562
debug: Debug(id),
1663
// Authentication: identity used by this StreamrClient instance
1764
auth: {}, // can contain member privateKey or (window.)ethereum
@@ -39,23 +86,30 @@ export default function ClientConfig(opts: Partial<StreamrClientOptions> = {}) {
3986

4087
// Ethereum and Data Union related options
4188
// For ethers.js provider params, see https://docs.ethers.io/ethers.js/v5-beta/api-providers.html#provider
42-
mainnet: null, // Default to ethers.js default provider settings
89+
mainnet: undefined, // Default to ethers.js default provider settings
4390
sidechain: {
44-
// @ts-expect-error
45-
url: null, // TODO: add our default public service sidechain node, also find good PoA params below
91+
url: undefined, // TODO: add our default public service sidechain node, also find good PoA params below
4692
// timeout:
4793
// pollingInterval:
4894
},
4995
tokenAddress: '0x0Cf0Ee63788A0849fE5297F3407f701E122cC023',
5096
minimumWithdrawTokenWei: '1000000', // Threshold value set in AMB configs, smallest token amount to pass over the bridge
51-
factoryMainnetAddress: 'TODO', // TODO define this value when we know it // Data Union factory that creates a new Data Union
52-
factorySidechainAddress: 'TODO', // TODO define this value when we know it
97+
sidechainTokenAddress: undefined, // TODO // sidechain token
98+
factoryMainnetAddress: undefined, // TODO // Data Union factory that creates a new Data Union
99+
factorySidechainAddress: undefined,
53100
payForSignatureTransport: true, // someone must pay for transporting the withdraw tx to mainnet, either us or bridge operator
54-
...opts,
55101
cache: {
56102
maxSize: 10000,
57103
maxAge: 30 * 60 * 1000, // 30 minutes
104+
}
105+
}
106+
107+
const options: StreamrClientConfig = {
108+
...defaults,
109+
...opts,
110+
cache: {
58111
...opts.cache,
112+
...defaults.cache,
59113
}
60114
}
61115

@@ -91,8 +145,13 @@ export default function ClientConfig(opts: Partial<StreamrClientOptions> = {}) {
91145
options.auth.apiKey = options.apiKey
92146
}
93147

94-
if (options.auth!.privateKey && !options.auth!.privateKey.startsWith('0x')) {
95-
options.auth!.privateKey = `0x${options.auth!.privateKey}`
148+
options.auth = options.auth || {}
149+
150+
if ('privateKey' in options.auth) {
151+
const { privateKey } = options.auth
152+
if (typeof privateKey === 'string' && !privateKey.startsWith('0x')) {
153+
options.auth.privateKey = `0x${options.auth!.privateKey}`
154+
}
96155
}
97156

98157
return options

0 commit comments

Comments
 (0)