@srfnstack/spliffy 1.2.6 → 1.2.7

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/decorator.mjs +30 -26
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@srfnstack/spliffy",
3
- "version": "1.2.6",
3
+ "version": "1.2.7",
4
4
  "author": "snowbldr",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/SRFNStack/spliffy",
package/src/decorator.mjs CHANGED
@@ -125,29 +125,31 @@ export function decorateResponse (res, req, finalizeResponse, errorTransformer,
125
125
 
126
126
  res.uwsWrite = res.write
127
127
  res.write = (chunk, encoding, cb) => {
128
- try {
129
- res.streaming = true
130
- res.flushHeaders()
131
- let data
132
- if (chunk instanceof Buffer) {
133
- data = toArrayBuffer(chunk)
134
- } else if (typeof chunk === 'string') {
135
- data = toArrayBuffer(Buffer.from(chunk, encoding || 'utf8'))
136
- } else {
137
- data = toArrayBuffer(Buffer.from(JSON.stringify(chunk), encoding || 'utf8'))
138
- }
139
- const result = res.uwsWrite(data)
140
- if (typeof cb === 'function') {
141
- cb()
142
- }
143
- return result
144
- } catch (e) {
145
- if (typeof cb === 'function') {
146
- cb(e)
147
- } else {
148
- throw e
128
+ res.cork(() => {
129
+ try {
130
+ res.streaming = true
131
+ res.flushHeaders()
132
+ let data
133
+ if (chunk instanceof Buffer) {
134
+ data = toArrayBuffer(chunk)
135
+ } else if (typeof chunk === 'string') {
136
+ data = toArrayBuffer(Buffer.from(chunk, encoding || 'utf8'))
137
+ } else {
138
+ data = toArrayBuffer(Buffer.from(JSON.stringify(chunk), encoding || 'utf8'))
139
+ }
140
+ const result = res.uwsWrite(data)
141
+ if (typeof cb === 'function') {
142
+ cb()
143
+ }
144
+ return result
145
+ } catch (e) {
146
+ if (typeof cb === 'function') {
147
+ cb(e)
148
+ } else {
149
+ throw e
150
+ }
149
151
  }
150
- }
152
+ })
151
153
  }
152
154
  let outStream
153
155
  res.getWritable = () => {
@@ -177,10 +179,12 @@ export function decorateResponse (res, req, finalizeResponse, errorTransformer,
177
179
  }
178
180
  // provide writableEnded like node does, with slightly different behavior
179
181
  if (!res.writableEnded) {
180
- res.flushHeaders()
181
- uwsEnd.call(res, body)
182
- res.writableEnded = true
183
- res.ended = true
182
+ res.cork(() => {
183
+ res.flushHeaders()
184
+ uwsEnd.call(res, body)
185
+ res.writableEnded = true
186
+ res.ended = true
187
+ })
184
188
  }
185
189
  if (typeof res.onEnd === 'function') {
186
190
  res.onEnd()