Skip to content
This repository was archived by the owner on Dec 21, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
"coverage": "jest --coverage",
"test-integration": "jest --forceExit test/integration",
"test-exports": "cd test/exports && npm run link && npm test",
"test-integration-no-resend": "jest --forceExit --testTimeout=10000 --testPathIgnorePatterns='resend|Resend' --testNamePattern='^((?!(resend|Resend|resent|Resent|gap|Gap)).)*$' test/integration/*.test.js",
"test-integration-resend": "jest --forceExit --testTimeout=15000 --testNamePattern='(resend|Resend|resent|Resent)' test/integration/*.test.js",
"test-integration-no-resend": "jest --forceExit --testTimeout=10000 --testPathIgnorePatterns='resend|Resend' --testNamePattern='^((?!(resend|Resend|resent|Resent|gap|Gap)).)*$' test/integration/*.test.*",
"test-integration-resend": "jest --forceExit --testTimeout=15000 --testNamePattern='(resend|Resend|resent|Resent)' test/integration/*.test.*",
"test-integration-dataunions": "jest --forceExit --testTimeout=15000 --runInBand test/integration/dataunion",
"test-flakey": "jest --forceExit test/flakey/*",
"test-browser": "node ./test/browser/server.js & node node_modules/nightwatch/bin/nightwatch ./test/browser/browser.js && pkill -f server.js",
Expand Down
8 changes: 4 additions & 4 deletions src/publish/Signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ const { SigningUtil } = Utils
const { SIGNATURE_TYPES } = StreamMessage

type AuthOption = {
ethereum: undefined
ethereum?: never
privateKey: string | Uint8Array
} | {
privateKey: undefined
privateKey?: never
ethereum: EthereumConfig
} | {
ethereum: undefined
privateKey: undefined
ethereum?: never
privateKey?: never
}

function getSigningFunction({
Expand Down
2 changes: 1 addition & 1 deletion src/stream/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import StorageNode from './StorageNode'
import { StreamrClient } from '../StreamrClient'

// TODO explicit types: e.g. we never provide both streamId and id, or both streamPartition and partition
export type StreamPartDefinition = string | { streamId?: string, streamPartition?: number, id?: string, partition?: number, stream?: Stream }
export type StreamPartDefinition = string | { streamId?: string, streamPartition?: number, id?: string, partition?: number, stream?: Stream|string }

export type ValidatedStreamPartDefinition = { streamId: string, streamPartition: number, key: string}

Expand Down
10 changes: 5 additions & 5 deletions src/subscribe/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import MessagePipeline from './pipeline'
import Validator from './Validator'
import messageStream from './messageStream'
import resendStream from './resendStream'
import { Todo } from '../types'
import { MaybeAsync, Todo } from '../types'
import StreamrClient, { StreamPartDefinition, SubscribeOptions } from '..'

/**
Expand Down Expand Up @@ -377,7 +377,7 @@ class Subscriptions {
this.subSessions = new Map()
}

async add(opts: StreamPartDefinition, onFinally: Todo = async () => {}) {
async add(opts: StreamPartDefinition, onFinally: MaybeAsync<(err?: any) => void> = async () => {}) {
const options = validateOptions(opts)
const { key } = options

Expand Down Expand Up @@ -536,7 +536,7 @@ export class Subscriber {
return this.subscriptions.count(options)
}

async subscribe(opts: StreamPartDefinition, onFinally?: Todo) {
async subscribe(opts: StreamPartDefinition, onFinally?: MaybeAsync<(err?: any) => void>) {
return this.subscriptions.add(opts, onFinally)
}

Expand Down Expand Up @@ -571,7 +571,7 @@ export class Subscriber {
return sub
}

async resendSubscribe(opts: SubscribeOptions & StreamPartDefinition, onMessage: Todo) {
async resendSubscribe(opts: SubscribeOptions & StreamPartDefinition, onFinally?: MaybeAsync<(err?: any) => void>) {
// This works by passing a custom message stream to a subscription
// the custom message stream iterates resends, then iterates realtime
const options = validateOptions(opts)
Expand Down Expand Up @@ -667,7 +667,7 @@ export class Subscriber {
}
},
],
}, onMessage)
}, onFinally)

// eslint-disable-next-line semi-style
;[resendSubscribeSub] = await Promise.all([
Expand Down
8 changes: 5 additions & 3 deletions src/user/index.js → src/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { computeAddress } from '@ethersproject/transactions'
import { Web3Provider } from '@ethersproject/providers'
import { hexlify } from '@ethersproject/bytes'
import { sha256 } from '@ethersproject/sha2'
import { StreamrClient } from '../StreamrClient'
import { EthereumConfig } from '../Config'

async function getUsername(client) {
async function getUsername(client: StreamrClient) {
const { options: { auth = {} } = {} } = client
if (auth.username) { return auth.username }

Expand All @@ -15,7 +17,7 @@ async function getUsername(client) {
)
}

export async function getAddressFromOptions({ ethereum, privateKey } = {}) {
export async function getAddressFromOptions({ ethereum, privateKey }: { ethereum?: EthereumConfig, privateKey?: any} = {}) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably get something like this into Config.ts then use the same types across the app e.g. here and Signer:

type PrivateKeyAuth = {
    privateKey: string
    ethereum?: never,
    apiKey?: never,
}

type ProviderAuth = {
    ethereum: ExternalProvider | JsonRpcFetchFunc
    privateKey?: never,
    apiKey?: never,
}

/**
 * deprecated: Please create an ethereum identity.
 */
type APIKeyAuth = {
    apiKey: string
    privateKey?: never,
    ethereum?: never,
}

type AnonymousAuth = {
    privateKey?: never,
    apiKey?: never,
    ethereum?: never,
}

type Authenticated = PrivateKeyAuth | ProviderAuth | APIKeyAuth
type AuthOptions =  Authenticated | AnonymousAuth

Maybe for another PR, your call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. It would be great if e.g. ts-toolbelt would have a good helper for this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah it might, worth looking into

if (privateKey) {
return computeAddress(privateKey).toLowerCase()
}
Expand All @@ -29,7 +31,7 @@ export async function getAddressFromOptions({ ethereum, privateKey } = {}) {
throw new Error('Need either "privateKey" or "ethereum".')
}

export async function getUserId(client) {
export async function getUserId(client: StreamrClient) {
if (client.session.isUnauthenticated()) {
throw new Error('Need to be authenticated to getUserId.')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,39 @@ import { StreamrClient } from '../../src/StreamrClient'
import Connection from '../../src/Connection'

import config from './config'
import { Stream } from '../../src/stream'
import { Subscriber, Subscription } from '../../src/subscribe'
import { MessageRef } from 'streamr-client-protocol/dist/src/protocol/message_layer'
import { StreamrClientOptions } from '../../src'

const MAX_MESSAGES = 10

describeRepeats('GapFill with resends', () => {
let expectErrors = 0 // check no errors by default
let publishTestMessages
let publishTestMessages: ReturnType<typeof getPublishTestMessages>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

let onError = jest.fn()
let client
let stream
let subscriber
let client: StreamrClient
let stream: Stream
let subscriber: Subscriber

const createClient = (opts = {}) => {
const c = new StreamrClient({
...config.clientOptions,
...opts,
auth: {
privateKey: fakePrivateKey(),
},
autoConnect: false,
autoDisconnect: false,
// @ts-expect-error
maxRetries: 2,
...config.clientOptions,
...opts,
})
c.onError = jest.fn()
c.on('error', onError)
return c
}

async function setupClient(opts) {
async function setupClient(opts: StreamrClientOptions) {
// eslint-disable-next-line require-atomic-updates
client = createClient(opts)
subscriber = client.subscriber
Expand All @@ -57,11 +62,11 @@ describeRepeats('GapFill with resends', () => {
if (!subscriber) { return }
expect(subscriber.count(stream.id)).toBe(0)
if (!client) { return }
expect(client.getSubscriptions(stream.id)).toEqual([])
expect(client.getSubscriptions()).toEqual([])
})

afterEach(async () => {
await wait()
await wait(0)
// ensure no unexpected errors
expect(onError).toHaveBeenCalledTimes(expectErrors)
if (client) {
Expand All @@ -70,7 +75,7 @@ describeRepeats('GapFill with resends', () => {
})

afterEach(async () => {
await wait()
await wait(0)
if (client) {
client.debug('disconnecting after test >>')
await client.disconnect()
Expand All @@ -84,7 +89,7 @@ describeRepeats('GapFill with resends', () => {
}
})

let subs = []
let subs: Subscription[] = []

beforeEach(async () => {
const existingSubs = subs
Expand All @@ -108,7 +113,7 @@ describeRepeats('GapFill with resends', () => {
const { parse } = client.connection
let count = 0
client.connection.parse = (...args) => {
const msg = parse.call(client.connection, ...args)
const msg: any = parse.call(client.connection, ...args)
if (!msg.streamMessage) {
return msg
}
Expand Down Expand Up @@ -143,7 +148,7 @@ describeRepeats('GapFill with resends', () => {
const { parse } = client.connection
let count = 0
client.connection.parse = (...args) => {
const msg = parse.call(client.connection, ...args)
const msg: any = parse.call(client.connection, ...args)
if (!msg.streamMessage) {
return msg
}
Expand Down Expand Up @@ -176,7 +181,7 @@ describeRepeats('GapFill with resends', () => {
const { parse } = client.connection
let count = 0
client.connection.parse = (...args) => {
const msg = parse.call(client.connection, ...args)
const msg: any = parse.call(client.connection, ...args)
if (!msg.streamMessage) {
return msg
}
Expand Down Expand Up @@ -208,7 +213,7 @@ describeRepeats('GapFill with resends', () => {
const { parse } = client.connection
let count = 0
client.connection.parse = (...args) => {
const msg = parse.call(client.connection, ...args)
const msg: any = parse.call(client.connection, ...args)
if (!msg.streamMessage) {
return msg
}
Expand Down Expand Up @@ -242,9 +247,9 @@ describeRepeats('GapFill with resends', () => {
it('can fill gaps in resends even if gap cannot be filled', async () => {
const { parse } = client.connection
let count = 0
let droppedMsgRef
let droppedMsgRef: MessageRef
client.connection.parse = (...args) => {
const msg = parse.call(client.connection, ...args)
const msg: any = parse.call(client.connection, ...args)
if (!msg.streamMessage) {
return msg
}
Expand Down Expand Up @@ -277,7 +282,7 @@ describeRepeats('GapFill with resends', () => {
received.push(m.getParsedContent())
// should not need to explicitly end
}
expect(received).toEqual(published.filter((_value, index) => index !== 2))
expect(received).toEqual(published.filter((_value: any, index: number) => index !== 2))
expect(client.connection.getState()).toBe('connected')
}, 60000)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import { StreamrClient } from '../../src/StreamrClient'
import config from './config'

describe('LoginEndpoints', () => {
let client
let client: StreamrClient

const createClient = (opts = {}) => new StreamrClient({
...config.clientOptions,
apiKey: 'tester1-api-key',
auth: {
apiKey: 'tester1-api-key',
},
autoConnect: false,
autoDisconnect: false,
...opts,
Expand All @@ -29,8 +31,10 @@ describe('LoginEndpoints', () => {
it('should retrieve a challenge', async () => {
const challenge = await client.getChallenge('some-address')
assert(challenge)
// @ts-expect-error
assert(challenge.id)
assert(challenge.challenge)
// @ts-expect-error
assert(challenge.expires)
})
})
Expand All @@ -39,6 +43,7 @@ describe('LoginEndpoints', () => {
it('should fail to get a session token', async () => {
await expect(async () => {
await client.sendChallengeResponse({
// @ts-expect-error
id: 'some-id',
challenge: 'some-challenge',
}, 'some-sig', 'some-address')
Expand All @@ -53,6 +58,7 @@ describe('LoginEndpoints', () => {
const sessionToken = await client.sendChallengeResponse(challenge, signature, wallet.address)
assert(sessionToken)
assert(sessionToken.token)
// @ts-expect-error
assert(sessionToken.expires)
})

Expand All @@ -61,6 +67,7 @@ describe('LoginEndpoints', () => {
const sessionToken = await client.loginWithChallengeResponse((d) => wallet.signMessage(d), wallet.address)
assert(sessionToken)
assert(sessionToken.token)
// @ts-expect-error
assert(sessionToken.expires)
})
})
Expand All @@ -76,6 +83,7 @@ describe('LoginEndpoints', () => {
const sessionToken = await client.loginWithApiKey('tester1-api-key')
assert(sessionToken)
assert(sessionToken.token)
// @ts-expect-error
assert(sessionToken.expires)
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { StreamrClient } from '../../src/StreamrClient'
import { Defer } from '../../src/utils'

import config from './config'
import { Stream } from '../../src/stream'
import { Subscription } from '../../src'
import { PublishRequest } from 'streamr-client-protocol/dist/src/protocol/control_layer'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blegh, ideally shouldn't have dist/src in any import, I'd consider that stuff "private".

For now you might have to grab it like this:

import { ControlLayer } from 'streamr-client-protocol'
const { PublishRequest } = ControlLayer

But if we update protocol to 8.0.0 you should be able to do this as of streamr-dev/streamr-client-protocol-js#53:

import { PublishRequest } from 'streamr-client-protocol'


const createClient = (opts = {}) => new StreamrClient({
...(config.clientOptions || {
url: config.websocketUrl,
restUrl: config.restUrl,
}),
...config.clientOptions,
auth: {
privateKey: fakePrivateKey(),
},
Expand All @@ -22,10 +22,10 @@ const createClient = (opts = {}) => new StreamrClient({
const MAX_MESSAGES = 3

describe('resend/reconnect', () => {
let client
let stream
let publishedMessages
let publishTestMessages
let client: StreamrClient
let stream: Stream
let publishedMessages: [message: any, request: PublishRequest][]
let publishTestMessages: ReturnType<typeof getPublishTestMessages>

beforeEach(async () => {
client = createClient()
Expand All @@ -49,13 +49,13 @@ describe('resend/reconnect', () => {

describe('reconnect with resend', () => {
let shouldDisconnect = false
let sub
let messages = []
let sub: Subscription
let messages: any[] = []
beforeEach(async () => {
const done = Defer()
messages = []
sub = await client.subscribe({
stream: stream.id,
streamId: stream.id,
resend: {
last: MAX_MESSAGES,
},
Expand Down
Loading