@drawcall/cache 0.0.0-stage → 0.8.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/LICENSE +21 -0
- package/README.md +39 -2
- package/dist/browser.d.ts +15 -0
- package/dist/browser.js +41 -0
- package/dist/browser.js.map +1 -0
- package/dist/discover.d.ts +22 -0
- package/dist/discover.js +245 -0
- package/dist/discover.js.map +1 -0
- package/dist/disk.d.ts +29 -0
- package/dist/disk.js +19 -0
- package/dist/disk.js.map +1 -0
- package/dist/entry.d.ts +38 -0
- package/dist/entry.js +29 -0
- package/dist/entry.js.map +1 -0
- package/dist/inputs.d.ts +15 -0
- package/dist/inputs.js +77 -0
- package/dist/inputs.js.map +1 -0
- package/dist/node.d.ts +30 -0
- package/dist/node.js +110 -0
- package/dist/node.js.map +1 -0
- package/dist/vite.d.ts +8 -0
- package/dist/vite.js +441 -0
- package/dist/vite.js.map +1 -0
- package/package.json +78 -4
- package/skills/cache/SKILL.md +111 -0
package/dist/inputs.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { stat } from 'node:fs/promises';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
import { join, resolve } from 'node:path';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import { missing, read } from './disk.js';
|
|
7
|
+
/** The state of a file with these bytes. */
|
|
8
|
+
export function hashOf(bytes) {
|
|
9
|
+
return createHash('sha256').update(bytes).digest('hex');
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* File hashes by path, reused while size, mtime and ctime are unchanged. As git does, a hash taken within two seconds
|
|
13
|
+
* of the file's mtime is not kept, since a write in the same timestamp tick could follow it unseen.
|
|
14
|
+
*/
|
|
15
|
+
const fileHashes = new Map();
|
|
16
|
+
async function fileHash(path) {
|
|
17
|
+
const stats = await stat(path, { bigint: true }).catch((error) => {
|
|
18
|
+
if (missing(error))
|
|
19
|
+
return undefined;
|
|
20
|
+
throw error;
|
|
21
|
+
});
|
|
22
|
+
if (!stats)
|
|
23
|
+
return undefined;
|
|
24
|
+
const signature = [stats.ino, stats.size, stats.mtimeNs, stats.ctimeNs].join();
|
|
25
|
+
const known = fileHashes.get(path);
|
|
26
|
+
if (known?.stats === signature)
|
|
27
|
+
return known.hash;
|
|
28
|
+
const hash = read(path).then((bytes) => bytes && hashOf(bytes));
|
|
29
|
+
if (BigInt(Date.now()) * 1000000n - stats.mtimeNs < 2000000000n)
|
|
30
|
+
fileHashes.delete(path);
|
|
31
|
+
else
|
|
32
|
+
fileHashes.set(path, { stats: signature, hash });
|
|
33
|
+
return hash;
|
|
34
|
+
}
|
|
35
|
+
/** The installed version of a package as `root` resolves it, its own name included. */
|
|
36
|
+
async function packageVersion(root, name) {
|
|
37
|
+
const own = join(root, 'package.json');
|
|
38
|
+
const directories = createRequire(own).resolve.paths(name) ?? [];
|
|
39
|
+
for (const file of [own, ...directories.map((directory) => join(directory, name, 'package.json'))]) {
|
|
40
|
+
const bytes = await read(file);
|
|
41
|
+
if (!bytes)
|
|
42
|
+
continue;
|
|
43
|
+
const manifest = JSON.parse(new TextDecoder().decode(bytes));
|
|
44
|
+
if (file === own && manifest.name !== name)
|
|
45
|
+
continue;
|
|
46
|
+
if (typeof manifest.version !== 'string')
|
|
47
|
+
throw new TypeError(`${file} declares no version.`);
|
|
48
|
+
return manifest.version;
|
|
49
|
+
}
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
/** The current state of each input, a path read below `root`. */
|
|
53
|
+
export function record(root, inputs) {
|
|
54
|
+
return Promise.all(inputs.map(async (dependency) => ({
|
|
55
|
+
dependency,
|
|
56
|
+
state: 'path' in dependency
|
|
57
|
+
? await fileHash(dependency.path.startsWith('file:') ? fileURLToPath(dependency.path) : resolve(root, dependency.path))
|
|
58
|
+
: await packageVersion(root, dependency.package),
|
|
59
|
+
})));
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Whether the entry `key` in `directory` was saved from inputs that are all unchanged below `root`, and, given
|
|
63
|
+
* `warmed`, while the plugin warmed its load.
|
|
64
|
+
*/
|
|
65
|
+
export async function current(root, directory, key, warmed) {
|
|
66
|
+
const metadata = await read(join(directory, `${key}.json`));
|
|
67
|
+
if (!metadata)
|
|
68
|
+
return false;
|
|
69
|
+
// Written by save() below; any other shape compares unequal. Metadata from before 0.4 has no inputs, and misses.
|
|
70
|
+
const saved = JSON.parse(new TextDecoder().decode(metadata));
|
|
71
|
+
const { inputs } = saved;
|
|
72
|
+
if (!inputs || (warmed && saved.warmed !== true))
|
|
73
|
+
return false;
|
|
74
|
+
const now = await record(root, inputs.map((input) => input.dependency));
|
|
75
|
+
return JSON.stringify(now) === JSON.stringify(inputs);
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=inputs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inputs.js","sourceRoot":"","sources":["../src/inputs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACxC,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAS1C,4CAA4C;AAC5C,MAAM,UAAU,MAAM,CAAC,KAAiB;IACtC,OAAO,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAC1D,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,GAAG,IAAI,GAAG,EAAgE,CAAC;AAE3F,KAAK,UAAU,QAAQ,CAAC,IAAY;IACl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,MAAM,CAAC,KAAK,UAAU,OAAO,CAAC,IAAY,EAAE,SAAiB,EAAE,GAAW,EAAE,MAAe;IACzF,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,GAAG,GAAG,OAAO,CAAC,CAAC,CAAC;IAC5D,IAAI,CAAC,QAAQ;QAAE,OAAO,KAAK,CAAC;IAC5B,iHAAiH;IACjH,MAAM,KAAK,GAA8C,IAAI,CAAC,KAAK,CAAC,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC;IACxG,MAAM,EAAE,MAAM,EAAE,GAAG,KAAK,CAAC;IACzB,IAAI,CAAC,MAAM,IAAI,CAAC,MAAM,IAAI,KAAK,CAAC,MAAM,KAAK,IAAI,CAAC;QAAE,OAAO,KAAK,CAAC;IAC/D,MAAM,GAAG,GAAG,MAAM,MAAM,CACtB,IAAI,EACJ,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,UAAU,CAAC,CACxC,CAAC;IACF,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,KAAK,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;AACxD,CAAC","sourcesContent":["import { createHash } from 'node:crypto';\nimport { stat } from 'node:fs/promises';\nimport { createRequire } from 'node:module';\nimport { join, resolve } from 'node:path';\nimport { fileURLToPath } from 'node:url';\nimport { missing, read } from './disk.js';\nimport type { Input } from './entry.js';\n\n/** An input and its state when it was read: a file's hash or a package's version, undefined when it has neither. */\nexport interface Recorded {\n dependency: Input;\n state: string | undefined;\n}\n\n/** The state of a file with these bytes. */\nexport function hashOf(bytes: Uint8Array): string {\n return createHash('sha256').update(bytes).digest('hex');\n}\n\n/**\n * File hashes by path, reused while size, mtime and ctime are unchanged. As git does, a hash taken within two seconds\n * of the file's mtime is not kept, since a write in the same timestamp tick could follow it unseen.\n */\nconst fileHashes = new Map<string, { stats: string; hash: Promise<string | undefined> }>();\n\nasync function fileHash(path: string): Promise<string | undefined> {\n const stats = await stat(path, { bigint: true }).catch((error: unknown) => {\n if (missing(error)) return undefined;\n throw error;\n });\n if (!stats) return undefined;\n const signature = [stats.ino, stats.size, stats.mtimeNs, stats.ctimeNs].join();\n const known = fileHashes.get(path);\n if (known?.stats === signature) return known.hash;\n const hash = read(path).then((bytes) => bytes && hashOf(bytes));\n if (BigInt(Date.now()) * 1_000_000n - stats.mtimeNs < 2_000_000_000n) fileHashes.delete(path);\n else fileHashes.set(path, { stats: signature, hash });\n return hash;\n}\n\n/** The installed version of a package as `root` resolves it, its own name included. */\nasync function packageVersion(root: string, name: string): Promise<string | undefined> {\n const own = join(root, 'package.json');\n const directories = createRequire(own).resolve.paths(name) ?? [];\n for (const file of [own, ...directories.map((directory) => join(directory, name, 'package.json'))]) {\n const bytes = await read(file);\n if (!bytes) continue;\n const manifest: { name?: unknown; version?: unknown } = JSON.parse(new TextDecoder().decode(bytes));\n if (file === own && manifest.name !== name) continue;\n if (typeof manifest.version !== 'string') throw new TypeError(`${file} declares no version.`);\n return manifest.version;\n }\n return undefined;\n}\n\n/** The current state of each input, a path read below `root`. */\nexport function record(root: string, inputs: readonly Input[]): Promise<Recorded[]> {\n return Promise.all(\n inputs.map(async (dependency) => ({\n dependency,\n state:\n 'path' in dependency\n ? await fileHash(\n dependency.path.startsWith('file:') ? fileURLToPath(dependency.path) : resolve(root, dependency.path),\n )\n : await packageVersion(root, dependency.package),\n })),\n );\n}\n\n/**\n * Whether the entry `key` in `directory` was saved from inputs that are all unchanged below `root`, and, given\n * `warmed`, while the plugin warmed its load.\n */\nexport async function current(root: string, directory: string, key: string, warmed: boolean): Promise<boolean> {\n const metadata = await read(join(directory, `${key}.json`));\n if (!metadata) return false;\n // Written by save() below; any other shape compares unequal. Metadata from before 0.4 has no inputs, and misses.\n const saved: { inputs?: Recorded[]; warmed?: boolean } = JSON.parse(new TextDecoder().decode(metadata));\n const { inputs } = saved;\n if (!inputs || (warmed && saved.warmed !== true)) return false;\n const now = await record(\n root,\n inputs.map((input) => input.dependency),\n );\n return JSON.stringify(now) === JSON.stringify(inputs);\n}\n"]}
|
package/dist/node.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type CacheEntryOptions, type SaveOptions } from './entry.js';
|
|
2
|
+
export type { CacheEntryOptions, Dependency, Input, LoadFunction, SaveOptions, Value } from './entry.js';
|
|
3
|
+
/**
|
|
4
|
+
* The `file:` URL of the public file at a root-absolute path such as `/tree/model.glb`, below publicDir, without the
|
|
5
|
+
* path's query and hash, as save() takes it as an input. Any other URL is returned unchanged. The publicDir is the
|
|
6
|
+
* plugin's once it has run in this process, else `public` below the working directory.
|
|
7
|
+
*/
|
|
8
|
+
export declare function publicFile(url: string): string;
|
|
9
|
+
/**
|
|
10
|
+
* The URL to fetch a public file at: for a root-absolute path, a `data:` URL of its publicFile(), since Node fetches no
|
|
11
|
+
* `file:` URLs. It holds the whole file, so a file must fit in memory as a string. The file becomes an input of the
|
|
12
|
+
* load the plugin is warming. Any other URL is returned unchanged.
|
|
13
|
+
*/
|
|
14
|
+
export declare function publicURL(url: string): string;
|
|
15
|
+
/** A cached result in the cache directory on disk, which Node computes and saves for every environment. */
|
|
16
|
+
export declare class CacheEntry {
|
|
17
|
+
#private;
|
|
18
|
+
constructor(options: CacheEntryOptions);
|
|
19
|
+
/**
|
|
20
|
+
* The saved bytes while every input they were built from is unchanged, otherwise undefined. While the plugin warms
|
|
21
|
+
* the load, also undefined for an entry saved without the plugin, which records only the declared inputs.
|
|
22
|
+
*/
|
|
23
|
+
load(): Promise<Uint8Array | undefined>;
|
|
24
|
+
/**
|
|
25
|
+
* Saves the result computed after load(), built from the declared inputs as that load() found them, `dependencies`
|
|
26
|
+
* as they are now and, while the plugin warms the load, what it used as it was when the load got it. Throws, and
|
|
27
|
+
* deletes the entry, when an input changed since then, since the result may have been computed from the old content.
|
|
28
|
+
*/
|
|
29
|
+
save(value: Uint8Array, options?: SaveOptions): Promise<void>;
|
|
30
|
+
}
|
package/dist/node.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { mkdir, rm } from 'node:fs/promises';
|
|
3
|
+
import { join, relative, resolve, sep } from 'node:path';
|
|
4
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
5
|
+
import writeFileAtomic from 'write-file-atomic';
|
|
6
|
+
import { cacheName, read } from './disk.js';
|
|
7
|
+
import { current, hashOf, record } from './inputs.js';
|
|
8
|
+
import { key } from './entry.js';
|
|
9
|
+
/**
|
|
10
|
+
* The root and publicDir at `root`; without one, the plugin's once it has run in this process, else the working
|
|
11
|
+
* directory's.
|
|
12
|
+
*/
|
|
13
|
+
function project(root) {
|
|
14
|
+
if (root === undefined && globalThis.drawcallCache)
|
|
15
|
+
return globalThis.drawcallCache;
|
|
16
|
+
root ??= process.cwd();
|
|
17
|
+
return { root, publicDir: `${root}/public` };
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The `file:` URL of the public file at a root-absolute path such as `/tree/model.glb`, below publicDir, without the
|
|
21
|
+
* path's query and hash, as save() takes it as an input. Any other URL is returned unchanged. The publicDir is the
|
|
22
|
+
* plugin's once it has run in this process, else `public` below the working directory.
|
|
23
|
+
*/
|
|
24
|
+
export function publicFile(url) {
|
|
25
|
+
if (!url.startsWith('/') || url.startsWith('//'))
|
|
26
|
+
return url;
|
|
27
|
+
const base = pathToFileURL(`${project().publicDir}/`);
|
|
28
|
+
const file = new URL(`.${url.replace(/[?#].*/s, '')}`, base);
|
|
29
|
+
if (!file.href.startsWith(base.href))
|
|
30
|
+
throw new Error(`cache: ${url} lies outside publicDir`);
|
|
31
|
+
return file.href;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The URL to fetch a public file at: for a root-absolute path, a `data:` URL of its publicFile(), since Node fetches no
|
|
35
|
+
* `file:` URLs. It holds the whole file, so a file must fit in memory as a string. The file becomes an input of the
|
|
36
|
+
* load the plugin is warming. Any other URL is returned unchanged.
|
|
37
|
+
*/
|
|
38
|
+
export function publicURL(url) {
|
|
39
|
+
const file = publicFile(url);
|
|
40
|
+
if (file === url)
|
|
41
|
+
return url;
|
|
42
|
+
const bytes = readFileSync(new URL(file));
|
|
43
|
+
globalThis.drawcallCache?.warming.getStore()?.files.push({ dependency: { path: file }, state: hashOf(bytes) });
|
|
44
|
+
return `data:application/octet-stream;base64,${bytes.toString('base64')}`;
|
|
45
|
+
}
|
|
46
|
+
/** A cached result in the cache directory on disk, which Node computes and saves for every environment. */
|
|
47
|
+
export class CacheEntry {
|
|
48
|
+
#key;
|
|
49
|
+
#inputs;
|
|
50
|
+
#root;
|
|
51
|
+
#directory;
|
|
52
|
+
/** The declared inputs as the last load() found them, before the computation read them. */
|
|
53
|
+
#loaded;
|
|
54
|
+
constructor(options) {
|
|
55
|
+
this.#key = key(options);
|
|
56
|
+
const dependencies = Object.values(options.dependencies ?? {});
|
|
57
|
+
this.#inputs = dependencies.filter((dependency) => !('value' in dependency));
|
|
58
|
+
const { root, publicDir } = project(options.root);
|
|
59
|
+
this.#root = resolve(root);
|
|
60
|
+
this.#directory = resolve(publicDir, cacheName);
|
|
61
|
+
}
|
|
62
|
+
/** An input as recorded: a `file:` URL becomes a root-relative path, so that metadata holds no machine's paths. */
|
|
63
|
+
#portable(input) {
|
|
64
|
+
if (!('path' in input) || !input.path.startsWith('file:'))
|
|
65
|
+
return input;
|
|
66
|
+
return { path: relative(this.#root, fileURLToPath(input.path)).split(sep).join('/') };
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* The saved bytes while every input they were built from is unchanged, otherwise undefined. While the plugin warms
|
|
70
|
+
* the load, also undefined for an entry saved without the plugin, which records only the declared inputs.
|
|
71
|
+
*/
|
|
72
|
+
async load() {
|
|
73
|
+
globalThis.drawcallCache?.loaded.add(this.#key);
|
|
74
|
+
this.#loaded = await record(this.#root, this.#inputs);
|
|
75
|
+
const warming = globalThis.drawcallCache?.warming.getStore() !== undefined;
|
|
76
|
+
if (!(await current(this.#root, this.#directory, this.#key, warming)))
|
|
77
|
+
return undefined;
|
|
78
|
+
return read(join(this.#directory, `${this.#key}.bin`));
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Saves the result computed after load(), built from the declared inputs as that load() found them, `dependencies`
|
|
82
|
+
* as they are now and, while the plugin warms the load, what it used as it was when the load got it. Throws, and
|
|
83
|
+
* deletes the entry, when an input changed since then, since the result may have been computed from the old content.
|
|
84
|
+
*/
|
|
85
|
+
async save(value, options = {}) {
|
|
86
|
+
if (!this.#loaded)
|
|
87
|
+
throw new Error('Call CacheEntry.load() before computing the result to save.');
|
|
88
|
+
const warming = globalThis.drawcallCache?.warming.getStore();
|
|
89
|
+
const earlier = [...this.#loaded, ...(warming ? [...warming.files, ...warming.modules()] : [])].map(({ dependency, state }) => ({ dependency: this.#portable(dependency), state }));
|
|
90
|
+
const later = (options.dependencies ?? []).map((input) => this.#portable(input));
|
|
91
|
+
const id = (input) => JSON.stringify(input);
|
|
92
|
+
const dependencies = [...earlier.map((input) => input.dependency), ...later];
|
|
93
|
+
const inputs = await record(this.#root, [...new Map(dependencies.map((input) => [id(input), input])).values()]);
|
|
94
|
+
const missing = inputs.find((input) => input.state === undefined);
|
|
95
|
+
if (missing)
|
|
96
|
+
throw new Error(`cache input has no file or package: ${id(missing.dependency)}`);
|
|
97
|
+
const states = new Map(inputs.map((input) => [id(input.dependency), input.state]));
|
|
98
|
+
const changed = earlier.find((input) => states.get(id(input.dependency)) !== input.state);
|
|
99
|
+
if (changed) {
|
|
100
|
+
await Promise.all(['bin', 'json'].map((extension) => rm(join(this.#directory, `${this.#key}.${extension}`), { force: true })));
|
|
101
|
+
throw new Error(`cache input changed while the result was computed: ${id(changed.dependency)}`);
|
|
102
|
+
}
|
|
103
|
+
// Atomic writes, so that a reader sees the old file or the new one.
|
|
104
|
+
await mkdir(this.#directory, { recursive: true });
|
|
105
|
+
await writeFileAtomic(join(this.#directory, `${this.#key}.bin`), Buffer.from(value.buffer, value.byteOffset, value.byteLength));
|
|
106
|
+
const metadata = { inputs, warmed: warming !== undefined };
|
|
107
|
+
await writeFileAtomic(join(this.#directory, `${this.#key}.json`), JSON.stringify(metadata));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
//# sourceMappingURL=node.js.map
|
package/dist/node.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node.js","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,KAAK,EAAE,EAAE,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AACzD,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACxD,OAAO,eAAe,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAiB,MAAM,aAAa,CAAC;AACrE,OAAO,EAAE,GAAG,EAAwD,MAAM,YAAY,CAAC;AAGvF;;;GAGG;AACH,SAAS,OAAO,CAAC,IAAa;IAC5B,IAAI,IAAI,KAAK,SAAS,IAAI,UAAU,CAAC,aAAa;QAAE,OAAO,UAAU,CAAC,aAAa,CAAC;IACpF,IAAI,KAAK,OAAO,CAAC,GAAG,EAAE,CAAC;IACvB,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,GAAG,IAAI,SAAS,EAAE,CAAC;AAC/C,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAC,GAAW;IACpC,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,GAAG,CAAC,UAAU,CAAC,I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,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QAClD,MAAM,eAAe,CACnB,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,GAAG,IAAI,CAAC,IAAI,MAAM,CAAC,EACzC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,KAAK,CAAC,UAAU,EAAE,KAAK,CAAC,UAAU,CAAC,CAC9D,CAAC;QACF,MAAM,QAAQ,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,KAAK,SAAS,EAAE,CAAC;QAC3D,MAAM,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,GAAG,IAAI,CAAC,IAAI,OAAO,CAAC,EAAE,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC;IAC9F,CAAC;CACF","sourcesContent":["import { readFileSync } from 'node:fs';\nimport { mkdir, rm } from 'node:fs/promises';\nimport { join, relative, resolve, sep } from 'node:path';\nimport { fileURLToPath, pathToFileURL } from 'node:url';\nimport writeFileAtomic from 'write-file-atomic';\nimport { cacheName, read } from './disk.js';\nimport { current, hashOf, record, type Recorded } from './inputs.js';\nimport { key, type CacheEntryOptions, type Input, type SaveOptions } from './entry.js';\nexport type { CacheEntryOptions, Dependency, Input, LoadFunction, SaveOptions, Value } from './entry.js';\n\n/**\n * The root and publicDir at `root`; without one, the plugin's once it has run in this process, else the working\n * directory's.\n */\nfunction project(root?: string): { root: string; publicDir: string } {\n if (root === undefined && globalThis.drawcallCache) return globalThis.drawcallCache;\n root ??= process.cwd();\n return { root, publicDir: `${root}/public` };\n}\n\n/**\n * The `file:` URL of the public file at a root-absolute path such as `/tree/model.glb`, below publicDir, without the\n * path's query and hash, as save() takes it as an input. Any other URL is returned unchanged. The publicDir is the\n * plugin's once it has run in this process, else `public` below the working directory.\n */\nexport function publicFile(url: string): string {\n if (!url.startsWith('/') || url.startsWith('//')) return url;\n const base = pathToFileURL(`${project().publicDir}/`);\n const file = new URL(`.${url.replace(/[?#].*/s, '')}`, base);\n if (!file.href.startsWith(base.href)) throw new Error(`cache: ${url} lies outside publicDir`);\n return file.href;\n}\n\n/**\n * The URL to fetch a public file at: for a root-absolute path, a `data:` URL of its publicFile(), since Node fetches no\n * `file:` URLs. It holds the whole file, so a file must fit in memory as a string. The file becomes an input of the\n * load the plugin is warming. Any other URL is returned unchanged.\n */\nexport function publicURL(url: string): string {\n const file = publicFile(url);\n if (file === url) return url;\n const bytes = readFileSync(new URL(file));\n globalThis.drawcallCache?.warming.getStore()?.files.push({ dependency: { path: file }, state: hashOf(bytes) });\n return `data:application/octet-stream;base64,${bytes.toString('base64')}`;\n}\n\n/** A cached result in the cache directory on disk, which Node computes and saves for every environment. */\nexport class CacheEntry {\n readonly #key: string;\n readonly #inputs: Input[];\n readonly #root: string;\n readonly #directory: string;\n /** The declared inputs as the last load() found them, before the computation read them. */\n #loaded: Recorded[] | undefined;\n\n constructor(options: CacheEntryOptions) {\n this.#key = key(options);\n const dependencies = Object.values(options.dependencies ?? {});\n this.#inputs = dependencies.filter((dependency): dependency is Input => !('value' in dependency));\n const { root, publicDir } = project(options.root);\n this.#root = resolve(root);\n this.#directory = resolve(publicDir, cacheName);\n }\n\n /** An input as recorded: a `file:` URL becomes a root-relative path, so that metadata holds no machine's paths. */\n #portable(input: Input): Input {\n if (!('path' in input) || !input.path.startsWith('file:')) return input;\n return { path: relative(this.#root, fileURLToPath(input.path)).split(sep).join('/') };\n }\n\n /**\n * The saved bytes while every input they were built from is unchanged, otherwise undefined. While the plugin warms\n * the load, also undefined for an entry saved without the plugin, which records only the declared inputs.\n */\n async load(): Promise<Uint8Array | undefined> {\n globalThis.drawcallCache?.loaded.add(this.#key);\n this.#loaded = await record(this.#root, this.#inputs);\n const warming = globalThis.drawcallCache?.warming.getStore() !== undefined;\n if (!(await current(this.#root, this.#directory, this.#key, warming))) return undefined;\n return read(join(this.#directory, `${this.#key}.bin`));\n }\n\n /**\n * Saves the result computed after load(), built from the declared inputs as that load() found them, `dependencies`\n * as they are now and, while the plugin warms the load, what it used as it was when the load got it. Throws, and\n * deletes the entry, when an input changed since then, since the result may have been computed from the old content.\n */\n async save(value: Uint8Array, options: SaveOptions = {}): Promise<void> {\n if (!this.#loaded) throw new Error('Call CacheEntry.load() before computing the result to save.');\n const warming = globalThis.drawcallCache?.warming.getStore();\n const earlier = [...this.#loaded, ...(warming ? [...warming.files, ...warming.modules()] : [])].map(\n ({ dependency, state }) => ({ dependency: this.#portable(dependency), state }),\n );\n const later = (options.dependencies ?? []).map((input) => this.#portable(input));\n const id = (input: Input) => JSON.stringify(input);\n const dependencies = [...earlier.map((input) => input.dependency), ...later];\n const inputs = await record(this.#root, [...new Map(dependencies.map((input) => [id(input), input])).values()]);\n const missing = inputs.find((input) => input.state === undefined);\n if (missing) throw new Error(`cache input has no file or package: ${id(missing.dependency)}`);\n const states = new Map(inputs.map((input) => [id(input.dependency), input.state]));\n const changed = earlier.find((input) => states.get(id(input.dependency)) !== input.state);\n if (changed) {\n await Promise.all(\n ['bin', 'json'].map((extension) => rm(join(this.#directory, `${this.#key}.${extension}`), { force: true })),\n );\n throw new Error(`cache input changed while the result was computed: ${id(changed.dependency)}`);\n }\n // Atomic writes, so that a reader sees the old file or the new one.\n await mkdir(this.#directory, { recursive: true });\n await writeFileAtomic(\n join(this.#directory, `${this.#key}.bin`),\n Buffer.from(value.buffer, value.byteOffset, value.byteLength),\n );\n const metadata = { inputs, warmed: warming !== undefined };\n await writeFileAtomic(join(this.#directory, `${this.#key}.json`), JSON.stringify(metadata));\n }\n}\n"]}
|
package/dist/vite.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type Plugin } from 'vite';
|
|
2
|
+
/**
|
|
3
|
+
* Finds cached loader loads in the project's source files, and in publicDir's `.glts` scripts and every JavaScript and
|
|
4
|
+
* TypeScript module there, and warms them in Node, so that browsers read what Node saved, then deletes the entries no
|
|
5
|
+
* load uses. In development it scans and warms at startup and again after every file change, and holds a request for an
|
|
6
|
+
* entry whose inputs changed, or that does not exist, until warming finishes. A build scans and warms once.
|
|
7
|
+
*/
|
|
8
|
+
export declare function cache(): Plugin[];
|