wapplr 1.0.77 → 1.0.78
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/server/static.js +92 -45
- package/package.json +1 -1
package/dist/server/static.js
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
|
|
3
4
|
Object.defineProperty(exports, "__esModule", {
|
|
4
5
|
value: true
|
|
5
6
|
});
|
|
6
7
|
exports.addCloseEventsForReadableStream = addCloseEventsForReadableStream;
|
|
7
8
|
exports["default"] = serveStatic;
|
|
8
9
|
exports.endStream = endStream;
|
|
9
|
-
var
|
|
10
|
+
var _regeneratorRuntime2 = _interopRequireDefault(require("@babel/runtime/helpers/regeneratorRuntime"));
|
|
11
|
+
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
|
12
|
+
var fs = require("node:fs/promises");
|
|
13
|
+
var fsSync = require("node:fs");
|
|
10
14
|
var path = require("path");
|
|
11
15
|
function destroy(stream) {
|
|
12
16
|
if (!(stream !== null && stream !== void 0 && stream.__destroyed)) {
|
|
@@ -81,49 +85,92 @@ function addCloseEventsForReadableStream(req, res, stream) {
|
|
|
81
85
|
});
|
|
82
86
|
}
|
|
83
87
|
function serveStatic(publicPath) {
|
|
84
|
-
return function
|
|
85
|
-
var
|
|
86
|
-
pathname
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
88
|
+
return /*#__PURE__*/function () {
|
|
89
|
+
var _staticMiddleware = (0, _asyncToGenerator2["default"])( /*#__PURE__*/(0, _regeneratorRuntime2["default"])().mark(function _callee(req, res, next) {
|
|
90
|
+
var parsedUrl, sanitizePath, pathname, parsedSanitizePath, ext, stream, data, stats;
|
|
91
|
+
return (0, _regeneratorRuntime2["default"])().wrap(function _callee$(_context) {
|
|
92
|
+
while (1) switch (_context.prev = _context.next) {
|
|
93
|
+
case 0:
|
|
94
|
+
parsedUrl = {
|
|
95
|
+
pathname: req.wappRequest.path
|
|
96
|
+
};
|
|
97
|
+
try {
|
|
98
|
+
parsedUrl = new URL(req.wappRequest.url);
|
|
99
|
+
} catch (e) {}
|
|
100
|
+
sanitizePath = path.normalize(parsedUrl.pathname).replace(/^(\.\.[\/\\])+/, "");
|
|
101
|
+
pathname = path.join(publicPath, sanitizePath);
|
|
102
|
+
parsedSanitizePath = path.parse(pathname);
|
|
103
|
+
ext = parsedSanitizePath.ext;
|
|
104
|
+
if (ext) {
|
|
105
|
+
_context.next = 10;
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
_context.next = 9;
|
|
109
|
+
return next();
|
|
110
|
+
case 9:
|
|
111
|
+
return _context.abrupt("return", _context.sent);
|
|
112
|
+
case 10:
|
|
113
|
+
_context.prev = 10;
|
|
114
|
+
_context.next = 13;
|
|
115
|
+
return fs.access(pathname);
|
|
116
|
+
case 13:
|
|
117
|
+
_context.next = 20;
|
|
118
|
+
break;
|
|
119
|
+
case 15:
|
|
120
|
+
_context.prev = 15;
|
|
121
|
+
_context.t0 = _context["catch"](10);
|
|
122
|
+
_context.next = 19;
|
|
123
|
+
return next();
|
|
124
|
+
case 19:
|
|
125
|
+
return _context.abrupt("return", _context.sent);
|
|
126
|
+
case 20:
|
|
127
|
+
_context.prev = 20;
|
|
128
|
+
_context.next = 23;
|
|
129
|
+
return fs.readFile(pathname);
|
|
130
|
+
case 23:
|
|
131
|
+
data = _context.sent;
|
|
132
|
+
_context.next = 26;
|
|
133
|
+
return fs.stat(pathname);
|
|
134
|
+
case 26:
|
|
135
|
+
stats = _context.sent;
|
|
136
|
+
res.wappResponse.status(200);
|
|
137
|
+
res.wappResponse.sendData = {
|
|
138
|
+
data: data,
|
|
139
|
+
stats: stats,
|
|
140
|
+
parsedPath: parsedSanitizePath
|
|
141
|
+
};
|
|
142
|
+
stream = fsSync.createReadStream(pathname);
|
|
143
|
+
addCloseEventsForReadableStream(req, res, stream);
|
|
144
|
+
stream.on("error", function onerror(err) {
|
|
145
|
+
endStream(req, res, stream);
|
|
146
|
+
res.wappResponse.status(err.statusCode || 500, err);
|
|
147
|
+
res.wapp.log(err, req, res);
|
|
148
|
+
next(err);
|
|
149
|
+
});
|
|
150
|
+
stream.on("open", function onopen() {
|
|
151
|
+
res.wapp.middleware.runSendMiddlewares(req, res, function next() {
|
|
152
|
+
res.wapp.log(req, res);
|
|
153
|
+
stream.pipe(res);
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
_context.next = 41;
|
|
157
|
+
break;
|
|
158
|
+
case 35:
|
|
159
|
+
_context.prev = 35;
|
|
160
|
+
_context.t1 = _context["catch"](20);
|
|
161
|
+
endStream(req, res, stream);
|
|
162
|
+
res.wappResponse.status(_context.t1.statusCode || 500, _context.t1);
|
|
163
|
+
res.wapp.log(_context.t1, req, res);
|
|
164
|
+
next(_context.t1);
|
|
165
|
+
case 41:
|
|
166
|
+
case "end":
|
|
167
|
+
return _context.stop();
|
|
168
|
+
}
|
|
169
|
+
}, _callee, null, [[10, 15], [20, 35]]);
|
|
170
|
+
}));
|
|
171
|
+
function staticMiddleware(_x, _x2, _x3) {
|
|
172
|
+
return _staticMiddleware.apply(this, arguments);
|
|
127
173
|
}
|
|
128
|
-
|
|
174
|
+
return staticMiddleware;
|
|
175
|
+
}();
|
|
129
176
|
}
|