@usertour/helpers 0.0.9 → 0.0.10

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,6 +1,3 @@
1
- import {
2
- defaultStep
3
- } from "./chunk-FW54TSA3.js";
4
1
  import {
5
2
  deepClone
6
3
  } from "./chunk-2AEGAICC.js";
@@ -18,6 +15,9 @@ import {
18
15
  import {
19
16
  isUrl
20
17
  } from "./chunk-ZNFXGN3M.js";
18
+ import {
19
+ defaultStep
20
+ } from "./chunk-FW54TSA3.js";
21
21
  import {
22
22
  getAuthToken,
23
23
  removeAuthToken,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@usertour/helpers",
3
- "version": "0.0.9",
3
+ "version": "0.0.10",
4
4
  "type": "module",
5
5
  "description": "Utility functions and helpers shared across the UserTour project",
6
6
  "homepage": "https://www.usertour.io",
@@ -14,11 +14,6 @@
14
14
  "import": "./dist/index.js",
15
15
  "require": "./dist/index.js",
16
16
  "types": "./dist/index.d.ts"
17
- },
18
- "./server": {
19
- "import": "./dist/server.js",
20
- "require": "./dist/server.js",
21
- "types": "./dist/server.d.ts"
22
17
  }
23
18
  },
24
19
  "files": ["dist"],
@@ -34,7 +29,6 @@
34
29
  "@floating-ui/dom": "^1.4.4",
35
30
  "@medv/finder": "^3.1.0",
36
31
  "@paralleldrive/cuid2": "^2.2.2",
37
- "@types/jsonwebtoken": "^9.0.10",
38
32
  "@usertour/types": "^0.0.5",
39
33
  "chroma-js": "^3.1.2",
40
34
  "class-variance-authority": "^0.4.0",
@@ -42,7 +36,6 @@
42
36
  "date-fns": "^2.30.0",
43
37
  "deepmerge-ts": "^7.1.3",
44
38
  "fast-deep-equal": "^3.1.3",
45
- "jsonwebtoken": "^9.0.2",
46
39
  "tailwind-merge": "^1.13.2",
47
40
  "uuid": "^9.0.1"
48
41
  },
@@ -1,112 +0,0 @@
1
- import {
2
- __publicField
3
- } from "./chunk-XEO3YXBM.js";
4
-
5
- // src/jwt-license-signer.ts
6
- import * as fs from "fs";
7
- import * as path from "path";
8
- import jwt from "jsonwebtoken";
9
- var JWTLicenseSigner = class {
10
- constructor(options) {
11
- __publicField(this, "privateKey");
12
- __publicField(this, "issuer");
13
- __publicField(this, "algorithm");
14
- this.privateKey = this.loadPrivateKey(options.privateKeyPath);
15
- this.issuer = options.issuer || "https://www.usertour.io";
16
- this.algorithm = options.algorithm || "RS256";
17
- }
18
- /**
19
- * Load RSA private key from file
20
- */
21
- loadPrivateKey(keyPath) {
22
- try {
23
- const fullPath = path.resolve(keyPath);
24
- return fs.readFileSync(fullPath, "utf8");
25
- } catch (error) {
26
- throw new Error(`Failed to load private key from ${keyPath}: ${error}`);
27
- }
28
- }
29
- /**
30
- * Generate a JWT license token
31
- */
32
- generateLicense(options) {
33
- const now = Math.floor(Date.now() / 1e3);
34
- const expiresAt = now + options.expiresInDays * 24 * 60 * 60;
35
- const payload = {
36
- plan: options.plan,
37
- sub: options.subject,
38
- projectId: options.projectId,
39
- iat: now,
40
- exp: expiresAt,
41
- issuer: options.issuer || this.issuer,
42
- features: options.features
43
- };
44
- try {
45
- return jwt.sign(payload, this.privateKey, {
46
- algorithm: this.algorithm,
47
- issuer: this.issuer
48
- });
49
- } catch (error) {
50
- throw new Error(`Failed to generate JWT license: ${error}`);
51
- }
52
- }
53
- /**
54
- * Generate a license and return both token and payload info
55
- */
56
- generateLicenseWithInfo(options) {
57
- const now = Math.floor(Date.now() / 1e3);
58
- const expiresAt = now + options.expiresInDays * 24 * 60 * 60;
59
- const payload = {
60
- plan: options.plan,
61
- sub: options.subject,
62
- projectId: options.projectId,
63
- iat: now,
64
- exp: expiresAt,
65
- issuer: options.issuer || this.issuer,
66
- features: options.features
67
- };
68
- const token = jwt.sign(payload, this.privateKey, {
69
- algorithm: this.algorithm,
70
- issuer: this.issuer
71
- });
72
- return {
73
- token,
74
- payload,
75
- expiresAt: new Date(expiresAt * 1e3)
76
- };
77
- }
78
- /**
79
- * Decode a JWT token without verification (for debugging)
80
- */
81
- decodeToken(token) {
82
- try {
83
- return jwt.decode(token);
84
- } catch (error) {
85
- console.error("Failed to decode JWT token:", error);
86
- return null;
87
- }
88
- }
89
- /**
90
- * Get token information without verification
91
- */
92
- getTokenInfo(token) {
93
- try {
94
- const decoded = jwt.decode(token, { complete: true });
95
- if (!decoded || typeof decoded === "string") {
96
- return null;
97
- }
98
- return {
99
- header: decoded.header,
100
- payload: decoded.payload,
101
- signature: decoded.signature
102
- };
103
- } catch (error) {
104
- console.error("Failed to get token info:", error);
105
- return null;
106
- }
107
- }
108
- };
109
-
110
- export {
111
- JWTLicenseSigner
112
- };
@@ -1,183 +0,0 @@
1
- // src/jwt-license-validator.ts
2
- import jwt from "jsonwebtoken";
3
- var JWTLicenseValidator = {
4
- /**
5
- * Validate a JWT license
6
- * @param license - JWT license string
7
- * @param publicKey - RSA public key in PEM format
8
- * @param options - Validation options
9
- * @returns Validation result
10
- */
11
- validateLicense(license, publicKey, options = {}) {
12
- try {
13
- const { checkExpiration = true, currentTime = /* @__PURE__ */ new Date() } = options;
14
- const decoded = jwt.verify(license, publicKey, {
15
- algorithms: ["RS256"],
16
- ignoreExpiration: !checkExpiration
17
- });
18
- const fieldValidation = this.validateRequiredFields(decoded);
19
- if (!fieldValidation.isValid) {
20
- return fieldValidation;
21
- }
22
- if (checkExpiration) {
23
- const expirationValidation = this.checkExpiration(decoded, currentTime);
24
- if (!expirationValidation.isValid) {
25
- return expirationValidation;
26
- }
27
- }
28
- const hasFeature = (feature) => {
29
- return decoded.features.includes("*") || decoded.features.includes(feature);
30
- };
31
- return {
32
- isValid: true,
33
- payload: decoded,
34
- isExpired: false,
35
- hasFeature
36
- };
37
- } catch (error) {
38
- if (error instanceof jwt.JsonWebTokenError) {
39
- return {
40
- isValid: false,
41
- error: `JWT validation failed: ${error.message}`
42
- };
43
- }
44
- if (error instanceof jwt.TokenExpiredError) {
45
- return {
46
- isValid: false,
47
- error: `License expired: ${error.message}`,
48
- isExpired: true
49
- };
50
- }
51
- return {
52
- isValid: false,
53
- error: `License validation failed: ${error instanceof Error ? error.message : "Unknown error"}`
54
- };
55
- }
56
- },
57
- /**
58
- * Validate that all required fields are present in license payload
59
- * @param payload - License payload to validate
60
- * @returns Validation result
61
- */
62
- validateRequiredFields(payload) {
63
- const requiredFields = ["plan", "sub", "projectId", "iat", "exp", "issuer", "features"];
64
- for (const field of requiredFields) {
65
- if (!(field in payload)) {
66
- return {
67
- isValid: false,
68
- error: `Missing required field: ${field}`
69
- };
70
- }
71
- }
72
- if (typeof payload.plan !== "string" || !payload.plan.trim()) {
73
- return {
74
- isValid: false,
75
- error: "Invalid plan: must be a non-empty string"
76
- };
77
- }
78
- if (typeof payload.sub !== "string" || !payload.sub.trim()) {
79
- return {
80
- isValid: false,
81
- error: "Invalid sub: must be a non-empty string"
82
- };
83
- }
84
- if (typeof payload.projectId !== "string" || !payload.projectId.trim()) {
85
- return {
86
- isValid: false,
87
- error: "Invalid projectId: must be a non-empty string"
88
- };
89
- }
90
- if (typeof payload.issuer !== "string" || !payload.issuer.trim()) {
91
- return {
92
- isValid: false,
93
- error: "Invalid issuer: must be a non-empty string"
94
- };
95
- }
96
- if (!Array.isArray(payload.features)) {
97
- return {
98
- isValid: false,
99
- error: "Invalid features: must be an array"
100
- };
101
- }
102
- if (typeof payload.iat !== "number" || payload.iat <= 0) {
103
- return {
104
- isValid: false,
105
- error: "Invalid iat: must be a positive number"
106
- };
107
- }
108
- if (typeof payload.exp !== "number" || payload.exp <= 0) {
109
- return {
110
- isValid: false,
111
- error: "Invalid exp: must be a positive number"
112
- };
113
- }
114
- if (payload.iat >= payload.exp) {
115
- return {
116
- isValid: false,
117
- error: "Invalid timestamps: iat must be before exp"
118
- };
119
- }
120
- return { isValid: true };
121
- },
122
- /**
123
- * Check if license has expired
124
- * @param payload - License payload
125
- * @param currentTime - Current time to check against (defaults to now)
126
- * @returns Validation result
127
- */
128
- checkExpiration(payload, currentTime = /* @__PURE__ */ new Date()) {
129
- const now = Math.floor(currentTime.getTime() / 1e3);
130
- const expiresAt = payload.exp;
131
- if (now > expiresAt) {
132
- return {
133
- isValid: false,
134
- error: `License expired on ${new Date(expiresAt * 1e3).toISOString()}`,
135
- isExpired: true
136
- };
137
- }
138
- return { isValid: true, isExpired: false };
139
- },
140
- /**
141
- * Check if license has a specific feature
142
- * @param payload - License payload
143
- * @param feature - Feature to check
144
- * @returns Whether the feature is available
145
- */
146
- hasFeature(payload, feature) {
147
- return payload.features.includes("*") || payload.features.includes(feature);
148
- },
149
- /**
150
- * Get license expiration status
151
- * @param payload - License payload
152
- * @param currentTime - Current time to check against (defaults to now)
153
- * @returns Expiration information
154
- */
155
- getExpirationInfo(payload, currentTime = /* @__PURE__ */ new Date()) {
156
- const now = Math.floor(currentTime.getTime() / 1e3);
157
- const expiresAt = payload.exp;
158
- const isExpired = now > expiresAt;
159
- const daysUntilExpiration = Math.ceil((expiresAt - now) / (24 * 60 * 60));
160
- return {
161
- isExpired,
162
- expiresAt: new Date(expiresAt * 1e3),
163
- daysUntilExpiration: isExpired ? 0 : daysUntilExpiration
164
- };
165
- },
166
- /**
167
- * Decode JWT license without verification (for debugging)
168
- * @param license - JWT license string
169
- * @returns Decoded payload or null if invalid
170
- */
171
- decodeLicense(license) {
172
- try {
173
- const decoded = jwt.decode(license);
174
- return decoded;
175
- } catch {
176
- return null;
177
- }
178
- }
179
- };
180
-
181
- export {
182
- JWTLicenseValidator
183
- };
@@ -1,147 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
- var __export = (target, all) => {
10
- for (var name in all)
11
- __defProp(target, name, { get: all[name], enumerable: true });
12
- };
13
- var __copyProps = (to, from, except, desc) => {
14
- if (from && typeof from === "object" || typeof from === "function") {
15
- for (let key of __getOwnPropNames(from))
16
- if (!__hasOwnProp.call(to, key) && key !== except)
17
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
- }
19
- return to;
20
- };
21
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
- // If the importer is in node compatibility mode or this is not an ESM
23
- // file that has been converted to a CommonJS file using a Babel-
24
- // compatible transform (i.e. "__esModule" has not been set), then set
25
- // "default" to the CommonJS "module.exports" for node compatibility.
26
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
- mod
28
- ));
29
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
- var __publicField = (obj, key, value) => {
31
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
32
- return value;
33
- };
34
-
35
- // src/jwt-license-signer.ts
36
- var jwt_license_signer_exports = {};
37
- __export(jwt_license_signer_exports, {
38
- JWTLicenseSigner: () => JWTLicenseSigner
39
- });
40
- module.exports = __toCommonJS(jwt_license_signer_exports);
41
- var fs = __toESM(require("fs"), 1);
42
- var path = __toESM(require("path"), 1);
43
- var import_jsonwebtoken = __toESM(require("jsonwebtoken"), 1);
44
- var JWTLicenseSigner = class {
45
- constructor(options) {
46
- __publicField(this, "privateKey");
47
- __publicField(this, "issuer");
48
- __publicField(this, "algorithm");
49
- this.privateKey = this.loadPrivateKey(options.privateKeyPath);
50
- this.issuer = options.issuer || "https://www.usertour.io";
51
- this.algorithm = options.algorithm || "RS256";
52
- }
53
- /**
54
- * Load RSA private key from file
55
- */
56
- loadPrivateKey(keyPath) {
57
- try {
58
- const fullPath = path.resolve(keyPath);
59
- return fs.readFileSync(fullPath, "utf8");
60
- } catch (error) {
61
- throw new Error(`Failed to load private key from ${keyPath}: ${error}`);
62
- }
63
- }
64
- /**
65
- * Generate a JWT license token
66
- */
67
- generateLicense(options) {
68
- const now = Math.floor(Date.now() / 1e3);
69
- const expiresAt = now + options.expiresInDays * 24 * 60 * 60;
70
- const payload = {
71
- plan: options.plan,
72
- sub: options.subject,
73
- projectId: options.projectId,
74
- iat: now,
75
- exp: expiresAt,
76
- issuer: options.issuer || this.issuer,
77
- features: options.features
78
- };
79
- try {
80
- return import_jsonwebtoken.default.sign(payload, this.privateKey, {
81
- algorithm: this.algorithm,
82
- issuer: this.issuer
83
- });
84
- } catch (error) {
85
- throw new Error(`Failed to generate JWT license: ${error}`);
86
- }
87
- }
88
- /**
89
- * Generate a license and return both token and payload info
90
- */
91
- generateLicenseWithInfo(options) {
92
- const now = Math.floor(Date.now() / 1e3);
93
- const expiresAt = now + options.expiresInDays * 24 * 60 * 60;
94
- const payload = {
95
- plan: options.plan,
96
- sub: options.subject,
97
- projectId: options.projectId,
98
- iat: now,
99
- exp: expiresAt,
100
- issuer: options.issuer || this.issuer,
101
- features: options.features
102
- };
103
- const token = import_jsonwebtoken.default.sign(payload, this.privateKey, {
104
- algorithm: this.algorithm,
105
- issuer: this.issuer
106
- });
107
- return {
108
- token,
109
- payload,
110
- expiresAt: new Date(expiresAt * 1e3)
111
- };
112
- }
113
- /**
114
- * Decode a JWT token without verification (for debugging)
115
- */
116
- decodeToken(token) {
117
- try {
118
- return import_jsonwebtoken.default.decode(token);
119
- } catch (error) {
120
- console.error("Failed to decode JWT token:", error);
121
- return null;
122
- }
123
- }
124
- /**
125
- * Get token information without verification
126
- */
127
- getTokenInfo(token) {
128
- try {
129
- const decoded = import_jsonwebtoken.default.decode(token, { complete: true });
130
- if (!decoded || typeof decoded === "string") {
131
- return null;
132
- }
133
- return {
134
- header: decoded.header,
135
- payload: decoded.payload,
136
- signature: decoded.signature
137
- };
138
- } catch (error) {
139
- console.error("Failed to get token info:", error);
140
- return null;
141
- }
142
- }
143
- };
144
- // Annotate the CommonJS export names for ESM import in node:
145
- 0 && (module.exports = {
146
- JWTLicenseSigner
147
- });
@@ -1,61 +0,0 @@
1
- import jwt from 'jsonwebtoken';
2
- import { JWTLicensePayload } from '@usertour/types';
3
-
4
- interface JWTLicenseSignerOptions {
5
- /** RSA private key path */
6
- privateKeyPath: string;
7
- /** JWT issuer */
8
- issuer?: string;
9
- /** JWT algorithm */
10
- algorithm?: jwt.Algorithm;
11
- }
12
- interface GenerateLicenseOptions {
13
- /** License plan type */
14
- plan: string;
15
- /** Subject (project name) */
16
- subject: string;
17
- /** Project identifier */
18
- projectId: string;
19
- /** Expiration days from now */
20
- expiresInDays: number;
21
- /** Array of enabled features, '*' means all features */
22
- features: string[];
23
- /** Custom issuer (optional) */
24
- issuer?: string;
25
- }
26
- declare class JWTLicenseSigner {
27
- private privateKey;
28
- private issuer;
29
- private algorithm;
30
- constructor(options: JWTLicenseSignerOptions);
31
- /**
32
- * Load RSA private key from file
33
- */
34
- private loadPrivateKey;
35
- /**
36
- * Generate a JWT license token
37
- */
38
- generateLicense(options: GenerateLicenseOptions): string;
39
- /**
40
- * Generate a license and return both token and payload info
41
- */
42
- generateLicenseWithInfo(options: GenerateLicenseOptions): {
43
- token: string;
44
- payload: JWTLicensePayload;
45
- expiresAt: Date;
46
- };
47
- /**
48
- * Decode a JWT token without verification (for debugging)
49
- */
50
- decodeToken(token: string): JWTLicensePayload | null;
51
- /**
52
- * Get token information without verification
53
- */
54
- getTokenInfo(token: string): {
55
- header: jwt.JwtHeader;
56
- payload: JWTLicensePayload;
57
- signature: string;
58
- } | null;
59
- }
60
-
61
- export { type GenerateLicenseOptions, JWTLicenseSigner, type JWTLicenseSignerOptions };
@@ -1,61 +0,0 @@
1
- import jwt from 'jsonwebtoken';
2
- import { JWTLicensePayload } from '@usertour/types';
3
-
4
- interface JWTLicenseSignerOptions {
5
- /** RSA private key path */
6
- privateKeyPath: string;
7
- /** JWT issuer */
8
- issuer?: string;
9
- /** JWT algorithm */
10
- algorithm?: jwt.Algorithm;
11
- }
12
- interface GenerateLicenseOptions {
13
- /** License plan type */
14
- plan: string;
15
- /** Subject (project name) */
16
- subject: string;
17
- /** Project identifier */
18
- projectId: string;
19
- /** Expiration days from now */
20
- expiresInDays: number;
21
- /** Array of enabled features, '*' means all features */
22
- features: string[];
23
- /** Custom issuer (optional) */
24
- issuer?: string;
25
- }
26
- declare class JWTLicenseSigner {
27
- private privateKey;
28
- private issuer;
29
- private algorithm;
30
- constructor(options: JWTLicenseSignerOptions);
31
- /**
32
- * Load RSA private key from file
33
- */
34
- private loadPrivateKey;
35
- /**
36
- * Generate a JWT license token
37
- */
38
- generateLicense(options: GenerateLicenseOptions): string;
39
- /**
40
- * Generate a license and return both token and payload info
41
- */
42
- generateLicenseWithInfo(options: GenerateLicenseOptions): {
43
- token: string;
44
- payload: JWTLicensePayload;
45
- expiresAt: Date;
46
- };
47
- /**
48
- * Decode a JWT token without verification (for debugging)
49
- */
50
- decodeToken(token: string): JWTLicensePayload | null;
51
- /**
52
- * Get token information without verification
53
- */
54
- getTokenInfo(token: string): {
55
- header: jwt.JwtHeader;
56
- payload: JWTLicensePayload;
57
- signature: string;
58
- } | null;
59
- }
60
-
61
- export { type GenerateLicenseOptions, JWTLicenseSigner, type JWTLicenseSignerOptions };
@@ -1,7 +0,0 @@
1
- import {
2
- JWTLicenseSigner
3
- } from "./chunk-HWWIAVVP.js";
4
- import "./chunk-XEO3YXBM.js";
5
- export {
6
- JWTLicenseSigner
7
- };
@@ -1,217 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/jwt-license-validator.ts
31
- var jwt_license_validator_exports = {};
32
- __export(jwt_license_validator_exports, {
33
- JWTLicenseValidator: () => JWTLicenseValidator
34
- });
35
- module.exports = __toCommonJS(jwt_license_validator_exports);
36
- var import_jsonwebtoken = __toESM(require("jsonwebtoken"), 1);
37
- var JWTLicenseValidator = {
38
- /**
39
- * Validate a JWT license
40
- * @param license - JWT license string
41
- * @param publicKey - RSA public key in PEM format
42
- * @param options - Validation options
43
- * @returns Validation result
44
- */
45
- validateLicense(license, publicKey, options = {}) {
46
- try {
47
- const { checkExpiration = true, currentTime = /* @__PURE__ */ new Date() } = options;
48
- const decoded = import_jsonwebtoken.default.verify(license, publicKey, {
49
- algorithms: ["RS256"],
50
- ignoreExpiration: !checkExpiration
51
- });
52
- const fieldValidation = this.validateRequiredFields(decoded);
53
- if (!fieldValidation.isValid) {
54
- return fieldValidation;
55
- }
56
- if (checkExpiration) {
57
- const expirationValidation = this.checkExpiration(decoded, currentTime);
58
- if (!expirationValidation.isValid) {
59
- return expirationValidation;
60
- }
61
- }
62
- const hasFeature = (feature) => {
63
- return decoded.features.includes("*") || decoded.features.includes(feature);
64
- };
65
- return {
66
- isValid: true,
67
- payload: decoded,
68
- isExpired: false,
69
- hasFeature
70
- };
71
- } catch (error) {
72
- if (error instanceof import_jsonwebtoken.default.JsonWebTokenError) {
73
- return {
74
- isValid: false,
75
- error: `JWT validation failed: ${error.message}`
76
- };
77
- }
78
- if (error instanceof import_jsonwebtoken.default.TokenExpiredError) {
79
- return {
80
- isValid: false,
81
- error: `License expired: ${error.message}`,
82
- isExpired: true
83
- };
84
- }
85
- return {
86
- isValid: false,
87
- error: `License validation failed: ${error instanceof Error ? error.message : "Unknown error"}`
88
- };
89
- }
90
- },
91
- /**
92
- * Validate that all required fields are present in license payload
93
- * @param payload - License payload to validate
94
- * @returns Validation result
95
- */
96
- validateRequiredFields(payload) {
97
- const requiredFields = ["plan", "sub", "projectId", "iat", "exp", "issuer", "features"];
98
- for (const field of requiredFields) {
99
- if (!(field in payload)) {
100
- return {
101
- isValid: false,
102
- error: `Missing required field: ${field}`
103
- };
104
- }
105
- }
106
- if (typeof payload.plan !== "string" || !payload.plan.trim()) {
107
- return {
108
- isValid: false,
109
- error: "Invalid plan: must be a non-empty string"
110
- };
111
- }
112
- if (typeof payload.sub !== "string" || !payload.sub.trim()) {
113
- return {
114
- isValid: false,
115
- error: "Invalid sub: must be a non-empty string"
116
- };
117
- }
118
- if (typeof payload.projectId !== "string" || !payload.projectId.trim()) {
119
- return {
120
- isValid: false,
121
- error: "Invalid projectId: must be a non-empty string"
122
- };
123
- }
124
- if (typeof payload.issuer !== "string" || !payload.issuer.trim()) {
125
- return {
126
- isValid: false,
127
- error: "Invalid issuer: must be a non-empty string"
128
- };
129
- }
130
- if (!Array.isArray(payload.features)) {
131
- return {
132
- isValid: false,
133
- error: "Invalid features: must be an array"
134
- };
135
- }
136
- if (typeof payload.iat !== "number" || payload.iat <= 0) {
137
- return {
138
- isValid: false,
139
- error: "Invalid iat: must be a positive number"
140
- };
141
- }
142
- if (typeof payload.exp !== "number" || payload.exp <= 0) {
143
- return {
144
- isValid: false,
145
- error: "Invalid exp: must be a positive number"
146
- };
147
- }
148
- if (payload.iat >= payload.exp) {
149
- return {
150
- isValid: false,
151
- error: "Invalid timestamps: iat must be before exp"
152
- };
153
- }
154
- return { isValid: true };
155
- },
156
- /**
157
- * Check if license has expired
158
- * @param payload - License payload
159
- * @param currentTime - Current time to check against (defaults to now)
160
- * @returns Validation result
161
- */
162
- checkExpiration(payload, currentTime = /* @__PURE__ */ new Date()) {
163
- const now = Math.floor(currentTime.getTime() / 1e3);
164
- const expiresAt = payload.exp;
165
- if (now > expiresAt) {
166
- return {
167
- isValid: false,
168
- error: `License expired on ${new Date(expiresAt * 1e3).toISOString()}`,
169
- isExpired: true
170
- };
171
- }
172
- return { isValid: true, isExpired: false };
173
- },
174
- /**
175
- * Check if license has a specific feature
176
- * @param payload - License payload
177
- * @param feature - Feature to check
178
- * @returns Whether the feature is available
179
- */
180
- hasFeature(payload, feature) {
181
- return payload.features.includes("*") || payload.features.includes(feature);
182
- },
183
- /**
184
- * Get license expiration status
185
- * @param payload - License payload
186
- * @param currentTime - Current time to check against (defaults to now)
187
- * @returns Expiration information
188
- */
189
- getExpirationInfo(payload, currentTime = /* @__PURE__ */ new Date()) {
190
- const now = Math.floor(currentTime.getTime() / 1e3);
191
- const expiresAt = payload.exp;
192
- const isExpired = now > expiresAt;
193
- const daysUntilExpiration = Math.ceil((expiresAt - now) / (24 * 60 * 60));
194
- return {
195
- isExpired,
196
- expiresAt: new Date(expiresAt * 1e3),
197
- daysUntilExpiration: isExpired ? 0 : daysUntilExpiration
198
- };
199
- },
200
- /**
201
- * Decode JWT license without verification (for debugging)
202
- * @param license - JWT license string
203
- * @returns Decoded payload or null if invalid
204
- */
205
- decodeLicense(license) {
206
- try {
207
- const decoded = import_jsonwebtoken.default.decode(license);
208
- return decoded;
209
- } catch {
210
- return null;
211
- }
212
- }
213
- };
214
- // Annotate the CommonJS export names for ESM import in node:
215
- 0 && (module.exports = {
216
- JWTLicenseValidator
217
- });
@@ -1,54 +0,0 @@
1
- import { JWTLicenseValidationOptions, JWTLicenseValidationResult, JWTLicensePayload } from '@usertour/types';
2
-
3
- /**
4
- * JWT License validator
5
- */
6
- declare const JWTLicenseValidator: {
7
- /**
8
- * Validate a JWT license
9
- * @param license - JWT license string
10
- * @param publicKey - RSA public key in PEM format
11
- * @param options - Validation options
12
- * @returns Validation result
13
- */
14
- validateLicense(license: string, publicKey: string, options?: JWTLicenseValidationOptions): JWTLicenseValidationResult;
15
- /**
16
- * Validate that all required fields are present in license payload
17
- * @param payload - License payload to validate
18
- * @returns Validation result
19
- */
20
- validateRequiredFields(payload: JWTLicensePayload): JWTLicenseValidationResult;
21
- /**
22
- * Check if license has expired
23
- * @param payload - License payload
24
- * @param currentTime - Current time to check against (defaults to now)
25
- * @returns Validation result
26
- */
27
- checkExpiration(payload: JWTLicensePayload, currentTime?: Date): JWTLicenseValidationResult;
28
- /**
29
- * Check if license has a specific feature
30
- * @param payload - License payload
31
- * @param feature - Feature to check
32
- * @returns Whether the feature is available
33
- */
34
- hasFeature(payload: JWTLicensePayload, feature: string): boolean;
35
- /**
36
- * Get license expiration status
37
- * @param payload - License payload
38
- * @param currentTime - Current time to check against (defaults to now)
39
- * @returns Expiration information
40
- */
41
- getExpirationInfo(payload: JWTLicensePayload, currentTime?: Date): {
42
- isExpired: boolean;
43
- expiresAt: Date;
44
- daysUntilExpiration: number;
45
- };
46
- /**
47
- * Decode JWT license without verification (for debugging)
48
- * @param license - JWT license string
49
- * @returns Decoded payload or null if invalid
50
- */
51
- decodeLicense(license: string): JWTLicensePayload | null;
52
- };
53
-
54
- export { JWTLicenseValidator };
@@ -1,54 +0,0 @@
1
- import { JWTLicenseValidationOptions, JWTLicenseValidationResult, JWTLicensePayload } from '@usertour/types';
2
-
3
- /**
4
- * JWT License validator
5
- */
6
- declare const JWTLicenseValidator: {
7
- /**
8
- * Validate a JWT license
9
- * @param license - JWT license string
10
- * @param publicKey - RSA public key in PEM format
11
- * @param options - Validation options
12
- * @returns Validation result
13
- */
14
- validateLicense(license: string, publicKey: string, options?: JWTLicenseValidationOptions): JWTLicenseValidationResult;
15
- /**
16
- * Validate that all required fields are present in license payload
17
- * @param payload - License payload to validate
18
- * @returns Validation result
19
- */
20
- validateRequiredFields(payload: JWTLicensePayload): JWTLicenseValidationResult;
21
- /**
22
- * Check if license has expired
23
- * @param payload - License payload
24
- * @param currentTime - Current time to check against (defaults to now)
25
- * @returns Validation result
26
- */
27
- checkExpiration(payload: JWTLicensePayload, currentTime?: Date): JWTLicenseValidationResult;
28
- /**
29
- * Check if license has a specific feature
30
- * @param payload - License payload
31
- * @param feature - Feature to check
32
- * @returns Whether the feature is available
33
- */
34
- hasFeature(payload: JWTLicensePayload, feature: string): boolean;
35
- /**
36
- * Get license expiration status
37
- * @param payload - License payload
38
- * @param currentTime - Current time to check against (defaults to now)
39
- * @returns Expiration information
40
- */
41
- getExpirationInfo(payload: JWTLicensePayload, currentTime?: Date): {
42
- isExpired: boolean;
43
- expiresAt: Date;
44
- daysUntilExpiration: number;
45
- };
46
- /**
47
- * Decode JWT license without verification (for debugging)
48
- * @param license - JWT license string
49
- * @returns Decoded payload or null if invalid
50
- */
51
- decodeLicense(license: string): JWTLicensePayload | null;
52
- };
53
-
54
- export { JWTLicenseValidator };
@@ -1,7 +0,0 @@
1
- import {
2
- JWTLicenseValidator
3
- } from "./chunk-Y5PCSFVZ.js";
4
- import "./chunk-XEO3YXBM.js";
5
- export {
6
- JWTLicenseValidator
7
- };
package/dist/server.cjs DELETED
@@ -1,331 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
- var __export = (target, all) => {
10
- for (var name in all)
11
- __defProp(target, name, { get: all[name], enumerable: true });
12
- };
13
- var __copyProps = (to, from, except, desc) => {
14
- if (from && typeof from === "object" || typeof from === "function") {
15
- for (let key of __getOwnPropNames(from))
16
- if (!__hasOwnProp.call(to, key) && key !== except)
17
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
- }
19
- return to;
20
- };
21
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
- // If the importer is in node compatibility mode or this is not an ESM
23
- // file that has been converted to a CommonJS file using a Babel-
24
- // compatible transform (i.e. "__esModule" has not been set), then set
25
- // "default" to the CommonJS "module.exports" for node compatibility.
26
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
- mod
28
- ));
29
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
- var __publicField = (obj, key, value) => {
31
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
32
- return value;
33
- };
34
-
35
- // src/server.ts
36
- var server_exports = {};
37
- __export(server_exports, {
38
- JWTLicenseSigner: () => JWTLicenseSigner,
39
- JWTLicenseValidator: () => JWTLicenseValidator
40
- });
41
- module.exports = __toCommonJS(server_exports);
42
-
43
- // src/jwt-license-signer.ts
44
- var fs = __toESM(require("fs"), 1);
45
- var path = __toESM(require("path"), 1);
46
- var import_jsonwebtoken = __toESM(require("jsonwebtoken"), 1);
47
- var JWTLicenseSigner = class {
48
- constructor(options) {
49
- __publicField(this, "privateKey");
50
- __publicField(this, "issuer");
51
- __publicField(this, "algorithm");
52
- this.privateKey = this.loadPrivateKey(options.privateKeyPath);
53
- this.issuer = options.issuer || "https://www.usertour.io";
54
- this.algorithm = options.algorithm || "RS256";
55
- }
56
- /**
57
- * Load RSA private key from file
58
- */
59
- loadPrivateKey(keyPath) {
60
- try {
61
- const fullPath = path.resolve(keyPath);
62
- return fs.readFileSync(fullPath, "utf8");
63
- } catch (error) {
64
- throw new Error(`Failed to load private key from ${keyPath}: ${error}`);
65
- }
66
- }
67
- /**
68
- * Generate a JWT license token
69
- */
70
- generateLicense(options) {
71
- const now = Math.floor(Date.now() / 1e3);
72
- const expiresAt = now + options.expiresInDays * 24 * 60 * 60;
73
- const payload = {
74
- plan: options.plan,
75
- sub: options.subject,
76
- projectId: options.projectId,
77
- iat: now,
78
- exp: expiresAt,
79
- issuer: options.issuer || this.issuer,
80
- features: options.features
81
- };
82
- try {
83
- return import_jsonwebtoken.default.sign(payload, this.privateKey, {
84
- algorithm: this.algorithm,
85
- issuer: this.issuer
86
- });
87
- } catch (error) {
88
- throw new Error(`Failed to generate JWT license: ${error}`);
89
- }
90
- }
91
- /**
92
- * Generate a license and return both token and payload info
93
- */
94
- generateLicenseWithInfo(options) {
95
- const now = Math.floor(Date.now() / 1e3);
96
- const expiresAt = now + options.expiresInDays * 24 * 60 * 60;
97
- const payload = {
98
- plan: options.plan,
99
- sub: options.subject,
100
- projectId: options.projectId,
101
- iat: now,
102
- exp: expiresAt,
103
- issuer: options.issuer || this.issuer,
104
- features: options.features
105
- };
106
- const token = import_jsonwebtoken.default.sign(payload, this.privateKey, {
107
- algorithm: this.algorithm,
108
- issuer: this.issuer
109
- });
110
- return {
111
- token,
112
- payload,
113
- expiresAt: new Date(expiresAt * 1e3)
114
- };
115
- }
116
- /**
117
- * Decode a JWT token without verification (for debugging)
118
- */
119
- decodeToken(token) {
120
- try {
121
- return import_jsonwebtoken.default.decode(token);
122
- } catch (error) {
123
- console.error("Failed to decode JWT token:", error);
124
- return null;
125
- }
126
- }
127
- /**
128
- * Get token information without verification
129
- */
130
- getTokenInfo(token) {
131
- try {
132
- const decoded = import_jsonwebtoken.default.decode(token, { complete: true });
133
- if (!decoded || typeof decoded === "string") {
134
- return null;
135
- }
136
- return {
137
- header: decoded.header,
138
- payload: decoded.payload,
139
- signature: decoded.signature
140
- };
141
- } catch (error) {
142
- console.error("Failed to get token info:", error);
143
- return null;
144
- }
145
- }
146
- };
147
-
148
- // src/jwt-license-validator.ts
149
- var import_jsonwebtoken2 = __toESM(require("jsonwebtoken"), 1);
150
- var JWTLicenseValidator = {
151
- /**
152
- * Validate a JWT license
153
- * @param license - JWT license string
154
- * @param publicKey - RSA public key in PEM format
155
- * @param options - Validation options
156
- * @returns Validation result
157
- */
158
- validateLicense(license, publicKey, options = {}) {
159
- try {
160
- const { checkExpiration = true, currentTime = /* @__PURE__ */ new Date() } = options;
161
- const decoded = import_jsonwebtoken2.default.verify(license, publicKey, {
162
- algorithms: ["RS256"],
163
- ignoreExpiration: !checkExpiration
164
- });
165
- const fieldValidation = this.validateRequiredFields(decoded);
166
- if (!fieldValidation.isValid) {
167
- return fieldValidation;
168
- }
169
- if (checkExpiration) {
170
- const expirationValidation = this.checkExpiration(decoded, currentTime);
171
- if (!expirationValidation.isValid) {
172
- return expirationValidation;
173
- }
174
- }
175
- const hasFeature = (feature) => {
176
- return decoded.features.includes("*") || decoded.features.includes(feature);
177
- };
178
- return {
179
- isValid: true,
180
- payload: decoded,
181
- isExpired: false,
182
- hasFeature
183
- };
184
- } catch (error) {
185
- if (error instanceof import_jsonwebtoken2.default.JsonWebTokenError) {
186
- return {
187
- isValid: false,
188
- error: `JWT validation failed: ${error.message}`
189
- };
190
- }
191
- if (error instanceof import_jsonwebtoken2.default.TokenExpiredError) {
192
- return {
193
- isValid: false,
194
- error: `License expired: ${error.message}`,
195
- isExpired: true
196
- };
197
- }
198
- return {
199
- isValid: false,
200
- error: `License validation failed: ${error instanceof Error ? error.message : "Unknown error"}`
201
- };
202
- }
203
- },
204
- /**
205
- * Validate that all required fields are present in license payload
206
- * @param payload - License payload to validate
207
- * @returns Validation result
208
- */
209
- validateRequiredFields(payload) {
210
- const requiredFields = ["plan", "sub", "projectId", "iat", "exp", "issuer", "features"];
211
- for (const field of requiredFields) {
212
- if (!(field in payload)) {
213
- return {
214
- isValid: false,
215
- error: `Missing required field: ${field}`
216
- };
217
- }
218
- }
219
- if (typeof payload.plan !== "string" || !payload.plan.trim()) {
220
- return {
221
- isValid: false,
222
- error: "Invalid plan: must be a non-empty string"
223
- };
224
- }
225
- if (typeof payload.sub !== "string" || !payload.sub.trim()) {
226
- return {
227
- isValid: false,
228
- error: "Invalid sub: must be a non-empty string"
229
- };
230
- }
231
- if (typeof payload.projectId !== "string" || !payload.projectId.trim()) {
232
- return {
233
- isValid: false,
234
- error: "Invalid projectId: must be a non-empty string"
235
- };
236
- }
237
- if (typeof payload.issuer !== "string" || !payload.issuer.trim()) {
238
- return {
239
- isValid: false,
240
- error: "Invalid issuer: must be a non-empty string"
241
- };
242
- }
243
- if (!Array.isArray(payload.features)) {
244
- return {
245
- isValid: false,
246
- error: "Invalid features: must be an array"
247
- };
248
- }
249
- if (typeof payload.iat !== "number" || payload.iat <= 0) {
250
- return {
251
- isValid: false,
252
- error: "Invalid iat: must be a positive number"
253
- };
254
- }
255
- if (typeof payload.exp !== "number" || payload.exp <= 0) {
256
- return {
257
- isValid: false,
258
- error: "Invalid exp: must be a positive number"
259
- };
260
- }
261
- if (payload.iat >= payload.exp) {
262
- return {
263
- isValid: false,
264
- error: "Invalid timestamps: iat must be before exp"
265
- };
266
- }
267
- return { isValid: true };
268
- },
269
- /**
270
- * Check if license has expired
271
- * @param payload - License payload
272
- * @param currentTime - Current time to check against (defaults to now)
273
- * @returns Validation result
274
- */
275
- checkExpiration(payload, currentTime = /* @__PURE__ */ new Date()) {
276
- const now = Math.floor(currentTime.getTime() / 1e3);
277
- const expiresAt = payload.exp;
278
- if (now > expiresAt) {
279
- return {
280
- isValid: false,
281
- error: `License expired on ${new Date(expiresAt * 1e3).toISOString()}`,
282
- isExpired: true
283
- };
284
- }
285
- return { isValid: true, isExpired: false };
286
- },
287
- /**
288
- * Check if license has a specific feature
289
- * @param payload - License payload
290
- * @param feature - Feature to check
291
- * @returns Whether the feature is available
292
- */
293
- hasFeature(payload, feature) {
294
- return payload.features.includes("*") || payload.features.includes(feature);
295
- },
296
- /**
297
- * Get license expiration status
298
- * @param payload - License payload
299
- * @param currentTime - Current time to check against (defaults to now)
300
- * @returns Expiration information
301
- */
302
- getExpirationInfo(payload, currentTime = /* @__PURE__ */ new Date()) {
303
- const now = Math.floor(currentTime.getTime() / 1e3);
304
- const expiresAt = payload.exp;
305
- const isExpired = now > expiresAt;
306
- const daysUntilExpiration = Math.ceil((expiresAt - now) / (24 * 60 * 60));
307
- return {
308
- isExpired,
309
- expiresAt: new Date(expiresAt * 1e3),
310
- daysUntilExpiration: isExpired ? 0 : daysUntilExpiration
311
- };
312
- },
313
- /**
314
- * Decode JWT license without verification (for debugging)
315
- * @param license - JWT license string
316
- * @returns Decoded payload or null if invalid
317
- */
318
- decodeLicense(license) {
319
- try {
320
- const decoded = import_jsonwebtoken2.default.decode(license);
321
- return decoded;
322
- } catch {
323
- return null;
324
- }
325
- }
326
- };
327
- // Annotate the CommonJS export names for ESM import in node:
328
- 0 && (module.exports = {
329
- JWTLicenseSigner,
330
- JWTLicenseValidator
331
- });
package/dist/server.d.cts DELETED
@@ -1,4 +0,0 @@
1
- export { GenerateLicenseOptions, JWTLicenseSigner, JWTLicenseSignerOptions } from './jwt-license-signer.cjs';
2
- export { JWTLicenseValidator } from './jwt-license-validator.cjs';
3
- import 'jsonwebtoken';
4
- import '@usertour/types';
package/dist/server.d.ts DELETED
@@ -1,4 +0,0 @@
1
- export { GenerateLicenseOptions, JWTLicenseSigner, JWTLicenseSignerOptions } from './jwt-license-signer.js';
2
- export { JWTLicenseValidator } from './jwt-license-validator.js';
3
- import 'jsonwebtoken';
4
- import '@usertour/types';
package/dist/server.js DELETED
@@ -1,11 +0,0 @@
1
- import {
2
- JWTLicenseSigner
3
- } from "./chunk-HWWIAVVP.js";
4
- import {
5
- JWTLicenseValidator
6
- } from "./chunk-Y5PCSFVZ.js";
7
- import "./chunk-XEO3YXBM.js";
8
- export {
9
- JWTLicenseSigner,
10
- JWTLicenseValidator
11
- };