@codeo-engineering/licenserail 0.0.0-stage → 0.1.6

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Codeo Solutions Limited
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,69 @@
1
- # Temporary Holding Version
1
+ # LicenseRail (TypeScript / Node / Electron)
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Client SDK for [LicenseRail](https://licenserail.com) licensing. MIT.
4
+
5
+ ```sh
6
+ npm install @codeo-engineering/licenserail
7
+ ```
8
+
9
+ ```ts
10
+ import { LicenseClient } from '@codeo-engineering/licenserail'
11
+
12
+ const client = new LicenseClient({ serverUrl: 'https://api.licenserail.com', vendor: 'ten_6f1a7e2c3b4d4e5f8a9b0c1d2e3f4a5b', product: 'my-app', publicKeyPem, appVersion: '1.0.0', keyId })
13
+ const result = await client.check() // offline check, refreshed online when due
14
+ if (!result.valid) console.log((await client.activate(licenseKey)).message)
15
+ const canExport = client.license?.hasFeature('export')
16
+ // await client.deactivate() frees the seat
17
+ ```
18
+
19
+ - `check()` on every start: offline verification; online refresh when the token is due, or when only the server can clear the
20
+ state (expired, suspended, offline too long, clock problems).
21
+ - `activate(key)` on the license screen; floating licenses are checked out and kept alive automatically where the platform allows.
22
+ - `deactivate()` frees the seat (needs internet).
23
+ - `startTrial()` starts this device's keyless trial (needs the `keyId` option, the environment's public signing key id) or gets
24
+ the running one back. The trial token is stored and checked like a license (`license.isTrial`, true only when the token says
25
+ `trial: true`); it is never refreshed. A keyed license of kind `trial` is an ordinary license: it refreshes and deactivates.
26
+ - `borrow(hours)` keeps a checked-out floating seat for offline use (up to the license's limit). Without a license it returns
27
+ `no_license`, for a license that isn't a checked-out floating one `borrow_not_allowed`, both without a request.
28
+ - `returnBorrow()` releases the seat of a floating license early. On a node-locked license or a keyless trial it returns
29
+ `borrow_not_allowed` without a request, so it never uses up a deactivation.
30
+ - `consume(count = 1)` counts uses of a license with a use limit: `{ success, uses, maxUses }`, or `errorCode: 'use_limit_reached'`.
31
+ A count below 1 gives `bad_request` and no license `no_license`, without a request. Refresh, borrow and consume remove the
32
+ local license only on the revoking codes below or `lease_expired`; any other refusal keeps it.
33
+ - Keyless trials (`trial: true`) are never sent to the server: `refresh()` leaves the trial as it is, and `consume()` /
34
+ `borrow()` return `bad_request` without a request.
35
+ - `isVirtualMachineAsync()` (and the exported `detectVirtualMachine()`): best-effort VM/container detection without admin
36
+ rights, with the same narrow rules in every LicenseRail SDK: hypervisor names (VMware, VirtualBox, QEMU, KVM, Xen, Parallels,
37
+ ..., and a Microsoft "Virtual Machine") in Win32_ComputerSystem or Linux DMI, the Linux `hypervisor` CPU flag, containers,
38
+ macOS `kern.hv_vmm_present`. Cloud vendor names, Hyper-V/VBS hosts and WSL are not virtual. The probe never blocks: it runs
39
+ once per process as a hidden child process, is killed after 5 seconds (then `virtualMachine` is not sent), and requests await
40
+ the cached answer. Sent with activations, floating checkouts and trials; override with
41
+ `virtualMachine: true | false | () => boolean | Promise<boolean>`, or `detectVirtualMachine: false` to not send it. The
42
+ synchronous `isVirtualMachine()` still works but blocks until the probe ends the first time. The probe never runs at import
43
+ or in the constructor: it starts at the first activate, checkout or trial (which waits for it, at most 5 seconds), or earlier
44
+ in the background when you call `client.warmUpVirtualMachineDetection()` (returns at once; once per process).
45
+ - Every request carries the device clock (`clientTime`); the server answers `clock_offset` when it is far off.
46
+ - Offline checks apply the token's `matching` (`default`, `strict`, `loose`) and `maxAppVersion` (status
47
+ `AppVersionNotAllowed` when `appVersion` is newer; versions compare like the server, `test-vectors/versions.json`). `license` also shows `isExpired` (expired, but its strategy keeps it
48
+ working), `isInGrace` / `graceUntil`, `borrowedUntil`, `maxUses` / `uses`, `isOverage` and `matching`.
49
+ - `license.licenseExpiresAt` is the license's own expiry (null = perpetual), for showing to the user; it is never enforced.
50
+ `expiresAt` is what the offline check enforces: the end of grace, or null when the expiration strategy keeps the license
51
+ working after expiry.
52
+ - `ERROR_CODES` lists the server's codes, including `deactivation_limit_reached`, `device_banned`, `license_locked`,
53
+ `clock_offset`, `app_version_not_allowed`, `location_not_allowed`, `borrow_not_allowed`, `seat_cooling_down`,
54
+ `use_limit_reached`, `virtual_machine_not_allowed` and `ip_blocked`; `errorCode` and the server's `message` come back as is.
55
+ - On `license_not_found`, `license_suspended`, `license_revoked`, `license_expired`, `activation_revoked`, `wrong_machine` and
56
+ `invalid_token` the local license is removed; other errors (network, `402`) keep it.
57
+ - Get the public key with `licenserail public-key --pem` or `licenserail init typescript`.
58
+
59
+ ## Changes
60
+
61
+ - 2026-12-01: additions only, existing calls keep working. New: the `AppVersionNotAllowed` status (an added member of
62
+ `LicenseStatus`: a `Record<LicenseStatus, ...>` or an exhaustive `switch` needs an entry for it), `ERROR_CODES.noLicense`,
63
+ `returnBorrow()`, `isVirtualMachineAsync()`, `detectVirtualMachine()`, `isWsl()`, `linuxIsVirtual()`,
64
+ `parseWindowsComputerSystem()`. Changed behaviour: `license.isTrial` is true only for keyless trials (`trial: true`), borrow
65
+ and consume without a license return `no_license`, consume removes the license on a revoking code, VM detection no longer
66
+ blocks the event loop, cloud vendor names no longer count as virtual machines, WSL is not virtual, and versions with a
67
+ 10-digit part up to 2147483647 compare like the server.
68
+
69
+ Source: [licenserail-sdks](https://github.com/codeo-engineering/licenserail-sdks) (`typescript/`). Tests (shared vectors in `../test-vectors`): `npm test`.
@@ -0,0 +1,192 @@
1
+ import { type Fingerprint, type LicensePayload, type LicenseStatus } from './core.js';
2
+ export interface LicenseClientOptions {
3
+ /** Base URL of the licensing service, e.g. https://api.licenserail.com */
4
+ serverUrl: string;
5
+ /** The product code from the dashboard. */
6
+ product: string;
7
+ /** Your organization ID from the dashboard (ten_...; part of every device identity). */
8
+ vendor: string;
9
+ /** The environment's public key (PEM); verifies license files offline. */
10
+ publicKeyPem: string;
11
+ /** The environment's signing key id (public, shown with the public key); needed only for startTrial(). */
12
+ keyId?: string;
13
+ /** Where the license file and run history live; default: a per-user folder. */
14
+ storageDir?: string;
15
+ /** The app's version: sent to the server and checked offline against the license's maxAppVersion. */
16
+ appVersion?: string;
17
+ /** Refresh online on every check() (default false: only when the token says it is due). */
18
+ refreshOnStartup?: boolean;
19
+ /** Request timeout in milliseconds (default 10 s). */
20
+ timeoutMs?: number;
21
+ /** Overrides device fingerprinting (tests, custom hardware ids). */
22
+ fingerprint?: () => Fingerprint;
23
+ /** Overrides the clock (tests). */
24
+ now?: () => Date;
25
+ /** Overrides fetch (tests, proxies). */
26
+ fetch?: typeof fetch;
27
+ machineName?: string;
28
+ /** Overrides virtual machine detection: a fixed answer, or your own detector (it may return a promise; null = not detected). */
29
+ virtualMachine?: boolean | (() => boolean | null | Promise<boolean | null>);
30
+ /** Detect virtual machines and containers and send the result (default true). false: `virtualMachine` is not sent. */
31
+ detectVirtualMachine?: boolean;
32
+ }
33
+ export interface ActivationResult {
34
+ success: boolean;
35
+ errorCode?: string;
36
+ message: string;
37
+ }
38
+ /** Result of consume(): the license's use counter after the use, or why it was refused. */
39
+ export interface UseResult {
40
+ success: boolean;
41
+ uses?: number;
42
+ maxUses?: number;
43
+ errorCode?: string;
44
+ message: string;
45
+ }
46
+ export interface CheckResult {
47
+ status: LicenseStatus;
48
+ valid: boolean;
49
+ license: License | null;
50
+ }
51
+ /** Read-only view of the current license. */
52
+ export declare class License {
53
+ readonly payload: LicensePayload;
54
+ constructor(payload: LicensePayload);
55
+ get key(): string;
56
+ get customer(): string;
57
+ get kind(): string;
58
+ /**
59
+ * What the offline check enforces: the end of grace when the license has grace days; null when perpetual or when the expiration
60
+ * strategy keeps the license working after expiry. Show licenseExpiresAt to the user instead.
61
+ */
62
+ get expiresAt(): Date | null;
63
+ /** The license's own expiry, for showing to the user (null = perpetual); never enforced. */
64
+ get licenseExpiresAt(): Date | null;
65
+ get offlineUntil(): Date;
66
+ get isTest(): boolean;
67
+ get isFloating(): boolean;
68
+ get features(): string[];
69
+ get metadata(): Record<string, unknown>;
70
+ hasFeature(code: string): boolean;
71
+ /**
72
+ * A keyless trial (startTrial()): the token says `trial: true`. A keyed license of kind "trial" is an ordinary license (see
73
+ * `kind`): it refreshes and deactivates on the server.
74
+ */
75
+ get isTrial(): boolean;
76
+ /** Device matching applied offline: 'default', 'strict' or 'loose'. */
77
+ get matching(): string;
78
+ /** The newest app version this license may run; null = any. */
79
+ get maxAppVersion(): string | null;
80
+ /** The license has expired but keeps working (expiration strategy maintain or allow); show a renewal notice. */
81
+ get isExpired(): boolean;
82
+ /** The license expired and works until graceUntil (grace period). */
83
+ get isInGrace(): boolean;
84
+ get graceUntil(): Date | null;
85
+ /** Floating: the seat is borrowed for offline use until this time. */
86
+ get borrowedUntil(): Date | null;
87
+ get isBorrowed(): boolean;
88
+ /** Use counting: the license's limit, and the uses when the token was issued. */
89
+ get maxUses(): number | null;
90
+ get uses(): number | null;
91
+ /** More devices are active than the license's seats (overdraft). */
92
+ get isOverage(): boolean;
93
+ }
94
+ /**
95
+ * The licensing client for a vendor's app: offline verification on every start, online refresh when due, activation,
96
+ * deactivation, keyless trials, floating leases (with borrowing) and use counting. Never put an API key in client code; this
97
+ * class needs only the public key.
98
+ */
99
+ export declare class LicenseClient {
100
+ private readonly options;
101
+ private readonly key;
102
+ private readonly dir;
103
+ private fp;
104
+ private heartbeat;
105
+ private warnedTest;
106
+ /** The last message from the server (e.g. why a license was removed). */
107
+ lastServerMessage: string | null;
108
+ constructor(options: LicenseClientOptions);
109
+ private now;
110
+ /** The device clock (UTC, ISO 8601) sent as `clientTime`; the server refuses requests whose clock is too far off. */
111
+ private clientTime;
112
+ private fingerprint;
113
+ /** Whether a license for this device would match it again; if not, activating would only use up a seat. */
114
+ private identified;
115
+ private get tokenPath();
116
+ private get historyPath();
117
+ /**
118
+ * What requests carry as `virtualMachine`: null (not sent) when detection is off (and no answer is set) or couldn't tell. The
119
+ * probe runs once per process without blocking (a child process with a 5-second limit); requests await the cached result.
120
+ */
121
+ private virtualMachineField;
122
+ /**
123
+ * Starts virtual machine detection in the background and returns at once, so the first activate, checkout or trial doesn't
124
+ * wait for it. Optional: without it the probe starts at the first activate, checkout or trial (which then waits for it, at
125
+ * most the 5-second probe limit). It never runs at app start or in the constructor. Runs once per process; does nothing when
126
+ * detection is off or the `virtualMachine` option gives the answer.
127
+ */
128
+ warmUpVirtualMachineDetection(): void;
129
+ /**
130
+ * Whether the app runs in a virtual machine or container (best effort): the `virtualMachine` option if set, otherwise
131
+ * detected (false when detection is off or failed). Sent with activations, floating checkouts and trials.
132
+ */
133
+ isVirtualMachineAsync(): Promise<boolean>;
134
+ /**
135
+ * Synchronous form of isVirtualMachineAsync(). Never starts a second probe: returns the cached result once detection has
136
+ * finished, and false while the asynchronous probe is still running. Blocks only before the first detection has started (up
137
+ * to 5 seconds, once per process). A custom detector that returns a promise counts as false here.
138
+ * @deprecated Use isVirtualMachineAsync().
139
+ */
140
+ isVirtualMachine(): boolean;
141
+ /** The current license after the last check, or null. */
142
+ get license(): License | null;
143
+ /** Offline evaluation only (no network). */
144
+ evaluateLocal(): CheckResult;
145
+ /** Call on startup: evaluates offline and refreshes online when due or when only the server can clear the state. */
146
+ check(): Promise<CheckResult>;
147
+ private activationBody;
148
+ /** Activates `licenseKey` on this device (or checks out a floating lease if the license is floating). */
149
+ activate(licenseKey: string, machineName?: string): Promise<ActivationResult>;
150
+ /** Checks out a floating seat and keeps it alive with heartbeats until release(). */
151
+ checkout(licenseKey: string): Promise<ActivationResult>;
152
+ /**
153
+ * Starts this device's keyless trial, or gets the running one back (the server keeps one trial per device and product). Needs
154
+ * the `keyId` option. The trial token is stored and checked like a license (kind "trial"); it is never refreshed.
155
+ */
156
+ startTrial(): Promise<ActivationResult>;
157
+ /**
158
+ * Floating: keeps this device's seat for offline use for `hours` (up to the license's limit). Return it early with
159
+ * returnBorrow() (the same as release()). Refused without a request with `no_license` when there is no license and
160
+ * `borrow_not_allowed` when it isn't a checked-out floating license; a keyless trial gets `bad_request` (it is never sent to the
161
+ * server). The license file is removed only on a revoking code or lease_expired; other refusals keep it.
162
+ */
163
+ borrow(hours: number): Promise<ActivationResult>;
164
+ /**
165
+ * Licenses with a use limit: consumes `count` uses on the server; refused with use_limit_reached when they are used up. A count
166
+ * below 1 is refused with `bad_request` and no license with `no_license`, both without a request; so is a keyless trial
167
+ * (`bad_request`: it is never sent to the server). The license file is removed only on a revoking code or lease_expired; any
168
+ * other refusal keeps it.
169
+ */
170
+ consume(count?: number): Promise<UseResult>;
171
+ /** Releases a floating seat (also returns a borrowed seat early). */
172
+ release(): Promise<ActivationResult>;
173
+ /**
174
+ * Returns a borrowed floating seat early (releases the seat of a floating license). On a node-locked license or a keyless trial
175
+ * it returns `borrow_not_allowed` without a request, so it can never use up a deactivation. Without a license it succeeds.
176
+ */
177
+ returnBorrow(): Promise<ActivationResult>;
178
+ /** Removes the license from this device and frees its seat on the server. */
179
+ deactivate(): Promise<ActivationResult>;
180
+ /** Asks the server for a fresh token now (floating licenses: a heartbeat). A keyless trial is never sent: it stays as it is. */
181
+ refresh(anomalies?: string[]): Promise<void>;
182
+ /** A user-facing sentence for a status. */
183
+ message(status: LicenseStatus): string;
184
+ private startHeartbeat;
185
+ private stopHeartbeat;
186
+ private post;
187
+ private saveIfValid;
188
+ private readPayload;
189
+ private clear;
190
+ private read;
191
+ private write;
192
+ }