@static-suite/static-suite-data-server 1.0.5 → 1.0.6
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/lib/store/dump/dumpIndexHelper.d.ts +1 -0
- package/dist/lib/store/dump/dumpIndexHelper.d.ts.map +1 -1
- package/dist/lib/store/dump/dumpIndexHelper.js +1 -0
- package/dist/lib/store/dump/dumpManager.d.ts.map +1 -1
- package/dist/lib/store/dump/dumpManager.js +44 -0
- package/package.json +3 -2
- package/src/lib/store/dump/dumpIndexHelper.ts +2 -0
- package/src/lib/store/dump/dumpManager.ts +59 -0
|
@@ -7,6 +7,7 @@ export declare const dumpIndexHelper: {
|
|
|
7
7
|
loadDumpIndex: () => void;
|
|
8
8
|
hasEntry: (relativeFilePath: string) => boolean;
|
|
9
9
|
getEntry: (relativeFilePath: string) => IndexEntry | undefined;
|
|
10
|
+
getKeys: () => MapIterator<string>;
|
|
10
11
|
addEntry: (relativeFilePath: string, entry: IndexEntry) => void;
|
|
11
12
|
removeEntry: (relativeFilePath: string) => void;
|
|
12
13
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dumpIndexHelper.d.ts","sourceRoot":"","sources":["../../../../src/lib/store/dump/dumpIndexHelper.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAiC1C,eAAO,MAAM,eAAe;4BACJ,OAAO;8BAYL,OAAO;yBAEZ,IAAI;2BASF,IAAI;yBAgCN,IAAI;iCAqBM,MAAM,KAAG,OAAO;iCAEhB,MAAM,KAAG,UAAU,GAAG,SAAS
|
|
1
|
+
{"version":3,"file":"dumpIndexHelper.d.ts","sourceRoot":"","sources":["../../../../src/lib/store/dump/dumpIndexHelper.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAiC1C,eAAO,MAAM,eAAe;4BACJ,OAAO;8BAYL,OAAO;yBAEZ,IAAI;2BASF,IAAI;yBAgCN,IAAI;iCAqBM,MAAM,KAAG,OAAO;iCAEhB,MAAM,KAAG,UAAU,GAAG,SAAS;;iCAK/B,MAAM,SAAS,UAAU,KAAG,IAAI;oCAK7B,MAAM,KAAG,IAAI;CAI9C,CAAC"}
|
|
@@ -100,6 +100,7 @@ exports.dumpIndexHelper = {
|
|
|
100
100
|
},
|
|
101
101
|
hasEntry: (relativeFilePath) => index.has(relativeFilePath),
|
|
102
102
|
getEntry: (relativeFilePath) => index.get(relativeFilePath),
|
|
103
|
+
getKeys: () => index.keys(),
|
|
103
104
|
addEntry: (relativeFilePath, entry) => {
|
|
104
105
|
setIsStale(true);
|
|
105
106
|
index.set(relativeFilePath, entry);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dumpManager.d.ts","sourceRoot":"","sources":["../../../../src/lib/store/dump/dumpManager.ts"],"names":[],"mappings":"AASA,OAAO,EAAQ,WAAW,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"dumpManager.d.ts","sourceRoot":"","sources":["../../../../src/lib/store/dump/dumpManager.ts"],"names":[],"mappings":"AASA,OAAO,EAAQ,WAAW,EAAE,MAAM,cAAc,CAAC;AAqKjD,eAAO,MAAM,WAAW,EAAE,WA+EzB,CAAC"}
|
|
@@ -99,6 +99,48 @@ const removeDeletedFiles = (diff, dump, filesDumpDir) => {
|
|
|
99
99
|
}
|
|
100
100
|
});
|
|
101
101
|
};
|
|
102
|
+
const removeStaleFiles = (diff, dump, filesDumpDir) => {
|
|
103
|
+
const iterator = dumpIndexHelper_1.dumpIndexHelper.getKeys();
|
|
104
|
+
for (const relativeFilepath of iterator) {
|
|
105
|
+
const absoluteFilepath = `${filesDumpDir}/${relativeFilepath}`;
|
|
106
|
+
if (!store_1.store.data.has(relativeFilepath) && fs_1.default.existsSync(absoluteFilepath)) {
|
|
107
|
+
logger_1.logger.info(`Dump: removing stale file: ${relativeFilepath}`);
|
|
108
|
+
try {
|
|
109
|
+
// Get old public URLs before they change on disk.
|
|
110
|
+
const dumpStaleFileString = fs_1.default
|
|
111
|
+
.readFileSync(absoluteFilepath)
|
|
112
|
+
.toString();
|
|
113
|
+
const isJson = (0, fs_2.isJsonFile)(relativeFilepath);
|
|
114
|
+
const dumpStaleFileContentData = isJson
|
|
115
|
+
? JSON.parse(dumpStaleFileString)
|
|
116
|
+
: dumpStaleFileString;
|
|
117
|
+
let oldPublicUrl = dumpStaleFileContentData.data?.content?.url?.path || null;
|
|
118
|
+
// An stale file can be a manually copied file.
|
|
119
|
+
// In that case, that stale file would have an oldPublicUrl from a valid file,
|
|
120
|
+
// which would lead to deleting a valid URL.
|
|
121
|
+
// To solve that case, check that oldPublicUrl is not a valid URL.
|
|
122
|
+
if (oldPublicUrl && store_1.store.index.url.has(oldPublicUrl)) {
|
|
123
|
+
logger_1.logger.info(`Dump: skipping valid url from stale file: ${oldPublicUrl}`);
|
|
124
|
+
oldPublicUrl = null;
|
|
125
|
+
}
|
|
126
|
+
// Delete it.
|
|
127
|
+
fs_1.default.unlinkSync(absoluteFilepath);
|
|
128
|
+
// Delete any empty directory.
|
|
129
|
+
(0, fs_2.removeEmptyDirsUpwards)(path_1.default.dirname(absoluteFilepath));
|
|
130
|
+
// Delete it from index.
|
|
131
|
+
dumpIndexHelper_1.dumpIndexHelper.removeEntry(relativeFilepath);
|
|
132
|
+
// Mark it as deleted.
|
|
133
|
+
dump.deleted.set(relativeFilepath, {
|
|
134
|
+
oldPublicUrl,
|
|
135
|
+
newPublicUrl: null,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
catch (e) {
|
|
139
|
+
logger_1.logger.error(`Dump: error deleting stale file "${absoluteFilepath}": ${e}`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
};
|
|
102
144
|
exports.dumpManager = {
|
|
103
145
|
dump(options = { incremental: true }) {
|
|
104
146
|
const startDate = microtime_1.default.now();
|
|
@@ -120,6 +162,8 @@ exports.dumpManager = {
|
|
|
120
162
|
storeUpdatedFiles(diff, dump, filesDumpDir);
|
|
121
163
|
// Remove deleted files.
|
|
122
164
|
removeDeletedFiles(diff, dump, filesDumpDir);
|
|
165
|
+
// Remove stale files.
|
|
166
|
+
removeStaleFiles(diff, dump, filesDumpDir);
|
|
123
167
|
// Save dump index
|
|
124
168
|
dumpIndexHelper_1.dumpIndexHelper.saveDumpIndex();
|
|
125
169
|
// Invoke "onDumpCreate" hook.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@static-suite/static-suite-data-server",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "A flexible, fast and developer friendly Data Server for Static Suite",
|
|
5
5
|
"main": "./dist/lib/index.js",
|
|
6
6
|
"typings": "./dist/lib/index.d.ts",
|
|
@@ -84,5 +84,6 @@
|
|
|
84
84
|
"src/__tests__/fixtures/*",
|
|
85
85
|
"src/__tests__/mocks/*"
|
|
86
86
|
]
|
|
87
|
-
}
|
|
87
|
+
},
|
|
88
|
+
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
|
|
88
89
|
}
|
|
@@ -120,6 +120,8 @@ export const dumpIndexHelper = {
|
|
|
120
120
|
getEntry: (relativeFilePath: string): IndexEntry | undefined =>
|
|
121
121
|
index.get(relativeFilePath),
|
|
122
122
|
|
|
123
|
+
getKeys: () => index.keys(),
|
|
124
|
+
|
|
123
125
|
addEntry: (relativeFilePath: string, entry: IndexEntry): void => {
|
|
124
126
|
setIsStale(true);
|
|
125
127
|
index.set(relativeFilePath, entry);
|
|
@@ -116,6 +116,62 @@ const removeDeletedFiles = (
|
|
|
116
116
|
});
|
|
117
117
|
};
|
|
118
118
|
|
|
119
|
+
const removeStaleFiles = (
|
|
120
|
+
diff: Diff,
|
|
121
|
+
dump: Dump,
|
|
122
|
+
filesDumpDir: string,
|
|
123
|
+
): void => {
|
|
124
|
+
const iterator = dumpIndexHelper.getKeys();
|
|
125
|
+
for (const relativeFilepath of iterator) {
|
|
126
|
+
const absoluteFilepath = `${filesDumpDir}/${relativeFilepath}`;
|
|
127
|
+
if (!store.data.has(relativeFilepath) && fs.existsSync(absoluteFilepath)) {
|
|
128
|
+
logger.info(`Dump: removing stale file: ${relativeFilepath}`);
|
|
129
|
+
try {
|
|
130
|
+
// Get old public URLs before they change on disk.
|
|
131
|
+
const dumpStaleFileString = fs
|
|
132
|
+
.readFileSync(absoluteFilepath)
|
|
133
|
+
.toString();
|
|
134
|
+
const isJson = isJsonFile(relativeFilepath);
|
|
135
|
+
const dumpStaleFileContentData = isJson
|
|
136
|
+
? JSON.parse(dumpStaleFileString)
|
|
137
|
+
: dumpStaleFileString;
|
|
138
|
+
let oldPublicUrl: string | null =
|
|
139
|
+
dumpStaleFileContentData.data?.content?.url?.path || null;
|
|
140
|
+
|
|
141
|
+
// An stale file can be a manually copied file.
|
|
142
|
+
// In that case, that stale file would have an oldPublicUrl from a valid file,
|
|
143
|
+
// which would lead to deleting a valid URL.
|
|
144
|
+
// To solve that case, check that oldPublicUrl is not a valid URL.
|
|
145
|
+
if (oldPublicUrl && store.index.url.has(oldPublicUrl)) {
|
|
146
|
+
logger.info(
|
|
147
|
+
`Dump: skipping valid url from stale file: ${oldPublicUrl}`,
|
|
148
|
+
);
|
|
149
|
+
oldPublicUrl = null;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Delete it.
|
|
153
|
+
fs.unlinkSync(absoluteFilepath);
|
|
154
|
+
|
|
155
|
+
// Delete any empty directory.
|
|
156
|
+
removeEmptyDirsUpwards(path.dirname(absoluteFilepath));
|
|
157
|
+
|
|
158
|
+
// Delete it from index.
|
|
159
|
+
dumpIndexHelper.removeEntry(relativeFilepath);
|
|
160
|
+
|
|
161
|
+
// Mark it as deleted.
|
|
162
|
+
dump.deleted.set(relativeFilepath, {
|
|
163
|
+
oldPublicUrl,
|
|
164
|
+
newPublicUrl: null,
|
|
165
|
+
});
|
|
166
|
+
} catch (e) {
|
|
167
|
+
logger.error(
|
|
168
|
+
`Dump: error deleting stale file "${absoluteFilepath}": ${e}`,
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
|
|
119
175
|
export const dumpManager: DumpManager = {
|
|
120
176
|
dump(options = { incremental: true }): Dump {
|
|
121
177
|
const startDate = microtime.now();
|
|
@@ -143,6 +199,9 @@ export const dumpManager: DumpManager = {
|
|
|
143
199
|
// Remove deleted files.
|
|
144
200
|
removeDeletedFiles(diff, dump, filesDumpDir);
|
|
145
201
|
|
|
202
|
+
// Remove stale files.
|
|
203
|
+
removeStaleFiles(diff, dump, filesDumpDir);
|
|
204
|
+
|
|
146
205
|
// Save dump index
|
|
147
206
|
dumpIndexHelper.saveDumpIndex();
|
|
148
207
|
|