@coreframe/deploy 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/staging.js +1 -1
  2. package/package.json +2 -2
package/dist/staging.js CHANGED
@@ -1,3 +1,3 @@
1
1
  import{runDeploySmokeChecks as e}from"./checks.js";import{resolveDeployConfig as t}from"./config.js";import{randomBytes as n}from"node:crypto";import{readProjectConfig as r}from"@coreframe/config";const i=`APP_ORIGIN`,a=[`amber`,`cedar`,`cobalt`,`coral`,`ember`,`fern`,`indigo`,`juniper`,`lilac`,`maple`,`marble`,`meadow`,`moss`,`pebble`,`pine`,`plum`,`quartz`,`river`,`sable`,`sage`,`silver`,`spruce`,`willow`];async function o(e={}){let t=await d(e),n=u(await f(t),t.config);if(!n)throw Error("No pinned staging hostname exists. Run `coreframe staging reassign` to assign one.");return T(n)}async function s(n={}){let i=await d(n),a=await f(i),o=u(a,i.config),s=o?new URL(T(o)).origin:await g(i),c=l(i.config,new Set(a.map(e=>e.hostname)),i.random),y=`https://${c}/`,b,x=!1,S=!1;try{x=!0,b=await p(i,c),await v(i,y),await h(i,new URL(y).origin),S=!0,await e(t((await r()).deploy??{},`staging`,{}),{environment:`staging`,releaseName:`staging-hostname-reassignment`,url:y,version:`staging`},i.request,()=>void 0)}catch(e){let t=[];if(S)try{s?await h(i,s):await _(i)}catch(e){t.push(M(e))}let n=b;if(!n&&x)try{n=(await f(i)).find(e=>e.hostname===c&&e.service===i.config.workerService&&e.zone_id===i.zoneId)}catch(e){t.push(M(e))}if(n)try{await m(i,n)}catch(e){t.push(M(e))}throw C(e,t,!!o)}if(o)try{await m(i,o)}catch(e){throw Error(`The new staging URL ${y} is ready, but the old hostname ${o.hostname} could not be detached: ${M(e).message}\nRotation is incomplete and discovery is now ambiguous. In the Cloudflare dashboard, verify ${y} and detach only domain ID ${o.id} (${o.hostname}); do not detach unrelated domains.`)}return{previousUrl:o?T(o):void 0,url:y}}function c(e,t=n){x(e);let r=t(5);if(r.length<5)throw Error(`The secure random source returned too few bytes.`);let i=a[r[0]%a.length],o=Array.from(r.slice(1,5),e=>String.fromCharCode(97+e%26)).join(``);return`${e.managedLabelPrefix}${i}-${o}.${e.parentHostname}`}function l(e,t,r=n){for(let n=0;n<24;n+=1){let n=c(e,r);if(!t.has(n))return n}throw Error(`Could not find an unused staging hostname after 24 attempts.`)}function u(e,t){x(t);let n=S(t),r=e.filter(e=>e.service===t.workerService&&e.hostname.endsWith(`.${t.parentHostname}`)&&n.test(e.hostname));if(r.length>1)throw Error(`Found multiple CLI-managed staging domains for ${t.workerService}: ${r.map(e=>`${e.hostname} (${e.id})`).join(`, `)}. Refusing to guess. In Cloudflare, retain the intended domain and detach only obsolete domains after verifying their IDs, then rerun the command.`);return r[0]}async function d(e){let t=e.env??process.env,i=(await r()).deploy?.stagingHostname;if(!i)throw Error(`Missing deploy.stagingHostname in coreframe.config.ts. Configure workerService, parentHostname, and managedLabelPrefix.`);return x(i),{accountId:w(t,`CLOUDFLARE_ACCOUNT_ID`),apiToken:w(t,`CLOUDFLARE_API_TOKEN`),config:i,env:t,random:e.randomBytes??n,request:e.fetch??fetch,sleep:e.sleep??(e=>new Promise(t=>setTimeout(t,e))),zoneId:w(t,`CLOUDFLARE_ZONE_ID`)}}async function f(e){let t=[],n=1,r=1;do{let i=new URLSearchParams({page:String(n),per_page:`50`,zone_id:e.zoneId}),a=await y(e,`/accounts/${encodeURIComponent(e.accountId)}/workers/domains?${i}`),o=D(a.result,`domain list`).map(b);if(o.some(t=>t.zone_id!==e.zoneId))throw Error(`Cloudflare returned a Custom Domain outside the configured zone.`);t.push(...o),r=E(a.result_info?.total_pages),n+=1}while(n<=r);return t}async function p(e,t){let n=b((await y(e,`/accounts/${encodeURIComponent(e.accountId)}/workers/domains`,{body:JSON.stringify({hostname:t,service:e.config.workerService,zone_id:e.zoneId}),method:`PUT`})).result);if(n.hostname!==t||n.service!==e.config.workerService||n.zone_id!==e.zoneId)throw Error(`Cloudflare returned a Custom Domain that did not match the attach request.`);return n}async function m(e,t){if(t.service!==e.config.workerService||t.zone_id!==e.zoneId||!S(e.config).test(t.hostname))throw Error(`Refusing to detach unmanaged hostname ${t.hostname}.`);await y(e,`/accounts/${encodeURIComponent(e.accountId)}/workers/domains/${encodeURIComponent(t.id)}`,{method:`DELETE`})}async function h(e,t){await y(e,`/accounts/${encodeURIComponent(e.accountId)}/workers/scripts/${encodeURIComponent(e.config.workerService)}/secrets`,{body:JSON.stringify({name:i,text:t,type:`secret_text`}),method:`PUT`})}async function g(e){if(!D((await y(e,`/accounts/${encodeURIComponent(e.accountId)}/workers/scripts/${encodeURIComponent(e.config.workerService)}/secrets`)).result,`secret list`).some(e=>{if(!O(e)||!k(e.name)||!k(e.type))throw Error(`Cloudflare returned an invalid Worker secret response.`);return e.name===i}))return;let t=e.env.DEPLOY_URL;if(!t)throw Error(`The staging Worker already has APP_ORIGIN but no managed hostname identifies its prior value. Refusing to mutate it without rollback information. Rerun with DEPLOY_URL set to the exact current staging origin.`);return N(t,`DEPLOY_URL`)}async function _(e){await y(e,`/accounts/${encodeURIComponent(e.accountId)}/workers/scripts/${encodeURIComponent(e.config.workerService)}/secrets/${i}`,{method:`DELETE`})}async function v(e,t){let n;for(let r=1;r<=24;r+=1)try{await e.request(t,{redirect:`manual`});return}catch(t){n=M(t),r<24&&await e.sleep(5e3)}throw Error(`Staging hostname did not become DNS/TLS/HTTP ready: ${n?.message}`)}async function y(e,t,n={}){let r=await e.request(`https://api.cloudflare.com/client/v4${t}`,{...n,headers:{Authorization:`Bearer ${e.apiToken}`,...n.body?{"Content-Type":`application/json`}:{}}}),i=await r.json().catch(()=>void 0);if(!O(i))throw Error(`Cloudflare returned an invalid response (${r.status}).`);let a=i;if(!r.ok||a.success!==!0){let e=a.errors?.map(e=>e.message).filter(Boolean).join(`; `);throw Error(`Cloudflare API request failed (${r.status})${e?`: ${e}`:`.`}`)}return a}function b(e){if(!O(e)||!k(e.id)||!k(e.hostname)||!k(e.service)||!k(e.zone_id)||!k(e.zone_name))throw Error(`Cloudflare returned an invalid Custom Domain response.`);return{hostname:e.hostname.toLowerCase(),id:e.id,service:e.service,zone_id:e.zone_id,zone_name:e.zone_name}}function x(e){let t=e.parentHostname.toLowerCase();if(e.parentHostname!==t||!A(t)||t.includes(`*`))throw Error(`deploy.stagingHostname.parentHostname must be a lowercase DNS hostname.`);if(!/^[a-z0-9-]*$/.test(e.managedLabelPrefix))throw Error(`deploy.stagingHostname.managedLabelPrefix must contain only lowercase letters, digits, or hyphens.`);let n=Math.max(...a.map(e=>e.length));if(e.managedLabelPrefix.length+n+5>63)throw Error(`deploy.stagingHostname.managedLabelPrefix is too long for a DNS label.`);if(!/^[a-z0-9][a-z0-9-]*$/.test(e.workerService))throw Error(`deploy.stagingHostname.workerService is invalid.`)}function S(e){let t=a.join(`|`);return RegExp(`^${j(e.managedLabelPrefix)}(?:${t})-[a-z]{4}\\.${j(e.parentHostname)}$`)}function C(e,t,n){let r=t.length?`\nRollback also failed: ${t.map(e=>e.message).join(`; `)}`:n?`
2
- The old staging hostname was retained and the new hostname was rolled back.`:`
2
+ The old staging hostname remained and the new hostname was rolled back.`:`
3
3
  The new hostname was rolled back and APP_ORIGIN was restored to its prior state.`;return Error(`Staging hostname reassignment failed: ${M(e).message}${r}`)}function w(e,t){let n=e[t];if(!n)throw Error(`Missing required environment variable ${t}.`);return n}function T(e){return`https://${e.hostname}/`}function E(e){if(e===void 0)return 1;if(!Number.isInteger(e)||e<1||e>1e4)throw Error(`Cloudflare returned invalid domain-list pagination.`);return e}function D(e,t){if(!Array.isArray(e))throw Error(`Cloudflare returned an invalid ${t} response.`);return e}function O(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function k(e){return typeof e==`string`&&e.length>0}function A(e){return e.length<=253&&e.split(`.`).every(e=>/^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/.test(e))}function j(e){return e.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`)}function M(e){return e instanceof Error?e:Error(String(e))}function N(e,t){let n;try{n=new URL(e)}catch{throw Error(`${t} must be an exact HTTPS origin.`)}if(n.protocol!==`https:`||n.username||n.password||n.pathname!==`/`||n.search||n.hash)throw Error(`${t} must be an exact HTTPS origin.`);return n.origin}export{u as discoverManagedDomain,o as discoverStagingUrl,l as generateAvailableHostname,c as generateStagingHostname,s as reassignStagingHostname,a as stagingHostnameWords};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coreframe/deploy",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Deployment configuration, validation, and release utilities for Coreframe apps",
5
5
  "repository": {
6
6
  "type": "git",
@@ -21,7 +21,7 @@
21
21
  "json5": "^2.2.3",
22
22
  "nano-spawn": "^2.1.0",
23
23
  "tinyglobby": "^0.2.17",
24
- "@coreframe/config": "^0.1.2"
24
+ "@coreframe/config": "^0.1.4"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@types/node": "^26.1.0",