MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

cleanup code in radix3

+38 -40
+35 -37
examples/server/radix3.js
··· 1 1 class Radix3Node { 2 - constructor() { 2 + constructor(method) { 3 + this.method = method; 3 4 this.prefix = ''; 4 5 this.handler = undefined; 5 6 this.children = []; ··· 11 12 12 13 export class Radix3 { 13 14 constructor() { 14 - this.root = new Radix3Node(); 15 + this.methods = {}; 15 16 } 16 17 17 18 longestCommonPrefix(a, b) { 18 19 const minLen = a.length < b.length ? a.length : b.length; 19 20 for (let i = 0; i < minLen; i = i + 1) { 20 - if (a[i] !== b[i]) { 21 - return i; 22 - } 21 + if (a[i] !== b[i]) return i; 23 22 } 24 23 return minLen; 25 24 } 26 25 27 26 insert(path, handler, method = 'GET') { 28 - this.insertPath(this.root, `${method}#${path}`, handler, 0); 27 + if (this.methods[method] === undefined) { 28 + this.methods[method] = new Radix3Node(method); 29 + } 30 + this.insertPath(this.methods[method], path, handler, 0); 29 31 } 30 32 31 33 get(path, handler) { ··· 67 69 if (char === ':') { 68 70 let end = start + 1; 69 71 for (let i = end; i < path.length; i = i + 1) { 70 - if (path[i] === '/') { 71 - break; 72 - } 72 + if (path[i] === '/') break; 73 73 end = i + 1; 74 74 } 75 75 ··· 98 98 99 99 let end = start; 100 100 for (let i = start; i < path.length; i = i + 1) { 101 - if (path[i] === ':' || path[i] === '*') { 102 - break; 103 - } 101 + if (path[i] === ':' || path[i] === '*') break; 104 102 end = i + 1; 105 103 } 106 104 ··· 142 140 this.insertPath(newChild, path, handler, end); 143 141 } 144 142 145 - // if the method is wrong return null instead of undefined 146 143 lookup(path, method = 'GET') { 144 + const methodRoot = this.methods[method]; 145 + if (methodRoot === undefined) return undefined; 146 + 147 147 const params = {}; 148 - const fullPath = `${method}#${path}`; 149 - const handler = this.matchPath(this.root, fullPath, 0, params); 148 + const handler = this.matchPath(methodRoot, path, 0, params); 150 149 151 150 if (!handler) return undefined; 152 - return { handler, params }; 151 + return { handler: handler, params: params }; 153 152 } 154 153 155 154 matchPath(node, path, depth, params) { ··· 172 171 173 172 if (matches) { 174 173 const result = this.matchPath(child, path, depth + child.prefix.length, params); 175 - if (result !== undefined) { 176 - return result; 177 - } 174 + if (result !== undefined) return result; 178 175 } 179 176 } 180 177 } ··· 184 181 let offset = depth; 185 182 186 183 for (let i = depth; i < path.length; i = i + 1) { 187 - if (path[i] === '/') { 188 - break; 189 - } 184 + if (path[i] === '/') break; 190 185 paramValue = paramValue + path[i]; 191 186 offset = i + 1; 192 187 } ··· 194 189 if (paramValue !== '') { 195 190 params[node.paramChild.paramName] = paramValue; 196 191 const result = this.matchPath(node.paramChild, path, offset, params); 197 - if (result !== undefined) { 198 - return result; 199 - } 192 + if (result !== undefined) return result; 200 193 delete params[node.paramChild.paramName]; 201 194 } 202 195 } ··· 211 204 } 212 205 213 206 printTree() { 214 - this.printNode(this.root, '', true); 207 + const methods = Object.keys(this.methods); 208 + for (let i = 0; i < methods.length; i = i + 1) { 209 + const method = methods[i]; 210 + console.log('[' + method + ']'); 211 + this.printNode(this.methods[method], '', true); 212 + if (i < methods.length - 1) console.log(''); 213 + } 215 214 } 216 215 217 216 printNode(node, prefix, isLast) { 218 217 const marker = isLast ? 'โ””โ”€ ' : 'โ”œโ”€ '; 219 - let line = `${prefix}${marker}`; 218 + let line = prefix + marker; 220 219 221 220 if (node.prefix !== '') { 222 - line = `${line}"${node.prefix}"`; 221 + line = line + '"' + node.prefix + '"'; 223 222 } else { 224 - line = `${line}(root)`; 223 + line = line + '(root)'; 225 224 } 226 225 227 226 if (node.handler !== undefined) { 228 - line = `${line} [HANDLER]`; 227 + line = line + ' [HANDLER]'; 229 228 } 230 229 231 230 if (node.paramName !== undefined) { 232 - line = `${line} :${node.paramName}`; 231 + line = line + ' :' + node.paramName; 233 232 } 234 233 235 234 console.log(line); 236 - 237 - const childPrefix = `${prefix}${isLast ? ' ' : 'โ”‚ '}`; 235 + const childPrefix = prefix + (isLast ? ' ' : 'โ”‚ '); 238 236 239 237 for (let i = 0; i < node.children.length; i = i + 1) { 240 238 const isLastChild = i === node.children.length - 1 && node.paramChild === undefined && node.wildcardChild === undefined; ··· 243 241 244 242 if (node.paramChild !== undefined) { 245 243 const isLastChild = node.wildcardChild === undefined; 246 - console.log(`${childPrefix}${isLastChild ? 'โ””โ”€ ' : 'โ”œโ”€ '}:${node.paramChild.paramName}`); 247 - this.printNode(node.paramChild, `${childPrefix}${isLastChild ? ' ' : 'โ”‚ '}`, true); 244 + console.log(childPrefix + (isLastChild ? 'โ””โ”€ ' : 'โ”œโ”€ ') + ':' + node.paramChild.paramName); 245 + this.printNode(node.paramChild, childPrefix + (isLastChild ? ' ' : 'โ”‚ '), true); 248 246 } 249 247 250 248 if (node.wildcardChild !== undefined) { 251 - console.log(`${childPrefix}โ””โ”€ *${node.wildcardChild.paramName}`); 252 - this.printNode(node.wildcardChild, `${childPrefix} `, true); 249 + console.log(childPrefix + 'โ””โ”€ *' + node.wildcardChild.paramName); 250 + this.printNode(node.wildcardChild, childPrefix + ' ', true); 253 251 } 254 252 } 255 253 }
+2 -2
examples/server/server.js
··· 27 27 return c.res.body(`server is responding with ${result}`); 28 28 }); 29 29 30 - router.get('/users/:id', async c => { 30 + router.post('/users/:id', async c => { 31 31 return c.res.body(`User ID: ${c.params.id}`); 32 32 }); 33 33 ··· 58 58 59 59 async function handleRequest(req, res) { 60 60 console.log('request:', req.method, req.uri); 61 - const result = router.lookup(req.uri, 'GET'); 61 + const result = router.lookup(req.uri, req.method); 62 62 63 63 if (result?.handler) { 64 64 const ctx = { req, res, params: result.params };
+1 -1
meson.build
··· 67 67 build_date = run_command('date', '+%Y-%m-%d', check: true).stdout().strip() 68 68 69 69 version_conf = configuration_data() 70 - version_conf.set('ANT_VERSION', '0.0.6.38') 70 + version_conf.set('ANT_VERSION', '0.0.6.39') 71 71 version_conf.set('ANT_GIT_HASH', git_hash) 72 72 version_conf.set('ANT_BUILD_DATE', build_date) 73 73