@putkoff/abstract-utilities 0.1.180 → 0.1.182

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/cjs/index.js CHANGED
@@ -1154,43 +1154,122 @@ function alertit(obj = null) {
1154
1154
  alert(msg);
1155
1155
  }
1156
1156
 
1157
- let _cachedConfig = null;
1158
- function loadConfig() {
1159
- return __awaiter(this, arguments, void 0, function* (filePath = null) {
1160
- const relativePath = filePath || "config.json";
1161
- // 1) Resolve URL against document.baseURI if available
1162
- let configUrl = relativePath;
1163
- const baseURI = safeGlobalProp("document", "baseURI");
1164
- if (baseURI) {
1157
+ // src/functions/read_utils/src/read_utils.ts
1158
+ /**
1159
+ * Reads a JSON file, either via Node’s fs (if available)
1160
+ * or via window.fetch in the browser. Never throws — returns
1161
+ * the parsed object or null on any error.
1162
+ */
1163
+ function readJsonFile(relativeOrAbsolutePath) {
1164
+ return __awaiter(this, void 0, void 0, function* () {
1165
+ // 1) Try Node.js fs
1166
+ if (typeof process !== 'undefined' &&
1167
+ process.versions != null &&
1168
+ process.versions.node) {
1165
1169
  try {
1166
- configUrl = new URL(relativePath, baseURI).href;
1170
+ const fs = yield import('fs');
1171
+ const path = yield import('path');
1172
+ const filePath = path.isAbsolute(relativeOrAbsolutePath)
1173
+ ? relativeOrAbsolutePath
1174
+ : path.resolve(process.cwd(), relativeOrAbsolutePath);
1175
+ const text = yield fs.promises.readFile(filePath, 'utf8');
1176
+ return JSON.parse(text);
1167
1177
  }
1168
1178
  catch (_a) {
1169
- // ignore—keep configUrl = relativePath
1179
+ // swallow and fall back to browser
1180
+ }
1181
+ }
1182
+ // 2) Try browser fetch
1183
+ const fetchFn = safeGlobalProp('fetch');
1184
+ if (typeof fetchFn !== 'function') {
1185
+ return null;
1186
+ }
1187
+ // Resolve URL against document.baseURI if possible
1188
+ let url = relativeOrAbsolutePath;
1189
+ const baseURI = safeGlobalProp('document', 'baseURI');
1190
+ if (baseURI) {
1191
+ try {
1192
+ url = new URL(relativeOrAbsolutePath, baseURI).href;
1193
+ }
1194
+ catch (_b) {
1195
+ // keep url as-is
1170
1196
  }
1171
1197
  }
1172
- // 2) Return cache if we’ve already fetched once
1198
+ try {
1199
+ const res = yield fetchFn(url);
1200
+ if (!res.ok)
1201
+ return null;
1202
+ return (yield res.json());
1203
+ }
1204
+ catch (_c) {
1205
+ return null;
1206
+ }
1207
+ });
1208
+ }
1209
+
1210
+ // src/functions/config_utils/src/config_utils.ts
1211
+ let _cachedConfig = null;
1212
+ function loadConfig() {
1213
+ return __awaiter(this, arguments, void 0, function* (filePath = null) {
1214
+ var _a;
1173
1215
  if (_cachedConfig) {
1174
1216
  return _cachedConfig;
1175
1217
  }
1176
- // 3) Bail out in non-browser if fetch isn’t present
1177
- const fetchFn = safeGlobalProp("fetch");
1178
- if (typeof fetchFn !== "function") {
1179
- return {};
1218
+ // 1) figure out where config.json lives
1219
+ let configUrl;
1220
+ if (filePath) {
1221
+ configUrl = filePath;
1180
1222
  }
1181
- // 4) Try to fetch + parse, swallowing any errors
1182
- try {
1183
- const res = yield fetchFn(configUrl).catch(() => null);
1184
- if (!res || !res.ok) {
1185
- return {};
1223
+ else if (typeof ({ url: (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.js', document.baseURI).href)) }) !== 'undefined' && typeof (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.js', document.baseURI).href)) === 'string') {
1224
+ // ES module: resolve relative to this file
1225
+ try {
1226
+ configUrl = new URL('./config.json', (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.js', document.baseURI).href))).href;
1227
+ }
1228
+ catch (_b) {
1229
+ configUrl = 'config.json';
1230
+ }
1231
+ }
1232
+ else {
1233
+ // browser fallback
1234
+ const baseURI = safeGlobalProp('document', 'baseURI');
1235
+ try {
1236
+ configUrl =
1237
+ typeof baseURI === 'string'
1238
+ ? new URL('config.json', baseURI).href
1239
+ : 'config.json';
1240
+ }
1241
+ catch (_c) {
1242
+ configUrl = 'config.json';
1243
+ }
1244
+ }
1245
+ // 2) if we have a fetch, try HTTP(S)
1246
+ const fetchFn = safeGlobalProp('fetch');
1247
+ if (typeof fetchFn === 'function') {
1248
+ try {
1249
+ const res = yield fetchFn(configUrl);
1250
+ if (res.ok) {
1251
+ const json = yield res.json();
1252
+ // cache & return
1253
+ _cachedConfig = (_a = json) !== null && _a !== void 0 ? _a : {};
1254
+ return _cachedConfig;
1255
+ }
1186
1256
  }
1187
- // parse JSON
1188
- _cachedConfig = (yield res.json());
1257
+ catch (_d) {
1258
+ /* swallow */
1259
+ }
1260
+ }
1261
+ // 3) Node fallback: try reading from disk (requires your readJsonFile util)
1262
+ try {
1263
+ const disk = yield readJsonFile(configUrl);
1264
+ _cachedConfig = disk !== null && disk !== void 0 ? disk : {};
1189
1265
  return _cachedConfig;
1190
1266
  }
1191
- catch (_b) {
1192
- return {};
1267
+ catch (_e) {
1268
+ /* swallow */
1193
1269
  }
1270
+ // 4) if all else fails, return an empty config
1271
+ _cachedConfig = {};
1272
+ return _cachedConfig;
1194
1273
  });
1195
1274
  }
1196
1275
  function getConfig(key) {
@@ -1240,7 +1319,7 @@ function get_app_config_url(endpoint) {
1240
1319
  // 2) fetch your BASE_API_URL
1241
1320
  const vars = getFetchVars(null, 'POST', {
1242
1321
  key: 'BASE_API_URL',
1243
- path: '/var/www/.../config.json'
1322
+ path: '/var/www/abstractendeavors/secure-files/public/config.json'
1244
1323
  });
1245
1324
  const resp = yield fetch(ensureAbstractUrl('/secure_env'), vars);
1246
1325
  checkResponse(resp);
@@ -1484,53 +1563,6 @@ function Spinner() {
1484
1563
  return (jsxRuntime.jsx("p", { className: 'animate-pulse', children: "Loading\u2026" }));
1485
1564
  }
1486
1565
 
1487
- // src/read_utils.ts
1488
- /**
1489
- * Attempt to load the Node-only modules.
1490
- * Returns { fs: null, path: null } in the browser.
1491
- */
1492
- function tryNodeModules() {
1493
- return __awaiter(this, void 0, void 0, function* () {
1494
- try {
1495
- const fsMod = yield import('fs');
1496
- const pathMod = yield import('path');
1497
- return {
1498
- fs: fsMod.promises, // keep the `promises` API
1499
- path: pathMod
1500
- };
1501
- }
1502
- catch (_a) {
1503
- return { fs: null, path: null };
1504
- }
1505
- });
1506
- }
1507
- /**
1508
- * Reads a file at `relativeOrAbsolutePath` and returns its contents as a string.
1509
- * In a browser this will reject immediately.
1510
- */
1511
- function readFileContents(relativeOrAbsolutePath) {
1512
- return __awaiter(this, void 0, void 0, function* () {
1513
- const { fs, path } = yield tryNodeModules();
1514
- if (!fs || !path) {
1515
- throw new Error('readFileContents can only be used in Node.js');
1516
- }
1517
- // resolve absolute
1518
- const filePath = path.isAbsolute(relativeOrAbsolutePath)
1519
- ? relativeOrAbsolutePath
1520
- : path.resolve(__dirname, relativeOrAbsolutePath);
1521
- return fs.readFile(filePath, 'utf8');
1522
- });
1523
- }
1524
- /**
1525
- * Reads a JSON file and returns the parsed object.
1526
- */
1527
- function readJsonFile(relativeOrAbsolutePath) {
1528
- return __awaiter(this, void 0, void 0, function* () {
1529
- const text = yield readFileContents(relativeOrAbsolutePath);
1530
- return JSON.parse(text);
1531
- });
1532
- }
1533
-
1534
1566
  Object.defineProperty(exports, "useCallback", {
1535
1567
  enumerable: true,
1536
1568
  get: function () { return react.useCallback; }
@@ -1636,7 +1668,6 @@ exports.make_path = make_path;
1636
1668
  exports.make_sanitized_path = make_sanitized_path;
1637
1669
  exports.normalizeUrl = normalizeUrl;
1638
1670
  exports.parseResult = parseResult;
1639
- exports.readFileContents = readFileContents;
1640
1671
  exports.readJsonFile = readJsonFile;
1641
1672
  exports.requestPatch = requestPatch;
1642
1673
  exports.requireToken = requireToken;