@tma.js/init-data-node 1.1.16 → 1.2.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/dist/dts/index.d.ts +3 -0
- package/dist/dts/initDataToSearchParams.d.ts +4 -0
- package/dist/dts/sign.d.ts +10 -0
- package/dist/dts/signData.d.ts +7 -0
- package/dist/dts/types.d.ts +4 -0
- package/dist/dts/validate.d.ts +14 -21
- package/dist/index.cjs +3 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +172 -108
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,50 +1,112 @@
|
|
|
1
1
|
import { createHmac as b } from "node:crypto";
|
|
2
|
-
import { URLSearchParams as
|
|
3
|
-
function
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
2
|
+
import { URLSearchParams as m } from "node:url";
|
|
3
|
+
function E(t, e) {
|
|
4
|
+
return b(
|
|
5
|
+
"sha256",
|
|
6
|
+
b("sha256", "WebAppData").update(e).digest()
|
|
7
|
+
).update(t).digest().toString("hex");
|
|
8
|
+
}
|
|
9
|
+
function $(t) {
|
|
10
|
+
const e = {};
|
|
11
|
+
for (const n in t) {
|
|
12
|
+
const r = t[n];
|
|
13
|
+
r !== void 0 && (e[n] = r);
|
|
14
|
+
}
|
|
15
|
+
return e;
|
|
16
|
+
}
|
|
17
|
+
function S(t) {
|
|
18
|
+
return t ? JSON.stringify({
|
|
19
|
+
added_to_attachment_menu: t.addedToAttachmentMenu,
|
|
20
|
+
allows_write_to_pm: t.allowsWriteToPm,
|
|
21
|
+
first_name: t.firstName,
|
|
22
|
+
id: t.id,
|
|
23
|
+
is_bot: t.isBot,
|
|
24
|
+
is_premium: t.isPremium,
|
|
25
|
+
language_code: t.languageCode,
|
|
26
|
+
last_name: t.lastName,
|
|
27
|
+
photo_url: t.photoUrl,
|
|
28
|
+
username: t.username
|
|
29
|
+
}) : void 0;
|
|
30
|
+
}
|
|
31
|
+
function N({
|
|
32
|
+
chat: t,
|
|
33
|
+
receiver: e,
|
|
34
|
+
user: n,
|
|
35
|
+
...r
|
|
36
|
+
}) {
|
|
37
|
+
var o;
|
|
38
|
+
return new m(
|
|
39
|
+
$({
|
|
40
|
+
auth_date: r.authDate ? (+r.authDate / 1e3 | 0).toString() : void 0,
|
|
41
|
+
can_send_after: (o = r.canSendAfter) == null ? void 0 : o.toString(),
|
|
42
|
+
chat: t ? JSON.stringify({
|
|
43
|
+
id: t.id,
|
|
44
|
+
type: t.type,
|
|
45
|
+
title: t.title,
|
|
46
|
+
photo_url: t.type,
|
|
47
|
+
username: t.username
|
|
48
|
+
}) : void 0,
|
|
49
|
+
chat_instance: r.chatInstance,
|
|
50
|
+
chat_type: r.chatType || void 0,
|
|
51
|
+
hash: r.hash,
|
|
52
|
+
query_id: r.queryId,
|
|
53
|
+
receiver: S(e),
|
|
54
|
+
start_param: r.startParam || void 0,
|
|
55
|
+
user: S(n)
|
|
56
|
+
})
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
function B(t, e, n) {
|
|
60
|
+
const r = N({
|
|
61
|
+
...t,
|
|
62
|
+
authDate: n
|
|
63
|
+
}), o = [...r.entries()].map(([i, u]) => `${i}=${u}`).sort();
|
|
64
|
+
return r.append("hash", E(o.join(`
|
|
65
|
+
`), e)), r.toString();
|
|
66
|
+
}
|
|
67
|
+
function M(t, e, n = {}) {
|
|
68
|
+
let r, o;
|
|
69
|
+
const i = [];
|
|
70
|
+
if (new m(
|
|
71
|
+
typeof t == "string" || t instanceof m ? t : N(t)
|
|
72
|
+
).forEach((s, l) => {
|
|
73
|
+
if (l === "hash") {
|
|
74
|
+
o = s;
|
|
10
75
|
return;
|
|
11
76
|
}
|
|
12
|
-
if (
|
|
13
|
-
const g = parseInt(
|
|
77
|
+
if (l === "auth_date") {
|
|
78
|
+
const g = parseInt(s, 10);
|
|
14
79
|
if (Number.isNaN(g))
|
|
15
80
|
throw new TypeError('"auth_date" should present integer');
|
|
16
|
-
|
|
81
|
+
r = new Date(g * 1e3);
|
|
17
82
|
}
|
|
18
|
-
|
|
19
|
-
}),
|
|
83
|
+
i.push(`${l}=${s}`);
|
|
84
|
+
}), !o)
|
|
20
85
|
throw new Error('"hash" is empty or not found');
|
|
21
|
-
if (
|
|
86
|
+
if (!r)
|
|
22
87
|
throw new Error('"auth_date" is empty or not found');
|
|
23
|
-
const { expiresIn:
|
|
24
|
-
if (
|
|
88
|
+
const { expiresIn: u = 86400 } = n;
|
|
89
|
+
if (u > 0 && +r + u * 1e3 < Date.now())
|
|
25
90
|
throw new Error("Init data expired");
|
|
26
|
-
if (
|
|
27
|
-
|
|
28
|
-
b("sha256", "WebAppData").update(e).digest()
|
|
29
|
-
).update(u.join(`
|
|
30
|
-
`)).digest().toString("hex") !== i)
|
|
91
|
+
if (i.sort(), E(i.join(`
|
|
92
|
+
`), e) !== o)
|
|
31
93
|
throw new Error("Signature is invalid");
|
|
32
94
|
}
|
|
33
|
-
class
|
|
34
|
-
constructor(e,
|
|
35
|
-
super(
|
|
95
|
+
class _ extends Error {
|
|
96
|
+
constructor(e, n, r) {
|
|
97
|
+
super(n, { cause: r }), this.type = e, Object.setPrototypeOf(this, _.prototype);
|
|
36
98
|
}
|
|
37
99
|
}
|
|
38
|
-
function
|
|
39
|
-
return new
|
|
100
|
+
function y(t, e, n) {
|
|
101
|
+
return new _(t, e, n);
|
|
40
102
|
}
|
|
41
|
-
const
|
|
103
|
+
const R = "ERR_UNEXPECTED_TYPE", P = "ERR_PARSE";
|
|
42
104
|
function h() {
|
|
43
|
-
return
|
|
105
|
+
return y(R, "Value has unexpected type");
|
|
44
106
|
}
|
|
45
107
|
class w {
|
|
46
|
-
constructor(e,
|
|
47
|
-
this.parser = e, this.isOptional =
|
|
108
|
+
constructor(e, n, r) {
|
|
109
|
+
this.parser = e, this.isOptional = n, this.type = r;
|
|
48
110
|
}
|
|
49
111
|
/**
|
|
50
112
|
* Attempts to parse passed value
|
|
@@ -56,11 +118,11 @@ class w {
|
|
|
56
118
|
if (!(this.isOptional && e === void 0))
|
|
57
119
|
try {
|
|
58
120
|
return this.parser(e);
|
|
59
|
-
} catch (
|
|
60
|
-
throw
|
|
121
|
+
} catch (n) {
|
|
122
|
+
throw y(
|
|
61
123
|
P,
|
|
62
124
|
`Unable to parse value${this.type ? ` as ${this.type}` : ""}`,
|
|
63
|
-
|
|
125
|
+
n
|
|
64
126
|
);
|
|
65
127
|
}
|
|
66
128
|
}
|
|
@@ -68,10 +130,10 @@ class w {
|
|
|
68
130
|
return this.isOptional = !0, this;
|
|
69
131
|
}
|
|
70
132
|
}
|
|
71
|
-
function
|
|
133
|
+
function d(t, e) {
|
|
72
134
|
return () => new w(t, !1, e);
|
|
73
135
|
}
|
|
74
|
-
const c =
|
|
136
|
+
const c = d((t) => {
|
|
75
137
|
if (typeof t == "boolean")
|
|
76
138
|
return t;
|
|
77
139
|
const e = String(t);
|
|
@@ -81,41 +143,41 @@ const c = l((t) => {
|
|
|
81
143
|
return !1;
|
|
82
144
|
throw h();
|
|
83
145
|
}, "boolean");
|
|
84
|
-
function
|
|
85
|
-
const
|
|
86
|
-
for (const
|
|
87
|
-
const o = t[
|
|
146
|
+
function D(t, e) {
|
|
147
|
+
const n = {};
|
|
148
|
+
for (const r in t) {
|
|
149
|
+
const o = t[r];
|
|
88
150
|
if (!o)
|
|
89
151
|
continue;
|
|
90
152
|
let i, u;
|
|
91
153
|
if (typeof o == "function" || "parse" in o)
|
|
92
|
-
i =
|
|
154
|
+
i = r, u = typeof o == "function" ? o : o.parse.bind(o);
|
|
93
155
|
else {
|
|
94
156
|
const { type: s } = o;
|
|
95
|
-
i = o.from ||
|
|
157
|
+
i = o.from || r, u = typeof s == "function" ? s : s.parse.bind(s);
|
|
96
158
|
}
|
|
97
159
|
try {
|
|
98
160
|
const s = u(e(i));
|
|
99
|
-
s !== void 0 && (r
|
|
161
|
+
s !== void 0 && (n[r] = s);
|
|
100
162
|
} catch (s) {
|
|
101
|
-
throw
|
|
163
|
+
throw y(P, `Unable to parse field "${r}"`, s);
|
|
102
164
|
}
|
|
103
165
|
}
|
|
104
|
-
return
|
|
166
|
+
return n;
|
|
105
167
|
}
|
|
106
|
-
function
|
|
168
|
+
function U(t) {
|
|
107
169
|
let e = t;
|
|
108
170
|
if (typeof e == "string" && (e = JSON.parse(e)), typeof e != "object" || e === null || Array.isArray(e))
|
|
109
171
|
throw h();
|
|
110
172
|
return e;
|
|
111
173
|
}
|
|
112
174
|
function p(t, e) {
|
|
113
|
-
return new w((
|
|
114
|
-
const
|
|
115
|
-
return
|
|
175
|
+
return new w((n) => {
|
|
176
|
+
const r = U(n);
|
|
177
|
+
return D(t, (o) => r[o]);
|
|
116
178
|
}, !1, e);
|
|
117
179
|
}
|
|
118
|
-
const f =
|
|
180
|
+
const f = d((t) => {
|
|
119
181
|
if (typeof t == "number")
|
|
120
182
|
return t;
|
|
121
183
|
if (typeof t == "string") {
|
|
@@ -125,55 +187,55 @@ const f = l((t) => {
|
|
|
125
187
|
}
|
|
126
188
|
throw h();
|
|
127
189
|
}, "number");
|
|
128
|
-
function
|
|
190
|
+
function T(t) {
|
|
129
191
|
return /^#[\da-f]{6}$/i.test(t);
|
|
130
192
|
}
|
|
131
|
-
function
|
|
193
|
+
function q(t) {
|
|
132
194
|
return /^#[\da-f]{3}$/i.test(t);
|
|
133
195
|
}
|
|
134
|
-
function
|
|
196
|
+
function x(t) {
|
|
135
197
|
const e = t.replace(/\s/g, "").toLowerCase();
|
|
136
|
-
if (
|
|
198
|
+
if (T(e))
|
|
137
199
|
return e;
|
|
138
|
-
if (
|
|
139
|
-
let
|
|
200
|
+
if (q(e)) {
|
|
201
|
+
let r = "#";
|
|
140
202
|
for (let o = 0; o < 3; o += 1)
|
|
141
|
-
|
|
142
|
-
return
|
|
203
|
+
r += e[1 + o].repeat(2);
|
|
204
|
+
return r;
|
|
143
205
|
}
|
|
144
|
-
const
|
|
145
|
-
if (!
|
|
206
|
+
const n = e.match(/^rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)$/) || e.match(/^rgba\((\d{1,3}),(\d{1,3}),(\d{1,3}),\d{1,3}\)$/);
|
|
207
|
+
if (!n)
|
|
146
208
|
throw new Error(`Value "${t}" does not satisfy any of known RGB formats.`);
|
|
147
|
-
return
|
|
209
|
+
return n.slice(1).reduce((r, o) => {
|
|
148
210
|
const i = parseInt(o, 10).toString(16);
|
|
149
|
-
return
|
|
211
|
+
return r + (i.length === 1 ? "0" : "") + i;
|
|
150
212
|
}, "#");
|
|
151
213
|
}
|
|
152
|
-
const
|
|
214
|
+
const a = d((t) => {
|
|
153
215
|
if (typeof t == "string" || typeof t == "number")
|
|
154
216
|
return t.toString();
|
|
155
217
|
throw h();
|
|
156
|
-
}, "string"),
|
|
157
|
-
button_id: (t) => t == null ? void 0 :
|
|
218
|
+
}, "string"), I = d((t) => x(a().parse(t)), "rgb"), O = p({
|
|
219
|
+
button_id: (t) => t == null ? void 0 : a().parse(t)
|
|
158
220
|
});
|
|
159
221
|
p({
|
|
160
|
-
req_id:
|
|
161
|
-
data: (t) => t === null ? t :
|
|
222
|
+
req_id: a(),
|
|
223
|
+
data: (t) => t === null ? t : a().optional().parse(t)
|
|
162
224
|
}), p({
|
|
163
|
-
req_id:
|
|
225
|
+
req_id: a(),
|
|
164
226
|
result: (t) => t,
|
|
165
|
-
error:
|
|
227
|
+
error: a().optional()
|
|
166
228
|
}), p({
|
|
167
|
-
slug:
|
|
168
|
-
status:
|
|
229
|
+
slug: a(),
|
|
230
|
+
status: a()
|
|
169
231
|
}), p({
|
|
170
|
-
status:
|
|
232
|
+
status: a()
|
|
171
233
|
}), p({
|
|
172
|
-
data:
|
|
234
|
+
data: a().optional()
|
|
173
235
|
}), p({
|
|
174
236
|
theme_params: (t) => {
|
|
175
|
-
const e =
|
|
176
|
-
return Object.entries(
|
|
237
|
+
const e = I().optional();
|
|
238
|
+
return Object.entries(U(t)).reduce((n, [r, o]) => (n[r] = e.parse(o), n), {});
|
|
177
239
|
}
|
|
178
240
|
}), p({
|
|
179
241
|
height: f(),
|
|
@@ -181,30 +243,30 @@ p({
|
|
|
181
243
|
is_state_stable: c(),
|
|
182
244
|
is_expanded: c()
|
|
183
245
|
}), p({
|
|
184
|
-
status:
|
|
246
|
+
status: a()
|
|
185
247
|
});
|
|
186
|
-
const
|
|
187
|
-
function
|
|
188
|
-
return new w((
|
|
189
|
-
if (typeof
|
|
248
|
+
const A = d((t) => t instanceof Date ? t : new Date(f().parse(t) * 1e3), "Date");
|
|
249
|
+
function C(t, e) {
|
|
250
|
+
return new w((n) => {
|
|
251
|
+
if (typeof n != "string" && !(n instanceof URLSearchParams))
|
|
190
252
|
throw h();
|
|
191
|
-
const
|
|
192
|
-
return
|
|
193
|
-
const i =
|
|
253
|
+
const r = typeof n == "string" ? new URLSearchParams(n) : n;
|
|
254
|
+
return D(t, (o) => {
|
|
255
|
+
const i = r.get(o);
|
|
194
256
|
return i === null ? void 0 : i;
|
|
195
257
|
});
|
|
196
258
|
}, !1, e);
|
|
197
259
|
}
|
|
198
|
-
const
|
|
260
|
+
const L = p({
|
|
199
261
|
id: f(),
|
|
200
|
-
type:
|
|
201
|
-
title:
|
|
262
|
+
type: a(),
|
|
263
|
+
title: a(),
|
|
202
264
|
photoUrl: {
|
|
203
|
-
type:
|
|
265
|
+
type: a().optional(),
|
|
204
266
|
from: "photo_url"
|
|
205
267
|
},
|
|
206
|
-
username:
|
|
207
|
-
}, "Chat").optional(),
|
|
268
|
+
username: a().optional()
|
|
269
|
+
}, "Chat").optional(), v = p({
|
|
208
270
|
addedToAttachmentMenu: {
|
|
209
271
|
type: c().optional(),
|
|
210
272
|
from: "added_to_attachment_menu"
|
|
@@ -214,7 +276,7 @@ const O = p({
|
|
|
214
276
|
from: "allows_write_to_pm"
|
|
215
277
|
},
|
|
216
278
|
firstName: {
|
|
217
|
-
type:
|
|
279
|
+
type: a(),
|
|
218
280
|
from: "first_name"
|
|
219
281
|
},
|
|
220
282
|
id: f(),
|
|
@@ -227,56 +289,58 @@ const O = p({
|
|
|
227
289
|
from: "is_premium"
|
|
228
290
|
},
|
|
229
291
|
languageCode: {
|
|
230
|
-
type:
|
|
292
|
+
type: a().optional(),
|
|
231
293
|
from: "language_code"
|
|
232
294
|
},
|
|
233
295
|
lastName: {
|
|
234
|
-
type:
|
|
296
|
+
type: a().optional(),
|
|
235
297
|
from: "last_name"
|
|
236
298
|
},
|
|
237
299
|
photoUrl: {
|
|
238
|
-
type:
|
|
300
|
+
type: a().optional(),
|
|
239
301
|
from: "photo_url"
|
|
240
302
|
},
|
|
241
|
-
username:
|
|
303
|
+
username: a().optional()
|
|
242
304
|
}, "User").optional();
|
|
243
|
-
function
|
|
244
|
-
return
|
|
305
|
+
function j() {
|
|
306
|
+
return C({
|
|
245
307
|
authDate: {
|
|
246
|
-
type:
|
|
308
|
+
type: A(),
|
|
247
309
|
from: "auth_date"
|
|
248
310
|
},
|
|
249
311
|
canSendAfter: {
|
|
250
312
|
type: f().optional(),
|
|
251
313
|
from: "can_send_after"
|
|
252
314
|
},
|
|
253
|
-
chat:
|
|
315
|
+
chat: L,
|
|
254
316
|
chatInstance: {
|
|
255
|
-
type:
|
|
317
|
+
type: a().optional(),
|
|
256
318
|
from: "chat_instance"
|
|
257
319
|
},
|
|
258
320
|
chatType: {
|
|
259
|
-
type:
|
|
321
|
+
type: a().optional(),
|
|
260
322
|
from: "chat_type"
|
|
261
323
|
},
|
|
262
|
-
hash:
|
|
324
|
+
hash: a(),
|
|
263
325
|
queryId: {
|
|
264
|
-
type:
|
|
326
|
+
type: a().optional(),
|
|
265
327
|
from: "query_id"
|
|
266
328
|
},
|
|
267
|
-
receiver:
|
|
329
|
+
receiver: v,
|
|
268
330
|
startParam: {
|
|
269
|
-
type:
|
|
331
|
+
type: a().optional(),
|
|
270
332
|
from: "start_param"
|
|
271
333
|
},
|
|
272
|
-
user:
|
|
334
|
+
user: v
|
|
273
335
|
}, "InitData");
|
|
274
336
|
}
|
|
275
|
-
function
|
|
276
|
-
return
|
|
337
|
+
function K(t) {
|
|
338
|
+
return j().parse(t);
|
|
277
339
|
}
|
|
278
340
|
export {
|
|
279
|
-
|
|
280
|
-
|
|
341
|
+
K as parse,
|
|
342
|
+
B as sign,
|
|
343
|
+
E as signData,
|
|
344
|
+
M as validate
|
|
281
345
|
};
|
|
282
346
|
//# sourceMappingURL=index.js.map
|