@atlaspack/cache 3.1.1-canary.2 → 3.1.1-canary.200

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.
@@ -0,0 +1,27 @@
1
+ import type { Readable } from 'stream';
2
+ import type { FilePath } from '@atlaspack/types';
3
+ import type { FileSystem } from '@atlaspack/fs';
4
+ import type { Cache } from './types';
5
+ export declare class FSCache implements Cache {
6
+ #private;
7
+ fs: FileSystem;
8
+ dir: FilePath;
9
+ constructor(fs: FileSystem, cacheDir: FilePath);
10
+ ensure(): Promise<void>;
11
+ _getCachePath(cacheId: string): FilePath;
12
+ getStream(key: string): Readable;
13
+ setStream(key: string, stream: Readable): Promise<void>;
14
+ has(key: string): Promise<boolean>;
15
+ getBlob(key: string): Promise<Buffer>;
16
+ setBlob(key: string, contents: Buffer | string): Promise<void>;
17
+ getBuffer(key: string): Promise<Buffer | null | undefined>;
18
+ hasLargeBlob(key: string): Promise<boolean>;
19
+ getLargeBlob(key: string): Promise<Buffer>;
20
+ setLargeBlob(key: string, contents: Buffer | string, options?: {
21
+ signal?: AbortSignal;
22
+ }): Promise<void>;
23
+ deleteLargeBlob(key: string): Promise<void>;
24
+ get<T>(key: string): Promise<T | null | undefined>;
25
+ set(key: string, value: unknown): Promise<void>;
26
+ refresh(): void;
27
+ }
@@ -0,0 +1,22 @@
1
+ import type { Cache } from './types';
2
+ import { Readable } from 'stream';
3
+ export declare class IDBCache implements Cache {
4
+ store: any;
5
+ constructor();
6
+ ensure(): Promise<void>;
7
+ serialize(): Record<any, any>;
8
+ static deserialize(): IDBCache;
9
+ has(key: string): Promise<boolean>;
10
+ get<T>(key: string): Promise<T | null | undefined>;
11
+ set(key: string, value: unknown): Promise<void>;
12
+ getStream(key: string): Readable;
13
+ setStream(key: string, stream: Readable): Promise<void>;
14
+ getBlob(key: string): Promise<Buffer>;
15
+ setBlob(key: string, contents: Buffer | string): Promise<void>;
16
+ getBuffer(key: string): Promise<Buffer | null | undefined>;
17
+ hasLargeBlob(key: string): Promise<boolean>;
18
+ getLargeBlob(key: string): Promise<Buffer>;
19
+ setLargeBlob(key: string, contents: Buffer | string): Promise<void>;
20
+ deleteLargeBlob(key: string): Promise<void>;
21
+ refresh(): void;
22
+ }
@@ -0,0 +1,4 @@
1
+ import type { Cache } from './types';
2
+ export declare class IDBCache implements Cache {
3
+ constructor();
4
+ }
@@ -0,0 +1,78 @@
1
+ import { Lmdb } from '@atlaspack/rust';
2
+ import type { FilePath } from '@atlaspack/types';
3
+ import type { Cache } from './types';
4
+ import type { Readable } from 'stream';
5
+ import { NodeFS } from '@atlaspack/fs';
6
+ import { FSCache } from './FSCache';
7
+ interface DBOpenOptions {
8
+ name: string;
9
+ encoding: string;
10
+ compression: boolean;
11
+ }
12
+ export declare class LmdbWrapper {
13
+ lmdb: Lmdb;
14
+ constructor(lmdb: Lmdb);
15
+ has(key: string): boolean;
16
+ delete(key: string): Promise<void>;
17
+ get(key: string): Buffer | null;
18
+ put(key: string, value: Buffer | string): Promise<void>;
19
+ keys(): Iterable<string>;
20
+ compact(targetPath: string): void;
21
+ }
22
+ export declare function open(directory: string, openOptions: DBOpenOptions): LmdbWrapper;
23
+ export type SerLMDBLiteCache = {
24
+ dir: FilePath;
25
+ };
26
+ export declare class LMDBLiteCache implements Cache {
27
+ fs: NodeFS;
28
+ dir: FilePath;
29
+ store: LmdbWrapper;
30
+ fsCache: FSCache;
31
+ /**
32
+ * Directory where we store raw files.
33
+ */
34
+ cacheFilesDirectory: FilePath;
35
+ constructor(cacheDir: FilePath);
36
+ /**
37
+ * Use this to pass the native LMDB instance back to Rust.
38
+ */
39
+ getNativeRef(): Lmdb;
40
+ ensure(): Promise<void>;
41
+ serialize(): SerLMDBLiteCache;
42
+ static deserialize(cache: SerLMDBLiteCache): LMDBLiteCache;
43
+ has(key: string): Promise<boolean>;
44
+ get<T>(key: string): Promise<T | null | undefined>;
45
+ set(key: string, value: unknown): Promise<void>;
46
+ getStream(key: string): Readable;
47
+ setStream(key: string, stream: Readable): Promise<void>;
48
+ getBlob(key: string): Promise<Buffer>;
49
+ getBlobSync(key: string): Buffer;
50
+ setBlob(key: string, contents: Buffer | string): Promise<void>;
51
+ getBuffer(key: string): Promise<Buffer | null | undefined>;
52
+ hasLargeBlob(key: string): Promise<boolean>;
53
+ getLargeBlob(key: string): Promise<Buffer>;
54
+ setLargeBlob(key: string, contents: Buffer | string, options?: {
55
+ signal?: AbortSignal;
56
+ }): Promise<void>;
57
+ /**
58
+ * @deprecated Use store.delete instead.
59
+ */
60
+ deleteLargeBlob(key: string): Promise<void>;
61
+ keys(): Iterable<string>;
62
+ compact(targetPath: string): Promise<void>;
63
+ refresh(): void;
64
+ /**
65
+ * Streams, packages are stored in files instead of LMDB.
66
+ *
67
+ * On this case, if a cache key happens to have a parent traversal, ../..
68
+ * it is treated specially
69
+ *
70
+ * That is, something/../something and something are meant to be different
71
+ * keys.
72
+ *
73
+ * Plus we do not want to store values outside of the cache directory.
74
+ */
75
+ getFileKey(key: string): string;
76
+ clear(): Promise<void>;
77
+ }
78
+ export {};
@@ -0,0 +1 @@
1
+ export declare const WRITE_LIMIT_CHUNK: number;
@@ -0,0 +1,4 @@
1
+ export * from './FSCache';
2
+ export * from './IDBCache';
3
+ export * from './LMDBLiteCache';
4
+ export type { Cache } from './types';
@@ -0,0 +1,2 @@
1
+ import type { Cache } from '@atlaspack/types';
2
+ export type { Cache };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@atlaspack/cache",
3
3
  "description": "Interface for defining caches and file-system, IDB and LMDB implementations.",
4
- "version": "3.1.1-canary.2+88fcaf47c",
4
+ "version": "3.1.1-canary.200+10ee3fa5b",
5
5
  "license": "(MIT OR Apache-2.0)",
6
6
  "type": "commonjs",
7
7
  "publishConfig": {
@@ -11,24 +11,25 @@
11
11
  "type": "git",
12
12
  "url": "https://github.com/atlassian-labs/atlaspack.git"
13
13
  },
14
- "main": "lib/index.js",
15
- "source": "src/index.js",
16
- "types": "index.d.ts",
14
+ "main": "./lib/index.js",
15
+ "source": "./src/index.ts",
16
+ "types": "./lib/types/index.d.ts",
17
17
  "engines": {
18
18
  "node": ">= 16.0.0"
19
19
  },
20
20
  "scripts": {
21
21
  "test": "mocha",
22
- "build-ts": "mkdir -p lib && flow-to-ts src/types.js > lib/types.d.ts",
23
- "check-ts": "tsc --noEmit index.d.ts"
22
+ "check-ts": "tsc --emitDeclarationOnly --rootDir src",
23
+ "build:lib": "gulp build --gulpfile ../../../gulpfile.js --cwd ."
24
24
  },
25
25
  "dependencies": {
26
- "@atlaspack/build-cache": "2.13.3-canary.70+88fcaf47c",
27
- "@atlaspack/feature-flags": "2.14.1-canary.70+88fcaf47c",
28
- "@atlaspack/fs": "2.14.5-canary.2+88fcaf47c",
29
- "@atlaspack/logger": "2.14.5-canary.2+88fcaf47c",
30
- "@atlaspack/rust": "3.2.1-canary.2+88fcaf47c",
31
- "@atlaspack/utils": "2.14.5-canary.2+88fcaf47c"
26
+ "@atlaspack/build-cache": "2.13.3-canary.268+10ee3fa5b",
27
+ "@atlaspack/feature-flags": "2.14.1-canary.268+10ee3fa5b",
28
+ "@atlaspack/fs": "2.14.5-canary.200+10ee3fa5b",
29
+ "@atlaspack/logger": "2.14.5-canary.200+10ee3fa5b",
30
+ "@atlaspack/rust": "3.2.1-canary.200+10ee3fa5b",
31
+ "@atlaspack/utils": "2.14.5-canary.200+10ee3fa5b",
32
+ "ncp": "^2.0.0"
32
33
  },
33
34
  "devDependencies": {
34
35
  "idb": "^5.0.8"
@@ -36,5 +37,5 @@
36
37
  "browser": {
37
38
  "./src/IDBCache.js": "./src/IDBCache.browser.js"
38
39
  },
39
- "gitHead": "88fcaf47c77a6b7a16003028dd65a4b7a1f19dd7"
40
- }
40
+ "gitHead": "10ee3fa5b75a92acde8973673d9b3c5b6f3958e5"
41
+ }
@@ -1,5 +1,3 @@
1
- // @flow strict-local
2
-
3
1
  import type {Readable, Writable} from 'stream';
4
2
  import type {FilePath} from '@atlaspack/types';
5
3
  import type {FileSystem} from '@atlaspack/fs';
@@ -9,6 +7,8 @@ import stream from 'stream';
9
7
  import path from 'path';
10
8
  import {promisify} from 'util';
11
9
 
10
+ import {hashString} from '@atlaspack/rust';
11
+ import {getFeatureFlag} from '@atlaspack/feature-flags';
12
12
  import logger from '@atlaspack/logger';
13
13
  import {
14
14
  deserialize,
@@ -21,7 +21,7 @@ import packageJson from '../package.json';
21
21
 
22
22
  import {WRITE_LIMIT_CHUNK} from './constants';
23
23
 
24
- const pipeline: (Readable, Writable) => Promise<void> = promisify(
24
+ const pipeline: (arg1: Readable, arg2: Writable) => Promise<void> = promisify(
25
25
  stream.pipeline,
26
26
  );
27
27
 
@@ -40,9 +40,10 @@ export class FSCache implements Cache {
40
40
 
41
41
  // In parallel, create sub-directories for every possible hex value
42
42
  // This speeds up large caches on many file systems since there are fewer files in a single directory.
43
- let dirPromises = [];
43
+ let dirPromises: Array<Promise<undefined>> = [];
44
44
  for (let i = 0; i < 256; i++) {
45
45
  dirPromises.push(
46
+ // @ts-expect-error TS2345
46
47
  this.fs.mkdirp(path.join(this.dir, ('00' + i.toString(16)).slice(-2))),
47
48
  );
48
49
  }
@@ -51,6 +52,10 @@ export class FSCache implements Cache {
51
52
  }
52
53
 
53
54
  _getCachePath(cacheId: string): FilePath {
55
+ if (getFeatureFlag('cachePerformanceImprovements')) {
56
+ const cleanId = hashString(cacheId);
57
+ return path.join(this.dir, cleanId.slice(0, 2), cleanId.slice(2));
58
+ }
54
59
  return path.join(this.dir, cacheId.slice(0, 2), cacheId.slice(2));
55
60
  }
56
61
 
@@ -77,10 +82,10 @@ export class FSCache implements Cache {
77
82
  await this.fs.writeFile(this._getCachePath(key), contents);
78
83
  }
79
84
 
80
- async getBuffer(key: string): Promise<?Buffer> {
85
+ async getBuffer(key: string): Promise<Buffer | null | undefined> {
81
86
  try {
82
87
  return await this.fs.readFile(this._getCachePath(key));
83
- } catch (err) {
88
+ } catch (err: any) {
84
89
  if (err.code === 'ENOENT') {
85
90
  return null;
86
91
  } else {
@@ -90,14 +95,17 @@ export class FSCache implements Cache {
90
95
  }
91
96
 
92
97
  #getFilePath(key: string, index: number): string {
98
+ if (getFeatureFlag('cachePerformanceImprovements')) {
99
+ return path.join(this.dir, `${hashString(key)}-${index}`);
100
+ }
93
101
  return path.join(this.dir, `${key}-${index}`);
94
102
  }
95
103
 
96
- async #unlinkChunks(key: string, index: number): Promise<void> {
104
+ async #unlinkChunks(key: string, index: number): Promise<undefined> {
97
105
  try {
98
106
  await this.fs.unlink(this.#getFilePath(key, index));
99
107
  await this.#unlinkChunks(key, index + 1);
100
- } catch (err) {
108
+ } catch (err: any) {
101
109
  // If there's an error, no more chunks are left to delete
102
110
  }
103
111
  }
@@ -120,21 +128,26 @@ export class FSCache implements Cache {
120
128
  async setLargeBlob(
121
129
  key: string,
122
130
  contents: Buffer | string,
123
- options?: {|signal?: AbortSignal|},
131
+ options?: {
132
+ signal?: AbortSignal;
133
+ },
124
134
  ): Promise<void> {
125
135
  const chunks = Math.ceil(contents.length / WRITE_LIMIT_CHUNK);
126
136
 
127
- const writePromises: Promise<void>[] = [];
137
+ const writePromises: Promise<undefined>[] = [];
128
138
  if (chunks === 1) {
129
139
  // If there's one chunk, don't slice the content
130
140
  writePromises.push(
141
+ // @ts-expect-error TS2345
131
142
  this.fs.writeFile(this.#getFilePath(key, 0), contents, {
143
+ // @ts-expect-error TS2353
132
144
  signal: options?.signal,
133
145
  }),
134
146
  );
135
147
  } else {
136
148
  for (let i = 0; i < chunks; i += 1) {
137
149
  writePromises.push(
150
+ // @ts-expect-error TS2345
138
151
  this.fs.writeFile(
139
152
  this.#getFilePath(key, i),
140
153
  typeof contents === 'string'
@@ -146,6 +159,7 @@ export class FSCache implements Cache {
146
159
  i * WRITE_LIMIT_CHUNK,
147
160
  (i + 1) * WRITE_LIMIT_CHUNK,
148
161
  ),
162
+ // @ts-expect-error TS2353
149
163
  {signal: options?.signal},
150
164
  ),
151
165
  );
@@ -159,12 +173,13 @@ export class FSCache implements Cache {
159
173
  }
160
174
 
161
175
  async deleteLargeBlob(key: string): Promise<void> {
162
- const deletePromises: Promise<void>[] = [];
176
+ const deletePromises: Promise<undefined>[] = [];
163
177
 
164
178
  let i = 0;
165
179
  let filePath = this.#getFilePath(key, i);
166
180
 
167
181
  while (await this.fs.exists(filePath)) {
182
+ // @ts-expect-error TS2345
168
183
  deletePromises.push(this.fs.rimraf(filePath));
169
184
  i += 1;
170
185
  filePath = this.#getFilePath(key, i);
@@ -173,11 +188,11 @@ export class FSCache implements Cache {
173
188
  await Promise.all(deletePromises);
174
189
  }
175
190
 
176
- async get<T>(key: string): Promise<?T> {
191
+ async get<T>(key: string): Promise<T | null | undefined> {
177
192
  try {
178
193
  let data = await this.fs.readFile(this._getCachePath(key));
179
194
  return deserialize(data);
180
- } catch (err) {
195
+ } catch (err: any) {
181
196
  if (err.code === 'ENOENT') {
182
197
  return null;
183
198
  } else {
@@ -186,13 +201,13 @@ export class FSCache implements Cache {
186
201
  }
187
202
  }
188
203
 
189
- async set(key: string, value: mixed): Promise<void> {
204
+ async set(key: string, value: unknown): Promise<void> {
190
205
  try {
191
206
  let blobPath = this._getCachePath(key);
192
207
  let data = serialize(value);
193
208
 
194
209
  await this.fs.writeFile(blobPath, data);
195
- } catch (err) {
210
+ } catch (err: any) {
196
211
  logger.error(err, '@atlaspack/cache');
197
212
  }
198
213
  }
@@ -1,4 +1,3 @@
1
- // @flow strict-local
2
1
  import type {Cache} from './types';
3
2
 
4
3
  import {Readable} from 'stream';
@@ -9,21 +8,18 @@ import {
9
8
  serialize,
10
9
  } from '@atlaspack/build-cache';
11
10
  import {bufferStream} from '@atlaspack/utils';
12
- // $FlowFixMe[untyped-import]
13
11
  import {openDB} from 'idb';
14
12
 
15
- // $FlowFixMe[untyped-import]
16
13
  import packageJson from '../package.json';
17
14
 
18
15
  const STORE_NAME = 'cache';
19
16
 
20
17
  export class IDBCache implements Cache {
21
- // $FlowFixMe
22
18
  store: any;
23
19
 
24
20
  constructor() {
25
21
  this.store = openDB('REPL-parcel-cache', 1, {
26
- upgrade(db) {
22
+ upgrade(db: any) {
27
23
  db.createObjectStore(STORE_NAME);
28
24
  },
29
25
  blocked() {},
@@ -36,7 +32,7 @@ export class IDBCache implements Cache {
36
32
  return Promise.resolve();
37
33
  }
38
34
 
39
- serialize(): {||} {
35
+ serialize(): Record<any, any> {
40
36
  return {
41
37
  /*::...null*/
42
38
  };
@@ -50,7 +46,7 @@ export class IDBCache implements Cache {
50
46
  return Promise.resolve(this.store.get(key) != null);
51
47
  }
52
48
 
53
- async get<T>(key: string): Promise<?T> {
49
+ async get<T>(key: string): Promise<T | null | undefined> {
54
50
  let data = await (await this.store).get(STORE_NAME, key);
55
51
  if (data == null) {
56
52
  return null;
@@ -59,17 +55,19 @@ export class IDBCache implements Cache {
59
55
  return Promise.resolve(deserialize(data));
60
56
  }
61
57
 
62
- async set(key: string, value: mixed): Promise<void> {
58
+ async set(key: string, value: unknown): Promise<void> {
63
59
  await (await this.store).put(STORE_NAME, serialize(value), key);
64
60
  }
65
61
 
66
62
  getStream(key: string): Readable {
67
63
  let dataPromise = this.store
64
+ // @ts-expect-error TS7006
68
65
  .then((s) => s.get(STORE_NAME, key))
66
+ // @ts-expect-error TS7006
69
67
  .then((d) => Buffer.from(d))
68
+ // @ts-expect-error TS7006
70
69
  .catch((e) => e);
71
70
  const stream = new Readable({
72
- // $FlowFixMe(incompatible-call)
73
71
  async read() {
74
72
  let data = await dataPromise;
75
73
  if (data instanceof Error) {
@@ -118,7 +116,7 @@ export class IDBCache implements Cache {
118
116
  // ]);
119
117
  // }
120
118
 
121
- async getBuffer(key: string): Promise<?Buffer> {
119
+ async getBuffer(key: string): Promise<Buffer | null | undefined> {
122
120
  let data = await (await this.store).get(STORE_NAME, key);
123
121
  if (data == null) {
124
122
  return null;
@@ -1,7 +1,6 @@
1
- // @flow strict-local
2
1
  import type {Cache} from './types';
3
2
 
4
- // $FlowFixMe
3
+ // @ts-expect-error TS2420
5
4
  export class IDBCache implements Cache {
6
5
  constructor() {
7
6
  throw new Error('IDBCache is only supported in the browser');