@cldmv/wisp 1.0.5 → 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.
- package/LICENSE +202 -21
- package/README.md +146 -26
- package/dist/index.cjs +52 -0
- package/dist/index.mjs +2 -0
- package/package.json +48 -19
- package/types/index.d.mts +3 -0
- package/types/index.d.mts.map +1 -0
- package/types/lib/resolve-from-caller.d.mts.map +1 -1
- package/types/wisp.d.mts.map +1 -1
- package/index.cjs +0 -24
- package/index.mjs +0 -22
- package/src/lib/resolve-from-caller.mjs +0 -479
- package/src/types/lib/resolve-from-caller.d.mts +0 -148
- package/src/types/wisp.d.mts +0 -54
- package/src/wisp.mjs +0 -188
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @function getStack
|
|
3
|
-
* @package
|
|
4
|
-
* @internal
|
|
5
|
-
* @param {Function} [skipFn] - Function to skip in stack trace via Error.captureStackTrace
|
|
6
|
-
* @returns {Array<CallSite>} Array of V8 CallSite objects with methods like getFileName(), getLineNumber(), etc.
|
|
7
|
-
*
|
|
8
|
-
* @description
|
|
9
|
-
* Get V8 stack trace as CallSite array for debugging and caller detection.
|
|
10
|
-
* Temporarily overrides Error.prepareStackTrace to access raw V8 CallSite objects
|
|
11
|
-
* instead of formatted string stack traces. Provides safe restoration of original handler.
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* // Get current call stack for debugging
|
|
15
|
-
* const stack = getStack();
|
|
16
|
-
* console.log(stack[0]?.getFileName?.()); // Current file path
|
|
17
|
-
* console.log(stack[0]?.getLineNumber?.()); // Current line number
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* // Skip current function from stack trace
|
|
21
|
-
* function myFunction() {
|
|
22
|
-
* return getStack(myFunction); // Stack starts from caller of myFunction
|
|
23
|
-
* }
|
|
24
|
-
* const callerStack = myFunction();
|
|
25
|
-
*
|
|
26
|
-
* @example
|
|
27
|
-
* // Stack analysis for caller detection
|
|
28
|
-
* function findCaller() {
|
|
29
|
-
* const stack = getStack(findCaller);
|
|
30
|
-
* for (const frame of stack) {
|
|
31
|
-
* const filename = frame?.getFileName?.();
|
|
32
|
-
* if (filename && !filename.includes("node_modules")) {
|
|
33
|
-
* return filename; // First non-dependency file
|
|
34
|
-
* }
|
|
35
|
-
* }
|
|
36
|
-
* }
|
|
37
|
-
*/
|
|
38
|
-
export function getStack(skipFn?: Function): Array<CallSite>;
|
|
39
|
-
/**
|
|
40
|
-
* @function resolvePathFromCaller
|
|
41
|
-
* @package
|
|
42
|
-
* @internal
|
|
43
|
-
* @param {string} rel - Relative path to resolve (e.g., "../config.json", "./data/file.txt")
|
|
44
|
-
* @returns {string} Absolute filesystem path with platform-specific separators
|
|
45
|
-
* @throws {TypeError} When rel parameter is not a string
|
|
46
|
-
*
|
|
47
|
-
* @description
|
|
48
|
-
* Resolve a relative path from the caller's context to an absolute filesystem path.
|
|
49
|
-
* Primary public API for filesystem path resolution with intelligent caller detection.
|
|
50
|
-
* Uses sophisticated stack trace analysis to determine the appropriate base directory.
|
|
51
|
-
*
|
|
52
|
-
* Resolution behavior:
|
|
53
|
-
* - file:// URLs: Converted to filesystem paths via fileURLToPath()
|
|
54
|
-
* - Absolute paths: Returned unchanged (already absolute)
|
|
55
|
-
* - Relative paths: Resolved using caller detection algorithm
|
|
56
|
-
*
|
|
57
|
-
* Caller detection process:
|
|
58
|
-
* 1. Primary: Use sophisticated slothlet.mjs-aware stack analysis
|
|
59
|
-
* 2. Fallback: Use first non-helper frame if primary fails
|
|
60
|
-
* 3. Existence check: Prefer primary if target exists, otherwise use fallback
|
|
61
|
-
*
|
|
62
|
-
* @example
|
|
63
|
-
* // From a file at /project/src/modules/math.mjs
|
|
64
|
-
* const configPath = resolvePathFromCaller("../config.json");
|
|
65
|
-
* // Returns: /project/config.json (absolute filesystem path)
|
|
66
|
-
*
|
|
67
|
-
* @example
|
|
68
|
-
* // Short-circuit cases
|
|
69
|
-
* resolvePathFromCaller("file:///absolute/path.txt");
|
|
70
|
-
* // Returns: /absolute/path.txt (converted from URL)
|
|
71
|
-
*
|
|
72
|
-
* resolvePathFromCaller("/already/absolute/path.txt");
|
|
73
|
-
* // Returns: /already/absolute/path.txt (unchanged)
|
|
74
|
-
*
|
|
75
|
-
* @example
|
|
76
|
-
* // Relative resolution from different contexts
|
|
77
|
-
* // If called from /project/src/lib/utils.mjs:
|
|
78
|
-
* resolvePathFromCaller("./helpers/format.js");
|
|
79
|
-
* // Returns: /project/src/lib/helpers/format.js
|
|
80
|
-
*
|
|
81
|
-
* resolvePathFromCaller("../../config/settings.json");
|
|
82
|
-
* // Returns: /project/config/settings.json
|
|
83
|
-
*/
|
|
84
|
-
export function resolvePathFromCaller(rel: string): string;
|
|
85
|
-
/**
|
|
86
|
-
* @function resolveUrlFromCaller
|
|
87
|
-
* @package
|
|
88
|
-
* @internal
|
|
89
|
-
* @param {string} rel - Relative path to resolve (e.g., "../config.json", "./data/file.txt")
|
|
90
|
-
* @returns {string} Absolute file:// URL suitable for dynamic imports and URL operations
|
|
91
|
-
* @throws {TypeError} When rel parameter is not a string
|
|
92
|
-
*
|
|
93
|
-
* @description
|
|
94
|
-
* Resolve a relative path from the caller's context to a file:// URL.
|
|
95
|
-
* Companion API to resolvePathFromCaller that returns file:// URLs instead of filesystem paths.
|
|
96
|
-
* Uses identical caller detection algorithm but outputs URL format for ESM imports.
|
|
97
|
-
*
|
|
98
|
-
* Resolution behavior:
|
|
99
|
-
* - file:// URLs: Returned unchanged (already in URL format)
|
|
100
|
-
* - Absolute paths: Converted to file:// URLs via pathToFileURL()
|
|
101
|
-
* - Relative paths: Resolved using caller detection, then converted to URL
|
|
102
|
-
*
|
|
103
|
-
* Caller detection uses the same sophisticated algorithm as resolvePathFromCaller,
|
|
104
|
-
* but the final result is converted to a file:// URL for compatibility with
|
|
105
|
-
* ESM dynamic imports and other URL-based operations.
|
|
106
|
-
*
|
|
107
|
-
* @example
|
|
108
|
-
* // From a file at /project/src/modules/math.mjs
|
|
109
|
-
* const configUrl = resolveUrlFromCaller("../config.json");
|
|
110
|
-
* // Returns: file:///project/config.json (absolute file:// URL)
|
|
111
|
-
*
|
|
112
|
-
* @example
|
|
113
|
-
* // Short-circuit cases
|
|
114
|
-
* resolveUrlFromCaller("file:///absolute/path.txt");
|
|
115
|
-
* // Returns: file:///absolute/path.txt (unchanged)
|
|
116
|
-
*
|
|
117
|
-
* resolveUrlFromCaller("/already/absolute/path.txt");
|
|
118
|
-
* // Returns: file:///already/absolute/path.txt (converted to URL)
|
|
119
|
-
*
|
|
120
|
-
* @example
|
|
121
|
-
* // Dynamic ESM import usage
|
|
122
|
-
* const modulePath = resolveUrlFromCaller("./dynamic-module.mjs");
|
|
123
|
-
* const dynamicModule = await import(modulePath);
|
|
124
|
-
* // Works seamlessly with ESM import() which expects URLs
|
|
125
|
-
*
|
|
126
|
-
* @example
|
|
127
|
-
* // Cross-platform URL handling
|
|
128
|
-
* // Unix: resolveUrlFromCaller("../config.json") → file:///project/config.json
|
|
129
|
-
* // Windows: resolveUrlFromCaller("../config.json") → file:///C:/project/config.json
|
|
130
|
-
*/
|
|
131
|
-
export function resolveUrlFromCaller(rel: string): string;
|
|
132
|
-
export function toFsPath(v: any): string | null;
|
|
133
|
-
export type CallSite = {
|
|
134
|
-
getFileName: () => string | undefined;
|
|
135
|
-
getLineNumber: () => number | undefined;
|
|
136
|
-
getFunctionName: () => string | undefined;
|
|
137
|
-
getTypeName: () => string | undefined;
|
|
138
|
-
getMethodName: () => string | undefined;
|
|
139
|
-
getScriptNameOrSourceURL: () => string | undefined;
|
|
140
|
-
getColumnNumber: () => number | undefined;
|
|
141
|
-
isNative: () => boolean | undefined;
|
|
142
|
-
isEval: () => boolean | undefined;
|
|
143
|
-
isConstructor: () => boolean | undefined;
|
|
144
|
-
isToplevel: () => boolean | undefined;
|
|
145
|
-
isAsync: () => boolean | undefined;
|
|
146
|
-
isPromiseAll: () => boolean | undefined;
|
|
147
|
-
getPromiseIndex: () => number | undefined;
|
|
148
|
-
};
|
package/src/types/wisp.d.mts
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Asynchronously loads JSON from a file, trying modern import with 'with', then 'assert', then fs.
|
|
3
|
-
* @public
|
|
4
|
-
* @param {string|URL} input - The path or URL to the JSON file.
|
|
5
|
-
* @param {Object} [options] - Options object.
|
|
6
|
-
* @param {string|URL} [options.base] - Base URL for relative paths.
|
|
7
|
-
* @param {Function} [options.validate] - Validation function for the parsed JSON.
|
|
8
|
-
* @param {Function} [options.reviver] - Reviver function for JSON.parse.
|
|
9
|
-
* @param {string} [options.type='json'] - The type of the file being imported. Defaults to 'json'.
|
|
10
|
-
* @param {string|URL} [options.fallback] - Fallback path or URL if the primary file is missing.
|
|
11
|
-
* @returns {Promise<*>} The parsed JSON value.
|
|
12
|
-
*
|
|
13
|
-
* @description
|
|
14
|
-
* Loads JSON asynchronously with version-agnostic support.
|
|
15
|
-
* Attempts modern import syntax first, falls back to legacy assert, then fs.readFile.
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
* import { wisp } from '@cldmv/wisp';
|
|
19
|
-
* const data = await wisp('./config.json');
|
|
20
|
-
*/
|
|
21
|
-
export function wisp(input: string | URL, options?: {
|
|
22
|
-
base?: string | URL;
|
|
23
|
-
validate?: Function;
|
|
24
|
-
reviver?: Function;
|
|
25
|
-
type?: string;
|
|
26
|
-
fallback?: string | URL;
|
|
27
|
-
}): Promise<any>;
|
|
28
|
-
/**
|
|
29
|
-
* Synchronously loads JSON from a file using fs.
|
|
30
|
-
* @public
|
|
31
|
-
* @param {string|URL} input - The path or URL to the JSON file.
|
|
32
|
-
* @param {Object} [options] - Options object.
|
|
33
|
-
* @param {string|URL} [options.base] - Base URL for relative paths.
|
|
34
|
-
* @param {Function} [options.validate] - Validation function for the parsed JSON.
|
|
35
|
-
* @param {Function} [options.reviver] - Reviver function for JSON.parse.
|
|
36
|
-
* @param {string} [options.type='json'] - The type of the file being imported. Defaults to 'json'.
|
|
37
|
-
* @param {string|URL} [options.fallback] - Fallback path or URL if the primary file is missing.
|
|
38
|
-
* @returns {*} The parsed JSON value.
|
|
39
|
-
*
|
|
40
|
-
* @description
|
|
41
|
-
* Loads JSON synchronously using fs.readFileSync.
|
|
42
|
-
*
|
|
43
|
-
* @example
|
|
44
|
-
* import { wispSync } from '@cldmv/wisp';
|
|
45
|
-
* const data = wispSync('./config.json');
|
|
46
|
-
*/
|
|
47
|
-
export function wispSync(input: string | URL, options?: {
|
|
48
|
-
base?: string | URL;
|
|
49
|
-
validate?: Function;
|
|
50
|
-
reviver?: Function;
|
|
51
|
-
type?: string;
|
|
52
|
-
fallback?: string | URL;
|
|
53
|
-
}): any;
|
|
54
|
-
export default wisp;
|
package/src/wisp.mjs
DELETED
|
@@ -1,188 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @Project: @cldmv/wisp
|
|
3
|
-
* @Filename: /src/wisp.mjs
|
|
4
|
-
* @Date: 2025-10-30 14:12:05 -07:00 (1761858725)
|
|
5
|
-
* @Author: Nate Hyson <CLDMV>
|
|
6
|
-
* @Email: <Shinrai@users.noreply.github.com>
|
|
7
|
-
* -----
|
|
8
|
-
* @Last modified by: Nate Hyson <CLDMV> (Shinrai@users.noreply.github.com)
|
|
9
|
-
* @Last modified time: 2025-10-30 14:59:15 -07:00 (1761861555)
|
|
10
|
-
* -----
|
|
11
|
-
* @Copyright: Copyright (c) 2013-2025 Catalyzed Motivation Inc. All rights reserved.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* @fileoverview Internal implementation of @cldmv/wisp. Not exported in package.json.
|
|
16
|
-
* @module @cldmv/wisp.src.wisp
|
|
17
|
-
* @internal
|
|
18
|
-
* @private
|
|
19
|
-
*
|
|
20
|
-
* @description
|
|
21
|
-
* This module provides the core implementation for version-agnostic JSON importing in Node.js.
|
|
22
|
-
* It includes functions to load JSON asynchronously and synchronously, with fallbacks for different Node versions.
|
|
23
|
-
*
|
|
24
|
-
* @example
|
|
25
|
-
* // Internal usage example
|
|
26
|
-
* import { wisp } from './src/wisp.mjs';
|
|
27
|
-
* const data = await wisp('./data.json');
|
|
28
|
-
*/
|
|
29
|
-
|
|
30
|
-
import fs from "node:fs";
|
|
31
|
-
import { readFile } from "node:fs/promises";
|
|
32
|
-
import path from "node:path";
|
|
33
|
-
import { pathToFileURL } from "node:url";
|
|
34
|
-
import { resolveUrlFromCaller } from "./lib/resolve-from-caller.mjs";
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Deep clones a value using structuredClone if available, otherwise JSON.parse/stringify.
|
|
38
|
-
* @private
|
|
39
|
-
* @param {*} v - The value to clone.
|
|
40
|
-
* @returns {*} The cloned value.
|
|
41
|
-
*/
|
|
42
|
-
/**
|
|
43
|
-
* Deep clones a value using structuredClone if available, otherwise JSON.parse/stringify.
|
|
44
|
-
* @private
|
|
45
|
-
* @param {*} v - The value to clone.
|
|
46
|
-
* @returns {*} The cloned value.
|
|
47
|
-
*/
|
|
48
|
-
function deepClone(v) {
|
|
49
|
-
return typeof globalThis.structuredClone === "function" ? globalThis.structuredClone(v) : JSON.parse(JSON.stringify(v));
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Asynchronously loads JSON from a file, trying modern import with 'with', then 'assert', then fs.
|
|
54
|
-
* @public
|
|
55
|
-
* @param {string|URL} input - The path or URL to the JSON file.
|
|
56
|
-
* @param {Object} [options] - Options object.
|
|
57
|
-
* @param {string|URL} [options.base] - Base URL for relative paths.
|
|
58
|
-
* @param {(val: any) => void} [options.validate] - Validation function for the parsed JSON.
|
|
59
|
-
* @param {(this: any, key: string, value: any) => any} [options.reviver] - Reviver function for JSON.parse.
|
|
60
|
-
* @param {string} [options.type='json'] - The type of the file being imported. Defaults to 'json'.
|
|
61
|
-
* @param {string|URL} [options.fallback] - Fallback path or URL if the primary file is missing.
|
|
62
|
-
* @returns {Promise<*>} The parsed JSON value.
|
|
63
|
-
*
|
|
64
|
-
* @description
|
|
65
|
-
* Loads JSON asynchronously with version-agnostic support.
|
|
66
|
-
* Attempts modern import syntax first, falls back to legacy assert, then fs.readFile.
|
|
67
|
-
*
|
|
68
|
-
* @example
|
|
69
|
-
* import { wisp } from '@cldmv/wisp';
|
|
70
|
-
* const data = await wisp('./config.json');
|
|
71
|
-
*/
|
|
72
|
-
export async function wisp(input, options = {}) {
|
|
73
|
-
const { base, validate, reviver, type = "json", fallback } = options;
|
|
74
|
-
let url;
|
|
75
|
-
if (input instanceof URL) url = input;
|
|
76
|
-
else {
|
|
77
|
-
const s = String(input);
|
|
78
|
-
if (s.startsWith("file://")) url = new URL(s);
|
|
79
|
-
else if (path.isAbsolute(s)) url = pathToFileURL(s);
|
|
80
|
-
else if (base) url = new URL(s, new URL(base));
|
|
81
|
-
else url = new URL(resolveUrlFromCaller(s));
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
try {
|
|
85
|
-
const mod = await import(url.href, { with: { type } });
|
|
86
|
-
if (!reviver && !validate) return mod?.default ?? mod;
|
|
87
|
-
let val = deepClone(mod?.default ?? mod);
|
|
88
|
-
if (reviver) val = JSON.parse(JSON.stringify(val), reviver);
|
|
89
|
-
if (validate) {
|
|
90
|
-
try {
|
|
91
|
-
validate(val);
|
|
92
|
-
} catch (e) {
|
|
93
|
-
throw new Error(`@cldmv/wisp: ${e?.message ?? e}`, { cause: e });
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
return val;
|
|
97
|
-
} catch {}
|
|
98
|
-
|
|
99
|
-
try {
|
|
100
|
-
// Legacy import assertions (`assert`) for Node 16.14-20.9, which predate `with`; the cast keeps the type checker from rejecting the key.
|
|
101
|
-
const mod = await import(url.href, /** @type {any} */ ({ assert: { type } }));
|
|
102
|
-
if (!reviver && !validate) return mod?.default ?? mod;
|
|
103
|
-
let val = deepClone(mod?.default ?? mod);
|
|
104
|
-
if (reviver) val = JSON.parse(JSON.stringify(val), reviver);
|
|
105
|
-
if (validate) {
|
|
106
|
-
try {
|
|
107
|
-
validate(val);
|
|
108
|
-
} catch (e) {
|
|
109
|
-
throw new Error(`@cldmv/wisp: ${e?.message ?? e}`, { cause: e });
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
return val;
|
|
113
|
-
} catch {}
|
|
114
|
-
|
|
115
|
-
if (type === "json") {
|
|
116
|
-
try {
|
|
117
|
-
const txt = await readFile(url, "utf8");
|
|
118
|
-
const val = deepClone(JSON.parse(txt, reviver));
|
|
119
|
-
if (validate) {
|
|
120
|
-
try {
|
|
121
|
-
validate(val);
|
|
122
|
-
} catch (e) {
|
|
123
|
-
throw new Error(`@cldmv/wisp: ${e?.message ?? e}`, { cause: e });
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
return val;
|
|
127
|
-
} catch (e) {
|
|
128
|
-
if (fallback) {
|
|
129
|
-
return wisp(fallback, options);
|
|
130
|
-
}
|
|
131
|
-
throw new Error(`@cldmv/wisp: Failed to load JSON file at ${url.href}: ${e.message}`, { cause: e });
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
throw new Error(`@cldmv/wisp: Unsupported type '${type}' or failed to load module at ${url.href}`);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Synchronously loads JSON from a file using fs.
|
|
140
|
-
* @public
|
|
141
|
-
* @param {string|URL} input - The path or URL to the JSON file.
|
|
142
|
-
* @param {Object} [options] - Options object.
|
|
143
|
-
* @param {string|URL} [options.base] - Base URL for relative paths.
|
|
144
|
-
* @param {(val: any) => void} [options.validate] - Validation function for the parsed JSON.
|
|
145
|
-
* @param {(this: any, key: string, value: any) => any} [options.reviver] - Reviver function for JSON.parse.
|
|
146
|
-
* @param {string} [options.type='json'] - The type of the file being imported. Defaults to 'json'.
|
|
147
|
-
* @param {string|URL} [options.fallback] - Fallback path or URL if the primary file is missing.
|
|
148
|
-
* @returns {*} The parsed JSON value.
|
|
149
|
-
*
|
|
150
|
-
* @description
|
|
151
|
-
* Loads JSON synchronously using fs.readFileSync.
|
|
152
|
-
*
|
|
153
|
-
* @example
|
|
154
|
-
* import { wispSync } from '@cldmv/wisp';
|
|
155
|
-
* const data = wispSync('./config.json');
|
|
156
|
-
*/
|
|
157
|
-
export function wispSync(input, options = {}) {
|
|
158
|
-
const { base, validate, reviver, fallback } = options;
|
|
159
|
-
let url;
|
|
160
|
-
if (input instanceof URL) url = input;
|
|
161
|
-
else {
|
|
162
|
-
const s = String(input);
|
|
163
|
-
if (s.startsWith("file://")) url = new URL(s);
|
|
164
|
-
else if (path.isAbsolute(s)) url = pathToFileURL(s);
|
|
165
|
-
else if (base) url = new URL(s, new URL(base));
|
|
166
|
-
else url = new URL(resolveUrlFromCaller(s));
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
try {
|
|
170
|
-
const txt = fs.readFileSync(url, "utf8");
|
|
171
|
-
const val = deepClone(JSON.parse(txt, reviver));
|
|
172
|
-
if (validate) {
|
|
173
|
-
try {
|
|
174
|
-
validate(val);
|
|
175
|
-
} catch (e) {
|
|
176
|
-
throw new Error(`@cldmv/wisp: ${e?.message ?? e}`, { cause: e });
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
return val;
|
|
180
|
-
} catch (e) {
|
|
181
|
-
if (fallback) {
|
|
182
|
-
return wispSync(fallback, options);
|
|
183
|
-
}
|
|
184
|
-
throw new Error(`@cldmv/wisp: Failed to load JSON file at ${url.href}: ${e.message}`, { cause: e });
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
export default wisp;
|