sliftutils 0.14.0 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.cursorrules +8 -0
- package/index.d.ts +8 -0
- package/misc/getSecret.d.ts +1 -0
- package/misc/getSecret.ts +89 -19
- package/misc/matchFilter.d.ts +3 -0
- package/misc/matchFilter.ts +15 -0
- package/package.json +1 -1
- package/web/browser.tsx +18 -0
package/.cursorrules
CHANGED
|
@@ -79,6 +79,14 @@ Coding Styles
|
|
|
79
79
|
|
|
80
80
|
Never use environment variables. All configuration should be on the disk, or, if specific to a current run, passed as command line parameters.
|
|
81
81
|
|
|
82
|
+
Never use inline styles, always use the CSS helper.
|
|
83
|
+
|
|
84
|
+
Don't use as any.
|
|
85
|
+
|
|
86
|
+
DO NOT redeclare constants and copy their value. JUST IMPORT THEM!
|
|
87
|
+
|
|
88
|
+
DO NOT redeclare types. JUST IMPORT THEM!
|
|
89
|
+
|
|
82
90
|
|
|
83
91
|
General Styling
|
|
84
92
|
Never use em or rem. Only use px or vw/vh/%.
|
package/index.d.ts
CHANGED
|
@@ -19,6 +19,7 @@ declare module "sliftutils/misc/fs" {
|
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
declare module "sliftutils/misc/getSecret" {
|
|
22
|
+
export declare function resetSecret(key: string): void;
|
|
22
23
|
export declare const getSecret: {
|
|
23
24
|
(key: string): Promise<string>;
|
|
24
25
|
clear(key: string): void;
|
|
@@ -30,6 +31,13 @@ declare module "sliftutils/misc/getSecret" {
|
|
|
30
31
|
|
|
31
32
|
}
|
|
32
33
|
|
|
34
|
+
declare module "sliftutils/misc/matchFilter" {
|
|
35
|
+
export declare function matchFilter(filter: {
|
|
36
|
+
value: string;
|
|
37
|
+
}, value: string): boolean;
|
|
38
|
+
|
|
39
|
+
}
|
|
40
|
+
|
|
33
41
|
declare module "sliftutils/misc/types" {
|
|
34
42
|
export declare function isDefined<T>(value: T | undefined | null): value is T;
|
|
35
43
|
|
package/misc/getSecret.d.ts
CHANGED
package/misc/getSecret.ts
CHANGED
|
@@ -1,28 +1,98 @@
|
|
|
1
1
|
|
|
2
2
|
import { cache } from "socket-function/src/caching";
|
|
3
|
-
import
|
|
4
|
-
import fs from "fs";
|
|
3
|
+
import { isNode } from "typesafecss";
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
const secretStoragePrefix = "secret_";
|
|
6
|
+
|
|
7
|
+
function getStorageKey(key: string) {
|
|
8
|
+
return secretStoragePrefix + key.replace(/[\/\\\.]/g, "_");
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function resetSecret(key: string) {
|
|
12
|
+
if (isNode()) {
|
|
13
|
+
throw new Error("resetSecret is only supported in the browser");
|
|
11
14
|
}
|
|
15
|
+
localStorage.removeItem(getStorageKey(key));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const getSecret = cache(async function getSecret(key: string): Promise<string> {
|
|
19
|
+
if (isNode()) {
|
|
20
|
+
const os = await import("os");
|
|
21
|
+
const fs = await import("fs");
|
|
22
|
+
const jsonIndex = key.indexOf(".json");
|
|
23
|
+
if (jsonIndex === -1) {
|
|
24
|
+
const filePath = os.homedir() + "/" + key;
|
|
25
|
+
return fs.readFileSync(filePath, "utf-8").trim();
|
|
26
|
+
}
|
|
12
27
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
28
|
+
const pathPart = key.slice(0, jsonIndex + ".json".length);
|
|
29
|
+
const filePath = os.homedir() + "/" + pathPart;
|
|
30
|
+
const contents = fs.readFileSync(filePath, "utf-8");
|
|
31
|
+
const json = JSON.parse(contents);
|
|
17
32
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
33
|
+
const keyPart = key.slice(jsonIndex + ".json.".length);
|
|
34
|
+
if (!keyPart) {
|
|
35
|
+
return JSON.stringify(json);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const value = json[keyPart];
|
|
39
|
+
if (value === undefined) {
|
|
40
|
+
throw new Error(`Expected key "${keyPart}" in ${filePath}, was undefined`);
|
|
41
|
+
}
|
|
42
|
+
return String(value);
|
|
21
43
|
}
|
|
22
44
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
45
|
+
// Browser implementation
|
|
46
|
+
const storageKey = getStorageKey(key);
|
|
47
|
+
const cached = localStorage.getItem(storageKey);
|
|
48
|
+
if (cached) {
|
|
49
|
+
return cached;
|
|
26
50
|
}
|
|
27
|
-
|
|
28
|
-
|
|
51
|
+
|
|
52
|
+
// Show modal to prompt user for secret
|
|
53
|
+
const { showFullscreenModal, FullscreenModal } = await import("../render-utils/FullscreenModal");
|
|
54
|
+
const { showModal } = await import("../render-utils/modal");
|
|
55
|
+
const { observable } = await import("mobx");
|
|
56
|
+
const preact = await import("preact");
|
|
57
|
+
|
|
58
|
+
return new Promise<string>((resolve) => {
|
|
59
|
+
const state = observable({
|
|
60
|
+
value: "",
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const { close } = showModal({
|
|
64
|
+
contents: preact.createElement(FullscreenModal, {
|
|
65
|
+
onCancel: () => {
|
|
66
|
+
// Don't allow cancel without a value
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
preact.createElement("div", { style: { display: "flex", flexDirection: "column", gap: 10 } },
|
|
70
|
+
preact.createElement("div", { style: { fontWeight: "bold" } }, `Enter secret for: ${key}`),
|
|
71
|
+
preact.createElement("input", {
|
|
72
|
+
style: { padding: 10, fontSize: 16 },
|
|
73
|
+
onInput: (e: Event) => {
|
|
74
|
+
state.value = (e.target as HTMLInputElement).value;
|
|
75
|
+
},
|
|
76
|
+
onKeyDown: (e: KeyboardEvent) => {
|
|
77
|
+
if (e.code === "Enter" && state.value) {
|
|
78
|
+
localStorage.setItem(storageKey, state.value);
|
|
79
|
+
close();
|
|
80
|
+
resolve(state.value);
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
}),
|
|
84
|
+
preact.createElement("button", {
|
|
85
|
+
style: { padding: 10, fontSize: 16, cursor: "pointer" },
|
|
86
|
+
onClick: () => {
|
|
87
|
+
if (state.value) {
|
|
88
|
+
localStorage.setItem(storageKey, state.value);
|
|
89
|
+
close();
|
|
90
|
+
resolve(state.value);
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
}, "Save"),
|
|
94
|
+
),
|
|
95
|
+
),
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export function matchFilter(filter: { value: string }, value: string) {
|
|
2
|
+
let filterValue = filter.value.toLowerCase().trim();
|
|
3
|
+
if (!filterValue) return true;
|
|
4
|
+
value = value.toLowerCase().trim();
|
|
5
|
+
return filterValue.split("|").some(part =>
|
|
6
|
+
part.split("&").every(part => {
|
|
7
|
+
part = part.trim();
|
|
8
|
+
if (part.startsWith("!")) {
|
|
9
|
+
part = part.slice(1).trim();
|
|
10
|
+
return !value.includes(part);
|
|
11
|
+
}
|
|
12
|
+
return value.includes(part);
|
|
13
|
+
})
|
|
14
|
+
);
|
|
15
|
+
}
|
package/package.json
CHANGED
package/web/browser.tsx
CHANGED
|
@@ -14,6 +14,24 @@ class App extends preact.Component {
|
|
|
14
14
|
count: 0,
|
|
15
15
|
});
|
|
16
16
|
|
|
17
|
+
onKeyDown = (e: KeyboardEvent) => {
|
|
18
|
+
// Skip if the current target is an ipnut
|
|
19
|
+
if (e.target instanceof HTMLInputElement || e.target instanceof HTMLTextAreaElement) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
let hotkeySelector = `[data-hotkey="${e.code}"]`;
|
|
23
|
+
let elements = document.querySelectorAll(hotkeySelector);
|
|
24
|
+
for (let element of elements) {
|
|
25
|
+
(element as HTMLElement).click();
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
componentDidMount(): void {
|
|
29
|
+
document.addEventListener("keydown", this.onKeyDown);
|
|
30
|
+
}
|
|
31
|
+
componentWillUnmount(): void {
|
|
32
|
+
document.removeEventListener("keydown", this.onKeyDown);
|
|
33
|
+
}
|
|
34
|
+
|
|
17
35
|
render() {
|
|
18
36
|
return (
|
|
19
37
|
<div className={css.pad2(20)}>
|