@jwn-js/common 2.2.3 → 2.2.4
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/ApiError.js +1 -37
- package/ApiError.mjs +1 -35
- package/Jwt-7tQL-rwa.js +1 -0
- package/Jwt-CDdbxwvH.js +1 -0
- package/Jwt.js +1 -8
- package/Jwt.mjs +1 -2
- package/Memcached.js +9 -166
- package/Memcached.mjs +9 -164
- package/Server.js +1 -316
- package/Server.mjs +1 -313
- package/cookieParse.js +1 -25
- package/cookieParse.mjs +1 -23
- package/cookieString.js +1 -22
- package/cookieString.mjs +1 -20
- package/docs/classes/ApiError.html +2 -2
- package/docs/classes/AsyncJwt.html +2 -2
- package/docs/classes/Controller.html +2 -2
- package/docs/classes/Jwt.html +2 -2
- package/docs/classes/Memcached.html +2 -2
- package/docs/classes/Model.html +2 -2
- package/docs/classes/Server.html +2 -2
- package/docs/classes/Ssr.html +2 -2
- package/docs/classes/Web.html +2 -2
- package/docs/functions/action.html +2 -2
- package/docs/functions/body.html +2 -2
- package/docs/functions/codeToStatus.html +2 -2
- package/docs/functions/config.html +2 -2
- package/docs/functions/connection.html +2 -2
- package/docs/functions/context.html +2 -2
- package/docs/functions/controller-1.html +2 -2
- package/docs/functions/cookies.html +2 -2
- package/docs/functions/db.html +2 -2
- package/docs/functions/headers.html +2 -2
- package/docs/functions/home.html +2 -2
- package/docs/functions/hostname.html +2 -2
- package/docs/functions/http.html +2 -2
- package/docs/functions/init.html +2 -2
- package/docs/functions/json.html +2 -2
- package/docs/functions/logerror.html +2 -2
- package/docs/functions/method.html +2 -2
- package/docs/functions/mixin.html +2 -2
- package/docs/functions/mount.html +2 -2
- package/docs/functions/pool.html +2 -2
- package/docs/functions/protocol.html +2 -2
- package/docs/functions/request.html +2 -2
- package/docs/functions/selectControllersSchema.html +1 -1
- package/docs/functions/stream.html +2 -2
- package/docs/functions/subaction.html +2 -2
- package/docs/functions/url.html +2 -2
- package/docs/functions/xml.html +2 -2
- package/docs/index.html +2 -2
- package/docs/interfaces/ApiErrorMessage.html +2 -2
- package/docs/interfaces/ContextSsr.html +2 -2
- package/docs/interfaces/ContextWeb.html +2 -2
- package/docs/interfaces/OptionsSsr.html +2 -2
- package/docs/interfaces/OptionsWeb.html +2 -2
- package/docs/interfaces/ResponseOptions.html +2 -2
- package/docs/interfaces/Route.html +2 -2
- package/docs/interfaces/Schema.html +2 -2
- package/docs/interfaces/ServerHandler.html +2 -2
- package/docs/interfaces/ServerOptions.html +2 -2
- package/docs/interfaces/ServerWebsocket.html +2 -2
- package/docs/modules.html +2 -2
- package/docs/types/ServerRoutes.html +1 -1
- package/docs/variables/helpers.html +2 -2
- package/index.js +4 -1473
- package/index.mjs +4 -1417
- package/jsonBody.js +1 -25
- package/jsonBody.mjs +1 -23
- package/multipartBody.js +1 -42
- package/multipartBody.mjs +1 -40
- package/package.json +2 -2
- package/readConfig.js +1 -11
- package/readConfig.mjs +1 -9
- package/readConfigSync.js +1 -11
- package/readConfigSync.mjs +1 -9
- package/staticBody.js +1 -205
- package/staticBody.mjs +1 -200
- package/urlencodedBody.js +1 -26
- package/urlencodedBody.mjs +1 -24
- package/Jwt-B4QZ2Ypb.js +0 -80
- package/Jwt-Be4GWrIO.js +0 -75
package/ApiError.js
CHANGED
|
@@ -1,37 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
class ApiError extends Error {
|
|
4
|
-
constructor(params = "", code = 0, statusCode = 404) {
|
|
5
|
-
super();
|
|
6
|
-
let paramsObj = {};
|
|
7
|
-
if (typeof params === "string") {
|
|
8
|
-
paramsObj.message = params;
|
|
9
|
-
paramsObj.code = code;
|
|
10
|
-
paramsObj.statusCode = statusCode;
|
|
11
|
-
} else {
|
|
12
|
-
paramsObj = params;
|
|
13
|
-
}
|
|
14
|
-
this.statusCode = paramsObj.statusCode ?? 404;
|
|
15
|
-
this.code = paramsObj.code ?? 0;
|
|
16
|
-
this.message = paramsObj.message ?? "";
|
|
17
|
-
this.data = paramsObj.data ?? {};
|
|
18
|
-
this.headers = paramsObj.headers ?? {};
|
|
19
|
-
}
|
|
20
|
-
getMessage() {
|
|
21
|
-
return this.message;
|
|
22
|
-
}
|
|
23
|
-
getStatusCode() {
|
|
24
|
-
return this.statusCode;
|
|
25
|
-
}
|
|
26
|
-
getCode() {
|
|
27
|
-
return this.code;
|
|
28
|
-
}
|
|
29
|
-
getData() {
|
|
30
|
-
return this.data;
|
|
31
|
-
}
|
|
32
|
-
getHeaders() {
|
|
33
|
-
return this.headers;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
exports.ApiError = ApiError;
|
|
1
|
+
"use strict";class a extends Error{constructor(t="",s=0,r=404){super();let e={};typeof t=="string"?(e.message=t,e.code=s,e.statusCode=r):e=t,this.statusCode=e.statusCode??404,this.code=e.code??0,this.message=e.message??"",this.data=e.data??{},this.headers=e.headers??{}}getMessage(){return this.message}getStatusCode(){return this.statusCode}getCode(){return this.code}getData(){return this.data}getHeaders(){return this.headers}}exports.ApiError=a;
|
package/ApiError.mjs
CHANGED
|
@@ -1,35 +1 @@
|
|
|
1
|
-
class
|
|
2
|
-
constructor(params = "", code = 0, statusCode = 404) {
|
|
3
|
-
super();
|
|
4
|
-
let paramsObj = {};
|
|
5
|
-
if (typeof params === "string") {
|
|
6
|
-
paramsObj.message = params;
|
|
7
|
-
paramsObj.code = code;
|
|
8
|
-
paramsObj.statusCode = statusCode;
|
|
9
|
-
} else {
|
|
10
|
-
paramsObj = params;
|
|
11
|
-
}
|
|
12
|
-
this.statusCode = paramsObj.statusCode ?? 404;
|
|
13
|
-
this.code = paramsObj.code ?? 0;
|
|
14
|
-
this.message = paramsObj.message ?? "";
|
|
15
|
-
this.data = paramsObj.data ?? {};
|
|
16
|
-
this.headers = paramsObj.headers ?? {};
|
|
17
|
-
}
|
|
18
|
-
getMessage() {
|
|
19
|
-
return this.message;
|
|
20
|
-
}
|
|
21
|
-
getStatusCode() {
|
|
22
|
-
return this.statusCode;
|
|
23
|
-
}
|
|
24
|
-
getCode() {
|
|
25
|
-
return this.code;
|
|
26
|
-
}
|
|
27
|
-
getData() {
|
|
28
|
-
return this.data;
|
|
29
|
-
}
|
|
30
|
-
getHeaders() {
|
|
31
|
-
return this.headers;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export { ApiError };
|
|
1
|
+
class a extends Error{constructor(t="",s=0,r=404){super();let e={};typeof t=="string"?(e.message=t,e.code=s,e.statusCode=r):e=t,this.statusCode=e.statusCode??404,this.code=e.code??0,this.message=e.message??"",this.data=e.data??{},this.headers=e.headers??{}}getMessage(){return this.message}getStatusCode(){return this.statusCode}getCode(){return this.code}getData(){return this.data}getHeaders(){return this.headers}}export{a as ApiError};
|
package/Jwt-7tQL-rwa.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import l from"crypto";const i=e=>e.replace(/\+/ig,"-").replace(/\//ig,"_").replace(/=+$/,""),h=e=>e.replace(/-/ig,"+").replace(/_/ig,"/"),o=e=>i(Buffer.from(JSON.stringify(e)).toString("base64")),c=e=>JSON.parse(Buffer.from(h(e),"base64").toString());class g{constructor(r,t){this.algorithm="SHA256",this.secret=r,this.algorithm=t?.algorithm||this.algorithm,this.algorithm=this.algorithm.replace("-","")}verify(r){if(!r)return!1;const t=r.split(".");return i(l.createHmac(this.algorithm,this.secret).update(`${t[0]}.${t[1]}`).digest("base64"))===t[2]}sign(r){const t=o({alg:this.algorithm.replace("SHA","HS"),typ:"JWT"}),s=o(r),a=i(l.createHmac(this.algorithm,this.secret).update(`${t}.${s}`).digest("base64"));return`${t}.${s}.${a}`}decode(r){const t=(r||"").split("."),s=c(t[0]),a=c(t[1]);return{head:s,body:a}}}export{g as J,c as f,o as t,i as u};
|
package/Jwt-CDdbxwvH.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var c=require("crypto");const i=t=>t.replace(/\+/ig,"-").replace(/\//ig,"_").replace(/=+$/,""),h=t=>t.replace(/-/ig,"+").replace(/_/ig,"/"),o=t=>i(Buffer.from(JSON.stringify(t)).toString("base64")),l=t=>JSON.parse(Buffer.from(h(t),"base64").toString());class g{constructor(r,e){this.algorithm="SHA256",this.secret=r,this.algorithm=e?.algorithm||this.algorithm,this.algorithm=this.algorithm.replace("-","")}verify(r){if(!r)return!1;const e=r.split(".");return i(c.createHmac(this.algorithm,this.secret).update(`${e[0]}.${e[1]}`).digest("base64"))===e[2]}sign(r){const e=o({alg:this.algorithm.replace("SHA","HS"),typ:"JWT"}),s=o(r),a=i(c.createHmac(this.algorithm,this.secret).update(`${e}.${s}`).digest("base64"));return`${e}.${s}.${a}`}decode(r){const e=(r||"").split("."),s=l(e[0]),a=l(e[1]);return{head:s,body:a}}}exports.Jwt=g,exports.fromBase64url=l,exports.toBase64url=o,exports.urlEncode=i;
|
package/Jwt.js
CHANGED
package/Jwt.mjs
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
export { J as Jwt } from './Jwt-Be4GWrIO.js';
|
|
1
|
+
import"crypto";import{J as m}from"./Jwt-7tQL-rwa.js";export{m as Jwt};
|
package/Memcached.js
CHANGED
|
@@ -1,166 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
*/
|
|
11
|
-
constructor(memjs, specialHeaders = { "x-cache": "memcached-js" }, maxKeyLength = 160, debug = 0) {
|
|
12
|
-
this.prefix = "";
|
|
13
|
-
this.memjs = memjs;
|
|
14
|
-
this.specialHeaders = specialHeaders;
|
|
15
|
-
this.maxKeyLength = maxKeyLength;
|
|
16
|
-
this.debug = debug;
|
|
17
|
-
this.prefix = process.env.NODE_ENV !== "production" ? "dev::" : "";
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Is Client exists
|
|
21
|
-
*/
|
|
22
|
-
isClient() {
|
|
23
|
-
return !!this.memjs;
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Is in client is one or more conected servers
|
|
27
|
-
*/
|
|
28
|
-
isConnectedServers() {
|
|
29
|
-
if (!this.isClient()) {
|
|
30
|
-
return false;
|
|
31
|
-
}
|
|
32
|
-
const servers = this.memjs?.servers || [];
|
|
33
|
-
for (const server of servers) {
|
|
34
|
-
if (server.connected) {
|
|
35
|
-
return true;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
return false;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Set value to memcache
|
|
42
|
-
* @param key string key
|
|
43
|
-
* @param value value
|
|
44
|
-
* @param expires expiers in second
|
|
45
|
-
*/
|
|
46
|
-
async setValue(key, value, expires) {
|
|
47
|
-
if ((expires || expires === 0) && key.length <= this.maxKeyLength && this.memjs) {
|
|
48
|
-
try {
|
|
49
|
-
await this.memjs.set(`${this.prefix}${key}`, Buffer.from(value), { expires });
|
|
50
|
-
} catch (e) {
|
|
51
|
-
if (this.debug) {
|
|
52
|
-
console.log(e);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Get value from memory
|
|
59
|
-
* @param key string key
|
|
60
|
-
*/
|
|
61
|
-
async getValue(key) {
|
|
62
|
-
if (key.length <= this.maxKeyLength && this.memjs) {
|
|
63
|
-
try {
|
|
64
|
-
const obj = await this.memjs.get(`${this.prefix}${key}`);
|
|
65
|
-
const result = typeof obj === "object" ? obj?.value : obj;
|
|
66
|
-
if (!result) {
|
|
67
|
-
return null;
|
|
68
|
-
}
|
|
69
|
-
return result;
|
|
70
|
-
} catch (e) {
|
|
71
|
-
if (this.debug) {
|
|
72
|
-
console.log(e);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
return null;
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Delete value
|
|
80
|
-
* @param key
|
|
81
|
-
*/
|
|
82
|
-
async deleteValue(key) {
|
|
83
|
-
if (key.length <= this.maxKeyLength && this.memjs) {
|
|
84
|
-
try {
|
|
85
|
-
return this.memjs.delete(`${this.prefix}${key}`);
|
|
86
|
-
} catch (e) {
|
|
87
|
-
if (this.debug) {
|
|
88
|
-
console.log(e);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
return false;
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Set page to cache
|
|
96
|
-
* @param url https://yur.site.net
|
|
97
|
-
* @param headers
|
|
98
|
-
* @param data eny data string or Buffer
|
|
99
|
-
* @param expires
|
|
100
|
-
*/
|
|
101
|
-
async setPage(url, headers, data, expires) {
|
|
102
|
-
if ((expires || expires === 0) && url.length <= this.maxKeyLength && this.memjs) {
|
|
103
|
-
try {
|
|
104
|
-
headers = this.objectKeysToLower(headers);
|
|
105
|
-
delete headers["set-cookie"];
|
|
106
|
-
const headersString = Object.entries(headers).map((v) => v.join(":")).join("\r\n");
|
|
107
|
-
const memjsHeaders = "EXTRACT_HEADERS\r\n" + headersString + "\r\n\r\n";
|
|
108
|
-
const toMamcache = Buffer.concat([Buffer.from(memjsHeaders), Buffer.from(data)]);
|
|
109
|
-
const key = /^http/.test(url) ? url : `${this.prefix}${url}`;
|
|
110
|
-
await this.memjs.set(key, toMamcache, { expires });
|
|
111
|
-
} catch (e) {
|
|
112
|
-
if (this.debug) {
|
|
113
|
-
console.log(e);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Get Page and headers
|
|
120
|
-
* @param url https://your.site.net
|
|
121
|
-
* @returns {data, headers}
|
|
122
|
-
*/
|
|
123
|
-
async getPage(url) {
|
|
124
|
-
if (url.length <= this.maxKeyLength && this.memjs) {
|
|
125
|
-
try {
|
|
126
|
-
let headers = {};
|
|
127
|
-
const key = /^http/.test(url) ? url : `${this.prefix}${url}`;
|
|
128
|
-
const obj = await this.memjs.get(key);
|
|
129
|
-
const result = typeof obj === "object" ? obj?.value : obj;
|
|
130
|
-
if (!result) {
|
|
131
|
-
return null;
|
|
132
|
-
}
|
|
133
|
-
const memData = Buffer.from(result);
|
|
134
|
-
const start = memData.indexOf(Buffer.from("EXTRACT_HEADERS\r\n"));
|
|
135
|
-
const end = memData.indexOf(Buffer.from("\r\n\r\n"));
|
|
136
|
-
if (start < 0 || end < 0) {
|
|
137
|
-
return null;
|
|
138
|
-
}
|
|
139
|
-
const arr = memData.subarray(start, end).toString().split("\r\n");
|
|
140
|
-
arr.shift();
|
|
141
|
-
arr.map((row) => {
|
|
142
|
-
const [index, value] = row.split(":");
|
|
143
|
-
headers[index] = value;
|
|
144
|
-
});
|
|
145
|
-
headers = Object.assign(headers, this.specialHeaders);
|
|
146
|
-
const data = memData.subarray(end + 4);
|
|
147
|
-
return { headers, data };
|
|
148
|
-
} catch (e) {
|
|
149
|
-
if (this.debug) {
|
|
150
|
-
console.log(e);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
return null;
|
|
155
|
-
}
|
|
156
|
-
/**
|
|
157
|
-
* Keys to lower case in Object
|
|
158
|
-
* @param o
|
|
159
|
-
* @private
|
|
160
|
-
*/
|
|
161
|
-
objectKeysToLower(o) {
|
|
162
|
-
return Object.keys(o).reduce((c, k) => (c[k.toLowerCase()] = o[k], c), {});
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
exports.Memcached = Memcached;
|
|
1
|
+
"use strict";class u{constructor(t,e={"x-cache":"memcached-js"},s=160,r=0){this.prefix="",this.memjs=t,this.specialHeaders=e,this.maxKeyLength=s,this.debug=r,this.prefix=process.env.NODE_ENV!=="production"?"dev::":""}isClient(){return!!this.memjs}isConnectedServers(){if(!this.isClient())return!1;const t=this.memjs?.servers||[];for(const e of t)if(e.connected)return!0;return!1}async setValue(t,e,s){if((s||s===0)&&t.length<=this.maxKeyLength&&this.memjs)try{await this.memjs.set(`${this.prefix}${t}`,Buffer.from(e),{expires:s})}catch(r){this.debug&&console.log(r)}}async getValue(t){if(t.length<=this.maxKeyLength&&this.memjs)try{const e=await this.memjs.get(`${this.prefix}${t}`);return(typeof e=="object"?e?.value:e)||null}catch(e){this.debug&&console.log(e)}return null}async deleteValue(t){if(t.length<=this.maxKeyLength&&this.memjs)try{return this.memjs.delete(`${this.prefix}${t}`)}catch(e){this.debug&&console.log(e)}return!1}async setPage(t,e,s,r){if((r||r===0)&&t.length<=this.maxKeyLength&&this.memjs)try{e=this.objectKeysToLower(e),delete e["set-cookie"];const i=`EXTRACT_HEADERS\r
|
|
2
|
+
`+Object.entries(e).map(h=>h.join(":")).join(`\r
|
|
3
|
+
`)+`\r
|
|
4
|
+
\r
|
|
5
|
+
`,n=Buffer.concat([Buffer.from(i),Buffer.from(s)]),c=/^http/.test(t)?t:`${this.prefix}${t}`;await this.memjs.set(c,n,{expires:r})}catch(i){this.debug&&console.log(i)}}async getPage(t){if(t.length<=this.maxKeyLength&&this.memjs)try{let e={};const s=/^http/.test(t)?t:`${this.prefix}${t}`,r=await this.memjs.get(s),i=typeof r=="object"?r?.value:r;if(!i)return null;const n=Buffer.from(i),c=n.indexOf(Buffer.from(`EXTRACT_HEADERS\r
|
|
6
|
+
`)),h=n.indexOf(Buffer.from(`\r
|
|
7
|
+
\r
|
|
8
|
+
`));if(c<0||h<0)return null;const o=n.subarray(c,h).toString().split(`\r
|
|
9
|
+
`);o.shift(),o.map(m=>{const[f,l]=m.split(":");e[f]=l}),e=Object.assign(e,this.specialHeaders);const a=n.subarray(h+4);return{headers:e,data:a}}catch(e){this.debug&&console.log(e)}return null}objectKeysToLower(t){return Object.keys(t).reduce((e,s)=>(e[s.toLowerCase()]=t[s],e),{})}}exports.Memcached=u;
|
package/Memcached.mjs
CHANGED
|
@@ -1,164 +1,9 @@
|
|
|
1
|
-
class
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
this.prefix = "";
|
|
11
|
-
this.memjs = memjs;
|
|
12
|
-
this.specialHeaders = specialHeaders;
|
|
13
|
-
this.maxKeyLength = maxKeyLength;
|
|
14
|
-
this.debug = debug;
|
|
15
|
-
this.prefix = process.env.NODE_ENV !== "production" ? "dev::" : "";
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Is Client exists
|
|
19
|
-
*/
|
|
20
|
-
isClient() {
|
|
21
|
-
return !!this.memjs;
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Is in client is one or more conected servers
|
|
25
|
-
*/
|
|
26
|
-
isConnectedServers() {
|
|
27
|
-
if (!this.isClient()) {
|
|
28
|
-
return false;
|
|
29
|
-
}
|
|
30
|
-
const servers = this.memjs?.servers || [];
|
|
31
|
-
for (const server of servers) {
|
|
32
|
-
if (server.connected) {
|
|
33
|
-
return true;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
return false;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Set value to memcache
|
|
40
|
-
* @param key string key
|
|
41
|
-
* @param value value
|
|
42
|
-
* @param expires expiers in second
|
|
43
|
-
*/
|
|
44
|
-
async setValue(key, value, expires) {
|
|
45
|
-
if ((expires || expires === 0) && key.length <= this.maxKeyLength && this.memjs) {
|
|
46
|
-
try {
|
|
47
|
-
await this.memjs.set(`${this.prefix}${key}`, Buffer.from(value), { expires });
|
|
48
|
-
} catch (e) {
|
|
49
|
-
if (this.debug) {
|
|
50
|
-
console.log(e);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Get value from memory
|
|
57
|
-
* @param key string key
|
|
58
|
-
*/
|
|
59
|
-
async getValue(key) {
|
|
60
|
-
if (key.length <= this.maxKeyLength && this.memjs) {
|
|
61
|
-
try {
|
|
62
|
-
const obj = await this.memjs.get(`${this.prefix}${key}`);
|
|
63
|
-
const result = typeof obj === "object" ? obj?.value : obj;
|
|
64
|
-
if (!result) {
|
|
65
|
-
return null;
|
|
66
|
-
}
|
|
67
|
-
return result;
|
|
68
|
-
} catch (e) {
|
|
69
|
-
if (this.debug) {
|
|
70
|
-
console.log(e);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
return null;
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Delete value
|
|
78
|
-
* @param key
|
|
79
|
-
*/
|
|
80
|
-
async deleteValue(key) {
|
|
81
|
-
if (key.length <= this.maxKeyLength && this.memjs) {
|
|
82
|
-
try {
|
|
83
|
-
return this.memjs.delete(`${this.prefix}${key}`);
|
|
84
|
-
} catch (e) {
|
|
85
|
-
if (this.debug) {
|
|
86
|
-
console.log(e);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
return false;
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Set page to cache
|
|
94
|
-
* @param url https://yur.site.net
|
|
95
|
-
* @param headers
|
|
96
|
-
* @param data eny data string or Buffer
|
|
97
|
-
* @param expires
|
|
98
|
-
*/
|
|
99
|
-
async setPage(url, headers, data, expires) {
|
|
100
|
-
if ((expires || expires === 0) && url.length <= this.maxKeyLength && this.memjs) {
|
|
101
|
-
try {
|
|
102
|
-
headers = this.objectKeysToLower(headers);
|
|
103
|
-
delete headers["set-cookie"];
|
|
104
|
-
const headersString = Object.entries(headers).map((v) => v.join(":")).join("\r\n");
|
|
105
|
-
const memjsHeaders = "EXTRACT_HEADERS\r\n" + headersString + "\r\n\r\n";
|
|
106
|
-
const toMamcache = Buffer.concat([Buffer.from(memjsHeaders), Buffer.from(data)]);
|
|
107
|
-
const key = /^http/.test(url) ? url : `${this.prefix}${url}`;
|
|
108
|
-
await this.memjs.set(key, toMamcache, { expires });
|
|
109
|
-
} catch (e) {
|
|
110
|
-
if (this.debug) {
|
|
111
|
-
console.log(e);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* Get Page and headers
|
|
118
|
-
* @param url https://your.site.net
|
|
119
|
-
* @returns {data, headers}
|
|
120
|
-
*/
|
|
121
|
-
async getPage(url) {
|
|
122
|
-
if (url.length <= this.maxKeyLength && this.memjs) {
|
|
123
|
-
try {
|
|
124
|
-
let headers = {};
|
|
125
|
-
const key = /^http/.test(url) ? url : `${this.prefix}${url}`;
|
|
126
|
-
const obj = await this.memjs.get(key);
|
|
127
|
-
const result = typeof obj === "object" ? obj?.value : obj;
|
|
128
|
-
if (!result) {
|
|
129
|
-
return null;
|
|
130
|
-
}
|
|
131
|
-
const memData = Buffer.from(result);
|
|
132
|
-
const start = memData.indexOf(Buffer.from("EXTRACT_HEADERS\r\n"));
|
|
133
|
-
const end = memData.indexOf(Buffer.from("\r\n\r\n"));
|
|
134
|
-
if (start < 0 || end < 0) {
|
|
135
|
-
return null;
|
|
136
|
-
}
|
|
137
|
-
const arr = memData.subarray(start, end).toString().split("\r\n");
|
|
138
|
-
arr.shift();
|
|
139
|
-
arr.map((row) => {
|
|
140
|
-
const [index, value] = row.split(":");
|
|
141
|
-
headers[index] = value;
|
|
142
|
-
});
|
|
143
|
-
headers = Object.assign(headers, this.specialHeaders);
|
|
144
|
-
const data = memData.subarray(end + 4);
|
|
145
|
-
return { headers, data };
|
|
146
|
-
} catch (e) {
|
|
147
|
-
if (this.debug) {
|
|
148
|
-
console.log(e);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
return null;
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Keys to lower case in Object
|
|
156
|
-
* @param o
|
|
157
|
-
* @private
|
|
158
|
-
*/
|
|
159
|
-
objectKeysToLower(o) {
|
|
160
|
-
return Object.keys(o).reduce((c, k) => (c[k.toLowerCase()] = o[k], c), {});
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
export { Memcached };
|
|
1
|
+
class u{constructor(t,e={"x-cache":"memcached-js"},s=160,r=0){this.prefix="",this.memjs=t,this.specialHeaders=e,this.maxKeyLength=s,this.debug=r,this.prefix=process.env.NODE_ENV!=="production"?"dev::":""}isClient(){return!!this.memjs}isConnectedServers(){if(!this.isClient())return!1;const t=this.memjs?.servers||[];for(const e of t)if(e.connected)return!0;return!1}async setValue(t,e,s){if((s||s===0)&&t.length<=this.maxKeyLength&&this.memjs)try{await this.memjs.set(`${this.prefix}${t}`,Buffer.from(e),{expires:s})}catch(r){this.debug&&console.log(r)}}async getValue(t){if(t.length<=this.maxKeyLength&&this.memjs)try{const e=await this.memjs.get(`${this.prefix}${t}`);return(typeof e=="object"?e?.value:e)||null}catch(e){this.debug&&console.log(e)}return null}async deleteValue(t){if(t.length<=this.maxKeyLength&&this.memjs)try{return this.memjs.delete(`${this.prefix}${t}`)}catch(e){this.debug&&console.log(e)}return!1}async setPage(t,e,s,r){if((r||r===0)&&t.length<=this.maxKeyLength&&this.memjs)try{e=this.objectKeysToLower(e),delete e["set-cookie"];const i=`EXTRACT_HEADERS\r
|
|
2
|
+
`+Object.entries(e).map(h=>h.join(":")).join(`\r
|
|
3
|
+
`)+`\r
|
|
4
|
+
\r
|
|
5
|
+
`,n=Buffer.concat([Buffer.from(i),Buffer.from(s)]),o=/^http/.test(t)?t:`${this.prefix}${t}`;await this.memjs.set(o,n,{expires:r})}catch(i){this.debug&&console.log(i)}}async getPage(t){if(t.length<=this.maxKeyLength&&this.memjs)try{let e={};const s=/^http/.test(t)?t:`${this.prefix}${t}`,r=await this.memjs.get(s),i=typeof r=="object"?r?.value:r;if(!i)return null;const n=Buffer.from(i),o=n.indexOf(Buffer.from(`EXTRACT_HEADERS\r
|
|
6
|
+
`)),h=n.indexOf(Buffer.from(`\r
|
|
7
|
+
\r
|
|
8
|
+
`));if(o<0||h<0)return null;const c=n.subarray(o,h).toString().split(`\r
|
|
9
|
+
`);c.shift(),c.map(m=>{const[f,l]=m.split(":");e[f]=l}),e=Object.assign(e,this.specialHeaders);const a=n.subarray(h+4);return{headers:e,data:a}}catch(e){this.debug&&console.log(e)}return null}objectKeysToLower(t){return Object.keys(t).reduce((e,s)=>(e[s.toLowerCase()]=t[s],e),{})}}export{u as Memcached};
|