edge.libx.js 0.0.7 → 0.1.0
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.
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { IRequest } from 'itty-router';
|
|
2
|
+
export interface IRequestInfo {
|
|
3
|
+
deviceType: any;
|
|
4
|
+
clientIP: any;
|
|
5
|
+
ip: any;
|
|
6
|
+
country: any;
|
|
7
|
+
region: any;
|
|
8
|
+
city: any;
|
|
9
|
+
'cf.tlsClientAuth': any;
|
|
10
|
+
'cf.tlsExportedAuthenticator': any;
|
|
11
|
+
'device.vendor': any;
|
|
12
|
+
'device.model': any;
|
|
13
|
+
'device.type': any;
|
|
14
|
+
'browser.name': any;
|
|
15
|
+
'browser.version': any;
|
|
16
|
+
'browser.major': any;
|
|
17
|
+
'engine.name': any;
|
|
18
|
+
'engine.version': any;
|
|
19
|
+
'os.name': any;
|
|
20
|
+
'os.version': any;
|
|
21
|
+
'cpu.architecture': any;
|
|
22
|
+
}
|
|
23
|
+
export declare class RequestInfo {
|
|
24
|
+
options?: Partial<ModuleOptions>;
|
|
25
|
+
constructor(options?: Partial<ModuleOptions>);
|
|
26
|
+
static process(request: IRequest): Promise<IRequestInfo>;
|
|
27
|
+
private static getDeviceType;
|
|
28
|
+
}
|
|
29
|
+
export declare class ModuleOptions {
|
|
30
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.ModuleOptions = exports.RequestInfo = void 0;
|
|
16
|
+
const edge_1 = require("@vercel/edge");
|
|
17
|
+
const essentials_js_1 = require("libx.js/build/bundles/essentials.js");
|
|
18
|
+
const ua_parser_js_1 = __importDefault(require("ua-parser-js"));
|
|
19
|
+
class RequestInfo {
|
|
20
|
+
constructor(options) {
|
|
21
|
+
this.options = options;
|
|
22
|
+
essentials_js_1.libx.log.v('RequestInfo:ctor: ');
|
|
23
|
+
this.options = Object.assign(Object.assign({}, new ModuleOptions()), options);
|
|
24
|
+
}
|
|
25
|
+
static process(request) {
|
|
26
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
27
|
+
const reqCf = Object.assign({}, request.cf);
|
|
28
|
+
const clientIP = request.headers.get('CF-Connecting-IP');
|
|
29
|
+
const device = request.headers.get('CF-Device-Type');
|
|
30
|
+
const ip = request.headers.get('x-real-ip');
|
|
31
|
+
const country = request.headers.get('X-Vercel-IP-Country');
|
|
32
|
+
const region = request.headers.get('X-Vercel-IP-Country-Region');
|
|
33
|
+
let city = request.headers.get('X-Vercel-IP-City');
|
|
34
|
+
const userAgent = request.headers.get('User-Agent') || '';
|
|
35
|
+
let parser = new ua_parser_js_1.default(userAgent);
|
|
36
|
+
if (city == null)
|
|
37
|
+
city = (0, edge_1.geolocation)(request).city;
|
|
38
|
+
reqCf.tlsClientAuth = JSON.stringify(reqCf.tlsClientAuth);
|
|
39
|
+
reqCf.tlsExportedAuthenticator = JSON.stringify(reqCf.tlsExportedAuthenticator);
|
|
40
|
+
let parsed = parser.getResult();
|
|
41
|
+
delete parsed.ua;
|
|
42
|
+
let ret = Object.assign(Object.assign({ clientIP, cf: reqCf, device,
|
|
43
|
+
ip,
|
|
44
|
+
country,
|
|
45
|
+
region,
|
|
46
|
+
city }, parsed), { deviceType: this.getDeviceType(userAgent) });
|
|
47
|
+
ret = essentials_js_1.libx.flatterObjectToDotNotation(ret);
|
|
48
|
+
return ret;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
static getDeviceType(ua) {
|
|
52
|
+
if (/mobile/i.test(ua)) {
|
|
53
|
+
return 'Mobile';
|
|
54
|
+
}
|
|
55
|
+
else if (/tablet/i.test(ua)) {
|
|
56
|
+
return 'Tablet';
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
return 'Desktop';
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
exports.RequestInfo = RequestInfo;
|
|
64
|
+
class ModuleOptions {
|
|
65
|
+
}
|
|
66
|
+
exports.ModuleOptions = ModuleOptions;
|
|
67
|
+
//# sourceMappingURL=RequestInfo.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RequestInfo.js","sourceRoot":"","sources":["../../src/helpers/RequestInfo.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,uCAA2C;AAE3C,uEAA2D;AAC3D,gEAAoC;AAwBpC,MAAa,WAAW;IACpB,YAA0B,OAAgC;QAAhC,YAAO,GAAP,OAAO,CAAyB;QACtD,oBAAI,CAAC,GAAG,CAAC,CAAC,CAAC,oBAAoB,CAAC,CAAC;QACjC,IAAI,CAAC,OAAO,mCAAQ,IAAI,aAAa,EAAE,GAAK,OAAO,CAAE,CAAC;IAC1D,CAAC;IAEM,MAAM,CAAO,OAAO,CAAC,OAAiB;;YACzC,MAAM,KAAK,qBAAQ,OAAO,CAAC,EAAE,CAAE,CAAC;YAEhC,MAAM,QAAQ,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAC;YACzD,MAAM,MAAM,GAAG,OAAO,CA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I,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC;YACrB,OAAO,QAAQ,CAAC;QACpB,CAAC;aAAM,IAAI,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC;YAC5B,OAAO,QAAQ,CAAC;QACpB,CAAC;aAAM,CAAC;YACJ,OAAO,SAAS,CAAC;QACrB,CAAC;IACL,CAAC;CACJ;AAnDD,kCAmDC;AAED,MAAa,aAAa;CAAG;AAA7B,sCAA6B","sourcesContent":["import { geolocation } from '@vercel/edge';\nimport { IRequest } from 'itty-router';\nimport { libx } from 'libx.js/build/bundles/essentials.js';\nimport UAParser from 'ua-parser-js';\n\nexport interface IRequestInfo {\n deviceType;\n clientIP;\n ip;\n country;\n region;\n city;\n 'cf.tlsClientAuth';\n 'cf.tlsExportedAuthenticator';\n 'device.vendor';\n 'device.model';\n 'device.type';\n 'browser.name';\n 'browser.version';\n 'browser.major';\n 'engine.name';\n 'engine.version';\n 'os.name';\n 'os.version';\n 'cpu.architecture';\n}\n\nexport class RequestInfo {\n public constructor(public options?: Partial<ModuleOptions>) {\n libx.log.v('RequestInfo:ctor: ');\n this.options = { ...new ModuleOptions(), ...options };\n }\n\n public static async process(request: IRequest): Promise<IRequestInfo> {\n const reqCf = { ...request.cf };\n\n const clientIP = request.headers.get('CF-Connecting-IP');\n const device = request.headers.get('CF-Device-Type');\n const ip = request.headers.get('x-real-ip');\n const country = request.headers.get('X-Vercel-IP-Country');\n const region = request.headers.get('X-Vercel-IP-Country-Region');\n let city = request.headers.get('X-Vercel-IP-City');\n const userAgent = request.headers.get('User-Agent') || '';\n let parser = new UAParser(userAgent);\n\n if (city == null) city = geolocation(request).city;\n\n reqCf.tlsClientAuth = JSON.stringify(reqCf.tlsClientAuth);\n reqCf.tlsExportedAuthenticator = JSON.stringify(reqCf.tlsExportedAuthenticator);\n\n let parsed = parser.getResult();\n delete parsed.ua;\n let ret = {\n clientIP,\n cf: reqCf,\n device,\n ip,\n country,\n region,\n city,\n ...parsed,\n deviceType: this.getDeviceType(userAgent),\n };\n\n ret = libx.flatterObjectToDotNotation(ret);\n\n return ret;\n }\n\n private static getDeviceType(ua: string) {\n if (/mobile/i.test(ua)) {\n return 'Mobile';\n } else if (/tablet/i.test(ua)) {\n return 'Tablet';\n } else {\n return 'Desktop';\n }\n }\n}\n\nexport class ModuleOptions {}\n"]}
|
package/package.json
CHANGED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { geolocation } from '@vercel/edge';
|
|
2
|
+
import { IRequest } from 'itty-router';
|
|
3
|
+
import { libx } from 'libx.js/build/bundles/essentials.js';
|
|
4
|
+
import UAParser from 'ua-parser-js';
|
|
5
|
+
|
|
6
|
+
export interface IRequestInfo {
|
|
7
|
+
deviceType;
|
|
8
|
+
clientIP;
|
|
9
|
+
ip;
|
|
10
|
+
country;
|
|
11
|
+
region;
|
|
12
|
+
city;
|
|
13
|
+
'cf.tlsClientAuth';
|
|
14
|
+
'cf.tlsExportedAuthenticator';
|
|
15
|
+
'device.vendor';
|
|
16
|
+
'device.model';
|
|
17
|
+
'device.type';
|
|
18
|
+
'browser.name';
|
|
19
|
+
'browser.version';
|
|
20
|
+
'browser.major';
|
|
21
|
+
'engine.name';
|
|
22
|
+
'engine.version';
|
|
23
|
+
'os.name';
|
|
24
|
+
'os.version';
|
|
25
|
+
'cpu.architecture';
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export class RequestInfo {
|
|
29
|
+
public constructor(public options?: Partial<ModuleOptions>) {
|
|
30
|
+
libx.log.v('RequestInfo:ctor: ');
|
|
31
|
+
this.options = { ...new ModuleOptions(), ...options };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public static async process(request: IRequest): Promise<IRequestInfo> {
|
|
35
|
+
const reqCf = { ...request.cf };
|
|
36
|
+
|
|
37
|
+
const clientIP = request.headers.get('CF-Connecting-IP');
|
|
38
|
+
const device = request.headers.get('CF-Device-Type');
|
|
39
|
+
const ip = request.headers.get('x-real-ip');
|
|
40
|
+
const country = request.headers.get('X-Vercel-IP-Country');
|
|
41
|
+
const region = request.headers.get('X-Vercel-IP-Country-Region');
|
|
42
|
+
let city = request.headers.get('X-Vercel-IP-City');
|
|
43
|
+
const userAgent = request.headers.get('User-Agent') || '';
|
|
44
|
+
let parser = new UAParser(userAgent);
|
|
45
|
+
|
|
46
|
+
if (city == null) city = geolocation(request).city;
|
|
47
|
+
|
|
48
|
+
reqCf.tlsClientAuth = JSON.stringify(reqCf.tlsClientAuth);
|
|
49
|
+
reqCf.tlsExportedAuthenticator = JSON.stringify(reqCf.tlsExportedAuthenticator);
|
|
50
|
+
|
|
51
|
+
let parsed = parser.getResult();
|
|
52
|
+
delete parsed.ua;
|
|
53
|
+
let ret = {
|
|
54
|
+
clientIP,
|
|
55
|
+
cf: reqCf,
|
|
56
|
+
device,
|
|
57
|
+
ip,
|
|
58
|
+
country,
|
|
59
|
+
region,
|
|
60
|
+
city,
|
|
61
|
+
...parsed,
|
|
62
|
+
deviceType: this.getDeviceType(userAgent),
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
ret = libx.flatterObjectToDotNotation(ret);
|
|
66
|
+
|
|
67
|
+
return ret;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
private static getDeviceType(ua: string) {
|
|
71
|
+
if (/mobile/i.test(ua)) {
|
|
72
|
+
return 'Mobile';
|
|
73
|
+
} else if (/tablet/i.test(ua)) {
|
|
74
|
+
return 'Tablet';
|
|
75
|
+
} else {
|
|
76
|
+
return 'Desktop';
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export class ModuleOptions {}
|