Skip to content

Commit

Permalink
Merge pull request #69 from 1395173231/main
Browse files Browse the repository at this point in the history
Make the service compatible with serverless and edge Function
  • Loading branch information
zcpua authored May 30, 2023
2 parents 1075e3b + 0cb4428 commit bc75880
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 36 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,5 @@ dist


# Directory for build generated files
libs
libs
.idea
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"snowyflake": "^2.0.0",
"throat": "^6.0.2",
"tslib": "^2.5.0",
"ws": "^8.13.0"
"ws": "^8.13.0",
"isomorphic-ws": "^5.0.0"
}
}
6 changes: 6 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;
DiscordBaseUrl?: string;
WsBaseUrl?: string;
}
export interface MessageConfigParam {
ChannelId: string;
Expand All @@ -19,6 +21,8 @@ export interface MessageConfigParam {
HuggingFaceToken?: string;
ServerId?: string;
SessionId?: string;
DiscordBaseUrl?: string;
WsBaseUrl?: string;
}
export interface MidjourneyConfig extends MessageConfig {
ServerId: string;
Expand All @@ -37,6 +41,8 @@ export const DefaultMessageConfig: MessageConfig = {
Debug: false,
Limit: 50,
MaxWait: 100,
DiscordBaseUrl: "https://discord.com",
WsBaseUrl: "wss://gateway.discord.gg"
};
export const DefaultMidjourneyConfig: MidjourneyConfig = {
...DefaultMessageConfig,
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.DiscordBaseUrl}/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.DiscordBaseUrl}/api/v9/interactions`, {
method: "POST",
body: JSON.stringify(payload),
headers: headers,
Expand Down
2 changes: 1 addition & 1 deletion src/utls/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const random = (min: number, max: number): number =>

// const snowflake = new Snowflake(1);
const snowflake = new Snowyflake({
workerId: 1n,
workerId: BigInt(1),
epoch: Epoch.Twitter, // BigInt timestamp
});

Expand Down
42 changes: 12 additions & 30 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 WebSocket from "isomorphic-ws";
import {
MessageConfig,
MessageConfigParam,
Expand All @@ -12,18 +11,16 @@ import {
import { VerifyHuman } from "./verify.human";

export class WsMessage {
DISCORD_GATEWAY =
"wss://gateway.discord.gg/?v=9&encoding=json&compress=zlib-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();
private reconnectTime: boolean[] = [];
private heartbeatInterval = 0;
private DISCORD_GATEWAY: string;

constructor(defaults: MessageConfigParam) {
const { ChannelId, SalaiToken } = defaults;
Expand All @@ -35,11 +32,9 @@ export class WsMessage {
...DefaultMessageConfig,
...defaults,
};
this.DISCORD_GATEWAY=`${this.config.WsBaseUrl}/?v=9&encoding=json&compress=gzip-stream`
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 @@ -73,8 +68,9 @@ export class WsMessage {
this.reconnectTime[num] = true;
this.reconnect();
};
await this.timeout(1000 * 10);
this.heartbeat(num);
setTimeout(() => {
this.heartbeat(num);
}, 1000 * 10);
}
// auth
private auth() {
Expand All @@ -98,21 +94,6 @@ 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)
);
}
}
private handleFlushComplete() {
const data =
this.zlibChunks.length > 1
? Buffer.concat(this.zlibChunks)
: this.zlibChunks[0];

this.zlibChunks = [];
this.parseMessage(data);
}

Expand Down Expand Up @@ -267,11 +248,12 @@ export class WsMessage {
"Content-Type": "application/json",
Authorization: this.config.SalaiToken,
};
const response = await fetch("https://discord.com/api/v9/interactions", {
method: "POST",
body: JSON.stringify(payload),
headers: headers,
});
const response = await fetch(`${this.config.DiscordBaseUrl}/api/v9/interactions`,{
method: "POST",
body: JSON.stringify(payload),
headers: headers,
},
);
callback && callback(response.status);
//discord api rate limit
if (response.status >= 400) {
Expand Down
7 changes: 6 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,11 @@ get-tsconfig@^4.4.0:
resolved "https://registry.npmjs.org/get-tsconfig/-/get-tsconfig-4.5.0.tgz"
integrity sha512-MjhiaIWCJ1sAU4pIQ5i5OfOuHHxVo1oYeNsWTON7jxYkod8pHocXeh+SSbmu5OZZZK73B6cbJ2XADzXehLyovQ==

isomorphic-ws@^5.0.0:
version "5.0.0"
resolved "https://registry.npmjs.org/isomorphic-ws/-/isomorphic-ws-5.0.0.tgz#e5529148912ecb9b451b46ed44d53dae1ce04bbf"
integrity sha512-muId7Zzn9ywDsyXgTIafTry2sV3nySZeUDe6YedVd1Hvuuep5AsIlqK+XefWpYTyJG5e503F2xIuT2lcU6rCSw==

make-error@^1.1.1:
version "1.3.6"
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
Expand Down Expand Up @@ -381,7 +386,7 @@ v8-compile-cache-lib@^3.0.1:

ws@^8.13.0:
version "8.13.0"
resolved "https://registry.yarnpkg.com/ws/-/ws-8.13.0.tgz#9a9fb92f93cf41512a0735c8f4dd09b8a1211cd0"
resolved "https://registry.npmjs.org/ws/-/ws-8.13.0.tgz#9a9fb92f93cf41512a0735c8f4dd09b8a1211cd0"
integrity sha512-x9vcZYTrFPC7aSIbj7sRCYo7L/Xb8Iy+pW0ng0wt2vCJv7M9HOMy0UoN3rr+IFC7hb7vXoqS+P9ktyLLLhO+LA==

yn@3.1.1:
Expand Down

0 comments on commit bc75880

Please sign in to comment.