sptc 0.0.31 → 0.0.32
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/httpServer.js +22 -5
- package/package.json +1 -1
- package/utils/base.js +14 -0
package/dist/httpServer.js
CHANGED
|
@@ -93,7 +93,7 @@ function CGI(req, res, option) {
|
|
|
93
93
|
}
|
|
94
94
|
|
|
95
95
|
function buildRequestContext(req, res, {srvDir, debug, env, isRouterMode}) {
|
|
96
|
-
const {fullname, pathname}=getRequestFile(req, srvDir)
|
|
96
|
+
const {fullname, pathname, query}=getRequestFile(req, srvDir)
|
|
97
97
|
const reqCtx={
|
|
98
98
|
$_RAW_REQUEST: req,
|
|
99
99
|
$_RAW_RESPONSE: res,
|
|
@@ -103,6 +103,8 @@ function buildRequestContext(req, res, {srvDir, debug, env, isRouterMode}) {
|
|
|
103
103
|
pathname,
|
|
104
104
|
},
|
|
105
105
|
$_WORKDIR: srvDir,
|
|
106
|
+
$_QUERY: query,
|
|
107
|
+
$_PATHNAME: pathname,
|
|
106
108
|
}
|
|
107
109
|
reqCtx.$_GLOBAL=reqCtx
|
|
108
110
|
|
|
@@ -112,6 +114,7 @@ function buildRequestContext(req, res, {srvDir, debug, env, isRouterMode}) {
|
|
|
112
114
|
responseHeaders: {'content-type': 'text/html; charset=utf8'},
|
|
113
115
|
error: null,
|
|
114
116
|
headerFlushed: false,
|
|
117
|
+
isSendFile: false,
|
|
115
118
|
|
|
116
119
|
is_debug: debug,
|
|
117
120
|
}
|
|
@@ -125,6 +128,13 @@ function buildRequestContext(req, res, {srvDir, debug, env, isRouterMode}) {
|
|
|
125
128
|
state.statusCode=code
|
|
126
129
|
if(text!==undefined) state.statusText=text
|
|
127
130
|
}
|
|
131
|
+
reqCtx.sendFile=(fn, headers)=>{
|
|
132
|
+
state.headerFlushed=true
|
|
133
|
+
state.isSendFile=true
|
|
134
|
+
const ws=fsResponse(fn, res, headers)
|
|
135
|
+
ws.on('error', e=>handler.onError(e))
|
|
136
|
+
ws.on('end', _=>res.end())
|
|
137
|
+
}
|
|
128
138
|
reqCtx.setResponseHeaders=headers=>{
|
|
129
139
|
Object.assign(state.responseHeaders, headers)
|
|
130
140
|
}
|
|
@@ -150,6 +160,7 @@ function buildRequestContext(req, res, {srvDir, debug, env, isRouterMode}) {
|
|
|
150
160
|
res.write(x)
|
|
151
161
|
},
|
|
152
162
|
end: _=>{
|
|
163
|
+
if(state.isSendFile) return;
|
|
153
164
|
flushHeader()
|
|
154
165
|
res.end()
|
|
155
166
|
},
|
|
@@ -164,11 +175,14 @@ function buildRequestContext(req, res, {srvDir, debug, env, isRouterMode}) {
|
|
|
164
175
|
return [reqCtx, handler]
|
|
165
176
|
}
|
|
166
177
|
function getRequestFile(req, srvDir) {
|
|
167
|
-
const _url=
|
|
168
|
-
const
|
|
178
|
+
const _url=req.url
|
|
179
|
+
const url=require('url')
|
|
180
|
+
const qs=require('querystring')
|
|
181
|
+
const {pathname, query}=url.parse(_url)
|
|
169
182
|
return {
|
|
170
183
|
fullname: safe_path(srvDir, pathname),
|
|
171
184
|
pathname,
|
|
185
|
+
query: qs.parse(query) || {},
|
|
172
186
|
}
|
|
173
187
|
}
|
|
174
188
|
const cacheStore={}
|
|
@@ -186,7 +200,7 @@ function createWithCacheStore() {
|
|
|
186
200
|
return withCache
|
|
187
201
|
}
|
|
188
202
|
|
|
189
|
-
function fsResponse(fn, res) {
|
|
203
|
+
function fsResponse(fn, res, headers={}) {
|
|
190
204
|
const fs=require('fs')
|
|
191
205
|
const ext=getExtension(fn)
|
|
192
206
|
res.writeHead(200, {
|
|
@@ -197,8 +211,11 @@ function fsResponse(fn, res) {
|
|
|
197
211
|
'.jpg': 'image/jpeg',
|
|
198
212
|
'.html': 'text/html',
|
|
199
213
|
})[ext] || 'application/octet-stream',
|
|
214
|
+
...headers,
|
|
200
215
|
})
|
|
201
|
-
fs.createReadStream(fn)
|
|
216
|
+
const ws=fs.createReadStream(fn)
|
|
217
|
+
ws.pipe(res)
|
|
218
|
+
return ws
|
|
202
219
|
}
|
|
203
220
|
|
|
204
221
|
function traverseDirectory(ref, dir, exts, debug) {
|
package/package.json
CHANGED
package/utils/base.js
CHANGED
|
@@ -75,6 +75,19 @@ function assert(x) {
|
|
|
75
75
|
if(!x) throw new Error('Uncaught AssertionError [ERR_ASSERTION]: '+x+' == true')
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
+
async function rs2Buffer(rs) {
|
|
79
|
+
const def=PromiseWithResolvers()
|
|
80
|
+
const buf=[]
|
|
81
|
+
rs.on('data', b=>buf.push(b))
|
|
82
|
+
rs.on('end', _=>{
|
|
83
|
+
def.resolve(Buffer.concat(buf))
|
|
84
|
+
})
|
|
85
|
+
rs.on('error', e=>{
|
|
86
|
+
def.reject(e)
|
|
87
|
+
})
|
|
88
|
+
return def.promise
|
|
89
|
+
}
|
|
90
|
+
|
|
78
91
|
module.exports={
|
|
79
92
|
readTextFile,
|
|
80
93
|
sleep,
|
|
@@ -87,4 +100,5 @@ module.exports={
|
|
|
87
100
|
getLocalIpv4Addresses,
|
|
88
101
|
PromiseWithResolvers,
|
|
89
102
|
assert,
|
|
103
|
+
rs2Buffer,
|
|
90
104
|
}
|