@kyyinfinite/zapo-message 0.0.0-stage → 1.0.1
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 +1401 -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 +36 -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,28 @@
|
|
|
1
|
+
import type { ZapoClient } from "../zapo-client.js";
|
|
2
|
+
import type { MediaSlotResolution } from "../payloads.js";
|
|
3
|
+
import { type MediaKind, type MediaSource, type NormalizedMediaSource } from "./media-source.js";
|
|
4
|
+
export interface MediaSlotOptions {
|
|
5
|
+
mimetype?: string;
|
|
6
|
+
thumbnail?: Buffer | Uint8Array | false;
|
|
7
|
+
width?: number;
|
|
8
|
+
height?: number;
|
|
9
|
+
caption?: string;
|
|
10
|
+
seconds?: number;
|
|
11
|
+
fileName?: string;
|
|
12
|
+
title?: string;
|
|
13
|
+
ptt?: boolean;
|
|
14
|
+
waveform?: Uint8Array;
|
|
15
|
+
pngThumbnail?: Uint8Array | false;
|
|
16
|
+
isAnimated?: boolean;
|
|
17
|
+
stickerSentTs?: number;
|
|
18
|
+
}
|
|
19
|
+
export interface MediaSlot {
|
|
20
|
+
kind: MediaKind;
|
|
21
|
+
source: MediaSource;
|
|
22
|
+
options: MediaSlotOptions;
|
|
23
|
+
label: string;
|
|
24
|
+
normalized?: NormalizedMediaSource | undefined;
|
|
25
|
+
promise?: Promise<MediaSlotResolution> | undefined;
|
|
26
|
+
result?: MediaSlotResolution | undefined;
|
|
27
|
+
}
|
|
28
|
+
export declare const resolveMediaSlot: (client: ZapoClient, slot: MediaSlot) => Promise<MediaSlotResolution>;
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
import { MediaError, ValidationError } from "../core/errors.js";
|
|
2
|
+
import { createReadStream } from "node:fs";
|
|
3
|
+
import { basename } from "node:path";
|
|
4
|
+
import { fileTypeFromFile } from "file-type";
|
|
5
|
+
import { getSharedMediaQueue } from "../media-gate.js";
|
|
6
|
+
import { normalizeMediaSource, sniffBytes, readBoundedBytes, readPathBounded, UPLOAD_TYPE_BY_KIND, } from "./media-source.js";
|
|
7
|
+
import { ensureJpegThumbnail, generateImageThumbnail, generatePngThumbnail, probeImageDimensions, } from "./thumbnail.js";
|
|
8
|
+
import { assertPositiveInt, assertText } from "./validators.js";
|
|
9
|
+
import { probeWebp } from "../message/webp.js";
|
|
10
|
+
const DISPLAY_FILENAME_MAX = 100;
|
|
11
|
+
const uploadFailure = (label, error) => {
|
|
12
|
+
if (error instanceof MediaError)
|
|
13
|
+
return error;
|
|
14
|
+
return new MediaError(`upload media header ${label} gagal`, {
|
|
15
|
+
cause: error,
|
|
16
|
+
kind: "upload_failed",
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
const KIND_MIME_PREFIX = {
|
|
20
|
+
image: "image/",
|
|
21
|
+
video: "video/",
|
|
22
|
+
audio: "audio/",
|
|
23
|
+
};
|
|
24
|
+
const resolveMimetype = async (source, kind, options, label, bytes) => {
|
|
25
|
+
const explicit = options.mimetype;
|
|
26
|
+
if (explicit !== undefined &&
|
|
27
|
+
(typeof explicit !== "string" || !explicit.trim())) {
|
|
28
|
+
throw new ValidationError(`${label}.mimetype harus string mimetype, diterima ${JSON.stringify(explicit)}`);
|
|
29
|
+
}
|
|
30
|
+
if (kind === "sticker") {
|
|
31
|
+
if (explicit !== undefined && explicit !== "image/webp") {
|
|
32
|
+
throw new ValidationError(`${label}.mimetype sticker harus "image/webp", diterima "${explicit}"`);
|
|
33
|
+
}
|
|
34
|
+
if (bytes) {
|
|
35
|
+
const sniffed = (await sniffBytes(bytes))?.mime;
|
|
36
|
+
if (sniffed && sniffed !== "image/webp") {
|
|
37
|
+
throw new ValidationError(`isi sumber ${label} terdeteksi "${sniffed}" — sticker harus WebP`);
|
|
38
|
+
}
|
|
39
|
+
if (!sniffed && !isWebpHeader(bytes)) {
|
|
40
|
+
throw new ValidationError(`isi sumber ${label} bukan WebP — sticker harus file .webp`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return "image/webp";
|
|
44
|
+
}
|
|
45
|
+
let sniffed;
|
|
46
|
+
if (bytes)
|
|
47
|
+
sniffed = (await sniffBytes(bytes))?.mime;
|
|
48
|
+
else if (source.kind === "path") {
|
|
49
|
+
try {
|
|
50
|
+
sniffed = (await fileTypeFromFile(source.path))?.mime;
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
throw new MediaError(`deteksi tipe file gagal untuk ${label}`, {
|
|
54
|
+
cause: error,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (explicit) {
|
|
59
|
+
const prefix = KIND_MIME_PREFIX[kind];
|
|
60
|
+
if (prefix && !explicit.startsWith(prefix)) {
|
|
61
|
+
throw new ValidationError(`${label}.mimetype "${explicit}" tidak cocok untuk header ${kind} (harus ${prefix}*)`);
|
|
62
|
+
}
|
|
63
|
+
if (sniffed && sniffed !== explicit) {
|
|
64
|
+
throw new ValidationError(`${label}.mimetype "${explicit}" bertentangan dengan hasil deteksi isi file "${sniffed}"`);
|
|
65
|
+
}
|
|
66
|
+
return explicit;
|
|
67
|
+
}
|
|
68
|
+
if (sniffed) {
|
|
69
|
+
const prefix = KIND_MIME_PREFIX[kind];
|
|
70
|
+
if (prefix && !sniffed.startsWith(prefix)) {
|
|
71
|
+
throw new ValidationError(`isi sumber ${label} terdeteksi "${sniffed}" — bukan ${kind} yang valid`);
|
|
72
|
+
}
|
|
73
|
+
return sniffed;
|
|
74
|
+
}
|
|
75
|
+
if (kind === "document")
|
|
76
|
+
return "application/octet-stream";
|
|
77
|
+
throw new ValidationError(`mimetype ${label} tidak diketahui — sumber ${source.kind} tidak bisa dideteksi isinya, berikan options.mimetype`);
|
|
78
|
+
};
|
|
79
|
+
const resolveThumbnail = async (bytes, kind, options, label) => {
|
|
80
|
+
if (options.thumbnail === false)
|
|
81
|
+
return undefined;
|
|
82
|
+
if (options.thumbnail !== undefined && options.thumbnail !== null) {
|
|
83
|
+
return ensureJpegThumbnail(options.thumbnail, {
|
|
84
|
+
label: `${label}.thumbnail`,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
if (kind === "image" && bytes) {
|
|
88
|
+
return generateImageThumbnail(bytes, {
|
|
89
|
+
label: `${label} (thumbnail otomatis)`,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
if (kind === "sticker" && bytes) {
|
|
93
|
+
return generatePngThumbnail(bytes, {
|
|
94
|
+
label: `${label} (thumbnail otomatis)`,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
return undefined;
|
|
98
|
+
};
|
|
99
|
+
const isWebpHeader = (bytes) => probeWebp(bytes) !== undefined;
|
|
100
|
+
const assertDimension = (value, label) => {
|
|
101
|
+
if (value === undefined)
|
|
102
|
+
return undefined;
|
|
103
|
+
return assertPositiveInt(value, label, { min: 1 });
|
|
104
|
+
};
|
|
105
|
+
const mediaProtoFields = ({ uploaded, mimetype, kind, options, dimensions, webp, thumbnail, label, normalized, }) => {
|
|
106
|
+
const base = {
|
|
107
|
+
url: uploaded.url,
|
|
108
|
+
mimetype,
|
|
109
|
+
fileSha256: uploaded.fileSha256,
|
|
110
|
+
fileEncSha256: uploaded.fileEncSha256,
|
|
111
|
+
fileLength: uploaded.fileLength,
|
|
112
|
+
mediaKey: uploaded.mediaKey,
|
|
113
|
+
directPath: uploaded.directPath,
|
|
114
|
+
mediaKeyTimestamp: uploaded.mediaKeyTimestamp,
|
|
115
|
+
};
|
|
116
|
+
if (kind === "image") {
|
|
117
|
+
const width = assertDimension(options.width, `${label}.width`) ?? dimensions?.width;
|
|
118
|
+
const height = assertDimension(options.height, `${label}.height`) ?? dimensions?.height;
|
|
119
|
+
const caption = options.caption !== undefined
|
|
120
|
+
? assertText(options.caption, `${label}.caption`, { required: false })
|
|
121
|
+
: undefined;
|
|
122
|
+
return {
|
|
123
|
+
field: "imageMessage",
|
|
124
|
+
message: {
|
|
125
|
+
...base,
|
|
126
|
+
...(width !== undefined ? { width } : {}),
|
|
127
|
+
...(height !== undefined ? { height } : {}),
|
|
128
|
+
...(thumbnail ? { jpegThumbnail: thumbnail } : {}),
|
|
129
|
+
...(caption !== undefined ? { caption } : {}),
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
if (kind === "video") {
|
|
134
|
+
const seconds = options.seconds === undefined
|
|
135
|
+
? undefined
|
|
136
|
+
: assertPositiveInt(options.seconds, `${label}.seconds`, { min: 0 });
|
|
137
|
+
const width = assertDimension(options.width, `${label}.width`);
|
|
138
|
+
const height = assertDimension(options.height, `${label}.height`);
|
|
139
|
+
return {
|
|
140
|
+
field: "videoMessage",
|
|
141
|
+
message: {
|
|
142
|
+
...base,
|
|
143
|
+
...(seconds !== undefined ? { seconds } : {}),
|
|
144
|
+
...(width !== undefined ? { width } : {}),
|
|
145
|
+
...(height !== undefined ? { height } : {}),
|
|
146
|
+
...(thumbnail ? { jpegThumbnail: thumbnail } : {}),
|
|
147
|
+
...(uploaded.streamingSidecar
|
|
148
|
+
? { streamingSidecar: uploaded.streamingSidecar }
|
|
149
|
+
: {}),
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
if (kind === "audio") {
|
|
154
|
+
const seconds = options.seconds === undefined
|
|
155
|
+
? undefined
|
|
156
|
+
: assertPositiveInt(options.seconds, `${label}.seconds`, { min: 0 });
|
|
157
|
+
return {
|
|
158
|
+
field: "audioMessage",
|
|
159
|
+
message: {
|
|
160
|
+
...base,
|
|
161
|
+
...(seconds !== undefined ? { seconds } : {}),
|
|
162
|
+
...(options.ptt === true ? { ptt: true } : {}),
|
|
163
|
+
...(options.waveform !== undefined
|
|
164
|
+
? { waveform: options.waveform }
|
|
165
|
+
: {}),
|
|
166
|
+
...(uploaded.streamingSidecar
|
|
167
|
+
? { streamingSidecar: uploaded.streamingSidecar }
|
|
168
|
+
: {}),
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
if (kind === "sticker") {
|
|
173
|
+
const probed = webp;
|
|
174
|
+
const width = assertDimension(options.width, `${label}.width`) ?? probed?.width;
|
|
175
|
+
const height = assertDimension(options.height, `${label}.height`) ?? probed?.height;
|
|
176
|
+
const pngThumbnail = options.pngThumbnail === false
|
|
177
|
+
? undefined
|
|
178
|
+
: options.pngThumbnail !== undefined
|
|
179
|
+
? options.pngThumbnail
|
|
180
|
+
: thumbnail;
|
|
181
|
+
const animated = options.isAnimated ?? probed?.isAnimated ?? false;
|
|
182
|
+
return {
|
|
183
|
+
field: "stickerMessage",
|
|
184
|
+
message: {
|
|
185
|
+
...base,
|
|
186
|
+
...(width !== undefined ? { width } : {}),
|
|
187
|
+
...(height !== undefined ? { height } : {}),
|
|
188
|
+
...(pngThumbnail !== undefined ? { pngThumbnail } : {}),
|
|
189
|
+
isAnimated: animated,
|
|
190
|
+
...(options.stickerSentTs !== undefined
|
|
191
|
+
? { stickerSentTs: options.stickerSentTs }
|
|
192
|
+
: {}),
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
const title = options.title !== undefined
|
|
197
|
+
? assertText(options.title, `${label}.title`, { required: false })
|
|
198
|
+
: undefined;
|
|
199
|
+
return {
|
|
200
|
+
field: "documentMessage",
|
|
201
|
+
message: {
|
|
202
|
+
...base,
|
|
203
|
+
fileName: resolveDocumentFileName(options, normalized, label),
|
|
204
|
+
...(title !== undefined ? { title } : {}),
|
|
205
|
+
...(thumbnail ? { jpegThumbnail: thumbnail } : {}),
|
|
206
|
+
},
|
|
207
|
+
};
|
|
208
|
+
};
|
|
209
|
+
const resolveDocumentFileName = (options, normalized, label) => {
|
|
210
|
+
if (options.fileName !== undefined) {
|
|
211
|
+
return assertText(options.fileName, `${label}.fileName`, {
|
|
212
|
+
max: DISPLAY_FILENAME_MAX,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
if (normalized.kind === "path")
|
|
216
|
+
return basename(normalized.path);
|
|
217
|
+
throw new ValidationError(`${label} document butuh fileName — berikan options.fileName (sumber bukan path tidak bisa menebak nama file)`);
|
|
218
|
+
};
|
|
219
|
+
export const resolveMediaSlot = async (client, slot) => {
|
|
220
|
+
const { kind, options = {}, label } = slot;
|
|
221
|
+
const uploadType = kind === "audio" && options.ptt === true
|
|
222
|
+
? "ptt"
|
|
223
|
+
: UPLOAD_TYPE_BY_KIND[kind];
|
|
224
|
+
const queue = getSharedMediaQueue();
|
|
225
|
+
if (!slot.normalized) {
|
|
226
|
+
try {
|
|
227
|
+
slot.normalized = await normalizeMediaSource(slot.source, { label });
|
|
228
|
+
}
|
|
229
|
+
catch (error) {
|
|
230
|
+
throw new MediaError(`sumber media ${label} tidak bisa dipakai`, {
|
|
231
|
+
cause: error,
|
|
232
|
+
kind: "source_invalid",
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
const normalized = slot.normalized;
|
|
237
|
+
try {
|
|
238
|
+
return await queue.run(async ({ maxBytes, noteBytes }) => {
|
|
239
|
+
let bytes;
|
|
240
|
+
if (normalized.kind === "bytes") {
|
|
241
|
+
bytes = normalized.bytes;
|
|
242
|
+
}
|
|
243
|
+
else if (normalized.kind === "stream" && normalized.streamConsumed) {
|
|
244
|
+
throw new MediaError("stream sumber media sudah dikonsumsi sekali dan tidak bisa dipakai ulang — sumber stream hanya mendukung satu upload", { kind: "stream_consumed" });
|
|
245
|
+
}
|
|
246
|
+
else if (kind === "image" || kind === "sticker") {
|
|
247
|
+
bytes =
|
|
248
|
+
normalized.kind === "path"
|
|
249
|
+
? await readPathBounded(normalized.path, maxBytes, label)
|
|
250
|
+
: normalized.kind === "stream"
|
|
251
|
+
? await readBoundedBytes(normalized.stream, maxBytes, label)
|
|
252
|
+
: undefined;
|
|
253
|
+
if (normalized.kind === "stream")
|
|
254
|
+
normalized.streamConsumed = true;
|
|
255
|
+
}
|
|
256
|
+
const mimetype = await resolveMimetype(normalized, kind, options, label, bytes);
|
|
257
|
+
const thumbnail = await resolveThumbnail(bytes, kind, options, label);
|
|
258
|
+
const dimensions = bytes && kind === "image"
|
|
259
|
+
? await probeImageDimensions(bytes)
|
|
260
|
+
: undefined;
|
|
261
|
+
const webp = bytes && kind === "sticker" ? probeWebp(bytes) : undefined;
|
|
262
|
+
let input;
|
|
263
|
+
if (bytes) {
|
|
264
|
+
if (bytes.byteLength > maxBytes) {
|
|
265
|
+
throw new MediaError(`media ${label} melebihi batas ukuran (${bytes.byteLength} > ${maxBytes} byte)`, { kind: "oversized" });
|
|
266
|
+
}
|
|
267
|
+
input = bytes;
|
|
268
|
+
}
|
|
269
|
+
else if (normalized.kind === "path") {
|
|
270
|
+
input = createReadStream(normalized.path);
|
|
271
|
+
}
|
|
272
|
+
else if (normalized.kind === "stream") {
|
|
273
|
+
normalized.streamConsumed = true;
|
|
274
|
+
input = normalized.stream;
|
|
275
|
+
}
|
|
276
|
+
else {
|
|
277
|
+
throw new MediaError(`media ${label} tidak punya sumber input`, {
|
|
278
|
+
kind: "source_invalid",
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
const upload = client.message.upload;
|
|
282
|
+
if (typeof upload !== "function") {
|
|
283
|
+
throw uploadFailure(label, new TypeError("client.message.upload is not a function"));
|
|
284
|
+
}
|
|
285
|
+
let uploaded;
|
|
286
|
+
try {
|
|
287
|
+
uploaded = await upload.call(client.message, input, {
|
|
288
|
+
type: uploadType,
|
|
289
|
+
mimetype,
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
catch (error) {
|
|
293
|
+
throw uploadFailure(label, error);
|
|
294
|
+
}
|
|
295
|
+
if (!uploaded || typeof uploaded !== "object" || !uploaded.url) {
|
|
296
|
+
throw new MediaError(`uploader mengembalikan hasil tidak valid untuk ${label} — UploadResult.url wajib ada`, { kind: "upload_invalid" });
|
|
297
|
+
}
|
|
298
|
+
const normalizedByteSize = normalized.kind === "bytes" || normalized.kind === "path"
|
|
299
|
+
? normalized.byteSize
|
|
300
|
+
: undefined;
|
|
301
|
+
noteBytes(Number(uploaded.fileLength) || normalizedByteSize || 0);
|
|
302
|
+
return mediaProtoFields({
|
|
303
|
+
uploaded,
|
|
304
|
+
mimetype,
|
|
305
|
+
kind,
|
|
306
|
+
options,
|
|
307
|
+
dimensions,
|
|
308
|
+
webp,
|
|
309
|
+
thumbnail,
|
|
310
|
+
label,
|
|
311
|
+
normalized,
|
|
312
|
+
});
|
|
313
|
+
}, { label: `btn-media:${label}` });
|
|
314
|
+
}
|
|
315
|
+
catch (error) {
|
|
316
|
+
if (error instanceof MediaError || error instanceof ValidationError)
|
|
317
|
+
throw error;
|
|
318
|
+
throw new MediaError(`pemrosesan media header ${label} gagal`, {
|
|
319
|
+
cause: error,
|
|
320
|
+
kind: "media_failed",
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { Readable } from "node:stream";
|
|
2
|
+
export type MediaSource = Buffer | Uint8Array | string | Readable;
|
|
3
|
+
export declare const UPLOAD_TYPE_BY_KIND: Record<"image" | "video" | "document" | "audio" | "sticker", "image" | "video" | "document" | "audio" | "sticker">;
|
|
4
|
+
export type MediaKind = "image" | "video" | "document" | "audio" | "sticker";
|
|
5
|
+
export interface NormalizedBytesSource {
|
|
6
|
+
kind: "bytes";
|
|
7
|
+
bytes: Buffer;
|
|
8
|
+
byteSize: number;
|
|
9
|
+
}
|
|
10
|
+
export interface NormalizedPathSource {
|
|
11
|
+
kind: "path";
|
|
12
|
+
path: string;
|
|
13
|
+
byteSize: number;
|
|
14
|
+
}
|
|
15
|
+
export interface NormalizedStreamSource {
|
|
16
|
+
kind: "stream";
|
|
17
|
+
stream: Readable;
|
|
18
|
+
streamConsumed?: boolean;
|
|
19
|
+
}
|
|
20
|
+
export type NormalizedMediaSource = NormalizedBytesSource | NormalizedPathSource | NormalizedStreamSource;
|
|
21
|
+
export interface NormalizedMediaSourceOptions {
|
|
22
|
+
label?: string;
|
|
23
|
+
}
|
|
24
|
+
export declare const isReadableStreamSource: (value: unknown) => value is Readable;
|
|
25
|
+
export declare const bufferFromBytes: (bytes: Uint8Array) => Buffer;
|
|
26
|
+
export declare const sniffBytes: (bytes: Uint8Array) => Promise<{
|
|
27
|
+
ext: string;
|
|
28
|
+
mime: string;
|
|
29
|
+
} | undefined>;
|
|
30
|
+
export declare const sniffFile: (path: string) => Promise<{
|
|
31
|
+
ext: string;
|
|
32
|
+
mime: string;
|
|
33
|
+
} | undefined>;
|
|
34
|
+
export declare const readBoundedBytes: (stream: Readable, maxBytes: number, label: string) => Promise<Buffer>;
|
|
35
|
+
export declare const readPathBounded: (path: string, maxBytes: number, label: string) => Promise<Buffer>;
|
|
36
|
+
export declare const normalizeMediaSource: (source: MediaSource | unknown, { label }?: NormalizedMediaSourceOptions) => Promise<NormalizedMediaSource>;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { stat } from "node:fs/promises";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { fileTypeFromBuffer, fileTypeFromFile } from "file-type";
|
|
4
|
+
import { MediaError, ValidationError } from "../core/errors.js";
|
|
5
|
+
export const UPLOAD_TYPE_BY_KIND = {
|
|
6
|
+
image: "image",
|
|
7
|
+
video: "video",
|
|
8
|
+
document: "document",
|
|
9
|
+
audio: "audio",
|
|
10
|
+
sticker: "sticker",
|
|
11
|
+
};
|
|
12
|
+
const isReadableStream = (value) => !!value &&
|
|
13
|
+
typeof value === "object" &&
|
|
14
|
+
typeof value.pipe === "function" &&
|
|
15
|
+
typeof value.read === "function";
|
|
16
|
+
export const isReadableStreamSource = isReadableStream;
|
|
17
|
+
export const bufferFromBytes = (bytes) => {
|
|
18
|
+
if (Buffer.isBuffer(bytes))
|
|
19
|
+
return bytes;
|
|
20
|
+
return Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
21
|
+
};
|
|
22
|
+
export const sniffBytes = async (bytes) => {
|
|
23
|
+
try {
|
|
24
|
+
return await fileTypeFromBuffer(bytes);
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
throw new MediaError("deteksi tipe file dari bytes gagal", {
|
|
28
|
+
cause: error,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
export const sniffFile = async (path) => {
|
|
33
|
+
try {
|
|
34
|
+
return await fileTypeFromFile(path);
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
throw new MediaError(`deteksi tipe file gagal untuk ${path}`, {
|
|
38
|
+
cause: error,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
export const readBoundedBytes = async (stream, maxBytes, label) => {
|
|
43
|
+
const chunks = [];
|
|
44
|
+
let total = 0;
|
|
45
|
+
try {
|
|
46
|
+
for await (const chunk of stream) {
|
|
47
|
+
const part = Buffer.isBuffer(chunk)
|
|
48
|
+
? chunk
|
|
49
|
+
: Buffer.from(chunk);
|
|
50
|
+
total += part.byteLength;
|
|
51
|
+
if (total > maxBytes) {
|
|
52
|
+
stream.destroy?.();
|
|
53
|
+
throw new MediaError(`media ${label} melebihi batas ukuran (${total} > ${maxBytes} byte)`, { kind: "oversized" });
|
|
54
|
+
}
|
|
55
|
+
chunks.push(part);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
if (error instanceof MediaError)
|
|
60
|
+
throw error;
|
|
61
|
+
throw new MediaError(`gagal membaca stream ${label}`, { cause: error });
|
|
62
|
+
}
|
|
63
|
+
return Buffer.concat(chunks);
|
|
64
|
+
};
|
|
65
|
+
export const readPathBounded = async (path, maxBytes, label) => {
|
|
66
|
+
let info;
|
|
67
|
+
try {
|
|
68
|
+
info = await stat(path);
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
throw new ValidationError(`file sumber media ${label} tidak ditemukan: ${path}`, { cause: error });
|
|
72
|
+
}
|
|
73
|
+
if (!info.isFile())
|
|
74
|
+
throw new ValidationError(`sumber media ${label} bukan file: ${path}`);
|
|
75
|
+
if (info.size > maxBytes) {
|
|
76
|
+
throw new MediaError(`media ${label} melebihi batas ukuran (${info.size} > ${maxBytes} byte)`, { kind: "oversized" });
|
|
77
|
+
}
|
|
78
|
+
try {
|
|
79
|
+
return await readFile(path);
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
throw new MediaError(`gagal membaca file ${label}: ${path}`, {
|
|
83
|
+
cause: error,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
export const normalizeMediaSource = async (source, { label = "media" } = {}) => {
|
|
88
|
+
if (source === undefined ||
|
|
89
|
+
source === null ||
|
|
90
|
+
(typeof source === "string" && source.trim() === "")) {
|
|
91
|
+
throw new ValidationError(`sumber ${label} kosong — butuh Buffer, Uint8Array, path file, atau Readable stream`);
|
|
92
|
+
}
|
|
93
|
+
if (Buffer.isBuffer(source)) {
|
|
94
|
+
return { kind: "bytes", bytes: source, byteSize: source.byteLength };
|
|
95
|
+
}
|
|
96
|
+
if (source instanceof Uint8Array) {
|
|
97
|
+
const bytes = bufferFromBytes(source);
|
|
98
|
+
return { kind: "bytes", bytes, byteSize: bytes.byteLength };
|
|
99
|
+
}
|
|
100
|
+
if (typeof source === "string") {
|
|
101
|
+
let info;
|
|
102
|
+
try {
|
|
103
|
+
info = await stat(source);
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
throw new ValidationError(`file sumber ${label} tidak ditemukan: ${source}`, { cause: error });
|
|
107
|
+
}
|
|
108
|
+
if (!info.isFile())
|
|
109
|
+
throw new ValidationError(`sumber ${label} bukan file: ${source}`);
|
|
110
|
+
return { kind: "path", path: source, byteSize: info.size };
|
|
111
|
+
}
|
|
112
|
+
if (isReadableStream(source)) {
|
|
113
|
+
return { kind: "stream", stream: source };
|
|
114
|
+
}
|
|
115
|
+
throw new ValidationError(`sumber ${label} tidak valid: ${typeof source} — didukung Buffer, Uint8Array, path file, atau Readable stream`);
|
|
116
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export type SharpModule = typeof import("sharp").default;
|
|
2
|
+
export declare const SHARP_INSTALL_HINT = "sharp tidak tersedia \u2014 instal sharp (peer dependency opsional) untuk pemrosesan gambar otomatis";
|
|
3
|
+
export declare const loadSharp: () => Promise<SharpModule | null>;
|
|
4
|
+
export declare const resetSharpCache: () => void;
|
|
5
|
+
export declare const isSharpCached: () => boolean;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
let cached;
|
|
2
|
+
export const SHARP_INSTALL_HINT = "sharp tidak tersedia — instal sharp (peer dependency opsional) untuk pemrosesan gambar otomatis";
|
|
3
|
+
export const loadSharp = async () => {
|
|
4
|
+
if (cached !== undefined)
|
|
5
|
+
return cached;
|
|
6
|
+
try {
|
|
7
|
+
const mod = await import("sharp");
|
|
8
|
+
cached = mod.default ?? null;
|
|
9
|
+
}
|
|
10
|
+
catch {
|
|
11
|
+
cached = null;
|
|
12
|
+
}
|
|
13
|
+
return cached;
|
|
14
|
+
};
|
|
15
|
+
export const resetSharpCache = () => {
|
|
16
|
+
cached = undefined;
|
|
17
|
+
};
|
|
18
|
+
export const isSharpCached = () => cached !== undefined;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare const THUMBNAIL_MAX_DIMENSION = 240;
|
|
2
|
+
export declare const THUMBNAIL_MAX_BYTES: number;
|
|
3
|
+
export declare const THUMBNAIL_JPEG_QUALITY = 72;
|
|
4
|
+
export declare const isJpegBytes: (bytes: Uint8Array | null | undefined) => boolean;
|
|
5
|
+
export declare const asThumbnailBytes: (bytes: unknown) => Uint8Array;
|
|
6
|
+
export interface ThumbnailLabelOptions {
|
|
7
|
+
label?: string;
|
|
8
|
+
}
|
|
9
|
+
export declare const ensureJpegThumbnail: (bytes: unknown, { label }?: ThumbnailLabelOptions) => Promise<Buffer>;
|
|
10
|
+
export declare const validateJpegThumbnail: (bytes: unknown, { label }?: ThumbnailLabelOptions) => Buffer;
|
|
11
|
+
export interface GenerateThumbnailOptions extends ThumbnailLabelOptions {
|
|
12
|
+
maxDimension?: number;
|
|
13
|
+
quality?: number;
|
|
14
|
+
}
|
|
15
|
+
export declare const generateImageThumbnail: (bytes: unknown, { label, maxDimension, quality, }?: GenerateThumbnailOptions) => Promise<Buffer>;
|
|
16
|
+
export interface ImageDimensions {
|
|
17
|
+
width: number;
|
|
18
|
+
height: number;
|
|
19
|
+
}
|
|
20
|
+
export declare const probeImageDimensions: (bytes: unknown) => Promise<ImageDimensions | undefined>;
|
|
21
|
+
export declare const generatePngThumbnail: (bytes: unknown, { label, maxDimension, }?: GenerateThumbnailOptions) => Promise<Buffer | undefined>;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { bufferFromBytes } from "./media-source.js";
|
|
2
|
+
import { loadSharp, SHARP_INSTALL_HINT } from "./sharp-loader.js";
|
|
3
|
+
import { MediaError, ValidationError } from "../core/errors.js";
|
|
4
|
+
export const THUMBNAIL_MAX_DIMENSION = 240;
|
|
5
|
+
export const THUMBNAIL_MAX_BYTES = 100 * 1024;
|
|
6
|
+
export const THUMBNAIL_JPEG_QUALITY = 72;
|
|
7
|
+
const JPEG_MAGIC = [0xff, 0xd8, 0xff];
|
|
8
|
+
export const isJpegBytes = (bytes) => {
|
|
9
|
+
if (!bytes || bytes.byteLength < 3)
|
|
10
|
+
return false;
|
|
11
|
+
return (bytes[0] === JPEG_MAGIC[0] &&
|
|
12
|
+
bytes[1] === JPEG_MAGIC[1] &&
|
|
13
|
+
bytes[2] === JPEG_MAGIC[2]);
|
|
14
|
+
};
|
|
15
|
+
export const asThumbnailBytes = (bytes) => {
|
|
16
|
+
if (!Buffer.isBuffer(bytes) && !(bytes instanceof Uint8Array)) {
|
|
17
|
+
throw new ValidationError(`thumbnail butuh Buffer atau Uint8Array, diterima ${typeof bytes}`);
|
|
18
|
+
}
|
|
19
|
+
if (!bytes.byteLength) {
|
|
20
|
+
throw new ValidationError("thumbnail kosong (0 byte)");
|
|
21
|
+
}
|
|
22
|
+
return bytes;
|
|
23
|
+
};
|
|
24
|
+
export const ensureJpegThumbnail = async (bytes, { label = "thumbnail" } = {}) => {
|
|
25
|
+
const input = asThumbnailBytes(bytes);
|
|
26
|
+
if (isJpegBytes(input))
|
|
27
|
+
return bufferFromBytes(input);
|
|
28
|
+
const sharp = await loadSharp();
|
|
29
|
+
if (!sharp) {
|
|
30
|
+
throw new ValidationError(`${label} bukan JPEG valid dan ${SHARP_INSTALL_HINT}`, { kind: "not_jpeg" });
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
return await sharp(input)
|
|
34
|
+
.jpeg({ quality: THUMBNAIL_JPEG_QUALITY })
|
|
35
|
+
.toBuffer();
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
throw new ValidationError(`${label} bukan JPEG valid dan gagal dikonversi ke JPEG — berikan JPEG asli atau gambar yang bisa diproses Sharp`, { cause: error, kind: "not_jpeg" });
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
export const validateJpegThumbnail = (bytes, { label = "thumbnail" } = {}) => {
|
|
42
|
+
const input = asThumbnailBytes(bytes);
|
|
43
|
+
if (!isJpegBytes(input)) {
|
|
44
|
+
throw new ValidationError(`${label} harus JPEG valid (magic bytes FFD8FF) — diterima ${describeBytes(input)}`, { kind: "not_jpeg" });
|
|
45
|
+
}
|
|
46
|
+
return bufferFromBytes(input);
|
|
47
|
+
};
|
|
48
|
+
export const generateImageThumbnail = async (bytes, { label = "thumbnail", maxDimension = THUMBNAIL_MAX_DIMENSION, quality = THUMBNAIL_JPEG_QUALITY, } = {}) => {
|
|
49
|
+
const input = asThumbnailBytes(bytes);
|
|
50
|
+
const sharp = await loadSharp();
|
|
51
|
+
if (!sharp) {
|
|
52
|
+
throw new MediaError(`generasi thumbnail otomatis ${label} gagal — ${SHARP_INSTALL_HINT}`, { kind: "thumbnail_failed" });
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
const thumbnail = await sharp(input)
|
|
56
|
+
.resize({
|
|
57
|
+
width: maxDimension,
|
|
58
|
+
height: maxDimension,
|
|
59
|
+
fit: "inside",
|
|
60
|
+
withoutEnlargement: true,
|
|
61
|
+
})
|
|
62
|
+
.jpeg({ quality })
|
|
63
|
+
.toBuffer();
|
|
64
|
+
if (thumbnail.byteLength > THUMBNAIL_MAX_BYTES) {
|
|
65
|
+
throw new MediaError(`thumbnail ${label} terlalu besar setelah kompresi (${thumbnail.byteLength} > ${THUMBNAIL_MAX_BYTES} byte)`, { kind: "thumbnail_oversized" });
|
|
66
|
+
}
|
|
67
|
+
return thumbnail;
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
if (error instanceof MediaError)
|
|
71
|
+
throw error;
|
|
72
|
+
throw new MediaError(`generasi thumbnail otomatis ${label} gagal — sumber bukan gambar yang bisa diproses Sharp`, { cause: error, kind: "thumbnail_failed" });
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
export const probeImageDimensions = async (bytes) => {
|
|
76
|
+
const input = asThumbnailBytes(bytes);
|
|
77
|
+
const sharp = await loadSharp();
|
|
78
|
+
if (!sharp)
|
|
79
|
+
return undefined;
|
|
80
|
+
try {
|
|
81
|
+
const metadata = await sharp(input).metadata();
|
|
82
|
+
const width = Number.isFinite(metadata.width) ? metadata.width : undefined;
|
|
83
|
+
const height = Number.isFinite(metadata.height)
|
|
84
|
+
? metadata.height
|
|
85
|
+
: undefined;
|
|
86
|
+
if (width && height)
|
|
87
|
+
return { width, height };
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
export const generatePngThumbnail = async (bytes, { label = "thumbnail", maxDimension = THUMBNAIL_MAX_DIMENSION, } = {}) => {
|
|
95
|
+
const input = asThumbnailBytes(bytes);
|
|
96
|
+
const sharp = await loadSharp();
|
|
97
|
+
if (!sharp)
|
|
98
|
+
return undefined;
|
|
99
|
+
try {
|
|
100
|
+
const thumbnail = await sharp(input)
|
|
101
|
+
.resize({
|
|
102
|
+
width: maxDimension,
|
|
103
|
+
height: maxDimension,
|
|
104
|
+
fit: "inside",
|
|
105
|
+
withoutEnlargement: true,
|
|
106
|
+
})
|
|
107
|
+
.png({ compressionLevel: 9 })
|
|
108
|
+
.toBuffer();
|
|
109
|
+
if (thumbnail.byteLength > THUMBNAIL_MAX_BYTES)
|
|
110
|
+
return undefined;
|
|
111
|
+
return thumbnail;
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
const describeBytes = (bytes) => {
|
|
118
|
+
if (bytes.byteLength < 3)
|
|
119
|
+
return `bytes terlalu pendek (${bytes.byteLength} byte)`;
|
|
120
|
+
const hex = [...bytes.slice(0, 3)]
|
|
121
|
+
.map((b) => b.toString(16).padStart(2, "0"))
|
|
122
|
+
.join(" ");
|
|
123
|
+
return `bytes dengan magic ${hex}`;
|
|
124
|
+
};
|