A grain has one login (Sandstorm's), one database (FerretDB on SQLite) and one
mailbox (the grain's own address), but the .spk carried every login package
WeKan has and a FerretDB with the PostgreSQL, MySQL and SAP HANA handlers.
releases/sandstorm-slim-spk.sh, used by sandstorm.yml, release-all.yml's
build-sandstorm job and build.sh's "Make the .spk package", builds with the
package profile of releases/sandstorm-slim.mjs - accounts-password, 2FA,
passwordless, lockout, OAuth2/OIDC, CAS, LDAP, SAML and the seven social
logins removed from a copy of .meteor/packages for that one build, then
restored - trims the bundle for a linux/x64 grain, verifies none of those
packages got in and packs with spk pack (meteor-spk pack rebuilds and would
undo the trim). sandstorm-src/build-deps.sh builds FerretDB from source with
ferretdb_no_postgresql,ferretdb_no_mysql.
Under Sandstorm the Admin Panel hides the E-mail, Login, Locked users, SAML,
LDAP, OAuth2, CAS, Header login, OAuth providers and Passwordless panes,
Attachments / Database migration, the Email-to-card IMAP and webhook settings,
the New user button and the password fields; a hidden pane opened by URL lands
on the first visible one. POST /users/login and /users/register answer 404
sandstorm-sso-only, database migration is refused and the IMAP/webhook
settings are not stored. Without accounts-password the server creates
Accounts.emailTemplates with the package's defaults and marks useraccounts
initialised, which otherwise stopped the first grain.
Against wekan-12.63-sandstorm.spk, built from the same source: 181,307,364
instead of 185,271,476 bytes (-3.8 MiB, -2.1 %), unpacked 26.7 MiB smaller.
Docker, Snap and the bundles build from the full package list, and a test fails
if that list is slimmed. Tested on a local Sandstorm 0.308: a grain starts, a
board, a list and a card are created, and the removed options are not shown.
tests/sandstormSlim.test.cjs and tests/playwright/specs/sandstorm-slim.e2e.js
pin it. Design: docs/Platforms/FOSS/Container/Sandstorm/Smaller-wekan-spk.md.
Thanks to xet7 !
Strengthen dependency analysis and expose retention reasons in an audit
report. Prune verified browser inputs, unused client polyfills and server
type declarations. Include safety regressions and packaged-runtime results.
List Mac app archives with unzip, handle shallow telemetry scanner paths, ship the Docker deny-hash policy, and discard inherited foreign ELF addons that shadow target prebuilds. Add offline regressions and a native macOS archive check.
Thanks to xet7 !
bcrypt and argon2 (Meteor's accounts-password) each ship one
prebuilds/<platform>-<arch>/ directory per platform they support: 21 native
binaries in every WeKan bundle, of which exactly one is ever opened. Their
loader is node-gyp-build, and its resolve() reads a single directory:
var tuples = readdirSync(path.join(dir, 'prebuilds')).map(parseTuple)
var tuple = tuples.filter(matchTuple(platform, arch)).sort(compareTuples)[0]
with platform and arch from os.platform() and os.arch(). This is the same
argument bundle-trim.mjs already makes about uWebSockets.js, so it is the same
code path: --trim-prebuilds keeps the directories node-gyp-build would match
and drops the rest. The tuple parsing here IS node-gyp-build's, multi-arch
names ("darwin-x64+arm64") included, and both libc flavours of the target are
kept because glibc versus musl is decided at runtime.
Three things make it safe rather than merely smaller, and all three are pinned
by tests:
- It is off by default and REFUSES to run without an explicit --platform and
--arch. The defaults are linux/x64, and a Windows or macOS bundle trimmed
with those would lose the only addon it can load, which is precisely the
fault the single EXE was just fixed for.
- build-amd64 does NOT pass it. Every other bundle WeKan ships is that bundle
repacked, so trimming there would take the prebuilds away from
architectures that have not been built yet. Each final per-platform job
passes its own target instead.
- Every decision is made before anything is deleted, so a package whose
prebuilds cannot be reasoned about is left whole rather than half-trimmed.
Running it on a real bundle found bare-fs, bare-path and bare-url, which
ship prebuilds/ of .bare files for the Bare runtime - a different loader
with different rules. Those are left untouched, and say so.
On the mac-arm64 bundle: 19 of the 21 removed, the two for darwin-arm64 kept.
For the single Windows EXE that takes the files it has to unpack from 33 down
to about fifteen.
Two tests were counting a script named in a YAML comment as a script the job
runs, which the new comments in build-amd64 tripped. Both now drop comment
lines before looking, which is what they meant in the first place.
Thanks to xet7 !
A released image crash-looped:
Error: ENOENT: no such file or directory,
open '/build/programs/server/packages/ecmascript.js.map'
at /build/programs/server/boot.js:101:29
My claim that "nothing on any loading path reads a .map" was true of the client
and false of the server. boot.js reads every map NAMED in
programs/server/program.json, at boot, unconditionally:
serverJson.load.forEach(function (fileInfo) {
if (fileInfo.sourceMap) {
var rawSourceMap = fs.readFileSync(path.resolve(serverDir, fileInfo.sourceMap), 'utf8');
63 of the 102 load entries name one, 60 MiB of them, so removing the files left
63 dangling names and the server died before opening its port.
The names now go with the files: the same pass deletes `sourceMap` and
`sourceMapRoot` from every load entry. The client is unaffected and always was -
its program.json names no maps at all (678 manifest entries, zero sourceMap
fields) and webapp reads only program.json itself at startup, so a client map is
found through the //# sourceMappingURL comment and 404s harmlessly when absent.
Verified by BOOTING a trimmed bundle rather than by reading the code again: with
uWebSockets.js, the legacy client and all 4766 maps removed, `node main.js`
loads the whole server and reaches AccountsServer.init, failing only on the
deliberately unreachable MONGO_URL it was given. That is the check that was
missing the first time.
tests/bundleTrim.test.cjs pins the invariant boot.js actually requires - every
map program.json names exists on disk - for BOTH settings of --keep-maps, so
neither branch can drift back into the crash.
Thanks to xet7 !
uws is not reliable enough yet to be what a default points at, so nothing WeKan
ships selects it any more, and nothing carries the module.
DEFAULTS. Every docker-compose file, start-wekan.sh, start-wekan.bat, build.sh,
build.bat, the image's own ENV block, the snap config and the Sandstorm grain
now say sockjs. The commented-out `#- DDP_TRANSPORT=uws` alternatives beside
them are replaced by a note saying why there is no alternative, rather than
advertising a value that no longer works.
BUNDLES. Every place a built bundle is post-processed - the amd64 build, each
per-arch rebuild, the repack container, install-node-for-arch.sh and the
Dockerfile - now runs bundle-trim.mjs beside the prune it already ran:
* --transport sockjs removes uWebSockets.js entirely. 121 MiB of prebuilt
binaries for OS/CPU/ABI combinations one machine cannot use, and a sockjs
server never requires the module at all: ddp-server's
`Npm.require('uWebSockets.js')` is inside the uws transport's setup(), which
runs only for the transport that was chosen.
* --drop-legacy-client removes programs/web.browser.legacy, a whole second
copy of the client for browsers without modern JS. 81 MiB measured. Meteor
supports running with architectures excluded and says so in webapp's
categorizeRequest(): "If our preferred arch is not available, it's better to
use another client arch that is available than to guarantee the site won't
work". An old browser is served web.browser; the 404 branch below that is
reached only when NO arch matches, which cannot happen while web.browser is
there, and autoupdate iterates the programs that actually loaded.
The files are only half of it. The arch is NAMED in programs/server/config.json
and star.json, and boot.js builds a dynamic-import root for every name in the
first - so the name is removed with the files. Those manifests are mode 444
as Meteor writes them, so they are made writable, rewritten, and set back:
failing there would leave the one state that actually breaks a server, files
gone and manifests still naming the arch.
UPGRADES. An existing docker-compose.yml that says DDP_TRANSPORT=uws would ask
the new image for a module it does not have and crash-loop on the require. So
wekan-entrypoint.sh and the bundle's start-wekan.sh coerce uws back to sockjs
before starting anything, and print why - a setting silently ignored is worse
than one that fails.
tests/sockjsEverywhere.test.cjs pins all three halves together, because each is
useless or harmful without the others: a default without the coercion is an
upgrade trap, a coercion without the trim is dead weight, and a trim without
both is a crash. tests/bundleTrim.test.cjs covers the legacy removal, the
manifest deregistration and the read-only rewrite.
Thanks to xet7 !
Trimming uWebSockets.js down to the prebuilds a grain's own platform can open
freed 93M of its 121M. It can all go, because a grain loads NONE of it.
The uws transport is optional in Meteor 3. `ddp-server`'s transports/index.js
resolves the transport from `Meteor.settings.packages['ddp-server'].transport`,
then `DDP_TRANSPORT`, then `DISABLE_SOCKJS`, and DEFAULTS to sockjs - and
`Npm.require('uWebSockets.js')` sits inside the uws transport's `setup()`, which
runs only for the transport that was actually chosen. `sandstorm-pkgdef.capnp`
sets none of those, and its `environ` is by its own comment the app's ENTIRE
environment, so a grain has been running sockjs all along while carrying 121M of
a module it never required.
So the pkgdef now PINS `DDP_TRANSPORT=sockjs` - the removal should rest on a
stated fact, not on an upstream default staying put - and `bundle-trim.mjs`
takes `--transport sockjs`, which removes the module whole rather than thinning
its prebuilds. 120 MiB measured, 27 MiB more than the platform trim, and the
.spk is now ~860M against the 1 GiB limit.
It is a FLAG and not the default because WeKan asks for uws nearly everywhere
else - docker-compose, start-wekan.sh, build.sh - and those bundles keep the
module, trimmed to the prebuilds their own platform can open.
tests/bundleTrim.test.cjs ties the grain's pinned transport to the flag the .spk
is trimmed with. If they ever disagreed, the grain would require a module that
was left out and fail to boot, which is the one way this can go wrong.
Thanks to xet7 !
v10.93, v10.94 and v10.95 all failed to pack with *App exceeds uncompressed size
limit of 1 GiB*. v10.95 was the first run whose size report worked, and it named
the gigabyte: `.meteor-spk/bundle` 852M plus `.meteor-spk/deps` 336M, so 1188M
against a 1024M ceiling. It also answered the question that report was written
to ask — a second `meteor-spk pack` REUSES the bundle rather than rebuilding it,
because the pruned tree stayed pruned across the retry (852M to 833M). So
trimming between the two attempts works; 19M was simply never going to close a
165M gap.
Measuring the bundle rather than guessing at it turned up two passengers that
are large, unreachable at runtime, and safe to drop:
* uWebSockets.js ships TWENTY prebuilt binaries — linux/macOS/Windows times
x64/arm64 times four Node ABIs, 121M — and its loader is one line:
`require('./uws_' + process.platform + '_' + process.arch + '_' +
process.versions.modules + '.node')`. A grain is one platform running one
Node; the other sixteen files can never be opened by it. Keeping every ABI
of the target platform+arch, so a Node major bump still finds its binary,
still frees ~93M.
* Source maps — 4766 files, 188M, over a fifth of the bundle. They exist for
a debugger attached to the process. A packed app has none, and a missing
.map degrades a stack trace at worst.
releases/bundle-trim.mjs does both, measured at 281 MiB on a real bundle, and
the Sandstorm leg runs it beside the existing prune before the retry pack:
1188M becomes ~888M, with 136M of headroom.
An architecture with NO uWebSockets.js prebuild at all — ppc64le, s390x,
riscv64, where ddp-server falls back to sockjs — is left completely alone, since
deleting the other platforms' files there would free nothing that matters and
could only break the fallback. tests/bundleTrim.test.cjs pins that, the four
kept ABIs, that a directory merely ending in `.map` is not a source map, and
that the trim runs before the retry pack rather than after it.
Thanks to xet7 !