Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bundle flow implemented & first load exception fixed #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Bundle
dist

# Logs
logs
*.log
Expand Down Expand Up @@ -44,5 +47,6 @@ jspm_packages

# package lock
package-lock.json
yarn.lock

.idea
.idea
64 changes: 33 additions & 31 deletions hiddie.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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)
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -149,21 +151,21 @@ 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)
}
}
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
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,23 @@
"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": {
"type": "git",
"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",
Expand Down
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
22 changes: 22 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -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/
}
]
}
}