@crombolo/transfer 0.56.0 → 0.58.0
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/package.json +3 -3
- package/src/import.ts +4 -0
- package/src/load.ts +68 -5
- package/src/model.ts +20 -3
- package/src/package.ts +77 -16
- package/src/revert.ts +1 -0
- package/src/write.ts +69 -6
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crombolo/transfer",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.58.0",
|
|
4
4
|
"description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. Content transfer for crombolo: bundles of content moved between environments.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Crombolo",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"access": "public"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@crombolo/core": "0.
|
|
32
|
-
"@crombolo/data": "0.
|
|
31
|
+
"@crombolo/core": "0.58.0",
|
|
32
|
+
"@crombolo/data": "0.58.0"
|
|
33
33
|
}
|
|
34
34
|
}
|
package/src/import.ts
CHANGED
|
@@ -83,6 +83,8 @@ export interface ImportOptions extends CheckOptions {
|
|
|
83
83
|
label?: string | undefined
|
|
84
84
|
/** The npm package the bundle arrived in, recorded on the run. */
|
|
85
85
|
package?: { name: string; version: string } | undefined
|
|
86
|
+
/** Who signed it, a key's fingerprint or a CI source's repository, recorded on the run (story 316). */
|
|
87
|
+
signedBy?: string | undefined
|
|
86
88
|
/**
|
|
87
89
|
* A node's values become exactly the bundle's, rather than the bundle's laid
|
|
88
90
|
* over what is here: a whole-site replace (story 105).
|
|
@@ -235,6 +237,8 @@ async function applyBundle(tx: Db, set: ContentSet, options: ImportOptions): Pro
|
|
|
235
237
|
bundleLabel: options.label ?? null,
|
|
236
238
|
packageName: options.package?.name ?? null,
|
|
237
239
|
packageVersion: options.package?.version ?? null,
|
|
240
|
+
bundleType: set.manifest.bundleType ?? null,
|
|
241
|
+
signedBy: options.signedBy ?? null,
|
|
238
242
|
direction: 'import',
|
|
239
243
|
revertsRunId: null,
|
|
240
244
|
schemaStateId: state.id,
|
package/src/load.ts
CHANGED
|
@@ -22,10 +22,12 @@ import {
|
|
|
22
22
|
type BundleRedirect,
|
|
23
23
|
type BundleSchedule,
|
|
24
24
|
type BundleSection,
|
|
25
|
+
type BundleType,
|
|
25
26
|
type BundleValue,
|
|
26
27
|
type BundleVariant,
|
|
27
28
|
type ContentSet,
|
|
28
29
|
ELEMENT_FOLDER_TYPE,
|
|
30
|
+
isBundleType,
|
|
29
31
|
OLDEST_BUNDLE_FORMAT_VERSION,
|
|
30
32
|
} from './model.ts'
|
|
31
33
|
import { type BundlePeople, openPeople, type PeopleBlock, parsePeopleBlock } from './people.ts'
|
|
@@ -38,6 +40,8 @@ import {
|
|
|
38
40
|
type SiteDataTable,
|
|
39
41
|
} from './site-data.ts'
|
|
40
42
|
import {
|
|
43
|
+
BUNDLE_TYPE_MISSING,
|
|
44
|
+
blankedManifest,
|
|
41
45
|
bundleIntegrity,
|
|
42
46
|
MANIFEST_FILE,
|
|
43
47
|
MEDIA_DIR,
|
|
@@ -45,6 +49,8 @@ import {
|
|
|
45
49
|
mediaPath,
|
|
46
50
|
mediaUri,
|
|
47
51
|
nodePath,
|
|
52
|
+
PACKAGE_FILE,
|
|
53
|
+
packageIdentity,
|
|
48
54
|
type SealedValues,
|
|
49
55
|
sectionOf,
|
|
50
56
|
valuesPath,
|
|
@@ -317,6 +323,10 @@ function parseManifest(text: string, problems: BundleProblem[]): BundleManifest
|
|
|
317
323
|
problems.push({ file: MANIFEST_FILE, message: OLD_LAYOUT })
|
|
318
324
|
return undefined
|
|
319
325
|
}
|
|
326
|
+
if (!isBundleType(raw.bundleType)) {
|
|
327
|
+
problems.push({ file: MANIFEST_FILE, message: BUNDLE_TYPE_MISSING })
|
|
328
|
+
return undefined
|
|
329
|
+
}
|
|
320
330
|
const provenance = isRecord(raw.provenance) ? raw.provenance : {}
|
|
321
331
|
const selector = isRecord(raw.selector) ? raw.selector : {}
|
|
322
332
|
const dependencies = isRecord(raw.dependencies) ? raw.dependencies : {}
|
|
@@ -327,6 +337,7 @@ function parseManifest(text: string, problems: BundleProblem[]): BundleManifest
|
|
|
327
337
|
return {
|
|
328
338
|
formatVersion,
|
|
329
339
|
id: str(raw.id) ?? '',
|
|
340
|
+
bundleType: raw.bundleType as BundleType,
|
|
330
341
|
...(str(raw.label) ? { label: str(raw.label) as string } : {}),
|
|
331
342
|
createdAt: str(raw.createdAt) ?? '',
|
|
332
343
|
createdBy: str(raw.createdBy) ?? '',
|
|
@@ -424,7 +435,7 @@ function parseCarries(raw: Record<string, unknown>): Partial<Record<BundleSectio
|
|
|
424
435
|
|
|
425
436
|
/** Why a bundle in the layout before format 4 is refused, and what to do about it. */
|
|
426
437
|
export const OLD_LAYOUT =
|
|
427
|
-
|
|
438
|
+
"this bundle was written by an older crombolo, in a layout no longer read; build it again with the command that made it: site build, site export, or the Bundle Editor's download"
|
|
428
439
|
|
|
429
440
|
/**
|
|
430
441
|
* `media.json`, read and checked: each entry's URI must be this bundle's own
|
|
@@ -537,6 +548,18 @@ const isSafeKey = (key: string): boolean =>
|
|
|
537
548
|
key !== '' && !key.startsWith('/') && !key.split('/').some((part) => part === '' || part === '..')
|
|
538
549
|
|
|
539
550
|
/** Reads a bundle directory. `set` is undefined when it cannot be trusted at all. */
|
|
551
|
+
/** The package's `package.json`, when the bundle has one; a broken one is a problem. */
|
|
552
|
+
function readPackage(dir: string, problems: BundleProblem[]): Record<string, unknown> | undefined {
|
|
553
|
+
const file = join(dir, PACKAGE_FILE)
|
|
554
|
+
if (!existsSync(file)) return undefined
|
|
555
|
+
try {
|
|
556
|
+
const parsed = JSON.parse(readFileSync(file, 'utf8')) as unknown
|
|
557
|
+
if (isRecord(parsed)) return parsed
|
|
558
|
+
} catch {}
|
|
559
|
+
problems.push({ file: PACKAGE_FILE, message: 'is not a JSON object' })
|
|
560
|
+
return undefined
|
|
561
|
+
}
|
|
562
|
+
|
|
540
563
|
export function loadBundle(dir: string): LoadedBundle {
|
|
541
564
|
const problems: BundleProblem[] = []
|
|
542
565
|
const blobs = new Map<string, string>()
|
|
@@ -546,8 +569,39 @@ export function loadBundle(dir: string): LoadedBundle {
|
|
|
546
569
|
problems.push({ file: MANIFEST_FILE, message: `no bundle at ${dir}` })
|
|
547
570
|
return { set: undefined, problems, blobs, files }
|
|
548
571
|
}
|
|
549
|
-
const
|
|
572
|
+
const manifestText = readFileSync(manifestFile, 'utf8')
|
|
573
|
+
const manifest = parseManifest(manifestText, problems)
|
|
550
574
|
if (!manifest) return { set: undefined, problems, blobs, files }
|
|
575
|
+
if (manifest.bundleType === 'extension' && (manifest.carries?.code?.length ?? 0) > 0)
|
|
576
|
+
problems.push({
|
|
577
|
+
file: MANIFEST_FILE,
|
|
578
|
+
message:
|
|
579
|
+
"it is an extension, which never carries the site's code, and it lists files under code/",
|
|
580
|
+
})
|
|
581
|
+
if (manifest.bundleType === 'extension' && (manifest.carries?.components?.length ?? 0) > 0)
|
|
582
|
+
problems.push({
|
|
583
|
+
file: MANIFEST_FILE,
|
|
584
|
+
message:
|
|
585
|
+
'it is an extension, whose components live in its package and render in its own process, and it lists files under components/',
|
|
586
|
+
})
|
|
587
|
+
if (
|
|
588
|
+
manifest.bundleType === 'extension' &&
|
|
589
|
+
(manifest.carries?.schema ?? []).some((file) =>
|
|
590
|
+
/(^|\/)migrations\/[^/]+\.[cm]?[jt]sx?$/.test(file),
|
|
591
|
+
)
|
|
592
|
+
)
|
|
593
|
+
problems.push({
|
|
594
|
+
file: MANIFEST_FILE,
|
|
595
|
+
message:
|
|
596
|
+
"it is an extension, and it carries value migrations under schema/migrations/, which are code: an extension's code runs only in its own process",
|
|
597
|
+
})
|
|
598
|
+
const pkg = readPackage(dir, problems)
|
|
599
|
+
const packageType = (pkg?.crombolo as { bundleType?: unknown } | undefined)?.bundleType
|
|
600
|
+
if (pkg && packageType !== manifest.bundleType)
|
|
601
|
+
problems.push({
|
|
602
|
+
file: PACKAGE_FILE,
|
|
603
|
+
message: `says it is ${isBundleType(packageType) ? `a ${packageType} bundle` : 'nothing'}, and bundle.json that it is a ${manifest.bundleType} bundle`,
|
|
604
|
+
})
|
|
551
605
|
|
|
552
606
|
const nodesDir = join(dir, 'nodes')
|
|
553
607
|
const nodeFiles = existsSync(nodesDir)
|
|
@@ -712,11 +766,20 @@ export function loadBundle(dir: string): LoadedBundle {
|
|
|
712
766
|
}
|
|
713
767
|
|
|
714
768
|
// Before shape: a truncated copy produces confusing field errors otherwise.
|
|
715
|
-
|
|
716
|
-
|
|
769
|
+
// The manifest and the package's identity are hashed too (story 306).
|
|
770
|
+
const integrity = bundleIntegrity(read, {
|
|
771
|
+
manifest: blankedManifest(manifestText, manifest.integrity),
|
|
772
|
+
...(pkg ? { packageIdentity: packageIdentity(pkg) } : {}),
|
|
773
|
+
})
|
|
774
|
+
if (!manifest.integrity)
|
|
775
|
+
problems.push({
|
|
776
|
+
file: MANIFEST_FILE,
|
|
777
|
+
message: 'has no integrity hash, so nothing says it is whole',
|
|
778
|
+
})
|
|
779
|
+
else if (manifest.integrity !== integrity)
|
|
717
780
|
problems.push({
|
|
718
781
|
file: MANIFEST_FILE,
|
|
719
|
-
message: `integrity does not match the
|
|
782
|
+
message: `integrity does not match the files (${manifest.integrity} vs ${integrity}) — the bundle is incomplete or was edited`,
|
|
720
783
|
})
|
|
721
784
|
|
|
722
785
|
const nodes = read.flatMap((file) => {
|
package/src/model.ts
CHANGED
|
@@ -23,14 +23,29 @@ import type { SiteDataBlock, SiteDataTable } from './site-data.ts'
|
|
|
23
23
|
* `media/<key>` with a `bundle://` URI (story 195).
|
|
24
24
|
* 6: a Library folder may travel as a node (story 283).
|
|
25
25
|
* 7: the site's `public/` folder, listed in `public.json` (story 284).
|
|
26
|
+
* 8: `bundleType` says whether it is a site package or an extension, and the
|
|
27
|
+
* hash covers `bundle.json` and the package's identity too (story 306).
|
|
26
28
|
*
|
|
27
29
|
* An older version is refused rather than read (no older layout is kept up),
|
|
28
30
|
* with a message saying to rebuild it.
|
|
29
31
|
*/
|
|
30
|
-
export const BUNDLE_FORMAT_VERSION =
|
|
32
|
+
export const BUNDLE_FORMAT_VERSION = 8
|
|
31
33
|
|
|
32
|
-
/** The oldest version this reader reads: the first
|
|
33
|
-
export const OLDEST_BUNDLE_FORMAT_VERSION =
|
|
34
|
+
/** The oldest version this reader reads: the first that says what kind of bundle it is. */
|
|
35
|
+
export const OLDEST_BUNDLE_FORMAT_VERSION = 8
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* What a bundle is (story 306). A site package is a site, or part of one, made to
|
|
39
|
+
* move it: trusted as the site is, it may carry the site's code and config, and is
|
|
40
|
+
* only ever applied from a file. An extension is installed as a dependency, and
|
|
41
|
+
* never carries the site's code.
|
|
42
|
+
*/
|
|
43
|
+
export type BundleType = 'site' | 'extension'
|
|
44
|
+
|
|
45
|
+
export const BUNDLE_TYPES: readonly BundleType[] = ['site', 'extension']
|
|
46
|
+
|
|
47
|
+
export const isBundleType = (value: unknown): value is BundleType =>
|
|
48
|
+
value === 'site' || value === 'extension'
|
|
34
49
|
|
|
35
50
|
/**
|
|
36
51
|
* The sections a bundle can carry beyond its content, and the directory each
|
|
@@ -289,6 +304,8 @@ export interface BundleManifest {
|
|
|
289
304
|
* selection, overlaid as always.
|
|
290
305
|
*/
|
|
291
306
|
scope?: 'site'
|
|
307
|
+
/** Whether this is a site package or an extension; every bundle says (story 306). */
|
|
308
|
+
bundleType?: BundleType
|
|
292
309
|
/**
|
|
293
310
|
* `omitted`: a whole-site file written without its media files (`--no-media`).
|
|
294
311
|
* `referenced`: without them because they move one at a time between the
|
package/src/package.ts
CHANGED
|
@@ -6,12 +6,20 @@
|
|
|
6
6
|
* The payload is `writeBundle`'s output unchanged. The package is what gives
|
|
7
7
|
* it a name, a version and a core it was written for.
|
|
8
8
|
*/
|
|
9
|
+
import type { BundleType } from './model.ts'
|
|
9
10
|
import { PEOPLE_DIR } from './people.ts'
|
|
10
11
|
import { SITE_DATA_DIR } from './site-data.ts'
|
|
11
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
type BundleFile,
|
|
14
|
+
blankedManifest,
|
|
15
|
+
bundleIntegrity,
|
|
16
|
+
MANIFEST_FILE,
|
|
17
|
+
PACKAGE_FILE,
|
|
18
|
+
packageIdentity,
|
|
19
|
+
} from './write.ts'
|
|
12
20
|
|
|
13
21
|
/** The npm keyword a bundle publishes so the marketplace can find it. */
|
|
14
|
-
export const BUNDLE_KEYWORD = 'crombolo-
|
|
22
|
+
export const BUNDLE_KEYWORD = 'crombolo-extension'
|
|
15
23
|
|
|
16
24
|
export interface BundlePackageInfo {
|
|
17
25
|
/** The npm package name. */
|
|
@@ -26,6 +34,19 @@ export interface BundlePackageInfo {
|
|
|
26
34
|
private?: boolean
|
|
27
35
|
/** The range on `@crombolo/crombolo` it is written for. */
|
|
28
36
|
peerRange: string
|
|
37
|
+
/** A site package or an extension (story 306); a site package is always private. */
|
|
38
|
+
bundleType: BundleType
|
|
39
|
+
/**
|
|
40
|
+
* An extension's code (story 312): its files under `src/`, outside the
|
|
41
|
+
* payload, the factory `crombolo.server` names, and the permissions it asks
|
|
42
|
+
* for, each with its reason.
|
|
43
|
+
*/
|
|
44
|
+
code?: {
|
|
45
|
+
files: readonly BundleFile[]
|
|
46
|
+
entry: string
|
|
47
|
+
server: string
|
|
48
|
+
permissions: Readonly<Record<string, string>>
|
|
49
|
+
}
|
|
29
50
|
}
|
|
30
51
|
|
|
31
52
|
const NPM_NAME = /^(?:@[a-z0-9][a-z0-9._-]*\/)?[a-z0-9][a-z0-9._-]*$/
|
|
@@ -83,9 +104,18 @@ export function bundlePackageJson(
|
|
|
83
104
|
version: info.version,
|
|
84
105
|
...(info.description ? { description: info.description } : {}),
|
|
85
106
|
...(info.private ? { private: true } : { keywords: [BUNDLE_KEYWORD] }),
|
|
86
|
-
|
|
107
|
+
...(info.code ? { exports: { '.': `./${info.code.entry}` } } : {}),
|
|
108
|
+
files: packageFilesFor([...paths, ...(info.code?.files.map((file) => file.path) ?? [])]),
|
|
87
109
|
peerDependencies: { '@crombolo/crombolo': info.peerRange },
|
|
88
|
-
crombolo: {
|
|
110
|
+
crombolo: {
|
|
111
|
+
id: info.id,
|
|
112
|
+
name: info.label,
|
|
113
|
+
bundleType: info.bundleType,
|
|
114
|
+
content: true,
|
|
115
|
+
...(info.code
|
|
116
|
+
? { server: info.code.server, schemaVersion: 4, permissions: { ...info.code.permissions } }
|
|
117
|
+
: {}),
|
|
118
|
+
},
|
|
89
119
|
}
|
|
90
120
|
}
|
|
91
121
|
|
|
@@ -95,6 +125,12 @@ export function writeBundlePackage(
|
|
|
95
125
|
info: BundlePackageInfo,
|
|
96
126
|
): BundleFile[] {
|
|
97
127
|
if (!isPackageName(info.name)) throw new Error(`"${info.name}" is not a package name npm accepts`)
|
|
128
|
+
if (info.code && info.bundleType !== 'extension')
|
|
129
|
+
throw new Error("Only an extension carries code of its own: a site package's is its payload.")
|
|
130
|
+
if (info.code?.files.some((file) => !file.path.startsWith('src/')))
|
|
131
|
+
throw new Error("An extension's code is under src/.")
|
|
132
|
+
if (info.bundleType === 'site' && !info.private)
|
|
133
|
+
throw new Error('A site package is never published: it can only be a private package.')
|
|
98
134
|
// Personal data never goes where `npm publish` can take it, sealed or not;
|
|
99
135
|
// and sealed content would be no use to anyone who installed it.
|
|
100
136
|
if (!info.private && payload.some((file) => file.path.startsWith(`${PEOPLE_DIR}/`)))
|
|
@@ -103,18 +139,43 @@ export function writeBundlePackage(
|
|
|
103
139
|
throw new Error("A bundle that carries the site's own data can only be a private package.")
|
|
104
140
|
if (!info.private && payload.some((file) => file.path.startsWith('values/')))
|
|
105
141
|
throw new Error('A bundle whose content is encrypted can only be a private package.')
|
|
142
|
+
const pkg = bundlePackageJson(
|
|
143
|
+
info,
|
|
144
|
+
payload.map((file) => file.path),
|
|
145
|
+
)
|
|
106
146
|
return [
|
|
107
|
-
{
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
bundlePackageJson(
|
|
111
|
-
info,
|
|
112
|
-
payload.map((file) => file.path),
|
|
113
|
-
),
|
|
114
|
-
null,
|
|
115
|
-
2,
|
|
116
|
-
)}\n`,
|
|
117
|
-
},
|
|
118
|
-
...payload,
|
|
147
|
+
{ path: PACKAGE_FILE, text: `${JSON.stringify(pkg, null, 2)}\n` },
|
|
148
|
+
...rehashed(payload, pkg),
|
|
149
|
+
...(info.code?.files ?? []),
|
|
119
150
|
]
|
|
120
151
|
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* The payload with its manifest's hash taken again over the package's identity
|
|
155
|
+
* too, now that there is one (story 306). Files a caller streams in after the
|
|
156
|
+
* hash, the public files, carry a `source` and are left out of it as before.
|
|
157
|
+
*/
|
|
158
|
+
function rehashed(payload: readonly BundleFile[], pkg: Record<string, unknown>): BundleFile[] {
|
|
159
|
+
const manifest = payload.find((file) => file.path === MANIFEST_FILE)
|
|
160
|
+
if (!manifest?.text) throw new Error('A bundle package needs its bundle.json.')
|
|
161
|
+
const parsed = JSON.parse(manifest.text) as { integrity?: string; bundleType?: string }
|
|
162
|
+
const crombolo = pkg.crombolo as { bundleType: string }
|
|
163
|
+
if (parsed.bundleType && parsed.bundleType !== crombolo.bundleType)
|
|
164
|
+
throw new Error(
|
|
165
|
+
`bundle.json says it is a ${parsed.bundleType} bundle, and package.json a ${crombolo.bundleType} one.`,
|
|
166
|
+
)
|
|
167
|
+
// Written where `writeManifest` puts it, straight after the id.
|
|
168
|
+
const typed = parsed.bundleType
|
|
169
|
+
? manifest.text
|
|
170
|
+
: manifest.text.replace(
|
|
171
|
+
/\n {2}"id": ("[^"\n]*"),\n/,
|
|
172
|
+
(line) => `${line} "bundleType": ${JSON.stringify(crombolo.bundleType)},\n`,
|
|
173
|
+
)
|
|
174
|
+
const blank = blankedManifest(typed, parsed.integrity ?? '')
|
|
175
|
+
const integrity = bundleIntegrity(
|
|
176
|
+
payload.filter((file) => file.text !== undefined || file.bytes !== undefined),
|
|
177
|
+
{ manifest: blank, packageIdentity: packageIdentity(pkg) },
|
|
178
|
+
)
|
|
179
|
+
const text = blank.replace('"integrity": ""', `"integrity": ${JSON.stringify(integrity)}`)
|
|
180
|
+
return payload.map((file) => (file.path === MANIFEST_FILE ? { ...file, text } : file))
|
|
181
|
+
}
|
package/src/revert.ts
CHANGED
|
@@ -349,6 +349,7 @@ export async function revertRun(
|
|
|
349
349
|
bundleLabel: `revert of ${runId}`,
|
|
350
350
|
packageName: original.packageName,
|
|
351
351
|
packageVersion: original.packageVersion,
|
|
352
|
+
bundleType: original.bundleType,
|
|
352
353
|
direction: 'revert',
|
|
353
354
|
revertsRunId: runId,
|
|
354
355
|
schemaStateId: state.id,
|
package/src/write.ts
CHANGED
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
type BundleManifest,
|
|
20
20
|
type BundleNode,
|
|
21
21
|
type BundleSection,
|
|
22
|
+
type BundleType,
|
|
22
23
|
type BundleValue,
|
|
23
24
|
type BundleVariant,
|
|
24
25
|
type ContentSet,
|
|
@@ -167,6 +168,7 @@ export function writeManifest(manifest: BundleManifest): string {
|
|
|
167
168
|
return json({
|
|
168
169
|
formatVersion: manifest.formatVersion,
|
|
169
170
|
id: manifest.id,
|
|
171
|
+
...(manifest.bundleType ? { bundleType: manifest.bundleType } : {}),
|
|
170
172
|
...(manifest.label ? { label: manifest.label } : {}),
|
|
171
173
|
createdAt: manifest.createdAt,
|
|
172
174
|
createdBy: manifest.createdBy,
|
|
@@ -280,24 +282,77 @@ export function writeMediaManifest(bundleId: string, blobs: readonly BundleBlob[
|
|
|
280
282
|
|
|
281
283
|
export const MANIFEST_FILE = 'bundle.json'
|
|
282
284
|
|
|
285
|
+
/** Said wherever a bundle does not say what it is (story 306). */
|
|
286
|
+
export const BUNDLE_TYPE_MISSING =
|
|
287
|
+
'this bundle does not say what it is: it needs `bundleType`, `site` or `extension`'
|
|
288
|
+
|
|
283
289
|
/**
|
|
284
290
|
* A hash over everything the bundle carries, in path order, so a bundle that
|
|
285
|
-
* was copied half-way is refused rather than imported as though it
|
|
286
|
-
*
|
|
287
|
-
*
|
|
291
|
+
* was copied half-way, or edited, is refused rather than imported as though it
|
|
292
|
+
* were whole. Media files are hashed by their bytes, which is what makes
|
|
293
|
+
* `--with-blobs` a claim the reader can check.
|
|
294
|
+
*
|
|
295
|
+
* After the files come the manifest, with its own hash blanked, and the
|
|
296
|
+
* package's identity (story 306): so `bundleType`, and what the package says it
|
|
297
|
+
* is, cannot change after the build without the hash failing. It has no key, so
|
|
298
|
+
* it catches an edit, not a forger; only a signature does that (story 060).
|
|
288
299
|
*/
|
|
289
|
-
export function bundleIntegrity(
|
|
300
|
+
export function bundleIntegrity(
|
|
301
|
+
files: readonly BundleFile[],
|
|
302
|
+
trailer: { manifest: string; packageIdentity?: string },
|
|
303
|
+
): string {
|
|
290
304
|
const hasher = new Bun.CryptoHasher('sha256')
|
|
291
305
|
for (const file of [...files].sort((a, b) => a.path.localeCompare(b.path))) {
|
|
292
|
-
if (file.path === MANIFEST_FILE) continue
|
|
306
|
+
if (file.path === MANIFEST_FILE || file.path === PACKAGE_FILE) continue
|
|
293
307
|
hasher.update(file.path)
|
|
294
308
|
hasher.update('\0')
|
|
295
309
|
hasher.update(file.bytes ?? (file.text as string))
|
|
296
310
|
hasher.update('\n')
|
|
297
311
|
}
|
|
312
|
+
updateTrailer(hasher, trailer)
|
|
298
313
|
return `sha256-${hasher.digest('hex')}`
|
|
299
314
|
}
|
|
300
315
|
|
|
316
|
+
/** The manifest and the package's identity, after the files: what `bundleIntegrity` ends with. */
|
|
317
|
+
export function updateTrailer(
|
|
318
|
+
hasher: Bun.CryptoHasher,
|
|
319
|
+
trailer: { manifest: string; packageIdentity?: string },
|
|
320
|
+
): void {
|
|
321
|
+
hasher.update(`${MANIFEST_FILE}\0`)
|
|
322
|
+
hasher.update(trailer.manifest)
|
|
323
|
+
hasher.update('\n')
|
|
324
|
+
if (trailer.packageIdentity === undefined) return
|
|
325
|
+
hasher.update(`${PACKAGE_FILE}\0`)
|
|
326
|
+
hasher.update(trailer.packageIdentity)
|
|
327
|
+
hasher.update('\n')
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
export const PACKAGE_FILE = 'package.json'
|
|
331
|
+
|
|
332
|
+
/** `bundle.json` as it was hashed: with its `integrity` value blanked. */
|
|
333
|
+
export function blankedManifest(text: string, integrity: string): string {
|
|
334
|
+
return text.replace(`"integrity": ${JSON.stringify(integrity)}`, '"integrity": ""')
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/** JSON with every object's keys sorted, so the same value always reads the same. */
|
|
338
|
+
function stableJson(value: unknown): string {
|
|
339
|
+
if (Array.isArray(value)) return `[${value.map(stableJson).join(',')}]`
|
|
340
|
+
if (value && typeof value === 'object')
|
|
341
|
+
return `{${Object.keys(value)
|
|
342
|
+
.sort()
|
|
343
|
+
.map((key) => `${JSON.stringify(key)}:${stableJson((value as Record<string, unknown>)[key])}`)
|
|
344
|
+
.join(',')}}`
|
|
345
|
+
return JSON.stringify(value)
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* What the hash takes of a `package.json`: its name, version and `crombolo`
|
|
350
|
+
* field, keys sorted. Not its bytes, which a registry or a packer may normalise.
|
|
351
|
+
*/
|
|
352
|
+
export function packageIdentity(pkg: Record<string, unknown>): string {
|
|
353
|
+
return stableJson({ name: pkg.name, version: pkg.version, crombolo: pkg.crombolo })
|
|
354
|
+
}
|
|
355
|
+
|
|
301
356
|
export interface WriteOptions {
|
|
302
357
|
/**
|
|
303
358
|
* How values, media files and protected-page usernames travel: `open`,
|
|
@@ -313,6 +368,8 @@ export interface WriteOptions {
|
|
|
313
368
|
siteData?: readonly SiteDataTable[]
|
|
314
369
|
/** The site's `public/` files, which `public.json` lists and the caller's bytes follow. */
|
|
315
370
|
public?: readonly PublicSource[]
|
|
371
|
+
/** What the bundle is (story 306), when the set's manifest does not say already. */
|
|
372
|
+
bundleType?: BundleType
|
|
316
373
|
}
|
|
317
374
|
|
|
318
375
|
/** Without values, media files or usernames: what `omitted` writes. Pure. */
|
|
@@ -361,7 +418,9 @@ export function writeBundle(
|
|
|
361
418
|
})
|
|
362
419
|
const manifest = {
|
|
363
420
|
...planned.manifest,
|
|
364
|
-
integrity: bundleIntegrity([...planned.files, ...carried]
|
|
421
|
+
integrity: bundleIntegrity([...planned.files, ...carried], {
|
|
422
|
+
manifest: writeManifest({ ...planned.manifest, integrity: '' }),
|
|
423
|
+
}),
|
|
365
424
|
}
|
|
366
425
|
const publicFiles = (options.public ?? []).map(({ path, source }) => ({ path, source }))
|
|
367
426
|
return [
|
|
@@ -430,8 +489,12 @@ function planBundle(
|
|
|
430
489
|
const publicFiles = options.public ?? []
|
|
431
490
|
const people = options.people && seal ? sealPeople(options.people, seal) : undefined
|
|
432
491
|
const siteData = options.siteData && seal ? sealSiteData(options.siteData, seal) : undefined
|
|
492
|
+
// A bundle written as a package takes what it is from the package when not
|
|
493
|
+
// given here; one read without a package must say (story 306).
|
|
494
|
+
const bundleType = options.bundleType ?? set.manifest.bundleType
|
|
433
495
|
const manifest: BundleManifest = {
|
|
434
496
|
...set.manifest,
|
|
497
|
+
bundleType,
|
|
435
498
|
formatVersion: BUNDLE_FORMAT_VERSION,
|
|
436
499
|
// Without values nothing names a media file, so none is expected either.
|
|
437
500
|
publicManifest: publicFiles.length > 0 ? PUBLIC_MANIFEST : undefined,
|