lib/request/agent.js

1.
const ua_parser = require("ua-parser-js");
2.

			
3.
/**
4.
 * This is the request.agent class. It contains information about the client by parsing the 
5.
 * user-agent. This is class is a wrapper of faisalman's ua-parser-js package.
6.
 * @class request.agent
7.
 * @nofunction
8.
 */
9.
module.exports = class agent {
10.
	constructor(request) {
11.
		this._agent = ua_parser(request.header("user-agent"));
12.
		this._browser = new browser(this._agent.browser);
13.
		this._device = new device(this._agent.device);
14.
		this._engine = new engine(this._agent.engine);
15.
		this._os = new os(this._agent.os);
16.
		this._cpu = new cpu(this._agent.cpu);
17.
	}
18.

			
19.
	/**
20.
	 * This function will return the full User-Agent header the client send. The User-Agent header 
21.
	 * contains information about the system the client is using and can therefore be used to get 
22.
	 * some information about the client. Please note that the client is able to fake his/her 
23.
	 * User-Agent, so this information shouldn't be depended on.
24.
     * @summary Returns the full User-Agent string.
25.
     * @return {String} The full User-Agent string.
26.
     * @function string
27.
	 */
28.
	string() {
29.
		return this._agent.ua;
30.
	}
31.

			
32.
	/**
33.
	 * This function returns the browser class. This class contains information about the browser 
34.
	 * the client was using when sending the request.
35.
	 * @summary Returns the browser class.
36.
	 * @return {Class} The browser class.
37.
	 * @function browser
38.
	 */
39.
	browser() {
40.
		return this._browser;
41.
	}
42.

			
43.
	/**
44.
	 * This function returns the device class. This class contains information about the device the
45.
	 * client was using when sending the request.
46.
	 * @summary Returns the device class.
47.
	 * @return {Class} The device class.
48.
	 * @function device
49.
	 */
50.
	device() {
51.
		return this._device;
52.
	}
53.

			
54.
	/**
55.
	 * This function returns the engine class. This class contains information about the engine the
56.
	 * client was using when sending the request.
57.
	 * @summary Returns the engine class.
58.
	 * @return {Class} The engine class.
59.
	 * @function engine
60.
	 */
61.
	engine() {
62.
		return this._engine;
63.
	}
64.

			
65.
	/**
66.
	 * This function returns the os class. This class contains information about the os the client
67.
	 * was using when sending the request.
68.
	 * @summary Returns the os class.
69.
	 * @return {Class} The os class.
70.
	 * @function os
71.
	 */
72.
	os() {
73.
		return this._os;
74.
	}
75.

			
76.
	/**
77.
	 * This function returns the cpu class. This class contains information about the cpu the
78.
	 * client was using when sending the request.
79.
	 * @summary Returns the cpu class.
80.
	 * @return {Class} The cpu class.
81.
	 * @function cpu
82.
	 */
83.
	cpu() {
84.
		return this._cpu;
85.
	}
86.
}
87.

			
88.
/**
89.
 * This is the browser class. This class contains information about the browser. It is parsed from
90.
 * the User-Agent header that was send by the client.
91.
 * @class request.agent.browser
92.
 * @nofunction
93.
 */
94.
class browser {
95.
	constructor(browser) {
96.
		this._browser = browser;
97.
		this._is = new is(this.name());
98.
	}
99.

			
100.
	/**
101.
	 * This function returns the name of the browser. When the name cannot be parsed from the 
102.
	 * User-Agent it will return undefined. The returned name will always be lowercased.
103.
	 * @summary Returns the name of the browser.
104.
	 * @return {String} The name of the browser.
105.
	 * @function name
106.
	 */
107.
	name() {
108.
		if(this._browser.name === undefined) return;
109.

			
110.
		return this._browser.name.toLowerCase();
111.
	}
112.

			
113.
	/**
114.
	 * This function returns the version of the browser. 
115.
	 * @summary Returns the version of the browser.
116.
	 * @return {String} The version of the browser.
117.
	 * @function version
118.
	 */
119.
	version() {
120.
		return this._browser.version;
121.
	}
122.

			
123.
	/**
124.
	 * This function returns the is class. With this class you can check if the client is using a 
125.
	 * certain browser.
126.
	 * @summary Returns the is class.
127.
	 * @return {Class} The is class.
128.
	 * @function is
129.
	 */
130.
	is() {
131.
		return this._is;
132.
	}
133.
}
134.

			
135.
/**
136.
 * This is the is class. With this class you can check if the client is using the specified 
137.
 * browser.
138.
 * @class request.agent.browser.is
139.
 * @nofunction
140.
 */
141.
class is {
142.
	constructor(name) {
143.
		this._name = name;
144.
	}
145.

			
146.
	/**
147.
	 * With this function you can check if the user is using android browser as browser. It matches
148.
	 * against "Android Browser".
149.
	 * @summary Returns if the user is using android browser.
150.
	 * @return {Boolean} If the user is using android browser.
151.
	 * @function android
152.
	 */
153.
	android() {
154.
		return this._name === "android browser";
155.
	}
156.

			
157.
	/**
158.
	 * With this function you can check if the user is using chrome as browser. It matches against
159.
	 * "Chrome" and "Chrome Webview".
160.
	 * @summary Returns if the user is using chome.
161.
	 * @return {Boolean} If the user is using chrome.
162.
	 * @function chrome
163.
	 */
164.
	chrome() {
165.
		return this._name === "chrome" || this._name === "chrome webview";
166.
	}
167.

			
168.
	/**
169.
	 * With this function you can check if the user is using opera as browser. It matches against
170.
	 * "Opera", "Opera Mini", "Opera Mobi" and "Opera Tablet".
171.
	 * @summary Returns if the user is using opera.
172.
	 * @return {Boolean} If the user is using opera.
173.
	 * @function opera
174.
	 */
175.
	opera() {
176.
		return this._name === "opera" ||  this._name === "opera mini" 
177.
		|| this._name === "opera mobi" || this._name === "opera tablet";
178.
	}
179.

			
180.
	/**
181.
	 * With this function you can check if the user is using firefox as browser. It matches against 
182.
	 * "Firefox".
183.
	 * @summary Returns if the user is using firefox.
184.
	 * @return {Boolean} If the user is using firefox.
185.
	 * @function firefox
186.
	 */
187.
	firefox() {
188.
		return this._name === "firefox";
189.
	}
190.

			
191.
	/**
192.
	 * With this function you can check if the user is using safari as browser. It matches against 
193.
	 * "Safari" and "Mobile Safari".
194.
	 * @summary Returns if the user is using safari.
195.
	 * @return {Boolean} If the user is using safari.
196.
	 * @function safari
197.
	 */
198.
	safari() {
199.
		return this._name === "safari" || this._name === "mobile safari";
200.
	}
201.

			
202.
	/**
203.
	 * With this function you can check if the user is using ie as browser. It matches against
204.
	 * "IE" and "IEMobile".
205.
	 * @summary Returns if the user is using ie.
206.
	 * @return {Boolean} If the user is using ie.
207.
	 * @function ie
208.
	 */
209.
	ie() {
210.
		return this._name === "ie" || this._name === "iemobile";
211.
	}
212.

			
213.
	/**
214.
	 * With this function you can check if the user is using edge as browser. It matches against 
215.
	 * "Edge".
216.
	 * @summary Returns if the user is using edge.
217.
	 * @return {Boolean} If the user is using edge.
218.
	 * @function edge
219.
	 */
220.
	edge() {
221.
		return this._name === "edge";
222.
	}
223.
}
224.

			
225.
/**
226.
 * This is the device class. This class contains information about the device. It is parsed from
227.
 * the User-Agent header that was send by the client.
228.
 * @class request.agent.device
229.
 * @nofunction
230.
 */
231.
class device {
232.
	constructor(device) {
233.
		this._device = device;
234.
	}
235.

			
236.
	/**
237.
	 * This function returns the type of the device. Often the type cannot be parsed and it will 
238.
	 * return undefined.
239.
	 * @summary Returns the type of the device.
240.
	 * @return {String} The type of the device.
241.
	 * @function type
242.
	 */
243.
	type() {
244.
		return this._device.type;
245.
	}
246.

			
247.
	/**
248.
	 * This function returns the vendor of the device. The vendor is the company that last sold the
249.
	 * device. If the vendor cannot be parsed it will return undefined. The vendor is returned in 
250.
	 * lowercase.
251.
	 * @summary Returns the vendor of the device.
252.
	 * @return {String} The vendor of the device.
253.
	 * @function vendor
254.
	 */
255.
	vendor() {
256.
		if(this._device.vendor === undefined) return;
257.

			
258.
		return this._device.vendor.toLowerCase();
259.
	}
260.

			
261.
	/**
262.
	 * This function returns the model of the device. Often the model cannot be parsed and it will 
263.
	 * return undefined.
264.
	 * @summary Returns the model of the device.
265.
	 * @return {String} The model of the device.
266.
	 * @function model
267.
	 */
268.
	model() {
269.
		return this._device.model;
270.
	}
271.
}
272.

			
273.
/**
274.
 * This is the engine class. This class contains information about the engine. It is parsed from
275.
 * the User-Agent header that was send by the client.
276.
 * @class request.agent.engine
277.
 * @nofunction
278.
 */
279.
class engine {
280.
	constructor(engine) {
281.
		this._engine = engine;
282.
	}
283.

			
284.
	/**
285.
	 * This function returns the name of the engine. If the name cannot be parsed it will return 
286.
	 * undefined. The name is returned in lowercase.
287.
	 * @summary Returns the name of the engine.
288.
	 * @return {String} The name of the engine.
289.
	 * @function name
290.
	 */
291.
	name() {
292.
		if(this._engine.name === undefined) return;
293.

			
294.
		return this._engine.name.toLowerCase();
295.
	}
296.

			
297.
	/**
298.
	 * This function returns the version of the engine. Often the version cannot be parsed and it will 
299.
	 * return undefined.
300.
	 * @summary Returns the version of the engine.
301.
	 * @return {String} The version of the engine.
302.
	 * @function version
303.
	 */
304.
	version() {
305.
		return this._engine.version;
306.
	}
307.
}
308.

			
309.
/**
310.
 * This is the engine class. This class contains information about the engine. It is parsed from
311.
 * the User-Agent header that was send by the client.
312.
 * @class request.agent.os
313.
 * @nofunction
314.
 */
315.
class os {
316.
	constructor(os) {
317.
		this._os = os;
318.
	}
319.

			
320.
	/**
321.
	 * This function returns the name of the operating system the client is using. If the name 
322.
	 * cannot be parsed it will return undefined and the name is returned in lowercase.
323.
	 * @summary Returns the name of the os.
324.
	 * @return {String} The name of the os.
325.
	 * @function name
326.
	 */
327.
	name() {
328.
		if(this._os.name === undefined) return
329.

			
330.
		return this._os.name.toLowerCase();
331.
	}
332.

			
333.
	/**
334.
	 * This function returns the version of the operating system the client is using. If the 
335.
	 * version cannot be parsed and it will return undefined.
336.
	 * @summary Returns the version of the os.
337.
	 * @return {String} The version of the os.
338.
	 * @function version
339.
	 */
340.
	version() {
341.
		return this._os.version;
342.
	}
343.
}
344.

			
345.
/**
346.
 * This is the cpu class. This class contains information about the central processing unit the 
347.
 * client is using. It is parsed from the User-Agent header that was send by the client.
348.
 * @class request.agent.cpu
349.
 * @nofunction
350.
 */
351.
class cpu {
352.
	constructor(cpu) {
353.
		this._cpu = cpu;
354.
	}
355.

			
356.
	/**
357.
	 * This function returns the architecture of the cpu the client is using.
358.
	 * @summary Returns the architecture of the cpu.
359.
	 * @return {String} The architecture of the cpu.
360.
	 * @function arch
361.
	 */
362.
	arch() {
363.
		return this._cpu.architecture;
364.
	}
365.
}
366.