nwjs/nw.js

RegExp variable seems changed its type after passing to another function

Opened this issue · 2 comments

  • Bug Report

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.

  • Successfully reproduced against the latest version of NW.js?
    YES

  • Operating System:windows 10

  • NW.js Version:nwjs-sdk-v0.70.1-win-x64 and nwjs-sdk-v0.90.0-win-x64

  • Code snippet:
    `
    const express=require('express')
    const express_unless_1 = require("express-unless");
    const app=express()

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')
})
`

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?

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.