Skip to content

Commit

Permalink
remove debouncing
Browse files Browse the repository at this point in the history
  • Loading branch information
benvp committed Jan 24, 2024
1 parent 0b46b80 commit 43f44d5
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 74 deletions.
156 changes: 110 additions & 46 deletions dist/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,58 @@ var __importStar = (this && this.__importStar) || function (mod) {
__setModuleDefault(result, mod);
return result;
};
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 __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const op_js_1 = require("@1password/op-js");
const cache = __importStar(require("./cache"));
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const lodash_1 = require("lodash");
const OP_PLUGIN_CONFIG_KEY = '__op_plugin';
const debouncedFetchEntry = (0, lodash_1.debounce)(fetchEntry, 500);
const fetchSecretTemplateTag = {
exports.templateTags = void 0;
var op_js_1 = require("@1password/op-js");
var cache = __importStar(require("./cache"));
var path_1 = __importDefault(require("path"));
var fs_1 = __importDefault(require("fs"));
var OP_PLUGIN_CONFIG_KEY = '__op_plugin';
var fetchSecretTemplateTag = {
name: 'op',
displayName: '1Password => Fetch Secret',
liveDisplayName: (args) => {
liveDisplayName: function (args) {
var _a, _b;
return `1Password => ${(_b = (_a = args[0]) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : '--'}`;
return "1Password => ".concat((_b = (_a = args[0]) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : '--');
},
description: 'Fetch a secret from your 1Password vault',
args: [
Expand All @@ -50,44 +85,73 @@ const fetchSecretTemplateTag = {
placeholder: "e.g. 'op://team-name.1password.com/vault-name/item-name'",
},
],
async run(context, reference) {
const config = context.context[OP_PLUGIN_CONFIG_KEY];
await checkCli(config === null || config === void 0 ? void 0 : config.cliPath);
const entry = await debouncedFetchEntry(reference);
return entry;
run: function (context, reference) {
return __awaiter(this, void 0, void 0, function () {
var config, entry;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
config = context.context[OP_PLUGIN_CONFIG_KEY];
return [4, checkCli(config === null || config === void 0 ? void 0 : config.cliPath)];
case 1:
_a.sent();
return [4, fetchEntry(reference)];
case 2:
entry = _a.sent();
return [2, entry];
}
});
});
},
};
async function checkCli(cliPath) {
if (cache.opCliInstalled() !== true) {
try {
if (cliPath && !fs_1.default.existsSync(cliPath)) {
throw new Error(`The file at path ${cliPath} does not exist.`);
function checkCli(cliPath) {
return __awaiter(this, void 0, void 0, function () {
var pathToAdd, stats, e_1, error;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!(cache.opCliInstalled() !== true)) return [3, 4];
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
if (cliPath && !fs_1.default.existsSync(cliPath)) {
throw new Error("The file at path ".concat(cliPath, " does not exist."));
}
pathToAdd = cliPath;
if (cliPath) {
stats = fs_1.default.statSync(cliPath);
if (stats.isFile()) {
pathToAdd = path_1.default.dirname(cliPath);
}
}
process.env.PATH = pathToAdd ? "".concat(pathToAdd, ":").concat(process.env.PATH) : process.env.PATH;
return [4, (0, op_js_1.validateCli)()];
case 2:
_a.sent();
cache.writeOpCliInstalled(true);
return [3, 4];
case 3:
e_1 = _a.sent();
error = new Error("There was an issue with the 1Password CLI. If you have the op CLI installed using e.g. Homebrew, please set the '__op_plugin.cliPath' environment variable to the directory containing the 'op' binary. (e.g. /opt/homebrew/bin/op). Error details: ".concat(e_1.message));
error.stack = e_1.stack;
throw error;
case 4: return [2];
}
let pathToAdd = cliPath;
if (cliPath) {
const stats = fs_1.default.statSync(cliPath);
if (stats.isFile()) {
pathToAdd = path_1.default.dirname(cliPath);
}
}
process.env.PATH = pathToAdd ? `${pathToAdd}:${process.env.PATH}` : process.env.PATH;
await (0, op_js_1.validateCli)();
cache.writeOpCliInstalled(true);
}
catch (e) {
const error = new Error(`There was an issue with the 1Password CLI. If you have the op CLI installed using e.g. Homebrew, please set the '__op_plugin.cliPath' environment variable to the directory containing the 'op' binary. (e.g. /opt/homebrew/bin/op). Error details: ${e.message}`);
error.stack = e.stack;
throw error;
}
}
});
});
}
async function fetchEntry(ref) {
const existing = cache.getEntry(ref);
if (existing) {
return existing;
}
const entry = op_js_1.read.parse(ref);
cache.writeEntry(ref, entry);
return entry;
function fetchEntry(ref) {
return __awaiter(this, void 0, void 0, function () {
var existing, entry;
return __generator(this, function (_a) {
existing = cache.getEntry(ref);
if (existing) {
return [2, existing];
}
entry = op_js_1.read.parse(ref);
cache.writeEntry(ref, entry);
return [2, entry];
});
});
}
module.exports.templateTags = [fetchSecretTemplateTag];
exports.templateTags = [fetchSecretTemplateTag];
4 changes: 2 additions & 2 deletions dist/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.writeOpCliInstalled = exports.opCliInstalled = exports.getEntry = exports.writeEntry = void 0;
const node_cache_1 = __importDefault(require("node-cache"));
const cache = new node_cache_1.default({ stdTTL: 60 * 60 });
var node_cache_1 = __importDefault(require("node-cache"));
var cache = new node_cache_1.default({ stdTTL: 60 * 60 });
function writeEntry(ref, value) {
return cache.set(ref, value);
}
Expand Down
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
{
"name": "insomnia-plugin-op",
"version": "0.1.1",
"version": "0.1.2",
"description": "Insomnia Plugin to integrate with 1Password CLI (op)",
"main": "./dist/app.js",
"type": "commonjs",
"scripts": {
"build": "tsc",
"dev": "tsc -w",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": ["insomnia", "insomnia plugin", "1password"],
"keywords": [
"insomnia",
"insomnia plugin",
"1password"
],
"author": {
"name": "Benjamin von Polheim",
"email": "[email protected]",
Expand All @@ -19,7 +24,7 @@
"url": "https://github.com/benvp/insomnia-plugin-op/issues"
},
"license": "MIT",
"homepage": "https://github.com/benvp/insomnia-plugin-op",
"homepage": "https://github.com/benvp/insomnia-pluggpin-op",
"repository": {
"type": "git",
"url": "https://github.com/benvp/insomnia-plugin-op"
Expand All @@ -39,7 +44,6 @@
"unlisted": false
},
"devDependencies": {
"@types/lodash": "^4.14.202",
"@types/node": "^20.11.5",
"@typescript-eslint/eslint-plugin": "^6.19.1",
"@typescript-eslint/parser": "^6.19.1",
Expand All @@ -51,7 +55,6 @@
},
"dependencies": {
"@1password/op-js": "^0.1.11",
"lodash": "^4.17.21",
"node-cache": "^5.1.2"
}
}
14 changes: 0 additions & 14 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ import { validateCli, read } from '@1password/op-js';
import * as cache from './cache';
import path from 'path';
import fs from 'fs';
import { debounce } from 'lodash';

type PluginConfig = {
cliPath?: string;
};

const OP_PLUGIN_CONFIG_KEY = '__op_plugin';

const debouncedFetchEntry = debounce(fetchEntry, 500);

const fetchSecretTemplateTag = {
name: 'op',
displayName: '1Password => Fetch Secret',
Expand All @@ -32,7 +29,7 @@ const fetchSecretTemplateTag = {
const config = context.context[OP_PLUGIN_CONFIG_KEY] as PluginConfig | undefined;

await checkCli(config?.cliPath);
const entry = await debouncedFetchEntry(reference);
const entry = await fetchEntry(reference);

return entry;
},
Expand Down Expand Up @@ -84,4 +81,4 @@ async function fetchEntry(ref: string) {
return entry;
}

module.exports.templateTags = [fetchSecretTemplateTag];
export const templateTags = [fetchSecretTemplateTag];
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"compilerOptions": {
"baseUrl": ".",
"outDir": "dist",
"target": "ES2017",
"lib": ["esnext"],
"strict": true,
"forceConsistentCasingInFileNames": true,
Expand All @@ -12,7 +11,7 @@
"moduleResolution": "node",
"isolatedModules": true,
"noUnusedLocals": false,
"removeComments": true
"removeComments": true,
},
"exclude": ["node_modules"],
"include": ["src/**/*"]
Expand Down

0 comments on commit 43f44d5

Please sign in to comment.