diff --git a/.gitignore b/.gitignore index 057102d..0dfee80 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# Bundle +dist + # Logs logs *.log @@ -44,5 +47,6 @@ jspm_packages # package lock package-lock.json +yarn.lock -.idea \ No newline at end of file +.idea diff --git a/hiddie.js b/hiddie.js index f3ad316..72a5ca0 100644 --- a/hiddie.js +++ b/hiddie.js @@ -1,11 +1,11 @@ 'use strict' -const reusify = require('reusify') -const { pathToRegexp } = require('path-to-regexp') +import reusify from 'reusify' +import { pathToRegexp } from 'path-to-regexp' -function hiddie (complete) { - var middlewares = [] - var pool = reusify(Holder) +const Hiddie = complete => { + const middlewares = [] + const pool = reusify(Holder) return { use, @@ -18,8 +18,8 @@ function hiddie (complete) { url = null } - var regexp - var keys = [] + let regexp + const keys = [] if (url) { regexp = pathToRegexp(sanitizePrefixUrl(url), keys, { end: false, @@ -28,7 +28,7 @@ function hiddie (complete) { } if (Array.isArray(f)) { - for (var val of f) { + for (const val of f) { middlewares.push({ regexp, keys, @@ -54,7 +54,7 @@ function hiddie (complete) { req.originalUrl = req.url - var holder = pool.get() + const holder = pool.get() holder.req = req holder.res = res holder.url = sanitizeUrl(req.url) @@ -70,13 +70,13 @@ function hiddie (complete) { this.context = null this.i = 0 - var that = this + const that = this this.done = function (err) { - var req = that.req - var res = that.res - var url = that.url - var context = that.context - var i = that.i++ + const req = that.req + const res = that.res + const url = that.url + const context = that.context + const i = that.i++ req.url = req.originalUrl @@ -97,18 +97,18 @@ function hiddie (complete) { that.i = 0 pool.release(that) } else { - var middleware = middlewares[i] - var fn = middleware.fn - var regexp = middleware.regexp - var keys = middleware.keys + const middleware = middlewares[i] + const fn = middleware.fn + const regexp = middleware.regexp + const keys = middleware.keys if (regexp) { - var result = regexp.exec(url) + const result = regexp.exec(url) if (result) { - var params = {} + const params = {} - for (var j = 1; j < result.length; j++) { - var prop = keys[j - 1].name - var val = decodeParam(result[j]) + for (let j = 1; j < result.length; j++) { + const prop = keys[j - 1].name + const val = decodeParam(result[j]) if (!!val || !Object.prototype.hasOwnProperty.call(params, prop)) { params[prop] = val @@ -125,14 +125,16 @@ function hiddie (complete) { that.done() } } else { - fn(req, res, that.done) + try { + fn(req, res, that.done) + } catch (_) {} } } } } } -function decodeParam (val) { +const decodeParam = (val) => { if (typeof val !== 'string' || val.length === 0) { return val } @@ -149,9 +151,9 @@ function decodeParam (val) { } } -function sanitizeUrl (url) { - for (var i = 0, len = url.length; i < len; i++) { - var charCode = url.charCodeAt(i) +const sanitizeUrl = (url) => { + for (let i = 0, len = url.length; i < len; i++) { + const charCode = url.charCodeAt(i) if (charCode === 63 || charCode === 35) { return url.slice(0, i) } @@ -159,11 +161,11 @@ function sanitizeUrl (url) { return url } -function sanitizePrefixUrl (url) { +const sanitizePrefixUrl = (url) => { if (url === '') return url if (url === '/') return '' if (url[url.length - 1] === '/') return url.slice(0, -1) return url } -module.exports = hiddie +export default Hiddie diff --git a/package.json b/package.json index 8cd1b96..e6ede12 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "main": "hiddie.js", "scripts": { "test": "standard && tap test.js", - "coverage": "tap --cov --coverage-report=html test.js" + "coverage": "tap --cov --coverage-report=html test.js", + "build": "webpack" }, "license": "MIT", "repository": { @@ -13,10 +14,14 @@ "url": "https://github.com/hepsiburada/hiddie" }, "devDependencies": { + "@babel/core": "^7.19.1", + "babel-loader": "^8.2.5", "pre-commit": "^1.2.2", "serve-static": "^1.12.4", "standard": "^14.0.2", - "tap": "^12.6.5" + "tap": "^12.6.5", + "webpack": "^5.74.0", + "webpack-cli": "^4.10.0" }, "dependencies": { "path-to-regexp": "^4.0.0", diff --git a/test.js b/test.js index f17896d..f3214bb 100644 --- a/test.js +++ b/test.js @@ -1,6 +1,6 @@ 'use strict' -const hiddie = require('./hiddie') +const hiddie = require('./hiddie').default const t = require('tap') const http = require('http') const serveStatic = require('serve-static') diff --git a/webpack.config.js b/webpack.config.js new file mode 100644 index 0000000..c3c6e2c --- /dev/null +++ b/webpack.config.js @@ -0,0 +1,22 @@ +const path = require('path') + +module.exports = { + entry: './hiddie.js', + mode: 'production', + output: { + filename: 'index.js', + path: path.resolve(__dirname, 'dist'), + libraryExport: 'default', + libraryTarget: 'umd', + globalObject: 'this' + }, + module: { + rules: [ + { + test: /.js$/, + loader: 'babel-loader', + exclude: /node_modules/ + } + ] + } +}