lien 3.4.4 → 5.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/DOCUMENTATION.md +4 -290
- package/LICENSE +1 -1
- package/README.md +14 -320
- package/assets/logo.png +0 -0
- package/example/index.js +8 -10
- package/example/login.js +23 -36
- package/example/secure.js +6 -10
- package/lib/errors.js +58 -0
- package/lib/hook-registry.js +221 -0
- package/lib/hooks.js +302 -0
- package/lib/index.js +15 -1114
- package/lib/lien.js +539 -0
- package/lib/server.js +590 -0
- package/lib/sessions.js +56 -0
- package/lib/to-buffer.js +32 -45
- package/lib/utils.js +75 -0
- package/package.json +34 -21
- package/.github/FUNDING.yml +0 -4
- package/.travis.yml +0 -3
- package/CONTRIBUTING.md +0 -41
- package/test/index.js +0 -280
- package/test/public/foo/index.html +0 -1
- package/test/public/index.html +0 -12
- package/test/public/test.html +0 -1
- package/test/views/main.ajs +0 -1
package/lib/hooks.js
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import { EventEmitter } from "node:events";
|
|
2
|
+
import { asError } from "./utils.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @callback HookNext
|
|
6
|
+
* @param {Error} [error] The failure, or no argument to continue.
|
|
7
|
+
* @returns {void}
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @callback HookCallback
|
|
12
|
+
* @param {*} data A Lien request, response data, or custom execution value.
|
|
13
|
+
* @param {HookNext} [next] Completes a callback-style hook; pass an error to fail.
|
|
14
|
+
* @returns {*} Async hooks may return a promise. Returned values do not replace data.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @callback HookDone
|
|
19
|
+
* @param {Error|null} error The failure, or null on success or cancellation.
|
|
20
|
+
* @param {*} data The original execution data.
|
|
21
|
+
* @returns {void}
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @typedef {number|string} HookMode
|
|
26
|
+
* Use 1/PARALLEL, 2/UNORDERED, or 3/ORDERED. Only these values and names are accepted.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @typedef {object} HookRunOptions
|
|
31
|
+
* @property {AbortSignal} [signal] Cancels execution without treating cancellation as a hook error.
|
|
32
|
+
* @property {function(): boolean} [isStopped] Stops further hooks when true.
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @typedef {Function|{sequence: Array<HookTask>}|{parallel: Array<HookTask>}} HookTask
|
|
37
|
+
* A snapshot entry containing a callback or a group of entries.
|
|
38
|
+
* @private
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @typedef {object} HookState
|
|
43
|
+
* @property {boolean} active Whether the execution is still running.
|
|
44
|
+
* @property {function(): boolean} stopped Checks completion, cancellation, and the caller's stop condition.
|
|
45
|
+
* @private
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Hooks
|
|
50
|
+
* Stores hook definitions. Execution state is local to run().
|
|
51
|
+
*
|
|
52
|
+
* @extends EventEmitter
|
|
53
|
+
* @fires Hooks#end
|
|
54
|
+
*/
|
|
55
|
+
export default class Hooks extends EventEmitter {
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Runs sequentially before the other entries.
|
|
59
|
+
* @constant {number}
|
|
60
|
+
*/
|
|
61
|
+
static PARALLEL = 1;
|
|
62
|
+
/**
|
|
63
|
+
* Runs consecutive entries together.
|
|
64
|
+
* @constant {number}
|
|
65
|
+
*/
|
|
66
|
+
static UNORDERED = 2;
|
|
67
|
+
/**
|
|
68
|
+
* Runs sequentially in registration order.
|
|
69
|
+
* @constant {number}
|
|
70
|
+
*/
|
|
71
|
+
static ORDERED = 3;
|
|
72
|
+
/**
|
|
73
|
+
* Registered callbacks and nested pipelines; contains no request state.
|
|
74
|
+
* @private
|
|
75
|
+
* @type {Array<{hook: HookCallback|Hooks, mode: number}>}
|
|
76
|
+
*/
|
|
77
|
+
#entries = [];
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Appends a hook, nested pipeline, or array of hooks.
|
|
81
|
+
* The legacy PARALLEL mode is a serial prelude; consecutive UNORDERED hooks run together.
|
|
82
|
+
*
|
|
83
|
+
* @param {HookCallback|Hooks|Array<HookCallback|Hooks>} hook The definitions to append.
|
|
84
|
+
* @param {HookMode} [mode=3] The execution mode.
|
|
85
|
+
* @returns {Hooks} This pipeline.
|
|
86
|
+
* @throws {TypeError} If the hook or mode is invalid, or composition would create a cycle.
|
|
87
|
+
*/
|
|
88
|
+
add (hook, mode = Hooks.ORDERED) {
|
|
89
|
+
if (Array.isArray(hook)) {
|
|
90
|
+
for (const item of hook) {
|
|
91
|
+
this.add(item, mode);
|
|
92
|
+
}
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
if (typeof mode === "string") {
|
|
96
|
+
mode = Hooks[mode];
|
|
97
|
+
if (typeof mode !== "number") {
|
|
98
|
+
throw new TypeError("Invalid hook execution mode.");
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
mode ??= Hooks.ORDERED;
|
|
102
|
+
if (![1, 2, 3].includes(mode)) {
|
|
103
|
+
throw new TypeError("Invalid hook execution mode.");
|
|
104
|
+
}
|
|
105
|
+
if (typeof hook !== "function" && !(hook instanceof Hooks)) {
|
|
106
|
+
throw new TypeError("Expected a hook function or Lien.Hooks pipeline.");
|
|
107
|
+
}
|
|
108
|
+
if (hook instanceof Hooks && hook.#contains(this)) {
|
|
109
|
+
throw new TypeError("Hook pipelines cannot contain cycles.");
|
|
110
|
+
}
|
|
111
|
+
this.#entries.push({ hook, mode });
|
|
112
|
+
return this;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Checks nested pipelines before adding a definition to avoid cycles.
|
|
117
|
+
*
|
|
118
|
+
* @private
|
|
119
|
+
* @param {Hooks} pipeline The pipeline to find.
|
|
120
|
+
* @returns {boolean}
|
|
121
|
+
*/
|
|
122
|
+
#contains (pipeline) {
|
|
123
|
+
return this === pipeline || this.#entries.some(({ hook }) => hook instanceof Hooks && hook.#contains(pipeline));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Copies the execution plan so later registrations cannot change an active run.
|
|
128
|
+
*
|
|
129
|
+
* @private
|
|
130
|
+
* @returns {HookTask} The execution plan, containing no request data.
|
|
131
|
+
*/
|
|
132
|
+
#snapshot () {
|
|
133
|
+
const first = [],
|
|
134
|
+
ordered = [];
|
|
135
|
+
let unordered;
|
|
136
|
+
for (const { hook, mode } of this.#entries) {
|
|
137
|
+
const task = hook instanceof Hooks ? hook.#snapshot() : hook;
|
|
138
|
+
if (mode === Hooks.UNORDERED) {
|
|
139
|
+
if (!unordered) {
|
|
140
|
+
unordered = [];
|
|
141
|
+
ordered.push({
|
|
142
|
+
parallel: unordered
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
unordered.push(task);
|
|
146
|
+
} else {
|
|
147
|
+
unordered = undefined;
|
|
148
|
+
(mode === Hooks.PARALLEL ? first : ordered).push(task);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
// Legacy PARALLEL entries actually execute sequentially before the rest.
|
|
152
|
+
return {
|
|
153
|
+
sequence: [...first, ...ordered]
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Executes hooks with independent state, awaiting async and callback hooks.
|
|
159
|
+
* Cancellation stops subsequent hooks and resolves with the same data; running work is not undone.
|
|
160
|
+
*
|
|
161
|
+
* @template T
|
|
162
|
+
* @param {T} [data={}] The shared value for this execution; mutate it in place.
|
|
163
|
+
* @param {HookRunOptions} [options={}] Cancellation and stop checks.
|
|
164
|
+
* @returns {Promise<T>} The original data value after hooks complete or are canceled.
|
|
165
|
+
* @throws {Error} If a hook throws, rejects, or passes an error to next().
|
|
166
|
+
* @fires Hooks#end
|
|
167
|
+
*/
|
|
168
|
+
async run (data = {}, options = {}) {
|
|
169
|
+
const { signal, isStopped = () => false } = options;
|
|
170
|
+
const plan = this.#snapshot();
|
|
171
|
+
const state = {
|
|
172
|
+
active: true,
|
|
173
|
+
stopped: () => !state.active || signal?.aborted || isStopped()
|
|
174
|
+
};
|
|
175
|
+
let onAbort;
|
|
176
|
+
const aborted = new Promise(resolve => {
|
|
177
|
+
onAbort = resolve;
|
|
178
|
+
if (signal?.aborted) {
|
|
179
|
+
resolve();
|
|
180
|
+
} else {
|
|
181
|
+
signal?.addEventListener("abort", onAbort, {
|
|
182
|
+
once: true
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
let failure = null;
|
|
187
|
+
try {
|
|
188
|
+
await Promise.race([execute(plan, data, state), aborted]);
|
|
189
|
+
return data;
|
|
190
|
+
} catch (error) {
|
|
191
|
+
failure = asError(error, "Hook failed.");
|
|
192
|
+
throw failure;
|
|
193
|
+
} finally {
|
|
194
|
+
state.active = false;
|
|
195
|
+
signal?.removeEventListener("abort", onAbort);
|
|
196
|
+
this.emit("end", failure, data);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Executes hooks with the legacy callback interface.
|
|
202
|
+
* start(callback) creates a fresh empty object. Prefer run() when awaiting the result.
|
|
203
|
+
*
|
|
204
|
+
* @param {*|HookDone} [data={}] The execution data, or the completion callback.
|
|
205
|
+
* @param {HookDone} [callback] Receives the error and original data.
|
|
206
|
+
* @param {function(): boolean} [isStopped] Stops subsequent hooks when true.
|
|
207
|
+
* @returns {function(): void} Cancels this execution; completion is reported once.
|
|
208
|
+
*/
|
|
209
|
+
start (data = {}, callback = () => {}, isStopped) {
|
|
210
|
+
if (typeof data === "function") {
|
|
211
|
+
callback = data;
|
|
212
|
+
data = {};
|
|
213
|
+
}
|
|
214
|
+
const controller = new AbortController();
|
|
215
|
+
this.run(data, {
|
|
216
|
+
signal: controller.signal,
|
|
217
|
+
isStopped
|
|
218
|
+
}).then(result => callback(null, result), error => callback(error, data));
|
|
219
|
+
return () => controller.abort();
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Executes a snapshot recursively, preserving sequential and unordered groups.
|
|
225
|
+
*
|
|
226
|
+
* @private
|
|
227
|
+
* @param {HookTask} task The task or group.
|
|
228
|
+
* @param {*} data The execution data.
|
|
229
|
+
* @param {HookState} state The state owned by this run.
|
|
230
|
+
* @returns {Promise<void>}
|
|
231
|
+
*/
|
|
232
|
+
async function execute (task, data, state) {
|
|
233
|
+
if (state.stopped()) {
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (typeof task === "function") {
|
|
238
|
+
return invoke(task, data, state);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (task.parallel) {
|
|
242
|
+
await Promise.all(task.parallel.map(child => execute(child, data, state)));
|
|
243
|
+
} else {
|
|
244
|
+
for (const child of task.sequence) {
|
|
245
|
+
if (state.stopped()) {
|
|
246
|
+
break;
|
|
247
|
+
}
|
|
248
|
+
await execute(child, data, state);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Invokes one hook. Callback hooks settle once even when they also return a promise.
|
|
255
|
+
*
|
|
256
|
+
* @private
|
|
257
|
+
* @param {HookCallback} hook The callback or async hook.
|
|
258
|
+
* @param {*} data The execution data.
|
|
259
|
+
* @param {HookState} state The state owned by this run.
|
|
260
|
+
* @returns {Promise<void>}
|
|
261
|
+
*/
|
|
262
|
+
async function invoke (hook, data, state) {
|
|
263
|
+
await Promise.resolve();
|
|
264
|
+
if (state.stopped()) {
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (hook.length < 2) {
|
|
269
|
+
await hook(data);
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
// Keep callback hooks working alongside async functions.
|
|
273
|
+
await new Promise((resolve, reject) => {
|
|
274
|
+
let settled = false;
|
|
275
|
+
const next = error => {
|
|
276
|
+
if (settled) {
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
settled = true;
|
|
280
|
+
if (error) {
|
|
281
|
+
reject(error);
|
|
282
|
+
} else {
|
|
283
|
+
resolve();
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
try {
|
|
287
|
+
const result = hook(data, next);
|
|
288
|
+
if (result && typeof result.then === "function") {
|
|
289
|
+
Promise.resolve(result).then(() => next(), error => next(asError(error, "Hook promise rejected.")));
|
|
290
|
+
}
|
|
291
|
+
} catch (error) {
|
|
292
|
+
next(asError(error, "Hook threw without an error."));
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Reports completion of one execution, including canceled executions.
|
|
299
|
+
* @event Hooks#end
|
|
300
|
+
* @param {Error|null} error The failure, or null on success or cancellation.
|
|
301
|
+
* @param {*} data The original execution data.
|
|
302
|
+
*/
|