@coreframe/preview 0.1.0 → 0.1.1
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.
- package/dist/cloudflare.js +1 -0
- package/dist/index.js +1 -1
- package/dist/keyring.js +1 -0
- package/package.json +4 -3
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{keyring as e}from"./keyring.js";import{mkdir as t,mkdtemp as n,readFile as r,rm as i,writeFile as a}from"node:fs/promises";import{join as o}from"node:path";import s from"nano-spawn";import{tmpdir as c}from"node:os";const l=process.platform===`win32`?`cloudflared.exe`:`cloudflared`,u=`dev.coreframe.preview.cloudflare.account`,d=`dev.coreframe.preview.cloudflare.tunnel`;async function f({config:e,cwd:t,env:r=process.env,now:s=()=>new Date().toISOString(),run:l=j}){let u=await p({config:e,env:r,now:s,run:l}),d=await m({account:u,config:e,cwd:t,env:r,now:s,run:l});e.dns===`managed`&&await _({account:u,config:e,cwd:t,env:r,run:l,tunnel:d});let f=await n(o(c(),`coreframe-preview-tunnel-`)),h=o(f,`tunnel.json`),g=!1;try{await a(h,`${JSON.stringify(d.credentials)}\n`,{mode:384})}catch(e){throw await i(f,{force:!0,recursive:!0}),e}return{credentialsFile:h,async dispose(){g||(g=!0,await i(f,{force:!0,recursive:!0}))},tunnel:{name:d.name,profile:d.profile,tunnelId:d.tunnelId}}}async function p({config:t,env:n,now:r,run:i}){let a=await e.get(u,t.profile);if(a)return C(a,t.profile);let o=await v({env:n,run:i}),s={createdAt:r(),profile:t.profile,originCertificate:o,version:1};return await e.set(u,t.profile,JSON.stringify(s)),s}async function m({account:t,config:n,cwd:r,env:i,now:a,run:o}){let s=A(n.profile,n.name),c=await e.get(d,s);if(c)return w(c,n);let l=(await h({account:t,cwd:r,env:i,run:o})).find(e=>e.name===n.name);if(l)throw Error(`Cloudflare tunnel "${n.name}" already exists as ${l.id}, but Coreframe has no Keychain credentials for it. Coreframe will not adopt an existing tunnel; choose a new preview.tunnel.name.`);let f=await g({account:t,config:n,cwd:r,env:i,run:o}),p={accountTag:f.AccountTag,createdAt:a(),credentials:f,hostname:n.hostname,name:n.name,profile:n.profile,tunnelId:f.TunnelID,version:1};return await e.set(d,s,JSON.stringify(p)),await e.set(u,n.profile,JSON.stringify({...t,lastAccountTag:p.accountTag,lastTunnelId:p.tunnelId,lastTunnelName:p.name})),p}async function h({account:e,cwd:t,env:n,run:r}){return y(e,async e=>D((await r([`tunnel`,`--origincert`,e,`list`,`--output`,`json`],{cwd:t,env:n})).stdout))}async function g({account:e,config:t,cwd:n,env:i,run:a}){return b(`coreframe-cloudflared-create-`,async s=>{let c=o(s,`cert.pem`),l=o(s,`tunnel.json`);return await x(c,e.originCertificate),await a([`tunnel`,`--origincert`,c,`create`,`--credentials-file`,l,t.name],{cwd:n,env:i}),T(E(await r(l,`utf8`),`tunnel credentials`))})}async function _({account:e,config:t,cwd:n,env:r,run:i,tunnel:a}){await y(e,async e=>{await i([`tunnel`,`--origincert`,e,`route`,`dns`,`--overwrite-dns`,a.tunnelId,t.hostname],{cwd:n,env:r})})}async function v({env:e,run:a}){let s=await n(o(c(),`coreframe-cloudflared-login-`)),l=o(s,`.cloudflared`);await t(l,{mode:448});try{await a([`tunnel`,`login`],{cwd:s,env:S(e,s),stdio:`inherit`});let t=await r(o(l,`cert.pem`),`utf8`);if(!t.trim())throw Error(`Cloudflare login completed without producing an origin certificate.`);return t}finally{await i(s,{force:!0,recursive:!0})}}async function y(e,t){return b(`coreframe-cloudflared-command-`,async n=>{let r=o(n,`cert.pem`);return await x(r,e.originCertificate),t(r)})}async function b(e,t){let r=await n(o(c(),e));try{return await t(r)}finally{await i(r,{force:!0,recursive:!0})}}async function x(e,t){await a(e,t,{mode:384})}function S(e,t){let n={...e};return delete n.TUNNEL_ORIGIN_CERT,{...n,HOME:t,USERPROFILE:t}}function C(e,t){let n;try{n=JSON.parse(e)}catch{throw k(`account`,t)}if(!O(n)||n.version!==1||n.profile!==t||typeof n.createdAt!=`string`||typeof n.originCertificate!=`string`||!n.originCertificate.trim())throw k(`account`,t);return n}function w(e,t){let n;try{n=JSON.parse(e)}catch{throw k(`tunnel`,`${t.profile}/${t.name}`)}if(!O(n)||n.version!==1||n.profile!==t.profile||n.name!==t.name||n.hostname!==t.hostname||typeof n.accountTag!=`string`||typeof n.createdAt!=`string`||typeof n.tunnelId!=`string`)throw k(`tunnel`,`${t.profile}/${t.name}`);let r=T(n.credentials);if(r.TunnelID!==n.tunnelId||r.AccountTag!==n.accountTag)throw k(`tunnel`,`${t.profile}/${t.name}`);return{...n,credentials:r}}function T(e){if(!O(e)||typeof e.AccountTag!=`string`||!e.AccountTag.trim()||typeof e.TunnelID!=`string`||!e.TunnelID.trim()||typeof e.TunnelSecret!=`string`||!e.TunnelSecret.trim()||e.Endpoint!==void 0&&typeof e.Endpoint!=`string`)throw Error(`Cloudflare returned invalid tunnel credentials.`);return{AccountTag:e.AccountTag,Endpoint:e.Endpoint,TunnelID:e.TunnelID,TunnelSecret:e.TunnelSecret}}function E(e,t){try{return JSON.parse(e)}catch{throw Error(`Cloudflare returned invalid ${t}.`)}}function D(e){let t;try{t=JSON.parse(e)}catch{throw Error(`Cloudflare returned an invalid tunnel list.`)}let n=Array.isArray(t)?t:O(t)&&Array.isArray(t.result)?t.result:void 0;if(!n)throw Error(`Cloudflare returned an invalid tunnel list.`);return n.map(e=>{if(!O(e)||typeof e.id!=`string`||!e.id.trim()||typeof e.name!=`string`||!e.name.trim())throw Error(`Cloudflare returned an invalid tunnel list.`);return{id:e.id,name:e.name}})}function O(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function k(e,t){return Error(`The Coreframe Cloudflare ${e} credential for "${t}" is invalid. Clear the matching Coreframe Keychain entry and run coreframe preview again.`)}function A(e,t){return`${e}/${t}`}async function j(e,{cwd:t,env:n,stdio:r=`pipe`}){let i=await s(l,e,{cwd:t,env:n,stdio:r});return{stderr:i.stderr,stdout:i.stdout}}export{f as preparePreviewTunnel};
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{preparePreviewTunnel as e}from"./cloudflare.js";import{mkdir as t,readFile as n}from"node:fs/promises";import{createServer as r}from"node:net";import i from"node:path";import a from"node:process";import{parseEnv as o}from"node:util";import{coreframeLocalWorkerVariables as s,readProjectConfig as c}from"@coreframe/config";import{migrateLocalDb as l,startProjectPostgres as u}from"@coreframe/db";import d from"nano-spawn";import{supervise as f}from"procband";const p=[`DATABASE_URL`,`PGDATABASE`,`PGHOST`,`PGPASSWORD`,`PGPORT`,`PGUSER`,`TURSO_AUTH_TOKEN`,`TURSO_DATABASE_URL`],m=[`POSTHOG_`,`VITE_POSTHOG_`,`VITE_GA_`],h=[`CI`,`COLORTERM`,`COMSPEC`,`ComSpec`,`FORCE_COLOR`,`HOME`,`NODE_EXTRA_CA_CERTS`,`NODE_OPTIONS`,`NO_COLOR`,`PATH`,`PATHEXT`,`PNPM_HOME`,`SHELL`,`SSL_CERT_DIR`,`SSL_CERT_FILE`,`SYSTEMROOT`,`SystemRoot`,`TEMP`,`TERM`,`TMP`,`TMPDIR`,`USERPROFILE`],g=a.platform===`win32`?`pnpm.cmd`:`pnpm`,_=a.platform===`win32`?`turso.cmd`:`turso`;async function v({cwd:e=a.cwd(),env:t=a.env,host:n,port:r},i){let o=await c({cwd:e}),s=y(o,{host:n,port:r}),l=await b({appOrigin:s.appOrigin,config:o,cwd:e,env:t}),u=S({config:o,cwd:e,env:l}),d,f,p,m=new Set,h=!1,g,_=()=>{if(!h){h=!0;for(let e of m)e.nodeChildProcess.then(e=>e.kill(`SIGTERM`)).catch(()=>void 0);d?.kill(`SIGTERM`),f?.kill(`SIGTERM`),u.stop()}},v=[`SIGINT`,`SIGTERM`].map(e=>{let t=()=>{g??=e,_()};return a.on(e,t),{handler:t,signal:e}});try{await u.start(),await i(x({config:s,cwd:e,database:u,env:l,processEnvironment:t,commands:m,onServe(e){d=e.server,f=e.tunnel,p=e.disposeTunnel},isStopping:()=>h,onStop:_}))}finally{let e=[...m];_();try{await u.dispose()}finally{await Promise.allSettled([...e,d?.wait(),f?.wait()].filter(Boolean)),await p?.();for(let{handler:e,signal:t}of v)a.off(t,e);g&&(a.exitCode??=g===`SIGINT`?130:143)}}}function y(e,t={}){let n=e.preview??{},r=t.host??n.host??`127.0.0.1`,i=t.port??n.port??4173,a=n.strictPort??!0,o=n.appOrigin;if(!Number.isInteger(i)||i<1||i>65535)throw Error(`Preview port must be an integer between 1 and 65535.`);if(!r.trim())throw Error(`Preview host must not be empty.`);return o&&E(o),n.tunnel&&T(n.tunnel,o,a),{appOrigin:o,host:r,port:i,strictPort:a,tunnel:n.tunnel}}async function b({appOrigin:e,config:t,cwd:n,env:r}){let i=await C(n),a=Object.keys(t.worker?.environment??{}).filter(e=>!m.some(t=>e.startsWith(t))),o=new Set([...s,...p,...a]),c={},l=new Set([...Object.keys(r),...Object.keys(i)].filter(e=>e.startsWith(`VITE_`)&&!m.some(t=>e.startsWith(t))));for(let e of h)r[e]!==void 0&&(c[e]=r[e]);for(let e of o){let t=r[e]??i[e];t!==void 0&&(c[e]=t)}for(let e of l){let t=r[e]??i[e];t!==void 0&&(c[e]=t)}return e!==void 0&&(o.add(`APP_ORIGIN`),c.APP_ORIGIN=e),c.CLOUDFLARE_ENV=`preview`,c.COREFRAME_ENV=`preview`,c.VITE_COREFRAME_ENV=`preview`,c.COREFRAME_PLATFORM=`web`,c.COREFRAME_PREVIEW_WORKER_VARIABLES=JSON.stringify([...o].filter(e=>!m.some(t=>e.startsWith(t)))),c}function x({config:t,cwd:n,database:r,env:i,processEnvironment:o,commands:s,onServe:c,isStopping:l,onStop:u}){let p=!1,m={cwd:n,database:r,env:i,host:t.host,origin:t.appOrigin??O(t.host,t.port),port:t.port,async build(){await m.run(g,[`run`,`build`,`--mode`,`preview`])},report(e,t){console.log(t===void 0?e:`${e}: ${t}`)},async run(e,t=[]){let r=d(e,t,{cwd:n,env:i,stdio:`inherit`});s.add(r);try{await r}finally{s.delete(r)}},async serve(){if(p)throw Error(`Preview serving can only be started once.`);p=!0;let r,s,d;try{if(r=t.tunnel?await e({config:t.tunnel,cwd:n,env:o}):void 0,l()){await r?.dispose();return}s=f({name:`preview`,command:g,args:[`exec`,`vite`,`preview`,`--mode`,`preview`,`--outDir`,`dist/client`,`--host`,t.host,`--port`,String(t.port),...t.strictPort?[`--strictPort`]:[]],cwd:n,env:i,prefix:!1,stdin:a.stdin}),d=r?f({name:`cloudflared`,command:a.platform===`win32`?`cloudflared.exe`:`cloudflared`,args:[`tunnel`,`run`,`--url`,`http://${A(k(t.host))}:${t.port}`,`--credentials-file`,r.credentialsFile,r.tunnel.tunnelId],cwd:n,env:i,prefix:!1}):void 0}catch(e){throw s?.kill(`SIGTERM`),await s?.wait(),await r?.dispose(),e}try{c({disposeTunnel:r?.dispose,server:s,tunnel:d}),m.report(`Preview`,m.url(`/`)),r&&m.report(`Cloudflare tunnel`,`${r.tunnel.name} (${r.tunnel.tunnelId})`),await Promise.race(d?[s.expectSuccess(),d.expectSuccess()]:[s.expectSuccess()])}catch(e){if(l()||u(),await r?.dispose(),l())return;throw e}},url(e=`/`){return new URL(e,`${m.origin.replace(/\/$/u,``)}/`).toString()}};return m}function S({config:e,cwd:n,env:r}){let a=e.database?.dialect??`postgres`,o,s,c,d=!1,p=r,m=()=>{d||(d=!0,s?.kill(`SIGTERM`))};return{dialect:a,get env(){return p},async migrate(){await l({config:e,cwd:n,env:p})},async start(){if(a===`sqlite`){let e=await j(),r=i.resolve(n,`.coreframe`),a=i.join(r,`turso.db`);await t(r,{recursive:!0}),delete p.DATABASE_URL,p.TURSO_DATABASE_URL=`http://127.0.0.1:${e}`,p.TURSO_AUTH_TOKEN=`dev`,s=f({name:`turso`,command:_,args:[`dev`,`--port`,String(e),`--db-file`,a],cwd:n,env:{...p},prefix:!1}),await s.waitFor(`listening`);return}o=await u({config:e,cwd:n,env:p,failIfRunning:!0,postgresOutput:`on-error`}),Object.assign(p,o.env)},stop:m,async dispose(){c??=(async()=>{m(),await Promise.allSettled([o?.[Symbol.asyncDispose](),s?.wait()]),o=void 0,s=void 0})(),await c}}}async function C(e){let t={};for(let n of[`.env`,`.env.local`,`.env.preview`,`.env.preview.local`]){let r=await w(i.resolve(e,n));r&&(t={...t,...r})}let n=await w(i.resolve(e,`.dev.vars`));n&&(t={...t,...n});let r=await w(i.resolve(e,`.dev.vars.preview`));return r&&(t={...t,...r}),t}async function w(e){try{return Object.fromEntries(Object.entries(o(await n(e,`utf8`))).filter(e=>e[1]!==void 0))}catch(e){if(e.code===`ENOENT`)return;throw e}}function T(e,t,n){if(!t)throw Error(`preview.appOrigin is required when preview.tunnel is configured.`);if(!n)throw Error(`preview.strictPort must be true when preview.tunnel is configured.`);let r=[`credentialsFile`,`tokenEnv`,`tokenFile`],i=e,a=r.find(e=>e in i);if(a)throw Error(`preview.tunnel.${a} is no longer supported. Coreframe preview now provisions Cloudflare credentials through the operating system credential store.`);if(!e.name.trim())throw Error(`preview.tunnel.name must not be empty.`);if(!/^[a-z0-9][a-z0-9._-]*$/u.test(e.profile))throw Error(`preview.tunnel.profile must start with a lowercase letter or number and contain only lowercase letters, numbers, dots, underscores, or hyphens.`);if(e.dns!==`managed`&&e.dns!==`external`)throw Error(`preview.tunnel.dns must be either "managed" or "external".`);let o=D(e.hostname);if(new URL(t).hostname!==o)throw Error(`preview.appOrigin hostname must match preview.tunnel.hostname.`)}function E(e){let t;try{t=new URL(e)}catch{throw Error(`preview.appOrigin must be an exact HTTPS browser origin.`)}if(t.protocol!==`https:`||t.origin!==e||t.pathname!==`/`||t.search||t.hash||t.username||t.password)throw Error(`preview.appOrigin must be an exact HTTPS browser origin.`);return t}function D(e){let t;try{t=new URL(`https://${e}`)}catch{throw Error(`preview.tunnel.hostname must be an exact hostname.`)}if(t.hostname!==e||t.origin!==`https://${e}`)throw Error(`preview.tunnel.hostname must be an exact hostname.`);return t.hostname}function O(e,t){return`http://${A(k(e))}:${t}`}function k(e){return e===`0.0.0.0`||e===`::`?`127.0.0.1`:e}function A(e){return e.includes(`:`)&&!e.startsWith(`[`)?`[${e}]`:e}async function j(){let e=r();await new Promise((t,n)=>{e.once(`error`,n),e.listen(0,`127.0.0.1`,()=>t())});let t=e.address();if(await new Promise((t,n)=>{e.close(e=>e?n(e):t())}),!t||typeof t==`string`)throw Error(`Could not allocate a local preview database port.`);return t.port}export{b as createPreviewEnvironment,y as resolvePreviewConfig,v as runPreview};
|
package/dist/keyring.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{AsyncEntry as e}from"@napi-rs/keyring";const t={async get(t,n){return await new e(t,n).getPassword()??void 0},async set(t,n,r){await new e(t,n).setPassword(r)}};export{t as keyring};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@coreframe/preview",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Production-like local preview lifecycle utilities for Coreframe apps",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -18,10 +18,11 @@
|
|
|
18
18
|
}
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
+
"@napi-rs/keyring": "1.3.0",
|
|
21
22
|
"nano-spawn": "^2.1.0",
|
|
22
23
|
"procband": "^0.3.8",
|
|
23
|
-
"@coreframe/config": "^0.1.
|
|
24
|
-
"@coreframe/db": "^0.1.
|
|
24
|
+
"@coreframe/config": "^0.1.3",
|
|
25
|
+
"@coreframe/db": "^0.1.3"
|
|
25
26
|
},
|
|
26
27
|
"devDependencies": {
|
|
27
28
|
"@types/node": "^26.1.0",
|