@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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@srfnstack/spliffy",
3
- "version": "1.1.4",
3
+ "version": "1.1.5",
4
4
  "author": "snowbldr",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/narcolepticsnowman/spliffy",
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.writeStatus(`${res.statusCode} ${res.statusMessage}`)
101
- if (typeof res.onFlushHeaders === 'function') {
102
- res.onFlushHeaders(res)
103
- }
104
- for (const header of Object.keys(res.headers)) {
105
- if (Array.isArray(res.headers[header])) {
106
- for (const multiple of res.headers[header]) {
107
- res.writeHeader(header, multiple.toString())
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.writeArrayBuffer = res.write
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
- if (chunk instanceof Buffer) {
138
- result = res.writeArrayBuffer(toArrayBuffer(chunk))
139
- } else if (typeof chunk === 'string') {
140
- result = res.writeArrayBuffer(toArrayBuffer(Buffer.from(chunk, encoding || 'utf8')))
141
- } else {
142
- result = res.writeArrayBuffer(toArrayBuffer(Buffer.from(JSON.stringify(chunk), encoding || 'utf8')))
143
- }
144
- if (typeof cb === 'function') {
145
- cb()
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.flushHeaders()
185
- uwsEnd.call(res, body)
186
- res.writableEnded = true
187
- res.ended = true
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
 
@@ -50,6 +50,7 @@ export const validateMiddleware = (middleware) => {
50
50
  }
51
51
 
52
52
  export const validateMiddlewareArray = (arr) => {
53
+ if (!arr) return
53
54
  if (!Array.isArray(arr)) {
54
55
  throw new Error('middleware must be an array of functions')
55
56
  }
@@ -71,4 +71,4 @@ export function createStaticHandler (fullPath, contentType, cacheStatic, staticC
71
71
  }
72
72
  }
73
73
  }
74
- }
74
+ }