diff --git a/example/typescript/src/mastodon/proxy_streaming.ts b/example/typescript/src/mastodon/proxy_streaming.ts index 824098694..d0f8eba1f 100644 --- a/example/typescript/src/mastodon/proxy_streaming.ts +++ b/example/typescript/src/mastodon/proxy_streaming.ts @@ -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') }) diff --git a/example/typescript/src/mastodon/streaming.ts b/example/typescript/src/mastodon/streaming.ts index 98de217cd..b4c16719b 100644 --- a/example/typescript/src/mastodon/streaming.ts +++ b/example/typescript/src/mastodon/streaming.ts @@ -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') }) diff --git a/example/typescript/src/pleroma/proxy_web_socket.ts b/example/typescript/src/pleroma/proxy_web_socket.ts index 759f20df7..9aabb0d6c 100644 --- a/example/typescript/src/pleroma/proxy_web_socket.ts +++ b/example/typescript/src/pleroma/proxy_web_socket.ts @@ -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' diff --git a/example/typescript/src/pleroma/web_socket.ts b/example/typescript/src/pleroma/web_socket.ts index 9cd25e262..4a54756a9 100644 --- a/example/typescript/src/pleroma/web_socket.ts +++ b/example/typescript/src/pleroma/web_socket.ts @@ -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' diff --git a/src/index.ts b/src/index.ts index 30c8b9e77..e4e37114f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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, diff --git a/src/mastodon.ts b/src/mastodon.ts index 71df21a93..95d41080c 100644 --- a/src/mastodon.ts +++ b/src/mastodon.ts @@ -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') } diff --git a/src/mastodon/api_client.ts b/src/mastodon/api_client.ts index 31db72714..da1ea4d23 100644 --- a/src/mastodon/api_client.ts +++ b/src/mastodon/api_client.ts @@ -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' diff --git a/src/stream_listener.ts b/src/mastodon/stream_listener.ts similarity index 90% rename from src/stream_listener.ts rename to src/mastodon/stream_listener.ts index 71d8579ce..731ff8583 100644 --- a/src/stream_listener.ts +++ b/src/mastodon/stream_listener.ts @@ -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 = [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) diff --git a/src/web_socket.ts b/src/mastodon/web_socket.ts similarity index 94% rename from src/web_socket.ts rename to src/mastodon/web_socket.ts index 10ceacad7..07c6f52c8 100644 --- a/src/web_socket.ts +++ b/src/mastodon/web_socket.ts @@ -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) diff --git a/src/megalodon.ts b/src/megalodon.ts index 52b24daf7..891f5c6f4 100644 --- a/src/megalodon.ts +++ b/src/megalodon.ts @@ -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 { diff --git a/src/misskey.ts b/src/misskey.ts index 3ee0ce0d8..835c5e325 100644 --- a/src/misskey.ts +++ b/src/misskey.ts @@ -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') } } diff --git a/src/pleroma.ts b/src/pleroma.ts index 8d2833a9f..d412c0c00 100644 --- a/src/pleroma.ts +++ b/src/pleroma.ts @@ -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') } } diff --git a/test/unit/webo_socket.spec.ts b/test/unit/webo_socket.spec.ts index 5000c584e..faa5b1f16 100644 --- a/test/unit/webo_socket.spec.ts +++ b/test/unit/webo_socket.spec.ts @@ -1,4 +1,4 @@ -import { Parser } from '@/web_socket' +import { Parser } from '@/mastodon/web_socket' import Entity from '@/entity' const account: Entity.Account = {