@tinyhttp/jsonp 2.0.20 → 2.0.22
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/dist/index.js +35 -45
- package/package.json +5 -3
- package/vite.config.ts +4 -0
package/dist/index.js
CHANGED
|
@@ -1,49 +1,39 @@
|
|
|
1
1
|
function stringify(value, replacer, spaces, escape) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
2
|
+
let json = replacer || spaces ? JSON.stringify(value, replacer, spaces) : JSON.stringify(value);
|
|
3
|
+
if (escape) {
|
|
4
|
+
json = json.replace(/[<>&]/g, (c) => {
|
|
5
|
+
switch (c.charCodeAt(0)) {
|
|
6
|
+
case 60:
|
|
7
|
+
return "\\u003c";
|
|
8
|
+
case 62:
|
|
9
|
+
return "\\u003e";
|
|
10
|
+
case 38:
|
|
11
|
+
return "\\u0026";
|
|
12
|
+
default:
|
|
13
|
+
return c;
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return json;
|
|
18
18
|
}
|
|
19
|
-
/**
|
|
20
|
-
* Send JSON response with JSONP callback support
|
|
21
|
-
* @param req Request
|
|
22
|
-
* @param res Response
|
|
23
|
-
* @param app App
|
|
24
|
-
*/
|
|
25
19
|
const jsonp = (req, res) => (obj, opts = {}) => {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
return res.send(body);
|
|
20
|
+
const val = obj;
|
|
21
|
+
const { escape, replacer, spaces, callbackName = "callback" } = opts;
|
|
22
|
+
let body = stringify(val, replacer, spaces, escape);
|
|
23
|
+
let callback = req.query[callbackName];
|
|
24
|
+
if (!res.get("Content-Type")) {
|
|
25
|
+
res.set("X-Content-Type-Options", "nosniff");
|
|
26
|
+
res.set("Content-Type", "application/json");
|
|
27
|
+
}
|
|
28
|
+
if (typeof callback === "string" && callback.length !== 0) {
|
|
29
|
+
res.set("X-Content-Type-Options", "nosniff");
|
|
30
|
+
res.set("Content-Type", "text/javascript");
|
|
31
|
+
callback = callback.replace(/[^[\]\w$.]/g, "");
|
|
32
|
+
body = body.replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029");
|
|
33
|
+
body = `/**/ typeof ${callback} === 'function' && ${callback}(${body});`;
|
|
34
|
+
}
|
|
35
|
+
return res.send(body);
|
|
36
|
+
};
|
|
37
|
+
export {
|
|
38
|
+
jsonp
|
|
47
39
|
};
|
|
48
|
-
|
|
49
|
-
export { jsonp };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tinyhttp/jsonp",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.22",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "JSONP response middleware",
|
|
6
6
|
"homepage": "https://tinyhttp.v1rtl.site",
|
|
@@ -25,9 +25,11 @@
|
|
|
25
25
|
"author": "v1rtl",
|
|
26
26
|
"license": "MIT",
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@tinyhttp/app": "2.0.
|
|
28
|
+
"@tinyhttp/app": "2.0.23"
|
|
29
29
|
},
|
|
30
30
|
"scripts": {
|
|
31
|
-
"
|
|
31
|
+
"dev": "vite",
|
|
32
|
+
"build": "vite build",
|
|
33
|
+
"postbuild": "tsc --emitDeclarationOnly"
|
|
32
34
|
}
|
|
33
35
|
}
|
package/vite.config.ts
ADDED