nwjs / nw.js

Call all Node.js modules directly from DOM/WebWorker and enable a new way of writing applications with all Web technologies.
https://nwjs.io
MIT License
40.3k stars 3.88k forks source link

RegExp variable seems changed its type after passing to another function #8206

Open xueqino1 opened 4 weeks ago

xueqino1 commented 4 weeks ago

as the following codes show, I pass the variable /^\/test1(.*)?$/ to function unless,when I run those code in nodejs(v16.9.1) , it works fine .when I access /login and /test1 , the console doesn't log 'customMiddleware called'. which means variable vv matchs the string '/test1' while '/login' matchs the string '/login'.

then I put those code in a html and run it in nwjs , when I access /login, it works fun, the console doesn't log anything. but when I access /test1, the console output 'customMiddleware called', which means vv didn't match the string '/test1'. I add some console code in function unless, it showed that (vv instanceof RexExp) is false and typeof vv is 'object'.

I run it in nwjs v0.70.1 and v0.90.0 , both not work.

const customMiddleware = function (req, res, next) { console.log('customMiddleware called'); next(); }; customMiddleware.unless=express_unless_1.unless;

const vv = /^\/test1(.*)?$/ console.log('vv is regexp:',vv instanceof RegExp) app.use(customMiddleware.unless({ path: ["/login",vv]
}))

app.get('/test',(req,res)=>{ res.send('test ') })

app.get('/login',(req,res)=>{ res.send('login ') }) app.listen(3000,()=>{ console.log('app listen 3000') }) `

ayushmanchhabra commented 2 weeks ago

Trying to understand the issue.. on first glance this looks more of a code issue than a NW.js API issue. From what I understand, vv should be a RegExp but it changes to Object type?

xueqino1 commented 2 weeks ago

yes ,you're right. vv is a RegExp, then it is passed to function unless as options {path: [vv]}. the function unless looks like function unless(opts) { const paths = toArray(opts.path); paths.some(function (p) { console.log(p,"---------------",typeof p,"---------------", p instanceof RegExp) } }

when running in node, "p instanceof RegExp" outputs true . but when running in nwjs, it outputs false.

I run into this issue when I browse the '/test1', it's expected to match the vv and ignore the middleware, but it works fine in node and doesn't work in nwjs.