-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathlicenseKeyValidator.ts
More file actions
100 lines (82 loc) · 3.06 KB
/
licenseKeyValidator.ts
File metadata and controls
100 lines (82 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* @license
* Copyright (c) 2025 Handsoncode. All rights reserved.
*/
import {checkKeySchema, extractTime} from './licenseKeyHelper'
/**
* The list of all available states which the license checker can return.
*/
export const enum LicenseKeyValidityState {
VALID = 'valid',
INVALID = 'invalid',
EXPIRED = 'expired',
MISSING = 'missing'
}
type LicenseKeyInvalidState = Exclude<LicenseKeyValidityState, LicenseKeyValidityState.VALID>
interface TemplateVars {
[key: string]: string,
}
type ConsoleMessages = {
[key in LicenseKeyInvalidState]: (templateVars: TemplateVars) => string
}
type MessageDescriptor = {
template: LicenseKeyValidityState,
vars: TemplateVars,
}
/**
* List of all not valid messages which may occur.
*/
const consoleMessages: ConsoleMessages = {
invalid: () => 'The license key for HyperFormula is invalid.',
expired: ({keyValidityDate}) => 'The license key for HyperFormula expired' +
` on ${keyValidityDate}, and is not valid for the installed version.`,
missing: () => 'The license key for HyperFormula is missing.',
}
let _notified = false
/**
* Checks if the provided license key is grammatically valid or not expired.
*
* @param {string} licenseKey The license key to check.
* @returns {LicenseKeyValidityState} Returns the checking state.
*/
export function checkLicenseKeyValidity(licenseKey: string): LicenseKeyValidityState {
const messageDescriptor: MessageDescriptor = {
template: LicenseKeyValidityState.MISSING,
vars: {},
}
if (licenseKey === 'gpl-v3' || licenseKey === 'internal-use-in-handsontable' || licenseKey === 'hftrial-0168e-1f2b7-47158-70b05-0842f') {
messageDescriptor.template = LicenseKeyValidityState.VALID
} else if (typeof licenseKey === 'string' && checkKeySchema(licenseKey)) {
const [day, month, year] = (process.env.HT_RELEASE_DATE || '').split('/')
const releaseDays = Math.floor(new Date(`${month}/${day}/${year}`).getTime() / 8.64e7)
const keyValidityDays = extractTime(licenseKey)
messageDescriptor.vars.keyValidityDate = formatDate(new Date((keyValidityDays + 1) * 8.64e7))
if (releaseDays > keyValidityDays) {
messageDescriptor.template = LicenseKeyValidityState.EXPIRED
} else {
messageDescriptor.template = LicenseKeyValidityState.VALID
}
} else if (licenseKey !== '') {
messageDescriptor.template = LicenseKeyValidityState.INVALID
}
if (!_notified && messageDescriptor.template !== LicenseKeyValidityState.VALID) {
console.warn(consoleMessages[messageDescriptor.template](messageDescriptor.vars))
_notified = true
}
return messageDescriptor.template
}
/**
* Formats a Date instance to hard-coded format MMMM DD, YYYY.
*
* @param {Date} date The date to format.
* @returns {string}
*/
function formatDate(date: Date): string {
const monthNames = ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'
]
const month = monthNames[date.getMonth()]
const day = date.getDate()
const year = date.getFullYear()
return `${month} ${day}, ${year}`
}