@@ -19,11 +19,28 @@ import { MissingArgumentError } from '../errors.ts'
19
19
import os from 'node:os'
20
20
import process from 'node:process'
21
21
22
+ /**
23
+ * A representation of a leaseholder who is identified by a username, hostname, and process id (PID). This implementation
24
+ * is serializable to/from a JSON object and is comparable to other leaseholders.
25
+ */
22
26
export class LeaseHolder {
27
+ /** The user's identity which is typically the OS login username. */
23
28
private readonly _username : string
29
+
30
+ /** The machine's identity which is typically the hostname. */
24
31
private readonly _hostname : string
32
+
33
+ /** The process identifier which is typically the OS PID. */
25
34
private readonly _processId : number
26
35
36
+ /**
37
+ * Constructs a new leaseholder with the given username, hostname, and process id. This constructor is private and
38
+ * should not be called directly. Use the static factory methods to create a new instance.
39
+ *
40
+ * @param username - the user's identity.
41
+ * @param hostname - the machine's identity.
42
+ * @param processId - the process identifier.
43
+ */
27
44
private constructor ( username : string , hostname : string , processId : number ) {
28
45
if ( ! username ) throw new MissingArgumentError ( 'username is required' )
29
46
if ( ! hostname ) throw new MissingArgumentError ( 'hostname is required' )
@@ -34,26 +51,54 @@ export class LeaseHolder {
34
51
this . _processId = processId
35
52
}
36
53
54
+ /**
55
+ * Creates a new leaseholder with the given username. The hostname is set to the current machine's hostname and the
56
+ * process id is set to the current process's PID.
57
+ * @param username - the user's identity.
58
+ * @returns a new leaseholder instance.
59
+ */
37
60
public static of ( username : string ) : LeaseHolder {
38
61
return new LeaseHolder ( username , os . hostname ( ) , process . pid )
39
62
}
40
63
64
+ /**
65
+ * Creates a new leaseholder by retrieving the current user's identity, the current machine's hostname, and the
66
+ * current process's PID.
67
+ * @returns a new leaseholder instance.
68
+ */
41
69
public static default ( ) : LeaseHolder {
42
70
return LeaseHolder . of ( os . userInfo ( ) . username )
43
71
}
44
72
73
+ /**
74
+ * The user's identity which is typically the OS login username.
75
+ * @returns the user's identity.
76
+ */
45
77
public get username ( ) : string {
46
78
return this . _username
47
79
}
48
80
81
+
82
+ /**
83
+ * The machine's identity which is typically the hostname.
84
+ * @returns the machine's identity.
85
+ */
49
86
public get hostname ( ) : string {
50
87
return this . _hostname
51
88
}
52
89
90
+ /**
91
+ * The process identifier which is typically the OS PID.
92
+ * @returns the process identifier.
93
+ */
53
94
public get processId ( ) : number {
54
95
return this . _processId
55
96
}
56
97
98
+ /**
99
+ * Returns a plain object representation of this leaseholder. This object may be serialized to JSON.
100
+ * @returns a plain object representation of this leaseholder.
101
+ */
57
102
public toObject ( ) : any {
58
103
return {
59
104
username : this . _username ,
@@ -62,14 +107,31 @@ export class LeaseHolder {
62
107
}
63
108
}
64
109
110
+ /**
111
+ * Compares this leaseholder to another leaseholder to determine if they are equal. Two leaseholders are equal if
112
+ * their username, hostname, and process id are the same.
113
+ * @param other - the other leaseholder to compare.
114
+ * @returns true if the leaseholders are equal; false otherwise.
115
+ */
65
116
public equals ( other : LeaseHolder ) : boolean {
66
117
return this . username === other . username && this . hostname === other . hostname && this . processId === other . processId
67
118
}
68
119
120
+ /**
121
+ * Compares this leaseholder to another leaseholder to determine if they are the same machine. Two leaseholders are
122
+ * the same machine if their username and hostname are the same.
123
+ * @param other - the other leaseholder to compare.
124
+ * @returns true if the leaseholders are the same machine; false otherwise.
125
+ */
69
126
public isSameMachineIdentity ( other : LeaseHolder ) : boolean {
70
127
return this . username === other . username && this . hostname === other . hostname
71
128
}
72
129
130
+ /**
131
+ * Determines if the process associated with this leaseholder is still alive. This method will return false if the
132
+ * process is not alive or an error occurs while checking the process status.
133
+ * @returns true if the process is alive; false otherwise.
134
+ */
73
135
public isProcessAlive ( ) : boolean {
74
136
try {
75
137
return process . kill ( this . processId , 0 )
@@ -78,10 +140,19 @@ export class LeaseHolder {
78
140
}
79
141
}
80
142
143
+ /**
144
+ * Serializes this leaseholder to a JSON string representation.
145
+ * @returns a JSON string representation of this leaseholder.
146
+ */
81
147
public toJson ( ) : string {
82
148
return JSON . stringify ( this . toObject ( ) )
83
149
}
84
150
151
+ /**
152
+ * Deserializes a JSON string representation of a leaseholder into a new leaseholder instance.
153
+ * @param json - the JSON string representation of a leaseholder.
154
+ * @returns a new leaseholder instance.
155
+ */
85
156
public static fromJson ( json : string ) : LeaseHolder {
86
157
const obj : ReturnType < LeaseHolder [ 'toObject' ] > = JSON . parse ( json )
87
158
return new LeaseHolder ( obj . username , obj . hostname , obj . pid )
0 commit comments