dsh-unlag 1.9.52 → 1.9.54

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/index.js +44 -1
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1312,6 +1312,7 @@ function serveBundlesAtCurrentRevision(ctx) {
1312
1312
  const ask = async (url) => {
1313
1313
  let held = null
1314
1314
  let body
1315
+ const parts = []
1315
1316
  let failed = false
1316
1317
  if (url !== originalUrl) {
1317
1318
  try {
@@ -1329,10 +1330,51 @@ function serveBundlesAtCurrentRevision(ctx) {
1329
1330
  return realWriteHead.call(res, status, ...rest)
1330
1331
  }
1331
1332
  }
1333
+ /**
1334
+ * A held 404 can still be written to in pieces. Passing those pieces through would send the
1335
+ * implicit 200 headers Node writes on a first `write`, and the real answer could then never
1336
+ * be sent; so the pieces are kept with the body and the socket stays untouched.
1337
+ */
1338
+ const realWrite = res.write
1339
+ // A held answer still owes the caller the callbacks it passed: dropping one would leave a
1340
+ // handler that waits for the flush awaiting forever, and the fallback answer would never
1341
+ // run. They are answered on the next turn once the held body is collected.
1342
+ const answerCallback = (args) => {
1343
+ for (let at = args.length - 1; at >= 0; at -= 1) {
1344
+ if (typeof args[at] === 'function') {
1345
+ const done = args[at]
1346
+ try {
1347
+ process.nextTick(() => {
1348
+ try {
1349
+ done()
1350
+ } catch {
1351
+ /* the caller's own callback failed; the document still needs its script */
1352
+ }
1353
+ })
1354
+ } catch {
1355
+ /* no event loop to defer on; the answer matters more than the callback */
1356
+ }
1357
+ return
1358
+ }
1359
+ }
1360
+ }
1361
+ if (typeof realWrite === 'function') {
1362
+ res.write = (chunk, ...rest) => {
1363
+ if (held !== null) {
1364
+ if (typeof chunk === 'string' || Buffer.isBuffer(chunk)) parts.push(Buffer.from(chunk))
1365
+ answerCallback(rest)
1366
+ return true
1367
+ }
1368
+ return realWrite.call(res, chunk, ...rest)
1369
+ }
1370
+ }
1332
1371
  if (typeof realEnd === 'function') {
1333
1372
  res.end = (...args) => {
1334
1373
  if (held !== null) {
1335
- body = typeof args[0] === 'string' || Buffer.isBuffer(args[0]) ? args[0] : ''
1374
+ const last = args[0]
1375
+ if (typeof last === 'string' || Buffer.isBuffer(last)) parts.push(Buffer.from(last))
1376
+ body = parts.length === 0 ? '' : Buffer.concat(parts)
1377
+ answerCallback(args)
1336
1378
  return res
1337
1379
  }
1338
1380
  return realEnd.apply(res, args)
@@ -1346,6 +1388,7 @@ function serveBundlesAtCurrentRevision(ctx) {
1346
1388
  } finally {
1347
1389
  try {
1348
1390
  res.writeHead = realWriteHead
1391
+ if (realWrite !== undefined) res.write = realWrite
1349
1392
  res.end = realEnd
1350
1393
  } catch {
1351
1394
  /* the response object owns its own shape */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-unlag",
3
- "version": "1.9.52",
3
+ "version": "1.9.54",
4
4
  "type": "module",
5
5
  "description": "A press applies plugin edits right away and brings a laggy interface back; automatic reloading stays off unless you turn it on, so the press is the only trigger, and nothing restarts.",
6
6
  "license": "MIT",