corebasic 1.0.29 → 1.0.31

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
@@ -1,2 +1,15 @@
1
-
2
- module.exports = require('./libs/app');
1
+
2
+
3
+ import * as Elabase from './libs/elabase.js'
4
+ import * as Kafka from './libs/kafka.js'
5
+ import * as Utils from './libs/utils.js'
6
+ import * as Session from './libs/session.js'
7
+ import * as Auth from './libs/auth.js'
8
+
9
+ export {
10
+ Elabase,
11
+ Kafka,
12
+ Utils,
13
+ Session,
14
+ Auth
15
+ }
package/libs/ObjectId.js CHANGED
@@ -17,7 +17,7 @@ if (!document) var document = { cookie: '' }; // fix crashes on node
17
17
  * Javascript class that mimics how WCF serializes a object of type MongoDB.Bson.ObjectId
18
18
  * and converts between that format and the standard 24 character representation.
19
19
  */
20
- var ObjectId = (function () {
20
+ export var ObjectId = (function () {
21
21
  var increment = Math.floor(Math.random() * (16777216));
22
22
  var pid = Math.floor(Math.random() * (65536));
23
23
  var machine = Math.floor(Math.random() * (16777216));
@@ -121,7 +121,3 @@ ObjectId.prototype.toString = function () {
121
121
  '000000'.substr(0, 6 - increment.length) + increment;
122
122
  };
123
123
 
124
-
125
- module.exports = {
126
- ObjectId,
127
- }
package/libs/auth.js CHANGED
@@ -1,16 +1,16 @@
1
- const axios = require('axios');
2
- const otpGenerator = require('otp-generator')
1
+ import * as axios from 'axios'
2
+ import otpGenerator from 'otp-generator'
3
3
 
4
- const Elabase = require('./elabase')
5
- const Utils = require('./utils')
6
- const Session = require('./session')
4
+ import * as Elabase from './elabase.js'
5
+ import * as Utils from './utils.js'
6
+ import * as Session from './session.js'
7
7
 
8
8
  let validateFn, validateErrMessage
9
- module.exports.validate = (callback, errMessage) => {
9
+ export const validate = (callback, errMessage) => {
10
10
  validateFn = callback
11
11
  }
12
12
 
13
- module.exports.start = (app, successCallback) => {
13
+ export const start = (app, successCallback) => {
14
14
  app.post("/login", async (req, res) => {
15
15
  if (validateFn && !validateFn(req)) {
16
16
  res.status(401).json({ mode: 'login', success: false, info: validateErrMessage ?? 'Validation Failed', message: 'Login Server Error' })
package/libs/elabase.js CHANGED
@@ -1,14 +1,6 @@
1
- const axios = require('axios')
1
+ import * as axios from 'axios'
2
2
 
3
3
 
4
- module.exports = {
5
- start,
6
- insert,
7
- update,
8
- query,
9
- remove
10
- }
11
-
12
4
 
13
5
 
14
6
 
@@ -54,7 +46,7 @@ let internal = {
54
46
 
55
47
  let db = internal.db // default database
56
48
 
57
- function start(dbName) {
49
+ export function start(dbName) {
58
50
  if (!Utils.isEmpty(dbName) && Utils.isEmpty(db)) {
59
51
  internal.db = "data/" + dbName
60
52
  db = "data/" + dbName
@@ -63,7 +55,7 @@ function start(dbName) {
63
55
  }
64
56
 
65
57
 
66
- function insert(collection, value, _id) {
58
+ export const insert = async (collection, value, _id) => {
67
59
  var arg = {
68
60
  collection: collection,
69
61
  insert: value
@@ -79,7 +71,7 @@ function insert(collection, value, _id) {
79
71
  })
80
72
  }
81
73
 
82
- function query(collection, query, options) {
74
+ export const query = async (collection, query, options) => {
83
75
  var arg = {
84
76
  collection: collection,
85
77
  query: query,
@@ -107,7 +99,7 @@ function collectionArray(col) {
107
99
  }
108
100
 
109
101
 
110
- function update(collection, query, update, options) {
102
+ export const update = async (collection, query, update, options) => {
111
103
  var arg = {
112
104
  collection: collection,
113
105
  query: query,
@@ -122,7 +114,7 @@ function update(collection, query, update, options) {
122
114
  })
123
115
  }
124
116
 
125
- function remove(collection, query) {
117
+ export const remove = async (collection, query) => {
126
118
  var arg = {
127
119
  collection: collection,
128
120
  query: query,
package/libs/kafka.js CHANGED
@@ -1,11 +1,13 @@
1
- const { Kafka, logLevel } = require('kafkajs')
1
+ import { Kafka, logLevel as KafkaLogLevel } from 'kafkajs'
2
+
3
+ export const logLevel = KafkaLogLevel
2
4
 
3
5
  let kafka, producer, admin
4
6
 
5
7
  let appName
6
8
  let topicPrefix = ''
7
9
 
8
- function start(arg) {
10
+ export const start = (arg) => {
9
11
  arg = !arg && !process.env.APP_DEPLOYMENT_NAME ? { clientId: (global.app?.name ?? 'app') + '-dev', brokers: ['127.0.0.1:9092'], sasl: false, logLevel: logLevel.ERROR } : arg
10
12
  appName = arg?.clientId ?? (process.env.APP_DEPLOYMENT_NAME ?? (global.app?.name ?? 'app'))
11
13
  topicPrefix = `${appName}.`
@@ -24,7 +26,7 @@ function start(arg) {
24
26
  }
25
27
 
26
28
 
27
- async function createTopic(topic, partition, replicas) {
29
+ export const createTopic = async (topic, partition, replicas) => {
28
30
  topic = topicPrefix + topic
29
31
  return await admin.createTopics({ topics: [{topic, numPartitions: partition ?? 1, replicationFactor: replicas ?? 1}] })
30
32
  }
@@ -51,9 +53,9 @@ const start_consumer = async function (topic, groupId, callback) {
51
53
  // callback(topic, message.value.toString(), partition) // toString() returns array so won't parse if json.
52
54
  let success
53
55
  try {
54
- success = callback(topic, JSON.parse(message.value), partition)
56
+ success = await callback(topic, JSON.parse(message.value), partition)
55
57
  } catch (ex) {
56
- success = callback(topic, message.value, partition)
58
+ success = await callback(topic, message.value, partition)
57
59
  }
58
60
 
59
61
  await consumer.commitOffsets([{ topic, partition, offset: (Number(message.offset) + 1).toString() }]);
@@ -91,24 +93,17 @@ const start_producer = async function (topic, message, key) {
91
93
  // start_consumer('quickstart-events')
92
94
  // start_producer('quickstart-events', 'Hello KafkaJS user! Little')
93
95
 
94
- const receive = async function(topic, groupId, callback) {
96
+ export const receive = async function(topic, groupId, callback) {
95
97
  topic = topicPrefix + topic
96
98
  await start_consumer(topic, groupId, callback)
97
99
  }
98
100
 
99
101
 
100
- const send = async function(topic, message, key) {
102
+ export const send = async function(topic, message, key) {
101
103
  topic = topicPrefix + topic
102
104
  await start_producer(topic, message, key)
103
105
  }
104
106
 
105
- module.exports = {
106
- start,
107
- createTopic,
108
- send,
109
- receive,
110
- logLevel,
111
- }
112
107
 
113
108
 
114
109
 
package/libs/session.js CHANGED
@@ -1,10 +1,6 @@
1
- const jwt = require('jsonwebtoken');
1
+ import * as jwt from 'jsonwebtoken'
2
2
  let app
3
3
 
4
- module.exports = {
5
- start,
6
- generateAccessToken,
7
- }
8
4
 
9
5
 
10
6
 
@@ -13,7 +9,7 @@ const REFRESH_TOKEN_SECRET = process.env.JWT_REFRESH_TOKEN_SECRET || "MY_SECRET_
13
9
 
14
10
  let urlsAllowed = []
15
11
 
16
- function start(expressApp, allowedUrls) {
12
+ export const start = (expressApp, allowedUrls) => {
17
13
  urlsAllowed = ["/refreshToken", "/login"].concat(allowedUrls ?? [])
18
14
  app = expressApp
19
15
 
@@ -55,7 +51,7 @@ function start(expressApp, allowedUrls) {
55
51
  }
56
52
 
57
53
 
58
- function generateAccessToken(userId, clientId) {
54
+ export const generateAccessToken = (userId, clientId) => {
59
55
  let data = { userId, clientId }
60
56
 
61
57
  const accessToken = jwt.sign(data, ACCESS_TOKEN_SECRET, { expiresIn: '1d' });
package/libs/utils.js CHANGED
@@ -1,34 +1,19 @@
1
- let ObjectId = require('./ObjectId')
2
-
3
- let pipeline = { execute: execute }
4
-
5
- module.exports = {
6
- uid,
7
- isEmpty,
8
- isEmptyJson,
9
- parseFloatValue,
10
- parseIntValue,
11
- now,
12
- pipeline,
13
- toDate,
14
- toAppDate,
15
- deepCopy,
16
- getDatesBetweenTwoDates,
17
- formatAMPM,
18
- sum
19
- }
1
+ import * as ObjectId from './ObjectId.js'
2
+
3
+ export let pipeline = { execute: execute }
20
4
 
21
- function uid() {
5
+
6
+ export function uid() {
22
7
  return ObjectId.ObjectId()
23
8
  }
24
9
 
25
- function isEmpty(str) { // returns true for undefined
10
+ export function isEmpty(str) { // returns true for undefined
26
11
  if(!str)
27
12
  return true
28
13
  return str.match(/\S/) ? false : true // matches a non space character
29
14
  }
30
15
 
31
- function isEmptyJson(json) {
16
+ export function isEmptyJson(json) {
32
17
  for (var i in json)
33
18
  return false
34
19
  return true
@@ -38,86 +23,65 @@ function isEmptyJson(json) {
38
23
  // Parse Numbers
39
24
  // --------------
40
25
 
41
- function parseFloatValue(val) {
26
+ export function parseFloatValue(val) {
42
27
  return parseFloat(val) ? parseFloat(val) : 0
43
28
  }
44
29
 
45
- function parseIntValue(val) {
30
+ export function parseIntValue(val) {
46
31
  return parseInt(val) ? parseInt(val) : 0
47
32
  }
48
33
 
49
- function now() {
50
- return new Date()
51
- }
52
- function toAppDate(date) { // New
53
- if (Object.prototype.toString.call(date) === '[object String]')
54
- date = new Date(date)
55
-
56
- var temp = date
57
- if (isNaN(temp) || date === '')
58
- return ''
59
-
60
- var day = temp.getDate().toString()
61
- if(day.length === 1) day = "0"+day
62
- var month = (temp.getMonth() + 1).toString()
63
- if(month.length == 1) month = "0"+month
64
- return (day + "/" + month + "/" + temp.getFullYear().toString())
65
- }
66
34
 
67
35
  // ----------------
68
- // Pipeline Execute
36
+ // Dates
69
37
  // ----------------
70
38
 
71
-
72
- function execute(pipeline, errfn) {
73
- startPipeline(pipeline, 0, undefined, errfn)
39
+ export function now() {
40
+ return new Date()
74
41
  }
75
42
 
76
- function startPipeline(pipeline, index, data, errfn) {
77
- index = index == undefined ? 0 : index
78
- if (index == pipeline.length)
79
- return
80
- pipeline[index](data).then(data => startPipeline(pipeline, index + 1, data)).catch(err => { if (errfn) errfn(err) } )
43
+
44
+ export function toAppDate(date) { // New
45
+ if (Object.prototype.toString.call(date) === '[object String]')
46
+ date = new Date(date)
47
+
48
+ var temp = date
49
+ if (isNaN(temp) || date === '')
50
+ return ''
51
+
52
+ var day = temp.getDate().toString()
53
+ if(day.length === 1) day = "0"+day
54
+ var month = (temp.getMonth() + 1).toString()
55
+ if(month.length == 1) month = "0"+month
56
+ return (day + "/" + month + "/" + temp.getFullYear().toString())
81
57
  }
82
58
 
83
- function toDate(arg, currentTime) { // New
84
59
 
85
- var st = arg.split('/')
60
+ export function toDate(arg, currentTime) { // New
86
61
 
87
- var temp = new Date(st[2],parseInt(st[1]) - 1, st[0]);
88
- temp.setHours(0) ; temp.setMinutes(0); temp.setSeconds(0); temp.setMilliseconds(0)
62
+ var st = arg.split('/')
89
63
 
90
- if (currentTime) {
91
- var curdate = getCurrentDate()
92
- temp.setHours(curdate.getHours())
93
- temp.setMinutes(curdate.getMinutes())
94
- temp.setSeconds(curdate.getSeconds())
95
- temp.setMilliseconds(curdate.getMilliseconds())
96
- }
64
+ var temp = new Date(st[2],parseInt(st[1]) - 1, st[0]);
65
+ temp.setHours(0) ; temp.setMinutes(0); temp.setSeconds(0); temp.setMilliseconds(0)
97
66
 
98
- if (isNaN(temp))
99
- return undefined
100
- return temp
67
+ if (currentTime) {
68
+ var curdate = getCurrentDate()
69
+ temp.setHours(curdate.getHours())
70
+ temp.setMinutes(curdate.getMinutes())
71
+ temp.setSeconds(curdate.getSeconds())
72
+ temp.setMilliseconds(curdate.getMilliseconds())
101
73
  }
102
74
 
103
- function toAppDate(date) { // New
104
- if (Object.prototype.toString.call(date) === '[object String]')
105
- date = new Date(date)
75
+ if (isNaN(temp))
76
+ return undefined
77
+ return temp
78
+ }
106
79
 
107
- var temp = date
108
- if (isNaN(temp) || date === '')
109
- return ''
110
80
 
111
- var day = temp.getDate().toString()
112
- if(day.length === 1) day = "0"+day
113
- var month = (temp.getMonth() + 1).toString()
114
- if(month.length == 1) month = "0"+month
115
- return (day + "/" + month + "/" + temp.getFullYear().toString())
116
- }
117
81
 
118
- function getDatesBetweenTwoDates(start, end) {
119
- var arr = [];
120
- var dt = new Date(start);
82
+ export function getDatesBetweenTwoDates(start, end) {
83
+ var arr = [];
84
+ var dt = new Date(start);
121
85
 
122
86
  dt.setHours(0)
123
87
  dt.setMinutes(0)
@@ -125,53 +89,53 @@ function getDatesBetweenTwoDates(start, end) {
125
89
  dt.setMilliseconds(0)
126
90
 
127
91
  while (dt <= end) {
128
- arr.push(new Date(dt));
129
- dt.setDate(dt.getDate() + 1);
130
- }
131
- return arr;
92
+ arr.push(new Date(dt));
93
+ dt.setDate(dt.getDate() + 1);
94
+ }
95
+ return arr;
132
96
  }
133
97
 
134
- function deepCopy(obj) {
135
- if (Object.prototype.toString.call(obj) === '[object Array]') {
136
- var out = [], i = 0, len = obj.length;
137
- for ( ; i < len; i++ ) {
138
- if (Object.prototype.toString.call(obj[i]) === '[object Date]')
139
- out[i] = new Date(obj[i])
140
- else
141
- out[i] = arguments.callee(obj[i]);
142
- }
143
- return out;
98
+ export function deepCopy(obj) {
99
+ if (Object.prototype.toString.call(obj) === '[object Array]') {
100
+ var out = [], i = 0, len = obj.length;
101
+ for ( ; i < len; i++ ) {
102
+ if (Object.prototype.toString.call(obj[i]) === '[object Date]')
103
+ out[i] = new Date(obj[i])
104
+ else
105
+ out[i] = arguments.callee(obj[i]);
144
106
  }
145
- if (typeof obj === 'object') {
146
- var out = {}, i;
147
- for ( i in obj ) {
148
- if (Object.prototype.toString.call(obj[i]) === '[object Date]')
149
- out[i] = new Date(obj[i])
150
- else
151
- out[i] = arguments.callee(obj[i]);
152
- }
153
- return out;
107
+ return out;
108
+ }
109
+ if (typeof obj === 'object') {
110
+ var out = {}, i;
111
+ for ( i in obj ) {
112
+ if (Object.prototype.toString.call(obj[i]) === '[object Date]')
113
+ out[i] = new Date(obj[i])
114
+ else
115
+ out[i] = arguments.callee(obj[i]);
154
116
  }
155
- return obj;
117
+ return out;
156
118
  }
119
+ return obj;
120
+ }
157
121
 
158
122
 
159
- function formatAMPM(date) {
160
- var hours = date.getHours();
161
- var minutes = date.getMinutes();
162
- var ampm = hours >= 12 ? 'pm' : 'am';
163
- hours = hours % 12;
164
- hours = hours ? hours : 12; // the hour '0' should be '12'
165
- minutes = minutes < 10 ? '0'+minutes : minutes;
166
- var strTime = hours + ':' + minutes + ' ' + ampm;
167
- return strTime;
168
- }
169
- function sum(array) { // Eg: sum([10,20,undefined, NaN, 40])
170
- var res = 0
171
- for (var i in array)
172
- res += parseFloat(array[i]) ? parseFloat(array[i]) : 0
173
- return res;
174
- }
123
+ export function formatAMPM(date) {
124
+ var hours = date.getHours();
125
+ var minutes = date.getMinutes();
126
+ var ampm = hours >= 12 ? 'pm' : 'am';
127
+ hours = hours % 12;
128
+ hours = hours ? hours : 12; // the hour '0' should be '12'
129
+ minutes = minutes < 10 ? '0'+minutes : minutes;
130
+ var strTime = hours + ':' + minutes + ' ' + ampm;
131
+ return strTime;
132
+ }
133
+ export function sum(array) { // Eg: sum([10,20,undefined, NaN, 40])
134
+ var res = 0
135
+ for (var i in array)
136
+ res += parseFloat(array[i]) ? parseFloat(array[i]) : 0
137
+ return res;
138
+ }
175
139
 
176
140
 
177
141
  // ----------------
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "corebasic",
3
- "version": "1.0.29",
3
+ "type": "module",
4
+ "version": "1.0.31",
4
5
  "description": "",
5
6
  "main": "index.js",
6
7
  "scripts": {
package/libs/app.js DELETED
@@ -1,15 +0,0 @@
1
-
2
-
3
- let Elabase = require('./elabase')
4
- let Kafka = require('./kafka')
5
- let Utils = require('./utils')
6
- let Session = require('./session')
7
- let Auth = require('./auth')
8
-
9
- module.exports = {
10
- Elabase,
11
- Kafka,
12
- Utils,
13
- Session,
14
- Auth
15
- }