mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
Extract the pure, security- and correctness-critical logic of the recent features into Meteor-free models/lib/* modules (so production and tests share one implementation) and add plain-Node tests with positive AND negative cases, wired into test:unit:node: - models/lib/mongoSelectorSafety.js hasWhere() — the windowed card publication refuses a client selector carrying $where (server-side JS). Tests: safe selectors pass; $where at top level / in $and / deep in arrays / in $elemMatch is rejected. (was inline in server/publications/cardsWindow.js) - models/lib/cardsLoading.js resolveCardsLoadingMode / parseCardsLoadingEnv / windowCountId — CARDS_LOADING maps to 'lazy' only for exactly "lazy" (garbage never enables the experimental mode); a falsy swimlane id collapses to one count-doc id (listBody uses undefined, listHeader uses ''). (used by server/cards-loading.js, server/models/settings.js, client/lib/lazyCards.js) - models/lib/base64Chunk.js encodeAligned/encodeFinal — streaming an attachment into the JSON export as base64 in any chunk sizes reconstructs exactly the whole-file base64; aligned pieces never carry padding. (used by Exporter.buildStream) - models/lib/backupPaths.js filesRootFrom/scheduleText — WRITABLE_PATH -> files root (no double /files suffix, Windows-safe) and the synced-cron schedule text (daily/weekly/monthly + defaults, unknown frequency falls back to daily). (used by server/methods/backup.js) 41 new assertions across 4 files, all passing; whole test:unit:node suite green.
33 lines
1.3 KiB
JavaScript
33 lines
1.3 KiB
JavaScript
'use strict';
|
|
|
|
// Pure, Meteor-free helper for streaming an attachment's bytes into the JSON
|
|
// board export as base64 WITHOUT buffering the whole file. Used by
|
|
// Exporter.buildStream (models/exporter.js) and unit-tested in
|
|
// tests/base64Chunk.test.cjs.
|
|
//
|
|
// base64 encodes 3 bytes -> 4 chars, so a stream can only be emitted in
|
|
// 3-byte-aligned pieces; the trailing 0-2 bytes are carried over to the next
|
|
// chunk (or flushed at the end, where they produce the '=' padding). Emitting
|
|
// aligned pieces and concatenating them yields exactly the same string as
|
|
// base64-encoding the whole buffer at once.
|
|
//
|
|
// Node Buffers only — the caller feeds fs.read chunks and holds `leftover`.
|
|
|
|
// Encode the largest 3-byte-aligned prefix of (leftover + chunk); return the
|
|
// base64 `piece` and the new `leftover` (0-2 bytes) to carry to the next call.
|
|
function encodeAligned(leftover, chunk) {
|
|
const buf = leftover && leftover.length ? Buffer.concat([leftover, chunk]) : chunk;
|
|
const usable = buf.length - (buf.length % 3);
|
|
return {
|
|
piece: usable ? buf.subarray(0, usable).toString('base64') : '',
|
|
leftover: buf.subarray(usable),
|
|
};
|
|
}
|
|
|
|
// Encode the final carried-over bytes (0-2), producing the padded tail.
|
|
function encodeFinal(leftover) {
|
|
return leftover && leftover.length ? leftover.toString('base64') : '';
|
|
}
|
|
|
|
module.exports = { encodeAligned, encodeFinal };
|