feat: add community publishing and news syndication
This commit is contained in:
@@ -0,0 +1,425 @@
|
||||
import { connect } from '@tursodatabase/database';
|
||||
import { spawnSync } from 'node:child_process';
|
||||
import { createHash, randomUUID } from 'node:crypto';
|
||||
import { mkdir, readFile, link, rm, writeFile } from 'node:fs/promises';
|
||||
import { basename, dirname, join, resolve } from 'node:path';
|
||||
import process from 'node:process';
|
||||
|
||||
const mode = process.argv[2];
|
||||
const subject = process.argv[3];
|
||||
const confirmation = '--confirm-article-import';
|
||||
const databasePath = resolve(
|
||||
process.env.TDR_DATABASE_PATH || './data/shd-build-development.db',
|
||||
);
|
||||
const imageDirectory = resolve(
|
||||
process.env.TDR_GAME_ICON_STORAGE_PATH || './data/game-icons',
|
||||
);
|
||||
const UUID_PATTERN =
|
||||
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
||||
const STORAGE_KEY_PATTERN = /^library-[0-9a-f-]{36}\.webp$/i;
|
||||
|
||||
const failUsage = () => {
|
||||
console.error(
|
||||
'Usage: node scripts/article-transfer.mjs export <article-id-or-slug> [output.json]',
|
||||
);
|
||||
console.error(
|
||||
` or: ARTICLE_IMPORT_ENABLED=true node scripts/article-transfer.mjs import <bundle.json> ${confirmation}`,
|
||||
);
|
||||
process.exit(2);
|
||||
};
|
||||
|
||||
if (!['export', 'import'].includes(mode) || !subject) failUsage();
|
||||
|
||||
const hash = (value) => createHash('sha256').update(value).digest('hex');
|
||||
const quoteIdentifier = (identifier) =>
|
||||
`"${String(identifier).replaceAll('"', '""')}"`;
|
||||
const isRecord = (value) =>
|
||||
Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
||||
|
||||
async function insertRow(database, table, row) {
|
||||
const columns = Object.keys(row);
|
||||
if (!columns.length) throw new Error(`Cannot insert an empty ${table} row.`);
|
||||
await (
|
||||
await database.prepare(
|
||||
`INSERT INTO ${quoteIdentifier(table)} (${columns.map(quoteIdentifier).join(',')}) VALUES (${columns.map(() => '?').join(',')})`,
|
||||
)
|
||||
).run(...columns.map((column) => row[column]));
|
||||
}
|
||||
|
||||
async function exportArticle() {
|
||||
const database = await connect(databasePath);
|
||||
const article = await (
|
||||
await database.prepare(
|
||||
`SELECT * FROM articles WHERE id=? OR slug=? ORDER BY CASE WHEN id=? THEN 0 ELSE 1 END LIMIT 1`,
|
||||
)
|
||||
).get(subject, subject, subject);
|
||||
if (!article) throw new Error('Article not found.');
|
||||
if (article.kind !== 'news')
|
||||
throw new Error('Only News articles can use this transfer command.');
|
||||
if (article.deleted_at)
|
||||
throw new Error('Deleted News articles cannot be transferred.');
|
||||
const external = await (
|
||||
await database.prepare(
|
||||
'SELECT article_id FROM external_news_sources WHERE article_id=?',
|
||||
)
|
||||
).get(article.id);
|
||||
if (external)
|
||||
throw new Error(
|
||||
'Syndicated News articles must come from their source feed.',
|
||||
);
|
||||
const embeds = await (
|
||||
await database.prepare(
|
||||
`SELECT COUNT(*) AS count FROM article_revision_build_embeds
|
||||
WHERE revision_id IN (SELECT id FROM article_revisions WHERE article_id=?)`,
|
||||
)
|
||||
).get(article.id);
|
||||
if (Number(embeds?.count ?? 0) > 0)
|
||||
throw new Error('News article transfers cannot contain Build embeds.');
|
||||
|
||||
const owner = await (
|
||||
await database.prepare('SELECT id,username FROM users WHERE id=?')
|
||||
).get(article.owner_id);
|
||||
if (!owner?.username)
|
||||
throw new Error('The article owner does not have a transferable username.');
|
||||
const draft = await (
|
||||
await database.prepare('SELECT * FROM article_drafts WHERE article_id=?')
|
||||
).get(article.id);
|
||||
const revisions = await (
|
||||
await database.prepare(
|
||||
'SELECT * FROM article_revisions WHERE article_id=? ORDER BY revision_number',
|
||||
)
|
||||
).all(article.id);
|
||||
if (!draft || !revisions.length)
|
||||
throw new Error('The article is missing its draft or revision history.');
|
||||
const revisionIds = revisions.map((revision) => String(revision.id));
|
||||
const placeholders = revisionIds.map(() => '?').join(',');
|
||||
const revisionTags = await (
|
||||
await database.prepare(
|
||||
`SELECT * FROM article_revision_tags WHERE revision_id IN (${placeholders}) ORDER BY revision_id,ordinal`,
|
||||
)
|
||||
).all(...revisionIds);
|
||||
const media = await (
|
||||
await database.prepare(
|
||||
'SELECT * FROM article_media WHERE article_id=? ORDER BY image_id',
|
||||
)
|
||||
).all(article.id);
|
||||
const search = await (
|
||||
await database.prepare('SELECT * FROM article_search WHERE article_id=?')
|
||||
).get(article.id);
|
||||
const imageIds = [...new Set(media.map((row) => String(row.image_id)))];
|
||||
const images = imageIds.length
|
||||
? await (
|
||||
await database.prepare(
|
||||
`SELECT * FROM managed_images WHERE id IN (${imageIds.map(() => '?').join(',')}) ORDER BY id`,
|
||||
)
|
||||
).all(...imageIds)
|
||||
: [];
|
||||
if (images.length !== imageIds.length)
|
||||
throw new Error('The article references missing managed-image metadata.');
|
||||
const files = [];
|
||||
for (const image of images) {
|
||||
const storageKey = String(image.storage_key);
|
||||
if (!STORAGE_KEY_PATTERN.test(storageKey))
|
||||
throw new Error('The article uses an unsupported managed-image key.');
|
||||
const bytes = await readFile(join(imageDirectory, storageKey));
|
||||
if (bytes.byteLength !== Number(image.byte_size))
|
||||
throw new Error(`Managed image ${image.id} has an invalid byte size.`);
|
||||
files.push({
|
||||
storageKey,
|
||||
byteSize: bytes.byteLength,
|
||||
sha256: hash(bytes),
|
||||
contentBase64: bytes.toString('base64'),
|
||||
});
|
||||
}
|
||||
|
||||
const bundle = {
|
||||
format: 'shd-build-article-transfer',
|
||||
version: 1,
|
||||
createdAt: new Date().toISOString(),
|
||||
owner: { sourceId: String(owner.id), username: String(owner.username) },
|
||||
article,
|
||||
draft,
|
||||
revisions,
|
||||
revisionTags,
|
||||
media,
|
||||
search: search ?? null,
|
||||
images,
|
||||
files,
|
||||
};
|
||||
const timestamp = new Date()
|
||||
.toISOString()
|
||||
.replaceAll(':', '-')
|
||||
.replace(/\.\d{3}Z$/, 'Z');
|
||||
const outputPath = resolve(
|
||||
process.argv[4] ||
|
||||
`./data/backups/article-${String(article.slug)}-${timestamp}.json`,
|
||||
);
|
||||
await mkdir(dirname(outputPath), { recursive: true });
|
||||
await writeFile(outputPath, `${JSON.stringify(bundle, null, 2)}\n`, {
|
||||
encoding: 'utf8',
|
||||
flag: 'wx',
|
||||
mode: 0o600,
|
||||
});
|
||||
console.log(
|
||||
`Exported News article "${article.slug}" with ${revisions.length} revisions and ${files.length} images.`,
|
||||
);
|
||||
console.log(`Article bundle: ${outputPath}`);
|
||||
}
|
||||
|
||||
function validateBundle(bundle) {
|
||||
if (
|
||||
!isRecord(bundle) ||
|
||||
bundle.format !== 'shd-build-article-transfer' ||
|
||||
bundle.version !== 1 ||
|
||||
!isRecord(bundle.owner) ||
|
||||
!isRecord(bundle.article) ||
|
||||
!isRecord(bundle.draft) ||
|
||||
!Array.isArray(bundle.revisions) ||
|
||||
!Array.isArray(bundle.revisionTags) ||
|
||||
!Array.isArray(bundle.media) ||
|
||||
!Array.isArray(bundle.images) ||
|
||||
!Array.isArray(bundle.files) ||
|
||||
bundle.article.kind !== 'news' ||
|
||||
!UUID_PATTERN.test(String(bundle.article.id)) ||
|
||||
!bundle.owner.username ||
|
||||
!bundle.revisions.length
|
||||
) {
|
||||
throw new Error(
|
||||
'The selected file is not a supported News article bundle.',
|
||||
);
|
||||
}
|
||||
const articleId = String(bundle.article.id);
|
||||
if (
|
||||
bundle.draft.article_id !== articleId ||
|
||||
bundle.revisions.some(
|
||||
(revision) =>
|
||||
!isRecord(revision) ||
|
||||
revision.article_id !== articleId ||
|
||||
!UUID_PATTERN.test(String(revision.id)),
|
||||
) ||
|
||||
bundle.media.some(
|
||||
(row) => !isRecord(row) || row.article_id !== articleId,
|
||||
) ||
|
||||
bundle.images.some(
|
||||
(image) =>
|
||||
!isRecord(image) ||
|
||||
!UUID_PATTERN.test(String(image.id)) ||
|
||||
!STORAGE_KEY_PATTERN.test(String(image.storage_key)) ||
|
||||
image.source_entity_type !== null ||
|
||||
image.source_entity_id !== null,
|
||||
)
|
||||
) {
|
||||
throw new Error('The News article bundle contains inconsistent records.');
|
||||
}
|
||||
const imageIds = new Set(bundle.images.map((image) => String(image.id)));
|
||||
const files = new Map();
|
||||
for (const file of bundle.files) {
|
||||
if (
|
||||
!isRecord(file) ||
|
||||
!STORAGE_KEY_PATTERN.test(String(file.storageKey)) ||
|
||||
!Number.isInteger(file.byteSize) ||
|
||||
file.byteSize <= 0 ||
|
||||
typeof file.sha256 !== 'string' ||
|
||||
!/^[0-9a-f]{64}$/.test(file.sha256) ||
|
||||
typeof file.contentBase64 !== 'string'
|
||||
) {
|
||||
throw new Error(
|
||||
'The News article bundle contains an invalid image file.',
|
||||
);
|
||||
}
|
||||
const bytes = Buffer.from(file.contentBase64, 'base64');
|
||||
if (bytes.byteLength !== file.byteSize || hash(bytes) !== file.sha256)
|
||||
throw new Error('The News article bundle contains a corrupt image file.');
|
||||
files.set(String(file.storageKey), bytes);
|
||||
}
|
||||
if (
|
||||
bundle.images.some((image) => !files.has(String(image.storage_key))) ||
|
||||
bundle.media.some((row) => !imageIds.has(String(row.image_id)))
|
||||
) {
|
||||
throw new Error('The News article bundle is missing image dependencies.');
|
||||
}
|
||||
const revisionIds = new Set(
|
||||
bundle.revisions.map((revision) => String(revision.id)),
|
||||
);
|
||||
if (
|
||||
!revisionIds.has(String(bundle.article.published_revision_id)) ||
|
||||
(bundle.article.pending_revision_id &&
|
||||
!revisionIds.has(String(bundle.article.pending_revision_id))) ||
|
||||
bundle.revisionTags.some((row) => !revisionIds.has(String(row.revision_id)))
|
||||
) {
|
||||
throw new Error('The News article bundle has invalid revision pointers.');
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
async function importArticle() {
|
||||
if (
|
||||
process.env.ARTICLE_IMPORT_ENABLED !== 'true' ||
|
||||
!process.argv.includes(confirmation)
|
||||
) {
|
||||
failUsage();
|
||||
}
|
||||
const bundlePath = resolve(subject);
|
||||
const bundle = JSON.parse(await readFile(bundlePath, 'utf8'));
|
||||
const files = validateBundle(bundle);
|
||||
|
||||
const safetyBackup = spawnSync(process.execPath, ['scripts/db-backup.mjs'], {
|
||||
cwd: process.cwd(),
|
||||
stdio: 'inherit',
|
||||
env: process.env,
|
||||
});
|
||||
if (safetyBackup.error) throw safetyBackup.error;
|
||||
if (safetyBackup.status !== 0)
|
||||
throw new Error('Article import stopped because its safety backup failed.');
|
||||
|
||||
const database = await connect(databasePath);
|
||||
const existing = await (
|
||||
await database.prepare(
|
||||
'SELECT id,slug FROM articles WHERE id=? OR (kind=? AND slug=?)',
|
||||
)
|
||||
).get(bundle.article.id, bundle.article.kind, bundle.article.slug);
|
||||
if (existing) {
|
||||
if (
|
||||
existing.id === bundle.article.id &&
|
||||
existing.slug === bundle.article.slug
|
||||
) {
|
||||
console.log(`News article "${existing.slug}" is already present.`);
|
||||
return;
|
||||
}
|
||||
throw new Error('The target already contains a conflicting News article.');
|
||||
}
|
||||
const owner = await (
|
||||
await database.prepare(
|
||||
'SELECT id,username FROM users WHERE id=? OR lower(username)=lower(?) ORDER BY CASE WHEN id=? THEN 0 ELSE 1 END LIMIT 1',
|
||||
)
|
||||
).get(bundle.owner.sourceId, bundle.owner.username, bundle.owner.sourceId);
|
||||
if (!owner)
|
||||
throw new Error(
|
||||
`The target does not contain Agent "${bundle.owner.username}".`,
|
||||
);
|
||||
const tagIds = [
|
||||
...new Set(bundle.revisionTags.map((row) => String(row.tag_id))),
|
||||
];
|
||||
if (tagIds.length) {
|
||||
const available = await (
|
||||
await database.prepare(
|
||||
`SELECT COUNT(*) AS count FROM article_tags WHERE id IN (${tagIds.map(() => '?').join(',')})`,
|
||||
)
|
||||
).get(...tagIds);
|
||||
if (Number(available?.count ?? 0) !== tagIds.length)
|
||||
throw new Error('The target is missing one or more article tags.');
|
||||
}
|
||||
const imageIds = bundle.images.map((image) => String(image.id));
|
||||
if (imageIds.length) {
|
||||
const collision = await (
|
||||
await database.prepare(
|
||||
`SELECT id FROM managed_images WHERE id IN (${imageIds.map(() => '?').join(',')}) OR storage_key IN (${imageIds.map(() => '?').join(',')}) LIMIT 1`,
|
||||
)
|
||||
).get(...imageIds, ...bundle.images.map((image) => image.storage_key));
|
||||
if (collision)
|
||||
throw new Error('The target contains a conflicting managed image.');
|
||||
}
|
||||
|
||||
await mkdir(imageDirectory, { recursive: true });
|
||||
const createdFiles = [];
|
||||
let transactionStarted = false;
|
||||
try {
|
||||
for (const image of bundle.images) {
|
||||
const storageKey = String(image.storage_key);
|
||||
const temporaryPath = join(
|
||||
imageDirectory,
|
||||
`.${storageKey}.${randomUUID()}.tmp`,
|
||||
);
|
||||
await writeFile(temporaryPath, files.get(storageKey), {
|
||||
flag: 'wx',
|
||||
mode: 0o600,
|
||||
});
|
||||
const finalPath = join(imageDirectory, storageKey);
|
||||
try {
|
||||
await link(temporaryPath, finalPath);
|
||||
createdFiles.push(finalPath);
|
||||
} finally {
|
||||
await rm(temporaryPath, { force: true });
|
||||
}
|
||||
}
|
||||
await database.exec('BEGIN IMMEDIATE');
|
||||
transactionStarted = true;
|
||||
for (const image of bundle.images) {
|
||||
await insertRow(database, 'managed_images', {
|
||||
...image,
|
||||
created_by: owner.id,
|
||||
});
|
||||
}
|
||||
await insertRow(database, 'articles', {
|
||||
...bundle.article,
|
||||
owner_id: owner.id,
|
||||
visibility: 'private',
|
||||
published_revision_id: null,
|
||||
pending_revision_id: null,
|
||||
});
|
||||
await insertRow(database, 'article_drafts', {
|
||||
...bundle.draft,
|
||||
updated_by: owner.id,
|
||||
});
|
||||
for (const revision of bundle.revisions) {
|
||||
await insertRow(database, 'article_revisions', {
|
||||
...revision,
|
||||
created_by: owner.id,
|
||||
});
|
||||
}
|
||||
for (const row of bundle.revisionTags)
|
||||
await insertRow(database, 'article_revision_tags', row);
|
||||
for (const row of bundle.media) {
|
||||
await insertRow(database, 'article_media', {
|
||||
...row,
|
||||
uploaded_by: owner.id,
|
||||
});
|
||||
}
|
||||
if (bundle.search)
|
||||
await insertRow(database, 'article_search', bundle.search);
|
||||
await (
|
||||
await database.prepare(
|
||||
`UPDATE articles SET visibility=?,published_revision_id=?,pending_revision_id=? WHERE id=?`,
|
||||
)
|
||||
).run(
|
||||
bundle.article.visibility,
|
||||
bundle.article.published_revision_id,
|
||||
bundle.article.pending_revision_id,
|
||||
bundle.article.id,
|
||||
);
|
||||
await insertRow(database, 'audit_log', {
|
||||
id: randomUUID(),
|
||||
actor_id: owner.id,
|
||||
entity_type: 'editorial_content',
|
||||
entity_id: bundle.article.id,
|
||||
action: 'news.promoted',
|
||||
before_json: null,
|
||||
after_json: JSON.stringify({
|
||||
sourceBundle: basename(bundlePath),
|
||||
slug: bundle.article.slug,
|
||||
publishedRevisionId: bundle.article.published_revision_id,
|
||||
revisions: bundle.revisions.length,
|
||||
images: bundle.images.length,
|
||||
}),
|
||||
created_at: new Date().toISOString(),
|
||||
});
|
||||
await database.exec('COMMIT');
|
||||
transactionStarted = false;
|
||||
} catch (error) {
|
||||
if (transactionStarted) {
|
||||
try {
|
||||
await database.exec('ROLLBACK');
|
||||
} catch {
|
||||
// Preserve the import error.
|
||||
}
|
||||
}
|
||||
for (const file of createdFiles) await rm(file, { force: true });
|
||||
throw error;
|
||||
}
|
||||
console.log(
|
||||
`Imported News article "${bundle.article.slug}" with ${bundle.revisions.length} revisions and ${bundle.images.length} images for ${owner.username}.`,
|
||||
);
|
||||
}
|
||||
|
||||
if (mode === 'export') await exportArticle();
|
||||
else await importArticle();
|
||||
@@ -1,40 +1,304 @@
|
||||
[
|
||||
{"name":"Weapon Critical Hit Chance","unit":"%","type":"Offensive","category":"Weapon","calculationType":"Additive","gearRange":[2.4,6]},
|
||||
{"name":"Weapon Critical Hit Damage","unit":"%","type":"Offensive","category":"Weapon","calculationType":"Additive","gearRange":[4,10],"weaponRange":[8,20]},
|
||||
{"name":"Weapon Damage","unit":"%","type":"Offensive","category":"Weapon","calculationType":"Additive","gearRange":[2,5],"weaponRange":[4,10]},
|
||||
{"name":"Weapon Multi-Shot Chance","unit":"%","type":"Offensive","category":"Weapon","calculationType":"Additive","gearRange":[2,5],"weaponRange":[4,10]},
|
||||
{"name":"Skill Critical Chance","unit":"%","type":"Offensive","category":"Skill","calculationType":"Additive","gearRange":[2.4,6]},
|
||||
{"name":"Skill Critical Damage","unit":"%","type":"Offensive","category":"Skill","calculationType":"Additive","gearRange":[4,10],"weaponRange":[8,20]},
|
||||
{"name":"Skill Intensity","unit":"%","type":"Offensive","category":"Skill","calculationType":"Additive","gearRange":[2,5],"weaponRange":[4,10]},
|
||||
{"name":"Skill Multi-Shot Chance","unit":"%","type":"Offensive","category":"Skill","calculationType":"Additive","gearRange":[2,5],"weaponRange":[4,10]},
|
||||
{"name":"Headshot Damage","unit":"%","type":"Offensive","category":"Weapon","calculationType":"Additive","gearRange":[4.8,12],"weaponRange":[9.6,24]},
|
||||
{"name":"Max Health","unit":"%","type":"Defensive","category":"Character","calculationType":"Additive","gearRange":[4,10],"weaponRange":[8,20]},
|
||||
{"name":"Armor","unit":"%","type":"Defensive","category":"Character","calculationType":"Additive","gearRange":[4.66,11.66],"weaponRange":[9.33,23.33]},
|
||||
{"name":"Damage Reduction","unit":"%","type":"Defensive","category":"Character","calculationType":"Multiplicative","gearRange":[2,5],"weaponRange":[4,10]},
|
||||
{"name":"Received Healing","unit":"%","type":"Defensive","category":"Character","calculationType":"Additive","gearRange":[4.8,12],"weaponRange":[9.6,24]},
|
||||
{"name":"Damage to Elites","unit":"%","type":"Offensive","category":"Character","calculationType":"Additive","gearRange":[2.4,6],"weaponRange":[4.8,12]},
|
||||
{"name":"Signature Ability Charge Efficiency","unit":"%","type":"Offensive","category":"Skill","calculationType":"Additive","gearRange":[4,10]},
|
||||
{"name":"Skill Duration","unit":"%","type":"Offensive","category":"Skill","calculationType":"Additive","gearRange":[3.2,8]},
|
||||
{"name":"Skill Radius","unit":"%","type":"Offensive","category":"Skill","calculationType":"Additive","gearRange":[4,10]},
|
||||
{"name":"Skill Cooldown Recovery","unit":"%","type":"Offensive","category":"Skill","calculationType":"Additive","gearRange":[-5,-2]},
|
||||
{"name":"Weapon Blast Damage","unit":"%","type":"Offensive","category":"Weapon","calculationType":"Additive","weaponRange":[4,10]},
|
||||
{"name":"Weapon Piercing Damage","unit":"%","type":"Offensive","category":"Weapon","calculationType":"Additive","weaponRange":[4,10]},
|
||||
{"name":"Weapon Shredding Damage","unit":"%","type":"Offensive","category":"Weapon","calculationType":"Additive","weaponRange":[4,10]},
|
||||
{"name":"Skill Blast Damage","unit":"%","type":"Offensive","category":"Skill","calculationType":"Additive","weaponRange":[4,10]},
|
||||
{"name":"Skill Piercing Damage","unit":"%","type":"Offensive","category":"Skill","calculationType":"Additive","weaponRange":[4,10]},
|
||||
{"name":"Skill Shredding Damage","unit":"%","type":"Offensive","category":"Skill","calculationType":"Additive","weaponRange":[4,10]},
|
||||
{"name":"Rate of Fire","unit":"%","type":"Offensive","category":"Weapon","calculationType":"Additive"},
|
||||
{"name":"Reload Speed","unit":"%","type":"Offensive","category":"Weapon","calculationType":"Additive"},
|
||||
{"name":"Health","unit":"","type":"Defensive","category":"Character","calculationType":"Additive"},
|
||||
{"name":"Armor","unit":"","type":"Defensive","category":"Character","calculationType":"Additive"},
|
||||
{"name":"Movement Speed","unit":"%","type":"Defensive","category":"Character","calculationType":"Additive"},
|
||||
{"name":"Piercing Damage","unit":"","type":"Offensive","category":"Weapon","calculationType":"Additive"},
|
||||
{"name":"RPM","unit":"Shots","type":"Offensive","category":"Weapon","calculationType":"Additive"},
|
||||
{"name":"Optimal Range","unit":"m","type":"Offensive","category":"Weapon","calculationType":"Additive"},
|
||||
{"name":"ADS Speed","unit":"s","type":"Offensive","category":"Weapon","calculationType":"Additive"},
|
||||
{"name":"Reload Speed","unit":"s","type":"Offensive","category":"Weapon","calculationType":"Additive"},
|
||||
{"name":"Magazine Size","unit":"Shots","type":"Offensive","category":"Weapon","calculationType":"Additive"},
|
||||
{"name":"Handling Speed","unit":"s","type":"Offensive","category":"Weapon","calculationType":"Additive"},
|
||||
{"name":"Accuracy","unit":"","type":"Offensive","category":"Weapon","calculationType":"Additive"},
|
||||
{"name":"Vertical Stability","unit":"","type":"Offensive","category":"Weapon","calculationType":"Additive"}
|
||||
{
|
||||
"name": "Weapon Critical Hit Chance",
|
||||
"unit": "%",
|
||||
"type": "Offensive",
|
||||
"category": "Weapon",
|
||||
"calculationType": "Additive",
|
||||
"gearRange": [2.4, 6]
|
||||
},
|
||||
{
|
||||
"name": "Weapon Critical Hit Damage",
|
||||
"unit": "%",
|
||||
"type": "Offensive",
|
||||
"category": "Weapon",
|
||||
"calculationType": "Additive",
|
||||
"gearRange": [4, 10],
|
||||
"weaponRange": [8, 20]
|
||||
},
|
||||
{
|
||||
"name": "Weapon Damage",
|
||||
"unit": "%",
|
||||
"type": "Offensive",
|
||||
"category": "Weapon",
|
||||
"calculationType": "Additive",
|
||||
"gearRange": [2, 5],
|
||||
"weaponRange": [4, 10]
|
||||
},
|
||||
{
|
||||
"name": "Weapon Multi-Shot Chance",
|
||||
"unit": "%",
|
||||
"type": "Offensive",
|
||||
"category": "Weapon",
|
||||
"calculationType": "Additive",
|
||||
"gearRange": [2, 5],
|
||||
"weaponRange": [4, 10]
|
||||
},
|
||||
{
|
||||
"name": "Skill Critical Chance",
|
||||
"unit": "%",
|
||||
"type": "Offensive",
|
||||
"category": "Skill",
|
||||
"calculationType": "Additive",
|
||||
"gearRange": [2.4, 6]
|
||||
},
|
||||
{
|
||||
"name": "Skill Critical Damage",
|
||||
"unit": "%",
|
||||
"type": "Offensive",
|
||||
"category": "Skill",
|
||||
"calculationType": "Additive",
|
||||
"gearRange": [4, 10],
|
||||
"weaponRange": [8, 20]
|
||||
},
|
||||
{
|
||||
"name": "Skill Intensity",
|
||||
"unit": "%",
|
||||
"type": "Offensive",
|
||||
"category": "Skill",
|
||||
"calculationType": "Additive",
|
||||
"gearRange": [2, 5],
|
||||
"weaponRange": [4, 10]
|
||||
},
|
||||
{
|
||||
"name": "Skill Multi-Shot Chance",
|
||||
"unit": "%",
|
||||
"type": "Offensive",
|
||||
"category": "Skill",
|
||||
"calculationType": "Additive",
|
||||
"gearRange": [2, 5],
|
||||
"weaponRange": [4, 10]
|
||||
},
|
||||
{
|
||||
"name": "Headshot Damage",
|
||||
"unit": "%",
|
||||
"type": "Offensive",
|
||||
"category": "Weapon",
|
||||
"calculationType": "Additive",
|
||||
"gearRange": [4.8, 12],
|
||||
"weaponRange": [9.6, 24]
|
||||
},
|
||||
{
|
||||
"name": "Max Health",
|
||||
"unit": "%",
|
||||
"type": "Defensive",
|
||||
"category": "Character",
|
||||
"calculationType": "Additive",
|
||||
"gearRange": [4, 10],
|
||||
"weaponRange": [8, 20]
|
||||
},
|
||||
{
|
||||
"name": "Armor",
|
||||
"unit": "%",
|
||||
"type": "Defensive",
|
||||
"category": "Character",
|
||||
"calculationType": "Additive",
|
||||
"gearRange": [4.66, 11.66],
|
||||
"weaponRange": [9.33, 23.33]
|
||||
},
|
||||
{
|
||||
"name": "Damage Reduction",
|
||||
"unit": "%",
|
||||
"type": "Defensive",
|
||||
"category": "Character",
|
||||
"calculationType": "Multiplicative",
|
||||
"gearRange": [2, 5],
|
||||
"weaponRange": [4, 10]
|
||||
},
|
||||
{
|
||||
"name": "Received Healing",
|
||||
"unit": "%",
|
||||
"type": "Defensive",
|
||||
"category": "Character",
|
||||
"calculationType": "Additive",
|
||||
"gearRange": [4.8, 12],
|
||||
"weaponRange": [9.6, 24]
|
||||
},
|
||||
{
|
||||
"name": "Damage to Elites",
|
||||
"unit": "%",
|
||||
"type": "Offensive",
|
||||
"category": "Character",
|
||||
"calculationType": "Additive",
|
||||
"gearRange": [2.4, 6],
|
||||
"weaponRange": [4.8, 12]
|
||||
},
|
||||
{
|
||||
"name": "Signature Ability Charge Efficiency",
|
||||
"unit": "%",
|
||||
"type": "Offensive",
|
||||
"category": "Skill",
|
||||
"calculationType": "Additive",
|
||||
"gearRange": [4, 10]
|
||||
},
|
||||
{
|
||||
"name": "Skill Duration",
|
||||
"unit": "%",
|
||||
"type": "Offensive",
|
||||
"category": "Skill",
|
||||
"calculationType": "Additive",
|
||||
"gearRange": [3.2, 8]
|
||||
},
|
||||
{
|
||||
"name": "Skill Radius",
|
||||
"unit": "%",
|
||||
"type": "Offensive",
|
||||
"category": "Skill",
|
||||
"calculationType": "Additive",
|
||||
"gearRange": [4, 10]
|
||||
},
|
||||
{
|
||||
"name": "Skill Cooldown Recovery",
|
||||
"unit": "%",
|
||||
"type": "Offensive",
|
||||
"category": "Skill",
|
||||
"calculationType": "Additive",
|
||||
"gearRange": [-5, -2]
|
||||
},
|
||||
{
|
||||
"name": "Weapon Blast Damage",
|
||||
"unit": "%",
|
||||
"type": "Offensive",
|
||||
"category": "Weapon",
|
||||
"calculationType": "Additive",
|
||||
"weaponRange": [4, 10]
|
||||
},
|
||||
{
|
||||
"name": "Weapon Piercing Damage",
|
||||
"unit": "%",
|
||||
"type": "Offensive",
|
||||
"category": "Weapon",
|
||||
"calculationType": "Additive",
|
||||
"weaponRange": [4, 10]
|
||||
},
|
||||
{
|
||||
"name": "Weapon Shredding Damage",
|
||||
"unit": "%",
|
||||
"type": "Offensive",
|
||||
"category": "Weapon",
|
||||
"calculationType": "Additive",
|
||||
"weaponRange": [4, 10]
|
||||
},
|
||||
{
|
||||
"name": "Skill Blast Damage",
|
||||
"unit": "%",
|
||||
"type": "Offensive",
|
||||
"category": "Skill",
|
||||
"calculationType": "Additive",
|
||||
"weaponRange": [4, 10]
|
||||
},
|
||||
{
|
||||
"name": "Skill Piercing Damage",
|
||||
"unit": "%",
|
||||
"type": "Offensive",
|
||||
"category": "Skill",
|
||||
"calculationType": "Additive",
|
||||
"weaponRange": [4, 10]
|
||||
},
|
||||
{
|
||||
"name": "Skill Shredding Damage",
|
||||
"unit": "%",
|
||||
"type": "Offensive",
|
||||
"category": "Skill",
|
||||
"calculationType": "Additive",
|
||||
"weaponRange": [4, 10]
|
||||
},
|
||||
{
|
||||
"name": "Rate of Fire",
|
||||
"unit": "%",
|
||||
"type": "Offensive",
|
||||
"category": "Weapon",
|
||||
"calculationType": "Additive"
|
||||
},
|
||||
{
|
||||
"name": "Reload Speed",
|
||||
"unit": "%",
|
||||
"type": "Offensive",
|
||||
"category": "Weapon",
|
||||
"calculationType": "Additive"
|
||||
},
|
||||
{
|
||||
"name": "Health",
|
||||
"unit": "",
|
||||
"type": "Defensive",
|
||||
"category": "Character",
|
||||
"calculationType": "Additive"
|
||||
},
|
||||
{
|
||||
"name": "Armor",
|
||||
"unit": "",
|
||||
"type": "Defensive",
|
||||
"category": "Character",
|
||||
"calculationType": "Additive"
|
||||
},
|
||||
{
|
||||
"name": "Movement Speed",
|
||||
"unit": "%",
|
||||
"type": "Defensive",
|
||||
"category": "Character",
|
||||
"calculationType": "Additive"
|
||||
},
|
||||
{
|
||||
"name": "Piercing Damage",
|
||||
"unit": "",
|
||||
"type": "Offensive",
|
||||
"category": "Weapon",
|
||||
"calculationType": "Additive"
|
||||
},
|
||||
{
|
||||
"name": "RPM",
|
||||
"unit": "Shots",
|
||||
"type": "Offensive",
|
||||
"category": "Weapon",
|
||||
"calculationType": "Additive"
|
||||
},
|
||||
{
|
||||
"name": "Optimal Range",
|
||||
"unit": "m",
|
||||
"type": "Offensive",
|
||||
"category": "Weapon",
|
||||
"calculationType": "Additive"
|
||||
},
|
||||
{
|
||||
"name": "ADS Speed",
|
||||
"unit": "s",
|
||||
"type": "Offensive",
|
||||
"category": "Weapon",
|
||||
"calculationType": "Additive"
|
||||
},
|
||||
{
|
||||
"name": "Reload Speed",
|
||||
"unit": "s",
|
||||
"type": "Offensive",
|
||||
"category": "Weapon",
|
||||
"calculationType": "Additive"
|
||||
},
|
||||
{
|
||||
"name": "Magazine Size",
|
||||
"unit": "Shots",
|
||||
"type": "Offensive",
|
||||
"category": "Weapon",
|
||||
"calculationType": "Additive"
|
||||
},
|
||||
{
|
||||
"name": "Handling Speed",
|
||||
"unit": "s",
|
||||
"type": "Offensive",
|
||||
"category": "Weapon",
|
||||
"calculationType": "Additive"
|
||||
},
|
||||
{
|
||||
"name": "Accuracy",
|
||||
"unit": "",
|
||||
"type": "Offensive",
|
||||
"category": "Weapon",
|
||||
"calculationType": "Additive"
|
||||
},
|
||||
{
|
||||
"name": "Vertical Stability",
|
||||
"unit": "",
|
||||
"type": "Offensive",
|
||||
"category": "Weapon",
|
||||
"calculationType": "Additive"
|
||||
}
|
||||
]
|
||||
|
||||
+25
-17
@@ -3,19 +3,25 @@ import { mkdir, writeFile } from 'node:fs/promises';
|
||||
import { basename, dirname, resolve } from 'node:path';
|
||||
import process from 'node:process';
|
||||
|
||||
const databasePath = resolve(process.env.TDR_DATABASE_PATH || './data/shd-build-development.db');
|
||||
const databasePath = resolve(
|
||||
process.env.TDR_DATABASE_PATH || './data/shd-build-development.db',
|
||||
);
|
||||
const requestedOutput = process.argv[2];
|
||||
const timestamp = new Date().toISOString().replaceAll(':', '-').replace(/\.\d{3}Z$/, 'Z');
|
||||
const timestamp = new Date()
|
||||
.toISOString()
|
||||
.replaceAll(':', '-')
|
||||
.replace(/\.\d{3}Z$/, 'Z');
|
||||
const databaseName = basename(databasePath).replace(/\.[^.]+$/, '');
|
||||
const outputPath = resolve(
|
||||
requestedOutput || `./data/backups/${databaseName}-${timestamp}.json`
|
||||
requestedOutput || `./data/backups/${databaseName}-${timestamp}.json`,
|
||||
);
|
||||
const quoteIdentifier = (identifier) => `"${String(identifier).replaceAll('"', '""')}"`;
|
||||
const quoteIdentifier = (identifier) =>
|
||||
`"${String(identifier).replaceAll('"', '""')}"`;
|
||||
|
||||
await mkdir(dirname(outputPath), { recursive: true });
|
||||
const database = await connect(databasePath);
|
||||
const tableRows = await (
|
||||
await database.prepare(`
|
||||
await database.prepare(`
|
||||
SELECT name
|
||||
FROM sqlite_master
|
||||
WHERE type = 'table'
|
||||
@@ -27,23 +33,25 @@ const tableRows = await (
|
||||
const tables = [];
|
||||
let totalRows = 0;
|
||||
for (const tableRow of tableRows) {
|
||||
const name = String(tableRow.name);
|
||||
const rows = await (await database.prepare(`SELECT * FROM ${quoteIdentifier(name)}`)).all();
|
||||
tables.push({ name, rowCount: rows.length, rows });
|
||||
totalRows += rows.length;
|
||||
const name = String(tableRow.name);
|
||||
const rows = await (
|
||||
await database.prepare(`SELECT * FROM ${quoteIdentifier(name)}`)
|
||||
).all();
|
||||
tables.push({ name, rowCount: rows.length, rows });
|
||||
totalRows += rows.length;
|
||||
}
|
||||
|
||||
const backup = {
|
||||
format: 'shd-build-database-backup',
|
||||
version: 1,
|
||||
createdAt: new Date().toISOString(),
|
||||
sourceDatabase: basename(databasePath),
|
||||
totalRows,
|
||||
tables
|
||||
format: 'shd-build-database-backup',
|
||||
version: 1,
|
||||
createdAt: new Date().toISOString(),
|
||||
sourceDatabase: basename(databasePath),
|
||||
totalRows,
|
||||
tables,
|
||||
};
|
||||
await writeFile(outputPath, `${JSON.stringify(backup, null, 2)}\n`, {
|
||||
encoding: 'utf8',
|
||||
flag: 'wx'
|
||||
encoding: 'utf8',
|
||||
flag: 'wx',
|
||||
});
|
||||
|
||||
console.log(`Backed up ${totalRows} rows from ${tables.length} tables.`);
|
||||
|
||||
+2129
-1187
File diff suppressed because it is too large
Load Diff
+41
-30
@@ -5,53 +5,64 @@ import process from 'node:process';
|
||||
|
||||
const backupArgument = process.argv[2];
|
||||
if (!backupArgument) {
|
||||
console.error('Usage: npm run db:restore -- <backup-file>');
|
||||
process.exit(2);
|
||||
console.error('Usage: npm run db:restore -- <backup-file>');
|
||||
process.exit(2);
|
||||
}
|
||||
|
||||
const backupPath = resolve(backupArgument);
|
||||
const databasePath = resolve(process.env.TDR_DATABASE_PATH || './data/shd-build-development.db');
|
||||
const databasePath = resolve(
|
||||
process.env.TDR_DATABASE_PATH || './data/shd-build-development.db',
|
||||
);
|
||||
const backup = JSON.parse(await readFile(backupPath, 'utf8'));
|
||||
if (backup?.format !== 'shd-build-database-backup' || backup?.version !== 1 || !Array.isArray(backup.tables)) {
|
||||
throw new Error('The selected file is not a supported SHD.build database backup.');
|
||||
if (
|
||||
backup?.format !== 'shd-build-database-backup' ||
|
||||
backup?.version !== 1 ||
|
||||
!Array.isArray(backup.tables)
|
||||
) {
|
||||
throw new Error(
|
||||
'The selected file is not a supported SHD.build database backup.',
|
||||
);
|
||||
}
|
||||
|
||||
const quoteIdentifier = (identifier) => `"${String(identifier).replaceAll('"', '""')}"`;
|
||||
const quoteIdentifier = (identifier) =>
|
||||
`"${String(identifier).replaceAll('"', '""')}"`;
|
||||
const database = await connect(databasePath);
|
||||
|
||||
// Restores are deliberately limited to empty databases. This prevents an
|
||||
// accidental merge from overwriting contributor-entered records.
|
||||
for (const table of backup.tables) {
|
||||
const existing = await (
|
||||
await database.prepare(`SELECT COUNT(*) AS count FROM ${quoteIdentifier(table.name)}`)
|
||||
).get();
|
||||
if (Number(existing?.count) > 0) {
|
||||
throw new Error(
|
||||
`Restore refused: table "${table.name}" is not empty. Use a new migrated database.`
|
||||
);
|
||||
}
|
||||
const existing = await (
|
||||
await database.prepare(
|
||||
`SELECT COUNT(*) AS count FROM ${quoteIdentifier(table.name)}`,
|
||||
)
|
||||
).get();
|
||||
if (Number(existing?.count) > 0) {
|
||||
throw new Error(
|
||||
`Restore refused: table "${table.name}" is not empty. Use a new migrated database.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let restoredRows = 0;
|
||||
await database.exec('BEGIN');
|
||||
try {
|
||||
for (const table of backup.tables) {
|
||||
for (const row of table.rows) {
|
||||
const columns = Object.keys(row);
|
||||
if (!columns.length) continue;
|
||||
const columnSql = columns.map(quoteIdentifier).join(', ');
|
||||
const placeholders = columns.map(() => '?').join(', ');
|
||||
const insert = await database.prepare(
|
||||
`INSERT INTO ${quoteIdentifier(table.name)} (${columnSql}) VALUES (${placeholders})`
|
||||
);
|
||||
await insert.run(...columns.map((column) => row[column]));
|
||||
restoredRows++;
|
||||
}
|
||||
}
|
||||
await database.exec('COMMIT');
|
||||
for (const table of backup.tables) {
|
||||
for (const row of table.rows) {
|
||||
const columns = Object.keys(row);
|
||||
if (!columns.length) continue;
|
||||
const columnSql = columns.map(quoteIdentifier).join(', ');
|
||||
const placeholders = columns.map(() => '?').join(', ');
|
||||
const insert = await database.prepare(
|
||||
`INSERT INTO ${quoteIdentifier(table.name)} (${columnSql}) VALUES (${placeholders})`,
|
||||
);
|
||||
await insert.run(...columns.map((column) => row[column]));
|
||||
restoredRows++;
|
||||
}
|
||||
}
|
||||
await database.exec('COMMIT');
|
||||
} catch (error) {
|
||||
await database.exec('ROLLBACK');
|
||||
throw error;
|
||||
await database.exec('ROLLBACK');
|
||||
throw error;
|
||||
}
|
||||
|
||||
console.log(`Restored ${restoredRows} rows into ${databasePath}.`);
|
||||
|
||||
+87
-70
@@ -2,30 +2,30 @@ import { argon2id, hash } from 'argon2';
|
||||
import { randomBytes, randomUUID } from 'node:crypto';
|
||||
|
||||
export const defaultUsers = [
|
||||
{
|
||||
username: 'Aewyn',
|
||||
role: 'user',
|
||||
passwordEnvironmentKey: 'DEFAULT_ADMIN_PASSWORD'
|
||||
},
|
||||
{
|
||||
username: 'Quartermaster',
|
||||
role: 'user',
|
||||
passwordEnvironmentKey: 'DEFAULT_USER_PASSWORD'
|
||||
}
|
||||
{
|
||||
username: 'Aewyn',
|
||||
role: 'user',
|
||||
passwordEnvironmentKey: 'DEFAULT_ADMIN_PASSWORD',
|
||||
},
|
||||
{
|
||||
username: 'Quartermaster',
|
||||
role: 'user',
|
||||
passwordEnvironmentKey: 'DEFAULT_USER_PASSWORD',
|
||||
},
|
||||
];
|
||||
|
||||
function temporaryPassword() {
|
||||
// 120 bits of entropy encoded without punctuation that is awkward to paste.
|
||||
return randomBytes(15).toString('base64url');
|
||||
// 120 bits of entropy encoded without punctuation that is awkward to paste.
|
||||
return randomBytes(15).toString('base64url');
|
||||
}
|
||||
|
||||
async function secureHash(password) {
|
||||
return hash(password, {
|
||||
type: argon2id,
|
||||
memoryCost: 19_456,
|
||||
timeCost: 2,
|
||||
parallelism: 1
|
||||
});
|
||||
return hash(password, {
|
||||
type: argon2id,
|
||||
memoryCost: 19_456,
|
||||
timeCost: 2,
|
||||
parallelism: 1,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -33,61 +33,78 @@ async function secureHash(password) {
|
||||
* users keep their IDs and related records but receive new password hashes.
|
||||
*/
|
||||
export async function initializeDefaultUsers(database, options = {}) {
|
||||
const resetPasswords = options.resetPasswords === true;
|
||||
const findUser = await database.prepare(
|
||||
'SELECT id, username, role FROM users WHERE login_username = ? COLLATE NOCASE OR username = ? COLLATE NOCASE'
|
||||
);
|
||||
const insertUser = await database.prepare(
|
||||
'INSERT INTO users (id, username, login_username, password_hash, role, created_at) VALUES (?, ?, ?, ?, ?, ?)'
|
||||
);
|
||||
const updateCredential = await database.prepare(
|
||||
'UPDATE users SET login_username = ?, password_hash = ?, role = ? WHERE id = ?'
|
||||
);
|
||||
const credentials = [];
|
||||
const resetPasswords = options.resetPasswords === true;
|
||||
const findUser = await database.prepare(
|
||||
'SELECT id, username, role FROM users WHERE login_username = ? COLLATE NOCASE OR username = ? COLLATE NOCASE',
|
||||
);
|
||||
const insertUser = await database.prepare(
|
||||
'INSERT INTO users (id, username, login_username, password_hash, role, created_at) VALUES (?, ?, ?, ?, ?, ?)',
|
||||
);
|
||||
const updateCredential = await database.prepare(
|
||||
'UPDATE users SET login_username = ?, password_hash = ?, role = ? WHERE id = ?',
|
||||
);
|
||||
const credentials = [];
|
||||
|
||||
for (const definition of defaultUsers) {
|
||||
const existing = await findUser.get(definition.username, definition.username);
|
||||
if (existing && !resetPasswords) {
|
||||
console.log(`${definition.username}: already exists (${existing.role}); left unchanged`);
|
||||
continue;
|
||||
}
|
||||
for (const definition of defaultUsers) {
|
||||
const existing = await findUser.get(
|
||||
definition.username,
|
||||
definition.username,
|
||||
);
|
||||
if (existing && !resetPasswords) {
|
||||
console.log(
|
||||
`${definition.username}: already exists (${existing.role}); left unchanged`,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
const configuredPassword = process.env[definition.passwordEnvironmentKey]?.trim();
|
||||
const password = configuredPassword || temporaryPassword();
|
||||
if (password.length < 12 || password.length > 128) {
|
||||
throw new Error(`${definition.passwordEnvironmentKey} must be between 12 and 128 characters.`);
|
||||
}
|
||||
const passwordHash = await secureHash(password);
|
||||
const configuredPassword =
|
||||
process.env[definition.passwordEnvironmentKey]?.trim();
|
||||
const password = configuredPassword || temporaryPassword();
|
||||
if (password.length < 12 || password.length > 128) {
|
||||
throw new Error(
|
||||
`${definition.passwordEnvironmentKey} must be between 12 and 128 characters.`,
|
||||
);
|
||||
}
|
||||
const passwordHash = await secureHash(password);
|
||||
|
||||
if (existing) {
|
||||
await updateCredential.run(definition.username, passwordHash, String(existing.role), existing.id);
|
||||
console.log(`${definition.username}: credentials reset; role and related data preserved`);
|
||||
} else {
|
||||
await insertUser.run(
|
||||
randomUUID(),
|
||||
definition.username,
|
||||
definition.username,
|
||||
passwordHash,
|
||||
definition.role,
|
||||
new Date().toISOString()
|
||||
);
|
||||
console.log(`${definition.username}: default account created`);
|
||||
}
|
||||
credentials.push({
|
||||
username: definition.username,
|
||||
role: existing ? String(existing.role) : definition.role,
|
||||
password,
|
||||
generated: !configuredPassword
|
||||
});
|
||||
}
|
||||
if (existing) {
|
||||
await updateCredential.run(
|
||||
definition.username,
|
||||
passwordHash,
|
||||
String(existing.role),
|
||||
existing.id,
|
||||
);
|
||||
console.log(
|
||||
`${definition.username}: credentials reset; role and related data preserved`,
|
||||
);
|
||||
} else {
|
||||
await insertUser.run(
|
||||
randomUUID(),
|
||||
definition.username,
|
||||
definition.username,
|
||||
passwordHash,
|
||||
definition.role,
|
||||
new Date().toISOString(),
|
||||
);
|
||||
console.log(`${definition.username}: default account created`);
|
||||
}
|
||||
credentials.push({
|
||||
username: definition.username,
|
||||
role: existing ? String(existing.role) : definition.role,
|
||||
password,
|
||||
generated: !configuredPassword,
|
||||
});
|
||||
}
|
||||
|
||||
if (credentials.length) {
|
||||
console.log('\nDefault user credentials (shown once):');
|
||||
for (const credential of credentials) {
|
||||
console.log(`${credential.username} (${credential.role}): ${credential.password}${credential.generated ? ' [generated]' : ' [from environment]'}`);
|
||||
}
|
||||
console.log('Change generated passwords after the first sign-in.\n');
|
||||
}
|
||||
if (credentials.length) {
|
||||
console.log('\nDefault user credentials (shown once):');
|
||||
for (const credential of credentials) {
|
||||
console.log(
|
||||
`${credential.username} (${credential.role}): ${credential.password}${credential.generated ? ' [generated]' : ' [from environment]'}`,
|
||||
);
|
||||
}
|
||||
console.log('Change generated passwords after the first sign-in.\n');
|
||||
}
|
||||
|
||||
return credentials;
|
||||
return credentials;
|
||||
}
|
||||
|
||||
+12
-9
@@ -8,25 +8,28 @@ const gitCommand = process.platform === 'win32' ? 'git.exe' : 'git';
|
||||
const dotenvxCli = resolve('node_modules/@dotenvx/dotenvx/src/cli/dotenvx.js');
|
||||
|
||||
function run(command, args) {
|
||||
const result = spawnSync(command, args, { cwd: process.cwd(), stdio: 'inherit' });
|
||||
if (result.error) throw result.error;
|
||||
if (result.status !== 0) process.exit(result.status ?? 1);
|
||||
const result = spawnSync(command, args, {
|
||||
cwd: process.cwd(),
|
||||
stdio: 'inherit',
|
||||
});
|
||||
if (result.error) throw result.error;
|
||||
if (result.status !== 0) process.exit(result.status ?? 1);
|
||||
}
|
||||
|
||||
function dotenvx(command, file) {
|
||||
run(process.execPath, [dotenvxCli, command, '-f', file]);
|
||||
run(process.execPath, [dotenvxCli, command, '-f', file]);
|
||||
}
|
||||
|
||||
if (!['encrypt', 'decrypt', 'stage'].includes(action)) {
|
||||
console.error('Usage: node scripts/env-files.mjs <encrypt|decrypt|stage>');
|
||||
process.exit(2);
|
||||
console.error('Usage: node scripts/env-files.mjs <encrypt|decrypt|stage>');
|
||||
process.exit(2);
|
||||
}
|
||||
|
||||
for (const file of files) {
|
||||
dotenvx(action === 'decrypt' ? 'decrypt' : 'encrypt', file);
|
||||
dotenvx(action === 'decrypt' ? 'decrypt' : 'encrypt', file);
|
||||
}
|
||||
|
||||
if (action === 'stage') {
|
||||
run(gitCommand, ['add', '--', ...files]);
|
||||
console.log('Encrypted and staged environment files.');
|
||||
run(gitCommand, ['add', '--', ...files]);
|
||||
console.log('Encrypted and staged environment files.');
|
||||
}
|
||||
|
||||
@@ -5,50 +5,81 @@ import process from 'node:process';
|
||||
|
||||
const target = process.argv[2];
|
||||
if (!['staging', 'production'].includes(target)) {
|
||||
console.error('Usage: node scripts/environment-bootstrap-export.mjs <staging|production> [output.json]');
|
||||
process.exit(2);
|
||||
console.error(
|
||||
'Usage: node scripts/environment-bootstrap-export.mjs <staging|production> [output.json]',
|
||||
);
|
||||
process.exit(2);
|
||||
}
|
||||
const outputArgument = process.argv[3];
|
||||
const timestamp = new Date().toISOString().replaceAll(':', '-').replace(/\.\d{3}Z$/, 'Z');
|
||||
const outputPath = resolve(outputArgument || `./data/backups/shd-${target}-bootstrap-${timestamp}.json`);
|
||||
const timestamp = new Date()
|
||||
.toISOString()
|
||||
.replaceAll(':', '-')
|
||||
.replace(/\.\d{3}Z$/, 'Z');
|
||||
const outputPath = resolve(
|
||||
outputArgument || `./data/backups/shd-${target}-bootstrap-${timestamp}.json`,
|
||||
);
|
||||
const temporaryBackupPath = `${outputPath}.database.json`;
|
||||
const databaseBackup = spawnSync(process.execPath, ['scripts/db-backup.mjs', temporaryBackupPath], {
|
||||
cwd: process.cwd(),
|
||||
stdio: 'inherit',
|
||||
env: process.env
|
||||
});
|
||||
const databaseBackup = spawnSync(
|
||||
process.execPath,
|
||||
['scripts/db-backup.mjs', temporaryBackupPath],
|
||||
{
|
||||
cwd: process.cwd(),
|
||||
stdio: 'inherit',
|
||||
env: process.env,
|
||||
},
|
||||
);
|
||||
if (databaseBackup.error) throw databaseBackup.error;
|
||||
if (databaseBackup.status !== 0) throw new Error(`Database backup failed; no ${target} bootstrap bundle was created.`);
|
||||
if (databaseBackup.status !== 0)
|
||||
throw new Error(
|
||||
`Database backup failed; no ${target} bootstrap bundle was created.`,
|
||||
);
|
||||
|
||||
async function collectFiles(directory, pattern) {
|
||||
try {
|
||||
const names = (await readdir(directory)).filter((name) => pattern.test(name)).sort();
|
||||
return await Promise.all(names.map(async (name) => ({
|
||||
name,
|
||||
contentBase64: (await readFile(join(directory, name))).toString('base64')
|
||||
})));
|
||||
} catch (error) {
|
||||
if (error?.code === 'ENOENT') return [];
|
||||
throw error;
|
||||
}
|
||||
try {
|
||||
const names = (await readdir(directory))
|
||||
.filter((name) => pattern.test(name))
|
||||
.sort();
|
||||
return await Promise.all(
|
||||
names.map(async (name) => ({
|
||||
name,
|
||||
contentBase64: (await readFile(join(directory, name))).toString(
|
||||
'base64',
|
||||
),
|
||||
})),
|
||||
);
|
||||
} catch (error) {
|
||||
if (error?.code === 'ENOENT') return [];
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const database = JSON.parse(await readFile(temporaryBackupPath, 'utf8'));
|
||||
const avatars = await collectFiles(resolve(process.env.TDR_AVATAR_STORAGE_PATH || './data/avatars'), /^[0-9a-f-]{36}\.webp$/i);
|
||||
const gameIcons = await collectFiles(resolve(process.env.TDR_GAME_ICON_STORAGE_PATH || './data/game-icons'), /^[a-z_]+-[0-9a-f-]{36}\.webp$/i);
|
||||
const bundle = {
|
||||
format: 'shd-build-environment-bootstrap',
|
||||
version: 1,
|
||||
target,
|
||||
createdAt: new Date().toISOString(),
|
||||
database,
|
||||
files: { avatars, gameIcons }
|
||||
};
|
||||
await mkdir(dirname(outputPath), { recursive: true });
|
||||
await writeFile(outputPath, `${JSON.stringify(bundle, null, 2)}\n`, { encoding: 'utf8', flag: 'wx' });
|
||||
console.log(`${target} bootstrap bundle includes ${avatars.length} avatars and ${gameIcons.length} managed image assets.`);
|
||||
console.log(`Bootstrap bundle: ${outputPath}`);
|
||||
const database = JSON.parse(await readFile(temporaryBackupPath, 'utf8'));
|
||||
const avatars = await collectFiles(
|
||||
resolve(process.env.TDR_AVATAR_STORAGE_PATH || './data/avatars'),
|
||||
/^[0-9a-f-]{36}\.webp$/i,
|
||||
);
|
||||
const gameIcons = await collectFiles(
|
||||
resolve(process.env.TDR_GAME_ICON_STORAGE_PATH || './data/game-icons'),
|
||||
/^[a-z_]+-[0-9a-f-]{36}\.webp$/i,
|
||||
);
|
||||
const bundle = {
|
||||
format: 'shd-build-environment-bootstrap',
|
||||
version: 1,
|
||||
target,
|
||||
createdAt: new Date().toISOString(),
|
||||
database,
|
||||
files: { avatars, gameIcons },
|
||||
};
|
||||
await mkdir(dirname(outputPath), { recursive: true });
|
||||
await writeFile(outputPath, `${JSON.stringify(bundle, null, 2)}\n`, {
|
||||
encoding: 'utf8',
|
||||
flag: 'wx',
|
||||
});
|
||||
console.log(
|
||||
`${target} bootstrap bundle includes ${avatars.length} avatars and ${gameIcons.length} managed image assets.`,
|
||||
);
|
||||
console.log(`Bootstrap bundle: ${outputPath}`);
|
||||
} finally {
|
||||
await rm(temporaryBackupPath, { force: true });
|
||||
await rm(temporaryBackupPath, { force: true });
|
||||
}
|
||||
|
||||
@@ -4,112 +4,178 @@ import { mkdir, readFile, writeFile } from 'node:fs/promises';
|
||||
import { join, resolve } from 'node:path';
|
||||
import process from 'node:process';
|
||||
|
||||
const bundleArgument = process.argv.slice(2).find((argument) => !argument.startsWith('--'));
|
||||
const bundleArgument = process.argv
|
||||
.slice(2)
|
||||
.find((argument) => !argument.startsWith('--'));
|
||||
const targetIndex = process.argv.indexOf('--target');
|
||||
const target = targetIndex >= 0 ? process.argv[targetIndex + 1] : '';
|
||||
const allowed = target === 'staging'
|
||||
? process.env.ALLOW_STAGING_BOOTSTRAP === 'true'
|
||||
: target === 'production'
|
||||
? process.env.ALLOW_PRODUCTION_BOOTSTRAP === 'true'
|
||||
: false;
|
||||
if (!bundleArgument || !process.argv.includes('--confirm-empty-target-replace') || !allowed) {
|
||||
console.error('Environment bootstrap requires the matching ALLOW_*_BOOTSTRAP=true guard and:');
|
||||
console.error('node scripts/environment-bootstrap-import.mjs <bundle.json> --target <staging|production> --confirm-empty-target-replace');
|
||||
process.exit(2);
|
||||
const allowed =
|
||||
target === 'staging'
|
||||
? process.env.ALLOW_STAGING_BOOTSTRAP === 'true'
|
||||
: target === 'production'
|
||||
? process.env.ALLOW_PRODUCTION_BOOTSTRAP === 'true'
|
||||
: false;
|
||||
if (
|
||||
!bundleArgument ||
|
||||
!process.argv.includes('--confirm-empty-target-replace') ||
|
||||
!allowed
|
||||
) {
|
||||
console.error(
|
||||
'Environment bootstrap requires the matching ALLOW_*_BOOTSTRAP=true guard and:',
|
||||
);
|
||||
console.error(
|
||||
'node scripts/environment-bootstrap-import.mjs <bundle.json> --target <staging|production> --confirm-empty-target-replace',
|
||||
);
|
||||
process.exit(2);
|
||||
}
|
||||
|
||||
const bundlePath = resolve(bundleArgument);
|
||||
const bundle = JSON.parse(await readFile(bundlePath, 'utf8'));
|
||||
if (bundle?.format !== 'shd-build-environment-bootstrap' || bundle?.version !== 1 || bundle?.target !== target
|
||||
|| bundle.database?.format !== 'shd-build-database-backup' || bundle.database?.version !== 1
|
||||
|| !Array.isArray(bundle.database.tables) || !Array.isArray(bundle.files?.avatars)
|
||||
|| !Array.isArray(bundle.files?.gameIcons)) {
|
||||
throw new Error('The selected file is not a supported SHD.build bootstrap bundle for the selected target.');
|
||||
if (
|
||||
bundle?.format !== 'shd-build-environment-bootstrap' ||
|
||||
bundle?.version !== 1 ||
|
||||
bundle?.target !== target ||
|
||||
bundle.database?.format !== 'shd-build-database-backup' ||
|
||||
bundle.database?.version !== 1 ||
|
||||
!Array.isArray(bundle.database.tables) ||
|
||||
!Array.isArray(bundle.files?.avatars) ||
|
||||
!Array.isArray(bundle.files?.gameIcons)
|
||||
) {
|
||||
throw new Error(
|
||||
'The selected file is not a supported SHD.build bootstrap bundle for the selected target.',
|
||||
);
|
||||
}
|
||||
for (const file of bundle.files.avatars) {
|
||||
if (typeof file.name !== 'string' || !/^[0-9a-f-]{36}\.webp$/i.test(file.name) || typeof file.contentBase64 !== 'string') {
|
||||
throw new Error('The bootstrap bundle contains an invalid avatar file.');
|
||||
}
|
||||
if (
|
||||
typeof file.name !== 'string' ||
|
||||
!/^[0-9a-f-]{36}\.webp$/i.test(file.name) ||
|
||||
typeof file.contentBase64 !== 'string'
|
||||
) {
|
||||
throw new Error('The bootstrap bundle contains an invalid avatar file.');
|
||||
}
|
||||
}
|
||||
for (const file of bundle.files.gameIcons) {
|
||||
if (typeof file.name !== 'string' || !/^[a-z_]+-[0-9a-f-]{36}\.webp$/i.test(file.name) || typeof file.contentBase64 !== 'string') {
|
||||
throw new Error('The bootstrap bundle contains an invalid game-data icon file.');
|
||||
}
|
||||
if (
|
||||
typeof file.name !== 'string' ||
|
||||
!/^[a-z_]+-[0-9a-f-]{36}\.webp$/i.test(file.name) ||
|
||||
typeof file.contentBase64 !== 'string'
|
||||
) {
|
||||
throw new Error(
|
||||
'The bootstrap bundle contains an invalid game-data icon file.',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const databasePath = resolve(process.env.TDR_DATABASE_PATH || `./data/shd-build-${target}.db`);
|
||||
const databasePath = resolve(
|
||||
process.env.TDR_DATABASE_PATH || `./data/shd-build-${target}.db`,
|
||||
);
|
||||
const safetyBackup = spawnSync(process.execPath, ['scripts/db-backup.mjs'], {
|
||||
cwd: process.cwd(),
|
||||
stdio: 'inherit',
|
||||
env: process.env
|
||||
cwd: process.cwd(),
|
||||
stdio: 'inherit',
|
||||
env: process.env,
|
||||
});
|
||||
if (safetyBackup.error) throw safetyBackup.error;
|
||||
if (safetyBackup.status !== 0) throw new Error(`${target} safety backup failed; the target was not changed.`);
|
||||
if (safetyBackup.status !== 0)
|
||||
throw new Error(
|
||||
`${target} safety backup failed; the target was not changed.`,
|
||||
);
|
||||
|
||||
const database = await connect(databasePath);
|
||||
for (const tableName of ['users', 'builds', 'audit_log', 'data_change_proposals']) {
|
||||
const row = await (await database.prepare(`SELECT COUNT(*) AS count FROM "${tableName}"`)).get();
|
||||
if (Number(row?.count ?? 0) > 0) {
|
||||
throw new Error(`${target} bootstrap refused: target table "${tableName}" is not empty.`);
|
||||
}
|
||||
for (const tableName of [
|
||||
'users',
|
||||
'builds',
|
||||
'audit_log',
|
||||
'data_change_proposals',
|
||||
]) {
|
||||
const row = await (
|
||||
await database.prepare(`SELECT COUNT(*) AS count FROM "${tableName}"`)
|
||||
).get();
|
||||
if (Number(row?.count ?? 0) > 0) {
|
||||
throw new Error(
|
||||
`${target} bootstrap refused: target table "${tableName}" is not empty.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const targetTables = await (await database.prepare(`
|
||||
const targetTables = await (
|
||||
await database.prepare(`
|
||||
SELECT name FROM sqlite_master
|
||||
WHERE type='table' AND name NOT LIKE 'sqlite_%'
|
||||
ORDER BY rowid
|
||||
`)).all();
|
||||
`)
|
||||
).all();
|
||||
const targetNames = new Set(targetTables.map((row) => String(row.name)));
|
||||
const sourceNames = new Set(bundle.database.tables.map((table) => String(table.name)));
|
||||
const sourceNames = new Set(
|
||||
bundle.database.tables.map((table) => String(table.name)),
|
||||
);
|
||||
for (const row of targetTables) {
|
||||
if (!sourceNames.has(String(row.name))) {
|
||||
throw new Error(`Bootstrap snapshot is missing target-schema table "${String(row.name)}".`);
|
||||
}
|
||||
if (!sourceNames.has(String(row.name))) {
|
||||
throw new Error(
|
||||
`Bootstrap snapshot is missing target-schema table "${String(row.name)}".`,
|
||||
);
|
||||
}
|
||||
}
|
||||
const sourceOnlyTables = bundle.database.tables
|
||||
.map((table) => String(table.name))
|
||||
.filter((name) => !targetNames.has(name));
|
||||
.map((table) => String(table.name))
|
||||
.filter((name) => !targetNames.has(name));
|
||||
if (sourceOnlyTables.length) {
|
||||
throw new Error(`Bootstrap snapshot contains tables outside the current target schema: ${sourceOnlyTables.join(', ')}`);
|
||||
throw new Error(
|
||||
`Bootstrap snapshot contains tables outside the current target schema: ${sourceOnlyTables.join(', ')}`,
|
||||
);
|
||||
}
|
||||
const quoteIdentifier = (identifier) => `"${String(identifier).replaceAll('"', '""')}"`;
|
||||
const quoteIdentifier = (identifier) =>
|
||||
`"${String(identifier).replaceAll('"', '""')}"`;
|
||||
let restoredRows = 0;
|
||||
await database.exec('PRAGMA foreign_keys=OFF');
|
||||
await database.exec('BEGIN');
|
||||
try {
|
||||
for (const row of [...targetTables].reverse()) {
|
||||
await database.exec(`DELETE FROM ${quoteIdentifier(row.name)}`);
|
||||
}
|
||||
for (const table of bundle.database.tables) {
|
||||
for (const row of table.rows) {
|
||||
const columns = Object.keys(row);
|
||||
if (!columns.length) continue;
|
||||
const insert = await database.prepare(
|
||||
`INSERT INTO ${quoteIdentifier(table.name)} (${columns.map(quoteIdentifier).join(', ')})
|
||||
VALUES (${columns.map(() => '?').join(', ')})`
|
||||
);
|
||||
await insert.run(...columns.map((column) => row[column]));
|
||||
restoredRows++;
|
||||
}
|
||||
}
|
||||
await database.exec('COMMIT');
|
||||
for (const row of [...targetTables].reverse()) {
|
||||
await database.exec(`DELETE FROM ${quoteIdentifier(row.name)}`);
|
||||
}
|
||||
for (const table of bundle.database.tables) {
|
||||
for (const row of table.rows) {
|
||||
const columns = Object.keys(row);
|
||||
if (!columns.length) continue;
|
||||
const insert = await database.prepare(
|
||||
`INSERT INTO ${quoteIdentifier(table.name)} (${columns.map(quoteIdentifier).join(', ')})
|
||||
VALUES (${columns.map(() => '?').join(', ')})`,
|
||||
);
|
||||
await insert.run(...columns.map((column) => row[column]));
|
||||
restoredRows++;
|
||||
}
|
||||
}
|
||||
await database.exec('COMMIT');
|
||||
} catch (error) {
|
||||
await database.exec('ROLLBACK');
|
||||
throw error;
|
||||
await database.exec('ROLLBACK');
|
||||
throw error;
|
||||
} finally {
|
||||
await database.exec('PRAGMA foreign_keys=ON');
|
||||
await database.exec('PRAGMA foreign_keys=ON');
|
||||
}
|
||||
|
||||
const avatarDirectory = resolve(process.env.TDR_AVATAR_STORAGE_PATH || './data/avatars');
|
||||
const gameIconDirectory = resolve(process.env.TDR_GAME_ICON_STORAGE_PATH || './data/game-icons');
|
||||
const avatarDirectory = resolve(
|
||||
process.env.TDR_AVATAR_STORAGE_PATH || './data/avatars',
|
||||
);
|
||||
const gameIconDirectory = resolve(
|
||||
process.env.TDR_GAME_ICON_STORAGE_PATH || './data/game-icons',
|
||||
);
|
||||
await mkdir(avatarDirectory, { recursive: true });
|
||||
await mkdir(gameIconDirectory, { recursive: true });
|
||||
for (const file of bundle.files.avatars) {
|
||||
await writeFile(join(avatarDirectory, file.name), Buffer.from(file.contentBase64, 'base64'), { mode: 0o600 });
|
||||
await writeFile(
|
||||
join(avatarDirectory, file.name),
|
||||
Buffer.from(file.contentBase64, 'base64'),
|
||||
{ mode: 0o600 },
|
||||
);
|
||||
}
|
||||
for (const file of bundle.files.gameIcons) {
|
||||
await writeFile(join(gameIconDirectory, file.name), Buffer.from(file.contentBase64, 'base64'), { mode: 0o600 });
|
||||
await writeFile(
|
||||
join(gameIconDirectory, file.name),
|
||||
Buffer.from(file.contentBase64, 'base64'),
|
||||
{ mode: 0o600 },
|
||||
);
|
||||
}
|
||||
|
||||
console.log(`Bootstrapped ${target} with ${restoredRows} database rows, ${bundle.files.avatars.length} avatars, and ${bundle.files.gameIcons.length} managed image assets.`);
|
||||
console.log(
|
||||
`Bootstrapped ${target} with ${restoredRows} database rows, ${bundle.files.avatars.length} avatars, and ${bundle.files.gameIcons.length} managed image assets.`,
|
||||
);
|
||||
console.log(`Source bundle: ${bundlePath}`);
|
||||
|
||||
@@ -4,121 +4,186 @@ import { randomUUID } from 'node:crypto';
|
||||
import { isDeepStrictEqual } from 'node:util';
|
||||
import { resolve } from 'node:path';
|
||||
|
||||
const sourcePath = resolve(process.argv[2] || './scripts/data/resurgence-attributes.json');
|
||||
const sourcePath = resolve(
|
||||
process.argv[2] || './scripts/data/resurgence-attributes.json',
|
||||
);
|
||||
const records = JSON.parse(await readFile(sourcePath, 'utf8'));
|
||||
if (!Array.isArray(records) || !records.length) throw new Error('The Attribute import source is empty or invalid.');
|
||||
if (!Array.isArray(records) || !records.length)
|
||||
throw new Error('The Attribute import source is empty or invalid.');
|
||||
|
||||
const identities = new Set();
|
||||
for (const record of records) {
|
||||
const identity = `${String(record.name).normalize('NFKC').trim().toLocaleLowerCase()}\u0000${String(record.unit ?? '')}`;
|
||||
if (identities.has(identity)) throw new Error(`Duplicate Attribute identity in import source: ${record.name} (${record.unit || 'flat'})`);
|
||||
identities.add(identity);
|
||||
if (!['Offensive', 'Defensive'].includes(record.type)
|
||||
|| !['Weapon', 'Skill', 'Character'].includes(record.category)
|
||||
|| !['Additive', 'Multiplicative'].includes(record.calculationType)
|
||||
|| !['', '%', 's', 'Shots', 'm'].includes(record.unit ?? '')) {
|
||||
throw new Error(`Invalid Attribute classification in import source: ${record.name}`);
|
||||
}
|
||||
for (const range of [record.gearRange, record.weaponRange]) {
|
||||
if (range === undefined) continue;
|
||||
if (!Array.isArray(range) || range.length !== 2 || !range.every(Number.isFinite) || range[0] > range[1]) {
|
||||
throw new Error(`Invalid source range in import source: ${record.name}`);
|
||||
}
|
||||
}
|
||||
const identity = `${String(record.name).normalize('NFKC').trim().toLocaleLowerCase()}\u0000${String(record.unit ?? '')}`;
|
||||
if (identities.has(identity))
|
||||
throw new Error(
|
||||
`Duplicate Attribute identity in import source: ${record.name} (${record.unit || 'flat'})`,
|
||||
);
|
||||
identities.add(identity);
|
||||
if (
|
||||
!['Offensive', 'Defensive'].includes(record.type) ||
|
||||
!['Weapon', 'Skill', 'Character'].includes(record.category) ||
|
||||
!['Additive', 'Multiplicative'].includes(record.calculationType) ||
|
||||
!['', '%', 's', 'Shots', 'm'].includes(record.unit ?? '')
|
||||
) {
|
||||
throw new Error(
|
||||
`Invalid Attribute classification in import source: ${record.name}`,
|
||||
);
|
||||
}
|
||||
for (const range of [record.gearRange, record.weaponRange]) {
|
||||
if (range === undefined) continue;
|
||||
if (
|
||||
!Array.isArray(range) ||
|
||||
range.length !== 2 ||
|
||||
!range.every(Number.isFinite) ||
|
||||
range[0] > range[1]
|
||||
) {
|
||||
throw new Error(`Invalid source range in import source: ${record.name}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const vite = await createServer({ server: { middlewareMode: true }, appType: 'custom', logLevel: 'error' });
|
||||
const vite = await createServer({
|
||||
server: { middlewareMode: true },
|
||||
appType: 'custom',
|
||||
logLevel: 'error',
|
||||
});
|
||||
try {
|
||||
const { getDatabase } = await vite.ssrLoadModule('/src/lib/server/database.ts');
|
||||
const { auditRecord, recordIsacAudit } = await vite.ssrLoadModule('/src/lib/server/audit.ts');
|
||||
const database = await getDatabase();
|
||||
const actor = await (await database.prepare(`SELECT users.id FROM users
|
||||
const { getDatabase } = await vite.ssrLoadModule(
|
||||
'/src/lib/server/database.ts',
|
||||
);
|
||||
const { auditRecord, recordIsacAudit } = await vite.ssrLoadModule(
|
||||
'/src/lib/server/audit.ts',
|
||||
);
|
||||
const database = await getDatabase();
|
||||
const actor = await (
|
||||
await database.prepare(`SELECT users.id FROM users
|
||||
LEFT JOIN user_profiles profile ON profile.user_id=users.id
|
||||
LEFT JOIN user_roles link ON link.user_id=users.id
|
||||
WHERE users.role='admin' OR link.role_id='role-admin'
|
||||
ORDER BY CASE WHEN users.username=? COLLATE NOCASE OR profile.display_name=? COLLATE NOCASE THEN 0 ELSE 1 END,users.created_at
|
||||
LIMIT 1`)).get('Aewyn', 'Aewyn');
|
||||
if (!actor?.id) throw new Error('No administrator account is available to own the Attribute import audit events.');
|
||||
LIMIT 1`)
|
||||
).get('Aewyn', 'Aewyn');
|
||||
if (!actor?.id)
|
||||
throw new Error(
|
||||
'No administrator account is available to own the Attribute import audit events.',
|
||||
);
|
||||
|
||||
let created = 0;
|
||||
let updated = 0;
|
||||
let unchanged = 0;
|
||||
await database.exec('BEGIN');
|
||||
try {
|
||||
for (const record of records) {
|
||||
const name = String(record.name).normalize('NFKC').trim();
|
||||
const unit = String(record.unit ?? '');
|
||||
const existing = await (await database.prepare(
|
||||
'SELECT * FROM stat_attributes WHERE name=? COLLATE NOCASE AND unit=?'
|
||||
)).get(name, unit);
|
||||
const id = String(existing?.id ?? randomUUID());
|
||||
const now = new Date().toISOString();
|
||||
const next = {
|
||||
...(existing ?? {}),
|
||||
id,
|
||||
name,
|
||||
description: existing?.description ?? null,
|
||||
type: record.type,
|
||||
category: record.category,
|
||||
calculation_type: record.calculationType,
|
||||
unit,
|
||||
gear_min: record.gearRange?.[0] ?? existing?.gear_min ?? null,
|
||||
gear_max: record.gearRange?.[1] ?? existing?.gear_max ?? null,
|
||||
gear_reference_level: Number(existing?.gear_reference_level ?? 50),
|
||||
weapon_min: record.weaponRange?.[0] ?? existing?.weapon_min ?? null,
|
||||
weapon_max: record.weaponRange?.[1] ?? existing?.weapon_max ?? null,
|
||||
weapon_reference_level: Number(existing?.weapon_reference_level ?? 50),
|
||||
skill_chip_min: existing?.skill_chip_min ?? null,
|
||||
skill_chip_max: existing?.skill_chip_max ?? null,
|
||||
cap_value: existing?.cap_value ?? null,
|
||||
active: 1,
|
||||
created_at: String(existing?.created_at ?? now),
|
||||
updated_at: now
|
||||
};
|
||||
const comparableExisting = existing ? { ...existing, updated_at: now } : null;
|
||||
if (comparableExisting && isDeepStrictEqual(comparableExisting, next)) {
|
||||
unchanged++;
|
||||
continue;
|
||||
}
|
||||
const before = existing ? await auditRecord('stat_attributes', id) : null;
|
||||
if (existing) {
|
||||
await (await database.prepare(`UPDATE stat_attributes SET
|
||||
let created = 0;
|
||||
let updated = 0;
|
||||
let unchanged = 0;
|
||||
await database.exec('BEGIN');
|
||||
try {
|
||||
for (const record of records) {
|
||||
const name = String(record.name).normalize('NFKC').trim();
|
||||
const unit = String(record.unit ?? '');
|
||||
const existing = await (
|
||||
await database.prepare(
|
||||
'SELECT * FROM stat_attributes WHERE name=? COLLATE NOCASE AND unit=?',
|
||||
)
|
||||
).get(name, unit);
|
||||
const id = String(existing?.id ?? randomUUID());
|
||||
const now = new Date().toISOString();
|
||||
const next = {
|
||||
...(existing ?? {}),
|
||||
id,
|
||||
name,
|
||||
description: existing?.description ?? null,
|
||||
type: record.type,
|
||||
category: record.category,
|
||||
calculation_type: record.calculationType,
|
||||
unit,
|
||||
gear_min: record.gearRange?.[0] ?? existing?.gear_min ?? null,
|
||||
gear_max: record.gearRange?.[1] ?? existing?.gear_max ?? null,
|
||||
gear_reference_level: Number(existing?.gear_reference_level ?? 50),
|
||||
weapon_min: record.weaponRange?.[0] ?? existing?.weapon_min ?? null,
|
||||
weapon_max: record.weaponRange?.[1] ?? existing?.weapon_max ?? null,
|
||||
weapon_reference_level: Number(existing?.weapon_reference_level ?? 50),
|
||||
skill_chip_min: existing?.skill_chip_min ?? null,
|
||||
skill_chip_max: existing?.skill_chip_max ?? null,
|
||||
cap_value: existing?.cap_value ?? null,
|
||||
active: 1,
|
||||
created_at: String(existing?.created_at ?? now),
|
||||
updated_at: now,
|
||||
};
|
||||
const comparableExisting = existing
|
||||
? { ...existing, updated_at: now }
|
||||
: null;
|
||||
if (comparableExisting && isDeepStrictEqual(comparableExisting, next)) {
|
||||
unchanged++;
|
||||
continue;
|
||||
}
|
||||
const before = existing ? await auditRecord('stat_attributes', id) : null;
|
||||
if (existing) {
|
||||
await (
|
||||
await database.prepare(`UPDATE stat_attributes SET
|
||||
name=?,description=?,type=?,category=?,calculation_type=?,unit=?,
|
||||
gear_min=?,gear_max=?,weapon_min=?,weapon_max=?,skill_chip_min=?,skill_chip_max=?,
|
||||
cap_value=?,active=1,updated_at=? WHERE id=?`)).run(
|
||||
next.name, next.description, next.type, next.category, next.calculation_type, next.unit,
|
||||
next.gear_min, next.gear_max, next.weapon_min, next.weapon_max, next.skill_chip_min,
|
||||
next.skill_chip_max, next.cap_value, now, id
|
||||
);
|
||||
updated++;
|
||||
} else {
|
||||
await (await database.prepare(`INSERT INTO stat_attributes(
|
||||
cap_value=?,active=1,updated_at=? WHERE id=?`)
|
||||
).run(
|
||||
next.name,
|
||||
next.description,
|
||||
next.type,
|
||||
next.category,
|
||||
next.calculation_type,
|
||||
next.unit,
|
||||
next.gear_min,
|
||||
next.gear_max,
|
||||
next.weapon_min,
|
||||
next.weapon_max,
|
||||
next.skill_chip_min,
|
||||
next.skill_chip_max,
|
||||
next.cap_value,
|
||||
now,
|
||||
id,
|
||||
);
|
||||
updated++;
|
||||
} else {
|
||||
await (
|
||||
await database.prepare(`INSERT INTO stat_attributes(
|
||||
id,name,description,type,category,calculation_type,unit,
|
||||
gear_min,gear_max,gear_reference_level,weapon_min,weapon_max,weapon_reference_level,
|
||||
skill_chip_min,skill_chip_max,cap_value,active,created_at,updated_at
|
||||
) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,1,?,?)`)).run(
|
||||
id, next.name, next.description, next.type, next.category, next.calculation_type, next.unit,
|
||||
next.gear_min, next.gear_max, next.gear_reference_level, next.weapon_min, next.weapon_max,
|
||||
next.weapon_reference_level, next.skill_chip_min, next.skill_chip_max, next.cap_value, now, now
|
||||
);
|
||||
created++;
|
||||
}
|
||||
await recordIsacAudit({
|
||||
actorId: String(actor.id),
|
||||
category: 'game_data',
|
||||
action: existing ? 'attribute.updated' : 'attribute.created',
|
||||
entityId: id,
|
||||
before,
|
||||
after: await auditRecord('stat_attributes', id)
|
||||
});
|
||||
}
|
||||
await database.exec('COMMIT');
|
||||
} catch (error) {
|
||||
await database.exec('ROLLBACK');
|
||||
throw error;
|
||||
}
|
||||
console.log(`Imported ${records.length} Attribute definitions: ${created} created, ${updated} updated, ${unchanged} unchanged.`);
|
||||
console.log(`Source: ${sourcePath}`);
|
||||
) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,1,?,?)`)
|
||||
).run(
|
||||
id,
|
||||
next.name,
|
||||
next.description,
|
||||
next.type,
|
||||
next.category,
|
||||
next.calculation_type,
|
||||
next.unit,
|
||||
next.gear_min,
|
||||
next.gear_max,
|
||||
next.gear_reference_level,
|
||||
next.weapon_min,
|
||||
next.weapon_max,
|
||||
next.weapon_reference_level,
|
||||
next.skill_chip_min,
|
||||
next.skill_chip_max,
|
||||
next.cap_value,
|
||||
now,
|
||||
now,
|
||||
);
|
||||
created++;
|
||||
}
|
||||
await recordIsacAudit({
|
||||
actorId: String(actor.id),
|
||||
category: 'game_data',
|
||||
action: existing ? 'attribute.updated' : 'attribute.created',
|
||||
entityId: id,
|
||||
before,
|
||||
after: await auditRecord('stat_attributes', id),
|
||||
});
|
||||
}
|
||||
await database.exec('COMMIT');
|
||||
} catch (error) {
|
||||
await database.exec('ROLLBACK');
|
||||
throw error;
|
||||
}
|
||||
console.log(
|
||||
`Imported ${records.length} Attribute definitions: ${created} created, ${updated} updated, ${unchanged} unchanged.`,
|
||||
);
|
||||
console.log(`Source: ${sourcePath}`);
|
||||
} finally {
|
||||
await vite.close();
|
||||
await vite.close();
|
||||
}
|
||||
|
||||
@@ -4,49 +4,83 @@ import { basename, dirname, join, resolve } from 'node:path';
|
||||
import process from 'node:process';
|
||||
import { ISAC_DATA_TABLES, quoteIdentifier } from './isac-data-tables.mjs';
|
||||
|
||||
const databasePath = resolve(process.env.TDR_DATABASE_PATH || './data/shd-build-development.db');
|
||||
const iconDirectory = resolve(process.env.TDR_GAME_ICON_STORAGE_PATH || './data/game-icons');
|
||||
const timestamp = new Date().toISOString().replaceAll(':', '-').replace(/\.\d{3}Z$/, 'Z');
|
||||
const outputPath = resolve(process.argv[2] || `./data/backups/isac-data-${timestamp}.json`);
|
||||
const databasePath = resolve(
|
||||
process.env.TDR_DATABASE_PATH || './data/shd-build-development.db',
|
||||
);
|
||||
const iconDirectory = resolve(
|
||||
process.env.TDR_GAME_ICON_STORAGE_PATH || './data/game-icons',
|
||||
);
|
||||
const timestamp = new Date()
|
||||
.toISOString()
|
||||
.replaceAll(':', '-')
|
||||
.replace(/\.\d{3}Z$/, 'Z');
|
||||
const outputPath = resolve(
|
||||
process.argv[2] || `./data/backups/isac-data-${timestamp}.json`,
|
||||
);
|
||||
const database = await connect(databasePath);
|
||||
const tables = [];
|
||||
let totalRows = 0;
|
||||
|
||||
for (const name of ISAC_DATA_TABLES) {
|
||||
const exists = await (await database.prepare(
|
||||
"SELECT 1 AS present FROM sqlite_master WHERE type='table' AND name=?"
|
||||
)).get(name);
|
||||
if (!exists) throw new Error(`ISAC export requires missing table "${name}". Run the schema migration first.`);
|
||||
const rows = await (await database.prepare(`SELECT * FROM ${quoteIdentifier(name)}`)).all();
|
||||
tables.push({ name, rowCount: rows.length, rows });
|
||||
totalRows += rows.length;
|
||||
const exists = await (
|
||||
await database.prepare(
|
||||
"SELECT 1 AS present FROM sqlite_master WHERE type='table' AND name=?",
|
||||
)
|
||||
).get(name);
|
||||
if (!exists)
|
||||
throw new Error(
|
||||
`ISAC export requires missing table "${name}". Run the schema migration first.`,
|
||||
);
|
||||
const rows = await (
|
||||
await database.prepare(`SELECT * FROM ${quoteIdentifier(name)}`)
|
||||
).all();
|
||||
tables.push({ name, rowCount: rows.length, rows });
|
||||
totalRows += rows.length;
|
||||
}
|
||||
|
||||
const icons = [];
|
||||
const exportedStorageKeys = new Set();
|
||||
const iconTable = tables.find((table) => table.name === 'game_data_icons');
|
||||
const managedImageTable = tables.find((table) => table.name === 'managed_images');
|
||||
for (const row of [...(iconTable?.rows ?? []), ...(managedImageTable?.rows ?? [])]) {
|
||||
if (typeof row.storage_key !== 'string' || !/^[a-z_]+-[0-9a-f-]{36}\.webp$/i.test(row.storage_key)) {
|
||||
throw new Error(`Invalid managed asset storage key "${String(row.storage_key)}".`);
|
||||
}
|
||||
if (exportedStorageKeys.has(row.storage_key)) continue;
|
||||
exportedStorageKeys.add(row.storage_key);
|
||||
const content = await readFile(join(iconDirectory, row.storage_key));
|
||||
icons.push({ storageKey: row.storage_key, contentBase64: content.toString('base64') });
|
||||
const managedImageTable = tables.find(
|
||||
(table) => table.name === 'managed_images',
|
||||
);
|
||||
for (const row of [
|
||||
...(iconTable?.rows ?? []),
|
||||
...(managedImageTable?.rows ?? []),
|
||||
]) {
|
||||
if (
|
||||
typeof row.storage_key !== 'string' ||
|
||||
!/^[a-z_]+-[0-9a-f-]{36}\.webp$/i.test(row.storage_key)
|
||||
) {
|
||||
throw new Error(
|
||||
`Invalid managed asset storage key "${String(row.storage_key)}".`,
|
||||
);
|
||||
}
|
||||
if (exportedStorageKeys.has(row.storage_key)) continue;
|
||||
exportedStorageKeys.add(row.storage_key);
|
||||
const content = await readFile(join(iconDirectory, row.storage_key));
|
||||
icons.push({
|
||||
storageKey: row.storage_key,
|
||||
contentBase64: content.toString('base64'),
|
||||
});
|
||||
}
|
||||
|
||||
const snapshot = {
|
||||
format: 'shd-build-isac-data',
|
||||
version: 2,
|
||||
createdAt: new Date().toISOString(),
|
||||
sourceDatabase: basename(databasePath),
|
||||
totalRows,
|
||||
tables,
|
||||
icons
|
||||
format: 'shd-build-isac-data',
|
||||
version: 2,
|
||||
createdAt: new Date().toISOString(),
|
||||
sourceDatabase: basename(databasePath),
|
||||
totalRows,
|
||||
tables,
|
||||
icons,
|
||||
};
|
||||
await mkdir(dirname(outputPath), { recursive: true });
|
||||
await writeFile(outputPath, `${JSON.stringify(snapshot, null, 2)}\n`, { encoding: 'utf8', flag: 'wx' });
|
||||
await writeFile(outputPath, `${JSON.stringify(snapshot, null, 2)}\n`, {
|
||||
encoding: 'utf8',
|
||||
flag: 'wx',
|
||||
});
|
||||
|
||||
console.log(`Exported ${totalRows} ISAC rows and ${icons.length} uploaded image assets.`);
|
||||
console.log(
|
||||
`Exported ${totalRows} ISAC rows and ${icons.length} uploaded image assets.`,
|
||||
);
|
||||
console.log(`ISAC snapshot: ${outputPath}`);
|
||||
|
||||
@@ -5,39 +5,77 @@ import { basename, join, resolve } from 'node:path';
|
||||
import process from 'node:process';
|
||||
import { ISAC_DATA_TABLES, quoteIdentifier } from './isac-data-tables.mjs';
|
||||
|
||||
const snapshotArgument = process.argv.slice(2).find((argument) => !argument.startsWith('--'));
|
||||
if (!snapshotArgument || !process.argv.includes('--confirm-replace-isac-data')) {
|
||||
console.error('Usage: node scripts/isac-data-import.mjs <snapshot.json> --confirm-replace-isac-data');
|
||||
process.exit(2);
|
||||
const snapshotArgument = process.argv
|
||||
.slice(2)
|
||||
.find((argument) => !argument.startsWith('--'));
|
||||
if (
|
||||
!snapshotArgument ||
|
||||
!process.argv.includes('--confirm-replace-isac-data')
|
||||
) {
|
||||
console.error(
|
||||
'Usage: node scripts/isac-data-import.mjs <snapshot.json> --confirm-replace-isac-data',
|
||||
);
|
||||
process.exit(2);
|
||||
}
|
||||
|
||||
const snapshotPath = resolve(snapshotArgument);
|
||||
const snapshot = JSON.parse(await readFile(snapshotPath, 'utf8'));
|
||||
if (snapshot?.format !== 'shd-build-isac-data' || snapshot?.version !== 2 || !Array.isArray(snapshot.tables) || !Array.isArray(snapshot.icons)) {
|
||||
throw new Error('The selected file is not a supported SHD.build ISAC data snapshot.');
|
||||
if (
|
||||
snapshot?.format !== 'shd-build-isac-data' ||
|
||||
snapshot?.version !== 2 ||
|
||||
!Array.isArray(snapshot.tables) ||
|
||||
!Array.isArray(snapshot.icons)
|
||||
) {
|
||||
throw new Error(
|
||||
'The selected file is not a supported SHD.build ISAC data snapshot.',
|
||||
);
|
||||
}
|
||||
const receivedNames = snapshot.tables.map((table) => table.name);
|
||||
if (receivedNames.length !== ISAC_DATA_TABLES.length || ISAC_DATA_TABLES.some((name, index) => receivedNames[index] !== name)) {
|
||||
throw new Error('The ISAC snapshot table manifest is incomplete or out of order.');
|
||||
if (
|
||||
receivedNames.length !== ISAC_DATA_TABLES.length ||
|
||||
ISAC_DATA_TABLES.some((name, index) => receivedNames[index] !== name)
|
||||
) {
|
||||
throw new Error(
|
||||
'The ISAC snapshot table manifest is incomplete or out of order.',
|
||||
);
|
||||
}
|
||||
for (const icon of snapshot.icons) {
|
||||
if (typeof icon.storageKey !== 'string' || !/^[a-z_]+-[0-9a-f-]{36}\.webp$/i.test(icon.storageKey)
|
||||
|| typeof icon.contentBase64 !== 'string') {
|
||||
throw new Error('The ISAC snapshot contains an invalid uploaded icon.');
|
||||
}
|
||||
if (
|
||||
typeof icon.storageKey !== 'string' ||
|
||||
!/^[a-z_]+-[0-9a-f-]{36}\.webp$/i.test(icon.storageKey) ||
|
||||
typeof icon.contentBase64 !== 'string'
|
||||
) {
|
||||
throw new Error('The ISAC snapshot contains an invalid uploaded icon.');
|
||||
}
|
||||
}
|
||||
|
||||
const timestamp = new Date().toISOString().replaceAll(':', '-').replace(/\.\d{3}Z$/, 'Z');
|
||||
const databasePath = resolve(process.env.TDR_DATABASE_PATH || './data/shd-build-development.db');
|
||||
const backupPath = resolve(`./data/backups/${basename(databasePath).replace(/\.[^.]+$/, '')}-before-isac-import-${timestamp}.json`);
|
||||
const isacBackupPath = resolve(`./data/backups/isac-data-before-import-${timestamp}.json`);
|
||||
const timestamp = new Date()
|
||||
.toISOString()
|
||||
.replaceAll(':', '-')
|
||||
.replace(/\.\d{3}Z$/, 'Z');
|
||||
const databasePath = resolve(
|
||||
process.env.TDR_DATABASE_PATH || './data/shd-build-development.db',
|
||||
);
|
||||
const backupPath = resolve(
|
||||
`./data/backups/${basename(databasePath).replace(/\.[^.]+$/, '')}-before-isac-import-${timestamp}.json`,
|
||||
);
|
||||
const isacBackupPath = resolve(
|
||||
`./data/backups/isac-data-before-import-${timestamp}.json`,
|
||||
);
|
||||
for (const [script, output] of [
|
||||
['scripts/db-backup.mjs', backupPath],
|
||||
['scripts/isac-data-export.mjs', isacBackupPath]
|
||||
['scripts/db-backup.mjs', backupPath],
|
||||
['scripts/isac-data-export.mjs', isacBackupPath],
|
||||
]) {
|
||||
const result = spawnSync(process.execPath, [script, output], { cwd: process.cwd(), stdio: 'inherit', env: process.env });
|
||||
if (result.error) throw result.error;
|
||||
if (result.status !== 0) throw new Error(`Safety backup failed in ${script}; no ISAC data was changed.`);
|
||||
const result = spawnSync(process.execPath, [script, output], {
|
||||
cwd: process.cwd(),
|
||||
stdio: 'inherit',
|
||||
env: process.env,
|
||||
});
|
||||
if (result.error) throw result.error;
|
||||
if (result.status !== 0)
|
||||
throw new Error(
|
||||
`Safety backup failed in ${script}; no ISAC data was changed.`,
|
||||
);
|
||||
}
|
||||
|
||||
const database = await connect(databasePath);
|
||||
@@ -45,35 +83,43 @@ let importedRows = 0;
|
||||
await database.exec('PRAGMA foreign_keys=OFF');
|
||||
await database.exec('BEGIN');
|
||||
try {
|
||||
for (const name of [...ISAC_DATA_TABLES].reverse()) {
|
||||
await database.exec(`DELETE FROM ${quoteIdentifier(name)}`);
|
||||
}
|
||||
for (const table of snapshot.tables) {
|
||||
for (const row of table.rows) {
|
||||
const columns = Object.keys(row);
|
||||
if (!columns.length) continue;
|
||||
const insert = await database.prepare(
|
||||
`INSERT INTO ${quoteIdentifier(table.name)} (${columns.map(quoteIdentifier).join(', ')})
|
||||
VALUES (${columns.map(() => '?').join(', ')})`
|
||||
);
|
||||
await insert.run(...columns.map((column) => row[column]));
|
||||
importedRows++;
|
||||
}
|
||||
}
|
||||
await database.exec('COMMIT');
|
||||
for (const name of [...ISAC_DATA_TABLES].reverse()) {
|
||||
await database.exec(`DELETE FROM ${quoteIdentifier(name)}`);
|
||||
}
|
||||
for (const table of snapshot.tables) {
|
||||
for (const row of table.rows) {
|
||||
const columns = Object.keys(row);
|
||||
if (!columns.length) continue;
|
||||
const insert = await database.prepare(
|
||||
`INSERT INTO ${quoteIdentifier(table.name)} (${columns.map(quoteIdentifier).join(', ')})
|
||||
VALUES (${columns.map(() => '?').join(', ')})`,
|
||||
);
|
||||
await insert.run(...columns.map((column) => row[column]));
|
||||
importedRows++;
|
||||
}
|
||||
}
|
||||
await database.exec('COMMIT');
|
||||
} catch (error) {
|
||||
await database.exec('ROLLBACK');
|
||||
throw error;
|
||||
await database.exec('ROLLBACK');
|
||||
throw error;
|
||||
} finally {
|
||||
await database.exec('PRAGMA foreign_keys=ON');
|
||||
await database.exec('PRAGMA foreign_keys=ON');
|
||||
}
|
||||
|
||||
const iconDirectory = resolve(process.env.TDR_GAME_ICON_STORAGE_PATH || './data/game-icons');
|
||||
const iconDirectory = resolve(
|
||||
process.env.TDR_GAME_ICON_STORAGE_PATH || './data/game-icons',
|
||||
);
|
||||
await mkdir(iconDirectory, { recursive: true });
|
||||
for (const icon of snapshot.icons) {
|
||||
await writeFile(join(iconDirectory, icon.storageKey), Buffer.from(icon.contentBase64, 'base64'), { mode: 0o600 });
|
||||
await writeFile(
|
||||
join(iconDirectory, icon.storageKey),
|
||||
Buffer.from(icon.contentBase64, 'base64'),
|
||||
{ mode: 0o600 },
|
||||
);
|
||||
}
|
||||
|
||||
console.log(`Imported ${importedRows} ISAC rows and ${snapshot.icons.length} uploaded image assets from ${snapshotPath}.`);
|
||||
console.log(
|
||||
`Imported ${importedRows} ISAC rows and ${snapshot.icons.length} uploaded image assets from ${snapshotPath}.`,
|
||||
);
|
||||
console.log(`Full pre-import backup: ${backupPath}`);
|
||||
console.log(`ISAC pre-import snapshot: ${isacBackupPath}`);
|
||||
|
||||
@@ -1,76 +1,77 @@
|
||||
export const ISAC_DATA_TABLES = [
|
||||
'stat_attributes',
|
||||
'talents',
|
||||
'talent_tiers',
|
||||
'conditional_effect_rules',
|
||||
'conditional_effect_state_pools',
|
||||
'conditional_effect_clauses',
|
||||
'conditional_effect_outcomes',
|
||||
'talent_tier_effects',
|
||||
'talent_description_templates',
|
||||
'talent_weapon_scopes',
|
||||
'weapon_talent_slots',
|
||||
'brand_sets',
|
||||
'brand_set_effects',
|
||||
'gear',
|
||||
'gear_brand_sets',
|
||||
'gear_talents',
|
||||
'gear_tactical_augments',
|
||||
'gear_tactical_augment_costs',
|
||||
'weapons',
|
||||
'core_attribute_tier_ranges',
|
||||
'weapon_tier_ranges',
|
||||
'weapon_talent_options',
|
||||
'weapon_augmentation_effects',
|
||||
'weapon_category_attribute_bonuses',
|
||||
'os_protocols',
|
||||
'os_protocol_primary_effects',
|
||||
'os_protocol_secondary_profiles',
|
||||
'os_protocol_secondary_effects',
|
||||
'os_protocol_talents',
|
||||
'os_protocol_talent_effects',
|
||||
'skill_chip_sets',
|
||||
'skill_chip_two_piece_bonuses',
|
||||
'skill_chip_primary_options',
|
||||
'skill_chip_secondary_options',
|
||||
'skill_chip_talents',
|
||||
'skill_chip_talent_effects',
|
||||
'weapon_mods',
|
||||
'weapon_mod_compatible_categories',
|
||||
'weapon_mod_slots',
|
||||
'weapon_mod_quality_values',
|
||||
'weapon_mod_secondary_options',
|
||||
'weapon_mod_secondary_quality_values',
|
||||
'weapon_mod_exotic_attributes',
|
||||
'weapon_mod_effects',
|
||||
'weapon_mod_effect_quality_values',
|
||||
'gear_mods',
|
||||
'gear_mod_quality_values',
|
||||
'gear_mod_secondary_options',
|
||||
'gear_mod_secondary_quality_values',
|
||||
'specialization_progression_config',
|
||||
'specialization_rank_cost_profiles',
|
||||
'upgrade_resources',
|
||||
'specializations',
|
||||
'specialization_combat_effects',
|
||||
'specialization_focuses',
|
||||
'specialization_abilities',
|
||||
'specialization_ability_attacks',
|
||||
'specialization_ability_effects',
|
||||
'specialization_focus_abilities',
|
||||
'specialization_tree_nodes',
|
||||
'specialization_tree_node_ranks',
|
||||
'specialization_tree_rank_effects',
|
||||
'specialization_tree_rank_costs',
|
||||
'specialization_tree_rank_combat_effects',
|
||||
'specialization_tree_rank_combat_modifiers',
|
||||
'calculation_revisions',
|
||||
'calculation_stages',
|
||||
'attribute_calculation_profiles',
|
||||
'skill_attribute_calculation_profiles',
|
||||
'calculation_calibration_cases',
|
||||
'game_data_icons',
|
||||
'managed_images'
|
||||
'stat_attributes',
|
||||
'talents',
|
||||
'talent_tiers',
|
||||
'conditional_effect_rules',
|
||||
'conditional_effect_state_pools',
|
||||
'conditional_effect_clauses',
|
||||
'conditional_effect_outcomes',
|
||||
'talent_tier_effects',
|
||||
'talent_description_templates',
|
||||
'talent_weapon_scopes',
|
||||
'weapon_talent_slots',
|
||||
'brand_sets',
|
||||
'brand_set_effects',
|
||||
'gear',
|
||||
'gear_brand_sets',
|
||||
'gear_talents',
|
||||
'gear_tactical_augments',
|
||||
'gear_tactical_augment_costs',
|
||||
'weapons',
|
||||
'core_attribute_tier_ranges',
|
||||
'weapon_tier_ranges',
|
||||
'weapon_talent_options',
|
||||
'weapon_augmentation_effects',
|
||||
'weapon_category_attribute_bonuses',
|
||||
'os_protocols',
|
||||
'os_protocol_primary_effects',
|
||||
'os_protocol_secondary_profiles',
|
||||
'os_protocol_secondary_effects',
|
||||
'os_protocol_talents',
|
||||
'os_protocol_talent_effects',
|
||||
'skill_chip_sets',
|
||||
'skill_chip_two_piece_bonuses',
|
||||
'skill_chip_primary_options',
|
||||
'skill_chip_secondary_options',
|
||||
'skill_chip_talents',
|
||||
'skill_chip_talent_effects',
|
||||
'weapon_mods',
|
||||
'weapon_mod_compatible_categories',
|
||||
'weapon_mod_slots',
|
||||
'weapon_mod_quality_values',
|
||||
'weapon_mod_secondary_options',
|
||||
'weapon_mod_secondary_quality_values',
|
||||
'weapon_mod_exotic_attributes',
|
||||
'weapon_mod_effects',
|
||||
'weapon_mod_effect_quality_values',
|
||||
'gear_mods',
|
||||
'gear_mod_quality_values',
|
||||
'gear_mod_secondary_options',
|
||||
'gear_mod_secondary_quality_values',
|
||||
'specialization_progression_config',
|
||||
'specialization_rank_cost_profiles',
|
||||
'upgrade_resources',
|
||||
'specializations',
|
||||
'specialization_combat_effects',
|
||||
'specialization_focuses',
|
||||
'specialization_abilities',
|
||||
'specialization_ability_attacks',
|
||||
'specialization_ability_effects',
|
||||
'specialization_focus_abilities',
|
||||
'specialization_tree_nodes',
|
||||
'specialization_tree_node_ranks',
|
||||
'specialization_tree_rank_effects',
|
||||
'specialization_tree_rank_costs',
|
||||
'specialization_tree_rank_combat_effects',
|
||||
'specialization_tree_rank_combat_modifiers',
|
||||
'calculation_revisions',
|
||||
'calculation_stages',
|
||||
'attribute_calculation_profiles',
|
||||
'skill_attribute_calculation_profiles',
|
||||
'calculation_calibration_cases',
|
||||
'game_data_icons',
|
||||
'managed_images',
|
||||
];
|
||||
|
||||
export const quoteIdentifier = (identifier) => `"${String(identifier).replaceAll('"', '""')}"`;
|
||||
export const quoteIdentifier = (identifier) =>
|
||||
`"${String(identifier).replaceAll('"', '""')}"`;
|
||||
|
||||
@@ -2,28 +2,53 @@ import { connect } from '@tursodatabase/database';
|
||||
import { mkdir, readFile, writeFile } from 'node:fs/promises';
|
||||
import { basename, dirname, join, resolve } from 'node:path';
|
||||
|
||||
const databasePath = resolve(process.env.TDR_DATABASE_PATH || './data/shd-build-development.db');
|
||||
const storageDirectory = resolve(process.env.TDR_GAME_ICON_STORAGE_PATH || './data/game-icons');
|
||||
const timestamp = new Date().toISOString().replaceAll(':', '-').replace(/\.\d{3}Z$/, 'Z');
|
||||
const outputPath = resolve(process.argv[2] || `./data/backups/managed-images-${timestamp}.json`);
|
||||
const databasePath = resolve(
|
||||
process.env.TDR_DATABASE_PATH || './data/shd-build-development.db',
|
||||
);
|
||||
const storageDirectory = resolve(
|
||||
process.env.TDR_GAME_ICON_STORAGE_PATH || './data/game-icons',
|
||||
);
|
||||
const timestamp = new Date()
|
||||
.toISOString()
|
||||
.replaceAll(':', '-')
|
||||
.replace(/\.\d{3}Z$/, 'Z');
|
||||
const outputPath = resolve(
|
||||
process.argv[2] || `./data/backups/managed-images-${timestamp}.json`,
|
||||
);
|
||||
const database = await connect(databasePath);
|
||||
const rows = await (await database.prepare('SELECT * FROM managed_images ORDER BY created_at')).all();
|
||||
const rows = await (
|
||||
await database.prepare('SELECT * FROM managed_images ORDER BY created_at')
|
||||
).all();
|
||||
const images = [];
|
||||
for (const row of rows) {
|
||||
if (typeof row.storage_key !== 'string'
|
||||
|| !/^(?:library|talent|brand_set|os_protocol|skill_chip|specialization|specialization_ability|specialization_node)-[0-9a-f-]{36}\.webp$/i.test(row.storage_key)) {
|
||||
throw new Error(`Invalid managed-image storage key "${String(row.storage_key)}".`);
|
||||
}
|
||||
images.push({ row, contentBase64: (await readFile(join(storageDirectory, row.storage_key))).toString('base64') });
|
||||
if (
|
||||
typeof row.storage_key !== 'string' ||
|
||||
!/^(?:library|talent|brand_set|os_protocol|skill_chip|specialization|specialization_ability|specialization_node)-[0-9a-f-]{36}\.webp$/i.test(
|
||||
row.storage_key,
|
||||
)
|
||||
) {
|
||||
throw new Error(
|
||||
`Invalid managed-image storage key "${String(row.storage_key)}".`,
|
||||
);
|
||||
}
|
||||
images.push({
|
||||
row,
|
||||
contentBase64: (
|
||||
await readFile(join(storageDirectory, row.storage_key))
|
||||
).toString('base64'),
|
||||
});
|
||||
}
|
||||
const bundle = {
|
||||
format: 'shd-build-managed-images',
|
||||
version: 1,
|
||||
createdAt: new Date().toISOString(),
|
||||
sourceDatabase: basename(databasePath),
|
||||
images
|
||||
format: 'shd-build-managed-images',
|
||||
version: 1,
|
||||
createdAt: new Date().toISOString(),
|
||||
sourceDatabase: basename(databasePath),
|
||||
images,
|
||||
};
|
||||
await mkdir(dirname(outputPath), { recursive: true });
|
||||
await writeFile(outputPath, `${JSON.stringify(bundle, null, 2)}\n`, { encoding: 'utf8', flag: 'wx' });
|
||||
await writeFile(outputPath, `${JSON.stringify(bundle, null, 2)}\n`, {
|
||||
encoding: 'utf8',
|
||||
flag: 'wx',
|
||||
});
|
||||
console.log(`Exported ${images.length} managed image assets.`);
|
||||
console.log(`Image bundle: ${outputPath}`);
|
||||
|
||||
@@ -4,47 +4,84 @@ import { mkdir, readFile, writeFile } from 'node:fs/promises';
|
||||
import { join, resolve } from 'node:path';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
|
||||
const bundleArgument = process.argv.slice(2).find((argument) => !argument.startsWith('--'));
|
||||
const bundleArgument = process.argv
|
||||
.slice(2)
|
||||
.find((argument) => !argument.startsWith('--'));
|
||||
if (!bundleArgument || !process.argv.includes('--confirm-merge')) {
|
||||
console.error('Usage: node scripts/managed-images-import.mjs <bundle.json> --confirm-merge [--update-existing]');
|
||||
process.exit(2);
|
||||
console.error(
|
||||
'Usage: node scripts/managed-images-import.mjs <bundle.json> --confirm-merge [--update-existing]',
|
||||
);
|
||||
process.exit(2);
|
||||
}
|
||||
const updateExisting = process.argv.includes('--update-existing');
|
||||
const bundlePath = resolve(bundleArgument);
|
||||
const bundle = JSON.parse(await readFile(bundlePath, 'utf8'));
|
||||
if (bundle?.format !== 'shd-build-managed-images' || bundle?.version !== 1 || !Array.isArray(bundle.images)) {
|
||||
throw new Error('The selected file is not a supported SHD.build managed-image bundle.');
|
||||
if (
|
||||
bundle?.format !== 'shd-build-managed-images' ||
|
||||
bundle?.version !== 1 ||
|
||||
!Array.isArray(bundle.images)
|
||||
) {
|
||||
throw new Error(
|
||||
'The selected file is not a supported SHD.build managed-image bundle.',
|
||||
);
|
||||
}
|
||||
for (const image of bundle.images) {
|
||||
if (typeof image?.row?.id !== 'string' || !/^[0-9a-f-]{36}$/i.test(image.row.id)
|
||||
|| typeof image.row.storage_key !== 'string'
|
||||
|| !/^(?:library|talent|brand_set|os_protocol|skill_chip|specialization|specialization_ability|specialization_node)-[0-9a-f-]{36}\.webp$/i.test(image.row.storage_key)
|
||||
|| typeof image.contentBase64 !== 'string') {
|
||||
throw new Error('The bundle contains an invalid managed image.');
|
||||
}
|
||||
const isItemRender = ['weapon_render', 'gear_render'].includes(image.row.source_entity_type);
|
||||
if (image.row.source_entity_type && (
|
||||
typeof image.row.source_entity_id !== 'string'
|
||||
|| !/^[0-9a-f-]{36}$/i.test(image.row.source_entity_id)
|
||||
|| (isItemRender
|
||||
? !image.row.storage_key.startsWith('library-')
|
||||
: !image.row.storage_key.startsWith(`${image.row.source_entity_type}-`))
|
||||
)) throw new Error('The bundle contains an invalid linked ISAC image.');
|
||||
if (
|
||||
typeof image?.row?.id !== 'string' ||
|
||||
!/^[0-9a-f-]{36}$/i.test(image.row.id) ||
|
||||
typeof image.row.storage_key !== 'string' ||
|
||||
!/^(?:library|talent|brand_set|os_protocol|skill_chip|specialization|specialization_ability|specialization_node)-[0-9a-f-]{36}\.webp$/i.test(
|
||||
image.row.storage_key,
|
||||
) ||
|
||||
typeof image.contentBase64 !== 'string'
|
||||
) {
|
||||
throw new Error('The bundle contains an invalid managed image.');
|
||||
}
|
||||
const isItemRender = ['weapon_render', 'gear_render'].includes(
|
||||
image.row.source_entity_type,
|
||||
);
|
||||
if (
|
||||
image.row.source_entity_type &&
|
||||
(typeof image.row.source_entity_id !== 'string' ||
|
||||
!/^[0-9a-f-]{36}$/i.test(image.row.source_entity_id) ||
|
||||
(isItemRender
|
||||
? !image.row.storage_key.startsWith('library-')
|
||||
: !image.row.storage_key.startsWith(
|
||||
`${image.row.source_entity_type}-`,
|
||||
)))
|
||||
)
|
||||
throw new Error('The bundle contains an invalid linked ISAC image.');
|
||||
}
|
||||
|
||||
const backup = spawnSync(process.execPath, ['scripts/db-backup.mjs'], {
|
||||
cwd: process.cwd(), stdio: 'inherit', env: process.env
|
||||
cwd: process.cwd(),
|
||||
stdio: 'inherit',
|
||||
env: process.env,
|
||||
});
|
||||
if (backup.error) throw backup.error;
|
||||
if (backup.status !== 0) throw new Error('Safety backup failed; no managed images were imported.');
|
||||
const assetBackup = spawnSync(process.execPath, ['scripts/isac-data-export.mjs'], {
|
||||
cwd: process.cwd(), stdio: 'inherit', env: process.env
|
||||
});
|
||||
if (backup.status !== 0)
|
||||
throw new Error('Safety backup failed; no managed images were imported.');
|
||||
const assetBackup = spawnSync(
|
||||
process.execPath,
|
||||
['scripts/isac-data-export.mjs'],
|
||||
{
|
||||
cwd: process.cwd(),
|
||||
stdio: 'inherit',
|
||||
env: process.env,
|
||||
},
|
||||
);
|
||||
if (assetBackup.error) throw assetBackup.error;
|
||||
if (assetBackup.status !== 0) throw new Error('Uploaded-asset safety snapshot failed; no managed images were imported.');
|
||||
if (assetBackup.status !== 0)
|
||||
throw new Error(
|
||||
'Uploaded-asset safety snapshot failed; no managed images were imported.',
|
||||
);
|
||||
|
||||
const databasePath = resolve(process.env.TDR_DATABASE_PATH || './data/shd-build-development.db');
|
||||
const storageDirectory = resolve(process.env.TDR_GAME_ICON_STORAGE_PATH || './data/game-icons');
|
||||
const databasePath = resolve(
|
||||
process.env.TDR_DATABASE_PATH || './data/shd-build-development.db',
|
||||
);
|
||||
const storageDirectory = resolve(
|
||||
process.env.TDR_GAME_ICON_STORAGE_PATH || './data/game-icons',
|
||||
);
|
||||
const database = await connect(databasePath);
|
||||
await mkdir(storageDirectory, { recursive: true });
|
||||
let inserted = 0;
|
||||
@@ -53,64 +90,140 @@ let unchanged = 0;
|
||||
|
||||
const preflight = [];
|
||||
for (const image of bundle.images) {
|
||||
const row = image.row;
|
||||
const existing = await (await database.prepare('SELECT * FROM managed_images WHERE id=?')).get(row.id);
|
||||
if (existing && !updateExisting) {
|
||||
const same = ['name','category','tags_json','is_icon','force_white','storage_key','byte_size','width','height','source_entity_type','source_entity_id']
|
||||
.every((key) => String(existing[key]) === String(row[key]));
|
||||
if (!same) throw new Error(`Image ${row.id} already exists with different metadata. Re-run with --update-existing after review.`);
|
||||
unchanged++;
|
||||
continue;
|
||||
}
|
||||
const creator = await (await database.prepare('SELECT id FROM users WHERE id=?')).get(row.created_by);
|
||||
if (!creator) throw new Error(`Image "${row.name}" references a creator that does not exist in the target database.`);
|
||||
const nameConflict = await (await database.prepare('SELECT id FROM managed_images WHERE name=? COLLATE NOCASE AND id<>?')).get(row.name, row.id);
|
||||
if (nameConflict) throw new Error(`Image name "${row.name}" is already assigned to a different target asset.`);
|
||||
if (row.source_entity_type && row.source_entity_id) {
|
||||
const sourceConflict = await (await database.prepare(
|
||||
'SELECT id FROM managed_images WHERE source_entity_type=? AND source_entity_id=? AND id<>?'
|
||||
)).get(row.source_entity_type, row.source_entity_id, row.id);
|
||||
if (sourceConflict) throw new Error(`The ${row.source_entity_type} image ${row.source_entity_id} is already registered under a different library asset.`);
|
||||
}
|
||||
preflight.push({ image, existing });
|
||||
const row = image.row;
|
||||
const existing = await (
|
||||
await database.prepare('SELECT * FROM managed_images WHERE id=?')
|
||||
).get(row.id);
|
||||
if (existing && !updateExisting) {
|
||||
const same = [
|
||||
'name',
|
||||
'category',
|
||||
'tags_json',
|
||||
'is_icon',
|
||||
'force_white',
|
||||
'storage_key',
|
||||
'byte_size',
|
||||
'width',
|
||||
'height',
|
||||
'source_entity_type',
|
||||
'source_entity_id',
|
||||
].every((key) => String(existing[key]) === String(row[key]));
|
||||
if (!same)
|
||||
throw new Error(
|
||||
`Image ${row.id} already exists with different metadata. Re-run with --update-existing after review.`,
|
||||
);
|
||||
unchanged++;
|
||||
continue;
|
||||
}
|
||||
const creator = await (
|
||||
await database.prepare('SELECT id FROM users WHERE id=?')
|
||||
).get(row.created_by);
|
||||
if (!creator)
|
||||
throw new Error(
|
||||
`Image "${row.name}" references a creator that does not exist in the target database.`,
|
||||
);
|
||||
const nameConflict = await (
|
||||
await database.prepare(
|
||||
'SELECT id FROM managed_images WHERE name=? COLLATE NOCASE AND id<>?',
|
||||
)
|
||||
).get(row.name, row.id);
|
||||
if (nameConflict)
|
||||
throw new Error(
|
||||
`Image name "${row.name}" is already assigned to a different target asset.`,
|
||||
);
|
||||
if (row.source_entity_type && row.source_entity_id) {
|
||||
const sourceConflict = await (
|
||||
await database.prepare(
|
||||
'SELECT id FROM managed_images WHERE source_entity_type=? AND source_entity_id=? AND id<>?',
|
||||
)
|
||||
).get(row.source_entity_type, row.source_entity_id, row.id);
|
||||
if (sourceConflict)
|
||||
throw new Error(
|
||||
`The ${row.source_entity_type} image ${row.source_entity_id} is already registered under a different library asset.`,
|
||||
);
|
||||
}
|
||||
preflight.push({ image, existing });
|
||||
}
|
||||
|
||||
await database.exec('BEGIN');
|
||||
try {
|
||||
for (const { image, existing } of preflight) {
|
||||
const row = image.row;
|
||||
await writeFile(join(storageDirectory, row.storage_key), Buffer.from(image.contentBase64, 'base64'), {
|
||||
mode: 0o600, flag: existing || row.source_entity_type ? 'w' : 'wx'
|
||||
});
|
||||
if (existing) {
|
||||
await (await database.prepare(`UPDATE managed_images SET
|
||||
for (const { image, existing } of preflight) {
|
||||
const row = image.row;
|
||||
await writeFile(
|
||||
join(storageDirectory, row.storage_key),
|
||||
Buffer.from(image.contentBase64, 'base64'),
|
||||
{
|
||||
mode: 0o600,
|
||||
flag: existing || row.source_entity_type ? 'w' : 'wx',
|
||||
},
|
||||
);
|
||||
if (existing) {
|
||||
await (
|
||||
await database.prepare(`UPDATE managed_images SET
|
||||
name=?,category=?,tags_json=?,is_icon=?,force_white=?,storage_key=?,original_filename=?,
|
||||
byte_size=?,width=?,height=?,source_entity_type=?,source_entity_id=?,updated_at=? WHERE id=?`)).run(
|
||||
row.name,row.category,row.tags_json,row.is_icon,row.force_white,row.storage_key,row.original_filename,
|
||||
row.byte_size,row.width,row.height,row.source_entity_type ?? null,row.source_entity_id ?? null,row.updated_at,row.id
|
||||
);
|
||||
updated++;
|
||||
} else {
|
||||
await (await database.prepare(`INSERT INTO managed_images
|
||||
byte_size=?,width=?,height=?,source_entity_type=?,source_entity_id=?,updated_at=? WHERE id=?`)
|
||||
).run(
|
||||
row.name,
|
||||
row.category,
|
||||
row.tags_json,
|
||||
row.is_icon,
|
||||
row.force_white,
|
||||
row.storage_key,
|
||||
row.original_filename,
|
||||
row.byte_size,
|
||||
row.width,
|
||||
row.height,
|
||||
row.source_entity_type ?? null,
|
||||
row.source_entity_id ?? null,
|
||||
row.updated_at,
|
||||
row.id,
|
||||
);
|
||||
updated++;
|
||||
} else {
|
||||
await (
|
||||
await database.prepare(`INSERT INTO managed_images
|
||||
(id,name,category,tags_json,is_icon,force_white,storage_key,original_filename,byte_size,width,height,
|
||||
source_entity_type,source_entity_id,created_by,created_at,updated_at)
|
||||
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`)).run(
|
||||
row.id,row.name,row.category,row.tags_json,row.is_icon,row.force_white,row.storage_key,row.original_filename,
|
||||
row.byte_size,row.width,row.height,row.source_entity_type ?? null,row.source_entity_id ?? null,
|
||||
row.created_by,row.created_at,row.updated_at
|
||||
);
|
||||
inserted++;
|
||||
}
|
||||
await (await database.prepare(`INSERT INTO audit_log
|
||||
(id,actor_id,entity_type,entity_id,action,before_json,after_json,created_at) VALUES(?,?,?,?,?,?,?,?)`)).run(
|
||||
randomUUID(), row.created_by, 'system_configuration', row.id,
|
||||
existing ? 'image.import_updated' : 'image.imported',
|
||||
existing ? JSON.stringify(existing) : null, JSON.stringify(row), new Date().toISOString()
|
||||
);
|
||||
}
|
||||
await database.exec('COMMIT');
|
||||
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`)
|
||||
).run(
|
||||
row.id,
|
||||
row.name,
|
||||
row.category,
|
||||
row.tags_json,
|
||||
row.is_icon,
|
||||
row.force_white,
|
||||
row.storage_key,
|
||||
row.original_filename,
|
||||
row.byte_size,
|
||||
row.width,
|
||||
row.height,
|
||||
row.source_entity_type ?? null,
|
||||
row.source_entity_id ?? null,
|
||||
row.created_by,
|
||||
row.created_at,
|
||||
row.updated_at,
|
||||
);
|
||||
inserted++;
|
||||
}
|
||||
await (
|
||||
await database.prepare(`INSERT INTO audit_log
|
||||
(id,actor_id,entity_type,entity_id,action,before_json,after_json,created_at) VALUES(?,?,?,?,?,?,?,?)`)
|
||||
).run(
|
||||
randomUUID(),
|
||||
row.created_by,
|
||||
'system_configuration',
|
||||
row.id,
|
||||
existing ? 'image.import_updated' : 'image.imported',
|
||||
existing ? JSON.stringify(existing) : null,
|
||||
JSON.stringify(row),
|
||||
new Date().toISOString(),
|
||||
);
|
||||
}
|
||||
await database.exec('COMMIT');
|
||||
} catch (error) {
|
||||
await database.exec('ROLLBACK');
|
||||
throw error;
|
||||
await database.exec('ROLLBACK');
|
||||
throw error;
|
||||
}
|
||||
console.log(`Managed-image merge complete: ${inserted} inserted, ${updated} updated, ${unchanged} unchanged.`);
|
||||
console.log(
|
||||
`Managed-image merge complete: ${inserted} inserted, ${updated} updated, ${unchanged} unchanged.`,
|
||||
);
|
||||
|
||||
@@ -2,40 +2,78 @@ import { connect } from '@tursodatabase/database';
|
||||
import nodemailer from 'nodemailer';
|
||||
import { resolve } from 'node:path';
|
||||
|
||||
if (!process.env.SMTP_HOST || !process.env.SMTP_FROM) throw new Error('SMTP delivery is not configured.');
|
||||
const database = await connect(resolve(process.env.TDR_DATABASE_PATH || './data/shd-build.db'));
|
||||
if (!process.env.SMTP_HOST || !process.env.SMTP_FROM)
|
||||
throw new Error('SMTP delivery is not configured.');
|
||||
const database = await connect(
|
||||
resolve(process.env.TDR_DATABASE_PATH || './data/shd-build.db'),
|
||||
);
|
||||
const transport = nodemailer.createTransport({
|
||||
host: process.env.SMTP_HOST,
|
||||
port: Number(process.env.SMTP_PORT || 587),
|
||||
secure: process.env.SMTP_SECURE === 'true',
|
||||
auth: process.env.SMTP_USER ? { user: process.env.SMTP_USER, pass: process.env.SMTP_PASSWORD } : undefined,
|
||||
requireTLS: process.env.SMTP_SECURE !== 'true',
|
||||
disableFileAccess: true,
|
||||
disableUrlAccess: true
|
||||
host: process.env.SMTP_HOST,
|
||||
port: Number(process.env.SMTP_PORT || 587),
|
||||
secure: process.env.SMTP_SECURE === 'true',
|
||||
auth: process.env.SMTP_USER
|
||||
? { user: process.env.SMTP_USER, pass: process.env.SMTP_PASSWORD }
|
||||
: undefined,
|
||||
requireTLS: process.env.SMTP_SECURE !== 'true',
|
||||
disableFileAccess: true,
|
||||
disableUrlAccess: true,
|
||||
});
|
||||
const origin = (process.env.APP_ORIGIN || 'http://localhost:5173').replace(/\/+$/, '');
|
||||
const recipients = await (await database.prepare(`SELECT DISTINCT n.user_id,e.email
|
||||
const origin = (process.env.APP_ORIGIN || 'http://localhost:5173').replace(
|
||||
/\/+$/,
|
||||
'',
|
||||
);
|
||||
const recipients = await (
|
||||
await database.prepare(`SELECT DISTINCT n.user_id,e.email
|
||||
FROM notifications n
|
||||
JOIN notification_preferences p ON p.user_id=n.user_id AND p.category=n.category AND p.email_frequency='daily'
|
||||
JOIN user_emails e ON e.user_id=n.user_id
|
||||
WHERE n.emailed_at IS NULL AND n.created_at>=p.updated_at`)).all();
|
||||
WHERE n.emailed_at IS NULL AND n.created_at>=p.updated_at`)
|
||||
).all();
|
||||
let sent = 0;
|
||||
for (const recipient of recipients) {
|
||||
const items = await (await database.prepare(`SELECT n.id,n.title,n.body,n.link FROM notifications n
|
||||
const items = await (
|
||||
await database.prepare(`SELECT n.id,n.title,n.body,n.link FROM notifications n
|
||||
JOIN notification_preferences p ON p.user_id=n.user_id AND p.category=n.category AND p.email_frequency='daily'
|
||||
WHERE n.user_id=? AND n.emailed_at IS NULL AND n.created_at>=p.updated_at ORDER BY n.created_at LIMIT 100`)).all(recipient.user_id);
|
||||
if (!items.length) continue;
|
||||
const escape = (value) => String(value).replace(/[&<>"']/g, (character) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[character]);
|
||||
const rows = items.map((item) => `<div style="border-top:1px solid #34393a;padding:16px 0"><strong>${escape(item.title)}</strong><p style="color:#a5aaab">${escape(item.body)}</p>${item.link ? `<a style="color:#f6a821" href="${origin}${item.link}">VIEW INTELLIGENCE</a>` : ''}</div>`).join('');
|
||||
await transport.sendMail({
|
||||
from: process.env.SMTP_FROM,
|
||||
to: recipient.email,
|
||||
subject: `SHD.build daily notification summary · ${items.length} update${items.length === 1 ? '' : 's'}`,
|
||||
text: items.map((item) => `${item.title}\n${item.body}\n${item.link ? `${origin}${item.link}` : ''}`).join('\n\n'),
|
||||
html: `<div style="background:#0a0c0d;color:#ecebe5;padding:28px;font-family:Arial,sans-serif"><p style="color:#f6a821;font-size:12px;letter-spacing:2px">SHD DAILY INTELLIGENCE</p><h1>${items.length} notification${items.length === 1 ? '' : 's'}</h1>${rows}</div>`
|
||||
});
|
||||
const now = new Date().toISOString();
|
||||
for (const item of items) await (await database.prepare('UPDATE notifications SET emailed_at=? WHERE id=? AND emailed_at IS NULL')).run(now, item.id);
|
||||
sent += 1;
|
||||
WHERE n.user_id=? AND n.emailed_at IS NULL AND n.created_at>=p.updated_at ORDER BY n.created_at LIMIT 100`)
|
||||
).all(recipient.user_id);
|
||||
if (!items.length) continue;
|
||||
const escape = (value) =>
|
||||
String(value).replace(
|
||||
/[&<>"']/g,
|
||||
(character) =>
|
||||
({
|
||||
'&': '&',
|
||||
'<': '<',
|
||||
'>': '>',
|
||||
'"': '"',
|
||||
"'": ''',
|
||||
})[character],
|
||||
);
|
||||
const rows = items
|
||||
.map(
|
||||
(item) =>
|
||||
`<div style="border-top:1px solid #34393a;padding:16px 0"><strong>${escape(item.title)}</strong><p style="color:#a5aaab">${escape(item.body)}</p>${item.link ? `<a style="color:#f6a821" href="${origin}${item.link}">VIEW INTELLIGENCE</a>` : ''}</div>`,
|
||||
)
|
||||
.join('');
|
||||
await transport.sendMail({
|
||||
from: process.env.SMTP_FROM,
|
||||
to: recipient.email,
|
||||
subject: `SHD.build daily notification summary · ${items.length} update${items.length === 1 ? '' : 's'}`,
|
||||
text: items
|
||||
.map(
|
||||
(item) =>
|
||||
`${item.title}\n${item.body}\n${item.link ? `${origin}${item.link}` : ''}`,
|
||||
)
|
||||
.join('\n\n'),
|
||||
html: `<div style="background:#0a0c0d;color:#ecebe5;padding:28px;font-family:Arial,sans-serif"><p style="color:#f6a821;font-size:12px;letter-spacing:2px">SHD DAILY INTELLIGENCE</p><h1>${items.length} notification${items.length === 1 ? '' : 's'}</h1>${rows}</div>`,
|
||||
});
|
||||
const now = new Date().toISOString();
|
||||
for (const item of items)
|
||||
await (
|
||||
await database.prepare(
|
||||
'UPDATE notifications SET emailed_at=? WHERE id=? AND emailed_at IS NULL',
|
||||
)
|
||||
).run(now, item.id);
|
||||
sent += 1;
|
||||
}
|
||||
console.log(`Sent ${sent} daily notification digest${sent === 1 ? '' : 's'}.`);
|
||||
|
||||
@@ -3,67 +3,100 @@ import nodemailer from 'nodemailer';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { resolve } from 'node:path';
|
||||
|
||||
const database = await connect(resolve(process.env.TDR_DATABASE_PATH || './data/shd-build.db'));
|
||||
const database = await connect(
|
||||
resolve(process.env.TDR_DATABASE_PATH || './data/shd-build.db'),
|
||||
);
|
||||
const now = new Date().toISOString();
|
||||
const due = await (await database.prepare(`SELECT * FROM oversight_notification_queue
|
||||
WHERE delivered_at IS NULL AND deliver_after<=? ORDER BY user_id,oversight_category,created_at`)).all(now);
|
||||
const due = await (
|
||||
await database.prepare(`SELECT * FROM oversight_notification_queue
|
||||
WHERE delivered_at IS NULL AND deliver_after<=? ORDER BY user_id,oversight_category,created_at`)
|
||||
).all(now);
|
||||
const groups = new Map();
|
||||
for (const item of due) {
|
||||
const key = `${item.user_id}:${item.oversight_category}:${item.frequency}:${item.deliver_after}`;
|
||||
if (!groups.has(key)) groups.set(key, []);
|
||||
groups.get(key).push(item);
|
||||
const key = `${item.user_id}:${item.oversight_category}:${item.frequency}:${item.deliver_after}`;
|
||||
if (!groups.has(key)) groups.set(key, []);
|
||||
groups.get(key).push(item);
|
||||
}
|
||||
|
||||
const smtpConfigured = Boolean(process.env.SMTP_HOST && process.env.SMTP_FROM);
|
||||
const transport = smtpConfigured ? nodemailer.createTransport({
|
||||
host: process.env.SMTP_HOST,
|
||||
port: Number(process.env.SMTP_PORT || 587),
|
||||
secure: process.env.SMTP_SECURE === 'true',
|
||||
auth: process.env.SMTP_USER ? { user: process.env.SMTP_USER, pass: process.env.SMTP_PASSWORD } : undefined,
|
||||
requireTLS: process.env.SMTP_SECURE !== 'true',
|
||||
disableFileAccess: true,
|
||||
disableUrlAccess: true
|
||||
}) : null;
|
||||
const origin = (process.env.APP_ORIGIN || 'http://localhost:5173').replace(/\/+$/, '');
|
||||
const transport = smtpConfigured
|
||||
? nodemailer.createTransport({
|
||||
host: process.env.SMTP_HOST,
|
||||
port: Number(process.env.SMTP_PORT || 587),
|
||||
secure: process.env.SMTP_SECURE === 'true',
|
||||
auth: process.env.SMTP_USER
|
||||
? { user: process.env.SMTP_USER, pass: process.env.SMTP_PASSWORD }
|
||||
: undefined,
|
||||
requireTLS: process.env.SMTP_SECURE !== 'true',
|
||||
disableFileAccess: true,
|
||||
disableUrlAccess: true,
|
||||
})
|
||||
: null;
|
||||
const origin = (process.env.APP_ORIGIN || 'http://localhost:5173').replace(
|
||||
/\/+$/,
|
||||
'',
|
||||
);
|
||||
let delivered = 0;
|
||||
|
||||
for (const items of groups.values()) {
|
||||
const first = items[0];
|
||||
const categoryLabel = first.oversight_category === 'avatars' ? 'avatar' : 'data change';
|
||||
const title = `${items.length} ${categoryLabel} review${items.length === 1 ? '' : 's'} awaiting Oversight`;
|
||||
const details = items.slice(0, 10).map((item) => `• ${item.summary}`).join('\n');
|
||||
const remainder = items.length > 10 ? `\n• ${items.length - 10} additional review items` : '';
|
||||
const body = `${details}${remainder}`;
|
||||
const notificationId = randomUUID();
|
||||
await (await database.prepare(`INSERT INTO notifications
|
||||
const first = items[0];
|
||||
const categoryLabel =
|
||||
first.oversight_category === 'avatars' ? 'avatar' : 'data change';
|
||||
const title = `${items.length} ${categoryLabel} review${items.length === 1 ? '' : 's'} awaiting Oversight`;
|
||||
const details = items
|
||||
.slice(0, 10)
|
||||
.map((item) => `• ${item.summary}`)
|
||||
.join('\n');
|
||||
const remainder =
|
||||
items.length > 10 ? `\n• ${items.length - 10} additional review items` : '';
|
||||
const body = `${details}${remainder}`;
|
||||
const notificationId = randomUUID();
|
||||
await (
|
||||
await database.prepare(`INSERT INTO notifications
|
||||
(id,user_id,category,source,title,body,link,mandatory,created_at)
|
||||
VALUES(?,?,'isac','ISAC OVERSIGHT NETWORK',?,?,?,1,?)`)).run(
|
||||
notificationId, first.user_id, title, body, first.link, now
|
||||
);
|
||||
for (const item of items) {
|
||||
await (await database.prepare('UPDATE oversight_notification_queue SET delivered_at=? WHERE id=? AND delivered_at IS NULL')).run(now, item.id);
|
||||
}
|
||||
const preference = await (await database.prepare(
|
||||
"SELECT email_frequency FROM notification_preferences WHERE user_id=? AND category='isac'"
|
||||
)).get(first.user_id);
|
||||
if (transport && preference?.email_frequency === 'immediate') {
|
||||
const identity = await (await database.prepare('SELECT email FROM user_emails WHERE user_id=?')).get(first.user_id);
|
||||
if (typeof identity?.email === 'string') {
|
||||
try {
|
||||
await transport.sendMail({
|
||||
from: process.env.SMTP_FROM,
|
||||
to: identity.email,
|
||||
subject: `SHD.build · ${title}`,
|
||||
text: `${body}\n\n${origin}${first.link}`,
|
||||
html: `<div style="background:#0a0c0d;color:#ecebe5;padding:28px;font-family:Arial,sans-serif"><p style="color:#f6a821;font-size:12px;letter-spacing:2px">ISAC OVERSIGHT NETWORK</p><h1>${title}</h1><p style="white-space:pre-line;color:#a5aaab">${body}</p><a style="color:#f6a821" href="${origin}${first.link}">OPEN OVERSIGHT QUEUE</a></div>`
|
||||
});
|
||||
await (await database.prepare('UPDATE notifications SET emailed_at=? WHERE id=?')).run(now, notificationId);
|
||||
} catch (error) {
|
||||
console.error(`Unable to email Oversight digest ${notificationId}`, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
delivered += items.length;
|
||||
VALUES(?,?,'isac','ISAC OVERSIGHT NETWORK',?,?,?,1,?)`)
|
||||
).run(notificationId, first.user_id, title, body, first.link, now);
|
||||
for (const item of items) {
|
||||
await (
|
||||
await database.prepare(
|
||||
'UPDATE oversight_notification_queue SET delivered_at=? WHERE id=? AND delivered_at IS NULL',
|
||||
)
|
||||
).run(now, item.id);
|
||||
}
|
||||
const preference = await (
|
||||
await database.prepare(
|
||||
"SELECT email_frequency FROM notification_preferences WHERE user_id=? AND category='isac'",
|
||||
)
|
||||
).get(first.user_id);
|
||||
if (transport && preference?.email_frequency === 'immediate') {
|
||||
const identity = await (
|
||||
await database.prepare('SELECT email FROM user_emails WHERE user_id=?')
|
||||
).get(first.user_id);
|
||||
if (typeof identity?.email === 'string') {
|
||||
try {
|
||||
await transport.sendMail({
|
||||
from: process.env.SMTP_FROM,
|
||||
to: identity.email,
|
||||
subject: `SHD.build · ${title}`,
|
||||
text: `${body}\n\n${origin}${first.link}`,
|
||||
html: `<div style="background:#0a0c0d;color:#ecebe5;padding:28px;font-family:Arial,sans-serif"><p style="color:#f6a821;font-size:12px;letter-spacing:2px">ISAC OVERSIGHT NETWORK</p><h1>${title}</h1><p style="white-space:pre-line;color:#a5aaab">${body}</p><a style="color:#f6a821" href="${origin}${first.link}">OPEN OVERSIGHT QUEUE</a></div>`,
|
||||
});
|
||||
await (
|
||||
await database.prepare(
|
||||
'UPDATE notifications SET emailed_at=? WHERE id=?',
|
||||
)
|
||||
).run(now, notificationId);
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`Unable to email Oversight digest ${notificationId}`,
|
||||
error,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
delivered += items.length;
|
||||
}
|
||||
|
||||
console.log(`Delivered ${delivered} queued Oversight review alert${delivered === 1 ? '' : 's'} in ${groups.size} digest${groups.size === 1 ? '' : 's'}.`);
|
||||
console.log(
|
||||
`Delivered ${delivered} queued Oversight review alert${delivered === 1 ? '' : 's'} in ${groups.size} digest${groups.size === 1 ? '' : 's'}.`,
|
||||
);
|
||||
|
||||
@@ -6,35 +6,47 @@ import sharp from 'sharp';
|
||||
|
||||
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
||||
const outputDirectory = path.join(root, 'static', 'brand');
|
||||
const fontDirectory = path.join(root, 'node_modules', '@fontsource', 'barlow', 'files');
|
||||
const heavy = opentype.parse(fs.readFileSync(path.join(fontDirectory, 'barlow-latin-900-normal.woff')).buffer);
|
||||
const medium = opentype.parse(fs.readFileSync(path.join(fontDirectory, 'barlow-latin-700-normal.woff')).buffer);
|
||||
const fontDirectory = path.join(
|
||||
root,
|
||||
'node_modules',
|
||||
'@fontsource',
|
||||
'barlow',
|
||||
'files',
|
||||
);
|
||||
const heavy = opentype.parse(
|
||||
fs.readFileSync(path.join(fontDirectory, 'barlow-latin-900-normal.woff'))
|
||||
.buffer,
|
||||
);
|
||||
const medium = opentype.parse(
|
||||
fs.readFileSync(path.join(fontDirectory, 'barlow-latin-700-normal.woff'))
|
||||
.buffer,
|
||||
);
|
||||
|
||||
fs.mkdirSync(outputDirectory, { recursive: true });
|
||||
|
||||
const colors = {
|
||||
background: '#0a0c0d',
|
||||
surface: '#111415',
|
||||
border: '#303536',
|
||||
grid: '#2a3031',
|
||||
orange: '#f6a821',
|
||||
white: '#ecebe5'
|
||||
background: '#0a0c0d',
|
||||
surface: '#111415',
|
||||
border: '#303536',
|
||||
grid: '#2a3031',
|
||||
orange: '#f6a821',
|
||||
white: '#ecebe5',
|
||||
};
|
||||
|
||||
function trackedPath(font, value, x, baseline, size, tracking = 0) {
|
||||
let cursor = x;
|
||||
let data = '';
|
||||
for (const character of value) {
|
||||
const glyph = font.charToGlyph(character);
|
||||
data += glyph.getPath(cursor, baseline, size).toPathData(2);
|
||||
cursor += glyph.advanceWidth * size / font.unitsPerEm + tracking;
|
||||
}
|
||||
return { data, width: cursor - x - tracking };
|
||||
let cursor = x;
|
||||
let data = '';
|
||||
for (const character of value) {
|
||||
const glyph = font.charToGlyph(character);
|
||||
data += glyph.getPath(cursor, baseline, size).toPathData(2);
|
||||
cursor += (glyph.advanceWidth * size) / font.unitsPerEm + tracking;
|
||||
}
|
||||
return { data, width: cursor - x - tracking };
|
||||
}
|
||||
|
||||
function emblem(x, y, size) {
|
||||
const scale = size / 100;
|
||||
return `
|
||||
const scale = size / 100;
|
||||
return `
|
||||
<g transform="translate(${x} ${y}) scale(${scale})">
|
||||
<g fill="none" stroke="${colors.orange}" stroke-width="12" stroke-linecap="butt">
|
||||
<path d="M73 20A38 38 0 0 0 15 63"/>
|
||||
@@ -47,10 +59,10 @@ function emblem(x, y, size) {
|
||||
}
|
||||
|
||||
async function writePng(svg, filename, width, height) {
|
||||
await sharp(Buffer.from(svg))
|
||||
.resize(width, height, { fit: 'fill' })
|
||||
.png({ compressionLevel: 9, adaptiveFiltering: true })
|
||||
.toFile(path.join(outputDirectory, filename));
|
||||
await sharp(Buffer.from(svg))
|
||||
.resize(width, height, { fit: 'fill' })
|
||||
.png({ compressionLevel: 9, adaptiveFiltering: true })
|
||||
.toFile(path.join(outputDirectory, filename));
|
||||
}
|
||||
|
||||
const transparentLogo = `
|
||||
@@ -154,10 +166,17 @@ const inviteSplash = `
|
||||
await writePng(transparentLogo, 'shd-core-segments.png', 1024, 1024);
|
||||
await writePng(serverIcon, 'shd-build-discord-icon.png', 1024, 1024);
|
||||
await writePng(banner, 'shd-build-discord-banner-1920x1080.png', 1920, 1080);
|
||||
await writePng(inviteSplash, 'shd-build-discord-invite-background-1920x1080.png', 1920, 1080);
|
||||
await sharp(path.join(outputDirectory, 'shd-build-discord-banner-1920x1080.png'))
|
||||
.resize(960, 540)
|
||||
.png({ compressionLevel: 9, adaptiveFiltering: true })
|
||||
.toFile(path.join(outputDirectory, 'shd-build-discord-banner-960x540.png'));
|
||||
await writePng(
|
||||
inviteSplash,
|
||||
'shd-build-discord-invite-background-1920x1080.png',
|
||||
1920,
|
||||
1080,
|
||||
);
|
||||
await sharp(
|
||||
path.join(outputDirectory, 'shd-build-discord-banner-1920x1080.png'),
|
||||
)
|
||||
.resize(960, 540)
|
||||
.png({ compressionLevel: 9, adaptiveFiltering: true })
|
||||
.toFile(path.join(outputDirectory, 'shd-build-discord-banner-960x540.png'));
|
||||
|
||||
console.log(`Rendered brand assets to ${outputDirectory}`);
|
||||
|
||||
@@ -1,17 +1,19 @@
|
||||
const endpoint = process.env.SCHEDULER_URL;
|
||||
const token = process.env.SCHEDULER_TOKEN;
|
||||
if (!endpoint || !token) throw new Error('SCHEDULER_URL and SCHEDULER_TOKEN are required.');
|
||||
if (!endpoint || !token)
|
||||
throw new Error('SCHEDULER_URL and SCHEDULER_TOKEN are required.');
|
||||
|
||||
while (true) {
|
||||
try {
|
||||
const response = await fetch(endpoint, {
|
||||
method: 'POST',
|
||||
headers: { authorization: `Bearer ${token}` }
|
||||
});
|
||||
if (!response.ok) throw new Error(`Scheduler endpoint returned HTTP ${response.status}.`);
|
||||
console.log(`Scheduler tick: ${JSON.stringify(await response.json())}`);
|
||||
} catch (error) {
|
||||
console.error('Scheduler tick failed', error);
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, 60_000));
|
||||
try {
|
||||
const response = await fetch(endpoint, {
|
||||
method: 'POST',
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
if (!response.ok)
|
||||
throw new Error(`Scheduler endpoint returned HTTP ${response.status}.`);
|
||||
console.log(`Scheduler tick: ${JSON.stringify(await response.json())}`);
|
||||
} catch (error) {
|
||||
console.error('Scheduler tick failed', error);
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, 60_000));
|
||||
}
|
||||
|
||||
@@ -3,7 +3,9 @@ import { resolve } from 'node:path';
|
||||
import process from 'node:process';
|
||||
import { initializeDefaultUsers } from './default-users.mjs';
|
||||
|
||||
const databasePath = resolve(process.env.TDR_DATABASE_PATH || './data/shd-build-development.db');
|
||||
const databasePath = resolve(
|
||||
process.env.TDR_DATABASE_PATH || './data/shd-build-development.db',
|
||||
);
|
||||
const database = await connect(databasePath);
|
||||
const resetPasswords = process.argv.includes('--reset-passwords');
|
||||
|
||||
|
||||
@@ -3,22 +3,63 @@ import { randomUUID } from 'node:crypto';
|
||||
import { resolve } from 'node:path';
|
||||
|
||||
const targetName = process.argv[2] || 'Aewyn';
|
||||
const database = await connect(resolve(process.env.TDR_DATABASE_PATH || './data/shd-build.db'));
|
||||
const user = await (await database.prepare(`SELECT users.id,COALESCE(user_profiles.display_name,users.username) AS display_name
|
||||
const database = await connect(
|
||||
resolve(process.env.TDR_DATABASE_PATH || './data/shd-build.db'),
|
||||
);
|
||||
const user = await (
|
||||
await database.prepare(`SELECT users.id,COALESCE(user_profiles.display_name,users.username) AS display_name
|
||||
FROM users LEFT JOIN user_profiles ON user_profiles.user_id=users.id
|
||||
WHERE users.login_username=? COLLATE NOCASE OR users.username=? COLLATE NOCASE OR user_profiles.display_name=? COLLATE NOCASE
|
||||
LIMIT 1`)).get(targetName, targetName, targetName);
|
||||
LIMIT 1`)
|
||||
).get(targetName, targetName, targetName);
|
||||
if (!user) throw new Error(`No user matched ${targetName}.`);
|
||||
const now = Date.now();
|
||||
const samples = [
|
||||
['isac', 'ISAC AVATAR OVERSIGHT', '[TEST] Custom avatar removed', 'Your custom avatar did not meet SHD.build image requirements and was removed. Oversight reason: The image contained unrelated promotional text.', '/account#avatar', 1],
|
||||
['contributions', 'ISAC DATA OVERSIGHT', '[TEST] Contribution approved', 'Quick Draw brand-set data was approved and deployed to the live builder registry.', '/isac/brands', 1],
|
||||
['contributions', 'ISAC DATA OVERSIGHT', '[TEST] Contribution needs revision', 'A talent update was not approved. Oversight reason: Tier II effect value does not match the supplied game evidence.', '/isac/talents', 1],
|
||||
['comments', 'SHD COMMUNITY', '[TEST] New comment on your build', 'An agent commented on “First Wave Vector Critical Build”: This setup worked really well in Legendary missions.', '/builds', 0]
|
||||
[
|
||||
'isac',
|
||||
'ISAC AVATAR OVERSIGHT',
|
||||
'[TEST] Custom avatar removed',
|
||||
'Your custom avatar did not meet SHD.build image requirements and was removed. Oversight reason: The image contained unrelated promotional text.',
|
||||
'/account#avatar',
|
||||
1,
|
||||
],
|
||||
[
|
||||
'contributions',
|
||||
'ISAC DATA OVERSIGHT',
|
||||
'[TEST] Contribution approved',
|
||||
'Quick Draw brand-set data was approved and deployed to the live builder registry.',
|
||||
'/isac/brands',
|
||||
1,
|
||||
],
|
||||
[
|
||||
'contributions',
|
||||
'ISAC DATA OVERSIGHT',
|
||||
'[TEST] Contribution needs revision',
|
||||
'A talent update was not approved. Oversight reason: Tier II effect value does not match the supplied game evidence.',
|
||||
'/isac/talents',
|
||||
1,
|
||||
],
|
||||
[
|
||||
'comments',
|
||||
'SHD COMMUNITY',
|
||||
'[TEST] New comment on your build',
|
||||
'An agent commented on “First Wave Vector Critical Build”: This setup worked really well in Legendary missions.',
|
||||
'/builds',
|
||||
0,
|
||||
],
|
||||
];
|
||||
for (const [index, sample] of samples.entries()) {
|
||||
await (await database.prepare(`INSERT INTO notifications
|
||||
await (
|
||||
await database.prepare(`INSERT INTO notifications
|
||||
(id,user_id,category,source,title,body,link,mandatory,created_at)
|
||||
VALUES(?,?,?,?,?,?,?,?,?)`)).run(randomUUID(), user.id, ...sample, new Date(now - index * 60_000).toISOString());
|
||||
VALUES(?,?,?,?,?,?,?,?,?)`)
|
||||
).run(
|
||||
randomUUID(),
|
||||
user.id,
|
||||
...sample,
|
||||
new Date(now - index * 60_000).toISOString(),
|
||||
);
|
||||
}
|
||||
console.log(`Added ${samples.length} test notifications for ${user.display_name}.`);
|
||||
console.log(
|
||||
`Added ${samples.length} test notifications for ${user.display_name}.`,
|
||||
);
|
||||
|
||||
@@ -4,51 +4,74 @@ import { dirname, join, resolve } from 'node:path';
|
||||
import process from 'node:process';
|
||||
|
||||
const outputArgument = process.argv[2];
|
||||
const timestamp = new Date().toISOString().replaceAll(':', '-').replace(/\.\d{3}Z$/, 'Z');
|
||||
const outputPath = resolve(outputArgument || `./data/backups/shd-stage-bootstrap-${timestamp}.json`);
|
||||
const timestamp = new Date()
|
||||
.toISOString()
|
||||
.replaceAll(':', '-')
|
||||
.replace(/\.\d{3}Z$/, 'Z');
|
||||
const outputPath = resolve(
|
||||
outputArgument || `./data/backups/shd-stage-bootstrap-${timestamp}.json`,
|
||||
);
|
||||
const temporaryBackupPath = `${outputPath}.database.json`;
|
||||
const databaseBackup = spawnSync(process.execPath, ['scripts/db-backup.mjs', temporaryBackupPath], {
|
||||
cwd: process.cwd(),
|
||||
stdio: 'inherit',
|
||||
env: process.env
|
||||
});
|
||||
const databaseBackup = spawnSync(
|
||||
process.execPath,
|
||||
['scripts/db-backup.mjs', temporaryBackupPath],
|
||||
{
|
||||
cwd: process.cwd(),
|
||||
stdio: 'inherit',
|
||||
env: process.env,
|
||||
},
|
||||
);
|
||||
if (databaseBackup.error) throw databaseBackup.error;
|
||||
if (databaseBackup.status !== 0) throw new Error('Database backup failed; no staging bootstrap bundle was created.');
|
||||
if (databaseBackup.status !== 0)
|
||||
throw new Error(
|
||||
'Database backup failed; no staging bootstrap bundle was created.',
|
||||
);
|
||||
|
||||
async function collectFiles(directory, pattern) {
|
||||
try {
|
||||
const names = (await readdir(directory)).filter((name) => pattern.test(name)).sort();
|
||||
return await Promise.all(names.map(async (name) => ({
|
||||
name,
|
||||
contentBase64: (await readFile(join(directory, name))).toString('base64')
|
||||
})));
|
||||
} catch (error) {
|
||||
if (error?.code === 'ENOENT') return [];
|
||||
throw error;
|
||||
}
|
||||
try {
|
||||
const names = (await readdir(directory))
|
||||
.filter((name) => pattern.test(name))
|
||||
.sort();
|
||||
return await Promise.all(
|
||||
names.map(async (name) => ({
|
||||
name,
|
||||
contentBase64: (await readFile(join(directory, name))).toString(
|
||||
'base64',
|
||||
),
|
||||
})),
|
||||
);
|
||||
} catch (error) {
|
||||
if (error?.code === 'ENOENT') return [];
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const database = JSON.parse(await readFile(temporaryBackupPath, 'utf8'));
|
||||
const avatars = await collectFiles(
|
||||
resolve(process.env.TDR_AVATAR_STORAGE_PATH || './data/avatars'),
|
||||
/^[0-9a-f-]{36}\.webp$/i
|
||||
);
|
||||
const gameIcons = await collectFiles(
|
||||
resolve(process.env.TDR_GAME_ICON_STORAGE_PATH || './data/game-icons'),
|
||||
/^[a-z_]+-[0-9a-f-]{36}\.webp$/i
|
||||
);
|
||||
const bundle = {
|
||||
format: 'shd-build-stage-bootstrap',
|
||||
version: 1,
|
||||
createdAt: new Date().toISOString(),
|
||||
database,
|
||||
files: { avatars, gameIcons }
|
||||
};
|
||||
await mkdir(dirname(outputPath), { recursive: true });
|
||||
await writeFile(outputPath, `${JSON.stringify(bundle, null, 2)}\n`, { encoding: 'utf8', flag: 'wx' });
|
||||
console.log(`Staging bootstrap bundle includes ${avatars.length} avatars and ${gameIcons.length} game-data icons.`);
|
||||
console.log(`Bootstrap bundle: ${outputPath}`);
|
||||
const database = JSON.parse(await readFile(temporaryBackupPath, 'utf8'));
|
||||
const avatars = await collectFiles(
|
||||
resolve(process.env.TDR_AVATAR_STORAGE_PATH || './data/avatars'),
|
||||
/^[0-9a-f-]{36}\.webp$/i,
|
||||
);
|
||||
const gameIcons = await collectFiles(
|
||||
resolve(process.env.TDR_GAME_ICON_STORAGE_PATH || './data/game-icons'),
|
||||
/^[a-z_]+-[0-9a-f-]{36}\.webp$/i,
|
||||
);
|
||||
const bundle = {
|
||||
format: 'shd-build-stage-bootstrap',
|
||||
version: 1,
|
||||
createdAt: new Date().toISOString(),
|
||||
database,
|
||||
files: { avatars, gameIcons },
|
||||
};
|
||||
await mkdir(dirname(outputPath), { recursive: true });
|
||||
await writeFile(outputPath, `${JSON.stringify(bundle, null, 2)}\n`, {
|
||||
encoding: 'utf8',
|
||||
flag: 'wx',
|
||||
});
|
||||
console.log(
|
||||
`Staging bootstrap bundle includes ${avatars.length} avatars and ${gameIcons.length} game-data icons.`,
|
||||
);
|
||||
console.log(`Bootstrap bundle: ${outputPath}`);
|
||||
} finally {
|
||||
await rm(temporaryBackupPath, { force: true });
|
||||
await rm(temporaryBackupPath, { force: true });
|
||||
}
|
||||
|
||||
@@ -4,106 +4,165 @@ import { mkdir, readFile, writeFile } from 'node:fs/promises';
|
||||
import { join, resolve } from 'node:path';
|
||||
import process from 'node:process';
|
||||
|
||||
const bundleArgument = process.argv.slice(2).find((argument) => !argument.startsWith('--'));
|
||||
if (!bundleArgument || !process.argv.includes('--confirm-empty-staging-replace')
|
||||
|| process.env.ALLOW_STAGING_BOOTSTRAP !== 'true') {
|
||||
console.error('Staging bootstrap requires ALLOW_STAGING_BOOTSTRAP=true and:');
|
||||
console.error('node scripts/stage-bootstrap-import.mjs <bundle.json> --confirm-empty-staging-replace');
|
||||
process.exit(2);
|
||||
const bundleArgument = process.argv
|
||||
.slice(2)
|
||||
.find((argument) => !argument.startsWith('--'));
|
||||
if (
|
||||
!bundleArgument ||
|
||||
!process.argv.includes('--confirm-empty-staging-replace') ||
|
||||
process.env.ALLOW_STAGING_BOOTSTRAP !== 'true'
|
||||
) {
|
||||
console.error('Staging bootstrap requires ALLOW_STAGING_BOOTSTRAP=true and:');
|
||||
console.error(
|
||||
'node scripts/stage-bootstrap-import.mjs <bundle.json> --confirm-empty-staging-replace',
|
||||
);
|
||||
process.exit(2);
|
||||
}
|
||||
|
||||
const bundlePath = resolve(bundleArgument);
|
||||
const bundle = JSON.parse(await readFile(bundlePath, 'utf8'));
|
||||
if (bundle?.format !== 'shd-build-stage-bootstrap' || bundle?.version !== 1
|
||||
|| bundle.database?.format !== 'shd-build-database-backup' || bundle.database?.version !== 1
|
||||
|| !Array.isArray(bundle.database.tables) || !Array.isArray(bundle.files?.avatars)
|
||||
|| !Array.isArray(bundle.files?.gameIcons)) {
|
||||
throw new Error('The selected file is not a supported SHD.build staging bootstrap bundle.');
|
||||
if (
|
||||
bundle?.format !== 'shd-build-stage-bootstrap' ||
|
||||
bundle?.version !== 1 ||
|
||||
bundle.database?.format !== 'shd-build-database-backup' ||
|
||||
bundle.database?.version !== 1 ||
|
||||
!Array.isArray(bundle.database.tables) ||
|
||||
!Array.isArray(bundle.files?.avatars) ||
|
||||
!Array.isArray(bundle.files?.gameIcons)
|
||||
) {
|
||||
throw new Error(
|
||||
'The selected file is not a supported SHD.build staging bootstrap bundle.',
|
||||
);
|
||||
}
|
||||
for (const file of bundle.files.avatars) {
|
||||
if (typeof file.name !== 'string' || !/^[0-9a-f-]{36}\.webp$/i.test(file.name) || typeof file.contentBase64 !== 'string') {
|
||||
throw new Error('The bootstrap bundle contains an invalid avatar file.');
|
||||
}
|
||||
if (
|
||||
typeof file.name !== 'string' ||
|
||||
!/^[0-9a-f-]{36}\.webp$/i.test(file.name) ||
|
||||
typeof file.contentBase64 !== 'string'
|
||||
) {
|
||||
throw new Error('The bootstrap bundle contains an invalid avatar file.');
|
||||
}
|
||||
}
|
||||
for (const file of bundle.files.gameIcons) {
|
||||
if (typeof file.name !== 'string' || !/^[a-z_]+-[0-9a-f-]{36}\.webp$/i.test(file.name) || typeof file.contentBase64 !== 'string') {
|
||||
throw new Error('The bootstrap bundle contains an invalid game-data icon file.');
|
||||
}
|
||||
if (
|
||||
typeof file.name !== 'string' ||
|
||||
!/^[a-z_]+-[0-9a-f-]{36}\.webp$/i.test(file.name) ||
|
||||
typeof file.contentBase64 !== 'string'
|
||||
) {
|
||||
throw new Error(
|
||||
'The bootstrap bundle contains an invalid game-data icon file.',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const databasePath = resolve(process.env.TDR_DATABASE_PATH || './data/shd-build-staging.db');
|
||||
const databasePath = resolve(
|
||||
process.env.TDR_DATABASE_PATH || './data/shd-build-staging.db',
|
||||
);
|
||||
const safetyBackup = spawnSync(process.execPath, ['scripts/db-backup.mjs'], {
|
||||
cwd: process.cwd(),
|
||||
stdio: 'inherit',
|
||||
env: process.env
|
||||
cwd: process.cwd(),
|
||||
stdio: 'inherit',
|
||||
env: process.env,
|
||||
});
|
||||
if (safetyBackup.error) throw safetyBackup.error;
|
||||
if (safetyBackup.status !== 0) throw new Error('Staging safety backup failed; the target was not changed.');
|
||||
if (safetyBackup.status !== 0)
|
||||
throw new Error('Staging safety backup failed; the target was not changed.');
|
||||
|
||||
const database = await connect(databasePath);
|
||||
for (const tableName of ['users', 'builds', 'audit_log', 'data_change_proposals']) {
|
||||
const row = await (await database.prepare(`SELECT COUNT(*) AS count FROM "${tableName}"`)).get();
|
||||
if (Number(row?.count ?? 0) > 0) {
|
||||
throw new Error(`Staging bootstrap refused: target table "${tableName}" is not empty.`);
|
||||
}
|
||||
for (const tableName of [
|
||||
'users',
|
||||
'builds',
|
||||
'audit_log',
|
||||
'data_change_proposals',
|
||||
]) {
|
||||
const row = await (
|
||||
await database.prepare(`SELECT COUNT(*) AS count FROM "${tableName}"`)
|
||||
).get();
|
||||
if (Number(row?.count ?? 0) > 0) {
|
||||
throw new Error(
|
||||
`Staging bootstrap refused: target table "${tableName}" is not empty.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const targetTables = await (await database.prepare(`
|
||||
const targetTables = await (
|
||||
await database.prepare(`
|
||||
SELECT name FROM sqlite_master
|
||||
WHERE type='table' AND name NOT LIKE 'sqlite_%'
|
||||
ORDER BY rowid
|
||||
`)).all();
|
||||
`)
|
||||
).all();
|
||||
const targetNames = new Set(targetTables.map((row) => String(row.name)));
|
||||
const sourceNames = new Set(bundle.database.tables.map((table) => String(table.name)));
|
||||
const sourceNames = new Set(
|
||||
bundle.database.tables.map((table) => String(table.name)),
|
||||
);
|
||||
for (const row of targetTables) {
|
||||
if (!sourceNames.has(String(row.name))) {
|
||||
throw new Error(`Bootstrap snapshot is missing target-schema table "${String(row.name)}".`);
|
||||
}
|
||||
if (!sourceNames.has(String(row.name))) {
|
||||
throw new Error(
|
||||
`Bootstrap snapshot is missing target-schema table "${String(row.name)}".`,
|
||||
);
|
||||
}
|
||||
}
|
||||
const sourceOnlyTables = bundle.database.tables
|
||||
.map((table) => String(table.name))
|
||||
.filter((name) => !targetNames.has(name));
|
||||
.map((table) => String(table.name))
|
||||
.filter((name) => !targetNames.has(name));
|
||||
if (sourceOnlyTables.length) {
|
||||
throw new Error(`Bootstrap snapshot contains tables outside the current target schema: ${sourceOnlyTables.join(', ')}`);
|
||||
throw new Error(
|
||||
`Bootstrap snapshot contains tables outside the current target schema: ${sourceOnlyTables.join(', ')}`,
|
||||
);
|
||||
}
|
||||
const quoteIdentifier = (identifier) => `"${String(identifier).replaceAll('"', '""')}"`;
|
||||
const quoteIdentifier = (identifier) =>
|
||||
`"${String(identifier).replaceAll('"', '""')}"`;
|
||||
let restoredRows = 0;
|
||||
await database.exec('PRAGMA foreign_keys=OFF');
|
||||
await database.exec('BEGIN');
|
||||
try {
|
||||
for (const row of [...targetTables].reverse()) {
|
||||
await database.exec(`DELETE FROM ${quoteIdentifier(row.name)}`);
|
||||
}
|
||||
for (const table of bundle.database.tables) {
|
||||
for (const row of table.rows) {
|
||||
const columns = Object.keys(row);
|
||||
if (!columns.length) continue;
|
||||
const insert = await database.prepare(
|
||||
`INSERT INTO ${quoteIdentifier(table.name)} (${columns.map(quoteIdentifier).join(', ')})
|
||||
VALUES (${columns.map(() => '?').join(', ')})`
|
||||
);
|
||||
await insert.run(...columns.map((column) => row[column]));
|
||||
restoredRows++;
|
||||
}
|
||||
}
|
||||
await database.exec('COMMIT');
|
||||
for (const row of [...targetTables].reverse()) {
|
||||
await database.exec(`DELETE FROM ${quoteIdentifier(row.name)}`);
|
||||
}
|
||||
for (const table of bundle.database.tables) {
|
||||
for (const row of table.rows) {
|
||||
const columns = Object.keys(row);
|
||||
if (!columns.length) continue;
|
||||
const insert = await database.prepare(
|
||||
`INSERT INTO ${quoteIdentifier(table.name)} (${columns.map(quoteIdentifier).join(', ')})
|
||||
VALUES (${columns.map(() => '?').join(', ')})`,
|
||||
);
|
||||
await insert.run(...columns.map((column) => row[column]));
|
||||
restoredRows++;
|
||||
}
|
||||
}
|
||||
await database.exec('COMMIT');
|
||||
} catch (error) {
|
||||
await database.exec('ROLLBACK');
|
||||
throw error;
|
||||
await database.exec('ROLLBACK');
|
||||
throw error;
|
||||
} finally {
|
||||
await database.exec('PRAGMA foreign_keys=ON');
|
||||
await database.exec('PRAGMA foreign_keys=ON');
|
||||
}
|
||||
|
||||
const avatarDirectory = resolve(process.env.TDR_AVATAR_STORAGE_PATH || './data/avatars');
|
||||
const gameIconDirectory = resolve(process.env.TDR_GAME_ICON_STORAGE_PATH || './data/game-icons');
|
||||
const avatarDirectory = resolve(
|
||||
process.env.TDR_AVATAR_STORAGE_PATH || './data/avatars',
|
||||
);
|
||||
const gameIconDirectory = resolve(
|
||||
process.env.TDR_GAME_ICON_STORAGE_PATH || './data/game-icons',
|
||||
);
|
||||
await mkdir(avatarDirectory, { recursive: true });
|
||||
await mkdir(gameIconDirectory, { recursive: true });
|
||||
for (const file of bundle.files.avatars) {
|
||||
await writeFile(join(avatarDirectory, file.name), Buffer.from(file.contentBase64, 'base64'), { mode: 0o600 });
|
||||
await writeFile(
|
||||
join(avatarDirectory, file.name),
|
||||
Buffer.from(file.contentBase64, 'base64'),
|
||||
{ mode: 0o600 },
|
||||
);
|
||||
}
|
||||
for (const file of bundle.files.gameIcons) {
|
||||
await writeFile(join(gameIconDirectory, file.name), Buffer.from(file.contentBase64, 'base64'), { mode: 0o600 });
|
||||
await writeFile(
|
||||
join(gameIconDirectory, file.name),
|
||||
Buffer.from(file.contentBase64, 'base64'),
|
||||
{ mode: 0o600 },
|
||||
);
|
||||
}
|
||||
|
||||
console.log(`Bootstrapped staging with ${restoredRows} database rows, ${bundle.files.avatars.length} avatars, and ${bundle.files.gameIcons.length} game-data icons.`);
|
||||
console.log(
|
||||
`Bootstrapped staging with ${restoredRows} database rows, ${bundle.files.avatars.length} avatars, and ${bundle.files.gameIcons.length} game-data icons.`,
|
||||
);
|
||||
console.log(`Source bundle: ${bundlePath}`);
|
||||
|
||||
@@ -6,41 +6,49 @@ import process from 'node:process';
|
||||
const stagingFile = resolve('.env.staging');
|
||||
const dotenvxCli = resolve('node_modules/@dotenvx/dotenvx/src/cli/dotenvx.js');
|
||||
const sharedKeys = [
|
||||
'DISCORD_CLIENT_ID',
|
||||
'DISCORD_CLIENT_SECRET',
|
||||
'SMTP_HOST',
|
||||
'SMTP_PORT',
|
||||
'SMTP_SECURE',
|
||||
'SMTP_USER',
|
||||
'SMTP_PASSWORD',
|
||||
'SMTP_FROM'
|
||||
'DISCORD_CLIENT_ID',
|
||||
'DISCORD_CLIENT_SECRET',
|
||||
'SMTP_HOST',
|
||||
'SMTP_PORT',
|
||||
'SMTP_SECURE',
|
||||
'SMTP_USER',
|
||||
'SMTP_PASSWORD',
|
||||
'SMTP_FROM',
|
||||
];
|
||||
|
||||
for (const key of sharedKeys) {
|
||||
if (!process.env[key]) throw new Error(`Source environment is missing ${key}.`);
|
||||
if (!process.env[key])
|
||||
throw new Error(`Source environment is missing ${key}.`);
|
||||
}
|
||||
|
||||
function dotenvx(action) {
|
||||
const result = spawnSync(process.execPath, [dotenvxCli, action, '-f', stagingFile], {
|
||||
cwd: process.cwd(),
|
||||
stdio: 'inherit'
|
||||
});
|
||||
if (result.error) throw result.error;
|
||||
if (result.status !== 0) throw new Error(`dotenvx ${action} failed.`);
|
||||
const result = spawnSync(
|
||||
process.execPath,
|
||||
[dotenvxCli, action, '-f', stagingFile],
|
||||
{
|
||||
cwd: process.cwd(),
|
||||
stdio: 'inherit',
|
||||
},
|
||||
);
|
||||
if (result.error) throw result.error;
|
||||
if (result.status !== 0) throw new Error(`dotenvx ${action} failed.`);
|
||||
}
|
||||
|
||||
dotenvx('decrypt');
|
||||
try {
|
||||
let contents = await readFile(stagingFile, 'utf8');
|
||||
for (const key of sharedKeys) {
|
||||
const replacement = `${key}=${JSON.stringify(process.env[key])}`;
|
||||
const pattern = new RegExp(`^${key}=.*$`, 'm');
|
||||
if (!pattern.test(contents)) throw new Error(`Staging environment is missing ${key}.`);
|
||||
contents = contents.replace(pattern, replacement);
|
||||
}
|
||||
await writeFile(stagingFile, contents, 'utf8');
|
||||
let contents = await readFile(stagingFile, 'utf8');
|
||||
for (const key of sharedKeys) {
|
||||
const replacement = `${key}=${JSON.stringify(process.env[key])}`;
|
||||
const pattern = new RegExp(`^${key}=.*$`, 'm');
|
||||
if (!pattern.test(contents))
|
||||
throw new Error(`Staging environment is missing ${key}.`);
|
||||
contents = contents.replace(pattern, replacement);
|
||||
}
|
||||
await writeFile(stagingFile, contents, 'utf8');
|
||||
} finally {
|
||||
dotenvx('encrypt');
|
||||
dotenvx('encrypt');
|
||||
}
|
||||
|
||||
console.log('Copied shared Discord and SMTP credentials into encrypted staging configuration.');
|
||||
console.log(
|
||||
'Copied shared Discord and SMTP credentials into encrypted staging configuration.',
|
||||
);
|
||||
|
||||
@@ -3,64 +3,102 @@ import nodemailer from 'nodemailer';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { resolve } from 'node:path';
|
||||
|
||||
const database = await connect(resolve(process.env.TDR_DATABASE_PATH || './data/shd-build.db'));
|
||||
const database = await connect(
|
||||
resolve(process.env.TDR_DATABASE_PATH || './data/shd-build.db'),
|
||||
);
|
||||
const now = new Date().toISOString();
|
||||
const due = await (await database.prepare(`SELECT delivery.id AS delivery_id,delivery.user_id,message.*
|
||||
const due = await (
|
||||
await database.prepare(`SELECT delivery.id AS delivery_id,delivery.user_id,message.*
|
||||
FROM automated_system_message_deliveries delivery
|
||||
JOIN automated_system_messages message ON message.id=delivery.message_id
|
||||
WHERE delivery.delivered_at IS NULL AND delivery.scheduled_for<=? AND message.enabled=1
|
||||
ORDER BY delivery.scheduled_for LIMIT 250`)).all(now);
|
||||
ORDER BY delivery.scheduled_for LIMIT 250`)
|
||||
).all(now);
|
||||
const smtpConfigured = Boolean(process.env.SMTP_HOST && process.env.SMTP_FROM);
|
||||
const transport = smtpConfigured ? nodemailer.createTransport({
|
||||
host: process.env.SMTP_HOST,
|
||||
port: Number(process.env.SMTP_PORT || 587),
|
||||
secure: process.env.SMTP_SECURE === 'true',
|
||||
auth: process.env.SMTP_USER ? { user: process.env.SMTP_USER, pass: process.env.SMTP_PASSWORD } : undefined,
|
||||
requireTLS: process.env.SMTP_SECURE !== 'true',
|
||||
disableFileAccess: true,
|
||||
disableUrlAccess: true
|
||||
}) : null;
|
||||
const origin = (process.env.APP_ORIGIN || 'http://localhost:5173').replace(/\/+$/, '');
|
||||
const escapeHtml = (value) => String(value).replace(/[&<>"']/g, (character) => ({
|
||||
'&': '&',
|
||||
'<': '<',
|
||||
'>': '>',
|
||||
'"': '"',
|
||||
"'": '''
|
||||
})[character]);
|
||||
const transport = smtpConfigured
|
||||
? nodemailer.createTransport({
|
||||
host: process.env.SMTP_HOST,
|
||||
port: Number(process.env.SMTP_PORT || 587),
|
||||
secure: process.env.SMTP_SECURE === 'true',
|
||||
auth: process.env.SMTP_USER
|
||||
? { user: process.env.SMTP_USER, pass: process.env.SMTP_PASSWORD }
|
||||
: undefined,
|
||||
requireTLS: process.env.SMTP_SECURE !== 'true',
|
||||
disableFileAccess: true,
|
||||
disableUrlAccess: true,
|
||||
})
|
||||
: null;
|
||||
const origin = (process.env.APP_ORIGIN || 'http://localhost:5173').replace(
|
||||
/\/+$/,
|
||||
'',
|
||||
);
|
||||
const escapeHtml = (value) =>
|
||||
String(value).replace(
|
||||
/[&<>"']/g,
|
||||
(character) =>
|
||||
({
|
||||
'&': '&',
|
||||
'<': '<',
|
||||
'>': '>',
|
||||
'"': '"',
|
||||
"'": ''',
|
||||
})[character],
|
||||
);
|
||||
let delivered = 0;
|
||||
|
||||
for (const message of due) {
|
||||
const notificationId = randomUUID();
|
||||
await (await database.prepare(`INSERT INTO notifications
|
||||
const notificationId = randomUUID();
|
||||
await (
|
||||
await database.prepare(`INSERT INTO notifications
|
||||
(id,user_id,category,source,title,body,link,actions_json,mandatory,created_at)
|
||||
VALUES(?,?,'isac',?,?,?,?,?,1,?)`)).run(
|
||||
notificationId, message.user_id, message.source, message.title, message.body,
|
||||
message.link ?? null, message.actions_json ?? null, now
|
||||
);
|
||||
await (await database.prepare(`UPDATE automated_system_message_deliveries
|
||||
SET delivered_at=?,notification_id=? WHERE id=? AND delivered_at IS NULL`))
|
||||
.run(now, notificationId, message.delivery_id);
|
||||
const preference = await (await database.prepare(
|
||||
"SELECT email_frequency FROM notification_preferences WHERE user_id=? AND category='isac'"
|
||||
)).get(message.user_id);
|
||||
if (transport && preference?.email_frequency === 'immediate') {
|
||||
const identity = await (await database.prepare('SELECT email FROM user_emails WHERE user_id=?')).get(message.user_id);
|
||||
if (typeof identity?.email === 'string') {
|
||||
try {
|
||||
await transport.sendMail({
|
||||
from: process.env.SMTP_FROM,
|
||||
to: identity.email,
|
||||
subject: `SHD.build · ${message.title}`,
|
||||
text: `${message.body}\n\n${message.link ? `${origin}${message.link}` : origin}`,
|
||||
html: `<div style="background:#0a0c0d;color:#ecebe5;padding:28px;font-family:Arial,sans-serif"><p style="color:#f6a821;font-size:12px;letter-spacing:2px">${escapeHtml(message.source)}</p><h1>${escapeHtml(message.title)}</h1><p style="color:#a5aaab">${escapeHtml(message.body)}</p>${message.link ? `<a style="color:#f6a821" href="${escapeHtml(`${origin}${message.link}`)}">OPEN SHD.BUILD</a>` : ''}</div>`
|
||||
});
|
||||
await (await database.prepare('UPDATE notifications SET emailed_at=? WHERE id=?')).run(now, notificationId);
|
||||
} catch (error) {
|
||||
console.error(`Unable to email automated notification ${notificationId}`, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
delivered += 1;
|
||||
VALUES(?,?,'isac',?,?,?,?,?,1,?)`)
|
||||
).run(
|
||||
notificationId,
|
||||
message.user_id,
|
||||
message.source,
|
||||
message.title,
|
||||
message.body,
|
||||
message.link ?? null,
|
||||
message.actions_json ?? null,
|
||||
now,
|
||||
);
|
||||
await (
|
||||
await database.prepare(`UPDATE automated_system_message_deliveries
|
||||
SET delivered_at=?,notification_id=? WHERE id=? AND delivered_at IS NULL`)
|
||||
).run(now, notificationId, message.delivery_id);
|
||||
const preference = await (
|
||||
await database.prepare(
|
||||
"SELECT email_frequency FROM notification_preferences WHERE user_id=? AND category='isac'",
|
||||
)
|
||||
).get(message.user_id);
|
||||
if (transport && preference?.email_frequency === 'immediate') {
|
||||
const identity = await (
|
||||
await database.prepare('SELECT email FROM user_emails WHERE user_id=?')
|
||||
).get(message.user_id);
|
||||
if (typeof identity?.email === 'string') {
|
||||
try {
|
||||
await transport.sendMail({
|
||||
from: process.env.SMTP_FROM,
|
||||
to: identity.email,
|
||||
subject: `SHD.build · ${message.title}`,
|
||||
text: `${message.body}\n\n${message.link ? `${origin}${message.link}` : origin}`,
|
||||
html: `<div style="background:#0a0c0d;color:#ecebe5;padding:28px;font-family:Arial,sans-serif"><p style="color:#f6a821;font-size:12px;letter-spacing:2px">${escapeHtml(message.source)}</p><h1>${escapeHtml(message.title)}</h1><p style="color:#a5aaab">${escapeHtml(message.body)}</p>${message.link ? `<a style="color:#f6a821" href="${escapeHtml(`${origin}${message.link}`)}">OPEN SHD.BUILD</a>` : ''}</div>`,
|
||||
});
|
||||
await (
|
||||
await database.prepare(
|
||||
'UPDATE notifications SET emailed_at=? WHERE id=?',
|
||||
)
|
||||
).run(now, notificationId);
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`Unable to email automated notification ${notificationId}`,
|
||||
error,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
delivered += 1;
|
||||
}
|
||||
console.log(`Delivered ${delivered} automated ISAC system notification${delivered === 1 ? '' : 's'}.`);
|
||||
console.log(
|
||||
`Delivered ${delivered} automated ISAC system notification${delivered === 1 ? '' : 's'}.`,
|
||||
);
|
||||
|
||||
@@ -4,37 +4,41 @@ import { join, resolve } from 'node:path';
|
||||
|
||||
const iconDirectory = resolve('./static');
|
||||
const iconFiles = (await readdir(iconDirectory))
|
||||
.filter((file) => file.toLocaleLowerCase().endsWith('.png'))
|
||||
.sort((left, right) => left.localeCompare(right));
|
||||
.filter((file) => file.toLocaleLowerCase().endsWith('.png'))
|
||||
.sort((left, right) => left.localeCompare(right));
|
||||
|
||||
for (const file of iconFiles) {
|
||||
const source = join(iconDirectory, file);
|
||||
const temporary = join(iconDirectory, `.${file}.trimmed`);
|
||||
const before = await sharp(source).metadata();
|
||||
const source = join(iconDirectory, file);
|
||||
const temporary = join(iconDirectory, `.${file}.trimmed`);
|
||||
const before = await sharp(source).metadata();
|
||||
|
||||
try {
|
||||
const result = await sharp(source)
|
||||
.trim({ background: { r: 0, g: 0, b: 0, alpha: 0 } })
|
||||
.extend({
|
||||
top: 2,
|
||||
right: 2,
|
||||
bottom: 2,
|
||||
left: 2,
|
||||
background: { r: 0, g: 0, b: 0, alpha: 0 }
|
||||
})
|
||||
.png({ compressionLevel: 9, adaptiveFiltering: true })
|
||||
.toFile(temporary);
|
||||
try {
|
||||
const result = await sharp(source)
|
||||
.trim({ background: { r: 0, g: 0, b: 0, alpha: 0 } })
|
||||
.extend({
|
||||
top: 2,
|
||||
right: 2,
|
||||
bottom: 2,
|
||||
left: 2,
|
||||
background: { r: 0, g: 0, b: 0, alpha: 0 },
|
||||
})
|
||||
.png({ compressionLevel: 9, adaptiveFiltering: true })
|
||||
.toFile(temporary);
|
||||
|
||||
if (!result.width || !result.height || result.channels !== 4) {
|
||||
throw new Error(`${file} did not produce a valid transparent RGBA icon.`);
|
||||
}
|
||||
if (!result.width || !result.height || result.channels !== 4) {
|
||||
throw new Error(`${file} did not produce a valid transparent RGBA icon.`);
|
||||
}
|
||||
|
||||
await rename(temporary, source);
|
||||
console.log(`${file}: ${before.width}x${before.height} -> ${result.width}x${result.height}`);
|
||||
} catch (error) {
|
||||
await unlink(temporary).catch(() => {});
|
||||
throw error;
|
||||
}
|
||||
await rename(temporary, source);
|
||||
console.log(
|
||||
`${file}: ${before.width}x${before.height} -> ${result.width}x${result.height}`,
|
||||
);
|
||||
} catch (error) {
|
||||
await unlink(temporary).catch(() => {});
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`Trimmed ${iconFiles.length} static PNG icons with a 2px transparent safety margin.`);
|
||||
console.log(
|
||||
`Trimmed ${iconFiles.length} static PNG icons with a 2px transparent safety margin.`,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user