shamela 1.0.6 → 1.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/main.js DELETED
@@ -1,629 +0,0 @@
1
- import {createClient as $SK0tW$createClient} from "@libsql/client";
2
- import {promises as $SK0tW$promises} from "node:fs";
3
- import $SK0tW$nodepath from "node:path";
4
- import $SK0tW$nodeprocess from "node:process";
5
- import {URL as $SK0tW$URL} from "node:url";
6
- import $SK0tW$pino from "pino";
7
- import $SK0tW$pinopretty from "pino-pretty";
8
- import $SK0tW$path from "path";
9
- import {promises as $SK0tW$promises1, createWriteStream as $SK0tW$createWriteStream} from "fs";
10
- import $SK0tW$https from "https";
11
- import $SK0tW$os from "os";
12
- import {pipeline as $SK0tW$pipeline} from "stream/promises";
13
- import $SK0tW$unzipper from "unzipper";
14
- import {Buffer as $SK0tW$Buffer} from "buffer";
15
- import $SK0tW$process from "process";
16
- import {URL as $SK0tW$URL1, URLSearchParams as $SK0tW$URLSearchParams} from "url";
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
- const $526b48a37e8a8ec6$var$stream = (0, $SK0tW$pinopretty)({
27
- colorize: true
28
- });
29
- const $526b48a37e8a8ec6$var$logger = (0, $SK0tW$pino)({
30
- base: {
31
- hostname: undefined,
32
- pid: undefined
33
- },
34
- level: (0, $SK0tW$nodeprocess).env.LOG_LEVEL || 'info'
35
- }, $526b48a37e8a8ec6$var$stream);
36
- var $526b48a37e8a8ec6$export$2e2bcd8739ae039 = $526b48a37e8a8ec6$var$logger;
37
-
38
-
39
- const $26460f21c4456828$export$1d57574773c8bc58 = async (db, dbName)=>{
40
- const { rows: tables } = await db.execute(`SELECT name,sql FROM ${dbName}.sqlite_master WHERE type='table'`);
41
- return tables.map((row)=>{
42
- const fields = row.sql.split(', ').map((field)=>field.split(' ')[0]);
43
- return {
44
- fields: fields,
45
- name: row.name
46
- };
47
- });
48
- };
49
- const $26460f21c4456828$export$3274d151f0598f1 = async (client, table)=>{
50
- const { rows: rows } = await client.execute(`SELECT * FROM ${table}`);
51
- return rows;
52
- };
53
-
54
-
55
- const $746504ca27eabe49$var$MAIN_DB_ALIAS = 'main';
56
- const $746504ca27eabe49$export$ee56083bb7df7ecc = (dbFile, alias)=>`ATTACH DATABASE '${dbFile}' AS ${alias}`;
57
- const $746504ca27eabe49$export$1f75c01d8a920a35 = (patchAlias, tableName, aslAlias = $746504ca27eabe49$var$MAIN_DB_ALIAS)=>`
58
- UPDATE ${aslAlias}.${tableName}
59
- SET content = ${$746504ca27eabe49$var$updatePageColumn('content', aslAlias, patchAlias)},
60
- part = ${$746504ca27eabe49$var$updatePageColumn('part', aslAlias, patchAlias)},
61
- page = ${$746504ca27eabe49$var$updatePageColumn('page', aslAlias, patchAlias)},
62
- number = ${$746504ca27eabe49$var$updatePageColumn('number', aslAlias, patchAlias)}
63
- WHERE EXISTS (
64
- SELECT 1
65
- FROM ${patchAlias}.${tableName}
66
- WHERE ${aslAlias}.${tableName}.id = ${patchAlias}.${tableName}.id
67
- );
68
- `;
69
- const $746504ca27eabe49$var$updateTitleColumn = (columnName, aslAlias, patchAlias)=>`
70
- (SELECT CASE
71
- WHEN ${patchAlias}.title.${columnName} != '#' THEN ${patchAlias}.title.${columnName}
72
- ELSE ${aslAlias}.title.${columnName}
73
- END
74
- FROM ${patchAlias}.title
75
- WHERE ${aslAlias}.title.id = ${patchAlias}.title.id)
76
- `;
77
- const $746504ca27eabe49$export$a38d1618b943c74f = (patchAlias, tableName, aslAlias = $746504ca27eabe49$var$MAIN_DB_ALIAS)=>`
78
- UPDATE ${aslAlias}.${tableName}
79
- SET content = ${$746504ca27eabe49$var$updateTitleColumn('content', aslAlias, patchAlias)},
80
- page = ${$746504ca27eabe49$var$updateTitleColumn('page', aslAlias, patchAlias)},
81
- parent = ${$746504ca27eabe49$var$updateTitleColumn('parent', aslAlias, patchAlias)}
82
- WHERE EXISTS (
83
- SELECT 1
84
- FROM ${patchAlias}.${tableName}
85
- WHERE ${aslAlias}.${tableName}.id = ${patchAlias}.${tableName}.id
86
- );
87
- `;
88
- const $746504ca27eabe49$export$33bbb3ec7652e187 = (name, fields)=>`CREATE TABLE IF NOT EXISTS ${name} (${fields.join(', ')})`;
89
- const $746504ca27eabe49$export$7fec5208c714b262 = (alias)=>`DETACH DATABASE ${alias}`;
90
- const $746504ca27eabe49$var$updatePageColumn = (columnName, aslAlias, patchAlias)=>`
91
- (SELECT CASE
92
- WHEN ${patchAlias}.page.${columnName} != '#' THEN ${patchAlias}.page.${columnName}
93
- ELSE ${aslAlias}.page.${columnName}
94
- END
95
- FROM ${patchAlias}.page
96
- WHERE ${aslAlias}.page.id = ${patchAlias}.page.id)
97
- `;
98
- const $746504ca27eabe49$export$3ef07b9580a45514 = (table, fieldToValue, isDeleted = false)=>{
99
- const combinedRecords = {
100
- ...fieldToValue,
101
- is_deleted: isDeleted ? '1' : '0'
102
- };
103
- const sortedKeys = Object.keys(combinedRecords).sort();
104
- const sortedValues = sortedKeys.map((key)=>combinedRecords[key]);
105
- return `INSERT INTO ${table} (${sortedKeys.toString()}) VALUES (${sortedValues.map((val)=>{
106
- if (val === null) return 'NULL';
107
- return typeof val === 'string' ? `'${val}'` : val;
108
- }).toString()})`;
109
- };
110
-
111
-
112
- var $e5a23c058b2763fa$export$a17a6870a08b950e = /*#__PURE__*/ function(Tables) {
113
- Tables["Authors"] = "authors";
114
- Tables["Books"] = "books";
115
- Tables["Categories"] = "categories";
116
- Tables["Page"] = "page";
117
- Tables["Title"] = "title";
118
- return Tables;
119
- }({});
120
-
121
-
122
- const $d7a7877f07ff6c69$var$PATCH_DB_ALIAS = 'patch';
123
- const $d7a7877f07ff6c69$var$ASL_DB_ALIAS = 'asl';
124
- const $d7a7877f07ff6c69$var$buildCopyStatements = (patchTables, aslTables, table, fields, patchQuery)=>{
125
- const statements = [];
126
- if (patchTables.find((t)=>t.name === table)) {
127
- statements.push(`INSERT INTO main.${table}
128
- SELECT ${fields.join(',')}
129
- FROM ${$d7a7877f07ff6c69$var$ASL_DB_ALIAS}.${table}
130
- WHERE id NOT IN (
131
- SELECT id
132
- FROM ${$d7a7877f07ff6c69$var$PATCH_DB_ALIAS}.${table}
133
- WHERE is_deleted='1'
134
- )`);
135
- statements.push(patchQuery);
136
- } else {
137
- let copyStatement = `INSERT INTO main.${table} SELECT ${fields.join(',')} FROM ${$d7a7877f07ff6c69$var$ASL_DB_ALIAS}.${table}`;
138
- if (aslTables.find((t)=>t.name === table)?.fields.includes('is_deleted')) copyStatement += ` WHERE is_deleted='0'`;
139
- statements.push(copyStatement);
140
- }
141
- return statements;
142
- };
143
- const $d7a7877f07ff6c69$export$a8b8e03e6bbe5473 = async (db, aslDB, patchDB)=>{
144
- await Promise.all([
145
- db.execute((0, $746504ca27eabe49$export$ee56083bb7df7ecc)(aslDB, $d7a7877f07ff6c69$var$ASL_DB_ALIAS)),
146
- db.execute((0, $746504ca27eabe49$export$ee56083bb7df7ecc)(patchDB, $d7a7877f07ff6c69$var$PATCH_DB_ALIAS))
147
- ]);
148
- const [patchTables, aslTables] = await Promise.all([
149
- (0, $26460f21c4456828$export$1d57574773c8bc58)(db, $d7a7877f07ff6c69$var$PATCH_DB_ALIAS),
150
- (0, $26460f21c4456828$export$1d57574773c8bc58)(db, $d7a7877f07ff6c69$var$ASL_DB_ALIAS)
151
- ]);
152
- (0, $526b48a37e8a8ec6$export$2e2bcd8739ae039).debug({
153
- aslTables: aslTables,
154
- patchTables: patchTables
155
- }, `Applying patches for...`);
156
- await db.batch([
157
- ...$d7a7877f07ff6c69$var$buildCopyStatements(patchTables, aslTables, (0, $e5a23c058b2763fa$export$a17a6870a08b950e).Page, [
158
- 'id',
159
- 'content',
160
- 'part',
161
- 'page',
162
- 'number'
163
- ], (0, $746504ca27eabe49$export$1f75c01d8a920a35)($d7a7877f07ff6c69$var$PATCH_DB_ALIAS, (0, $e5a23c058b2763fa$export$a17a6870a08b950e).Page)),
164
- ...$d7a7877f07ff6c69$var$buildCopyStatements(patchTables, aslTables, (0, $e5a23c058b2763fa$export$a17a6870a08b950e).Title, [
165
- 'id',
166
- 'content',
167
- 'page',
168
- 'parent'
169
- ], (0, $746504ca27eabe49$export$a38d1618b943c74f)($d7a7877f07ff6c69$var$PATCH_DB_ALIAS, (0, $e5a23c058b2763fa$export$a17a6870a08b950e).Title))
170
- ]);
171
- return db.batch([
172
- (0, $746504ca27eabe49$export$7fec5208c714b262)($d7a7877f07ff6c69$var$ASL_DB_ALIAS),
173
- (0, $746504ca27eabe49$export$7fec5208c714b262)($d7a7877f07ff6c69$var$PATCH_DB_ALIAS)
174
- ]);
175
- };
176
- const $d7a7877f07ff6c69$export$61101aa23c771e7c = async (db, aslDB)=>{
177
- await db.execute((0, $746504ca27eabe49$export$ee56083bb7df7ecc)(aslDB, $d7a7877f07ff6c69$var$ASL_DB_ALIAS));
178
- const tables = await (0, $26460f21c4456828$export$1d57574773c8bc58)(db, $d7a7877f07ff6c69$var$ASL_DB_ALIAS);
179
- (0, $526b48a37e8a8ec6$export$2e2bcd8739ae039).debug({
180
- tables: tables
181
- }, `copyTableData...`);
182
- await db.batch([
183
- `INSERT INTO main.${(0, $e5a23c058b2763fa$export$a17a6870a08b950e).Title} SELECT id,content,page,parent FROM ${$d7a7877f07ff6c69$var$ASL_DB_ALIAS}.${(0, $e5a23c058b2763fa$export$a17a6870a08b950e).Title}`,
184
- `INSERT INTO main.${(0, $e5a23c058b2763fa$export$a17a6870a08b950e).Page} SELECT id,content,part,page,number FROM ${$d7a7877f07ff6c69$var$ASL_DB_ALIAS}.${(0, $e5a23c058b2763fa$export$a17a6870a08b950e).Page}`
185
- ]);
186
- return db.execute((0, $746504ca27eabe49$export$7fec5208c714b262)($d7a7877f07ff6c69$var$ASL_DB_ALIAS));
187
- };
188
- const $d7a7877f07ff6c69$export$5d28a6b0dd65e4c4 = async (db)=>{
189
- return db.batch([
190
- `CREATE TABLE page (id INTEGER PRIMARY KEY, content TEXT, part INTEGER, page INTEGER, number INTEGER)`,
191
- `CREATE TABLE title (id INTEGER PRIMARY KEY, content TEXT, page INTEGER, parent INTEGER)`
192
- ]);
193
- };
194
- const $d7a7877f07ff6c69$export$3bd13330ac8e761e = async (db)=>{
195
- const rows = await (0, $26460f21c4456828$export$3274d151f0598f1)(db, (0, $e5a23c058b2763fa$export$a17a6870a08b950e).Page);
196
- const pages = rows.map((row)=>{
197
- const { content: content, id: id, number: number, page: page, part: part } = row;
198
- return {
199
- content: content,
200
- id: id,
201
- ...page && {
202
- page: page
203
- },
204
- ...number && {
205
- number: number
206
- },
207
- ...part && {
208
- part: part
209
- }
210
- };
211
- });
212
- return pages;
213
- };
214
- const $d7a7877f07ff6c69$export$8987cf231214301f = async (db)=>{
215
- const rows = await (0, $26460f21c4456828$export$3274d151f0598f1)(db, (0, $e5a23c058b2763fa$export$a17a6870a08b950e).Title);
216
- const titles = rows.map((row)=>{
217
- const r = row;
218
- return {
219
- content: r.content,
220
- id: r.id,
221
- page: r.page,
222
- ...r.parent && {
223
- number: r.parent
224
- }
225
- };
226
- });
227
- return titles;
228
- };
229
- const $d7a7877f07ff6c69$export$7a171f172be0782e = async (db)=>{
230
- const [pages, titles] = await Promise.all([
231
- $d7a7877f07ff6c69$export$3bd13330ac8e761e(db),
232
- $d7a7877f07ff6c69$export$8987cf231214301f(db)
233
- ]);
234
- return {
235
- pages: pages,
236
- titles: titles
237
- };
238
- };
239
-
240
-
241
-
242
- const $1857efaf39b32304$export$5bc725975f47e62c = 0;
243
- const $1857efaf39b32304$export$3deaf0b0365f781e = '99999';
244
-
245
-
246
-
247
-
248
-
249
- const $09a9521c7a529893$export$b3179f41dfd6e35b = async (db, sourceTables)=>{
250
- const aliasToPath = sourceTables.reduce((acc, tablePath)=>{
251
- const { name: name } = (0, $SK0tW$path).parse(tablePath);
252
- return {
253
- ...acc,
254
- [name]: tablePath
255
- };
256
- }, {});
257
- const attachStatements = Object.entries(aliasToPath).map(([alias, dbPath])=>(0, $746504ca27eabe49$export$ee56083bb7df7ecc)(dbPath, alias));
258
- await db.batch(attachStatements);
259
- const insertStatements = [
260
- `INSERT INTO ${(0, $e5a23c058b2763fa$export$a17a6870a08b950e).Authors} SELECT id,name,biography,(CASE WHEN death_number = ${(0, $1857efaf39b32304$export$3deaf0b0365f781e)} THEN NULL ELSE death_number END) AS death_number FROM author WHERE is_deleted='0'`,
261
- `INSERT INTO ${(0, $e5a23c058b2763fa$export$a17a6870a08b950e).Books} SELECT id,name,category,type,(CASE WHEN date = ${(0, $1857efaf39b32304$export$3deaf0b0365f781e)} THEN NULL ELSE date END) AS date,author,printed,major_release,minor_release,bibliography,hint,pdf_links,metadata FROM book WHERE is_deleted='0'`,
262
- `INSERT INTO ${(0, $e5a23c058b2763fa$export$a17a6870a08b950e).Categories} SELECT id,name FROM category WHERE is_deleted='0'`
263
- ];
264
- await db.batch(insertStatements);
265
- const detachStatements = Object.keys(aliasToPath).map((0, $746504ca27eabe49$export$7fec5208c714b262));
266
- await db.batch(detachStatements);
267
- };
268
- const $09a9521c7a529893$export$5d28a6b0dd65e4c4 = async (db)=>{
269
- return db.batch([
270
- `CREATE TABLE authors (id INTEGER PRIMARY KEY, name TEXT, biography TEXT, death INTEGER)`,
271
- `CREATE TABLE books (id INTEGER PRIMARY KEY, name TEXT, category INTEGER, type INTEGER, date INTEGER, author TEXT, printed INTEGER, major INTEGER, minor INTEGER, bibliography TEXT, hint TEXT, pdf_links TEXT, metadata TEXT)`,
272
- `CREATE TABLE categories (id INTEGER PRIMARY KEY, name TEXT)`
273
- ]);
274
- };
275
- const $09a9521c7a529893$export$b3b931905baa18df = async (db)=>{
276
- const rows = await (0, $26460f21c4456828$export$3274d151f0598f1)(db, (0, $e5a23c058b2763fa$export$a17a6870a08b950e).Authors);
277
- const authors = rows.map((r)=>({
278
- ...r.biography && {
279
- biography: r.biography
280
- },
281
- ...r.death && {
282
- death: r.death
283
- },
284
- id: r.id,
285
- name: r.name
286
- }));
287
- return authors;
288
- };
289
- const $09a9521c7a529893$export$7111c27bf38a004f = async (db)=>{
290
- const rows = await (0, $26460f21c4456828$export$3274d151f0598f1)(db, (0, $e5a23c058b2763fa$export$a17a6870a08b950e).Books);
291
- const books = rows.map((row)=>{
292
- const r = row;
293
- return {
294
- author: $09a9521c7a529893$var$parseAuthor(r.author),
295
- bibliography: r.bibliography,
296
- category: r.category,
297
- id: r.id,
298
- major: r.major,
299
- metadata: JSON.parse(r.metadata),
300
- name: r.name,
301
- printed: r.printed,
302
- type: r.type,
303
- ...r.date && r.date.toString() !== (0, $1857efaf39b32304$export$3deaf0b0365f781e) && {
304
- date: r.date
305
- },
306
- ...r.hint && {
307
- hint: r.hint
308
- },
309
- ...r.pdf_links && {
310
- pdfLinks: $09a9521c7a529893$var$parsePdfLinks(r.pdf_links)
311
- },
312
- ...r.minor && {
313
- minorRelease: r.minor
314
- }
315
- };
316
- });
317
- return books;
318
- };
319
- const $09a9521c7a529893$export$36bfd9279b3a24b7 = async (db)=>{
320
- const rows = await (0, $26460f21c4456828$export$3274d151f0598f1)(db, (0, $e5a23c058b2763fa$export$a17a6870a08b950e).Categories);
321
- const categories = rows.map((r)=>({
322
- id: r.id,
323
- name: r.name
324
- }));
325
- return categories;
326
- };
327
- const $09a9521c7a529893$var$parseAuthor = (value)=>{
328
- const result = value.split(',\\s+').map((id)=>parseInt(id.trim()));
329
- return result.length > 1 ? result : result[0];
330
- };
331
- const $09a9521c7a529893$var$parsePdfLinks = (value)=>{
332
- const result = JSON.parse(value);
333
- if (result.files) result.files = result.files.map((f)=>{
334
- const [file, id] = f.split('|');
335
- return {
336
- ...id && {
337
- id: id
338
- },
339
- file: file
340
- };
341
- });
342
- return result;
343
- };
344
- const $09a9521c7a529893$export$7a171f172be0782e = async (db)=>{
345
- const [authors, books, categories] = await Promise.all([
346
- $09a9521c7a529893$export$b3b931905baa18df(db),
347
- $09a9521c7a529893$export$7111c27bf38a004f(db),
348
- $09a9521c7a529893$export$36bfd9279b3a24b7(db)
349
- ]);
350
- return {
351
- authors: authors,
352
- books: books,
353
- categories: categories
354
- };
355
- };
356
-
357
-
358
-
359
-
360
-
361
-
362
-
363
-
364
-
365
- const $fd2c9bc034ddc705$export$1c500f521ad591da = async (prefix = 'shamela')=>{
366
- const tempDirBase = (0, $SK0tW$path).join((0, $SK0tW$os).tmpdir(), prefix);
367
- return (0, $SK0tW$promises1).mkdtemp(tempDirBase);
368
- };
369
- const $fd2c9bc034ddc705$export$ffc21166d570a16 = async (path)=>!!await (0, $SK0tW$promises1).stat(path).catch(()=>false);
370
- async function $fd2c9bc034ddc705$export$fb61e277af91ac0(url, outputDir) {
371
- const extractedFiles = [];
372
- const entryPromises = [];
373
- try {
374
- // Make HTTPS request and get the response stream
375
- const response = await new Promise((resolve, reject)=>{
376
- (0, $SK0tW$https).get(url, (res)=>{
377
- if (res.statusCode !== 200) reject(new Error(`Failed to download ZIP file: ${res.statusCode} ${res.statusMessage}`));
378
- else resolve(res);
379
- }).on('error', (err)=>{
380
- reject(new Error(`HTTPS request failed: ${err.message}`));
381
- });
382
- });
383
- // Create unzip stream
384
- const unzipStream = (0, $SK0tW$unzipper).Parse();
385
- // Handle entries in the ZIP file
386
- unzipStream.on('entry', (entry)=>{
387
- const entryPromise = (async ()=>{
388
- const filePath = (0, $SK0tW$path).join(outputDir, entry.path);
389
- if (entry.type === 'Directory') {
390
- // Ensure the directory exists
391
- await (0, $SK0tW$promises1).mkdir(filePath, {
392
- recursive: true
393
- });
394
- entry.autodrain();
395
- } else {
396
- // Ensure the parent directory exists
397
- const dir = (0, $SK0tW$path).dirname(filePath);
398
- await (0, $SK0tW$promises1).mkdir(dir, {
399
- recursive: true
400
- });
401
- // Pipe the entry to a file
402
- await (0, $SK0tW$pipeline)(entry, (0, $SK0tW$createWriteStream)(filePath));
403
- extractedFiles.push(filePath);
404
- }
405
- })().catch((err)=>{
406
- // Emit errors to be handled by the unzipStream error handler
407
- unzipStream.emit('error', err);
408
- });
409
- // Collect the promises
410
- entryPromises.push(entryPromise);
411
- });
412
- // Handle errors in the unzip stream
413
- unzipStream.on('error', (err)=>{
414
- throw new Error(`Error during extraction: ${err.message}`);
415
- });
416
- // Pipe the response into the unzip stream
417
- await (0, $SK0tW$pipeline)(response, unzipStream);
418
- // Wait for all entry promises to complete
419
- await Promise.all(entryPromises);
420
- return extractedFiles;
421
- } catch (error) {
422
- throw new Error(`Error processing URL: ${error.message}`);
423
- }
424
- }
425
-
426
-
427
-
428
-
429
-
430
-
431
-
432
- const $59b98b7fd5edf160$export$b5cd97dee32de81d = (endpoint, queryParams, useAuth = true)=>{
433
- const url = new (0, $SK0tW$URL1)(endpoint);
434
- {
435
- const params = new (0, $SK0tW$URLSearchParams)();
436
- Object.entries(queryParams).forEach(([key, value])=>{
437
- params.append(key, value.toString());
438
- });
439
- if (useAuth) params.append('api_key', (0, $SK0tW$process).env.SHAMELA_API_KEY);
440
- url.search = params.toString();
441
- }
442
- return url;
443
- };
444
- const $59b98b7fd5edf160$export$c9e6217566c54f42 = (url)=>{
445
- return new Promise((resolve, reject)=>{
446
- (0, $SK0tW$https).get(url, (res)=>{
447
- const contentType = res.headers['content-type'] || '';
448
- const dataChunks = [];
449
- res.on('data', (chunk)=>{
450
- dataChunks.push(chunk);
451
- });
452
- res.on('end', ()=>{
453
- const fullData = (0, $SK0tW$Buffer).concat(dataChunks);
454
- if (contentType.includes('application/json')) try {
455
- const json = JSON.parse(fullData.toString('utf-8'));
456
- resolve(json);
457
- } catch (error) {
458
- reject(new Error(`Failed to parse JSON: ${error.message}`));
459
- }
460
- else resolve(fullData);
461
- });
462
- }).on('error', (error)=>{
463
- reject(new Error(`Error making request: ${error.message}`));
464
- });
465
- });
466
- };
467
-
468
-
469
-
470
-
471
- const $6605706916cc9c29$var$SOURCE_TABLES = [
472
- 'author.sqlite',
473
- 'book.sqlite',
474
- 'category.sqlite'
475
- ];
476
- const $6605706916cc9c29$export$37467b7f8cfc50b0 = ()=>{
477
- if (!(0, $SK0tW$process).env.SHAMELA_API_MASTER_PATCH_ENDPOINT) throw new Error('SHAMELA_API_MASTER_PATCH_ENDPOINT environment variable not set');
478
- if (!(0, $SK0tW$process).env.SHAMELA_API_KEY) throw new Error('SHAMELA_API_KEY environment variable not set');
479
- };
480
- const $6605706916cc9c29$export$c7660b0cda39b7c3 = (sourceTablePaths)=>{
481
- const sourceTableNames = sourceTablePaths.map((tablePath)=>(0, $SK0tW$path).parse(tablePath).base);
482
- return $6605706916cc9c29$var$SOURCE_TABLES.every((table)=>sourceTableNames.includes(table));
483
- };
484
-
485
-
486
- const $e6ba238abdbefe7a$var$fixHttpsProtocol = (originalUrl)=>{
487
- const url = new (0, $SK0tW$URL)(originalUrl);
488
- url.protocol = 'https';
489
- return url.toString();
490
- };
491
- const $e6ba238abdbefe7a$export$4c209aa17b4b3e57 = async (id, options)=>{
492
- (0, $6605706916cc9c29$export$37467b7f8cfc50b0)();
493
- const url = (0, $59b98b7fd5edf160$export$b5cd97dee32de81d)(`${(0, $SK0tW$nodeprocess).env.SHAMELA_API_BOOKS_ENDPOINT}/${id}`, {
494
- major_release: (options?.majorVersion || 0).toString(),
495
- minor_release: (options?.minorVersion || 0).toString()
496
- });
497
- (0, $526b48a37e8a8ec6$export$2e2bcd8739ae039).info(`Fetching shamela.ws book link: ${url.toString()}`);
498
- try {
499
- const response = await (0, $59b98b7fd5edf160$export$c9e6217566c54f42)(url);
500
- return {
501
- majorRelease: response.major_release,
502
- majorReleaseUrl: $e6ba238abdbefe7a$var$fixHttpsProtocol(response.major_release_url),
503
- ...response.minor_release_url && {
504
- minorReleaseUrl: $e6ba238abdbefe7a$var$fixHttpsProtocol(response.minor_release_url)
505
- },
506
- ...response.minor_release_url && {
507
- minorRelease: response.minor_release
508
- }
509
- };
510
- } catch (error) {
511
- throw new Error(`Error fetching master patch: ${error.message}`);
512
- }
513
- };
514
- const $e6ba238abdbefe7a$export$3560c45fd9de930d = async (id, options)=>{
515
- (0, $526b48a37e8a8ec6$export$2e2bcd8739ae039).info(`downloadBook ${id} ${JSON.stringify(options)}`);
516
- const outputDir = await (0, $fd2c9bc034ddc705$export$1c500f521ad591da)('shamela_downloadBook');
517
- const bookResponse = options?.bookMetadata || await $e6ba238abdbefe7a$export$4c209aa17b4b3e57(id);
518
- const [[bookDatabase], [patchDatabase] = []] = await Promise.all([
519
- (0, $fd2c9bc034ddc705$export$fb61e277af91ac0)(bookResponse.majorReleaseUrl, outputDir),
520
- ...bookResponse.minorReleaseUrl ? [
521
- (0, $fd2c9bc034ddc705$export$fb61e277af91ac0)(bookResponse.minorReleaseUrl, outputDir)
522
- ] : []
523
- ]);
524
- const dbPath = (0, $SK0tW$nodepath).join(outputDir, 'book.db');
525
- const client = (0, $SK0tW$createClient)({
526
- url: `file:${dbPath}`
527
- });
528
- try {
529
- (0, $526b48a37e8a8ec6$export$2e2bcd8739ae039).info(`Creating tables`);
530
- await (0, $d7a7877f07ff6c69$export$5d28a6b0dd65e4c4)(client);
531
- if (patchDatabase) {
532
- (0, $526b48a37e8a8ec6$export$2e2bcd8739ae039).info(`Applying patches from ${patchDatabase} to ${bookDatabase}`);
533
- await (0, $d7a7877f07ff6c69$export$a8b8e03e6bbe5473)(client, bookDatabase, patchDatabase);
534
- } else {
535
- (0, $526b48a37e8a8ec6$export$2e2bcd8739ae039).info(`Copying table data from ${bookDatabase}`);
536
- await (0, $d7a7877f07ff6c69$export$61101aa23c771e7c)(client, bookDatabase);
537
- }
538
- const { ext: extension } = (0, $SK0tW$nodepath).parse(options.outputFile.path);
539
- if (extension === '.json') {
540
- const result = await (0, $d7a7877f07ff6c69$export$7a171f172be0782e)(client);
541
- await (0, $SK0tW$promises).writeFile(options.outputFile.path, JSON.stringify(result, undefined, 2), 'utf8');
542
- }
543
- client.close();
544
- if (extension === '.db' || extension === '.sqlite') await (0, $SK0tW$promises).rename(dbPath, options.outputFile.path);
545
- await (0, $SK0tW$promises).rm(outputDir, {
546
- recursive: true
547
- });
548
- } finally{
549
- client.close();
550
- }
551
- return options.outputFile.path;
552
- };
553
- const $e6ba238abdbefe7a$export$b96de494209cdc35 = async (version = 0)=>{
554
- (0, $6605706916cc9c29$export$37467b7f8cfc50b0)();
555
- const url = (0, $59b98b7fd5edf160$export$b5cd97dee32de81d)((0, $SK0tW$nodeprocess).env.SHAMELA_API_MASTER_PATCH_ENDPOINT, {
556
- version: version.toString()
557
- });
558
- (0, $526b48a37e8a8ec6$export$2e2bcd8739ae039).info(`Fetching shamela.ws master database patch link: ${url.toString()}`);
559
- try {
560
- const response = await (0, $59b98b7fd5edf160$export$c9e6217566c54f42)(url);
561
- return {
562
- url: response.patch_url,
563
- version: response.version
564
- };
565
- } catch (error) {
566
- throw new Error(`Error fetching master patch: ${error.message}`);
567
- }
568
- };
569
- const $e6ba238abdbefe7a$export$52a3cb82930b902c = (bookId)=>{
570
- const { host: host } = new (0, $SK0tW$URL)((0, $SK0tW$nodeprocess).env.SHAMELA_API_MASTER_PATCH_ENDPOINT);
571
- return `${host}/covers/${bookId}.jpg`;
572
- };
573
- const $e6ba238abdbefe7a$export$fd8b6353fde3f1de = async (options)=>{
574
- (0, $526b48a37e8a8ec6$export$2e2bcd8739ae039).info(`downloadMasterDatabase ${JSON.stringify(options)}`);
575
- const outputDir = await (0, $fd2c9bc034ddc705$export$1c500f521ad591da)('shamela_downloadMaster');
576
- const masterResponse = options.masterMetadata || await $e6ba238abdbefe7a$export$b96de494209cdc35((0, $1857efaf39b32304$export$5bc725975f47e62c));
577
- (0, $526b48a37e8a8ec6$export$2e2bcd8739ae039).info(`Downloading master database from: ${JSON.stringify(masterResponse)}`);
578
- const sourceTables = await (0, $fd2c9bc034ddc705$export$fb61e277af91ac0)($e6ba238abdbefe7a$var$fixHttpsProtocol(masterResponse.url), outputDir);
579
- (0, $526b48a37e8a8ec6$export$2e2bcd8739ae039).info(`sourceTables downloaded: ${sourceTables.toString()}`);
580
- if (!(0, $6605706916cc9c29$export$c7660b0cda39b7c3)(sourceTables)) {
581
- (0, $526b48a37e8a8ec6$export$2e2bcd8739ae039).error(`Some source tables were not found: ${sourceTables.toString()}`);
582
- throw new Error('Expected tables not found!');
583
- }
584
- const dbPath = (0, $SK0tW$nodepath).join(outputDir, 'master.db');
585
- const client = (0, $SK0tW$createClient)({
586
- url: `file:${dbPath}`
587
- });
588
- try {
589
- (0, $526b48a37e8a8ec6$export$2e2bcd8739ae039).info(`Creating tables`);
590
- await (0, $09a9521c7a529893$export$5d28a6b0dd65e4c4)(client);
591
- (0, $526b48a37e8a8ec6$export$2e2bcd8739ae039).info(`Copying data to master table`);
592
- await (0, $09a9521c7a529893$export$b3179f41dfd6e35b)(client, sourceTables);
593
- const { ext: extension } = (0, $SK0tW$nodepath).parse(options.outputFile.path);
594
- if (extension === '.json') {
595
- const result = await (0, $09a9521c7a529893$export$7a171f172be0782e)(client);
596
- await (0, $SK0tW$promises).writeFile(options.outputFile.path, JSON.stringify(result, undefined, 2), 'utf8');
597
- }
598
- client.close();
599
- if (extension === '.db' || extension === '.sqlite') await (0, $SK0tW$promises).rename(dbPath, options.outputFile.path);
600
- await (0, $SK0tW$promises).rm(outputDir, {
601
- recursive: true
602
- });
603
- } finally{
604
- client.close();
605
- }
606
- return options.outputFile.path;
607
- };
608
- const $e6ba238abdbefe7a$export$be7c2acc48adceee = async (id)=>{
609
- const outputDir = await (0, $fd2c9bc034ddc705$export$1c500f521ad591da)('shamela_getBookData');
610
- const outputPath = await $e6ba238abdbefe7a$export$3560c45fd9de930d(id, {
611
- outputFile: {
612
- path: (0, $SK0tW$nodepath).join(outputDir, `${id}.json`)
613
- }
614
- });
615
- const data = JSON.parse(await (0, $SK0tW$promises).readFile(outputPath, 'utf8'));
616
- await (0, $SK0tW$promises).rm(outputDir, {
617
- recursive: true
618
- });
619
- return data;
620
- };
621
-
622
-
623
- var $e9528ebec83086b6$exports = {};
624
-
625
-
626
-
627
-
628
- export {$e6ba238abdbefe7a$export$3560c45fd9de930d as downloadBook, $e6ba238abdbefe7a$export$fd8b6353fde3f1de as downloadMasterDatabase, $e6ba238abdbefe7a$export$be7c2acc48adceee as getBook, $e6ba238abdbefe7a$export$4c209aa17b4b3e57 as getBookMetadata, $e6ba238abdbefe7a$export$b96de494209cdc35 as getMasterMetadata};
629
- //# sourceMappingURL=main.js.map