@peerbit/indexer-sqlite3 1.2.28 → 1.2.30

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.
@@ -125,7 +125,7 @@ const create = async (directory?: string) => {
125
125
  sqlite3 =
126
126
  sqlite3 || (await sqlite3InitModule({ print: log, printErr: error }));
127
127
  let sqliteDb: OpfsSAHPoolDatabase | SQLDatabase | undefined = undefined;
128
- let close: (() => Promise<any> | any) | undefined = async () => {
128
+ let closeInternal = async () => {
129
129
  await Promise.all([...statements.values()].map((x) => x.finalize?.()));
130
130
  statements.clear();
131
131
 
@@ -134,12 +134,80 @@ const create = async (directory?: string) => {
134
134
  };
135
135
  let dbFileName: string;
136
136
 
137
- let drop = async () => {
138
- if (poolUtil && dbFileName != null) {
139
- poolUtil.unlink(dbFileName);
137
+ const cleanupPool = async (_label: string, preserveDbFile: boolean) => {
138
+ if (!poolUtil || dbFileName == null) {
139
+ return;
140
+ }
141
+
142
+ const relatedFiles = new Set([
143
+ dbFileName,
144
+ `${dbFileName}-journal`,
145
+ `${dbFileName}-wal`,
146
+ `${dbFileName}-shm`,
147
+ ]);
148
+
149
+ for (const fileName of relatedFiles) {
150
+ if (preserveDbFile && fileName === dbFileName) {
151
+ continue;
152
+ }
153
+ try {
154
+ poolUtil.unlink(fileName);
155
+ } catch {
156
+ // ignore unlink failures
157
+ }
140
158
  }
141
159
 
142
- return close();
160
+ const wipePool = async () => {
161
+ if (preserveDbFile || !poolUtil?.wipeFiles) {
162
+ return;
163
+ }
164
+ try {
165
+ await poolUtil.wipeFiles();
166
+ } catch {
167
+ // ignore wipe failures
168
+ }
169
+ };
170
+
171
+ const directoryPrefix = directory
172
+ ? `${directory.replace(/\/$/, "")}/`
173
+ : undefined;
174
+ if (!directoryPrefix) {
175
+ await wipePool();
176
+ return;
177
+ }
178
+ let poolFiles: string[] = [];
179
+ try {
180
+ poolFiles = poolUtil.getFileNames?.() ?? [];
181
+ } catch {
182
+ poolFiles = [];
183
+ }
184
+ for (const name of poolFiles) {
185
+ if (preserveDbFile && name === dbFileName) {
186
+ continue;
187
+ }
188
+ if (relatedFiles.has(name)) {
189
+ continue;
190
+ }
191
+ if (name.startsWith(directoryPrefix)) {
192
+ try {
193
+ poolUtil.unlink(name);
194
+ } catch {
195
+ // ignore unlink failures
196
+ }
197
+ }
198
+ }
199
+ await wipePool();
200
+ };
201
+
202
+ let close: (() => Promise<any> | any) | undefined = async () => {
203
+ await closeInternal();
204
+ const preserve = Boolean(directory);
205
+ await cleanupPool("close", preserve);
206
+ };
207
+
208
+ let drop = async () => {
209
+ await closeInternal();
210
+ await cleanupPool("drop", false);
143
211
  };
144
212
  let open = async () => {
145
213
  if (sqliteDb) {
package/src/types.ts CHANGED
@@ -7,7 +7,7 @@ export type SQLite = {
7
7
  export type Database = {
8
8
  exec: (sql: string) => Promise<any> | any;
9
9
  prepare: (sql: string, id?: string) => Promise<Statement> | Statement;
10
- close: (err?: (err: any) => any) => Promise<any> | any;
10
+ close: () => Promise<any> | any;
11
11
  drop: () => Promise<any> | any;
12
12
  open(): Promise<any> | any;
13
13
  statements: {