@@ -3,7 +3,7 @@ import './utils/PatchTsyringe'
33
44import { DhtAddress } from '@streamr/dht'
55import { ProxyDirection } from '@streamr/trackerless-network'
6- import { DEFAULT_PARTITION_COUNT , EthereumAddress , HexString , StreamID , TheGraphClient , toEthereumAddress , toUserId } from '@streamr/utils'
6+ import { DEFAULT_PARTITION_COUNT , EthereumAddress , HexString , Logger , StreamID , TheGraphClient , toEthereumAddress , toUserId } from '@streamr/utils'
77import type { Overrides } from 'ethers'
88import EventEmitter from 'eventemitter3'
99import merge from 'lodash/merge'
@@ -52,6 +52,7 @@ import { LoggerFactory } from './utils/LoggerFactory'
5252import { pOnce } from './utils/promises'
5353import { convertPeerDescriptorToNetworkPeerDescriptor , createTheGraphClient } from './utils/utils'
5454import { addStreamToStorageNode } from './utils/addStreamToStorageNode'
55+ import { map } from './utils/GeneratorUtils'
5556
5657// TODO: this type only exists to enable tsdoc to generate proper documentation
5758export type SubscribeOptions = StreamDefinition & ExtraSubscribeOptions
@@ -73,6 +74,8 @@ export interface ExtraSubscribeOptions {
7374 erc1271Contract ?: HexString
7475}
7576
77+ const logger = new Logger ( module )
78+
7679/**
7780 * The main API used to interact with Streamr.
7881 *
@@ -357,8 +360,11 @@ export class StreamrClient {
357360 */
358361 async getStream ( streamIdOrPath : string ) : Promise < Stream > {
359362 const streamId = await this . streamIdBuilder . toStreamID ( streamIdOrPath )
360- const metadata = await this . streamRegistry . getStreamMetadata ( streamId , false )
361- return new Stream ( streamId , metadata , this )
363+ // Check if the stream exists by querying its metadata. Throws an error if no metadata is found,
364+ // indicating the stream doesn't exist. As a side-effect this populates StreamRegistry's metadata
365+ // cache for future use, such as stream.getPartitionCount() calls.
366+ await this . streamRegistry . getStreamMetadata ( streamId , false )
367+ return new Stream ( streamId , this )
362368 }
363369
364370 /**
@@ -376,7 +382,7 @@ export class StreamrClient {
376382 const streamId = await this . streamIdBuilder . toStreamID ( props . id )
377383 const metadata = merge ( { partitions : DEFAULT_PARTITION_COUNT } , omit ( props , 'id' ) )
378384 await this . streamRegistry . createStream ( streamId , metadata )
379- return new Stream ( streamId , metadata , this )
385+ return new Stream ( streamId , this )
380386 }
381387
382388 /**
@@ -434,12 +440,16 @@ export class StreamrClient {
434440 permissionFilter : SearchStreamsPermissionFilter | undefined ,
435441 orderBy : SearchStreamsOrderBy = { field : 'id' , direction : 'asc' }
436442 ) : AsyncIterable < Stream > {
437- return this . streamRegistry . searchStreams (
443+ logger . debug ( 'Search for streams' , { term, permissionFilter } )
444+ if ( ( term === undefined ) && ( permissionFilter === undefined ) ) {
445+ throw new Error ( 'Requires a search term or a permission filter' )
446+ }
447+ const streamIds = this . streamRegistry . searchStreams (
438448 term ,
439449 ( permissionFilter !== undefined ) ? toInternalSearchStreamsPermissionFilter ( permissionFilter ) : undefined ,
440- orderBy ,
441- this
450+ orderBy
442451 )
452+ return map ( streamIds , ( id ) => new Stream ( id , this ) )
443453 }
444454
445455 // --------------------------------------------------------------------------------------------
@@ -572,8 +582,11 @@ export class StreamrClient {
572582 */
573583 async getStoredStreams ( storageNodeAddress : HexString ) : Promise < { streams : Stream [ ] , blockNumber : number } > {
574584 const queryResult = await this . streamStorageRegistry . getStoredStreams ( toEthereumAddress ( storageNodeAddress ) )
585+ for ( const stream of queryResult . streams ) {
586+ this . streamRegistry . populateMetadataCache ( stream . id , stream . metadata )
587+ }
575588 return {
576- streams : queryResult . streams . map ( ( item ) => new Stream ( item . id , item . metadata , this ) ) ,
589+ streams : queryResult . streams . map ( ( item ) => new Stream ( item . id , this ) ) ,
577590 blockNumber : queryResult . blockNumber
578591 }
579592 }
0 commit comments