@spyglassmc/core 0.4.34 → 0.4.35

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.
@@ -1,40 +0,0 @@
1
- import type { ExternalDownloaderOptions, Externals, Logger, RemoteUriString } from '../common/index.js';
2
- import type { RootUriString } from './fileUtil.js';
3
- export interface DownloaderDownloadOut {
4
- cacheUri?: string;
5
- checksum?: string;
6
- }
7
- export declare class Downloader {
8
- #private;
9
- private readonly cacheRoot;
10
- private readonly externals;
11
- private readonly logger;
12
- constructor(cacheRoot: RootUriString, externals: Externals, logger: Logger);
13
- download<R>(job: Job<R>, out?: DownloaderDownloadOut): Promise<R | undefined>;
14
- }
15
- interface Job<R> {
16
- /**
17
- * A unique ID for the cache.
18
- *
19
- * It also determines where the file is cached. Use slashes (`/`) to create directories.
20
- */
21
- id: string;
22
- uri: RemoteUriString;
23
- cache?: {
24
- /**
25
- * A download {@link Job} that will return a checksum of the latest remote data.
26
- */
27
- checksumJob: Omit<Job<string>, 'cache' | 'id'>;
28
- checksumExtension: `.${string}`;
29
- serializer?: (data: Uint8Array) => Uint8Array;
30
- deserializer?: (cache: Uint8Array) => Uint8Array;
31
- };
32
- transformer: (data: Uint8Array) => PromiseLike<R> | R;
33
- options?: ExternalDownloaderOptions;
34
- /**
35
- * If set, caches the result in memory. Time in milliseconds.
36
- */
37
- ttl?: number;
38
- }
39
- export {};
40
- //# sourceMappingURL=Downloader.d.ts.map
@@ -1,127 +0,0 @@
1
- import { bufferToString, Uri } from '../common/index.js';
2
- import { fileUtil } from './fileUtil.js';
3
- export class Downloader {
4
- cacheRoot;
5
- externals;
6
- logger;
7
- #memoryCache = new Map();
8
- constructor(cacheRoot, externals, logger) {
9
- this.cacheRoot = cacheRoot;
10
- this.externals = externals;
11
- this.logger = logger;
12
- }
13
- async download(job, out = {}) {
14
- const { id, cache, uri, options, transformer, ttl } = job;
15
- if (ttl && this.#memoryCache.has(uri)) {
16
- const memoryCacheEntry = this.#memoryCache.get(uri);
17
- const { buffer, time, cacheUri, checksum } = memoryCacheEntry;
18
- if (performance.now() <= time + ttl) {
19
- this.logger.info(`[Downloader] [${id}] Skipped thanks to valid cache in memory`);
20
- out.cacheUri = cacheUri;
21
- out.checksum = checksum;
22
- return await transformer(buffer);
23
- }
24
- else {
25
- this.#memoryCache.delete(uri);
26
- }
27
- }
28
- let checksum;
29
- let cacheUri;
30
- let cacheChecksumUri;
31
- if (cache) {
32
- const { checksumJob, checksumExtension } = cache;
33
- out.cacheUri = cacheUri = new Uri(`downloader/${id}`, this.cacheRoot).toString();
34
- cacheChecksumUri = new Uri(`downloader/${id}${checksumExtension}`, this.cacheRoot)
35
- .toString();
36
- try {
37
- out.checksum = checksum = await this.download({
38
- ...checksumJob,
39
- id: id + checksumExtension,
40
- });
41
- try {
42
- const cacheChecksum = bufferToString(await fileUtil.readFile(this.externals, cacheChecksumUri)).slice(0, -1); // Remove ending newline
43
- if (checksum === cacheChecksum) {
44
- try {
45
- const cachedBuffer = await fileUtil.readFile(this.externals, cacheUri);
46
- if (ttl) {
47
- this.#memoryCache.set(uri, {
48
- buffer: cachedBuffer,
49
- cacheUri,
50
- checksum,
51
- time: performance.now(),
52
- });
53
- }
54
- const deserializer = cache.deserializer ?? ((b) => b);
55
- const ans = await transformer(deserializer(cachedBuffer));
56
- this.logger.info(`[Downloader] [${id}] Skipped downloading thanks to cache ${cacheChecksum} (${cachedBuffer.length} bytes)`);
57
- return ans;
58
- }
59
- catch (e) {
60
- this.logger.error(`[Downloader] [${id}] Loading cached file ${cacheUri}`, e);
61
- if (this.externals.error.isKind(e, 'ENOENT')) {
62
- // Cache checksum exists, but cached file doesn't.
63
- // Remove the invalid cache checksum.
64
- try {
65
- await this.externals.fs.unlink(cacheChecksumUri);
66
- }
67
- catch (e) {
68
- this.logger.error(`[Downloader] [${id}] Removing invalid cache checksum ${cacheChecksumUri}`, e);
69
- }
70
- }
71
- }
72
- }
73
- }
74
- catch (e) {
75
- if (!this.externals.error.isKind(e, 'ENOENT')) {
76
- this.logger.error(`[Downloader] [${id}] Loading cache checksum ${cacheChecksumUri}`, e);
77
- }
78
- }
79
- }
80
- catch (e) {
81
- this.logger.error(`[Downloader] [${id}] Fetching latest checksum ${checksumJob.uri}`, e);
82
- }
83
- }
84
- try {
85
- const buffer = await this.externals.downloader.get(uri, options);
86
- if (ttl) {
87
- this.#memoryCache.set(uri, { buffer, time: performance.now() });
88
- }
89
- if (cache && cacheUri && cacheChecksumUri) {
90
- if (checksum) {
91
- try {
92
- await fileUtil.writeFile(this.externals, cacheChecksumUri, `${checksum}\n`);
93
- }
94
- catch (e) {
95
- this.logger.error(`[Downloader] [${id}] Saving cache checksum ${cacheChecksumUri}`, e);
96
- }
97
- }
98
- try {
99
- const serializer = cache.serializer ?? ((b) => b);
100
- await fileUtil.writeFile(this.externals, cacheUri, serializer(buffer));
101
- }
102
- catch (e) {
103
- this.logger.error(`[Downloader] [${id}] Caching file ${cacheUri}`, e);
104
- }
105
- }
106
- this.logger.info(`[Downloader] [${id}] Downloaded from ${uri} (${buffer.length} bytes)`);
107
- return await transformer(buffer);
108
- }
109
- catch (e) {
110
- this.logger.error(`[Downloader] [${id}] Downloading ${uri}`, e);
111
- if (cache && cacheUri) {
112
- try {
113
- const cachedBuffer = await fileUtil.readFile(this.externals, cacheUri);
114
- const deserializer = cache.deserializer ?? ((b) => b);
115
- const ans = await transformer(deserializer(cachedBuffer));
116
- this.logger.warn(`[Downloader] [${id}] Fell back to cached file ${cacheUri} (${cachedBuffer.length} bytes)`);
117
- return ans;
118
- }
119
- catch (e) {
120
- this.logger.error(`[Downloader] [${id}] Fallback: loading cached file ${cacheUri}`, e);
121
- }
122
- }
123
- }
124
- return undefined;
125
- }
126
- }
127
- //# sourceMappingURL=Downloader.js.map