@hasna/shortlinks 0.1.24 → 0.2.1

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/dist/runtime.js DELETED
@@ -1,214 +0,0 @@
1
- // @bun
2
- var __create = Object.create;
3
- var __getProtoOf = Object.getPrototypeOf;
4
- var __defProp = Object.defineProperty;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- function __accessProp(key) {
8
- return this[key];
9
- }
10
- var __toESMCache_node;
11
- var __toESMCache_esm;
12
- var __toESM = (mod, isNodeMode, target) => {
13
- var canCache = mod != null && typeof mod === "object";
14
- if (canCache) {
15
- var cache = isNodeMode ? __toESMCache_node ??= new WeakMap : __toESMCache_esm ??= new WeakMap;
16
- var cached = cache.get(mod);
17
- if (cached)
18
- return cached;
19
- }
20
- target = mod != null ? __create(__getProtoOf(mod)) : {};
21
- const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
22
- for (let key of __getOwnPropNames(mod))
23
- if (!__hasOwnProp.call(to, key))
24
- __defProp(to, key, {
25
- get: __accessProp.bind(mod, key),
26
- enumerable: true
27
- });
28
- if (canCache)
29
- cache.set(mod, to);
30
- return to;
31
- };
32
- var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
33
- var __require = import.meta.require;
34
-
35
- // src/runtime.ts
36
- var SHORTLINKS_RUNTIME_ENV = {
37
- store: "HASNA_SHORTLINKS_STORE",
38
- databaseUrl: "HASNA_SHORTLINKS_DATABASE_URL",
39
- databaseSsl: "HASNA_SHORTLINKS_DATABASE_SSL"
40
- };
41
- var SHORTLINKS_RUNTIME_FALLBACK_ENV = {
42
- store: "SHORTLINKS_STORE",
43
- databaseUrl: "SHORTLINKS_DATABASE_URL",
44
- databaseSsl: "SHORTLINKS_DATABASE_SSL"
45
- };
46
- var CANONICAL_SHORTLINKS_POSTGRES_CLUSTER = "hasna-xyz-infra-apps-prod-postgres";
47
- var CANONICAL_SHORTLINKS_POSTGRES_DATABASE = "shortlinks";
48
- var CANONICAL_SHORTLINKS_RUNTIME_SECRET_PATH = "hasna/xyz/opensource/shortlinks/prod/postgres";
49
- function getCanonicalShortlinksPostgresConfig() {
50
- return {
51
- cluster: CANONICAL_SHORTLINKS_POSTGRES_CLUSTER,
52
- database: CANONICAL_SHORTLINKS_POSTGRES_DATABASE,
53
- runtimeSecretPath: CANONICAL_SHORTLINKS_RUNTIME_SECRET_PATH,
54
- primaryEnv: SHORTLINKS_RUNTIME_ENV.databaseUrl,
55
- fallbackEnv: SHORTLINKS_RUNTIME_FALLBACK_ENV.databaseUrl
56
- };
57
- }
58
- function parseShortlinksStoreMode(value) {
59
- const normalized = clean(value)?.toLowerCase();
60
- if (!normalized)
61
- return "local";
62
- if (normalized === "local" || normalized === "postgres")
63
- return normalized;
64
- if (normalized === "pg")
65
- return "postgres";
66
- throw new Error(`${SHORTLINKS_RUNTIME_ENV.store} must be local or postgres`);
67
- }
68
- function getShortlinksStoreMode(env = process.env) {
69
- return parseShortlinksStoreMode(readRuntimeEnv(env, "store").value);
70
- }
71
- function getShortlinksDatabaseUrl(env = process.env) {
72
- return readRuntimeEnv(env, "databaseUrl").value;
73
- }
74
- function getShortlinksDatabaseSsl(env = process.env) {
75
- return parseBoolean(readRuntimeEnv(env, "databaseSsl").value, true);
76
- }
77
- function getShortlinksRuntimeEnvName(env, key) {
78
- const primary = SHORTLINKS_RUNTIME_ENV[key];
79
- if (clean(env[primary]))
80
- return primary;
81
- return SHORTLINKS_RUNTIME_FALLBACK_ENV[key];
82
- }
83
- function loadShortlinksRuntimeConfig(env = process.env) {
84
- const mode = getShortlinksStoreMode(env);
85
- const databaseUrl = getShortlinksDatabaseUrl(env);
86
- return {
87
- service: "shortlinks",
88
- mode,
89
- ...databaseUrl ? {
90
- database: {
91
- provider: "postgres",
92
- url: databaseUrl,
93
- ssl: getShortlinksDatabaseSsl(env)
94
- }
95
- } : {}
96
- };
97
- }
98
- function assertShortlinksPostgresConfig(config) {
99
- if (config.mode !== "postgres")
100
- return;
101
- if (!config.database?.url) {
102
- throw new Error(`${SHORTLINKS_RUNTIME_ENV.databaseUrl} is required when ${SHORTLINKS_RUNTIME_ENV.store}=postgres`);
103
- }
104
- }
105
- function getShortlinksRuntimeStatus(env = process.env) {
106
- const issues = [];
107
- const warnings = [];
108
- let config;
109
- try {
110
- config = loadShortlinksRuntimeConfig(env);
111
- } catch (error) {
112
- issues.push(error instanceof Error ? error.message : String(error));
113
- config = { service: "shortlinks", mode: "local" };
114
- }
115
- try {
116
- assertShortlinksPostgresConfig(config);
117
- } catch (error) {
118
- issues.push(error instanceof Error ? error.message : String(error));
119
- }
120
- if (config.mode === "local" && config.database?.url) {
121
- warnings.push(`${SHORTLINKS_RUNTIME_ENV.store}=local ignores configured Postgres database settings`);
122
- }
123
- return {
124
- ok: issues.length === 0,
125
- service: "shortlinks",
126
- mode: config.mode,
127
- local_default: config.mode === "local",
128
- postgres_enabled: config.mode === "postgres",
129
- database: {
130
- configured: Boolean(config.database?.url),
131
- provider: config.database?.provider ?? null,
132
- redacted_url: redactDatabaseUrl(config.database?.url),
133
- ssl: config.database?.ssl ?? null
134
- },
135
- env: runtimeEnvStatus(env),
136
- canonical: getCanonicalShortlinksPostgresConfig(),
137
- issues,
138
- warnings,
139
- no_network: true
140
- };
141
- }
142
- function redactDatabaseUrl(value) {
143
- if (!value)
144
- return null;
145
- try {
146
- const url = new URL(value);
147
- if (url.username)
148
- url.username = "***";
149
- if (url.password)
150
- url.password = "***";
151
- for (const key of Array.from(url.searchParams.keys())) {
152
- if (isSensitiveQueryKey(key))
153
- url.searchParams.set(key, "***");
154
- }
155
- return url.toString();
156
- } catch {
157
- return "(redacted)";
158
- }
159
- }
160
- function runtimeEnvStatus(env) {
161
- return Object.fromEntries(Object.entries(SHORTLINKS_RUNTIME_ENV).map(([key, name]) => {
162
- const activeName = getShortlinksRuntimeEnvName(env, key);
163
- return [
164
- key,
165
- {
166
- name,
167
- active_name: activeName,
168
- configured: Boolean(clean(env[activeName]))
169
- }
170
- ];
171
- }));
172
- }
173
- function readRuntimeEnv(env, key) {
174
- const primary = SHORTLINKS_RUNTIME_ENV[key];
175
- const primaryValue = clean(env[primary]);
176
- if (primaryValue)
177
- return { name: primary, value: primaryValue };
178
- const fallback = SHORTLINKS_RUNTIME_FALLBACK_ENV[key];
179
- return { name: fallback, value: clean(env[fallback]) };
180
- }
181
- function parseBoolean(value, fallback) {
182
- const normalized = clean(value)?.toLowerCase();
183
- if (!normalized)
184
- return fallback;
185
- if (["1", "true", "yes", "on"].includes(normalized))
186
- return true;
187
- if (["0", "false", "no", "off"].includes(normalized))
188
- return false;
189
- throw new Error(`${SHORTLINKS_RUNTIME_ENV.databaseSsl} must be true or false`);
190
- }
191
- function isSensitiveQueryKey(key) {
192
- return /(?:^|[_-])(pass(?:word)?|pwd|secret|token|credential|auth|api[_-]?key|access[_-]?key)(?:$|[_-])/i.test(key);
193
- }
194
- function clean(value) {
195
- const trimmed = value?.trim();
196
- return trimmed ? trimmed : undefined;
197
- }
198
- export {
199
- redactDatabaseUrl,
200
- parseShortlinksStoreMode,
201
- loadShortlinksRuntimeConfig,
202
- getShortlinksStoreMode,
203
- getShortlinksRuntimeStatus,
204
- getShortlinksRuntimeEnvName,
205
- getShortlinksDatabaseUrl,
206
- getShortlinksDatabaseSsl,
207
- getCanonicalShortlinksPostgresConfig,
208
- assertShortlinksPostgresConfig,
209
- SHORTLINKS_RUNTIME_FALLBACK_ENV,
210
- SHORTLINKS_RUNTIME_ENV,
211
- CANONICAL_SHORTLINKS_RUNTIME_SECRET_PATH,
212
- CANONICAL_SHORTLINKS_POSTGRES_DATABASE,
213
- CANONICAL_SHORTLINKS_POSTGRES_CLUSTER
214
- };