codehooks-js 1.2.14 → 1.2.15
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 +2 -55
- package/package.json +1 -3
- package/types/index.d.ts +40 -5
- package/coderunner.mjs +0 -22
- package/cronjob.mjs +0 -36
package/index.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
import {agg} from './aggregation/index.mjs';
|
|
2
2
|
import {crudlify as crud} from './crudlify/index.mjs';
|
|
3
3
|
import {serveStatic as ws, render as renderView} from './webserver.mjs';
|
|
4
|
-
let cronjob = null;
|
|
5
|
-
let coderun = null;
|
|
6
|
-
|
|
7
4
|
|
|
8
5
|
function createRoute(str) {
|
|
9
6
|
if(str instanceof RegExp) {
|
|
@@ -127,7 +124,6 @@ class Codehooks {
|
|
|
127
124
|
jobhooks: this.jobs,
|
|
128
125
|
authhooks: this.auths,
|
|
129
126
|
globalhooks: this.appmiddleware,
|
|
130
|
-
useExpress: this.useExpress,
|
|
131
127
|
settings: this.settings,
|
|
132
128
|
realtime: this.realtime,
|
|
133
129
|
app: this
|
|
@@ -140,56 +136,6 @@ class Codehooks {
|
|
|
140
136
|
// alias
|
|
141
137
|
start = this.init;
|
|
142
138
|
|
|
143
|
-
useExpress = async (express, options) => {
|
|
144
|
-
cronjob = import("./cronjob.mjs")
|
|
145
|
-
coderun = import('./coderunner.mjs')
|
|
146
|
-
const { datastore, space = '/dev' } = options;
|
|
147
|
-
await datastore.connect();
|
|
148
|
-
this.setDatastore(datastore);
|
|
149
|
-
|
|
150
|
-
for (let key in this.auths) {
|
|
151
|
-
const func = this.auth[key];
|
|
152
|
-
console.error('Auth middleware not implemented:', key)
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
for (let key in this.appmiddleware) {
|
|
156
|
-
const func = this.appmiddleware[key];
|
|
157
|
-
if (func.path) {
|
|
158
|
-
express.use(`${space}${func.path}`, func.func);
|
|
159
|
-
} else {
|
|
160
|
-
express.use(func);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
for (let key in this.routes) {
|
|
165
|
-
const func = this.routes[key];
|
|
166
|
-
const [method, route] = key.split(' ');
|
|
167
|
-
switch (method) {
|
|
168
|
-
case 'GET': express.get(`${space}${route}`, ...func); break;
|
|
169
|
-
case 'PUT': express.put(`${space}${route}`, ...func); break;
|
|
170
|
-
case 'POST': express.post(`${space}${route}`, ...func); break;
|
|
171
|
-
case 'PATCH': express.patch(`${space}${route}`, ...func); break;
|
|
172
|
-
case 'DELETE': express.delete(`${space}${route}`, ...func); break;
|
|
173
|
-
case '*': express.all(`${space}${route}`, ...func); break;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// apply jobs
|
|
178
|
-
for (let key in this.jobs) {
|
|
179
|
-
const func = this.jobs[key];
|
|
180
|
-
const cron = key;
|
|
181
|
-
cronjob(cron, func);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
// apply queue workers
|
|
185
|
-
for (let key in this.queues) {
|
|
186
|
-
const func = this.queues[key];
|
|
187
|
-
const topic = key;
|
|
188
|
-
//console.debug('Apply queue', topic, func);
|
|
189
|
-
this.datastore.setQueue(topic, func);
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
|
|
193
139
|
setDatastore = (ds) => {
|
|
194
140
|
this.datastore = ds;
|
|
195
141
|
this.listeners.forEach(observer => observer(this))
|
|
@@ -205,9 +151,11 @@ class Codehooks {
|
|
|
205
151
|
}
|
|
206
152
|
}
|
|
207
153
|
}
|
|
154
|
+
|
|
208
155
|
addListener = (observer) => {
|
|
209
156
|
this.listeners.push(observer);
|
|
210
157
|
}
|
|
158
|
+
|
|
211
159
|
static getInstance() {
|
|
212
160
|
if (!this.instance) {
|
|
213
161
|
this.instance = new Codehooks();
|
|
@@ -239,7 +187,6 @@ export const aggregation = agg;
|
|
|
239
187
|
export const crudlify = crud;
|
|
240
188
|
export const coho = _coho;
|
|
241
189
|
export const app = _coho;
|
|
242
|
-
export const coderunner = coderun;
|
|
243
190
|
|
|
244
191
|
export const realtime = {
|
|
245
192
|
createChannel: (path, ...hook) => {
|
package/package.json
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "codehooks-js",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.15",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Codehooks.io official library - provides express.JS like syntax",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"types": "./types",
|
|
8
8
|
"files": [
|
|
9
9
|
"index.js",
|
|
10
|
-
"coderunner.mjs",
|
|
11
|
-
"cronjob.mjs",
|
|
12
10
|
"./aggregation/index.mjs",
|
|
13
11
|
"./webserver.mjs",
|
|
14
12
|
"./crudlify/index.mjs",
|
package/types/index.d.ts
CHANGED
|
@@ -476,26 +476,40 @@ export type httpResponse = {
|
|
|
476
476
|
end: (data: string | object | void) => void;
|
|
477
477
|
/**
|
|
478
478
|
* - Send text|JSON data to client and end request
|
|
479
|
+
* @example
|
|
480
|
+
* res.end()
|
|
481
|
+
* res.status(404).end()
|
|
479
482
|
*/
|
|
480
483
|
send: (data: string | object) => void;
|
|
481
484
|
/**
|
|
482
485
|
* - End request and send JSON data to client
|
|
486
|
+
* @example
|
|
487
|
+
* res.json({ user: 'tobi' })
|
|
488
|
+
* res.status(500).json({ error: 'message' })
|
|
483
489
|
*/
|
|
484
490
|
json: (document: object) => any;
|
|
485
491
|
/**
|
|
486
492
|
* - Write stream data to the client response.
|
|
487
493
|
* - Content-type must be set before any write operations
|
|
488
494
|
* - set optional type to 'buffer' for binary write operations
|
|
495
|
+
* @example
|
|
496
|
+
* res.set('content-type', 'text/plain')
|
|
497
|
+
* res.write('line one')
|
|
498
|
+
* res.write('\n')
|
|
499
|
+
* res.write('line two')
|
|
500
|
+
* res.end()
|
|
489
501
|
*/
|
|
490
502
|
write: (data: any, type?: string) => any;
|
|
491
503
|
/**
|
|
492
504
|
* - Set a response header value,
|
|
493
|
-
*
|
|
505
|
+
* @example
|
|
506
|
+
* res.set('Content-Type', 'text/html; charset=UTF-8');
|
|
494
507
|
*/
|
|
495
508
|
set: (header: string, value: string) => any;
|
|
496
509
|
/**
|
|
497
510
|
* - Set multiple response header values,
|
|
498
|
-
*
|
|
511
|
+
* @example
|
|
512
|
+
* res.headers({'Content-Type': 'text/html; charset=UTF-8','X-myheader': '123456890'});
|
|
499
513
|
*/
|
|
500
514
|
headers: (headers: object) => any;
|
|
501
515
|
/**
|
|
@@ -513,14 +527,26 @@ export type httpResponse = {
|
|
|
513
527
|
removeHeader: (header: string) => any;
|
|
514
528
|
/**
|
|
515
529
|
* - Return a HTTP response status code for a client request,
|
|
516
|
-
*
|
|
530
|
+
* @example
|
|
531
|
+
* res.status(401).end();
|
|
517
532
|
*/
|
|
518
533
|
status: (code: number) => any;
|
|
519
534
|
/**
|
|
520
535
|
* - Render template with object data
|
|
521
|
-
*
|
|
536
|
+
* @example
|
|
537
|
+
* res.render('services', {title: "Services page"})
|
|
522
538
|
*/
|
|
523
539
|
render: (template: string, context: object) => void;
|
|
540
|
+
/**
|
|
541
|
+
* - Sets the response header to redirect client request
|
|
542
|
+
* @param statusCode - optional, set to 302 by default
|
|
543
|
+
* @param url
|
|
544
|
+
* @returns
|
|
545
|
+
* @example
|
|
546
|
+
* res.redirect('/assets/login.html'); // default status 302, Temporary
|
|
547
|
+
* res.redirect(301, '/home/correct.html'); // status code 301, Permanently
|
|
548
|
+
*/
|
|
549
|
+
redirect: (statusCode: number | string, url?: string) => void;
|
|
524
550
|
};
|
|
525
551
|
|
|
526
552
|
export type nextFunction = (error?: string) => void;
|
|
@@ -840,7 +866,16 @@ declare class Codehooks {
|
|
|
840
866
|
|
|
841
867
|
/**
|
|
842
868
|
* Serve static file content from a source code diretory
|
|
843
|
-
* @param {Object} options -
|
|
869
|
+
* @param {Object} options -
|
|
870
|
+
* - options.route - API route to serve assets
|
|
871
|
+
* - options.directory - path to directory with assets/files
|
|
872
|
+
* - options.default - default file name if root is /
|
|
873
|
+
@param {...function(httpRequest, httpResponse, function(string):void)} hook - Optional middleware functions to be applied before resource is served
|
|
874
|
+
* @example
|
|
875
|
+
* app.static({route:'/static', directory: '/assets'}, (req, res, next) => {
|
|
876
|
+
* console.log("Serving a static resource", req.path);
|
|
877
|
+
* next();
|
|
878
|
+
* })
|
|
844
879
|
* @returns void
|
|
845
880
|
*/
|
|
846
881
|
static: (options: any,
|
package/coderunner.mjs
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
export default function execAll(funcarr, context, callfuncs, nextfunc) {
|
|
2
|
-
const runOne = (pos) => {
|
|
3
|
-
(async function () {
|
|
4
|
-
try {
|
|
5
|
-
if (funcarr[pos] !== null && funcarr[pos] !== undefined) {
|
|
6
|
-
await funcarr[pos](context, callfuncs, (nextparam) => {
|
|
7
|
-
if (nextparam && nextfunc) {
|
|
8
|
-
return nextfunc(nextparam)
|
|
9
|
-
}
|
|
10
|
-
runOne(pos + 1);
|
|
11
|
-
});
|
|
12
|
-
} else {
|
|
13
|
-
if (nextfunc) nextfunc();
|
|
14
|
-
}
|
|
15
|
-
} catch (ex) {
|
|
16
|
-
console.error("Unhandled Codehook function exception:", ex || ex, funcarr, pos);
|
|
17
|
-
callfuncs.end("Unhandled Codehook exception:" + ex.message || ex);
|
|
18
|
-
}
|
|
19
|
-
})();
|
|
20
|
-
}
|
|
21
|
-
runOne(0);
|
|
22
|
-
}
|
package/cronjob.mjs
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import crontab from 'node-cron';
|
|
2
|
-
import cronparser from 'cron-parser';
|
|
3
|
-
import execAll from './coderunner.mjs';
|
|
4
|
-
const debug = console.debug;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export default function run(cronexpr, theFunc) {
|
|
8
|
-
//return console.log('Not implemented in standalone version')
|
|
9
|
-
try {
|
|
10
|
-
// verify cron expression
|
|
11
|
-
cronparser.parseExpression(cronexpr);
|
|
12
|
-
const newtimer = crontab.schedule(cronexpr, async () => {
|
|
13
|
-
const theContext = {
|
|
14
|
-
end: function (output) {
|
|
15
|
-
if (output) console.log(output);
|
|
16
|
-
return this;
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
|
-
debug('tick', cronexpr, theFunc);
|
|
20
|
-
try {
|
|
21
|
-
if (Array.isArray(theFunc)) {
|
|
22
|
-
await execAll(theFunc, {}, theContext);
|
|
23
|
-
} else {
|
|
24
|
-
await theFunc({}, theContext);
|
|
25
|
-
}
|
|
26
|
-
} catch (ex) {
|
|
27
|
-
console.log("Cron err", ex.message)
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
} catch (ex) {
|
|
31
|
-
console.error("Something is fishy with the cron expression", `'${cronexpr}'`)
|
|
32
|
-
if (argv.debug) console.error(ex.message)
|
|
33
|
-
debug(ex)
|
|
34
|
-
process.exit(1)
|
|
35
|
-
}
|
|
36
|
-
}
|