dt-common-device
Version:
A secure and robust device management library for IoT applications
192 lines (191 loc) • 7.83 kB
JavaScript
;
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
var _, done = false;
for (var i = decorators.length - 1; i >= 0; i--) {
var context = {};
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
if (kind === "accessor") {
if (result === void 0) continue;
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
if (_ = accept(result.get)) descriptor.get = _;
if (_ = accept(result.set)) descriptor.set = _;
if (_ = accept(result.init)) initializers.unshift(_);
}
else if (_ = accept(result)) {
if (kind === "field") initializers.unshift(_);
else descriptor[key] = _;
}
}
if (target) Object.defineProperty(target, contextIn.name, descriptor);
done = true;
};
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
var useValue = arguments.length > 2;
for (var i = 0; i < initializers.length; i++) {
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
}
return useValue ? value : void 0;
};
var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) {
if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RedisUtils = void 0;
const typedi_1 = require("typedi");
const redis_1 = require("../db/redis");
let RedisUtils = (() => {
let _classDecorators = [(0, typedi_1.Service)()];
let _classDescriptor;
let _classExtraInitializers = [];
let _classThis;
var RedisUtils = _classThis = class {
constructor() {
this.client = (0, redis_1.getRedisClient)(); // singleton Redis client instance
}
/**
* Get a value from Redis
* @param key - The key to get
* @param field - The field to get
* @returns The value
*/
async hget(key, field) {
try {
const value = await this.client.hget(key, field);
if (!value) {
return null;
}
return JSON.parse(value);
}
catch (error) {
console.error(`Error getting value for key ${key}:`, error);
throw error;
}
}
/**
* Set a value in Redis with a TTL
* @param key - The key to set
* @param field - The field to set
* @param value - The value to set (JSON stringified)
* @param ttl - The TTL in seconds
* @returns The number of fields set
*/
async hsetWithTTL(key, field, value, ttl) {
try {
await this.hset(key, field, value);
await this.expire(key, ttl);
return 1;
}
catch (error) {
console.error(`Error setting value for key ${key}:`, error);
throw error;
}
}
/**
* Set a value in Redis
* @param key - The key to set
* @param field - The field to set
* @param value - The value to set (JSON stringified)
* @returns The number of fields set
*/
async hset(key, field, value) {
try {
return await this.client.hset(key, field, value);
}
catch (error) {
console.error(`Error setting value for key ${key}:`, error);
throw error;
}
}
/**
* Delete a field from a hash
* @param key - The key to delete from
* @param fields - The fields to delete
* @returns The number of fields deleted
*/
async hdel(key, ...fields) {
return await this.client.hdel(key, ...fields);
}
/**
* Set a value in Redis with a TTL
* @param key - The key to set
* @param value - The value to set (JSON stringified)
* @param ttl - The TTL in seconds
* @returns The value
*/
async set(key, value, ttl) {
return await this.client.set(key, value, "EX", ttl);
}
/**
* Get a value from Redis
* @param key - The key to get
* @returns The value
*/
async get(key) {
return await this.client.get(key);
}
/**
* Delete a key from Redis
* @param key - The key to delete
* @returns The number of keys deleted
*/
async del(key) {
return await this.client.del(key);
}
/**
* Check if a key exists
* @param key - The key to check
* @returns 1 if the key exists, 0 otherwise
*/
async exists(key) {
try {
return await this.client.exists(key);
}
catch (error) {
console.error(`Error checking existence for key ${key}:`, error);
throw error;
}
}
/**
* Set an expiration time for a key
* @param key - The key to set the expiration for
* @param seconds - The number of seconds until the key expires
* @returns 1 if the expiration was set, 0 if the key does not exist
*/
async expire(key, seconds) {
try {
return await this.client.expire(key, seconds);
}
catch (error) {
console.error(`Error setting expiration for key ${key}:`, error);
throw error;
}
}
async hgetAll(key) {
try {
return await this.client.hgetall(key);
}
catch (error) {
console.error(`Error getting all values for key ${key}:`, error);
throw error;
}
}
};
__setFunctionName(_classThis, "RedisUtils");
(() => {
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
RedisUtils = _classThis = _classDescriptor.value;
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
__runInitializers(_classThis, _classExtraInitializers);
})();
return RedisUtils = _classThis;
})();
exports.RedisUtils = RedisUtils;