@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 CHANGED
@@ -1,49 +1,39 @@
1
1
  function stringify(value, replacer, spaces, escape) {
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 0x3c:
7
- return '\\u003c';
8
- case 0x3e:
9
- return '\\u003e';
10
- case 0x26:
11
- return '\\u0026';
12
- default:
13
- return c;
14
- }
15
- });
16
- }
17
- return json;
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
- const val = obj;
27
- const { escape, replacer, spaces, callbackName = 'callback' } = opts;
28
- let body = stringify(val, replacer, spaces, escape);
29
- let callback = req.query[callbackName];
30
- if (!res.get('Content-Type')) {
31
- res.set('X-Content-Type-Options', 'nosniff');
32
- res.set('Content-Type', 'application/json');
33
- }
34
- // jsonp
35
- if (typeof callback === 'string' && callback.length !== 0) {
36
- res.set('X-Content-Type-Options', 'nosniff');
37
- res.set('Content-Type', 'text/javascript');
38
- // restrict callback charset
39
- callback = callback.replace(/[^[\]\w$.]/g, '');
40
- // replace chars not allowed in JavaScript that are in JSON
41
- body = body.replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029');
42
- // the /**/ is a specific security mitigation for "Rosetta Flash JSONP abuse"
43
- // the typeof check is just to reduce client error noise
44
- body = `/**/ typeof ${callback} === 'function' && ${callback}(${body});`;
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.20",
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.21"
28
+ "@tinyhttp/app": "2.0.23"
29
29
  },
30
30
  "scripts": {
31
- "build": "rollup -c ../../build/defaultConfig.js"
31
+ "dev": "vite",
32
+ "build": "vite build",
33
+ "postbuild": "tsc --emitDeclarationOnly"
32
34
  }
33
35
  }
package/vite.config.ts ADDED
@@ -0,0 +1,4 @@
1
+ import { build } from '../../config/build'
2
+ import { dependencies } from './package.json'
3
+
4
+ export default build(dependencies)