seyfert 2.1.1-dev-11847255319.0 → 2.1.1-dev-11871861192.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/api/api.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type Awaitable, Logger } from '../common';
|
|
2
|
+
import type { WorkerSendApiRequest } from '../websocket/discord/worker';
|
|
2
3
|
import type { APIRoutes } from './Routes';
|
|
3
4
|
import { Bucket } from './bucket';
|
|
4
5
|
import { type ApiHandlerInternalOptions, type ApiHandlerOptions, type ApiRequestOptions, type HttpMethods, type RawFile, type RequestHeaders } from './shared';
|
|
@@ -22,6 +23,8 @@ export declare class ApiHandler {
|
|
|
22
23
|
set debug(active: boolean);
|
|
23
24
|
get proxy(): APIRoutes;
|
|
24
25
|
globalUnblock(): void;
|
|
26
|
+
private sendMessage;
|
|
27
|
+
protected postMessage<T = unknown>(body: WorkerSendApiRequest): Promise<T>;
|
|
25
28
|
request<T = unknown>(method: HttpMethods, url: `/${string}`, { auth, ...request }?: ApiRequestOptions): Promise<T>;
|
|
26
29
|
parseError(method: HttpMethods, route: `/${string}`, response: Response, result: string | Record<string, any>): Error;
|
|
27
30
|
parseValidationError(data: Record<string, any>, path?: string, errors?: string[]): string[];
|
package/lib/api/api.js
CHANGED
|
@@ -3,12 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ApiHandler = void 0;
|
|
4
4
|
const node_crypto_1 = require("node:crypto");
|
|
5
5
|
const common_1 = require("../common");
|
|
6
|
+
const utils_1 = require("../common/it/utils");
|
|
6
7
|
const Router_1 = require("./Router");
|
|
7
8
|
const bucket_1 = require("./bucket");
|
|
8
9
|
const shared_1 = require("./shared");
|
|
9
|
-
const
|
|
10
|
-
let parentPort;
|
|
11
|
-
let workerData;
|
|
10
|
+
const utils_2 = require("./utils/utils");
|
|
12
11
|
class ApiHandler {
|
|
13
12
|
options;
|
|
14
13
|
globalBlock = false;
|
|
@@ -28,14 +27,33 @@ class ApiHandler {
|
|
|
28
27
|
if (options.debug)
|
|
29
28
|
this.debug = true;
|
|
30
29
|
const worker_threads = (0, common_1.lazyLoadPackage)('node:worker_threads');
|
|
31
|
-
if (options.workerProxy && !worker_threads?.parentPort)
|
|
30
|
+
if (options.workerProxy && !worker_threads?.parentPort && !process.send)
|
|
32
31
|
throw new Error('Cannot use workerProxy without a parent.');
|
|
33
32
|
if (options.workerProxy)
|
|
34
33
|
this.workerPromises = new Map();
|
|
35
|
-
if (worker_threads) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
parentPort
|
|
34
|
+
if (worker_threads?.parentPort) {
|
|
35
|
+
this.sendMessage = async (body) => {
|
|
36
|
+
console;
|
|
37
|
+
worker_threads.parentPort.postMessage(body, body.requestOptions.files
|
|
38
|
+
?.filter(x => !['string', 'boolean', 'number'].includes(typeof x.data))
|
|
39
|
+
.map(x => (x.data instanceof Buffer ? (0, utils_1.toArrayBuffer)(x.data) : x.data)));
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
else if (process.send) {
|
|
43
|
+
this.sendMessage = body => {
|
|
44
|
+
const data = {
|
|
45
|
+
...body,
|
|
46
|
+
requestOptions: {
|
|
47
|
+
...body.requestOptions,
|
|
48
|
+
files: body.requestOptions.files?.map(file => {
|
|
49
|
+
if (file.data instanceof ArrayBuffer)
|
|
50
|
+
file.data = (0, utils_1.toBuffer)(file.data);
|
|
51
|
+
return file;
|
|
52
|
+
}),
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
process.send(data);
|
|
56
|
+
};
|
|
39
57
|
}
|
|
40
58
|
}
|
|
41
59
|
set debug(active) {
|
|
@@ -61,21 +79,25 @@ class ApiHandler {
|
|
|
61
79
|
return this.#randomUUID();
|
|
62
80
|
return uuid;
|
|
63
81
|
}
|
|
82
|
+
sendMessage(_body) {
|
|
83
|
+
throw new Error('Function not implemented');
|
|
84
|
+
}
|
|
85
|
+
postMessage(body) {
|
|
86
|
+
this.sendMessage(body);
|
|
87
|
+
return new Promise((res, rej) => {
|
|
88
|
+
this.workerPromises.set(body.nonce, { reject: rej, resolve: res });
|
|
89
|
+
});
|
|
90
|
+
}
|
|
64
91
|
async request(method, url, { auth = true, ...request } = {}) {
|
|
65
92
|
if (this.options.workerProxy) {
|
|
66
93
|
const nonce = this.#randomUUID();
|
|
67
|
-
|
|
94
|
+
return this.postMessage({
|
|
68
95
|
method,
|
|
69
96
|
url,
|
|
70
97
|
type: 'WORKER_API_REQUEST',
|
|
71
|
-
workerId: workerData.workerId,
|
|
98
|
+
workerId: this.workerData.workerId,
|
|
72
99
|
nonce,
|
|
73
100
|
requestOptions: { auth, ...request },
|
|
74
|
-
}, request.files
|
|
75
|
-
?.filter(x => !['string', 'boolean', 'number'].includes(typeof x.data))
|
|
76
|
-
.map(x => x.data));
|
|
77
|
-
return new Promise((res, rej) => {
|
|
78
|
-
this.workerPromises.set(nonce, { reject: rej, resolve: res });
|
|
79
101
|
});
|
|
80
102
|
}
|
|
81
103
|
const route = request.route || this.routefy(url, method);
|
|
@@ -306,7 +328,7 @@ class ApiHandler {
|
|
|
306
328
|
const formData = new FormData();
|
|
307
329
|
for (const [index, file] of options.request.files.entries()) {
|
|
308
330
|
const fileKey = file.key ?? `files[${index}]`;
|
|
309
|
-
if ((0,
|
|
331
|
+
if ((0, utils_2.isBufferLike)(file.data)) {
|
|
310
332
|
formData.append(fileKey, new Blob([file.data], { type: file.contentType }), file.filename);
|
|
311
333
|
}
|
|
312
334
|
else {
|
|
@@ -99,6 +99,7 @@ class WorkerClient extends base_1.BaseClient {
|
|
|
99
99
|
});
|
|
100
100
|
}
|
|
101
101
|
this.cache.intents = workerData.intents;
|
|
102
|
+
this.rest.workerData = workerData;
|
|
102
103
|
this.postMessage({
|
|
103
104
|
type: workerData.resharding ? 'WORKER_START_RESHARDING' : 'WORKER_START',
|
|
104
105
|
workerId: workerData.workerId,
|
package/lib/common/it/utils.d.ts
CHANGED
|
@@ -118,3 +118,5 @@ export declare function resolveEmoji(emoji: EmojiResolvable, cache: Cache): Prom
|
|
|
118
118
|
export declare function encodeEmoji(rawEmoji: APIPartialEmoji): string;
|
|
119
119
|
export declare function hasProps<T extends Record<any, any>>(target: T, props: TypeArray<keyof T>): boolean;
|
|
120
120
|
export declare function hasIntent(intents: number, target: keyof typeof GatewayIntentBits | GatewayIntentBits): boolean;
|
|
121
|
+
export declare function toArrayBuffer(buffer: Buffer): ArrayBuffer;
|
|
122
|
+
export declare function toBuffer(arrayBuffer: ArrayBuffer): Buffer;
|
package/lib/common/it/utils.js
CHANGED
|
@@ -19,6 +19,8 @@ exports.resolveEmoji = resolveEmoji;
|
|
|
19
19
|
exports.encodeEmoji = encodeEmoji;
|
|
20
20
|
exports.hasProps = hasProps;
|
|
21
21
|
exports.hasIntent = hasIntent;
|
|
22
|
+
exports.toArrayBuffer = toArrayBuffer;
|
|
23
|
+
exports.toBuffer = toBuffer;
|
|
22
24
|
const node_fs_1 = require("node:fs");
|
|
23
25
|
const node_path_1 = require("node:path");
|
|
24
26
|
const __1 = require("..");
|
|
@@ -367,3 +369,19 @@ function hasIntent(intents, target) {
|
|
|
367
369
|
const intent = typeof target === 'string' ? types_1.GatewayIntentBits[target] : target;
|
|
368
370
|
return (intents & intent) === intent;
|
|
369
371
|
}
|
|
372
|
+
function toArrayBuffer(buffer) {
|
|
373
|
+
const arrayBuffer = new ArrayBuffer(buffer.length);
|
|
374
|
+
const view = new Uint8Array(arrayBuffer);
|
|
375
|
+
for (let i = 0; i < buffer.length; ++i) {
|
|
376
|
+
view[i] = buffer[i];
|
|
377
|
+
}
|
|
378
|
+
return arrayBuffer;
|
|
379
|
+
}
|
|
380
|
+
function toBuffer(arrayBuffer) {
|
|
381
|
+
const buffer = Buffer.alloc(arrayBuffer.byteLength);
|
|
382
|
+
const view = new Uint8Array(arrayBuffer);
|
|
383
|
+
for (let i = 0; i < buffer.length; ++i) {
|
|
384
|
+
buffer[i] = view[i];
|
|
385
|
+
}
|
|
386
|
+
return buffer;
|
|
387
|
+
}
|
|
@@ -359,6 +359,14 @@ class WorkerManager extends Map {
|
|
|
359
359
|
break;
|
|
360
360
|
case 'WORKER_API_REQUEST':
|
|
361
361
|
{
|
|
362
|
+
if (this.options.mode === 'clusters' && message.requestOptions.files?.length) {
|
|
363
|
+
message.requestOptions.files.forEach(file => {
|
|
364
|
+
//@ts-expect-error
|
|
365
|
+
if (file.data.type === 'Buffer' && Array.isArray(file.data?.data))
|
|
366
|
+
//@ts-expect-error
|
|
367
|
+
file.data = new Uint8Array(file.data.data);
|
|
368
|
+
});
|
|
369
|
+
}
|
|
362
370
|
const response = await this.rest.request(message.method, message.url, message.requestOptions);
|
|
363
371
|
this.postMessage(message.workerId, {
|
|
364
372
|
nonce: message.nonce,
|