@tma.js/init-data-node 1.1.12 → 1.1.14

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 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;
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);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 x(){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 $(){return S({authDate:{type:P(),from:"auth_date"},canSendAfter:{type:f().optional(),from:"can_send_after"},chat:x().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 j(e){return $().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=j;exports.validate=A;
package/dist/index.mjs CHANGED
@@ -1,9 +1,9 @@
1
1
  import { createHmac as N } from "node:crypto";
2
2
  import { URLSearchParams as E } from "node:url";
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);
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
4
  class l extends Error {
5
5
  constructor(t, { cause: r, 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;
6
+ super(`Unable to parse value${o ? ` as ${o}` : ""}`, { cause: r }), U(this, "type"), this.value = t, Object.setPrototypeOf(this, l.prototype), this.type = o;
7
7
  }
8
8
  }
9
9
  class m extends Error {
@@ -41,7 +41,7 @@ function P(e, t) {
41
41
  function f() {
42
42
  return new TypeError("Value has unexpected type");
43
43
  }
44
- function I(e) {
44
+ function x(e) {
45
45
  let t = e;
46
46
  if (typeof t == "string" && (t = JSON.parse(t)), typeof t != "object" || t === null || Array.isArray(t))
47
47
  throw f();
@@ -65,7 +65,7 @@ class w {
65
65
  }
66
66
  function _(e, t) {
67
67
  return new w((r) => {
68
- const o = I(r);
68
+ const o = x(r);
69
69
  return P(e, (a) => o[a]);
70
70
  }, !1, t);
71
71
  }
@@ -95,7 +95,7 @@ const n = y((e) => {
95
95
  }
96
96
  throw f();
97
97
  }, "number");
98
- function T() {
98
+ function I() {
99
99
  return _({
100
100
  id: c(),
101
101
  type: n(),
@@ -167,7 +167,7 @@ function $() {
167
167
  type: c().optional(),
168
168
  from: "can_send_after"
169
169
  },
170
- chat: T().optional(),
170
+ chat: I().optional(),
171
171
  chatInstance: {
172
172
  type: n().optional(),
173
173
  from: "chat_instance"
@@ -189,7 +189,7 @@ function $() {
189
189
  user: D().optional()
190
190
  }, "InitData");
191
191
  }
192
- function A(e) {
192
+ function R(e) {
193
193
  return $().parse(e);
194
194
  }
195
195
  S({
@@ -248,6 +248,6 @@ function L(e, t, r = {}) {
248
248
  throw new Error("Signature is invalid");
249
249
  }
250
250
  export {
251
- A as parse,
251
+ R as parse,
252
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.12",
3
+ "version": "1.1.14",
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.5.0"
49
+ "@tma.js/sdk": "^1.5.2"
50
50
  },
51
51
  "scripts": {
52
52
  "test": "vitest --run",