upd: megalodon to v7

This commit is contained in:
Mar0xy 2023-09-24 01:44:53 +02:00
parent b4674ce65c
commit afda15260f
No known key found for this signature in database
GPG key ID: 56569BBE47D2C828
234 changed files with 21334 additions and 7675 deletions

File diff suppressed because it is too large Load diff

View file

@ -1,6 +0,0 @@
namespace MisskeyEntity {
export type GetAll = {
tutorial: number;
defaultNoteVisibility: "public" | "home" | "followers" | "specified";
};
}

View file

@ -1,10 +1,11 @@
namespace MisskeyEntity {
export type Announcement = {
id: string;
createdAt: string;
updatedAt: string;
text: string;
title: string;
isRead?: boolean;
};
export type Announcement = {
id: string
createdAt: string
updatedAt: string | null
text: string
title: string
imageurl: string | null
isRead?: boolean
}
}

View file

@ -1,9 +1,9 @@
namespace MisskeyEntity {
export type App = {
id: string;
name: string;
callbackUrl: string;
permission: Array<string>;
secret: string;
};
export type App = {
id: string
name: string
callbackUrl: string
permission: Array<string>
secret: string
}
}

View file

@ -1,10 +1,10 @@
/// <reference path="userDetail.ts" />
namespace MisskeyEntity {
export type Blocking = {
id: string;
createdAt: string;
blockeeId: string;
blockee: UserDetail;
};
export type Blocking = {
id: string
createdAt: string
blockeeId: string
blockee: UserDetail
}
}

View file

@ -1,7 +1,7 @@
/// <reference path="note.ts" />
namespace MisskeyEntity {
export type CreatedNote = {
createdNote: Note;
};
export type CreatedNote = {
createdNote: Note
}
}

View file

@ -1,9 +1,8 @@
namespace MisskeyEntity {
export type Emoji = {
name: string;
host: string | null;
url: string;
aliases: Array<string>;
category: string;
};
export type Emoji = {
name: string
url: string
aliases: Array<string>
category: string
}
}

View file

@ -1,10 +1,10 @@
/// <reference path="note.ts" />
namespace MisskeyEntity {
export type Favorite = {
id: string;
createdAt: string;
noteId: string;
note: Note;
};
export type Favorite = {
id: string
createdAt: string
noteId: string
note: Note
}
}

View file

@ -1,7 +0,0 @@
namespace MisskeyEntity {
export type Field = {
name: string;
value: string;
verified?: string;
};
}

View file

@ -1,20 +1,18 @@
namespace MisskeyEntity {
export type File = {
id: string;
createdAt: string;
name: string;
type: string;
md5: string;
size: number;
isSensitive: boolean;
properties: {
width: number;
height: number;
avgColor: string;
};
url: string;
thumbnailUrl: string;
comment: string;
blurhash: string;
};
export type File = {
id: string
createdAt: string
name: string
type: string
md5: string
size: number
isSensitive: boolean
properties: {
width: number
height: number
avgColor: string
}
url: string
thumbnailUrl: string
}
}

View file

@ -1,9 +1,9 @@
/// <reference path="user.ts" />
namespace MisskeyEntity {
export type FollowRequest = {
id: string;
follower: User;
followee: User;
};
export type FollowRequest = {
id: string
follower: User
followee: User
}
}

View file

@ -1,11 +1,11 @@
/// <reference path="userDetail.ts" />
namespace MisskeyEntity {
export type Follower = {
id: string;
createdAt: string;
followeeId: string;
followerId: string;
follower: UserDetail;
};
export type Follower = {
id: string
createdAt: string
followeeId: string
followerId: string
follower: UserDetail
}
}

View file

@ -1,11 +1,11 @@
/// <reference path="userDetail.ts" />
namespace MisskeyEntity {
export type Following = {
id: string;
createdAt: string;
followeeId: string;
followerId: string;
followee: UserDetail;
};
export type Following = {
id: string
createdAt: string
followeeId: string
followerId: string
followee: UserDetail
}
}

View file

@ -1,7 +1,7 @@
namespace MisskeyEntity {
export type Hashtag = {
tag: string;
chart: Array<number>;
usersCount: number;
};
export type Hashtag = {
tag: string
chart: Array<number>
usersCount: number
}
}

View file

@ -1,8 +1,8 @@
namespace MisskeyEntity {
export type List = {
id: string;
createdAt: string;
name: string;
userIds: Array<string>;
};
export type List = {
id: string
createdAt: string
name: string
userIds: Array<string>
}
}

View file

@ -1,18 +1,47 @@
/// <reference path="emoji.ts" />
namespace MisskeyEntity {
export type Meta = {
maintainerName: string;
maintainerEmail: string;
name: string;
version: string;
uri: string;
description: string;
langs: Array<string>;
disableRegistration: boolean;
disableLocalTimeline: boolean;
bannerUrl: string;
maxNoteTextLength: 3000;
emojis: Array<Emoji>;
};
export type Meta = {
maintainerName: string
maintainerEmail: string
name: string
version: string
uri: string
description: string
langs: Array<string>
disableRegistration: boolean
disableLocalTimeline: boolean
bannerUrl: string
maxNoteTextLength: number
emojis: Array<Emoji>
policies: {
gtlAvailable: boolean
ltlAvailable: boolean
canPublicNote: boolean
canInvite: boolean
canManageCustomEmojis: boolean
canHideAds: boolean
driveCapacityMb: number
pinLimit: number
antennaLimit: number
wordMuteLimit: number
webhookLimit: number
clipLimit: number
noteEachClipsLimit: number
userListLimit: number
userEachUserListsLimit: number
rateLimitFactor: number
}
features: {
registration: boolean
emailRequiredForSignup: boolean
elasticsearch: boolean
hcaptcha: boolean
recaptcha: boolean
turnstile: boolean
objectStorage: boolean
serviceWorker: boolean
miauth: boolean
}
}
}

View file

@ -1,10 +1,10 @@
/// <reference path="userDetail.ts" />
namespace MisskeyEntity {
export type Mute = {
id: string;
createdAt: string;
muteeId: string;
mutee: UserDetail;
};
export type Mute = {
id: string
createdAt: string
muteeId: string
mutee: UserDetail
}
}

View file

@ -4,29 +4,31 @@
/// <reference path="poll.ts" />
namespace MisskeyEntity {
export type Note = {
id: string;
createdAt: string;
userId: string;
user: User;
text: string | null;
cw: string | null;
visibility: "public" | "home" | "followers" | "specified";
renoteCount: number;
repliesCount: number;
reactions: { [key: string]: number };
emojis: Array<Emoji>;
fileIds: Array<string>;
files: Array<File>;
replyId: string | null;
renoteId: string | null;
uri?: string;
reply?: Note;
renote?: Note;
viaMobile?: boolean;
tags?: Array<string>;
poll?: Poll;
mentions?: Array<string>;
myReaction?: string;
};
export type Note = {
id: string
createdAt: string
userId: string
user: User
text: string | null
cw: string | null
visibility: 'public' | 'home' | 'followers' | 'specified'
renoteCount: number
repliesCount: number
reactions: { [key: string]: number }
// This field includes only remote emojis
reactionEmojis: { [key: string]: string }
emojis: Array<Emoji> | { [key: string]: string }
fileIds: Array<string>
files: Array<File>
replyId: string | null
renoteId: string | null
uri?: string
reply?: Note
renote?: Note
viaMobile?: boolean
tags?: Array<string>
poll?: Poll
mentions?: Array<string>
myReaction?: string
}
}

View file

@ -2,16 +2,16 @@
/// <reference path="note.ts" />
namespace MisskeyEntity {
export type Notification = {
id: string;
createdAt: string;
// https://github.com/syuilo/misskey/blob/056942391aee135eb6c77aaa63f6ed5741d701a6/src/models/entities/notification.ts#L50-L62
type: NotificationType;
userId: string;
user: User;
note?: Note;
reaction?: string;
};
export type Notification = {
id: string
createdAt: string
// https://github.com/syuilo/misskey/blob/056942391aee135eb6c77aaa63f6ed5741d701a6/src/models/entities/notification.ts#L50-L62
type: NotificationType
userId: string
user: User
note?: Note
reaction?: string
}
export type NotificationType = string;
export type NotificationType = string
}

View file

@ -1,13 +1,13 @@
namespace MisskeyEntity {
export type Choice = {
text: string;
votes: number;
isVoted: boolean;
};
export type Choice = {
text: string
votes: number
isVoted: boolean
}
export type Poll = {
multiple: boolean;
expiresAt: string;
choices: Array<Choice>;
};
export type Poll = {
multiple: boolean
expiresAt: string
choices: Array<Choice>
}
}

View file

@ -1,11 +1,10 @@
/// <reference path="user.ts" />
namespace MisskeyEntity {
export type Reaction = {
id: string;
createdAt: string;
user: User;
url?: string;
type: string;
};
export type Reaction = {
id: string
createdAt: string
user: User
type: string
}
}

View file

@ -1,12 +1,12 @@
namespace MisskeyEntity {
export type Relation = {
id: string;
isFollowing: boolean;
hasPendingFollowRequestFromYou: boolean;
hasPendingFollowRequestToYou: boolean;
isFollowed: boolean;
isBlocking: boolean;
isBlocked: boolean;
isMuted: boolean;
};
export type Relation = {
id: string
isFollowing: boolean
hasPendingFollowRequestFromYou: boolean
hasPendingFollowRequestToYou: boolean
isFollowed: boolean
isBlocking: boolean
isBlocked: boolean
isMuted: boolean
}
}

View file

@ -1,6 +1,6 @@
namespace MisskeyEntity {
export type Session = {
token: string;
url: string;
};
export type Session = {
token: string
url: string
}
}

View file

@ -1,7 +0,0 @@
namespace MisskeyEntity {
export type State = {
isFavorited: boolean;
isMutedThread: boolean;
isWatching: boolean;
};
}

View file

@ -1,9 +1,9 @@
namespace MisskeyEntity {
export type Stats = {
notesCount: number;
originalNotesCount: number;
usersCount: number;
originalUsersCount: number;
instances: number;
};
export type Stats = {
notesCount: number
originalNotesCount: number
usersCount: number
originalUsersCount: number
instances: number
}
}

View file

@ -1,13 +1,13 @@
/// <reference path="emoji.ts" />
namespace MisskeyEntity {
export type User = {
id: string;
name: string;
username: string;
host: string | null;
avatarUrl: string;
avatarColor: string;
emojis: Array<Emoji>;
};
export type User = {
id: string
name: string
username: string
host: string | null
avatarUrl: string
avatarColor: string
emojis: Array<Emoji> | { [key: string]: string }
}
}

View file

@ -1,34 +1,32 @@
/// <reference path="emoji.ts" />
/// <reference path="field.ts" />
/// <reference path="note.ts" />
namespace MisskeyEntity {
export type UserDetail = {
id: string;
name: string;
username: string;
host: string | null;
avatarUrl: string;
avatarColor: string;
isAdmin: boolean;
isModerator: boolean;
isBot: boolean;
isCat: boolean;
emojis: Array<Emoji>;
createdAt: string;
bannerUrl: string;
bannerColor: string;
isLocked: boolean;
isSilenced: boolean;
isSuspended: boolean;
description: string;
followersCount: number;
followingCount: number;
notesCount: number;
avatarId: string;
bannerId: string;
pinnedNoteIds?: Array<string>;
pinnedNotes?: Array<Note>;
fields: Array<Field>;
};
export type UserDetail = {
id: string
name: string
username: string
host: string | null
avatarUrl: string
avatarColor: string
isAdmin: boolean
isModerator: boolean
isBot: boolean
isCat: boolean
emojis: Array<Emoji> | { [key: string]: string }
createdAt: string
bannerUrl: string
bannerColor: string
isLocked: boolean
isSilenced: boolean
isSuspended: boolean
description: string
followersCount: number
followingCount: number
notesCount: number
avatarId: string
bannerId: string
pinnedNoteIds?: Array<string>
pinnedNotes?: Array<Note>
}
}

View file

@ -1,36 +0,0 @@
/// <reference path="emoji.ts" />
/// <reference path="field.ts" />
/// <reference path="note.ts" />
namespace MisskeyEntity {
export type UserDetailMe = {
id: string;
name: string;
username: string;
host: string | null;
avatarUrl: string;
avatarColor: string;
isAdmin: boolean;
isModerator: boolean;
isBot: boolean;
isCat: boolean;
emojis: Array<Emoji>;
createdAt: string;
bannerUrl: string;
bannerColor: string;
isLocked: boolean;
isSilenced: boolean;
isSuspended: boolean;
description: string;
followersCount: number;
followingCount: number;
notesCount: number;
avatarId: string;
bannerId: string;
pinnedNoteIds?: Array<string>;
pinnedNotes?: Array<Note>;
fields: Array<Field>;
alwaysMarkNsfw: boolean;
lang: string | null;
};
}

View file

@ -1,8 +1,8 @@
/// <reference path="user.ts" />
namespace MisskeyEntity {
export type UserKey = {
accessToken: string;
user: User;
};
export type UserKey = {
accessToken: string
user: User
}
}

View file

@ -1,10 +1,9 @@
/// <reference path="entities/app.ts" />
/// <reference path="entities/announcement.ts" />
/// <reference path="entities/app.ts" />
/// <reference path="entities/blocking.ts" />
/// <reference path="entities/createdNote.ts" />
/// <reference path="entities/emoji.ts" />
/// <reference path="entities/favorite.ts" />
/// <reference path="entities/field.ts" />
/// <reference path="entities/file.ts" />
/// <reference path="entities/follower.ts" />
/// <reference path="entities/following.ts" />
@ -20,9 +19,8 @@
/// <reference path="entities/relation.ts" />
/// <reference path="entities/user.ts" />
/// <reference path="entities/userDetail.ts" />
/// <reference path="entities/userDetailMe.ts" />
/// <reference path="entities/userkey.ts" />
/// <reference path="entities/session.ts" />
/// <reference path="entities/stats.ts" />
export default MisskeyEntity;
export default MisskeyEntity

View file

@ -1,18 +1,16 @@
import MisskeyEntity from "./entity";
import MisskeyEntity from './entity'
namespace MisskeyNotificationType {
export const Follow: MisskeyEntity.NotificationType = "follow";
export const Mention: MisskeyEntity.NotificationType = "mention";
export const Reply: MisskeyEntity.NotificationType = "reply";
export const Renote: MisskeyEntity.NotificationType = "renote";
export const Quote: MisskeyEntity.NotificationType = "quote";
export const Reaction: MisskeyEntity.NotificationType = "favourite";
export const PollEnded: MisskeyEntity.NotificationType = "pollEnded";
export const ReceiveFollowRequest: MisskeyEntity.NotificationType =
"receiveFollowRequest";
export const FollowRequestAccepted: MisskeyEntity.NotificationType =
"followRequestAccepted";
export const GroupInvited: MisskeyEntity.NotificationType = "groupInvited";
export const Follow: MisskeyEntity.NotificationType = 'follow'
export const Mention: MisskeyEntity.NotificationType = 'mention'
export const Reply: MisskeyEntity.NotificationType = 'reply'
export const Renote: MisskeyEntity.NotificationType = 'renote'
export const Quote: MisskeyEntity.NotificationType = 'quote'
export const Reaction: MisskeyEntity.NotificationType = 'reaction'
export const PollVote: MisskeyEntity.NotificationType = 'pollVote'
export const ReceiveFollowRequest: MisskeyEntity.NotificationType = 'receiveFollowRequest'
export const FollowRequestAccepted: MisskeyEntity.NotificationType = 'followRequestAccepted'
export const GroupInvited: MisskeyEntity.NotificationType = 'groupInvited'
}
export default MisskeyNotificationType;
export default MisskeyNotificationType

View file

@ -1,365 +1,328 @@
import WS from "ws";
import dayjs, { Dayjs } from "dayjs";
import { v4 as uuid } from "uuid";
import { EventEmitter } from "events";
import { WebSocketInterface } from "../megalodon";
import proxyAgent, { ProxyConfig } from "../proxy_config";
import MisskeyAPI from "./api_client";
import WS from 'ws'
import dayjs, { Dayjs } from 'dayjs'
import { v4 as uuid } from 'uuid'
import { EventEmitter } from 'events'
import { WebSocketInterface } from '../megalodon'
import proxyAgent, { ProxyConfig } from '../proxy_config'
import MisskeyAPI from './api_client'
import { UnknownNotificationTypeError } from '../notification'
/**
* WebSocket
* Misskey is not support http streaming. It supports websocket instead of streaming.
* So this class connect to Misskey server with WebSocket.
*/
export default class WebSocket
extends EventEmitter
implements WebSocketInterface
{
public url: string;
public channel:
| "user"
| "localTimeline"
| "hybridTimeline"
| "globalTimeline"
| "conversation"
| "list";
public parser: any;
public headers: { [key: string]: string };
public proxyConfig: ProxyConfig | false = false;
public listId: string | null = null;
private _converter: MisskeyAPI.Converter;
private _accessToken: string;
private _reconnectInterval: number;
private _reconnectMaxAttempts: number;
private _reconnectCurrentAttempts: number;
private _connectionClosed: boolean;
private _client: WS | null = null;
private _channelID: string;
private _pongReceivedTimestamp: Dayjs;
private _heartbeatInterval = 60000;
private _pongWaiting = false;
export default class WebSocket extends EventEmitter implements WebSocketInterface {
public url: string
public channel: 'user' | 'localTimeline' | 'hybridTimeline' | 'globalTimeline' | 'conversation' | 'list'
public parser: Parser
public headers: { [key: string]: string }
public proxyConfig: ProxyConfig | false = false
public listId: string | null = null
private _accessToken: string
private _reconnectInterval: number
private _reconnectMaxAttempts: number
private _reconnectCurrentAttempts: number
private _connectionClosed: boolean
private _client: WS | null = null
private _channelID: string
private _pongReceivedTimestamp: Dayjs
private _heartbeatInterval: number = 60000
private _pongWaiting: boolean = false
/**
* @param url Full url of websocket: e.g. wss://misskey.io/streaming
* @param channel Channel name is user, localTimeline, hybridTimeline, globalTimeline, conversation or list.
* @param accessToken The access token.
* @param listId This parameter is required when you specify list as channel.
*/
constructor(
url: string,
channel:
| "user"
| "localTimeline"
| "hybridTimeline"
| "globalTimeline"
| "conversation"
| "list",
accessToken: string,
listId: string | undefined,
userAgent: string,
proxyConfig: ProxyConfig | false = false,
converter: MisskeyAPI.Converter,
) {
super();
this.url = url;
this.parser = new Parser();
this.channel = channel;
this.headers = {
"User-Agent": userAgent,
};
if (listId === undefined) {
this.listId = null;
} else {
this.listId = listId;
}
this.proxyConfig = proxyConfig;
this._accessToken = accessToken;
this._reconnectInterval = 10000;
this._reconnectMaxAttempts = Infinity;
this._reconnectCurrentAttempts = 0;
this._connectionClosed = false;
this._channelID = uuid();
this._pongReceivedTimestamp = dayjs();
this._converter = converter;
}
/**
* @param url Full url of websocket: e.g. wss://misskey.io/streaming
* @param channel Channel name is user, localTimeline, hybridTimeline, globalTimeline, conversation or list.
* @param accessToken The access token.
* @param listId This parameter is required when you specify list as channel.
*/
constructor(
url: string,
channel: 'user' | 'localTimeline' | 'hybridTimeline' | 'globalTimeline' | 'conversation' | 'list',
accessToken: string,
listId: string | undefined,
userAgent: string,
proxyConfig: ProxyConfig | false = false
) {
super()
this.url = url
this.parser = new Parser()
this.channel = channel
this.headers = {
'User-Agent': userAgent
}
if (listId === undefined) {
this.listId = null
} else {
this.listId = listId
}
this.proxyConfig = proxyConfig
this._accessToken = accessToken
this._reconnectInterval = 10000
this._reconnectMaxAttempts = Infinity
this._reconnectCurrentAttempts = 0
this._connectionClosed = false
this._channelID = uuid()
this._pongReceivedTimestamp = dayjs()
}
/**
* Start websocket connection.
*/
public start() {
this._connectionClosed = false;
this._resetRetryParams();
this._startWebSocketConnection();
}
/**
* Start websocket connection.
*/
public start() {
this._connectionClosed = false
this._resetRetryParams()
this._startWebSocketConnection()
}
private baseUrlToHost(baseUrl: string): string {
return baseUrl.replace("https://", "");
}
/**
* Reset connection and start new websocket connection.
*/
private _startWebSocketConnection() {
this._resetConnection()
this._setupParser()
this._client = this._connect()
this._bindSocket(this._client)
}
/**
* Reset connection and start new websocket connection.
*/
private _startWebSocketConnection() {
this._resetConnection();
this._setupParser();
this._client = this._connect();
this._bindSocket(this._client);
}
/**
* Stop current connection.
*/
public stop() {
this._connectionClosed = true
this._resetConnection()
this._resetRetryParams()
}
/**
* Stop current connection.
*/
public stop() {
this._connectionClosed = true;
this._resetConnection();
this._resetRetryParams();
}
/**
* Clean up current connection, and listeners.
*/
private _resetConnection() {
if (this._client) {
this._client.close(1000)
this._client.removeAllListeners()
this._client = null
}
/**
* Clean up current connection, and listeners.
*/
private _resetConnection() {
if (this._client) {
this._client.close(1000);
this._client.removeAllListeners();
this._client = null;
}
if (this.parser) {
this.parser.removeAllListeners()
}
}
if (this.parser) {
this.parser.removeAllListeners();
}
}
/**
* Resets the parameters used in reconnect.
*/
private _resetRetryParams() {
this._reconnectCurrentAttempts = 0
}
/**
* Resets the parameters used in reconnect.
*/
private _resetRetryParams() {
this._reconnectCurrentAttempts = 0;
}
/**
* Connect to the endpoint.
*/
private _connect(): WS {
let options: WS.ClientOptions = {
headers: this.headers
}
if (this.proxyConfig) {
options = Object.assign(options, {
agent: proxyAgent(this.proxyConfig)
})
}
const cli: WS = new WS(`${this.url}?i=${this._accessToken}`, options)
return cli
}
/**
* Connect to the endpoint.
*/
private _connect(): WS {
let options: WS.ClientOptions = {
headers: this.headers,
};
if (this.proxyConfig) {
options = Object.assign(options, {
agent: proxyAgent(this.proxyConfig),
});
}
const cli: WS = new WS(`${this.url}?i=${this._accessToken}`, options);
return cli;
}
/**
* Connect specified channels in websocket.
*/
private _channel() {
if (!this._client) {
return
}
switch (this.channel) {
case 'conversation':
this._client.send(
JSON.stringify({
type: 'connect',
body: {
channel: 'main',
id: this._channelID
}
})
)
break
case 'user':
this._client.send(
JSON.stringify({
type: 'connect',
body: {
channel: 'main',
id: this._channelID
}
})
)
this._client.send(
JSON.stringify({
type: 'connect',
body: {
channel: 'homeTimeline',
id: this._channelID
}
})
)
break
case 'list':
this._client.send(
JSON.stringify({
type: 'connect',
body: {
channel: 'userList',
id: this._channelID,
params: {
listId: this.listId
}
}
})
)
break
default:
this._client.send(
JSON.stringify({
type: 'connect',
body: {
channel: this.channel,
id: this._channelID
}
})
)
break
}
}
/**
* Connect specified channels in websocket.
*/
private _channel() {
if (!this._client) {
return;
}
switch (this.channel) {
case "conversation":
this._client.send(
JSON.stringify({
type: "connect",
body: {
channel: "main",
id: this._channelID,
},
}),
);
break;
case "user":
this._client.send(
JSON.stringify({
type: "connect",
body: {
channel: "main",
id: this._channelID,
},
}),
);
this._client.send(
JSON.stringify({
type: "connect",
body: {
channel: "homeTimeline",
id: this._channelID,
},
}),
);
break;
case "list":
this._client.send(
JSON.stringify({
type: "connect",
body: {
channel: "userList",
id: this._channelID,
params: {
listId: this.listId,
},
},
}),
);
break;
default:
this._client.send(
JSON.stringify({
type: "connect",
body: {
channel: this.channel,
id: this._channelID,
},
}),
);
break;
}
}
/**
* Reconnects to the same endpoint.
*/
/**
* Reconnects to the same endpoint.
*/
private _reconnect() {
setTimeout(() => {
// Skip reconnect when client is connecting.
// https://github.com/websockets/ws/blob/7.2.1/lib/websocket.js#L365
if (this._client && this._client.readyState === WS.CONNECTING) {
return
}
private _reconnect() {
setTimeout(() => {
// Skip reconnect when client is connecting.
// https://github.com/websockets/ws/blob/7.2.1/lib/websocket.js#L365
if (this._client && this._client.readyState === WS.CONNECTING) {
return;
}
if (this._reconnectCurrentAttempts < this._reconnectMaxAttempts) {
this._reconnectCurrentAttempts++
this._clearBinding()
if (this._client) {
// In reconnect, we want to close the connection immediately,
// because recoonect is necessary when some problems occur.
this._client.terminate()
}
// Call connect methods
console.log('Reconnecting')
this._client = this._connect()
this._bindSocket(this._client)
}
}, this._reconnectInterval)
}
if (this._reconnectCurrentAttempts < this._reconnectMaxAttempts) {
this._reconnectCurrentAttempts++;
this._clearBinding();
if (this._client) {
// In reconnect, we want to close the connection immediately,
// because recoonect is necessary when some problems occur.
this._client.terminate();
}
// Call connect methods
console.log("Reconnecting");
this._client = this._connect();
this._bindSocket(this._client);
}
}, this._reconnectInterval);
}
/**
* Clear binding event for websocket client.
*/
private _clearBinding() {
if (this._client) {
this._client.removeAllListeners('close')
this._client.removeAllListeners('pong')
this._client.removeAllListeners('open')
this._client.removeAllListeners('message')
this._client.removeAllListeners('error')
}
}
/**
* Clear binding event for websocket client.
*/
private _clearBinding() {
if (this._client) {
this._client.removeAllListeners("close");
this._client.removeAllListeners("pong");
this._client.removeAllListeners("open");
this._client.removeAllListeners("message");
this._client.removeAllListeners("error");
}
}
/**
* Bind event for web socket client.
* @param client A WebSocket instance.
*/
private _bindSocket(client: WS) {
client.on('close', (code: number, _reason: Buffer) => {
if (code === 1000) {
this.emit('close', {})
} else {
console.log(`Closed connection with ${code}`)
if (!this._connectionClosed) {
this._reconnect()
}
}
})
client.on('pong', () => {
this._pongWaiting = false
this.emit('pong', {})
this._pongReceivedTimestamp = dayjs()
// It is required to anonymous function since get this scope in checkAlive.
setTimeout(() => this._checkAlive(this._pongReceivedTimestamp), this._heartbeatInterval)
})
client.on('open', () => {
this.emit('connect', {})
this._channel()
// Call first ping event.
setTimeout(() => {
client.ping('')
}, 10000)
})
client.on('message', (data: WS.Data, isBinary: boolean) => {
this.parser.parse(data, isBinary, this._channelID)
})
client.on('error', (err: Error) => {
this.emit('error', err)
})
}
/**
* Bind event for web socket client.
* @param client A WebSocket instance.
*/
private _bindSocket(client: WS) {
client.on("close", (code: number, _reason: Buffer) => {
if (code === 1000) {
this.emit("close", {});
} else {
console.log(`Closed connection with ${code}`);
if (!this._connectionClosed) {
this._reconnect();
}
}
});
client.on("pong", () => {
this._pongWaiting = false;
this.emit("pong", {});
this._pongReceivedTimestamp = dayjs();
// It is required to anonymous function since get this scope in checkAlive.
setTimeout(
() => this._checkAlive(this._pongReceivedTimestamp),
this._heartbeatInterval,
);
});
client.on("open", () => {
this.emit("connect", {});
this._channel();
// Call first ping event.
setTimeout(() => {
client.ping("");
}, 10000);
});
client.on("message", (data: WS.Data, isBinary: boolean) => {
this.parser.parse(data, isBinary, this._channelID);
});
client.on("error", (err: Error) => {
this.emit("error", err);
});
}
/**
* Set up parser when receive message.
*/
private _setupParser() {
this.parser.on('update', (note: MisskeyAPI.Entity.Note) => {
this.emit('update', MisskeyAPI.Converter.note(note))
})
this.parser.on('notification', (notification: MisskeyAPI.Entity.Notification) => {
const n = MisskeyAPI.Converter.notification(notification)
if (n instanceof UnknownNotificationTypeError) {
console.warn(`Unknown notification event has received: ${notification}`)
} else {
this.emit('notification', n)
}
})
this.parser.on('conversation', (note: MisskeyAPI.Entity.Note) => {
this.emit('conversation', MisskeyAPI.Converter.noteToConversation(note))
})
this.parser.on('error', (err: Error) => {
this.emit('parser-error', err)
})
}
/**
* Set up parser when receive message.
*/
private _setupParser() {
this.parser.on("update", (note: MisskeyAPI.Entity.Note) => {
this.emit(
"update",
this._converter.note(note, this.baseUrlToHost(this.url)),
);
});
this.parser.on(
"notification",
(notification: MisskeyAPI.Entity.Notification) => {
this.emit(
"notification",
this._converter.notification(
notification,
this.baseUrlToHost(this.url),
),
);
},
);
this.parser.on("conversation", (note: MisskeyAPI.Entity.Note) => {
this.emit(
"conversation",
this._converter.noteToConversation(note, this.baseUrlToHost(this.url)),
);
});
this.parser.on("error", (err: Error) => {
this.emit("parser-error", err);
});
}
/**
* Call ping and wait to pong.
*/
private _checkAlive(timestamp: Dayjs) {
const now: Dayjs = dayjs();
// Block multiple calling, if multiple pong event occur.
// It the duration is less than interval, through ping.
if (
now.diff(timestamp) > this._heartbeatInterval - 1000 &&
!this._connectionClosed
) {
// Skip ping when client is connecting.
// https://github.com/websockets/ws/blob/7.2.1/lib/websocket.js#L289
if (this._client && this._client.readyState !== WS.CONNECTING) {
this._pongWaiting = true;
this._client.ping("");
setTimeout(() => {
if (this._pongWaiting) {
this._pongWaiting = false;
this._reconnect();
}
}, 10000);
}
}
}
/**
* Call ping and wait to pong.
*/
private _checkAlive(timestamp: Dayjs) {
const now: Dayjs = dayjs()
// Block multiple calling, if multiple pong event occur.
// It the duration is less than interval, through ping.
if (now.diff(timestamp) > this._heartbeatInterval - 1000 && !this._connectionClosed) {
// Skip ping when client is connecting.
// https://github.com/websockets/ws/blob/7.2.1/lib/websocket.js#L289
if (this._client && this._client.readyState !== WS.CONNECTING) {
this._pongWaiting = true
this._client.ping('')
setTimeout(() => {
if (this._pongWaiting) {
this._pongWaiting = false
this._reconnect()
}
}, 10000)
}
}
}
}
/**
@ -367,92 +330,84 @@ export default class WebSocket
* This class provides parser for websocket message.
*/
export class Parser extends EventEmitter {
/**
* @param message Message body of websocket.
* @param channelID Parse only messages which has same channelID.
*/
public parse(data: WS.Data, isBinary: boolean, channelID: string) {
const message = isBinary ? data : data.toString();
if (typeof message !== "string") {
this.emit("heartbeat", {});
return;
}
/**
* @param message Message body of websocket.
* @param channelID Parse only messages which has same channelID.
*/
public parse(data: WS.Data, isBinary: boolean, channelID: string) {
const message = isBinary ? data : data.toString()
if (typeof message !== 'string') {
this.emit('heartbeat', {})
return
}
if (message === "") {
this.emit("heartbeat", {});
return;
}
if (message === '') {
this.emit('heartbeat', {})
return
}
let obj: {
type: string;
body: {
id: string;
type: string;
body: any;
};
};
let body: {
id: string;
type: string;
body: any;
};
let obj: {
type: string
body: {
id: string
type: string
body: any
}
}
let body: {
id: string
type: string
body: any
}
try {
obj = JSON.parse(message);
if (obj.type !== "channel") {
return;
}
if (!obj.body) {
return;
}
body = obj.body;
if (body.id !== channelID) {
return;
}
} catch (err) {
this.emit(
"error",
new Error(
`Error parsing websocket reply: ${message}, error message: ${err}`,
),
);
return;
}
try {
obj = JSON.parse(message)
if (obj.type !== 'channel') {
return
}
if (!obj.body) {
return
}
body = obj.body
if (body.id !== channelID) {
return
}
} catch (err) {
this.emit('error', new Error(`Error parsing websocket reply: ${message}, error message: ${err}`))
return
}
switch (body.type) {
case "note":
this.emit("update", body.body as MisskeyAPI.Entity.Note);
break;
case "notification":
this.emit("notification", body.body as MisskeyAPI.Entity.Notification);
break;
case "mention": {
const note = body.body as MisskeyAPI.Entity.Note;
if (note.visibility === "specified") {
this.emit("conversation", note);
}
break;
}
// When renote and followed event, the same notification will be received.
case "renote":
case "followed":
case "follow":
case "unfollow":
case "receiveFollowRequest":
case "meUpdated":
case "readAllNotifications":
case "readAllUnreadSpecifiedNotes":
case "readAllAntennas":
case "readAllUnreadMentions":
case "unreadNotification":
// Ignore these events
break;
default:
this.emit(
"error",
new Error(`Unknown event has received: ${JSON.stringify(body)}`),
);
break;
}
}
switch (body.type) {
case 'note':
this.emit('update', body.body as MisskeyAPI.Entity.Note)
break
case 'notification':
this.emit('notification', body.body as MisskeyAPI.Entity.Notification)
break
case 'mention': {
const note = body.body as MisskeyAPI.Entity.Note
if (note.visibility === 'specified') {
this.emit('conversation', note)
}
break
}
// When renote and followed event, the same notification will be received.
case 'renote':
case 'followed':
case 'follow':
case 'unfollow':
case 'receiveFollowRequest':
case 'meUpdated':
case 'readAllNotifications':
case 'readAllUnreadSpecifiedNotes':
case 'readAllAntennas':
case 'readAllUnreadMentions':
case 'unreadNotification':
// Ignore these events
break
default:
this.emit('error', new Error(`Unknown event has received: ${JSON.stringify(body)}`))
break
}
}
}