@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/index.js CHANGED
@@ -1,50 +1,112 @@
1
1
  import { createHmac as b } from "node:crypto";
2
- import { URLSearchParams as D } from "node:url";
3
- function H(t, e, r = {}) {
4
- const a = typeof t == "string" ? new D(t) : t;
5
- let o = /* @__PURE__ */ new Date(0), i = "";
6
- const u = [];
7
- if (a.forEach((d, m) => {
8
- if (m === "hash") {
9
- i = d;
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 (m === "auth_date") {
13
- const g = parseInt(d, 10);
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
- o = new Date(g * 1e3);
81
+ r = new Date(g * 1e3);
17
82
  }
18
- u.push(`${m}=${d}`);
19
- }), i.length === 0)
83
+ i.push(`${l}=${s}`);
84
+ }), !o)
20
85
  throw new Error('"hash" is empty or not found');
21
- if (o.getTime() === 0)
86
+ if (!r)
22
87
  throw new Error('"auth_date" is empty or not found');
23
- const { expiresIn: s = 86400 } = r;
24
- if (s > 0 && o.getTime() + s * 1e3 < (/* @__PURE__ */ new Date()).getTime())
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 (u.sort(), b(
27
- "sha256",
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 y extends Error {
34
- constructor(e, r, a) {
35
- super(r, { cause: a }), this.type = e, Object.setPrototypeOf(this, y.prototype);
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 _(t, e, r) {
39
- return new y(t, e, r);
100
+ function y(t, e, n) {
101
+ return new _(t, e, n);
40
102
  }
41
- const N = "ERR_UNEXPECTED_TYPE", P = "ERR_PARSE";
103
+ const R = "ERR_UNEXPECTED_TYPE", P = "ERR_PARSE";
42
104
  function h() {
43
- return _(N, "Value has unexpected type");
105
+ return y(R, "Value has unexpected type");
44
106
  }
45
107
  class w {
46
- constructor(e, r, a) {
47
- this.parser = e, this.isOptional = r, this.type = a;
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 (r) {
60
- throw _(
121
+ } catch (n) {
122
+ throw y(
61
123
  P,
62
124
  `Unable to parse value${this.type ? ` as ${this.type}` : ""}`,
63
- r
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 l(t, e) {
133
+ function d(t, e) {
72
134
  return () => new w(t, !1, e);
73
135
  }
74
- const c = l((t) => {
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 S(t, e) {
85
- const r = {};
86
- for (const a in t) {
87
- const o = t[a];
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 = a, u = typeof o == "function" ? o : o.parse.bind(o);
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 || a, u = typeof s == "function" ? s : s.parse.bind(s);
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[a] = s);
161
+ s !== void 0 && (n[r] = s);
100
162
  } catch (s) {
101
- throw _(P, `Unable to parse field "${a}"`, s);
163
+ throw y(P, `Unable to parse field "${r}"`, s);
102
164
  }
103
165
  }
104
- return r;
166
+ return n;
105
167
  }
106
- function v(t) {
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((r) => {
114
- const a = v(r);
115
- return S(t, (o) => a[o]);
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 = l((t) => {
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 $(t) {
190
+ function T(t) {
129
191
  return /^#[\da-f]{6}$/i.test(t);
130
192
  }
131
- function R(t) {
193
+ function q(t) {
132
194
  return /^#[\da-f]{3}$/i.test(t);
133
195
  }
134
- function U(t) {
196
+ function x(t) {
135
197
  const e = t.replace(/\s/g, "").toLowerCase();
136
- if ($(e))
198
+ if (T(e))
137
199
  return e;
138
- if (R(e)) {
139
- let a = "#";
200
+ if (q(e)) {
201
+ let r = "#";
140
202
  for (let o = 0; o < 3; o += 1)
141
- a += e[1 + o].repeat(2);
142
- return a;
203
+ r += e[1 + o].repeat(2);
204
+ return r;
143
205
  }
144
- const r = 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}\)$/);
145
- if (!r)
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 r.slice(1).reduce((a, o) => {
209
+ return n.slice(1).reduce((r, o) => {
148
210
  const i = parseInt(o, 10).toString(16);
149
- return a + (i.length === 1 ? "0" : "") + i;
211
+ return r + (i.length === 1 ? "0" : "") + i;
150
212
  }, "#");
151
213
  }
152
- const n = l((t) => {
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"), x = l((t) => U(n().parse(t)), "rgb"), T = p({
157
- button_id: (t) => t == null ? void 0 : n().parse(t)
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: n(),
161
- data: (t) => t === null ? t : n().optional().parse(t)
222
+ req_id: a(),
223
+ data: (t) => t === null ? t : a().optional().parse(t)
162
224
  }), p({
163
- req_id: n(),
225
+ req_id: a(),
164
226
  result: (t) => t,
165
- error: n().optional()
227
+ error: a().optional()
166
228
  }), p({
167
- slug: n(),
168
- status: n()
229
+ slug: a(),
230
+ status: a()
169
231
  }), p({
170
- status: n()
232
+ status: a()
171
233
  }), p({
172
- data: n().optional()
234
+ data: a().optional()
173
235
  }), p({
174
236
  theme_params: (t) => {
175
- const e = x().optional();
176
- return Object.entries(v(t)).reduce((r, [a, o]) => (r[a] = e.parse(o), r), {});
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: n()
246
+ status: a()
185
247
  });
186
- const q = l((t) => t instanceof Date ? t : new Date(f().parse(t) * 1e3), "Date");
187
- function I(t, e) {
188
- return new w((r) => {
189
- if (typeof r != "string" && !(r instanceof URLSearchParams))
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 a = typeof r == "string" ? new URLSearchParams(r) : r;
192
- return S(t, (o) => {
193
- const i = a.get(o);
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 O = p({
260
+ const L = p({
199
261
  id: f(),
200
- type: n(),
201
- title: n(),
262
+ type: a(),
263
+ title: a(),
202
264
  photoUrl: {
203
- type: n().optional(),
265
+ type: a().optional(),
204
266
  from: "photo_url"
205
267
  },
206
- username: n().optional()
207
- }, "Chat").optional(), E = p({
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: n(),
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: n().optional(),
292
+ type: a().optional(),
231
293
  from: "language_code"
232
294
  },
233
295
  lastName: {
234
- type: n().optional(),
296
+ type: a().optional(),
235
297
  from: "last_name"
236
298
  },
237
299
  photoUrl: {
238
- type: n().optional(),
300
+ type: a().optional(),
239
301
  from: "photo_url"
240
302
  },
241
- username: n().optional()
303
+ username: a().optional()
242
304
  }, "User").optional();
243
- function A() {
244
- return I({
305
+ function j() {
306
+ return C({
245
307
  authDate: {
246
- type: q(),
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: O,
315
+ chat: L,
254
316
  chatInstance: {
255
- type: n().optional(),
317
+ type: a().optional(),
256
318
  from: "chat_instance"
257
319
  },
258
320
  chatType: {
259
- type: n().optional(),
321
+ type: a().optional(),
260
322
  from: "chat_type"
261
323
  },
262
- hash: n(),
324
+ hash: a(),
263
325
  queryId: {
264
- type: n().optional(),
326
+ type: a().optional(),
265
327
  from: "query_id"
266
328
  },
267
- receiver: E,
329
+ receiver: v,
268
330
  startParam: {
269
- type: n().optional(),
331
+ type: a().optional(),
270
332
  from: "start_param"
271
333
  },
272
- user: E
334
+ user: v
273
335
  }, "InitData");
274
336
  }
275
- function W(t) {
276
- return A().parse(t);
337
+ function K(t) {
338
+ return j().parse(t);
277
339
  }
278
340
  export {
279
- W as parse,
280
- H as validate
341
+ K as parse,
342
+ B as sign,
343
+ E as signData,
344
+ M as validate
281
345
  };
282
346
  //# sourceMappingURL=index.js.map