Skip to content

Commit

Permalink
去除zlib
Browse files Browse the repository at this point in the history
  • Loading branch information
1395173231 committed May 29, 2023
1 parent 1075e3b commit fcd6968
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/aws.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/midjourney-api.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/interfaces/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export interface MessageConfig {
SessionId: string;
Ws?: boolean;
HuggingFaceToken?: string;
discord_baseurl?:string;
ws_baseurl?:string;
}
export interface MessageConfigParam {
ChannelId: string;
Expand All @@ -19,6 +21,8 @@ export interface MessageConfigParam {
HuggingFaceToken?: string;
ServerId?: string;
SessionId?: string;
discord_baseurl?:string;
ws_baseurl?:string;
}
export interface MidjourneyConfig extends MessageConfig {
ServerId: string;
Expand Down
2 changes: 1 addition & 1 deletion src/midjourney.message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export class MidjourneyMessage {
async RetrieveMessages(limit = this.config.Limit) {
const headers = { authorization: this.config.SalaiToken };
const response = await fetch(
`https://discord.com/api/v10/channels/${this.config.ChannelId}/messages?limit=${limit}`,
(this.config.discord_baseurl?this.config.discord_baseurl:"https://discord.com")+`/api/v10/channels/${this.config.ChannelId}/messages?limit=${limit}`,
{
headers: headers,
}
Expand Down
2 changes: 1 addition & 1 deletion src/midjourney.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class Midjourney extends MidjourneyMessage {
"Content-Type": "application/json",
Authorization: this.config.SalaiToken,
};
const response = await fetch("https://discord.com/api/v9/interactions", {
const response = await fetch(this.config.discord_baseurl?this.config.discord_baseurl+"/api/v9/interactions":"https://discord.com/api/v9/interactions", {
method: "POST",
body: JSON.stringify(payload),
headers: headers,
Expand Down
20 changes: 7 additions & 13 deletions src/ws.message.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import WebSocket from "ws";
import { createInflate, Inflate, constants as ZlibConstants } from "zlib";
import {
MessageConfig,
MessageConfigParam,
Expand All @@ -13,12 +12,11 @@ import { VerifyHuman } from "./verify.human";

export class WsMessage {
DISCORD_GATEWAY =
"wss://gateway.discord.gg/?v=9&encoding=json&compress=zlib-stream";
"wss://gateway.discord.gg/?v=9&encoding=json&compress=gzip-stream";
ws: WebSocket;
MJBotId = "936929561302675456";
private zlibChunks: Buffer[] = [];
public config: MessageConfig;
private inflate: Inflate;
private event: Array<{ event: string; callback: (message: any) => void }> =
[];
private waitMjEvents: Map<string, WaitMjEvent> = new Map();
Expand All @@ -35,11 +33,12 @@ export class WsMessage {
...DefaultMessageConfig,
...defaults,
};
if(this.config.ws_baseurl){
this.DISCORD_GATEWAY.replace("wss://gateway.discord.gg",this.config.ws_baseurl)
}
this.ws = new WebSocket(this.DISCORD_GATEWAY, {});
this.ws.on("open", this.open.bind(this));

this.inflate = createInflate({ flush: ZlibConstants.Z_SYNC_FLUSH });
this.inflate.on("data", (data) => this.zlibChunks.push(data));
}

private reconnect() {
Expand Down Expand Up @@ -98,13 +97,8 @@ export class WsMessage {
return new Promise((resolve) => setTimeout(resolve, ms));
}
private incomingMessage(data: Buffer) {
this.inflate.write(data);
if (data.length >= 4 && data.readUInt32BE(data.length - 4) === 0x0ffff) {
this.inflate.flush(
ZlibConstants.Z_SYNC_FLUSH,
this.handleFlushComplete.bind(this)
);
}
this.zlibChunks.push(data)
this.handleFlushComplete.bind(this)
}
private handleFlushComplete() {
const data =
Expand Down Expand Up @@ -267,7 +261,7 @@ export class WsMessage {
"Content-Type": "application/json",
Authorization: this.config.SalaiToken,
};
const response = await fetch("https://discord.com/api/v9/interactions", {
const response = await fetch(this.config.discord_baseurl?this.config.discord_baseurl:"https://discord.com"+"/api/v9/interactions", {
method: "POST",
body: JSON.stringify(payload),
headers: headers,
Expand Down

0 comments on commit fcd6968

Please sign in to comment.