UNPKG

@sphereon/ssi-sdk.geolocation-store

Version:

324 lines (321 loc) • 10.1 kB
var __defProp = Object.defineProperty; var __getOwnPropNames = Object.getOwnPropertyNames; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __commonJS = (cb, mod) => function __require() { return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; }; // plugin.schema.json var require_plugin_schema = __commonJS({ "plugin.schema.json"(exports, module) { module.exports = { IGeolocationStore: { components: { schemas: { GeolocationStoreClearAllLocationsArgs: { $ref: '#/components/schemas/Pick<GeolocationStoreArgs,"storeId">' }, 'Pick<GeolocationStoreArgs,"storeId">': { type: "object", properties: { storeId: { type: "string" } }, required: ["storeId"], additionalProperties: false }, GeolocationStoreLocationResult: { $ref: "#/components/schemas/IKeyValueStore<GeolocationStoreLocation>" }, "IKeyValueStore<GeolocationStoreLocation>": { type: "object", additionalProperties: false, description: "A Key Value store is responsible for managing Values identified by keys." }, GeolocationStoreArgs: { type: "object", properties: { ipOrHostname: { type: "string" }, storeId: { type: "string" }, namespace: { type: "string" } }, required: ["ipOrHostname", "storeId", "namespace"], additionalProperties: false }, GeolocationStoreLocationResultOrUndefined: { anyOf: [ { $ref: "#/components/schemas/GeolocationStoreLocation" }, { not: {} } ] }, GeolocationStoreLocation: { type: "object", properties: { continent: { type: "string" }, country: { type: "string" } }, additionalProperties: false }, GeolocationStoreLocationPersistArgs: { type: "object", additionalProperties: false, properties: { locationArgs: { $ref: "#/components/schemas/GeolocationStoreLocation" }, ipOrHostname: { type: "string" }, overwriteExisting: { type: "boolean" }, validation: { type: "boolean" }, ttl: { type: "number" }, storeId: { type: "string" }, namespace: { type: "string" } }, required: ["ipOrHostname", "locationArgs"] }, GeolocationStoreLocationResultIValueData: { $ref: "#/components/schemas/IValueData<GeolocationStoreLocation>" }, "IValueData<GeolocationStoreLocation>": { type: "object", properties: { value: { $ref: "#/components/schemas/GeolocationStoreLocation" }, expires: { type: "number" } }, additionalProperties: false, description: "This is how the store will actually store the value. It contains an optional `expires` property, which indicates when the value would expire" } }, methods: { geolocationStoreClearAllLocations: { description: "", arguments: { $ref: "#/components/schemas/GeolocationStoreClearAllLocationsArgs" }, returnType: { type: "boolean" } }, geolocationStoreDefaultLocationStore: { description: "", arguments: { type: "object" }, returnType: { $ref: "#/components/schemas/GeolocationStoreLocationResult" } }, geolocationStoreGetLocation: { description: "", arguments: { $ref: "#/components/schemas/GeolocationStoreArgs" }, returnType: { $ref: "#/components/schemas/GeolocationStoreLocationResultOrUndefined" } }, geolocationStoreHasLocation: { description: "", arguments: { $ref: "#/components/schemas/GeolocationStoreArgs" }, returnType: { type: "boolean" } }, geolocationStorePersistLocation: { description: "", arguments: { $ref: "#/components/schemas/GeolocationStoreLocationPersistArgs" }, returnType: { $ref: "#/components/schemas/GeolocationStoreLocationResultIValueData" } }, geolocationStoreRemoveLocation: { description: "", arguments: { $ref: "#/components/schemas/GeolocationStoreArgs" }, returnType: { type: "boolean" } } } } } }; } }); // src/agent/GeolocationStore.ts import { KeyValueStore } from "@sphereon/ssi-sdk.kv-store-temp"; var geolocationStoreMethods = [ "geolocationStorePersistLocation", "geolocationStoreHasLocation", "geolocationStoreRemoveLocation", "geolocationStoreClearAllLocations", "geolocationStoreGetLocation", "geolocationStoreDefaultLocationStore" ]; var GeolocationStore = class { static { __name(this, "GeolocationStore"); } schema = schema.IAnomalyDetectionStore; defaultStoreId; defaultNamespace; _dnsLookupStore; methods = { geolocationStorePersistLocation: this.geolocationStorePersistLocation.bind(this), geolocationStoreHasLocation: this.geolocationStoreHasLocation.bind(this), geolocationStoreRemoveLocation: this.geolocationStoreRemoveLocation.bind(this), geolocationStoreClearAllLocations: this.geolocationStoreClearAllLocations.bind(this), geolocationStoreGetLocation: this.geolocationStoreGetLocation.bind(this), geolocationStoreDefaultLocationStore: this.geolocationStoreDefaultLocationStore.bind(this) }; constructor(args) { this.defaultStoreId = args?.defaultStoreId ?? "_default"; this.defaultNamespace = args?.defaultNamespace ?? "anomaly-detection"; if (args?.dnsLookupStore !== void 0 && args?.dnsLookupStore !== null && args.dnsLookupStore instanceof Map) { this._dnsLookupStore = args.dnsLookupStore; } else { this._dnsLookupStore = (/* @__PURE__ */ new Map()).set(this.defaultStoreId, new KeyValueStore({ namespace: this.defaultNamespace, store: /* @__PURE__ */ new Map() })); } } async geolocationStorePersistLocation(args) { const storeId = this.storeIdStr(args); const namespace = this.namespaceStr(args); const { ipOrHostname, locationArgs, ttl } = args; if (args?.validation !== false) { } const existing = await this.store({ stores: this._dnsLookupStore, storeId }).getAsValueData(this.prefix({ namespace, ipOrHostname })); if (!existing.value || existing.value && args?.overwriteExisting !== false) { return await this.store({ stores: this._dnsLookupStore, storeId }).set(this.prefix({ namespace, ipOrHostname }), locationArgs, ttl); } return existing; } async geolocationStoreHasLocation(args) { const { storeId, namespace, ipOrHostname } = { ...args }; return this.store({ stores: this._dnsLookupStore, storeId }).has(this.prefix({ namespace, ipOrHostname })); } async geolocationStoreRemoveLocation(args) { const { storeId, namespace, ipOrHostname } = { ...args }; return this.store({ stores: this._dnsLookupStore, storeId }).delete(this.prefix({ namespace, ipOrHostname })); } async geolocationStoreClearAllLocations(args) { const { storeId } = { ...args }; return await this.store({ stores: this._dnsLookupStore, storeId }).clear().then(() => true); } async geolocationStoreGetLocation(args) { const { storeId, namespace, ipOrHostname } = { ...args }; return this.store({ stores: this._dnsLookupStore, storeId }).get(this.prefix({ namespace, ipOrHostname })); } store(args) { const storeId = this.storeIdStr({ storeId: args.storeId }); const store = args.stores.get(storeId); if (!store) { throw Error(`Could not get geolocation store: ${storeId}`); } return store; } storeIdStr({ storeId }) { return storeId ?? this.defaultStoreId; } geolocationStoreDefaultLocationStore() { return Promise.resolve(this.store({ stores: this._dnsLookupStore, storeId: this.defaultStoreId })); } namespaceStr({ namespace }) { return namespace ?? this.defaultNamespace; } prefix({ namespace, ipOrHostname }) { return `${this.namespaceStr({ namespace })}:${ipOrHostname}`; } }; // src/index.ts var schema = require_plugin_schema(); export { GeolocationStore, geolocationStoreMethods, schema }; //# sourceMappingURL=index.js.map