| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- import fs from 'node:fs';
- import path from 'node:path';
- import crypto from 'node:crypto';
- import {fileURLToPath} from 'node:url';
- const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
- const hash = value => crypto.createHash('sha256').update(value).digest('hex');
- const normalize = value => value.replaceAll('\r\n', '\n').replace(/^import .*;\n/gm, '').replace(/^export /gm, '').trim();
- const endpoint = {id: 'bTjPEnHYWF', name: 'ksfWorkshopAccount', beforeSha256: 'c9709c1a30e36011a69adc712d1fbecb824b5c63c6c2dbf3154d6badbe5d65d6', afterSha256: 'beb24469e58947b8e80ffc4b10663ea2f86f47506f2b666c97dc015175a6fa51'};
- export function shortLinkFunction(code) {
- const before = fs.readFileSync(path.join(root, 'deploy/account-binding-short-links-20261112/previous-account-binding.mjs'));
- if (hash(before) !== 'd02811b864e8cc75e5e061c9120ac769c09af8cb3b421e9c7757c723841dc2b4') throw Error('SHORT_LINK_BASELINE_CHANGED');
- const oldModule = normalize(before.toString('utf8'));
- const released = fs.readFileSync(path.join(root, 'deploy/account-binding-short-links-20261112/released-account-binding.mjs'), 'utf8');
- if (hash(released) !== 'fcfefc23a3a0f3dc25ece94723d011f3457baac8dbee961b7f24c451d38656a2') throw Error('SHORT_LINK_RELEASE_CHANGED');
- const nextModule = normalize(released);
- if (code.split(oldModule).length !== 2) throw Error('SHORT_LINK_MODULE_MISMATCH');
- const result = code.replace(oldModule, nextModule);
- new Function(result);
- return result;
- }
- export async function deployAccountShortLinks({request, apply = false, outDir = path.join(root, 'work/chat-single-reset-20261112')}) {
- const row = await request('GET', '/classes/Function/' + endpoint.id);
- if (row.name !== endpoint.name || row.path !== '/' + endpoint.name) throw Error('SHORT_LINK_ENDPOINT_MISMATCH');
- 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};
- if (hash(row.code) !== endpoint.beforeSha256) throw Error('UNKNOWN_SHORT_LINK_FUNCTION_VERSION');
- const code = shortLinkFunction(row.code);
- if (hash(code) !== endpoint.afterSha256) throw Error('SHORT_LINK_PAYLOAD_CHANGED');
- const receipt = {ok: true, applied: false, endpoint: endpoint.name, objectId: endpoint.id, beforeSha256: hash(row.code), afterSha256: hash(code), schemaChanges: 0, permissionsChanged: false};
- fs.mkdirSync(outDir, {recursive: true});
- fs.writeFileSync(path.join(outDir, 'short-link-deploy-plan.json'), JSON.stringify(receipt, null, 2));
- if (!apply) return receipt;
- const backup = path.join(outDir, 'short-link-function-before-' + crypto.randomUUID() + '.private.json');
- fs.writeFileSync(backup, JSON.stringify(row, null, 2), {flag: 'wx', mode: 0o600});
- const current = await request('GET', '/classes/Function/' + endpoint.id);
- if (current.code !== row.code || JSON.stringify(current.ACL) !== JSON.stringify(row.ACL)) throw Error('CONCURRENT_SHORT_LINK_CHANGE');
- let written = false;
- try {
- await request('PUT', '/classes/Function/' + endpoint.id, {code}); written = true;
- const read = await request('GET', '/classes/Function/' + endpoint.id);
- 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');
- receipt.applied = true; receipt.readbackVerified = true; receipt.backup = backup;
- fs.writeFileSync(path.join(outDir, 'short-link-deploy-receipt.json'), JSON.stringify(receipt, null, 2));
- return receipt;
- } catch (error) {
- receipt.ok = false;
- receipt.error = /^[A-Z_]+$/.test(error.message) ? error.message : 'SHORT_LINK_UPDATE_FAILED';
- if (written) {
- const read = await request('GET', '/classes/Function/' + endpoint.id);
- if (read.code === code) {
- await request('PUT', '/classes/Function/' + endpoint.id, {code: row.code});
- receipt.rolledBack = (await request('GET', '/classes/Function/' + endpoint.id)).code === row.code;
- }
- }
- fs.writeFileSync(path.join(outDir, 'short-link-deploy-failure.json'), JSON.stringify(receipt, null, 2));
- throw Object.assign(Error(receipt.error), {receipt});
- }
- }
- if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
- if (!['plan', 'apply'].includes(process.argv[2]) || process.argv.length !== 3) throw Error('USAGE_PLAN_OR_APPLY');
- const {request} = await import('../server/scripts/platform-client.mjs');
- try { console.log(JSON.stringify(await deployAccountShortLinks({request, apply: process.argv[2] === 'apply'}))); }
- 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; }
- }
|