kcode-web-libs 0.2.0

Managed plain HTML, CSS, and JavaScript libraries with browser checks and immutable publication
Documentation
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
use crate::model::{
    DOCUMENTATION_PATH, File, MANIFEST_PATH, MAX_FILE_BYTES, MAX_FILES, MAX_TREE_BYTES,
    ValidatedTree, validate_module_name, validate_tree,
};
use crate::storage;
use crate::{Error, Lib, Result};
use fs2::FileExt;
use std::fs::{self, OpenOptions};
use std::io::Write;
use std::path::{Path, PathBuf};
use uuid::Uuid;

struct HeadReplaceFailure {
    error: Error,
    head_replaced: bool,
}

pub(crate) fn create(web_libs_root: &Path, publications_root: &Path, name: &str) -> Result<Lib> {
    validate_module_name(name)?;
    let root = root_path(web_libs_root, true)?;
    let publications_root = crate::publication::root_path(publications_root, true)?;
    ensure_separate_roots(&root, &publications_root)?;
    let _root_lock = lock_file(&root.join(".kcode-web-libs.lock"))?;
    let library_root = root.join(name);
    match fs::symlink_metadata(&library_root) {
        Ok(_) => {
            return Err(Error::new(format!(
                "already_exists: managed web library `{name}` already exists"
            )));
        }
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
        Err(error) => return Err(Error::io("inspect library destination", error)),
    }

    let mut files = initial_files(name);
    let validated = validate_tree(&files, name)?;
    files.sort_by(|left, right| left.path.cmp(&right.path));
    let staging = root.join(format!(".create-{}", Uuid::new_v4()));
    fs::create_dir(&staging).map_err(|error| Error::io("create staged managed library", error))?;

    let creation = (|| {
        let lock_path = staging.join(".lock");
        let lock_file = OpenOptions::new()
            .create_new(true)
            .write(true)
            .open(&lock_path)
            .map_err(|error| Error::io("create repository lock file", error))?;
        lock_file
            .sync_all()
            .map_err(|error| Error::io("sync repository lock file", error))?;

        let generations = staging.join("generations");
        fs::create_dir(&generations)
            .map_err(|error| Error::io("create generations directory", error))?;
        let generation = Uuid::new_v4().to_string();
        let generation_root = generations.join(&generation);
        fs::create_dir(&generation_root)
            .map_err(|error| Error::io("create initial generation", error))?;
        materialize_source(&generation_root, &files)?;
        storage::sync_tree_directories(&generation_root)?;
        storage::sync_directory(&generations)?;

        let head_path = staging.join("HEAD");
        let mut head = OpenOptions::new()
            .create_new(true)
            .write(true)
            .open(&head_path)
            .map_err(|error| Error::io("create initial repository HEAD", error))?;
        head.write_all(format!("{generation}\n").as_bytes())
            .map_err(|error| Error::io("write initial repository HEAD", error))?;
        head.sync_all()
            .map_err(|error| Error::io("sync initial repository HEAD", error))?;
        drop(head);
        storage::sync_directory(&staging)?;

        fs::rename(&staging, &library_root)
            .map_err(|error| Error::io("commit new managed library", error))?;
        if let Err(error) = storage::sync_directory(&root) {
            return Err(Error::new(format!(
                "source_create_commit_uncertain: managed web library `{name}` was installed but \
                 the source root could not be synchronized: {error}"
            )));
        }

        Ok(generation)
    })();

    let generation = match creation {
        Ok(generation) => generation,
        Err(error) => {
            if fs::remove_dir_all(&staging).is_ok() {
                let _ = storage::sync_directory(&root);
            }
            return Err(error);
        }
    };

    Ok(Lib::from_parts(
        root,
        publications_root,
        name.to_owned(),
        generation,
        files_with_validated_order(files, &validated),
    ))
}

pub(crate) fn open(web_libs_root: &Path, publications_root: &Path, name: &str) -> Result<Lib> {
    validate_module_name(name)?;
    let root = root_path(web_libs_root, false)?;
    let publications_root = crate::publication::root_path(publications_root, true)?;
    ensure_separate_roots(&root, &publications_root)?;
    let library_root = checked_repository(&root.join(name), name)?;
    ensure_complete_layout(&library_root, name)?;
    let lock = lock_file(&library_root.join(".lock"))?;
    ensure_complete_layout(&library_root, name)?;
    let generation = read_head(&library_root)?;
    let files = read_generation(&library_root, &generation)?;
    validate_tree(&files, name)?;
    drop(lock);

    Ok(Lib::from_parts(
        root,
        publications_root,
        name.to_owned(),
        generation,
        files,
    ))
}

pub(crate) fn docs(web_libs_root: &Path, name: &str) -> Result<(String, String)> {
    validate_module_name(name)?;
    let root = root_path(web_libs_root, false)?;
    let library_root = checked_repository(&root.join(name), name)?;
    ensure_complete_layout(&library_root, name)?;
    let lock = lock_file(&library_root.join(".lock"))?;
    ensure_complete_layout(&library_root, name)?;
    let generation = read_head(&library_root)?;
    let generation_root = checked_generation(&library_root, &generation)?;
    let manifest_contents = read_regular_utf8(&generation_root.join(MANIFEST_PATH))?;
    let documentation = read_regular_utf8(&generation_root.join(DOCUMENTATION_PATH))?;
    let manifest = crate::model::validate_manifest(&manifest_contents, name)?;
    drop(lock);

    Ok((manifest.version, documentation))
}

pub(crate) fn write(
    root: &Path,
    name: &str,
    expected_generation: &str,
    files: &[File],
) -> Result<String> {
    validate_module_name(name)?;
    validate_generation(expected_generation)?;
    validate_tree(files, name)?;

    let library_root = root.join(name);
    let lock = lock_file(&library_root.join(".lock"))?;
    ensure_complete_layout(&library_root, name)?;

    let current = read_head(&library_root)?;
    if current != expected_generation {
        return Err(stale_error(expected_generation, &current));
    }

    let generation = write_generation(&library_root, files)?;
    if let Err(failure) = replace_head(&library_root, &generation) {
        if !failure.head_replaced {
            let generations_root = library_root.join("generations");
            let _ = fs::remove_dir_all(generations_root.join(generation.as_str()));
            let _ = storage::sync_directory(&generations_root);
        }
        return Err(failure.error);
    }

    let generations_root = library_root.join("generations");
    if fs::remove_dir_all(generations_root.join(&current)).is_ok() {
        let _ = storage::sync_directory(&generations_root);
    }

    drop(lock);
    Ok(generation)
}

fn initial_files(name: &str) -> Vec<File> {
    let manifest = serde_json::json!({
        "name": name,
        "version": "0.1.0",
        "entry": "index.js",
        "tests": "tests.js"
    });

    vec![
        File::new(
            MANIFEST_PATH,
            format!(
                "{}\n",
                serde_json::to_string_pretty(&manifest)
                    .expect("serializing a fixed manifest cannot fail")
            ),
        ),
        File::new(
            DOCUMENTATION_PATH,
            format!("# {name}\n\nA buildless ES module managed by `kcode-web-libs`.\n"),
        ),
        File::new(
            "index.js",
            "export function mount(root) {\n  root.textContent = \"Hello from a kcode web library.\";\n}\n",
        ),
        File::new(
            "tests.js",
            "export async function runTests() {\n  const module = await import(\"./index.js\");\n  if (typeof module.mount !== \"function\") {\n    throw new Error(\"index.js must export mount\");\n  }\n}\n",
        ),
    ]
}

fn files_with_validated_order(mut files: Vec<File>, _validated: &ValidatedTree) -> Vec<File> {
    files.sort_by(|left, right| left.path.cmp(&right.path));
    files
}

fn lock_file(path: &Path) -> Result<fs::File> {
    match fs::symlink_metadata(path) {
        Ok(metadata) if metadata.file_type().is_symlink() || !metadata.is_file() => {
            return Err(Error::new(format!(
                "unsafe_source: lock path is not a regular file: {}",
                path.display()
            )));
        }
        Ok(_) => {}
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
        Err(error) => return Err(Error::io("inspect lock file", error)),
    }
    let file = OpenOptions::new()
        .create(true)
        .truncate(false)
        .read(true)
        .write(true)
        .open(path)
        .map_err(|error| Error::io("open library lock", error))?;
    if !file
        .metadata()
        .map_err(|error| Error::io("inspect opened lock file", error))?
        .is_file()
    {
        return Err(Error::new(format!(
            "unsafe_source: opened lock path is not a regular file: {}",
            path.display()
        )));
    }
    FileExt::lock_exclusive(&file).map_err(|error| Error::io("lock library", error))?;
    Ok(file)
}

fn ensure_complete_layout(library_root: &Path, name: &str) -> Result<()> {
    let head = fs::symlink_metadata(library_root.join("HEAD"));
    let lock = fs::symlink_metadata(library_root.join(".lock"));
    let generations = fs::symlink_metadata(library_root.join("generations"));

    match (head, lock, generations) {
        (Ok(head), Ok(lock), Ok(generations))
            if head.file_type().is_file() && lock.file_type().is_file() && generations.is_dir() =>
        {
            Ok(())
        }
        (Err(head), Err(lock), Err(generations))
            if head.kind() == std::io::ErrorKind::NotFound
                && lock.kind() == std::io::ErrorKind::NotFound
                && generations.kind() == std::io::ErrorKind::NotFound =>
        {
            Err(Error::new(format!(
                "invalid_repository: managed web library `{name}` is uninitialized"
            )))
        }
        (Err(error), _, _) if error.kind() != std::io::ErrorKind::NotFound => {
            Err(Error::io("inspect HEAD", error))
        }
        (_, Err(error), _) if error.kind() != std::io::ErrorKind::NotFound => {
            Err(Error::io("inspect repository lock", error))
        }
        (_, _, Err(error)) if error.kind() != std::io::ErrorKind::NotFound => {
            Err(Error::io("inspect generations", error))
        }
        _ => Err(Error::new(format!(
            "invalid_repository: managed web library `{name}` has an incomplete layout"
        ))),
    }
}

fn root_path(path: &Path, create: bool) -> Result<PathBuf> {
    if create {
        storage::create_dir_all_durable(path, "create managed-library root")?;
    }
    let absolute = if path.is_absolute() {
        path.to_path_buf()
    } else {
        std::env::current_dir()
            .map_err(|error| Error::io("read current directory", error))?
            .join(path)
    };
    let metadata = fs::symlink_metadata(&absolute)
        .map_err(|error| Error::io("inspect managed-library root", error))?;
    if metadata.file_type().is_symlink() || !metadata.is_dir() {
        return Err(Error::new(format!(
            "unsafe_source: managed-library root is not a regular directory: {}",
            absolute.display()
        )));
    }
    fs::canonicalize(&absolute)
        .map_err(|error| Error::io("canonicalize managed-library root", error))
}

fn ensure_separate_roots(source: &Path, publications: &Path) -> Result<()> {
    if source.starts_with(publications) || publications.starts_with(source) {
        return Err(Error::new(
            "invalid_publication_storage: source and publication roots must be disjoint",
        ));
    }
    Ok(())
}

fn checked_repository(path: &Path, name: &str) -> Result<PathBuf> {
    match fs::symlink_metadata(path) {
        Ok(metadata) if metadata.file_type().is_symlink() => Err(Error::new(format!(
            "unsafe_source: managed web library `{name}` is a symlink"
        ))),
        Ok(metadata) if metadata.is_dir() => Ok(path.to_path_buf()),
        Ok(_) => Err(Error::new(format!(
            "invalid_repository: managed web library `{name}` is not a directory"
        ))),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Err(Error::new(format!(
            "not_found: managed web library `{name}` does not exist"
        ))),
        Err(error) => Err(Error::io("inspect managed web library", error)),
    }
}

fn materialize_source(root: &Path, files: &[File]) -> Result<()> {
    for file in files {
        let destination = root.join(&file.path);
        let parent = destination.parent().ok_or_else(|| {
            Error::new(format!(
                "unsafe_path: source path has no parent: `{}`",
                file.path
            ))
        })?;
        fs::create_dir_all(parent).map_err(|error| Error::io("create source directory", error))?;
        let mut output = OpenOptions::new()
            .create_new(true)
            .write(true)
            .open(&destination)
            .map_err(|error| Error::io("create source file", error))?;
        output
            .write_all(file.contents.as_bytes())
            .map_err(|error| Error::io("write source file", error))?;
        output
            .sync_all()
            .map_err(|error| Error::io("sync source file", error))?;
    }
    Ok(())
}

fn read_head(library_root: &Path) -> Result<String> {
    let path = library_root.join("HEAD");
    let value = read_regular_utf8(&path)?;
    let generation = value.trim_end_matches(['\r', '\n']);
    if generation.len() != value.trim().len() || generation != value.trim() {
        return Err(Error::new(
            "invalid_repository: HEAD contains surrounding whitespace",
        ));
    }
    validate_generation(generation)?;
    Ok(generation.to_owned())
}

fn validate_generation(generation: &str) -> Result<()> {
    let parsed = Uuid::parse_str(generation)
        .map_err(|_| Error::new("invalid_repository: invalid generation identifier"))?;
    if parsed.to_string() != generation {
        return Err(Error::new(
            "invalid_repository: generation identifier is not canonical",
        ));
    }
    Ok(())
}

fn stale_error(expected: &str, current: &str) -> Error {
    Error::new(format!(
        "stale_snapshot: expected generation `{expected}`, current generation is `{current}`; reopen and reconcile"
    ))
}

fn write_generation(library_root: &Path, files: &[File]) -> Result<String> {
    let generation = Uuid::new_v4().to_string();
    let generations_root = library_root.join("generations");
    let staging = generations_root.join(format!(".staging-{generation}"));
    let final_path = generations_root.join(&generation);

    fs::create_dir(&staging).map_err(|error| Error::io("create staged generation", error))?;

    let staging_write = (|| {
        materialize_source(&staging, files)?;
        storage::sync_tree_directories(&staging)
    })();

    if let Err(error) = staging_write {
        remove_staged_generation(&staging, &generations_root);
        return Err(error);
    }

    if let Err(error) = fs::rename(&staging, &final_path) {
        remove_staged_generation(&staging, &generations_root);
        return Err(Error::io("commit source generation", error));
    }

    if let Err(error) = storage::sync_directory(&generations_root) {
        return Err(Error::new(format!(
            "source_generation_commit_uncertain: generation `{generation}` was installed but \
             its parent directory could not be synchronized: {error}"
        )));
    }

    Ok(generation)
}

fn replace_head(
    library_root: &Path,
    generation: &str,
) -> std::result::Result<(), HeadReplaceFailure> {
    let temporary = library_root.join(format!(".HEAD-{}.tmp", Uuid::new_v4()));
    let mut file = OpenOptions::new()
        .create_new(true)
        .write(true)
        .open(&temporary)
        .map_err(|error| HeadReplaceFailure {
            error: Error::io("create temporary HEAD", error),
            head_replaced: false,
        })?;
    let preparation = file
        .write_all(format!("{generation}\n").as_bytes())
        .map_err(|error| Error::io("write temporary HEAD", error))
        .and_then(|()| {
            file.sync_all()
                .map_err(|error| Error::io("sync temporary HEAD", error))
        });
    drop(file);
    if let Err(error) = preparation {
        remove_temporary_head(&temporary, library_root);
        return Err(HeadReplaceFailure {
            error,
            head_replaced: false,
        });
    }

    if let Err(error) = fs::rename(&temporary, library_root.join("HEAD")) {
        remove_temporary_head(&temporary, library_root);
        return Err(HeadReplaceFailure {
            error: Error::io("replace HEAD", error),
            head_replaced: false,
        });
    }

    storage::sync_directory(library_root).map_err(|error| HeadReplaceFailure {
        error: Error::new(format!(
            "source_commit_uncertain: HEAD was replaced with generation `{generation}` but the \
             library directory could not be synchronized: {error}"
        )),
        head_replaced: true,
    })?;

    Ok(())
}

fn remove_staged_generation(staging: &Path, generations_root: &Path) {
    if fs::remove_dir_all(staging).is_ok() {
        let _ = storage::sync_directory(generations_root);
    }
}

fn remove_temporary_head(temporary: &Path, library_root: &Path) {
    if fs::remove_file(temporary).is_ok() {
        let _ = storage::sync_directory(library_root);
    }
}

fn read_generation(library_root: &Path, generation: &str) -> Result<Vec<File>> {
    validate_generation(generation)?;
    let root = checked_generation(library_root, generation)?;

    let mut files = Vec::new();
    let mut total_bytes = 0usize;
    read_directory(&root, &root, &mut files, &mut total_bytes)?;
    files.sort_by(|left, right| left.path.cmp(&right.path));
    Ok(files)
}

fn checked_generation(library_root: &Path, generation: &str) -> Result<PathBuf> {
    let path = library_root.join("generations").join(generation);
    let metadata =
        fs::symlink_metadata(&path).map_err(|error| Error::io("inspect generation", error))?;
    if metadata.file_type().is_symlink() || !metadata.is_dir() {
        return Err(Error::new(
            "invalid_repository: generation is not a regular directory",
        ));
    }
    Ok(path)
}

fn read_regular_utf8(path: &Path) -> Result<String> {
    let metadata = fs::symlink_metadata(path)
        .map_err(|error| Error::io("inspect required source file", error))?;
    if metadata.file_type().is_symlink() || !metadata.is_file() {
        return Err(Error::new(format!(
            "unsafe_source: required source is not a regular file: {}",
            path.display()
        )));
    }
    let bytes = fs::read(path).map_err(|error| Error::io("read source file", error))?;
    String::from_utf8(bytes).map_err(|_| {
        Error::new(format!(
            "unsafe_source: source file is not UTF-8: {}",
            path.display()
        ))
    })
}

fn read_directory(
    root: &Path,
    directory: &Path,
    files: &mut Vec<File>,
    total_bytes: &mut usize,
) -> Result<()> {
    let mut entries = fs::read_dir(directory)
        .map_err(|error| Error::io("read generation directory", error))?
        .collect::<std::result::Result<Vec<_>, _>>()
        .map_err(|error| Error::io("read generation entry", error))?;
    entries.sort_by_key(|entry| entry.file_name());

    for entry in entries {
        let path = entry.path();
        let metadata = fs::symlink_metadata(&path)
            .map_err(|error| Error::io("inspect generation entry", error))?;
        if metadata.file_type().is_symlink() {
            return Err(Error::new(
                "invalid_repository: source generations may not contain symlinks",
            ));
        }
        if metadata.is_dir() {
            read_directory(root, &path, files, total_bytes)?;
            continue;
        }
        if !metadata.is_file() {
            return Err(Error::new(
                "invalid_repository: source generations may contain only regular files",
            ));
        }

        if files.len() >= MAX_FILES {
            return Err(Error::invalid_source(format!(
                "a web library may contain at most {MAX_FILES} files"
            )));
        }
        let length = usize::try_from(metadata.len())
            .map_err(|_| Error::invalid_source("source file length does not fit usize"))?;
        if length > MAX_FILE_BYTES {
            return Err(Error::invalid_source(format!(
                "a source file exceeds the {MAX_FILE_BYTES} byte limit"
            )));
        }
        *total_bytes = total_bytes
            .checked_add(length)
            .ok_or_else(|| Error::invalid_source("source byte count overflow"))?;
        if *total_bytes > MAX_TREE_BYTES {
            return Err(Error::invalid_source(format!(
                "the source tree exceeds the {MAX_TREE_BYTES} byte limit"
            )));
        }

        let relative = path
            .strip_prefix(root)
            .map_err(|_| Error::new("invalid_repository: source path escaped generation"))?;
        let relative = relative
            .to_str()
            .ok_or_else(|| Error::new("invalid_repository: source path is not valid UTF-8"))?;
        let relative = relative.replace(std::path::MAIN_SEPARATOR, "/");
        crate::model::validate_file_path(&relative)?;

        let bytes = fs::read(&path).map_err(|error| Error::io("read source file", error))?;
        let contents = String::from_utf8(bytes)
            .map_err(|_| Error::invalid_source(format!("`{relative}` is not valid UTF-8")))?;
        files.push(File::new(relative, contents));
    }

    Ok(())
}