@atlaspack/cache 3.2.16 → 3.2.17-typescript-17c3d1dec.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 +201 -0
- package/lib/FSCache.d.ts +27 -0
- package/lib/FSCache.js +14 -3
- package/lib/IDBCache.browser.d.ts +22 -0
- package/lib/IDBCache.browser.js +7 -6
- package/lib/IDBCache.d.ts +4 -0
- package/lib/IDBCache.js +1 -1
- package/lib/LMDBLiteCache.d.ts +78 -0
- package/lib/LMDBLiteCache.js +2 -1
- package/lib/constants.d.ts +1 -0
- package/lib/index.d.ts +4 -0
- package/lib/types.d.ts +1 -1
- package/package.json +14 -14
- package/src/{FSCache.js → FSCache.ts} +21 -15
- package/src/{IDBCache.browser.js → IDBCache.browser.ts} +8 -10
- package/src/{IDBCache.js → IDBCache.ts} +1 -2
- package/src/{LMDBLiteCache.js → LMDBLiteCache.ts} +11 -11
- package/src/{constants.js → constants.ts} +0 -2
- package/src/{index.js → index.ts} +0 -2
- package/src/{types.js → types.ts} +0 -1
- package/test/{LMDBLiteCache.test.js → LMDBLiteCache.test.ts} +9 -11
- package/tsconfig.json +4 -0
- package/index.d.ts +0 -13
package/LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright (c) 2024 Atlassian US., Inc.
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
package/lib/FSCache.d.ts
ADDED
@@ -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
|
+
}
|
package/lib/FSCache.js
CHANGED
@@ -57,6 +57,7 @@ var _package = _interopRequireDefault(require("../package.json"));
|
|
57
57
|
var _constants = require("./constants");
|
58
58
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
59
59
|
// flowlint-next-line untyped-import:off
|
60
|
+
|
60
61
|
const pipeline = (0, _util().promisify)(_stream().default.pipeline);
|
61
62
|
class FSCache {
|
62
63
|
constructor(fs, cacheDir) {
|
@@ -71,7 +72,9 @@ class FSCache {
|
|
71
72
|
// This speeds up large caches on many file systems since there are fewer files in a single directory.
|
72
73
|
let dirPromises = [];
|
73
74
|
for (let i = 0; i < 256; i++) {
|
74
|
-
dirPromises.push(
|
75
|
+
dirPromises.push(
|
76
|
+
// @ts-expect-error TS2345
|
77
|
+
this.fs.mkdirp(_path().default.join(this.dir, ('00' + i.toString(16)).slice(-2))));
|
75
78
|
}
|
76
79
|
await Promise.all(dirPromises);
|
77
80
|
}
|
@@ -138,12 +141,19 @@ class FSCache {
|
|
138
141
|
const writePromises = [];
|
139
142
|
if (chunks === 1) {
|
140
143
|
// If there's one chunk, don't slice the content
|
141
|
-
writePromises.push(
|
144
|
+
writePromises.push(
|
145
|
+
// @ts-expect-error TS2345
|
146
|
+
this.fs.writeFile(this.#getFilePath(key, 0), contents, {
|
147
|
+
// @ts-expect-error TS2353
|
142
148
|
signal: options === null || options === void 0 ? void 0 : options.signal
|
143
149
|
}));
|
144
150
|
} else {
|
145
151
|
for (let i = 0; i < chunks; i += 1) {
|
146
|
-
writePromises.push(
|
152
|
+
writePromises.push(
|
153
|
+
// @ts-expect-error TS2345
|
154
|
+
this.fs.writeFile(this.#getFilePath(key, i), typeof contents === 'string' ? contents.slice(i * _constants.WRITE_LIMIT_CHUNK, (i + 1) * _constants.WRITE_LIMIT_CHUNK) : contents.subarray(i * _constants.WRITE_LIMIT_CHUNK, (i + 1) * _constants.WRITE_LIMIT_CHUNK),
|
155
|
+
// @ts-expect-error TS2353
|
156
|
+
{
|
147
157
|
signal: options === null || options === void 0 ? void 0 : options.signal
|
148
158
|
}));
|
149
159
|
}
|
@@ -158,6 +168,7 @@ class FSCache {
|
|
158
168
|
let i = 0;
|
159
169
|
let filePath = this.#getFilePath(key, i);
|
160
170
|
while (await this.fs.exists(filePath)) {
|
171
|
+
// @ts-expect-error TS2345
|
161
172
|
deletePromises.push(this.fs.rimraf(filePath));
|
162
173
|
i += 1;
|
163
174
|
filePath = this.#getFilePath(key, i);
|
@@ -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
|
+
}
|
package/lib/IDBCache.browser.js
CHANGED
@@ -34,12 +34,8 @@ function _idb() {
|
|
34
34
|
}
|
35
35
|
var _package = _interopRequireDefault(require("../package.json"));
|
36
36
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
37
|
-
// $FlowFixMe[untyped-import]
|
38
|
-
// $FlowFixMe[untyped-import]
|
39
37
|
const STORE_NAME = 'cache';
|
40
38
|
class IDBCache {
|
41
|
-
// $FlowFixMe
|
42
|
-
|
43
39
|
constructor() {
|
44
40
|
this.store = (0, _idb().openDB)('REPL-parcel-cache', 1, {
|
45
41
|
upgrade(db) {
|
@@ -75,9 +71,14 @@ class IDBCache {
|
|
75
71
|
await (await this.store).put(STORE_NAME, (0, _buildCache().serialize)(value), key);
|
76
72
|
}
|
77
73
|
getStream(key) {
|
78
|
-
let dataPromise = this.store
|
74
|
+
let dataPromise = this.store
|
75
|
+
// @ts-expect-error TS7006
|
76
|
+
.then(s => s.get(STORE_NAME, key))
|
77
|
+
// @ts-expect-error TS7006
|
78
|
+
.then(d => Buffer.from(d))
|
79
|
+
// @ts-expect-error TS7006
|
80
|
+
.catch(e => e);
|
79
81
|
const stream = new (_stream().Readable)({
|
80
|
-
// $FlowFixMe(incompatible-call)
|
81
82
|
async read() {
|
82
83
|
let data = await dataPromise;
|
83
84
|
if (data instanceof Error) {
|
package/lib/IDBCache.js
CHANGED
@@ -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 {};
|
package/lib/LMDBLiteCache.js
CHANGED
@@ -71,7 +71,8 @@ function _logger() {
|
|
71
71
|
return data;
|
72
72
|
}
|
73
73
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
74
|
-
//
|
74
|
+
// @ts-expect-error TS7016
|
75
|
+
|
75
76
|
const ncpAsync = (0, _util().promisify)(_ncp().default);
|
76
77
|
class LmdbWrapper {
|
77
78
|
constructor(lmdb) {
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare const WRITE_LIMIT_CHUNK: number;
|
package/lib/index.d.ts
ADDED
package/lib/types.d.ts
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
import type { Cache } from
|
1
|
+
import type { Cache } from '@atlaspack/types';
|
2
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.2.
|
4
|
+
"version": "3.2.17-typescript-17c3d1dec.0",
|
5
5
|
"license": "(MIT OR Apache-2.0)",
|
6
6
|
"type": "commonjs",
|
7
7
|
"publishConfig": {
|
@@ -11,24 +11,23 @@
|
|
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.
|
16
|
-
"types": "index.d.ts",
|
14
|
+
"main": "./lib/index.js",
|
15
|
+
"source": "./src/index.ts",
|
16
|
+
"types": "./lib/index.d.ts",
|
17
17
|
"engines": {
|
18
18
|
"node": ">= 16.0.0"
|
19
19
|
},
|
20
20
|
"scripts": {
|
21
21
|
"test": "mocha",
|
22
|
-
"
|
23
|
-
"check-ts": "tsc --noEmit index.d.ts"
|
22
|
+
"check-ts": "tsc --emitDeclarationOnly --rootDir src"
|
24
23
|
},
|
25
24
|
"dependencies": {
|
26
|
-
"@atlaspack/build-cache": "2.13.
|
27
|
-
"@atlaspack/feature-flags": "2.20.0",
|
28
|
-
"@atlaspack/fs": "2.15.
|
29
|
-
"@atlaspack/logger": "2.14.
|
30
|
-
"@atlaspack/rust": "3.4.
|
31
|
-
"@atlaspack/utils": "2.17.
|
25
|
+
"@atlaspack/build-cache": "2.13.4-typescript-17c3d1dec.0",
|
26
|
+
"@atlaspack/feature-flags": "2.20.1-typescript-17c3d1dec.0",
|
27
|
+
"@atlaspack/fs": "2.15.17-typescript-17c3d1dec.0",
|
28
|
+
"@atlaspack/logger": "2.14.14-typescript-17c3d1dec.0",
|
29
|
+
"@atlaspack/rust": "3.4.2-typescript-17c3d1dec.0",
|
30
|
+
"@atlaspack/utils": "2.17.4-typescript-17c3d1dec.0",
|
32
31
|
"ncp": "^2.0.0"
|
33
32
|
},
|
34
33
|
"devDependencies": {
|
@@ -36,5 +35,6 @@
|
|
36
35
|
},
|
37
36
|
"browser": {
|
38
37
|
"./src/IDBCache.js": "./src/IDBCache.browser.js"
|
39
|
-
}
|
40
|
-
|
38
|
+
},
|
39
|
+
"gitHead": "17c3d1decf641e688fcfe3ca985e80e9897b7573"
|
40
|
+
}
|
@@ -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';
|
@@ -23,7 +21,7 @@ import packageJson from '../package.json';
|
|
23
21
|
|
24
22
|
import {WRITE_LIMIT_CHUNK} from './constants';
|
25
23
|
|
26
|
-
const pipeline: (Readable, Writable) => Promise<void> = promisify(
|
24
|
+
const pipeline: (arg1: Readable, arg2: Writable) => Promise<void> = promisify(
|
27
25
|
stream.pipeline,
|
28
26
|
);
|
29
27
|
|
@@ -42,9 +40,10 @@ export class FSCache implements Cache {
|
|
42
40
|
|
43
41
|
// In parallel, create sub-directories for every possible hex value
|
44
42
|
// This speeds up large caches on many file systems since there are fewer files in a single directory.
|
45
|
-
let dirPromises = [];
|
43
|
+
let dirPromises: Array<Promise<undefined>> = [];
|
46
44
|
for (let i = 0; i < 256; i++) {
|
47
45
|
dirPromises.push(
|
46
|
+
// @ts-expect-error TS2345
|
48
47
|
this.fs.mkdirp(path.join(this.dir, ('00' + i.toString(16)).slice(-2))),
|
49
48
|
);
|
50
49
|
}
|
@@ -83,10 +82,10 @@ export class FSCache implements Cache {
|
|
83
82
|
await this.fs.writeFile(this._getCachePath(key), contents);
|
84
83
|
}
|
85
84
|
|
86
|
-
async getBuffer(key: string): Promise
|
85
|
+
async getBuffer(key: string): Promise<Buffer | null | undefined> {
|
87
86
|
try {
|
88
87
|
return await this.fs.readFile(this._getCachePath(key));
|
89
|
-
} catch (err) {
|
88
|
+
} catch (err: any) {
|
90
89
|
if (err.code === 'ENOENT') {
|
91
90
|
return null;
|
92
91
|
} else {
|
@@ -102,11 +101,11 @@ export class FSCache implements Cache {
|
|
102
101
|
return path.join(this.dir, `${key}-${index}`);
|
103
102
|
}
|
104
103
|
|
105
|
-
async #unlinkChunks(key: string, index: number): Promise<
|
104
|
+
async #unlinkChunks(key: string, index: number): Promise<undefined> {
|
106
105
|
try {
|
107
106
|
await this.fs.unlink(this.#getFilePath(key, index));
|
108
107
|
await this.#unlinkChunks(key, index + 1);
|
109
|
-
} catch (err) {
|
108
|
+
} catch (err: any) {
|
110
109
|
// If there's an error, no more chunks are left to delete
|
111
110
|
}
|
112
111
|
}
|
@@ -129,21 +128,26 @@ export class FSCache implements Cache {
|
|
129
128
|
async setLargeBlob(
|
130
129
|
key: string,
|
131
130
|
contents: Buffer | string,
|
132
|
-
options?: {
|
131
|
+
options?: {
|
132
|
+
signal?: AbortSignal;
|
133
|
+
},
|
133
134
|
): Promise<void> {
|
134
135
|
const chunks = Math.ceil(contents.length / WRITE_LIMIT_CHUNK);
|
135
136
|
|
136
|
-
const writePromises: Promise<
|
137
|
+
const writePromises: Promise<undefined>[] = [];
|
137
138
|
if (chunks === 1) {
|
138
139
|
// If there's one chunk, don't slice the content
|
139
140
|
writePromises.push(
|
141
|
+
// @ts-expect-error TS2345
|
140
142
|
this.fs.writeFile(this.#getFilePath(key, 0), contents, {
|
143
|
+
// @ts-expect-error TS2353
|
141
144
|
signal: options?.signal,
|
142
145
|
}),
|
143
146
|
);
|
144
147
|
} else {
|
145
148
|
for (let i = 0; i < chunks; i += 1) {
|
146
149
|
writePromises.push(
|
150
|
+
// @ts-expect-error TS2345
|
147
151
|
this.fs.writeFile(
|
148
152
|
this.#getFilePath(key, i),
|
149
153
|
typeof contents === 'string'
|
@@ -155,6 +159,7 @@ export class FSCache implements Cache {
|
|
155
159
|
i * WRITE_LIMIT_CHUNK,
|
156
160
|
(i + 1) * WRITE_LIMIT_CHUNK,
|
157
161
|
),
|
162
|
+
// @ts-expect-error TS2353
|
158
163
|
{signal: options?.signal},
|
159
164
|
),
|
160
165
|
);
|
@@ -168,12 +173,13 @@ export class FSCache implements Cache {
|
|
168
173
|
}
|
169
174
|
|
170
175
|
async deleteLargeBlob(key: string): Promise<void> {
|
171
|
-
const deletePromises: Promise<
|
176
|
+
const deletePromises: Promise<undefined>[] = [];
|
172
177
|
|
173
178
|
let i = 0;
|
174
179
|
let filePath = this.#getFilePath(key, i);
|
175
180
|
|
176
181
|
while (await this.fs.exists(filePath)) {
|
182
|
+
// @ts-expect-error TS2345
|
177
183
|
deletePromises.push(this.fs.rimraf(filePath));
|
178
184
|
i += 1;
|
179
185
|
filePath = this.#getFilePath(key, i);
|
@@ -182,11 +188,11 @@ export class FSCache implements Cache {
|
|
182
188
|
await Promise.all(deletePromises);
|
183
189
|
}
|
184
190
|
|
185
|
-
async get<T>(key: string): Promise
|
191
|
+
async get<T>(key: string): Promise<T | null | undefined> {
|
186
192
|
try {
|
187
193
|
let data = await this.fs.readFile(this._getCachePath(key));
|
188
194
|
return deserialize(data);
|
189
|
-
} catch (err) {
|
195
|
+
} catch (err: any) {
|
190
196
|
if (err.code === 'ENOENT') {
|
191
197
|
return null;
|
192
198
|
} else {
|
@@ -195,13 +201,13 @@ export class FSCache implements Cache {
|
|
195
201
|
}
|
196
202
|
}
|
197
203
|
|
198
|
-
async set(key: string, value:
|
204
|
+
async set(key: string, value: unknown): Promise<void> {
|
199
205
|
try {
|
200
206
|
let blobPath = this._getCachePath(key);
|
201
207
|
let data = serialize(value);
|
202
208
|
|
203
209
|
await this.fs.writeFile(blobPath, data);
|
204
|
-
} catch (err) {
|
210
|
+
} catch (err: any) {
|
205
211
|
logger.error(err, '@atlaspack/cache');
|
206
212
|
}
|
207
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
|
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:
|
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
|
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,5 +1,3 @@
|
|
1
|
-
// @flow strict-local
|
2
|
-
|
3
1
|
import {
|
4
2
|
deserialize,
|
5
3
|
registerSerializableClass,
|
@@ -10,12 +8,12 @@ import {Lmdb} from '@atlaspack/rust';
|
|
10
8
|
import type {FilePath} from '@atlaspack/types';
|
11
9
|
import type {Cache} from './types';
|
12
10
|
import type {Readable, Writable} from 'stream';
|
11
|
+
// @ts-expect-error TS7016
|
13
12
|
import ncp from 'ncp';
|
14
13
|
import {promisify} from 'util';
|
15
14
|
import stream from 'stream';
|
16
15
|
import path from 'path';
|
17
16
|
import {NodeFS} from '@atlaspack/fs';
|
18
|
-
// $FlowFixMe
|
19
17
|
import packageJson from '../package.json';
|
20
18
|
import {FSCache} from './FSCache';
|
21
19
|
import {instrumentAsync} from '@atlaspack/logger';
|
@@ -89,13 +87,13 @@ export function open(
|
|
89
87
|
);
|
90
88
|
}
|
91
89
|
|
92
|
-
const pipeline: (Readable, Writable) => Promise<void> = promisify(
|
90
|
+
const pipeline: (arg1: Readable, arg2: Writable) => Promise<void> = promisify(
|
93
91
|
stream.pipeline,
|
94
92
|
);
|
95
93
|
|
96
|
-
export type SerLMDBLiteCache = {
|
97
|
-
dir: FilePath
|
98
|
-
|
94
|
+
export type SerLMDBLiteCache = {
|
95
|
+
dir: FilePath;
|
96
|
+
};
|
99
97
|
|
100
98
|
export class LMDBLiteCache implements Cache {
|
101
99
|
fs: NodeFS;
|
@@ -149,7 +147,7 @@ export class LMDBLiteCache implements Cache {
|
|
149
147
|
return Promise.resolve(this.store.has(key));
|
150
148
|
}
|
151
149
|
|
152
|
-
get<T>(key: string): Promise
|
150
|
+
get<T>(key: string): Promise<T | null | undefined> {
|
153
151
|
let data = this.store.get(key);
|
154
152
|
if (data == null) {
|
155
153
|
return Promise.resolve(null);
|
@@ -158,7 +156,7 @@ export class LMDBLiteCache implements Cache {
|
|
158
156
|
return Promise.resolve(deserialize(data));
|
159
157
|
}
|
160
158
|
|
161
|
-
async set(key: string, value:
|
159
|
+
async set(key: string, value: unknown): Promise<void> {
|
162
160
|
await this.setBlob(key, serialize(value));
|
163
161
|
}
|
164
162
|
|
@@ -200,7 +198,7 @@ export class LMDBLiteCache implements Cache {
|
|
200
198
|
await this.store.put(key, contents);
|
201
199
|
}
|
202
200
|
|
203
|
-
getBuffer(key: string): Promise
|
201
|
+
getBuffer(key: string): Promise<Buffer | null | undefined> {
|
204
202
|
return Promise.resolve(this.store.get(key));
|
205
203
|
}
|
206
204
|
|
@@ -222,7 +220,9 @@ export class LMDBLiteCache implements Cache {
|
|
222
220
|
async setLargeBlob(
|
223
221
|
key: string,
|
224
222
|
contents: Buffer | string,
|
225
|
-
options?: {
|
223
|
+
options?: {
|
224
|
+
signal?: AbortSignal;
|
225
|
+
},
|
226
226
|
): Promise<void> {
|
227
227
|
if (!getFeatureFlag('cachePerformanceImprovements')) {
|
228
228
|
return this.fsCache.setLargeBlob(key, contents, options);
|
@@ -1,5 +1,3 @@
|
|
1
|
-
// @flow
|
2
|
-
|
3
1
|
import * as fs from 'fs';
|
4
2
|
import * as path from 'path';
|
5
3
|
import {tmpdir} from 'os';
|
@@ -12,7 +10,7 @@ import {initializeMonitoring} from '@atlaspack/rust';
|
|
12
10
|
const cacheDir = path.join(tmpdir(), 'lmdb-lite-cache-tests');
|
13
11
|
|
14
12
|
describe('LMDBLiteCache', () => {
|
15
|
-
let cache;
|
13
|
+
let cache: any;
|
16
14
|
|
17
15
|
beforeEach(async () => {
|
18
16
|
await fs.promises.rm(cacheDir, {recursive: true, force: true});
|
@@ -129,7 +127,7 @@ describe('LMDBLiteCache', () => {
|
|
129
127
|
await cache.ensure();
|
130
128
|
await cache.setBlob(`key${i}`, Buffer.from(serialize({value: i})));
|
131
129
|
|
132
|
-
await new Promise((resolve) => setTimeout(resolve, 10));
|
130
|
+
await new Promise((resolve: any) => setTimeout(resolve, 10));
|
133
131
|
}
|
134
132
|
|
135
133
|
const finalCache = new LMDBLiteCache(testDir);
|
@@ -144,7 +142,7 @@ describe('LMDBLiteCache', () => {
|
|
144
142
|
|
145
143
|
try {
|
146
144
|
initializeMonitoring();
|
147
|
-
} catch (error) {
|
145
|
+
} catch (error: any) {
|
148
146
|
/* empty */
|
149
147
|
}
|
150
148
|
|
@@ -165,8 +163,8 @@ describe('LMDBLiteCache', () => {
|
|
165
163
|
|
166
164
|
const numWorkers = 10;
|
167
165
|
|
168
|
-
const workers = [];
|
169
|
-
const responsePromises = [];
|
166
|
+
const workers: Array<any> = [];
|
167
|
+
const responsePromises: Array<any> = [];
|
170
168
|
for (let i = 0; i < numWorkers; i++) {
|
171
169
|
const worker = new Worker(path.join(__dirname, 'workerThreadsTest.js'), {
|
172
170
|
workerData: {
|
@@ -175,16 +173,16 @@ describe('LMDBLiteCache', () => {
|
|
175
173
|
});
|
176
174
|
workers.push(worker);
|
177
175
|
|
178
|
-
const responsePromise = new Promise((resolve, reject) => {
|
176
|
+
const responsePromise = new Promise((resolve: any, reject: any) => {
|
179
177
|
worker.addListener('error', (error: Error) => {
|
180
178
|
reject(error);
|
181
179
|
});
|
182
|
-
worker.addListener('message', (message) => {
|
180
|
+
worker.addListener('message', (message: any) => {
|
183
181
|
resolve(message);
|
184
182
|
});
|
185
183
|
});
|
186
184
|
|
187
|
-
worker.addListener('message', (message) => {
|
185
|
+
worker.addListener('message', (message: any) => {
|
188
186
|
// eslint-disable-next-line no-console
|
189
187
|
console.log('Worker message', message);
|
190
188
|
});
|
@@ -236,7 +234,7 @@ describe('LMDBLiteCache', () => {
|
|
236
234
|
workerId: worker.threadId,
|
237
235
|
});
|
238
236
|
|
239
|
-
await new Promise((resolve) => setTimeout(resolve, 500));
|
237
|
+
await new Promise((resolve: any) => setTimeout(resolve, 500));
|
240
238
|
worker.terminate();
|
241
239
|
}
|
242
240
|
});
|
package/tsconfig.json
ADDED
package/index.d.ts
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
import type {FilePath} from '@atlaspack/types';
|
2
|
-
|
3
|
-
import type {Cache} from './lib/types';
|
4
|
-
|
5
|
-
export type {Cache} from './lib/types';
|
6
|
-
|
7
|
-
export const FSCache: {
|
8
|
-
new (cacheDir: FilePath): Cache;
|
9
|
-
};
|
10
|
-
|
11
|
-
export const LMDBLiteCache: {
|
12
|
-
new (cacheDir: FilePath): Cache;
|
13
|
-
};
|