@wxn0brp/db-storage-bin 0.0.3 → 0.0.5
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/CHANGELOG.md +15 -0
- package/dist/bin/data.d.ts +1 -1
- package/dist/bin/data.js +4 -3
- package/dist/bin/head.js +3 -3
- package/dist/bin/index.d.ts +6 -3
- package/dist/bin/index.js +3 -0
- package/package.json +1 -1
- package/src/bin/data.ts +5 -4
- package/src/bin/head.ts +3 -3
- package/src/bin/index.ts +9 -3
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
### [0.0.5](https://github.com/wxn0brp/ValtheraDB-storage-bin/compare/v0.0.4...v0.0.5) (2025-09-04)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* crc and new crc mode ([cc79715](https://github.com/wxn0brp/ValtheraDB-storage-bin/commit/cc7971591dbcd39fd5b9371644d2c3e9997f0dbc))
|
|
11
|
+
|
|
12
|
+
### [0.0.4](https://github.com/wxn0brp/ValtheraDB-storage-bin/compare/v0.0.3...v0.0.4) (2025-09-04)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### Features
|
|
16
|
+
|
|
17
|
+
* add generic, BinManager.write<T> ([a2bb851](https://github.com/wxn0brp/ValtheraDB-storage-bin/commit/a2bb851c2b3977d133b85bde917dd268b3256a41))
|
|
18
|
+
* format.encode(data, collection) ([6a20cb5](https://github.com/wxn0brp/ValtheraDB-storage-bin/commit/6a20cb57444ac6e9e1d5a61a63316f6e3184118e))
|
|
19
|
+
|
|
5
20
|
### [0.0.3](https://github.com/wxn0brp/ValtheraDB-storage-bin/compare/v0.0.2...v0.0.3) (2025-08-24)
|
|
6
21
|
|
|
7
22
|
|
package/dist/bin/data.d.ts
CHANGED
|
@@ -2,5 +2,5 @@ import { BinManager, CollectionMeta } from "./index.js";
|
|
|
2
2
|
import { FileMeta } from "./head.js";
|
|
3
3
|
export declare function findCollection(cmp: BinManager, name: string): CollectionMeta | undefined;
|
|
4
4
|
export declare function findFreeSlot(cmp: BinManager, size: number): Promise<FileMeta["freeList"][number] | undefined>;
|
|
5
|
-
export declare function writeLogic(cmp: BinManager, collection: string, data:
|
|
5
|
+
export declare function writeLogic(cmp: BinManager, collection: string, data: any): Promise<void>;
|
|
6
6
|
export declare function readLogic(cmp: BinManager, collection: string): Promise<any>;
|
package/dist/bin/data.js
CHANGED
|
@@ -23,7 +23,7 @@ export async function writeLogic(cmp, collection, data) {
|
|
|
23
23
|
const { fd, meta } = cmp;
|
|
24
24
|
await _log(3, "Writing data to collection:", collection);
|
|
25
25
|
const existingCollection = findCollection(cmp, collection);
|
|
26
|
-
const encoded = Buffer.from(await cmp.options.format.encode(data));
|
|
26
|
+
const encoded = Buffer.from(await cmp.options.format.encode(data, collection));
|
|
27
27
|
const length = encoded.length;
|
|
28
28
|
const capacity = roundUpCapacity(meta, length + 4);
|
|
29
29
|
let offset = existingCollection?.offset;
|
|
@@ -71,7 +71,8 @@ export async function writeLogic(cmp, collection, data) {
|
|
|
71
71
|
await _log(2, "Capacity exceeded");
|
|
72
72
|
}
|
|
73
73
|
else {
|
|
74
|
-
|
|
74
|
+
const crc = cmp.options.crc;
|
|
75
|
+
if (crc === 3 || crc === 4) {
|
|
75
76
|
const { computedCrc } = await getFileCrc(fd);
|
|
76
77
|
const crcBuf = Buffer.alloc(16);
|
|
77
78
|
crcBuf.writeUInt32LE(computedCrc);
|
|
@@ -85,5 +86,5 @@ export async function readLogic(cmp, collection) {
|
|
|
85
86
|
throw new Error("Collection not found");
|
|
86
87
|
const len = await readData(cmp.fd, collectionMeta.offset, 4);
|
|
87
88
|
const data = await readData(cmp.fd, collectionMeta.offset + 4, len.readUInt32LE(0));
|
|
88
|
-
return await cmp.options.format.decode(data);
|
|
89
|
+
return await cmp.options.format.decode(data, collection);
|
|
89
90
|
}
|
package/dist/bin/head.js
CHANGED
|
@@ -51,7 +51,7 @@ export async function openFile(cmp) {
|
|
|
51
51
|
}
|
|
52
52
|
if (!validCrc) {
|
|
53
53
|
await _log(0, "err", "Invalid CRC");
|
|
54
|
-
if (options.crc === 2)
|
|
54
|
+
if (options.crc === 2 || options.crc === 4)
|
|
55
55
|
throw new Error("Invalid CRC");
|
|
56
56
|
}
|
|
57
57
|
}
|
|
@@ -76,7 +76,7 @@ export async function readHeaderPayload(cmp) {
|
|
|
76
76
|
await _log(6, "err", `Incomplete payload header read: expected ${payloadLength} bytes, got ${bytesRead}`);
|
|
77
77
|
throw new Error(`Incomplete payload header read: expected ${payloadLength} bytes, got ${bytesRead}`);
|
|
78
78
|
}
|
|
79
|
-
const obj = await cmp.options.format.decode(payloadBuf);
|
|
79
|
+
const obj = await cmp.options.format.decode(payloadBuf, "");
|
|
80
80
|
meta.collections = (obj.c || []).map(([name, offset, capacity]) => ({ name, offset, capacity }));
|
|
81
81
|
meta.freeList = (obj.f || []).map(([offset, capacity]) => ({ offset, capacity }));
|
|
82
82
|
await _log(6, "Collections and freeList loaded", meta);
|
|
@@ -94,7 +94,7 @@ export async function saveHeaderAndPayload(cmp, recursion = false) {
|
|
|
94
94
|
const { collections, freeList, fileSize } = meta;
|
|
95
95
|
await _log(6, "Saving header payload:", collections, freeList);
|
|
96
96
|
const payloadObj = getHeaderPayload(meta);
|
|
97
|
-
const payloadBuf = Buffer.from(await cmp.options.format.encode(payloadObj));
|
|
97
|
+
const payloadBuf = Buffer.from(await cmp.options.format.encode(payloadObj, ""));
|
|
98
98
|
if (payloadBuf.length > 64 * 1024) {
|
|
99
99
|
console.error("Header payload too large");
|
|
100
100
|
throw new Error("Header payload too large");
|
package/dist/bin/index.d.ts
CHANGED
|
@@ -11,12 +11,14 @@ export interface Options {
|
|
|
11
11
|
* 0 - crc off
|
|
12
12
|
* 1 - warn if error
|
|
13
13
|
* 2 - throw if error
|
|
14
|
+
* 3 - 1 & save always on edit
|
|
15
|
+
* 4 - 2 & save always on edit
|
|
14
16
|
*/
|
|
15
17
|
crc: number;
|
|
16
18
|
overwriteRemovedCollection: boolean;
|
|
17
19
|
format: {
|
|
18
|
-
encode(data: any): Promise<Parameters<typeof Buffer.from>[0]>;
|
|
19
|
-
decode(data: Buffer): Promise<any>;
|
|
20
|
+
encode(data: any, collection: string): Promise<Parameters<typeof Buffer.from>[0]>;
|
|
21
|
+
decode(data: Buffer, collection: string): Promise<any>;
|
|
20
22
|
};
|
|
21
23
|
}
|
|
22
24
|
export declare class BinManager {
|
|
@@ -34,7 +36,8 @@ export declare class BinManager {
|
|
|
34
36
|
constructor(path: string, options?: Partial<Options>);
|
|
35
37
|
open(): Promise<void>;
|
|
36
38
|
close(): Promise<void>;
|
|
37
|
-
|
|
39
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
40
|
+
write<T = object[]>(collection: string, data: T): Promise<void>;
|
|
38
41
|
read(collection: string): Promise<any>;
|
|
39
42
|
optimize(): Promise<void>;
|
|
40
43
|
removeCollection(collection: string): Promise<void>;
|
package/dist/bin/index.js
CHANGED
package/package.json
CHANGED
package/src/bin/data.ts
CHANGED
|
@@ -27,12 +27,12 @@ export async function findFreeSlot(cmp: BinManager, size: number): Promise<FileM
|
|
|
27
27
|
return slot;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
export async function writeLogic(cmp: BinManager, collection: string, data:
|
|
30
|
+
export async function writeLogic(cmp: BinManager, collection: string, data: any) {
|
|
31
31
|
const { fd, meta } = cmp;
|
|
32
32
|
await _log(3, "Writing data to collection:", collection);
|
|
33
33
|
|
|
34
34
|
const existingCollection = findCollection(cmp, collection);
|
|
35
|
-
const encoded = Buffer.from(await cmp.options.format.encode(data));
|
|
35
|
+
const encoded = Buffer.from(await cmp.options.format.encode(data, collection));
|
|
36
36
|
const length = encoded.length;
|
|
37
37
|
const capacity = roundUpCapacity(meta, length + 4);
|
|
38
38
|
|
|
@@ -80,7 +80,8 @@ export async function writeLogic(cmp: BinManager, collection: string, data: obje
|
|
|
80
80
|
await saveHeaderAndPayload(cmp);
|
|
81
81
|
await _log(2, "Capacity exceeded");
|
|
82
82
|
} else {
|
|
83
|
-
|
|
83
|
+
const crc = cmp.options.crc;
|
|
84
|
+
if (crc === 3 || crc === 4) {
|
|
84
85
|
const { computedCrc } = await getFileCrc(fd);
|
|
85
86
|
const crcBuf = Buffer.alloc(16);
|
|
86
87
|
crcBuf.writeUInt32LE(computedCrc);
|
|
@@ -95,5 +96,5 @@ export async function readLogic(cmp: BinManager, collection: string) {
|
|
|
95
96
|
|
|
96
97
|
const len = await readData(cmp.fd, collectionMeta.offset, 4);
|
|
97
98
|
const data = await readData(cmp.fd, collectionMeta.offset + 4, len.readUInt32LE(0));
|
|
98
|
-
return await cmp.options.format.decode(data);
|
|
99
|
+
return await cmp.options.format.decode(data, collection);
|
|
99
100
|
}
|
package/src/bin/head.ts
CHANGED
|
@@ -74,7 +74,7 @@ export async function openFile(cmp: BinManager) {
|
|
|
74
74
|
}
|
|
75
75
|
if (!validCrc) {
|
|
76
76
|
await _log(0, "err", "Invalid CRC");
|
|
77
|
-
if (options.crc === 2)
|
|
77
|
+
if (options.crc === 2 || options.crc === 4)
|
|
78
78
|
throw new Error("Invalid CRC");
|
|
79
79
|
}
|
|
80
80
|
}
|
|
@@ -106,7 +106,7 @@ export async function readHeaderPayload(cmp: BinManager) {
|
|
|
106
106
|
throw new Error(`Incomplete payload header read: expected ${payloadLength} bytes, got ${bytesRead}`);
|
|
107
107
|
}
|
|
108
108
|
|
|
109
|
-
const obj = await cmp.options.format.decode(payloadBuf) as {
|
|
109
|
+
const obj = await cmp.options.format.decode(payloadBuf, "") as {
|
|
110
110
|
c: [string, number, number][];
|
|
111
111
|
f: [number, number][];
|
|
112
112
|
};
|
|
@@ -133,7 +133,7 @@ export async function saveHeaderAndPayload(cmp: BinManager, recursion = false) {
|
|
|
133
133
|
|
|
134
134
|
const payloadObj = getHeaderPayload(meta);
|
|
135
135
|
|
|
136
|
-
const payloadBuf = Buffer.from(await cmp.options.format.encode(payloadObj));
|
|
136
|
+
const payloadBuf = Buffer.from(await cmp.options.format.encode(payloadObj, ""));
|
|
137
137
|
if (payloadBuf.length > 64 * 1024) {
|
|
138
138
|
console.error("Header payload too large");
|
|
139
139
|
throw new Error("Header payload too large");
|
package/src/bin/index.ts
CHANGED
|
@@ -29,12 +29,14 @@ export interface Options {
|
|
|
29
29
|
* 0 - crc off
|
|
30
30
|
* 1 - warn if error
|
|
31
31
|
* 2 - throw if error
|
|
32
|
+
* 3 - 1 & save always on edit
|
|
33
|
+
* 4 - 2 & save always on edit
|
|
32
34
|
*/
|
|
33
35
|
crc: number;
|
|
34
36
|
overwriteRemovedCollection: boolean;
|
|
35
37
|
format: {
|
|
36
|
-
encode(data: any): Promise<Parameters<typeof Buffer.from>[0]>;
|
|
37
|
-
decode(data: Buffer): Promise<any>;
|
|
38
|
+
encode(data: any, collection: string): Promise<Parameters<typeof Buffer.from>[0]>;
|
|
39
|
+
decode(data: Buffer, collection: string): Promise<any>;
|
|
38
40
|
}
|
|
39
41
|
}
|
|
40
42
|
|
|
@@ -87,7 +89,11 @@ export class BinManager {
|
|
|
87
89
|
}
|
|
88
90
|
}
|
|
89
91
|
|
|
90
|
-
|
|
92
|
+
[Symbol.asyncDispose]() {
|
|
93
|
+
return this.close();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async write<T = object[]>(collection: string, data: T) {
|
|
91
97
|
if (!this.fd) throw new Error("File not open");
|
|
92
98
|
await writeLogic(this, collection, data);
|
|
93
99
|
}
|