@sphereon/ssi-sdk.geolocation-store
Version:
344 lines (340 loc) • 11 kB
JavaScript
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
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;
};
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// plugin.schema.json
var require_plugin_schema = __commonJS({
"plugin.schema.json"(exports, module2) {
module2.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/index.ts
var index_exports = {};
__export(index_exports, {
GeolocationStore: () => GeolocationStore,
geolocationStoreMethods: () => geolocationStoreMethods,
schema: () => schema
});
module.exports = __toCommonJS(index_exports);
// src/agent/GeolocationStore.ts
var import_ssi_sdk = require("@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 import_ssi_sdk.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();
//# sourceMappingURL=index.cjs.map