Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@grexie/keystore

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@grexie/keystore - npm Package Compare versions

Comparing version 0.1.25 to 0.1.26

20

lib/index.js

@@ -1,18 +0,2 @@

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
__exportStar(require("./types"), exports);
__exportStar(require("./memory"), exports);
export * from './types';
export * from './memory';

166

lib/memory.js

@@ -1,109 +0,67 @@

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _instances, _start, _secret, _secretId, _secrets, _persistor, _hydrator, _ttl, _setSecret, _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MemoryProvider = void 0;
const events_1 = require("events");
const MemoryProvider = (_a = class extends events_1.EventEmitter {
constructor({ persistor, hydrator, ttl = 0, initialSecret, }) {
super();
_instances.add(this);
_start.set(this, void 0);
_secret.set(this, void 0);
_secretId.set(this, void 0);
_secrets.set(this, {});
_persistor.set(this, void 0);
_hydrator.set(this, void 0);
_ttl.set(this, void 0);
__classPrivateFieldSet(this, _persistor, persistor, "f");
__classPrivateFieldSet(this, _hydrator, hydrator, "f");
__classPrivateFieldSet(this, _ttl, ttl, "f");
const resolver = {};
__classPrivateFieldSet(this, _start, new Promise(resolve => Object.assign(resolver, { resolve })), "f");
if (initialSecret) {
this.setSecret(initialSecret).finally(resolver.resolve);
import { EventEmitter } from 'events';
const MemoryProvider = class extends EventEmitter {
#start;
#secret;
#secretId;
#secrets = {};
#persistor;
#hydrator;
#ttl;
constructor({ persistor, hydrator, ttl = 0, initialSecret, }) {
super();
this.#persistor = persistor;
this.#hydrator = hydrator;
this.#ttl = ttl;
const resolver = {};
this.#start = new Promise(resolve => Object.assign(resolver, { resolve }));
if (initialSecret) {
this.setSecret(initialSecret).finally(resolver.resolve);
}
else {
this.rotateSecret().finally(resolver.resolve);
}
}
get secret() {
return this.#start.then(() => this.#secret);
}
async #setSecret(id, key) {
if (id === this.#secretId && this.#secrets[id].timeout) {
clearTimeout(this.#secrets[id].timeout);
this.#secrets[id].timeout = undefined;
}
this.#secrets[id] = { key };
if (this.#secretId) {
const oldSecretId = this.#secretId;
if (this.#ttl > 0) {
this.#secrets[oldSecretId].timeout = setTimeout(() => {
delete this.#secrets[oldSecretId];
}, this.#ttl);
}
else {
this.rotateSecret().finally(resolver.resolve);
else if (this.#ttl === 0) {
delete this.#secrets[oldSecretId];
}
}
get secret() {
return __classPrivateFieldGet(this, _start, "f").then(() => __classPrivateFieldGet(this, _secret, "f"));
this.#secretId = id;
this.#secret = Promise.resolve(this.#hydrator(key));
this.emit('update', await this.#secret);
return this.#secret;
}
async setSecret(secret) {
const key = await this.#persistor(secret);
if (!key) {
return this.#secret;
}
setSecret(secret) {
return __awaiter(this, void 0, void 0, function* () {
const key = yield __classPrivateFieldGet(this, _persistor, "f").call(this, secret);
if (!key) {
return __classPrivateFieldGet(this, _secret, "f");
}
return __classPrivateFieldGet(this, _instances, "m", _setSecret).call(this, key.id, key.key);
});
return this.#setSecret(key.id, key.key);
}
async rotateSecret() {
return this.setSecret(null);
}
async restoreSecret(id) {
if (!this.#secrets[id]) {
throw new Error(`secret ${id} not found`);
}
rotateSecret() {
return __awaiter(this, void 0, void 0, function* () {
return this.setSecret(null);
});
}
restoreSecret(id) {
return __awaiter(this, void 0, void 0, function* () {
if (!__classPrivateFieldGet(this, _secrets, "f")[id]) {
throw new Error(`secret ${id} not found`);
}
const { key } = __classPrivateFieldGet(this, _secrets, "f")[id];
return __classPrivateFieldGet(this, _instances, "m", _setSecret).call(this, id, key);
});
}
},
_start = new WeakMap(),
_secret = new WeakMap(),
_secretId = new WeakMap(),
_secrets = new WeakMap(),
_persistor = new WeakMap(),
_hydrator = new WeakMap(),
_ttl = new WeakMap(),
_instances = new WeakSet(),
_setSecret = function _setSecret(id, key) {
return __awaiter(this, void 0, void 0, function* () {
if (id === __classPrivateFieldGet(this, _secretId, "f") && __classPrivateFieldGet(this, _secrets, "f")[id].timeout) {
clearTimeout(__classPrivateFieldGet(this, _secrets, "f")[id].timeout);
__classPrivateFieldGet(this, _secrets, "f")[id].timeout = undefined;
}
__classPrivateFieldGet(this, _secrets, "f")[id] = { key };
if (__classPrivateFieldGet(this, _secretId, "f")) {
const oldSecretId = __classPrivateFieldGet(this, _secretId, "f");
if (__classPrivateFieldGet(this, _ttl, "f") > 0) {
__classPrivateFieldGet(this, _secrets, "f")[oldSecretId].timeout = setTimeout(() => {
delete __classPrivateFieldGet(this, _secrets, "f")[oldSecretId];
}, __classPrivateFieldGet(this, _ttl, "f"));
}
else if (__classPrivateFieldGet(this, _ttl, "f") === 0) {
delete __classPrivateFieldGet(this, _secrets, "f")[oldSecretId];
}
}
__classPrivateFieldSet(this, _secretId, id, "f");
__classPrivateFieldSet(this, _secret, Promise.resolve(__classPrivateFieldGet(this, _hydrator, "f").call(this, key)), "f");
this.emit('update', yield __classPrivateFieldGet(this, _secret, "f"));
return __classPrivateFieldGet(this, _secret, "f");
});
},
_a);
exports.MemoryProvider = MemoryProvider;
const { key } = this.#secrets[id];
return this.#setSecret(id, key);
}
};
export { MemoryProvider };

@@ -1,51 +0,22 @@

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
export const createKeyStoreFactory = ({ persistor, hydrator }) => ({ provider: Provider, ...options }) => new Provider({
...options,
persistor,
hydrator,
});
export const createJSONKeyStoreFactory = ({ idField, create, }) => createKeyStoreFactory({
async persistor(key) {
if (!key) {
if (!create) {
return;
}
key = create();
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createJSONKeyStoreFactory = exports.createKeyStoreFactory = void 0;
const createKeyStoreFactory = ({ persistor, hydrator }) => (_a) => {
var { provider: Provider } = _a, options = __rest(_a, ["provider"]);
return new Provider(Object.assign(Object.assign({}, options), { persistor,
hydrator }));
};
exports.createKeyStoreFactory = createKeyStoreFactory;
const createJSONKeyStoreFactory = ({ idField, create, }) => (0, exports.createKeyStoreFactory)({
persistor(key) {
return __awaiter(this, void 0, void 0, function* () {
if (!key) {
if (!create) {
return;
}
key = create();
}
return {
id: key[idField],
key: Buffer.from(JSON.stringify(key), 'utf8'),
};
});
return {
id: key[idField],
key: Buffer.from(JSON.stringify(key), 'utf8'),
};
},
hydrator(buffer) {
return __awaiter(this, void 0, void 0, function* () {
return JSON.parse(buffer.toString('utf8'));
});
async hydrator(buffer) {
return JSON.parse(buffer.toString('utf8'));
},
});
exports.createJSONKeyStoreFactory = createJSONKeyStoreFactory;
{
"name": "@grexie/keystore",
"version": "0.1.25",
"version": "0.1.26",
"license": "MIT",
"repository": "github:grexie/keystore/packages/keystore",
"main": "lib/index.js",
"type": "module",
"module": "lib/index.js",
"types": "lib/index.d.ts",

@@ -8,0 +9,0 @@ "scripts": {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc