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

Commit 4bdc631

Browse files
authored
Internal properties and methods (#214)
Hide internal properties and methods from the public classes. Currently the internals are annotated with a weak /** @internal */ . Some of the properties/methods could be marked with TypeScript's private to force the encapsulation. That can be done later, after we have converted all classes to TypeScript and the usage boundaries are clear.
1 parent 569146e commit 4bdc631

File tree

10 files changed

+165
-80
lines changed

10 files changed

+165
-80
lines changed

src/Session.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,21 @@ export default class Session extends EventEmitter {
4747
if (typeof this.options.privateKey !== 'undefined') {
4848
const wallet = new Wallet(this.options.privateKey)
4949
this.loginFunction = async () => (
50-
this._client.loginEndpoints.loginWithChallengeResponse((d: string) => wallet.signMessage(d), wallet.address)
50+
this._client.loginWithChallengeResponse((d: string) => wallet.signMessage(d), wallet.address)
5151
)
5252
} else if (typeof this.options.ethereum !== 'undefined') {
5353
const provider = new Web3Provider(this.options.ethereum)
5454
const signer = provider.getSigner()
5555
this.loginFunction = async () => (
56-
this._client.loginEndpoints.loginWithChallengeResponse((d: string) => signer.signMessage(d), await signer.getAddress())
56+
this._client.loginWithChallengeResponse((d: string) => signer.signMessage(d), await signer.getAddress())
5757
)
5858
} else if (typeof this.options.apiKey !== 'undefined') {
5959
this.loginFunction = async () => (
60-
this._client.loginEndpoints.loginWithApiKey(this.options.apiKey!)
60+
this._client.loginWithApiKey(this.options.apiKey!)
6161
)
6262
} else if (typeof this.options.username !== 'undefined' && typeof this.options.password !== 'undefined') {
6363
this.loginFunction = async () => (
64-
this._client.loginEndpoints.loginWithUsernamePassword(this.options.username!, this.options.password!)
64+
this._client.loginWithUsernamePassword(this.options.username!, this.options.password!)
6565
)
6666
} else {
6767
if (!this.options.sessionToken) {
@@ -128,7 +128,7 @@ export default class Session extends EventEmitter {
128128
}
129129

130130
this.updateState(State.LOGGING_OUT)
131-
await this._client.loginEndpoints.logoutEndpoint()
131+
await this._client.logoutEndpoint()
132132
this.options.sessionToken = undefined
133133
this.updateState(State.LOGGED_OUT)
134134
}

src/StreamrClient.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import StreamrEthereum from './Ethereum'
99
import Session from './Session'
1010
import Connection, { ConnectionError } from './Connection'
1111
import Publisher from './publish'
12-
import { Subscriber } from './subscribe'
12+
import { Subscriber, Subscription } from './subscribe'
1313
import { getUserId } from './user'
1414
import { Todo, MaybeAsync, EthereumAddress } from './types'
1515
import { StreamEndpoints } from './rest/StreamEndpoints'
@@ -141,18 +141,24 @@ export interface StreamrClient extends StreamEndpoints, LoginEndpoints {}
141141

142142
// eslint-disable-next-line no-redeclare
143143
export class StreamrClient extends EventEmitter {
144+
/** @internal */
144145
id: string
146+
/** @internal */
145147
debug: Debug.Debugger
148+
/** @internal */
146149
options: StrictStreamrClientOptions
147150
/** @internal */
148151
session: Session
152+
/** @internal */
149153
connection: StreamrConnection
154+
/** @internal */
150155
publisher: Todo
156+
/** @internal */
151157
subscriber: Subscriber
158+
/** @internal */
152159
cached: StreamrCached
160+
/** @internal */
153161
ethereum: StreamrEthereum
154-
streamEndpoints: StreamEndpoints
155-
loginEndpoints: LoginEndpoints
156162

157163
constructor(options: StreamrClientOptions = {}, connection?: StreamrConnection) {
158164
super()
@@ -189,25 +195,29 @@ export class StreamrClient extends EventEmitter {
189195
this.subscriber = new Subscriber(this)
190196
this.ethereum = new StreamrEthereum(this)
191197

192-
this.streamEndpoints = Plugin(this, new StreamEndpoints(this))
193-
this.loginEndpoints = Plugin(this, new LoginEndpoints(this))
198+
Plugin(this, new StreamEndpoints(this))
199+
Plugin(this, new LoginEndpoints(this))
194200
this.cached = new StreamrCached(this)
195201
}
196202

203+
/** @internal */
197204
async onConnectionConnected() {
198205
this.debug('Connected!')
199206
this.emit('connected')
200207
}
201208

209+
/** @internal */
202210
async onConnectionDisconnected() {
203211
this.debug('Disconnected.')
204212
this.emit('disconnected')
205213
}
206214

215+
/** @internal */
207216
onConnectionError(err: Todo) {
208217
this.emit('error', new ConnectionError(err))
209218
}
210219

220+
/** @internal */
211221
getErrorEmitter(source: Todo) {
212222
return (err: Todo) => {
213223
if (!(err instanceof ConnectionError || err.reason instanceof ConnectionError)) {
@@ -219,19 +229,20 @@ export class StreamrClient extends EventEmitter {
219229
}
220230
}
221231

232+
/** @internal */
222233
_onError(err: Todo, ...args: Todo) {
223234
// @ts-expect-error
224235
this.onError(err, ...args)
225236
}
226237

238+
/** @internal */
227239
async send(request: Todo) {
228240
return this.connection.send(request)
229241
}
230242

231243
/**
232244
* Override to control output
233-
*/
234-
245+
* @internal */
235246
onError(error: Todo) { // eslint-disable-line class-methods-use-this
236247
console.error(error)
237248
}
@@ -256,6 +267,7 @@ export class StreamrClient extends EventEmitter {
256267
return this.connection.connect()
257268
}
258269

270+
/** @internal */
259271
async nextConnection() {
260272
return this.connection.nextConnection()
261273
}
@@ -297,10 +309,12 @@ export class StreamrClient extends EventEmitter {
297309
return getUserId(this)
298310
}
299311

312+
/** @internal */
300313
setNextGroupKey(...args: Todo) {
301314
return this.publisher.setNextGroupKey(...args)
302315
}
303316

317+
/** @internal */
304318
rotateGroupKey(...args: Todo) {
305319
return this.publisher.rotateGroupKey(...args)
306320
}
@@ -340,7 +354,7 @@ export class StreamrClient extends EventEmitter {
340354
await this.subscriber.unsubscribe(opts)
341355
}
342356

343-
async resend(opts: Todo, onMessage?: OnMessageCallback) {
357+
async resend(opts: Todo, onMessage?: OnMessageCallback): Promise<Subscription> {
344358
const task = this.subscriber.resend(opts)
345359
if (typeof onMessage !== 'function') {
346360
return task
@@ -367,11 +381,11 @@ export class StreamrClient extends EventEmitter {
367381
return this.connection.enableAutoDisconnect(...args)
368382
}
369383

370-
getAddress() {
384+
getAddress(): EthereumAddress {
371385
return this.ethereum.getAddress()
372386
}
373387

374-
async getPublisherId() {
388+
async getPublisherId(): Promise<EthereumAddress> {
375389
return this.getAddress()
376390
}
377391

@@ -406,6 +420,7 @@ export class StreamrClient extends EventEmitter {
406420
return DataUnion._deploy(options, this) // eslint-disable-line no-underscore-dangle
407421
}
408422

423+
/** @internal */
409424
_getDataUnionFromName({ dataUnionName, deployerAddress }: { dataUnionName: string, deployerAddress: EthereumAddress}) {
410425
return DataUnion._fromName({ // eslint-disable-line no-underscore-dangle
411426
dataUnionName,

src/dataunion/DataUnion.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ const log = debug('StreamrClient::DataUnion')
6868

6969
export class DataUnion {
7070

71-
contractAddress: EthereumAddress
72-
sidechainAddress: EthereumAddress
73-
client: StreamrClient
71+
private contractAddress: EthereumAddress
72+
private sidechainAddress: EthereumAddress
73+
private client: StreamrClient
7474

75+
/** @internal */
7576
constructor(contractAddress: EthereumAddress, sidechainAddress: EthereumAddress, client: StreamrClient) {
7677
// validate and convert to checksum case
7778
this.contractAddress = getAddress(contractAddress)
@@ -460,6 +461,7 @@ export class DataUnion {
460461
* Create a new DataUnionMainnet contract to mainnet with DataUnionFactoryMainnet
461462
* This triggers DataUnionSidechain contract creation in sidechain, over the bridge (AMB)
462463
* @return that resolves when the new DU is deployed over the bridge to side-chain
464+
* @internal
463465
*/
464466
static async _deploy(options: DataUnionDeployOptions = {}, client: StreamrClient): Promise<DataUnion> {
465467
const deployerAddress = client.getAddress()
@@ -513,18 +515,21 @@ export class DataUnion {
513515

514516
// Internal functions
515517

518+
/** @internal */
516519
static _fromContractAddress(contractAddress: string, client: StreamrClient) {
517520
const contracts = new Contracts(client)
518521
const sidechainAddress = contracts.getDataUnionSidechainAddress(getAddress(contractAddress)) // throws if bad address
519522
return new DataUnion(contractAddress, sidechainAddress, client)
520523
}
521524

525+
/** @internal */
522526
static _fromName({ dataUnionName, deployerAddress }: { dataUnionName: string, deployerAddress: string}, client: StreamrClient) {
523527
const contracts = new Contracts(client)
524528
const contractAddress = contracts.getDataUnionMainnetAddress(dataUnionName, getAddress(deployerAddress)) // throws if bad address
525529
return DataUnion._fromContractAddress(contractAddress, client) // eslint-disable-line no-underscore-dangle
526530
}
527531

532+
/** @internal */
528533
async _getContract() {
529534
const ret = this.getContracts().getMainnetContract(this.contractAddress)
530535
// @ts-expect-error

src/rest/LoginEndpoints.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ async function getSessionToken(url: string, props: any) {
2828
/** TODO the class should be annotated with at-internal, but adding the annotation hides the methods */
2929
export class LoginEndpoints {
3030

31+
/** @internal */
3132
client: StreamrClient
3233

3334
constructor(client: StreamrClient) {
3435
this.client = client
3536
}
3637

38+
/** @internal */
3739
async getChallenge(address: string) {
3840
this.client.debug('getChallenge %o', {
3941
address,
@@ -48,6 +50,7 @@ export class LoginEndpoints {
4850
)
4951
}
5052

53+
/** @internal */
5154
async sendChallengeResponse(challenge: { challenge: string }, signature: string, address: string) {
5255
this.client.debug('sendChallengeResponse %o', {
5356
challenge,
@@ -63,6 +66,7 @@ export class LoginEndpoints {
6366
return getSessionToken(url, props)
6467
}
6568

69+
/** @internal */
6670
async loginWithChallengeResponse(signingFunction: (challenge: string) => Promise<string>, address: string) {
6771
this.client.debug('loginWithChallengeResponse %o', {
6872
address,
@@ -72,6 +76,7 @@ export class LoginEndpoints {
7276
return this.sendChallengeResponse(challenge, signature, address)
7377
}
7478

79+
/** @internal */
7580
async loginWithApiKey(apiKey: string) {
7681
this.client.debug('loginWithApiKey %o', {
7782
apiKey,
@@ -83,6 +88,7 @@ export class LoginEndpoints {
8388
return getSessionToken(url, props)
8489
}
8590

91+
/** @internal */
8692
async loginWithUsernamePassword(username: string, password: string) {
8793
this.client.debug('loginWithUsernamePassword %o', {
8894
username,
@@ -110,6 +116,7 @@ export class LoginEndpoints {
110116
return authFetch<UserDetails>(`${this.client.options.restUrl}/users/me`, this.client.session)
111117
}
112118

119+
/** @internal */
113120
async logoutEndpoint(): Promise<void> {
114121
this.client.debug('logoutEndpoint')
115122
await authFetch(`${this.client.options.restUrl}/logout`, this.client.session, {

src/rest/StreamEndpoints.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ function getKeepAliveAgentForUrl(url: string) {
7979
/** TODO the class should be annotated with at-internal, but adding the annotation hides the methods */
8080
export class StreamEndpoints {
8181

82+
/** @internal */
8283
client: StreamrClient
8384

8485
constructor(client: StreamrClient) {
@@ -171,7 +172,7 @@ export class StreamEndpoints {
171172
return json.addresses.map((a: string) => a.toLowerCase())
172173
}
173174

174-
async isStreamPublisher(streamId: string, ethAddress: string) {
175+
async isStreamPublisher(streamId: string, ethAddress: EthereumAddress) {
175176
this.client.debug('isStreamPublisher %o', {
176177
streamId,
177178
ethAddress,
@@ -198,7 +199,7 @@ export class StreamEndpoints {
198199
return json.addresses.map((a: string) => a.toLowerCase())
199200
}
200201

201-
async isStreamSubscriber(streamId: string, ethAddress: string) {
202+
async isStreamSubscriber(streamId: string, ethAddress: EthereumAddress) {
202203
this.client.debug('isStreamSubscriber %o', {
203204
streamId,
204205
ethAddress,

src/stream/StorageNode.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { EthereumAddress } from '../types'
22

33
export default class StorageNode {
4-
_address: EthereumAddress
4+
5+
private _address: EthereumAddress
6+
57
constructor(address: EthereumAddress) {
68
this._address = address
79
}

src/stream/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,21 @@ function getFieldType(value: any): (Field['type'] | undefined) {
7070
}
7171

7272
export class Stream {
73-
// TODO add field definitions for all fields
7473
// @ts-expect-error
7574
id: string
7675
// @ts-expect-error
7776
name: string
77+
description?: string
7878
config: {
7979
fields: Field[];
8080
} = { fields: [] }
81+
partitions?: number
82+
/** @internal */
8183
_client: StreamrClient
8284
requireEncryptedData?: boolean
8385
requireSignedData?: boolean
86+
storageDays?: number
87+
inactivityThresholdHours?: number
8488

8589
constructor(client: StreamrClient, props: StreamProperties) {
8690
this._client = client
@@ -99,6 +103,7 @@ export class Stream {
99103
return json ? new Stream(this._client, json) : undefined
100104
}
101105

106+
/** @internal */
102107
toObject() {
103108
const result = {}
104109
Object.keys(this).forEach((key) => {

0 commit comments

Comments
 (0)