-
-
Notifications
You must be signed in to change notification settings - Fork 380
feat(middleware): add content
option (options.content.filter
, options.content.transform
)
#336
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ module.exports = function wrapper(context) { | |
return new Promise(((resolve) => { | ||
handleRequest(context, filename, processRequest, req); | ||
function processRequest() { | ||
let notFound; | ||
try { | ||
let stat = context.fs.statSync(filename); | ||
|
||
|
@@ -60,10 +61,25 @@ module.exports = function wrapper(context) { | |
throw new DevMiddlewareError('next'); | ||
} | ||
} | ||
notFound = false; | ||
} catch (e) { | ||
notFound = true; | ||
} | ||
const currentRequest = Object.assign({}, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
url: req.url, | ||
method: req.method, | ||
headers: req.headers, | ||
filename, | ||
notFound | ||
}); | ||
if (context.options && context.options.content && context.options.content.filter) { | ||
if (!context.options.content.filter(currentRequest)) { | ||
return goNext(); | ||
} | ||
} | ||
if (notFound) { | ||
return resolve(goNext()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question: why is it that every other "goNext" is a regular return but this one is a return resolve(goNext())? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question but I don't know the answer, try to remove the |
||
} | ||
|
||
// server content | ||
let content = context.fs.readFileSync(filename); | ||
content = handleRangeHeaders(content, req, res); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -152,6 +152,42 @@ describe('Server', () => { | |
}); | ||
}); | ||
|
||
describe('filters', () => { | ||
before((done) => { | ||
app = express(); | ||
const compiler = webpack(webpackConfig); | ||
instance = middleware(compiler, { | ||
stats: 'errors-only', | ||
content: { | ||
filter(currentRequest) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
let result = true; | ||
if (/.js$/.test(currentRequest.filename)) { | ||
result = false; | ||
} | ||
return result; | ||
} | ||
}, | ||
logLevel, | ||
publicPath: '/public/' | ||
}); | ||
app.use(instance); | ||
listen = listenShorthand(done); | ||
}); | ||
after(close); | ||
|
||
it('GET request to bundle file with /.js$/ filtered', (done) => { | ||
request(app).get('/public/bundle.js') | ||
.expect(404, done); | ||
}); | ||
|
||
it('request to image with /.js$/ filtered', (done) => { | ||
request(app).get('/public/svg.svg') | ||
.expect('Content-Type', 'image/svg+xml; charset=UTF-8') | ||
.expect('Content-Length', '4778') | ||
.expect(200, done); | ||
}); | ||
}); | ||
|
||
describe('no index mode', () => { | ||
before((done) => { | ||
app = express(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please elaborate further on the need for this