@radatek/microserver 3.0.4 → 3.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/microserver.d.ts +1 -1
- package/microserver.js +25 -22
- package/package.json +1 -1
package/microserver.d.ts
CHANGED
package/microserver.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* MicroServer
|
|
3
|
-
* @version 3.0.
|
|
3
|
+
* @version 3.0.5
|
|
4
4
|
* @package @radatek/microserver
|
|
5
5
|
* @copyright Darius Kisonas 2022
|
|
6
6
|
* @license MIT
|
|
@@ -695,7 +695,9 @@ export class MicroServer extends EventEmitter {
|
|
|
695
695
|
if (routes)
|
|
696
696
|
await this.use(routes);
|
|
697
697
|
}
|
|
698
|
-
if (plugin.
|
|
698
|
+
if (plugin.name) {
|
|
699
|
+
if (this._plugins[plugin.name])
|
|
700
|
+
throw new Error(`Plugin ${plugin.name} already added`);
|
|
699
701
|
this._plugins[plugin.name] = plugin;
|
|
700
702
|
this.emit('plugin', plugin.name);
|
|
701
703
|
this.emit('plugin:' + plugin.name);
|
|
@@ -1672,9 +1674,6 @@ export class StaticFilesPlugin extends Plugin {
|
|
|
1672
1674
|
options = {};
|
|
1673
1675
|
if (typeof options === 'string')
|
|
1674
1676
|
options = { root: options };
|
|
1675
|
-
// allow multiple instances
|
|
1676
|
-
if (server && !server.getPlugin('static'))
|
|
1677
|
-
this.name = 'static';
|
|
1678
1677
|
this.mimeTypes = options.mimeTypes ? { ...StaticFilesPlugin.mimeTypes, ...options.mimeTypes } : Object.freeze(StaticFilesPlugin.mimeTypes);
|
|
1679
1678
|
this.root = (options.root && path.isAbsolute(options.root) ? options.root : path.resolve(options.root || options?.path || 'public')).replace(/[\/\\]$/, '') + path.sep;
|
|
1680
1679
|
this.ignore = (options.ignore || []).map((p) => path.normalize(path.join(this.root, p)) + path.sep);
|
|
@@ -1686,24 +1685,28 @@ export class StaticFilesPlugin extends Plugin {
|
|
|
1686
1685
|
this.errors = options.errors;
|
|
1687
1686
|
this.prefix = ('/' + (options.path?.replace(/^[.\/]*/, '') || '').replace(/\/$/, '')).replace(/\/$/, '');
|
|
1688
1687
|
const defSend = ServerResponse.prototype.send;
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1688
|
+
if (server && !server.getPlugin('static')) {
|
|
1689
|
+
this.name = 'static'; // only first plugin instance is registered as
|
|
1690
|
+
const defSend = ServerResponse.prototype.send;
|
|
1691
|
+
ServerResponse.prototype.send = function (data) {
|
|
1692
|
+
const plugin = this.req.server.getPlugin('static');
|
|
1693
|
+
if (this.statusCode < 400 || this.isJson || typeof data !== 'string' || !plugin?.errors || this.getHeader('Content-Type'))
|
|
1694
|
+
return defSend.call(this, data);
|
|
1695
|
+
const errFile = plugin.errors[this.statusCode] || plugin.errors['*'];
|
|
1696
|
+
if (errFile)
|
|
1697
|
+
plugin.serveFile(this.req, this, { path: errFile, mimeType: 'text/html' });
|
|
1692
1698
|
return defSend.call(this, data);
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
plugin
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
mimeType: StaticFilesPlugin.mimeTypes[extname(path)] || 'application/octet-stream'
|
|
1705
|
-
});
|
|
1706
|
-
};
|
|
1699
|
+
};
|
|
1700
|
+
ServerResponse.prototype.file = function (path) {
|
|
1701
|
+
const plugin = this.req.server.getPlugin('static');
|
|
1702
|
+
if (!plugin)
|
|
1703
|
+
throw new Error('Server error');
|
|
1704
|
+
plugin.serveFile(this.req, this, typeof path === 'object' ? path : {
|
|
1705
|
+
path,
|
|
1706
|
+
mimeType: StaticFilesPlugin.mimeTypes[extname(path)] || 'application/octet-stream'
|
|
1707
|
+
});
|
|
1708
|
+
};
|
|
1709
|
+
}
|
|
1707
1710
|
}
|
|
1708
1711
|
/** Default static files handler */
|
|
1709
1712
|
handler(req, res, next) {
|