Merge pull request #240 from h3poteto/streaming_interface
Apply interface for stream listener and websocket
This commit is contained in:
commit
ba43bf9b82
|
@ -1,4 +1,4 @@
|
|||
import generator, { Entity, StreamListener, ProxyConfig } from 'megalodon'
|
||||
import generator, { Entity, StreamListenerInterface, ProxyConfig } from 'megalodon'
|
||||
|
||||
declare var process: {
|
||||
env: {
|
||||
|
@ -21,7 +21,7 @@ const proxy: ProxyConfig = {
|
|||
|
||||
const client = generator('mastodon', BASE_URL, access_token, null, proxy)
|
||||
|
||||
const stream: StreamListener = client.userStream()
|
||||
const stream: StreamListenerInterface = client.userStream()
|
||||
stream.on('connect', _ => {
|
||||
console.log('connect')
|
||||
})
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import generator, { Entity, StreamListener } from 'megalodon'
|
||||
import generator, { Entity, StreamListenerInterface } from 'megalodon'
|
||||
|
||||
declare var process: {
|
||||
env: {
|
||||
|
@ -12,7 +12,7 @@ const access_token: string = process.env.MASTODON_ACCESS_TOKEN
|
|||
|
||||
const client = generator('mastodon', BASE_URL, access_token)
|
||||
|
||||
const stream: StreamListener = client.publicStream()
|
||||
const stream: StreamListenerInterface = client.publicStream()
|
||||
stream.on('connect', _ => {
|
||||
console.log('connect')
|
||||
})
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import generator, { Entity, WebSocket, ProxyConfig } from 'megalodon'
|
||||
import generator, { Entity, WebSocketInterface, ProxyConfig } from 'megalodon'
|
||||
import log4js from 'log4js'
|
||||
|
||||
declare var process: {
|
||||
|
@ -22,7 +22,7 @@ const proxy: ProxyConfig = {
|
|||
|
||||
const client = generator('pleroma', BASE_URL, access_token, null, proxy)
|
||||
|
||||
const stream: WebSocket = client.userSocket()
|
||||
const stream: WebSocketInterface = client.userSocket()
|
||||
|
||||
const logger = log4js.getLogger()
|
||||
logger.level = 'debug'
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import generator, { Entity, WebSocket } from 'megalodon'
|
||||
import generator, { Entity, WebSocketInterface } from 'megalodon'
|
||||
import log4js from 'log4js'
|
||||
|
||||
declare var process: {
|
||||
|
@ -13,7 +13,7 @@ const access_token: string = process.env.PLEROMA_ACCESS_TOKEN
|
|||
|
||||
const client = generator('pleroma', BASE_URL, access_token)
|
||||
|
||||
const stream: WebSocket = client.userSocket()
|
||||
const stream: WebSocketInterface = client.userSocket()
|
||||
|
||||
const logger = log4js.getLogger()
|
||||
logger.level = 'debug'
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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')
|
||||
}
|
||||
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -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)
|
|
@ -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)
|
|
@ -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 {
|
||||
|
|
|
@ -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')
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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')
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { Parser } from '@/web_socket'
|
||||
import { Parser } from '@/mastodon/web_socket'
|
||||
import Entity from '@/entity'
|
||||
|
||||
const account: Entity.Account = {
|
||||
|
|
Loading…
Reference in a new issue