-
Notifications
You must be signed in to change notification settings - Fork 48
feat: introduce Send API #2583
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
weboko
wants to merge
22
commits into
master
Choose a base branch
from
weboko/send-api
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: introduce Send API #2583
Changes from 12 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
d29994d
add draft
weboko 97f0fb2
Merge branch 'master' of github.com:waku-org/js-waku into weboko/send…
weboko 01288ff
add draft api
weboko e54645a
Merge branch 'master' of github.com:waku-org/js-waku into weboko/send…
weboko 3de906a
implement basic entites and structure, decouple into separate files
weboko 4fe8bfd
implement main ack manager, improve message store, implement Sender e…
weboko 37ee490
implement background send, requestID
weboko 998c2a1
Merge branch 'master' of github.com:waku-org/js-waku into weboko/send…
weboko 0c852e4
add utils, fix typings
weboko 9e7719e
add ICodec
weboko ad675d8
implement send on waku
weboko 7c67abe
implement and fix queuing mechanics of message store
weboko 4385104
implement acks and message hash saving for sent messages
weboko 7f98bb1
move from encoder/decoder/codec to simple message parameter
weboko da11478
implement working version of sendind
weboko 62b43f0
merge with master
weboko 80cef4b
remove Codec, update types
weboko 82be279
Merge branch 'master' of github.com:waku-org/js-waku into weboko/send…
weboko 969330d
add unit tests
weboko de70791
Merge branch 'master' of github.com:waku-org/js-waku into weboko/send…
weboko e39c6c9
address review
weboko 974fb21
fix start
weboko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import type { | ||
| ICodec, | ||
| IDecodedMessage, | ||
| IDecoder, | ||
| IEncoder, | ||
| IMessage, | ||
| IMetaSetter, | ||
| IProtoMessage, | ||
| IRoutingInfo, | ||
| PubsubTopic | ||
| } from "@waku/interfaces"; | ||
|
|
||
| import { Decoder, Encoder } from "./version_0.js"; | ||
|
|
||
| export class Codec implements ICodec<IDecodedMessage> { | ||
| private encoder: IEncoder; | ||
| private decoder: IDecoder<IDecodedMessage>; | ||
|
|
||
| public constructor( | ||
| public contentTopic: string, | ||
| public ephemeral: boolean = false, | ||
| public routingInfo: IRoutingInfo, | ||
| public metaSetter?: IMetaSetter | ||
| ) { | ||
| this.encoder = new Encoder( | ||
| contentTopic, | ||
| ephemeral, | ||
| routingInfo, | ||
| metaSetter | ||
| ); | ||
| this.decoder = new Decoder(contentTopic, routingInfo); | ||
| } | ||
|
|
||
| public get pubsubTopic(): PubsubTopic { | ||
| return this.routingInfo.pubsubTopic; | ||
| } | ||
|
|
||
| public async toWire(message: IMessage): Promise<Uint8Array | undefined> { | ||
| return this.encoder.toWire(message); | ||
| } | ||
|
|
||
| public async toProtoObj( | ||
| message: IMessage | ||
| ): Promise<IProtoMessage | undefined> { | ||
| return this.encoder.toProtoObj(message); | ||
| } | ||
|
|
||
| public fromWireToProtoObj( | ||
| bytes: Uint8Array | ||
| ): Promise<IProtoMessage | undefined> { | ||
| return this.decoder.fromWireToProtoObj(bytes); | ||
| } | ||
|
|
||
| public async fromProtoObj( | ||
| pubsubTopic: string, | ||
| proto: IProtoMessage | ||
| ): Promise<IDecodedMessage | undefined> { | ||
| return this.decoder.fromProtoObj(pubsubTopic, proto); | ||
| } | ||
| } | ||
|
|
||
| type CodecParams = { | ||
| contentTopic: string; | ||
| ephemeral: boolean; | ||
| routingInfo: IRoutingInfo; | ||
| metaSetter?: IMetaSetter; | ||
| }; | ||
|
|
||
| export function createCodec(params: CodecParams): Codec { | ||
| return new Codec( | ||
| params.contentTopic, | ||
| params.ephemeral, | ||
| params.routingInfo, | ||
| params.metaSetter | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export const OneMillion = BigInt(1_000_000); | ||
| export const Version = 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| export * as version_0 from "./version_0.js"; | ||
| export { Codec, createCodec } from "./codec.js"; | ||
| export { OneMillion, Version } from "./constants.js"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| import { ICodec, IDecodedMessage, IFilter, IStore } from "@waku/interfaces"; | ||
|
|
||
| import { MessageStore } from "./message_store.js"; | ||
| import { IAckManager } from "./utils.js"; | ||
|
|
||
| type AckManagerConstructorParams = { | ||
| messageStore: MessageStore; | ||
| filter: IFilter; | ||
| store: IStore; | ||
| }; | ||
|
|
||
| export class AckManager implements IAckManager { | ||
| private readonly messageStore: MessageStore; | ||
| private readonly filterAckManager: FilterAckManager; | ||
| private readonly storeAckManager: StoreAckManager; | ||
|
|
||
| public constructor(params: AckManagerConstructorParams) { | ||
| this.messageStore = params.messageStore; | ||
|
|
||
| this.filterAckManager = new FilterAckManager( | ||
| this.messageStore, | ||
| params.filter | ||
| ); | ||
|
|
||
| this.storeAckManager = new StoreAckManager(this.messageStore, params.store); | ||
| } | ||
|
|
||
| public start(): void { | ||
| this.filterAckManager.start(); | ||
| this.storeAckManager.start(); | ||
| } | ||
|
|
||
| public async stop(): Promise<void> { | ||
| await this.filterAckManager.stop(); | ||
| this.storeAckManager.stop(); | ||
| } | ||
|
|
||
| public async subscribe(codec: ICodec<IDecodedMessage>): Promise<boolean> { | ||
| return ( | ||
| (await this.filterAckManager.subscribe(codec)) || | ||
| (await this.storeAckManager.subscribe(codec)) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| class FilterAckManager implements IAckManager { | ||
| private codecs: Set<ICodec<IDecodedMessage>> = new Set(); | ||
|
|
||
| public constructor( | ||
| private messageStore: MessageStore, | ||
| private filter: IFilter | ||
| ) {} | ||
|
|
||
| public start(): void { | ||
| return; | ||
| } | ||
|
|
||
| public async stop(): Promise<void> { | ||
| const promises = Array.from(this.codecs.entries()).map((codec) => | ||
| this.filter.unsubscribe(codec) | ||
| ); | ||
| await Promise.all(promises); | ||
| this.codecs.clear(); | ||
| } | ||
|
|
||
| public async subscribe(codec: ICodec<IDecodedMessage>): Promise<boolean> { | ||
| const success = await this.filter.subscribe( | ||
| codec, | ||
| this.onMessage.bind(this) | ||
| ); | ||
| if (success) { | ||
| this.codecs.add(codec); | ||
| } | ||
| return success; | ||
| } | ||
|
|
||
| private async onMessage(message: IDecodedMessage): Promise<void> { | ||
| if (!this.messageStore.has(message.hashStr)) { | ||
| this.messageStore.add(message, { filterAck: true }); | ||
| } | ||
|
|
||
| this.messageStore.markFilterAck(message.hashStr); | ||
| } | ||
| } | ||
|
|
||
| class StoreAckManager implements IAckManager { | ||
| private interval: ReturnType<typeof setInterval> | null = null; | ||
|
|
||
| private codecs: Set<ICodec<IDecodedMessage>> = new Set(); | ||
|
|
||
| public constructor( | ||
| private messageStore: MessageStore, | ||
| private store: IStore | ||
| ) {} | ||
|
|
||
| public start(): void { | ||
| if (this.interval) { | ||
| return; | ||
| } | ||
|
|
||
| this.interval = setInterval(() => { | ||
| void this.query(); | ||
| }, 1000); | ||
| } | ||
|
|
||
| public stop(): void { | ||
| if (!this.interval) { | ||
| return; | ||
| } | ||
|
|
||
| clearInterval(this.interval); | ||
| this.interval = null; | ||
| } | ||
|
|
||
| public async subscribe(codec: ICodec<IDecodedMessage>): Promise<boolean> { | ||
| this.codecs.add(codec); | ||
| return true; | ||
| } | ||
|
|
||
| private async query(): Promise<void> { | ||
| for (const codec of this.codecs) { | ||
| await this.store.queryWithOrderedCallback( | ||
| [codec], | ||
| (message) => { | ||
| if (!this.messageStore.has(message.hashStr)) { | ||
| this.messageStore.add(message, { storeAck: true }); | ||
| } | ||
|
|
||
| this.messageStore.markStoreAck(message.hashStr); | ||
| }, | ||
| { | ||
| timeStart: new Date(Date.now() - 60 * 60 * 1000), | ||
| timeEnd: new Date() | ||
| } | ||
| ); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export { Messaging } from "./messaging.js"; | ||
| export type { RequestId } from "./utils.js"; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.