codehooks-js 1.2.8 → 1.2.10

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/index.js CHANGED
@@ -86,8 +86,12 @@ class Codehooks {
86
86
  this.jobs[cronExpression] = hook;
87
87
  }
88
88
 
89
- static = (options) => {
90
- return ws(options, Codehooks.getInstance(), _filestore)
89
+ static = (options, hook) => {
90
+ return ws(options, Codehooks.getInstance(), _filestore, hook, {source: true})
91
+ }
92
+
93
+ storage = (options, hook) => {
94
+ return ws(options, Codehooks.getInstance(), _filestore, hook)
91
95
  }
92
96
 
93
97
  set = (key, val) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codehooks-js",
3
- "version": "1.2.8",
3
+ "version": "1.2.10",
4
4
  "type": "module",
5
5
  "description": "Codehooks.io official library - provides express.JS like syntax",
6
6
  "main": "index.js",
package/types/index.d.ts CHANGED
@@ -498,6 +498,19 @@ export type httpResponse = {
498
498
  * - e.g. res.headers({'Content-Type': 'text/html; charset=UTF-8','X-myheader': '123456890'});
499
499
  */
500
500
  headers: (headers: object) => any;
501
+ /**
502
+ * Set a response header value
503
+ * @param header string
504
+ * @param value string
505
+ * @returns
506
+ */
507
+ setHeader: (header: string, value: string) => any;
508
+ /**
509
+ * Remove any default headers from a response
510
+ * @param header string
511
+ * @returns
512
+ */
513
+ removeHeader: (header: string) => any;
501
514
  /**
502
515
  * - Return a HTTP response status code for a client request,
503
516
  * - e.g. res.status(401);
@@ -826,11 +839,27 @@ declare class Codehooks {
826
839
  ): void;
827
840
 
828
841
  /**
829
- * Serve static file content
842
+ * Serve static file content from a source code diretory
830
843
  * @param {Object} options - {route: "/", default: "index.html", directory: "/static"}
831
844
  * @returns void
832
845
  */
833
- static: (options: any) => void;
846
+ static: (options: any,
847
+ ...hook: ((
848
+ request: httpRequest,
849
+ response: httpResponse,
850
+ next: nextFunction
851
+ ) => any)[]) => void;
852
+ /**
853
+ * Serve file content from a blob storage diretory
854
+ * @param {Object} options - {route: "/documents", directory: "/docs"}
855
+ * @returns void
856
+ */
857
+ storage: (options: any,
858
+ ...hook: ((
859
+ request: httpRequest,
860
+ response: httpResponse,
861
+ next: nextFunction
862
+ ) => any)[]) => void;
834
863
  /**
835
864
  * Configuration settings
836
865
  * @param {string} key
package/webserver.mjs CHANGED
@@ -16,7 +16,7 @@ function promisify(cb, fn) {
16
16
 
17
17
  const defaultOptions = { route: '/public', default: "index.html", directory: "/" };
18
18
 
19
- export function serveStatic(options, app, filestore) {
19
+ export function serveStatic(options, app, filestore, hook, fsoptions) {
20
20
  if (!options) {
21
21
  options = defaultOptions;
22
22
  }
@@ -42,7 +42,7 @@ export function serveStatic(options, app, filestore) {
42
42
  }
43
43
  })
44
44
 
45
- app.get(regex, async (req, res) => {
45
+ const readFunc = async (req, res) => {
46
46
  let filePath = decodeURIComponent(req.apiPath);
47
47
  try {
48
48
  // serve root '' or '/'
@@ -65,7 +65,7 @@ export function serveStatic(options, app, filestore) {
65
65
  // get mime type
66
66
  const type = mime.getType(filePath)
67
67
  console.debug('Serve file', filePath, type)
68
- const filestream = await filestore.getReadStream(filePath);
68
+ const filestream = await filestore.getReadStream(filePath, fsoptions);
69
69
  res.set('Content-Type', type || 'application/octet-stream');
70
70
  if (options.headers) {
71
71
  res.headers(options.headers);
@@ -90,8 +90,12 @@ export function serveStatic(options, app, filestore) {
90
90
  console.error(error)
91
91
  res.status(404).end('No file here:' + filePath)
92
92
  }
93
- })
94
-
93
+ }
94
+ const farr = [readFunc]
95
+ if (typeof hook === 'function'){
96
+ farr.unshift(hook)
97
+ }
98
+ app.get(regex, ...farr)
95
99
  }
96
100
 
97
101
  /* template engine support */
@@ -129,7 +133,7 @@ async function ejs(viewFile, data, settings, cb) {
129
133
  if (tmplCache[viewFile]) {
130
134
  tmpl = tmplCache[viewFile];
131
135
  } else {
132
- const txtfile = await _coho_fs.readFile(`${settings.views}/${viewFile}.ejs`);
136
+ const txtfile = await _coho_fs.readFile(`${settings.views}/${viewFile}.ejs`, {source: true});
133
137
  //console.log('Txtfile', txtfile)
134
138
  tmplCache[viewFile] = engine.compile(txtfile);
135
139
  tmpl = tmplCache[viewFile];
@@ -157,7 +161,7 @@ async function handlebars(viewFile, data, settings, cb) {
157
161
  //console.log('Layout file', layoutFile)
158
162
  engine.registerPartial('layout', layoutFile);
159
163
  }
160
- const txtfile = await _coho_fs.readFile(`${settings.views}/${viewFile}.hbs`);
164
+ const txtfile = await _coho_fs.readFile(`${settings.views}/${viewFile}.hbs`, {source: true});
161
165
  //console.log('Txtfile', txtfile)
162
166
  tmplCache[viewFile] = engine.compile(txtfile);
163
167
  tmpl = tmplCache[viewFile];