@cldmv/slothlet 1.0.0
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 +210 -0
- package/README.md +115 -0
- package/package.json +56 -0
- package/slothlet.mjs +1218 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License.
|
|
67
|
+
|
|
68
|
+
Subject to the terms and conditions of this License, each Contributor
|
|
69
|
+
hereby grants to You a perpetual, worldwide, non-exclusive, no-charge,
|
|
70
|
+
royalty-free, irrevocable copyright license to reproduce, prepare
|
|
71
|
+
Derivative Works of, publicly display, publicly perform, sublicense,
|
|
72
|
+
and distribute the Work and such Derivative Works in Source or Object
|
|
73
|
+
form.
|
|
74
|
+
|
|
75
|
+
3. Grant of Patent License.
|
|
76
|
+
|
|
77
|
+
Subject to the terms and conditions of this License, each Contributor
|
|
78
|
+
hereby grants to You a perpetual, worldwide, non-exclusive, no-charge,
|
|
79
|
+
royalty-free, irrevocable (except as stated in this section) patent
|
|
80
|
+
license to make, have made, use, offer to sell, sell, import, and
|
|
81
|
+
otherwise transfer the Work, where such license applies only to those
|
|
82
|
+
patent claims licensable by such Contributor that are necessarily
|
|
83
|
+
infringed by their Contribution(s) alone or by combination of their
|
|
84
|
+
Contribution(s) with the Work to which such Contribution(s) was
|
|
85
|
+
submitted. If You institute patent litigation against any entity
|
|
86
|
+
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
|
87
|
+
the Work or a Contribution incorporated within the Work constitutes
|
|
88
|
+
direct or contributory patent infringement, then any patent licenses
|
|
89
|
+
granted to You under this License for that Work shall terminate as of
|
|
90
|
+
the date such litigation is filed.
|
|
91
|
+
|
|
92
|
+
4. Redistribution.
|
|
93
|
+
|
|
94
|
+
You may reproduce and distribute copies of the Work or Derivative
|
|
95
|
+
Works thereof in any medium, with or without modifications, and in
|
|
96
|
+
Source or Object form, provided that You meet the following conditions:
|
|
97
|
+
|
|
98
|
+
(a) You must give any other recipients of the Work or Derivative Works
|
|
99
|
+
a copy of this License; and
|
|
100
|
+
|
|
101
|
+
(b) You must cause any modified files to carry prominent notices
|
|
102
|
+
stating that You changed the files; and
|
|
103
|
+
|
|
104
|
+
(c) You must retain, in the Source form of any Derivative Works that
|
|
105
|
+
You distribute, all copyright, patent, trademark, and attribution
|
|
106
|
+
notices from the Source form of the Work, excluding those notices
|
|
107
|
+
that do not pertain to any part of the Derivative Works; and
|
|
108
|
+
|
|
109
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
110
|
+
distribution, then any Derivative Works that You distribute must
|
|
111
|
+
include a readable copy of the attribution notices contained
|
|
112
|
+
within such NOTICE file, excluding those notices that do not
|
|
113
|
+
pertain to any part of the Derivative Works, in at least one
|
|
114
|
+
of the following places: within a NOTICE text file distributed
|
|
115
|
+
as part of the Derivative Works; within the Source form or
|
|
116
|
+
documentation, if provided along with the Derivative Works; or,
|
|
117
|
+
within a display generated by the Derivative Works, if and
|
|
118
|
+
wherever such third-party notices normally appear. The contents
|
|
119
|
+
of the NOTICE file are for informational purposes only and
|
|
120
|
+
do not modify the License. You may add Your own attribution
|
|
121
|
+
notices within Derivative Works that You distribute, alongside
|
|
122
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
123
|
+
that such additional attribution notices cannot be construed
|
|
124
|
+
as modifying the License.
|
|
125
|
+
|
|
126
|
+
You may add Your own copyright statement to Your modifications and
|
|
127
|
+
may provide additional or different license terms and conditions
|
|
128
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
129
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
130
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
131
|
+
the conditions stated in this License.
|
|
132
|
+
|
|
133
|
+
5. Submission of Contributions.
|
|
134
|
+
|
|
135
|
+
Unless You explicitly state otherwise, any Contribution intentionally
|
|
136
|
+
submitted for inclusion in the Work by You to the Licensor shall be
|
|
137
|
+
under the terms and conditions of this License, without any additional
|
|
138
|
+
terms or conditions. Notwithstanding the above, nothing herein shall
|
|
139
|
+
supersede or modify the terms of any separate license agreement you
|
|
140
|
+
may have executed with Licensor regarding such Contributions.
|
|
141
|
+
|
|
142
|
+
6. Trademarks.
|
|
143
|
+
|
|
144
|
+
This License does not grant permission to use the trade names,
|
|
145
|
+
trademarks, service marks, or product names of the Licensor, except
|
|
146
|
+
as required for describing the origin of the Work and reproducing
|
|
147
|
+
the content of the NOTICE file.
|
|
148
|
+
|
|
149
|
+
7. Disclaimer of Warranty.
|
|
150
|
+
|
|
151
|
+
Unless required by applicable law or agreed to in writing, Licensor
|
|
152
|
+
provides the Work (and each Contributor provides its Contributions)
|
|
153
|
+
on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
|
154
|
+
either express or implied, including, without limitation, any
|
|
155
|
+
warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY,
|
|
156
|
+
or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for
|
|
157
|
+
determining the appropriateness of using or redistributing the Work
|
|
158
|
+
and assume any risks associated with Your exercise of permissions
|
|
159
|
+
under this License.
|
|
160
|
+
|
|
161
|
+
8. Limitation of Liability.
|
|
162
|
+
|
|
163
|
+
In no event and under no legal theory, whether in tort (including
|
|
164
|
+
negligence), contract, or otherwise, unless required by applicable
|
|
165
|
+
law (such as deliberate and grossly negligent acts) or agreed to in
|
|
166
|
+
writing, shall any Contributor be liable to You for damages, including
|
|
167
|
+
any direct, indirect, special, incidental, or consequential damages
|
|
168
|
+
of any character arising as a result of this License or out of the
|
|
169
|
+
use or inability to use the Work (including but not limited to damages
|
|
170
|
+
for loss of goodwill, work stoppage, computer failure or malfunction,
|
|
171
|
+
or any and all other commercial damages or losses), even if such
|
|
172
|
+
Contributor has been advised of the possibility of such damages.
|
|
173
|
+
|
|
174
|
+
9. Accepting Warranty or Additional Liability.
|
|
175
|
+
|
|
176
|
+
While redistributing the Work or Derivative Works thereof, You may choose
|
|
177
|
+
to offer, and charge a fee for, acceptance of support, warranty, indemnity,
|
|
178
|
+
or other liability obligations and/or rights consistent with this License.
|
|
179
|
+
However, in accepting such obligations, You may act only on Your own behalf
|
|
180
|
+
and on Your sole responsibility, not on behalf of any other Contributor,
|
|
181
|
+
and only if You agree to indemnify, defend, and hold each Contributor
|
|
182
|
+
harmless for any liability incurred by, or claims asserted against, such
|
|
183
|
+
Contributor by reason of your accepting any such warranty or additional
|
|
184
|
+
liability.
|
|
185
|
+
|
|
186
|
+
END OF TERMS AND CONDITIONS
|
|
187
|
+
|
|
188
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
189
|
+
|
|
190
|
+
To apply the Apache License to your work, attach the following boilerplate
|
|
191
|
+
notice, with the fields enclosed by brackets "[]" replaced with your own
|
|
192
|
+
identifying information. (Don't include the brackets!) The text should be
|
|
193
|
+
enclosed in the appropriate comment syntax for the file format. We also
|
|
194
|
+
recommend that a file or class name and description of purpose be included
|
|
195
|
+
on the same "printed page" as the copyright notice for easier
|
|
196
|
+
identification within third-party archives.
|
|
197
|
+
|
|
198
|
+
Copyright [yyyy] [name of copyright owner]
|
|
199
|
+
|
|
200
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
201
|
+
you may not use this file except in compliance with the License.
|
|
202
|
+
You may obtain a copy of the License at
|
|
203
|
+
|
|
204
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
205
|
+
|
|
206
|
+
Unless required by applicable law or agreed to in writing, software
|
|
207
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
208
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
209
|
+
See the License for the specific language governing permissions and
|
|
210
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# @cldmv/slothlet
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@cldmv/slothlet)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+

|
|
6
|
+

|
|
7
|
+

|
|
8
|
+
|
|
9
|
+
> **⚠️ WARNING: This module is under active development!**
|
|
10
|
+
>
|
|
11
|
+
> - The standard **eager mode** works well and is stable for production use.
|
|
12
|
+
> - **Lazy mode** has been tested and works for most use cases.
|
|
13
|
+
> - **Experimental modes** (such as worker, child process, and advanced context binding) are in development and may not be fully functional or stable.
|
|
14
|
+
>
|
|
15
|
+
> Please report issues, contribute, and use experimental features with caution.
|
|
16
|
+
|
|
17
|
+
**@cldmv/slothlet** is a modern, lazy modular API loader for Node.js projects. Designed for developers who want fast, flexible, and memory-efficient access to large modular APIs, it dynamically loads API modules and submodules only when accessed. Slothlet supports both lazy and eager loading, automatic flattening of single-file modules, and descriptive error handling—all with a fun branding twist: "slothlet is anything but slow."
|
|
18
|
+
|
|
19
|
+
## Features
|
|
20
|
+
|
|
21
|
+
- **Lazy loading:** API endpoints are loaded on demand using ES6 Proxy, minimizing initial load time and memory usage.
|
|
22
|
+
- **Eager loading:** Optionally load all modules up front for maximum performance.
|
|
23
|
+
- **Automatic flattening:** Single-file modules (e.g., `math/math.mjs`) become `api.math`.
|
|
24
|
+
- **CamelCase mapping:** Filenames and folders with dashes are mapped to camelCase API properties (e.g., `root-math.mjs` → `api.rootMath`).
|
|
25
|
+
- **Descriptive errors:** Throws clear errors for missing API paths or methods.
|
|
26
|
+
- **Configurable:** Set root directory and lazy loading depth.
|
|
27
|
+
- **ESM-first:** Clean, standards-based API.
|
|
28
|
+
- **TypeScript-friendly:** JSDoc-annotated API for editor support.
|
|
29
|
+
- **Lightweight:** No dependencies, minimal footprint.
|
|
30
|
+
|
|
31
|
+
## Installation
|
|
32
|
+
|
|
33
|
+
```sh
|
|
34
|
+
npm install @cldmv/slothlet
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
|
|
39
|
+
> **Note:**
|
|
40
|
+
>
|
|
41
|
+
> - When using **lazy mode**, all API calls must be `await`ed, as modules are loaded asynchronously on access.
|
|
42
|
+
> - If you need to create more than one slothlet instance, you must import the slothlet module using a param string (e.g., `import slothlet from "./slothlet.mjs?instance=1"`).
|
|
43
|
+
> - **Caveat:** Live bindings (`self`, `context`, `reference`) will only work correctly if you use the same param string for both the loader and any submodules that import those bindings. Mismatched param strings will result in separate module instances and broken live bindings.
|
|
44
|
+
|
|
45
|
+
### Node.js API
|
|
46
|
+
|
|
47
|
+
```js
|
|
48
|
+
import slothlet from "@cldmv/slothlet";
|
|
49
|
+
|
|
50
|
+
// Lazy load from default directory
|
|
51
|
+
const api = await slothlet.create({ lazy: true, context });
|
|
52
|
+
|
|
53
|
+
// Access API endpoints
|
|
54
|
+
const sum = await api.math.add(2, 3); // 5
|
|
55
|
+
const upper = await api.string.upper("abc"); // 'ABC'
|
|
56
|
+
const today = await api.nested.date.today(); // '2025-08-15'
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
// Eager load from a custom directory
|
|
61
|
+
const api = await slothlet.create({ lazy: false, dir: "./api_test" context });
|
|
62
|
+
|
|
63
|
+
// Access API endpoints
|
|
64
|
+
const sum = api.math.add(2, 3); // 5
|
|
65
|
+
const upper = api.string.upper("abc"); // 'ABC'
|
|
66
|
+
const today = api.nested.date.today(); // '2025-08-15'
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## API Reference
|
|
70
|
+
|
|
71
|
+
### Main Methods
|
|
72
|
+
|
|
73
|
+
- `slothlet.create(options)` — Loads API modules and returns a bound API object. Options include lazy/eager mode, context, reference, and directory.
|
|
74
|
+
- `slothlet.load(config, ctxRef)` — Loads API modules (lazy or eager) and binds context/reference. Returns the API object.
|
|
75
|
+
- `slothlet.createBoundApi(context, reference)` — Returns an API object bound to the provided context and reference.
|
|
76
|
+
- `slothlet.getApi()` — Returns the loaded API object (Proxy or plain object).
|
|
77
|
+
- `slothlet.getBoundApi()` — Returns the bound API object (with context/reference).
|
|
78
|
+
- `slothlet.isLoaded()` — Returns true if the API is loaded.
|
|
79
|
+
- `slothlet.shutdown()` — Gracefully shuts down the API and internal resources.
|
|
80
|
+
|
|
81
|
+
### Config Options
|
|
82
|
+
|
|
83
|
+
- `lazy` (boolean): Enable lazy loading (default: true)
|
|
84
|
+
- `lazyDepth` (number): How deep to lazy load subdirectories (default: Infinity)
|
|
85
|
+
- `dir` (string): Directory to load API modules from (default: loader's directory)
|
|
86
|
+
- `debug` (boolean): Enable debug logging (default: false)
|
|
87
|
+
|
|
88
|
+
### Context & Reference
|
|
89
|
+
|
|
90
|
+
- `context`: Object passed to API modules for contextual data (e.g., user/session info)
|
|
91
|
+
- `reference`: Object passed to API modules for additional references or configuration
|
|
92
|
+
|
|
93
|
+
### Error Handling
|
|
94
|
+
|
|
95
|
+
- Throws descriptive errors for missing API paths or methods
|
|
96
|
+
- All errors include the requested path and directory for easier debugging
|
|
97
|
+
|
|
98
|
+
### JSDoc
|
|
99
|
+
|
|
100
|
+
See JSDoc comments in the source code for detailed type annotations and usage examples.
|
|
101
|
+
|
|
102
|
+
## Module Structure
|
|
103
|
+
|
|
104
|
+
- Single-file modules: `api_test/math/math.mjs` → `api.math`
|
|
105
|
+
- Multi-file modules: `api_test/multi/alpha.mjs` → `api.multi.alpha`, `api_test/multi/beta.mjs` → `api.multi.beta`
|
|
106
|
+
- Dashes in names: `api_test/root-math.mjs` → `api.rootMath`
|
|
107
|
+
|
|
108
|
+
## Safety & Errors
|
|
109
|
+
|
|
110
|
+
- Throws descriptive errors for missing API paths or methods
|
|
111
|
+
- Only loads modules when accessed (lazy) or all at once (eager)
|
|
112
|
+
|
|
113
|
+
## License
|
|
114
|
+
|
|
115
|
+
Apache-2.0 © Shinrai / CLDMV
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cldmv/slothlet",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Slothlet: Lazy Modular API Loader for Node.js. Dynamically loads API modules and submodules only when accessed, supporting both lazy and eager loading.",
|
|
5
|
+
"main": "slothlet.mjs",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./slothlet.mjs"
|
|
8
|
+
},
|
|
9
|
+
"directories": {
|
|
10
|
+
"test": "tests"
|
|
11
|
+
},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"test": "vitest run",
|
|
14
|
+
"testjest": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"api",
|
|
18
|
+
"lazy-loading",
|
|
19
|
+
"modular",
|
|
20
|
+
"nodejs",
|
|
21
|
+
"esm"
|
|
22
|
+
],
|
|
23
|
+
"author": {
|
|
24
|
+
"name": "Shinrai",
|
|
25
|
+
"email": "git@cldmv.net",
|
|
26
|
+
"url": "https://cldmv.net"
|
|
27
|
+
},
|
|
28
|
+
"contributors": [
|
|
29
|
+
{
|
|
30
|
+
"name": "CLDMV",
|
|
31
|
+
"url": "https://github.com/CLDMV"
|
|
32
|
+
}
|
|
33
|
+
],
|
|
34
|
+
"license": "Apache-2.0",
|
|
35
|
+
"type": "module",
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"jest": "^30.0.5",
|
|
38
|
+
"vest": "^5.4.6",
|
|
39
|
+
"vitest": "^3.2.4"
|
|
40
|
+
},
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "git+https://github.com/CLDMV/slothlet.git"
|
|
44
|
+
},
|
|
45
|
+
"bugs": {
|
|
46
|
+
"url": "https://github.com/CLDMV/slothlet/issues"
|
|
47
|
+
},
|
|
48
|
+
"homepage": "https://github.com/CLDMV/slothlet#readme",
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"access": "public"
|
|
51
|
+
},
|
|
52
|
+
"files": [
|
|
53
|
+
"README.md",
|
|
54
|
+
"LICENSE"
|
|
55
|
+
]
|
|
56
|
+
}
|