@atlaspack/cache 2.12.1-dev.3398 → 2.12.1-dev.3443
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/lib/LMDBLiteCache.js +174 -0
- package/lib/index.js +11 -0
- package/package.json +9 -7
- package/src/LMDBLiteCache.js +196 -0
- package/src/index.js +1 -0
- package/test/LMDBLiteCache.test.js +32 -0
@@ -0,0 +1,174 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.LmdbWrapper = exports.LMDBLiteCache = void 0;
|
7
|
+
exports.open = open;
|
8
|
+
function _rust() {
|
9
|
+
const data = require("@atlaspack/rust");
|
10
|
+
_rust = function () {
|
11
|
+
return data;
|
12
|
+
};
|
13
|
+
return data;
|
14
|
+
}
|
15
|
+
function _stream() {
|
16
|
+
const data = _interopRequireDefault(require("stream"));
|
17
|
+
_stream = function () {
|
18
|
+
return data;
|
19
|
+
};
|
20
|
+
return data;
|
21
|
+
}
|
22
|
+
function _path() {
|
23
|
+
const data = _interopRequireDefault(require("path"));
|
24
|
+
_path = function () {
|
25
|
+
return data;
|
26
|
+
};
|
27
|
+
return data;
|
28
|
+
}
|
29
|
+
function _util() {
|
30
|
+
const data = require("util");
|
31
|
+
_util = function () {
|
32
|
+
return data;
|
33
|
+
};
|
34
|
+
return data;
|
35
|
+
}
|
36
|
+
function _core() {
|
37
|
+
const data = require("@atlaspack/core");
|
38
|
+
_core = function () {
|
39
|
+
return data;
|
40
|
+
};
|
41
|
+
return data;
|
42
|
+
}
|
43
|
+
function _fs() {
|
44
|
+
const data = require("@atlaspack/fs");
|
45
|
+
_fs = function () {
|
46
|
+
return data;
|
47
|
+
};
|
48
|
+
return data;
|
49
|
+
}
|
50
|
+
var _package = _interopRequireDefault(require("../package.json"));
|
51
|
+
var _FSCache = require("./FSCache");
|
52
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
53
|
+
// $FlowFixMe
|
54
|
+
class LmdbWrapper {
|
55
|
+
constructor(lmdb) {
|
56
|
+
this.lmdb = lmdb;
|
57
|
+
|
58
|
+
// $FlowFixMe
|
59
|
+
this[Symbol.dispose] = () => {
|
60
|
+
this.lmdb.close();
|
61
|
+
};
|
62
|
+
}
|
63
|
+
get(key) {
|
64
|
+
return this.lmdb.getSync(key);
|
65
|
+
}
|
66
|
+
async put(key, value) {
|
67
|
+
const buffer = typeof value === 'string' ? Buffer.from(value) : value;
|
68
|
+
await this.lmdb.put(key, buffer);
|
69
|
+
}
|
70
|
+
resetReadTxn() {}
|
71
|
+
}
|
72
|
+
exports.LmdbWrapper = LmdbWrapper;
|
73
|
+
function open(directory
|
74
|
+
// eslint-disable-next-line no-unused-vars
|
75
|
+
) {
|
76
|
+
return new LmdbWrapper(new (_rust().Lmdb)({
|
77
|
+
path: directory,
|
78
|
+
asyncWrites: true,
|
79
|
+
mapSize: 1024 * 1024 * 1024 * 15
|
80
|
+
}));
|
81
|
+
}
|
82
|
+
const pipeline = (0, _util().promisify)(_stream().default.pipeline);
|
83
|
+
class LMDBLiteCache {
|
84
|
+
// $FlowFixMe
|
85
|
+
|
86
|
+
constructor(cacheDir) {
|
87
|
+
this.fs = new (_fs().NodeFS)();
|
88
|
+
this.dir = cacheDir;
|
89
|
+
this.fsCache = new _FSCache.FSCache(this.fs, cacheDir);
|
90
|
+
this.store = open(cacheDir, {
|
91
|
+
name: 'parcel-cache',
|
92
|
+
encoding: 'binary',
|
93
|
+
compression: true
|
94
|
+
});
|
95
|
+
}
|
96
|
+
ensure() {
|
97
|
+
return Promise.resolve();
|
98
|
+
}
|
99
|
+
serialize() {
|
100
|
+
return {
|
101
|
+
dir: this.dir
|
102
|
+
};
|
103
|
+
}
|
104
|
+
static deserialize(opts) {
|
105
|
+
return new LMDBLiteCache(opts.dir);
|
106
|
+
}
|
107
|
+
has(key) {
|
108
|
+
return Promise.resolve(this.store.get(key) != null);
|
109
|
+
}
|
110
|
+
get(key) {
|
111
|
+
let data = this.store.get(key);
|
112
|
+
if (data == null) {
|
113
|
+
return Promise.resolve(null);
|
114
|
+
}
|
115
|
+
return Promise.resolve((0, _core().deserialize)(data));
|
116
|
+
}
|
117
|
+
async set(key, value) {
|
118
|
+
await this.setBlob(key, (0, _core().serialize)(value));
|
119
|
+
}
|
120
|
+
getStream(key) {
|
121
|
+
return this.fs.createReadStream(_path().default.join(this.dir, key));
|
122
|
+
}
|
123
|
+
setStream(key, stream) {
|
124
|
+
return pipeline(stream, this.fs.createWriteStream(_path().default.join(this.dir, key)));
|
125
|
+
}
|
126
|
+
getBlob(key) {
|
127
|
+
try {
|
128
|
+
return Promise.resolve(this.getBlobSync(key));
|
129
|
+
} catch (err) {
|
130
|
+
return Promise.reject(err);
|
131
|
+
}
|
132
|
+
}
|
133
|
+
getBlobSync(key) {
|
134
|
+
const buffer = this.store.get(key);
|
135
|
+
if (buffer == null) {
|
136
|
+
throw new Error(`Key ${key} not found in cache`);
|
137
|
+
}
|
138
|
+
return buffer;
|
139
|
+
}
|
140
|
+
async setBlob(key, contents) {
|
141
|
+
await this.store.put(key, contents);
|
142
|
+
}
|
143
|
+
getBuffer(key) {
|
144
|
+
return Promise.resolve(this.store.get(key));
|
145
|
+
}
|
146
|
+
#getFilePath(key, index) {
|
147
|
+
return _path().default.join(this.dir, `${key}-${index}`);
|
148
|
+
}
|
149
|
+
hasLargeBlob(key) {
|
150
|
+
return this.fs.exists(this.#getFilePath(key, 0));
|
151
|
+
}
|
152
|
+
|
153
|
+
// eslint-disable-next-line require-await
|
154
|
+
async getLargeBlob(key) {
|
155
|
+
return this.fsCache.getLargeBlob(key);
|
156
|
+
}
|
157
|
+
|
158
|
+
// eslint-disable-next-line require-await
|
159
|
+
async setLargeBlob(key, contents, options) {
|
160
|
+
return this.fsCache.setLargeBlob(key, contents, options);
|
161
|
+
}
|
162
|
+
deleteLargeBlob(key) {
|
163
|
+
return this.fsCache.deleteLargeBlob(key);
|
164
|
+
}
|
165
|
+
refresh() {
|
166
|
+
// Reset the read transaction for the store. This guarantees that
|
167
|
+
// the next read will see the latest changes to the store.
|
168
|
+
// Useful in scenarios where reads and writes are multi-threaded.
|
169
|
+
// See https://github.com/kriszyp/lmdb-js#resetreadtxn-void
|
170
|
+
this.store.resetReadTxn();
|
171
|
+
}
|
172
|
+
}
|
173
|
+
exports.LMDBLiteCache = LMDBLiteCache;
|
174
|
+
(0, _core().registerSerializableClass)(`${_package.default.version}:LMDBLiteCache`, LMDBLiteCache);
|
package/lib/index.js
CHANGED
@@ -14,6 +14,17 @@ Object.keys(_LMDBCache).forEach(function (key) {
|
|
14
14
|
}
|
15
15
|
});
|
16
16
|
});
|
17
|
+
var _LMDBLiteCache = require("./LMDBLiteCache");
|
18
|
+
Object.keys(_LMDBLiteCache).forEach(function (key) {
|
19
|
+
if (key === "default" || key === "__esModule") return;
|
20
|
+
if (key in exports && exports[key] === _LMDBLiteCache[key]) return;
|
21
|
+
Object.defineProperty(exports, key, {
|
22
|
+
enumerable: true,
|
23
|
+
get: function () {
|
24
|
+
return _LMDBLiteCache[key];
|
25
|
+
}
|
26
|
+
});
|
27
|
+
});
|
17
28
|
var _FSCache = require("./FSCache");
|
18
29
|
Object.keys(_FSCache).forEach(function (key) {
|
19
30
|
if (key === "default" || key === "__esModule") return;
|
package/package.json
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
{
|
2
2
|
"name": "@atlaspack/cache",
|
3
3
|
"description": "Interface for defining caches and file-system, IDB and LMDB implementations.",
|
4
|
-
"version": "2.12.1-dev.
|
5
|
-
"license": "MIT",
|
4
|
+
"version": "2.12.1-dev.3443+d1170cfc7",
|
5
|
+
"license": "(MIT OR Apache-2.0)",
|
6
6
|
"publishConfig": {
|
7
7
|
"access": "public"
|
8
8
|
},
|
@@ -17,17 +17,19 @@
|
|
17
17
|
"node": ">= 16.0.0"
|
18
18
|
},
|
19
19
|
"scripts": {
|
20
|
+
"test": "mocha",
|
20
21
|
"build-ts": "mkdir -p lib && flow-to-ts src/types.js > lib/types.d.ts",
|
21
22
|
"check-ts": "tsc --noEmit index.d.ts"
|
22
23
|
},
|
23
24
|
"dependencies": {
|
24
|
-
"@atlaspack/fs": "2.12.1-dev.
|
25
|
-
"@atlaspack/logger": "2.12.1-dev.
|
26
|
-
"@atlaspack/
|
25
|
+
"@atlaspack/fs": "2.12.1-dev.3443+d1170cfc7",
|
26
|
+
"@atlaspack/logger": "2.12.1-dev.3443+d1170cfc7",
|
27
|
+
"@atlaspack/rust": "2.12.1-dev.3443+d1170cfc7",
|
28
|
+
"@atlaspack/utils": "2.12.1-dev.3443+d1170cfc7",
|
27
29
|
"lmdb": "2.8.5"
|
28
30
|
},
|
29
31
|
"peerDependencies": {
|
30
|
-
"@atlaspack/core": "^2.12.1-dev.
|
32
|
+
"@atlaspack/core": "^2.12.1-dev.3443+d1170cfc7"
|
31
33
|
},
|
32
34
|
"devDependencies": {
|
33
35
|
"idb": "^5.0.8"
|
@@ -36,5 +38,5 @@
|
|
36
38
|
"./src/IDBCache.js": "./src/IDBCache.browser.js",
|
37
39
|
"./src/LMDBCache.js": false
|
38
40
|
},
|
39
|
-
"gitHead": "
|
41
|
+
"gitHead": "d1170cfc79beb290b2a066f472f68f71f7d7cb23"
|
40
42
|
}
|
@@ -0,0 +1,196 @@
|
|
1
|
+
// @flow strict-local
|
2
|
+
|
3
|
+
import {Lmdb} from '@atlaspack/rust';
|
4
|
+
import type {FilePath} from '@atlaspack/types';
|
5
|
+
import type {Cache} from './types';
|
6
|
+
import type {Readable, Writable} from 'stream';
|
7
|
+
|
8
|
+
import stream from 'stream';
|
9
|
+
import path from 'path';
|
10
|
+
import {promisify} from 'util';
|
11
|
+
import {
|
12
|
+
serialize,
|
13
|
+
deserialize,
|
14
|
+
registerSerializableClass,
|
15
|
+
} from '@atlaspack/core';
|
16
|
+
import {NodeFS} from '@atlaspack/fs';
|
17
|
+
// $FlowFixMe
|
18
|
+
import packageJson from '../package.json';
|
19
|
+
|
20
|
+
import {FSCache} from './FSCache';
|
21
|
+
|
22
|
+
interface DBOpenOptions {
|
23
|
+
name: string;
|
24
|
+
// unused
|
25
|
+
encoding: string;
|
26
|
+
// unused
|
27
|
+
compression: boolean;
|
28
|
+
}
|
29
|
+
|
30
|
+
export class LmdbWrapper {
|
31
|
+
lmdb: Lmdb;
|
32
|
+
|
33
|
+
constructor(lmdb: Lmdb) {
|
34
|
+
this.lmdb = lmdb;
|
35
|
+
|
36
|
+
// $FlowFixMe
|
37
|
+
this[Symbol.dispose] = () => {
|
38
|
+
this.lmdb.close();
|
39
|
+
};
|
40
|
+
}
|
41
|
+
|
42
|
+
get(key: string): Buffer | null {
|
43
|
+
return this.lmdb.getSync(key);
|
44
|
+
}
|
45
|
+
|
46
|
+
async put(key: string, value: Buffer | string): Promise<void> {
|
47
|
+
const buffer: Buffer =
|
48
|
+
typeof value === 'string' ? Buffer.from(value) : value;
|
49
|
+
await this.lmdb.put(key, buffer);
|
50
|
+
}
|
51
|
+
|
52
|
+
resetReadTxn() {}
|
53
|
+
}
|
54
|
+
|
55
|
+
export function open(
|
56
|
+
directory: string,
|
57
|
+
// eslint-disable-next-line no-unused-vars
|
58
|
+
openOptions: DBOpenOptions,
|
59
|
+
): LmdbWrapper {
|
60
|
+
return new LmdbWrapper(
|
61
|
+
new Lmdb({
|
62
|
+
path: directory,
|
63
|
+
asyncWrites: true,
|
64
|
+
mapSize: 1024 * 1024 * 1024 * 15,
|
65
|
+
}),
|
66
|
+
);
|
67
|
+
}
|
68
|
+
|
69
|
+
const pipeline: (Readable, Writable) => Promise<void> = promisify(
|
70
|
+
stream.pipeline,
|
71
|
+
);
|
72
|
+
|
73
|
+
export class LMDBLiteCache implements Cache {
|
74
|
+
fs: NodeFS;
|
75
|
+
dir: FilePath;
|
76
|
+
// $FlowFixMe
|
77
|
+
store: any;
|
78
|
+
fsCache: FSCache;
|
79
|
+
|
80
|
+
constructor(cacheDir: FilePath) {
|
81
|
+
this.fs = new NodeFS();
|
82
|
+
this.dir = cacheDir;
|
83
|
+
this.fsCache = new FSCache(this.fs, cacheDir);
|
84
|
+
|
85
|
+
this.store = open(cacheDir, {
|
86
|
+
name: 'parcel-cache',
|
87
|
+
encoding: 'binary',
|
88
|
+
compression: true,
|
89
|
+
});
|
90
|
+
}
|
91
|
+
|
92
|
+
ensure(): Promise<void> {
|
93
|
+
return Promise.resolve();
|
94
|
+
}
|
95
|
+
|
96
|
+
serialize(): {|dir: FilePath|} {
|
97
|
+
return {
|
98
|
+
dir: this.dir,
|
99
|
+
};
|
100
|
+
}
|
101
|
+
|
102
|
+
static deserialize(opts: {|dir: FilePath|}): LMDBLiteCache {
|
103
|
+
return new LMDBLiteCache(opts.dir);
|
104
|
+
}
|
105
|
+
|
106
|
+
has(key: string): Promise<boolean> {
|
107
|
+
return Promise.resolve(this.store.get(key) != null);
|
108
|
+
}
|
109
|
+
|
110
|
+
get<T>(key: string): Promise<?T> {
|
111
|
+
let data = this.store.get(key);
|
112
|
+
if (data == null) {
|
113
|
+
return Promise.resolve(null);
|
114
|
+
}
|
115
|
+
|
116
|
+
return Promise.resolve(deserialize(data));
|
117
|
+
}
|
118
|
+
|
119
|
+
async set(key: string, value: mixed): Promise<void> {
|
120
|
+
await this.setBlob(key, serialize(value));
|
121
|
+
}
|
122
|
+
|
123
|
+
getStream(key: string): Readable {
|
124
|
+
return this.fs.createReadStream(path.join(this.dir, key));
|
125
|
+
}
|
126
|
+
|
127
|
+
setStream(key: string, stream: Readable): Promise<void> {
|
128
|
+
return pipeline(
|
129
|
+
stream,
|
130
|
+
this.fs.createWriteStream(path.join(this.dir, key)),
|
131
|
+
);
|
132
|
+
}
|
133
|
+
|
134
|
+
getBlob(key: string): Promise<Buffer> {
|
135
|
+
try {
|
136
|
+
return Promise.resolve(this.getBlobSync(key));
|
137
|
+
} catch (err) {
|
138
|
+
return Promise.reject(err);
|
139
|
+
}
|
140
|
+
}
|
141
|
+
|
142
|
+
getBlobSync(key: string): Buffer {
|
143
|
+
const buffer = this.store.get(key);
|
144
|
+
if (buffer == null) {
|
145
|
+
throw new Error(`Key ${key} not found in cache`);
|
146
|
+
}
|
147
|
+
return buffer;
|
148
|
+
}
|
149
|
+
|
150
|
+
async setBlob(key: string, contents: Buffer | string): Promise<void> {
|
151
|
+
await this.store.put(key, contents);
|
152
|
+
}
|
153
|
+
|
154
|
+
getBuffer(key: string): Promise<?Buffer> {
|
155
|
+
return Promise.resolve(this.store.get(key));
|
156
|
+
}
|
157
|
+
|
158
|
+
#getFilePath(key: string, index: number): string {
|
159
|
+
return path.join(this.dir, `${key}-${index}`);
|
160
|
+
}
|
161
|
+
|
162
|
+
hasLargeBlob(key: string): Promise<boolean> {
|
163
|
+
return this.fs.exists(this.#getFilePath(key, 0));
|
164
|
+
}
|
165
|
+
|
166
|
+
// eslint-disable-next-line require-await
|
167
|
+
async getLargeBlob(key: string): Promise<Buffer> {
|
168
|
+
return this.fsCache.getLargeBlob(key);
|
169
|
+
}
|
170
|
+
|
171
|
+
// eslint-disable-next-line require-await
|
172
|
+
async setLargeBlob(
|
173
|
+
key: string,
|
174
|
+
contents: Buffer | string,
|
175
|
+
options?: {|signal?: AbortSignal|},
|
176
|
+
): Promise<void> {
|
177
|
+
return this.fsCache.setLargeBlob(key, contents, options);
|
178
|
+
}
|
179
|
+
|
180
|
+
deleteLargeBlob(key: string): Promise<void> {
|
181
|
+
return this.fsCache.deleteLargeBlob(key);
|
182
|
+
}
|
183
|
+
|
184
|
+
refresh(): void {
|
185
|
+
// Reset the read transaction for the store. This guarantees that
|
186
|
+
// the next read will see the latest changes to the store.
|
187
|
+
// Useful in scenarios where reads and writes are multi-threaded.
|
188
|
+
// See https://github.com/kriszyp/lmdb-js#resetreadtxn-void
|
189
|
+
this.store.resetReadTxn();
|
190
|
+
}
|
191
|
+
}
|
192
|
+
|
193
|
+
registerSerializableClass(
|
194
|
+
`${packageJson.version}:LMDBLiteCache`,
|
195
|
+
LMDBLiteCache,
|
196
|
+
);
|
package/src/index.js
CHANGED
@@ -0,0 +1,32 @@
|
|
1
|
+
import * as path from 'node:path';
|
2
|
+
import {tmpdir} from 'os';
|
3
|
+
import {LMDBLiteCache} from '../src/index';
|
4
|
+
import {deserialize, serialize} from 'node:v8';
|
5
|
+
import assert from 'node:assert';
|
6
|
+
|
7
|
+
const cacheDir = path.join(tmpdir(), 'lmdb-lite-cache-tests');
|
8
|
+
|
9
|
+
describe('LMDBLiteCache', () => {
|
10
|
+
it('can be constructed', async () => {
|
11
|
+
const cache = new LMDBLiteCache(cacheDir);
|
12
|
+
await cache.ensure();
|
13
|
+
});
|
14
|
+
|
15
|
+
it('can retrieve keys', async () => {
|
16
|
+
const cache = new LMDBLiteCache(cacheDir);
|
17
|
+
await cache.ensure();
|
18
|
+
await cache.setBlob('key', Buffer.from(serialize({value: 42})));
|
19
|
+
const buffer = await cache.getBlob('key');
|
20
|
+
const result = deserialize(buffer);
|
21
|
+
assert.equal(result.value, 42);
|
22
|
+
});
|
23
|
+
|
24
|
+
it('can retrieve keys synchronously', async () => {
|
25
|
+
const cache = new LMDBLiteCache(cacheDir);
|
26
|
+
await cache.ensure();
|
27
|
+
cache.setBlob('key', Buffer.from(serialize({value: 42})));
|
28
|
+
const buffer = cache.getBlobSync('key');
|
29
|
+
const result = deserialize(buffer);
|
30
|
+
assert.equal(result.value, 42);
|
31
|
+
});
|
32
|
+
});
|