@kyyinfinite/zapo-message 0.0.0-stage → 1.0.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/CHANGELOG.md +159 -0
- package/LICENSE +20 -0
- package/README.md +532 -2
- package/build.config.json +28 -0
- package/dist/airich/airich.d.ts +180 -0
- package/dist/airich/airich.js +548 -0
- package/dist/airich/context.d.ts +24 -0
- package/dist/airich/context.js +48 -0
- package/dist/airich/encode.d.ts +3 -0
- package/dist/airich/encode.js +16 -0
- package/dist/airich/index.d.ts +20 -0
- package/dist/airich/index.js +11 -0
- package/dist/airich/inline.d.ts +45 -0
- package/dist/airich/inline.js +152 -0
- package/dist/airich/nodes.d.ts +30 -0
- package/dist/airich/nodes.js +121 -0
- package/dist/airich/primitives.d.ts +144 -0
- package/dist/airich/primitives.js +230 -0
- package/dist/airich/protocol.d.ts +35 -0
- package/dist/airich/protocol.js +65 -0
- package/dist/airich/table.d.ts +22 -0
- package/dist/airich/table.js +35 -0
- package/dist/airich/tokenizer.d.ts +13 -0
- package/dist/airich/tokenizer.js +717 -0
- package/dist/airich/transport.d.ts +15 -0
- package/dist/airich/transport.js +56 -0
- package/dist/branding.d.ts +5 -0
- package/dist/branding.js +9 -0
- package/dist/btn/builders.d.ts +246 -0
- package/dist/btn/builders.js +710 -0
- package/dist/btn/media-slot.d.ts +28 -0
- package/dist/btn/media-slot.js +323 -0
- package/dist/btn/media-source.d.ts +38 -0
- package/dist/btn/media-source.js +116 -0
- package/dist/btn/sharp-loader.d.ts +5 -0
- package/dist/btn/sharp-loader.js +18 -0
- package/dist/btn/thumbnail.d.ts +21 -0
- package/dist/btn/thumbnail.js +124 -0
- package/dist/btn/validators.d.ts +40 -0
- package/dist/btn/validators.js +135 -0
- package/dist/btn.d.ts +1 -0
- package/dist/btn.js +1 -0
- package/dist/core/async-send.d.ts +20 -0
- package/dist/core/async-send.js +1 -0
- package/dist/core/base-builder.d.ts +20 -0
- package/dist/core/base-builder.js +67 -0
- package/dist/core/errors.d.ts +30 -0
- package/dist/core/errors.js +61 -0
- package/dist/download.d.ts +24 -0
- package/dist/download.js +123 -0
- package/dist/index.d.ts +38 -0
- package/dist/index.js +22 -0
- package/dist/integration/augment.d.ts +97 -0
- package/dist/integration/augment.js +1 -0
- package/dist/integration/auto.d.ts +8 -0
- package/dist/integration/auto.js +79 -0
- package/dist/integration/index.d.ts +4 -0
- package/dist/integration/index.js +2 -0
- package/dist/integration/methods.d.ts +25 -0
- package/dist/integration/methods.js +24 -0
- package/dist/interactive.d.ts +24 -0
- package/dist/interactive.js +121 -0
- package/dist/media-gate.d.ts +99 -0
- package/dist/media-gate.js +322 -0
- package/dist/message/album.d.ts +33 -0
- package/dist/message/album.js +107 -0
- package/dist/message/contact.d.ts +29 -0
- package/dist/message/contact.js +123 -0
- package/dist/message/forward.d.ts +23 -0
- package/dist/message/forward.js +77 -0
- package/dist/message/index.d.ts +26 -0
- package/dist/message/index.js +13 -0
- package/dist/message/link-preview.d.ts +28 -0
- package/dist/message/link-preview.js +98 -0
- package/dist/message/location.d.ts +24 -0
- package/dist/message/location.js +94 -0
- package/dist/message/media.d.ts +90 -0
- package/dist/message/media.js +123 -0
- package/dist/message/poll.d.ts +24 -0
- package/dist/message/poll.js +98 -0
- package/dist/message/reaction.d.ts +20 -0
- package/dist/message/reaction.js +71 -0
- package/dist/message/sticker.d.ts +29 -0
- package/dist/message/sticker.js +44 -0
- package/dist/message/target.d.ts +18 -0
- package/dist/message/target.js +24 -0
- package/dist/message/text.d.ts +17 -0
- package/dist/message/text.js +29 -0
- package/dist/message/vcard.d.ts +9 -0
- package/dist/message/vcard.js +23 -0
- package/dist/message/webp.d.ts +6 -0
- package/dist/message/webp.js +57 -0
- package/dist/payloads.d.ts +180 -0
- package/dist/payloads.js +1 -0
- package/dist/serialize.d.ts +41 -0
- package/dist/serialize.js +93 -0
- package/dist/status.d.ts +3 -0
- package/dist/status.js +21 -0
- package/dist/zapo-client.d.ts +44 -0
- package/dist/zapo-client.js +1 -0
- package/examples/airich.ts +59 -0
- package/examples/buttons.ts +48 -0
- package/examples/client-methods.ts +58 -0
- package/examples/hybrid.ts +133 -0
- package/examples/serialize.ts +61 -0
- package/package.json +271 -4
- package/scripts/check-no-comments.mjs +101 -0
- package/scripts/compare-golden.mjs +461 -0
- package/scripts/lib-config.mjs +9 -0
- package/scripts/smoke-auto.mjs +75 -0
- package/scripts/test-external-consumer.mjs +316 -0
- package/scripts/verify-package.mjs +78 -0
- package/src/airich/airich.ts +970 -0
- package/src/airich/context.ts +96 -0
- package/src/airich/encode.ts +25 -0
- package/src/airich/index.ts +98 -0
- package/src/airich/inline.ts +272 -0
- package/src/airich/nodes.ts +174 -0
- package/src/airich/primitives.ts +474 -0
- package/src/airich/protocol.ts +133 -0
- package/src/airich/table.ts +70 -0
- package/src/airich/tokenizer.ts +799 -0
- package/src/airich/transport.ts +106 -0
- package/src/branding.ts +23 -0
- package/src/btn/builders.ts +1073 -0
- package/src/btn/media-slot.ts +486 -0
- package/src/btn/media-source.ts +189 -0
- package/src/btn/sharp-loader.ts +23 -0
- package/src/btn/thumbnail.ts +182 -0
- package/src/btn/validators.ts +237 -0
- package/src/btn.ts +8 -0
- package/src/core/async-send.ts +28 -0
- package/src/core/base-builder.ts +104 -0
- package/src/core/errors.ts +82 -0
- package/src/download.ts +212 -0
- package/src/index.ts +229 -0
- package/src/integration/augment.ts +262 -0
- package/src/integration/auto.ts +103 -0
- package/src/integration/index.ts +8 -0
- package/src/integration/methods.ts +45 -0
- package/src/interactive.ts +188 -0
- package/src/media-gate.ts +499 -0
- package/src/message/album.ts +187 -0
- package/src/message/contact.ts +170 -0
- package/src/message/forward.ts +123 -0
- package/src/message/index.ts +83 -0
- package/src/message/link-preview.ts +148 -0
- package/src/message/location.ts +150 -0
- package/src/message/media.ts +323 -0
- package/src/message/poll.ts +158 -0
- package/src/message/reaction.ts +133 -0
- package/src/message/sticker.ts +100 -0
- package/src/message/target.ts +38 -0
- package/src/message/text.ts +61 -0
- package/src/message/vcard.ts +36 -0
- package/src/message/webp.ts +78 -0
- package/src/payloads.ts +204 -0
- package/src/serialize.ts +180 -0
- package/src/status.ts +29 -0
- package/src/zapo-client.ts +84 -0
- package/test/airich-inline.test.ts +200 -0
- package/test/airich-primitives.test.ts +226 -0
- package/test/airich-tokenizer.test.ts +145 -0
- package/test/airich.golden.test.ts +406 -0
- package/test/airich.test.ts +1006 -0
- package/test/btn-button.test.ts +416 -0
- package/test/btn-header.test.ts +646 -0
- package/test/btn-legacy.test.ts +342 -0
- package/test/btn-lifecycle.test.ts +295 -0
- package/test/btn-list.test.ts +261 -0
- package/test/btn-media-source.test.ts +150 -0
- package/test/btn-thumbnail.test.ts +160 -0
- package/test/builder-compat.test.ts +264 -0
- package/test/helpers.ts +27 -0
- package/test/hybrid-airich.test.ts +345 -0
- package/test/hybrid-client.test.ts +239 -0
- package/test/hybrid-errors.test.ts +557 -0
- package/test/hybrid-interactive.test.ts +455 -0
- package/test/hybrid.golden.test.ts +349 -0
- package/test/hybrid.test.ts +479 -0
- package/test/integration-auto.test.ts +159 -0
- package/test/integration.fakeserver.test.ts +440 -0
- package/test/media-gate.test.ts +215 -0
- package/test/message-album.test.ts +145 -0
- package/test/message-builders.test.ts +401 -0
- package/test/message-media.test.ts +303 -0
- package/test/message.fakeserver.test.ts +284 -0
- package/test/message.golden.test.ts +254 -0
- package/test/serializer.test.ts +448 -0
- package/tsconfig.json +26 -0
- package/tsconfig.test.json +10 -0
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
import { PassThrough } from "node:stream";
|
|
2
|
+
export class QueueOverflowError extends Error {
|
|
3
|
+
code;
|
|
4
|
+
detail;
|
|
5
|
+
constructor(message, detail = {}) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = "QueueOverflowError";
|
|
8
|
+
this.code = "QUEUE_OVERFLOW";
|
|
9
|
+
this.detail = detail;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export class QueueClosedError extends Error {
|
|
13
|
+
code;
|
|
14
|
+
detail;
|
|
15
|
+
constructor(message = "queue sudah ditutup (shutdown)", detail = {}) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.name = "QueueClosedError";
|
|
18
|
+
this.code = "QUEUE_CLOSED";
|
|
19
|
+
this.detail = detail;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export class QueueTimeoutError extends Error {
|
|
23
|
+
code;
|
|
24
|
+
detail;
|
|
25
|
+
constructor(message, detail = {}) {
|
|
26
|
+
super(message);
|
|
27
|
+
this.name = "QueueTimeoutError";
|
|
28
|
+
this.code = "QUEUE_TIMEOUT";
|
|
29
|
+
this.detail = detail;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export class MediaAdmissionPausedError extends Error {
|
|
33
|
+
code;
|
|
34
|
+
detail;
|
|
35
|
+
constructor(message = "admission media dipause (governor)", detail = {}) {
|
|
36
|
+
super(message);
|
|
37
|
+
this.name = "MediaAdmissionPausedError";
|
|
38
|
+
this.code = "MEDIA_PAUSED";
|
|
39
|
+
this.detail = detail;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export const createConcurrencyController = ({ minConcurrency = 1, } = {}) => {
|
|
43
|
+
let factor = 1;
|
|
44
|
+
const setFactor = (next) => {
|
|
45
|
+
const value = Number(next);
|
|
46
|
+
if (!Number.isFinite(value))
|
|
47
|
+
return factor;
|
|
48
|
+
factor = Math.max(0, Math.min(1, value));
|
|
49
|
+
return factor;
|
|
50
|
+
};
|
|
51
|
+
const getFactor = () => factor;
|
|
52
|
+
const effectiveLimit = (baseConcurrency) => {
|
|
53
|
+
const base = Math.max(1, Number(baseConcurrency) || 1);
|
|
54
|
+
if (factor === 0)
|
|
55
|
+
return 0;
|
|
56
|
+
return Math.max(minConcurrency, Math.floor(base * factor));
|
|
57
|
+
};
|
|
58
|
+
const snapshot = () => ({
|
|
59
|
+
factor,
|
|
60
|
+
minConcurrency,
|
|
61
|
+
});
|
|
62
|
+
return { setFactor, getFactor, effectiveLimit, snapshot };
|
|
63
|
+
};
|
|
64
|
+
const MEDIA_QUEUE_DEFAULTS = {
|
|
65
|
+
concurrency: 2,
|
|
66
|
+
capacity: 50,
|
|
67
|
+
timeoutMs: 120_000,
|
|
68
|
+
maxBytes: 50 * 1024 * 1024,
|
|
69
|
+
};
|
|
70
|
+
export const createMediaQueue = (options = {}) => {
|
|
71
|
+
const concurrency = Math.max(1, Number(options.concurrency) || MEDIA_QUEUE_DEFAULTS.concurrency);
|
|
72
|
+
const capacity = Math.max(1, Number(options.capacity) || MEDIA_QUEUE_DEFAULTS.capacity);
|
|
73
|
+
const defaultTimeoutMs = Number(options.timeoutMs) || MEDIA_QUEUE_DEFAULTS.timeoutMs;
|
|
74
|
+
const maxBytes = Math.max(1024, Number(options.maxBytes) || MEDIA_QUEUE_DEFAULTS.maxBytes);
|
|
75
|
+
const controller = options.controller ?? null;
|
|
76
|
+
const metrics = options.metrics ?? null;
|
|
77
|
+
const pending = [];
|
|
78
|
+
const running = new Set();
|
|
79
|
+
let closed = false;
|
|
80
|
+
let admissionPaused = false;
|
|
81
|
+
let processed = 0;
|
|
82
|
+
let failed = 0;
|
|
83
|
+
let timeouts = 0;
|
|
84
|
+
let oversized = 0;
|
|
85
|
+
let bytesLastJob = 0;
|
|
86
|
+
const effectiveLimit = () => controller ? controller.effectiveLimit(concurrency) : concurrency;
|
|
87
|
+
const pump = () => {
|
|
88
|
+
if (closed)
|
|
89
|
+
return;
|
|
90
|
+
const limit = effectiveLimit();
|
|
91
|
+
while (running.size < limit && pending.length > 0) {
|
|
92
|
+
const entry = pending.shift();
|
|
93
|
+
if (entry === undefined)
|
|
94
|
+
break;
|
|
95
|
+
startJob(entry);
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
const startJob = (entry) => {
|
|
99
|
+
running.add(entry);
|
|
100
|
+
let settled = false;
|
|
101
|
+
const settleResolve = (value) => {
|
|
102
|
+
if (settled)
|
|
103
|
+
return;
|
|
104
|
+
settled = true;
|
|
105
|
+
processed += 1;
|
|
106
|
+
entry.resolve(value);
|
|
107
|
+
};
|
|
108
|
+
const settleReject = (error) => {
|
|
109
|
+
if (settled)
|
|
110
|
+
return;
|
|
111
|
+
settled = true;
|
|
112
|
+
failed += 1;
|
|
113
|
+
entry.reject(error);
|
|
114
|
+
};
|
|
115
|
+
const timeoutMs = entry.timeoutMs ?? defaultTimeoutMs;
|
|
116
|
+
let timeout = null;
|
|
117
|
+
let timedOut = false;
|
|
118
|
+
if (timeoutMs > 0) {
|
|
119
|
+
timeout = setTimeout(() => {
|
|
120
|
+
timedOut = true;
|
|
121
|
+
timeouts += 1;
|
|
122
|
+
metrics?.inc("mediaJobTimeouts");
|
|
123
|
+
entry.controller.abort(new QueueTimeoutError(`media job ${entry.label} melebihi ${timeoutMs}ms`));
|
|
124
|
+
settleReject(new QueueTimeoutError(`media job ${entry.label} melebihi ${timeoutMs}ms`));
|
|
125
|
+
}, timeoutMs);
|
|
126
|
+
}
|
|
127
|
+
const onExternalAbort = () => {
|
|
128
|
+
entry.controller.abort(entry.externalSignal?.reason ??
|
|
129
|
+
new Error("media job dibatalkan (external)"));
|
|
130
|
+
};
|
|
131
|
+
if (entry.externalSignal) {
|
|
132
|
+
if (entry.externalSignal.aborted)
|
|
133
|
+
onExternalAbort();
|
|
134
|
+
else
|
|
135
|
+
entry.externalSignal.addEventListener("abort", onExternalAbort, {
|
|
136
|
+
once: true,
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
Promise.resolve()
|
|
140
|
+
.then(() => entry.fn({
|
|
141
|
+
signal: entry.controller.signal,
|
|
142
|
+
maxBytes,
|
|
143
|
+
noteBytes: (bytes) => {
|
|
144
|
+
bytesLastJob = Math.max(0, Number(bytes) || 0);
|
|
145
|
+
},
|
|
146
|
+
}))
|
|
147
|
+
.then((result) => {
|
|
148
|
+
if (result instanceof Uint8Array) {
|
|
149
|
+
const bytes = result.byteLength ?? 0;
|
|
150
|
+
bytesLastJob = bytes;
|
|
151
|
+
if (bytes > maxBytes) {
|
|
152
|
+
oversized += 1;
|
|
153
|
+
throw new Error(`media melebihi batas ukuran (${bytes} > ${maxBytes} byte)`);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return result;
|
|
157
|
+
})
|
|
158
|
+
.then((result) => {
|
|
159
|
+
settleResolve(result);
|
|
160
|
+
}, (error) => {
|
|
161
|
+
if (settled) {
|
|
162
|
+
metrics?.inc("mediaOverrunSettled");
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (timedOut)
|
|
166
|
+
metrics?.inc("mediaJobTimeouts");
|
|
167
|
+
settleReject(error);
|
|
168
|
+
})
|
|
169
|
+
.finally(() => {
|
|
170
|
+
if (timeout)
|
|
171
|
+
clearTimeout(timeout);
|
|
172
|
+
if (entry.externalSignal)
|
|
173
|
+
entry.externalSignal.removeEventListener("abort", onExternalAbort);
|
|
174
|
+
entry.controller.abort(new Error("media job selesai — cleanup signal"));
|
|
175
|
+
running.delete(entry);
|
|
176
|
+
pump();
|
|
177
|
+
});
|
|
178
|
+
};
|
|
179
|
+
const run = (fn, { label = "", timeoutMs, signal } = {}) => {
|
|
180
|
+
if (closed)
|
|
181
|
+
return Promise.reject(new QueueClosedError("media queue ditutup"));
|
|
182
|
+
if (admissionPaused) {
|
|
183
|
+
metrics?.inc("mediaAdmissionPaused");
|
|
184
|
+
return Promise.reject(new MediaAdmissionPausedError());
|
|
185
|
+
}
|
|
186
|
+
if (pending.length >= capacity) {
|
|
187
|
+
metrics?.inc("mediaQueueRejections");
|
|
188
|
+
return Promise.reject(new QueueOverflowError(`antrean media penuh (${capacity})`, { label }));
|
|
189
|
+
}
|
|
190
|
+
return new Promise((resolve, reject) => {
|
|
191
|
+
const entry = {
|
|
192
|
+
label: String(label || "media"),
|
|
193
|
+
fn: fn,
|
|
194
|
+
resolve: resolve,
|
|
195
|
+
reject,
|
|
196
|
+
controller: new AbortController(),
|
|
197
|
+
externalSignal: signal instanceof AbortSignal ? signal : null,
|
|
198
|
+
timeoutMs: Number.isFinite(timeoutMs) ? Number(timeoutMs) : undefined,
|
|
199
|
+
};
|
|
200
|
+
pending.push(entry);
|
|
201
|
+
pump();
|
|
202
|
+
});
|
|
203
|
+
};
|
|
204
|
+
const setAdmissionPaused = (paused) => {
|
|
205
|
+
admissionPaused = !!paused;
|
|
206
|
+
return admissionPaused;
|
|
207
|
+
};
|
|
208
|
+
const shutdown = async ({ timeoutMs: shutdownTimeoutMs = 10_000, } = {}) => {
|
|
209
|
+
closed = true;
|
|
210
|
+
const rejectedCount = pending.length;
|
|
211
|
+
for (const entry of [...pending]) {
|
|
212
|
+
entry.reject(new QueueClosedError("shutdown — media job belum dijalankan"));
|
|
213
|
+
}
|
|
214
|
+
pending.length = 0;
|
|
215
|
+
const deadline = Date.now() + Math.max(0, Number(shutdownTimeoutMs) || 0);
|
|
216
|
+
while (running.size > 0 && Date.now() < deadline) {
|
|
217
|
+
await new Promise((resolve) => setTimeout(resolve, 25));
|
|
218
|
+
}
|
|
219
|
+
return { rejectedPending: rejectedCount, stillRunning: running.size };
|
|
220
|
+
};
|
|
221
|
+
const snapshot = () => ({
|
|
222
|
+
active: running.size,
|
|
223
|
+
depth: pending.length,
|
|
224
|
+
capacity,
|
|
225
|
+
concurrency: effectiveLimit(),
|
|
226
|
+
paused: admissionPaused,
|
|
227
|
+
closed,
|
|
228
|
+
processed,
|
|
229
|
+
failed,
|
|
230
|
+
timeouts,
|
|
231
|
+
oversized,
|
|
232
|
+
bytesLastJob,
|
|
233
|
+
maxBytes,
|
|
234
|
+
});
|
|
235
|
+
return {
|
|
236
|
+
run,
|
|
237
|
+
setAdmissionPaused,
|
|
238
|
+
shutdown,
|
|
239
|
+
snapshot,
|
|
240
|
+
isUnderPressure: () => admissionPaused || pending.length >= capacity * 0.6,
|
|
241
|
+
get pendingCount() {
|
|
242
|
+
return pending.length;
|
|
243
|
+
},
|
|
244
|
+
get runningCount() {
|
|
245
|
+
return running.size;
|
|
246
|
+
},
|
|
247
|
+
get maxBytes() {
|
|
248
|
+
return maxBytes;
|
|
249
|
+
},
|
|
250
|
+
get stats() {
|
|
251
|
+
return { processed, failed, timeouts, oversized };
|
|
252
|
+
},
|
|
253
|
+
markOversized: () => {
|
|
254
|
+
oversized += 1;
|
|
255
|
+
},
|
|
256
|
+
};
|
|
257
|
+
};
|
|
258
|
+
export const limitStreamBytes = (source, maxBytes, onLimit) => {
|
|
259
|
+
const limited = new PassThrough();
|
|
260
|
+
let total = 0;
|
|
261
|
+
const onDrain = () => {
|
|
262
|
+
source.resume();
|
|
263
|
+
};
|
|
264
|
+
limited.on("drain", onDrain);
|
|
265
|
+
source.on("data", (chunk) => {
|
|
266
|
+
total +=
|
|
267
|
+
typeof chunk === "string" ? Buffer.byteLength(chunk) : chunk.length;
|
|
268
|
+
if (total > maxBytes) {
|
|
269
|
+
const error = new Error(`media melebihi batas ukuran (${maxBytes} byte)`);
|
|
270
|
+
error.code = "MEDIA_TOO_LARGE";
|
|
271
|
+
onLimit?.(total);
|
|
272
|
+
limited.destroy(error);
|
|
273
|
+
source.destroy(error);
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
if (limited.destroyed) {
|
|
277
|
+
source.destroy();
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
const ready = limited.push(chunk);
|
|
281
|
+
if (!ready)
|
|
282
|
+
source.pause();
|
|
283
|
+
});
|
|
284
|
+
source.on("end", () => {
|
|
285
|
+
if (!limited.destroyed)
|
|
286
|
+
limited.end();
|
|
287
|
+
});
|
|
288
|
+
source.on("error", (error) => {
|
|
289
|
+
limited.destroy(error);
|
|
290
|
+
});
|
|
291
|
+
limited.bytesRead = () => total;
|
|
292
|
+
return limited;
|
|
293
|
+
};
|
|
294
|
+
const envNumber = (name, fallback) => {
|
|
295
|
+
const raw = process.env[name];
|
|
296
|
+
if (raw === undefined || raw === "")
|
|
297
|
+
return fallback;
|
|
298
|
+
const value = Number(raw);
|
|
299
|
+
return Number.isFinite(value) && value > 0 ? value : fallback;
|
|
300
|
+
};
|
|
301
|
+
let shared = null;
|
|
302
|
+
export const configureSharedMediaQueue = (options = {}) => {
|
|
303
|
+
shared = createMediaQueue({
|
|
304
|
+
concurrency: options.concurrency ??
|
|
305
|
+
envNumber("ARM_MEDIA_CONCURRENCY", MEDIA_QUEUE_DEFAULTS.concurrency),
|
|
306
|
+
capacity: options.capacity ?? 50,
|
|
307
|
+
timeoutMs: options.timeoutMs ??
|
|
308
|
+
envNumber("ARM_MEDIA_TIMEOUT_MS", MEDIA_QUEUE_DEFAULTS.timeoutMs),
|
|
309
|
+
maxBytes: options.maxBytes ??
|
|
310
|
+
envNumber("ARM_MEDIA_MAX_BYTES", MEDIA_QUEUE_DEFAULTS.maxBytes),
|
|
311
|
+
controller: options.controller,
|
|
312
|
+
});
|
|
313
|
+
return shared;
|
|
314
|
+
};
|
|
315
|
+
export const getSharedMediaQueue = () => {
|
|
316
|
+
if (!shared)
|
|
317
|
+
return configureSharedMediaQueue();
|
|
318
|
+
return shared;
|
|
319
|
+
};
|
|
320
|
+
export const resetSharedMediaQueue = () => {
|
|
321
|
+
shared = null;
|
|
322
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { BaseBuilder, type SendResolutionArg } from "../core/base-builder.js";
|
|
2
|
+
import { type MessageSendOptions } from "../core/async-send.js";
|
|
3
|
+
import type { MessageSendResult } from "../zapo-client.js";
|
|
4
|
+
import { type MediaSlotOptions } from "../btn/media-slot.js";
|
|
5
|
+
import type { MediaSource } from "../btn/media-source.js";
|
|
6
|
+
import { type AlbumBuildResult, type MediaSlotResolution } from "../payloads.js";
|
|
7
|
+
import type { ZapoClient } from "../zapo-client.js";
|
|
8
|
+
export type { MediaSlotOptions } from "../btn/media-slot.js";
|
|
9
|
+
export interface AlbumMediaItem {
|
|
10
|
+
type: "image" | "video";
|
|
11
|
+
source: MediaSource;
|
|
12
|
+
options?: MediaSlotOptions;
|
|
13
|
+
}
|
|
14
|
+
export interface AlbumConfig {
|
|
15
|
+
media: readonly AlbumMediaItem[];
|
|
16
|
+
}
|
|
17
|
+
export interface AlbumChild {
|
|
18
|
+
kind: "image" | "video";
|
|
19
|
+
resolution: MediaSlotResolution;
|
|
20
|
+
}
|
|
21
|
+
export declare class AlbumBuilder extends BaseBuilder<AlbumBuildResult> {
|
|
22
|
+
#private;
|
|
23
|
+
image(source: MediaSource, options?: MediaSlotOptions): this;
|
|
24
|
+
video(source: MediaSource, options?: MediaSlotOptions): this;
|
|
25
|
+
count(): number;
|
|
26
|
+
build(): AlbumBuildResult;
|
|
27
|
+
resolveChildren(): Promise<AlbumChild[]>;
|
|
28
|
+
send(jid?: string, arg?: SendResolutionArg): Promise<unknown>;
|
|
29
|
+
}
|
|
30
|
+
export declare function album(client?: ZapoClient): AlbumBuilder;
|
|
31
|
+
export declare function album(client: ZapoClient | undefined, jid: string, config: AlbumConfig, options?: MessageSendOptions): Promise<MessageSendResult>;
|
|
32
|
+
export declare const applyAlbumConfig: (builder: AlbumBuilder, config: AlbumConfig) => void;
|
|
33
|
+
export declare const sendAlbum: (client: ZapoClient | undefined, jid: string | undefined, config: AlbumConfig, options?: MessageSendOptions) => Promise<MessageSendResult>;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { BaseBuilder } from "../core/base-builder.js";
|
|
2
|
+
import { MessageError, ProtocolSendError } from "../core/errors.js";
|
|
3
|
+
import { sendConfigured } from "../core/async-send.js";
|
|
4
|
+
import { resolveMediaSlot, } from "../btn/media-slot.js";
|
|
5
|
+
import { mediaSlotToMessage, } from "../payloads.js";
|
|
6
|
+
export class AlbumBuilder extends BaseBuilder {
|
|
7
|
+
#slots = [];
|
|
8
|
+
#addChild(kind, source, options) {
|
|
9
|
+
this.#slots.push({
|
|
10
|
+
kind,
|
|
11
|
+
source,
|
|
12
|
+
options: options ?? {},
|
|
13
|
+
label: `album:${kind}:${this.#slots.length + 1}`,
|
|
14
|
+
});
|
|
15
|
+
return this;
|
|
16
|
+
}
|
|
17
|
+
image(source, options) {
|
|
18
|
+
return this.#addChild("image", source, options);
|
|
19
|
+
}
|
|
20
|
+
video(source, options) {
|
|
21
|
+
return this.#addChild("video", source, options);
|
|
22
|
+
}
|
|
23
|
+
count() {
|
|
24
|
+
return this.#slots.length;
|
|
25
|
+
}
|
|
26
|
+
build() {
|
|
27
|
+
if (this.#slots.length === 0) {
|
|
28
|
+
throw new MessageError("album butuh minimal satu media — panggil image()/video() sebelum send()", "NO_MEDIA");
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
albumMessage: {
|
|
32
|
+
expectedImageCount: this.#slots.filter((s) => s.kind === "image")
|
|
33
|
+
.length,
|
|
34
|
+
expectedVideoCount: this.#slots.filter((s) => s.kind === "video")
|
|
35
|
+
.length,
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
async resolveChildren() {
|
|
40
|
+
if (this.#slots.length === 0) {
|
|
41
|
+
throw new MessageError("album butuh minimal satu media — panggil image()/video() sebelum send()", "NO_MEDIA");
|
|
42
|
+
}
|
|
43
|
+
const { client } = this._resolve();
|
|
44
|
+
const slots = [...this.#slots];
|
|
45
|
+
const resolutions = await Promise.all(slots.map((slot) => resolveMediaSlot(client, slot)));
|
|
46
|
+
return resolutions.map((resolution, index) => ({
|
|
47
|
+
kind: slots[index].kind,
|
|
48
|
+
resolution,
|
|
49
|
+
}));
|
|
50
|
+
}
|
|
51
|
+
async send(jid, arg) {
|
|
52
|
+
const { client, options } = this._resolve(arg);
|
|
53
|
+
const target = typeof jid === "string" && jid ? jid : this._defaultTarget();
|
|
54
|
+
if (!target) {
|
|
55
|
+
throw new MessageError("tujuan album tidak diketahui. panggil send(jid) atau .defaultTo(jid)", "NO_TARGET");
|
|
56
|
+
}
|
|
57
|
+
const children = await this.resolveChildren();
|
|
58
|
+
const album = this.build();
|
|
59
|
+
const albumOptions = { ...options };
|
|
60
|
+
const albumSend = client.message.send.bind(client.message);
|
|
61
|
+
const albumResult = (await (Object.keys(albumOptions).length
|
|
62
|
+
? albumSend(target, album, albumOptions)
|
|
63
|
+
: albumSend(target, album)));
|
|
64
|
+
const albumId = albumResult?.id;
|
|
65
|
+
if (typeof albumId !== "string" || !albumId) {
|
|
66
|
+
throw new ProtocolSendError("album_id");
|
|
67
|
+
}
|
|
68
|
+
const selfJid = resolveSelfJid(client);
|
|
69
|
+
for (const child of children) {
|
|
70
|
+
const childOptions = {
|
|
71
|
+
...options,
|
|
72
|
+
quote: {
|
|
73
|
+
id: albumId,
|
|
74
|
+
remoteJid: target,
|
|
75
|
+
fromMe: true,
|
|
76
|
+
...(selfJid !== undefined ? { participant: selfJid } : {}),
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
await client.message.send(target, mediaSlotToMessage(child.resolution), childOptions);
|
|
80
|
+
}
|
|
81
|
+
return albumResult;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
const resolveSelfJid = (client) => {
|
|
85
|
+
const credentials = client.auth?.getCurrentCredentials?.();
|
|
86
|
+
if (!credentials)
|
|
87
|
+
return undefined;
|
|
88
|
+
return credentials.meLid ?? credentials.meJid ?? undefined;
|
|
89
|
+
};
|
|
90
|
+
export function album(client, jid, config, options) {
|
|
91
|
+
if (config === undefined)
|
|
92
|
+
return new AlbumBuilder(client);
|
|
93
|
+
return sendAlbum(client, jid, config, options);
|
|
94
|
+
}
|
|
95
|
+
export const applyAlbumConfig = (builder, config) => {
|
|
96
|
+
for (const item of config.media) {
|
|
97
|
+
if (item.type === "image")
|
|
98
|
+
builder.image(item.source, item.options);
|
|
99
|
+
else
|
|
100
|
+
builder.video(item.source, item.options);
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
export const sendAlbum = async (client, jid, config, options) => {
|
|
104
|
+
const builder = new AlbumBuilder(client);
|
|
105
|
+
applyAlbumConfig(builder, config);
|
|
106
|
+
return sendConfigured(builder, jid, options);
|
|
107
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { BaseBuilder } from "../core/base-builder.js";
|
|
2
|
+
import { type MessageSendOptions } from "../core/async-send.js";
|
|
3
|
+
import type { MessageSendResult } from "../zapo-client.js";
|
|
4
|
+
import type { ContactBuildResult } from "../payloads.js";
|
|
5
|
+
import type { ZapoClient } from "../zapo-client.js";
|
|
6
|
+
export interface ContactConfig {
|
|
7
|
+
name?: string;
|
|
8
|
+
phone?: string;
|
|
9
|
+
organization?: string;
|
|
10
|
+
email?: string;
|
|
11
|
+
url?: string;
|
|
12
|
+
note?: string;
|
|
13
|
+
vcard?: string;
|
|
14
|
+
}
|
|
15
|
+
export declare class ContactBuilder extends BaseBuilder<ContactBuildResult> {
|
|
16
|
+
#private;
|
|
17
|
+
name(displayName: string): this;
|
|
18
|
+
vcard(value: string): this;
|
|
19
|
+
phone(value: string): this;
|
|
20
|
+
organization(value: string): this;
|
|
21
|
+
email(value: string): this;
|
|
22
|
+
url(value: string): this;
|
|
23
|
+
note(value: string): this;
|
|
24
|
+
build(): ContactBuildResult;
|
|
25
|
+
}
|
|
26
|
+
export declare function contact(client?: ZapoClient): ContactBuilder;
|
|
27
|
+
export declare function contact(client: ZapoClient | undefined, jid: string, config: ContactConfig, options?: MessageSendOptions): Promise<MessageSendResult>;
|
|
28
|
+
export declare const applyContactConfig: (builder: ContactBuilder, config: ContactConfig) => void;
|
|
29
|
+
export declare const sendContact: (client: ZapoClient | undefined, jid: string | undefined, config: ContactConfig, options?: MessageSendOptions) => Promise<MessageSendResult>;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { BaseBuilder } from "../core/base-builder.js";
|
|
2
|
+
import { sendConfigured } from "../core/async-send.js";
|
|
3
|
+
import { assertText } from "../btn/validators.js";
|
|
4
|
+
import { MessageError, ValidationError } from "../core/errors.js";
|
|
5
|
+
import { buildVcard } from "./vcard.js";
|
|
6
|
+
const DISPLAY_NAME_MAX = 200;
|
|
7
|
+
const VCARD_MAX = 4096;
|
|
8
|
+
const fnOfVcard = (vcard) => {
|
|
9
|
+
for (const line of vcard.split("\n")) {
|
|
10
|
+
if (line.startsWith("FN:"))
|
|
11
|
+
return line.slice(3).trim() || undefined;
|
|
12
|
+
}
|
|
13
|
+
return undefined;
|
|
14
|
+
};
|
|
15
|
+
export class ContactBuilder extends BaseBuilder {
|
|
16
|
+
#displayName;
|
|
17
|
+
#vcard;
|
|
18
|
+
#phone;
|
|
19
|
+
#organization;
|
|
20
|
+
#email;
|
|
21
|
+
#url;
|
|
22
|
+
#note;
|
|
23
|
+
name(displayName) {
|
|
24
|
+
this.#displayName = assertText(displayName, "contact.name", {
|
|
25
|
+
max: DISPLAY_NAME_MAX,
|
|
26
|
+
});
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
vcard(value) {
|
|
30
|
+
const raw = assertText(value, "contact.vcard", {
|
|
31
|
+
max: VCARD_MAX,
|
|
32
|
+
required: false,
|
|
33
|
+
});
|
|
34
|
+
if (raw !== undefined && !raw.startsWith("BEGIN:VCARD")) {
|
|
35
|
+
throw new ValidationError("contact.vcard harus diawali BEGIN:VCARD dan diakhiri END:VCARD");
|
|
36
|
+
}
|
|
37
|
+
this.#vcard = raw;
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
phone(value) {
|
|
41
|
+
this.#phone = assertText(value, "contact.phone", { max: 64 });
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
organization(value) {
|
|
45
|
+
this.#organization = assertText(value, "contact.organization", {
|
|
46
|
+
max: 256,
|
|
47
|
+
required: false,
|
|
48
|
+
});
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
email(value) {
|
|
52
|
+
this.#email = assertText(value, "contact.email", {
|
|
53
|
+
max: 256,
|
|
54
|
+
required: false,
|
|
55
|
+
});
|
|
56
|
+
return this;
|
|
57
|
+
}
|
|
58
|
+
url(value) {
|
|
59
|
+
this.#url = assertText(value, "contact.url", {
|
|
60
|
+
max: 512,
|
|
61
|
+
required: false,
|
|
62
|
+
});
|
|
63
|
+
return this;
|
|
64
|
+
}
|
|
65
|
+
note(value) {
|
|
66
|
+
this.#note = assertText(value, "contact.note", {
|
|
67
|
+
max: 1024,
|
|
68
|
+
required: false,
|
|
69
|
+
});
|
|
70
|
+
return this;
|
|
71
|
+
}
|
|
72
|
+
build() {
|
|
73
|
+
if (this.#vcard !== undefined) {
|
|
74
|
+
const displayName = this.#displayName ?? fnOfVcard(this.#vcard) ?? "Kontak";
|
|
75
|
+
return { contactMessage: { displayName, vcard: this.#vcard } };
|
|
76
|
+
}
|
|
77
|
+
if (this.#displayName === undefined) {
|
|
78
|
+
throw new MessageError('contact.name belum diset — panggil name("Nama Orang")', "NO_NAME");
|
|
79
|
+
}
|
|
80
|
+
const vcard = buildVcard({
|
|
81
|
+
displayName: this.#displayName,
|
|
82
|
+
...(this.#phone !== undefined ? { phoneNumber: this.#phone } : {}),
|
|
83
|
+
...(this.#organization !== undefined
|
|
84
|
+
? { organization: this.#organization }
|
|
85
|
+
: {}),
|
|
86
|
+
...(this.#email !== undefined ? { email: this.#email } : {}),
|
|
87
|
+
...(this.#url !== undefined ? { url: this.#url } : {}),
|
|
88
|
+
...(this.#note !== undefined ? { note: this.#note } : {}),
|
|
89
|
+
});
|
|
90
|
+
if (vcard.length > VCARD_MAX) {
|
|
91
|
+
throw new ValidationError(`contact vcard melebihi batas ${VCARD_MAX} karakter (${vcard.length})`);
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
contactMessage: { displayName: this.#displayName, vcard },
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
export function contact(client, jid, config, options) {
|
|
99
|
+
if (config === undefined)
|
|
100
|
+
return new ContactBuilder(client);
|
|
101
|
+
return sendContact(client, jid, config, options);
|
|
102
|
+
}
|
|
103
|
+
export const applyContactConfig = (builder, config) => {
|
|
104
|
+
if (config.name !== undefined)
|
|
105
|
+
builder.name(config.name);
|
|
106
|
+
if (config.phone !== undefined)
|
|
107
|
+
builder.phone(config.phone);
|
|
108
|
+
if (config.organization !== undefined)
|
|
109
|
+
builder.organization(config.organization);
|
|
110
|
+
if (config.email !== undefined)
|
|
111
|
+
builder.email(config.email);
|
|
112
|
+
if (config.url !== undefined)
|
|
113
|
+
builder.url(config.url);
|
|
114
|
+
if (config.note !== undefined)
|
|
115
|
+
builder.note(config.note);
|
|
116
|
+
if (config.vcard !== undefined)
|
|
117
|
+
builder.vcard(config.vcard);
|
|
118
|
+
};
|
|
119
|
+
export const sendContact = async (client, jid, config, options) => {
|
|
120
|
+
const builder = new ContactBuilder(client);
|
|
121
|
+
applyContactConfig(builder, config);
|
|
122
|
+
return sendConfigured(builder, jid, options);
|
|
123
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Proto } from "zapo-js";
|
|
2
|
+
import { BaseBuilder, type SendResolutionArg } from "../core/base-builder.js";
|
|
3
|
+
import { type MessageSendOptions } from "../core/async-send.js";
|
|
4
|
+
import type { MessageSendResult } from "../zapo-client.js";
|
|
5
|
+
import type { ZapoClient } from "../zapo-client.js";
|
|
6
|
+
export interface ForwardConfig {
|
|
7
|
+
message: unknown;
|
|
8
|
+
}
|
|
9
|
+
export declare class ForwardBuilder extends BaseBuilder<Proto.IMessage> {
|
|
10
|
+
#private;
|
|
11
|
+
message(source: unknown): this;
|
|
12
|
+
build(): Proto.IMessage;
|
|
13
|
+
send(jid?: string, arg?: SendResolutionArg): Promise<unknown>;
|
|
14
|
+
targetMessageKey(): {
|
|
15
|
+
remoteJid?: string;
|
|
16
|
+
id?: string;
|
|
17
|
+
fromMe?: boolean;
|
|
18
|
+
} | undefined;
|
|
19
|
+
}
|
|
20
|
+
export declare function forward(client?: ZapoClient): ForwardBuilder;
|
|
21
|
+
export declare function forward(client: ZapoClient | undefined, jid: string, config: ForwardConfig, options?: MessageSendOptions): Promise<MessageSendResult>;
|
|
22
|
+
export declare const applyForwardConfig: (builder: ForwardBuilder, config: ForwardConfig) => void;
|
|
23
|
+
export declare const sendForward: (client: ZapoClient | undefined, jid: string | undefined, config: ForwardConfig, options?: MessageSendOptions) => Promise<MessageSendResult>;
|