@jwn-js/common 1.3.21 → 1.3.25

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.
Files changed (47) hide show
  1. package/Jwt-e49753f6.js +57 -0
  2. package/Jwt.d.ts +5 -25
  3. package/Jwt.js +3 -43
  4. package/{Server-77093d28.js → Server-dadc1f87.js} +5 -5
  5. package/Server.js +1 -1
  6. package/docs/.nojekyll +1 -0
  7. package/docs/assets/highlight.css +36 -0
  8. package/docs/assets/icons.css +1043 -0
  9. package/docs/assets/{images/icons.png → icons.png} +0 -0
  10. package/docs/assets/{images/icons@2x.png → icons@2x.png} +0 -0
  11. package/docs/assets/main.js +52 -0
  12. package/docs/assets/search.js +1 -0
  13. package/docs/assets/style.css +1388 -0
  14. package/docs/assets/{images/widgets.png → widgets.png} +0 -0
  15. package/docs/assets/{images/widgets@2x.png → widgets@2x.png} +0 -0
  16. package/docs/classes/ApiError.html +18 -560
  17. package/docs/classes/AsyncJwt.html +17 -0
  18. package/docs/classes/Controller.html +27 -1008
  19. package/docs/classes/Jwt.html +17 -374
  20. package/docs/classes/Memcached.html +20 -387
  21. package/docs/classes/Model.html +7 -667
  22. package/docs/classes/Server.html +25 -464
  23. package/docs/classes/Ssr.html +11 -310
  24. package/docs/classes/Web.html +11 -440
  25. package/docs/index.html +18 -258
  26. package/docs/interfaces/ApiErrorMessage.html +1 -222
  27. package/docs/interfaces/ContextSsr.html +23 -327
  28. package/docs/interfaces/ContextWeb.html +1 -291
  29. package/docs/interfaces/OptionsSsr.html +1 -184
  30. package/docs/interfaces/OptionsWeb.html +1 -212
  31. package/docs/interfaces/Route.html +1 -194
  32. package/docs/interfaces/Schema.html +1 -180
  33. package/docs/interfaces/ServerHandler.html +1 -221
  34. package/docs/interfaces/ServerOptions.html +1 -236
  35. package/docs/interfaces/ServerWebsocket.html +1 -194
  36. package/docs/modules.html +70 -2004
  37. package/index.d.ts +50 -1
  38. package/index.js +67 -14
  39. package/multipartBody.js +1 -1
  40. package/package.json +4 -4
  41. package/readConfig.js +1 -1
  42. package/readConfigSync.js +2 -2
  43. package/staticBody.js +1 -1
  44. package/urlencodedBody.js +1 -1
  45. package/docs/assets/css/main.css +0 -2660
  46. package/docs/assets/js/main.js +0 -248
  47. package/docs/assets/js/search.js +0 -1
package/index.d.ts CHANGED
@@ -11,6 +11,51 @@ import { Client } from 'memjs';
11
11
  import { Query } from 'buildmsql';
12
12
  export { Context as ContextSsr } from 'vite-ssr-vue';
13
13
 
14
+ /**
15
+ * @class Jwt
16
+ * @description working with jwt tokens use webcrypto
17
+ */
18
+ declare class AsyncJwt {
19
+ private readonly secret;
20
+ private readonly algorithm;
21
+ /**
22
+ * @constructor
23
+ * @param secret
24
+ * @param opt addition options
25
+ */
26
+ constructor(secret: string, opt?: {
27
+ algorithm: string;
28
+ });
29
+ /**
30
+ * Verify jwt token
31
+ * @param jwt token
32
+ * @returns is token valid
33
+ */
34
+ verify(jwt: string): Promise<boolean>;
35
+ /**
36
+ * Get token
37
+ * @param data - user data
38
+ * @returns jwt
39
+ */
40
+ sign(data: any): Promise<string>;
41
+ /**
42
+ * Decode token
43
+ * @param jwt - jwt token
44
+ * @returns {head, body}
45
+ */
46
+ decode(jwt: string): {
47
+ head: any;
48
+ body: any;
49
+ };
50
+ /**
51
+ * Sign
52
+ * @param str input string
53
+ * @returns base64 sign
54
+ * @private
55
+ */
56
+ private signString;
57
+ }
58
+
14
59
  /**
15
60
  * Reed static files
16
61
  * @param req
@@ -673,6 +718,10 @@ declare const helpers: {
673
718
  staticBody: typeof staticBody;
674
719
  extensions: string[];
675
720
  getExt: (path: string) => string;
721
+ jwtDecode: (jwt: string) => {
722
+ head: any;
723
+ body: any;
724
+ };
676
725
  };
677
726
 
678
- export { ContextWeb$1 as ContextWeb, Controller, Model, OptionsSsr, OptionsWeb, Route$1 as Route, Schema, Ssr, Web, action, body, config, connection, context, controller, cookies, db, headers, helpers, home, hostname, http, init, json, logerror, method, mixin, mountWithContext as mount, pool, protocol, request, stream, subaction, url, xml };
727
+ export { AsyncJwt, ContextWeb$1 as ContextWeb, Controller, Model, OptionsSsr, OptionsWeb, Route$1 as Route, Schema, Ssr, Web, action, body, config, connection, context, controller, cookies, db, headers, helpers, home, hostname, http, init, json, logerror, method, mixin, mountWithContext as mount, pool, protocol, request, stream, subaction, url, xml };
package/index.js CHANGED
@@ -3,8 +3,10 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var ApiError = require('./ApiError.js');
6
- var Server = require('./Server-77093d28.js');
7
- var Jwt = require('./Jwt.js');
6
+ var Server = require('./Server-dadc1f87.js');
7
+ var Jwt = require('./Jwt-e49753f6.js');
8
+ var crypto = require('crypto');
9
+ var util = require('util');
8
10
  var Memcached = require('./Memcached.js');
9
11
  var jsonBody$1 = require('./jsonBody.js');
10
12
  var ApiError$1 = require('./ApiError-b517f53a.js');
@@ -24,7 +26,6 @@ var querystring = require('querystring');
24
26
  var xmljs = require('xml-js');
25
27
  var formidable = require('formidable');
26
28
  require('reflect-metadata');
27
- require('crypto');
28
29
 
29
30
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
30
31
 
@@ -34,6 +35,56 @@ var querystring__default = /*#__PURE__*/_interopDefaultLegacy(querystring);
34
35
  var xmljs__default = /*#__PURE__*/_interopDefaultLegacy(xmljs);
35
36
  var formidable__default = /*#__PURE__*/_interopDefaultLegacy(formidable);
36
37
 
38
+ const { subtle } = crypto.webcrypto;
39
+ class AsyncJwt {
40
+ constructor(secret, opt) {
41
+ this.algorithm = "SHA-256";
42
+ this.secret = secret;
43
+ this.algorithm = opt?.algorithm || this.algorithm;
44
+ this.algorithm = this.algorithm.replace("-", "").replace("SHA", "SHA-");
45
+ }
46
+ async verify(jwt) {
47
+ if (!jwt) {
48
+ return false;
49
+ }
50
+ const parts = jwt.split(".");
51
+ const signature = await this.signString(`${parts[0]}.${parts[1]}`);
52
+ return signature === parts[2];
53
+ }
54
+ async sign(data) {
55
+ const head = Jwt.toBase64url({
56
+ alg: this.algorithm.replace("-", "").replace("SHA", "HS"),
57
+ typ: "JWT"
58
+ });
59
+ const body = Jwt.toBase64url(data);
60
+ const signature = await this.signString(`${head}.${body}`);
61
+ return `${head}.${body}.${signature}`;
62
+ }
63
+ decode(jwt) {
64
+ const parts = (jwt || "").split(".");
65
+ const head = Jwt.fromBase64url(parts[0]);
66
+ const body = Jwt.fromBase64url(parts[1]);
67
+ return { head, body };
68
+ }
69
+ async signString(str) {
70
+ const enc = new util.TextEncoder();
71
+ const key = await subtle.importKey("raw", enc.encode(this.secret), {
72
+ name: "HMAC",
73
+ hash: {
74
+ name: this.algorithm
75
+ }
76
+ }, false, ["sign", "verify"]);
77
+ return Jwt.urlEncode(Buffer.from(await subtle.sign("HMAC", key, enc.encode(str))).toString("base64"));
78
+ }
79
+ }
80
+
81
+ const jwtDecode = (jwt) => {
82
+ const parts = (jwt || "").split(".");
83
+ const head = Jwt.fromBase64url(parts[0]);
84
+ const body = Jwt.fromBase64url(parts[1]);
85
+ return { head, body };
86
+ };
87
+
37
88
  const rawBody$1 = (res, req) => new Promise((resolve, reject) => {
38
89
  readRaw$1(res, (obj) => {
39
90
  resolve(obj);
@@ -251,7 +302,7 @@ async function staticBody(res, req, base) {
251
302
  }));
252
303
  let url = req.getUrl().replace(/\.\.\//ig, "").replace(/^\.\/?/, "");
253
304
  url = `./${url}`;
254
- const file = path__default['default'].resolve(base, url);
305
+ const file = path__default["default"].resolve(base, url);
255
306
  const content = await fs.promises.readFile(file);
256
307
  res.writeStatus("200 OK").writeHeader("content-type", getContentType(getExt(url))).end(content);
257
308
  } catch (e) {
@@ -588,7 +639,7 @@ const multipartBody = (res, req, options = { multiples: true }) => new Promise(a
588
639
  stream.end();
589
640
  }
590
641
  });
591
- const form = formidable__default['default'](options);
642
+ const form = formidable__default["default"](options);
592
643
  form.parse(stream, (err2, fields, files) => {
593
644
  if (err2) {
594
645
  reject(err2);
@@ -614,7 +665,7 @@ function readBody(res, cb, err) {
614
665
  buffer = Buffer.concat([buffer, Buffer.from(ab)]);
615
666
  if (isLast) {
616
667
  try {
617
- cb(querystring__default['default'].parse(buffer.toString()));
668
+ cb(querystring__default["default"].parse(buffer.toString()));
618
669
  } catch (e) {
619
670
  cb(null);
620
671
  return;
@@ -689,7 +740,7 @@ class Web {
689
740
  };
690
741
  this.req.forEach((key, value) => this.contextWeb.headers[key] = value);
691
742
  const type = this.contextWeb.headers["content-type"] || "application/json";
692
- request = Object.assign({}, this.defaultRequest, request, querystring__default['default'].parse(this.req.getQuery()));
743
+ request = Object.assign({}, this.defaultRequest, request, querystring__default["default"].parse(this.req.getQuery()));
693
744
  if (type.indexOf("application/json") !== -1 || type.indexOf("text/json") !== -1) {
694
745
  Object.assign(request, await jsonBody(this.res, this.req));
695
746
  } else if (!["get", "head"].includes(this.contextWeb.method) && type.indexOf("multipart/form-data") !== -1) {
@@ -698,7 +749,7 @@ class Web {
698
749
  Object.assign(request, await urlencodedBody(this.res, this.req));
699
750
  } else if (!["get", "head"].includes(this.contextWeb.method) && (type.indexOf("application/xml") !== -1 || type.indexOf("text/xml") !== -1)) {
700
751
  const raw = (await rawBody(this.res, this.req)).toString();
701
- const records = xmljs__default['default'].xml2js(raw, { compact: true, cdataKey: "_value", textKey: "_value" });
752
+ const records = xmljs__default["default"].xml2js(raw, { compact: true, cdataKey: "_value", textKey: "_value" });
702
753
  Object.assign(request, records || {});
703
754
  } else if (!["get", "head"].includes(this.contextWeb.method) && type.indexOf("application/octet-stream") !== -1) {
704
755
  Object.defineProperty(this.contextWeb, "$stream", {
@@ -810,8 +861,8 @@ class Web {
810
861
  }
811
862
 
812
863
  const readConfigSync = (jsonfile) => {
813
- const file = path__default['default'].resolve(jsonfile);
814
- return JSON.parse(fs__default['default'].readFileSync(file, "utf-8"));
864
+ const file = path__default["default"].resolve(jsonfile);
865
+ return JSON.parse(fs__default["default"].readFileSync(file, "utf-8"));
815
866
  };
816
867
 
817
868
  const config$1 = readConfigSync("./connect.json");
@@ -932,7 +983,7 @@ class Controller {
932
983
  const response = this.success(data, options);
933
984
  return {
934
985
  headers: Object.assign({ "content-type": "application/xml" }, this.responseHeaders, options?.headers || {}),
935
- body: xmljs__default['default'].js2xml(Object.assign(defaultXml, { body: { ...response.body } }), xmlOptions),
986
+ body: xmljs__default["default"].js2xml(Object.assign(defaultXml, { body: { ...response.body } }), xmlOptions),
936
987
  memcache: response.memcache
937
988
  };
938
989
  }
@@ -950,7 +1001,7 @@ class Controller {
950
1001
  const headers = Object.assign({ "content-type": "application/json" }, e.headers);
951
1002
  return {
952
1003
  headers,
953
- body: xmljs__default['default'].js2xml(Object.assign(defaultXml, { body: { ...response.body } }), xmlOptions),
1004
+ body: xmljs__default["default"].js2xml(Object.assign(defaultXml, { body: { ...response.body } }), xmlOptions),
954
1005
  statusCode: response.statusCode
955
1006
  };
956
1007
  }
@@ -1302,7 +1353,7 @@ function http() {
1302
1353
  continue;
1303
1354
  }
1304
1355
  const logger = existingLoggers[key].params;
1305
- await this[logger].error(e, { responce: output?.body || {} });
1356
+ await this[logger].error(e, { response: easyAsh.omit(output?.body || {}, ["stack"]) });
1306
1357
  }
1307
1358
  return output;
1308
1359
  } finally {
@@ -1334,7 +1385,8 @@ const helpers = {
1334
1385
  readConfigSync: readConfigSync$1.readConfigSync,
1335
1386
  staticBody,
1336
1387
  extensions,
1337
- getExt
1388
+ getExt,
1389
+ jwtDecode
1338
1390
  };
1339
1391
 
1340
1392
  exports.ApiError = ApiError.ApiError;
@@ -1342,6 +1394,7 @@ exports.Server = Server.Server;
1342
1394
  exports.codeToStatus = Server.codeToStatus;
1343
1395
  exports.Jwt = Jwt.Jwt;
1344
1396
  exports.Memcached = Memcached.Memcached;
1397
+ exports.AsyncJwt = AsyncJwt;
1345
1398
  exports.Controller = Controller;
1346
1399
  exports.Model = Model;
1347
1400
  exports.Ssr = Ssr;
package/multipartBody.js CHANGED
@@ -24,7 +24,7 @@ const multipartBody = (res, req, options = { multiples: true }) => new Promise(a
24
24
  stream$1.end();
25
25
  }
26
26
  });
27
- const form = formidable__default['default'](options);
27
+ const form = formidable__default["default"](options);
28
28
  form.parse(stream$1, (err2, fields, files) => {
29
29
  if (err2) {
30
30
  reject(err2);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jwn-js/common",
3
3
  "private": false,
4
- "version": "1.3.21",
4
+ "version": "1.3.25",
5
5
  "description": "@jwn-js/common package",
6
6
  "main": "./index.js",
7
7
  "types": "./index.d.ts",
@@ -22,7 +22,7 @@
22
22
  "@types/formidable": "^1.2.3",
23
23
  "@types/jest": "^27.0.3",
24
24
  "@types/memjs": "^1.2.3",
25
- "@types/node": "^15.3.0",
25
+ "@types/node": "^17.0.5",
26
26
  "@types/supertest": "^2.0.11",
27
27
  "@typescript-eslint/eslint-plugin": "^4.23.0",
28
28
  "@typescript-eslint/parser": "^4.26.1",
@@ -33,12 +33,12 @@
33
33
  "jest": "^27.4.3",
34
34
  "rollup": "^2.53.2",
35
35
  "rollup-plugin-dts": "^3.0.2",
36
- "rollup-plugin-esbuild": "^4.5.0",
36
+ "rollup-plugin-esbuild": "4.1",
37
37
  "supertest": "^6.1.6",
38
38
  "superwstest": "^1.8.0",
39
39
  "ts-jest": "^27.1.0",
40
40
  "ts-node": "^10.4.0",
41
- "typedoc": "^0.21.4",
41
+ "typedoc": "^0.22.10",
42
42
  "typescript": "^4.3.2",
43
43
  "zlib": "^1.0.5"
44
44
  },
package/readConfig.js CHANGED
@@ -10,7 +10,7 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
10
10
  var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
11
11
 
12
12
  const readConfig = async (jsonfile) => {
13
- const file = path__default['default'].resolve(jsonfile);
13
+ const file = path__default["default"].resolve(jsonfile);
14
14
  return JSON.parse(await fs.promises.readFile(file, "utf-8"));
15
15
  };
16
16
 
package/readConfigSync.js CHANGED
@@ -11,8 +11,8 @@ var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
11
11
  var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
12
12
 
13
13
  const readConfigSync = (jsonfile) => {
14
- const file = path__default['default'].resolve(jsonfile);
15
- return JSON.parse(fs__default['default'].readFileSync(file, "utf-8"));
14
+ const file = path__default["default"].resolve(jsonfile);
15
+ return JSON.parse(fs__default["default"].readFileSync(file, "utf-8"));
16
16
  };
17
17
 
18
18
  exports.readConfigSync = readConfigSync;
package/staticBody.js CHANGED
@@ -193,7 +193,7 @@ async function staticBody(res, req, base) {
193
193
  }));
194
194
  let url = req.getUrl().replace(/\.\.\//ig, "").replace(/^\.\/?/, "");
195
195
  url = `./${url}`;
196
- const file = path__default['default'].resolve(base, url);
196
+ const file = path__default["default"].resolve(base, url);
197
197
  const content = await fs.promises.readFile(file);
198
198
  res.writeStatus("200 OK").writeHeader("content-type", getContentType(getExt(url))).end(content);
199
199
  } catch (e) {
package/urlencodedBody.js CHANGED
@@ -22,7 +22,7 @@ function readBody(res, cb, err) {
22
22
  buffer = Buffer.concat([buffer, Buffer.from(ab)]);
23
23
  if (isLast) {
24
24
  try {
25
- cb(querystring__default['default'].parse(buffer.toString()));
25
+ cb(querystring__default["default"].parse(buffer.toString()));
26
26
  } catch (e) {
27
27
  cb(null);
28
28
  return;