vaderjs 1.3.2 → 1.3.3-alpha-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.
package/vaderRouter.js DELETED
@@ -1,231 +0,0 @@
1
-
2
- /**
3
- * @class VaderRouter
4
- * @description - creates an instance of Vader Express Router
5
- *
6
- * @param {String} path
7
- * @param {Function} handler
8
- * @param {object} req request object
9
- * @param {object} res response object
10
- * @returns {Object} Express
11
- *
12
- */
13
- class VaderRouter{
14
- /**
15
- * @constructor
16
- * @param {*} basePath
17
- *
18
- */
19
- constructor(basePath) {
20
- this.routes = [];
21
- this.middlewares = [];
22
- this.errorMiddlewares = [];
23
- this.listeners = [];
24
-
25
- this.basePath = basePath;
26
- }
27
-
28
- /**
29
- * @method get
30
- * @param {String} path
31
- * @param {Function} handler
32
- * @description This method is used to register a get route
33
- * @returns {void}
34
- * @memberof Express
35
- */
36
- get(path, handler) {
37
-
38
- this.routes.push({
39
- path,
40
- handler,
41
- method: 'get',
42
- });
43
- }
44
- /**
45
- * @method use
46
- * @description This method allows you to use middlewares
47
- * @param {Function} middleware
48
- */
49
-
50
- use(/* path, */ middleware) {
51
- this.middlewares.push(middleware);
52
- }
53
-
54
- /**
55
- * @method listen
56
- * @param {*} port - unique id for the listener
57
- * @param {Function} callback - callback function
58
- * @description This method is used to start listening to the routes
59
- * @returns {void}
60
- *
61
- */
62
-
63
- listen(port, callback) {
64
- if(!port){
65
- port = Math.random().toString(36).substring(7);
66
- }
67
- this.listeners.push(port);
68
- callback();
69
- if (this.listeners.length === 1) {
70
- this.handleRoute(window.location.hash);
71
- }else{
72
- this.listeners.pop();
73
- }
74
- if (callback) {
75
- callback();
76
- }
77
- window.onhashchange = () => {
78
- this.handleRoute(window.location.hash);
79
- }
80
- }
81
- /**
82
- * @method extractParams
83
- * @description This method is used to extract parameters from the route path
84
- * @param {*} routePath
85
- * @param {*} hash
86
- * @returns {Object} params
87
- * @memberof Express
88
- */
89
-
90
- extractParams(routePath, hash) {
91
- const routeParts = routePath.split('/');
92
- const hashParts = hash.split('/');
93
- const params = {};
94
- routeParts.forEach((part, index) => {
95
- if (part.startsWith(':')) {
96
- const paramName = part.slice(1);
97
- params[paramName] = hashParts[index];
98
- }else if(part.startsWith('*')){
99
- // remove queries from this par
100
- params[0] = hashParts.slice(index).join('/').split('?')[0];
101
- }
102
- });
103
- return params;
104
- }
105
- extractQueryParams(hash){
106
-
107
- const queryParams = hash.split('?')[1];
108
- if(!queryParams){
109
- return {};
110
- }
111
- const params = {};
112
- queryParams.split('&').forEach((param)=>{
113
- const [key, value] = param.split('=');
114
- params[key] = value;
115
- })
116
- return params;
117
- }
118
-
119
- /**
120
- * @method handleRoute
121
- * @param {String} hash
122
- * @description This method is used to handle the route
123
- */
124
-
125
- handleRoute(hash) {
126
- hash = hash.slice(1);
127
- let status = 200;
128
- let route = this.routes.find((route) => {
129
-
130
- if (route.path === hash) {
131
- return true;
132
- }
133
- const routePathParts = route.path.split('/');
134
- const hashParts = hash.split('/');
135
- if (routePathParts.length !== hashParts.length) {
136
- return false;
137
- }else if(routePathParts[routePathParts.length-1].startsWith('*')){
138
- return true;
139
- }
140
- const params = this.extractParams( route.path, hash);
141
- return Object.keys(params).length > 0;
142
- });
143
-
144
- if (!route) {
145
- route = this.routes.find((route) => {
146
-
147
- if(route.path === '/404'){
148
- return true;
149
- }else{
150
- window.location.hash = this.basePath
151
- }
152
- });
153
-
154
- route ? status = 200 :
155
-
156
- status = 404;
157
- }
158
-
159
-
160
- const queryParams = this.extractQueryParams(hash);
161
- const params = route && route.path ? this.extractParams(route.path, hash) : {};
162
- const req = {
163
- headers: {},
164
- params: params,
165
- query: queryParams,
166
- path: hash,
167
- method: route ? route.method : 'get',
168
- };
169
-
170
- // @ts-ignore
171
- window.$CURRENT_URL = req.path
172
-
173
- // @ts-ignore
174
- window.$FULL_URL = window.location.href.replace('#', '')
175
-
176
- const res = {
177
- status: status,
178
- /**
179
- * @method log
180
- * @param {String} type
181
- * @description This method is used to log the request and response
182
- */
183
- log: (type) => {
184
- if(type === undefined){
185
- console.log(`${req.path} ${req.method} ${res.status} ${req.timestamp}`);
186
- }else{
187
- console.table({
188
- 'Request Path': req.path,
189
- 'Request Method': route.method,
190
- 'Response Status': res.status,
191
- 'Request Timestamp': req.timestamp,
192
- });
193
- }
194
- },
195
- send: (selector, data) => {
196
- if(typeof selector === 'string'){
197
- // @ts-ignore
198
- document.querySelector(selector).innerHTML = data;
199
- }else if(typeof selector === 'number'){
200
- res.status = selector;
201
- }else if(typeof selector === 'object'){
202
- res.status = selector.status;
203
- }
204
- },
205
- json: (selector, data) => {
206
-
207
- if(typeof selector === 'string'){
208
- // @ts-ignore
209
- let obj = document.createElement('object');
210
- // data url
211
- obj.data = URL.createObjectURL(new Blob([JSON.stringify(data)], {type: 'application/json'}));
212
- // @ts-ignore
213
- document.querySelector(selector).appendChild(obj);
214
- }else{
215
- throw new Error('Selector must be a string');
216
- }
217
- },
218
- };
219
- this.middlewares.forEach((middleware) => {
220
- middleware(req, res);
221
- });
222
-
223
- route ? route.handler(req, res) : null;
224
-
225
- }
226
-
227
- }
228
-
229
- export default VaderRouter;
230
-
231
-
package/worker-min.js DELETED
@@ -1 +0,0 @@
1
- onmessage=e=>{let a=Date.now(),l=e.data.strings,s=e.data.args,t="",r=e.data.location.split("/").slice(0,-1).join("/"),c="";for(let e=0;e<l.length;e++)c+=l[e],e<s.length&&(c+=s[e]);let o=c.match(/--([^>]*)--/gs);if(o)for(;o.length;){let e=o.pop();console.log(e),c=c.replace(e,"")}if(c=c.replace(/(#+)(.*)/g,((e,a,l)=>{if(console.log(e),e.includes("<")&&e.includes(">"))return e;{let e=a.length;return`<h ${e} class="markdown_heading">${l}</h${e}>`}})),c=c.replace(/\*\*(.*?)\*\*/g,((e,a)=>`<b class="markdown_bold">${a}</b>`)),c=c.replace(/\*(.*?)\*/g,((e,a)=>`<i class="markdown_italic">${a}</i>`)),c=c.replace(/`(.*?)`/g,((e,a)=>`<code>${a}</code>`)),c=c.replace(/\[([^\]]+)\]\(([^)]+)\)/g,((e,a,l)=>`<a class="markdown_link" href="${l}">${a}</a>`)),c=c.replace(/!\[([^\]]+)\]\(([^)]+)\)/g,((e,a,l)=>`<img class="markdown_image" src="${l}" alt="${a}" />`)),c.split("\n").forEach(((e,a,l)=>{e.match(/^\s*-\s+(.*?)$/gm)&&(c=0!==a&&l[a-1].match(/^\s*-\s+(.*?)$/gm)?a!==l.length-1&&l[a+1].match(/^\s*-\s+(.*?)$/gm)?c.replace(e,`<li>${e.replace(/^\s*-\s+(.*?)$/gm,"$1")}</li>`):c.replace(e,`<li>${e.replace(/^\s*-\s+(.*?)$/gm,"$1")}</li></ul>`):c.replace(e,`<ul class="markdown_unordered" style="list-style-type:disc;list-style:inside"><li>${e.replace(/^\s*-\s+(.*?)$/gm,"$1")}</li>`))})),c.split("\n").forEach(((e,a,l)=>{e.match(/^\s*\d+\.\s+(.*?)$/gm)&&(c=0!==a&&l[a-1].match(/^\s*\d+\.\s+(.*?)$/gm)?a!==l.length-1&&l[a+1].match(/^\s*\d+\.\s+(.*?)$/gm)?c.replace(e,`<li>${e.replace(/^\s*\d+\.\s+(.*?)$/gm,"$1")}</li>`):c.replace(e,`<li>${e.replace(/^\s*\d+\.\s+(.*?)$/gm,"$1")}</li></ol>`):c.replace(e,`<ol class="markdown_ordered" style="list-style-type:decimal;"><li>${e.replace(/^\s*\d+\.\s+(.*?)$/gm,"$1")}</li>`))})),c=c.replace(/^\s*-\s+(.*?)$/gm,((e,a)=>`<li class="markdown_list_item">${a}</li>`)),c=c.replace(/^\s*---\s*$/gm,'<hr class="markdown_horizontal" />'),c=c.replace(/^\s*> (.*)$/gm,((e,a)=>`<blockquote class="markdown_blockquote">${a}</blockquote>`)),c=c.replace(/((?: *\|.*?)+)\n((?: *\|.*?)+)/gm,((e,a,l)=>{const s=a.split("|").slice(1,-1),t=l.split("|").slice(1,-1);let r='<table class="markdown_table">';return r+='<thead class="markdown_table_head"><tr class="markdown_table_row">',s.forEach((e=>{r+=`<th class="markdown_table_header_cell">${e}</th>`})),r+='</tr></thead><tbody class="markdown_table_body"><tr class="markdown_table_row">',t.forEach((e=>{r+=`<td class="markdown_table_body_cell">${e}</td>`})),r+="</tr></tbody></table>",r})),!c.includes("<body>"))throw new Error(`Vader Error: You must enclose your html in a body tag for all components. \n\n${c}`);c=c.replace(/classname/g,"class");let i=c.match(/<img([^>]*)>/g);if(i)for(let a=0;a<i.length;a++){let l=i[a],s=l.match(/src="([^"]*)"/),t=l.match(/alt="([^"]*)"/);if(s)if(s[1].includes("http")||c.includes("\x3c!-- #vader-disable_relative-paths --\x3e")){if(!s[1].includes("http")&&s[1].startsWith(".")&&!c.includes("\x3c!-- #vader-disable_relative-paths --\x3e"))throw new Error(`Vader Error: You cannot use absolute paths since relative paths are not disabled in ${e.data.file}. Use relative paths instead. \n\n${s[1]}`)}else c=c.replace(s[1],`${r}/${s[1]}`);if(!t&&!c.includes("\x3c!-- #vader-disable_accessibility --\x3e"))throw new Error(`Vader Error: You must include an alt attribute in the image tag \n\n${l} of class ${e.data.name}. `);caches.match(`${r}/${s[1]}`)?console.log("already cached",caches.match(`${r}/${s[1]}`)):caches.open("vader").then((e=>{e.add(`${r}/${s[1]}`),console.log("cached",`${r}/${s[1]}`)})).catch((e=>{console.log(e)}))}let n=c.match(/href="([^"]*)"/g);if(n)for(;n.length;){let a=n.pop();if(a=a.replace('href="',"").replace('"',""),a.includes("http")||c.includes("\x3c!-- #vader-disable_relative-paths --\x3e")){if(!a.includes("http")&&a.startsWith(".")&&!c.includes("\x3c!-- #vader-disable_relative-paths --\x3e"))throw new Error(`Vader Error: You cannot use absolute paths since relative paths are not disabled in ${e.data.file}. Use relative paths instead. \n\n${a}`)}else c=c.replace(`href="${a}"`,`href="#${a}"`)}let d=Date.now()-a,h=!1;(r.includes("localhost")||r.includes("127.0.0.1")&&!h)&&(h=!0,c+=`\${console.log('%c${e.data.name} component rendered in ${d}ms','color:#fff;background:#000;padding:5px;border-radius:5px;font-size:12px;font-weight:bold'),""}`);const p=c.split("<script>");p&&p.forEach(((e,a)=>{if(0===a)c=e;else{if(e.includes("// <![CDATA[ <-- For SVG support"))return;let a=e.split("<\/script>")[0];t+=a}}));let $=c.match(/(\$\(.*?\))/gs);if($)for(;$.length;){let e=$.pop();c=c.replace(e,`\${${e.replace("$(","").replace(")","")}}`)}postMessage({template:`<div data-component=${e.data.name}>${c}</div>`,js:t||""})};
package/worker.js DELETED
@@ -1,226 +0,0 @@
1
-
2
- onmessage = (e)=>{
3
- let time_started = Date.now()
4
- let strings = e.data.strings
5
- let args = e.data.args
6
- let js = ''
7
- let l = e.data.location.split('/').slice(0,-1).join('/')
8
- let result = "";
9
- for (let i = 0; i < strings.length; i++) {
10
- result += strings[i];
11
- if (i < args.length) {
12
- result += args[i];
13
- }
14
- }
15
-
16
- let comments = result.match(/--([^>]*)--/gs)
17
- if(comments){
18
- while(comments.length){
19
- let comment = comments.pop()
20
- console.log(comment)
21
- // @ts-ignore
22
- result = result.replace(comment,'')
23
- }
24
- }
25
-
26
-
27
- // Convert headings (e.g., #1-6 Heading => <h1-6>Heading</h1-6>)
28
- // @ts-ignore
29
- result = result.replace(/(#+)(.*)/g, (match, hashes, text) => {
30
- console.log(match)
31
- if(!match.includes('<') || !match.includes('>')){
32
- let level = hashes.length;
33
- return `<h ${level} class="markdown_heading">${text}</h${level}>`;
34
- }else{
35
- return match
36
- }
37
- });
38
-
39
-
40
-
41
-
42
- // Convert bold (e.g., **Bold** => <b>Bold</b>)
43
- result = result.replace(/\*\*(.*?)\*\*/g, (match, text) => {
44
- return `<b class="markdown_bold">${text}</b>`;
45
- });
46
-
47
- // Convert italic (e.g., *Italic* => <i>Italic</i>)
48
- result = result.replace(/\*(.*?)\*/g, (match, text) => {
49
- return `<i class="markdown_italic">${text}</i>`;
50
- });
51
-
52
- // Convert code (e.g., `code` => <code>code</code>)
53
- result = result.replace(/`(.*?)`/g, (match, text) => {
54
- return `<code>${text}</code>`;
55
- });
56
-
57
- // Convert links (e.g., [Text](URL) => <a href="URL">Text</a>)
58
- result = result.replace(/\[([^\]]+)\]\(([^)]+)\)/g, (match, text, url) => {
59
- return `<a class="markdown_link" href="${url}">${text}</a>`;
60
- });
61
-
62
- // Convert images (e.g., ![Alt](URL) => <img src="URL" alt="Alt" />)
63
- result = result.replace(/!\[([^\]]+)\]\(([^)]+)\)/g, (match, alt, src) => {
64
- return `<img class="markdown_image" src="${src}" alt="${alt}" />`;
65
- });
66
-
67
- // Convert unordered lists (e.g., * Item => <ul><li>Item</li></ul>)
68
- result.split('\n').forEach((line, index, arr) => {
69
- if (line.match(/^\s*-\s+(.*?)$/gm)) {
70
- if (index === 0 || !arr[index - 1].match(/^\s*-\s+(.*?)$/gm)) {
71
- result = result.replace(line, `<ul class="markdown_unordered" style="list-style-type:disc;list-style:inside"><li>${line.replace(/^\s*-\s+(.*?)$/gm, '$1')}</li>`);
72
- } else if (index === arr.length - 1 || !arr[index + 1].match(/^\s*-\s+(.*?)$/gm)) {
73
- result = result.replace(line, `<li>${line.replace(/^\s*-\s+(.*?)$/gm, '$1')}</li></ul>`);
74
- } else {
75
- result = result.replace(line, `<li>${line.replace(/^\s*-\s+(.*?)$/gm, '$1')}</li>`);
76
- }
77
- }
78
- });
79
-
80
- // Convert ordered lists (e.g., 1. Item => <ol><li>Item</li></ol>) in order
81
-
82
- result.split('\n').forEach((line, index, arr) => {
83
- if (line.match(/^\s*\d+\.\s+(.*?)$/gm)) {
84
- if (index === 0 || !arr[index - 1].match(/^\s*\d+\.\s+(.*?)$/gm)) {
85
- result = result.replace(line, `<ol class="markdown_ordered" style="list-style-type:decimal;"><li>${line.replace(/^\s*\d+\.\s+(.*?)$/gm, '$1')}</li>`);
86
- } else if (index === arr.length - 1 || !arr[index + 1].match(/^\s*\d+\.\s+(.*?)$/gm)) {
87
- result = result.replace(line, `<li>${line.replace(/^\s*\d+\.\s+(.*?)$/gm, '$1')}</li></ol>`);
88
- } else {
89
- result = result.replace(line, `<li>${line.replace(/^\s*\d+\.\s+(.*?)$/gm, '$1')}</li>`);
90
- }
91
- }
92
- });
93
-
94
-
95
- result = result.replace(/^\s*-\s+(.*?)$/gm, (match, text) => {
96
- return `<li class="markdown_list_item">${text}</li>`;
97
- });
98
- result = result.replace(/^\s*---\s*$/gm, '<hr class="markdown_horizontal" />');
99
-
100
- // Convert blockquotes (e.g., > Quote => <blockquote>Quote</blockquote>)
101
- result = result.replace(/^\s*> (.*)$/gm, (match, text) => {
102
- return `<blockquote class="markdown_blockquote">${text}</blockquote>`;
103
- });
104
-
105
- // Convert tables (e.g., | Header | Cell | => <table><thead><tr><th>Header</th><th>Cell</th></tr></thead></table>)
106
- result = result.replace(/((?: *\|.*?)+)\n((?: *\|.*?)+)/gm, (match, header, cell) => {
107
- const headerCells = header.split('|').slice(1, -1);
108
- const cells = cell.split('|').slice(1, -1);
109
- let table = '<table class="markdown_table">';
110
- table += '<thead class="markdown_table_head"><tr class="markdown_table_row">';
111
- headerCells.forEach((headerCell) => {
112
- table += `<th class="markdown_table_header_cell">${headerCell}</th>`;
113
- });
114
- table += '</tr></thead><tbody class="markdown_table_body"><tr class="markdown_table_row">';
115
- cells.forEach((cell) => {
116
- table += `<td class="markdown_table_body_cell">${cell}</td>`;
117
- });
118
- table += '</tr></tbody></table>';
119
- return table;
120
- });
121
-
122
-
123
-
124
-
125
- if(!result.includes('<body>')){
126
- throw new Error(`Vader Error: You must enclose your html in a body tag for all components. \n\n${result}`)
127
- }
128
- /**
129
- * @type {string[]}
130
- * @description - grabbing all className attributes and replace them with class
131
- */
132
- // @ts-ignore
133
- result = result.replace(/classname/g,'class')
134
- /**
135
- * @type {string[]}
136
- * @description - grabbing all image tags and replace the src attribute with the absolute path
137
- */
138
- // @ts-ignore
139
- let images = result.match(/<img([^>]*)>/g)
140
- if(images){
141
- for(let i = 0; i < images.length; i++){
142
- let image = images[i]
143
- let src = image.match(/src="([^"]*)"/)
144
- let alt = image.match(/alt="([^"]*)"/)
145
- if(src){
146
- if(!src[1].includes('http') && !result.includes('<!-- #vader-disable_relative-paths -->')){
147
- // @ts-ignore
148
- result = result.replace(src[1],`${l}/${src[1]}`)
149
- } else if(!src[1].includes('http') && src[1].startsWith('.') && !result.includes('<!-- #vader-disable_relative-paths -->')){
150
- throw new Error(`Vader Error: You cannot use absolute paths since relative paths are not disabled in ${e.data.file}. Use relative paths instead. \n\n${src[1]}`)
151
- }
152
- }
153
- if(!alt && !result.includes('<!-- #vader-disable_accessibility -->')){
154
- throw new Error(`Vader Error: You must include an alt attribute in the image tag \n\n${image} of class ${e.data.name}. `)
155
- }
156
-
157
- // @ts-ignore
158
- if(!caches.match(`${l}/${src[1]}`)){
159
- caches.open('vader').then((cache)=>{
160
- // @ts-ignore
161
- cache.add(`${l}/${src[1]}`)
162
- // @ts-ignore
163
- console.log('cached', `${l}/${src[1]}`)
164
- }).catch((err)=>{
165
- console.log(err)
166
- })
167
- }else{
168
- // @ts-ignore
169
- console.log('already cached', caches.match(`${l}/${src[1]}`))
170
- }
171
- }
172
- }
173
-
174
- let href = result.match(/href="([^"]*)"/g)
175
- if(href){
176
- while(href.length){
177
- let h = href.pop()
178
- // @ts-ignore
179
- h = h.replace('href="','').replace('"','')
180
- if(!h.includes('http') && !result.includes('<!-- #vader-disable_relative-paths -->')){
181
- result = result.replace(`href="${h}"`,`href="#${h}"`)
182
- }else if(!h.includes('http') && h.startsWith('.') && !result.includes('<!-- #vader-disable_relative-paths -->')){
183
- throw new Error(`Vader Error: You cannot use absolute paths since relative paths are not disabled in ${e.data.file}. Use relative paths instead. \n\n${h}`)
184
- }
185
- }
186
- }
187
-
188
- let time_ended = Date.now()
189
- let time_taken = time_ended - time_started
190
- let hasran = false
191
- if(l.includes('localhost') || l.includes('127.0.0.1') && !hasran){
192
- hasran = true
193
- result+= `\$\{console.log('%c${e.data.name} component rendered in ${time_taken}ms','color:#fff;background:#000;padding:5px;border-radius:5px;font-size:12px;font-weight:bold'),""\}`
194
- }
195
-
196
-
197
- const d = result.split('<script>')
198
-
199
- if (d) {
200
- d.forEach((scriptTag, index) => {
201
- if (index === 0) {
202
- result = scriptTag;
203
- } else {
204
- if(scriptTag.includes('// <![CDATA[ <-- For SVG support')){
205
- return
206
- }
207
- let script = scriptTag.split('</script>')[0];
208
- js += script;
209
- }
210
- });
211
- }
212
-
213
- let jstemplates = result.match(/(\$\(.*?\))/gs)
214
- if(jstemplates){
215
- while(jstemplates.length){
216
- let jstemplate = jstemplates.pop()
217
- // @ts-ignore
218
- result = result.replace(jstemplate,`$\{${jstemplate.replace('$(','').replace(')','')}\}`)
219
- }
220
- }
221
- postMessage({
222
- template: `<div data-component=${e.data.name}>${result}</div>`,
223
- js: js ? js : ''
224
- })
225
-
226
- }