Update misskey-js/streaming.ts

This commit is contained in:
2025-01-01 18:51:53 +00:00
parent bcd65e304e
commit e489624213
2 changed files with 80 additions and 154 deletions

View File

@@ -218,6 +218,9 @@ function initializeStream() {
{ {
token: Bun.env["MISSKEY_CREDENTIAL"] ?? "", token: Bun.env["MISSKEY_CREDENTIAL"] ?? "",
}, },
{
binaryType: "arraybuffer"
}
) as unknown as MisskeyStream; ) as unknown as MisskeyStream;
channel = stream.useChannel("main"); channel = stream.useChannel("main");

View File

@@ -1,25 +1,18 @@
import { EventEmitter } from "eventemitter3"; import { EventEmitter } from 'eventemitter3';
import ReconnectingWebsocket, { import ReconnectingWebsocket, { type Options } from 'reconnecting-websocket';
type Options as WebsocketOptions, import type { BroadcastEvents, Channels } from './streaming.types.js';
} from "reconnecting-websocket";
import type { BroadcastEvents, Channels } from "./streaming.types.js";
export function urlQuery( export function urlQuery(obj: Record<string, string | number | boolean | undefined>): string {
obj: Record<string, string | number | boolean | undefined>,
): string {
const params = Object.entries(obj) const params = Object.entries(obj)
.filter(([, v]) => (Array.isArray(v) ? v.length : v !== undefined)) .filter(([, v]) => Array.isArray(v) ? v.length : v !== undefined)
.reduce(
// biome-ignore lint/style/noNonNullAssertion: <explanation>
// biome-ignore lint/suspicious/noAssignInExpressions: <explanation> // biome-ignore lint/suspicious/noAssignInExpressions: <explanation>
// biome-ignore lint/style/noCommaOperator: <explanation> // biome-ignore lint/style/noCommaOperator: <explanation>
(a, [k, v]) => ((a[k] = v!), a), // biome-ignore lint/style/noNonNullAssertion: <explanation>
{} as Record<string, string | number | boolean>, .reduce((a, [k, v]) => (a[k] = v!, a), {} as Record<string, string | number | boolean>);
);
return Object.entries(params) return Object.entries(params)
.map((e) => `${e[0]}=${encodeURIComponent(e[1])}`) .map((e) => `${e[0]}=${encodeURIComponent(e[1])}`)
.join("&"); .join('&');
} }
type AnyOf<T extends Record<PropertyKey, unknown>> = T[keyof T]; type AnyOf<T extends Record<PropertyKey, unknown>> = T[keyof T];
@@ -30,23 +23,16 @@ export type StreamEvents = {
} & BroadcastEvents; } & BroadcastEvents;
export interface IStream extends EventEmitter<StreamEvents> { export interface IStream extends EventEmitter<StreamEvents> {
state: "initializing" | "reconnecting" | "connected"; state: 'initializing' | 'reconnecting' | 'connected';
useChannel<C extends keyof Channels>( useChannel<C extends keyof Channels>(channel: C, params?: Channels[C]['params'], name?: string): IChannelConnection<Channels[C]>;
channel: C,
params?: Channels[C]["params"],
name?: string,
): IChannelConnection<Channels[C]>;
removeSharedConnection(connection: SharedConnection): void; removeSharedConnection(connection: SharedConnection): void;
removeSharedConnectionPool(pool: Pool): void; removeSharedConnectionPool(pool: Pool): void;
disconnectToChannel(connection: NonSharedConnection): void; disconnectToChannel(connection: NonSharedConnection): void;
send(typeOrPayload: string): void; send(typeOrPayload: string): void;
send(typeOrPayload: string, payload: unknown): void; send(typeOrPayload: string, payload: unknown): void;
send(typeOrPayload: Record<string, unknown> | unknown[]): void; send(typeOrPayload: Record<string, unknown> | unknown[]): void;
send( send(typeOrPayload: string | Record<string, unknown> | unknown[], payload?: unknown): void;
typeOrPayload: string | Record<string, unknown> | unknown[],
payload?: unknown,
): void;
ping(): void; ping(): void;
heartbeat(): void; heartbeat(): void;
close(): void; close(): void;
@@ -56,29 +42,25 @@ export interface IStream extends EventEmitter<StreamEvents> {
* Misskey stream connection * Misskey stream connection
*/ */
// eslint-disable-next-line import/no-default-export // eslint-disable-next-line import/no-default-export
export default class Stream export default class Stream extends EventEmitter<StreamEvents> implements IStream {
extends EventEmitter<StreamEvents>
implements IStream {
private stream: ReconnectingWebsocket; private stream: ReconnectingWebsocket;
public state: "initializing" | "reconnecting" | "connected" = "initializing"; public state: 'initializing' | 'reconnecting' | 'connected' = 'initializing';
private sharedConnectionPools: Pool[] = []; private sharedConnectionPools: Pool[] = [];
private sharedConnections: SharedConnection[] = []; private sharedConnections: SharedConnection[] = [];
private nonSharedConnections: NonSharedConnection[] = []; private nonSharedConnections: NonSharedConnection[] = [];
private idCounter = 0; private idCounter = 0;
constructor( constructor(origin: string, user: { token: string; } | null, options: {
origin: string, WebSocket?: Options['WebSocket'];
user: { token: string } | null, binaryType?: ReconnectingWebsocket['binaryType'];
options?: WebsocketOptions, } = {}) {
) {
super(); super();
this.genId = this.genId.bind(this); this.genId = this.genId.bind(this);
this.useChannel = this.useChannel.bind(this); this.useChannel = this.useChannel.bind(this);
this.useSharedConnection = this.useSharedConnection.bind(this); this.useSharedConnection = this.useSharedConnection.bind(this);
this.removeSharedConnection = this.removeSharedConnection.bind(this); this.removeSharedConnection = this.removeSharedConnection.bind(this);
this.removeSharedConnectionPool = this.removeSharedConnectionPool = this.removeSharedConnectionPool.bind(this);
this.removeSharedConnectionPool.bind(this);
this.connectToChannel = this.connectToChannel.bind(this); this.connectToChannel = this.connectToChannel.bind(this);
this.disconnectToChannel = this.disconnectToChannel.bind(this); this.disconnectToChannel = this.disconnectToChannel.bind(this);
this.onOpen = this.onOpen.bind(this); this.onOpen = this.onOpen.bind(this);
@@ -94,102 +76,70 @@ export default class Stream
_t: Date.now(), _t: Date.now(),
}); });
const wsOrigin = origin const wsOrigin = origin.replace('http://', 'ws://').replace('https://', 'wss://');
.replace("http://", "ws://")
.replace("https://", "wss://");
this.stream = new ReconnectingWebsocket( this.stream = new ReconnectingWebsocket(`${wsOrigin}/streaming?${query}`, '', {
`${wsOrigin}/streaming?${query}`,
"",
{
minReconnectionDelay: 1, // https://github.com/pladaria/reconnecting-websocket/issues/91 minReconnectionDelay: 1, // https://github.com/pladaria/reconnecting-websocket/issues/91
...(options ?? {}), WebSocket: options.WebSocket,
}, });
); if (options.binaryType) {
this.stream.binaryType = "arraybuffer"; this.stream.binaryType = options.binaryType;
this.stream.addEventListener("open", this.onOpen); }
this.stream.addEventListener("close", this.onClose); this.stream.addEventListener('open', this.onOpen);
this.stream.addEventListener("message", this.onMessage); this.stream.addEventListener('close', this.onClose);
this.stream.addEventListener('message', this.onMessage);
} }
private genId(): string { private genId(): string {
return (++this.idCounter).toString(); return (++this.idCounter).toString();
} }
public useChannel<C extends keyof Channels>( public useChannel<C extends keyof Channels>(channel: C, params?: Channels[C]['params'], name?: string): Connection<Channels[C]> {
channel: C,
params?: Channels[C]["params"],
name?: string,
): Connection<Channels[C]> {
if (params) { if (params) {
return this.connectToChannel(channel, params); return this.connectToChannel(channel, params);
} }
return this.useSharedConnection(channel, name); return this.useSharedConnection(channel, name);
} }
private useSharedConnection<C extends keyof Channels>( private useSharedConnection<C extends keyof Channels>(channel: C, name?: string): SharedConnection<Channels[C]> {
channel: C, let pool = this.sharedConnectionPools.find(p => p.channel === channel);
name?: string,
): SharedConnection<Channels[C]> {
let pool = this.sharedConnectionPools.find((p) => p.channel === channel);
if (pool == null) { if (pool == null) {
pool = new Pool(this, channel, this.genId()); pool = new Pool(this, channel, this.genId());
this.sharedConnectionPools.push(pool); this.sharedConnectionPools.push(pool);
} }
const connection = new SharedConnection<Channels[C]>( const connection = new SharedConnection<Channels[C]>(this, channel, pool, name);
this,
channel,
pool,
name,
);
this.sharedConnections.push(connection as unknown as SharedConnection); this.sharedConnections.push(connection as unknown as SharedConnection);
return connection; return connection;
} }
public removeSharedConnection(connection: SharedConnection): void { public removeSharedConnection(connection: SharedConnection): void {
this.sharedConnections = this.sharedConnections.filter( this.sharedConnections = this.sharedConnections.filter(c => c !== connection);
(c) => c !== connection,
);
} }
public removeSharedConnectionPool(pool: Pool): void { public removeSharedConnectionPool(pool: Pool): void {
this.sharedConnectionPools = this.sharedConnectionPools.filter( this.sharedConnectionPools = this.sharedConnectionPools.filter(p => p !== pool);
(p) => p !== pool,
);
} }
private connectToChannel<C extends keyof Channels>( private connectToChannel<C extends keyof Channels>(channel: C, params: Channels[C]['params']): NonSharedConnection<Channels[C]> {
channel: C, const connection = new NonSharedConnection(this, channel, this.genId(), params);
params: Channels[C]["params"], this.nonSharedConnections.push(connection as unknown as NonSharedConnection);
): NonSharedConnection<Channels[C]> {
const connection = new NonSharedConnection(
this,
channel,
this.genId(),
params,
);
this.nonSharedConnections.push(
connection as unknown as NonSharedConnection,
);
return connection; return connection;
} }
public disconnectToChannel(connection: NonSharedConnection): void { public disconnectToChannel(connection: NonSharedConnection): void {
this.nonSharedConnections = this.nonSharedConnections.filter( this.nonSharedConnections = this.nonSharedConnections.filter(c => c !== connection);
(c) => c !== connection,
);
} }
/** /**
* Callback of when open connection * Callback of when open connection
*/ */
private onOpen(): void { private onOpen(): void {
const isReconnect = this.state === "reconnecting"; const isReconnect = this.state === 'reconnecting';
this.state = "connected"; this.state = 'connected';
this.emit("_connected_"); this.emit('_connected_');
// チャンネル再接続 // チャンネル再接続
if (isReconnect) { if (isReconnect) {
@@ -202,27 +152,27 @@ export default class Stream
* Callback of when close connection * Callback of when close connection
*/ */
private onClose(): void { private onClose(): void {
if (this.state === "connected") { if (this.state === 'connected') {
this.state = "reconnecting"; this.state = 'reconnecting';
this.emit("_disconnected_"); this.emit('_disconnected_');
} }
} }
/** /**
* Callback of when received a message from connection * Callback of when received a message from connection
*/ */
private onMessage(message: { data: string }): void { private onMessage(message: { data: string; }): void {
const { type, body } = JSON.parse(message.data); const { type, body } = JSON.parse(message.data);
if (type === "channel") { if (type === 'channel') {
const id = body.id; const id = body.id;
let connections: Connection[]; let connections: Connection[];
connections = this.sharedConnections.filter((c) => c.id === id); connections = this.sharedConnections.filter(c => c.id === id);
if (connections.length === 0) { if (connections.length === 0) {
const found = this.nonSharedConnections.find((c) => c.id === id); const found = this.nonSharedConnections.find(c => c.id === id);
if (found) { if (found) {
connections = [found]; connections = [found];
} }
@@ -241,20 +191,15 @@ export default class Stream
* Send a message to connection * Send a message to connection
* ! ストリーム上のやり取りはすべてJSONで行われます ! * ! ストリーム上のやり取りはすべてJSONで行われます !
*/ */
public send(typeOrPayload: string): void; public send(typeOrPayload: string): void
public send(typeOrPayload: string, payload: unknown): void; public send(typeOrPayload: string, payload: unknown): void
public send(typeOrPayload: Record<string, unknown> | unknown[]): void; public send(typeOrPayload: Record<string, unknown> | unknown[]): void
public send( public send(typeOrPayload: string | Record<string, unknown> | unknown[], payload?: unknown): void {
typeOrPayload: string | Record<string, unknown> | unknown[], if (typeof typeOrPayload === 'string') {
payload?: unknown, this.stream.send(JSON.stringify({
): void {
if (typeof typeOrPayload === "string") {
this.stream.send(
JSON.stringify({
type: typeOrPayload, type: typeOrPayload,
...(payload === undefined ? {} : { body: payload }), ...(payload === undefined ? {} : { body: payload }),
}), }));
);
return; return;
} }
@@ -262,11 +207,11 @@ export default class Stream
} }
public ping(): void { public ping(): void {
this.stream.send("ping"); this.stream.send('ping');
} }
public heartbeat(): void { public heartbeat(): void {
this.stream.send("h"); this.stream.send('h');
} }
/** /**
@@ -298,7 +243,7 @@ class Pool {
this.stream = stream; this.stream = stream;
this.id = id; this.id = id;
this.stream.on("_disconnected_", this.onStreamDisconnected); this.stream.on('_disconnected_', this.onStreamDisconnected);
} }
private onStreamDisconnected(): void { private onStreamDisconnected(): void {
@@ -335,41 +280,31 @@ class Pool {
public connect(): void { public connect(): void {
if (this.isConnected) return; if (this.isConnected) return;
this.isConnected = true; this.isConnected = true;
this.stream.send("connect", { this.stream.send('connect', {
channel: this.channel, channel: this.channel,
id: this.id, id: this.id,
}); });
} }
private disconnect(): void { private disconnect(): void {
this.stream.off("_disconnected_", this.onStreamDisconnected); this.stream.off('_disconnected_', this.onStreamDisconnected);
this.stream.send("disconnect", { id: this.id }); this.stream.send('disconnect', { id: this.id });
this.stream.removeSharedConnectionPool(this); this.stream.removeSharedConnectionPool(this);
} }
} }
export interface IChannelConnection< export interface IChannelConnection<Channel extends AnyOf<Channels> = AnyOf<Channels>> extends EventEmitter<Channel['events']> {
Channel extends AnyOf<Channels> = AnyOf<Channels>,
> extends EventEmitter<Channel["events"]> {
id: string; id: string;
name?: string; name?: string;
inCount: number; inCount: number;
outCount: number; outCount: number;
channel: string; channel: string;
send<T extends keyof Channel["receives"]>( send<T extends keyof Channel['receives']>(type: T, body: Channel['receives'][T]): void;
type: T,
body: Channel["receives"][T],
): void;
dispose(): void; dispose(): void;
} }
export abstract class Connection< export abstract class Connection<Channel extends AnyOf<Channels> = AnyOf<Channels>> extends EventEmitter<Channel['events']> implements IChannelConnection<Channel> {
Channel extends AnyOf<Channels> = AnyOf<Channels>,
>
extends EventEmitter<Channel["events"]>
implements IChannelConnection<Channel>
{
public channel: string; public channel: string;
protected stream: Stream; protected stream: Stream;
public abstract id: string; public abstract id: string;
@@ -390,11 +325,8 @@ export abstract class Connection<
} }
} }
public send<T extends keyof Channel["receives"]>( public send<T extends keyof Channel['receives']>(type: T, body: Channel['receives'][T]): void {
type: T, this.stream.send('ch', {
body: Channel["receives"][T],
): void {
this.stream.send("ch", {
id: this.id, id: this.id,
type: type, type: type,
body: body, body: body,
@@ -406,9 +338,7 @@ export abstract class Connection<
public abstract dispose(): void; public abstract dispose(): void;
} }
class SharedConnection< class SharedConnection<Channel extends AnyOf<Channels> = AnyOf<Channels>> extends Connection<Channel> {
Channel extends AnyOf<Channels> = AnyOf<Channels>,
> extends Connection<Channel> {
private pool: Pool; private pool: Pool;
public get id(): string { public get id(): string {
@@ -431,18 +361,11 @@ class SharedConnection<
} }
} }
class NonSharedConnection< class NonSharedConnection<Channel extends AnyOf<Channels> = AnyOf<Channels>> extends Connection<Channel> {
Channel extends AnyOf<Channels> = AnyOf<Channels>,
> extends Connection<Channel> {
public id: string; public id: string;
protected params: Channel["params"]; protected params: Channel['params'];
constructor( constructor(stream: Stream, channel: string, id: string, params: Channel['params']) {
stream: Stream,
channel: string,
id: string,
params: Channel["params"],
) {
super(stream, channel); super(stream, channel);
this.connect = this.connect.bind(this); this.connect = this.connect.bind(this);
@@ -455,7 +378,7 @@ class NonSharedConnection<
} }
public connect(): void { public connect(): void {
this.stream.send("connect", { this.stream.send('connect', {
channel: this.channel, channel: this.channel,
id: this.id, id: this.id,
params: this.params, params: this.params,
@@ -464,7 +387,7 @@ class NonSharedConnection<
public dispose(): void { public dispose(): void {
this.removeAllListeners(); this.removeAllListeners();
this.stream.send("disconnect", { id: this.id }); this.stream.send('disconnect', { id: this.id });
this.stream.disconnectToChannel(this as unknown as NonSharedConnection); this.stream.disconnectToChannel(this as unknown as NonSharedConnection);
} }
} }