Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | 4x 2893x 2603x 578x 1466x 1465x 578x 2025x 608x 408x 191x 83x 83x 190x 87x 34x 34x 87x 82x 34x 11x 14x 11x 5x 5x 3x 3x 5x 10x 10x 4x 6x 6x 5x 4x | /**
* The baseline of a copy installed from a pack: what each field, the forms
* and the email templates looked like when the copy last took them from the
* pack, as two short hashes, the definition as the version held it (`pack`)
* and as it was written to the copy (`copy`, references pointing at the
* flow's copies). An upgrade compares each with its own kind: the version's
* definition against `pack` tells whether Alice changed it, the copy's
* against `copy` whether Bob did (docs/architecture/ENTITY_STORE.md
* "Upgrades: changes, not only additions").
*
* @implements UC-STORE-005.1, UC-STORE-013.1
*/
const { createHash } = require('crypto')
/** JSON with object keys sorted at every level, so equal definitions hash alike. */
function canonicalJson(value) {
if (Array.isArray(value)) return `[${value.map(canonicalJson).join(',')}]`
if (value && typeof value === 'object') {
const entries = Object.keys(value)
.filter((key) => value[key] !== undefined)
.sort()
.map((key) => `${JSON.stringify(key)}:${canonicalJson(value[key])}`)
return `{${entries.join(',')}}`
}
return JSON.stringify(value ?? null)
}
/** The first 16 hex characters of the SHA-256 of the value's canonical JSON. */
function definitionHash(value) {
return createHash('sha256')
.update(canonicalJson(value ?? null))
.digest('hex')
.slice(0, 16)
}
/** Forms or email templates as a whole: none and an empty list are alike. */
function sectionHash(section) {
return definitionHash(Array.isArray(section) && section.length > 0 ? section : null)
}
/** Whether two definitions are the same, keys in any order. */
const sameDefinition = (a, b) => canonicalJson(a ?? null) === canonicalJson(b ?? null)
/**
* The top-level properties that differ between two field definitions, sorted;
* `fieldId` never differs.
*/
function differingProperties(a, b) {
const keys = new Set([...Object.keys(a || {}), ...Object.keys(b || {})])
keys.delete('fieldId')
return [...keys].filter((key) => !sameDefinition(a?.[key], b?.[key])).sort()
}
/**
* The baseline of an installed copy: for each field written, the hashes of
* its definition in the snapshot and as written; then forms and templates.
*
* @param {{ fields?: Array, forms?: Array, emailTemplates?: Array }} packEntity the snapshot's
* @param {{ fields: Array, forms?: Array, emailTemplates?: Array }} written the copy's
*/
function baselineOf(packEntity, written) {
const packFields = new Map((packEntity?.fields || []).map((field) => [field.fieldId, field]))
const fields = {}
for (const field of written.fields || []) {
if (!packFields.has(field.fieldId)) continue
fields[field.fieldId] = {
pack: definitionHash(packFields.get(field.fieldId)),
copy: definitionHash(field)
}
}
return {
fields,
forms: { pack: sectionHash(packEntity?.forms), copy: sectionHash(written.forms) },
emailTemplates: {
pack: sectionHash(packEntity?.emailTemplates),
copy: sectionHash(written.emailTemplates)
}
}
}
/**
* The baseline after an upgrade. What took the version's definition (fields
* added or changed, sections applied) gets new hashes; what Bob kept keeps
* its old ones, so the next version still sees it as his. A section the
* upgrade rewrote without Bob having changed it (forms completed with a short
* form) follows what was written. A copy without a baseline gets one for
* everything it shares with the version.
*
* @param {object | undefined} previous the copy's baseline, if any
* @param {{ fields?: Array, forms?: Array, emailTemplates?: Array }} packEntity the version's entity
* @param {{ fields?: Array, forms?: Array, emailTemplates?: Array }} before the copy as it was
* @param {{ fields: Array, forms?: Array, emailTemplates?: Array }} written the copy as written
* @param {{ fieldIds: string[], sections: string[] }} taken what took the version's definition
*/
function nextBaseline(previous, packEntity, before, written, taken) {
if (!previous) return baselineOf(packEntity, written)
const packFields = new Map((packEntity?.fields || []).map((field) => [field.fieldId, field]))
const writtenFields = new Map((written.fields || []).map((field) => [field.fieldId, field]))
const fields = { ...(previous.fields || {}) }
for (const fieldId of taken.fieldIds) {
Iif (!packFields.has(fieldId) || !writtenFields.has(fieldId)) continue
fields[fieldId] = {
pack: definitionHash(packFields.get(fieldId)),
copy: definitionHash(writtenFields.get(fieldId))
}
}
const section = (name) => {
const old = previous[name]
if (taken.sections.includes(name) || !old) {
return { pack: sectionHash(packEntity?.[name]), copy: sectionHash(written[name]) }
}
const untouchedByBob = sectionHash(before[name]) === old.copy
return untouchedByBob ? { ...old, copy: sectionHash(written[name]) } : old
}
return { fields, forms: section('forms'), emailTemplates: section('emailTemplates') }
}
module.exports = {
canonicalJson,
definitionHash,
sectionHash,
sameDefinition,
differingProperties,
baselineOf,
nextBaseline
}
|