recover-gateway.mjs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import 'dotenv/config';
  2. import { readFile } from 'node:fs/promises';
  3. import { randomUUID } from 'node:crypto';
  4. import { fileURLToPath } from 'node:url';
  5. import { dirname, resolve } from 'node:path';
  6. const required = ['PARSE_SERVER_URL', 'PARSE_APP_ID', 'PARSE_MASTER_KEY'];
  7. for (const name of required) if (!process.env[name]?.trim()) throw new Error(`${name} is required`);
  8. const registryBase = (process.env.FUNCTION_REGISTRY_SERVER_URL || process.env.PARSE_SERVER_URL).replace(/\/+$/, '');
  9. const registryAppId = process.env.FUNCTION_REGISTRY_APP_ID || process.env.PARSE_APP_ID;
  10. const registryMasterKey = process.env.FUNCTION_REGISTRY_MASTER_KEY || process.env.PARSE_MASTER_KEY;
  11. const headers = { 'Content-Type': 'application/json', 'X-Parse-Application-Id': registryAppId, 'X-Parse-Master-Key': registryMasterKey };
  12. const __dirname = dirname(fileURLToPath(import.meta.url));
  13. // Deploy only the original consolidated gateway
  14. const FUNCTION = {
  15. name: 'vSaaSVocGateway',
  16. path: '/saas-voc-gateway',
  17. file: 'saas-voc-gateway.js',
  18. desc: 'SaaS VOC allow-listed Cloud Function gateway',
  19. objectId: 'ZLDvkbQ16D',
  20. };
  21. async function deployFunction() {
  22. const filePath = resolve(__dirname, '..', 'cloud-functions', FUNCTION.file);
  23. const code = await readFile(filePath, 'utf8');
  24. const url = `${registryBase}/classes/Function/${FUNCTION.objectId}`;
  25. const payload = {
  26. name: FUNCTION.name,
  27. desc: FUNCTION.desc,
  28. type: 'standalone',
  29. path: FUNCTION.path,
  30. paramList: [{ name: 'params', type: 'Object', required: true }],
  31. code,
  32. respType: 'json',
  33. respJson: { success: true, data: null, requestId: randomUUID() },
  34. isDeleted: false,
  35. };
  36. const response = await fetch(url, {
  37. method: 'PUT',
  38. headers,
  39. body: JSON.stringify(payload),
  40. });
  41. if (!response.ok) {
  42. const errorText = await response.text();
  43. throw new Error(`Function deployment failed: HTTP ${response.status} - ${errorText}`);
  44. }
  45. const result = await response.json();
  46. return { ok: true, objectId: result.objectId };
  47. }
  48. async function main() {
  49. console.log(`Deploying ${FUNCTION.name}...`);
  50. const result = await deployFunction();
  51. console.log(`✓ ${FUNCTION.name} (${result.objectId})`);
  52. console.log(`Path: ${FUNCTION.path}`);
  53. }
  54. main().catch((error) => {
  55. console.error('Deployment failed:', error);
  56. process.exitCode = 1;
  57. });