1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
import { readKey } from "openpgp";
export const armored: unique symbol = Symbol();
export const binary: unique symbol = Symbol();
export type KeyFileFormat = typeof armored | typeof binary;
export interface KeyDiscoveryRules {
formats?: Partial<Record<KeyFileFormat, Set<string> | undefined>>;
recursive?: boolean | number;
}
export const DEFAULT_KEY_DISCOVERY_RULES = {
formats: {
[armored]: new Set(["asc"]),
[binary]: new Set(["gpg"]),
},
} satisfies KeyDiscoveryRules;
export async function* createKeysFromFs(
key: string | URL,
rules: KeyDiscoveryRules = DEFAULT_KEY_DISCOVERY_RULES,
coders: { decoder?: TextDecoder; encoder?: TextEncoder } = {},
): AsyncGenerator<Awaited<ReturnType<typeof readKey>>, void, void> {
key = new URL(key);
validateKeyDiscoveryRules(rules);
const stat = await Deno.stat(key);
if (stat.isDirectory) {
const generator = createKeysFromDir(key, rules, coders);
yield* generator;
} else if (stat.isFile) {
const period = key.pathname.lastIndexOf(".");
const ext = period === -1 ? "" : key.pathname.slice(period + 1);
if (
rules.formats?.[armored] !== undefined && rules.formats[armored].has(ext)
) {
yield createKeyFromFile(
key,
armored,
coders?.decoder,
);
} else if (
rules.formats?.[binary] !== undefined && rules.formats[binary].has(ext)
) {
yield createKeyFromFile(
key,
binary,
coders?.encoder,
);
}
}
}
export async function* createKeysFromDir(
key: string | URL,
rules: KeyDiscoveryRules = DEFAULT_KEY_DISCOVERY_RULES,
coders: { decoder?: TextDecoder; encoder?: TextEncoder } = {},
): AsyncGenerator<Awaited<ReturnType<typeof readKey>>, void, void> {
key = new URL(key);
validateKeyDiscoveryRules(rules);
for await (const dirEntry of Deno.readDir(key)) {
const filePath = new URL(dirEntry.name, key);
if (dirEntry.isFile) {
const period = filePath.pathname.lastIndexOf(".");
const ext = period === -1 ? "" : filePath.pathname.slice(period + 1);
if (
rules.formats?.[armored] !== undefined &&
rules.formats[armored].has(ext)
) {
yield createKeyFromFile(
filePath,
armored,
coders?.decoder,
);
} else if (
rules.formats?.[binary] !== undefined && rules.formats[binary].has(ext)
) {
yield createKeyFromFile(
filePath,
binary,
coders?.encoder,
);
}
} else if (dirEntry.isDirectory) {
const depth = typeof rules.recursive === "number"
? rules.recursive
: rules.recursive
? Infinity
: 0;
if (depth > 0) {
yield* createKeysFromDir(filePath, {
...rules,
recursive: depth - 1,
}, coders);
}
}
}
}
export async function createKeyFromFile(
key: string | URL,
type: typeof armored,
coder?: TextDecoder,
): ReturnType<typeof readKey>;
export async function createKeyFromFile(
key: string | URL,
type: typeof binary,
coder?: TextEncoder,
): ReturnType<typeof readKey>;
export async function createKeyFromFile(
key: string | URL,
type: typeof armored | typeof binary,
coder?: TextDecoder | TextEncoder,
): ReturnType<typeof readKey> {
switch (type) {
case armored:
return await Deno.readTextFile(key).then((key) =>
createKeyFromArmor(key, coder as TextDecoder)
);
case binary:
return await Deno.readFile(key).then((key) =>
createKeyFromBinary(key, coder as TextEncoder)
);
}
}
export function createKeyFromArmor(
key: string | Uint8Array,
decoder?: TextDecoder,
): ReturnType<typeof readKey> {
return readKey({
armoredKey: typeof key === "string"
? key
: (decoder ?? new TextDecoder()).decode(key),
});
}
export function createKeyFromBinary(
key: string | Uint8Array,
encoder?: TextEncoder,
): ReturnType<typeof readKey> {
return readKey({
binaryKey: typeof key === "string"
? (encoder ?? new TextEncoder()).encode(key)
: key,
});
}
function validateKeyDiscoveryRules(rules: KeyDiscoveryRules) {
let disjoint = true;
let union: Set<string> | undefined = undefined;
const keys = rules.formats !== undefined
? Object.getOwnPropertySymbols(rules.formats) as KeyFileFormat[]
: [];
for (const i of keys) {
const set = rules.formats?.[i];
if (union === undefined) {
union = set;
continue;
}
if (set === undefined) {
continue;
}
disjoint &&= union.isDisjointFrom(set);
union = union.union(set);
if (!disjoint) {
break;
}
}
if (!disjoint) {
throw new Error(
`\`Set\`s from \`rules.formats\` aren't disjoint`,
);
}
}
|