topotopo 0.4.0 → 0.5.1
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/NOTICE +10 -10
- package/README.md +460 -117
- package/dist/cli.js +327 -147
- package/dist/config.d.ts +48 -0
- package/dist/config.js +170 -0
- package/dist/core/bake.d.ts +14 -3
- package/dist/core/bake.js +153 -4
- package/dist/core/bevel.d.ts +3 -1
- package/dist/core/bevel.js +76 -3
- package/dist/core/decimate.d.ts +65 -0
- package/dist/core/decimate.js +409 -3
- package/dist/core/erosion.d.ts +37 -0
- package/dist/core/erosion.js +41 -1
- package/dist/core/geometry.d.ts +5 -1
- package/dist/core/geometry.js +203 -3
- package/dist/core/heightfield.d.ts +27 -4
- package/dist/core/heightfield.js +54 -16
- package/dist/core/intrinsics.d.ts +7 -0
- package/dist/core/intrinsics.js +94 -0
- package/dist/core/manifold.d.ts +12 -1
- package/dist/core/manifold.js +78 -6
- package/dist/core/materials.d.ts +7 -0
- package/dist/core/materials.js +45 -0
- package/dist/core/noise.d.ts +9 -0
- package/dist/core/noise.js +10 -1
- package/dist/core/polymerge.d.ts +21 -0
- package/dist/core/polymerge.js +327 -0
- package/dist/core/repair.d.ts +2 -0
- package/dist/core/repair.js +2 -0
- package/dist/core/report.d.ts +70 -0
- package/dist/core/report.js +275 -0
- package/dist/core/scene.js +2 -1
- package/dist/core/sha256.d.ts +4 -0
- package/dist/core/sha256.js +69 -0
- package/dist/core/shapes.d.ts +3 -0
- package/dist/core/shapes.js +34 -0
- package/dist/core/snippet-lib.d.ts +14 -1
- package/dist/core/snippet-lib.js +39 -13
- package/dist/core/tangents.d.ts +2 -0
- package/dist/core/tangents.js +5 -1
- package/dist/core/texnoise.d.ts +6 -1
- package/dist/core/texnoise.js +6 -2
- package/dist/core/texsdf.d.ts +131 -0
- package/dist/core/texsdf.js +637 -0
- package/dist/core/texture.d.ts +163 -9
- package/dist/core/texture.js +844 -20
- package/dist/core/topology.d.ts +10 -0
- package/dist/core/topology.js +168 -0
- package/dist/core/unwrap.d.ts +72 -0
- package/dist/core/unwrap.js +800 -0
- package/dist/core/usercode.d.ts +27 -0
- package/dist/core/usercode.js +87 -0
- package/dist/core/uv.d.ts +4 -18
- package/dist/core/uv.js +35 -188
- package/dist/core/uvislands.d.ts +62 -0
- package/dist/core/uvislands.js +574 -0
- package/dist/core/uvoverlap.d.ts +58 -0
- package/dist/core/uvoverlap.js +220 -0
- package/dist/core/uvpack.d.ts +64 -0
- package/dist/core/uvpack.js +628 -0
- package/dist/core/uvreport.d.ts +61 -0
- package/dist/core/uvreport.js +207 -143
- package/dist/core/wrangle.d.ts +6 -1
- package/dist/core/wrangle.js +27 -16
- package/dist/core/xatlas.d.ts +16 -1
- package/dist/core/xatlas.js +66 -62
- package/dist/engines.d.ts +63 -0
- package/dist/engines.js +180 -0
- package/dist/explain.d.ts +4 -3
- package/dist/explain.js +129 -12
- package/dist/graph/engine.d.ts +92 -29
- package/dist/graph/engine.js +298 -148
- package/dist/graph/expr.d.ts +6 -3
- package/dist/graph/expr.js +18 -8
- package/dist/graph/outputs.d.ts +23 -0
- package/dist/graph/outputs.js +41 -0
- package/dist/graph/packages.d.ts +72 -0
- package/dist/graph/packages.js +261 -0
- package/dist/graph/parallel.d.ts +3 -1
- package/dist/graph/parallel.js +4 -1
- package/dist/graph/params.d.ts +6 -0
- package/dist/graph/params.js +34 -7
- package/dist/graph/scope.d.ts +55 -0
- package/dist/graph/scope.js +14 -0
- package/dist/graph/types.d.ts +75 -12
- package/dist/graph/types.js +4 -2
- package/dist/graph/vars.d.ts +16 -0
- package/dist/graph/vars.js +80 -0
- package/dist/host/artifacts.d.ts +10 -1
- package/dist/host/artifacts.js +37 -6
- package/dist/host/cook.d.ts +241 -0
- package/dist/host/cook.js +844 -0
- package/dist/host/export-targets.d.ts +42 -5
- package/dist/host/export-targets.js +72 -13
- package/dist/host/godot-commands.d.ts +4 -0
- package/dist/host/godot-commands.js +21 -0
- package/dist/host/godot.d.ts +183 -0
- package/dist/host/godot.js +916 -0
- package/dist/host/node.d.ts +12 -7
- package/dist/host/node.js +27 -9
- package/dist/host/pool.js +51 -2
- package/dist/host/sidecars.d.ts +8 -0
- package/dist/host/sidecars.js +50 -0
- package/dist/host/types.d.ts +19 -6
- package/dist/host/version.d.ts +1 -0
- package/dist/host/version.js +3 -0
- package/dist/host/worker.js +5 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/inspect.d.ts +80 -2
- package/dist/inspect.js +265 -30
- package/dist/io/common.d.ts +25 -5
- package/dist/io/export.js +2 -1
- package/dist/io/files.d.ts +23 -4
- package/dist/io/files.js +60 -8
- package/dist/io/gltf.js +134 -47
- package/dist/io/heightmap.d.ts +3 -0
- package/dist/io/heightmap.js +1 -0
- package/dist/io/import.js +10 -0
- package/dist/io/inspect-scene.d.ts +55 -0
- package/dist/io/inspect-scene.js +155 -0
- package/dist/io/kinds.d.ts +81 -0
- package/dist/io/kinds.js +163 -0
- package/dist/io/points.d.ts +2 -1
- package/dist/io/points.js +3 -1
- package/dist/io/sidecars.d.ts +24 -0
- package/dist/io/sidecars.js +57 -0
- package/dist/io/source.d.ts +65 -0
- package/dist/io/source.js +112 -0
- package/dist/io/targets.d.ts +47 -3
- package/dist/io/targets.js +148 -13
- package/dist/io/textures.js +21 -1
- package/dist/io/unreal.js +5 -1
- package/dist/mcp/docs.d.ts +10 -1
- package/dist/mcp/docs.js +786 -143
- package/dist/mcp/inputs.d.ts +12 -5
- package/dist/mcp/inputs.js +50 -13
- package/dist/mcp/server.js +56 -27
- package/dist/mcp/tools.d.ts +14 -7
- package/dist/mcp/tools.js +198 -103
- package/dist/mcp/verify.d.ts +2 -2
- package/dist/mcp/verify.js +1 -1
- package/dist/mcp/workspace.d.ts +15 -0
- package/dist/mcp/workspace.js +349 -0
- package/dist/nodes/attributes.js +198 -1
- package/dist/nodes/bake.js +151 -47
- package/dist/nodes/beam.d.ts +1 -0
- package/dist/nodes/beam.js +201 -0
- package/dist/nodes/bevel.js +51 -3
- package/dist/nodes/cloth.js +2 -0
- package/dist/nodes/code.js +21 -9
- package/dist/nodes/curves.js +433 -93
- package/dist/nodes/flow.js +53 -33
- package/dist/nodes/generate.js +12 -38
- package/dist/nodes/heightfield.js +248 -77
- package/dist/nodes/imageops.js +16 -19
- package/dist/nodes/index.d.ts +1 -0
- package/dist/nodes/index.js +1 -0
- package/dist/nodes/material.js +89 -24
- package/dist/nodes/measure.js +43 -17
- package/dist/nodes/particles.js +2 -0
- package/dist/nodes/pcg.js +6 -5
- package/dist/nodes/procedural.js +4 -3
- package/dist/nodes/remesh.js +71 -15
- package/dist/nodes/scene.js +4 -2
- package/dist/nodes/sdf.js +10 -5
- package/dist/nodes/solid.js +20 -5
- package/dist/nodes/splines.d.ts +15 -1
- package/dist/nodes/splines.js +112 -105
- package/dist/nodes/texture.d.ts +21 -6
- package/dist/nodes/texture.js +1318 -185
- package/dist/nodes/topology.d.ts +7 -0
- package/dist/nodes/topology.js +133 -6
- package/dist/nodes/transform.js +6 -2
- package/dist/nodes/uv.js +192 -174
- package/dist/nodes/wasmcall.js +5 -1
- package/dist/open.d.ts +19 -0
- package/dist/open.js +12 -0
- package/dist/package.d.ts +288 -0
- package/dist/package.js +772 -0
- package/dist/packages/check.d.ts +32 -0
- package/dist/packages/check.js +169 -0
- package/dist/packages/commands.d.ts +57 -0
- package/dist/packages/commands.js +537 -0
- package/dist/packages/registry.d.ts +796 -0
- package/dist/packages/registry.js +2998 -0
- package/dist/packages/update.d.ts +124 -0
- package/dist/packages/update.js +476 -0
- package/dist/render/options.d.ts +2 -0
- package/dist/render/options.js +57 -5
- package/dist/render/preview.d.ts +17 -4
- package/dist/render/preview.js +6 -4
- package/dist/render/raster.d.ts +74 -5
- package/dist/render/raster.js +527 -143
- package/dist/render/texview.d.ts +6 -0
- package/dist/render/texview.js +27 -0
- package/dist/scan.d.ts +61 -0
- package/dist/scan.js +228 -0
- package/dist/session.d.ts +311 -0
- package/dist/session.js +849 -0
- package/dist/viewer/app/api.js +48 -6
- package/dist/viewer/app/cooking.js +78 -0
- package/dist/viewer/app/embed.js +27 -7
- package/dist/viewer/app/export-popover.js +118 -16
- package/dist/viewer/app/grid.js +5 -1
- package/dist/viewer/app/guides.js +122 -0
- package/dist/viewer/app/keymap.js +6 -3
- package/dist/viewer/app/layers.js +4 -4
- package/dist/viewer/app/main.js +185 -52
- package/dist/viewer/app/markers.js +60 -0
- package/dist/viewer/app/outliner.js +72 -27
- package/dist/viewer/app/output-info.js +45 -0
- package/dist/viewer/app/outputs.css +147 -8
- package/dist/viewer/app/outputs.js +168 -54
- package/dist/viewer/app/packages.js +614 -0
- package/dist/viewer/app/props.js +176 -52
- package/dist/viewer/app/scene.js +30 -5
- package/dist/viewer/app/select.js +4 -3
- package/dist/viewer/app/status.js +12 -15
- package/dist/viewer/app/style.css +65 -7
- package/dist/viewer/app/vars.js +47 -0
- package/dist/viewer/app/welcome.js +72 -0
- package/dist/viewer/graph-file.d.ts +62 -0
- package/dist/viewer/graph-file.js +175 -0
- package/dist/viewer/index.html +23 -10
- package/dist/viewer/json-edit.d.ts +13 -0
- package/dist/viewer/json-edit.js +274 -0
- package/dist/viewer/outputs.d.ts +49 -5
- package/dist/viewer/outputs.js +264 -56
- package/dist/viewer/packages.d.ts +121 -0
- package/dist/viewer/packages.js +584 -0
- package/dist/viewer/server.d.ts +14 -9
- package/dist/viewer/server.js +266 -28
- package/dist/web/app/web.css +419 -0
- package/dist/web/app/web.js +188 -0
- package/dist/{gallery → web}/bake.d.ts +4 -4
- package/dist/{gallery → web}/bake.js +24 -15
- package/dist/web/folder-page.d.ts +101 -0
- package/dist/web/folder-page.js +146 -0
- package/dist/web/folders.d.ts +280 -0
- package/dist/web/folders.js +970 -0
- package/dist/web/library.d.ts +91 -0
- package/dist/web/library.js +228 -0
- package/dist/web/markdown.d.ts +45 -0
- package/dist/web/markdown.js +677 -0
- package/dist/web/pages.d.ts +258 -0
- package/dist/web/pages.js +301 -0
- package/dist/web/server.d.ts +24 -0
- package/dist/web/server.js +1068 -0
- package/dist/workspace.d.ts +88 -118
- package/dist/workspace.js +358 -460
- package/integrations/godot/addons/topotopo/LICENSE +202 -0
- package/integrations/godot/addons/topotopo/README.md +57 -0
- package/integrations/godot/addons/topotopo/context_menu.gd +43 -0
- package/integrations/godot/addons/topotopo/dock.gd +266 -0
- package/integrations/godot/addons/topotopo/dock.tscn +72 -0
- package/integrations/godot/addons/topotopo/export_filter.gd +72 -0
- package/integrations/godot/addons/topotopo/gltf_instancing.gd +131 -0
- package/integrations/godot/addons/topotopo/icons/topotopo.svg +1 -0
- package/integrations/godot/addons/topotopo/inspector.gd +38 -0
- package/integrations/godot/addons/topotopo/instances_post_import.gd +111 -0
- package/integrations/godot/addons/topotopo/package.gd +23 -0
- package/integrations/godot/addons/topotopo/package_importer.gd +187 -0
- package/integrations/godot/addons/topotopo/plugin.cfg +7 -0
- package/integrations/godot/addons/topotopo/plugin.gd +970 -0
- package/integrations/godot/addons/topotopo/record.gd +435 -0
- package/integrations/godot/addons/topotopo/runner.gd +458 -0
- package/integrations/godot/addons/topotopo/tools/check.gd +42 -0
- package/integrations/godot/addons/topotopo/watcher.gd +199 -0
- package/kernels/pkg/README.md +2 -1
- package/kernels/pkg-simd/README.md +2 -1
- package/library/architecture/versions/1.0/architecture.topo +12 -0
- package/{shelf → library/architecture/versions/1.0/graphs}/arch.json +3 -3
- package/{shelf → library/architecture/versions/1.0/graphs}/beam_truss.json +6 -6
- package/{shelf → library/architecture/versions/1.0/graphs}/brick_wall.json +8 -8
- package/{shelf → library/architecture/versions/1.0/graphs}/door_frame.json +3 -3
- package/{shelf → library/architecture/versions/1.0/graphs}/hex_grid_floor.json +3 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/house.json +8 -8
- package/library/architecture/versions/1.0/graphs/lineup.json +42 -0
- package/{shelf → library/architecture/versions/1.0/graphs}/pillar.json +3 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/railing.json +5 -5
- package/{shelf → library/architecture/versions/1.0/graphs}/roof_gable.json +2 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/roof_hip.json +2 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/roof_skeleton.json +2 -1
- package/{shelf → library/architecture/versions/1.0/graphs}/scaffold.json +4 -4
- package/{shelf → library/architecture/versions/1.0/graphs}/spiral_stairs.json +3 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/stairs.json +6 -6
- package/{shelf → library/architecture/versions/1.0/graphs}/tile_floor.json +3 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/wall_segment.json +10 -10
- package/{shelf → library/architecture/versions/1.0/graphs}/window_frame.json +2 -2
- package/library/architecture/versions/1.0/version.json +1119 -0
- package/{shelf → library/nature/versions/1.0/graphs}/bush.json +2 -2
- package/{shelf → library/nature/versions/1.0/graphs}/cliff_block.json +4 -3
- package/{shelf → library/nature/versions/1.0/graphs}/forest_patch.json +5 -5
- package/{shelf → library/nature/versions/1.0/graphs}/grass_clumps.json +2 -2
- package/library/nature/versions/1.0/graphs/lineup.json +24 -0
- package/{shelf → library/nature/versions/1.0/graphs}/log.json +6 -6
- package/{shelf → library/nature/versions/1.0/graphs}/rock.json +5 -4
- package/{shelf → library/nature/versions/1.0/graphs}/scatter_rocks.json +5 -5
- package/{shelf → library/nature/versions/1.0/graphs}/tree_lowpoly.json +3 -2
- package/library/nature/versions/1.0/nature.topo +12 -0
- package/library/nature/versions/1.0/version.json +534 -0
- package/{shelf → library/props/versions/1.0/graphs}/barrel.json +5 -4
- package/{shelf → library/props/versions/1.0/graphs}/crate.json +3 -3
- package/library/props/versions/1.0/graphs/lineup.json +18 -0
- package/{shelf → library/props/versions/1.0/graphs}/pallet.json +4 -3
- package/{shelf → library/props/versions/1.0/graphs}/pipe_run.json +3 -3
- package/{shelf → library/props/versions/1.0/graphs}/sandbag_wall.json +5 -5
- package/library/props/versions/1.0/props.topo +12 -0
- package/library/props/versions/1.0/version.json +336 -0
- package/{shelf → library/street/versions/1.0/graphs}/bench.json +5 -5
- package/{shelf → library/street/versions/1.0/graphs}/bollard.json +2 -1
- package/{shelf → library/street/versions/1.0/graphs}/cable.json +4 -4
- package/{shelf → library/street/versions/1.0/graphs}/fence.json +7 -7
- package/{shelf → library/street/versions/1.0/graphs}/lamp_post.json +2 -2
- package/library/street/versions/1.0/graphs/lineup.json +22 -0
- package/{shelf → library/street/versions/1.0/graphs}/road_strip.json +2 -2
- package/{shelf → library/street/versions/1.0/graphs}/sign_post.json +1 -1
- package/library/street/versions/1.0/street.topo +12 -0
- package/library/street/versions/1.0/version.json +421 -0
- package/package.json +13 -8
- package/dist/gallery/app/gallery.css +0 -100
- package/dist/gallery/app/gallery.js +0 -80
- package/dist/gallery/cli.d.ts +0 -4
- package/dist/gallery/cli.js +0 -157
- package/dist/gallery/library.d.ts +0 -124
- package/dist/gallery/library.js +0 -366
- package/dist/gallery/pages.d.ts +0 -94
- package/dist/gallery/pages.js +0 -148
- package/dist/gallery/server.d.ts +0 -21
- package/dist/gallery/server.js +0 -391
- package/dist/graph/migrate.d.ts +0 -23
- package/dist/graph/migrate.js +0 -58
- package/dist/mcp/library.d.ts +0 -12
- package/dist/mcp/library.js +0 -159
- package/shelf/index.json +0 -190
|
@@ -0,0 +1,970 @@
|
|
|
1
|
+
// The web app's folder pages, below the pages themselves (src/web/folder-page.ts): every registered workspace and
|
|
2
|
+
// library is a tree of folders, and this file reads that tree. Paths from a URL are checked to stay inside their root
|
|
3
|
+
// (no "..", no dot names, no symlink leading out); the plumbing a page never shows is left out (dot folders,
|
|
4
|
+
// node_modules, topotopo-packages, a package's versions/ and packages/, the engines' caches); a folder is listed with
|
|
5
|
+
// its subfolders and its files by type (images, audio, video, models, notes, the rest), the files engines write
|
|
6
|
+
// beside ours hidden and Unreal's assets counted; links in notes are resolved (relative to the note's folder, res://
|
|
7
|
+
// to the Godot project, /Game/ to Unreal's Content/, YouTube and Vimeo to their privacy players); files are served
|
|
8
|
+
// with ranges; and a tree is audited for its flags: links to missing files, previews that did not cook, versions not
|
|
9
|
+
// published, exports older than their graphs, published outputs no package of the workspace uses.
|
|
10
|
+
import fs from 'node:fs';
|
|
11
|
+
import path from 'node:path';
|
|
12
|
+
import { MANIFEST_EXT } from '../session.js';
|
|
13
|
+
import { engineAbove, engineTypeOf, godotProjectOf } from '../engines.js';
|
|
14
|
+
import { canonicalJSON } from '../io/source.js';
|
|
15
|
+
import { sha256Hex } from '../core/sha256.js';
|
|
16
|
+
import { jsonFormat, SIDECAR_FORMATS, SIDECAR_HEAD_BYTES } from '../io/sidecars.js';
|
|
17
|
+
import { skippedName, isCheckout, WORKSPACE_FILE } from '../scan.js';
|
|
18
|
+
import { escHtml, markdownLinks, renderMarkdown, slug } from './markdown.js';
|
|
19
|
+
export const rootKey = (r) => `${r.kind}/${r.name}`;
|
|
20
|
+
const KINDS = {
|
|
21
|
+
'.png': 'image', '.jpg': 'image', '.jpeg': 'image', '.webp': 'image', '.gif': 'image', '.avif': 'image', '.bmp': 'image',
|
|
22
|
+
'.wav': 'audio', '.ogg': 'audio', '.mp3': 'audio', '.flac': 'audio',
|
|
23
|
+
'.mp4': 'video', '.webm': 'video',
|
|
24
|
+
'.glb': 'model', '.gltf': 'model',
|
|
25
|
+
'.md': 'note', '.txt': 'note',
|
|
26
|
+
};
|
|
27
|
+
export const kindOf = (name) => KINDS[path.extname(name).toLowerCase()] ?? 'other';
|
|
28
|
+
/** The media types files are served with; anything else is application/octet-stream. */
|
|
29
|
+
export const MIME_TYPES = {
|
|
30
|
+
'.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.webp': 'image/webp', '.gif': 'image/gif', '.avif': 'image/avif', '.bmp': 'image/bmp',
|
|
31
|
+
'.wav': 'audio/wav', '.ogg': 'audio/ogg', '.mp3': 'audio/mpeg', '.flac': 'audio/flac',
|
|
32
|
+
'.mp4': 'video/mp4', '.webm': 'video/webm',
|
|
33
|
+
'.glb': 'model/gltf-binary', '.gltf': 'model/gltf+json',
|
|
34
|
+
'.md': 'text/plain; charset=utf-8', '.txt': 'text/plain; charset=utf-8', '.log': 'text/plain; charset=utf-8', '.csv': 'text/csv; charset=utf-8',
|
|
35
|
+
'.json': 'application/json; charset=utf-8', '.pdf': 'application/pdf',
|
|
36
|
+
'.svg': 'image/svg+xml', '.html': 'text/html; charset=utf-8', '.htm': 'text/html; charset=utf-8',
|
|
37
|
+
};
|
|
38
|
+
/** Shown in the browser; everything else is a download (SVG and HTML among them: they run scripts). */
|
|
39
|
+
const INLINE = new Set(['.png', '.jpg', '.jpeg', '.webp', '.gif', '.avif', '.bmp', '.wav', '.ogg', '.mp3', '.flac', '.mp4', '.webm', '.glb', '.gltf', '.bin', '.md', '.txt', '.log', '.csv', '.json', '.pdf']);
|
|
40
|
+
/** Files Godot writes beside the files it imports: hidden. */
|
|
41
|
+
const GODOT_SIDE = /\.(import|uid|translation)$/i;
|
|
42
|
+
/** Unreal's assets: counted, as one line. */
|
|
43
|
+
const UNREAL_ASSET = /\.(uasset|umap)$/i;
|
|
44
|
+
const isTopo = (f) => f.endsWith(MANIFEST_EXT) && f.length > MANIFEST_EXT.length;
|
|
45
|
+
/**
|
|
46
|
+
* Why a folder named `name` is plumbing no page shows, or null: what the scan never enters (src/scan.ts: dot folders,
|
|
47
|
+
* node_modules, topotopo-packages, the engines' caches and build folders), and a package's versions/ and packages/.
|
|
48
|
+
* `parentFiles`: the files of the folder it is in, or a function that lists them (called only when they matter).
|
|
49
|
+
*/
|
|
50
|
+
export function plumbing(name, parentFiles) {
|
|
51
|
+
const why = skippedName(name, parentFiles);
|
|
52
|
+
if (why)
|
|
53
|
+
return why;
|
|
54
|
+
const l = name.toLowerCase();
|
|
55
|
+
if ((l === 'versions' || l === 'packages') && (typeof parentFiles === 'function' ? parentFiles() : parentFiles).some(isTopo))
|
|
56
|
+
return `a package's ${l}/ folder`;
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
/** Why a path from `base` along `parts` is not shown (a dot name or plumbing on the way, the last part included), or null. */
|
|
60
|
+
function hiddenOnWay(base, parts) {
|
|
61
|
+
let d = base;
|
|
62
|
+
for (const p of parts) {
|
|
63
|
+
const why = p.startsWith('.') ? `${p} is a dot name` : plumbing(p, () => filesIn(d));
|
|
64
|
+
if (why)
|
|
65
|
+
return why;
|
|
66
|
+
d = path.join(d, p);
|
|
67
|
+
}
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
const leaves = (r) => r === '..' || r.startsWith(`..${path.sep}`) || path.isAbsolute(r);
|
|
71
|
+
/**
|
|
72
|
+
* Where an existing `abs` really is below `top`, links and short names resolved (fs.realpathSync.native: a junction,
|
|
73
|
+
* a symlink, a Windows 8.3 name like GIT~1, the letter case on disk): its posix path from `top`, or why it is not shown
|
|
74
|
+
* (it leads out of `top`, or through a dot folder or plumbing by another name; `dotsOnly`: a dot name only).
|
|
75
|
+
*/
|
|
76
|
+
export function realBelow(top, abs, dotsOnly = false) {
|
|
77
|
+
const outside = { why: 'it leads out of the folder', out: true };
|
|
78
|
+
const root = path.resolve(top), relative = path.relative(root, path.resolve(abs));
|
|
79
|
+
if (leaves(relative))
|
|
80
|
+
return outside;
|
|
81
|
+
let realTop;
|
|
82
|
+
try {
|
|
83
|
+
realTop = fs.realpathSync.native(top);
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
return { why: 'it cannot be read', out: false };
|
|
87
|
+
}
|
|
88
|
+
let parts = relative ? relative.split(path.sep) : [], current = realTop, links = 0;
|
|
89
|
+
while (parts.length) {
|
|
90
|
+
const part = parts.shift();
|
|
91
|
+
const hidden = part.startsWith('.') ? 'a dot name' : !dotsOnly && plumbing(part, () => filesIn(current));
|
|
92
|
+
if (hidden)
|
|
93
|
+
return { why: `hidden plumbing by another name (${hidden})`, out: false };
|
|
94
|
+
const next = path.join(current, part);
|
|
95
|
+
let st;
|
|
96
|
+
try {
|
|
97
|
+
st = fs.lstatSync(next, { throwIfNoEntry: false });
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
return { why: 'it cannot be read', out: false };
|
|
101
|
+
}
|
|
102
|
+
if (!st) {
|
|
103
|
+
// A missing child still belongs to its canonical parent. Never probe through a hidden alias just to
|
|
104
|
+
// distinguish a missing file from one that exists.
|
|
105
|
+
const hiddenTail = dotsOnly ? parts.some((p) => p.startsWith('.')) : hiddenOnWay(current, [part, ...parts]);
|
|
106
|
+
if (hiddenTail)
|
|
107
|
+
return { why: 'hidden plumbing', out: false };
|
|
108
|
+
current = path.join(next, ...parts);
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
if (st.isSymbolicLink()) {
|
|
112
|
+
if (++links > 40)
|
|
113
|
+
return { why: 'too many symbolic links', out: false };
|
|
114
|
+
let target;
|
|
115
|
+
try {
|
|
116
|
+
target = fs.readlinkSync(next);
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
return { why: 'it cannot be read', out: false };
|
|
120
|
+
}
|
|
121
|
+
// readlink reads the link itself. Reject network and device targets before any call follows them.
|
|
122
|
+
if (/^[\\/]{2}/.test(target))
|
|
123
|
+
return outside;
|
|
124
|
+
const destination = path.resolve(current, target);
|
|
125
|
+
let r = path.relative(realTop, destination);
|
|
126
|
+
// A junction can retain the configured root's short name or alias. That prefix already resolves to
|
|
127
|
+
// realTop; map it without following the target, so hidden and external paths are still checked first.
|
|
128
|
+
if (leaves(r))
|
|
129
|
+
r = path.relative(root, destination);
|
|
130
|
+
if (leaves(r))
|
|
131
|
+
return outside;
|
|
132
|
+
parts = [...(r ? r.split(path.sep) : []), ...parts];
|
|
133
|
+
current = realTop;
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
// With links checked one component at a time, native realpath only expands this component's short name
|
|
137
|
+
// and letter case. Check that expanded name before proceeding to any child.
|
|
138
|
+
try {
|
|
139
|
+
current = fs.realpathSync.native(next);
|
|
140
|
+
}
|
|
141
|
+
catch {
|
|
142
|
+
return { why: 'it cannot be read', out: false };
|
|
143
|
+
}
|
|
144
|
+
const r = path.relative(realTop, current);
|
|
145
|
+
if (leaves(r))
|
|
146
|
+
return outside;
|
|
147
|
+
const canonical = r ? r.split(path.sep) : [];
|
|
148
|
+
const why = dotsOnly ? canonical.some((p) => p.startsWith('.')) : hiddenOnWay(realTop, canonical);
|
|
149
|
+
if (why)
|
|
150
|
+
return { why: 'hidden plumbing by another name', out: false };
|
|
151
|
+
}
|
|
152
|
+
return { rel: path.relative(realTop, current).split(path.sep).join('/') };
|
|
153
|
+
}
|
|
154
|
+
/** fs.statSync that never throws (a path Windows refuses, a share that does not answer: not there). */
|
|
155
|
+
const statOf = (p) => { try {
|
|
156
|
+
return fs.statSync(p, { throwIfNoEntry: false }) ?? null;
|
|
157
|
+
}
|
|
158
|
+
catch {
|
|
159
|
+
return null;
|
|
160
|
+
} };
|
|
161
|
+
const collator = new Intl.Collator('en', { numeric: true, sensitivity: 'base' });
|
|
162
|
+
/** File name order, numbers as numbers ("2-x" before "10-x"), case aside. */
|
|
163
|
+
export const byName = (a, b) => collator.compare(a, b) || (a < b ? -1 : a > b ? 1 : 0);
|
|
164
|
+
export class PathError extends Error {
|
|
165
|
+
status;
|
|
166
|
+
constructor(status, msg) { super(msg); this.status = status; }
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* The file or folder that URL path segments name below a root, or a PathError: each segment is percent-decoded and
|
|
170
|
+
* must be a plain name (no "", ".", "..", dot names, separators, NUL, and on Windows no ":" or reserved characters);
|
|
171
|
+
* no folder on the way may be plumbing; and with links and short names resolved the path must stay inside the root and
|
|
172
|
+
* clear of plumbing too. `rel` is in the letter case on disk when only the case differs from the request.
|
|
173
|
+
*/
|
|
174
|
+
export function resolveIn(rootDir, segs) {
|
|
175
|
+
const parts = [];
|
|
176
|
+
for (const raw of segs) {
|
|
177
|
+
let s;
|
|
178
|
+
try {
|
|
179
|
+
s = decodeURIComponent(raw);
|
|
180
|
+
}
|
|
181
|
+
catch {
|
|
182
|
+
throw new PathError(400, 'a path segment is not valid percent-encoding');
|
|
183
|
+
}
|
|
184
|
+
if (!s || s === '.' || s === '..' || s.startsWith('.') || /[\\/\u0000-\u001f]/.test(s) || (process.platform === 'win32' && (/[:*?"<>|]/.test(s) || /[. ]$/.test(s))))
|
|
185
|
+
throw new PathError(400, `"${s}" is no file or folder name the web app serves (no "..", no dot names, no separators)`);
|
|
186
|
+
parts.push(s);
|
|
187
|
+
}
|
|
188
|
+
const root = path.resolve(rootDir);
|
|
189
|
+
const abs = path.join(root, ...parts);
|
|
190
|
+
if (leaves(path.relative(root, abs)))
|
|
191
|
+
throw new PathError(400, 'the path leaves the folder');
|
|
192
|
+
if (!parts.length)
|
|
193
|
+
return { abs, rel: '', parts };
|
|
194
|
+
const real = realBelow(root, abs);
|
|
195
|
+
if ('why' in real)
|
|
196
|
+
throw real.out ? new PathError(400, 'the path leaves the folder (a link to elsewhere)') : new PathError(404, `not shown: ${real.why}`);
|
|
197
|
+
if (!statOf(abs))
|
|
198
|
+
throw new PathError(404, `nothing at ${parts.join('/')}`);
|
|
199
|
+
// the same folder asked for in another letter case is the one on disk (its flags and notices are found by that path)
|
|
200
|
+
const asked = parts.join('/');
|
|
201
|
+
return { abs, rel: real.rel.toLowerCase() === asked.toLowerCase() ? real.rel : asked, parts };
|
|
202
|
+
}
|
|
203
|
+
const filesIn = (dir) => { try {
|
|
204
|
+
return fs.readdirSync(dir, { withFileTypes: true }).filter((e) => e.isFile()).map((e) => e.name);
|
|
205
|
+
}
|
|
206
|
+
catch {
|
|
207
|
+
return [];
|
|
208
|
+
} };
|
|
209
|
+
/**
|
|
210
|
+
* Send a file: its media type (SVG, HTML and unknown types as downloads, `download` for any), a sandbox policy, an
|
|
211
|
+
* ETag, and byte ranges (one range: 206 with Content-Range; past the end: 416).
|
|
212
|
+
*/
|
|
213
|
+
export function sendFile(req, res, abs, o = {}) {
|
|
214
|
+
const st = fs.statSync(abs);
|
|
215
|
+
const ext = path.extname(abs).toLowerCase();
|
|
216
|
+
const name = path.basename(abs);
|
|
217
|
+
const etag = `"${st.size.toString(36)}-${Math.round(st.mtimeMs).toString(36)}"`;
|
|
218
|
+
const headers = {
|
|
219
|
+
'content-type': MIME_TYPES[ext] ?? 'application/octet-stream', 'x-content-type-options': 'nosniff', 'content-security-policy': 'sandbox',
|
|
220
|
+
'accept-ranges': 'bytes', 'cache-control': 'no-cache', etag, 'last-modified': st.mtime.toUTCString(),
|
|
221
|
+
};
|
|
222
|
+
if (o.download || !INLINE.has(ext))
|
|
223
|
+
headers['content-disposition'] = `attachment; filename="${name.replace(/[^\x20-\x7e]|["\\]/g, '_')}"; filename*=UTF-8''${encodeURIComponent(name)}`;
|
|
224
|
+
if (req.headers['if-none-match'] === etag && !req.headers.range) {
|
|
225
|
+
res.writeHead(304, { etag, 'cache-control': 'no-cache' });
|
|
226
|
+
res.end();
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
const size = st.size;
|
|
230
|
+
let start = 0, end = size - 1, status = 200;
|
|
231
|
+
const range = req.headers.range;
|
|
232
|
+
const ifRange = req.headers['if-range'];
|
|
233
|
+
if (range && (!ifRange || ifRange === etag)) {
|
|
234
|
+
// one range: "bytes=a-b", "bytes=a-", "bytes=-n"; several ranges or another unit: the whole file
|
|
235
|
+
const m = /^bytes=(\d*)-(\d*)$/.exec(range.trim());
|
|
236
|
+
if (m && (m[1] !== '' || m[2] !== '')) {
|
|
237
|
+
if (m[1] === '') {
|
|
238
|
+
start = Math.max(0, size - Number(m[2]));
|
|
239
|
+
end = Number(m[2]) === 0 ? -1 : size - 1;
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
start = Number(m[1]);
|
|
243
|
+
end = m[2] === '' ? size - 1 : Math.min(Number(m[2]), size - 1);
|
|
244
|
+
}
|
|
245
|
+
if (start >= size || start > end) {
|
|
246
|
+
res.writeHead(416, { 'content-range': `bytes */${size}`, 'content-type': 'text/plain; charset=utf-8', 'x-content-type-options': 'nosniff' });
|
|
247
|
+
res.end('range not satisfiable');
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
status = 206;
|
|
251
|
+
headers['content-range'] = `bytes ${start}-${end}/${size}`;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
headers['content-length'] = String(size ? end - start + 1 : 0);
|
|
255
|
+
res.writeHead(status, headers);
|
|
256
|
+
if (req.method === 'HEAD' || !size) {
|
|
257
|
+
res.end();
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
const stream = fs.createReadStream(abs, { start, end });
|
|
261
|
+
stream.on('error', () => res.destroy());
|
|
262
|
+
stream.pipe(res);
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* The files of a folder that its page lists (`names`: the folder's file names): dot files, the files engines write
|
|
266
|
+
* beside ours and .topo files left out, Unreal's assets counted apart, an export's sidecar joined to the file it
|
|
267
|
+
* describes. The folder cards, the front page and the folder page count by this.
|
|
268
|
+
*/
|
|
269
|
+
function shownFiles(abs, names) {
|
|
270
|
+
const shown = [], side = [];
|
|
271
|
+
let unreal = 0;
|
|
272
|
+
for (const f of names) {
|
|
273
|
+
if (f.startsWith('.') || GODOT_SIDE.test(f) || isTopo(f))
|
|
274
|
+
continue;
|
|
275
|
+
if (UNREAL_ASSET.test(f)) {
|
|
276
|
+
unreal++;
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
(isSidecarName(f) ? side : shown).push(f);
|
|
280
|
+
}
|
|
281
|
+
const have = new Set(shown.filter((f) => kindOf(f) !== 'note'));
|
|
282
|
+
for (const s of side)
|
|
283
|
+
if (sidecarTargetName(s, (n) => have.has(n)) === undefined || !readSource(path.join(abs, s)))
|
|
284
|
+
shown.push(s);
|
|
285
|
+
return { shown, unreal };
|
|
286
|
+
}
|
|
287
|
+
/** A folder's look from outside: its shown files and folders, its first image, what it is. */
|
|
288
|
+
function peek(abs, rel) {
|
|
289
|
+
let entries;
|
|
290
|
+
try {
|
|
291
|
+
entries = fs.readdirSync(abs, { withFileTypes: true });
|
|
292
|
+
}
|
|
293
|
+
catch {
|
|
294
|
+
return { files: 0, folders: 0, unreal: 0 };
|
|
295
|
+
}
|
|
296
|
+
const files = entries.filter((e) => e.isFile()).map((e) => e.name);
|
|
297
|
+
const { shown, unreal } = shownFiles(abs, files);
|
|
298
|
+
const out = { files: shown.length, folders: 0, unreal };
|
|
299
|
+
const project = files.find((f) => engineTypeOf(f));
|
|
300
|
+
if (project)
|
|
301
|
+
out.engine = { type: engineTypeOf(project), title: engineTitle(path.join(abs, project)) };
|
|
302
|
+
if (files.includes(WORKSPACE_FILE))
|
|
303
|
+
out.workspace = true;
|
|
304
|
+
if (files.some(isTopo))
|
|
305
|
+
out.package = true;
|
|
306
|
+
const image = shown.filter((f) => kindOf(f) === 'image').sort(byName)[0];
|
|
307
|
+
out.folders = entries.filter((e) => e.isDirectory() && !plumbing(e.name, files)).length;
|
|
308
|
+
if (image)
|
|
309
|
+
out.image = `${rel}/${image}`;
|
|
310
|
+
return out;
|
|
311
|
+
}
|
|
312
|
+
/** An engine project's own name: project.godot's config/name, a .uproject's file name. */
|
|
313
|
+
export function engineTitle(file) {
|
|
314
|
+
if (engineTypeOf(file) === 'unreal')
|
|
315
|
+
return path.basename(file).replace(/\.uproject$/i, '');
|
|
316
|
+
try {
|
|
317
|
+
return /^config\/name\s*=\s*"([^"]*)"/m.exec(fs.readFileSync(file, 'utf8'))?.[1] || path.basename(path.dirname(file));
|
|
318
|
+
}
|
|
319
|
+
catch {
|
|
320
|
+
return path.basename(path.dirname(file));
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* A folder below a root (`rel` posix, '' for the root): its subfolders, files by type and notes; plumbing left out, and
|
|
325
|
+
* links that lead out of `root` (when given).
|
|
326
|
+
*/
|
|
327
|
+
export function listFolder(abs, rel, root) {
|
|
328
|
+
const at = (n) => (rel ? `${rel}/${n}` : n);
|
|
329
|
+
let entries;
|
|
330
|
+
try {
|
|
331
|
+
entries = fs.readdirSync(abs, { withFileTypes: true });
|
|
332
|
+
}
|
|
333
|
+
catch {
|
|
334
|
+
entries = [];
|
|
335
|
+
}
|
|
336
|
+
const fileNames = entries.filter((e) => e.isFile()).map((e) => e.name);
|
|
337
|
+
const out = { folders: [], files: [], notes: [], engineAssets: 0, topo: fileNames.filter(isTopo).sort() };
|
|
338
|
+
const sidecars = [];
|
|
339
|
+
for (const e of entries.sort((a, b) => byName(a.name, b.name))) {
|
|
340
|
+
let dir = e.isDirectory(), file = e.isFile();
|
|
341
|
+
if (e.isSymbolicLink()) {
|
|
342
|
+
// a link is shown when it leads somewhere inside the root, and not into plumbing (resolveIn checks it again when
|
|
343
|
+
// it is opened)
|
|
344
|
+
if (!root || 'why' in realBelow(root, path.join(abs, e.name)))
|
|
345
|
+
continue;
|
|
346
|
+
const st = statOf(path.join(abs, e.name));
|
|
347
|
+
dir = !!st?.isDirectory();
|
|
348
|
+
file = !!st?.isFile();
|
|
349
|
+
}
|
|
350
|
+
if (dir) {
|
|
351
|
+
if (plumbing(e.name, fileNames))
|
|
352
|
+
continue;
|
|
353
|
+
out.folders.push({ name: e.name, rel: at(e.name), ...peek(path.join(abs, e.name), at(e.name)) });
|
|
354
|
+
continue;
|
|
355
|
+
}
|
|
356
|
+
if (!file || e.name.startsWith('.') || GODOT_SIDE.test(e.name) || isTopo(e.name))
|
|
357
|
+
continue;
|
|
358
|
+
if (UNREAL_ASSET.test(e.name)) {
|
|
359
|
+
out.engineAssets++;
|
|
360
|
+
continue;
|
|
361
|
+
}
|
|
362
|
+
if (isSidecarName(e.name)) {
|
|
363
|
+
sidecars.push(e.name);
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
const st = fs.statSync(path.join(abs, e.name), { throwIfNoEntry: false });
|
|
367
|
+
if (!st)
|
|
368
|
+
continue;
|
|
369
|
+
const f = { name: e.name, rel: at(e.name), size: st.size, mtime: Math.round(st.mtimeMs), kind: kindOf(e.name) };
|
|
370
|
+
(f.kind === 'note' ? out.notes : out.files).push(f);
|
|
371
|
+
}
|
|
372
|
+
// sidecars: an export's recorded source joins the file it describes; one that describes nothing here is a file
|
|
373
|
+
const byFile = new Map(out.files.map((f) => [f.name, f]));
|
|
374
|
+
for (const s of sidecars) {
|
|
375
|
+
const t = sidecarTargetName(s, (n) => byFile.has(n));
|
|
376
|
+
const of = t === undefined ? undefined : t === null ? null : byFile.get(t);
|
|
377
|
+
const made = of !== undefined ? readSource(path.join(abs, s)) : null;
|
|
378
|
+
if (of === null && made) {
|
|
379
|
+
out.made = made;
|
|
380
|
+
continue;
|
|
381
|
+
}
|
|
382
|
+
if (of && made) {
|
|
383
|
+
of.made = made;
|
|
384
|
+
continue;
|
|
385
|
+
}
|
|
386
|
+
const st = fs.statSync(path.join(abs, s), { throwIfNoEntry: false });
|
|
387
|
+
if (st)
|
|
388
|
+
out.files.push({ name: s, rel: at(s), size: st.size, mtime: Math.round(st.mtimeMs), kind: 'other' });
|
|
389
|
+
}
|
|
390
|
+
out.files.sort((a, b) => byName(a.name, b.name));
|
|
391
|
+
return out;
|
|
392
|
+
}
|
|
393
|
+
/** Names an export's sidecar may have (src/io/sidecars.ts): <file>.source.json, source.json, landscape.json, <base>.meshes.json, <base>.json. */
|
|
394
|
+
const isSidecarName = (n) => /\.json$/i.test(n) && n.toLowerCase() !== 'package.json';
|
|
395
|
+
/** The name of the file a sidecar describes, among those `has` knows (undefined: none, so not a sidecar; null: the folder itself). */
|
|
396
|
+
function sidecarTargetName(name, has) {
|
|
397
|
+
const l = name.toLowerCase();
|
|
398
|
+
const first = (...c) => c.find(has);
|
|
399
|
+
if (l.endsWith('.source.json'))
|
|
400
|
+
return first(name.slice(0, -'.source.json'.length));
|
|
401
|
+
if (l === 'source.json' || l === 'landscape.json')
|
|
402
|
+
return null;
|
|
403
|
+
if (l.endsWith('.meshes.json')) {
|
|
404
|
+
const b = name.slice(0, -'.meshes.json'.length);
|
|
405
|
+
return first(`${b}.csv`, `${b}.points.json`);
|
|
406
|
+
}
|
|
407
|
+
const b = name.slice(0, -'.json'.length);
|
|
408
|
+
return first(`${b}.png`, `${b}.r16`);
|
|
409
|
+
}
|
|
410
|
+
/** The export source a sidecar records (its "format" names a TopoTopo sidecar), or null. */
|
|
411
|
+
export function readSource(file) {
|
|
412
|
+
try {
|
|
413
|
+
const fd = fs.openSync(file, 'r');
|
|
414
|
+
let head;
|
|
415
|
+
try {
|
|
416
|
+
head = Buffer.alloc(SIDECAR_HEAD_BYTES);
|
|
417
|
+
fs.readSync(fd, head, 0, SIDECAR_HEAD_BYTES, 0);
|
|
418
|
+
}
|
|
419
|
+
finally {
|
|
420
|
+
fs.closeSync(fd);
|
|
421
|
+
}
|
|
422
|
+
const fmt = jsonFormat(head);
|
|
423
|
+
if (!fmt || !SIDECAR_FORMATS[fmt] || fs.statSync(file).size > 8 * 2 ** 20)
|
|
424
|
+
return null;
|
|
425
|
+
return madeOf(JSON.parse(fs.readFileSync(file, 'utf8'))?.source);
|
|
426
|
+
}
|
|
427
|
+
catch {
|
|
428
|
+
return null;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
const madeOf = (s) => s && typeof s === 'object' && typeof s.graph === 'string' && typeof s.graph_sha256 === 'string'
|
|
432
|
+
? { graph: s.graph, output: typeof s.output === 'string' ? s.output : null, node: String(s.node ?? ''), graph_sha256: s.graph_sha256, vars: s.vars && typeof s.vars === 'object' ? s.vars : {} }
|
|
433
|
+
: null;
|
|
434
|
+
/** The source a GLB or glTF export carries in asset.extras.source, or null (its JSON is read, never its buffers). */
|
|
435
|
+
export function modelSource(file) {
|
|
436
|
+
try {
|
|
437
|
+
if (/\.gltf$/i.test(file))
|
|
438
|
+
return fs.statSync(file).size <= 16 * 2 ** 20 ? madeOf(JSON.parse(fs.readFileSync(file, 'utf8'))?.asset?.extras?.source) : null;
|
|
439
|
+
const fd = fs.openSync(file, 'r');
|
|
440
|
+
try {
|
|
441
|
+
const head = Buffer.alloc(20);
|
|
442
|
+
if (fs.readSync(fd, head, 0, 20, 0) < 20 || head.toString('latin1', 0, 4) !== 'glTF' || head.readUInt32LE(16) !== 0x4e4f534a)
|
|
443
|
+
return null;
|
|
444
|
+
const len = head.readUInt32LE(12);
|
|
445
|
+
if (len > 16 * 2 ** 20)
|
|
446
|
+
return null;
|
|
447
|
+
const json = Buffer.alloc(len);
|
|
448
|
+
fs.readSync(fd, json, 0, len, 20);
|
|
449
|
+
return madeOf(JSON.parse(json.toString('utf8'))?.asset?.extras?.source);
|
|
450
|
+
}
|
|
451
|
+
finally {
|
|
452
|
+
fs.closeSync(fd);
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
catch {
|
|
456
|
+
return null;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Whether the graph an export was made from changed since (the hash it recorded is not the graph file's, with the
|
|
461
|
+
* vars it was made with); null when the graph cannot be read.
|
|
462
|
+
*/
|
|
463
|
+
export function exportStale(graphFile, m) {
|
|
464
|
+
let g;
|
|
465
|
+
try {
|
|
466
|
+
g = JSON.parse(fs.readFileSync(graphFile, 'utf8'));
|
|
467
|
+
}
|
|
468
|
+
catch {
|
|
469
|
+
return null;
|
|
470
|
+
}
|
|
471
|
+
if (sha256Hex(canonicalJSON(g)) === m.graph_sha256)
|
|
472
|
+
return false;
|
|
473
|
+
if (g && typeof g === 'object' && g.vars && typeof g.vars === 'object' && sha256Hex(canonicalJSON({ ...g, vars: m.vars })) === m.graph_sha256)
|
|
474
|
+
return false;
|
|
475
|
+
return true;
|
|
476
|
+
}
|
|
477
|
+
const YT_ID = /^[A-Za-z0-9_-]{11}$/;
|
|
478
|
+
/** A YouTube or Vimeo address as its privacy player's address (youtube-nocookie.com, player.vimeo.com), or null. */
|
|
479
|
+
export function videoPlayer(url) {
|
|
480
|
+
let u;
|
|
481
|
+
try {
|
|
482
|
+
u = new URL(url);
|
|
483
|
+
}
|
|
484
|
+
catch {
|
|
485
|
+
return null;
|
|
486
|
+
}
|
|
487
|
+
if (u.protocol !== 'https:' && u.protocol !== 'http:')
|
|
488
|
+
return null;
|
|
489
|
+
const host = u.hostname.toLowerCase().replace(/^(www\.|m\.)/, '');
|
|
490
|
+
const seconds = (t) => {
|
|
491
|
+
if (!t)
|
|
492
|
+
return 0;
|
|
493
|
+
const m = /^(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s?)?$/.exec(t);
|
|
494
|
+
return m ? Number(m[1] ?? 0) * 3600 + Number(m[2] ?? 0) * 60 + Number(m[3] ?? 0) : 0;
|
|
495
|
+
};
|
|
496
|
+
let id = null;
|
|
497
|
+
if (host === 'youtu.be')
|
|
498
|
+
id = u.pathname.slice(1).split('/')[0];
|
|
499
|
+
else if (host === 'youtube.com' || host === 'youtube-nocookie.com' || host === 'music.youtube.com') {
|
|
500
|
+
if (u.pathname === '/watch')
|
|
501
|
+
id = u.searchParams.get('v');
|
|
502
|
+
else {
|
|
503
|
+
const m = /^\/(?:embed|shorts|live|v)\/([^/?#]+)/.exec(u.pathname);
|
|
504
|
+
id = m?.[1] ?? null;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
if (id !== null) {
|
|
508
|
+
if (!YT_ID.test(id))
|
|
509
|
+
return null;
|
|
510
|
+
const start = seconds(u.searchParams.get('t') ?? u.searchParams.get('start'));
|
|
511
|
+
return `https://www.youtube-nocookie.com/embed/${id}${start ? `?start=${start}` : ''}`;
|
|
512
|
+
}
|
|
513
|
+
if (host === 'vimeo.com' || host === 'player.vimeo.com') {
|
|
514
|
+
const m = /^\/(?:video\/|channels\/[^/]+\/|groups\/[^/]+\/videos\/)?(\d{3,12})(?:\/([0-9a-f]{6,20}))?\/?$/i.exec(u.pathname);
|
|
515
|
+
if (!m)
|
|
516
|
+
return null;
|
|
517
|
+
const h = m[2] ?? u.searchParams.get('h');
|
|
518
|
+
return `https://player.vimeo.com/video/${m[1]}?dnt=1${h && /^[0-9a-f]{6,20}$/i.test(h) ? `&h=${h}` : ''}`;
|
|
519
|
+
}
|
|
520
|
+
return null;
|
|
521
|
+
}
|
|
522
|
+
/**
|
|
523
|
+
* Where a link written in a note in `folder` leads, below `root`: a web address; a heading of the note ("#usage"); a
|
|
524
|
+
* path relative to the folder, from the root when it starts with "/", res:// from the Godot project (the workspace's
|
|
525
|
+
* engine, else the one the folder is in), /Game/ from Unreal's Content/ (with .uasset or .umap added when that is the
|
|
526
|
+
* file); anything else stays text. A path is judged by its letters before the file system is asked anything: one that
|
|
527
|
+
* is absolute (a drive, a network share \\host\share), leaves the root (or the engine project), or passes a dot folder
|
|
528
|
+
* or plumbing stays text and is never looked up; only what is left is, and then by where it really is (links and short
|
|
529
|
+
* names resolved). A lookup that fails says "not there"; this never throws.
|
|
530
|
+
*/
|
|
531
|
+
export function resolveLink(root, folder, href) {
|
|
532
|
+
const h = href.trim();
|
|
533
|
+
if (!h)
|
|
534
|
+
return { t: 'text', why: 'an empty link' };
|
|
535
|
+
if (/^https?:\/\//i.test(h)) {
|
|
536
|
+
const player = videoPlayer(h);
|
|
537
|
+
return { t: 'web', url: h, ...(player ? { player } : {}) };
|
|
538
|
+
}
|
|
539
|
+
if (/^mailto:/i.test(h))
|
|
540
|
+
return { t: 'web', url: h };
|
|
541
|
+
if (h.startsWith('#'))
|
|
542
|
+
return { t: 'anchor', id: h.slice(1) };
|
|
543
|
+
/** where the path is read from, and what it must stay inside */
|
|
544
|
+
let base, scope, p, game = false;
|
|
545
|
+
if (/^res:\/\//i.test(h)) {
|
|
546
|
+
const godot = root.engine?.type === 'godot' ? path.dirname(root.engine.file) : godotProjectOf(folder);
|
|
547
|
+
if (!godot)
|
|
548
|
+
return { t: 'text', why: 'res:// names a file of a Godot project, and there is none here' };
|
|
549
|
+
base = scope = godot;
|
|
550
|
+
p = h.slice('res://'.length);
|
|
551
|
+
}
|
|
552
|
+
else if (/^\/Game\//.test(h)) {
|
|
553
|
+
const ue = root.engine?.type === 'unreal' ? root.engine.file : (() => { const a = engineAbove(folder); return a?.type === 'unreal' ? a.file : null; })();
|
|
554
|
+
if (!ue)
|
|
555
|
+
return { t: 'text', why: '/Game/ names an asset of an Unreal project, and there is none here' };
|
|
556
|
+
base = scope = path.join(path.dirname(ue), 'Content');
|
|
557
|
+
p = h.slice('/Game/'.length);
|
|
558
|
+
game = true;
|
|
559
|
+
}
|
|
560
|
+
else if (/^[a-z][a-z0-9+.-]*:/i.test(h) || h.startsWith('//') || h.startsWith('\\')) {
|
|
561
|
+
return { t: 'text', why: 'only web addresses and paths inside this tree are links in a note' };
|
|
562
|
+
}
|
|
563
|
+
else if (h.startsWith('/')) {
|
|
564
|
+
base = scope = root.dir;
|
|
565
|
+
p = h.slice(1);
|
|
566
|
+
}
|
|
567
|
+
else {
|
|
568
|
+
base = folder;
|
|
569
|
+
scope = root.dir;
|
|
570
|
+
p = h;
|
|
571
|
+
}
|
|
572
|
+
const hashAt = p.indexOf('#');
|
|
573
|
+
const hash = hashAt >= 0 ? p.slice(hashAt + 1) : '';
|
|
574
|
+
p = (hashAt >= 0 ? p.slice(0, hashAt) : p).split('?')[0];
|
|
575
|
+
try {
|
|
576
|
+
p = decodeURIComponent(p);
|
|
577
|
+
}
|
|
578
|
+
catch { /* as written */ }
|
|
579
|
+
// judged by its letters: no control characters, nothing absolute (a drive, \\host\share, a second leading slash)
|
|
580
|
+
if (/[\u0000-\u001f]/.test(p))
|
|
581
|
+
return { t: 'text', why: 'not a file name' };
|
|
582
|
+
if (/^[\\/]/.test(p) || /^[A-Za-z]:/.test(p) || path.isAbsolute(p))
|
|
583
|
+
return { t: 'text', why: 'an absolute path: a note links to files inside this tree' };
|
|
584
|
+
let abs = path.resolve(base, p);
|
|
585
|
+
const r = path.relative(path.resolve(scope), abs);
|
|
586
|
+
if (leaves(r))
|
|
587
|
+
return { t: 'text', why: 'outside this tree' };
|
|
588
|
+
const real = realBelow(scope, abs);
|
|
589
|
+
if ('why' in real)
|
|
590
|
+
return { t: 'text', why: real.out ? 'outside this tree' : `not shown: ${real.why}` };
|
|
591
|
+
// only now is the file system asked
|
|
592
|
+
let st = statOf(abs);
|
|
593
|
+
if (!st && game) {
|
|
594
|
+
// /Game/Props/SM_Crate or /Game/Props/SM_Crate.SM_Crate: the package file SM_Crate.uasset (or a map's .umap)
|
|
595
|
+
const noObj = abs.replace(/\.[^.\\/]+$/, '');
|
|
596
|
+
for (const c of [abs + '.uasset', abs + '.umap', noObj + '.uasset', noObj + '.umap']) {
|
|
597
|
+
const s = statOf(c);
|
|
598
|
+
if (s) {
|
|
599
|
+
abs = c;
|
|
600
|
+
st = s;
|
|
601
|
+
break;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
const lexical = () => { const x = path.relative(path.resolve(root.dir), abs); return leaves(x) ? null : x.split(path.sep).join('/'); };
|
|
606
|
+
if (!st)
|
|
607
|
+
return { t: 'path', abs, rel: lexical(), exists: false, dir: false, hash };
|
|
608
|
+
// where it really is: a link or short name that leads out, or into plumbing, stays text
|
|
609
|
+
const inRoot = realBelow(root.dir, abs);
|
|
610
|
+
return { t: 'path', abs, rel: 'rel' in inRoot ? inRoot.rel : null, exists: true, dir: st.isDirectory(), hash };
|
|
611
|
+
}
|
|
612
|
+
/** The anchor of a note on its folder's page. */
|
|
613
|
+
export const noteId = (name) => `note-${name.replace(/[^A-Za-z0-9_.-]/g, '-')}`;
|
|
614
|
+
/** What a note's heading ids start with on its folder's page: the note's anchor, so a link can name a heading of another note. */
|
|
615
|
+
export const headingPrefix = (name) => `${noteId(name)}--`;
|
|
616
|
+
const decodeSafe = (s) => { try {
|
|
617
|
+
return decodeURIComponent(s);
|
|
618
|
+
}
|
|
619
|
+
catch {
|
|
620
|
+
return s;
|
|
621
|
+
} };
|
|
622
|
+
/** A heading's anchor in the note `name`, from what a link writes after "#" ("Usage", "usage"). */
|
|
623
|
+
export const headingId = (name, hash) => headingPrefix(name) + slug(decodeSafe(hash));
|
|
624
|
+
const fileName = (p) => p.split(/[\\/]/).filter(Boolean).pop() ?? p;
|
|
625
|
+
/** An image's size in pixels from its header (PNG, GIF, JPEG, WebP), kept while the file is unchanged; null when unknown. */
|
|
626
|
+
const sizes = new Map();
|
|
627
|
+
export function imageSize(file) {
|
|
628
|
+
const st = statOf(file);
|
|
629
|
+
if (!st?.isFile())
|
|
630
|
+
return null;
|
|
631
|
+
const key = `${st.size}-${st.mtimeMs}`, have = sizes.get(file);
|
|
632
|
+
if (have?.key === key)
|
|
633
|
+
return have.size;
|
|
634
|
+
let size = null;
|
|
635
|
+
try {
|
|
636
|
+
const fd = fs.openSync(file, 'r');
|
|
637
|
+
try {
|
|
638
|
+
const head = Buffer.alloc(Math.min(st.size, 64 * 1024));
|
|
639
|
+
fs.readSync(fd, head, 0, head.length, 0);
|
|
640
|
+
size = headerSize(head, (at, n) => { const b = Buffer.alloc(n); const got = fs.readSync(fd, b, 0, n, at); return b.subarray(0, got); });
|
|
641
|
+
}
|
|
642
|
+
finally {
|
|
643
|
+
fs.closeSync(fd);
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
catch {
|
|
647
|
+
size = null;
|
|
648
|
+
}
|
|
649
|
+
if (sizes.size > 4096)
|
|
650
|
+
sizes.clear();
|
|
651
|
+
sizes.set(file, { key, size });
|
|
652
|
+
return size;
|
|
653
|
+
}
|
|
654
|
+
function headerSize(b, more) {
|
|
655
|
+
const ok = (w, h) => (w > 0 && h > 0 && w < 100_000 && h < 100_000 ? { w, h } : null);
|
|
656
|
+
if (b.length >= 24 && b.readUInt32BE(0) === 0x89504e47 && b.toString('latin1', 12, 16) === 'IHDR')
|
|
657
|
+
return ok(b.readUInt32BE(16), b.readUInt32BE(20));
|
|
658
|
+
if (b.length >= 10 && b.toString('latin1', 0, 4) === 'GIF8')
|
|
659
|
+
return ok(b.readUInt16LE(6), b.readUInt16LE(8));
|
|
660
|
+
if (b.length >= 30 && b.toString('latin1', 0, 4) === 'RIFF' && b.toString('latin1', 8, 12) === 'WEBP') {
|
|
661
|
+
const chunk = b.toString('latin1', 12, 16);
|
|
662
|
+
if (chunk === 'VP8 ')
|
|
663
|
+
return ok(b.readUInt16LE(26) & 0x3fff, b.readUInt16LE(28) & 0x3fff);
|
|
664
|
+
if (chunk === 'VP8L')
|
|
665
|
+
return ok(1 + (((b[22] & 0x3f) << 8) | b[21]), 1 + (((b[24] & 0x0f) << 10) | (b[23] << 2) | ((b[22] & 0xc0) >> 6)));
|
|
666
|
+
if (chunk === 'VP8X')
|
|
667
|
+
return ok(1 + b.readUIntLE(24, 3), 1 + b.readUIntLE(27, 3));
|
|
668
|
+
return null;
|
|
669
|
+
}
|
|
670
|
+
if (b.length >= 4 && b[0] === 0xff && b[1] === 0xd8) {
|
|
671
|
+
// JPEG: the markers up to the frame header (SOFn), which may lie past a large EXIF block
|
|
672
|
+
let at = 2;
|
|
673
|
+
for (let guard = 0; guard < 512; guard++) {
|
|
674
|
+
const m = more(at, 10);
|
|
675
|
+
if (m.length < 4 || m[0] !== 0xff)
|
|
676
|
+
return null;
|
|
677
|
+
const marker = m[1];
|
|
678
|
+
if (marker === 0xff) {
|
|
679
|
+
at++;
|
|
680
|
+
continue;
|
|
681
|
+
}
|
|
682
|
+
if (marker === 0xd8 || (marker >= 0xd0 && marker <= 0xd7) || marker === 0x01) {
|
|
683
|
+
at += 2;
|
|
684
|
+
continue;
|
|
685
|
+
}
|
|
686
|
+
const len = m.readUInt16BE(2);
|
|
687
|
+
if (marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc)
|
|
688
|
+
return m.length >= 9 ? ok(m.readUInt16BE(7), m.readUInt16BE(5)) : null;
|
|
689
|
+
if (len < 2)
|
|
690
|
+
return null;
|
|
691
|
+
at += 2 + len;
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
return null;
|
|
695
|
+
}
|
|
696
|
+
/** An inline link of a note that names what it leads to with a small icon ("lk-dir", "lk-pkg", "lk-img" …). */
|
|
697
|
+
const inlineLink = (kind, href, label, extra = '') => `<a class="lk lk-${kind}" href="${escHtml(href)}"${extra}>${label}</a>`;
|
|
698
|
+
/**
|
|
699
|
+
* A link of a note as HTML, by what it leads to: an image, audio and video in a player, a model as a 3D view on
|
|
700
|
+
* click, a folder or package as a card, a note as a link to its box (or one of its headings), a heading of the note
|
|
701
|
+
* itself, YouTube and Vimeo in their privacy players, a missing file struck through (`broken` gets it), anything else
|
|
702
|
+
* a plain link. The author's words stay: a card is titled with the link's text (the folder's name and counts below
|
|
703
|
+
* it), a linked image carries it as a caption. A card, picture or player is shown in place when the link stands alone
|
|
704
|
+
* in its paragraph or list item (`at.alone`); in running text the link stays a link, marked with an icon, and the
|
|
705
|
+
* card, picture or player follows the paragraph (`block`).
|
|
706
|
+
*/
|
|
707
|
+
export function linkHtml(env, folder, l, inner, broken, at = { alone: false, idPrefix: '' }) {
|
|
708
|
+
const t = resolveLink(env.root, folder, l.href);
|
|
709
|
+
const label = inner || escHtml(l.text || l.href);
|
|
710
|
+
const words = l.text && l.text !== l.href ? l.text : '';
|
|
711
|
+
const place = (inline, block) => (at.alone ? { inline: '', block } : { inline, block });
|
|
712
|
+
if (t.t === 'text')
|
|
713
|
+
return `<span class="lnk-text" title="${escHtml(t.why)}">${label}</span>`;
|
|
714
|
+
if (t.t === 'anchor')
|
|
715
|
+
return `<a href="#${escHtml(at.idPrefix + slug(decodeSafe(t.id)))}">${label}</a>`;
|
|
716
|
+
if (t.t === 'web') {
|
|
717
|
+
if (t.player) {
|
|
718
|
+
const player = `<span class="embed web"><iframe src="${escHtml(t.player)}" title="${escHtml(words || 'video')}" loading="lazy" allow="encrypted-media; picture-in-picture; fullscreen" referrerpolicy="strict-origin-when-cross-origin" sandbox="allow-scripts allow-same-origin allow-presentation allow-popups"></iframe>`
|
|
719
|
+
+ `<a class="cap" href="${escHtml(t.url)}" target="_blank" rel="noopener noreferrer">${label}</a></span>`;
|
|
720
|
+
return place(`<a class="lk lk-video" href="${escHtml(t.url)}" target="_blank" rel="noopener noreferrer">${label}</a>`, player);
|
|
721
|
+
}
|
|
722
|
+
return `<a href="${escHtml(t.url)}" target="_blank" rel="noopener noreferrer">${label}</a>`;
|
|
723
|
+
}
|
|
724
|
+
if (!t.exists) {
|
|
725
|
+
broken.push(l.href);
|
|
726
|
+
return `<s class="missing" title="${escHtml(`missing: ${l.href}`)}">${label}</s>`;
|
|
727
|
+
}
|
|
728
|
+
if (t.rel === null)
|
|
729
|
+
return `<span class="lnk-text" title="${escHtml('outside this tree')}">${label}</span>`;
|
|
730
|
+
if (t.dir) {
|
|
731
|
+
const c = env.card(t.rel, t.abs);
|
|
732
|
+
const href = env.folderUrl(t.rel);
|
|
733
|
+
// the author's words as the title; what the folder is below them
|
|
734
|
+
const title = words || c.title, sub = words ? `${c.title} · ${c.sub}` : c.sub;
|
|
735
|
+
const card = `<a class="lcard" href="${escHtml(href)}">${c.thumb ? `<img src="${escHtml(c.thumb)}" alt="" loading="lazy">` : '<span class="lcard-icon"></span>'}<span><b>${escHtml(title)}</b><span>${escHtml(sub)}</span></span></a>`;
|
|
736
|
+
return place(inlineLink(c.sub.startsWith('package') ? 'pkg' : 'dir', href, label), card);
|
|
737
|
+
}
|
|
738
|
+
const url = env.fileUrl(t.rel);
|
|
739
|
+
const fname = escHtml(fileName(t.rel));
|
|
740
|
+
const cap = words ? escHtml(words) : fname;
|
|
741
|
+
switch (kindOf(t.abs)) {
|
|
742
|
+
case 'image': {
|
|
743
|
+
const s = imageSize(t.abs);
|
|
744
|
+
const img = `<img src="${escHtml(url)}" alt="${escHtml(l.text)}"${s ? ` width="${s.w}" height="${s.h}"` : ''} loading="lazy">`;
|
|
745
|
+
const pic = `<a class="limg" href="${escHtml(url)}" data-view="image" data-name="${fname}">${img}</a>`;
|
|
746
|
+
// an image written as an image is a picture where it stands; a link to one keeps its words as a caption
|
|
747
|
+
if (l.image)
|
|
748
|
+
return pic;
|
|
749
|
+
return place(inlineLink('img', url, label, ` data-view="image" data-name="${fname}"`), `<figure class="lfig">${pic}<figcaption>${cap}</figcaption></figure>`);
|
|
750
|
+
}
|
|
751
|
+
case 'audio': return place(inlineLink('audio', url, label), `<span class="embed audio"><span class="cap">${cap}</span><audio controls preload="metadata" src="${escHtml(url)}"></audio></span>`);
|
|
752
|
+
case 'video': return place(inlineLink('video', url, label), `<span class="embed video"><video controls preload="metadata" src="${escHtml(url)}"></video><span class="cap">${cap}</span></span>`);
|
|
753
|
+
case 'model': {
|
|
754
|
+
const view = ` data-view="model" data-src="${escHtml(env.modelUrl(t.rel))}" data-name="${fname}"`;
|
|
755
|
+
return place(inlineLink('model', env.modelUrl(t.rel), label, view), `<button type="button" class="lmodel"${view}><span class="cube"></span><span><b>${cap}</b><span>${words && words !== fileName(t.rel) ? `${fname} · ` : ''}View in 3D</span></span></button>`);
|
|
756
|
+
}
|
|
757
|
+
case 'note': {
|
|
758
|
+
const name = fileName(t.rel);
|
|
759
|
+
return `<a href="${escHtml(env.noteUrl(t.rel, t.hash ? headingId(name, t.hash) : noteId(name)))}">${label}</a>`;
|
|
760
|
+
}
|
|
761
|
+
default: return `<a href="${escHtml(url)}">${label}</a>`;
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
/** Notes longer than this are linked, not shown. */
|
|
765
|
+
export const MAX_NOTE_BYTES = 1024 * 1024;
|
|
766
|
+
/** Web addresses in plain text, linked: found in the raw text, then each piece escaped (a "&" or "'" stays in its address). */
|
|
767
|
+
export function linkText(text) {
|
|
768
|
+
let out = '', last = 0;
|
|
769
|
+
for (const m of text.matchAll(/https?:\/\/[^\s<>"]+/gi)) {
|
|
770
|
+
let url = m[0];
|
|
771
|
+
// trailing punctuation ends the sentence, not the address; so does a ")" without its "("
|
|
772
|
+
while (/[.,:;!?'"*_~]$/.test(url))
|
|
773
|
+
url = url.slice(0, -1);
|
|
774
|
+
while (url.endsWith(')') && (url.match(/\(/g)?.length ?? 0) < (url.match(/\)/g)?.length ?? 0))
|
|
775
|
+
url = url.slice(0, -1);
|
|
776
|
+
out += escHtml(text.slice(last, m.index)) + `<a href="${escHtml(url)}" target="_blank" rel="noopener noreferrer">${escHtml(url)}</a>`;
|
|
777
|
+
last = m.index + url.length;
|
|
778
|
+
}
|
|
779
|
+
return out + escHtml(text.slice(last));
|
|
780
|
+
}
|
|
781
|
+
/** A note (.md rendered, .txt as text with its web addresses linked) as HTML, and the links in it to missing files. */
|
|
782
|
+
export function noteHtml(env, folder, file, idPrefix) {
|
|
783
|
+
const broken = [];
|
|
784
|
+
let text;
|
|
785
|
+
try {
|
|
786
|
+
if (fs.statSync(file).size > MAX_NOTE_BYTES)
|
|
787
|
+
return { html: '', broken, long: true };
|
|
788
|
+
text = fs.readFileSync(file, 'utf8');
|
|
789
|
+
}
|
|
790
|
+
catch {
|
|
791
|
+
return { html: '<p class="dim">This note cannot be read.</p>', broken, long: false };
|
|
792
|
+
}
|
|
793
|
+
if (/\.txt$/i.test(file))
|
|
794
|
+
return { html: `<pre class="txt">${linkText(text.replace(/^\uFEFF/, '').replace(/\r\n?/g, '\n'))}</pre>`, broken, long: false };
|
|
795
|
+
const html = renderMarkdown(text, { idPrefix, link: (l, inner, at) => linkHtml(env, folder, l, inner, broken, { alone: at.alone, idPrefix }) });
|
|
796
|
+
return { html, broken, long: false };
|
|
797
|
+
}
|
|
798
|
+
export const WALK_LIMITS = { depth: 16, folders: 50_000, notes: 10_000 };
|
|
799
|
+
const turn = () => new Promise((r) => setImmediate(r));
|
|
800
|
+
/**
|
|
801
|
+
* Walk a root: count its folders and files (as its pages count them), keep its first images, and check every link of
|
|
802
|
+
* its notes (.md files), a flag for each that leads to a missing file. Engine projects below the root, other
|
|
803
|
+
* workspaces and TopoTopo checkouts are not entered (as the scan); plumbing is not either. A walk that stops short
|
|
804
|
+
* says where (`cut`).
|
|
805
|
+
*/
|
|
806
|
+
export async function walkTree(root, o = {}) {
|
|
807
|
+
const t0 = performance.now();
|
|
808
|
+
const lim = { ...WALK_LIMITS, ...o.limits };
|
|
809
|
+
const out = { folders: 0, files: 0, notes: 0, images: [], engines: [], flags: [], cut: [], ms: 0 };
|
|
810
|
+
const top = path.resolve(root.dir);
|
|
811
|
+
const queue = [{ abs: top, rel: '', depth: 0 }];
|
|
812
|
+
let since = performance.now(), deep = false, many = false, notesCut = false;
|
|
813
|
+
const pause = async () => { if (o.slice !== undefined && performance.now() - since > o.slice) {
|
|
814
|
+
await turn();
|
|
815
|
+
since = performance.now();
|
|
816
|
+
} };
|
|
817
|
+
for (let qi = 0; qi < queue.length; qi++) {
|
|
818
|
+
await pause();
|
|
819
|
+
const { abs, rel, depth } = queue[qi];
|
|
820
|
+
let entries;
|
|
821
|
+
try {
|
|
822
|
+
entries = fs.readdirSync(abs, { withFileTypes: true });
|
|
823
|
+
}
|
|
824
|
+
catch {
|
|
825
|
+
continue;
|
|
826
|
+
}
|
|
827
|
+
const files = entries.filter((e) => e.isFile()).map((e) => e.name);
|
|
828
|
+
if (rel) {
|
|
829
|
+
if (files.some((f) => engineTypeOf(f))) {
|
|
830
|
+
out.engines.push(rel);
|
|
831
|
+
continue;
|
|
832
|
+
}
|
|
833
|
+
if (files.includes(WORKSPACE_FILE) || (files.includes('package.json') && isCheckout(abs)))
|
|
834
|
+
continue;
|
|
835
|
+
}
|
|
836
|
+
const { shown } = shownFiles(abs, files);
|
|
837
|
+
out.files += shown.length;
|
|
838
|
+
for (const f of shown.sort(byName)) {
|
|
839
|
+
if (kindOf(f) === 'image' && out.images.length < 8)
|
|
840
|
+
out.images.push(rel ? `${rel}/${f}` : f);
|
|
841
|
+
if (!/\.md$/i.test(f))
|
|
842
|
+
continue;
|
|
843
|
+
if (out.notes >= lim.notes) {
|
|
844
|
+
notesCut = true;
|
|
845
|
+
continue;
|
|
846
|
+
}
|
|
847
|
+
out.notes++;
|
|
848
|
+
for (const href of noteHrefs(path.join(abs, f), o.links)) {
|
|
849
|
+
// one link that cannot be looked up never hides the others
|
|
850
|
+
let t;
|
|
851
|
+
try {
|
|
852
|
+
t = resolveLink(root, abs, href);
|
|
853
|
+
}
|
|
854
|
+
catch {
|
|
855
|
+
continue;
|
|
856
|
+
}
|
|
857
|
+
if (t.t === 'path' && !t.exists)
|
|
858
|
+
out.flags.push({ kind: 'link', at: rel, note: f, what: `${f} links to ${href}, which is not there`, todo: 'Fix the link, or put the file there' });
|
|
859
|
+
}
|
|
860
|
+
await pause();
|
|
861
|
+
}
|
|
862
|
+
for (const e of entries) {
|
|
863
|
+
if (!e.isDirectory() || plumbing(e.name, files))
|
|
864
|
+
continue;
|
|
865
|
+
out.folders++;
|
|
866
|
+
if (depth + 1 > lim.depth) {
|
|
867
|
+
deep = true;
|
|
868
|
+
continue;
|
|
869
|
+
}
|
|
870
|
+
if (queue.length >= lim.folders) {
|
|
871
|
+
many = true;
|
|
872
|
+
continue;
|
|
873
|
+
}
|
|
874
|
+
queue.push({ abs: path.join(abs, e.name), rel: rel ? `${rel}/${e.name}` : e.name, depth: depth + 1 });
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
if (many)
|
|
878
|
+
out.cut.push(`the first ${lim.folders.toLocaleString('en')} folders were audited`);
|
|
879
|
+
if (deep)
|
|
880
|
+
out.cut.push(`folders more than ${lim.depth} below the root were not audited`);
|
|
881
|
+
if (notesCut)
|
|
882
|
+
out.cut.push(`the first ${lim.notes.toLocaleString('en')} notes were checked for links`);
|
|
883
|
+
out.ms = Math.round(performance.now() - t0);
|
|
884
|
+
return out;
|
|
885
|
+
}
|
|
886
|
+
/** The links a note makes (each once), from `cache` while the note is unchanged. */
|
|
887
|
+
function noteHrefs(file, cache) {
|
|
888
|
+
const st = statOf(file);
|
|
889
|
+
if (!st || st.size > MAX_NOTE_BYTES)
|
|
890
|
+
return [];
|
|
891
|
+
const key = `${st.size}-${st.mtimeMs}`, have = cache?.get(file);
|
|
892
|
+
if (have?.key === key)
|
|
893
|
+
return have.hrefs;
|
|
894
|
+
let hrefs = [];
|
|
895
|
+
try {
|
|
896
|
+
hrefs = [...new Set(markdownLinks(fs.readFileSync(file, 'utf8')).map((l) => l.href))];
|
|
897
|
+
}
|
|
898
|
+
catch { /* unreadable: no links */ }
|
|
899
|
+
cache?.set(file, { key, hrefs });
|
|
900
|
+
return hrefs;
|
|
901
|
+
}
|
|
902
|
+
/** The node types a graph file's nodes have, nested graphs included ("owner/name/output" when it uses a package). */
|
|
903
|
+
export function graphTypes(file) {
|
|
904
|
+
let g;
|
|
905
|
+
try {
|
|
906
|
+
g = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
907
|
+
}
|
|
908
|
+
catch {
|
|
909
|
+
return [];
|
|
910
|
+
}
|
|
911
|
+
const out = new Set();
|
|
912
|
+
const walk = (x, depth) => {
|
|
913
|
+
if (!x || typeof x !== 'object' || depth > 32)
|
|
914
|
+
return;
|
|
915
|
+
if (Array.isArray(x)) {
|
|
916
|
+
for (const v of x)
|
|
917
|
+
walk(v, depth + 1);
|
|
918
|
+
return;
|
|
919
|
+
}
|
|
920
|
+
if (x.nodes && typeof x.nodes === 'object' && !Array.isArray(x.nodes)) {
|
|
921
|
+
for (const n of Object.values(x.nodes)) {
|
|
922
|
+
if (n && typeof n.type === 'string')
|
|
923
|
+
out.add(n.type);
|
|
924
|
+
if (n && n.params && typeof n.params === 'object')
|
|
925
|
+
walk(n.params, depth + 1);
|
|
926
|
+
}
|
|
927
|
+
return;
|
|
928
|
+
}
|
|
929
|
+
for (const v of Object.values(x))
|
|
930
|
+
walk(v, depth + 1);
|
|
931
|
+
};
|
|
932
|
+
walk(g, 0);
|
|
933
|
+
return [...out];
|
|
934
|
+
}
|
|
935
|
+
/** Exports below a package's exports/ with the source each records: sidecars, and GLB / glTF files that carry one. */
|
|
936
|
+
export function exportSources(dir) {
|
|
937
|
+
const out = [];
|
|
938
|
+
const walk = (abs, rel, depth) => {
|
|
939
|
+
let entries;
|
|
940
|
+
try {
|
|
941
|
+
entries = fs.readdirSync(abs, { withFileTypes: true });
|
|
942
|
+
}
|
|
943
|
+
catch {
|
|
944
|
+
return;
|
|
945
|
+
}
|
|
946
|
+
const names = new Set(entries.filter((e) => e.isFile()).map((e) => e.name));
|
|
947
|
+
for (const e of entries.sort((a, b) => byName(a.name, b.name))) {
|
|
948
|
+
if (e.name.startsWith('.'))
|
|
949
|
+
continue;
|
|
950
|
+
const r = rel ? `${rel}/${e.name}` : e.name;
|
|
951
|
+
if (e.isDirectory()) {
|
|
952
|
+
if (depth < 8)
|
|
953
|
+
walk(path.join(abs, e.name), r, depth + 1);
|
|
954
|
+
continue;
|
|
955
|
+
}
|
|
956
|
+
if (isSidecarName(e.name)) {
|
|
957
|
+
const made = readSource(path.join(abs, e.name));
|
|
958
|
+
if (made)
|
|
959
|
+
out.push({ rel: e.name.toLowerCase().endsWith('.source.json') ? r.slice(0, -'.source.json'.length) : r, made });
|
|
960
|
+
}
|
|
961
|
+
else if (/\.(glb|gltf)$/i.test(e.name) && !names.has(`${e.name}.source.json`)) {
|
|
962
|
+
const made = modelSource(path.join(abs, e.name));
|
|
963
|
+
if (made)
|
|
964
|
+
out.push({ rel: r, made });
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
};
|
|
968
|
+
walk(path.join(dir, 'exports'), '', 0);
|
|
969
|
+
return out;
|
|
970
|
+
}
|