Apply interface for stream listener and websocket

This commit is contained in:
AkiraFukushima 2020-03-09 01:45:47 +09:00
parent 890816f8b9
commit fef1176da2
9 changed files with 89 additions and 66 deletions

View file

@ -1,12 +1,10 @@
import MastodonAPI from './mastodon/api_client'
import StreamListener from './stream_listener'
import WebSocket from './web_socket'
import Response from './response'
import OAuth from './oauth'
import { isCancel, RequestCanceledError } from './cancel'
import { ProxyConfig } from './proxy_config'
//
import generator, { MegalodonInterface } from './megalodon'
import generator, { MegalodonInterface, WebSocketInterface, StreamListenerInterface } from './megalodon'
import Mastodon from './mastodon'
import Pleroma from './pleroma'
import Misskey from './misskey'
@ -14,8 +12,6 @@ import Entity from './entity'
export {
MastodonAPI,
StreamListener,
WebSocket,
Response,
OAuth,
RequestCanceledError,
@ -23,6 +19,8 @@ export {
ProxyConfig,
//
MegalodonInterface,
WebSocketInterface,
StreamListenerInterface,
Mastodon,
Pleroma,
Misskey,

View file

@ -3,9 +3,8 @@ import MastodonAPI from './mastodon/api_client'
import { ProxyConfig } from './proxy_config'
import OAuth from './oauth'
import Response from './response'
import StreamListener from './stream_listener'
import WebSocket from './web_socket'
import { MegalodonInterface, NoImplementedError } from './megalodon'
import WebSocket from './mastodon/web_socket'
import { MegalodonInterface, StreamListenerInterface, NoImplementedError } from './megalodon'
import Entity from './entity'
import { NO_REDIRECT, DEFAULT_SCOPE, DEFAULT_UA } from './default'
@ -1913,27 +1912,27 @@ export default class Mastodon implements MegalodonInterface {
// ======================================
// HTTP Streaming
// ======================================
public userStream(): StreamListener {
public userStream(): StreamListenerInterface {
return this.client.stream('/api/v1/streaming/user')
}
public publicStream(): StreamListener {
public publicStream(): StreamListenerInterface {
return this.client.stream('/api/v1/streaming/public')
}
public localStream(): StreamListener {
public localStream(): StreamListenerInterface {
return this.client.stream('/api/v1/streaming/public/local')
}
public tagStream(tag: string): StreamListener {
public tagStream(tag: string): StreamListenerInterface {
return this.client.stream(`/api/v1/streaming/hashtag?tag=${tag}`)
}
public listStream(list_id: string): StreamListener {
public listStream(list_id: string): StreamListenerInterface {
return this.client.stream(`/api/v1/streaming/list?list=${list_id}`)
}
public directStream(): StreamListener {
public directStream(): StreamListenerInterface {
return this.client.stream('/api/v1/streaming/direct')
}

View file

@ -1,6 +1,6 @@
import axios, { AxiosResponse, CancelTokenSource, AxiosRequestConfig } from 'axios'
import StreamListener from '../stream_listener'
import WebSocket from '../web_socket'
import StreamListener from './stream_listener'
import WebSocket from './web_socket'
import Response from '../response'
import { RequestCanceledError } from '../cancel'
import proxyAgent, { ProxyConfig } from '../proxy_config'

View file

@ -1,9 +1,11 @@
import { EventEmitter } from 'events'
import axios, { CancelTokenSource, AxiosRequestConfig } from 'axios'
import httpAdapter from 'axios/lib/adapters/http'
import { Parser } from './parser'
import proxyAgent, { ProxyConfig } from './proxy_config'
import { Parser } from '../parser'
import proxyAgent, { ProxyConfig } from '../proxy_config'
import Entity from './entity'
import { StreamListenerInterface } from '../megalodon'
import MastodonAPI from './api_client'
const STATUS_CODES_TO_ABORT_ON: Array<number> = [400, 401, 403, 404, 406, 410, 422]
@ -22,7 +24,7 @@ export class StreamListenerError extends Error {
* EventStream
* Listener of text/event-stream. It receives data, and parse when streaming.
*/
export default class StreamListener extends EventEmitter {
export default class StreamListener extends EventEmitter implements StreamListenerInterface {
public url: string
public headers: object
public parser: Parser
@ -161,13 +163,13 @@ export default class StreamListener extends EventEmitter {
**/
private _setupParser() {
this.parser.on('update', (status: Entity.Status) => {
this.emit('update', status)
this.emit('update', MastodonAPI.Converter.status(status))
})
this.parser.on('notification', (notification: Entity.Notification) => {
this.emit('notification', notification)
this.emit('notification', MastodonAPI.Converter.notification(notification))
})
this.parser.on('conversation', (conversation: Entity.Conversation) => {
this.emit('conversation', conversation)
this.emit('conversation', MastodonAPI.Converter.conversation(conversation))
})
this.parser.on('delete', (id: string) => {
this.emit('delete', id)

View file

@ -1,15 +1,17 @@
import WS from 'ws'
import moment, { Moment } from 'moment'
import { EventEmitter } from 'events'
import proxyAgent, { ProxyConfig } from './proxy_config'
import Entity from './entity'
import proxyAgent, { ProxyConfig } from '../proxy_config'
import Entity from '../entity'
import { WebSocketInterface } from '../megalodon'
import MastodonAPI from './api_client'
/**
* WebSocket
* Pleroma is not support streaming. It is support websocket instead of streaming.
* So this class connect to Phoenix websocket for Pleroma.
*/
export default class WebSocket extends EventEmitter {
export default class WebSocket extends EventEmitter implements WebSocketInterface {
public url: string
public stream: string
public params: string | null
@ -232,16 +234,16 @@ export default class WebSocket extends EventEmitter {
*/
private _setupParser() {
this.parser.on('update', (status: Entity.Status) => {
this.emit('update', status)
this.emit('update', MastodonAPI.Converter.status(status))
})
this.parser.on('notification', (notification: Entity.Notification) => {
this.emit('notification', notification)
this.emit('notification', MastodonAPI.Converter.notification(notification))
})
this.parser.on('delete', (id: string) => {
this.emit('delete', id)
})
this.parser.on('conversation', (conversation: Entity.Conversation) => {
this.emit('conversation', conversation)
this.emit('conversation', MastodonAPI.Converter.conversation(conversation))
})
this.parser.on('error', (err: Error) => {
this.emit('parser-error', err)

View file

@ -1,5 +1,3 @@
import StreamListener from './stream_listener'
import WebSocket from './web_socket'
import Response from './response'
import OAuth from './oauth'
import Pleroma from './pleroma'
@ -8,6 +6,26 @@ import Mastodon from './mastodon'
import Entity from './entity'
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'
export interface WebSocketInterface {
start(): void
stop(): void
// EventEmitter
on(event: string | symbol, listener: (...args: any[]) => void): this
once(event: string | symbol, listener: (...args: any[]) => void): this
removeListener(event: string | symbol, listener: (...args: any[]) => void): this
removeAllListeners(event?: string | symbol): this
}
export interface StreamListenerInterface {
start(): void
stop(): void
// EventEmitter
on(event: string | symbol, listener: (...args: any[]) => void): this
once(event: string | symbol, listener: (...args: any[]) => void): this
removeListener(event: string | symbol, listener: (...args: any[]) => void): this
removeAllListeners(event?: string | symbol): this
}
export interface MegalodonInterface {
/**
* Cancel all requests in this instance.
@ -1132,22 +1150,22 @@ export interface MegalodonInterface {
// ======================================
// HTTP Streaming
// ======================================
userStream(): StreamListener
publicStream(): StreamListener
localStream(): StreamListener
tagStream(tag: string): StreamListener
listStream(list_id: string): StreamListener
directStream(): StreamListener
userStream(): StreamListenerInterface
publicStream(): StreamListenerInterface
localStream(): StreamListenerInterface
tagStream(tag: string): StreamListenerInterface
listStream(list_id: string): StreamListenerInterface
directStream(): StreamListenerInterface
// ======================================
// WebSocket
// ======================================
userSocket(): WebSocket
publicSocket(): WebSocket
localSocket(): WebSocket
tagSocket(tag: string): WebSocket
listSocket(list_id: string): WebSocket
directSocket(): WebSocket
userSocket(): WebSocketInterface
publicSocket(): WebSocketInterface
localSocket(): WebSocketInterface
tagSocket(tag: string): WebSocketInterface
listSocket(list_id: string): WebSocketInterface
directSocket(): WebSocketInterface
}
export class NoImplementedError extends Error {

View file

@ -3,9 +3,14 @@ import { DEFAULT_UA } from './default'
import { ProxyConfig } from './proxy_config'
import OAuth from './oauth'
import Response from './response'
import { MegalodonInterface, NoImplementedError, ArgumentError, UnexpectedError } from './megalodon'
import StreamListener from './stream_listener'
import WebSocket from './web_socket'
import {
MegalodonInterface,
StreamListenerInterface,
WebSocketInterface,
NoImplementedError,
ArgumentError,
UnexpectedError
} from './megalodon'
export default class Misskey implements MegalodonInterface {
public client: MisskeyAPI.Client
@ -1830,51 +1835,51 @@ export default class Misskey implements MegalodonInterface {
// ======================================
// HTTP Streaming
// ======================================
public userStream(): StreamListener {
public userStream(): StreamListenerInterface {
throw new NoImplementedError('misskey does not support')
}
public publicStream(): StreamListener {
public publicStream(): StreamListenerInterface {
throw new NoImplementedError('misskey does not support')
}
public localStream(): StreamListener {
public localStream(): StreamListenerInterface {
throw new NoImplementedError('misskey does not support')
}
public tagStream(_tag: string): StreamListener {
public tagStream(_tag: string): StreamListenerInterface {
throw new NoImplementedError('misskey does not support')
}
public listStream(_list_id: string): StreamListener {
public listStream(_list_id: string): StreamListenerInterface {
throw new NoImplementedError('misskey does not support')
}
public directStream(): StreamListener {
public directStream(): StreamListenerInterface {
throw new NoImplementedError('misskey does not support')
}
public userSocket(): WebSocket {
public userSocket(): WebSocketInterface {
throw new NoImplementedError('TODO: implement')
}
public publicSocket(): WebSocket {
public publicSocket(): WebSocketInterface {
throw new NoImplementedError('TODO: implement')
}
public localSocket(): WebSocket {
public localSocket(): WebSocketInterface {
throw new NoImplementedError('TODO: implement')
}
public tagSocket(_tag: string): WebSocket {
public tagSocket(_tag: string): WebSocketInterface {
throw new NoImplementedError('TODO: implement')
}
public listSocket(_list_id: string): WebSocket {
public listSocket(_list_id: string): WebSocketInterface {
throw new NoImplementedError('TODO: implement')
}
public directSocket(): WebSocket {
public directSocket(): WebSocketInterface {
throw new NoImplementedError('TODO: implement')
}
}

View file

@ -1,6 +1,5 @@
import { MegalodonInterface, NoImplementedError } from './megalodon'
import { MegalodonInterface, StreamListenerInterface, NoImplementedError } from './megalodon'
import Mastodon from './mastodon'
import StreamListener from './stream_listener'
import Response from './response'
import Entity from './entity'
import PleromaAPI from './pleroma/api_client'
@ -57,27 +56,27 @@ export default class Pleroma extends Mastodon implements MegalodonInterface {
// ======================================
// HTTP Streaming
// ======================================
public userStream(): StreamListener {
public userStream(): StreamListenerInterface {
throw new NoImplementedError('pleroma does not support')
}
public publicStream(): StreamListener {
public publicStream(): StreamListenerInterface {
throw new NoImplementedError('pleroma does not support')
}
public localStream(): StreamListener {
public localStream(): StreamListenerInterface {
throw new NoImplementedError('pleroma does not support')
}
public tagStream(_tag: string): StreamListener {
public tagStream(_tag: string): StreamListenerInterface {
throw new NoImplementedError('pleroma does not support')
}
public listStream(_list_id: string): StreamListener {
public listStream(_list_id: string): StreamListenerInterface {
throw new NoImplementedError('pleroma does not support')
}
public directStream(): StreamListener {
public directStream(): StreamListenerInterface {
throw new NoImplementedError('pleroma does not support')
}
}

View file

@ -1,4 +1,4 @@
import { Parser } from '@/web_socket'
import { Parser } from '@/mastodon/web_socket'
import Entity from '@/entity'
const account: Entity.Account = {