@renovatebot/osv-offline-db 3.1.0 → 4.1.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/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/lib/db-index.d.ts +48 -0
- package/dist/lib/db-index.js +76 -0
- package/dist/lib/db.d.ts +9 -0
- package/dist/lib/db.js +46 -27
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { Ecosystem } from './ecosystem.ts';
|
|
2
|
+
import type { Vulnerability } from './osv.ts';
|
|
3
|
+
export interface RecordPointer {
|
|
4
|
+
readonly offset: number;
|
|
5
|
+
readonly length: number;
|
|
6
|
+
}
|
|
7
|
+
export type EcosystemIndex = Map<string, RecordPointer[]>;
|
|
8
|
+
/**
|
|
9
|
+
* A record of an ecosystem database, with the `_id` the updater adds.
|
|
10
|
+
*/
|
|
11
|
+
export type DatabaseRecord = Vulnerability & {
|
|
12
|
+
_id?: string;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* The last record of the database an index was built from, to check that the index still matches it.
|
|
16
|
+
*/
|
|
17
|
+
export interface IndexCheck extends RecordPointer {
|
|
18
|
+
readonly id: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Returns the file name of the precomputed index of an ecosystem database.
|
|
22
|
+
*/
|
|
23
|
+
export declare function getIndexFileName(ecosystem: Ecosystem): string;
|
|
24
|
+
/**
|
|
25
|
+
* Builds the index of an ecosystem database line by line, as the updater writes it and `OsvOfflineDb` reads it.
|
|
26
|
+
*/
|
|
27
|
+
export declare class DatabaseIndexBuilder {
|
|
28
|
+
readonly index: EcosystemIndex;
|
|
29
|
+
private size;
|
|
30
|
+
private last;
|
|
31
|
+
/**
|
|
32
|
+
* Adds the next line of the database, with its parsed record, or without one for a malformed line.
|
|
33
|
+
*/
|
|
34
|
+
add(line: string, record?: DatabaseRecord): void;
|
|
35
|
+
/**
|
|
36
|
+
* Returns the index file content.
|
|
37
|
+
*/
|
|
38
|
+
serialize(): string;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Parses an index file, returning `null` when it isn't one or was built from a database of another size.
|
|
42
|
+
*
|
|
43
|
+
* @returns the index, and the last record to check against the database
|
|
44
|
+
*/
|
|
45
|
+
export declare function parseIndex(content: string, size: number): {
|
|
46
|
+
index: EcosystemIndex;
|
|
47
|
+
last: IndexCheck | undefined;
|
|
48
|
+
} | null;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the file name of the precomputed index of an ecosystem database.
|
|
3
|
+
*/
|
|
4
|
+
export function getIndexFileName(ecosystem) {
|
|
5
|
+
return `${ecosystem.toLowerCase()}.idx.json`;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Builds the index of an ecosystem database line by line, as the updater writes it and `OsvOfflineDb` reads it.
|
|
9
|
+
*/
|
|
10
|
+
export class DatabaseIndexBuilder {
|
|
11
|
+
index = new Map();
|
|
12
|
+
size = 0;
|
|
13
|
+
last;
|
|
14
|
+
/**
|
|
15
|
+
* Adds the next line of the database, with its parsed record, or without one for a malformed line.
|
|
16
|
+
*/
|
|
17
|
+
add(line, record) {
|
|
18
|
+
const pointer = { offset: this.size, length: Buffer.byteLength(line) };
|
|
19
|
+
// a record can affect the same package more than once
|
|
20
|
+
const names = new Set((record?.affected ?? [])
|
|
21
|
+
.map((affected) => affected.package?.name)
|
|
22
|
+
.filter((name) => !!name));
|
|
23
|
+
for (const name of names) {
|
|
24
|
+
let pointers = this.index.get(name);
|
|
25
|
+
if (!pointers) {
|
|
26
|
+
pointers = [];
|
|
27
|
+
this.index.set(name, pointers);
|
|
28
|
+
}
|
|
29
|
+
pointers.push(pointer);
|
|
30
|
+
}
|
|
31
|
+
this.last = record?._id ? { ...pointer, id: record._id } : undefined;
|
|
32
|
+
// each line ends with a line break
|
|
33
|
+
this.size += pointer.length + 1;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Returns the index file content.
|
|
37
|
+
*/
|
|
38
|
+
serialize() {
|
|
39
|
+
const file = {
|
|
40
|
+
size: this.size,
|
|
41
|
+
...(this.last && { last: this.last }),
|
|
42
|
+
packages: Object.fromEntries(Array.from(this.index, ([name, pointers]) => [
|
|
43
|
+
name,
|
|
44
|
+
pointers.map(({ offset, length }) => [
|
|
45
|
+
offset,
|
|
46
|
+
length,
|
|
47
|
+
]),
|
|
48
|
+
])),
|
|
49
|
+
};
|
|
50
|
+
return JSON.stringify(file);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Parses an index file, returning `null` when it isn't one or was built from a database of another size.
|
|
55
|
+
*
|
|
56
|
+
* @returns the index, and the last record to check against the database
|
|
57
|
+
*/
|
|
58
|
+
export function parseIndex(content, size) {
|
|
59
|
+
let file;
|
|
60
|
+
try {
|
|
61
|
+
file = JSON.parse(content);
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
if (file?.size !== size ||
|
|
67
|
+
typeof file.packages !== 'object' ||
|
|
68
|
+
file.packages === null) {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
const index = new Map(Object.entries(file.packages).map(([name, pointers]) => [
|
|
72
|
+
name,
|
|
73
|
+
pointers.map(([offset, length]) => ({ offset, length })),
|
|
74
|
+
]));
|
|
75
|
+
return { index, last: file.last };
|
|
76
|
+
}
|
package/dist/lib/db.d.ts
CHANGED
|
@@ -10,6 +10,15 @@ export declare class OsvOfflineDb {
|
|
|
10
10
|
private constructor();
|
|
11
11
|
private _load;
|
|
12
12
|
private _init;
|
|
13
|
+
/**
|
|
14
|
+
* Returns the precomputed index of an ecosystem database, if there is one and it was built from this database.
|
|
15
|
+
*/
|
|
16
|
+
private _loadIndex;
|
|
17
|
+
/**
|
|
18
|
+
* Checks that the database holds the last record the index was built with.
|
|
19
|
+
*/
|
|
20
|
+
private _isLastRecord;
|
|
21
|
+
private _readRecord;
|
|
13
22
|
private _buildIndex;
|
|
14
23
|
static create(): OsvOfflineDb;
|
|
15
24
|
query(ecosystem: Ecosystem, packageName: string): Promise<Vulnerability[]>;
|
package/dist/lib/db.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { stat } from 'node:fs/promises';
|
|
1
|
+
import { readFile, stat } from 'node:fs/promises';
|
|
2
2
|
import { closeSync, createReadStream, read as fsRead, openSync } from 'node:fs';
|
|
3
3
|
import { promisify } from 'node:util';
|
|
4
4
|
import path from 'node:path';
|
|
5
5
|
import { tmpdir } from 'node:os';
|
|
6
6
|
import readline from 'node:readline';
|
|
7
|
+
import { DatabaseIndexBuilder, getIndexFileName, parseIndex, } from "./db-index.js";
|
|
7
8
|
import { packageToPurl } from "./purl-helper.js";
|
|
8
9
|
import debug from 'debug';
|
|
9
10
|
const logger = debug('osv-offline:db');
|
|
@@ -63,7 +64,14 @@ export class OsvOfflineDb {
|
|
|
63
64
|
index: new Map(),
|
|
64
65
|
mtime: fileStat.mtimeMs,
|
|
65
66
|
};
|
|
66
|
-
await this.
|
|
67
|
+
const index = await this._loadIndex(ecosystem, data, fileStat.size);
|
|
68
|
+
if (index) {
|
|
69
|
+
logger(`Using precomputed index for ecosystem '${ecosystem}'`);
|
|
70
|
+
data.index = index;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
await this._buildIndex(ac, data);
|
|
74
|
+
}
|
|
67
75
|
if (ac.signal.aborted) {
|
|
68
76
|
logger(`Initializing ecosystem '${ecosystem}' aborted.`);
|
|
69
77
|
delete this._db[ecosystem];
|
|
@@ -78,43 +86,58 @@ export class OsvOfflineDb {
|
|
|
78
86
|
this._pendingAcs.delete(ac);
|
|
79
87
|
}
|
|
80
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* Returns the precomputed index of an ecosystem database, if there is one and it was built from this database.
|
|
91
|
+
*/
|
|
92
|
+
async _loadIndex(ecosystem, data, size) {
|
|
93
|
+
const content = await readFile(path.join(OsvOfflineDb.rootDirectory, getIndexFileName(ecosystem)), 'utf8').catch(() => null);
|
|
94
|
+
if (content === null) {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
const parsed = parseIndex(content, size);
|
|
98
|
+
if (!parsed || !(await this._isLastRecord(data, parsed.last))) {
|
|
99
|
+
logger(`Ignoring outdated index for ecosystem '${ecosystem}'`);
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
return parsed.index;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Checks that the database holds the last record the index was built with.
|
|
106
|
+
*/
|
|
107
|
+
async _isLastRecord(data, last) {
|
|
108
|
+
if (!last) {
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
const record = await this._readRecord(data, last).catch(() => null);
|
|
112
|
+
return record?._id === last.id;
|
|
113
|
+
}
|
|
114
|
+
async _readRecord(data, { offset, length }) {
|
|
115
|
+
const buffer = Buffer.allocUnsafe(length);
|
|
116
|
+
const { bytesRead } = await readAsync(data.fd, buffer, 0, length, offset);
|
|
117
|
+
return JSON.parse(buffer.toString('utf8', 0, bytesRead));
|
|
118
|
+
}
|
|
81
119
|
async _buildIndex(ac, data) {
|
|
82
120
|
const fileStream = createReadStream(data.filePath);
|
|
83
121
|
const rl = readline.createInterface({
|
|
84
122
|
input: fileStream,
|
|
85
123
|
crlfDelay: Infinity,
|
|
86
124
|
});
|
|
87
|
-
|
|
88
|
-
const affectedPackageNames = new Set();
|
|
125
|
+
const builder = new DatabaseIndexBuilder();
|
|
89
126
|
for await (const line of rl) {
|
|
90
127
|
if (ac.signal.aborted) {
|
|
91
128
|
fileStream.destroy();
|
|
92
129
|
return;
|
|
93
130
|
}
|
|
94
|
-
|
|
131
|
+
let record;
|
|
95
132
|
try {
|
|
96
|
-
|
|
97
|
-
if (record.affected) {
|
|
98
|
-
for (const affected of record.affected) {
|
|
99
|
-
const packageName = affected.package?.name;
|
|
100
|
-
if (packageName && !affectedPackageNames.has(packageName)) {
|
|
101
|
-
affectedPackageNames.add(packageName);
|
|
102
|
-
let pointers = data.index.get(packageName);
|
|
103
|
-
if (!pointers) {
|
|
104
|
-
pointers = [];
|
|
105
|
-
data.index.set(packageName, pointers);
|
|
106
|
-
}
|
|
107
|
-
pointers.push({ offset: currentOffset, length: lineByteLength });
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
affectedPackageNames.clear();
|
|
111
|
-
}
|
|
133
|
+
record = JSON.parse(line);
|
|
112
134
|
}
|
|
113
135
|
catch {
|
|
114
136
|
// Skip malformed lines
|
|
115
137
|
}
|
|
116
|
-
|
|
138
|
+
builder.add(line, record);
|
|
117
139
|
}
|
|
140
|
+
data.index = builder.index;
|
|
118
141
|
}
|
|
119
142
|
static create() {
|
|
120
143
|
const osvOfflineDb = new OsvOfflineDb();
|
|
@@ -132,11 +155,7 @@ export class OsvOfflineDb {
|
|
|
132
155
|
if (!pointers || pointers.length === 0) {
|
|
133
156
|
return [];
|
|
134
157
|
}
|
|
135
|
-
const advisories = await Promise.all(pointers.map(
|
|
136
|
-
const buffer = Buffer.allocUnsafe(length);
|
|
137
|
-
const { bytesRead } = await readAsync(data.fd, buffer, 0, length, offset);
|
|
138
|
-
return JSON.parse(buffer.toString('utf8', 0, bytesRead));
|
|
139
|
-
}));
|
|
158
|
+
const advisories = await Promise.all(pointers.map((pointer) => this._readRecord(data, pointer)));
|
|
140
159
|
if (this.disposed)
|
|
141
160
|
return [];
|
|
142
161
|
const targetPurl = packageToPurl(ecosystem, packageName);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@renovatebot/osv-offline-db",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.1.0",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -15,15 +15,15 @@
|
|
|
15
15
|
"debug": "^4.4.3"
|
|
16
16
|
},
|
|
17
17
|
"devDependencies": {
|
|
18
|
-
"@tsconfig/
|
|
18
|
+
"@tsconfig/node24": "24.0.5",
|
|
19
19
|
"@tsconfig/strictest": "2.0.8",
|
|
20
20
|
"@types/debug": "4.1.13",
|
|
21
21
|
"@types/fs-extra": "11.0.4",
|
|
22
|
-
"@types/node": "
|
|
22
|
+
"@types/node": "24.19.2",
|
|
23
23
|
"fs-extra": "11.4.1"
|
|
24
24
|
},
|
|
25
25
|
"engines": {
|
|
26
|
-
"node": ">=
|
|
26
|
+
"node": ">=24.11.0",
|
|
27
27
|
"pnpm": ">=10.0.0"
|
|
28
28
|
}
|
|
29
29
|
}
|