{"version":3,"sources":["../plugin.schema.json","../src/index.ts","../src/agent/GeolocationStore.ts"],"sourcesContent":["{\n  \"IGeolocationStore\": {\n    \"components\": {\n      \"schemas\": {\n        \"GeolocationStoreClearAllLocationsArgs\": {\n          \"$ref\": \"#/components/schemas/Pick<GeolocationStoreArgs,\\\"storeId\\\">\"\n        },\n        \"Pick<GeolocationStoreArgs,\\\"storeId\\\">\": {\n          \"type\": \"object\",\n          \"properties\": {\n            \"storeId\": {\n              \"type\": \"string\"\n            }\n          },\n          \"required\": [\"storeId\"],\n          \"additionalProperties\": false\n        },\n        \"GeolocationStoreLocationResult\": {\n          \"$ref\": \"#/components/schemas/IKeyValueStore<GeolocationStoreLocation>\"\n        },\n        \"IKeyValueStore<GeolocationStoreLocation>\": {\n          \"type\": \"object\",\n          \"additionalProperties\": false,\n          \"description\": \"A Key Value store is responsible for managing Values identified by keys.\"\n        },\n        \"GeolocationStoreArgs\": {\n          \"type\": \"object\",\n          \"properties\": {\n            \"ipOrHostname\": {\n              \"type\": \"string\"\n            },\n            \"storeId\": {\n              \"type\": \"string\"\n            },\n            \"namespace\": {\n              \"type\": \"string\"\n            }\n          },\n          \"required\": [\"ipOrHostname\", \"storeId\", \"namespace\"],\n          \"additionalProperties\": false\n        },\n        \"GeolocationStoreLocationResultOrUndefined\": {\n          \"anyOf\": [\n            {\n              \"$ref\": \"#/components/schemas/GeolocationStoreLocation\"\n            },\n            {\n              \"not\": {}\n            }\n          ]\n        },\n        \"GeolocationStoreLocation\": {\n          \"type\": \"object\",\n          \"properties\": {\n            \"continent\": {\n              \"type\": \"string\"\n            },\n            \"country\": {\n              \"type\": \"string\"\n            }\n          },\n          \"additionalProperties\": false\n        },\n        \"GeolocationStoreLocationPersistArgs\": {\n          \"type\": \"object\",\n          \"additionalProperties\": false,\n          \"properties\": {\n            \"locationArgs\": {\n              \"$ref\": \"#/components/schemas/GeolocationStoreLocation\"\n            },\n            \"ipOrHostname\": {\n              \"type\": \"string\"\n            },\n            \"overwriteExisting\": {\n              \"type\": \"boolean\"\n            },\n            \"validation\": {\n              \"type\": \"boolean\"\n            },\n            \"ttl\": {\n              \"type\": \"number\"\n            },\n            \"storeId\": {\n              \"type\": \"string\"\n            },\n            \"namespace\": {\n              \"type\": \"string\"\n            }\n          },\n          \"required\": [\"ipOrHostname\", \"locationArgs\"]\n        },\n        \"GeolocationStoreLocationResultIValueData\": {\n          \"$ref\": \"#/components/schemas/IValueData<GeolocationStoreLocation>\"\n        },\n        \"IValueData<GeolocationStoreLocation>\": {\n          \"type\": \"object\",\n          \"properties\": {\n            \"value\": {\n              \"$ref\": \"#/components/schemas/GeolocationStoreLocation\"\n            },\n            \"expires\": {\n              \"type\": \"number\"\n            }\n          },\n          \"additionalProperties\": false,\n          \"description\": \"This is how the store will actually store the value. It contains an optional `expires` property, which indicates when the value would expire\"\n        }\n      },\n      \"methods\": {\n        \"geolocationStoreClearAllLocations\": {\n          \"description\": \"\",\n          \"arguments\": {\n            \"$ref\": \"#/components/schemas/GeolocationStoreClearAllLocationsArgs\"\n          },\n          \"returnType\": {\n            \"type\": \"boolean\"\n          }\n        },\n        \"geolocationStoreDefaultLocationStore\": {\n          \"description\": \"\",\n          \"arguments\": {\n            \"type\": \"object\"\n          },\n          \"returnType\": {\n            \"$ref\": \"#/components/schemas/GeolocationStoreLocationResult\"\n          }\n        },\n        \"geolocationStoreGetLocation\": {\n          \"description\": \"\",\n          \"arguments\": {\n            \"$ref\": \"#/components/schemas/GeolocationStoreArgs\"\n          },\n          \"returnType\": {\n            \"$ref\": \"#/components/schemas/GeolocationStoreLocationResultOrUndefined\"\n          }\n        },\n        \"geolocationStoreHasLocation\": {\n          \"description\": \"\",\n          \"arguments\": {\n            \"$ref\": \"#/components/schemas/GeolocationStoreArgs\"\n          },\n          \"returnType\": {\n            \"type\": \"boolean\"\n          }\n        },\n        \"geolocationStorePersistLocation\": {\n          \"description\": \"\",\n          \"arguments\": {\n            \"$ref\": \"#/components/schemas/GeolocationStoreLocationPersistArgs\"\n          },\n          \"returnType\": {\n            \"$ref\": \"#/components/schemas/GeolocationStoreLocationResultIValueData\"\n          }\n        },\n        \"geolocationStoreRemoveLocation\": {\n          \"description\": \"\",\n          \"arguments\": {\n            \"$ref\": \"#/components/schemas/GeolocationStoreArgs\"\n          },\n          \"returnType\": {\n            \"type\": \"boolean\"\n          }\n        }\n      }\n    }\n  }\n}\n","/**\n * @public\n */\nconst schema = require('../plugin.schema.json')\nexport { schema }\nexport { GeolocationStore, geolocationStoreMethods } from './agent/GeolocationStore'\nexport * from './types/IGeolocationStore'\n","import { IAgentPlugin } from '@veramo/core'\nimport {\n  GeolocationStoreArgs,\n  GeolocationStoreClearAllLocationsArgs,\n  GeolocationStoreLocation,\n  GeolocationStoreLocationPersistArgs,\n  GeolocationStoreLocationResult,\n  GeolocationStoreLocationResultIValueData,\n  GeolocationStoreLocationResultOrUndefined,\n  IGeolocationStore,\n  schema,\n} from '../index'\n\nimport { IKeyValueStore, KeyValueStore, ValueStoreType } from '@sphereon/ssi-sdk.kv-store-temp'\n\nexport const geolocationStoreMethods: Array<string> = [\n  'geolocationStorePersistLocation',\n  'geolocationStoreHasLocation',\n  'geolocationStoreRemoveLocation',\n  'geolocationStoreClearAllLocations',\n  'geolocationStoreGetLocation',\n  'geolocationStoreDefaultLocationStore',\n]\n\n/**\n * {@inheritDoc IGeolocationStore}\n */\nexport class GeolocationStore implements IAgentPlugin {\n  readonly schema = schema.IAnomalyDetectionStore\n  private readonly defaultStoreId: string\n  private readonly defaultNamespace: string\n  private readonly _dnsLookupStore: Map<string, IKeyValueStore<GeolocationStoreLocation>>\n\n  readonly methods: IGeolocationStore = {\n    geolocationStorePersistLocation: this.geolocationStorePersistLocation.bind(this),\n    geolocationStoreHasLocation: this.geolocationStoreHasLocation.bind(this),\n    geolocationStoreRemoveLocation: this.geolocationStoreRemoveLocation.bind(this),\n    geolocationStoreClearAllLocations: this.geolocationStoreClearAllLocations.bind(this),\n    geolocationStoreGetLocation: this.geolocationStoreGetLocation.bind(this),\n    geolocationStoreDefaultLocationStore: this.geolocationStoreDefaultLocationStore.bind(this),\n  }\n\n  constructor(args: {\n    defaultStoreId?: string\n    defaultNamespace?: string\n    dnsLookupStore?: Map<string, IKeyValueStore<GeolocationStoreLocation>> | IKeyValueStore<GeolocationStoreLocation>\n  }) {\n    this.defaultStoreId = args?.defaultStoreId ?? '_default'\n    this.defaultNamespace = args?.defaultNamespace ?? 'anomaly-detection'\n\n    if (args?.dnsLookupStore !== undefined && args?.dnsLookupStore !== null && args.dnsLookupStore instanceof Map) {\n      this._dnsLookupStore = args.dnsLookupStore\n    } else {\n      this._dnsLookupStore = new Map().set(\n        this.defaultStoreId,\n        new KeyValueStore({\n          namespace: this.defaultNamespace,\n          store: new Map<string, GeolocationStoreLocation>(),\n        }),\n      )\n    }\n  }\n\n  private async geolocationStorePersistLocation(args: GeolocationStoreLocationPersistArgs): Promise<GeolocationStoreLocationResultIValueData> {\n    const storeId = this.storeIdStr(args)\n    const namespace = this.namespaceStr(args)\n    const { ipOrHostname, locationArgs, ttl } = args\n\n    if (args?.validation !== false) {\n      // TODO\n    }\n    const existing = await this.store({ stores: this._dnsLookupStore, storeId }).getAsValueData(\n      this.prefix({\n        namespace,\n        ipOrHostname,\n      }),\n    )\n    if (!existing.value || (existing.value && args?.overwriteExisting !== false)) {\n      return await this.store({ stores: this._dnsLookupStore, storeId }).set(\n        this.prefix({\n          namespace,\n          ipOrHostname,\n        }),\n        locationArgs,\n        ttl,\n      )\n    }\n    return existing\n  }\n\n  private async geolocationStoreHasLocation(args: GeolocationStoreArgs): Promise<boolean> {\n    const { storeId, namespace, ipOrHostname } = { ...args }\n    return this.store({ stores: this._dnsLookupStore, storeId }).has(this.prefix({ namespace, ipOrHostname }))\n  }\n\n  private async geolocationStoreRemoveLocation(args: GeolocationStoreArgs): Promise<boolean> {\n    const { storeId, namespace, ipOrHostname } = { ...args }\n    return this.store({ stores: this._dnsLookupStore, storeId }).delete(this.prefix({ namespace, ipOrHostname }))\n  }\n\n  private async geolocationStoreClearAllLocations(args: GeolocationStoreClearAllLocationsArgs): Promise<boolean> {\n    const { storeId } = { ...args }\n    return await this.store({ stores: this._dnsLookupStore, storeId })\n      .clear()\n      .then(() => true)\n  }\n\n  private async geolocationStoreGetLocation(args: GeolocationStoreArgs): Promise<GeolocationStoreLocationResultOrUndefined> {\n    const { storeId, namespace, ipOrHostname } = { ...args }\n    return this.store<GeolocationStoreLocation>({\n      stores: this._dnsLookupStore,\n      storeId,\n    }).get(this.prefix({ namespace, ipOrHostname }))\n  }\n\n  private store<T extends ValueStoreType>(args: { stores: Map<string, IKeyValueStore<T>>; storeId?: string }): IKeyValueStore<T> {\n    const storeId = this.storeIdStr({ storeId: args.storeId })\n    const store = args.stores.get(storeId)\n    if (!store) {\n      throw Error(`Could not get geolocation store: ${storeId}`)\n    }\n    return store\n  }\n\n  private storeIdStr({ storeId }: { storeId?: string }): string {\n    return storeId ?? this.defaultStoreId\n  }\n\n  private geolocationStoreDefaultLocationStore(): Promise<GeolocationStoreLocationResult> {\n    return Promise.resolve(this.store({ stores: this._dnsLookupStore, storeId: this.defaultStoreId }))\n  }\n\n  private namespaceStr({ namespace }: { namespace?: string }): string {\n    return namespace ?? this.defaultNamespace\n  }\n\n  private prefix({ namespace, ipOrHostname }: { namespace?: string; ipOrHostname: string }): string {\n    return `${this.namespaceStr({ namespace })}:${ipOrHostname}`\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA,gCAAAA,SAAA;AAAA,IAAAA,QAAA;AAAA,MACE,mBAAqB;AAAA,QACnB,YAAc;AAAA,UACZ,SAAW;AAAA,YACT,uCAAyC;AAAA,cACvC,MAAQ;AAAA,YACV;AAAA,YACA,wCAA0C;AAAA,cACxC,MAAQ;AAAA,cACR,YAAc;AAAA,gBACZ,SAAW;AAAA,kBACT,MAAQ;AAAA,gBACV;AAAA,cACF;AAAA,cACA,UAAY,CAAC,SAAS;AAAA,cACtB,sBAAwB;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A;AACxD,UAAMD,QAAQP,KAAKgB,OAAOS,IAAIjB,OAAAA;AAC9B,QAAI,CAACD,OAAO;AACV,YAAMmB,MAAM,oCAAoClB,OAAAA,EAAS;IAC3D;AACA,WAAOD;EACT;EAEQE,WAAW,EAAED,QAAO,GAAkC;AAC5D,WAAOA,WAAW,KAAKpB;EACzB;EAEQU,uCAAgF;AACtF,WAAO6B,QAAQC,QAAQ,KAAKrB,MAAM;MAAES,QAAQ,KAAK1B;MAAiBkB,SAAS,KAAKpB;IAAe,CAAA,CAAA;EACjG;EAEQsB,aAAa,EAAEJ,UAAS,GAAoC;AAClE,WAAOA,aAAa,KAAKjB;EAC3B;EAEQ6B,OAAO,EAAEZ,WAAWK,aAAY,GAA0D;AAChG,WAAO,GAAG,KAAKD,aAAa;MAAEJ;IAAU,CAAA,CAAA,IAAMK,YAAAA;EAChD;AACF;;;ADxIA,IAAMkB,SAASC;","names":["module","geolocationStoreMethods","GeolocationStore","schema","IAnomalyDetectionStore","defaultStoreId","defaultNamespace","_dnsLookupStore","methods","geolocationStorePersistLocation","bind","geolocationStoreHasLocation","geolocationStoreRemoveLocation","geolocationStoreClearAllLocations","geolocationStoreGetLocation","geolocationStoreDefaultLocationStore","constructor","args","dnsLookupStore","undefined","Map","set","KeyValueStore","namespace","store","storeId","storeIdStr","namespaceStr","ipOrHostname","locationArgs","ttl","validation","existing","stores","getAsValueData","prefix","value","overwriteExisting","has","delete","clear","then","get","Error","Promise","resolve","schema","require"]}