@cldmv/wisp 1.0.6 → 1.0.7

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,3 @@
1
+ export * from "./wisp.mjs";
2
+ export { default } from "./wisp.mjs";
3
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.mjs"],"names":[],"mappings":""}
@@ -1 +1 @@
1
- {"version":3,"file":"resolve-from-caller.d.mts","sourceRoot":"","sources":["../../src/lib/resolve-from-caller.mjs"],"names":[],"mappings":"AAuFA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,6CAhCa,KAAK,CAAC,QAAQ,CAAC,CA0C3B;AA4MD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AACH,2CAzCW,MAAM,GACJ,MAAM,CAsDlB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AACH,0CA1CW,MAAM,GACJ,MAAM,CAuDlB;AAtXM,4BAvBI,GAAG,GACD,MAAM,GAAC,IAAI,CAsB+F;;iBA0XzG,MAAY,MAAM,GAAC,SAAS;mBAC5B,MAAY,MAAM,GAAC,SAAS;qBAC5B,MAAY,MAAM,GAAC,SAAS;iBAC5B,MAAY,MAAM,GAAC,SAAS;mBAC5B,MAAY,MAAM,GAAC,SAAS;8BAC5B,MAAY,MAAM,GAAC,SAAS;qBAC5B,MAAY,MAAM,GAAC,SAAS;cAC5B,MAAY,OAAO,GAAC,SAAS;YAC7B,MAAY,OAAO,GAAC,SAAS;mBAC7B,MAAY,OAAO,GAAC,SAAS;gBAC7B,MAAY,OAAO,GAAC,SAAS;aAC7B,MAAY,OAAO,GAAC,SAAS;kBAC7B,MAAY,OAAO,GAAC,SAAS;qBAC7B,MAAY,MAAM,GAAC,SAAS"}
1
+ {"version":3,"file":"resolve-from-caller.d.mts","sourceRoot":"","sources":["../../src/lib/resolve-from-caller.mjs"],"names":[],"mappings":"AAuFA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,6CAhCa,KAAK,CAAC,QAAQ,CAAC,CA0C3B;AA8MD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AACH,2CAzCW,MAAM,GACJ,MAAM,CAsDlB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AACH,0CA1CW,MAAM,GACJ,MAAM,CAuDlB;AAxXM,4BAvBI,GAAG,GACD,MAAM,GAAC,IAAI,CAsB+F;;iBA4XzG,MAAY,MAAM,GAAC,SAAS;mBAC5B,MAAY,MAAM,GAAC,SAAS;qBAC5B,MAAY,MAAM,GAAC,SAAS;iBAC5B,MAAY,MAAM,GAAC,SAAS;mBAC5B,MAAY,MAAM,GAAC,SAAS;8BAC5B,MAAY,MAAM,GAAC,SAAS;qBAC5B,MAAY,MAAM,GAAC,SAAS;cAC5B,MAAY,OAAO,GAAC,SAAS;YAC7B,MAAY,OAAO,GAAC,SAAS;mBAC7B,MAAY,OAAO,GAAC,SAAS;gBAC7B,MAAY,OAAO,GAAC,SAAS;aAC7B,MAAY,OAAO,GAAC,SAAS;kBAC7B,MAAY,OAAO,GAAC,SAAS;qBAC7B,MAAY,MAAM,GAAC,SAAS"}
@@ -1 +1 @@
1
- {"version":3,"file":"wisp.d.mts","sourceRoot":"","sources":["../src/wisp.mjs"],"names":[],"mappings":"AAqDA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,4BAjBW,MAAM,GAAC,GAAG,YAElB;IAA6B,IAAI,GAAzB,MAAM,GAAC,GAAG;IACmB,QAAQ,GAArC,CAAC,GAAG,EAAE,GAAG,KAAK,IAAI;IACoC,OAAO,GAA7D,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,KAAK,GAAG;IAC1B,IAAI,GAArB,MAAM;IACe,QAAQ,GAA7B,MAAM,GAAC,GAAG;CAClB,GAAU,OAAO,CAAC,GAAC,CAAC,CA0EtB;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,gCAhBW,MAAM,GAAC,GAAG,YAElB;IAA6B,IAAI,GAAzB,MAAM,GAAC,GAAG;IACmB,QAAQ,GAArC,CAAC,GAAG,EAAE,GAAG,KAAK,IAAI;IACoC,OAAO,GAA7D,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,KAAK,GAAG;IAC1B,IAAI,GAArB,MAAM;IACe,QAAQ,GAA7B,MAAM,GAAC,GAAG;CAClB,GAAU,GAAC,CAsCb"}
1
+ {"version":3,"file":"wisp.d.mts","sourceRoot":"","sources":["../src/wisp.mjs"],"names":[],"mappings":"AAkFA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,4BAjBW,MAAM,GAAC,GAAG,YAElB;IAA6B,IAAI,GAAzB,MAAM,GAAC,GAAG;IACmB,QAAQ,GAArC,CAAC,GAAG,EAAE,GAAG,KAAK,IAAI;IACoC,OAAO,GAA7D,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,KAAK,GAAG;IAC1B,IAAI,GAArB,MAAM;IACe,QAAQ,GAA7B,MAAM,GAAC,GAAG;CAClB,GAAU,OAAO,CAAC,GAAC,CAAC,CAuDtB;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,gCAhBW,MAAM,GAAC,GAAG,YAElB;IAA6B,IAAI,GAAzB,MAAM,GAAC,GAAG;IACmB,QAAQ,GAArC,CAAC,GAAG,EAAE,GAAG,KAAK,IAAI;IACoC,OAAO,GAA7D,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,KAAK,GAAG;IAC1B,IAAI,GAArB,MAAM;IACe,QAAQ,GAA7B,MAAM,GAAC,GAAG;CAClB,GAAU,GAAC,CAmCb"}
package/index.cjs DELETED
@@ -1,39 +0,0 @@
1
- /**
2
- *
3
- * @Project: @cldmv/wisp
4
- * @Filename: /index.cjs
5
- * @Date: 2025-10-30T15:06:33-07:00 (1761861993)
6
- * @Author: Nate Corcoran <CLDMV>
7
- * @Email: <Shinrai@users.noreply.github.com>
8
- * -----
9
- * @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
10
- * @Last modified time: 2026-10-02T15:12:05-07:00 (1790979125)
11
- * -----
12
- * @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
13
- *
14
- */
15
-
16
- /**
17
- * @fileoverview CJS entry point for @cldmv/wisp, providing version-agnostic JSON importing.
18
- * @module @cldmv/wisp
19
- * @public
20
- *
21
- * @description
22
- * This module provides CommonJS exports for the wisp and wispSync functions.
23
- * It uses createRequire to load the ESM implementation and re-exports it.
24
- *
25
- * @example
26
- * const { wisp, wispSync } = require('@cldmv/wisp');
27
- * const data = wispSync('./data.json');
28
- */
29
-
30
- "use strict";
31
-
32
- const { createRequire } = require("module");
33
- const requireESM = createRequire(__filename);
34
- const esm = requireESM("./index.mjs");
35
-
36
- module.exports = esm.default;
37
- module.exports.default = esm.default;
38
- module.exports.wisp = esm.wisp;
39
- module.exports.wispSync = esm.wispSync;
package/index.mjs DELETED
@@ -1,37 +0,0 @@
1
- /**
2
- *
3
- * @Project: @cldmv/wisp
4
- * @Filename: /index.mjs
5
- * @Date: 2025-10-30T15:06:33-07:00 (1761861993)
6
- * @Author: Nate Corcoran <CLDMV>
7
- * @Email: <Shinrai@users.noreply.github.com>
8
- * -----
9
- * @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
10
- * @Last modified time: 2026-10-02T15:12:05-07:00 (1790979125)
11
- * -----
12
- * @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
13
- *
14
- */
15
-
16
- /**
17
- * @fileoverview Main entry point for @cldmv/wisp, providing version-agnostic JSON importing.
18
- * @module @cldmv/wisp
19
- * @public
20
- *
21
- * @description
22
- * This module exports the wisp and wispSync functions for loading JSON files
23
- * with support for different Node.js versions and import syntaxes.
24
- *
25
- * @example
26
- * // ESM
27
- * import { wisp, wispSync } from '@cldmv/wisp';
28
- * const data = await wisp('./data.json');
29
- *
30
- * @example
31
- * // CJS
32
- * const { wisp, wispSync } = require('@cldmv/wisp');
33
- * const data = wispSync('./data.json');
34
- */
35
-
36
- export * from "./src/wisp.mjs";
37
- export { default } from "./src/wisp.mjs";
@@ -1,481 +0,0 @@
1
- /**
2
- *
3
- * @Project: @cldmv/wisp
4
- * @Filename: /src/lib/resolve-from-caller.mjs
5
- * @Date: 2025-09-09T13:22:38-07:00 (1757449358)
6
- * @Author: Nate Corcoran <CLDMV>
7
- * @Email: <Shinrai@users.noreply.github.com>
8
- * -----
9
- * @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
10
- * @Last modified time: 2026-10-02T15:12:05-07:00 (1790979125)
11
- * -----
12
- * @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
13
- *
14
- */
15
-
16
- /**
17
- * @fileoverview Path resolution utilities for resolving paths from the caller context. Internal file (not exported in package.json).
18
- * @module @cldmv/wisp.helpers.resolve-from-caller
19
- * @memberof module:@cldmv/wisp.helpers
20
- * @internal
21
- * @package
22
- *
23
- * @description
24
- * Advanced path resolution system that uses V8 stack trace analysis to determine caller context.
25
- * Provides utilities for resolving relative paths from the caller's directory, implementing
26
- * sophisticated caller detection algorithms to handle complex module loading scenarios.
27
- *
28
- * Key features:
29
- * - V8 CallSite-based stack trace analysis
30
- * - Primary base file detection with fallback strategies
31
- * - Support for both filesystem paths and file:// URLs
32
- * - Smart handling of slothlet.mjs and index file patterns
33
- * - Existence-based resolution with automatic fallback
34
- *
35
- * Technical implementation:
36
- * - Uses Error.prepareStackTrace to access V8 CallSite objects
37
- * - Implements dual-phase resolution (primary + fallback)
38
- * - Handles edge cases like node:internal modules and helper directories
39
- * - Provides both path and URL resolution variants
40
- *
41
- *
42
- * @example
43
- * // ESM (internal)
44
- * import { resolvePathFromCaller } from "@cldmv/slothlet/helpers/resolve-from-caller";
45
- * // Internal example using package.json exports
46
- *
47
- * @example
48
- * // Relative import (internal)
49
- * import { resolvePathFromCaller, resolveUrlFromCaller } from "./resolve-from-caller.mjs";
50
- * const configPath = resolvePathFromCaller("../config.json");
51
- */
52
-
53
- import fs from "node:fs";
54
- import path from "node:path";
55
- import { fileURLToPath, pathToFileURL } from "node:url";
56
-
57
- /* ---------- tiny utils ---------- */
58
-
59
- /**
60
- * @function toFsPath
61
- * @package
62
- * @internal
63
- * @param {any} v - Value to convert (file:// URL, path string, or any other value)
64
- * @returns {string|null} Filesystem path if URL conversion successful, original string if already a path, null if input is falsy
65
- *
66
- * @description
67
- * Convert file:// URL to filesystem path, or return string as-is.
68
- * Handles URL-to-path conversion for cross-platform compatibility while preserving
69
- * non-URL strings unchanged. Provides null-safe operation for invalid inputs.
70
- *
71
- * @example
72
- * // URL conversion
73
- * toFsPath("file:///project/config.json"); // "/project/config.json" (Unix) or "C:\\project\\config.json" (Windows)
74
- *
75
- * @example
76
- * // String passthrough
77
- * toFsPath("/project/config.json"); // "/project/config.json"
78
- * toFsPath("C:\\project\\config.json"); // "C:\\project\\config.json"
79
- *
80
- * @example
81
- * // Null-safe handling
82
- * toFsPath(null); // null
83
- * toFsPath(undefined); // null
84
- * toFsPath(""); // null
85
- */
86
- export const toFsPath = (v) => (v && String(v).startsWith("file://") ? fileURLToPath(String(v)) : v ? String(v) : null);
87
-
88
- /**
89
- * @function getStack
90
- * @package
91
- * @internal
92
- * @param {Function} [skipFn] - Function to skip in stack trace via Error.captureStackTrace
93
- * @returns {Array<CallSite>} Array of V8 CallSite objects with methods like getFileName(), getLineNumber(), etc.
94
- *
95
- * @description
96
- * Get V8 stack trace as CallSite array for debugging and caller detection.
97
- * Temporarily overrides Error.prepareStackTrace to access raw V8 CallSite objects
98
- * instead of formatted string stack traces. Provides safe restoration of original handler.
99
- *
100
- * @example
101
- * // Get current call stack for debugging
102
- * const stack = getStack();
103
- * console.log(stack[0]?.getFileName?.()); // Current file path
104
- * console.log(stack[0]?.getLineNumber?.()); // Current line number
105
- *
106
- * @example
107
- * // Skip current function from stack trace
108
- * function myFunction() {
109
- * return getStack(myFunction); // Stack starts from caller of myFunction
110
- * }
111
- * const callerStack = myFunction();
112
- *
113
- * @example
114
- * // Stack analysis for caller detection
115
- * function findCaller() {
116
- * const stack = getStack(findCaller);
117
- * for (const frame of stack) {
118
- * const filename = frame?.getFileName?.();
119
- * if (filename && !filename.includes("node_modules")) {
120
- * return filename; // First non-dependency file
121
- * }
122
- * }
123
- * }
124
- */
125
- export function getStack(skipFn) {
126
- const orig = Error.prepareStackTrace;
127
- try {
128
- Error.prepareStackTrace = (_, s) => s; // V8 CallSite[]
129
- const e = new Error("Stack trace");
130
- if (skipFn) Error.captureStackTrace(e, skipFn);
131
- return /** @type {NodeJS.CallSite[]} */ (/** @type {unknown} */ (e.stack)) || [];
132
- } finally {
133
- Error.prepareStackTrace = orig;
134
- }
135
- }
136
-
137
- const THIS_FILE = fileURLToPath(import.meta.url);
138
-
139
- // Find the package root by looking for package.json
140
- function findPackageRoot(startPath) {
141
- let currentPath = startPath;
142
- while (currentPath !== path.dirname(currentPath)) {
143
- const packageJsonPath = path.join(currentPath, "package.json");
144
- if (fs.existsSync(packageJsonPath)) {
145
- return currentPath;
146
- }
147
- currentPath = path.dirname(currentPath);
148
- }
149
- return null;
150
- }
151
-
152
- const PACKAGE_ROOT = findPackageRoot(THIS_FILE);
153
-
154
- /* ---------- base selection (shared) ---------- */
155
- // Two-flag state machine for detecting when we've exited package infrastructure:
156
- // 1) Flag 1: Set when we exit src/ or dist/ directories
157
- // 2) Flag 2: Set when we exit index.* entry point files
158
- // 3) Once both flags are set, we've found the actual caller outside the package
159
-
160
- /**
161
- * @function pickPrimaryBaseFile
162
- * @internal
163
- * @private
164
- * @returns {string|null} Primary base file path for resolution, null if detection fails
165
- *
166
- * @description
167
- * Find the primary base file using stack trace analysis with a two-flag state machine.
168
- * Tracks when we **exit** from package infrastructure directories/files.
169
- *
170
- * Algorithm:
171
- * 1. Walk stack trace backwards (from deepest to shallowest calls)
172
- * 2. Flag 1: Set when we exit src/ or dist/ folder (transition from src/dist to non-src/dist)
173
- * 3. Flag 2: Set when we exit index.* files (transition from index.* to non-index.*)
174
- * 4. Once both flags are set, we've fully exited the package infrastructure
175
- * 5. Return the current file as the actual caller
176
- *
177
- * @example
178
- * // Stack trace flow (wisp example):
179
- * // 0: src/lib/resolve-from-caller.mjs [in src/]
180
- * // 1: src/wisp.mjs [still in src/]
181
- * // 2: index.mjs [exited src/ - Flag 1 set]
182
- * // 3: user-code.mjs [exited index.* - Flag 2 set, return this!]
183
- */
184
- function pickPrimaryBaseFile() {
185
- let exitedSrcOrDist = false; // Flag 1: Have we exited src/ or dist/?
186
- // This function's own docstring documents a "Flag 2 set, return this!"
187
- // scenario, but nothing reads exitedIndex to return early once it's set --
188
- // looks like a pre-existing gap between the documented intent and the
189
- // implementation. Left as-is; fixing the behavior is a separate decision
190
- // from unblocking lint.
191
- let exitedIndex = false; // Flag 2: Have we exited index.*?
192
- let previousWasInSrcOrDist = false;
193
- let previousWasIndex = false;
194
-
195
- for (const cs of getStack(pickPrimaryBaseFile)) {
196
- const f = toFsPath(cs?.getFileName?.());
197
- if (!f) continue;
198
- if (f.startsWith?.("node:internal")) continue;
199
- if (f === THIS_FILE) continue;
200
-
201
- let currentIsInSrcOrDist = false;
202
- let currentIsIndex = false;
203
-
204
- // Check if this file is in our package
205
- if (PACKAGE_ROOT && f.startsWith(PACKAGE_ROOT + path.sep)) {
206
- const relativePath = path.relative(PACKAGE_ROOT, f);
207
- const segments = relativePath.split(path.sep);
208
-
209
- // Check if current file is in src/ or dist/
210
- currentIsInSrcOrDist = segments[0] === "src" || segments[0] === "dist";
211
-
212
- // Check if current file is index.*
213
- currentIsIndex = segments.length === 1 && /^index\.(mjs|cjs|js|ts)$/.test(segments[0]);
214
- }
215
-
216
- // Flag 1: Check if we've exited src/dist (was in, now not in)
217
- if (previousWasInSrcOrDist && !currentIsInSrcOrDist) {
218
- exitedSrcOrDist = true;
219
- }
220
-
221
- // Flag 2: Check if we've exited index.* (was index, now not index)
222
- if (previousWasIndex && !currentIsIndex) {
223
- // eslint-disable-next-line no-unused-vars -- see the declaration above
224
- exitedIndex = true;
225
- }
226
-
227
- // If we've exited src/dist, we've found the caller (index.mjs might be optimized away)
228
- if (exitedSrcOrDist) {
229
- return f;
230
- }
231
-
232
- // Update previous state for next iteration
233
- previousWasInSrcOrDist = currentIsInSrcOrDist;
234
- previousWasIndex = currentIsIndex;
235
- }
236
- return null;
237
- }
238
- /**
239
- * @function pickFallbackBaseFile
240
- * @internal
241
- * @private
242
- * @returns {string} Fallback base file path, guaranteed to return a valid path
243
- *
244
- * @description
245
- * Find fallback base file when primary detection fails.
246
- * Provides robust fallback strategy by finding the first legitimate user code frame.
247
- * Filters out internal Node.js modules, helper utilities, and slothlet infrastructure.
248
- *
249
- * Fallback algorithm:
250
- * 1. Iterate through stack frames from top to bottom
251
- * 2. Skip node:internal modules (Node.js internals)
252
- * 3. Skip this file itself (resolve-from-caller.mjs)
253
- * 4. Skip other files in the helpers directory
254
- * 5. Skip slothlet.mjs infrastructure files
255
- * 6. Return first remaining frame, or THIS_FILE as ultimate fallback
256
- *
257
- * This ensures we always have a base path for resolution, even in edge cases.
258
- *
259
- * @example
260
- * // Fallback scenarios:
261
- * // - Primary detection failed (no slothlet.mjs in stack)
262
- * // - Complex loading chain with multiple loaders
263
- * // - Edge cases like REPL or test environments
264
- * // - Direct API usage without slothlet loader
265
- */
266
- function pickFallbackBaseFile() {
267
- // Use simpler fallback logic - just find first non-Node.js-internal file
268
- for (const cs of getStack(pickFallbackBaseFile)) {
269
- const f = toFsPath(cs?.getFileName?.());
270
- if (!f) continue;
271
- if (f.startsWith?.("node:internal")) continue;
272
- if (f === THIS_FILE) continue;
273
- return f;
274
- }
275
- return THIS_FILE;
276
- }
277
-
278
- /* ---------- generic resolver (shared) ---------- */
279
-
280
- /**
281
- * @function resolveWith
282
- * @internal
283
- * @private
284
- * @param {string} rel - Relative path to resolve (must be a string)
285
- * @param {Function} makePrimary - Function to build primary candidate: (baseFile, rel) => candidate
286
- * @param {Function} exists - Existence check function: (candidate) => boolean
287
- * @param {Function} makeFallback - Function to build fallback result: (baseFile, rel) => result
288
- * @returns {string} Resolved path or URL (type depends on make functions)
289
- * @throws {TypeError} When rel parameter is not a string
290
- *
291
- * @description
292
- * Generic resolver that tries primary base file detection with fallback.
293
- * Core resolution engine that orchestrates the two-phase detection strategy.
294
- * Attempts primary resolution first, then falls back if target doesn't exist.
295
- *
296
- * Resolution strategy:
297
- * 1. Get primary base file using sophisticated stack analysis
298
- * 2. Build primary candidate using makePrimary function
299
- * 3. Check if primary candidate exists using exists function
300
- * 4. If exists, return primary candidate
301
- * 5. Otherwise, get fallback base file and build fallback result
302
- * 6. Return fallback result (no second existence check)
303
- *
304
- * This pattern ensures we try the most accurate resolution first, but always
305
- * provide a reasonable fallback even if the target doesn't exist yet.
306
- *
307
- * @example
308
- * // Usage pattern for filesystem paths:
309
- * resolveWith(
310
- * "../config.json",
311
- * (base, rel) => path.resolve(path.dirname(base), rel), // makePrimary
312
- * (candidate) => fs.existsSync(candidate), // exists
313
- * (base, rel) => path.resolve(path.dirname(base), rel) // makeFallback
314
- * );
315
- *
316
- * @example
317
- * // Usage pattern for URLs:
318
- * resolveWith(
319
- * "../config.json",
320
- * (base, rel) => new URL(rel, pathToFileURL(base)).href, // makePrimary
321
- * (href) => fs.existsSync(fileURLToPath(href)), // exists
322
- * (base, rel) => new URL(rel, pathToFileURL(base)).href // makeFallback
323
- * );
324
- */
325
- function resolveWith(rel, makePrimary, exists, makeFallback) {
326
- if (typeof rel !== "string") throw new TypeError("rel must be a string");
327
-
328
- // absolute / already-URL cases are handled in the public wrappers
329
- const primaryBase = pickPrimaryBaseFile() ?? pickFallbackBaseFile();
330
- const primary = makePrimary(primaryBase, rel);
331
- if (exists(primary)) return primary;
332
-
333
- const fbBase = pickFallbackBaseFile();
334
- return makeFallback(fbBase, rel);
335
- }
336
-
337
- /* ---------- public API (thin wrappers) ---------- */
338
-
339
- /**
340
- * @function resolvePathFromCaller
341
- * @package
342
- * @internal
343
- * @param {string} rel - Relative path to resolve (e.g., "../config.json", "./data/file.txt")
344
- * @returns {string} Absolute filesystem path with platform-specific separators
345
- * @throws {TypeError} When rel parameter is not a string
346
- *
347
- * @description
348
- * Resolve a relative path from the caller's context to an absolute filesystem path.
349
- * Primary public API for filesystem path resolution with intelligent caller detection.
350
- * Uses sophisticated stack trace analysis to determine the appropriate base directory.
351
- *
352
- * Resolution behavior:
353
- * - file:// URLs: Converted to filesystem paths via fileURLToPath()
354
- * - Absolute paths: Returned unchanged (already absolute)
355
- * - Relative paths: Resolved using caller detection algorithm
356
- *
357
- * Caller detection process:
358
- * 1. Primary: Use sophisticated slothlet.mjs-aware stack analysis
359
- * 2. Fallback: Use first non-helper frame if primary fails
360
- * 3. Existence check: Prefer primary if target exists, otherwise use fallback
361
- *
362
- * @example
363
- * // From a file at /project/src/modules/math.mjs
364
- * const configPath = resolvePathFromCaller("../config.json");
365
- * // Returns: /project/config.json (absolute filesystem path)
366
- *
367
- * @example
368
- * // Short-circuit cases
369
- * resolvePathFromCaller("file:///absolute/path.txt");
370
- * // Returns: /absolute/path.txt (converted from URL)
371
- *
372
- * resolvePathFromCaller("/already/absolute/path.txt");
373
- * // Returns: /already/absolute/path.txt (unchanged)
374
- *
375
- * @example
376
- * // Relative resolution from different contexts
377
- * // If called from /project/src/lib/utils.mjs:
378
- * resolvePathFromCaller("./helpers/format.js");
379
- * // Returns: /project/src/lib/helpers/format.js
380
- *
381
- * resolvePathFromCaller("../../config/settings.json");
382
- * // Returns: /project/config/settings.json
383
- */
384
- export function resolvePathFromCaller(rel) {
385
- // short-circuits
386
- if (rel.startsWith?.("file://")) return fileURLToPath(rel);
387
- if (path.isAbsolute(rel)) return rel;
388
-
389
- return resolveWith(
390
- rel,
391
- // makePrimary (PATH)
392
- (baseFile, r) => path.resolve(path.dirname(baseFile), r),
393
- // exists (PATH)
394
- (candidate) => fs.existsSync(candidate),
395
- // makeFallback (PATH)
396
- (baseFile, r) => path.resolve(path.dirname(baseFile), r)
397
- );
398
- }
399
-
400
- /**
401
- * @function resolveUrlFromCaller
402
- * @package
403
- * @internal
404
- * @param {string} rel - Relative path to resolve (e.g., "../config.json", "./data/file.txt")
405
- * @returns {string} Absolute file:// URL suitable for dynamic imports and URL operations
406
- * @throws {TypeError} When rel parameter is not a string
407
- *
408
- * @description
409
- * Resolve a relative path from the caller's context to a file:// URL.
410
- * Companion API to resolvePathFromCaller that returns file:// URLs instead of filesystem paths.
411
- * Uses identical caller detection algorithm but outputs URL format for ESM imports.
412
- *
413
- * Resolution behavior:
414
- * - file:// URLs: Returned unchanged (already in URL format)
415
- * - Absolute paths: Converted to file:// URLs via pathToFileURL()
416
- * - Relative paths: Resolved using caller detection, then converted to URL
417
- *
418
- * Caller detection uses the same sophisticated algorithm as resolvePathFromCaller,
419
- * but the final result is converted to a file:// URL for compatibility with
420
- * ESM dynamic imports and other URL-based operations.
421
- *
422
- * @example
423
- * // From a file at /project/src/modules/math.mjs
424
- * const configUrl = resolveUrlFromCaller("../config.json");
425
- * // Returns: file:///project/config.json (absolute file:// URL)
426
- *
427
- * @example
428
- * // Short-circuit cases
429
- * resolveUrlFromCaller("file:///absolute/path.txt");
430
- * // Returns: file:///absolute/path.txt (unchanged)
431
- *
432
- * resolveUrlFromCaller("/already/absolute/path.txt");
433
- * // Returns: file:///already/absolute/path.txt (converted to URL)
434
- *
435
- * @example
436
- * // Dynamic ESM import usage
437
- * const modulePath = resolveUrlFromCaller("./dynamic-module.mjs");
438
- * const dynamicModule = await import(modulePath);
439
- * // Works seamlessly with ESM import() which expects URLs
440
- *
441
- * @example
442
- * // Cross-platform URL handling
443
- * // Unix: resolveUrlFromCaller("../config.json") → file:///project/config.json
444
- * // Windows: resolveUrlFromCaller("../config.json") → file:///C:/project/config.json
445
- */
446
- export function resolveUrlFromCaller(rel) {
447
- // short-circuits
448
- if (rel.startsWith?.("file://")) return rel;
449
- if (path.isAbsolute(rel)) return pathToFileURL(rel).href;
450
-
451
- return resolveWith(
452
- rel,
453
- // makePrimary (URL)
454
- (baseFile, r) => new URL(r, pathToFileURL(baseFile)).href,
455
- // exists (URL→check target path)
456
- (href) => fs.existsSync(fileURLToPath(href)),
457
- // makeFallback (URL)
458
- (baseFile, r) => new URL(r, pathToFileURL(baseFile)).href
459
- );
460
- }
461
-
462
- /**
463
- * @typedef {object} CallSite
464
- * @property {function(): string|undefined} getFileName
465
- * @property {function(): number|undefined} getLineNumber
466
- * @property {function(): string|undefined} getFunctionName
467
- * @property {function(): string|undefined} getTypeName
468
- * @property {function(): string|undefined} getMethodName
469
- * @property {function(): string|undefined} getScriptNameOrSourceURL
470
- * @property {function(): number|undefined} getColumnNumber
471
- * @property {function(): boolean|undefined} isNative
472
- * @property {function(): boolean|undefined} isEval
473
- * @property {function(): boolean|undefined} isConstructor
474
- * @property {function(): boolean|undefined} isToplevel
475
- * @property {function(): boolean|undefined} isAsync
476
- * @property {function(): boolean|undefined} isPromiseAll
477
- * @property {function(): number|undefined} getPromiseIndex
478
- *
479
- * @description
480
- * Minimal V8 CallSite object type for stack trace analysis. Only includes methods used in this module.
481
- */