@awesomeness-js/server 1.1.10 → 1.1.13

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 (76) hide show
  1. package/build/build.js +4 -1
  2. package/build/postBuild.js +4 -1
  3. package/index.js +6 -4
  4. package/package.json +1 -1
  5. package/src/componentAndPageMemory.js +297 -0
  6. package/src/componentDependencies.js +7 -19
  7. package/src/extractUiComponentRefs.js +55 -0
  8. package/src/fetchPage.js +79 -4
  9. package/src/resolveRealCasePath.js +0 -4
  10. package/tests/componentAndPageMemory.test.js +172 -0
  11. package/tests/fetchPage.test.js +259 -0
  12. package/tests/fixtures/site-and-components/components/app/cleanMain/index.js +26 -0
  13. package/tests/fixtures/site-and-components/components/app/cleanMain.js +14 -0
  14. package/tests/fixtures/site-and-components/components/app/index.css +4 -0
  15. package/tests/fixtures/site-and-components/components/app/index.js +42 -0
  16. package/tests/fixtures/site-and-components/components/app/insertIntoList.jquery.js +150 -0
  17. package/tests/fixtures/site-and-components/components/app/keyUpWithTimeout.jQuery.js +26 -0
  18. package/tests/fixtures/site-and-components/components/app/onEnter.jQuery.js +39 -0
  19. package/tests/fixtures/site-and-components/components/app/onResize.jQuery.js +64 -0
  20. package/tests/fixtures/site-and-components/components/app/pwa/_.css +305 -0
  21. package/tests/fixtures/site-and-components/components/app/pwa/index.js +235 -0
  22. package/tests/fixtures/site-and-components/components/app/pwa/updateProfileImage.js +7 -0
  23. package/tests/fixtures/site-and-components/components/app/shapes.css +3 -0
  24. package/tests/fixtures/site-and-components/components/app/simple/_.css +151 -0
  25. package/tests/fixtures/site-and-components/components/app/simple/index.js +170 -0
  26. package/tests/fixtures/site-and-components/components/app/start.js +165 -0
  27. package/tests/fixtures/site-and-components/components/app/vanilla/_.css +1 -0
  28. package/tests/fixtures/site-and-components/components/app/vanilla/index.js +27 -0
  29. package/tests/fixtures/site-and-components/components/card/index.js +88 -0
  30. package/tests/fixtures/site-and-components/components/cardMain/index.js +86 -0
  31. package/tests/fixtures/site-and-components/components/cardMount/index.js +86 -0
  32. package/tests/fixtures/site-and-components/components/pageInit/index.js +86 -0
  33. package/tests/fixtures/site-and-components/components/pageScript/index.js +86 -0
  34. package/tests/fixtures/site-and-components/components/pageWidget/index.js +86 -0
  35. package/tests/fixtures/site-and-components/components/scrollSpy/elm.js +172 -0
  36. package/tests/fixtures/site-and-components/components/scrollSpy/index.js +63 -0
  37. package/tests/fixtures/site-and-components/components/scrollSpy/observerPoolGet.js +91 -0
  38. package/tests/fixtures/site-and-components/components/scrollSpy/observerPoolRegistry.js +18 -0
  39. package/tests/fixtures/site-and-components/components/scrollSpy/observerPoolSubscribe.js +37 -0
  40. package/tests/fixtures/site-and-components/components/scrollSpy/observerPoolUnsubscribe.js +44 -0
  41. package/tests/fixtures/site-and-components/components/scrollSpy/top.js +86 -0
  42. package/tests/fixtures/site-and-components/sites/site-a/pages/home/init.js +78 -0
  43. package/tests/fixtures/site-and-components/sites/site-a/pages/home/js/index.js +93 -0
  44. package/tests/setup.js +1 -0
  45. package/tsconfig.json +18 -0
  46. package/types/errors.d.ts +0 -0
  47. package/types/index.d.ts +109 -0
  48. package/types/src/applicationMap.d.ts +1 -0
  49. package/types/src/awesomenessNormalizeRequest.d.ts +27 -0
  50. package/types/src/brotliJsonResponse.d.ts +10 -0
  51. package/types/src/checkAccess.d.ts +5 -0
  52. package/types/src/componentAndPageMemory.d.ts +75 -0
  53. package/types/src/componentDependencies.d.ts +6 -0
  54. package/types/src/config.d.ts +7 -0
  55. package/types/src/extractUiComponentRefs.d.ts +5 -0
  56. package/types/src/fetchPage.d.ts +4 -0
  57. package/types/src/getConfig.d.ts +2 -0
  58. package/types/src/getMD.d.ts +1 -0
  59. package/types/src/init.d.ts +2 -0
  60. package/types/src/koa/attachAwesomenessRequest.d.ts +1 -0
  61. package/types/src/koa/cors.d.ts +1 -0
  62. package/types/src/koa/errorHandler.d.ts +1 -0
  63. package/types/src/koa/finalFormat.d.ts +1 -0
  64. package/types/src/koa/jsonBodyParser.d.ts +1 -0
  65. package/types/src/koa/routeRequest.d.ts +1 -0
  66. package/types/src/koa/serverUp.d.ts +1 -0
  67. package/types/src/koa/staticFiles.d.ts +1 -0
  68. package/types/src/koa/timeout.d.ts +1 -0
  69. package/types/src/pageInfo.d.ts +8 -0
  70. package/types/src/reRoute.d.ts +7 -0
  71. package/types/src/resolveRealCasePath.d.ts +1 -0
  72. package/types/src/specialPaths.d.ts +3 -0
  73. package/types/src/start.d.ts +1 -0
  74. package/types/src/validateRequest.d.ts +2 -0
  75. package/types/src/ws/handlers.d.ts +4 -0
  76. package/types/src/ws/index.d.ts +1 -0
@@ -0,0 +1,86 @@
1
+ export default function() {
2
+
3
+ const items = [
4
+ {
5
+ id: "itm-1",
6
+ title: "First",
7
+ score: 11
8
+ },
9
+ {
10
+ id: "itm-2",
11
+ title: "Second",
12
+ score: 27
13
+ },
14
+ {
15
+ id: "itm-3",
16
+ title: "Third",
17
+ score: 42
18
+ },
19
+ {
20
+ id: "itm-4",
21
+ title: "Fourth",
22
+ score: 31
23
+ },
24
+ {
25
+ id: "itm-5",
26
+ title: "Fifth",
27
+ score: 18
28
+ },
29
+ {
30
+ id: "itm-6",
31
+ title: "Sixth",
32
+ score: 54
33
+ },
34
+ {
35
+ id: "itm-7",
36
+ title: "Seventh",
37
+ score: 39
38
+ },
39
+ {
40
+ id: "itm-8",
41
+ title: "Eighth",
42
+ score: 23
43
+ },
44
+ ];
45
+
46
+ const grouped = items.reduce((acc, item) => {
47
+
48
+ const bucket = item.score >= 40 ? "high" : item.score >= 25 ? "medium" : "low";
49
+
50
+ acc[bucket] = acc[bucket] || [];
51
+ acc[bucket].push(item);
52
+
53
+ return acc;
54
+
55
+ }, {});
56
+
57
+ const sorted = [ ...items ].sort((a, b) => b.score - a.score);
58
+ const topThree = sorted.slice(0, 3);
59
+
60
+ const details = {
61
+ count: items.length,
62
+ topThree,
63
+ grouped,
64
+ mountRef,
65
+ signature: `${topThree.map((item) => item.id).join("|")}:${items.length}`,
66
+ };
67
+
68
+ if (details.count > 5) {
69
+
70
+ ui.cardMain();
71
+
72
+ }
73
+
74
+ if (details.topThree.length === 3) {
75
+
76
+ ui.cardMount.mount();
77
+
78
+ }
79
+
80
+ return {
81
+ $card,
82
+ details,
83
+ };
84
+
85
+
86
+ }
@@ -0,0 +1,86 @@
1
+ export default function() {
2
+
3
+ const items = [
4
+ {
5
+ id: "itm-1",
6
+ title: "First",
7
+ score: 11
8
+ },
9
+ {
10
+ id: "itm-2",
11
+ title: "Second",
12
+ score: 27
13
+ },
14
+ {
15
+ id: "itm-3",
16
+ title: "Third",
17
+ score: 42
18
+ },
19
+ {
20
+ id: "itm-4",
21
+ title: "Fourth",
22
+ score: 31
23
+ },
24
+ {
25
+ id: "itm-5",
26
+ title: "Fifth",
27
+ score: 18
28
+ },
29
+ {
30
+ id: "itm-6",
31
+ title: "Sixth",
32
+ score: 54
33
+ },
34
+ {
35
+ id: "itm-7",
36
+ title: "Seventh",
37
+ score: 39
38
+ },
39
+ {
40
+ id: "itm-8",
41
+ title: "Eighth",
42
+ score: 23
43
+ },
44
+ ];
45
+
46
+ const grouped = items.reduce((acc, item) => {
47
+
48
+ const bucket = item.score >= 40 ? "high" : item.score >= 25 ? "medium" : "low";
49
+
50
+ acc[bucket] = acc[bucket] || [];
51
+ acc[bucket].push(item);
52
+
53
+ return acc;
54
+
55
+ }, {});
56
+
57
+ const sorted = [ ...items ].sort((a, b) => b.score - a.score);
58
+ const topThree = sorted.slice(0, 3);
59
+
60
+ const details = {
61
+ count: items.length,
62
+ topThree,
63
+ grouped,
64
+ mountRef,
65
+ signature: `${topThree.map((item) => item.id).join("|")}:${items.length}`,
66
+ };
67
+
68
+ if (details.count > 5) {
69
+
70
+ ui.cardMain();
71
+
72
+ }
73
+
74
+ if (details.topThree.length === 3) {
75
+
76
+ ui.cardMount.mount();
77
+
78
+ }
79
+
80
+ return {
81
+ $card,
82
+ details,
83
+ };
84
+
85
+
86
+ }
@@ -0,0 +1,86 @@
1
+ export default function() {
2
+
3
+ const items = [
4
+ {
5
+ id: "itm-1",
6
+ title: "First",
7
+ score: 11
8
+ },
9
+ {
10
+ id: "itm-2",
11
+ title: "Second",
12
+ score: 27
13
+ },
14
+ {
15
+ id: "itm-3",
16
+ title: "Third",
17
+ score: 42
18
+ },
19
+ {
20
+ id: "itm-4",
21
+ title: "Fourth",
22
+ score: 31
23
+ },
24
+ {
25
+ id: "itm-5",
26
+ title: "Fifth",
27
+ score: 18
28
+ },
29
+ {
30
+ id: "itm-6",
31
+ title: "Sixth",
32
+ score: 54
33
+ },
34
+ {
35
+ id: "itm-7",
36
+ title: "Seventh",
37
+ score: 39
38
+ },
39
+ {
40
+ id: "itm-8",
41
+ title: "Eighth",
42
+ score: 23
43
+ },
44
+ ];
45
+
46
+ const grouped = items.reduce((acc, item) => {
47
+
48
+ const bucket = item.score >= 40 ? "high" : item.score >= 25 ? "medium" : "low";
49
+
50
+ acc[bucket] = acc[bucket] || [];
51
+ acc[bucket].push(item);
52
+
53
+ return acc;
54
+
55
+ }, {});
56
+
57
+ const sorted = [ ...items ].sort((a, b) => b.score - a.score);
58
+ const topThree = sorted.slice(0, 3);
59
+
60
+ const details = {
61
+ count: items.length,
62
+ topThree,
63
+ grouped,
64
+ mountRef,
65
+ signature: `${topThree.map((item) => item.id).join("|")}:${items.length}`,
66
+ };
67
+
68
+ if (details.count > 5) {
69
+
70
+ ui.cardMain();
71
+
72
+ }
73
+
74
+ if (details.topThree.length === 3) {
75
+
76
+ ui.cardMount.mount();
77
+
78
+ }
79
+
80
+ return {
81
+ $card,
82
+ details,
83
+ };
84
+
85
+
86
+ }
@@ -0,0 +1,86 @@
1
+ export default function() {
2
+
3
+ const items = [
4
+ {
5
+ id: "itm-1",
6
+ title: "First",
7
+ score: 11
8
+ },
9
+ {
10
+ id: "itm-2",
11
+ title: "Second",
12
+ score: 27
13
+ },
14
+ {
15
+ id: "itm-3",
16
+ title: "Third",
17
+ score: 42
18
+ },
19
+ {
20
+ id: "itm-4",
21
+ title: "Fourth",
22
+ score: 31
23
+ },
24
+ {
25
+ id: "itm-5",
26
+ title: "Fifth",
27
+ score: 18
28
+ },
29
+ {
30
+ id: "itm-6",
31
+ title: "Sixth",
32
+ score: 54
33
+ },
34
+ {
35
+ id: "itm-7",
36
+ title: "Seventh",
37
+ score: 39
38
+ },
39
+ {
40
+ id: "itm-8",
41
+ title: "Eighth",
42
+ score: 23
43
+ },
44
+ ];
45
+
46
+ const grouped = items.reduce((acc, item) => {
47
+
48
+ const bucket = item.score >= 40 ? "high" : item.score >= 25 ? "medium" : "low";
49
+
50
+ acc[bucket] = acc[bucket] || [];
51
+ acc[bucket].push(item);
52
+
53
+ return acc;
54
+
55
+ }, {});
56
+
57
+ const sorted = [ ...items ].sort((a, b) => b.score - a.score);
58
+ const topThree = sorted.slice(0, 3);
59
+
60
+ const details = {
61
+ count: items.length,
62
+ topThree,
63
+ grouped,
64
+ mountRef,
65
+ signature: `${topThree.map((item) => item.id).join("|")}:${items.length}`,
66
+ };
67
+
68
+ if (details.count > 5) {
69
+
70
+ ui.cardMain();
71
+
72
+ }
73
+
74
+ if (details.topThree.length === 3) {
75
+
76
+ ui.cardMount.mount();
77
+
78
+ }
79
+
80
+ return {
81
+ $card,
82
+ details,
83
+ };
84
+
85
+
86
+ }
@@ -0,0 +1,172 @@
1
+ import ui from '#ui';
2
+
3
+ export default ({
4
+ $elm,
5
+ callback = () => {},
6
+ root = null,
7
+ rootMargin = "0px 0px",
8
+ threshold = 0.01,
9
+ once = true,
10
+
11
+ // auto cleanup
12
+ destroyOnRemove = true,
13
+ removeRoot = null, // defaults below
14
+ debug = false
15
+ } = {}) => {
16
+
17
+ const registry = ui.scrollSpy.observerPoolRegistry();
18
+
19
+ if (!$elm) throw new Error("observer: missing $elm");
20
+
21
+ const el = $elm?.[0] || $elm;
22
+
23
+ if (!el || !el.nodeType) {
24
+
25
+ throw new Error("observer: $elm must be a DOM element or a jQuery-like wrapper");
26
+
27
+ }
28
+
29
+ if (!removeRoot) removeRoot = document.documentElement || document.body;
30
+
31
+ let pool = null;
32
+ let mo = null;
33
+ let connectMo = null;
34
+
35
+ let last = null;
36
+ let destroyed = false;
37
+ let started = false;
38
+ const subscriber = {
39
+ safeCall: (isIntersecting, entry) => safeCall(isIntersecting, entry)
40
+ };
41
+
42
+ const log = (...args) => debug && console.log("[observer]", ...args);
43
+
44
+ const safeCall = (v, entry) => {
45
+
46
+ if (destroyed) return;
47
+
48
+ v = !!v;
49
+
50
+ if (v === last) return;
51
+ last = v;
52
+
53
+ try {
54
+
55
+ callback(v, entry);
56
+
57
+ } catch (e) {}
58
+
59
+ if (once && v) destroy();
60
+
61
+ };
62
+
63
+ const destroy = () => {
64
+
65
+ if (destroyed) return;
66
+ destroyed = true;
67
+
68
+ ui.scrollSpy.observerPoolUnsubscribe({
69
+ registry,
70
+ poolEntry: pool,
71
+ targetElm: el,
72
+ subscriber
73
+ });
74
+ pool = null;
75
+
76
+ if (mo) mo.disconnect();
77
+ mo = null;
78
+
79
+ if (connectMo) connectMo.disconnect();
80
+ connectMo = null;
81
+
82
+ log("destroyed");
83
+
84
+ };
85
+
86
+ const start = () => {
87
+
88
+ if (destroyed || started) return;
89
+ started = true;
90
+
91
+ pool = ui.scrollSpy.observerPoolGet({
92
+ registry,
93
+ root,
94
+ rootMargin,
95
+ threshold
96
+ });
97
+
98
+ ui.scrollSpy.observerPoolSubscribe({
99
+ poolEntry: pool,
100
+ targetElm: el,
101
+ subscriber
102
+ });
103
+
104
+ log("started", {
105
+ el,
106
+ root,
107
+ rootMargin,
108
+ threshold,
109
+ sharedObserverPool: true
110
+ });
111
+
112
+ // auto destroy on removal
113
+ if (destroyOnRemove) {
114
+
115
+ mo = new MutationObserver(() => {
116
+
117
+ if (!el.isConnected) destroy();
118
+
119
+ });
120
+
121
+ mo.observe(removeRoot, {
122
+ childList: true,
123
+ subtree: true
124
+ });
125
+
126
+ }
127
+
128
+ };
129
+
130
+ // If it's already connected, start immediately.
131
+ // If not, wait until it becomes connected.
132
+ if (el.isConnected) {
133
+
134
+ start();
135
+
136
+ } else {
137
+
138
+ log("waiting for element to connect...");
139
+
140
+ connectMo = new MutationObserver(() => {
141
+
142
+ if (destroyed) return;
143
+
144
+ if (el.isConnected) {
145
+
146
+ connectMo.disconnect();
147
+ connectMo = null;
148
+
149
+ // wait 1 frame so layout exists
150
+ requestAnimationFrame(() => start());
151
+
152
+ }
153
+
154
+ });
155
+
156
+ connectMo.observe(removeRoot, {
157
+ childList: true,
158
+ subtree: true
159
+ });
160
+
161
+ }
162
+
163
+ // stash destroy
164
+ try {
165
+
166
+ $elm?.data?.("observerDestroy", destroy);
167
+
168
+ } catch (e) {}
169
+
170
+ return { destroy };
171
+
172
+ };
@@ -0,0 +1,63 @@
1
+ export default ({
2
+ $spyOn,
3
+ $applyTo,
4
+ deadZone = 10
5
+ } = {}) => {
6
+
7
+ const $classEl = ($applyTo && $applyTo.length)
8
+ ? $applyTo
9
+ : $('body');
10
+
11
+ const spyTarget = ($spyOn && $spyOn.length)
12
+ ? $spyOn.get(0)
13
+ : window;
14
+
15
+ const isWindow = (spyTarget === window);
16
+
17
+ const className = 'spy-scrolling-down';
18
+
19
+ let lastTop = isWindow
20
+ ? (window.pageYOffset || document.documentElement.scrollTop || 0)
21
+ : (spyTarget.scrollTop || 0);
22
+
23
+ let ticking = false;
24
+
25
+ function apply(isDown) {
26
+
27
+ if (isDown) $classEl.addClass(className);
28
+ else $classEl.removeClass(className);
29
+
30
+ }
31
+
32
+ function readTop() {
33
+
34
+ return isWindow
35
+ ? (window.pageYOffset || document.documentElement.scrollTop || 0)
36
+ : (spyTarget.scrollTop || 0);
37
+
38
+ }
39
+
40
+ function onRaf() {
41
+
42
+ ticking = false;
43
+
44
+ const topNow = readTop();
45
+ const delta = topNow - lastTop;
46
+
47
+ if (Math.abs(delta) < deadZone) return;
48
+
49
+ apply(delta > 0);
50
+ lastTop = topNow;
51
+
52
+ }
53
+
54
+ spyTarget.addEventListener('scroll', function() {
55
+
56
+ if (ticking) return;
57
+
58
+ ticking = true;
59
+ requestAnimationFrame(onRaf);
60
+
61
+ }, { passive: true });
62
+
63
+ };
@@ -0,0 +1,91 @@
1
+ export default ({
2
+ registry,
3
+ root,
4
+ rootMargin,
5
+ threshold
6
+ } = {}) => {
7
+
8
+ if (!registry) {
9
+
10
+ throw new Error('observerPoolGet: missing registry');
11
+
12
+ }
13
+
14
+ const toThresholdKey = (value) => {
15
+
16
+ if (Array.isArray(value)) {
17
+
18
+ return value.join(',');
19
+
20
+ }
21
+
22
+ return String(value);
23
+
24
+ };
25
+
26
+ const getRootKey = (rootElm) => {
27
+
28
+ if (!rootElm) {
29
+
30
+ return 'viewport';
31
+
32
+ }
33
+
34
+ if (!registry.rootIds.has(rootElm)) {
35
+
36
+ registry.rootSeq += 1;
37
+ registry.rootIds.set(rootElm, `root-${registry.rootSeq}`);
38
+
39
+ }
40
+
41
+ return registry.rootIds.get(rootElm);
42
+
43
+ };
44
+
45
+ const key = `${getRootKey(root)}|${rootMargin}|${toThresholdKey(threshold)}`;
46
+
47
+ if (registry.pools.has(key)) {
48
+
49
+ return registry.pools.get(key);
50
+
51
+ }
52
+
53
+ const targets = new Map();
54
+
55
+ const observer = new IntersectionObserver((entries) => {
56
+
57
+ entries.forEach((entry) => {
58
+
59
+ const subscribers = targets.get(entry.target);
60
+
61
+ if (!subscribers || subscribers.size === 0) {
62
+
63
+ return;
64
+
65
+ }
66
+
67
+ subscribers.forEach((subscriber) => {
68
+
69
+ subscriber.safeCall(entry.isIntersecting, entry);
70
+
71
+ });
72
+
73
+ });
74
+
75
+ }, {
76
+ root,
77
+ rootMargin,
78
+ threshold
79
+ });
80
+
81
+ const poolEntry = {
82
+ key,
83
+ observer,
84
+ targets
85
+ };
86
+
87
+ registry.pools.set(key, poolEntry);
88
+
89
+ return poolEntry;
90
+
91
+ };
@@ -0,0 +1,18 @@
1
+ export default () => {
2
+
3
+ const OBSERVER_POOL_KEY = '__awesomenessScrollSpyElmPool__';
4
+ const globalScope = typeof window !== 'undefined' ? window : globalThis;
5
+
6
+ if (!globalScope[OBSERVER_POOL_KEY]) {
7
+
8
+ globalScope[OBSERVER_POOL_KEY] = {
9
+ pools: new Map(),
10
+ rootIds: new WeakMap(),
11
+ rootSeq: 0
12
+ };
13
+
14
+ }
15
+
16
+ return globalScope[OBSERVER_POOL_KEY];
17
+
18
+ };
@@ -0,0 +1,37 @@
1
+ export default ({
2
+ poolEntry,
3
+ targetElm,
4
+ subscriber
5
+ } = {}) => {
6
+
7
+ if (!poolEntry) {
8
+
9
+ throw new Error('observerPoolSubscribe: missing poolEntry');
10
+
11
+ }
12
+
13
+ if (!targetElm) {
14
+
15
+ throw new Error('observerPoolSubscribe: missing targetElm');
16
+
17
+ }
18
+
19
+ if (!subscriber) {
20
+
21
+ throw new Error('observerPoolSubscribe: missing subscriber');
22
+
23
+ }
24
+
25
+ let subscribers = poolEntry.targets.get(targetElm);
26
+
27
+ if (!subscribers) {
28
+
29
+ subscribers = new Set();
30
+ poolEntry.targets.set(targetElm, subscribers);
31
+ poolEntry.observer.observe(targetElm);
32
+
33
+ }
34
+
35
+ subscribers.add(subscriber);
36
+
37
+ };