deploy-account-short-links.mjs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import fs from 'node:fs';
  2. import path from 'node:path';
  3. import crypto from 'node:crypto';
  4. import {fileURLToPath} from 'node:url';
  5. const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
  6. const hash = value => crypto.createHash('sha256').update(value).digest('hex');
  7. const normalize = value => value.replaceAll('\r\n', '\n').replace(/^import .*;\n/gm, '').replace(/^export /gm, '').trim();
  8. const endpoint = {id: 'bTjPEnHYWF', name: 'ksfWorkshopAccount', beforeSha256: 'c9709c1a30e36011a69adc712d1fbecb824b5c63c6c2dbf3154d6badbe5d65d6', afterSha256: 'beb24469e58947b8e80ffc4b10663ea2f86f47506f2b666c97dc015175a6fa51'};
  9. export function shortLinkFunction(code) {
  10. const before = fs.readFileSync(path.join(root, 'deploy/account-binding-short-links-20261112/previous-account-binding.mjs'));
  11. if (hash(before) !== 'd02811b864e8cc75e5e061c9120ac769c09af8cb3b421e9c7757c723841dc2b4') throw Error('SHORT_LINK_BASELINE_CHANGED');
  12. const oldModule = normalize(before.toString('utf8'));
  13. const released = fs.readFileSync(path.join(root, 'deploy/account-binding-short-links-20261112/released-account-binding.mjs'), 'utf8');
  14. if (hash(released) !== 'fcfefc23a3a0f3dc25ece94723d011f3457baac8dbee961b7f24c451d38656a2') throw Error('SHORT_LINK_RELEASE_CHANGED');
  15. const nextModule = normalize(released);
  16. if (code.split(oldModule).length !== 2) throw Error('SHORT_LINK_MODULE_MISMATCH');
  17. const result = code.replace(oldModule, nextModule);
  18. new Function(result);
  19. return result;
  20. }
  21. export async function deployAccountShortLinks({request, apply = false, outDir = path.join(root, 'work/chat-single-reset-20261112')}) {
  22. const row = await request('GET', '/classes/Function/' + endpoint.id);
  23. if (row.name !== endpoint.name || row.path !== '/' + endpoint.name) throw Error('SHORT_LINK_ENDPOINT_MISMATCH');
  24. if (hash(row.code) === endpoint.afterSha256) return {ok: true, applied: false, alreadyApplied: true, endpoint: endpoint.name, objectId: endpoint.id, afterSha256: endpoint.afterSha256, schemaChanges: 0, permissionsChanged: false};
  25. if (hash(row.code) !== endpoint.beforeSha256) throw Error('UNKNOWN_SHORT_LINK_FUNCTION_VERSION');
  26. const code = shortLinkFunction(row.code);
  27. if (hash(code) !== endpoint.afterSha256) throw Error('SHORT_LINK_PAYLOAD_CHANGED');
  28. const receipt = {ok: true, applied: false, endpoint: endpoint.name, objectId: endpoint.id, beforeSha256: hash(row.code), afterSha256: hash(code), schemaChanges: 0, permissionsChanged: false};
  29. fs.mkdirSync(outDir, {recursive: true});
  30. fs.writeFileSync(path.join(outDir, 'short-link-deploy-plan.json'), JSON.stringify(receipt, null, 2));
  31. if (!apply) return receipt;
  32. const backup = path.join(outDir, 'short-link-function-before-' + crypto.randomUUID() + '.private.json');
  33. fs.writeFileSync(backup, JSON.stringify(row, null, 2), {flag: 'wx', mode: 0o600});
  34. const current = await request('GET', '/classes/Function/' + endpoint.id);
  35. if (current.code !== row.code || JSON.stringify(current.ACL) !== JSON.stringify(row.ACL)) throw Error('CONCURRENT_SHORT_LINK_CHANGE');
  36. let written = false;
  37. try {
  38. await request('PUT', '/classes/Function/' + endpoint.id, {code}); written = true;
  39. const read = await request('GET', '/classes/Function/' + endpoint.id);
  40. if (read.code !== code || read.name !== row.name || read.path !== row.path || JSON.stringify(read.ACL) !== JSON.stringify(row.ACL)) throw Error('SHORT_LINK_READBACK_MISMATCH');
  41. receipt.applied = true; receipt.readbackVerified = true; receipt.backup = backup;
  42. fs.writeFileSync(path.join(outDir, 'short-link-deploy-receipt.json'), JSON.stringify(receipt, null, 2));
  43. return receipt;
  44. } catch (error) {
  45. receipt.ok = false;
  46. receipt.error = /^[A-Z_]+$/.test(error.message) ? error.message : 'SHORT_LINK_UPDATE_FAILED';
  47. if (written) {
  48. const read = await request('GET', '/classes/Function/' + endpoint.id);
  49. if (read.code === code) {
  50. await request('PUT', '/classes/Function/' + endpoint.id, {code: row.code});
  51. receipt.rolledBack = (await request('GET', '/classes/Function/' + endpoint.id)).code === row.code;
  52. }
  53. }
  54. fs.writeFileSync(path.join(outDir, 'short-link-deploy-failure.json'), JSON.stringify(receipt, null, 2));
  55. throw Object.assign(Error(receipt.error), {receipt});
  56. }
  57. }
  58. if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
  59. if (!['plan', 'apply'].includes(process.argv[2]) || process.argv.length !== 3) throw Error('USAGE_PLAN_OR_APPLY');
  60. const {request} = await import('../server/scripts/platform-client.mjs');
  61. try { console.log(JSON.stringify(await deployAccountShortLinks({request, apply: process.argv[2] === 'apply'}))); }
  62. catch (error) { console.error(JSON.stringify(error.receipt || {ok: false, error: /^[A-Z_]+$/.test(error.message) ? error.message : 'SHORT_LINK_DEPLOY_FAILED'})); process.exitCode = 1; }
  63. }