goatly 0.1.1 → 0.1.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.
- package/README.md +22 -1
- package/bin/goatly.js +8 -0
- package/dist/node/bootstrap.js +33 -3
- package/dist/node/build/index.js +16 -5
- package/dist/node/build/memory-governor.js +3 -1
- package/dist/node/build/page-files.js +133 -1
- package/dist/node/build/query-worker.js +187 -97
- package/dist/node/build/query-workers.js +42 -11
- package/dist/node/build/render-thread.js +103 -0
- package/dist/node/build/render-worker.js +49 -18
- package/dist/node/build/timing-breakdown.js +66 -5
- package/dist/node/build/work-clock.js +75 -0
- package/dist/node/build/worker-pool.js +8 -2
- package/dist/node/develop/index.js +1 -1
- package/dist/node/utils/cpus.js +104 -0
- package/dist/node/utils/env.js +1 -0
- package/dist/node/utils/memory-signals.js +42 -11
- package/dist/node/utils/memory.js +305 -48
- package/dist/node/vite/plugin.js +4 -2
- package/dist/node/vite/transforms.js +39 -8
- package/dist/runtime/client.js +1 -1
- package/package.json +2 -2
- package/types/node.d.ts +11 -2
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { parseVmStatAvailableMb, sampleMemoryOnce } from "./memory-signals.js";
|
|
2
|
+
import { PerformanceObserver, constants, performance } from "node:perf_hooks";
|
|
2
3
|
import os from "node:os";
|
|
3
4
|
import v8 from "node:v8";
|
|
4
5
|
import vm from "node:vm";
|
|
@@ -291,95 +292,351 @@ function heapUsedMb(runtime = RUNTIME) {
|
|
|
291
292
|
}
|
|
292
293
|
return v8.getHeapStatistics().used_heap_size / MB;
|
|
293
294
|
}
|
|
295
|
+
/** batches between two releases of a query worker's caches, at least and at most */
|
|
296
|
+
const RELEASE_SPACING = 8;
|
|
297
|
+
const MAX_RELEASE_SPACING = 64;
|
|
298
|
+
/** a release freed little when it freed less than this share of the heap */
|
|
299
|
+
const FUTILE_RELEASE = .1;
|
|
294
300
|
/**
|
|
295
|
-
*
|
|
296
|
-
*
|
|
297
|
-
* `futile` – the last release freed little.
|
|
301
|
+
* a release didn't last when the heap is back at this share of what it was: the worker needs what
|
|
302
|
+
* was dropped. It lasted when the heap is still below that this many batches later.
|
|
298
303
|
*/
|
|
299
|
-
|
|
300
|
-
|
|
304
|
+
const REBOUND = .9;
|
|
305
|
+
const REBOUND_WINDOW = 64;
|
|
306
|
+
/**
|
|
307
|
+
* the most of a worker's time its heap guard spends on the collections it forces for a budget –
|
|
308
|
+
* and what they may take in any case (ms: a worker that just started has no time to take a share
|
|
309
|
+
* of)
|
|
310
|
+
*/
|
|
311
|
+
const FORCED_GC_SHARE = .05;
|
|
312
|
+
const FORCED_GC_GRACE_MS = 250;
|
|
313
|
+
/**
|
|
314
|
+
* a budget is a worker's heap limit, for its heap guard, when the limit is less than this multiple
|
|
315
|
+
* of it: then there is no room above the budget to keep caches in
|
|
316
|
+
*/
|
|
317
|
+
const BUDGET_IS_LIMIT = 1.25;
|
|
318
|
+
/** the share of a heap limit up to which a worker may keep the caches it needs */
|
|
319
|
+
const NEAR_HEAP_LIMIT = .7;
|
|
320
|
+
/**
|
|
321
|
+
* The heap in use (a share of the budget) at which a query worker's heap guard collects garbage
|
|
322
|
+
* for a budget below its heap limit, `live` being what the last collection left: at 70%, and never
|
|
323
|
+
* before garbage could be 15% of the budget or a quarter of the live heap. Garbage is what a
|
|
324
|
+
* collection frees, and a collection costs time by the live heap: with less room than that, a heap
|
|
325
|
+
* that is nearly all live data would be collected over and over for the little garbage a few pages
|
|
326
|
+
* leave.
|
|
327
|
+
*/
|
|
328
|
+
function heapCollectAt(live) {
|
|
329
|
+
return Math.max(.7, live + Math.max(.15, .25 * live));
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* The live heap (a share of the budget) at which a query worker drops its caches: 70%, or 15% more
|
|
333
|
+
* than the last release left (`floor`), at most 85%. With a budget below the heap limit (`ceiling`:
|
|
334
|
+
* the limit as a multiple of the budget), a worker whose heap came back after a release needs what
|
|
335
|
+
* was dropped (`need`): its caches stay until the heap is 15% above that – up to 70% of the heap
|
|
336
|
+
* limit, or `hold` (a share of the budget) when that is less.
|
|
337
|
+
*/
|
|
338
|
+
function heapReleaseAt(floor, need = 0, ceiling = 1, hold = 1e400) {
|
|
339
|
+
const full = Math.min(.85, Math.max(.7, floor + .15));
|
|
340
|
+
if (ceiling < BUDGET_IS_LIMIT) return full;
|
|
341
|
+
return Math.max(full, Math.min(need + .15, NEAR_HEAP_LIMIT * ceiling, hold));
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* The heap guard of a heap limit (see createHeapGuard): at 70% of the limit in use – live + 15%
|
|
345
|
+
* when the last release left more, at most 85% – at most every `spacing` batches (8, doubled up to
|
|
346
|
+
* 64 while releases free less than 10%), at 85% at once (unless the last release freed little). A
|
|
347
|
+
* GC runs first: when it frees enough, the pressure was garbage and the caches stay (checks are
|
|
348
|
+
* spaced out further).
|
|
349
|
+
*/
|
|
350
|
+
function createLimitGuard(o) {
|
|
351
|
+
let since = 1e400;
|
|
352
|
+
let live = 0;
|
|
353
|
+
let spacing = RELEASE_SPACING;
|
|
354
|
+
let futile = false;
|
|
355
|
+
return {
|
|
356
|
+
/** forgets what the last checks found: the next batch is checked */
|
|
357
|
+
reset() {
|
|
358
|
+
since = 1e400;
|
|
359
|
+
live = 0;
|
|
360
|
+
spacing = RELEASE_SPACING;
|
|
361
|
+
futile = false;
|
|
362
|
+
},
|
|
363
|
+
/** what it did: dropped the caches, or collected garbage (the caches stay) */
|
|
364
|
+
afterBatch() {
|
|
365
|
+
since++;
|
|
366
|
+
let used = o.ratio();
|
|
367
|
+
const threshold = Math.min(.85, Math.max(.7, live + .15));
|
|
368
|
+
if (!(used >= threshold && (since >= spacing || used >= .85 && !futile))) return void 0;
|
|
369
|
+
o.gc(false);
|
|
370
|
+
used = o.ratio();
|
|
371
|
+
since = 0;
|
|
372
|
+
if (used < threshold) {
|
|
373
|
+
live = used;
|
|
374
|
+
spacing = Math.min(MAX_RELEASE_SPACING, spacing * 2);
|
|
375
|
+
return "collected";
|
|
376
|
+
}
|
|
377
|
+
o.release();
|
|
378
|
+
o.gc(true);
|
|
379
|
+
const after = o.ratio();
|
|
380
|
+
futile = used - after < FUTILE_RELEASE;
|
|
381
|
+
spacing = futile ? Math.min(MAX_RELEASE_SPACING, spacing * 2) : RELEASE_SPACING;
|
|
382
|
+
live = after;
|
|
383
|
+
return "released";
|
|
384
|
+
}
|
|
385
|
+
};
|
|
301
386
|
}
|
|
302
387
|
/**
|
|
303
388
|
* Drops what a query worker only keeps for reuse (`release`: query engine caches, plugin cache
|
|
304
|
-
* entries, decoded nodes) when
|
|
305
|
-
* (nothing in flight)
|
|
306
|
-
*
|
|
307
|
-
*
|
|
308
|
-
*
|
|
309
|
-
*
|
|
310
|
-
*
|
|
311
|
-
*
|
|
389
|
+
* entries, decoded nodes) when live data fills its heap, then collects garbage. Called between
|
|
390
|
+
* batches (nothing in flight). Those caches are bounded by entries, not bytes: large nodes or many
|
|
391
|
+
* views of a large type could otherwise run a worker out of memory.
|
|
392
|
+
*
|
|
393
|
+
* `ratio` measures the heap against the worker's budget (heapRatio), `ceiling` tells its heap limit
|
|
394
|
+
* as a multiple of that. Two rules apply:
|
|
395
|
+
*
|
|
396
|
+
* - **The heap limit** (createLimitGuard), where V8 itself would run out of memory: at 70–85% of
|
|
397
|
+
* it the caches are dropped, at most every 8 batches while that frees memory. It is the only rule
|
|
398
|
+
* when the budget is the limit, or close to it (less than 25% below), and on Bun, whose threads
|
|
399
|
+
* have no limit but the budget (`ceiling` 1).
|
|
400
|
+
* - **A budget below the limit** – a worker's share of the free memory – is not where the worker
|
|
401
|
+
* runs out of memory, and what the guard does for it is paid by the live heap: a forced
|
|
402
|
+
* collection takes about a third of a second per GB, and what a release dropped is built again
|
|
403
|
+
* by the next queries that need it. So for a budget:
|
|
404
|
+
* - The heap in use includes garbage, and only a collection tells how much. V8's own collections
|
|
405
|
+
* tell an upper bound (`collected`); the guard forces one when garbage could be a good part of
|
|
406
|
+
* the heap (heapCollectAt) – not for the garbage of a few rendered pages – or when a release
|
|
407
|
+
* is due, and not while forced collections have taken more than 5% of the worker's time.
|
|
408
|
+
* - The caches are dropped when the live heap reaches heapReleaseAt, at most every 8 batches. A
|
|
409
|
+
* release that freed less than 10% had nothing to free: the next one waits twice as long (up
|
|
410
|
+
* to 64 batches). One after which the heap is back where it was dropped what the worker needs:
|
|
411
|
+
* the next one waits twice as long, too, and only happens once the heap is 15% above what it
|
|
412
|
+
* came back to (at most 70% of the heap limit, or `hold`). A worker whose data needs more than
|
|
413
|
+
* its budget so stays above it instead of building its caches again every few batches – the
|
|
414
|
+
* adaptive pool sees its heap, and gives it a larger budget or retires a worker.
|
|
415
|
+
*
|
|
416
|
+
* `rescale` follows a budget that changed. On Bun without a budget `ratio` is 0 and nothing is
|
|
417
|
+
* released.
|
|
312
418
|
* (GOATLY_HEAP_GUARD=off|always: internal, for tests)
|
|
313
419
|
*/
|
|
314
420
|
function createHeapGuard(options) {
|
|
315
421
|
const mode = (options.env ?? process.env).GOATLY_HEAP_GUARD;
|
|
316
422
|
const ratio = options.ratio ?? heapRatio;
|
|
317
423
|
const gc = options.gc ?? ((full) => collectGarbage(full));
|
|
424
|
+
const now = options.now ?? (() => performance.now());
|
|
425
|
+
const started = now();
|
|
426
|
+
/** batches since the last release */
|
|
318
427
|
let since = 1e400;
|
|
428
|
+
/** the heap in use after the last collection known of (forced, or V8's own) */
|
|
319
429
|
let live = 0;
|
|
320
|
-
let
|
|
321
|
-
|
|
430
|
+
let known = false;
|
|
431
|
+
/** what the last release left, and what the heap came back to after one */
|
|
432
|
+
let floor = 0;
|
|
433
|
+
let need = 0;
|
|
434
|
+
let spacing = RELEASE_SPACING;
|
|
435
|
+
/** the last release, while it is not known whether it lasted: the live heap before it */
|
|
436
|
+
let dropped;
|
|
322
437
|
let releases = 0;
|
|
323
|
-
let checked = false;
|
|
324
438
|
let checks = 0;
|
|
439
|
+
let skipped = 0;
|
|
440
|
+
let forcedMs = 0;
|
|
441
|
+
const ceiling = () => Math.max(1, options.ceiling?.() ?? 1);
|
|
442
|
+
const releaseAt = () => heapReleaseAt(floor, need, ceiling(), options.hold?.());
|
|
443
|
+
const collect = (full) => {
|
|
444
|
+
const start = now();
|
|
445
|
+
gc(full);
|
|
446
|
+
forcedMs += now() - start;
|
|
447
|
+
checks++;
|
|
448
|
+
};
|
|
449
|
+
const release = () => {
|
|
450
|
+
options.release();
|
|
451
|
+
releases++;
|
|
452
|
+
};
|
|
453
|
+
/**
|
|
454
|
+
* the rule of the heap limit: against the limit itself (`limitScale`: the limit as a multiple of
|
|
455
|
+
* the budget), or against a budget that is the limit (1)
|
|
456
|
+
*/
|
|
457
|
+
let limitScale = 1;
|
|
458
|
+
const limit = createLimitGuard({
|
|
459
|
+
ratio: () => ratio() / limitScale,
|
|
460
|
+
release,
|
|
461
|
+
gc: collect
|
|
462
|
+
});
|
|
463
|
+
/** how far a budget that is the limit was lowered since the limit's rule last looked */
|
|
464
|
+
let lowered = 1;
|
|
465
|
+
/** the release lasted: the heap stayed below what it was */
|
|
466
|
+
const lasted = () => {
|
|
467
|
+
dropped = void 0;
|
|
468
|
+
need = 0;
|
|
469
|
+
spacing = RELEASE_SPACING;
|
|
470
|
+
};
|
|
471
|
+
/** `exact`: measured right after a collection (not an upper bound, with the garbage since) */
|
|
472
|
+
const learn = (value, exact) => {
|
|
473
|
+
live = value;
|
|
474
|
+
known = true;
|
|
475
|
+
if (!exact || dropped === void 0) return;
|
|
476
|
+
if (value < REBOUND * dropped) {
|
|
477
|
+
if (since >= REBOUND_WINDOW) lasted();
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
need = Math.max(need, value);
|
|
481
|
+
spacing = Math.min(MAX_RELEASE_SPACING, spacing * 2);
|
|
482
|
+
dropped = void 0;
|
|
483
|
+
};
|
|
325
484
|
return {
|
|
326
485
|
get releases() {
|
|
327
486
|
return releases;
|
|
328
487
|
},
|
|
329
|
-
/**
|
|
488
|
+
/** the collections it forced so far */
|
|
330
489
|
get checks() {
|
|
331
490
|
return checks;
|
|
332
491
|
},
|
|
333
|
-
/**
|
|
492
|
+
/** checks left out because forced collections had taken their share of the time */
|
|
493
|
+
get skipped() {
|
|
494
|
+
return skipped;
|
|
495
|
+
},
|
|
496
|
+
/** ms spent in forced collections */
|
|
497
|
+
get forcedMs() {
|
|
498
|
+
return forcedMs;
|
|
499
|
+
},
|
|
500
|
+
/** the share of the heap in use after the last collection known of, 0: none yet */
|
|
334
501
|
get live() {
|
|
335
|
-
return
|
|
502
|
+
return known ? live : 0;
|
|
336
503
|
},
|
|
337
504
|
/**
|
|
338
|
-
*
|
|
339
|
-
* next batch
|
|
505
|
+
* The budget it measures against changed by `factor` (old / new): what it knows about the heap
|
|
506
|
+
* stays true. A lower budget applies from the next batch – what holds a release back still
|
|
507
|
+
* does: the batches since the last one, the time forced collections took, and what the worker
|
|
508
|
+
* showed it needs (a release that didn't last is not tried again for a lower budget). A budget
|
|
509
|
+
* that is the heap limit is checked at once when it was lowered by a tenth.
|
|
340
510
|
*/
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
live
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
511
|
+
rescale(factor) {
|
|
512
|
+
if (!(factor > 0) || factor === 1) return;
|
|
513
|
+
live *= factor;
|
|
514
|
+
floor *= factor;
|
|
515
|
+
need *= factor;
|
|
516
|
+
if (dropped !== void 0) dropped *= factor;
|
|
517
|
+
lowered = Math.max(1, lowered * factor);
|
|
347
518
|
},
|
|
348
519
|
afterBatch() {
|
|
349
520
|
if (mode === "off") return;
|
|
350
521
|
since++;
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
522
|
+
if (mode === "always") {
|
|
523
|
+
release();
|
|
524
|
+
collect(true);
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
const scale = ceiling() < BUDGET_IS_LIMIT ? 1 : ceiling();
|
|
528
|
+
if (scale !== limitScale) limit.reset();
|
|
529
|
+
else if (scale === 1 && lowered >= 1 / .9) {
|
|
530
|
+
limit.reset();
|
|
531
|
+
lowered = 1;
|
|
532
|
+
}
|
|
533
|
+
limitScale = scale;
|
|
534
|
+
const acted = limit.afterBatch();
|
|
535
|
+
if (acted || scale === 1) {
|
|
536
|
+
if (!acted) return;
|
|
537
|
+
lowered = 1;
|
|
538
|
+
const left = ratio();
|
|
539
|
+
if (acted === "released") {
|
|
540
|
+
floor = left;
|
|
541
|
+
dropped = void 0;
|
|
542
|
+
since = 0;
|
|
363
543
|
}
|
|
544
|
+
live = left;
|
|
545
|
+
known = true;
|
|
546
|
+
return;
|
|
547
|
+
}
|
|
548
|
+
let used = ratio();
|
|
549
|
+
if (dropped !== void 0 && since >= REBOUND_WINDOW && used < REBOUND * dropped) lasted();
|
|
550
|
+
const seen = options.collected?.();
|
|
551
|
+
if (seen !== void 0) learn(Math.min(seen, used), false);
|
|
552
|
+
const garbage = used >= heapCollectAt(live);
|
|
553
|
+
const due = known && live >= releaseAt() && since >= spacing;
|
|
554
|
+
if (!garbage && !due) return;
|
|
555
|
+
if (forcedMs > FORCED_GC_GRACE_MS + FORCED_GC_SHARE * (now() - started)) {
|
|
556
|
+
skipped++;
|
|
557
|
+
return;
|
|
364
558
|
}
|
|
365
|
-
|
|
366
|
-
|
|
559
|
+
collect(false);
|
|
560
|
+
used = ratio();
|
|
561
|
+
learn(used, true);
|
|
562
|
+
if (used < releaseAt() || since < spacing) return;
|
|
563
|
+
release();
|
|
564
|
+
collect(true);
|
|
367
565
|
const after = ratio();
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
566
|
+
if (used - after < FUTILE_RELEASE) {
|
|
567
|
+
spacing = Math.min(MAX_RELEASE_SPACING, spacing * 2);
|
|
568
|
+
dropped = void 0;
|
|
569
|
+
} else dropped = used;
|
|
570
|
+
floor = live = after;
|
|
571
|
+
known = true;
|
|
371
572
|
since = 0;
|
|
372
|
-
checked = true;
|
|
373
|
-
checks++;
|
|
374
|
-
releases++;
|
|
375
573
|
}
|
|
376
574
|
};
|
|
377
575
|
}
|
|
378
576
|
/**
|
|
577
|
+
* Watches the garbage collections V8 runs on its own in this thread (perf_hooks' "gc" entries).
|
|
578
|
+
* Node reports them when the event loop turns, in the order they ran – so often after the work
|
|
579
|
+
* they interrupted. The collections `collectGarbage` forces are left out (by their flag – which a
|
|
580
|
+
* full one doesn't have – and by when they ran): who forces them measures them. Without the
|
|
581
|
+
* entries (Bun) nothing is reported.
|
|
582
|
+
*/
|
|
583
|
+
function watchGarbageCollections(onCollection) {
|
|
584
|
+
let observer;
|
|
585
|
+
const take = (entries) => {
|
|
586
|
+
for (const entry of entries) {
|
|
587
|
+
const detail = entry.detail;
|
|
588
|
+
if (!detail) continue;
|
|
589
|
+
if (((detail.flags ?? 0) & constants.NODE_PERFORMANCE_GC_FLAGS_FORCED) !== 0) continue;
|
|
590
|
+
const start = entry.startTime;
|
|
591
|
+
if (forcedCollections.some(([from, to]) => start >= from && start <= to)) continue;
|
|
592
|
+
onCollection({
|
|
593
|
+
start: entry.startTime,
|
|
594
|
+
ms: entry.duration,
|
|
595
|
+
major: detail.kind === constants.NODE_PERFORMANCE_GC_MAJOR
|
|
596
|
+
});
|
|
597
|
+
}
|
|
598
|
+
};
|
|
599
|
+
try {
|
|
600
|
+
if (!isBun) {
|
|
601
|
+
observer = new PerformanceObserver((list) => take(list.getEntries()));
|
|
602
|
+
observer.observe({ entryTypes: ["gc"] });
|
|
603
|
+
}
|
|
604
|
+
} catch {
|
|
605
|
+
observer = void 0;
|
|
606
|
+
}
|
|
607
|
+
return { close() {
|
|
608
|
+
observer?.disconnect();
|
|
609
|
+
observer = void 0;
|
|
610
|
+
} };
|
|
611
|
+
}
|
|
612
|
+
/**
|
|
613
|
+
* The CPU time (ms) this thread used so far (`process.threadCpuUsage`), or undefined where the
|
|
614
|
+
* runtime doesn't measure it per thread
|
|
615
|
+
*/
|
|
616
|
+
function threadCpuMs() {
|
|
617
|
+
try {
|
|
618
|
+
const usage = process.threadCpuUsage?.();
|
|
619
|
+
return usage ? (usage.user + usage.system) / 1e3 : void 0;
|
|
620
|
+
} catch {
|
|
621
|
+
return;
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
/** when this thread last forced collections (performance.now(): from, to), see collectGarbage */
|
|
625
|
+
const forcedCollections = [];
|
|
626
|
+
/**
|
|
379
627
|
* A full, memory-reducing GC (releases heap pages, not only objects) – e.g. before workers start.
|
|
380
628
|
* `full = false`: a plain major GC.
|
|
381
629
|
*/
|
|
382
630
|
function collectGarbage(full = true) {
|
|
631
|
+
const from = performance.now();
|
|
632
|
+
try {
|
|
633
|
+
collect(full);
|
|
634
|
+
} finally {
|
|
635
|
+
forcedCollections.push([from, performance.now()]);
|
|
636
|
+
if (forcedCollections.length > 32) forcedCollections.shift();
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
function collect(full) {
|
|
383
640
|
try {
|
|
384
641
|
if (isBun) {
|
|
385
642
|
globalThis.Bun?.gc?.(true);
|
|
@@ -397,4 +654,4 @@ function collectGarbage(full = true) {
|
|
|
397
654
|
});
|
|
398
655
|
} catch {}
|
|
399
656
|
}
|
|
400
|
-
export { RUNTIME, WASM_QUERY_WORKER_MB, availableMemoryMb, collectGarbage, configureBuildHeap, createHeapGuard, heapRatio, heapText, heapUsedMb, maxOldSpaceSizeArg, nodeDefaultHeapMb, outOfMemoryHint, parseVmStatAvailableMb, planQueryWorkers, planRenderWorkers, planWorkerHeap,
|
|
657
|
+
export { RUNTIME, WASM_QUERY_WORKER_MB, availableMemoryMb, collectGarbage, configureBuildHeap, createHeapGuard, heapCollectAt, heapRatio, heapReleaseAt, heapText, heapUsedMb, maxOldSpaceSizeArg, nodeDefaultHeapMb, outOfMemoryHint, parseVmStatAvailableMb, planQueryWorkers, planRenderWorkers, planWorkerHeap, threadCpuMs, totalMemoryMb, userHeapLimit, watchGarbageCollections, workerBudgetMb, workerLimitMb };
|
package/dist/node/vite/plugin.js
CHANGED
|
@@ -139,6 +139,7 @@ function goatlyVitePlugin(ctx) {
|
|
|
139
139
|
const isUserFile = (file) => isSourceFile(file, pluginDirs);
|
|
140
140
|
const apiFilePattern = /\/gatsby-(browser|ssr)\.(js|jsx|cjs|mjs)$/;
|
|
141
141
|
let isServe = false;
|
|
142
|
+
let defines = {};
|
|
142
143
|
return {
|
|
143
144
|
name: "goatly",
|
|
144
145
|
enforce: "pre",
|
|
@@ -148,6 +149,7 @@ function goatlyVitePlugin(ctx) {
|
|
|
148
149
|
},
|
|
149
150
|
configResolved(config) {
|
|
150
151
|
isServe = config.command === "serve";
|
|
152
|
+
defines = Object.fromEntries(Object.entries(config.define ?? {}).filter(([, v]) => typeof v === "string"));
|
|
151
153
|
},
|
|
152
154
|
resolveId(id) {
|
|
153
155
|
if (id === VIRTUAL.renderer) return slash(getRenderer().runtime.shared);
|
|
@@ -193,13 +195,13 @@ function goatlyVitePlugin(ctx) {
|
|
|
193
195
|
const file = id.split("?")[0];
|
|
194
196
|
let result = null;
|
|
195
197
|
if (isServe && !file.includes("/.vite/") && !file.includes("/.cache/vite/") && isCommonJsSource(code)) {
|
|
196
|
-
const converted = transformCommonJsApiFile(code, file);
|
|
198
|
+
const converted = transformCommonJsApiFile(code, file, defines);
|
|
197
199
|
if (converted != null) {
|
|
198
200
|
code = converted;
|
|
199
201
|
result = { code };
|
|
200
202
|
}
|
|
201
203
|
} else if (isServe && (isUserFile(file) || apiFilePattern.test(file)) && !file.includes("/.vite/") && !file.includes("/.cache/vite/")) {
|
|
202
|
-
const r = transformRequireInEsm(code, id);
|
|
204
|
+
const r = transformRequireInEsm(code, id, defines);
|
|
203
205
|
if (r) {
|
|
204
206
|
code = r.code;
|
|
205
207
|
result = r;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { extractFromSource } from "../query/extract.js";
|
|
2
2
|
import { getStaticExports } from "../plugins/static-exports.js";
|
|
3
3
|
import { parseSync, walk } from "../utils/parser.js";
|
|
4
|
+
import { slash } from "../utils/paths.js";
|
|
4
5
|
import path from "node:path";
|
|
5
6
|
import { MagicString } from "magic-string";
|
|
6
7
|
// src/node/vite/transforms.ts
|
|
@@ -89,17 +90,22 @@ function transformHtmlBodyJsx(code, id, headTagsModule) {
|
|
|
89
90
|
})
|
|
90
91
|
};
|
|
91
92
|
}
|
|
92
|
-
/**
|
|
93
|
-
|
|
93
|
+
/**
|
|
94
|
+
* What `require(x)` returns, given `import * as ns from x` (CommonJS, ESM and asset modules).
|
|
95
|
+
* `"module.exports"` is not a named export: Node 24+ adds it to the namespace of every CommonJS
|
|
96
|
+
* module (the same value as `default`), so counting it made a `module.exports = function` with
|
|
97
|
+
* `__esModule` (Babel's `@babel/runtime` helpers) return the namespace instead of the function.
|
|
98
|
+
*/
|
|
99
|
+
const REQUIRE_INTEROP = `function __goatly_interop(ns) { if (ns && ns.default !== undefined && Object.keys(ns).filter((k) => k !== "default" && k !== "__esModule" && k !== "module.exports").length === 0) return ns.default; return ns && ns.__esModule ? ns : (ns && ns.default && typeof ns.default === "object" ? Object.assign({}, ns.default, ns) : ns) }`;
|
|
94
100
|
/**
|
|
95
101
|
* Converts a CommonJS gatsby-browser / gatsby-ssr file (`exports.onRouteUpdate = ...`,
|
|
96
102
|
* `require("./global.css")`) into ESM so Vite can serve it unbundled in development. Export names
|
|
97
103
|
* found statically (`exports.a = `, `module.exports = { a, b }`) also become named exports, so
|
|
98
104
|
* ESM modules can `import { a } from` it, as with bundlers.
|
|
99
105
|
*/
|
|
100
|
-
function transformCommonJsApiFile(code, file) {
|
|
106
|
+
function transformCommonJsApiFile(code, file, defines) {
|
|
101
107
|
const named = file ? getStaticExports(file, code).filter((name) => /^[A-Za-z_$][\w$]*$/.test(name) && name !== "default" && name !== "__esModule") : [];
|
|
102
|
-
const calls = findStaticRequires(code, file ?? "module.cjs", "commonjs");
|
|
108
|
+
const calls = findStaticRequires(code, file ?? "module.cjs", "commonjs", defines);
|
|
103
109
|
if (!calls) return null;
|
|
104
110
|
const imports = [];
|
|
105
111
|
const s = new MagicString(code);
|
|
@@ -128,10 +134,10 @@ ${named.length ? `export { ${named.map((name, i) => `__goatly_export_${i} as ${n
|
|
|
128
134
|
* imports are hoisted, so the required modules are evaluated before the module, not at the call.
|
|
129
135
|
* Scopes that bind `require` themselves are left alone.
|
|
130
136
|
*/
|
|
131
|
-
function transformRequireInEsm(code, id) {
|
|
137
|
+
function transformRequireInEsm(code, id, defines) {
|
|
132
138
|
if (!/\brequire\s*\(/.test(code)) return null;
|
|
133
139
|
const file = id.split("?")[0];
|
|
134
|
-
const calls = findStaticRequires(code, file, "module");
|
|
140
|
+
const calls = findStaticRequires(code, file, "module", defines);
|
|
135
141
|
if (!calls?.length) return null;
|
|
136
142
|
const s = new MagicString(code);
|
|
137
143
|
const locals = /* @__PURE__ */ new Map();
|
|
@@ -232,8 +238,15 @@ const isRequireId = (n) => n?.type === "Identifier" && n.name === "require";
|
|
|
232
238
|
* refer to the global `require`, not to one a surrounding scope binds (a parameter, or a declaration
|
|
233
239
|
* in the function or file outside nested functions; block scoping is not distinguished). `null` if
|
|
234
240
|
* the code does not parse, or (for `"module"`) has no import/export statements.
|
|
241
|
+
*
|
|
242
|
+
* `require(NAME)` counts as static too when `defines` (Vite's `define`, which includes the
|
|
243
|
+
* DefinePlugin definitions of `onCreateWebpackConfig`) replaces `NAME` with a string. That is what
|
|
244
|
+
* gatsby-plugin-layout does: `require(GATSBY_LAYOUT_COMPONENT_PATH)`, with the path of the site's
|
|
245
|
+
* layout component defined in its `onCreateWebpackConfig`. webpack substitutes definitions before
|
|
246
|
+
* it resolves requires, and so does Rolldown in production builds; in develop the call is
|
|
247
|
+
* converted here, before Vite's define replacement runs, so the name is looked up here.
|
|
235
248
|
*/
|
|
236
|
-
function findStaticRequires(code, file, sourceType) {
|
|
249
|
+
function findStaticRequires(code, file, sourceType, defines) {
|
|
237
250
|
let parsed;
|
|
238
251
|
try {
|
|
239
252
|
parsed = parseSync(file, code, {
|
|
@@ -286,7 +299,7 @@ function findStaticRequires(code, file, sourceType) {
|
|
|
286
299
|
CallExpression(node) {
|
|
287
300
|
if (shadowed || !isRequireId(node.callee) || node.arguments.length !== 1) return;
|
|
288
301
|
const arg = node.arguments[0];
|
|
289
|
-
const spec = arg.type === "Literal" && typeof arg.value === "string" ? arg.value : arg.type === "TemplateLiteral" && arg.expressions.length === 0 ? arg.quasis[0].value.cooked : null;
|
|
302
|
+
const spec = arg.type === "Literal" && typeof arg.value === "string" ? arg.value : arg.type === "TemplateLiteral" && arg.expressions.length === 0 ? arg.quasis[0].value.cooked : arg.type === "Identifier" ? definedString(defines, arg.name) : null;
|
|
290
303
|
if (spec != null) calls.push({
|
|
291
304
|
start: node.start,
|
|
292
305
|
end: node.end,
|
|
@@ -296,4 +309,22 @@ function findStaticRequires(code, file, sourceType) {
|
|
|
296
309
|
});
|
|
297
310
|
return calls;
|
|
298
311
|
}
|
|
312
|
+
/**
|
|
313
|
+
* The string that `defines` replaces `name` with (a definition's value is code, e.g.
|
|
314
|
+
* `"\"/site/src/layout.js\""`), or null when `name` is not defined or not as a string literal. A
|
|
315
|
+
* local binding of the same name is not looked for: Vite's and webpack's define replace by name
|
|
316
|
+
* too, so such a module gets the definition in its other uses of the name as well. An absolute
|
|
317
|
+
* Windows path (`require.resolve()` in gatsby-config gives one) gets forward slashes, which is what
|
|
318
|
+
* Vite resolves as a file system path in an import.
|
|
319
|
+
*/
|
|
320
|
+
function definedString(defines, name) {
|
|
321
|
+
if (!defines || !Object.hasOwn(defines, name)) return null;
|
|
322
|
+
try {
|
|
323
|
+
const value = JSON.parse(defines[name]);
|
|
324
|
+
if (typeof value !== "string") return null;
|
|
325
|
+
return /^[A-Za-z]:[\\/]/.test(value) ? slash(value) : value;
|
|
326
|
+
} catch {
|
|
327
|
+
return null;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
299
330
|
export { transformCjsImportsInEsm, transformCommonJsApiFile, transformGraphQLTags, transformHtmlBodyJsx, transformRequireInEsm };
|
package/dist/runtime/client.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { createApiRunner } from "./api-runner.js";
|
|
2
|
-
import { globalHistory } from "./router/history.js";
|
|
3
2
|
import { Root, pagePath } from "./app.js";
|
|
4
3
|
import { findPath, hasNotFoundSections, isNotFoundPageForUrl, isNotFoundPageOwnUrl, isNotFoundPagePath, loader, normalizePagePath, onNotFoundPagesChange } from "./loader.js";
|
|
4
|
+
import { globalHistory } from "./router/history.js";
|
|
5
5
|
import { notFoundLookupPath } from "./not-found.js";
|
|
6
6
|
import { setNavigateImplementation } from "./router/index.js";
|
|
7
7
|
import { parsePath } from "./link.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "goatly",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "A Gatsby-compatible static site generator built on Vite, Rolldown and React 18/19 – drop-in replacement for Gatsby's Node, SSR and Browser APIs.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"gatsby",
|
|
@@ -72,7 +72,7 @@
|
|
|
72
72
|
"sirv": "^3.0.2",
|
|
73
73
|
"tinyglobby": "^0.2.17",
|
|
74
74
|
"vite": "^8.3.8",
|
|
75
|
-
"@goatly/wasm": "0.1.
|
|
75
|
+
"@goatly/wasm": "0.1.2"
|
|
76
76
|
},
|
|
77
77
|
"devDependencies": {
|
|
78
78
|
"@gql.tada/internal": "1.3.0",
|
package/types/node.d.ts
CHANGED
|
@@ -20,11 +20,20 @@ export interface BuildResult {
|
|
|
20
20
|
timings: Record<string, number>
|
|
21
21
|
/**
|
|
22
22
|
* wall-clock milliseconds of the predefined build segments (they add up to `timings.total`):
|
|
23
|
-
* bootstrap,
|
|
23
|
+
* the bootstrap's phases (config, cache, preBootstrap, schemaCustomization, sourcing, schema,
|
|
24
|
+
* createPages, extractQueries, postBootstrap), staticQueries, queryWorkers, pageQueries, bundle (time waited for it), render,
|
|
24
25
|
* postBuild, finalize
|
|
25
26
|
*/
|
|
26
27
|
breakdown: Record<
|
|
27
|
-
| "
|
|
28
|
+
| "config"
|
|
29
|
+
| "cache"
|
|
30
|
+
| "preBootstrap"
|
|
31
|
+
| "schemaCustomization"
|
|
32
|
+
| "sourcing"
|
|
33
|
+
| "schema"
|
|
34
|
+
| "createPages"
|
|
35
|
+
| "extractQueries"
|
|
36
|
+
| "postBootstrap"
|
|
28
37
|
| "staticQueries"
|
|
29
38
|
| "queryWorkers"
|
|
30
39
|
| "pageQueries"
|