dsh-unlag 1.8.1 → 1.8.2

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 (3) hide show
  1. package/client.js +69 -1
  2. package/index.js +7 -1
  3. package/package.json +1 -1
package/client.js CHANGED
@@ -212,10 +212,78 @@ window.__ModuleLoader__.load({
212
212
  return out;
213
213
  }
214
214
 
215
+ /**
216
+ * Whether a script has a `return` at the top level. A function body accepts one and a classic
217
+ * script does not, so that difference decides whether the document runs the body at all. Only
218
+ * depth zero counts: a return inside a callback, method or block is ordinary code, and reading
219
+ * one as a refusal held every real bundle back.
220
+ */
221
+ function hasTopLevelReturn(source) {
222
+ let depth = 0;
223
+ let index = 0;
224
+ let previous = '';
225
+ while (index < source.length) {
226
+ const here = source[index];
227
+ const next = source[index + 1];
228
+ if (here === '/' && next === '*') {
229
+ const end = source.indexOf('*/', index + 2);
230
+ index = end < 0 ? source.length : end + 2;
231
+ continue;
232
+ }
233
+ if (here === '/' && next === '/') {
234
+ const end = source.indexOf('\n', index + 2);
235
+ index = end < 0 ? source.length : end;
236
+ continue;
237
+ }
238
+ if (here === '"' || here === "'" || here === '`') {
239
+ index += 1;
240
+ while (index < source.length && source[index] !== here) {
241
+ if (source[index] === '\\') index += 1;
242
+ index += 1;
243
+ }
244
+ index += 1;
245
+ previous = 'a';
246
+ continue;
247
+ }
248
+ if (here === '/' && /(?:^|[(,=:[!&|?{};+\-*%<>~^])$/u.test(previous)) {
249
+ // A regular expression literal; its body is pattern text, not code.
250
+ index += 1;
251
+ let inClass = false;
252
+ while (index < source.length) {
253
+ const part = source[index];
254
+ if (part === '\\') index += 2;
255
+ else if (part === '[') {
256
+ inClass = true;
257
+ index += 1;
258
+ } else if (part === ']') {
259
+ inClass = false;
260
+ index += 1;
261
+ } else if (part === '/' && !inClass) {
262
+ index += 1;
263
+ break;
264
+ } else if (part === '\n') break;
265
+ else index += 1;
266
+ }
267
+ previous = 'a';
268
+ continue;
269
+ }
270
+ if (here === '(' || here === '[' || here === '{') depth += 1;
271
+ else if (here === ')' || here === ']' || here === '}') depth = Math.max(0, depth - 1);
272
+ else if (depth === 0 && here === 'r' && source.startsWith('return', index)) {
273
+ const before = source[index - 1] ?? '';
274
+ const after = source[index + 6] ?? '';
275
+ if (!/[\w$]/u.test(before) && !/[\w$]/u.test(after)) return true;
276
+ }
277
+ if (!/\s/u.test(here)) previous = here;
278
+ index += 1;
279
+ }
280
+ return false;
281
+ }
282
+
215
283
  function parsesAsClassicScript(body) {
216
284
  try {
217
285
  // A top-level return parses in a function body but not as a classic script.
218
- if (/(?:^|[;{}])\s*return\b/u.test(codeOnly(body))) return false;
286
+ if (hasTopLevelReturn(body)) return false;
219
287
  new Function(body); // eslint-disable-line no-new-func
220
288
  return true;
221
289
  } catch (error) {
package/index.js CHANGED
@@ -621,7 +621,13 @@ async function regroupedBundleAnswer(clientModules, graph, request) {
621
621
  parts.push(body)
622
622
  }
623
623
  if (parts.length === 0) return null
624
- const body = Buffer.concat(parts)
624
+ /**
625
+ * Each entry body is a script of its own and need not end where another can begin: an entry
626
+ * that ends in `})` followed by one that starts with an identifier is a syntax error, and a
627
+ * document would run neither. A line holding just `;` between them keeps every body its own
628
+ * statement whatever the two sides end and start with, and leaves each body itself untouched.
629
+ */
630
+ const body = Buffer.concat(parts.flatMap((part, index) => (index === parts.length - 1 ? [part] : [part, Buffer.from('\n;\n')])))
625
631
  if (!request.ids.every((id) => body.includes(id))) return null
626
632
  return { body, contentType: 'text/javascript; charset=utf-8', ids: request.ids }
627
633
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-unlag",
3
- "version": "1.8.1",
3
+ "version": "1.8.2",
4
4
  "type": "module",
5
5
  "description": "One press applies plugin edits right away and brings a laggy interface back; running turns, queued messages and unsent input are untouched, and nothing restarts.",
6
6
  "license": "MIT",