|
1 | 1 | import { IntervalStore } from './types';
|
2 | 2 | import * as fs from 'fs';
|
3 | 3 | import * as path from 'path';
|
| 4 | +import { encode, decode } from '@msgpack/msgpack'; |
4 | 5 |
|
5 |
| -const FILE_VERSION = 1; |
| 6 | +const FILE_VERSION = 3; |
6 | 7 |
|
7 | 8 | export async function saveStorageToFile(
|
8 | 9 | storage: IntervalStore,
|
@@ -30,26 +31,26 @@ export async function saveStorageToFile(
|
30 | 31 |
|
31 | 32 | const filename = path.join(
|
32 | 33 | storageDir,
|
33 |
| - `${resourceSlug}-${sectionName}-storage.json` |
34 |
| - ); |
35 |
| - await fs.promises.writeFile( |
36 |
| - filename, |
37 |
| - JSON.stringify( |
38 |
| - { |
39 |
| - fileVersion: FILE_VERSION, |
40 |
| - latestResourceTimestamp, |
41 |
| - latestMarketTimestamp, |
42 |
| - store: storage, |
43 |
| - }, |
44 |
| - (key, value) => (typeof value === 'bigint' ? value.toString() : value), |
45 |
| - 2 |
46 |
| - ) |
| 34 | + `${resourceSlug}-${sectionName}-storage.msgpack` |
47 | 35 | );
|
48 | 36 |
|
| 37 | + const data = { |
| 38 | + fileVersion: FILE_VERSION, |
| 39 | + latestResourceTimestamp, |
| 40 | + latestMarketTimestamp, |
| 41 | + store: storage, |
| 42 | + }; |
| 43 | + |
| 44 | + // Encode and save |
| 45 | + const buffer = encode(data); |
| 46 | + await fs.promises.writeFile(filename, buffer); |
| 47 | + |
49 | 48 | console.timeEnd(
|
50 | 49 | ` ResourcePerformance - processResourceData.${resourceName}.${sectionName}.saveStorage`
|
51 | 50 | );
|
52 |
| - console.log(` ResourcePerformance --> Saved storage to ${filename}`); |
| 51 | + console.log( |
| 52 | + ` ResourcePerformance --> Saved storage to ${filename} (${buffer.length} bytes)` |
| 53 | + ); |
53 | 54 | }
|
54 | 55 |
|
55 | 56 | export async function loadStorageFromFile(
|
@@ -78,48 +79,36 @@ export async function loadStorageFromFile(
|
78 | 79 |
|
79 | 80 | const filename = path.join(
|
80 | 81 | storageDir,
|
81 |
| - `${resourceSlug}-${sectionName}-storage.json` |
| 82 | + `${resourceSlug}-${sectionName}-storage.msgpack` |
82 | 83 | );
|
83 |
| - if (!fs.existsSync(filename)) { |
84 |
| - console.log(`!! Storage file ${filename} does not exist`); |
85 |
| - return undefined; |
86 |
| - } |
87 | 84 |
|
88 | 85 | try {
|
89 |
| - const fileContent = await fs.promises.readFile(filename, 'utf-8'); |
90 |
| - const storage = JSON.parse(fileContent, (key, value) => { |
91 |
| - // Convert string numbers that might be bigints back to bigint |
92 |
| - if (typeof value === 'string' && /^\d+$/.test(value)) { |
93 |
| - try { |
94 |
| - return BigInt(value); |
95 |
| - } catch { |
96 |
| - return value; |
97 |
| - } |
98 |
| - } |
99 |
| - return value; |
100 |
| - }) as { |
| 86 | + const buffer = await fs.promises.readFile(filename); |
| 87 | + const data = decode(buffer) as { |
101 | 88 | fileVersion: number;
|
102 | 89 | latestResourceTimestamp: number;
|
103 | 90 | latestMarketTimestamp: number;
|
104 | 91 | store: IntervalStore;
|
105 | 92 | };
|
106 |
| - console.timeEnd( |
107 |
| - ` ResourcePerformance - processResourceData.${resourceName}.${sectionName}.loadStorage` |
108 |
| - ); |
109 |
| - console.log(` ResourcePerformance - -> Loaded storage from ${filename}`); |
110 |
| - if (storage.fileVersion !== FILE_VERSION) { |
| 93 | + |
| 94 | + if (data.fileVersion !== FILE_VERSION) { |
111 | 95 | console.log(
|
112 |
| - `!! Storage file ${filename} has an unsupported version -${storage.fileVersion}-. Expected -${FILE_VERSION}-` |
| 96 | + `!! Storage file ${filename} has an unsupported version -${data.fileVersion}-. Expected -${FILE_VERSION}-` |
113 | 97 | );
|
114 | 98 | return undefined;
|
115 | 99 | }
|
| 100 | + |
| 101 | + console.timeEnd( |
| 102 | + ` ResourcePerformance - processResourceData.${resourceName}.${sectionName}.loadStorage` |
| 103 | + ); |
| 104 | + console.log(` ResourcePerformance - -> Loaded storage from ${filename}`); |
116 | 105 | return {
|
117 |
| - latestResourceTimestamp: storage.latestResourceTimestamp, |
118 |
| - latestMarketTimestamp: storage.latestMarketTimestamp, |
119 |
| - store: storage.store, |
| 106 | + latestResourceTimestamp: data.latestResourceTimestamp, |
| 107 | + latestMarketTimestamp: data.latestMarketTimestamp, |
| 108 | + store: data.store, |
120 | 109 | };
|
121 | 110 | } catch (error) {
|
122 |
| - console.error(`!! Error loading storage from ${filename}: ${error}`); |
| 111 | + console.log(` ResourcePerformance - load storage failed: ${error}`); |
123 | 112 | console.timeEnd(
|
124 | 113 | ` ResourcePerformance - processResourceData.${resourceName}.${sectionName}.loadStorage`
|
125 | 114 | );
|
|
0 commit comments