@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
package/LICENSE
CHANGED
|
@@ -1,21 +1,202 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,12 +1,48 @@
|
|
|
1
1
|
# @cldmv/wisp
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**@cldmv/wisp** loads JSON files in Node.js without caring which JSON import syntax the running Node.js version understands. It tries the modern `import ... with { type: "json" }` form first, falls back to the legacy `assert` form, and finally reads and parses the file itself, so the same call works from Node.js 16 through the current release.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Relative paths resolve from the file that calls wisp, not from wisp's own location, so `wispSync("./config.json")` means what it looks like it means from anywhere in your project — including from packages that depend on wisp.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
> _Load JSON the same way on every Node.js version — quietly, like a wisp._
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
[![npm version]][npm_version_url] [![npm downloads]][npm_downloads_url] [![GitHub downloads]][github_downloads_url] [![Last commit]][last_commit_url] [![npm last update]][npm_last_update_url]
|
|
10
|
+
|
|
11
|
+
[![Contributors]][contributors_url] [![Sponsor shinrai]][sponsor_url]
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## ✨ What's New
|
|
16
|
+
|
|
17
|
+
### Latest: v1.0.7 (October 2026)
|
|
18
|
+
|
|
19
|
+
- **`require()` works in bundles and fails clearly on older Node.js** — `index.cjs` now loads the ESM entry with a plain `require("./index.mjs")` instead of `createRequire(__filename)`, so `require("@cldmv/wisp")` survives esbuild and webpack bundling. On Node.js versions without synchronous `require(esm)`, where `require()` never worked, it now throws an `ERR_REQUIRE_ESM` error that names the supported versions (`^20.19.0` or `>=22.12.0`) and points to `import()` ([#30](https://github.com/CLDMV/wisp/pull/30)).
|
|
20
|
+
- **A failed validation throws instead of loading the fallback** — `fallback` is now used only when the primary file cannot be read or parsed; a `validate` rejection is reported as an error, and a fallback that also fails no longer loops forever ([#35](https://github.com/CLDMV/wisp/pull/35)). On the `import()` paths, a `validate` rejection now throws the validation error once instead of `Unsupported type`, and `reviver` / `validate` on a module without a default export receive a plain-object copy of its exports instead of failing ([#41](https://github.com/CLDMV/wisp/pull/41)). The package is also relicensed under Apache-2.0 ([#34](https://github.com/CLDMV/wisp/pull/34)).
|
|
21
|
+
- **Built package in `dist/`** — the published package is now bundled with tsup into `dist/index.mjs`, with `dist/index.cjs` as a thin `require()` wrapper, and ships only `dist/`, `types/`, `README.md` and `LICENSE`. `import` and `require()` of `@cldmv/wisp` work exactly as before; code that loaded the old root `index.mjs` / `index.cjs` or `src/` files by path must use the package specifier ([#40](https://github.com/CLDMV/wisp/pull/40)). The test suite now runs on `@cldmv/vitest-runner` with 100% coverage ([#37](https://github.com/CLDMV/wisp/pull/37)).
|
|
22
|
+
- [View full v1.0.7 Changelog](https://github.com/CLDMV/wisp/blob/master/docs/changelog/v1/v1.0.7.md)
|
|
23
|
+
|
|
24
|
+
### Recent Releases
|
|
25
|
+
|
|
26
|
+
- **v1.0.6** (October 2026) — Uniform file headers via `@cldmv/fix-headers`, a CI fix and a development-dependency security update; no runtime change ([Changelog](https://github.com/CLDMV/wisp/blob/master/docs/changelog/v1/v1.0.6.md))
|
|
27
|
+
- **v1.0.5** (October 2026) — First npm release since v1.0.1; TypeScript 6, chai 6 and `@types/node` 26 for development, v4 workflow syncs; no runtime change ([Changelog](https://github.com/CLDMV/wisp/blob/master/docs/changelog/v1/v1.0.5.md))
|
|
28
|
+
- **v1.0.4** (September 2026) — Thrown errors now carry the original error as `cause`; ESLint wired up; mocha 12 ([Changelog](https://github.com/CLDMV/wisp/blob/master/docs/changelog/v1/v1.0.4.md))
|
|
29
|
+
- **v1.0.3** (August 2026) — Development-dependency security update (`picomatch`); no runtime change ([Changelog](https://github.com/CLDMV/wisp/blob/master/docs/changelog/v1/v1.0.3.md))
|
|
30
|
+
|
|
31
|
+
📚 **For complete version history and detailed release notes, see the [docs/changelog/](https://github.com/CLDMV/wisp/tree/master/docs/changelog/) folder.**
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## 🚀 Key Features
|
|
36
|
+
|
|
37
|
+
- **Version-agnostic JSON imports** — `with`, then `assert`, then a file-system read; whichever the running Node.js supports.
|
|
38
|
+
- **Caller-aware paths** — relative paths resolve from the calling file; `base` overrides it.
|
|
39
|
+
- **Async and sync** — `wisp()` returns a promise, `wispSync()` returns the value directly.
|
|
40
|
+
- **Validation and revivers** — `validate` rejects bad data, `reviver` is passed to `JSON.parse`.
|
|
41
|
+
- **Fallback files** — `fallback` names a second file to try when the first cannot be loaded.
|
|
42
|
+
- **ESM and CommonJS** — `import` and `require()` entry points, with TypeScript declarations included.
|
|
43
|
+
- **Zero runtime dependencies.**
|
|
44
|
+
|
|
45
|
+
### Node.js Version Support
|
|
10
46
|
|
|
11
47
|
| Node Version | `import ... with { type: 'json' }` | `import ... assert { type: 'json' }` | Fallback |
|
|
12
48
|
| ------------ | ---------------------------------- | ------------------------------------ | -------- |
|
|
@@ -16,15 +52,26 @@ A Node.js module for version-agnostic JSON importing, providing transparent supp
|
|
|
16
52
|
| ≥ 16.14 | ❌ | ✅ | ✅ |
|
|
17
53
|
| < 16.14 | ❌ | ❌ | ✅ |
|
|
18
54
|
|
|
19
|
-
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
## 📦 Installation
|
|
58
|
+
|
|
59
|
+
### Requirements
|
|
60
|
+
|
|
61
|
+
- **Node.js 16 or higher** for `import` (ESM).
|
|
62
|
+
- **`require()` needs Node.js ^20.19.0 or >=22.12.0** (synchronous `require(esm)`). On older Node.js versions, load the package with `import()` instead.
|
|
63
|
+
|
|
64
|
+
### Install
|
|
20
65
|
|
|
21
66
|
```bash
|
|
22
67
|
npm install @cldmv/wisp
|
|
23
68
|
```
|
|
24
69
|
|
|
25
|
-
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## 🚀 Quick Start
|
|
26
73
|
|
|
27
|
-
### ESM
|
|
74
|
+
### ESM
|
|
28
75
|
|
|
29
76
|
```javascript
|
|
30
77
|
import { wisp, wispSync } from "@cldmv/wisp";
|
|
@@ -36,7 +83,7 @@ const config = await wisp("./config.json");
|
|
|
36
83
|
const data = wispSync("./data.json");
|
|
37
84
|
```
|
|
38
85
|
|
|
39
|
-
###
|
|
86
|
+
### CommonJS
|
|
40
87
|
|
|
41
88
|
```javascript
|
|
42
89
|
const { wisp, wispSync } = require("@cldmv/wisp");
|
|
@@ -50,7 +97,9 @@ wisp("./config.json").then((config) => {
|
|
|
50
97
|
const data = wispSync("./data.json");
|
|
51
98
|
```
|
|
52
99
|
|
|
53
|
-
|
|
100
|
+
---
|
|
101
|
+
|
|
102
|
+
## 📖 API Reference
|
|
54
103
|
|
|
55
104
|
### `wisp(input, options?)`
|
|
56
105
|
|
|
@@ -62,7 +111,9 @@ Asynchronously loads JSON from a file.
|
|
|
62
111
|
- `options` (object, optional):
|
|
63
112
|
- `base` (string | URL, optional): Base URL for resolving relative paths. Defaults to the caller's file URL.
|
|
64
113
|
- `validate` (function, optional): Validation function called with the parsed JSON. Throws if validation fails.
|
|
65
|
-
- `reviver` (function, optional): Reviver function passed to `JSON.parse`.
|
|
114
|
+
- `reviver` (function, optional): Reviver function passed to `JSON.parse`. For a module loaded through `import()` that has no default export, `reviver` and `validate` receive a plain-object copy of its exports.
|
|
115
|
+
- `type` (string, optional): Import attribute type used for the `import()` attempts. Defaults to `"json"`; the file-system fallback only runs for `"json"`.
|
|
116
|
+
- `fallback` (string | URL, optional): A second file to load when `input` cannot be read or parsed. A `validate` failure on `input` throws rather than falling back.
|
|
66
117
|
|
|
67
118
|
#### Returns
|
|
68
119
|
|
|
@@ -88,11 +139,11 @@ Synchronously loads JSON from a file.
|
|
|
88
139
|
#### Parameters
|
|
89
140
|
|
|
90
141
|
- `input` (string | URL): Path or URL to the JSON file
|
|
91
|
-
- `options` (object, optional): Same as `wisp` options.
|
|
142
|
+
- `options` (object, optional): Same as `wisp` options, except `type` (the file is always read and parsed as JSON).
|
|
92
143
|
|
|
93
144
|
#### Returns
|
|
94
145
|
|
|
95
|
-
|
|
146
|
+
`*`: The parsed JSON value.
|
|
96
147
|
|
|
97
148
|
#### Example
|
|
98
149
|
|
|
@@ -106,15 +157,21 @@ const data = wispSync("./config.json", {
|
|
|
106
157
|
});
|
|
107
158
|
```
|
|
108
159
|
|
|
109
|
-
|
|
160
|
+
---
|
|
110
161
|
|
|
111
|
-
|
|
112
|
-
| ---------- | ---------- | ------------------------------------------------------------------------ |
|
|
113
|
-
| `base` | string/URL | Base URL for relative path resolution. Defaults to caller's file URL. |
|
|
114
|
-
| `validate` | function | Validation function. Receives parsed JSON, should throw on invalid data. |
|
|
115
|
-
| `reviver` | function | JSON.parse reviver function for custom parsing. |
|
|
162
|
+
## ⚙️ Options
|
|
116
163
|
|
|
117
|
-
|
|
164
|
+
| Option | Type | Description |
|
|
165
|
+
| ---------- | ---------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
|
|
166
|
+
| `base` | string/URL | Base URL for relative path resolution. Defaults to caller's file URL. |
|
|
167
|
+
| `validate` | function | Validation function. Receives parsed JSON, should throw on invalid data. |
|
|
168
|
+
| `reviver` | function | JSON.parse reviver function for custom parsing. |
|
|
169
|
+
| `type` | string | Import attribute type for `wisp()`'s `import()` attempts. Defaults to `"json"`. |
|
|
170
|
+
| `fallback` | string/URL | File to load instead when `input` cannot be read or parsed (missing, unreadable, or not valid JSON). A `validate` failure throws instead. |
|
|
171
|
+
|
|
172
|
+
---
|
|
173
|
+
|
|
174
|
+
## 🧭 Path Resolution
|
|
118
175
|
|
|
119
176
|
`@cldmv/wisp` uses caller-aware path resolution:
|
|
120
177
|
|
|
@@ -122,7 +179,9 @@ const data = wispSync("./config.json", {
|
|
|
122
179
|
- Absolute paths and URLs are used as-is
|
|
123
180
|
- The `base` option overrides the default caller-based resolution
|
|
124
181
|
|
|
125
|
-
|
|
182
|
+
---
|
|
183
|
+
|
|
184
|
+
## 🔁 Fallback Order
|
|
126
185
|
|
|
127
186
|
The module attempts to load JSON in this order:
|
|
128
187
|
|
|
@@ -130,11 +189,13 @@ The module attempts to load JSON in this order:
|
|
|
130
189
|
2. `import(url, { assert: { type: 'json' } })` (Node ≥ 16.14)
|
|
131
190
|
3. `fs.readFile` / `fs.readFileSync` (all supported Node versions)
|
|
132
191
|
|
|
133
|
-
This ensures maximum compatibility across Node.js versions.
|
|
192
|
+
This ensures maximum compatibility across Node.js versions. `wispSync` always uses `fs.readFileSync`.
|
|
134
193
|
|
|
135
|
-
|
|
194
|
+
---
|
|
136
195
|
|
|
137
|
-
|
|
196
|
+
## 🛡 Error Handling
|
|
197
|
+
|
|
198
|
+
Errors thrown by wisp are prefixed with `@cldmv/wisp:` for easy identification, and carry the underlying error as `error.cause`. A validation failure is reported as part of the load error:
|
|
138
199
|
|
|
139
200
|
```javascript
|
|
140
201
|
try {
|
|
@@ -144,10 +205,69 @@ try {
|
|
|
144
205
|
}
|
|
145
206
|
});
|
|
146
207
|
} catch (error) {
|
|
147
|
-
console.log(error.message); // "@cldmv/wisp: Custom validation failed"
|
|
208
|
+
console.log(error.message); // "@cldmv/wisp: Failed to load JSON file at file:///…/invalid.json: @cldmv/wisp: Custom validation failed"
|
|
148
209
|
}
|
|
149
210
|
```
|
|
150
211
|
|
|
151
|
-
|
|
212
|
+
---
|
|
213
|
+
|
|
214
|
+
## 📚 Documentation
|
|
215
|
+
|
|
216
|
+
- **[Changelog](https://github.com/CLDMV/wisp/tree/master/docs/changelog/)** — release notes for every version
|
|
217
|
+
- **[Bug reports and fixes](https://github.com/CLDMV/wisp/blob/master/BUGS.md)** — write-ups of notable bugs and how they were fixed
|
|
218
|
+
|
|
219
|
+
[![CodeFactor]][codefactor_url] [![OpenSSF Scorecard]][ossf_scorecard_url] [![npms.io score]][npms_url] [![npm unpacked size]][npm_size_url] [![Repo size]][repo_size_url]
|
|
220
|
+
|
|
221
|
+
---
|
|
222
|
+
|
|
223
|
+
## 🤝 Contributing
|
|
224
|
+
|
|
225
|
+
Contributions are welcome — open an [issue](https://github.com/CLDMV/wisp/issues) or a pull request.
|
|
226
|
+
|
|
227
|
+
[![Contributors]][contributors_url] [![Sponsor shinrai]][sponsor_url]
|
|
228
|
+
|
|
229
|
+
---
|
|
230
|
+
|
|
231
|
+
## 🔗 Links
|
|
232
|
+
|
|
233
|
+
- **npm**: [@cldmv/wisp](https://www.npmjs.com/package/@cldmv/wisp)
|
|
234
|
+
- **GitHub**: [CLDMV/wisp](https://github.com/CLDMV/wisp)
|
|
235
|
+
- **Issues**: [GitHub Issues](https://github.com/CLDMV/wisp/issues)
|
|
236
|
+
- **Changelog**: [docs/changelog/](https://github.com/CLDMV/wisp/tree/master/docs/changelog/)
|
|
237
|
+
|
|
238
|
+
---
|
|
239
|
+
|
|
240
|
+
## 📄 License
|
|
241
|
+
|
|
242
|
+
[![GitHub license]][github_license_url] [![npm license]][npm_license_url]
|
|
243
|
+
|
|
244
|
+
Apache-2.0 © CLDMV Inc. See [LICENSE](https://github.com/CLDMV/wisp/blob/master/LICENSE) for the full text.
|
|
152
245
|
|
|
153
|
-
|
|
246
|
+
[npm version]: https://img.shields.io/npm/v/%40cldmv%2Fwisp.svg?style=for-the-badge&logo=npm&logoColor=white&labelColor=CB3837
|
|
247
|
+
[npm_version_url]: https://www.npmjs.com/package/@cldmv/wisp
|
|
248
|
+
[last commit]: https://img.shields.io/github/last-commit/CLDMV/wisp?style=for-the-badge&logo=github&logoColor=white&labelColor=181717
|
|
249
|
+
[last_commit_url]: https://github.com/CLDMV/wisp/commits
|
|
250
|
+
[npm last update]: https://img.shields.io/npm/last-update/%40cldmv%2Fwisp?style=for-the-badge&logo=npm&logoColor=white&labelColor=CB3837
|
|
251
|
+
[npm_last_update_url]: https://www.npmjs.com/package/@cldmv/wisp
|
|
252
|
+
[codefactor]: https://img.shields.io/codefactor/grade/github/CLDMV/wisp?style=for-the-badge&logo=codefactor&logoColor=white&labelColor=F44A6A
|
|
253
|
+
[codefactor_url]: https://www.codefactor.io/repository/github/cldmv/wisp
|
|
254
|
+
[openssf scorecard]: https://img.shields.io/ossf-scorecard/github.com/CLDMV/wisp?style=for-the-badge&label=OpenSSF%20Scorecard
|
|
255
|
+
[ossf_scorecard_url]: https://scorecard.dev/viewer/?uri=github.com/CLDMV/wisp
|
|
256
|
+
[npms.io score]: https://img.shields.io/npms-io/final-score/%40cldmv%2Fwisp?style=for-the-badge&logo=npms&logoColor=white&labelColor=0B5D57
|
|
257
|
+
[npms_url]: https://npms.io/search?q=%40cldmv%2Fwisp
|
|
258
|
+
[npm downloads]: https://img.shields.io/npm/dm/%40cldmv%2Fwisp.svg?style=for-the-badge&logo=npm&logoColor=white&labelColor=CB3837
|
|
259
|
+
[npm_downloads_url]: https://www.npmjs.com/package/@cldmv/wisp
|
|
260
|
+
[github downloads]: https://img.shields.io/github/downloads/CLDMV/wisp/total?style=for-the-badge&logo=github&logoColor=white&labelColor=181717
|
|
261
|
+
[github_downloads_url]: https://github.com/CLDMV/wisp/releases
|
|
262
|
+
[npm unpacked size]: https://img.shields.io/npm/unpacked-size/%40cldmv%2Fwisp.svg?style=for-the-badge&logo=npm&logoColor=white&labelColor=CB3837
|
|
263
|
+
[npm_size_url]: https://www.npmjs.com/package/@cldmv/wisp
|
|
264
|
+
[repo size]: https://img.shields.io/github/repo-size/CLDMV/wisp?style=for-the-badge&logo=github&logoColor=white&labelColor=181717
|
|
265
|
+
[repo_size_url]: https://github.com/CLDMV/wisp
|
|
266
|
+
[github license]: https://img.shields.io/github/license/CLDMV/wisp.svg?style=for-the-badge&logo=github&logoColor=white&labelColor=181717
|
|
267
|
+
[github_license_url]: https://github.com/CLDMV/wisp/blob/HEAD/LICENSE
|
|
268
|
+
[npm license]: https://img.shields.io/npm/l/%40cldmv%2Fwisp.svg?style=for-the-badge&logo=npm&logoColor=white&labelColor=CB3837
|
|
269
|
+
[npm_license_url]: https://www.npmjs.com/package/@cldmv/wisp
|
|
270
|
+
[contributors]: https://img.shields.io/github/contributors/CLDMV/wisp.svg?style=for-the-badge&logo=github&logoColor=white&labelColor=181717
|
|
271
|
+
[contributors_url]: https://github.com/CLDMV/wisp/graphs/contributors
|
|
272
|
+
[sponsor shinrai]: https://img.shields.io/github/sponsors/shinrai?style=for-the-badge&logo=githubsponsors&logoColor=white&labelColor=EA4AAA&label=Sponsor
|
|
273
|
+
[sponsor_url]: https://github.com/sponsors/shinrai
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @Project: @cldmv/wisp
|
|
4
|
+
* @Filename: /src/cjs-shim.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-03T19:34:10-07:00 (1791081250)
|
|
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 is a thin wrapper: it loads ./index.mjs through Node's synchronous require(esm)
|
|
23
|
+
* and re-exports it. It is not bundled — tsup copies it verbatim to dist/index.cjs (see
|
|
24
|
+
* tsup.config.mjs), where ./index.mjs is the ESM bundle; under the `wisp-dev` export
|
|
25
|
+
* condition it is served from src/, where ./index.mjs is the source entry. It uses a plain
|
|
26
|
+
* require() rather than createRequire so the file keeps working when bundled by
|
|
27
|
+
* esbuild/webpack.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* const { wisp, wispSync } = require('@cldmv/wisp');
|
|
31
|
+
* const data = wispSync('./data.json');
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
"use strict";
|
|
35
|
+
|
|
36
|
+
// This is a thin wrapper: it loads ./index.mjs through Node's synchronous require(esm).
|
|
37
|
+
// Node.js versions without require(esm) would fail with a bare ERR_REQUIRE_ESM, so fail
|
|
38
|
+
// early with a message that says what to do instead.
|
|
39
|
+
if (!process.features?.require_module) {
|
|
40
|
+
const error = new Error(
|
|
41
|
+
`@cldmv/wisp: require() needs Node.js ^20.19.0 or >=22.12.0 (this is ${process.version}). On older Node.js, load the package with import() instead.`
|
|
42
|
+
);
|
|
43
|
+
error.code = "ERR_REQUIRE_ESM";
|
|
44
|
+
throw error;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const esm = require("./index.mjs");
|
|
48
|
+
|
|
49
|
+
module.exports = esm.default;
|
|
50
|
+
module.exports.default = esm.default;
|
|
51
|
+
module.exports.wisp = esm.wisp;
|
|
52
|
+
module.exports.wispSync = esm.wispSync;
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var E=Object.defineProperty;var l=(t,e)=>E(t,"name",{value:e,configurable:!0});import N from"node:fs";import{readFile as J}from"node:fs/promises";import F from"node:path";import{pathToFileURL as L}from"node:url";import R from"node:fs";import p from"node:path";import{fileURLToPath as g,pathToFileURL as m}from"node:url";var b=l(t=>t&&String(t).startsWith("file://")?g(String(t)):t?String(t):null,"toFsPath");function k(t){let e=Error.prepareStackTrace;try{Error.prepareStackTrace=(r,f)=>f;let n=new Error("Stack trace");return t&&Error.captureStackTrace(n,t),n.stack||[]}finally{Error.prepareStackTrace=e}}l(k,"getStack");var h=g(import.meta.url);function T(t){let e=t;for(;e!==p.dirname(e);){let n=p.join(e,"package.json");if(R.existsSync(n))return e;e=p.dirname(e)}return null}l(T,"findPackageRoot");var w=T(h);function v(){var f,c;let t=!1,e=!1,n=!1,r=!1;for(let a of k(v)){let i=b((f=a==null?void 0:a.getFileName)==null?void 0:f.call(a));if(!i||(c=i.startsWith)!=null&&c.call(i,"node:internal")||i===h)continue;let o=!1,s=!1;if(w&&i.startsWith(w+p.sep)){let d=p.relative(w,i).split(p.sep);o=d[0]==="src"||d[0]==="dist",s=d.length===1&&/^index\.(mjs|cjs|js|ts)$/.test(d[0])}if(n&&!o&&(t=!0),r&&!s&&(e=!0),t)return i;n=o,r=s}return null}l(v,"pickPrimaryBaseFile");function S(){var t,e;for(let n of k(S)){let r=b((t=n==null?void 0:n.getFileName)==null?void 0:t.call(n));if(r&&!((e=r.startsWith)!=null&&e.call(r,"node:internal"))&&r!==h)return r}return h}l(S,"pickFallbackBaseFile");function W(t,e,n,r){if(typeof t!="string")throw new TypeError("rel must be a string");let f=v()??S(),c=e(f,t);if(n(c))return c;let a=S();return r(a,t)}l(W,"resolveWith");function y(t){var e;return(e=t.startsWith)!=null&&e.call(t,"file://")?t:p.isAbsolute(t)?m(t).href:W(t,(n,r)=>new URL(r,m(n)).href,n=>R.existsSync(g(n)),(n,r)=>new URL(r,m(n)).href)}l(y,"resolveUrlFromCaller");function U(t){return typeof globalThis.structuredClone=="function"?globalThis.structuredClone(t):JSON.parse(JSON.stringify(t))}l(U,"deepClone");function P(t,e,n){let r=t.default??t;if(!e&&!n)return r;let f=r===t?{...t}:r;return e?JSON.parse(JSON.stringify(f),e):U(f)}l(P,"moduleValue");function x(t,e,n){if(t)try{t(e)}catch(r){throw new Error(`@cldmv/wisp: Failed to load JSON file at ${n.href}: @cldmv/wisp: ${(r==null?void 0:r.message)??r}`,{cause:r})}}l(x,"runValidate");async function O(t,e={}){let{base:n,validate:r,reviver:f,type:c="json",fallback:a}=e,i;if(t instanceof URL)i=t;else{let s=String(t);s.startsWith("file://")?i=new URL(s):F.isAbsolute(s)?i=L(s):n?i=new URL(s,new URL(n)):i=new URL(y(s))}let o=l(async s=>P(await import(i.href,s),f,r),"loadWith");for(let s of[{with:{type:c}},{assert:{type:c}}]){let u;try{u=await o(s)}catch{continue}return x(r,u,i),u}if(c==="json"){let s;try{let u=await J(i,"utf8");s=U(JSON.parse(u,f))}catch(u){if(a)return O(a,{...e,fallback:void 0});throw new Error(`@cldmv/wisp: Failed to load JSON file at ${i.href}: ${u.message}`,{cause:u})}return x(r,s,i),s}throw new Error(`@cldmv/wisp: Unsupported type '${c}' or failed to load module at ${i.href}`)}l(O,"wisp");function I(t,e={}){let{base:n,validate:r,reviver:f,fallback:c}=e,a;if(t instanceof URL)a=t;else{let o=String(t);o.startsWith("file://")?a=new URL(o):F.isAbsolute(o)?a=L(o):n?a=new URL(o,new URL(n)):a=new URL(y(o))}let i;try{let o=N.readFileSync(a,"utf8");i=U(JSON.parse(o,f))}catch(o){if(c)return I(c,{...e,fallback:void 0});throw new Error(`@cldmv/wisp: Failed to load JSON file at ${a.href}: ${o.message}`,{cause:o})}return x(r,i,a),i}l(I,"wispSync");var $=O;export{$ as default,O as wisp,I as wispSync};
|
|
2
|
+
//# sourceMappingURL=index.mjs.map
|
package/package.json
CHANGED
|
@@ -1,21 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cldmv/wisp",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
4
4
|
"description": "Version-agnostic JSON importing for Node.js with fallback handling and caller path resolution.",
|
|
5
5
|
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.mjs",
|
|
8
|
+
"types": "./types/index.d.mts",
|
|
6
9
|
"exports": {
|
|
7
10
|
".": {
|
|
8
|
-
"
|
|
9
|
-
|
|
11
|
+
"wisp-dev": {
|
|
12
|
+
"types": "./types/index.d.mts",
|
|
13
|
+
"import": "./src/index.mjs",
|
|
14
|
+
"require": "./src/cjs-shim.cjs"
|
|
15
|
+
},
|
|
16
|
+
"types": "./types/index.d.mts",
|
|
17
|
+
"import": "./dist/index.mjs",
|
|
18
|
+
"require": "./dist/index.cjs"
|
|
10
19
|
}
|
|
11
20
|
},
|
|
12
21
|
"scripts": {
|
|
13
|
-
"
|
|
14
|
-
"
|
|
22
|
+
"build": "tsup",
|
|
23
|
+
"build:ci": "npm run lint && npm run format:check && npm run types:build && npm run build && npm run test && npm run test:types",
|
|
24
|
+
"prepack": "npm run types:build && npm run build",
|
|
25
|
+
"prepare": "node -e \"import('./.githooks/install.mjs').catch(()=>{})\"",
|
|
26
|
+
"test": "node tests/run-vitest.mjs && npm run test:cjs",
|
|
27
|
+
"test:cjs": "npm run build && node --test tests/cjs/entry.test.cjs tests/bundle/caller-resolution.test.mjs",
|
|
28
|
+
"test:watch": "vitest --config .configs/vitest.config.mjs",
|
|
29
|
+
"coverage": "node tests/run-vitest.mjs --coverage-quiet && npm run test:cjs",
|
|
30
|
+
"ci:coverage": "npm run coverage",
|
|
31
|
+
"types:build": "tsc",
|
|
32
|
+
"types:check": "tsc --noEmit",
|
|
33
|
+
"test:types": "npm run types:check",
|
|
15
34
|
"lint": "eslint --config .configs/eslint.config.mjs .",
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"
|
|
35
|
+
"lint:fix": "eslint --config .configs/eslint.config.mjs . --fix",
|
|
36
|
+
"format": "prettier --config .configs/.prettierrc --write .",
|
|
37
|
+
"format:check": "prettier --config .configs/.prettierrc --check .",
|
|
38
|
+
"fix:headers": "fix-headers --config .configs/fix-headers.json"
|
|
19
39
|
},
|
|
20
40
|
"keywords": [
|
|
21
41
|
"json",
|
|
@@ -49,28 +69,37 @@
|
|
|
49
69
|
"url": "https://github.com/CLDMV/wisp/issues"
|
|
50
70
|
},
|
|
51
71
|
"homepage": "https://github.com/CLDMV/wisp#readme",
|
|
52
|
-
"license": "
|
|
72
|
+
"license": "Apache-2.0",
|
|
53
73
|
"funding": {
|
|
54
74
|
"type": "github",
|
|
55
75
|
"url": "https://github.com/sponsors/shinrai"
|
|
56
76
|
},
|
|
57
77
|
"files": [
|
|
58
|
-
"
|
|
78
|
+
"dist/",
|
|
79
|
+
"!dist/**/*.map",
|
|
59
80
|
"types/",
|
|
60
|
-
"index.mjs",
|
|
61
|
-
"index.cjs",
|
|
62
81
|
"README.md",
|
|
63
|
-
"LICENSE"
|
|
64
|
-
"package.json"
|
|
82
|
+
"LICENSE"
|
|
65
83
|
],
|
|
66
84
|
"devDependencies": {
|
|
85
|
+
"@cldmv/configs": "^1.2.4",
|
|
86
|
+
"@cldmv/eslint-plugin-jsonv": "^1.0.15",
|
|
87
|
+
"@cldmv/fix-headers": "^2.2.0",
|
|
88
|
+
"@cldmv/jsonv": "^1.1.3",
|
|
89
|
+
"@cldmv/prettier-plugin-jsonv": "^1.1.3",
|
|
90
|
+
"@cldmv/vitest-runner": "^1.5.3",
|
|
91
|
+
"@eslint/css": "^2.0.0",
|
|
67
92
|
"@eslint/js": "^10.0.1",
|
|
93
|
+
"@eslint/json": "^2.1.0",
|
|
94
|
+
"@eslint/markdown": "^8.0.3",
|
|
68
95
|
"@types/node": "^26.6.2",
|
|
69
|
-
"
|
|
70
|
-
"eslint": "^10.
|
|
71
|
-
"globals": "^17.
|
|
72
|
-
"
|
|
73
|
-
"
|
|
96
|
+
"@vitest/coverage-v8": "^5.0.3",
|
|
97
|
+
"eslint": "^10.12.0",
|
|
98
|
+
"globals": "^17.13.0",
|
|
99
|
+
"prettier": "^3.9.9",
|
|
100
|
+
"tsup": "^8.5.1",
|
|
101
|
+
"typescript": "^6.0.3",
|
|
102
|
+
"vitest": "^5.0.3"
|
|
74
103
|
},
|
|
75
104
|
"publishConfig": {
|
|
76
105
|
"access": "public"
|