@firstdraft.com/cli 0.1.0-alpha.2

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.
@@ -0,0 +1,149 @@
1
+ import { createHash } from "node:crypto";
2
+
3
+ export const MAX_DERIVED_APPLICATION_KEY_BYTES = 63;
4
+
5
+ const DIGEST_HEX_LENGTH = 12;
6
+ const APPLICATION_KEY_PATTERN = /^[a-z][a-z0-9_]*$/;
7
+ const MARK_PATTERN = /\p{Mark}/u;
8
+ // NFKD does not decompose these Latin letters, so map them before ASCII filtering.
9
+ const LATIN_TRANSLITERATIONS = new Map([
10
+ ["Æ", "ae"],
11
+ ["æ", "ae"],
12
+ ["Œ", "oe"],
13
+ ["œ", "oe"],
14
+ ["Ø", "o"],
15
+ ["ø", "o"],
16
+ ["Ł", "l"],
17
+ ["ł", "l"],
18
+ ["Đ", "d"],
19
+ ["đ", "d"],
20
+ ["Ð", "d"],
21
+ ["ð", "d"],
22
+ ["Þ", "th"],
23
+ ["þ", "th"],
24
+ ["ẞ", "ss"],
25
+ ["ß", "ss"],
26
+ ["ı", "i"],
27
+ ]);
28
+
29
+ /** @param {string} name */
30
+ export function deriveApplicationKey(name) {
31
+ if (!isValidApplicationName(name)) {
32
+ throw new TypeError(
33
+ "Application name must be valid nonblank Unicode text.",
34
+ );
35
+ }
36
+
37
+ const normalizedName = name.normalize("NFKD");
38
+ const readable = asciiWords(normalizedName);
39
+ const candidate =
40
+ readable.length === 0
41
+ ? `app_${stableDigest(normalizedName)}`
42
+ : /^[0-9]/.test(readable)
43
+ ? `app_${readable}`
44
+ : readable;
45
+
46
+ if (Buffer.byteLength(candidate) <= MAX_DERIVED_APPLICATION_KEY_BYTES) {
47
+ return candidate;
48
+ }
49
+
50
+ const suffix = `_${stableDigest(normalizedName)}`;
51
+ const prefix = candidate
52
+ .slice(0, MAX_DERIVED_APPLICATION_KEY_BYTES - suffix.length)
53
+ .replace(/_+$/, "");
54
+
55
+ return `${prefix}${suffix}`;
56
+ }
57
+
58
+ /** @param {string} applicationKey */
59
+ export function deriveApplicationName(applicationKey) {
60
+ if (!isValidApplicationKey(applicationKey)) {
61
+ throw new TypeError(
62
+ "Application key must be a valid lower-snake identifier.",
63
+ );
64
+ }
65
+
66
+ return applicationKey
67
+ .split(/_+/)
68
+ .filter((word) => word.length > 0)
69
+ .map((word) => `${word[0]?.toUpperCase() ?? ""}${word.slice(1)}`)
70
+ .join(" ");
71
+ }
72
+
73
+ /** @param {unknown} value @returns {value is string} */
74
+ export function isValidApplicationKey(value) {
75
+ return typeof value === "string" && APPLICATION_KEY_PATTERN.test(value);
76
+ }
77
+
78
+ /** @param {unknown} value @returns {value is string} */
79
+ export function isValidApplicationName(value) {
80
+ if (typeof value !== "string") return false;
81
+
82
+ let hasNonWhitespace = false;
83
+ for (const character of value) {
84
+ const codePoint = character.codePointAt(0) ?? 0;
85
+ if (
86
+ codePoint === 0 ||
87
+ (codePoint >= 0xd800 && codePoint <= 0xdfff) ||
88
+ (codePoint >= 0xfdd0 && codePoint <= 0xfdef) ||
89
+ (codePoint & 0xfffe) === 0xfffe
90
+ ) {
91
+ return false;
92
+ }
93
+
94
+ if (!isUnicodeWhitespace(codePoint)) hasNonWhitespace = true;
95
+ }
96
+
97
+ return hasNonWhitespace;
98
+ }
99
+
100
+ /** @param {string} name */
101
+ function asciiWords(name) {
102
+ let value = "";
103
+
104
+ for (const character of name) {
105
+ if (MARK_PATTERN.test(character)) continue;
106
+
107
+ const expanded = LATIN_TRANSLITERATIONS.get(character) ?? character;
108
+ for (const asciiCharacter of expanded) {
109
+ const codePoint = asciiCharacter.codePointAt(0) ?? 0;
110
+ if (codePoint >= 0x41 && codePoint <= 0x5a) {
111
+ value += asciiCharacter.toLowerCase();
112
+ } else if (
113
+ (codePoint >= 0x61 && codePoint <= 0x7a) ||
114
+ (codePoint >= 0x30 && codePoint <= 0x39)
115
+ ) {
116
+ value += asciiCharacter;
117
+ } else {
118
+ value += "_";
119
+ }
120
+ }
121
+ }
122
+
123
+ return value.replace(/_+/g, "_").replace(/^_+|_+$/g, "");
124
+ }
125
+
126
+ /** @param {string} value */
127
+ function stableDigest(value) {
128
+ return createHash("sha256")
129
+ .update(Buffer.from(value, "utf8"))
130
+ .digest("hex")
131
+ .slice(0, DIGEST_HEX_LENGTH);
132
+ }
133
+
134
+ /** @param {number} codePoint */
135
+ function isUnicodeWhitespace(codePoint) {
136
+ return (
137
+ (codePoint >= 0x0009 && codePoint <= 0x000d) ||
138
+ codePoint === 0x0020 ||
139
+ codePoint === 0x0085 ||
140
+ codePoint === 0x00a0 ||
141
+ codePoint === 0x1680 ||
142
+ (codePoint >= 0x2000 && codePoint <= 0x200a) ||
143
+ codePoint === 0x2028 ||
144
+ codePoint === 0x2029 ||
145
+ codePoint === 0x202f ||
146
+ codePoint === 0x205f ||
147
+ codePoint === 0x3000
148
+ );
149
+ }