Skip to content

Commit

Permalink
StaticFiles refactoring, Response method notFound
Browse files Browse the repository at this point in the history
  • Loading branch information
piliugin-anton committed Jul 18, 2022
1 parent b616413 commit e8ec066
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "uquik",
"version": "1.0.47",
"version": "1.0.48",
"description": "uQuik HTTP(S) framework",
"main": "index.js",
"scripts": {
Expand Down
8 changes: 8 additions & 0 deletions src/Response.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,14 @@ class Response extends Writable {
throw new Error('Response.throw() expects an instance of an Error.')
}

/**
* This method calls on_not_found handler
*
*/
notFound () {
this.appHandlers.get('on_not_found')(this.wrapped_request, this)
}

/* Response Getters */

/**
Expand Down
11 changes: 5 additions & 6 deletions src/StaticFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ const StaticFiles = (options = {}) => {
end
})

dataTransfer.readable.once('end', () => !dataTransfer.readable.destroyed && dataTransfer.readable.destroy())

res
.header('Content-Type', mimeType)
.header('Last-Modified', timeUTC)
Expand All @@ -129,14 +127,15 @@ const StaticFiles = (options = {}) => {

dataTransfer.transform = zlib.createGzip()

dataTransfer.transform.once('end', () => !dataTransfer.transform.destroyed && dataTransfer.transform.destroy())

pipeline(dataTransfer.readable, dataTransfer.transform, res, () => destroy(dataTransfer))
pipeline(dataTransfer.readable, dataTransfer.transform, res, (ex) => {
destroy(dataTransfer)
if (ex) res.throw(ex)
})
} else {
res.stream(dataTransfer.readable, size)
}
} catch (ex) {
if (ex.status && ex.status === 404) return res.master_context.handlers.get('on_not_found')(req, res)
if (ex.status && ex.status === 404) return res.notFound()
res.throw(ex)
}
}
Expand Down

0 comments on commit e8ec066

Please sign in to comment.