-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: Change WebSocket to AsyncIterator
* feat!: Change WebSocket to AsyncIterator wip Add tests fix types fix unit tests improve tests fix CI errors Update streaming example Retry with an exponential backoff prettify improve tests * disable public:media temporarily
- Loading branch information
Showing
73 changed files
with
1,425 additions
and
771 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
"words": [ | ||
"AGPL", | ||
"asynckit", | ||
"Backoff", | ||
"blurhash", | ||
"builtins", | ||
"carryforward", | ||
|
This file contains 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,19 +1,69 @@ | ||
import { login } from 'masto'; | ||
import { createWebSocketClient } from 'masto'; | ||
|
||
const masto = await login({ | ||
url: 'https://example.com', | ||
accessToken: 'YOUR TOKEN', | ||
}); | ||
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); | ||
|
||
// Connect to the streaming api | ||
const stream = await masto.v1.stream.streamPublicTimeline(); | ||
// https://en.wikipedia.org/wiki/Exponential_backoff | ||
class ExponentialBackoff { | ||
private errorCount = 0; | ||
|
||
// Subscribe to updates | ||
stream.on('update', (status) => { | ||
console.log(`${status.account.username}: ${status.content}`); | ||
}); | ||
constructor(private readonly baseSeconds: number) {} | ||
|
||
// Subscribe to notifications | ||
stream.on('notification', (notification) => { | ||
console.log(`${notification.account.username}: ${notification.type}`); | ||
}); | ||
get timeout() { | ||
return this.baseSeconds ** this.errorCount * 1000; | ||
} | ||
|
||
clear() { | ||
this.errorCount = 0; | ||
} | ||
|
||
async sleep() { | ||
await sleep(this.timeout); | ||
this.errorCount++; | ||
} | ||
} | ||
|
||
// ======================================== | ||
|
||
const backoff = new ExponentialBackoff(2); | ||
|
||
const subscribe = async (): Promise<void> => { | ||
try { | ||
const masto = createWebSocketClient({ | ||
url: '<API URL>', | ||
accessToken: '<TOKEN>', | ||
streamingApiUrl: '<STREAMING API URL>', | ||
}); | ||
|
||
console.log('subscribed to #mastojs'); | ||
|
||
for await (const event of masto.subscribe('hashtag', { tag: 'mastojs' })) { | ||
switch (event.event) { | ||
case 'update': { | ||
console.log('new post', event.payload.content); | ||
break; | ||
} | ||
case 'delete': { | ||
console.log('deleted post', event.payload); | ||
break; | ||
} | ||
default: { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
backoff.clear(); | ||
} catch (error) { | ||
console.error(error); | ||
} finally { | ||
console.log('Reconnecting in ' + backoff.timeout + 'ms'); | ||
await backoff.sleep(); | ||
await subscribe(); | ||
} | ||
}; | ||
|
||
try { | ||
await subscribe(); | ||
} catch (error) { | ||
console.error(error); | ||
} |
This file contains 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 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,3 +1,2 @@ | ||
export * from './http-mock-impl'; | ||
export * from './ws-mock-impl'; | ||
export * from './logger-mock-impl'; |
This file was deleted.
Oops, something went wrong.
This file contains 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 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 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 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 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 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,47 @@ | ||
import type { Event } from './event'; | ||
|
||
export type Stream = | ||
| 'public' | ||
| 'public:media' | ||
| 'public:local' | ||
| 'public:local:media' | ||
| 'public:remote' | ||
| 'public:remote:media' | ||
| 'hashtag' | ||
| 'hashtag:local' | ||
| 'user' | ||
| 'user:notification' | ||
| 'list' | ||
| 'direct'; | ||
|
||
export type SubscribeListParams = { | ||
readonly list: string; | ||
}; | ||
|
||
export type SubscribeHashtagParams = { | ||
readonly tag: string; | ||
}; | ||
|
||
export interface WebSocketAPIConnection { | ||
readonly events: AsyncIterableIterator<Event>; | ||
readonly readyState: number; | ||
|
||
subscribe( | ||
stream: 'list', | ||
params: SubscribeListParams, | ||
): AsyncIterableIterator<Event>; | ||
subscribe( | ||
stream: 'hashtag' | 'hashtag:local', | ||
params: SubscribeHashtagParams, | ||
): AsyncIterableIterator<Event>; | ||
subscribe(stream: Stream): AsyncIterableIterator<Event>; | ||
|
||
unsubscribe(stream: 'list', params: SubscribeListParams): void; | ||
unsubscribe( | ||
stream: 'hashtag' | 'hashtag:local', | ||
params: SubscribeHashtagParams, | ||
): void; | ||
unsubscribe(stream: Stream): void; | ||
|
||
close(): void; | ||
} |
This file contains 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,60 @@ | ||
import type { mastodon } from '../..'; | ||
|
||
export type RawEventOk = { | ||
stream: string[]; | ||
event: string; | ||
payload: string; | ||
}; | ||
|
||
export type RawEventError = { | ||
error: string; | ||
}; | ||
|
||
export type RawEvent = RawEventOk | RawEventError; | ||
|
||
type BaseEvent<T, U> = { | ||
stream: string[]; | ||
event: T; | ||
payload: U; | ||
}; | ||
|
||
export type UpdateEvent = BaseEvent<'update', mastodon.v1.Status>; | ||
|
||
export type DeleteEvent = BaseEvent<'delete', string>; | ||
|
||
export type NotificationEvent = BaseEvent< | ||
'notification', | ||
mastodon.v1.Notification | ||
>; | ||
|
||
export type FiltersChangedEvent = BaseEvent<'filters_changed', undefined>; | ||
|
||
export type ConversationEvent = BaseEvent< | ||
'conversation', | ||
mastodon.v1.Conversation | ||
>; | ||
|
||
export type AnnouncementEvent = BaseEvent< | ||
'announcement', | ||
mastodon.v1.Announcement | ||
>; | ||
|
||
export type AnnouncementReactionEvent = BaseEvent< | ||
'announcement.reaction', | ||
mastodon.v1.Reaction | ||
>; | ||
|
||
export type AnnouncementDeleteEvent = BaseEvent<'announcement.delete', string>; | ||
|
||
export type StatusUpdateEvent = BaseEvent<'status.update', mastodon.v1.Status>; | ||
|
||
export type Event = | ||
| UpdateEvent | ||
| DeleteEvent | ||
| NotificationEvent | ||
| FiltersChangedEvent | ||
| ConversationEvent | ||
| AnnouncementEvent | ||
| AnnouncementReactionEvent | ||
| AnnouncementDeleteEvent | ||
| StatusUpdateEvent; |
This file contains 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 * from './client'; | ||
export * from './event'; |
This file contains 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 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 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 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
Oops, something went wrong.