@sphereon/ssi-sdk.anomaly-detection 0.33.1-feature.vcdm2.tsup.32 → 0.33.1-next.2

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,21 @@
1
+ import { IAgentPlugin } from '@veramo/core';
2
+ import { IAnomalyDetection } from '../index';
3
+ type DnsLookupFn = (hostname: string) => Promise<string>;
4
+ export declare const anomalyDetectionMethods: Array<string>;
5
+ /**
6
+ * {@inheritDoc IAnomalyDetection}
7
+ */
8
+ export declare class AnomalyDetection implements IAgentPlugin {
9
+ readonly schema: any;
10
+ private readonly db;
11
+ private readonly dnsLookup?;
12
+ readonly methods: IAnomalyDetection;
13
+ constructor(args: {
14
+ geoIpDB: Buffer;
15
+ dnsLookupCallback?: DnsLookupFn;
16
+ });
17
+ private anomalyDetectionLookupLocation;
18
+ private resolveDns;
19
+ }
20
+ export {};
21
+ //# sourceMappingURL=AnomalyDetection.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AnomalyDetection.d.ts","sourceRoot":"","sources":["../../src/agent/AnomalyDetection.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAE3C,OAAO,EAA4E,iBAAiB,EAA4B,MAAM,UAAU,CAAA;AAEhJ,KAAK,WAAW,GAAG,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAAA;AAExD,eAAO,MAAM,uBAAuB,EAAE,KAAK,CAAC,MAAM,CAAsB,CAAA;AAExE;;GAEG;AACH,qBAAa,gBAAiB,YAAW,YAAY;IACnD,QAAQ,CAAC,MAAM,MAA2B;IAC1C,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAQ;IAC3B,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAa;IACxC,QAAQ,CAAC,OAAO,EAAE,iBAAiB,CAElC;gBAEW,IAAI,EAAE;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,iBAAiB,CAAC,EAAE,WAAW,CAAA;KAAE;YASxD,8BAA8B;YAiC9B,UAAU;CA0BzB"}
@@ -0,0 +1,89 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.AnomalyDetection = exports.anomalyDetectionMethods = void 0;
13
+ const ssi_sdk_agent_config_1 = require("@sphereon/ssi-sdk.agent-config");
14
+ const mmdb_lib_1 = require("mmdb-lib");
15
+ const index_1 = require("../index");
16
+ exports.anomalyDetectionMethods = ['lookupLocation'];
17
+ /**
18
+ * {@inheritDoc IAnomalyDetection}
19
+ */
20
+ class AnomalyDetection {
21
+ constructor(args) {
22
+ this.schema = index_1.schema.IAnomalyDetection;
23
+ this.methods = {
24
+ anomalyDetectionLookupLocation: this.anomalyDetectionLookupLocation.bind(this),
25
+ };
26
+ const { geoIpDB, dnsLookupCallback } = Object.assign({}, args);
27
+ if (geoIpDB === undefined || geoIpDB === null) {
28
+ throw new Error('The geoIpDB argument is required');
29
+ }
30
+ this.db = geoIpDB;
31
+ this.dnsLookup = dnsLookupCallback;
32
+ }
33
+ anomalyDetectionLookupLocation(args, context) {
34
+ return __awaiter(this, void 0, void 0, function* () {
35
+ var _a, _b;
36
+ const { ipOrHostname, storeId, namespace } = Object.assign({}, args);
37
+ const reader = new mmdb_lib_1.Reader(this.db);
38
+ const ipv4Reg = '(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])';
39
+ const ipv6Reg = '(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))';
40
+ let result;
41
+ if (!new RegExp(ipv4Reg).test(ipOrHostname) && !new RegExp(ipv6Reg).test(ipOrHostname)) {
42
+ const ip = yield this.resolveDns(ipOrHostname);
43
+ result = reader.get(ip);
44
+ }
45
+ else {
46
+ result = reader.get(ipOrHostname);
47
+ }
48
+ const lookupResult = {
49
+ continent: (_a = result === null || result === void 0 ? void 0 : result.continent) === null || _a === void 0 ? void 0 : _a.code,
50
+ country: (_b = result === null || result === void 0 ? void 0 : result.country) === null || _b === void 0 ? void 0 : _b.iso_code,
51
+ };
52
+ if ((0, ssi_sdk_agent_config_1.contextHasPlugin)(context, 'geolocationStorePersistLocation'))
53
+ yield context.agent.geolocationStorePersistLocation({
54
+ namespace,
55
+ storeId,
56
+ ipOrHostname,
57
+ locationArgs: lookupResult,
58
+ });
59
+ return Promise.resolve(lookupResult);
60
+ });
61
+ }
62
+ resolveDns(hostname) {
63
+ return __awaiter(this, void 0, void 0, function* () {
64
+ if (this.dnsLookup) {
65
+ return this.dnsLookup(hostname);
66
+ }
67
+ // Fallback to Node.js dns
68
+ let dns;
69
+ try {
70
+ dns = eval('require("dns")');
71
+ }
72
+ catch (e) {
73
+ console.error(e);
74
+ throw new Error(`DNS resolution not available on this platform, use the dnsLookupCallback in the AnomalyDetection constructor to implement DNS resolution for your platform.\r\n${e.message}`);
75
+ }
76
+ return new Promise((resolve, reject) => {
77
+ dns.lookup(hostname, (error, address, family) => {
78
+ if (error) {
79
+ reject(error);
80
+ return;
81
+ }
82
+ resolve(address);
83
+ });
84
+ });
85
+ });
86
+ }
87
+ }
88
+ exports.AnomalyDetection = AnomalyDetection;
89
+ //# sourceMappingURL=AnomalyDetection.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AnomalyDetection.js","sourceRoot":"","sources":["../../src/agent/AnomalyDetection.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,yEAAiE;AAEjE,uCAAkD;AAClD,oCAAgJ;AAInI,QAAA,uBAAuB,GAAkB,CAAC,gBAAgB,CAAC,CAAA;AAExE;;GAEG;AACH,MAAa,gBAAgB;IAQ3B,YAAY,IAA0D;QAP7D,WAAM,GAAG,cAAM,CAAC,iBAAiB,CAAA;QAGjC,YAAO,GAAsB;YACpC,8BAA8B,EAAE,IAAI,CAAC,8BAA8B,CAAC,IAAI,CAAC,IAAI,CAAC;SAC/E,CAAA;QAGC,MAAM,EAAE,OAAO,EAAE,iBAAiB,EAAE,qBAAQ,IAAI,CAAE,CAAA;QAClD,IAAI,OAAO,KAAK,SAAS,IAAI,OAAO,KAAK,IAAI,EAAE,CAAC;YAC9C,MAAM,IAAI,KAAK,CAAC,kCAAk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AO,CAAC,OAAO,CAAC,YAAY,CAAC,CAAA;QACtC,CAAC;KAAA;IAEa,UAAU,CAAC,QAAgB;;YACvC,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;gBACnB,OAAO,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAA;YACjC,CAAC;YAED,0BAA0B;YAC1B,IAAI,GAAG,CAAA;YACP,IAAI,CAAC;gBACH,GAAG,GAAG,IAAI,CAAC,gBAAgB,CAAC,CAAA;YAC9B,CAAC;YAAC,OAAO,CAAC,EAAE,CAAC;gBACX,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;gBAChB,MAAM,IAAI,KAAK,CACb,kKAAkK,CAAC,CAAC,OAAO,EAAE,CAC9K,CAAA;YACH,CAAC;YAED,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;gBACrC,GAAG,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC,KAAmC,EAAE,OAAe,EAAE,MAAc,EAAE,EAAE;oBAC5F,IAAI,KAAK,EAAE,CAAC;wBACV,MAAM,CAAC,KAAK,CAAC,CAAA;wBACb,OAAM;oBACR,CAAC;oBACD,OAAO,CAAC,OAAO,CAAC,CAAA;gBAClB,CAAC,CAAC,CAAA;YACJ,CAAC,CAAC,CAAA;QACJ,CAAC;KAAA;CACF;AA5ED,4CA4EC"}
package/dist/index.d.ts CHANGED
@@ -1,38 +1,8 @@
1
- import { IAgentPlugin, IPluginMethodMap, IAgentContext } from '@veramo/core';
2
- import { GeolocationStoreArgs, IGeolocationStore } from '@sphereon/ssi-sdk.geolocation-store';
3
-
4
- type DnsLookupFn = (hostname: string) => Promise<string>;
5
- declare const anomalyDetectionMethods: Array<string>;
6
- /**
7
- * {@inheritDoc IAnomalyDetection}
8
- */
9
- declare class AnomalyDetection implements IAgentPlugin {
10
- readonly schema: any;
11
- private readonly db;
12
- private readonly dnsLookup?;
13
- readonly methods: IAnomalyDetection;
14
- constructor(args: {
15
- geoIpDB: Uint8Array;
16
- dnsLookupCallback?: DnsLookupFn;
17
- });
18
- private anomalyDetectionLookupLocation;
19
- private resolveDns;
20
- }
21
-
22
- interface IAnomalyDetection extends IPluginMethodMap {
23
- anomalyDetectionLookupLocation(args: AnomalyDetectionLookupLocationArgs, context: IRequiredContext): Promise<AnomalyDetectionLookupLocationResult>;
24
- }
25
- type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
26
- type AnomalyDetectionLookupLocationArgs = PartialBy<GeolocationStoreArgs, 'storeId' | 'namespace'>;
27
- type AnomalyDetectionLookupLocationResult = {
28
- continent?: string;
29
- country?: string;
30
- };
31
- type IRequiredContext = IAgentContext<IGeolocationStore>;
32
-
33
1
  /**
34
2
  * @public
35
3
  */
36
4
  declare const schema: any;
37
-
38
- export { AnomalyDetection, type AnomalyDetectionLookupLocationArgs, type AnomalyDetectionLookupLocationResult, type IAnomalyDetection, type IRequiredContext, type PartialBy, anomalyDetectionMethods, schema };
5
+ export { schema };
6
+ export { AnomalyDetection, anomalyDetectionMethods } from './agent/AnomalyDetection';
7
+ export * from './types/IAnomalyDetection';
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,QAAA,MAAM,MAAM,KAAmC,CAAA;AAC/C,OAAO,EAAE,MAAM,EAAE,CAAA;AACjB,OAAO,EAAE,gBAAgB,EAAE,uBAAuB,EAAE,MAAM,0BAA0B,CAAA;AACpF,cAAc,2BAA2B,CAAA"}
package/dist/index.js CHANGED
@@ -1,146 +1,27 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropNames = Object.getOwnPropertyNames;
3
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
4
- var __commonJS = (cb, mod) => function __require() {
5
- return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
6
- };
7
-
8
- // plugin.schema.json
9
- var require_plugin_schema = __commonJS({
10
- "plugin.schema.json"(exports, module) {
11
- module.exports = {
12
- IAnomalyDetection: {
13
- components: {
14
- schemas: {
15
- AnomalyDetectionLookupLocationArgs: {
16
- $ref: '#/components/schemas/PartialBy<GeolocationStoreArgs,("storeId"|"namespace")>'
17
- },
18
- 'PartialBy<GeolocationStoreArgs,("storeId"|"namespace")>': {
19
- type: "object",
20
- properties: {
21
- storeId: {
22
- type: "string"
23
- },
24
- namespace: {
25
- type: "string"
26
- },
27
- ipOrHostname: {
28
- type: "string"
29
- }
30
- },
31
- required: [
32
- "ipOrHostname"
33
- ]
34
- },
35
- AnomalyDetectionLookupLocationResult: {
36
- type: "object",
37
- properties: {
38
- continent: {
39
- type: "string"
40
- },
41
- country: {
42
- type: "string"
43
- }
44
- }
45
- }
46
- },
47
- methods: {
48
- anomalyDetectionLookupLocation: {
49
- description: "",
50
- arguments: {
51
- $ref: "#/components/schemas/AnomalyDetectionLookupLocationArgs"
52
- },
53
- returnType: {
54
- $ref: "#/components/schemas/AnomalyDetectionLookupLocationResult"
55
- }
56
- }
57
- }
58
- }
59
- }
60
- };
61
- }
62
- });
63
-
64
- // src/agent/AnomalyDetection.ts
65
- import { contextHasPlugin } from "@sphereon/ssi-sdk.agent-config";
66
- import { Reader } from "mmdb-lib";
67
- var anomalyDetectionMethods = [
68
- "lookupLocation"
69
- ];
70
- var AnomalyDetection = class {
71
- static {
72
- __name(this, "AnomalyDetection");
73
- }
74
- schema = schema.IAnomalyDetection;
75
- db;
76
- dnsLookup;
77
- methods = {
78
- anomalyDetectionLookupLocation: this.anomalyDetectionLookupLocation.bind(this)
79
- };
80
- constructor(args) {
81
- const { geoIpDB, dnsLookupCallback } = {
82
- ...args
83
- };
84
- if (geoIpDB === void 0 || geoIpDB === null) {
85
- throw new Error("The geoIpDB argument is required");
86
- }
87
- this.db = geoIpDB;
88
- this.dnsLookup = dnsLookupCallback;
89
- }
90
- async anomalyDetectionLookupLocation(args, context) {
91
- const { ipOrHostname, storeId, namespace } = {
92
- ...args
93
- };
94
- const reader = new Reader(Buffer.from(this.db));
95
- const ipv4Reg = "(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])";
96
- const ipv6Reg = "(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))";
97
- let result;
98
- if (!new RegExp(ipv4Reg).test(ipOrHostname) && !new RegExp(ipv6Reg).test(ipOrHostname)) {
99
- const ip = await this.resolveDns(ipOrHostname);
100
- result = reader.get(ip);
101
- } else {
102
- result = reader.get(ipOrHostname);
103
- }
104
- const lookupResult = {
105
- continent: result?.continent?.code,
106
- country: result?.country?.iso_code
107
- };
108
- if (contextHasPlugin(context, "geolocationStorePersistLocation")) await context.agent.geolocationStorePersistLocation({
109
- namespace,
110
- storeId,
111
- ipOrHostname,
112
- locationArgs: lookupResult
113
- });
114
- return Promise.resolve(lookupResult);
115
- }
116
- async resolveDns(hostname) {
117
- if (this.dnsLookup) {
118
- return this.dnsLookup(hostname);
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
119
7
  }
120
- try {
121
- const dns = await import("dns");
122
- return new Promise((resolve, reject) => {
123
- dns.lookup(hostname, (error, address, family) => {
124
- if (error) {
125
- reject(error);
126
- return;
127
- }
128
- resolve(address);
129
- });
130
- });
131
- } catch (e) {
132
- console.error(e);
133
- throw new Error(`DNS resolution not available on this platform, use the dnsLookupCallback in the AnomalyDetection constructor to implement DNS resolution for your platform.\r
134
- ${e.message}`);
135
- }
136
- }
137
- };
138
-
139
- // src/index.ts
140
- var schema = require_plugin_schema();
141
- export {
142
- AnomalyDetection,
143
- anomalyDetectionMethods,
144
- schema
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
145
15
  };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.anomalyDetectionMethods = exports.AnomalyDetection = exports.schema = void 0;
18
+ /**
19
+ * @public
20
+ */
21
+ const schema = require('../plugin.schema.json');
22
+ exports.schema = schema;
23
+ var AnomalyDetection_1 = require("./agent/AnomalyDetection");
24
+ Object.defineProperty(exports, "AnomalyDetection", { enumerable: true, get: function () { return AnomalyDetection_1.AnomalyDetection; } });
25
+ Object.defineProperty(exports, "anomalyDetectionMethods", { enumerable: true, get: function () { return AnomalyDetection_1.anomalyDetectionMethods; } });
26
+ __exportStar(require("./types/IAnomalyDetection"), exports);
146
27
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../plugin.schema.json","../src/agent/AnomalyDetection.ts","../src/index.ts"],"sourcesContent":["{\n \"IAnomalyDetection\": {\n \"components\": {\n \"schemas\": {\n \"AnomalyDetectionLookupLocationArgs\": {\n \"$ref\": \"#/components/schemas/PartialBy<GeolocationStoreArgs,(\\\"storeId\\\"|\\\"namespace\\\")>\"\n },\n \"PartialBy<GeolocationStoreArgs,(\\\"storeId\\\"|\\\"namespace\\\")>\": {\n \"type\": \"object\",\n \"properties\": {\n \"storeId\": {\n \"type\": \"string\"\n },\n \"namespace\": {\n \"type\": \"string\"\n },\n \"ipOrHostname\": {\n \"type\": \"string\"\n }\n },\n \"required\": [\n \"ipOrHostname\"\n ]\n },\n \"AnomalyDetectionLookupLocationResult\": {\n \"type\": \"object\",\n \"properties\": {\n \"continent\": {\n \"type\": \"string\"\n },\n \"country\": {\n \"type\": \"string\"\n }\n }\n }\n },\n \"methods\": {\n \"anomalyDetectionLookupLocation\": {\n \"description\": \"\",\n \"arguments\": {\n \"$ref\": \"#/components/schemas/AnomalyDetectionLookupLocationArgs\"\n },\n \"returnType\": {\n \"$ref\": \"#/components/schemas/AnomalyDetectionLookupLocationResult\"\n }\n }\n }\n }\n }\n}","import { contextHasPlugin } from '@sphereon/ssi-sdk.agent-config'\nimport { IAgentPlugin } from '@veramo/core'\nimport { CountryResponse, Reader } from 'mmdb-lib'\nimport { AnomalyDetectionLookupLocationArgs, AnomalyDetectionLookupLocationResult, IAnomalyDetection, IRequiredContext, schema } from '../index'\n\ntype DnsLookupFn = (hostname: string) => Promise<string>\n\nexport const anomalyDetectionMethods: Array<string> = ['lookupLocation']\n\n/**\n * {@inheritDoc IAnomalyDetection}\n */\nexport class AnomalyDetection implements IAgentPlugin {\n readonly schema = schema.IAnomalyDetection\n private readonly db: Uint8Array\n private readonly dnsLookup?: DnsLookupFn\n readonly methods: IAnomalyDetection = {\n anomalyDetectionLookupLocation: this.anomalyDetectionLookupLocation.bind(this),\n }\n\n constructor(args: { geoIpDB: Uint8Array; dnsLookupCallback?: DnsLookupFn }) {\n const { geoIpDB, dnsLookupCallback } = { ...args }\n if (geoIpDB === undefined || geoIpDB === null) {\n throw new Error('The geoIpDB argument is required')\n }\n this.db = geoIpDB\n this.dnsLookup = dnsLookupCallback\n }\n\n private async anomalyDetectionLookupLocation(\n args: AnomalyDetectionLookupLocationArgs,\n context: IRequiredContext,\n ): Promise<AnomalyDetectionLookupLocationResult> {\n const { ipOrHostname, storeId, namespace } = { ...args }\n const reader = new Reader<CountryResponse>(Buffer.from(this.db))\n const ipv4Reg = '(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\\\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'\n const ipv6Reg =\n '(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))'\n let result: CountryResponse | null\n\n if (!new RegExp(ipv4Reg).test(ipOrHostname) && !new RegExp(ipv6Reg).test(ipOrHostname)) {\n const ip = await this.resolveDns(ipOrHostname)\n result = reader.get(ip)\n } else {\n result = reader.get(ipOrHostname)\n }\n\n const lookupResult = {\n continent: result?.continent?.code,\n country: result?.country?.iso_code,\n }\n\n if (contextHasPlugin(context, 'geolocationStorePersistLocation'))\n await context.agent.geolocationStorePersistLocation({\n namespace,\n storeId,\n ipOrHostname,\n locationArgs: lookupResult,\n })\n return Promise.resolve(lookupResult)\n }\n\n private async resolveDns(hostname: string): Promise<string> {\n if (this.dnsLookup) {\n return this.dnsLookup(hostname)\n }\n\n // Fallback to Node dns\n try {\n const dns = await import('dns')\n return new Promise((resolve, reject) => {\n dns.lookup(hostname, (error: NodeJS.ErrnoException | null, address: string, family: number) => {\n if (error) {\n reject(error)\n return\n }\n resolve(address)\n })\n })\n } catch (e) {\n console.error(e)\n throw new Error(\n `DNS resolution not available on this platform, use the dnsLookupCallback in the AnomalyDetection constructor to implement DNS resolution for your platform.\\r\\n${e.message}`,\n )\n }\n }\n}\n","/**\n * @public\n */\nconst schema = require('../plugin.schema.json')\nexport { schema }\nexport { AnomalyDetection, anomalyDetectionMethods } from './agent/AnomalyDetection'\nexport * from './types/IAnomalyDetection'\n"],"mappings":";;;;;;;;AAAA;AAAA;AAAA;AAAA,MACE,mBAAqB;AAAA,QACnB,YAAc;AAAA,UACZ,SAAW;AAAA,YACT,oCAAsC;AAAA,cACpC,MAAQ;AAAA,YACV;AAAA,YACA,2DAA+D;AAAA,cAC7D,MAAQ;AAAA,cACR,YAAc;AAAA,gBACZ,SAAW;AAAA,kBACT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,QAAI,CAAC,IAAIC,OAAOH,OAAAA,EAASI,KAAKX,YAAAA,KAAiB,CAAC,IAAIU,OAAOF,OAAAA,EAASG,KAAKX,YAAAA,GAAe;AACtF,YAAMY,KAAK,MAAM,KAAKC,WAAWb,YAAAA;AACjCS,eAASN,OAAOW,IAAIF,EAAAA;IACtB,OAAO;AACLH,eAASN,OAAOW,IAAId,YAAAA;IACtB;AAEA,UAAMe,eAAe;MACnBC,WAAWP,QAAQO,WAAWC;MAC9BC,SAAST,QAAQS,SAASC;IAC5B;AAEA,QAAIC,iBAAiBrB,SAAS,iCAAA,EAC5B,OAAMA,QAAQsB,MAAMC,gCAAgC;MAClDpB;MACAD;MACAD;MACAuB,cAAcR;IAChB,CAAA;AACF,WAAOS,QAAQC,QAAQV,YAAAA;EACzB;EAEA,MAAcF,WAAWa,UAAmC;AAC1D,QAAI,KAAKrC,WAAW;AAClB,aAAO,KAAKA,UAAUqC,QAAAA;IACxB;AAGA,QAAI;AACF,YAAMC,MAAM,MAAM,OAAO,KAAA;AACzB,aAAO,IAAIH,QAAQ,CAACC,SAASG,WAAAA;AAC3BD,YAAIE,OAAOH,UAAU,CAACI,OAAqCC,SAAiBC,WAAAA;AAC1E,cAAIF,OAAO;AACTF,mBAAOE,KAAAA;AACP;UACF;AACAL,kBAAQM,OAAAA;QACV,CAAA;MACF,CAAA;IACF,SAASE,GAAG;AACVC,cAAQJ,MAAMG,CAAAA;AACd,YAAM,IAAInC,MACR;EAAkKmC,EAAEE,OAAO,EAAE;IAEjL;EACF;AACF;;;ACnFA,IAAMC,SAASC;","names":["contextHasPlugin","Reader","anomalyDetectionMethods","AnomalyDetection","schema","IAnomalyDetection","db","dnsLookup","methods","anomalyDetectionLookupLocation","bind","constructor","args","geoIpDB","dnsLookupCallback","undefined","Error","context","ipOrHostname","storeId","namespace","reader","Reader","Buffer","from","ipv4Reg","ipv6Reg","result","RegExp","test","ip","resolveDns","get","lookupResult","continent","code","country","iso_code","contextHasPlugin","agent","geolocationStorePersistLocation","locationArgs","Promise","resolve","hostname","dns","reject","lookup","error","address","family","e","console","message","schema","require"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA;;GAEG;AACH,MAAM,MAAM,GAAG,OAAO,CAAC,uBAAuB,CAAC,CAAA;AACtC,wBAAM;AACf,6DAAoF;AAA3E,oHAAA,gBAAgB,OAAA;AAAE,2HAAA,uBAAuB,OAAA;AAClD,4DAAyC"}
@@ -0,0 +1,47 @@
1
+ import { GeolocationStoreArgs } from '@sphereon/ssi-sdk.geolocation-store';
2
+ import { IAgentContext } from '@veramo/core';
3
+ import { IAgentPlugin } from '@veramo/core';
4
+ import { IGeolocationStore } from '@sphereon/ssi-sdk.geolocation-store';
5
+ import { IPluginMethodMap } from '@veramo/core';
6
+
7
+ /**
8
+ * {@inheritDoc IAnomalyDetection}
9
+ */
10
+ export declare class AnomalyDetection implements IAgentPlugin {
11
+ readonly schema: any;
12
+ private readonly db;
13
+ private readonly dnsLookup?;
14
+ readonly methods: IAnomalyDetection;
15
+ constructor(args: {
16
+ geoIpDB: Buffer;
17
+ dnsLookupCallback?: DnsLookupFn;
18
+ });
19
+ private anomalyDetectionLookupLocation;
20
+ private resolveDns;
21
+ }
22
+
23
+ export declare type AnomalyDetectionLookupLocationArgs = PartialBy<GeolocationStoreArgs, 'storeId' | 'namespace'>;
24
+
25
+ export declare type AnomalyDetectionLookupLocationResult = {
26
+ continent?: string;
27
+ country?: string;
28
+ };
29
+
30
+ export declare const anomalyDetectionMethods: Array<string>;
31
+
32
+ declare type DnsLookupFn = (hostname: string) => Promise<string>;
33
+
34
+ export declare interface IAnomalyDetection extends IPluginMethodMap {
35
+ anomalyDetectionLookupLocation(args: AnomalyDetectionLookupLocationArgs, context: IRequiredContext): Promise<AnomalyDetectionLookupLocationResult>;
36
+ }
37
+
38
+ export declare type IRequiredContext = IAgentContext<IGeolocationStore>;
39
+
40
+ export declare type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
41
+
42
+ /**
43
+ * @public
44
+ */
45
+ export declare const schema: any;
46
+
47
+ export { }
@@ -0,0 +1,11 @@
1
+ // This file is read by tools that parse documentation comments conforming to the TSDoc standard.
2
+ // It should be published with your NPM package. It should not be tracked by Git.
3
+ {
4
+ "tsdocVersion": "0.12",
5
+ "toolPackages": [
6
+ {
7
+ "packageName": "@microsoft/api-extractor",
8
+ "packageVersion": "7.52.1"
9
+ }
10
+ ]
11
+ }
@@ -0,0 +1,13 @@
1
+ import { IAgentContext, IPluginMethodMap } from '@veramo/core';
2
+ import { GeolocationStoreArgs, IGeolocationStore } from '@sphereon/ssi-sdk.geolocation-store';
3
+ export interface IAnomalyDetection extends IPluginMethodMap {
4
+ anomalyDetectionLookupLocation(args: AnomalyDetectionLookupLocationArgs, context: IRequiredContext): Promise<AnomalyDetectionLookupLocationResult>;
5
+ }
6
+ export type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
7
+ export type AnomalyDetectionLookupLocationArgs = PartialBy<GeolocationStoreArgs, 'storeId' | 'namespace'>;
8
+ export type AnomalyDetectionLookupLocationResult = {
9
+ continent?: string;
10
+ country?: string;
11
+ };
12
+ export type IRequiredContext = IAgentContext<IGeolocationStore>;
13
+ //# sourceMappingURL=IAnomalyDetection.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IAnomalyDetection.d.ts","sourceRoot":"","sources":["../../src/types/IAnomalyDetection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAA;AAC9D,OAAO,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,MAAM,qCAAqC,CAAA;AAE7F,MAAM,WAAW,iBAAkB,SAAQ,gBAAgB;IACzD,8BAA8B,CAAC,IAAI,EAAE,kCAAkC,EAAE,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,oCAAoC,CAAC,CAAA;CACnJ;AAED,MAAM,MAAM,SAAS,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,IAAI,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA;AAE9E,MAAM,MAAM,kCAAkC,GAAG,SAAS,CAAC,oBAAoB,EAAE,SAAS,GAAG,WAAW,CAAC,CAAA;AAEzG,MAAM,MAAM,oCAAoC,GAAG;IACjD,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB,CAAA;AAED,MAAM,MAAM,gBAAgB,GAAG,aAAa,CAAC,iBAAiB,CAAC,CAAA"}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=IAnomalyDetection.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IAnomalyDetection.js","sourceRoot":"","sources":["../../src/types/IAnomalyDetection.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,49 +1,37 @@
1
1
  {
2
2
  "name": "@sphereon/ssi-sdk.anomaly-detection",
3
- "version": "0.33.1-feature.vcdm2.tsup.32+203a9d60",
3
+ "version": "0.33.1-next.2+6f7f40b9",
4
4
  "source": "src/index.ts",
5
- "type": "module",
6
- "main": "./dist/index.cjs",
7
- "module": "./dist/index.js",
8
- "types": "./dist/index.d.ts",
9
- "exports": {
10
- "react-native": "./dist/index.js",
11
- "import": {
12
- "types": "./dist/index.d.ts",
13
- "import": "./dist/index.js"
14
- },
15
- "require": {
16
- "types": "./dist/index.d.cts",
17
- "require": "./dist/index.cjs"
18
- }
19
- },
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
20
7
  "veramo": {
21
8
  "pluginInterfaces": {
22
9
  "IAnomalyDetection": "./src/types/IAnomalyDetection.ts"
23
10
  }
24
11
  },
25
12
  "scripts": {
26
- "build": "tsup --config ../../tsup.config.ts --tsconfig ../../tsconfig.tsup.json",
13
+ "build": "tsc",
14
+ "build:clean": "tsc --build --clean && tsc --build",
27
15
  "generate-plugin-schema": "ts-node ../../packages/dev/bin/sphereon.js dev generate-plugin-schema"
28
16
  },
29
17
  "dependencies": {
30
- "@sphereon/ssi-sdk.geolocation-store": "^0.33.1-feature.vcdm2.tsup.32+203a9d60",
31
- "@sphereon/ssi-sdk.kv-store-temp": "^0.33.1-feature.vcdm2.tsup.32+203a9d60",
18
+ "@sphereon/ssi-sdk.geolocation-store": "0.33.1-next.2+6f7f40b9",
19
+ "@sphereon/ssi-sdk.kv-store-temp": "0.33.1-next.2+6f7f40b9",
32
20
  "cross-fetch": "^3.1.8",
33
21
  "debug": "^4.3.5",
34
22
  "dns-query": "^0.11.2",
35
23
  "mmdb-lib": "^2.1.1",
36
- "typeorm": "0.3.20"
24
+ "typeorm": "^0.3.21"
37
25
  },
38
26
  "devDependencies": {
39
- "@sphereon/ssi-sdk.agent-config": "^0.33.1-feature.vcdm2.tsup.32+203a9d60",
27
+ "@sphereon/ssi-sdk.agent-config": "0.33.1-next.2+6f7f40b9",
40
28
  "@types/node": "^20.17.1",
41
29
  "@veramo/remote-client": "4.2.0",
42
30
  "@veramo/remote-server": "4.2.0"
43
31
  },
44
32
  "files": [
45
- "dist",
46
- "src",
33
+ "dist/**/*",
34
+ "src/**/*",
47
35
  "README.md",
48
36
  "plugin.schema.json",
49
37
  "LICENSE"
@@ -61,5 +49,6 @@
61
49
  "Veramo",
62
50
  "Anomaly Detection"
63
51
  ],
64
- "gitHead": "203a9d6076275e58d7959b7d212b8b938ad6a49e"
52
+ "nx": {},
53
+ "gitHead": "6f7f40b94beb385369fede046c3912bd0c053408"
65
54
  }
@@ -12,13 +12,13 @@ export const anomalyDetectionMethods: Array<string> = ['lookupLocation']
12
12
  */
13
13
  export class AnomalyDetection implements IAgentPlugin {
14
14
  readonly schema = schema.IAnomalyDetection
15
- private readonly db: Uint8Array
15
+ private readonly db: Buffer
16
16
  private readonly dnsLookup?: DnsLookupFn
17
17
  readonly methods: IAnomalyDetection = {
18
18
  anomalyDetectionLookupLocation: this.anomalyDetectionLookupLocation.bind(this),
19
19
  }
20
20
 
21
- constructor(args: { geoIpDB: Uint8Array; dnsLookupCallback?: DnsLookupFn }) {
21
+ constructor(args: { geoIpDB: Buffer; dnsLookupCallback?: DnsLookupFn }) {
22
22
  const { geoIpDB, dnsLookupCallback } = { ...args }
23
23
  if (geoIpDB === undefined || geoIpDB === null) {
24
24
  throw new Error('The geoIpDB argument is required')
@@ -32,7 +32,7 @@ export class AnomalyDetection implements IAgentPlugin {
32
32
  context: IRequiredContext,
33
33
  ): Promise<AnomalyDetectionLookupLocationResult> {
34
34
  const { ipOrHostname, storeId, namespace } = { ...args }
35
- const reader = new Reader<CountryResponse>(Buffer.from(this.db))
35
+ const reader = new Reader<CountryResponse>(this.db)
36
36
  const ipv4Reg = '(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'
37
37
  const ipv6Reg =
38
38
  '(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))'
@@ -65,23 +65,25 @@ export class AnomalyDetection implements IAgentPlugin {
65
65
  return this.dnsLookup(hostname)
66
66
  }
67
67
 
68
- // Fallback to Node dns
68
+ // Fallback to Node.js dns
69
+ let dns
69
70
  try {
70
- const dns = await import('dns')
71
- return new Promise((resolve, reject) => {
72
- dns.lookup(hostname, (error: NodeJS.ErrnoException | null, address: string, family: number) => {
73
- if (error) {
74
- reject(error)
75
- return
76
- }
77
- resolve(address)
78
- })
79
- })
71
+ dns = eval('require("dns")')
80
72
  } catch (e) {
81
73
  console.error(e)
82
74
  throw new Error(
83
75
  `DNS resolution not available on this platform, use the dnsLookupCallback in the AnomalyDetection constructor to implement DNS resolution for your platform.\r\n${e.message}`,
84
76
  )
85
77
  }
78
+
79
+ return new Promise((resolve, reject) => {
80
+ dns.lookup(hostname, (error: NodeJS.ErrnoException | null, address: string, family: number) => {
81
+ if (error) {
82
+ reject(error)
83
+ return
84
+ }
85
+ resolve(address)
86
+ })
87
+ })
86
88
  }
87
89
  }
package/dist/index.cjs DELETED
@@ -1,176 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
9
- var __commonJS = (cb, mod) => function __require() {
10
- return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
11
- };
12
- var __export = (target, all) => {
13
- for (var name in all)
14
- __defProp(target, name, { get: all[name], enumerable: true });
15
- };
16
- var __copyProps = (to, from, except, desc) => {
17
- if (from && typeof from === "object" || typeof from === "function") {
18
- for (let key of __getOwnPropNames(from))
19
- if (!__hasOwnProp.call(to, key) && key !== except)
20
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
21
- }
22
- return to;
23
- };
24
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
25
- // If the importer is in node compatibility mode or this is not an ESM
26
- // file that has been converted to a CommonJS file using a Babel-
27
- // compatible transform (i.e. "__esModule" has not been set), then set
28
- // "default" to the CommonJS "module.exports" for node compatibility.
29
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
30
- mod
31
- ));
32
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
33
-
34
- // plugin.schema.json
35
- var require_plugin_schema = __commonJS({
36
- "plugin.schema.json"(exports, module2) {
37
- module2.exports = {
38
- IAnomalyDetection: {
39
- components: {
40
- schemas: {
41
- AnomalyDetectionLookupLocationArgs: {
42
- $ref: '#/components/schemas/PartialBy<GeolocationStoreArgs,("storeId"|"namespace")>'
43
- },
44
- 'PartialBy<GeolocationStoreArgs,("storeId"|"namespace")>': {
45
- type: "object",
46
- properties: {
47
- storeId: {
48
- type: "string"
49
- },
50
- namespace: {
51
- type: "string"
52
- },
53
- ipOrHostname: {
54
- type: "string"
55
- }
56
- },
57
- required: [
58
- "ipOrHostname"
59
- ]
60
- },
61
- AnomalyDetectionLookupLocationResult: {
62
- type: "object",
63
- properties: {
64
- continent: {
65
- type: "string"
66
- },
67
- country: {
68
- type: "string"
69
- }
70
- }
71
- }
72
- },
73
- methods: {
74
- anomalyDetectionLookupLocation: {
75
- description: "",
76
- arguments: {
77
- $ref: "#/components/schemas/AnomalyDetectionLookupLocationArgs"
78
- },
79
- returnType: {
80
- $ref: "#/components/schemas/AnomalyDetectionLookupLocationResult"
81
- }
82
- }
83
- }
84
- }
85
- }
86
- };
87
- }
88
- });
89
-
90
- // src/index.ts
91
- var index_exports = {};
92
- __export(index_exports, {
93
- AnomalyDetection: () => AnomalyDetection,
94
- anomalyDetectionMethods: () => anomalyDetectionMethods,
95
- schema: () => schema
96
- });
97
- module.exports = __toCommonJS(index_exports);
98
-
99
- // src/agent/AnomalyDetection.ts
100
- var import_ssi_sdk = require("@sphereon/ssi-sdk.agent-config");
101
- var import_mmdb_lib = require("mmdb-lib");
102
- var anomalyDetectionMethods = [
103
- "lookupLocation"
104
- ];
105
- var AnomalyDetection = class {
106
- static {
107
- __name(this, "AnomalyDetection");
108
- }
109
- schema = schema.IAnomalyDetection;
110
- db;
111
- dnsLookup;
112
- methods = {
113
- anomalyDetectionLookupLocation: this.anomalyDetectionLookupLocation.bind(this)
114
- };
115
- constructor(args) {
116
- const { geoIpDB, dnsLookupCallback } = {
117
- ...args
118
- };
119
- if (geoIpDB === void 0 || geoIpDB === null) {
120
- throw new Error("The geoIpDB argument is required");
121
- }
122
- this.db = geoIpDB;
123
- this.dnsLookup = dnsLookupCallback;
124
- }
125
- async anomalyDetectionLookupLocation(args, context) {
126
- const { ipOrHostname, storeId, namespace } = {
127
- ...args
128
- };
129
- const reader = new import_mmdb_lib.Reader(Buffer.from(this.db));
130
- const ipv4Reg = "(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])";
131
- const ipv6Reg = "(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))";
132
- let result;
133
- if (!new RegExp(ipv4Reg).test(ipOrHostname) && !new RegExp(ipv6Reg).test(ipOrHostname)) {
134
- const ip = await this.resolveDns(ipOrHostname);
135
- result = reader.get(ip);
136
- } else {
137
- result = reader.get(ipOrHostname);
138
- }
139
- const lookupResult = {
140
- continent: result?.continent?.code,
141
- country: result?.country?.iso_code
142
- };
143
- if ((0, import_ssi_sdk.contextHasPlugin)(context, "geolocationStorePersistLocation")) await context.agent.geolocationStorePersistLocation({
144
- namespace,
145
- storeId,
146
- ipOrHostname,
147
- locationArgs: lookupResult
148
- });
149
- return Promise.resolve(lookupResult);
150
- }
151
- async resolveDns(hostname) {
152
- if (this.dnsLookup) {
153
- return this.dnsLookup(hostname);
154
- }
155
- try {
156
- const dns = await import("dns");
157
- return new Promise((resolve, reject) => {
158
- dns.lookup(hostname, (error, address, family) => {
159
- if (error) {
160
- reject(error);
161
- return;
162
- }
163
- resolve(address);
164
- });
165
- });
166
- } catch (e) {
167
- console.error(e);
168
- throw new Error(`DNS resolution not available on this platform, use the dnsLookupCallback in the AnomalyDetection constructor to implement DNS resolution for your platform.\r
169
- ${e.message}`);
170
- }
171
- }
172
- };
173
-
174
- // src/index.ts
175
- var schema = require_plugin_schema();
176
- //# sourceMappingURL=index.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../plugin.schema.json","../src/index.ts","../src/agent/AnomalyDetection.ts"],"sourcesContent":["{\n \"IAnomalyDetection\": {\n \"components\": {\n \"schemas\": {\n \"AnomalyDetectionLookupLocationArgs\": {\n \"$ref\": \"#/components/schemas/PartialBy<GeolocationStoreArgs,(\\\"storeId\\\"|\\\"namespace\\\")>\"\n },\n \"PartialBy<GeolocationStoreArgs,(\\\"storeId\\\"|\\\"namespace\\\")>\": {\n \"type\": \"object\",\n \"properties\": {\n \"storeId\": {\n \"type\": \"string\"\n },\n \"namespace\": {\n \"type\": \"string\"\n },\n \"ipOrHostname\": {\n \"type\": \"string\"\n }\n },\n \"required\": [\n \"ipOrHostname\"\n ]\n },\n \"AnomalyDetectionLookupLocationResult\": {\n \"type\": \"object\",\n \"properties\": {\n \"continent\": {\n \"type\": \"string\"\n },\n \"country\": {\n \"type\": \"string\"\n }\n }\n }\n },\n \"methods\": {\n \"anomalyDetectionLookupLocation\": {\n \"description\": \"\",\n \"arguments\": {\n \"$ref\": \"#/components/schemas/AnomalyDetectionLookupLocationArgs\"\n },\n \"returnType\": {\n \"$ref\": \"#/components/schemas/AnomalyDetectionLookupLocationResult\"\n }\n }\n }\n }\n }\n}","/**\n * @public\n */\nconst schema = require('../plugin.schema.json')\nexport { schema }\nexport { AnomalyDetection, anomalyDetectionMethods } from './agent/AnomalyDetection'\nexport * from './types/IAnomalyDetection'\n","import { contextHasPlugin } from '@sphereon/ssi-sdk.agent-config'\nimport { IAgentPlugin } from '@veramo/core'\nimport { CountryResponse, Reader } from 'mmdb-lib'\nimport { AnomalyDetectionLookupLocationArgs, AnomalyDetectionLookupLocationResult, IAnomalyDetection, IRequiredContext, schema } from '../index'\n\ntype DnsLookupFn = (hostname: string) => Promise<string>\n\nexport const anomalyDetectionMethods: Array<string> = ['lookupLocation']\n\n/**\n * {@inheritDoc IAnomalyDetection}\n */\nexport class AnomalyDetection implements IAgentPlugin {\n readonly schema = schema.IAnomalyDetection\n private readonly db: Uint8Array\n private readonly dnsLookup?: DnsLookupFn\n readonly methods: IAnomalyDetection = {\n anomalyDetectionLookupLocation: this.anomalyDetectionLookupLocation.bind(this),\n }\n\n constructor(args: { geoIpDB: Uint8Array; dnsLookupCallback?: DnsLookupFn }) {\n const { geoIpDB, dnsLookupCallback } = { ...args }\n if (geoIpDB === undefined || geoIpDB === null) {\n throw new Error('The geoIpDB argument is required')\n }\n this.db = geoIpDB\n this.dnsLookup = dnsLookupCallback\n }\n\n private async anomalyDetectionLookupLocation(\n args: AnomalyDetectionLookupLocationArgs,\n context: IRequiredContext,\n ): Promise<AnomalyDetectionLookupLocationResult> {\n const { ipOrHostname, storeId, namespace } = { ...args }\n const reader = new Reader<CountryResponse>(Buffer.from(this.db))\n const ipv4Reg = '(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\\\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'\n const ipv6Reg =\n '(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))'\n let result: CountryResponse | null\n\n if (!new RegExp(ipv4Reg).test(ipOrHostname) && !new RegExp(ipv6Reg).test(ipOrHostname)) {\n const ip = await this.resolveDns(ipOrHostname)\n result = reader.get(ip)\n } else {\n result = reader.get(ipOrHostname)\n }\n\n const lookupResult = {\n continent: result?.continent?.code,\n country: result?.country?.iso_code,\n }\n\n if (contextHasPlugin(context, 'geolocationStorePersistLocation'))\n await context.agent.geolocationStorePersistLocation({\n namespace,\n storeId,\n ipOrHostname,\n locationArgs: lookupResult,\n })\n return Promise.resolve(lookupResult)\n }\n\n private async resolveDns(hostname: string): Promise<string> {\n if (this.dnsLookup) {\n return this.dnsLookup(hostname)\n }\n\n // Fallback to Node dns\n try {\n const dns = await import('dns')\n return new Promise((resolve, reject) => {\n dns.lookup(hostname, (error: NodeJS.ErrnoException | null, address: string, family: number) => {\n if (error) {\n reject(error)\n return\n }\n resolve(address)\n })\n })\n } catch (e) {\n console.error(e)\n throw new Error(\n `DNS resolution not available on this platform, use the dnsLookupCallback in the AnomalyDetection constructor to implement DNS resolution for your platform.\\r\\n${e.message}`,\n )\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA,gCAAAA,SAAA;AAAA,IAAAA,QAAA;AAAA,MACE,mBAAqB;AAAA,QACnB,YAAc;AAAA,UACZ,SAAW;AAAA,YACT,oCAAsC;AAAA,cACpC,MAAQ;AAAA,YACV;AAAA,YACA,2DAA+D;AAAA,cAC7D,MAAQ;AAAA,cACR,YAAc;AAAA,gBACZ,SAAW;AAAA,kBACT,MAAQ;AAAA,gBACV;AAAA,gBACA,WAAa;AAAA,k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,KAAKX,YAAAA,KAAiB,CAAC,IAAIU,OAAOF,OAAAA,EAASG,KAAKX,YAAAA,GAAe;AACtF,YAAMY,KAAK,MAAM,KAAKC,WAAWb,YAAAA;AACjCS,eAASN,OAAOW,IAAIF,EAAAA;IACtB,OAAO;AACLH,eAASN,OAAOW,IAAId,YAAAA;IACtB;AAEA,UAAMe,eAAe;MACnBC,WAAWP,QAAQO,WAAWC;MAC9BC,SAAST,QAAQS,SAASC;IAC5B;AAEA,YAAIC,iCAAiBrB,SAAS,iCAAA,EAC5B,OAAMA,QAAQsB,MAAMC,gCAAgC;MAClDpB;MACAD;MACAD;MACAuB,cAAcR;IAChB,CAAA;AACF,WAAOS,QAAQC,QAAQV,YAAAA;EACzB;EAEA,MAAcF,WAAWa,UAAmC;AAC1D,QAAI,KAAKrC,WAAW;AAClB,aAAO,KAAKA,UAAUqC,QAAAA;IACxB;AAGA,QAAI;AACF,YAAMC,MAAM,MAAM,OAAO,KAAA;AACzB,aAAO,IAAIH,QAAQ,CAACC,SAASG,WAAAA;AAC3BD,YAAIE,OAAOH,UAAU,CAACI,OAAqCC,SAAiBC,WAAAA;AAC1E,cAAIF,OAAO;AACTF,mBAAOE,KAAAA;AACP;UACF;AACAL,kBAAQM,OAAAA;QACV,CAAA;MACF,CAAA;IACF,SAASE,GAAG;AACVC,cAAQJ,MAAMG,CAAAA;AACd,YAAM,IAAInC,MACR;EAAkKmC,EAAEE,OAAO,EAAE;IAEjL;EACF;AACF;;;ADnFA,IAAMC,SAASC;","names":["module","anomalyDetectionMethods","AnomalyDetection","schema","IAnomalyDetection","db","dnsLookup","methods","anomalyDetectionLookupLocation","bind","constructor","args","geoIpDB","dnsLookupCallback","undefined","Error","context","ipOrHostname","storeId","namespace","reader","Reader","Buffer","from","ipv4Reg","ipv6Reg","result","RegExp","test","ip","resolveDns","get","lookupResult","continent","code","country","iso_code","contextHasPlugin","agent","geolocationStorePersistLocation","locationArgs","Promise","resolve","hostname","dns","reject","lookup","error","address","family","e","console","message","schema","require"]}
package/dist/index.d.cts DELETED
@@ -1,38 +0,0 @@
1
- import { IAgentPlugin, IPluginMethodMap, IAgentContext } from '@veramo/core';
2
- import { GeolocationStoreArgs, IGeolocationStore } from '@sphereon/ssi-sdk.geolocation-store';
3
-
4
- type DnsLookupFn = (hostname: string) => Promise<string>;
5
- declare const anomalyDetectionMethods: Array<string>;
6
- /**
7
- * {@inheritDoc IAnomalyDetection}
8
- */
9
- declare class AnomalyDetection implements IAgentPlugin {
10
- readonly schema: any;
11
- private readonly db;
12
- private readonly dnsLookup?;
13
- readonly methods: IAnomalyDetection;
14
- constructor(args: {
15
- geoIpDB: Uint8Array;
16
- dnsLookupCallback?: DnsLookupFn;
17
- });
18
- private anomalyDetectionLookupLocation;
19
- private resolveDns;
20
- }
21
-
22
- interface IAnomalyDetection extends IPluginMethodMap {
23
- anomalyDetectionLookupLocation(args: AnomalyDetectionLookupLocationArgs, context: IRequiredContext): Promise<AnomalyDetectionLookupLocationResult>;
24
- }
25
- type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
26
- type AnomalyDetectionLookupLocationArgs = PartialBy<GeolocationStoreArgs, 'storeId' | 'namespace'>;
27
- type AnomalyDetectionLookupLocationResult = {
28
- continent?: string;
29
- country?: string;
30
- };
31
- type IRequiredContext = IAgentContext<IGeolocationStore>;
32
-
33
- /**
34
- * @public
35
- */
36
- declare const schema: any;
37
-
38
- export { AnomalyDetection, type AnomalyDetectionLookupLocationArgs, type AnomalyDetectionLookupLocationResult, type IAnomalyDetection, type IRequiredContext, type PartialBy, anomalyDetectionMethods, schema };