-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PathMiddleware.js
142 lines (130 loc) · 4.04 KB
/
PathMiddleware.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { posix } from 'node:path';
/** @typedef {import('../types').HttpTransaction} HttpTransaction */
/** @typedef {import('../types').IMiddleware} IMiddleware */
/** @typedef {import('../types').MiddlewareFunction} MiddlewareFunction */
/** @typedef {import('../types').RequestMethod} RequestMethod */
/** @typedef {RegExp|string} PathEntry */
/**
* @typedef {Object} PathHistoryEntry hello?
* @prop {string} base
* @prop {number[]} treeIndex
*/
/**
* @typedef {Object} PathState
* @prop {PathHistoryEntry[]} history
* @prop {string} currentPath
*/
/**
* @typedef {Object} PathMiddlewareOptions
* @prop {PathEntry|PathEntry[]} [path]
* @prop {boolean} [absolute=false]
* Path is not relative to previous PathMiddleware. Defaults to `false`.
* @prop {boolean} [subPath=false]
* Path values are subpaths. Default to `false`;
*/
export default class PathMiddleware {
/**
* @param {PathEntry|PathEntry[]} entry
*/
static SUBPATH(entry) {
const path = Array.isArray(entry) ? entry : [entry];
return new PathMiddleware({
path: path.map((p) => (typeof p === 'string' ? new RegExp(`^(${p})/*.*$`) : p)),
subPath: true,
});
}
/**
* @param {string} path
* @param {RegExp | string} input
* @return {?string}
*/
static test(path, input) {
if (typeof input === 'string') {
return (path === input ? input : null);
}
const result = input.exec(path);
if (!result) return null;
if (result.length === 1) {
return result[0];
}
return result[1] ?? result[0];
}
/**
* @param {HttpTransaction} transaction
* @param {string} base new base subpath
* @param {string} currentPath
* @return {void}
*/
static WritePathState(transaction, base, currentPath) {
const { state } = transaction;
if (!state.path) {
state.path = { history: [], currentPath };
} else if (!state.path.history) {
state.path.history = [];
}
state.path.history.push({ base, treeIndex: [...state.treeIndex] });
state.path.currentPath = currentPath;
}
/**
* @param {HttpTransaction} transaction
* @return {string} joined base path
*/
static ReadPathState(transaction) {
const { path, treeIndex } = transaction.state;
if (!path || !path.history || !path.history.length) {
return '/';
}
const paths = [];
let newLength = 0;
/* eslint-disable no-labels */
historyLoop: {
for (const item of path.history) {
if (item.treeIndex.length >= treeIndex.length) break;
// TODO: Confirm length-1
for (let index = 0; index < item.treeIndex.length - 1; index++) {
if (item.treeIndex[index] !== treeIndex[index]) break historyLoop;
}
paths.push(item.base);
newLength++;
}
}
if (path.history.length !== newLength) {
path.history.length = newLength;
}
if (!paths.length) {
return '/';
}
return posix.join(...paths);
}
/** @param {PathMiddlewareOptions|PathEntry|PathEntry[]} options */
constructor(options) {
if (Array.isArray(options)) {
this.path = options;
this.absolute = false;
this.subPath = false;
} else if (typeof options === 'string' || options instanceof RegExp) {
this.path = [options];
this.absolute = false;
this.subPath = false;
} else {
this.path = Array.isArray(options.path) ? options.path : [options.path];
this.absolute = options.absolute === true;
this.subPath = options.subPath === true;
}
}
/** @type {MiddlewareFunction} */
execute(transaction) {
const currentPath = this.absolute ? '' : PathMiddleware.ReadPathState(transaction);
const comparison = this.absolute ? transaction.request.pathname : `/${posix.relative(currentPath, transaction.request.pathname)}`;
for (const path of this.path) {
const result = PathMiddleware.test(comparison, path);
if (result) {
if (this.subPath) {
PathMiddleware.WritePathState(transaction, result, posix.join(currentPath, result));
}
return true;
}
}
return false;
}
}