@tma.js/init-data-node 1.1.9 → 1.1.11

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.cjs CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const N=require("node:crypto"),O=require("node:url");var T=Object.defineProperty,E=(e,t,r)=>t in e?T(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,I=(e,t,r)=>(E(e,typeof t!="symbol"?t+"":t,r),r);function c(){return new TypeError("Value has unexpected type")}class m extends Error{constructor(t,{cause:r,type:o}={}){super(`Unable to parse value${o?` as ${o}`:""}`,{cause:r}),I(this,"type"),this.value=t,Object.setPrototypeOf(this,m.prototype),this.type=o}}class w{constructor(t,r,o){this.parser=t,this.isOptional=r,this.type=o}parse(t){if(!(this.isOptional&&t===void 0))try{return this.parser(t)}catch(r){throw new m(t,{type:this.type,cause:r})}}optional(){return this.isOptional=!0,this}}function y(e,t){return()=>new w(e,!1,t)}class l extends Error{constructor(t,{cause:r,type:o}={}){super(`Unable to parse field "${t}"${o?` as ${o}`:""}`,{cause:r}),Object.setPrototypeOf(this,l.prototype)}}function v(e,t){const r={};for(const o in e){const a=e[o];if(!a)continue;let s,p;if(typeof a=="function"||"parse"in a)s=o,p=typeof a=="function"?a:a.parse.bind(a);else{const{type:i}=a;s=a.from||o,p=typeof i=="function"?i:i.parse.bind(i)}let u;const b=t(s);try{u=p(b)}catch(i){throw i instanceof m?new l(s,{type:i.type,cause:i}):new l(s,{cause:i})}u!==void 0&&(r[o]=u)}return r}const h=y(e=>{if(typeof e=="boolean")return e;const t=String(e);if(t==="1"||t==="true")return!0;if(t==="0"||t==="false")return!1;throw c()},"boolean"),f=y(e=>{if(typeof e=="number")return e;if(typeof e=="string"){const t=Number(e);if(!Number.isNaN(t))return t}throw c()},"number"),P=y(e=>e instanceof Date?e:new Date(f().parse(e)*1e3),"Date");function U(e){let t=e;if(typeof t=="string"&&(t=JSON.parse(t)),typeof t!="object"||t===null||Array.isArray(t))throw c();return t}function _(e,t){return new w(r=>{const o=U(r);return v(e,a=>o[a])},!1,t)}const n=y(e=>{if(typeof e=="string"||typeof e=="number")return e.toString();throw c()},"string");function S(e,t){return new w(r=>{if(typeof r!="string"&&!(r instanceof URLSearchParams))throw c();const o=typeof r=="string"?new URLSearchParams(r):r;return v(e,a=>{const s=o.get(a);return s===null?void 0:s})},!1,t)}function $(){return _({id:f(),type:n(),title:n(),photoUrl:{type:n().optional(),from:"photo_url"},username:n().optional()},"Chat")}function D(){return _({addedToAttachmentMenu:{type:h().optional(),from:"added_to_attachment_menu"},allowsWriteToPm:{type:h().optional(),from:"allows_write_to_pm"},firstName:{type:n(),from:"first_name"},id:f(),isBot:{type:h().optional(),from:"is_bot"},isPremium:{type:h().optional(),from:"is_premium"},languageCode:{type:n().optional(),from:"language_code"},lastName:{type:n().optional(),from:"last_name"},photoUrl:{type:n().optional(),from:"photo_url"},username:n().optional()},"User")}function j(){return S({authDate:{type:P(),from:"auth_date"},canSendAfter:{type:f().optional(),from:"can_send_after"},chat:$().optional(),chatInstance:{type:n().optional(),from:"chat_instance"},chatType:{type:n().optional(),from:"chat_type"},hash:n(),queryId:{type:n().optional(),from:"query_id"},receiver:D().optional(),startParam:{type:n().optional(),from:"start_param"},user:D().optional()},"InitData")}function x(e){return j().parse(e)}S({contact:_({userId:{type:f(),from:"user_id"},phoneNumber:{type:n(),from:"phone_number"},firstName:{type:n(),from:"first_name"},lastName:{type:n().optional(),from:"last_name"}}),authDate:{type:P(),from:"auth_date"},hash:n()});function A(e,t,r={}){const o=typeof e=="string"?new O.URLSearchParams(e):e;let a=new Date(0),s="";const p=[];if(o.forEach((i,d)=>{if(d==="hash"){s=i;return}if(d==="auth_date"){const g=parseInt(i,10);if(Number.isNaN(g))throw new TypeError('"auth_date" should present integer');a=new Date(g*1e3)}p.push(`${d}=${i}`)}),s.length===0)throw new Error('"hash" is empty or not found');if(a.getTime()===0)throw new Error('"auth_date" is empty or not found');const{expiresIn:u=86400}=r;if(u>0&&a.getTime()+u*1e3<new Date().getTime())throw new Error("Init data expired");if(p.sort(),N.createHmac("sha256",N.createHmac("sha256","WebAppData").update(t).digest()).update(p.join(`
2
- `)).digest().toString("hex")!==s)throw new Error("Signature is invalid")}exports.parse=x;exports.validate=A;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const N=require("node:crypto"),O=require("node:url");var E=Object.defineProperty,T=(e,t,r)=>t in e?E(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,I=(e,t,r)=>(T(e,typeof t!="symbol"?t+"":t,r),r);class m extends Error{constructor(t,{cause:r,type:o}={}){super(`Unable to parse value${o?` as ${o}`:""}`,{cause:r}),I(this,"type"),this.value=t,Object.setPrototypeOf(this,m.prototype),this.type=o}}class l extends Error{constructor(t,{cause:r,type:o}={}){super(`Unable to parse field "${t}"${o?` as ${o}`:""}`,{cause:r}),Object.setPrototypeOf(this,l.prototype)}}function D(e,t){const r={};for(const o in e){const a=e[o];if(!a)continue;let s,p;if(typeof a=="function"||"parse"in a)s=o,p=typeof a=="function"?a:a.parse.bind(a);else{const{type:i}=a;s=a.from||o,p=typeof i=="function"?i:i.parse.bind(i)}let u;const b=t(s);try{u=p(b)}catch(i){throw i instanceof m?new l(s,{type:i.type,cause:i}):new l(s,{cause:i})}u!==void 0&&(r[o]=u)}return r}function c(){return new TypeError("Value has unexpected type")}function U(e){let t=e;if(typeof t=="string"&&(t=JSON.parse(t)),typeof t!="object"||t===null||Array.isArray(t))throw c();return t}class w{constructor(t,r,o){this.parser=t,this.isOptional=r,this.type=o}parse(t){if(!(this.isOptional&&t===void 0))try{return this.parser(t)}catch(r){throw new m(t,{type:this.type,cause:r})}}optional(){return this.isOptional=!0,this}}function _(e,t){return new w(r=>{const o=U(r);return D(e,a=>o[a])},!1,t)}function y(e,t){return()=>new w(e,!1,t)}const n=y(e=>{if(typeof e=="string"||typeof e=="number")return e.toString();throw c()},"string"),h=y(e=>{if(typeof e=="boolean")return e;const t=String(e);if(t==="1"||t==="true")return!0;if(t==="0"||t==="false")return!1;throw c()},"boolean"),f=y(e=>{if(typeof e=="number")return e;if(typeof e=="string"){const t=Number(e);if(!Number.isNaN(t))return t}throw c()},"number");function j(){return _({id:f(),type:n(),title:n(),photoUrl:{type:n().optional(),from:"photo_url"},username:n().optional()},"Chat")}function v(){return _({addedToAttachmentMenu:{type:h().optional(),from:"added_to_attachment_menu"},allowsWriteToPm:{type:h().optional(),from:"allows_write_to_pm"},firstName:{type:n(),from:"first_name"},id:f(),isBot:{type:h().optional(),from:"is_bot"},isPremium:{type:h().optional(),from:"is_premium"},languageCode:{type:n().optional(),from:"language_code"},lastName:{type:n().optional(),from:"last_name"},photoUrl:{type:n().optional(),from:"photo_url"},username:n().optional()},"User")}const P=y(e=>e instanceof Date?e:new Date(f().parse(e)*1e3),"Date");function S(e,t){return new w(r=>{if(typeof r!="string"&&!(r instanceof URLSearchParams))throw c();const o=typeof r=="string"?new URLSearchParams(r):r;return D(e,a=>{const s=o.get(a);return s===null?void 0:s})},!1,t)}function x(){return S({authDate:{type:P(),from:"auth_date"},canSendAfter:{type:f().optional(),from:"can_send_after"},chat:j().optional(),chatInstance:{type:n().optional(),from:"chat_instance"},chatType:{type:n().optional(),from:"chat_type"},hash:n(),queryId:{type:n().optional(),from:"query_id"},receiver:v().optional(),startParam:{type:n().optional(),from:"start_param"},user:v().optional()},"InitData")}function $(e){return x().parse(e)}S({contact:_({userId:{type:f(),from:"user_id"},phoneNumber:{type:n(),from:"phone_number"},firstName:{type:n(),from:"first_name"},lastName:{type:n().optional(),from:"last_name"}}),authDate:{type:P(),from:"auth_date"},hash:n()});function A(e,t,r={}){const o=typeof e=="string"?new O.URLSearchParams(e):e;let a=new Date(0),s="";const p=[];if(o.forEach((i,d)=>{if(d==="hash"){s=i;return}if(d==="auth_date"){const g=parseInt(i,10);if(Number.isNaN(g))throw new TypeError('"auth_date" should present integer');a=new Date(g*1e3)}p.push(`${d}=${i}`)}),s.length===0)throw new Error('"hash" is empty or not found');if(a.getTime()===0)throw new Error('"auth_date" is empty or not found');const{expiresIn:u=86400}=r;if(u>0&&a.getTime()+u*1e3<new Date().getTime())throw new Error("Init data expired");if(p.sort(),N.createHmac("sha256",N.createHmac("sha256","WebAppData").update(t).digest()).update(p.join(`
2
+ `)).digest().toString("hex")!==s)throw new Error("Signature is invalid")}exports.parse=$;exports.validate=A;
package/dist/index.mjs CHANGED
@@ -1,33 +1,11 @@
1
1
  import { createHmac as N } from "node:crypto";
2
2
  import { URLSearchParams as E } from "node:url";
3
- var O = Object.defineProperty, T = (e, t, r) => t in e ? O(e, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : e[t] = r, U = (e, t, r) => (T(e, typeof t != "symbol" ? t + "" : t, r), r);
4
- function f() {
5
- return new TypeError("Value has unexpected type");
6
- }
3
+ var O = Object.defineProperty, U = (e, t, r) => t in e ? O(e, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : e[t] = r, x = (e, t, r) => (U(e, typeof t != "symbol" ? t + "" : t, r), r);
7
4
  class l extends Error {
8
5
  constructor(t, { cause: r, type: o } = {}) {
9
- super(`Unable to parse value${o ? ` as ${o}` : ""}`, { cause: r }), U(this, "type"), this.value = t, Object.setPrototypeOf(this, l.prototype), this.type = o;
6
+ super(`Unable to parse value${o ? ` as ${o}` : ""}`, { cause: r }), x(this, "type"), this.value = t, Object.setPrototypeOf(this, l.prototype), this.type = o;
10
7
  }
11
8
  }
12
- class w {
13
- constructor(t, r, o) {
14
- this.parser = t, this.isOptional = r, this.type = o;
15
- }
16
- parse(t) {
17
- if (!(this.isOptional && t === void 0))
18
- try {
19
- return this.parser(t);
20
- } catch (r) {
21
- throw new l(t, { type: this.type, cause: r });
22
- }
23
- }
24
- optional() {
25
- return this.isOptional = !0, this;
26
- }
27
- }
28
- function y(e, t) {
29
- return () => new w(e, !1, t);
30
- }
31
9
  class m extends Error {
32
10
  constructor(t, { cause: r, type: o } = {}) {
33
11
  super(`Unable to parse field "${t}"${o ? ` as ${o}` : ""}`, { cause: r }), Object.setPrototypeOf(this, m.prototype);
@@ -60,7 +38,45 @@ function P(e, t) {
60
38
  }
61
39
  return r;
62
40
  }
63
- const h = y((e) => {
41
+ function f() {
42
+ return new TypeError("Value has unexpected type");
43
+ }
44
+ function I(e) {
45
+ let t = e;
46
+ if (typeof t == "string" && (t = JSON.parse(t)), typeof t != "object" || t === null || Array.isArray(t))
47
+ throw f();
48
+ return t;
49
+ }
50
+ class w {
51
+ constructor(t, r, o) {
52
+ this.parser = t, this.isOptional = r, this.type = o;
53
+ }
54
+ parse(t) {
55
+ if (!(this.isOptional && t === void 0))
56
+ try {
57
+ return this.parser(t);
58
+ } catch (r) {
59
+ throw new l(t, { type: this.type, cause: r });
60
+ }
61
+ }
62
+ optional() {
63
+ return this.isOptional = !0, this;
64
+ }
65
+ }
66
+ function _(e, t) {
67
+ return new w((r) => {
68
+ const o = I(r);
69
+ return P(e, (a) => o[a]);
70
+ }, !1, t);
71
+ }
72
+ function y(e, t) {
73
+ return () => new w(e, !1, t);
74
+ }
75
+ const n = y((e) => {
76
+ if (typeof e == "string" || typeof e == "number")
77
+ return e.toString();
78
+ throw f();
79
+ }, "string"), h = y((e) => {
64
80
  if (typeof e == "boolean")
65
81
  return e;
66
82
  const t = String(e);
@@ -78,36 +94,8 @@ const h = y((e) => {
78
94
  return t;
79
95
  }
80
96
  throw f();
81
- }, "number"), v = y((e) => e instanceof Date ? e : new Date(c().parse(e) * 1e3), "Date");
82
- function I(e) {
83
- let t = e;
84
- if (typeof t == "string" && (t = JSON.parse(t)), typeof t != "object" || t === null || Array.isArray(t))
85
- throw f();
86
- return t;
87
- }
88
- function _(e, t) {
89
- return new w((r) => {
90
- const o = I(r);
91
- return P(e, (a) => o[a]);
92
- }, !1, t);
93
- }
94
- const n = y((e) => {
95
- if (typeof e == "string" || typeof e == "number")
96
- return e.toString();
97
- throw f();
98
- }, "string");
99
- function S(e, t) {
100
- return new w((r) => {
101
- if (typeof r != "string" && !(r instanceof URLSearchParams))
102
- throw f();
103
- const o = typeof r == "string" ? new URLSearchParams(r) : r;
104
- return P(e, (a) => {
105
- const s = o.get(a);
106
- return s === null ? void 0 : s;
107
- });
108
- }, !1, t);
109
- }
110
- function $() {
97
+ }, "number");
98
+ function T() {
111
99
  return _({
112
100
  id: c(),
113
101
  type: n(),
@@ -157,7 +145,19 @@ function D() {
157
145
  username: n().optional()
158
146
  }, "User");
159
147
  }
160
- function x() {
148
+ const v = y((e) => e instanceof Date ? e : new Date(c().parse(e) * 1e3), "Date");
149
+ function S(e, t) {
150
+ return new w((r) => {
151
+ if (typeof r != "string" && !(r instanceof URLSearchParams))
152
+ throw f();
153
+ const o = typeof r == "string" ? new URLSearchParams(r) : r;
154
+ return P(e, (a) => {
155
+ const s = o.get(a);
156
+ return s === null ? void 0 : s;
157
+ });
158
+ }, !1, t);
159
+ }
160
+ function $() {
161
161
  return S({
162
162
  authDate: {
163
163
  type: v(),
@@ -167,7 +167,7 @@ function x() {
167
167
  type: c().optional(),
168
168
  from: "can_send_after"
169
169
  },
170
- chat: $().optional(),
170
+ chat: T().optional(),
171
171
  chatInstance: {
172
172
  type: n().optional(),
173
173
  from: "chat_instance"
@@ -189,8 +189,8 @@ function x() {
189
189
  user: D().optional()
190
190
  }, "InitData");
191
191
  }
192
- function L(e) {
193
- return x().parse(e);
192
+ function A(e) {
193
+ return $().parse(e);
194
194
  }
195
195
  S({
196
196
  contact: _({
@@ -217,7 +217,7 @@ S({
217
217
  },
218
218
  hash: n()
219
219
  });
220
- function R(e, t, r = {}) {
220
+ function L(e, t, r = {}) {
221
221
  const o = typeof e == "string" ? new E(e) : e;
222
222
  let a = /* @__PURE__ */ new Date(0), s = "";
223
223
  const p = [];
@@ -248,6 +248,6 @@ function R(e, t, r = {}) {
248
248
  throw new Error("Signature is invalid");
249
249
  }
250
250
  export {
251
- L as parse,
252
- R as validate
251
+ A as parse,
252
+ L as validate
253
253
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tma.js/init-data-node",
3
- "version": "1.1.9",
3
+ "version": "1.1.11",
4
4
  "description": "TypeScript Node library to operate with Telegram init data.",
5
5
  "author": "Vladislav Kibenko <wolfram.deus@gmail.com>",
6
6
  "homepage": "https://github.com/Telegram-Mini-Apps/tma.js#readme",
@@ -46,7 +46,7 @@
46
46
  "access": "public"
47
47
  },
48
48
  "dependencies": {
49
- "@tma.js/sdk": "^1.4.8"
49
+ "@tma.js/sdk": "^1.5.0"
50
50
  },
51
51
  "scripts": {
52
52
  "test": "vitest --run",