@srfnstack/spliffy 1.1.4 → 1.1.5
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/package.json +1 -1
- package/src/decorator.mjs +36 -28
- package/src/handler.mjs +5 -2
- package/src/middleware.mjs +1 -0
- package/src/staticHandler.mjs +1 -1
package/package.json
CHANGED
package/src/decorator.mjs
CHANGED
|
@@ -97,19 +97,21 @@ export function decorateResponse (res, req, finalizeResponse, errorTransformer,
|
|
|
97
97
|
if (!res.statusCode) res.statusCode = httpStatusCodes.OK
|
|
98
98
|
if (!res.statusMessage) res.statusMessage = defaultStatusMessages[res.statusCode]
|
|
99
99
|
res.headersSent = true
|
|
100
|
-
res.
|
|
101
|
-
|
|
102
|
-
res.onFlushHeaders
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
100
|
+
res.cork(() => {
|
|
101
|
+
res.writeStatus(`${res.statusCode} ${res.statusMessage}`)
|
|
102
|
+
if (typeof res.onFlushHeaders === 'function') {
|
|
103
|
+
res.onFlushHeaders(res)
|
|
104
|
+
}
|
|
105
|
+
for (const header of Object.keys(res.headers)) {
|
|
106
|
+
if (Array.isArray(res.headers[header])) {
|
|
107
|
+
for (const multiple of res.headers[header]) {
|
|
108
|
+
res.writeHeader(header, multiple.toString())
|
|
109
|
+
}
|
|
110
|
+
} else {
|
|
111
|
+
res.writeHeader(header, res.headers[header].toString())
|
|
108
112
|
}
|
|
109
|
-
} else {
|
|
110
|
-
res.writeHeader(header, res.headers[header].toString())
|
|
111
113
|
}
|
|
112
|
-
}
|
|
114
|
+
})
|
|
113
115
|
}
|
|
114
116
|
res.writeHead = (status, headers) => {
|
|
115
117
|
res.statusCode = status
|
|
@@ -128,22 +130,26 @@ export function decorateResponse (res, req, finalizeResponse, errorTransformer,
|
|
|
128
130
|
return this
|
|
129
131
|
}
|
|
130
132
|
|
|
131
|
-
res.
|
|
133
|
+
res.uwsWrite = res.write
|
|
132
134
|
res.write = (chunk, encoding, cb) => {
|
|
133
135
|
try {
|
|
134
|
-
res.streaming = true
|
|
135
|
-
res.flushHeaders()
|
|
136
136
|
let result
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
137
|
+
res.cork(() => {
|
|
138
|
+
res.streaming = true
|
|
139
|
+
res.flushHeaders()
|
|
140
|
+
let data
|
|
141
|
+
if (chunk instanceof Buffer) {
|
|
142
|
+
data = toArrayBuffer(chunk)
|
|
143
|
+
} else if (typeof chunk === 'string') {
|
|
144
|
+
data = toArrayBuffer(Buffer.from(chunk, encoding || 'utf8'))
|
|
145
|
+
} else {
|
|
146
|
+
data = toArrayBuffer(Buffer.from(JSON.stringify(chunk), encoding || 'utf8'))
|
|
147
|
+
}
|
|
148
|
+
result = res.uwsWrite(data)
|
|
149
|
+
if (typeof cb === 'function') {
|
|
150
|
+
cb()
|
|
151
|
+
}
|
|
152
|
+
})
|
|
147
153
|
return result
|
|
148
154
|
} catch (e) {
|
|
149
155
|
if (typeof cb === 'function') {
|
|
@@ -181,10 +187,12 @@ export function decorateResponse (res, req, finalizeResponse, errorTransformer,
|
|
|
181
187
|
}
|
|
182
188
|
// provide writableEnded like node does, with slightly different behavior
|
|
183
189
|
if (!res.writableEnded) {
|
|
184
|
-
res.
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
190
|
+
res.cork(() => {
|
|
191
|
+
res.flushHeaders()
|
|
192
|
+
uwsEnd.call(res, body)
|
|
193
|
+
res.writableEnded = true
|
|
194
|
+
res.ended = true
|
|
195
|
+
})
|
|
188
196
|
}
|
|
189
197
|
if (typeof res.onEnd === 'function') {
|
|
190
198
|
res.onEnd()
|
package/src/handler.mjs
CHANGED
|
@@ -56,7 +56,6 @@ const end = (res, defaultStatusCode, statusCodeOverride, body) => {
|
|
|
56
56
|
if (body instanceof Readable || res.streaming) {
|
|
57
57
|
res.streaming = true
|
|
58
58
|
if (body instanceof Readable) {
|
|
59
|
-
res.flushHeaders()
|
|
60
59
|
pipeResponse(res, body)
|
|
61
60
|
}
|
|
62
61
|
// handler is responsible for ending the response if they are streaming
|
|
@@ -65,10 +64,14 @@ const end = (res, defaultStatusCode, statusCodeOverride, body) => {
|
|
|
65
64
|
}
|
|
66
65
|
}
|
|
67
66
|
|
|
67
|
+
const ipv6CompressRegex = /\b:?(?:0+:?){2,}/g
|
|
68
|
+
|
|
69
|
+
const compressIpv6 = ip => ip && ip.includes(':') ? ip.replaceAll(ipv6CompressRegex, '::') : ip
|
|
70
|
+
|
|
68
71
|
const writeAccess = function (req, res) {
|
|
69
72
|
const start = new Date().getTime()
|
|
70
73
|
return () => {
|
|
71
|
-
log.access(req.remoteAddress, res.proxiedRemoteAddress || '', res.statusCode, req.method, req.url, new Date().getTime() - start + 'ms')
|
|
74
|
+
log.access(compressIpv6(req.remoteAddress), compressIpv6(res.proxiedRemoteAddress) || '', res.statusCode, req.method, req.url, new Date().getTime() - start + 'ms')
|
|
72
75
|
}
|
|
73
76
|
}
|
|
74
77
|
|
package/src/middleware.mjs
CHANGED
package/src/staticHandler.mjs
CHANGED