ovr 5.0.0 → 6.0.1

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.
Files changed (86) hide show
  1. package/dist/LICENSE.md +26 -0
  2. package/dist/app/index.d.ts +49 -83
  3. package/dist/app/index.d.ts.map +1 -1
  4. package/dist/app/index.js +40 -92
  5. package/dist/context/index.d.ts +138 -0
  6. package/dist/context/index.d.ts.map +1 -0
  7. package/dist/context/index.js +218 -0
  8. package/dist/cookie/index.d.ts +121 -0
  9. package/dist/cookie/index.d.ts.map +1 -0
  10. package/dist/cookie/index.js +56 -0
  11. package/dist/index.d.ts +6 -7
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +4 -6
  14. package/dist/jsx/{intrinsic-elements.d.ts → elements.d.ts} +14 -7
  15. package/dist/jsx/elements.d.ts.map +1 -0
  16. package/dist/jsx/index.d.ts +40 -53
  17. package/dist/jsx/index.d.ts.map +1 -1
  18. package/dist/jsx/index.js +71 -200
  19. package/dist/middleware/index.d.ts +22 -0
  20. package/dist/middleware/index.d.ts.map +1 -0
  21. package/dist/multipart/index.d.ts +122 -0
  22. package/dist/multipart/index.d.ts.map +1 -0
  23. package/dist/multipart/index.js +340 -0
  24. package/dist/render/index.d.ts +78 -0
  25. package/dist/render/index.d.ts.map +1 -0
  26. package/dist/render/index.js +248 -0
  27. package/dist/route/index.d.ts +147 -0
  28. package/dist/route/index.d.ts.map +1 -0
  29. package/dist/route/index.js +126 -0
  30. package/dist/trie/index.d.ts +30 -30
  31. package/dist/trie/index.d.ts.map +1 -1
  32. package/dist/trie/index.js +67 -82
  33. package/dist/util/index.d.ts +36 -0
  34. package/dist/util/index.d.ts.map +1 -0
  35. package/dist/util/index.js +87 -0
  36. package/package.json +3 -3
  37. package/src/app/index.ts +99 -224
  38. package/src/context/index.ts +262 -0
  39. package/src/cookie/index.ts +189 -0
  40. package/src/index.ts +6 -7
  41. package/src/jsx/{intrinsic-elements.ts → elements.ts} +14 -7
  42. package/src/jsx/index.ts +81 -221
  43. package/src/middleware/index.ts +29 -0
  44. package/src/multipart/index.ts +485 -0
  45. package/src/render/index.ts +300 -0
  46. package/src/route/index.ts +332 -0
  47. package/src/trie/index.ts +87 -111
  48. package/src/util/index.ts +109 -0
  49. package/dist/app/context.d.ts +0 -149
  50. package/dist/app/context.d.ts.map +0 -1
  51. package/dist/app/context.js +0 -246
  52. package/dist/app/helper/get.d.ts +0 -30
  53. package/dist/app/helper/get.d.ts.map +0 -1
  54. package/dist/app/helper/get.js +0 -33
  55. package/dist/app/helper/index.d.ts +0 -51
  56. package/dist/app/helper/index.d.ts.map +0 -1
  57. package/dist/app/helper/index.js +0 -82
  58. package/dist/app/helper/post.d.ts +0 -35
  59. package/dist/app/helper/post.d.ts.map +0 -1
  60. package/dist/app/helper/post.js +0 -15
  61. package/dist/app/memo/index.d.ts +0 -12
  62. package/dist/app/memo/index.d.ts.map +0 -1
  63. package/dist/app/memo/index.js +0 -34
  64. package/dist/jsx/chunk/index.d.ts +0 -38
  65. package/dist/jsx/chunk/index.d.ts.map +0 -1
  66. package/dist/jsx/chunk/index.js +0 -69
  67. package/dist/jsx/intrinsic-elements.d.ts.map +0 -1
  68. package/dist/jsx/merge-async-generators.d.ts +0 -14
  69. package/dist/jsx/merge-async-generators.d.ts.map +0 -1
  70. package/dist/jsx/merge-async-generators.js +0 -42
  71. package/dist/types/index.d.ts +0 -17
  72. package/dist/types/index.d.ts.map +0 -1
  73. package/dist/util/hash.d.ts +0 -11
  74. package/dist/util/hash.d.ts.map +0 -1
  75. package/dist/util/hash.js +0 -16
  76. package/src/app/context.ts +0 -303
  77. package/src/app/helper/get.ts +0 -46
  78. package/src/app/helper/index.ts +0 -156
  79. package/src/app/helper/post.ts +0 -54
  80. package/src/app/memo/index.ts +0 -40
  81. package/src/jsx/chunk/index.ts +0 -79
  82. package/src/jsx/merge-async-generators.ts +0 -47
  83. package/src/types/index.ts +0 -42
  84. package/src/util/hash.ts +0 -17
  85. /package/dist/jsx/{intrinsic-elements.js → elements.js} +0 -0
  86. /package/dist/{types → middleware}/index.js +0 -0
@@ -1,54 +1,44 @@
1
- export class Route {
2
- /** Route pattern */
3
- pattern;
4
- /** Store returned when route is found */
5
- store;
6
- /**
7
- * @param pattern route pattern
8
- * @param store value to store in the patterns final node
9
- */
10
- constructor(pattern, store) {
11
- if (pattern[0] !== "/") {
12
- throw new Error(`Invalid route: ${pattern} - route pattern must begin with "/"`);
13
- }
14
- this.pattern = pattern;
15
- this.store = store;
16
- }
17
- }
18
1
  class ParamNode {
19
2
  /** Name of the parameter (without the colon ":") */
20
3
  name;
21
4
  /** Matched route */
22
- route = null;
5
+ route;
23
6
  /** Static child node */
24
- staticChild = null;
7
+ child;
8
+ /**
9
+ * Create a new parameter node.
10
+ *
11
+ * @param name Name of the parameter.
12
+ */
25
13
  constructor(name) {
26
14
  this.name = name;
27
15
  }
28
16
  }
29
17
  export class Trie {
30
18
  /** Unique segment of the pattern trie */
31
- segment;
19
+ seg;
32
20
  /** Static child node map, key is the first character in the segment */
33
- staticMap = null;
21
+ map;
34
22
  /** Parametric child node */
35
- paramChild = null;
23
+ param;
36
24
  /** Matched route */
37
- route = null;
25
+ route;
38
26
  /** Matched wildcard route */
39
- wildcardRoute = null;
27
+ wild;
40
28
  static #paramMatch = /:.+?(?=\/|$)/g;
41
29
  static #paramSplit = /:.+?(?=\/|$)/;
42
30
  /**
31
+ * Create a new trie.
32
+ *
43
33
  * @param segment pattern segment
44
- * @param staticChildren static children nodes to add to staticMap
34
+ * @param children static children nodes to add to staticMap
45
35
  */
46
- constructor(segment = "/", staticChildren) {
47
- this.segment = segment;
48
- if (staticChildren?.length) {
49
- this.staticMap ??= new Map();
50
- for (const child of staticChildren) {
51
- this.staticMap.set(child.segment.charCodeAt(0), child);
36
+ constructor(segment = "/", children) {
37
+ this.seg = segment;
38
+ if (children?.length) {
39
+ this.map ??= new Map();
40
+ for (const child of children) {
41
+ this.map.set(child.seg.charCodeAt(0), child);
52
42
  }
53
43
  }
54
44
  }
@@ -58,10 +48,10 @@ export class Trie {
58
48
  */
59
49
  clone(segment) {
60
50
  const clone = new Trie(segment);
61
- clone.staticMap = this.staticMap;
62
- clone.paramChild = this.paramChild;
51
+ clone.map = this.map;
52
+ clone.param = this.param;
63
53
  clone.route = this.route;
64
- clone.wildcardRoute = this.wildcardRoute;
54
+ clone.wild = this.wild;
65
55
  return clone;
66
56
  }
67
57
  /**
@@ -74,11 +64,13 @@ export class Trie {
74
64
  * @returns the new child produced from the new segment
75
65
  */
76
66
  fork(charIndex, segment) {
77
- const existingChild = this.clone(this.segment.slice(charIndex)); // "posts/"
78
67
  const newChild = new Trie(segment.slice(charIndex)); // "movies/"
79
68
  Object.assign(this,
80
69
  // "api/" with the above as children
81
- new Trie(this.segment.slice(0, charIndex), [existingChild, newChild]));
70
+ new Trie(this.seg.slice(0, charIndex), [
71
+ this.clone(this.seg.slice(charIndex)), // "posts/"
72
+ newChild,
73
+ ]));
82
74
  return newChild;
83
75
  }
84
76
  /**
@@ -89,19 +81,14 @@ export class Trie {
89
81
  * @param segment
90
82
  */
91
83
  split(segment) {
92
- const secondHalf = this.clone(this.segment.slice(segment.length));
93
- Object.assign(this, new Trie(segment, [secondHalf]));
84
+ Object.assign(this, new Trie(segment, [this.clone(this.seg.slice(segment.length))]));
94
85
  }
95
86
  /**
96
87
  * @param name name of the param
97
88
  * @returns the existing child with the same name, or creates a new
98
89
  */
99
- setParamChild(name) {
100
- if (this.paramChild && this.paramChild.name !== name) {
101
- throw new Error(`Cannot create parameter "${name}" because a different parameter ` +
102
- `("${this.paramChild.name}") already exists in this location.\n\n${this}`);
103
- }
104
- return (this.paramChild ??= new ParamNode(name));
90
+ set(name) {
91
+ return (this.param ??= new ParamNode(name));
105
92
  }
106
93
  /**
107
94
  * @param route route return when pattern is matched
@@ -109,7 +96,7 @@ export class Trie {
109
96
  */
110
97
  add(route) {
111
98
  let current = this;
112
- let pattern = route.pattern; // created to not modify the original
99
+ let pattern = route.method + route.pattern; // created to not modify the original
113
100
  const endsWithWildcard = pattern.endsWith("*");
114
101
  if (endsWithWildcard)
115
102
  pattern = pattern.slice(0, -1);
@@ -126,37 +113,37 @@ export class Trie {
126
113
  if (staticIndex > 0) {
127
114
  // there is only a second static segment (could just be "/")
128
115
  // if there is a param to split them, so there must be a param here
129
- const paramChild = current.setParamChild(
116
+ const paramChild = current.set(
130
117
  // param without the ":" (only increment when this is reached)
131
118
  paramSegments[paramIndex++].slice(1));
132
- if (!paramChild.staticChild) {
119
+ if (!paramChild.child) {
133
120
  // new - create node with the next static segment
134
- current = paramChild.staticChild = new Trie(staticSegment);
121
+ current = paramChild.child = new Trie(staticSegment);
135
122
  continue; // next segment - no need to check since it's new
136
123
  }
137
124
  // there's already a static child - need to check if it's a match
138
- current = paramChild.staticChild;
125
+ current = paramChild.child;
139
126
  }
140
127
  // check if the staticSegment matches the current node
141
128
  for (let charIndex = 0;;) {
142
129
  if (charIndex === staticSegment.length) {
143
130
  // finished iterating through the staticSegment
144
- if (charIndex < current.segment.length) {
131
+ if (charIndex < current.seg.length) {
145
132
  // too short
146
133
  current.split(staticSegment);
147
134
  }
148
135
  break; // next segment
149
136
  }
150
- if (charIndex === current.segment.length) {
137
+ if (charIndex === current.seg.length) {
151
138
  // passed the end of the current node
152
- if (!current.staticMap) {
139
+ if (!current.map) {
153
140
  // new pattern, create new leaf
154
- current.staticMap = new Map();
141
+ current.map = new Map();
155
142
  }
156
143
  else {
157
144
  // there's already static children,
158
145
  // check to see if there's a leaf that starts with the char
159
- const staticChild = current.staticMap.get(staticSegment.charCodeAt(charIndex));
146
+ const staticChild = current.map.get(staticSegment.charCodeAt(charIndex));
160
147
  if (staticChild) {
161
148
  // re-run loop with existing staticChild
162
149
  current = staticChild;
@@ -167,11 +154,11 @@ export class Trie {
167
154
  }
168
155
  // otherwise, add new staticChild
169
156
  const staticChild = new Trie(staticSegment.slice(charIndex));
170
- current.staticMap.set(staticSegment.charCodeAt(charIndex), staticChild);
157
+ current.map.set(staticSegment.charCodeAt(charIndex), staticChild);
171
158
  current = staticChild;
172
159
  break; // next segment
173
160
  }
174
- if (staticSegment[charIndex] !== current.segment[charIndex]) {
161
+ if (staticSegment[charIndex] !== current.seg[charIndex]) {
175
162
  // different than the node - fork
176
163
  current = current.fork(charIndex, staticSegment);
177
164
  break; // next segment
@@ -182,16 +169,15 @@ export class Trie {
182
169
  }
183
170
  if (paramIndex < paramSegments.length) {
184
171
  // final segment is a param
185
- current.setParamChild(paramSegments[paramIndex].slice(1)).route ??=
186
- route;
172
+ current.set(paramSegments[paramIndex].slice(1)).route = route;
187
173
  }
188
174
  else if (endsWithWildcard) {
189
175
  // final segment is a wildcard
190
- current.wildcardRoute ??= route;
176
+ current.wild = route;
191
177
  }
192
178
  else {
193
179
  // final segment is static
194
- current.route ??= route;
180
+ current.route = route;
195
181
  }
196
182
  return this;
197
183
  }
@@ -200,64 +186,63 @@ export class Trie {
200
186
  * @returns `Route` and the matched `params` if found, otherwise `null`
201
187
  */
202
188
  find(pathname) {
189
+ const segmentLength = this.seg.length;
203
190
  if (
204
191
  // too short
205
- pathname.length < this.segment.length ||
192
+ pathname.length < segmentLength ||
206
193
  // segment does not match current node segment
207
- !pathname.startsWith(this.segment)) {
194
+ !pathname.startsWith(this.seg)) {
208
195
  return null;
209
196
  }
210
- if (pathname === this.segment) {
197
+ if (pathname === this.seg) {
211
198
  // reached the end of the path
212
199
  if (this.route)
213
200
  return { route: this.route, params: {} };
214
- if (this.wildcardRoute)
215
- return { route: this.wildcardRoute, params: { "*": "" } };
201
+ if (this.wild)
202
+ return { route: this.wild, params: { "*": "" } };
216
203
  return null;
217
204
  }
218
- if (this.staticMap) {
205
+ if (this.map) {
219
206
  // check for a static leaf that starts with the first character
220
- const staticChild = this.staticMap.get(pathname.charCodeAt(this.segment.length));
207
+ const staticChild = this.map.get(pathname.charCodeAt(segmentLength));
221
208
  if (staticChild) {
222
- const result = staticChild.find(pathname.slice(this.segment.length));
209
+ const result = staticChild.find(pathname.slice(segmentLength));
223
210
  if (result)
224
211
  return result;
225
212
  }
226
213
  }
227
214
  // check for param leaf
228
- if (this.paramChild) {
229
- const slashIndex = pathname.indexOf("/", this.segment.length);
215
+ if (this.param) {
216
+ const slashIndex = pathname.indexOf("/", segmentLength);
230
217
  // if there is not a slash immediately following this.segment
231
- if (slashIndex !== this.segment.length) {
218
+ if (slashIndex !== segmentLength) {
232
219
  // there is a valid parameter
233
220
  if (
234
221
  // param is the end of the pathname
235
222
  slashIndex === -1 &&
236
- this.paramChild.route) {
223
+ this.param.route) {
237
224
  return {
238
- route: this.paramChild.route,
239
- params: {
240
- [this.paramChild.name]: pathname.slice(this.segment.length),
241
- },
225
+ route: this.param.route,
226
+ params: { [this.param.name]: pathname.slice(segmentLength) },
242
227
  };
243
228
  }
244
- else if (this.paramChild.staticChild) {
229
+ else if (this.param.child) {
245
230
  // there's a static node after the param
246
231
  // this is how there can be multiple params, "/" in between
247
- const result = this.paramChild.staticChild.find(pathname.slice(slashIndex));
232
+ const result = this.param.child.find(pathname.slice(slashIndex));
248
233
  if (result) {
249
234
  // add original params to the result
250
- result.params[this.paramChild.name] = pathname.slice(this.segment.length, slashIndex);
235
+ result.params[this.param.name] = pathname.slice(segmentLength, slashIndex);
251
236
  return result;
252
237
  }
253
238
  }
254
239
  }
255
240
  }
256
241
  // check for wildcard leaf
257
- if (this.wildcardRoute) {
242
+ if (this.wild) {
258
243
  return {
259
- route: this.wildcardRoute,
260
- params: { "*": pathname.slice(this.segment.length) },
244
+ route: this.wild,
245
+ params: { "*": pathname.slice(segmentLength) },
261
246
  };
262
247
  }
263
248
  return null;
@@ -0,0 +1,36 @@
1
+ /** General utility types */
2
+ export declare namespace Util {
3
+ type DeepArray<T> = T | DeepArray<T>[];
4
+ }
5
+ /** Header parsing utils */
6
+ export declare class Header {
7
+ static readonly contentType = "content-type";
8
+ /**
9
+ * @param header header value to parse
10
+ * @returns Base/first param
11
+ */
12
+ static shift(header: string | null): [string | null, string | null];
13
+ /**
14
+ * @param header header value to parse
15
+ * @returns Map containing each key=value pair
16
+ */
17
+ static params(header: string | null): Record<string, string>;
18
+ }
19
+ /** Hash util */
20
+ export declare class Hash {
21
+ /**
22
+ * Fast hashing algorithm - [djb2](http://www.cse.yorku.ca/~oz/hash.html)
23
+ *
24
+ * @param s String to hash
25
+ * @returns Hashed string
26
+ */
27
+ static djb2(s: string): string;
28
+ }
29
+ /** Shared encoding to use across features */
30
+ export declare class Codec {
31
+ #private;
32
+ static encode: (s: string) => Uint8Array<ArrayBuffer>;
33
+ /** DO NOT USE FOR STREAMS */
34
+ static decode: (input?: Uint8Array) => string;
35
+ }
36
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/util/index.ts"],"names":[],"mappings":"AAAA,4BAA4B;AAC5B,yBAAiB,IAAI,CAAC;IACrB,KAAY,SAAS,CAAC,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC;CAC9C;AAED,2BAA2B;AAC3B,qBAAa,MAAM;IAClB,MAAM,CAAC,QAAQ,CAAC,WAAW,kBAAkB;IAE7C;;;OAGG;IACH,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,CAAC,MAAM,GAAG,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAWnE;;;OAGG;IACH,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;CAmDnC;AAED,gBAAgB;AAChB,qBAAa,IAAI;IAChB;;;;;OAKG;IACH,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,MAAM;CAQrB;AAED,6CAA6C;AAC7C,qBAAa,KAAK;;IAIjB,MAAM,CAAC,MAAM,GAAI,GAAG,MAAM,6BAA8B;IAExD,6BAA6B;IAC7B,MAAM,CAAC,MAAM,GAAI,QAAQ,UAAU,YAAkC;CACrE"}
@@ -0,0 +1,87 @@
1
+ /** Header parsing utils */
2
+ export class Header {
3
+ static contentType = "content-type";
4
+ /**
5
+ * @param header header value to parse
6
+ * @returns Base/first param
7
+ */
8
+ static shift(header) {
9
+ if (header) {
10
+ const semi = header.indexOf(";");
11
+ if (semi !== -1) {
12
+ return [header.slice(0, semi), header.slice(semi)];
13
+ }
14
+ }
15
+ return [header, null];
16
+ }
17
+ /**
18
+ * @param header header value to parse
19
+ * @returns Map containing each key=value pair
20
+ */
21
+ static params(header) {
22
+ const parsed = {};
23
+ if (header) {
24
+ const headerLength = header.length;
25
+ for (let cursor = 0; cursor < headerLength;) {
26
+ const equal = header.indexOf("=", cursor);
27
+ if (equal === -1)
28
+ break; // done
29
+ let semi = header.indexOf(";", cursor);
30
+ if (semi === -1)
31
+ semi = headerLength;
32
+ if (equal > semi) {
33
+ // equal is in the next pair, current is malformed
34
+ // move the cursor to the start of the next and ignore
35
+ cursor = header.lastIndexOf(";", equal - 1) + 1;
36
+ continue;
37
+ }
38
+ const key = header.slice(cursor, equal).trim();
39
+ if (!(key in parsed)) {
40
+ // first value should take precedence
41
+ let value = header.slice(equal + 1, semi).trim();
42
+ if (value[0] === '"' && value[value.length - 1] === '"') {
43
+ // remove quotes
44
+ value = value.slice(1, -1);
45
+ }
46
+ if (value.includes("%")) {
47
+ // most values don't need to be decoded
48
+ try {
49
+ // cookies must be a simple string
50
+ value = decodeURIComponent(value);
51
+ }
52
+ catch {
53
+ // ignore
54
+ }
55
+ }
56
+ parsed[key] = value;
57
+ }
58
+ cursor = semi + 1;
59
+ }
60
+ }
61
+ return parsed;
62
+ }
63
+ }
64
+ /** Hash util */
65
+ export class Hash {
66
+ /**
67
+ * Fast hashing algorithm - [djb2](http://www.cse.yorku.ca/~oz/hash.html)
68
+ *
69
+ * @param s String to hash
70
+ * @returns Hashed string
71
+ */
72
+ static djb2(s) {
73
+ let hash = 5381;
74
+ let i = s.length;
75
+ while (i)
76
+ hash = (hash * 33) ^ s.charCodeAt(--i);
77
+ return (hash >>> 0).toString(36);
78
+ }
79
+ }
80
+ /** Shared encoding to use across features */
81
+ export class Codec {
82
+ static #encoder = new TextEncoder();
83
+ static #decoder = new TextDecoder("utf-8", { fatal: true });
84
+ static encode = (s) => Codec.#encoder.encode(s);
85
+ /** DO NOT USE FOR STREAMS */
86
+ static decode = (input) => Codec.#decoder.decode(input);
87
+ }
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "ovr",
3
3
  "description": "The Streaming Framework",
4
- "version": "5.0.0",
4
+ "version": "6.0.1",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
8
- "url": "git+https://github.com/rossrobino/ovr.git"
8
+ "url": "git+https://github.com/ovrjs/ovr.git"
9
9
  },
10
10
  "author": {
11
11
  "name": "Ross Robino",
@@ -40,6 +40,6 @@
40
40
  "scripts": {
41
41
  "check": "tsc --noEmit",
42
42
  "dev": "tsc --watch",
43
- "build": "tsc"
43
+ "build": "tsc && cp ../../LICENSE.md dist"
44
44
  }
45
45
  }