@yogesh0333/yogiway
Version:
YOGIWAY Format - Ultra-compact, nested-aware data format for LLM prompts. Handles deeply nested JSON efficiently, 10-15% more efficient than TOON.
396 lines • 13.7 kB
JavaScript
;
/**
* Premium License Verification System for YOGIWAY Library
* Only verified premium users can use the library
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.activateLicense = activateLicense;
exports.getRemainingTrialDays = getRemainingTrialDays;
exports.checkLicense = checkLicense;
exports.verifyLicense = verifyLicense;
exports.requireLicense = requireLicense;
exports.clearLicense = clearLicense;
let cachedLicense = null;
let licenseCheckPromise = null;
// License server endpoint (deprecated - library is now free)
const LICENSE_SERVER_URL = process.env.YOGIWAY_LICENSE_SERVER || "";
const TRIAL_DAYS = 30;
const TRIAL_STORAGE_KEY = "yogiway_trial_started";
/**
* Verify license with server
*/
async function verifyLicenseWithServer(licenseKey) {
try {
const response = await fetch(LICENSE_SERVER_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ licenseKey }),
});
if (!response.ok) {
throw new Error("License server error");
}
const data = (await response.json());
if (!data.valid) {
return {
isValid: false,
type: "trial",
licenseKey,
};
}
return {
isValid: true,
type: (data.type || "premium"),
expiresAt: data.expiresAt,
userId: data.userId,
licenseKey,
subscriptionPlan: data.subscriptionPlan,
};
}
catch (error) {
// If server is unavailable, check local storage for cached license
const cached = getCachedLicense();
if (cached && cached.licenseKey === licenseKey) {
return cached;
}
throw new Error("License verification failed. Please check your internet connection.");
}
}
/**
* Get cached license from localStorage (browser) or file system (Node.js)
*/
function getCachedLicense() {
if (typeof window !== "undefined" && window.localStorage) {
// Browser environment
const cached = window.localStorage.getItem("yogiway_license");
if (cached) {
try {
const license = JSON.parse(cached);
// Check if expired
if (license.expiresAt && license.expiresAt < Date.now()) {
window.localStorage.removeItem("yogiway_license");
return null;
}
return license;
}
catch {
return null;
}
}
}
else if (typeof require !== "undefined") {
// Node.js environment
try {
const fs = require("fs");
const path = require("path");
const os = require("os");
const licensePath = path.join(os.homedir(), ".yogiway", "license.json");
if (fs.existsSync(licensePath)) {
const cached = JSON.parse(fs.readFileSync(licensePath, "utf8"));
if (cached.expiresAt && cached.expiresAt < Date.now()) {
fs.unlinkSync(licensePath);
return null;
}
return cached;
}
}
catch {
// Ignore errors
}
}
return null;
}
/**
* Cache license locally
*/
function cacheLicense(license) {
if (typeof window !== "undefined" && window.localStorage) {
// Browser environment
window.localStorage.setItem("yogiway_license", JSON.stringify(license));
}
else if (typeof require !== "undefined") {
// Node.js environment
try {
const fs = require("fs");
const path = require("path");
const os = require("os");
const licenseDir = path.join(os.homedir(), ".yogiway");
if (!fs.existsSync(licenseDir)) {
fs.mkdirSync(licenseDir, { recursive: true });
}
const licensePath = path.join(licenseDir, "license.json");
fs.writeFileSync(licensePath, JSON.stringify(license, null, 2));
}
catch {
// Ignore errors
}
}
}
/**
* Activate premium license
*/
async function activateLicense(licenseKey) {
if (!licenseKey || typeof licenseKey !== "string") {
throw new Error("Invalid license key. Please provide a valid premium license key.");
}
// Validate format
if (!/^YOGIWAY-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}$/.test(licenseKey)) {
throw new Error("Invalid license key format. Expected format: YOGIWAY-XXXX-XXXX-XXXX-XXXX");
}
// Verify with server
const license = await verifyLicenseWithServer(licenseKey);
if (!license.isValid) {
throw new Error("Invalid or expired license key. Please purchase a premium license.");
}
// Cache the license
cacheLicense(license);
cachedLicense = license;
return license;
}
/**
* Start free trial (30 days)
*/
function startTrial() {
const trialStartedAt = Date.now();
const trialExpiresAt = trialStartedAt + TRIAL_DAYS * 24 * 60 * 60 * 1000;
const trialLicense = {
isValid: true,
type: "trial",
expiresAt: trialExpiresAt,
trialStartedAt: trialStartedAt,
};
// Store trial start date
if (typeof window !== "undefined" && window.localStorage) {
window.localStorage.setItem(TRIAL_STORAGE_KEY, trialStartedAt.toString());
}
else if (typeof require !== "undefined") {
try {
const fs = require("fs");
const path = require("path");
const os = require("os");
const trialDir = path.join(os.homedir(), ".yogiway");
if (!fs.existsSync(trialDir)) {
fs.mkdirSync(trialDir, { recursive: true });
}
const trialPath = path.join(trialDir, "trial.json");
fs.writeFileSync(trialPath, JSON.stringify({ startedAt: trialStartedAt }, null, 2));
}
catch {
// Ignore errors
}
}
cacheLicense(trialLicense);
cachedLicense = trialLicense;
return trialLicense;
}
/**
* Get trial start date
*/
function getTrialStartDate() {
if (typeof window !== "undefined" && window.localStorage) {
const started = window.localStorage.getItem(TRIAL_STORAGE_KEY);
return started ? parseInt(started, 10) : null;
}
else if (typeof require !== "undefined") {
try {
const fs = require("fs");
const path = require("path");
const os = require("os");
const trialPath = path.join(os.homedir(), ".yogiway", "trial.json");
if (fs.existsSync(trialPath)) {
const data = JSON.parse(fs.readFileSync(trialPath, "utf8"));
return data.startedAt || null;
}
}
catch {
// Ignore errors
}
}
return null;
}
/**
* Check if trial is still valid
*/
function isTrialValid(trialStartedAt) {
const trialExpiresAt = trialStartedAt + TRIAL_DAYS * 24 * 60 * 60 * 1000;
return Date.now() < trialExpiresAt;
}
/**
* Get remaining trial days
*/
function getRemainingTrialDays() {
const trialStartedAt = getTrialStartDate();
if (!trialStartedAt)
return TRIAL_DAYS;
const trialExpiresAt = trialStartedAt + TRIAL_DAYS * 24 * 60 * 60 * 1000;
const remaining = trialExpiresAt - Date.now();
const days = Math.ceil(remaining / (24 * 60 * 60 * 1000));
return Math.max(0, days);
}
/**
* Check if license is valid (synchronous check using cache)
*/
function checkLicense() {
// Return cached license if available
if (cachedLicense) {
// Check expiration
if (cachedLicense.expiresAt && cachedLicense.expiresAt < Date.now()) {
// Check if trial can be started
const trialStarted = getTrialStartDate();
if (!trialStarted || !isTrialValid(trialStarted)) {
cachedLicense = { isValid: false, type: "trial" };
return cachedLicense;
}
// Trial is valid, update cached license
const trialLicense = {
isValid: true,
type: "trial",
expiresAt: trialStarted + TRIAL_DAYS * 24 * 60 * 60 * 1000,
trialStartedAt: trialStarted,
};
cachedLicense = trialLicense;
cacheLicense(trialLicense);
return cachedLicense;
}
return cachedLicense;
}
// Try to get from cache
const cached = getCachedLicense();
if (cached) {
cachedLicense = cached;
// Check if expired
if (cached.expiresAt && cached.expiresAt < Date.now()) {
// Check if trial is still valid
const trialStarted = getTrialStartDate();
if (trialStarted && isTrialValid(trialStarted)) {
// Trial still valid, return trial license
return {
isValid: true,
type: "trial",
expiresAt: trialStarted + TRIAL_DAYS * 24 * 60 * 60 * 1000,
trialStartedAt: trialStarted,
};
}
return { isValid: false, type: "trial" };
}
return cached;
}
// No license found - check if trial can be started
const trialStarted = getTrialStartDate();
if (trialStarted && isTrialValid(trialStarted)) {
// Trial already started and valid
return {
isValid: true,
type: "trial",
expiresAt: trialStarted + TRIAL_DAYS * 24 * 60 * 60 * 1000,
trialStartedAt: trialStarted,
};
}
// No trial started yet - start free trial
if (!trialStarted) {
return startTrial();
}
// Trial expired
return { isValid: false, type: "trial" };
}
/**
* Verify license (async, checks with server)
*/
async function verifyLicense(licenseKey) {
// If license key provided, activate it
if (licenseKey) {
return await activateLicense(licenseKey);
}
// Otherwise, check cached license
const cached = getCachedLicense();
if (cached) {
// Verify with server in background
if (cached.licenseKey) {
licenseCheckPromise = verifyLicenseWithServer(cached.licenseKey)
.then((verified) => {
if (verified.isValid) {
cacheLicense(verified);
cachedLicense = verified;
}
return verified;
})
.catch(() => cached);
}
return cached;
}
return { isValid: false, type: "trial" };
}
/**
* Require valid license or throw error
*/
function requireLicense() {
const license = checkLicense();
if (!license.isValid) {
const trialStarted = getTrialStartDate();
const remainingDays = getRemainingTrialDays();
if (trialStarted && remainingDays > 0) {
// Trial should be valid but something went wrong, restart it
const newTrial = startTrial();
if (newTrial.isValid) {
return;
}
}
if (trialStarted && remainingDays === 0) {
// Trial expired
throw new Error("YOGIWAY Free Trial Expired\n\n" +
`Your 30-day free trial has ended. Please subscribe to continue using YOGIWAY.\n\n` +
"Subscribe to a plan:\n" +
" - Monthly: $9.99/month\n" +
" - Yearly: $99.99/year (Save 17%)\n\n" +
"Get your subscription:\n" +
" - VS Code Extension purchase\n" +
" - https://yogiways.dev\n" +
" - Contact: yogeshkumar0333@gmail.com\n\n" +
"After subscribing, activate your license:\n" +
' import { activateLicense } from "@yogesh0333/yogiway";\n' +
' await activateLicense("YOUR_LICENSE_KEY");');
}
// No trial started (shouldn't happen, but just in case)
throw new Error("YOGIWAY is now completely free!\n\n" +
"No license required. Use it freely in any project.\n\n" +
"Get started:\n" +
" - npm: https://www.npmjs.com/package/@yogesh0333/yogiway\n" +
" - npm: npm install @yogesh0333/yogiway\n" +
" - VS Code Extension: Free on marketplace");
}
// License is valid - check if it's trial and show reminder
if (license.type === "trial") {
const remainingDays = getRemainingTrialDays();
if (remainingDays <= 7 && remainingDays > 0) {
// Show warning in console (non-blocking)
if (typeof console !== "undefined") {
console.warn(`⚠️ YOGIWAY: Library is now completely free! No subscription needed.`);
}
}
}
}
/**
* Clear cached license
*/
function clearLicense() {
cachedLicense = null;
if (typeof window !== "undefined" && window.localStorage) {
window.localStorage.removeItem("yogiway_license");
}
else if (typeof require !== "undefined") {
try {
const fs = require("fs");
const path = require("path");
const os = require("os");
const licensePath = path.join(os.homedir(), ".yogiway", "license.json");
if (fs.existsSync(licensePath)) {
fs.unlinkSync(licensePath);
}
}
catch {
// Ignore errors
}
}
}
//# sourceMappingURL=license.js.map