@lunora/cli 1.0.0-alpha.179 → 1.0.0-alpha.180

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/index.mjs CHANGED
@@ -1 +1 @@
1
- import{C as o,V as a,r as n}from"./packem_shared/cli-DuCR0hN5.mjs";import{runCodegenCommand as t}from"./packem_chunks/runCodegenCommand.mjs";import{runDeployCommand as p}from"./packem_chunks/runDeployCommand.mjs";import{planDevCommand as x,runDevCommand as i}from"./packem_chunks/planDevCommand.mjs";import{runInitCommand as s}from"./packem_chunks/runInitCommand.mjs";import{runMigrateGenerateCommand as u}from"./packem_chunks/runMigrateGenerateCommand.mjs";import{runResetCommand as g}from"./packem_chunks/runResetCommand.mjs";import{runRpcCommand as R}from"./packem_chunks/runRpcCommand.mjs";import{insertSchemaExtension as E}from"./packem_shared/insertSchemaExtension-DY4pA8wE.mjs";import{createLogger as A,pail as c}from"./packem_shared/createLogger-hGafwf-3.mjs";import{diffSnapshots as v,renderAddColumn as _,renderCreateIndex as b,renderCreateTable as h,renderDropIndex as y,renderDropTable as L,renderMigrationFile as O,validatorKindToSqlType as w}from"./packem_shared/diffSnapshots-CIOFQN_a.mjs";import{default as F}from"./packem_shared/schemaIrToSnapshot-qpRoV7HY.mjs";import{createRecordingSpawner as U,defaultSpawner as V}from"./packem_shared/createRecordingSpawner-DJlNacZ6.mjs";import{default as G}from"./packem_shared/parseManifest-CwPTKdtS.mjs";import{D as Q,r as j}from"./packem_shared/import-Db3Gk4mx.mjs";import{REQUIRED_COMPATIBILITY_DATE as H,REQUIRED_FLAG as K,validateWranglerProject as Y,validateWranglerConfig as Z}from"@lunora/config/cloudflare";import{buildRegistryIndex as z}from"./packem_shared/buildRegistryIndex-Zi3bBqpc.mjs";import{r as X,a as $,b as rr}from"./packem_shared/commands-BOgN2nF7.mjs";import{runExportCommand as or}from"./packem_shared/runExportCommand-DmXKEWSq.mjs";export{o as COMMANDS,Q as DEFAULT_IMPORT_BATCH_SIZE,H as REQUIRED_COMPATIBILITY_DATE,K as REQUIRED_FLAG,a as VERSION,z as buildRegistryIndex,A as createLogger,U as createRecordingSpawner,V as defaultSpawner,v as diffSnapshots,E as insertSchemaExtension,c as pail,G as parseManifest,x as planDevCommand,_ as renderAddColumn,b as renderCreateIndex,h as renderCreateTable,y as renderDropIndex,L as renderDropTable,O as renderMigrationFile,X as runAddCommand,$ as runBuildIndexCommand,n as runCli,t as runCodegenCommand,p as runDeployCommand,i as runDevCommand,or as runExportCommand,j as runImportCommand,s as runInitCommand,u as runMigrateGenerateCommand,rr as runRegistryViewCommand,g as runResetCommand,R as runRpcCommand,F as schemaIrToSnapshot,Y as validateWrangler,Z as validateWranglerConfig,w as validatorKindToSqlType};
1
+ import{C as o,V as a,r as n}from"./packem_shared/cli-DuCR0hN5.mjs";import{runCodegenCommand as t}from"./packem_chunks/runCodegenCommand.mjs";import{runDeployCommand as p}from"./packem_chunks/runDeployCommand.mjs";import{planDevCommand as x,runDevCommand as i}from"./packem_chunks/planDevCommand.mjs";import{runInitCommand as s}from"./packem_chunks/runInitCommand.mjs";import{runMigrateGenerateCommand as u}from"./packem_chunks/runMigrateGenerateCommand.mjs";import{runResetCommand as g}from"./packem_chunks/runResetCommand.mjs";import{runRpcCommand as R}from"./packem_chunks/runRpcCommand.mjs";import{insertSchemaExtension as E}from"./packem_shared/insertSchemaExtension-DY4pA8wE.mjs";import{createLogger as A,pail as c}from"./packem_shared/createLogger-hGafwf-3.mjs";import{diffSnapshots as v,renderAddColumn as _,renderCreateIndex as b,renderCreateTable as h,renderDropIndex as y,renderDropTable as L,renderMigrationFile as O,validatorKindToSqlType as w}from"./packem_shared/diffSnapshots-CIOFQN_a.mjs";import{default as F}from"./packem_shared/schemaIrToSnapshot-qpRoV7HY.mjs";import{createRecordingSpawner as U,defaultSpawner as V}from"./packem_shared/createRecordingSpawner-DJlNacZ6.mjs";import{default as G}from"./packem_shared/parseManifest-CwPTKdtS.mjs";import{D as Q,r as j}from"./packem_shared/import-CrVGrOt3.mjs";import{REQUIRED_COMPATIBILITY_DATE as H,REQUIRED_FLAG as K,validateWranglerProject as Y,validateWranglerConfig as Z}from"@lunora/config/cloudflare";import{buildRegistryIndex as z}from"./packem_shared/buildRegistryIndex-Zi3bBqpc.mjs";import{r as X,a as $,b as rr}from"./packem_shared/commands-BOgN2nF7.mjs";import{runExportCommand as or}from"./packem_shared/runExportCommand-DmXKEWSq.mjs";export{o as COMMANDS,Q as DEFAULT_IMPORT_BATCH_SIZE,H as REQUIRED_COMPATIBILITY_DATE,K as REQUIRED_FLAG,a as VERSION,z as buildRegistryIndex,A as createLogger,U as createRecordingSpawner,V as defaultSpawner,v as diffSnapshots,E as insertSchemaExtension,c as pail,G as parseManifest,x as planDevCommand,_ as renderAddColumn,b as renderCreateIndex,h as renderCreateTable,y as renderDropIndex,L as renderDropTable,O as renderMigrationFile,X as runAddCommand,$ as runBuildIndexCommand,n as runCli,t as runCodegenCommand,p as runDeployCommand,i as runDevCommand,or as runExportCommand,j as runImportCommand,s as runInitCommand,u as runMigrateGenerateCommand,rr as runRegistryViewCommand,g as runResetCommand,R as runRpcCommand,F as schemaIrToSnapshot,Y as validateWrangler,Z as validateWranglerConfig,w as validatorKindToSqlType};
@@ -1,3 +1,3 @@
1
- import{existsSync as S}from"node:fs";import{mkdtemp as k,writeFile as v,rm as R}from"node:fs/promises";import{tmpdir as T}from"node:os";import{discoverSchema as $,schemaFromIr as j}from"@lunora/codegen";import{seedPlan as C}from"@lunora/seed";import{join as m}from"@visulima/path";import{Project as F}from"ts-morph";import{i as x}from"../packem_shared/admin-token-rxXZbVMZ.mjs";import{d as z}from"../packem_shared/command-DwC11Cwe.mjs";import{a as I}from"../packem_shared/resolve-target-C_ZloTvd.mjs";import{b as A}from"../packem_shared/tui-prompts-Dd0pDuNr.mjs";import{runResetCommand as P}from"./runResetCommand.mjs";import{r as U}from"../packem_shared/import-Db3Gk4mx.mjs";const b=e=>e===""?!1:x(e),L=(e,r)=>typeof r=="bigint"?Number(r):r instanceof ArrayBuffer?[...new Uint8Array(r)]:r,l=e=>({code:e,conflicts:0,generated:0,inserted:0,ndjson:""}),N=(e,r)=>{if(!S(r))return e.logger.error(`schema not found: ${r} — run \`vis generate lunora-table --name=<name>\` to create one`),l(1);if(e.reset===!0&&(e.prod===!0||!b(e.url)))return e.logger.error("--reset only clears local .wrangler/state and cannot be combined with --prod or a remote --url"),l(1)},_=async(e,r,t,n)=>{const i=await k(m(T(),"lunora-seed-")),s=m(i,"rows.ndjson");await v(s,e,"utf8");try{const o=await U({batchSize:n.batchSize,cwd:t,fetchImpl:n.fetchImpl,file:s,logger:n.logger,prod:n.prod,token:n.token,url:n.url}),d=o.body?.conflicts??0;return d>0&&n.logger.warn(`${String(d)} row(s) skipped — their _id already exists. Seeding is deterministic; re-run with --reset to wipe local state first, or a different --seed for fresh ids.`),{code:o.code,conflicts:d,generated:r,inserted:o.inserted,ndjson:e}}finally{await R(i,{force:!0,recursive:!0}).catch(()=>{})}},B=(e,r)=>{if(e.table===void 0||r.tables.some(n=>n.name===e.table))return;const t=r.tables.map(n=>n.name).join(", ");return e.logger.error(`unknown table "${e.table}" — schema defines: ${t||"(no tables)"}`),l(1)},D=async(e,r)=>{if(!(e.prod===!0||!b(e.url))||e.yes===!0)return;if(!process.stdin.isTTY&&e.confirm===void 0)return e.logger.error("seed: refusing to insert into a non-local target without confirmation — re-run with --yes"),l(1);if(!await(e.confirm??A)(`This will insert ${String(r)} generated row(s) into ${e.url??"the production worker"}. Continue?`))return e.logger.info("seed: aborted"),l(1)},H=async e=>{const r=e.cwd??process.cwd(),t=m(r,"lunora","schema.ts"),n=N(e,t);if(n!==void 0)return n;const i=new F({skipAddingFilesFromTsConfig:!0}),s=$(i,t),o=B(e,s);if(o!==void 0)return o;const d=j(s),g=C(d,{defaultCount:e.count??10,now:e.now,only:e.table===void 0?void 0:[e.table],seed:e.seed??0}),u=[];for(const{rows:f,table:h}of g)for(const y of f)u.push(JSON.stringify({doc:y,table:h},L));const a=u.length>0?`${u.join(`
1
+ import{existsSync as S}from"node:fs";import{mkdtemp as k,writeFile as v,rm as R}from"node:fs/promises";import{tmpdir as T}from"node:os";import{discoverSchema as $,schemaFromIr as j}from"@lunora/codegen";import{seedPlan as C}from"@lunora/seed";import{join as m}from"@visulima/path";import{Project as F}from"ts-morph";import{i as x}from"../packem_shared/admin-token-rxXZbVMZ.mjs";import{d as z}from"../packem_shared/command-DwC11Cwe.mjs";import{a as I}from"../packem_shared/resolve-target-C_ZloTvd.mjs";import{b as A}from"../packem_shared/tui-prompts-Dd0pDuNr.mjs";import{runResetCommand as P}from"./runResetCommand.mjs";import{r as U}from"../packem_shared/import-CrVGrOt3.mjs";const b=e=>e===""?!1:x(e),L=(e,r)=>typeof r=="bigint"?Number(r):r instanceof ArrayBuffer?[...new Uint8Array(r)]:r,l=e=>({code:e,conflicts:0,generated:0,inserted:0,ndjson:""}),N=(e,r)=>{if(!S(r))return e.logger.error(`schema not found: ${r} — run \`vis generate lunora-table --name=<name>\` to create one`),l(1);if(e.reset===!0&&(e.prod===!0||!b(e.url)))return e.logger.error("--reset only clears local .wrangler/state and cannot be combined with --prod or a remote --url"),l(1)},_=async(e,r,t,n)=>{const i=await k(m(T(),"lunora-seed-")),s=m(i,"rows.ndjson");await v(s,e,"utf8");try{const o=await U({batchSize:n.batchSize,cwd:t,fetchImpl:n.fetchImpl,file:s,logger:n.logger,prod:n.prod,token:n.token,url:n.url}),d=o.body?.conflicts??0;return d>0&&n.logger.warn(`${String(d)} row(s) skipped — their _id already exists. Seeding is deterministic; re-run with --reset to wipe local state first, or a different --seed for fresh ids.`),{code:o.code,conflicts:d,generated:r,inserted:o.inserted,ndjson:e}}finally{await R(i,{force:!0,recursive:!0}).catch(()=>{})}},B=(e,r)=>{if(e.table===void 0||r.tables.some(n=>n.name===e.table))return;const t=r.tables.map(n=>n.name).join(", ");return e.logger.error(`unknown table "${e.table}" — schema defines: ${t||"(no tables)"}`),l(1)},D=async(e,r)=>{if(!(e.prod===!0||!b(e.url))||e.yes===!0)return;if(!process.stdin.isTTY&&e.confirm===void 0)return e.logger.error("seed: refusing to insert into a non-local target without confirmation — re-run with --yes"),l(1);if(!await(e.confirm??A)(`This will insert ${String(r)} generated row(s) into ${e.url??"the production worker"}. Continue?`))return e.logger.info("seed: aborted"),l(1)},H=async e=>{const r=e.cwd??process.cwd(),t=m(r,"lunora","schema.ts"),n=N(e,t);if(n!==void 0)return n;const i=new F({skipAddingFilesFromTsConfig:!0}),s=$(i,t),o=B(e,s);if(o!==void 0)return o;const d=j(s),g=C(d,{defaultCount:e.count??10,now:e.now,only:e.table===void 0?void 0:[e.table],seed:e.seed??0}),u=[];for(const{rows:f,table:h}of g)for(const y of f)u.push(JSON.stringify({doc:y,table:h},L));const a=u.length>0?`${u.join(`
2
2
  `)}
3
3
  `:"",c=u.length;if(e.dryRun===!0)return a.length>0&&process.stdout.write(a),e.logger.info(`generated ${String(c)} row(s) across ${String(g.length)} table(s) — dry run, nothing inserted`),{code:0,conflicts:0,generated:c,inserted:0,ndjson:a};if(e.reset===!0){const f=await P({cwd:r,logger:e.logger,yes:!0});if(f.code!==0)return{code:f.code,conflicts:0,generated:c,inserted:0,ndjson:a}}if(c===0)return e.logger.warn("no rows generated — nothing to insert"),{code:0,conflicts:0,generated:0,inserted:0,ndjson:a};const w=await D(e,c);return w!==void 0?w:_(a,c,r,e)},p=z(async({cwd:e,logger:r,options:t})=>({code:(await H({batchSize:t.batchSize,count:t.count,cwd:e,dryRun:t.dryRun===!0,logger:r,prod:t.prod===!0,reset:t.reset===!0,now:t.now,seed:t.seed,table:t.table,token:t.token,url:I({cwd:e,prod:t.prod===!0,url:t.url}),yes:t.yes===!0})).code}));export{p as execute,H as runSeedCommand};
@@ -1,3 +1,3 @@
1
- import{writeFileSync as E,existsSync as y,cpSync as C,statSync as b,readdirSync as F,mkdirSync as D,readFileSync as L}from"node:fs";import{join as f,sep as j,resolve as O,dirname as I}from"node:path";import{SDK_TARGETS as M,SDK_LANGUAGES as x,generateSdk as B,isTypedSchema as K}from"@lunora/codegen";import{LunoraError as l}from"@lunora/errors";import{d as V}from"../packem_shared/command-DwC11Cwe.mjs";import{r as _,a as U,b as G,c as P,s as Q}from"../packem_shared/resolve-DZAGzg67.mjs";const S="gh:anolilab/lunora/sdks",v="lunora-transport.json",J=/_test\.|Tests?\./u,q=r=>r.startsWith("test_")||J.test(r),N=(r,s="")=>F(r).toSorted((e,t)=>e.localeCompare(t)).flatMap(e=>{const t=f(r,e),a=s.length>0?`${s}/${e}`:e;return b(t).isDirectory()?N(t,a):[a]}),A=(r,s,e,t)=>{const a=f(r,e);if(!y(a))throw new l("NOT_FOUND",`transport is missing ${e}`);const n=f(s,t);return C(a,n,{filter:o=>!q(o.slice(o.lastIndexOf(j)+1)),force:!0,recursive:!0}),b(n).isDirectory()?N(n).map(o=>`${t}/${o}`):[t]},k=(r,s)=>s.vendor.every(e=>y(f(r,e.from))),H=async(r,s)=>{const{language:e,logger:t,outputDirectory:a,registryOptions:n,target:o}=s,{cleanup:u,directory:h}=await P(e,{...n,ref:r},S);try{if(!k(h,o)){t.warn(`${S}/${e} does not exist at ${r}.`);return}return{files:o.vendor.flatMap(c=>A(h,a,c.from,c.to)).toSorted((c,d)=>c.localeCompare(d)),ref:r,source:`${S}/${e}`,versionMatched:r===_()}}finally{u()}},W=(r,s)=>{const{language:e,outputDirectory:t,target:a}=s,n=f(r,e);if(!y(n))throw new l("NOT_FOUND",`no transport at ${n} — --from must point at a directory of per-language transports (the repo's \`sdks/\`)`);if(!k(n,a)){const o=a.vendor.map(u=>u.from).join(", ");throw new l("NOT_FOUND",`${n} is not a ${e} transport — expected it to contain ${o}`)}return{files:a.vendor.flatMap(o=>A(n,t,o.from,o.to)).toSorted((o,u)=>o.localeCompare(u)),ref:"local",source:n,versionMatched:!1}},X=async r=>{const{allowUnsafeSource:s,from:e,language:t,logger:a,outputDirectory:n,ref:o,source:u,target:h}=r;if(e!==void 0)return W(e,{language:t,outputDirectory:n,target:h});const c={allowUnsafeSource:s,logger:a,names:[],source:u},d=async i=>H(await G(i,a),{language:t,logger:a,outputDirectory:n,registryOptions:c,target:h});if(o!==void 0&&o.length>0){const i=await d(o);if(i===void 0)throw new l("NOT_FOUND",`--ref ${o} carries no ${t} transport — pick a ref that does, or pass --from <dir> to copy a local one`);return i}const m=_(),g=await d(m).catch(i=>{const w=i instanceof Error?i.message.split(`
2
- `)[0]??i.message:String(i);a.warn(`could not fetch the transport at ${m} (${w}).`)});if(g!==void 0)return g;const p=await d(void 0);if(p===void 0)throw new l("NOT_FOUND",`no ${t} transport at ${m} or on the release branch. This language may not be released yet — pass --from <dir> to copy one from a checkout, or --ref <tag> to name a ref that has it.`);return a.warn(`FALLBACK: the transport is NOT version-matched to this CLI (${U()}). Wanted ${m}, used ${p.ref}. The vendored wire protocol may not be the one this surface was generated against; ${v} records what was actually copied.`),p},z=(r,s,e)=>{const t={cliVersion:U(),files:e.files,language:s,note:e.versionMatched?"Vendored by `lunora sdk generate` at the ref matching the CLI that generated the surface beside it. Regenerate with a newer CLI to upgrade.":"Vendored by `lunora sdk generate` from a ref that is NOT this CLI's release tag, so the transport and the generated surface may be different protocol vintages.",ref:e.ref,source:e.source,versionMatched:e.versionMatched};E(f(r,v),`${JSON.stringify(t,void 0,4)}
3
- `,"utf8")},Y=f("lunora","_generated","openrpc.json"),Z=r=>{if(!y(r))throw new l("NOT_FOUND",`no OpenRPC document at ${r} — run \`lunora codegen --api-spec openrpc\` (or \`--api-spec both\`) first, or pass --spec`);let s;try{s=JSON.parse(L(r,"utf8"))}catch(e){throw new l("BAD_REQUEST",`${r} is not valid JSON: ${e instanceof Error?e.message:String(e)}`)}if(s===null||typeof s!="object"||!Array.isArray(s.methods))throw new l("BAD_REQUEST",`${r} is not an OpenRPC document (no \`methods\` array)`);return s},ae=V(async({argument:r,cwd:s,logger:e,options:t})=>{const a=r[0]??"generate";if(a!=="generate")throw new l("BAD_REQUEST",`unknown subcommand "${a}" — the only one is \`lunora sdk generate\``);const n=t.lang??"python",o=M[n];if(o===void 0)throw new l("BAD_REQUEST",`unsupported --lang "${n}" — expected one of: ${x.join(" | ")}`);const u=Q("sdk generate",{allowUnsafeSource:t.allowUnsafeSource,logger:e,names:[],source:t.source});if(u!==void 0)throw new l("BAD_REQUEST",u);const h=O(s,t.spec??Y),c=O(s,t.out??f("sdk",n)),d=Z(h);d.methods.length===0&&e.warn(`${h} declares no methods — writing an empty SDK.`);const{files:m,undeclared:g,unrepresentable:p}=await B(d,o);D(c,{recursive:!0});const i=await X({allowUnsafeSource:t.allowUnsafeSource,from:t.from,language:n,logger:e,outputDirectory:c,ref:t.ref,source:t.source,target:o});z(c,n,i);for(const[$,R]of Object.entries(m)){const T=f(c,$);D(I(T),{recursive:!0}),E(T,R,"utf8")}const w=d.methods.filter($=>!K($.result?.schema)).length;return e.success(`Generated ${n} SDK for ${String(d.methods.length)} function(s) → ${c}`),e.info(`Vendored the ${n} transport (${String(i.files.length)} file(s)) from ${i.source} @ ${i.ref}${i.versionMatched?" — version-matched to this CLI":""}; see ${v}.`),e.info(o.requires.length>0?`Install in the consuming project: ${o.requires.join(", ")}.`:"Nothing to install — the transport is vendored and needs only the standard library."),g.length>0&&e.warn(`${String(g.length)} schema(s) are not expressible as a named ${n} model, so those arguments or returns stay untyped: ${g.join(", ")}`),p.length>0&&e.warn(`${String(p.length)} function(s) take or return a \`v.bigint()\`/\`v.bytes()\`, which no generated model can carry — their parameters stay untyped, so pass wire values directly: ${p.join(", ")}`),w>0&&e.warn(`${String(w)} of ${String(d.methods.length)} function(s) return an untyped result — declare \`.output()\` on them for typed returns.`),{code:0}});export{ae as execute};
1
+ import{writeFileSync as E,existsSync as w,cpSync as j,statSync as b,lstatSync as I,readdirSync as N,mkdirSync as D,readFileSync as M}from"node:fs";import{join as u,sep as x,relative as B,resolve as O,dirname as K}from"node:path";import{SDK_TARGETS as V,SDK_LANGUAGES as G,generateSdk as P,isTypedSchema as Q}from"@lunora/codegen";import{LunoraError as d}from"@lunora/errors";import{d as J}from"../packem_shared/command-DwC11Cwe.mjs";import{r as _,a as U,b as q,c as H,s as W}from"../packem_shared/resolve-DZAGzg67.mjs";const $="gh:anolilab/lunora/sdks",v="lunora-transport.json",X=/_test\.|Tests?\./u,k=t=>t.startsWith("test_")||X.test(t),A=(t,n="")=>N(t).toSorted((e,r)=>e.localeCompare(r)).flatMap(e=>{const r=u(t,e),a=n.length>0?`${n}/${e}`:e;return b(r).isDirectory()?A(r,a):[a]}),C=(t,n)=>{const e=I(n);if(e.isSymbolicLink())throw new d("INTERNAL",`refusing to vendor "${B(t,n)}" — it is a symlink, not a regular file`);if(e.isDirectory())for(const r of N(n))k(r)||C(t,u(n,r))},F=(t,n,e,r)=>{const a=u(t,e);if(!w(a))throw new d("NOT_FOUND",`transport is missing ${e}`);C(t,a);const s=u(n,r);return j(a,s,{filter:o=>!k(o.slice(o.lastIndexOf(x)+1)),force:!0,recursive:!0}),b(s).isDirectory()?A(s).map(o=>`${r}/${o}`):[r]},R=(t,n)=>n.vendor.every(e=>w(u(t,e.from))),z=async(t,n)=>{const{language:e,logger:r,outputDirectory:a,registryOptions:s,target:o}=n,{cleanup:f,directory:h}=await H(e,{...s,ref:t},$);try{if(!R(h,o)){r.warn(`${$}/${e} does not exist at ${t}.`);return}return{files:o.vendor.flatMap(c=>F(h,a,c.from,c.to)).toSorted((c,l)=>c.localeCompare(l)),ref:t,source:`${$}/${e}`,versionMatched:t===_()}}finally{f()}},Y=(t,n)=>{const{language:e,outputDirectory:r,target:a}=n,s=u(t,e);if(!w(s))throw new d("NOT_FOUND",`no transport at ${s} — --from must point at a directory of per-language transports (the repo's \`sdks/\`)`);if(!R(s,a)){const o=a.vendor.map(f=>f.from).join(", ");throw new d("NOT_FOUND",`${s} is not a ${e} transport — expected it to contain ${o}`)}return{files:a.vendor.flatMap(o=>F(s,r,o.from,o.to)).toSorted((o,f)=>o.localeCompare(f)),ref:"local",source:s,versionMatched:!1}},Z=async t=>{const{allowUnsafeSource:n,from:e,language:r,logger:a,outputDirectory:s,ref:o,source:f,target:h}=t;if(e!==void 0)return Y(e,{language:r,outputDirectory:s,target:h});const c={allowUnsafeSource:n,logger:a,names:[],source:f},l=async i=>z(await q(i,a),{language:r,logger:a,outputDirectory:s,registryOptions:c,target:h});if(o!==void 0&&o.length>0){const i=await l(o);if(i===void 0)throw new d("NOT_FOUND",`--ref ${o} carries no ${r} transport — pick a ref that does, or pass --from <dir> to copy a local one`);return i}const m=_(),g=await l(m).catch(i=>{const y=i instanceof Error?i.message.split(`
2
+ `)[0]??i.message:String(i);a.warn(`could not fetch the transport at ${m} (${y}).`)});if(g!==void 0)return g;const p=await l(void 0);if(p===void 0)throw new d("NOT_FOUND",`no ${r} transport at ${m} or on the release branch. This language may not be released yet — pass --from <dir> to copy one from a checkout, or --ref <tag> to name a ref that has it.`);return a.warn(`FALLBACK: the transport is NOT version-matched to this CLI (${U()}). Wanted ${m}, used ${p.ref}. The vendored wire protocol may not be the one this surface was generated against; ${v} records what was actually copied.`),p},ee=(t,n,e)=>{const r={cliVersion:U(),files:e.files,language:n,note:e.versionMatched?"Vendored by `lunora sdk generate` at the ref matching the CLI that generated the surface beside it. Regenerate with a newer CLI to upgrade.":"Vendored by `lunora sdk generate` from a ref that is NOT this CLI's release tag, so the transport and the generated surface may be different protocol vintages.",ref:e.ref,source:e.source,versionMatched:e.versionMatched};E(u(t,v),`${JSON.stringify(r,void 0,4)}
3
+ `,"utf8")},te=u("lunora","_generated","openrpc.json"),re=t=>{if(!w(t))throw new d("NOT_FOUND",`no OpenRPC document at ${t} — run \`lunora codegen --api-spec openrpc\` (or \`--api-spec both\`) first, or pass --spec`);let n;try{n=JSON.parse(M(t,"utf8"))}catch(e){throw new d("BAD_REQUEST",`${t} is not valid JSON: ${e instanceof Error?e.message:String(e)}`)}if(n===null||typeof n!="object"||!Array.isArray(n.methods))throw new d("BAD_REQUEST",`${t} is not an OpenRPC document (no \`methods\` array)`);return n},de=J(async({argument:t,cwd:n,logger:e,options:r})=>{const a=t[0]??"generate";if(a!=="generate")throw new d("BAD_REQUEST",`unknown subcommand "${a}" — the only one is \`lunora sdk generate\``);const s=r.lang??"python",o=V[s];if(o===void 0)throw new d("BAD_REQUEST",`unsupported --lang "${s}" — expected one of: ${G.join(" | ")}`);const f=W("sdk generate",{allowUnsafeSource:r.allowUnsafeSource,logger:e,names:[],source:r.source});if(f!==void 0)throw new d("BAD_REQUEST",f);const h=O(n,r.spec??te),c=O(n,r.out??u("sdk",s)),l=re(h);l.methods.length===0&&e.warn(`${h} declares no methods — writing an empty SDK.`);const{files:m,undeclared:g,unrepresentable:p}=await P(l,o);D(c,{recursive:!0});const i=await Z({allowUnsafeSource:r.allowUnsafeSource,from:r.from,language:s,logger:e,outputDirectory:c,ref:r.ref,source:r.source,target:o});ee(c,s,i);for(const[S,L]of Object.entries(m)){const T=u(c,S);D(K(T),{recursive:!0}),E(T,L,"utf8")}const y=l.methods.filter(S=>!Q(S.result?.schema)).length;return e.success(`Generated ${s} SDK for ${String(l.methods.length)} function(s) → ${c}`),e.info(`Vendored the ${s} transport (${String(i.files.length)} file(s)) from ${i.source} @ ${i.ref}${i.versionMatched?" — version-matched to this CLI":""}; see ${v}.`),e.info(o.requires.length>0?`Install in the consuming project: ${o.requires.join(", ")}.`:"Nothing to install — the transport is vendored and needs only the standard library."),g.length>0&&e.warn(`${String(g.length)} schema(s) are not expressible as a named ${s} model, so those arguments or returns stay untyped: ${g.join(", ")}`),p.length>0&&e.warn(`${String(p.length)} function(s) take or return a \`v.bigint()\`/\`v.bytes()\`, which no generated model can carry — their parameters stay untyped, so pass wire values directly: ${p.join(", ")}`),y>0&&e.warn(`${String(y)} of ${String(l.methods.length)} function(s) return an untyped result — declare \`.output()\` on them for typed returns.`),{code:0}});export{de as execute};
@@ -1,3 +1,3 @@
1
- import K,{join as l,resolve as T}from"node:path";import{isInteractive as F,promptYesNo as z}from"@lunora/config";import{backupObjectKey as p,isBackupManifestEntry as x,normalizeBackupPrefix as H,BACKUP_KEY_PREFIX as J,backupManifestKey as G,isBackupManifestKey as Y,backupObjectKeyOfManifest as O}from"@lunora/runtime";import{r as A}from"../packem_shared/admin-token-rxXZbVMZ.mjs";import{r as v}from"../packem_shared/admin-url-Cil3Or6N.mjs";import{d as W}from"../packem_shared/command-DwC11Cwe.mjs";import{a as X}from"../packem_shared/resolve-target-C_ZloTvd.mjs";import{readAndLogBody as b}from"./runRpcCommand.mjs";import{randomUUID as U,createHash as Q}from"node:crypto";import{existsSync as $,createReadStream as V,createWriteStream as Z}from"node:fs";import{mkdir as ee,rm as w,rename as B,readFile as D,writeFile as re,realpath as N,mkdtemp as S}from"node:fs/promises";import{LunoraError as h}from"@lunora/errors";import{i as E}from"../packem_shared/path-containment-CgxYZggb.mjs";import{tmpdir as I}from"node:os";import{Readable as te}from"node:stream";import{pipeline as ae}from"node:stream/promises";import{b as oe,l as ne,M as _,u as ie,r as ce}from"../packem_shared/import-Db3Gk4mx.mjs";import{runExportCommand as se}from"../packem_shared/runExportCommand-DmXKEWSq.mjs";const y="manifest.json",j=async e=>{const r=Q("sha256");let a=0;for await(const t of V(e)){const o=t;a+=o.byteLength,r.update(o)}return{bytes:a,sha256:r.digest("hex")}},P=async e=>{const r=l(e,y);if(!$(r))return[];let a;try{a=JSON.parse(await D(r,"utf8"))}catch(t){const o=t instanceof Error?t.message:String(t);throw new h("INTERNAL",`backup: ${r} exists but is not valid JSON (${o}) — refusing to overwrite it; fix or remove it manually`,{cause:t})}if(!Array.isArray(a))throw new TypeError(`backup: ${r} exists but is not a JSON array — refusing to overwrite it; fix or remove it manually`);return a.filter(t=>x(t))},ue=async(e,r)=>{const a=l(e,`${y}.${U()}.tmp`);try{await re(a,`${JSON.stringify(r,void 0,2)}
1
+ import K,{join as l,resolve as T}from"node:path";import{isInteractive as F,promptYesNo as z}from"@lunora/config";import{backupObjectKey as p,isBackupManifestEntry as x,normalizeBackupPrefix as H,BACKUP_KEY_PREFIX as J,backupManifestKey as G,isBackupManifestKey as Y,backupObjectKeyOfManifest as O}from"@lunora/runtime";import{r as A}from"../packem_shared/admin-token-rxXZbVMZ.mjs";import{r as v}from"../packem_shared/admin-url-Cil3Or6N.mjs";import{d as W}from"../packem_shared/command-DwC11Cwe.mjs";import{a as X}from"../packem_shared/resolve-target-C_ZloTvd.mjs";import{readAndLogBody as b}from"./runRpcCommand.mjs";import{randomUUID as U,createHash as Q}from"node:crypto";import{existsSync as $,createReadStream as V,createWriteStream as Z}from"node:fs";import{mkdir as ee,rm as w,rename as B,readFile as D,writeFile as re,realpath as N,mkdtemp as S}from"node:fs/promises";import{LunoraError as h}from"@lunora/errors";import{i as E}from"../packem_shared/path-containment-CgxYZggb.mjs";import{tmpdir as I}from"node:os";import{Readable as te}from"node:stream";import{pipeline as ae}from"node:stream/promises";import{b as oe,l as ne,M as _,u as ie,r as ce}from"../packem_shared/import-CrVGrOt3.mjs";import{runExportCommand as se}from"../packem_shared/runExportCommand-DmXKEWSq.mjs";const y="manifest.json",j=async e=>{const r=Q("sha256");let a=0;for await(const t of V(e)){const o=t;a+=o.byteLength,r.update(o)}return{bytes:a,sha256:r.digest("hex")}},P=async e=>{const r=l(e,y);if(!$(r))return[];let a;try{a=JSON.parse(await D(r,"utf8"))}catch(t){const o=t instanceof Error?t.message:String(t);throw new h("INTERNAL",`backup: ${r} exists but is not valid JSON (${o}) — refusing to overwrite it; fix or remove it manually`,{cause:t})}if(!Array.isArray(a))throw new TypeError(`backup: ${r} exists but is not a JSON array — refusing to overwrite it; fix or remove it manually`);return a.filter(t=>x(t))},ue=async(e,r)=>{const a=l(e,`${y}.${U()}.tmp`);try{await re(a,`${JSON.stringify(r,void 0,2)}
2
2
  `,"utf8"),await B(a,l(e,y))}catch(t){throw await w(a,{force:!0}),t}},de=async(e,r)=>{const a=T(e),t=T(a,r.file),o=()=>{throw new h("INTERNAL",`backup: entry ${r.id} points outside ${a} (${r.file}) — refusing to read it`)};if(E(a,t)||o(),!!$(t))return E(await N(a),await N(t))||o(),t},le=e=>{const r=a=>p("",a);return{commit:async(a,t)=>{await B(t,l(e,a))},label:e,list:async()=>P(e),locate:r,materialize:async(a,t)=>{const o=i=>i===void 0?void 0:{path:i,release:()=>Promise.resolve()};return o(a===void 0?$(t)?t:void 0:await de(e,a))},record:async a=>{const t=await P(e);t.push(a),await ue(e,t)},stage:async a=>{await ee(e,{recursive:!0});const t=l(e,`${r(a)}.${U()}.partial`);return{path:t,release:()=>w(t,{force:!0})}}}},fe="/_lunora/admin/storage/object",me="/_lunora/admin/storage",R=8,ge=(e,r=K)=>{const a=r.basename(e);return a===""||a==="."||a===".."?"snapshot.ndjson":a},he=e=>{const{context:r,logger:a}=e,t=H(e.prefix??J),o=`bucket ${r.bucket??"(default)"} under ${t}`,i=oe(r),u=c=>`${r.baseUrl}${fe}?key=${encodeURIComponent(c)}${i}`,f=async c=>{const n=await r.fetchImpl(u(c),{headers:{authorization:`Bearer ${r.token}`},method:"GET"});if(!n.ok){const d=await n.text().catch(()=>"<no body>");throw new h("INTERNAL",`backup: could not read ${c} (HTTP ${String(n.status)}): ${d}`)}return n.text()},C=async c=>{try{const n=JSON.parse(await f(c));if(x(n))return n;a.warn(`backup: ignoring ${c} — not a backup manifest`)}catch(n){a.warn(`backup: ignoring ${c} — ${n instanceof Error?n.message:String(n)}`)}};return{commit:async(c,n,d)=>{if(d.bytes>_)throw new h("INTERNAL",`backup: the snapshot is ${String(d.bytes)} bytes, above the ${String(_)}-byte limit for a checksum-verified upload — nothing was written. Narrow it with \`--tables\`, or write it to a directory with \`--dir\` and move the file yourself (\`wrangler r2 object put\`).`);const s=await D(n);await ie(r,c,s,{contentType:"application/x-ndjson",id:c,sha256:d.sha256,size:d.bytes},a)},label:o,list:async()=>{const n=(await ne(r,t)).map(s=>s.key).filter(s=>Y(s)),d=[];for(let s=0;s<n.length;s+=R){const m=await Promise.all(n.slice(s,s+R).map(async g=>C(g)));d.push(...m.filter(g=>g!==void 0))}return d.toSorted((s,m)=>s.id.localeCompare(m.id))},locate:c=>p(t,c),materialize:async(c,n)=>{const d=c?.file??n,s=await r.fetchImpl(u(d),{headers:{authorization:`Bearer ${r.token}`},method:"GET"});if(s.status===404)return;if(!s.ok||s.body===null){const k=await s.text().catch(()=>"<no body>");throw new h("INTERNAL",`backup: could not download ${d} (HTTP ${String(s.status)}): ${k}`)}const m=await S(l(I(),"lunora-backup-")),g=l(m,ge(d));try{await ae(te.from(s.body),Z(g))}catch(k){throw await w(m,{force:!0,recursive:!0}),k}return{path:g,release:async()=>w(m,{force:!0,recursive:!0})}},record:async c=>{const n=G(c.file),d=await r.fetchImpl(`${r.baseUrl}${me}?key=${encodeURIComponent(n)}${i}`,{body:`${JSON.stringify(c,void 0,2)}
3
3
  `,headers:{authorization:`Bearer ${r.token}`,"content-type":"application/json"},method:"PUT"});if(!d.ok){const s=await d.text().catch(()=>"<no body>");throw new h("INTERNAL",`backup: snapshot ${c.file} was written but its manifest was not (HTTP ${String(d.status)}): ${s}`)}},stage:async c=>{const n=await S(l(I(),"lunora-backup-"));return{path:l(n,p("",c)),release:async()=>w(n,{force:!0,recursive:!0})}}}},we=".lunora-backups",L="/_lunora/admin/backup/retention",be="/_lunora/admin/backup/prune",ke="/_lunora/admin/pitr",pe="__lunora_admin__:getPitrBookmark",ye="__lunora_admin__:pitrRestore",ve=async(e,r)=>{const a=(e.now??(()=>new Date))().toISOString(),t=await r.stage(a);try{const o=await se({cwd:e.cwd,fetchImpl:e.fetchImpl,logger:e.logger,out:t.path,prod:e.prod,tables:e.tables,token:e.token,url:e.url});if(o.code!==0)return{code:o.code};const i=r.locate(a),u=await j(t.path);await r.commit(i,t.path,u);const f={bytes:u.bytes,createdAt:a,file:i,id:a,rows:o.rows,sha256:u.sha256,tables:e.tables};return await r.record(f),e.logger.success(`backup created: ${i} (${o.rows.toString()} rows, ${u.bytes.toString()} bytes)`),{code:0,entry:f}}finally{await t.release().catch(o=>{e.logger.warn(`backup: could not clean up staging (${o instanceof Error?o.message:String(o)})`)})}},$e=async(e,r,a)=>{if(r?.sha256===void 0)return a.error(r===void 0?"--verify needs a recorded checksum, and a snapshot restored by path/key has no manifest entry — restore it by its backup id instead":`--verify: backup ${r.id} carries no recorded checksum — it was taken by a release before checksums existed`),{code:1};const t=await j(e);if(t.sha256!==r.sha256)return a.error(`--verify: ${r.file} does not match its recorded checksum (expected ${r.sha256}, got ${t.sha256}) — nothing was imported`),{code:1};a.success(`verified ${r.file} against its recorded sha256`)},Te=async(e,r)=>{const a=await r.list();if(a.length===0)return e.logger.info(`no backups found in ${r.label}`),{code:0};for(const t of a)e.logger.info(`${t.id} ${t.rows.toString()} rows ${t.bytes.toString()} bytes ${t.file}`);return{code:0}},Ne=async(e,r)=>{const{target:a}=e;if(a===void 0||a.length===0)return e.logger.error("restore requires a backup id or file path. Usage: lunora backup restore <id|file>"),{code:1};const o=(await r.list()).find(u=>u.id===a),i=await r.materialize(o,a);if(i===void 0)return e.logger.error(`backup not found: ${a}`),{code:1};try{if(e.verify===!0){const f=await $e(i.path,o,e.logger);if(f!==void 0)return f}return{code:(await ce({cwd:e.cwd,fetchImpl:e.fetchImpl,file:i.path,logger:e.logger,prod:e.prod,token:e.token,url:e.url,yes:e.yes})).code}}finally{await i.release().catch(u=>{e.logger.warn(`backup: could not clean up the downloaded snapshot (${u instanceof Error?u.message:String(u)})`)})}},Se=e=>{const r=e.token??process.env.LUNORA_ADMIN_TOKEN;if(!r){e.logger.error("admin token required — pass --token or set LUNORA_ADMIN_TOKEN");return}if(e.prod&&e.url===void 0){e.logger.error("--prod requires an explicit --url (refusing to target the implicit localhost worker)");return}if(e.restore===!0&&e.at===void 0&&e.bookmark===void 0){e.logger.error("pitr --restore requires --at <time> or --bookmark <bookmark>");return}if(e.restore===!0&&e.prod===!0&&e.yes!==!0){e.logger.error("pitr --restore --prod restores production data in place. Re-run with --yes to confirm.");return}const a=v(e.url,e.logger,e.cwd);if(a===void 0)return;const t=e.adminFetch??globalThis.fetch;if(typeof t!="function")throw new TypeError("no fetch implementation available — pass pitrFetch or run on Node >= 18");return{fetchImpl:t,requestUrl:`${a}${ke}`,token:r}},Ee=(e,r)=>{const a={};return e.at!==void 0&&(a.time=e.at),r&&e.bookmark!==void 0&&(a.bookmark=e.bookmark),r&&e.restart===!0&&(a.restart=!0),a},Ie=async e=>{const r=Se(e);if(r===void 0)return{code:1};const a=e.restore===!0,t=a?ye:pe,o=Ee(e,a),i=a?"restore":"bookmark";e.logger.info(`POST ${r.requestUrl} -> pitr ${i}${e.shard===void 0?"":` (shard "${e.shard}")`}`);const u=await r.fetchImpl(r.requestUrl,{body:JSON.stringify({args:o,functionPath:t,shardKey:e.shard}),headers:{authorization:`Bearer ${r.token}`,"content-type":"application/json"},method:"POST"});return await b(u,e.logger),{code:u.ok?0:1}},_e=(e,r)=>{if(e.bucket===void 0)return le(l(r,e.dir??we));if(e.prod===!0&&e.url===void 0){e.logger.error("--prod requires an explicit --url (refusing to target the implicit localhost worker)");return}const a=v(e.url,e.logger,e.cwd);if(a===void 0)return;const{token:t}=A({cwd:r,token:e.token,url:a});if(!t){e.logger.error("admin token required — pass --token, set LUNORA_ADMIN_TOKEN, or add it to .dev.vars (local targets only)");return}const o=e.fetchImpl??globalThis.fetch;if(typeof o!="function")throw new TypeError("no fetch implementation available — pass fetchImpl or run on Node >= 18");return he({context:{baseUrl:a,bucket:e.bucket==="default"?void 0:e.bucket,fetchImpl:o,token:t},logger:e.logger,prefix:e.prefix})},q=e=>{const r=[e.bucket===void 0?void 0:"--bucket",e.prefix===void 0?void 0:"--prefix",e.dir===void 0?void 0:"--dir"].filter(i=>i!==void 0);if(r.length>0){e.logger.error(`${r.join(" / ")} ${r.length===1?"does":"do"} not apply here — retention and prune act on the store the worker itself is configured with (\`backupStore\`), not a destination you name.`);return}if(e.prod===!0&&e.url===void 0){e.logger.error("--prod requires an explicit --url (refusing to target the implicit localhost worker)");return}const a=v(e.url,e.logger,e.cwd);if(a===void 0)return;const{token:t}=A({cwd:e.cwd??process.cwd(),token:e.token,url:a});if(!t){e.logger.error("admin token required — pass --token, set LUNORA_ADMIN_TOKEN, or add it to .dev.vars (local targets only)");return}const o=e.adminFetch??globalThis.fetch;if(typeof o!="function")throw new TypeError("no fetch implementation available — pass pitrFetch or run on Node >= 18");return{baseUrl:a,fetchImpl:o,token:t}},M=(e,r)=>{e.logger.info(`backup retention would keep the newest ${r.keep.toString()} of ${r.eligible.toString()} under ${r.prefix} and delete ${r.wouldDelete.length.toString()}`);for(const a of r.wouldDelete)e.logger.info(` ${O(a)}`)},Pe=async e=>{const r=q(e);if(r===void 0)return{code:1};const a=await r.fetchImpl(`${r.baseUrl}${L}`,{headers:{authorization:`Bearer ${r.token}`},method:"GET"});if(!a.ok)return await b(a,e.logger),{code:1};const t=await a.json();return t.cron===void 0?(e.logger.info("no scheduled backup is configured (`backupCron`), so retention would delete nothing"),{code:0,preview:t}):t.keep<=0?(e.logger.info(`retention is off for cron "${t.cron}" (\`backupRetain\` unset) — every snapshot under ${t.prefix} is kept`),{code:0,preview:t}):(M(e,t),t.eligible===0&&e.logger.info(`no snapshot under ${t.prefix} was written by this cron, so retention has nothing of its own to remove`),{code:0,preview:t})},Re=(e,r)=>{e.logger.success(`deleted ${r.deleted.length.toString()} backup(s)`);for(const a of r.deleted)e.logger.info(` ${O(a)}`);r.failed.length>0&&e.logger.warn(`${r.failed.length.toString()} could not be removed — the snapshot may be gone with its manifest left behind; run again to retry`),r.ignored>0&&e.logger.info(`${r.ignored.toString()} were already gone by the time the delete ran`),r.remaining>0&&e.logger.info(`${r.remaining.toString()} more are past the window — run \`lunora backup prune\` again`)},xe=async e=>{const r=q(e);if(r===void 0)return{code:1};const a=await r.fetchImpl(`${r.baseUrl}${L}`,{headers:{authorization:`Bearer ${r.token}`},method:"GET"});if(!a.ok)return await b(a,e.logger),{code:1};const t=await a.json();if(t.keep<=0||t.cron===void 0)return e.logger.error("backup prune needs a retention window: set `backupRetain` (and `backupCron`) on the worker. Without one there is nothing past the window to remove."),{code:1,preview:t};if(t.wouldDelete.length===0)return e.logger.info(`nothing to prune — ${t.eligible.toString()} snapshot(s) under ${t.prefix} and retention keeps the newest ${t.keep.toString()}`),{code:0,preview:t};if(M(e,t),e.yes!==!0){const u=e.confirm??(F()?z:void 0);if(u===void 0)return e.logger.error("refusing to delete backups without confirmation — re-run with --yes (there is no undo for an object-store delete)"),{code:1,preview:t};if(!await u(`delete ${t.wouldDelete.length.toString()} backup(s)? This cannot be undone. [y/N] `))return e.logger.info("nothing was deleted"),{code:0,preview:t}}const o=await r.fetchImpl(`${r.baseUrl}${be}`,{body:JSON.stringify({confirm:t.wouldDelete}),headers:{authorization:`Bearer ${r.token}`,"content-type":"application/json"},method:"POST"});if(!o.ok)return await b(o,e.logger),{code:1,preview:t};const i=await o.json();return Re(e,i),{code:i.failed.length>0?1:0,deleted:i.deleted,preview:t}},Oe=async e=>{const r=e.cwd??process.cwd();try{if(e.subcommand==="pitr")return await Ie(e);if(e.subcommand==="retention")return await Pe(e);if(e.subcommand==="prune")return await xe(e);const a=_e(e,r);return a===void 0?{code:1}:e.subcommand==="create"?await ve(e,a):e.subcommand==="list"?await Te(e,a):await Ne(e,a)}catch(a){return e.logger.error(a instanceof Error?a.message:String(a)),{code:1}}},Ae=e=>e==="create"||e==="list"||e==="pitr"||e==="prune"||e==="restore"||e==="retention",Ve=W(({argument:e,cwd:r,logger:a,options:t})=>{const o=e[0];return Ae(o)?Oe({at:t.at,bookmark:t.bookmark,bucket:t.bucket,cwd:r,dir:t.dir,logger:a,prefix:t.prefix,prod:t.prod===!0,restart:t.restart===!0,restore:t.restore===!0,shard:t.shard,subcommand:o,tables:t.tables,target:e[1],token:t.token,url:X({cwd:r,prod:t.prod===!0,url:t.url}),verify:t.verify===!0,yes:t.yes===!0}):(a.error(`backup: unknown subcommand "${o??""}" — expected create | list | restore | retention | prune | pitr`),{code:1})});export{Ve as execute,Oe as runBackupCommand};
@@ -1 +1 @@
1
- import{d as o}from"../packem_shared/command-DwC11Cwe.mjs";import{a as f}from"../packem_shared/resolve-target-C_ZloTvd.mjs";import{I as u,r as d}from"../packem_shared/import-Db3Gk4mx.mjs";const n=o(({argument:m,cwd:a,logger:e,options:r})=>{const t=m[0];return t?r.from!==void 0&&!u.includes(r.from)?(e.error(`--from ${r.from} is not a known source. Expected one of: ${u.join(", ")}.`),{code:1}):d({batchSize:r.batchSize,cwd:a,file:t,from:r.from,logger:e,prod:r.prod===!0,scan:r.scan===!0,storageDir:r.storageDir,table:r.table,token:r.token,url:f({cwd:a,prod:r.prod===!0,url:r.url}),verify:r.verify===!0,withStorage:r.withStorage===!0,yes:r.yes===!0}):(e.error("import requires a path. Usage: lunora import <file.ndjson | convex-export-dir> [--table <name>]"),{code:1})});export{n as execute};
1
+ import{d as o}from"../packem_shared/command-DwC11Cwe.mjs";import{a as f}from"../packem_shared/resolve-target-C_ZloTvd.mjs";import{I as u,r as d}from"../packem_shared/import-CrVGrOt3.mjs";const n=o(({argument:m,cwd:a,logger:e,options:r})=>{const t=m[0];return t?r.from!==void 0&&!u.includes(r.from)?(e.error(`--from ${r.from} is not a known source. Expected one of: ${u.join(", ")}.`),{code:1}):d({batchSize:r.batchSize,cwd:a,file:t,from:r.from,logger:e,prod:r.prod===!0,scan:r.scan===!0,storageDir:r.storageDir,table:r.table,token:r.token,url:f({cwd:a,prod:r.prod===!0,url:r.url}),verify:r.verify===!0,withStorage:r.withStorage===!0,yes:r.yes===!0}):(e.error("import requires a path. Usage: lunora import <file.ndjson | convex-export-dir> [--table <name>]"),{code:1})});export{n as execute};
@@ -1,7 +1,7 @@
1
- import{spawnSync as I,spawn as Y}from"node:child_process";import{existsSync as R,watch as ge,readFileSync as z,mkdirSync as pe,openSync as X,closeSync as Q,statSync as we,readSync as he}from"node:fs";import{join as y,dirname as Se}from"node:path";import{runCodegen as Z}from"@lunora/codegen";import{detectAgentRules as q,claimDevServerState as ee,DEV_HANDOFF_ENV as E,clearDevServerState as w,readDevServerState as _,readLiveDevServerState as b,detectFramework as re,readProjectDependencyNames as Ee,DEV_LOG_FILE_ENV as te,DEV_DAEMON_ENV as O,updateDevServerState as oe,isRecordedProcessCurrent as ke,DEV_LOG_FILE as ye,detectAiAgent as be,readProjectRemotePreference as Te,resolveTargetOrThrow as Le,inferLunoraBindings as Ie,packageNamesFromBindings as Re,ensureDevVarsExample as _e,ensureDevVariables as Oe,isInteractive as Pe,DEV_VARS_FILE as $,DEV_VARS_EXAMPLE_FILE as Ae,fillDevSecrets as De,claimAgentRulesHint as Ne,AGENT_RULES_HINT as $e,resolveDeployDriver as xe,resolveProjectTarget as Ce,discoverContainerInfo as Me,streamContainerLogs as Fe,formatLunoraEvent as Ge}from"@lunora/config";import{resolveRemoteEnabled as je,findWranglerFile as ne,readWranglerJsonc as se,materializeRemoteWranglerConfig as Ue}from"@lunora/config/cloudflare";import{p as Ve}from"../packem_shared/api-spec-lYyq4vL6.mjs";import{r as He}from"../packem_shared/codegen-error-C04fGQjf.mjs";import{r as We}from"../packem_shared/platform-diagnostics-uOXqysk3.mjs";import{d as Be}from"../packem_shared/command-DwC11Cwe.mjs";import{d as ae,e as T,a as Ke}from"../packem_shared/detect-package-manager-DSaAqRof.mjs";import{createServer as Je,connect as Ye}from"node:net";import{forceJsonLogging as ze}from"../packem_shared/createLogger-hGafwf-3.mjs";import{networkInterfaces as Xe}from"node:os";import{spawnShellCompat as ie}from"../packem_shared/createRecordingSpawner-DJlNacZ6.mjs";import{createServer as Qe,request as Ze}from"node:http";import{loadStudioAssets as x,studioAssetsStamp as C,renderStudioHtml as qe,resolveAdminToken as er,transportRejectionReason as M,SCHEMA_EDIT_ENDPOINT as rr,POLICY_SCAFFOLD_ENDPOINT as tr,SEED_ENDPOINT as or,serveJsonHandler as nr,isStandaloneModulePath as sr,readStandaloneAsset as ar,assetContentType as ir,handleSchemaEditRequest as cr,handlePolicyScaffoldRequest as dr,handleSeedRequest as lr}from"@lunora/config/studio-host";import{c as ur}from"../packem_shared/tui-prompts-Dd0pDuNr.mjs";import{setTimeout as L}from"node:timers/promises";import{p as h}from"../packem_shared/output-format-DfogXx0p.mjs";const fr=100,mr=/[/\\]/u,F=(e,r,o)=>{const{apiSpec:t,logger:n,projectRoot:s,target:i}=e;try{const a=Z({apiSpec:t,lunoraDirectory:r,projectRoot:s,target:i});n.success(`codegen: wrote ${r}/_generated (${o})`),We(a.platformDiagnostics,n)}catch(a){n.error(He(a,o))}},vr=e=>{const r=e.lunoraDirectory??"lunora",o=y(e.projectRoot,r),t=e.debounceMs??fr;F(e,r,"startup");const n=c=>(e.logger.warn(`codegen watch unavailable (${c}) — schema edits will NOT auto-regenerate. Run \`lunora codegen\` manually after each edit.`),{close:()=>{},watchAvailable:!1});let s,i;if(!R(o))return n(`no such directory: ${o}`);try{i=ge(o,{recursive:!0},(c,d)=>{typeof d=="string"&&d.split(mr).includes("_generated")||(s&&clearTimeout(s),s=setTimeout(()=>{F(e,r,`change: ${d??"?"}`)},t))})}catch(c){return n(c instanceof Error?c.message:String(c))}const a=i;return{close:()=>{s&&clearTimeout(s),a.close()},watchAvailable:!0}},gr=64,ce="127.0.0.1",pr=65535,wr=(e,r=ce)=>new Promise(o=>{const t=Je();t.once("error",()=>{o(!1)}),t.once("listening",()=>{t.close(()=>{o(!0)})}),t.listen(e,r)}),hr=async(e,r=ce,o=gr)=>{for(let t=e;t<e+o&&t<=pr;t+=1)if(await wr(t,r))return t;return e},G=(e=Xe)=>Object.values(e()).some(r=>r?.some(o=>o.internal&&o.address==="::1")),j="/_lunora",U=e=>{const r=e.indexOf("?");return r===-1?e:e.slice(0,r)},Sr=(e,r,o)=>{const t=Ze({headers:{...e.headers,host:o.host},hostname:o.hostname,method:e.method,path:e.url,port:o.port},n=>{r.writeHead(n.statusCode??502,n.headers),n.pipe(r)});t.on("error",n=>{r.statusCode=502,r.end(`lunora dev: worker unreachable (${n.message})`)}),e.pipe(t)},Er=5e3,kr=(e,r,o,t)=>{const n=Ye({host:t.hostname,port:Number(t.port)},()=>{n.setTimeout(0);const s=[`${e.method??"GET"} ${e.url??"/"} HTTP/1.1`];for(const[i,a]of Object.entries(e.headers))if(i!=="host")for(const c of Array.isArray(a)?a:[a])c!==void 0&&s.push(`${i}: ${c}`);s.push(`host: ${t.host}`,"",""),n.write(s.join(`\r
2
- `)),o.length>0&&n.write(o),n.pipe(r),r.pipe(n)});n.setTimeout(Er,()=>n.destroy()),n.on("error",()=>r.destroy()),r.on("error",()=>n.destroy())},yr=async e=>{const r=e.host??"127.0.0.1",o=r==="127.0.0.1"||r==="localhost"||r==="::1",t=new URL(e.workerOrigin);let n=x(e.logger,import.meta.url),s=C(import.meta.url);const i=qe({adminToken:o?er(e.cwd):void 0,basePath:"/",dataEditable:o,rulesInstalled:q(e.cwd).installed,runAsIdentity:o,schemaEditable:o,scriptSrc:"/studio.js",styleHref:"/styles.css"}),a=(l,u,f)=>{l.statusCode=200,l.setHeader("Content-Type",f),l.end(u)},c=(l,u,f)=>{l.statusCode=u,l.setHeader("Content-Type","text/plain"),l.end(f)},d=(l,u,f,p)=>{if(o){nr(l,u,f,e.cwd);return}c(u,403,p)},m=(l,u)=>{const f=l==="/styles.css",p=sr(l);if(!f&&!p)return!1;const A=C(import.meta.url);if(A!==s&&(n=x(e.logger,import.meta.url),s=A),n===void 0)return c(u,501,"Lunora studio assets not found — install and build @lunora/studio."),!0;if(f)return a(u,n.styles,"text/css; charset=utf-8"),!0;const D=l.slice(l.lastIndexOf("/")+1),N=ar(D,import.meta.url);return N===void 0?(c(u,404,"Not found"),!0):(a(u,N,ir(D)),!0)},v=Buffer.from(i),g=Qe((l,u)=>{const f=U(l.url??"/");if(o){const p=M(l,e.logger);if(p!==void 0){c(u,403,p);return}}if(f.startsWith(j)){if(!o){c(u,403,"Lunora studio: the worker admin proxy is only available on a loopback bind.");return}Sr(l,u,t);return}if(f===rr){d(l,u,cr,"Lunora schema editing is only available on loopback hosts in dev.");return}if(f===tr){d(l,u,dr,"Lunora policy scaffolding is only available on loopback hosts in dev.");return}if(f===or){d(l,u,lr,"Lunora data seeding is only available on loopback hosts in dev.");return}m(f,u)||a(u,v,"text/html; charset=utf-8")});return g.on("upgrade",(l,u,f)=>{if(!o||M(l,e.logger)!==void 0){u.destroy();return}U(l.url??"").startsWith(j)?kr(l,u,f,t):u.destroy()}),await new Promise((l,u)=>{g.once("error",f=>{f.code==="EADDRINUSE"?u(new Error(`studio port ${String(e.port)} is already in use — pass a different port with --port, or stop the process using it`,{cause:f})):u(f)}),g.listen(e.port,r,()=>{l()})}),{close:()=>new Promise(l=>{g.close(()=>{l()})}),url:`http://${r==="0.0.0.0"||r==="::"?"localhost":r}:${String(e.port)}`}},br=12e4,de=250,Tr=1e4,Lr=100,Ir=40,S=256*1024,Rr="LUNORA_DEV_READY_TIMEOUT_MS",_r=new Set(["nuxt","sveltekit"]),P=e=>_r.has(re(e).framework)?"framework-worker":Ee(e).has("@lunora/vite")?"vite":"wrangler",le=e=>{const r=ae(e);let o;try{const t=z(y(e,"package.json"),"utf8");o=JSON.parse(t).scripts?.dev}catch{}if(o===void 0||o.trim()===""||o.includes("lunora")){const t=T(r,"vite",["dev"]);return{args:t.args,command:t.command}}return{args:["run","dev"],command:r}},Or=async e=>{try{return await fetch(new URL("/_lunora/status",e),{signal:AbortSignal.timeout(1e3)}),!0}catch{return!1}},Pr=e=>{pe(Se(e.logPath),{recursive:!0});const r=X(e.logPath,"w",384);let o;const t=ie(e.command,e.args);try{o=Y(t.command,t.args,{cwd:e.cwd,detached:!0,env:e.env,shell:t.shell,stdio:["ignore",r,r],windowsHide:!0})}finally{Q(r)}return o.unref(),{exited:new Promise(n=>{o.on("error",()=>{n(1)}),o.on("exit",(s,i)=>{n(s??(i?1:0))})}),pid:o.pid}},Ar=e=>{try{const{size:r}=we(e);if(r<=S)return z(e,"utf8");const o=X(e,"r");try{const t=Buffer.alloc(S),n=he(o,t,0,S,r-S),s=t.toString("utf8",0,n);return s.slice(s.indexOf(`
3
- `)+1)}finally{Q(o)}}catch{return}},ue=(e,r)=>{const o=Ar(e);if(o===void 0)return[];const t=o.split(`
4
- `);return t.at(-1)===""&&t.pop(),r>0?t.slice(-r):t},fe=(e,r)=>r?.logFile??y(e,ye),me=e=>{e.info(" Stop: lunora dev stop"),e.info(" Status: lunora dev status"),e.info(" Logs: lunora dev logs")},k=(e,r)=>{e.warn(`Dev server already running at ${r.url} (pid ${String(r.pid)})`),me(e)},Dr=(e,r)=>{e.success(`Dev server running at ${r.url} (pid ${String(r.pid)})`),r.studioUrl!==void 0&&e.info(` Studio: ${r.studioUrl}`),me(e)},Nr=async e=>{const{child:r,cwd:o,deadline:t,pollIntervalMs:n,probe:s}=e,i=r.exited.then(a=>({exitCode:a}));for(;Date.now()<t;){const a=b(o);if(a!==void 0&&a.pid!==process.pid&&await s(a.url))return{state:a,status:"ready"};const c=await Promise.race([i,L(n)]);if(c!==void 0)return{...c,status:"exited"}}return{status:"timeout"}},$r=async e=>{const{cwd:r,logger:o}=e,t=fe(r,void 0),n=Number(process.env[Rr]??""),s=e.readyTimeoutMs??(Number.isFinite(n)&&n>0?n:br),i=(e.spawnDetached??Pr)({args:e.command.args,command:e.command.command,cwd:r,env:{...process.env,...e.env,[O]:"1",[te]:t,...process.env.FORCE_COLOR===void 0?{NO_COLOR:"1"}:{},...e.json?{LUNORA_LOG_JSON:"1"}:{}},logPath:t}),a=await Nr({child:i,cwd:r,deadline:Date.now()+s,pollIntervalMs:e.pollIntervalMs??de,probe:e.probe??Or});if(a.status==="ready")return Dr(o,a.state),{code:0};if(a.status==="exited"){o.error(`dev server exited before becoming ready (exit code ${String(a.exitCode)}). Last output:`);for(const d of ue(t,Ir))o.error(` ${d}`);return w(r,i.pid??process.pid),{code:a.exitCode===0?1:a.exitCode}}i.pid!==void 0&&_(r)?.pid===process.pid&&oe(r,{logFile:t,pid:i.pid});const c=i.pid===void 0?"":` (pid ${String(i.pid)})`;return o.warn(`dev server did not confirm ready within ${String(Math.round(s/1e3))}s — it may still be compiling${c}. Check \`lunora dev status\` and \`lunora dev logs\`; \`lunora dev stop\` shuts it down.`),{code:1}},xr=(e,r)=>{const o=["dev"];return e.apiSpec!==void 0&&o.push("--api-spec",e.apiSpec),e.port!==void 0&&o.push("--port",String(e.port)),e.workerPort!==void 0&&o.push("--worker-port",String(e.workerPort)),e.codegen===!1&&o.push("--no-codegen"),e.studio===!1&&o.push("--no-studio"),e.worker===!1&&o.push("--no-worker"),e.target!==void 0&&o.push("--target",e.target),r&&o.push("--remote"),o},Cr=async e=>{const{cwd:r,jsonLogs:o,logger:t,options:n,remote:s}=e,i=e.run??$r,a=P(r),c=a==="wrangler"?`http://localhost:${String(n.workerPort??8787)}`:"http://localhost:5173",d=ee(r,{background:!0,mode:"cli",pid:process.pid,startedAt:new Date().toISOString(),url:c});if(!d.ok)return d.existing!==void 0&&k(t,d.existing),{code:0};const m={[E]:String(process.pid)};try{return a==="vite"?await i({command:le(r),cwd:r,env:{...s?{LUNORA_REMOTE:"1"}:{},...m},json:o,logger:t}):await i({command:{args:[process.argv[1]??"lunora",...xr(n,s)],command:process.execPath},cwd:r,env:m,json:o,logger:t})}finally{w(r,process.pid)}},Mr=e=>{try{const r=I("ps",["-o","pgid=","-p",String(e)],{encoding:"utf8"}),o=Number.parseInt(r.stdout.trim(),10);return Number.isInteger(o)&&o>0?o:void 0}catch{return}},Fr=(e,r,o=process.platform,t=I)=>{if(o==="win32"){try{t("taskkill",["/pid",String(e.pid),"/T","/F"],{stdio:"ignore"})}catch{}return}if(e.background===!0)try{r(-(Mr(e.pid)??e.pid),"SIGKILL");return}catch{}try{r(e.pid,"SIGKILL")}catch{}},Gr=(e,r)=>(r.json?h({pid:e.pid,stopped:!1}):r.logger.error(`dev server (pid ${String(e.pid)}) survived the force-kill — record kept; retry \`lunora dev stop\` or kill the process manually.`),{code:1}),jr=async e=>{const{cwd:r,logger:o}=e,t=_(r),n=e.alive??(v=>v===t?.pid&&ke(t)),s=e.signal??((v,g)=>{process.kill(v,g)}),i=e.pollIntervalMs??de,a=e.stopGraceMs??Tr,c=e.platform??process.platform,d=e.spawnSyncImpl??I;if(t===void 0||!n(t.pid))return t!==void 0&&w(r,t.pid),e.json?h({running:!1,stopped:!1}):o.info("No dev server running."),{code:0};try{s(t.pid,"SIGTERM")}catch{}const m=Date.now()+a;for(;n(t.pid)&&Date.now()<m;)await L(i);return n(t.pid)&&(Fr(t,s,c,d),await L(i),n(t.pid))?Gr(t,e):(w(r,t.pid),e.json?h({pid:t.pid,stopped:!0}):o.success(`Stopped dev server (pid ${String(t.pid)}).`),{code:0})},Ur=e=>{const{cwd:r,logger:o}=e,t=b(r);if(t===void 0)return e.json?h({running:!1}):o.info("No dev server running."),{code:0};const n=e.now??Date.now,s=t.startedAt===void 0?Number.NaN:Date.parse(t.startedAt),i=Number.isFinite(s)?Math.max(0,Math.round((n()-s)/1e3)):void 0;if(e.json)return h({background:t.background===!0,logFile:t.logFile,mode:t.mode,pid:t.pid,running:!0,startedAt:t.startedAt,studioUrl:t.studioUrl,uptimeSeconds:i,url:t.url}),{code:0};const a=[`pid ${String(t.pid)}`,...i===void 0?[]:[`uptime ${String(i)}s`],t.background===!0?"background":"foreground"];return o.success(`Dev server running at ${t.url} (${a.join(", ")})`),t.studioUrl!==void 0&&o.info(` Studio: ${t.studioUrl}`),t.logFile!==void 0&&o.info(` Logs: lunora dev logs (${t.logFile})`),{code:0}},Vr=e=>{const{cwd:r,logger:o}=e,t=_(r),n=fe(r,t);if(!R(n))return o.info("No dev server logs found — output is captured when the server runs in background mode (`lunora dev --background`)."),{code:0};const s=ue(n,e.lines??Lr);return s.length>0&&process.stdout.write(`${s.join(`
1
+ import{spawnSync as _,spawn as z}from"node:child_process";import{existsSync as O,watch as pe,readFileSync as X,mkdirSync as we,openSync as Q,closeSync as Z,statSync as he,readSync as Se}from"node:fs";import{join as y,dirname as Ee}from"node:path";import{runCodegen as q}from"@lunora/codegen";import{detectAgentRules as ee,claimDevServerState as re,DEV_HANDOFF_ENV as E,clearDevServerState as w,readDevServerState as A,readLiveDevServerState as b,detectFramework as te,readProjectDependencyNames as ke,DEV_LOG_FILE_ENV as oe,DEV_DAEMON_ENV as P,updateDevServerState as ne,isRecordedProcessCurrent as ye,DEV_LOG_FILE as be,detectAiAgent as Te,readProjectRemotePreference as Le,resolveTargetOrThrow as Ie,inferLunoraBindings as Re,packageNamesFromBindings as _e,ensureDevVarsExample as Oe,ensureDevVariables as Ae,isInteractive as Pe,DEV_VARS_FILE as x,DEV_VARS_EXAMPLE_FILE as De,fillDevSecrets as Ne,claimAgentRulesHint as $e,AGENT_RULES_HINT as xe,resolveDeployDriver as Ce,resolveProjectTarget as Me,discoverContainerInfo as Fe,streamContainerLogs as Ge,formatLunoraEvent as je}from"@lunora/config";import{resolveRemoteEnabled as Ue,findWranglerFile as se,readWranglerJsonc as ie,materializeRemoteWranglerConfig as Ve}from"@lunora/config/cloudflare";import{p as He}from"../packem_shared/api-spec-lYyq4vL6.mjs";import{r as We}from"../packem_shared/codegen-error-C04fGQjf.mjs";import{r as Be}from"../packem_shared/platform-diagnostics-uOXqysk3.mjs";import{d as Ke}from"../packem_shared/command-DwC11Cwe.mjs";import{d as ae,e as I,a as Je}from"../packem_shared/detect-package-manager-DSaAqRof.mjs";import{createServer as Ye,connect as ze}from"node:net";import{forceJsonLogging as Xe}from"../packem_shared/createLogger-hGafwf-3.mjs";import{networkInterfaces as Qe}from"node:os";import{spawnShellCompat as ce}from"../packem_shared/createRecordingSpawner-DJlNacZ6.mjs";import{createServer as Ze,request as qe}from"node:http";import{loadStudioAssets as C,studioAssetsStamp as M,renderStudioHtml as er,resolveAdminToken as rr,transportRejectionReason as F,SCHEMA_EDIT_ENDPOINT as tr,POLICY_SCAFFOLD_ENDPOINT as or,SEED_ENDPOINT as nr,sendStudioDocument as sr,serveJsonHandler as ir,isStandaloneModulePath as ar,readStandaloneAsset as cr,assetContentType as dr,handleSchemaEditRequest as lr,handlePolicyScaffoldRequest as ur,handleSeedRequest as fr,applyStudioAssetCache as mr}from"@lunora/config/studio-host";import{c as vr}from"../packem_shared/tui-prompts-Dd0pDuNr.mjs";import{setTimeout as R}from"node:timers/promises";import{p as h}from"../packem_shared/output-format-DfogXx0p.mjs";const gr=100,pr=/[/\\]/u,G=(e,r,o)=>{const{apiSpec:t,logger:n,projectRoot:s,target:a}=e;try{const i=q({apiSpec:t,lunoraDirectory:r,projectRoot:s,target:a});n.success(`codegen: wrote ${r}/_generated (${o})`),Be(i.platformDiagnostics,n)}catch(i){n.error(We(i,o))}},wr=e=>{const r=e.lunoraDirectory??"lunora",o=y(e.projectRoot,r),t=e.debounceMs??gr;G(e,r,"startup");const n=c=>(e.logger.warn(`codegen watch unavailable (${c}) — schema edits will NOT auto-regenerate. Run \`lunora codegen\` manually after each edit.`),{close:()=>{},watchAvailable:!1});let s,a;if(!O(o))return n(`no such directory: ${o}`);try{a=pe(o,{recursive:!0},(c,d)=>{typeof d=="string"&&d.split(pr).includes("_generated")||(s&&clearTimeout(s),s=setTimeout(()=>{G(e,r,`change: ${d??"?"}`)},t))})}catch(c){return n(c instanceof Error?c.message:String(c))}const i=a;return{close:()=>{s&&clearTimeout(s),i.close()},watchAvailable:!0}},hr=64,de="127.0.0.1",Sr=65535,Er=(e,r=de)=>new Promise(o=>{const t=Ye();t.once("error",()=>{o(!1)}),t.once("listening",()=>{t.close(()=>{o(!0)})}),t.listen(e,r)}),kr=async(e,r=de,o=hr)=>{for(let t=e;t<e+o&&t<=Sr;t+=1)if(await Er(t,r))return t;return e},j=(e=Qe)=>Object.values(e()).some(r=>r?.some(o=>o.internal&&o.address==="::1")),U="/_lunora",V=e=>{const r=e.indexOf("?");return r===-1?e:e.slice(0,r)},yr=(e,r,o)=>{const t=qe({headers:{...e.headers,host:o.host},hostname:o.hostname,method:e.method,path:e.url,port:o.port},n=>{r.writeHead(n.statusCode??502,n.headers),n.pipe(r)});t.on("error",n=>{r.statusCode=502,r.end(`lunora dev: worker unreachable (${n.message})`)}),e.pipe(t)},br=5e3,Tr=(e,r,o,t)=>{const n=ze({host:t.hostname,port:Number(t.port)},()=>{n.setTimeout(0);const s=[`${e.method??"GET"} ${e.url??"/"} HTTP/1.1`];for(const[a,i]of Object.entries(e.headers))if(a!=="host")for(const c of Array.isArray(i)?i:[i])c!==void 0&&s.push(`${a}: ${c}`);s.push(`host: ${t.host}`,"",""),n.write(s.join(`\r
2
+ `)),o.length>0&&n.write(o),n.pipe(r),r.pipe(n)});n.setTimeout(br,()=>n.destroy()),n.on("error",()=>r.destroy()),r.on("error",()=>n.destroy())},Lr=async e=>{const r=e.host??"127.0.0.1",o=r==="127.0.0.1"||r==="localhost"||r==="::1",t=new URL(e.workerOrigin);let n=C(e.logger,import.meta.url),s=M(import.meta.url);const a=er({adminToken:o?rr(e.cwd):void 0,basePath:"/",dataEditable:o,rulesInstalled:ee(e.cwd).installed,runAsIdentity:o,schemaEditable:o,scriptSrc:"/studio.js",styleHref:"/styles.css"}),i=(l,u,f,p,T)=>{mr(l,u,T,s)||(u.statusCode=200,u.setHeader("Content-Type",p),u.end(f))},c=(l,u,f)=>{l.statusCode=u,l.setHeader("Content-Type","text/plain"),l.end(f)},d=(l,u,f,p)=>{if(o){ir(l,u,f,e.cwd);return}c(u,403,p)},m=(l,u,f)=>{const p=l==="/styles.css",T=ar(l);if(!p&&!T)return!1;const N=M(import.meta.url);if(N!==s&&(n=C(e.logger,import.meta.url),s=N),n===void 0)return c(f,501,"Lunora studio assets not found — install and build @lunora/studio."),!0;if(p)return i(u,f,n.styles,"text/css; charset=utf-8","styles.css"),!0;const L=l.slice(l.lastIndexOf("/")+1),$=cr(L,import.meta.url);return $===void 0?(c(f,404,"Not found"),!0):(i(u,f,$,dr(L),L),!0)},v=Buffer.from(a),g=Ze((l,u)=>{const f=V(l.url??"/");if(o){const p=F(l,e.logger);if(p!==void 0){c(u,403,p);return}}if(f.startsWith(U)){if(!o){c(u,403,"Lunora studio: the worker admin proxy is only available on a loopback bind.");return}yr(l,u,t);return}if(f===tr){d(l,u,lr,"Lunora schema editing is only available on loopback hosts in dev.");return}if(f===or){d(l,u,ur,"Lunora policy scaffolding is only available on loopback hosts in dev.");return}if(f===nr){d(l,u,fr,"Lunora data seeding is only available on loopback hosts in dev.");return}m(f,l,u)||sr(u,v)});return g.on("upgrade",(l,u,f)=>{if(!o||F(l,e.logger)!==void 0){u.destroy();return}V(l.url??"").startsWith(U)?Tr(l,u,f,t):u.destroy()}),await new Promise((l,u)=>{g.once("error",f=>{f.code==="EADDRINUSE"?u(new Error(`studio port ${String(e.port)} is already in use — pass a different port with --port, or stop the process using it`,{cause:f})):u(f)}),g.listen(e.port,r,()=>{l()})}),{close:()=>new Promise(l=>{g.close(()=>{l()})}),url:`http://${r==="0.0.0.0"||r==="::"?"localhost":r}:${String(e.port)}`}},Ir=12e4,le=250,Rr=1e4,_r=100,Or=40,S=256*1024,Ar="LUNORA_DEV_READY_TIMEOUT_MS",Pr=new Set(["nuxt","sveltekit"]),D=e=>Pr.has(te(e).framework)?"framework-worker":ke(e).has("@lunora/vite")?"vite":"wrangler",ue=e=>{const r=ae(e);let o;try{const t=X(y(e,"package.json"),"utf8");o=JSON.parse(t).scripts?.dev}catch{}if(o===void 0||o.trim()===""||o.includes("lunora")){const t=I(r,"vite",["dev"]);return{args:t.args,command:t.command}}return{args:["run","dev"],command:r}},Dr=async e=>{try{return await fetch(new URL("/_lunora/status",e),{signal:AbortSignal.timeout(1e3)}),!0}catch{return!1}},Nr=e=>{we(Ee(e.logPath),{recursive:!0});const r=Q(e.logPath,"w",384);let o;const t=ce(e.command,e.args);try{o=z(t.command,t.args,{cwd:e.cwd,detached:!0,env:e.env,shell:t.shell,stdio:["ignore",r,r],windowsHide:!0})}finally{Z(r)}return o.unref(),{exited:new Promise(n=>{o.on("error",()=>{n(1)}),o.on("exit",(s,a)=>{n(s??(a?1:0))})}),pid:o.pid}},$r=e=>{try{const{size:r}=he(e);if(r<=S)return X(e,"utf8");const o=Q(e,"r");try{const t=Buffer.alloc(S),n=Se(o,t,0,S,r-S),s=t.toString("utf8",0,n);return s.slice(s.indexOf(`
3
+ `)+1)}finally{Z(o)}}catch{return}},fe=(e,r)=>{const o=$r(e);if(o===void 0)return[];const t=o.split(`
4
+ `);return t.at(-1)===""&&t.pop(),r>0?t.slice(-r):t},me=(e,r)=>r?.logFile??y(e,be),ve=e=>{e.info(" Stop: lunora dev stop"),e.info(" Status: lunora dev status"),e.info(" Logs: lunora dev logs")},k=(e,r)=>{e.warn(`Dev server already running at ${r.url} (pid ${String(r.pid)})`),ve(e)},xr=(e,r)=>{e.success(`Dev server running at ${r.url} (pid ${String(r.pid)})`),r.studioUrl!==void 0&&e.info(` Studio: ${r.studioUrl}`),ve(e)},Cr=async e=>{const{child:r,cwd:o,deadline:t,pollIntervalMs:n,probe:s}=e,a=r.exited.then(i=>({exitCode:i}));for(;Date.now()<t;){const i=b(o);if(i!==void 0&&i.pid!==process.pid&&await s(i.url))return{state:i,status:"ready"};const c=await Promise.race([a,R(n)]);if(c!==void 0)return{...c,status:"exited"}}return{status:"timeout"}},Mr=async e=>{const{cwd:r,logger:o}=e,t=me(r,void 0),n=Number(process.env[Ar]??""),s=e.readyTimeoutMs??(Number.isFinite(n)&&n>0?n:Ir),a=(e.spawnDetached??Nr)({args:e.command.args,command:e.command.command,cwd:r,env:{...process.env,...e.env,[P]:"1",[oe]:t,...process.env.FORCE_COLOR===void 0?{NO_COLOR:"1"}:{},...e.json?{LUNORA_LOG_JSON:"1"}:{}},logPath:t}),i=await Cr({child:a,cwd:r,deadline:Date.now()+s,pollIntervalMs:e.pollIntervalMs??le,probe:e.probe??Dr});if(i.status==="ready")return xr(o,i.state),{code:0};if(i.status==="exited"){o.error(`dev server exited before becoming ready (exit code ${String(i.exitCode)}). Last output:`);for(const d of fe(t,Or))o.error(` ${d}`);return w(r,a.pid??process.pid),{code:i.exitCode===0?1:i.exitCode}}a.pid!==void 0&&A(r)?.pid===process.pid&&ne(r,{logFile:t,pid:a.pid});const c=a.pid===void 0?"":` (pid ${String(a.pid)})`;return o.warn(`dev server did not confirm ready within ${String(Math.round(s/1e3))}s — it may still be compiling${c}. Check \`lunora dev status\` and \`lunora dev logs\`; \`lunora dev stop\` shuts it down.`),{code:1}},Fr=(e,r)=>{const o=["dev"];return e.apiSpec!==void 0&&o.push("--api-spec",e.apiSpec),e.port!==void 0&&o.push("--port",String(e.port)),e.workerPort!==void 0&&o.push("--worker-port",String(e.workerPort)),e.codegen===!1&&o.push("--no-codegen"),e.studio===!1&&o.push("--no-studio"),e.worker===!1&&o.push("--no-worker"),e.target!==void 0&&o.push("--target",e.target),r&&o.push("--remote"),o},Gr=async e=>{const{cwd:r,jsonLogs:o,logger:t,options:n,remote:s}=e,a=e.run??Mr,i=D(r),c=i==="wrangler"?`http://localhost:${String(n.workerPort??8787)}`:"http://localhost:5173",d=re(r,{background:!0,mode:"cli",pid:process.pid,startedAt:new Date().toISOString(),url:c});if(!d.ok)return d.existing!==void 0&&k(t,d.existing),{code:0};const m={[E]:String(process.pid)};try{return i==="vite"?await a({command:ue(r),cwd:r,env:{...s?{LUNORA_REMOTE:"1"}:{},...m},json:o,logger:t}):await a({command:{args:[process.argv[1]??"lunora",...Fr(n,s)],command:process.execPath},cwd:r,env:m,json:o,logger:t})}finally{w(r,process.pid)}},jr=e=>{try{const r=_("ps",["-o","pgid=","-p",String(e)],{encoding:"utf8"}),o=Number.parseInt(r.stdout.trim(),10);return Number.isInteger(o)&&o>0?o:void 0}catch{return}},Ur=(e,r,o=process.platform,t=_)=>{if(o==="win32"){try{t("taskkill",["/pid",String(e.pid),"/T","/F"],{stdio:"ignore"})}catch{}return}if(e.background===!0)try{r(-(jr(e.pid)??e.pid),"SIGKILL");return}catch{}try{r(e.pid,"SIGKILL")}catch{}},Vr=(e,r)=>(r.json?h({pid:e.pid,stopped:!1}):r.logger.error(`dev server (pid ${String(e.pid)}) survived the force-kill — record kept; retry \`lunora dev stop\` or kill the process manually.`),{code:1}),Hr=async e=>{const{cwd:r,logger:o}=e,t=A(r),n=e.alive??(v=>v===t?.pid&&ye(t)),s=e.signal??((v,g)=>{process.kill(v,g)}),a=e.pollIntervalMs??le,i=e.stopGraceMs??Rr,c=e.platform??process.platform,d=e.spawnSyncImpl??_;if(t===void 0||!n(t.pid))return t!==void 0&&w(r,t.pid),e.json?h({running:!1,stopped:!1}):o.info("No dev server running."),{code:0};try{s(t.pid,"SIGTERM")}catch{}const m=Date.now()+i;for(;n(t.pid)&&Date.now()<m;)await R(a);return n(t.pid)&&(Ur(t,s,c,d),await R(a),n(t.pid))?Vr(t,e):(w(r,t.pid),e.json?h({pid:t.pid,stopped:!0}):o.success(`Stopped dev server (pid ${String(t.pid)}).`),{code:0})},Wr=e=>{const{cwd:r,logger:o}=e,t=b(r);if(t===void 0)return e.json?h({running:!1}):o.info("No dev server running."),{code:0};const n=e.now??Date.now,s=t.startedAt===void 0?Number.NaN:Date.parse(t.startedAt),a=Number.isFinite(s)?Math.max(0,Math.round((n()-s)/1e3)):void 0;if(e.json)return h({background:t.background===!0,logFile:t.logFile,mode:t.mode,pid:t.pid,running:!0,startedAt:t.startedAt,studioUrl:t.studioUrl,uptimeSeconds:a,url:t.url}),{code:0};const i=[`pid ${String(t.pid)}`,...a===void 0?[]:[`uptime ${String(a)}s`],t.background===!0?"background":"foreground"];return o.success(`Dev server running at ${t.url} (${i.join(", ")})`),t.studioUrl!==void 0&&o.info(` Studio: ${t.studioUrl}`),t.logFile!==void 0&&o.info(` Logs: lunora dev logs (${t.logFile})`),{code:0}},Br=e=>{const{cwd:r,logger:o}=e,t=A(r),n=me(r,t);if(!O(n))return o.info("No dev server logs found — output is captured when the server runs in background mode (`lunora dev --background`)."),{code:0};const s=fe(n,e.lines??_r);return s.length>0&&process.stdout.write(`${s.join(`
5
5
  `)}
6
- `),{code:0}},Hr=e=>{const{cwd:r,json:o,lines:t,logger:n,subcommand:s}=e;if(s==="stop")return jr({cwd:r,json:o,logger:n});if(s==="status")return Ur({cwd:r,json:o,logger:n});if(s==="logs")return Vr({cwd:r,lines:t,logger:n});if(s!==void 0)return n.error(`dev: unknown subcommand "${s}" — expected stop | status | logs (or no subcommand to start the dev server)`),{code:1}},V="wrangler.dev.jsonc",H=6173,ve=8787,W=5173,Wr=5e3,Br=(e,r)=>{const o=()=>{};if(!e.remote)return{args:[],plan:{bindings:[],cleanup:o,enabled:!1}};const n=(e.materializeRemote??Ue)({enabled:!0,projectRoot:r}),s=n.remoteBindings.map(a=>`${a.binding} (${a.kind})`),{cleanup:i}=n;return n.configPath===void 0?{args:[],plan:{bindings:s,cleanup:i,enabled:!0,reason:n.reason}}:{args:["--config",n.configPath],plan:{bindings:s,cleanup:i,enabled:!0}}},B=e=>se(e).parsed?.dev?.ip,K=(e,r,o)=>{if(o!==void 0){const n=y(e,o);if(R(n)&&B(n)!==void 0)return[]}const t=ne(e);return t!==void 0&&B(t)!==void 0?[]:r()?[]:["--ip","127.0.0.1"]},Kr=async(e,r)=>{if(e.workerPort!==void 0)return e.workerPort;const o=ne(r);if(o!==void 0){const{parsed:t}=se(o);if(typeof t?.dev?.port=="number")return t.dev.port}return(e.findFreePort??hr)(ve)},Jr=e=>{const r=e.cwd??process.cwd(),o=ae(r),t=e.flavor??P(r);if(t==="vite"||t==="framework-worker"){const m=le(r);let v;if(t==="framework-worker"){const g=K(r,e.hasIpv6Loopback??G,V),l=xe(Ce(r)).toolchain?.dev({configPath:V,extraArgs:[...g,"--var","WORKER_ENV:development"]}),u=T(o,l?.tool??"wrangler",l?.args??[]);v={args:u.args,command:u.command,cwd:r,tag:"worker"}}return e.worker===!1&&e.logger.warn(`--no-worker does not apply to the ${t} flavor: Vite owns the worker, codegen and studio in-process. Run your framework's dev script instead.`),{codegenEnabled:!1,flavor:t,ipv4LoopbackForced:!1,remote:{bindings:[],cleanup:()=>{},enabled:e.remote===!0},...v?{sidecar:v}:{},studioEnabled:!1,studioPort:e.port??H,workerEnabled:!0,workerOrigin:`http://localhost:${String(W)}`,workerPort:W,wrangler:{args:m.args,command:m.command,cwd:r,...e.remote===!0?{env:{LUNORA_REMOTE:"1"}}:{},tag:"vite"}}}const n=re(r),s=n.framework==="none"?void 0:`this project uses ${n.framework} — the worker runs inside Vite there. run \`${Ke(o,"dev")}\` for the full app (frontend + HMR); \`lunora dev\` starts only the worker.`,i=e.workerPort??ve,a=Br(e,r),c=K(r,e.hasIpv6Loopback??G),d=T(o,"wrangler",["dev","--port",String(i),...c,"--var","WORKER_ENV:development",...a.args]);return{codegenEnabled:e.codegen!==!1,flavor:t,frameworkHint:s,ipv4LoopbackForced:c.length>0,remote:a.plan,studioEnabled:e.studio!==!1,studioPort:e.port??H,workerEnabled:e.worker!==!1,workerOrigin:`http://localhost:${String(i)}`,workerPort:i,wrangler:{args:d.args,command:d.command,cwd:r,tag:"wrangler"}}},J=(e,r,o,t)=>{if(e.length===0)return;const n=Ge(e);if(n){t[n.level](`[lunora] ${n.text}`);return}const s=`[${r}] ${e}`;o==="stderr"?t.warn(s):t.info(s)},Yr=(e,r,o)=>{const t=(n,s)=>{if(!n)return;let i="";n.on("data",a=>{i+=a.toString("utf8");const c=i.split(`
7
- `);i=c.pop()??"";for(const d of c)J(d.trimEnd(),r,s,o)}),n.on("end",()=>{J(i.trimEnd(),r,s,o),i=""})};t(e.stdout,"stdout"),t(e.stderr,"stderr")},zr=(e,r)=>{const o=ie(e.command,e.args),t=Y(o.command,o.args,{cwd:e.cwd??process.cwd(),env:e.env?{...process.env,...e.env}:process.env,shell:o.shell,stdio:["inherit","pipe","pipe"]});return Yr(t,e.tag,r),{exited:new Promise(n=>{t.on("error",s=>{r.error(`[${e.tag}] failed to start: ${s.message}`),n(1)}),t.on("exit",s=>{n(s??0)})}),kill:n=>{try{t.kill(n)}catch{}}}},Xr=(e,r,o)=>{e.info(""),e.success("Lunora dev"),e.info(` ➜ Worker: ${r.workerOrigin}`),o!==void 0&&e.info(` ➜ Studio: ${o}`),r.codegenEnabled&&e.info(" ➜ Codegen: watching lunora/"),r.remote.enabled&&(r.remote.bindings.length>0?e.info(` ➜ Remote: ${r.remote.bindings.join(", ")} → deployed worker`):e.warn(` ➜ Remote: requested but inactive (${r.remote.reason??"no eligible bindings"}) — running fully local`)),e.info("")},Qr=(e,r)=>{q(r).installed||!Ne()||(e.info(` ⓘ ${$e}`),e.info(""))},Zr=(e,r)=>{if(process.env.LUNORA_CONTAINER_LOGS==="0")return;const t=Me(e,"lunora").containers.map(n=>({className:n.className,exportName:n.exportName}));if(t.length!==0)return Fe({containers:t,onLine:n=>{const s=`[container:${n.name}] ${n.text}`;n.level==="error"?r.warn(s):r.info(s)},onUnavailable:n=>{r.warn(`[container] Docker engine unreachable — container logs unavailable (${n})`)}})},qr=async e=>{e.codegen?.close(),e.containerLogs?.close(),await e.studio?.close().catch(()=>{});try{e.remoteCleanup?.()}catch{}},et=async(e,r)=>{try{const t=await Ie({projectRoot:r}),n=Re(t),s=(e.ensureExample??_e)(r,n);s.length>0&&e.logger.info(`Updated .dev.vars.example with secrets for: ${n.join(", ")} (${s.join(", ")})`)}catch{}(await(e.ensureEnv??Oe)({confirm:ur(),cwd:r,info:t=>{e.logger.info(t)}})).status==="declined"&&!Pe()&&e.logger.info(`hint: ${$} was not scaffolded (non-interactive run). Copy ${Ae} → ${$} and fill in secrets, or run \`lunora dev\` in an interactive terminal to scaffold automatically.`);try{(e.fillSecrets??De)({cwd:r,info:t=>{e.logger.info(t)}})}catch{}},rt=(e,r,o,t)=>{if(e.flavor!=="wrangler")return;t!==void 0&&oe(r,{studioUrl:t});let n;try{n=Zr(r,o)}catch{}return Xr(o,e,t),n},tt=(e,r)=>{const o=Number(process.env[E]),t=ee(r,{background:process.env[O]==="1",logFile:process.env[te],mode:"cli",pid:process.pid,startedAt:new Date().toISOString(),url:e.workerOrigin},Number.isInteger(o)&&o>0?{supersedePid:o}:void 0);return t.ok?void 0:t.existing},ot=async e=>{const r=e.cwd??process.cwd(),o=e.flavor??P(r),t=o==="wrangler"?await Kr(e,r):e.workerPort;return Jr({...e,cwd:r,flavor:o,workerPort:t})},nt=async e=>await new Promise(r=>{const o={},t=i=>{e.info(`received ${i} — shutting down`),o.sigint&&process.off("SIGINT",o.sigint),o.sigterm&&process.off("SIGTERM",o.sigterm),r(0)},n=()=>{t("SIGINT")},s=()=>{t("SIGTERM")};o.sigint=n,o.sigterm=s,process.on("SIGINT",n),process.on("SIGTERM",s)}),st=async(e,r,o)=>{let t=0,n;const s=d=>{e.kill(d),r?.kill(d)},i=()=>{t+=1,t===1?(o.info("received SIGINT — shutting down (press Ctrl-C again to force-kill)"),s("SIGTERM"),n=setTimeout(()=>{s("SIGKILL")},Wr),n.unref()):s("SIGKILL")},a=()=>{s("SIGTERM")};process.on("SIGINT",i),process.on("SIGTERM",a);const c=await Promise.race([e.exited.then(d=>({code:d,who:"worker"})),...r===void 0?[]:[r.exited.then(d=>({code:d,who:"sidecar"}))]]);return r!==void 0&&(c.who==="sidecar"?(o.warn("[worker] the Lunora sidecar (wrangler dev) exited — shutting down the framework dev server"),e.kill("SIGTERM")):r.kill("SIGTERM"),await Promise.allSettled([e.exited,r.exited])),n&&clearTimeout(n),process.off("SIGINT",i),process.off("SIGTERM",a),c.code},at=async(e,r,o,t)=>{if(r.studioEnabled)try{return await(e.startStudio??yr)({cwd:o,logger:{warnOnce:n=>{t.warn(n)}},port:r.studioPort,workerOrigin:r.workerOrigin})}catch(n){t.warn(`studio server failed to start (${n instanceof Error?n.message:String(n)}) — continuing without it`);return}},it=e=>{const r=[e.codegenEnabled?"codegen watch":void 0,e.studioEnabled?"studio":void 0].filter(t=>t!==void 0);return`--no-worker: not starting wrangler. ${r.length>0?`${r.join(" + ")} running`:"nothing else to run"}; your task runner owns the worker on ${e.workerOrigin}.`},ct=(e,r,o,t,n)=>{if(e.sidecar!==void 0)try{Z({apiSpec:r.apiSpec,lunoraDirectory:"lunora",projectRoot:o,target:n})}catch(s){t.warn(`codegen (pre-sidecar) failed: ${s instanceof Error?s.message:String(s)} — the framework dev server will retry`)}},dt=async e=>{const r=await ot(e),{logger:o}=e,t=r.wrangler.cwd??process.cwd(),n=Le(t,e.target),s={remoteCleanup:r.remote.cleanup};try{const i=Number(process.env[E]),a=b(t);if(a!==void 0&&a.pid!==process.pid&&a.pid!==i)return k(o,a),{code:0,plan:r};const c=tt(r,t);if(c!==void 0)return k(o,c),{code:0,plan:r};(r.flavor==="vite"||r.flavor==="framework-worker")&&(r.wrangler.env={...r.wrangler.env,[E]:String(process.pid)}),await et(e,t),o.info(r.flavor==="vite"?"starting vite dev (worker + studio + codegen run inside Vite via @lunora/vite)":"starting wrangler dev + studio"),r.ipv4LoopbackForced&&o.info("no IPv6 loopback (::1) on this host — binding the worker to 127.0.0.1 (--ip) so wrangler dev doesn't crash. Pin `dev.ip` in wrangler.jsonc to override."),r.codegenEnabled&&(s.codegen=(e.startCodegen??vr)({apiSpec:e.apiSpec,logger:o,projectRoot:t,target:n})),s.studio=await at(e,r,t,o);const d=s.studio?.url;if(r.frameworkHint!==void 0&&o.warn(r.frameworkHint),!r.workerEnabled)return o.info(it(r)),{code:await nt(o),plan:r};ct(r,e,t,o,n);const m=e.startWorker??zr,v=m(r.wrangler,o),g=r.sidecar===void 0?void 0:m(r.sidecar,o);return s.containerLogs=rt(r,t,o,d),Qr(o,t),{code:await st(v,g,o),plan:r}}finally{w(t,process.pid),await qr(s)}},Pt=Be(async({argument:e,cwd:r,logger:o,options:t})=>{const n=t.json===!0,s=Hr({cwd:r,json:n,lines:t.lines,logger:o,subcommand:e[0]});if(s!==void 0)return s;const i=process.env[O]==="1",a=i?void 0:be(),c=n||a!==void 0;c&&ze(),a!==void 0&&t.background!==!0&&o.info(`AI agent detected (${a.name} via ${a.variable}) — starting the dev server in background mode with JSON logs. Set LUNORA_AGENT_MODE=0 to opt out.`);const d=je({configPreference:Te(r),envValue:process.env.LUNORA_REMOTE,flag:t.remote});if(!i&&(t.background===!0||a!==void 0)){const m=b(r);return m!==void 0?(k(o,m),{code:0}):Cr({cwd:r,jsonLogs:c,logger:o,options:t,remote:d})}return dt({apiSpec:Ve(t.apiSpec),codegen:t.codegen===!1?!1:void 0,cwd:r,logger:o,port:t.port,remote:d,studio:t.studio===!1?!1:void 0,target:t.target,workerPort:t.workerPort})});export{P as detectDevFlavor,Pt as execute,Jr as planDevCommand,Kr as resolveWorkerPort,dt as runDevCommand};
6
+ `),{code:0}},Kr=e=>{const{cwd:r,json:o,lines:t,logger:n,subcommand:s}=e;if(s==="stop")return Hr({cwd:r,json:o,logger:n});if(s==="status")return Wr({cwd:r,json:o,logger:n});if(s==="logs")return Br({cwd:r,lines:t,logger:n});if(s!==void 0)return n.error(`dev: unknown subcommand "${s}" — expected stop | status | logs (or no subcommand to start the dev server)`),{code:1}},H="wrangler.dev.jsonc",W=6173,ge=8787,B=5173,Jr=5e3,Yr=(e,r)=>{const o=()=>{};if(!e.remote)return{args:[],plan:{bindings:[],cleanup:o,enabled:!1}};const n=(e.materializeRemote??Ve)({enabled:!0,projectRoot:r}),s=n.remoteBindings.map(i=>`${i.binding} (${i.kind})`),{cleanup:a}=n;return n.configPath===void 0?{args:[],plan:{bindings:s,cleanup:a,enabled:!0,reason:n.reason}}:{args:["--config",n.configPath],plan:{bindings:s,cleanup:a,enabled:!0}}},K=e=>ie(e).parsed?.dev?.ip,J=(e,r,o)=>{if(o!==void 0){const n=y(e,o);if(O(n)&&K(n)!==void 0)return[]}const t=se(e);return t!==void 0&&K(t)!==void 0?[]:r()?[]:["--ip","127.0.0.1"]},zr=async(e,r)=>{if(e.workerPort!==void 0)return e.workerPort;const o=se(r);if(o!==void 0){const{parsed:t}=ie(o);if(typeof t?.dev?.port=="number")return t.dev.port}return(e.findFreePort??kr)(ge)},Xr=e=>{const r=e.cwd??process.cwd(),o=ae(r),t=e.flavor??D(r);if(t==="vite"||t==="framework-worker"){const m=ue(r);let v;if(t==="framework-worker"){const g=J(r,e.hasIpv6Loopback??j,H),l=Ce(Me(r)).toolchain?.dev({configPath:H,extraArgs:[...g,"--var","WORKER_ENV:development"]}),u=I(o,l?.tool??"wrangler",l?.args??[]);v={args:u.args,command:u.command,cwd:r,tag:"worker"}}return e.worker===!1&&e.logger.warn(`--no-worker does not apply to the ${t} flavor: Vite owns the worker, codegen and studio in-process. Run your framework's dev script instead.`),{codegenEnabled:!1,flavor:t,ipv4LoopbackForced:!1,remote:{bindings:[],cleanup:()=>{},enabled:e.remote===!0},...v?{sidecar:v}:{},studioEnabled:!1,studioPort:e.port??W,workerEnabled:!0,workerOrigin:`http://localhost:${String(B)}`,workerPort:B,wrangler:{args:m.args,command:m.command,cwd:r,...e.remote===!0?{env:{LUNORA_REMOTE:"1"}}:{},tag:"vite"}}}const n=te(r),s=n.framework==="none"?void 0:`this project uses ${n.framework} — the worker runs inside Vite there. run \`${Je(o,"dev")}\` for the full app (frontend + HMR); \`lunora dev\` starts only the worker.`,a=e.workerPort??ge,i=Yr(e,r),c=J(r,e.hasIpv6Loopback??j),d=I(o,"wrangler",["dev","--port",String(a),...c,"--var","WORKER_ENV:development",...i.args]);return{codegenEnabled:e.codegen!==!1,flavor:t,frameworkHint:s,ipv4LoopbackForced:c.length>0,remote:i.plan,studioEnabled:e.studio!==!1,studioPort:e.port??W,workerEnabled:e.worker!==!1,workerOrigin:`http://localhost:${String(a)}`,workerPort:a,wrangler:{args:d.args,command:d.command,cwd:r,tag:"wrangler"}}},Y=(e,r,o,t)=>{if(e.length===0)return;const n=je(e);if(n){t[n.level](`[lunora] ${n.text}`);return}const s=`[${r}] ${e}`;o==="stderr"?t.warn(s):t.info(s)},Qr=(e,r,o)=>{const t=(n,s)=>{if(!n)return;let a="";n.on("data",i=>{a+=i.toString("utf8");const c=a.split(`
7
+ `);a=c.pop()??"";for(const d of c)Y(d.trimEnd(),r,s,o)}),n.on("end",()=>{Y(a.trimEnd(),r,s,o),a=""})};t(e.stdout,"stdout"),t(e.stderr,"stderr")},Zr=(e,r)=>{const o=ce(e.command,e.args),t=z(o.command,o.args,{cwd:e.cwd??process.cwd(),env:e.env?{...process.env,...e.env}:process.env,shell:o.shell,stdio:["inherit","pipe","pipe"]});return Qr(t,e.tag,r),{exited:new Promise(n=>{t.on("error",s=>{r.error(`[${e.tag}] failed to start: ${s.message}`),n(1)}),t.on("exit",s=>{n(s??0)})}),kill:n=>{try{t.kill(n)}catch{}}}},qr=(e,r,o)=>{e.info(""),e.success("Lunora dev"),e.info(` ➜ Worker: ${r.workerOrigin}`),o!==void 0&&e.info(` ➜ Studio: ${o}`),r.codegenEnabled&&e.info(" ➜ Codegen: watching lunora/"),r.remote.enabled&&(r.remote.bindings.length>0?e.info(` ➜ Remote: ${r.remote.bindings.join(", ")} → deployed worker`):e.warn(` ➜ Remote: requested but inactive (${r.remote.reason??"no eligible bindings"}) — running fully local`)),e.info("")},et=(e,r)=>{ee(r).installed||!$e()||(e.info(` ⓘ ${xe}`),e.info(""))},rt=(e,r)=>{if(process.env.LUNORA_CONTAINER_LOGS==="0")return;const t=Fe(e,"lunora").containers.map(n=>({className:n.className,exportName:n.exportName}));if(t.length!==0)return Ge({containers:t,onLine:n=>{const s=`[container:${n.name}] ${n.text}`;n.level==="error"?r.warn(s):r.info(s)},onUnavailable:n=>{r.warn(`[container] Docker engine unreachable — container logs unavailable (${n})`)}})},tt=async e=>{e.codegen?.close(),e.containerLogs?.close(),await e.studio?.close().catch(()=>{});try{e.remoteCleanup?.()}catch{}},ot=async(e,r)=>{try{const t=await Re({projectRoot:r}),n=_e(t),s=(e.ensureExample??Oe)(r,n);s.length>0&&e.logger.info(`Updated .dev.vars.example with secrets for: ${n.join(", ")} (${s.join(", ")})`)}catch{}(await(e.ensureEnv??Ae)({confirm:vr(),cwd:r,info:t=>{e.logger.info(t)}})).status==="declined"&&!Pe()&&e.logger.info(`hint: ${x} was not scaffolded (non-interactive run). Copy ${De} → ${x} and fill in secrets, or run \`lunora dev\` in an interactive terminal to scaffold automatically.`);try{(e.fillSecrets??Ne)({cwd:r,info:t=>{e.logger.info(t)}})}catch{}},nt=(e,r,o,t)=>{if(e.flavor!=="wrangler")return;t!==void 0&&ne(r,{studioUrl:t});let n;try{n=rt(r,o)}catch{}return qr(o,e,t),n},st=(e,r)=>{const o=Number(process.env[E]),t=re(r,{background:process.env[P]==="1",logFile:process.env[oe],mode:"cli",pid:process.pid,startedAt:new Date().toISOString(),url:e.workerOrigin},Number.isInteger(o)&&o>0?{supersedePid:o}:void 0);return t.ok?void 0:t.existing},it=async e=>{const r=e.cwd??process.cwd(),o=e.flavor??D(r),t=o==="wrangler"?await zr(e,r):e.workerPort;return Xr({...e,cwd:r,flavor:o,workerPort:t})},at=async e=>await new Promise(r=>{const o={},t=a=>{e.info(`received ${a} — shutting down`),o.sigint&&process.off("SIGINT",o.sigint),o.sigterm&&process.off("SIGTERM",o.sigterm),r(0)},n=()=>{t("SIGINT")},s=()=>{t("SIGTERM")};o.sigint=n,o.sigterm=s,process.on("SIGINT",n),process.on("SIGTERM",s)}),ct=async(e,r,o)=>{let t=0,n;const s=d=>{e.kill(d),r?.kill(d)},a=()=>{t+=1,t===1?(o.info("received SIGINT — shutting down (press Ctrl-C again to force-kill)"),s("SIGTERM"),n=setTimeout(()=>{s("SIGKILL")},Jr),n.unref()):s("SIGKILL")},i=()=>{s("SIGTERM")};process.on("SIGINT",a),process.on("SIGTERM",i);const c=await Promise.race([e.exited.then(d=>({code:d,who:"worker"})),...r===void 0?[]:[r.exited.then(d=>({code:d,who:"sidecar"}))]]);return r!==void 0&&(c.who==="sidecar"?(o.warn("[worker] the Lunora sidecar (wrangler dev) exited — shutting down the framework dev server"),e.kill("SIGTERM")):r.kill("SIGTERM"),await Promise.allSettled([e.exited,r.exited])),n&&clearTimeout(n),process.off("SIGINT",a),process.off("SIGTERM",i),c.code},dt=async(e,r,o,t)=>{if(r.studioEnabled)try{return await(e.startStudio??Lr)({cwd:o,logger:{warnOnce:n=>{t.warn(n)}},port:r.studioPort,workerOrigin:r.workerOrigin})}catch(n){t.warn(`studio server failed to start (${n instanceof Error?n.message:String(n)}) — continuing without it`);return}},lt=e=>{const r=[e.codegenEnabled?"codegen watch":void 0,e.studioEnabled?"studio":void 0].filter(t=>t!==void 0);return`--no-worker: not starting wrangler. ${r.length>0?`${r.join(" + ")} running`:"nothing else to run"}; your task runner owns the worker on ${e.workerOrigin}.`},ut=(e,r,o,t,n)=>{if(e.sidecar!==void 0)try{q({apiSpec:r.apiSpec,lunoraDirectory:"lunora",projectRoot:o,target:n})}catch(s){t.warn(`codegen (pre-sidecar) failed: ${s instanceof Error?s.message:String(s)} — the framework dev server will retry`)}},ft=async e=>{const r=await it(e),{logger:o}=e,t=r.wrangler.cwd??process.cwd(),n=Ie(t,e.target),s={remoteCleanup:r.remote.cleanup};try{const a=Number(process.env[E]),i=b(t);if(i!==void 0&&i.pid!==process.pid&&i.pid!==a)return k(o,i),{code:0,plan:r};const c=st(r,t);if(c!==void 0)return k(o,c),{code:0,plan:r};(r.flavor==="vite"||r.flavor==="framework-worker")&&(r.wrangler.env={...r.wrangler.env,[E]:String(process.pid)}),await ot(e,t),o.info(r.flavor==="vite"?"starting vite dev (worker + studio + codegen run inside Vite via @lunora/vite)":"starting wrangler dev + studio"),r.ipv4LoopbackForced&&o.info("no IPv6 loopback (::1) on this host — binding the worker to 127.0.0.1 (--ip) so wrangler dev doesn't crash. Pin `dev.ip` in wrangler.jsonc to override."),r.codegenEnabled&&(s.codegen=(e.startCodegen??wr)({apiSpec:e.apiSpec,logger:o,projectRoot:t,target:n})),s.studio=await dt(e,r,t,o);const d=s.studio?.url;if(r.frameworkHint!==void 0&&o.warn(r.frameworkHint),!r.workerEnabled)return o.info(lt(r)),{code:await at(o),plan:r};ut(r,e,t,o,n);const m=e.startWorker??Zr,v=m(r.wrangler,o),g=r.sidecar===void 0?void 0:m(r.sidecar,o);return s.containerLogs=nt(r,t,o,d),et(o,t),{code:await ct(v,g,o),plan:r}}finally{w(t,process.pid),await tt(s)}},Nt=Ke(async({argument:e,cwd:r,logger:o,options:t})=>{const n=t.json===!0,s=Kr({cwd:r,json:n,lines:t.lines,logger:o,subcommand:e[0]});if(s!==void 0)return s;const a=process.env[P]==="1",i=a?void 0:Te(),c=n||i!==void 0;c&&Xe(),i!==void 0&&t.background!==!0&&o.info(`AI agent detected (${i.name} via ${i.variable}) — starting the dev server in background mode with JSON logs. Set LUNORA_AGENT_MODE=0 to opt out.`);const d=Ue({configPreference:Le(r),envValue:process.env.LUNORA_REMOTE,flag:t.remote});if(!a&&(t.background===!0||i!==void 0)){const m=b(r);return m!==void 0?(k(o,m),{code:0}):Gr({cwd:r,jsonLogs:c,logger:o,options:t,remote:d})}return ft({apiSpec:He(t.apiSpec),codegen:t.codegen===!1?!1:void 0,cwd:r,logger:o,port:t.port,remote:d,studio:t.studio===!1?!1:void 0,target:t.target,workerPort:t.workerPort})});export{D as detectDevFlavor,Nt as execute,Xr as planDevCommand,zr as resolveWorkerPort,ft as runDevCommand};
@@ -1,4 +1,4 @@
1
- import{existsSync as m,mkdirSync as T,writeFileSync as h,mkdtempSync as N,rmSync as M,readFileSync as I}from"node:fs";import{tmpdir as R}from"node:os";import{discoverSchema as _,readPackageDependencies as U,discoverMigrations as x}from"@lunora/codegen";import{isInteractive as C,promptSelect as O,promptText as F}from"@lunora/config";import{LunoraError as y}from"@lunora/errors";import{join as c}from"@visulima/path";import{Project as b}from"ts-morph";import{r as P}from"../packem_shared/admin-url-Cil3Or6N.mjs";import{d as D}from"../packem_shared/command-DwC11Cwe.mjs";import{diffSnapshots as z,renderMigrationFile as L}from"../packem_shared/diffSnapshots-CIOFQN_a.mjs";import{a as j}from"../packem_shared/resolve-target-C_ZloTvd.mjs";import q from"../packem_shared/schemaIrToSnapshot-qpRoV7HY.mjs";import{readAndLogBody as H}from"./runRpcCommand.mjs";import{runExportCommand as G}from"../packem_shared/runExportCommand-DmXKEWSq.mjs";import{r as B}from"../packem_shared/import-Db3Gk4mx.mjs";const u="__lunora_reproject__",W=e=>{if(!e.startsWith(u))return;const r=e.slice(u.length);return r===""?void 0:r},J=".snapshot.json",k=/[^\da-z]+/gu,A=(e,r)=>{let n=0,t=e.length;for(;n<t&&e[n]===r;)n+=1;for(;t>n&&e[t-1]===r;)t-=1;return e.slice(n,t)},K=e=>{const r=A(e.toLowerCase().replaceAll(k,"_"),"_");return r===""?"auto":r},V=e=>{const r=(n,t=2)=>n.toString().padStart(t,"0");return`${String(e.getUTCFullYear())}${r(e.getUTCMonth()+1)}${r(e.getUTCDate())}${r(e.getUTCHours())}${r(e.getUTCMinutes())}${r(e.getUTCSeconds())}`},Z=e=>{if(m(e))try{const r=I(e,"utf8"),n=JSON.parse(r);if(n.version!==1)throw new y("INTERNAL",`unsupported snapshot version: ${n.version}`);return n}catch(r){const n=r instanceof Error?r.message:String(r);throw new y("INTERNAL",`failed to read ${e}: ${n}`,{cause:r})}},X=e=>{const r=e.cwd??process.cwd(),n=c(r,"lunora","schema.ts");if(!m(n))return e.logger.error(`schema not found: ${n} — run \`vis generate lunora-table --name=<name>\` to create one`),{code:1,empty:!0,migrationFile:""};const t=new b({skipAddingFilesFromTsConfig:!0}),a=_(t,n),s=q(a),o=c(r,"lunora","migrations"),i=c(o,J);let d;try{d=Z(i)}catch(g){return e.logger.error(g instanceof Error?g.message:String(g)),{code:1,empty:!0,migrationFile:""}}const l=z(d,s);if(l.empty)return e.logger.info("no schema changes detected — snapshot is already up to date"),{code:0,empty:!0,migrationFile:""};const v=(e.now??(()=>new Date))(),p=K(e.name??"auto"),w=`${V(v)}_${p}.sql`,f=c(o,w);T(o,{recursive:!0});const E=L(p,l,v.toISOString());return h(f,E,"utf8"),h(i,`${JSON.stringify(s,void 0,4)}
1
+ import{existsSync as m,mkdirSync as T,writeFileSync as h,mkdtempSync as N,rmSync as M,readFileSync as I}from"node:fs";import{tmpdir as R}from"node:os";import{discoverSchema as _,readPackageDependencies as U,discoverMigrations as x}from"@lunora/codegen";import{isInteractive as C,promptSelect as O,promptText as F}from"@lunora/config";import{LunoraError as y}from"@lunora/errors";import{join as c}from"@visulima/path";import{Project as b}from"ts-morph";import{r as P}from"../packem_shared/admin-url-Cil3Or6N.mjs";import{d as D}from"../packem_shared/command-DwC11Cwe.mjs";import{diffSnapshots as z,renderMigrationFile as L}from"../packem_shared/diffSnapshots-CIOFQN_a.mjs";import{a as j}from"../packem_shared/resolve-target-C_ZloTvd.mjs";import q from"../packem_shared/schemaIrToSnapshot-qpRoV7HY.mjs";import{readAndLogBody as H}from"./runRpcCommand.mjs";import{runExportCommand as G}from"../packem_shared/runExportCommand-DmXKEWSq.mjs";import{r as B}from"../packem_shared/import-CrVGrOt3.mjs";const u="__lunora_reproject__",W=e=>{if(!e.startsWith(u))return;const r=e.slice(u.length);return r===""?void 0:r},J=".snapshot.json",k=/[^\da-z]+/gu,A=(e,r)=>{let n=0,t=e.length;for(;n<t&&e[n]===r;)n+=1;for(;t>n&&e[t-1]===r;)t-=1;return e.slice(n,t)},K=e=>{const r=A(e.toLowerCase().replaceAll(k,"_"),"_");return r===""?"auto":r},V=e=>{const r=(n,t=2)=>n.toString().padStart(t,"0");return`${String(e.getUTCFullYear())}${r(e.getUTCMonth()+1)}${r(e.getUTCDate())}${r(e.getUTCHours())}${r(e.getUTCMinutes())}${r(e.getUTCSeconds())}`},Z=e=>{if(m(e))try{const r=I(e,"utf8"),n=JSON.parse(r);if(n.version!==1)throw new y("INTERNAL",`unsupported snapshot version: ${n.version}`);return n}catch(r){const n=r instanceof Error?r.message:String(r);throw new y("INTERNAL",`failed to read ${e}: ${n}`,{cause:r})}},X=e=>{const r=e.cwd??process.cwd(),n=c(r,"lunora","schema.ts");if(!m(n))return e.logger.error(`schema not found: ${n} — run \`vis generate lunora-table --name=<name>\` to create one`),{code:1,empty:!0,migrationFile:""};const t=new b({skipAddingFilesFromTsConfig:!0}),a=_(t,n),s=q(a),o=c(r,"lunora","migrations"),i=c(o,J);let d;try{d=Z(i)}catch(g){return e.logger.error(g instanceof Error?g.message:String(g)),{code:1,empty:!0,migrationFile:""}}const l=z(d,s);if(l.empty)return e.logger.info("no schema changes detected — snapshot is already up to date"),{code:0,empty:!0,migrationFile:""};const v=(e.now??(()=>new Date))(),p=K(e.name??"auto"),w=`${V(v)}_${p}.sql`,f=c(o,w);T(o,{recursive:!0});const E=L(p,l,v.toISOString());return h(f,E,"utf8"),h(i,`${JSON.stringify(s,void 0,4)}
2
2
  `,"utf8"),e.logger.success(`wrote ${f}`),l.unsupported.length>0&&e.logger.warn(`${String(l.unsupported.length)} unsupported diff(s) — see the comment block in ${w} and write the SQL manually`),{code:0,empty:!1,migrationFile:f}},Q="migrations.ts",$=/^[A-Za-z_]\w*$/u,Y=new Set(["await","break","case","catch","class","const","continue","debugger","default","delete","do","else","enum","export","extends","false","finally","for","function","if","implements","import","in","instanceof","interface","let","new","null","package","private","protected","public","return","static","super","switch","this","throw","true","try","typeof","var","void","while","with","yield"]),S=/^import\s*\{\s*defineMigration\s*\}\s*from\s*["'](?:@lunora\/server|lunorash\/server)["'][^\n]*$/mu,ee=e=>`import { defineMigration } from "${U(e)?.has("lunorash")??!1?"lunorash/server":"@lunora/server"}";`,re="__lunora_admin__:runMigration",te="__lunora_admin__:migrationStatus",ne="/_lunora/migrate",ae=e=>A(e.trim().toLowerCase().replaceAll(k,"-"),"-"),oe=e=>e.split("-").filter(r=>r.length>0).map((r,n)=>n===0?r:r.charAt(0).toUpperCase()+r.slice(1)).join(""),ie=e=>{const r=c(e,"lunora","schema.ts");if(!m(r))return[];try{const n=new b({skipAddingFilesFromTsConfig:!0});return _(n,r).tables.map(t=>t.name)}catch{return[]}},se=async e=>e.length>0?O("Which table does this migration iterate?",e.map(r=>({label:r,value:r}))):F("Target table for the migration: "),ce=async(e,r)=>{if(r.table!==void 0)return r.table;const n=r.promptTable??(C()?se:void 0);if(n===void 0){r.logger.error("migrate create requires a target table when not running interactively — re-run with --table <table>");return}const a=(await n(ie(e)))?.trim();if(a===void 0||a===""){r.logger.error("no table selected — re-run with --table <table>");return}return a},de=async e=>{const r=e.cwd??process.cwd(),n=ae(e.name);if(n==="")return e.logger.error(`invalid migration name: "${e.name}" — must contain at least one alphanumeric character`),{code:1,file:""};const t=oe(n);if(!$.test(t)||Y.has(t))return e.logger.error(`invalid migration name: "${e.name}" derives the export \`${t}\`, which is not a valid identifier — pick a name that starts with a letter and isn't a reserved word`),{code:1,file:""};const a=await ce(r,e);if(a===void 0)return{code:1,file:""};if(!$.test(a))return e.logger.error(`invalid table: "${a}" — must be a valid identifier ([A-Za-z_][A-Za-z0-9_]*)`),{code:1,file:""};const s=c(r,"lunora"),o=c(s,Q);let i=m(o)?I(o,"utf8"):"";if(i.includes(`id: "${n}"`)||new RegExp(String.raw`\bexport const ${t}\b`,"u").test(i))return e.logger.error(`a migration with id "${n}" (export \`${t}\`) already exists in ${o}`),{code:1,file:""};const d=ee(r);i.trim()===""?i=`${d}
3
3
  `:S.test(i)?i=i.replace(S,d):i=`${d}
4
4
  ${i}`;const l=`export const ${t} = defineMigration({
@@ -1 +1 @@
1
- import"./admin-token-rxXZbVMZ.mjs";import"./admin-url-Cil3Or6N.mjs";import{D as a,r as i}from"./import-Db3Gk4mx.mjs";import"./shared-D-zCOmgY.mjs";export{a as DEFAULT_IMPORT_BATCH_SIZE,i as runImportCommand};
1
+ import"./admin-token-rxXZbVMZ.mjs";import"./admin-url-Cil3Or6N.mjs";import{D as a,r as i}from"./import-CrVGrOt3.mjs";import"./shared-D-zCOmgY.mjs";export{a as DEFAULT_IMPORT_BATCH_SIZE,i as runImportCommand};
@@ -1,12 +1,12 @@
1
- import{createReadStream as v}from"node:fs";import{open as Ve,stat as _,readdir as R,realpath as Q,readFile as $,mkdir as P,writeFile as le,appendFile as He}from"node:fs/promises";import{r as Ge}from"./admin-token-rxXZbVMZ.mjs";import{r as We}from"./admin-url-Cil3Or6N.mjs";import{join as y,resolve as k,basename as m,dirname as ue,relative as Ye,sep as Xe}from"node:path";import{createInterface as x}from"node:readline";import qe from"adm-zip";import{i as fe}from"./path-containment-CgxYZggb.mjs";import{Transform as Ke}from"node:stream";import{createInflateRaw as Ze,crc32 as Qe}from"node:zlib";import{LunoraError as f}from"@lunora/errors";import{parse as et}from"csv-parse/sync";import{parse as he}from"csv-parse";import{STORAGE_UPLOAD_MAX_BODY_BYTES as tt}from"@lunora/runtime";import{S as U,a as rt,I as nt}from"./shared-D-zCOmgY.mjs";import{createHash as st}from"node:crypto";const ot=67324752,I=30,it=26,at=28,ee=0,ct=8,dt=async(e,t)=>{const r=await Ve(e,"r");try{const n=Buffer.alloc(I),{bytesRead:s}=await r.read(n,0,I,t);if(s<I||n.readUInt32LE(0)!==ot)throw new Error(`${e}: no local file header at offset ${String(t)} — the archive is truncated or corrupt`);return t+I+n.readUInt16LE(it)+n.readUInt16LE(at)}finally{await r.close()}},te=e=>{let t=0;return new Ke({flush(r){if(t===e.header.crc){r();return}r(new Error(`${e.entryName} failed its CRC check — the archive is corrupt or truncated (expected ${String(e.header.crc)}, read ${String(t)})`))},transform(r,n,s){t=Qe(r,t),s(void 0,r)}})},lt=async(e,t)=>{const{compressedSize:r,encrypted:n,method:s,offset:o}=t.header;if(n)throw new Error(`${t.entryName} is encrypted — decrypt the archive before importing`);if(s!==ee&&s!==ct)throw new Error(`${t.entryName} uses unsupported compression method ${String(s)} — re-create the archive with standard deflate`);if(r===0){if(t.header.size>0||t.header.crc!==0)throw new Error(`${t.entryName} declares 0 compressed bytes but ${String(t.header.size)} uncompressed with CRC ${String(t.header.crc)} — the archive is corrupt`);return}const i=await dt(e,o),a=v(e,{end:i+r-1,start:i});if(s===ee){const d=te(t);return a.on("error",u=>d.destroy(u)),a.pipe(d)}const c=Ze(),l=te(t);return a.on("error",d=>c.destroy(d)),c.on("error",d=>l.destroy(d)),a.pipe(c).pipe(l)},N="_storage",ge=e=>e.startsWith("_"),ut=e=>{for(const t of e.getEntries()){const n=t.entryName.replaceAll("\\","/").split("/");if(n.length>=2&&n[n.length-2]==="_storage")return n.slice(0,-1).join("/")}return"_storage"},ft=async e=>{const t=await _(e).catch(()=>{});if(t?.isDirectory())return{kind:"directory",root:e};if(t?.isFile()&&e.toLowerCase().endsWith(".zip")){const r=new qe(e);return{kind:"zip",storagePrefix:ut(r),zip:r,zipPath:e}}},ht=async e=>{const t=[];for(const r of await R(e,{withFileTypes:!0})){if(!r.isDirectory())continue;const n=y(e,r.name,"documents.jsonl");(await _(n).catch(()=>{}))?.isFile()&&t.push({file:n,table:r.name})}return t},gt=e=>{const t=[];for(const r of e.getEntries()){if(r.isDirectory)continue;const n=r.entryName.replaceAll("\\","/"),s=n.split("/");s.length>=2&&s[s.length-1]==="documents.jsonl"&&t.push({file:n,table:s[s.length-2]})}return t},pt=async e=>{const t=e.kind==="directory"?await ht(e.root):gt(e.zip);return t.length>0?t.toSorted((r,n)=>r.table.localeCompare(n.table)):void 0},F=async function*(e,t){if(e.kind==="directory"){for await(const o of x({crlfDelay:Number.POSITIVE_INFINITY,input:v(t.file,{encoding:"utf8"})}))yield o;return}const r=e.zip.getEntry(t.file);if(r===null)throw new Error(`missing ${t.file} in archive`);const n=await lt(e.zipPath,r);if(n===void 0)return;const s=x({crlfDelay:Number.POSITIVE_INFINITY,input:n});try{for await(const o of s)yield o}finally{s.close(),n.destroy()}},mt=async(e,t)=>{if(e.kind==="directory"){const n=await Q(y(e.root,"_storage")),s=await Q(k(n,t)).catch(()=>{});if(s===void 0||!fe(n,s))throw new Error(`blob ${t} resolves outside the snapshot's _storage directory`);return $(s)}const r=e.zip.readFile(`${e.storagePrefix}/${t}`);if(r===null)throw new Error(`missing blob ${t} in archive`);return Buffer.from(r)},yt=e=>{const t={conflicts:0,errors:[],inserted:{},received:0,warnings:[]};let r=[],n=0;const s=a=>{for(const[c,l]of Object.entries(a.inserted??{}))t.inserted[c]=(t.inserted[c]??0)+l;t.errors.push(...a.errors??[]),t.conflicts+=a.conflicts??0,t.received+=a.received??0;for(const c of a.warnings??[])t.warnings.includes(c)||t.warnings.push(c)},o=async()=>{if(r.length===0)return;const a=r.join(`
2
- `);r=[],n=0;const c=await e.fetchImpl(e.requestUrl,{body:a,headers:{authorization:`Bearer ${e.token}`,"content-type":"application/x-ndjson"},method:"POST"});if(!c.ok){const l=await c.text().catch(()=>"<no body>");throw new f("INTERNAL",`import batch failed (HTTP ${String(c.status)}): ${l}`)}s(await c.json())};return{flush:o,push:async a=>{const c=Buffer.byteLength(a)+1;r.length>0&&n+c>e.maxBatchBytes&&await o(),r.push(a),n+=c,r.length>=e.batchSize&&await o()},totals:t}},pe=(e,t,r,n)=>{const s=[],o=[];let i=0;const a=d=>n?.[r]?.includes(d)===!0,c=(d,u,h=!1)=>{if(Array.isArray(d))return d.map(g=>c(g,u,h));if(d!==null&&typeof d=="object"){const g=d;if(typeof g.$storage=="string"){const p=g.$storage,w=t.get(p);return w===void 0?(o.push({column:u,storageId:p,table:r}),d):(i+=1,w)}return Object.fromEntries(Object.entries(g).map(([p,w])=>[p,c(w,u)]))}return typeof d=="string"&&t.has(d)?a(u)?(i+=1,t.get(d)??d):(s.push({column:u,storageId:d,table:r}),d):(h&&typeof d=="string"&&d.length>0&&a(u)&&n!==void 0&&o.push({column:u,storageId:d,table:r}),d)},l=Object.fromEntries(Object.entries(e).map(([d,u])=>[d,c(u,d,!0)]));return{ambiguous:s,document:l,rewritten:i,unmigrated:o}},bt=e=>{const{remapDocument:t,report:r,storageColumns:n,storageIdMap:s,table:o}=e,i=(c,l)=>{let d;try{d=JSON.parse(c)}catch(u){const h=u instanceof Error?u.message:String(u);throw new f("INTERNAL",`invalid JSON on line ${String(l)}: ${h}`,{cause:u})}return JSON.stringify({doc:d,table:o})},a=(c,l)=>{let d;try{d=JSON.parse(c)}catch(u){throw new f("INTERNAL",`line ${String(l)}: import envelope is not valid JSON — ${u instanceof Error?u.message:String(u)}`,{cause:u})}if(typeof d.table!="string")throw new f("INTERNAL",`line ${String(l)}: import envelope is missing a string \`table\``);if(d.doc!==null&&typeof d.doc=="object"&&!Array.isArray(d.doc)){let u=d.doc;if(s!==void 0){const h=pe(u,s,d.table,n);u=h.document,r.rewritten+=h.rewritten,r.ambiguous.push(...h.ambiguous),r.unmigrated.push(...h.unmigrated)}d.doc=t===void 0?u:t(u,d.table)}return JSON.stringify(d)};return(c,l)=>{const d=c.trim();if(d.length!==0)return o!==void 0?i(d,l):s===void 0&&t===void 0?d:a(d,l)}},me=async(e,t,r,n)=>{const s=await n(e).catch(()=>{});if(s===void 0)throw new f("INTERNAL",`${e} is not a readable directory`);const o=new Map;for(const[l,d]of Object.entries(t?.tables??{}))d.file!==void 0&&o.set(m(d.file),l);const i=s.filter(l=>l.isFile()&&r.matches(l.name)&&!r.authFiles.has(l.name)).map(l=>({file:y(e,l.name),table:o.get(l.name)??r.tableNameOf(l.name)}));if(i.length===0)throw new f("INTERNAL",`${e} ${r.emptyMessage}`);const a=new Set(i.map(l=>m(l.file))),c=[...o].filter(([l])=>!a.has(l));if(c.length>0)throw new f("INTERNAL",`${e}: the mapping names ${String(c.length)} file(s) that are not importable from this directory — ${c.map(([l,d])=>`\`${l}\` (table \`${d}\`)`).join(", ")}. Check the name and that the file is one this source reads.`);return i.toSorted((l,d)=>l.table.localeCompare(d.table))},ye=async function*(e,t,r,n){for(const s of e){r.has(s.table)||r.set(s.table,0),t.info(`reading ${m(s.file)} → ${s.table}`);for await(const o of n(s))r.set(s.table,(r.get(s.table)??0)+1),yield`${JSON.stringify({doc:o,table:s.table})}
3
- `}},be=["timestamp-ms","timestamp-iso","json","bytea-base64","int8-string","number","boolean","text-array"],wt=e=>be.includes(e),$t=/^\\x([\dA-Fa-f]*)$/,re=/^[+-]?\d+$/,we=/[+-]\d{2}$/,$e=/(?:Z|[+-]\d{2}:\d{2})$/i,Se=/^\+/,St=/e/i,vt=/e/i,Nt=e=>{const[t="0",r="0"]=e.split(vt),n=Number(r),s=t.startsWith("-"),[o="0",i=""]=(s?t.slice(1):t).replace(Se,"").split("."),a=`${o}${i}`,c=o.length+n;let l;return c<=0?l=`0.${"0".repeat(-c)}${a}`:c>=a.length?l=`${a}${"0".repeat(c-a.length)}`:l=`${a.slice(0,c)}.${a.slice(c)}`,s?`-${l}`:l},Et=/^0+(?=\d)/,ne=e=>{const r=(St.test(e.trim())?Nt(e.trim()):e.trim()).replace(Se,""),n=r.startsWith("-"),[s="0",o=""]=(n?r.slice(1):r).split("."),i=s.replace(Et,"");let a=o.length;for(;a>0&&o[a-1]==="0";)a-=1;const c=o.slice(0,a),l=c.length>0?`${i}.${c}`:i;return n&&Number(l)!==0?`-${l}`:l},b=(e,t,r,n)=>{throw new f("INTERNAL",`column \`${e}\`: cannot reshape ${JSON.stringify(r)} as \`${t}\` — ${n}`)},se=(e,t,r)=>{const n=r.includes("T")?r:r.replace(" ","T"),s=we.test(n)?`${n}:00`:n,o=$e.test(s)?s:`${s}Z`,i=Date.parse(o);return Number.isNaN(i)&&b(e,t,r,"not a date Postgres or ISO-8601 syntax can express"),i},Tt=(e,t)=>{(!t.startsWith("{")||!t.endsWith("}"))&&b(e,"text-array",t,"not a Postgres array literal (expected `{…}`)");const r=t.slice(1,-1);if(r.length===0)return[];r.includes("{")&&b(e,"text-array",t,"nested arrays are not supported — map the column to `json` instead");const n=[];let s="",o=!1,i=!1,a=!1;const c=()=>{n.push(!i&&s==="NULL"?null:s),s="",i=!1};for(const l of r)a?(s+=l,a=!1):l==="\\"?a=!0:l==='"'?(o=!o,i=!0):l===","&&!o?c():s+=l;return c(),n},ve=(e,t,r)=>{if(r===null)return null;switch(t){case"boolean":return["1","t","TRUE","true"].includes(r)?!0:["0","f","FALSE","false"].includes(r)?!1:b(e,t,r,"not a Postgres boolean literal");case"bytea-base64":{const n=$t.exec(r);if(n===null)return b(e,t,r,"not `bytea` hex output (expected a leading `\\x`) — set `bytea_output = 'hex'` before dumping");const s=n[1];return s.length%2!==0?b(e,t,r,"has an odd number of hex digits, so the dump is truncated — re-export the column"):Buffer.from(s,"hex").toString("base64")}case"int8-string":return re.test(r)?r:b(e,t,r,"not an integer");case"json":try{return JSON.parse(r)}catch(n){return b(e,t,r,`invalid JSON — ${n instanceof Error?n.message:String(n)}`)}case"number":{const n=Number(r);if(r.trim().length===0||!Number.isFinite(n))return b(e,t,r,"not a finite number");if(re.test(r)){if(!Number.isSafeInteger(n))return b(e,t,r,"exceeds Number.MAX_SAFE_INTEGER — map this column to `int8-string` to keep it lossless")}else if(ne(String(n))!==ne(r))return b(e,t,r,"has more precision than a JS number holds — map this column to `int8-string` or `json` to keep it lossless");return n}case"text-array":return Tt(e,r);case"timestamp-iso":{se(e,t,r);const n=r.includes("T")?r:r.replace(" ","T"),s=we.test(n)?`${n}:00`:n;return $e.test(s)?s:`${s}Z`}case"timestamp-ms":return se(e,t,r);default:return r}},It="auth.",At=new Set(["confirmation_token","email_change_token_current","email_change_token_new","encrypted_password","password","password_hash","passwordhash","reauthentication_token","recovery_token","salt"]),_t=String.raw`\N`,Ne=(e,t)=>t.header?e:!t.quoting&&e.length===0||e===_t?null:e,Rt=e=>new Set([e?.auth?.file,e?.auth?.identitiesFile].filter(t=>t!==void 0).map(t=>m(t))),Ot=async(e,t)=>me(e,t,{authFiles:Rt(t),emptyMessage:"holds no .csv files — export each table with `COPY <table> TO STDOUT WITH CSV HEADER` first",matches:r=>r.toLowerCase().endsWith(".csv")&&!r.toLowerCase().startsWith(It),tableNameOf:r=>m(r,".csv")},async r=>R(r,{withFileTypes:!0})),Lt=(e,t,r)=>{const n=t?.idColumn??"id",s=t?.types??{},o={};let i=!1;for(const[a,c]of Object.entries(e)){if(At.has(a.toLowerCase()))throw new f("INTERNAL",`${r}.${a} is credential material — this looks like an auth dump being imported as a table. Name it under \`auth\` in the mapping instead; passwords are never migrated.`);const l=s[a],d=l===void 0?c:ve(a,l,c);if(a===n){if(c===null)throw new f("INTERNAL",`id column \`${n}\` is NULL — every row needs an id to preserve`);o._id=c,i=!0}else{if(a==="_id")throw new f("INTERNAL",`${r}: source column \`_id\` collides with the reserved id field. Rename it in the source, or map it via \`tables.${r}.idColumn\` if it IS the id.`);o[a]=d}}if(!i)throw new f("INTERNAL",`${r}: no \`${n}\` column to preserve as the id (columns present: ${Object.keys(e).join(", ")}). Set \`tables.${r}.idColumn\` in the mapping.`);return o},kt=async function*(e,t){const r=t?.tables?.[e.table],n=v(e.file).pipe(he({cast:Ne,columns:!0,relaxColumnCountLess:!1,skipEmptyLines:!0}));let s=0;const o=n[Symbol.asyncIterator]();for(;;){let i,a;try{if(i=await o.next(),i.done===!0)return;s+=1,a=Lt(i.value,r,e.table)}catch(c){throw new f("INTERNAL",`${m(e.file)} row ${String(s+1)}: ${c instanceof Error?c.message:String(c)}`,{cause:c})}yield a}},xt=async function*(e,t,r,n,s){yield*Ie("supabase",s,t,r,n),yield*ye(e,r,n,o=>kt(o,t))},Ct=/^\d+$/,jt=/[+-]\d{2}$/,C=e=>{if(e==null)return;if(typeof e=="number")return e;const t=Number(e);if(Number.isFinite(t)&&Ct.test(e))return t;const r=e.includes("T")?e:e.replace(" ","T"),n=Date.parse(jt.test(r)?`${r}:00`:r);return Number.isNaN(n)?void 0:n},Ee=e=>{if(typeof e=="string")try{const t=JSON.parse(e);return t!==null&&typeof t=="object"?t:void 0}catch{return}return e!==null&&typeof e=="object"?e:void 0},Pt=e=>{const t=Ee(e);if(t!==void 0){for(const r of["name","full_name","user_name","preferred_username"])if(typeof t[r]=="string"&&t[r].length>0)return t[r]}},Ut=e=>{const t=Ee(e);if(t!==void 0){for(const r of["avatar_url","picture"])if(typeof t[r]=="string"&&t[r].length>0)return t[r]}},Ft=(e,t)=>{const{id:r}=e;if(typeof r!="string"||r.length===0)throw new f("INTERNAL","auth row is missing `id` — every user needs an id to preserve");const n=e.raw_user_meta_data,s={_id:r,email:e.email??null,emailVerified:typeof e.email_confirmed_at=="string"&&e.email_confirmed_at.length>0,id:r},o=Pt(n),i=Ut(n),a=C(e.created_at),c=C(e.updated_at);return o!==void 0&&(s.name=o),i!==void 0&&(s.image=i),a!==void 0&&(s.createdAt=a),c!==void 0&&(s.updatedAt=c),{accounts:t.map(d=>{const u=typeof d.provider=="string"?d.provider:"unknown",h=typeof d.provider_id=="string"?d.provider_id:r,g=`${r}:${u}:${h}`;return{_id:g,accountId:h,id:g,providerId:u,userId:r}}),user:s}},zt=e=>{const t=e.localId;if(typeof t!="string"||t.length===0)throw new f("INTERNAL","auth row is missing `localId` — every user needs an id to preserve");const r={_id:t,email:e.email??null,emailVerified:e.emailVerified===!0,id:t},n=C(e.createdAt);return typeof e.displayName=="string"&&(r.name=e.displayName),typeof e.photoUrl=="string"&&(r.image=e.photoUrl),n!==void 0&&(r.createdAt=n),{accounts:(e.providerUserInfo??[]).filter(o=>typeof o.providerId=="string"&&o.providerId!=="password").map(o=>{const i=o.providerId,a=o.rawId??o.federatedId??t,c=`${t}:${i}:${a}`;return{_id:c,accountId:a,id:c,providerId:i,userId:t}}),user:r}},Mt=(e,t)=>{const r=new Map,n=[],s=[];for(const{accounts:o,user:i}of e){const a=typeof i.email=="string"?i.email.toLowerCase():void 0;if(a!==void 0&&a.length>0){const c=r.get(a);c===void 0?r.set(a,String(i._id)):n.push(`${a} (ids ${c} and ${String(i._id)})`)}s.push(`${JSON.stringify({doc:i,table:"user"})}
1
+ import{createReadStream as v}from"node:fs";import{open as Ve,stat as _,readdir as R,realpath as Q,readFile as $,mkdir as P,writeFile as le,appendFile as He}from"node:fs/promises";import{r as Ge}from"./admin-token-rxXZbVMZ.mjs";import{r as We}from"./admin-url-Cil3Or6N.mjs";import{join as y,resolve as k,basename as m,dirname as ue,relative as Ye,sep as Xe}from"node:path";import{createInterface as x}from"node:readline";import qe from"adm-zip";import{i as fe}from"./path-containment-CgxYZggb.mjs";import{Transform as Ke}from"node:stream";import{createInflateRaw as Ze,crc32 as Qe}from"node:zlib";import{LunoraError as f}from"@lunora/errors";import{parse as et}from"csv-parse/sync";import{parse as he}from"csv-parse";import{STORAGE_UPLOAD_MAX_BODY_BYTES as tt}from"@lunora/runtime";import{S as U,a as rt,I as nt}from"./shared-D-zCOmgY.mjs";import{createHash as st}from"node:crypto";const ot=67324752,I=30,it=26,at=28,ee=0,ct=8,dt=async(e,t)=>{const r=await Ve(e,"r");try{const n=Buffer.alloc(I),{bytesRead:s}=await r.read(n,0,I,t);if(s<I||n.readUInt32LE(0)!==ot)throw new Error(`${e}: no local file header at offset ${String(t)} — the archive is truncated or corrupt`);return t+I+n.readUInt16LE(it)+n.readUInt16LE(at)}finally{await r.close()}},te=e=>{let t=0;return new Ke({flush(r){if(t===e.header.crc){r();return}r(new Error(`${e.entryName} failed its CRC check — the archive is corrupt or truncated (expected ${String(e.header.crc)}, read ${String(t)})`))},transform(r,n,s){t=Qe(r,t),s(void 0,r)}})},lt=async(e,t)=>{const{compressedSize:r,encrypted:n,method:s,offset:o}=t.header;if(n)throw new Error(`${t.entryName} is encrypted — decrypt the archive before importing`);if(s!==ee&&s!==ct)throw new Error(`${t.entryName} uses unsupported compression method ${String(s)} — re-create the archive with standard deflate`);if(r===0){if(t.header.size>0||t.header.crc!==0)throw new Error(`${t.entryName} declares 0 compressed bytes but ${String(t.header.size)} uncompressed with CRC ${String(t.header.crc)} — the archive is corrupt`);return}const i=await dt(e,o),a=v(e,{end:i+r-1,start:i});if(s===ee){const l=te(t);return a.on("error",u=>l.destroy(u)),a.pipe(l)}const c=Ze(),d=te(t);return a.on("error",l=>c.destroy(l)),c.on("error",l=>d.destroy(l)),a.pipe(c).pipe(d)},N="_storage",ge=e=>e.startsWith("_"),ut=e=>{for(const t of e.getEntries()){const n=t.entryName.replaceAll("\\","/").split("/");if(n.length>=2&&n[n.length-2]==="_storage")return n.slice(0,-1).join("/")}return"_storage"},ft=async e=>{const t=await _(e).catch(()=>{});if(t?.isDirectory())return{kind:"directory",root:e};if(t?.isFile()&&e.toLowerCase().endsWith(".zip")){const r=new qe(e);return{kind:"zip",storagePrefix:ut(r),zip:r,zipPath:e}}},ht=async e=>{const t=[];for(const r of await R(e,{withFileTypes:!0})){if(!r.isDirectory())continue;const n=y(e,r.name,"documents.jsonl");(await _(n).catch(()=>{}))?.isFile()&&t.push({file:n,table:r.name})}return t},gt=e=>{const t=[];for(const r of e.getEntries()){if(r.isDirectory)continue;const n=r.entryName.replaceAll("\\","/"),s=n.split("/");s.length>=2&&s[s.length-1]==="documents.jsonl"&&t.push({file:n,table:s[s.length-2]})}return t},pt=async e=>{const t=e.kind==="directory"?await ht(e.root):gt(e.zip);return t.length>0?t.toSorted((r,n)=>r.table.localeCompare(n.table)):void 0},F=async function*(e,t){if(e.kind==="directory"){for await(const o of x({crlfDelay:Number.POSITIVE_INFINITY,input:v(t.file,{encoding:"utf8"})}))yield o;return}const r=e.zip.getEntry(t.file);if(r===null)throw new Error(`missing ${t.file} in archive`);const n=await lt(e.zipPath,r);if(n===void 0)return;const s=x({crlfDelay:Number.POSITIVE_INFINITY,input:n});try{for await(const o of s)yield o}finally{s.close(),n.destroy()}},mt=async(e,t)=>{if(e.kind==="directory"){const n=await Q(y(e.root,"_storage")),s=await Q(k(n,t)).catch(()=>{});if(s===void 0||!fe(n,s))throw new Error(`blob ${t} resolves outside the snapshot's _storage directory`);return $(s)}const r=e.zip.readFile(`${e.storagePrefix}/${t}`);if(r===null)throw new Error(`missing blob ${t} in archive`);return Buffer.from(r)},yt=e=>{const t={conflicts:0,errors:[],inserted:{},received:0,warnings:[]};let r=[],n=0;const s=a=>{for(const[c,d]of Object.entries(a.inserted??{}))t.inserted[c]=(t.inserted[c]??0)+d;t.errors.push(...a.errors??[]),t.conflicts+=a.conflicts??0,t.received+=a.received??0;for(const c of a.warnings??[])t.warnings.includes(c)||t.warnings.push(c)},o=async()=>{if(r.length===0)return;const a=r.join(`
2
+ `);r=[],n=0;const c=await e.fetchImpl(e.requestUrl,{body:a,headers:{authorization:`Bearer ${e.token}`,"content-type":"application/x-ndjson"},method:"POST"});if(!c.ok){const d=await c.text().catch(()=>"<no body>");throw new f("INTERNAL",`import batch failed (HTTP ${String(c.status)}): ${d}`)}s(await c.json())};return{flush:o,push:async a=>{const c=Buffer.byteLength(a)+1;r.length>0&&n+c>e.maxBatchBytes&&await o(),r.push(a),n+=c,r.length>=e.batchSize&&await o()},totals:t}},pe=(e,t,r,n)=>{const s=[],o=[];let i=0;const a=l=>n?.[r]?.includes(l)===!0,c=(l,u,h=!1)=>{if(Array.isArray(l))return l.map(g=>c(g,u,h));if(l!==null&&typeof l=="object"){const g=l;if(typeof g.$storage=="string"){const p=g.$storage,w=t.get(p);return w===void 0?(o.push({column:u,storageId:p,table:r}),l):(i+=1,w)}return Object.fromEntries(Object.entries(g).map(([p,w])=>[p,c(w,u)]))}return typeof l=="string"&&t.has(l)?a(u)?(i+=1,t.get(l)??l):(s.push({column:u,storageId:l,table:r}),l):(h&&typeof l=="string"&&l.length>0&&a(u)&&n!==void 0&&o.push({column:u,storageId:l,table:r}),l)},d=Object.fromEntries(Object.entries(e).map(([l,u])=>[l,c(u,l,!0)]));return{ambiguous:s,document:d,rewritten:i,unmigrated:o}},bt=e=>{const{remapDocument:t,report:r,storageColumns:n,storageIdMap:s,table:o}=e,i=(d,l)=>{let u;try{u=JSON.parse(d)}catch(h){const g=h instanceof Error?h.message:String(h);throw new f("INTERNAL",`invalid JSON on line ${String(l)}: ${g}`,{cause:h})}return JSON.stringify({doc:u,table:o})},a=(d,l)=>{let u;try{u=JSON.parse(d)}catch(g){throw new f("INTERNAL",`line ${String(l)}: import envelope is not valid JSON — ${g instanceof Error?g.message:String(g)}`,{cause:g})}const h=u===null||typeof u!="object"||Array.isArray(u)?void 0:u;if(h===void 0||typeof h.table!="string")throw new f("INTERNAL",`line ${String(l)}: import envelope is missing a string \`table\``);return h},c=d=>{if(d.doc!==null&&typeof d.doc=="object"&&!Array.isArray(d.doc)){let l=d.doc;if(s!==void 0){const u=pe(l,s,d.table,n);l=u.document,r.rewritten+=u.rewritten,r.ambiguous.push(...u.ambiguous),r.unmigrated.push(...u.unmigrated)}return JSON.stringify({...d,doc:t===void 0?l:t(l,d.table)})}return JSON.stringify(d)};return(d,l)=>{const u=d.trim();if(u.length===0)return;if(o!==void 0)return i(u,l);const h=a(u,l);return s===void 0&&t===void 0?u:c(h)}},me=async(e,t,r,n)=>{const s=await n(e).catch(()=>{});if(s===void 0)throw new f("INTERNAL",`${e} is not a readable directory`);const o=new Map;for(const[d,l]of Object.entries(t?.tables??{}))l.file!==void 0&&o.set(m(l.file),d);const i=s.filter(d=>d.isFile()&&r.matches(d.name)&&!r.authFiles.has(d.name)).map(d=>({file:y(e,d.name),table:o.get(d.name)??r.tableNameOf(d.name)}));if(i.length===0)throw new f("INTERNAL",`${e} ${r.emptyMessage}`);const a=new Set(i.map(d=>m(d.file))),c=[...o].filter(([d])=>!a.has(d));if(c.length>0)throw new f("INTERNAL",`${e}: the mapping names ${String(c.length)} file(s) that are not importable from this directory — ${c.map(([d,l])=>`\`${d}\` (table \`${l}\`)`).join(", ")}. Check the name and that the file is one this source reads.`);return i.toSorted((d,l)=>d.table.localeCompare(l.table))},ye=async function*(e,t,r,n){for(const s of e){r.has(s.table)||r.set(s.table,0),t.info(`reading ${m(s.file)} → ${s.table}`);for await(const o of n(s))r.set(s.table,(r.get(s.table)??0)+1),yield`${JSON.stringify({doc:o,table:s.table})}
3
+ `}},be=["timestamp-ms","timestamp-iso","json","bytea-base64","int8-string","number","boolean","text-array"],wt=e=>be.includes(e),$t=/^\\x([\dA-Fa-f]*)$/,re=/^[+-]?\d+$/,we=/[+-]\d{2}$/,$e=/(?:Z|[+-]\d{2}:\d{2})$/i,Se=/^\+/,St=/e/i,vt=/e/i,Nt=e=>{const[t="0",r="0"]=e.split(vt),n=Number(r),s=t.startsWith("-"),[o="0",i=""]=(s?t.slice(1):t).replace(Se,"").split("."),a=`${o}${i}`,c=o.length+n;let d;return c<=0?d=`0.${"0".repeat(-c)}${a}`:c>=a.length?d=`${a}${"0".repeat(c-a.length)}`:d=`${a.slice(0,c)}.${a.slice(c)}`,s?`-${d}`:d},Et=/^0+(?=\d)/,ne=e=>{const r=(St.test(e.trim())?Nt(e.trim()):e.trim()).replace(Se,""),n=r.startsWith("-"),[s="0",o=""]=(n?r.slice(1):r).split("."),i=s.replace(Et,"");let a=o.length;for(;a>0&&o[a-1]==="0";)a-=1;const c=o.slice(0,a),d=c.length>0?`${i}.${c}`:i;return n&&Number(d)!==0?`-${d}`:d},b=(e,t,r,n)=>{throw new f("INTERNAL",`column \`${e}\`: cannot reshape ${JSON.stringify(r)} as \`${t}\` — ${n}`)},se=(e,t,r)=>{const n=r.includes("T")?r:r.replace(" ","T"),s=we.test(n)?`${n}:00`:n,o=$e.test(s)?s:`${s}Z`,i=Date.parse(o);return Number.isNaN(i)&&b(e,t,r,"not a date Postgres or ISO-8601 syntax can express"),i},Tt=(e,t)=>{(!t.startsWith("{")||!t.endsWith("}"))&&b(e,"text-array",t,"not a Postgres array literal (expected `{…}`)");const r=t.slice(1,-1);if(r.length===0)return[];r.includes("{")&&b(e,"text-array",t,"nested arrays are not supported — map the column to `json` instead");const n=[];let s="",o=!1,i=!1,a=!1;const c=()=>{n.push(!i&&s==="NULL"?null:s),s="",i=!1};for(const d of r)a?(s+=d,a=!1):d==="\\"?a=!0:d==='"'?(o=!o,i=!0):d===","&&!o?c():s+=d;return c(),n},ve=(e,t,r)=>{if(r===null)return null;switch(t){case"boolean":return["1","t","TRUE","true"].includes(r)?!0:["0","f","FALSE","false"].includes(r)?!1:b(e,t,r,"not a Postgres boolean literal");case"bytea-base64":{const n=$t.exec(r);if(n===null)return b(e,t,r,"not `bytea` hex output (expected a leading `\\x`) — set `bytea_output = 'hex'` before dumping");const s=n[1];return s.length%2!==0?b(e,t,r,"has an odd number of hex digits, so the dump is truncated — re-export the column"):Buffer.from(s,"hex").toString("base64")}case"int8-string":return re.test(r)?r:b(e,t,r,"not an integer");case"json":try{return JSON.parse(r)}catch(n){return b(e,t,r,`invalid JSON — ${n instanceof Error?n.message:String(n)}`)}case"number":{const n=Number(r);if(r.trim().length===0||!Number.isFinite(n))return b(e,t,r,"not a finite number");if(re.test(r)){if(!Number.isSafeInteger(n))return b(e,t,r,"exceeds Number.MAX_SAFE_INTEGER — map this column to `int8-string` to keep it lossless")}else if(ne(String(n))!==ne(r))return b(e,t,r,"has more precision than a JS number holds — map this column to `int8-string` or `json` to keep it lossless");return n}case"text-array":return Tt(e,r);case"timestamp-iso":{se(e,t,r);const n=r.includes("T")?r:r.replace(" ","T"),s=we.test(n)?`${n}:00`:n;return $e.test(s)?s:`${s}Z`}case"timestamp-ms":return se(e,t,r);default:return r}},It="auth.",At=new Set(["confirmation_token","email_change_token_current","email_change_token_new","encrypted_password","password","password_hash","passwordhash","reauthentication_token","recovery_token","salt"]),_t=String.raw`\N`,Ne=(e,t)=>t.header?e:!t.quoting&&e.length===0||e===_t?null:e,Rt=e=>new Set([e?.auth?.file,e?.auth?.identitiesFile].filter(t=>t!==void 0).map(t=>m(t))),Ot=async(e,t)=>me(e,t,{authFiles:Rt(t),emptyMessage:"holds no .csv files — export each table with `COPY <table> TO STDOUT WITH CSV HEADER` first",matches:r=>r.toLowerCase().endsWith(".csv")&&!r.toLowerCase().startsWith(It),tableNameOf:r=>m(r,".csv")},async r=>R(r,{withFileTypes:!0})),Lt=(e,t,r)=>{const n=t?.idColumn??"id",s=t?.types??{},o={};let i=!1;for(const[a,c]of Object.entries(e)){if(At.has(a.toLowerCase()))throw new f("INTERNAL",`${r}.${a} is credential material — this looks like an auth dump being imported as a table. Name it under \`auth\` in the mapping instead; passwords are never migrated.`);const d=s[a],l=d===void 0?c:ve(a,d,c);if(a===n){if(c===null)throw new f("INTERNAL",`id column \`${n}\` is NULL — every row needs an id to preserve`);o._id=c,i=!0}else{if(a==="_id")throw new f("INTERNAL",`${r}: source column \`_id\` collides with the reserved id field. Rename it in the source, or map it via \`tables.${r}.idColumn\` if it IS the id.`);o[a]=l}}if(!i)throw new f("INTERNAL",`${r}: no \`${n}\` column to preserve as the id (columns present: ${Object.keys(e).join(", ")}). Set \`tables.${r}.idColumn\` in the mapping.`);return o},kt=async function*(e,t){const r=t?.tables?.[e.table],n=v(e.file).pipe(he({cast:Ne,columns:!0,relaxColumnCountLess:!1,skipEmptyLines:!0}));let s=0;const o=n[Symbol.asyncIterator]();for(;;){let i,a;try{if(i=await o.next(),i.done===!0)return;s+=1,a=Lt(i.value,r,e.table)}catch(c){throw new f("INTERNAL",`${m(e.file)} row ${String(s+1)}: ${c instanceof Error?c.message:String(c)}`,{cause:c})}yield a}},xt=async function*(e,t,r,n,s){yield*Ie("supabase",s,t,r,n),yield*ye(e,r,n,o=>kt(o,t))},Ct=/^\d+$/,jt=/[+-]\d{2}$/,C=e=>{if(e==null)return;if(typeof e=="number")return e;const t=Number(e);if(Number.isFinite(t)&&Ct.test(e))return t;const r=e.includes("T")?e:e.replace(" ","T"),n=Date.parse(jt.test(r)?`${r}:00`:r);return Number.isNaN(n)?void 0:n},Ee=e=>{if(typeof e=="string")try{const t=JSON.parse(e);return t!==null&&typeof t=="object"?t:void 0}catch{return}return e!==null&&typeof e=="object"?e:void 0},Pt=e=>{const t=Ee(e);if(t!==void 0){for(const r of["name","full_name","user_name","preferred_username"])if(typeof t[r]=="string"&&t[r].length>0)return t[r]}},Ut=e=>{const t=Ee(e);if(t!==void 0){for(const r of["avatar_url","picture"])if(typeof t[r]=="string"&&t[r].length>0)return t[r]}},Ft=(e,t)=>{const{id:r}=e;if(typeof r!="string"||r.length===0)throw new f("INTERNAL","auth row is missing `id` — every user needs an id to preserve");const n=e.raw_user_meta_data,s={_id:r,email:e.email??null,emailVerified:typeof e.email_confirmed_at=="string"&&e.email_confirmed_at.length>0,id:r},o=Pt(n),i=Ut(n),a=C(e.created_at),c=C(e.updated_at);return o!==void 0&&(s.name=o),i!==void 0&&(s.image=i),a!==void 0&&(s.createdAt=a),c!==void 0&&(s.updatedAt=c),{accounts:t.map(l=>{const u=typeof l.provider=="string"?l.provider:"unknown",h=typeof l.provider_id=="string"?l.provider_id:r,g=`${r}:${u}:${h}`;return{_id:g,accountId:h,id:g,providerId:u,userId:r}}),user:s}},zt=e=>{const t=e.localId;if(typeof t!="string"||t.length===0)throw new f("INTERNAL","auth row is missing `localId` — every user needs an id to preserve");const r={_id:t,email:e.email??null,emailVerified:e.emailVerified===!0,id:t},n=C(e.createdAt);return typeof e.displayName=="string"&&(r.name=e.displayName),typeof e.photoUrl=="string"&&(r.image=e.photoUrl),n!==void 0&&(r.createdAt=n),{accounts:(e.providerUserInfo??[]).filter(o=>typeof o.providerId=="string"&&o.providerId!=="password").map(o=>{const i=o.providerId,a=o.rawId??o.federatedId??t,c=`${t}:${i}:${a}`;return{_id:c,accountId:a,id:c,providerId:i,userId:t}}),user:r}},Mt=(e,t)=>{const r=new Map,n=[],s=[];for(const{accounts:o,user:i}of e){const a=typeof i.email=="string"?i.email.toLowerCase():void 0;if(a!==void 0&&a.length>0){const c=r.get(a);c===void 0?r.set(a,String(i._id)):n.push(`${a} (ids ${c} and ${String(i._id)})`)}s.push(`${JSON.stringify({doc:i,table:"user"})}
4
4
  `),t.set("user",(t.get("user")??0)+1);for(const c of o)s.push(`${JSON.stringify({doc:c,table:"account"})}
5
- `),t.set("account",(t.get("account")??0)+1)}if(n.length>0)throw new f("INTERNAL",`auth import found ${String(n.length)} duplicate email(s), which would merge distinct users: ${n.slice(0,10).join("; ")}${n.length>10?" …":""}`);return s},z=(e,t)=>y(e,m(t)),Te=async e=>{const t=await $(e,"utf8");return et(t,{cast:Ne,columns:!0,skipEmptyLines:!0})},Dt=async(e,t)=>{const r=new Map;if(t===void 0)return r;for(const n of await Te(z(e,t))){const s=n.user_id;typeof s=="string"&&r.set(s,[...r.get(s)??[],n])}return r},Bt=async(e,t)=>{const r=t.auth?.file;if(r===void 0)return[];const n=await Te(z(e,r)),s=await Dt(e,t.auth?.identitiesFile);return n.map(o=>Ft(o,s.get(o.id??"")??[]))},Jt=async(e,t)=>{const r=t.auth?.file;if(r===void 0)return[];const n=z(e,r);let s;try{s=JSON.parse(await $(n,"utf8"))}catch(i){throw new f("INTERNAL",`${m(n)}: invalid JSON — ${i instanceof Error?i.message:String(i)}`,{cause:i})}const o=Array.isArray(s)?s:s.users??[];if(!Array.isArray(o))throw new f("INTERNAL",`${m(n)}: expected \`{ users: [...] }\` from \`firebase auth:export\`, or a bare array`);return o.map(i=>zt(i))},Ie=async function*(e,t,r,n,s){if(r?.auth?.file===void 0)return;const o=e==="supabase"?await Bt(t,r):await Jt(t,r);n.info(`auth: ${String(o.length)} user(s) — passwords are never migrated; users reset via "forgot password"`);for(const i of Mt(o,s))yield i},oe=/\.(?:nd)?json$/i,Ae=e=>{const t=e.split("/").filter(r=>r.length>0);return t[t.length-1]??e},Vt=e=>{const t=Number(e.seconds??0),r=e.nanos??0;return Number.isFinite(t)&&Number.isFinite(r)?t*1e3+Math.floor(r/1e6):Number.NaN},Ht=(e,t)=>{const r=Array.isArray(e)?e:e.data;if(!Array.isArray(r))throw new f("INTERNAL",`${t}: \`bytesValue\` ${JSON.stringify(e)} is neither base64 nor a byte array`);return Buffer.from(r).toString("base64")},Gt=(e,t)=>{const r=typeof e=="string"?Date.parse(e):Vt(e);if(Number.isNaN(r))throw new f("INTERNAL",`${t}: \`timestampValue\` ${JSON.stringify(e)} is neither an RFC-3339 string nor a \`{ seconds, nanos }\` protobuf timestamp`);return r},Wt=(e,t)=>typeof e=="string"?e:Ht(e,t),_e=(e,t)=>{if("nullValue"in e)return null;if(e.stringValue!==void 0)return e.stringValue;if(e.booleanValue!==void 0)return e.booleanValue;if(e.integerValue!==void 0){const r=String(e.integerValue),n=Number(r);return Number.isSafeInteger(n)?n:r}if(e.doubleValue!==void 0)return Number(e.doubleValue);if(e.timestampValue!==void 0)return Gt(e.timestampValue,t);if(e.bytesValue!==void 0)return Wt(e.bytesValue,t);if(e.geoPointValue!==void 0)return{latitude:e.geoPointValue.latitude??0,longitude:e.geoPointValue.longitude??0};if(e.referenceValue!==void 0)return Ae(e.referenceValue);if(e.arrayValue!==void 0)return(e.arrayValue.values??[]).map((r,n)=>_e(r,`${t}[${String(n)}]`));if(e.mapValue!==void 0)return j(e.mapValue.fields??{},t);throw new f("INTERNAL",`${t}: unrecognised Firestore value ${JSON.stringify(e).slice(0,80)}`)},Yt=new Set(["createTime","fields","name","readTime","updateTime"]),Xt=e=>{if(typeof e!="object"||e===null||Array.isArray(e))return!1;const t=Object.keys(e);return t.length===1&&t[0]?.endsWith("Value")===!0},qt=e=>{if(typeof e.name=="string")return!0;const{fields:t}=e;return typeof t!="object"||t===null?!1:Object.keys(e).every(r=>Yt.has(r))&&!Xt(t)},j=(e,t)=>Object.fromEntries(Object.entries(e).map(([r,n])=>[r,_e(n,`${t}.${r}`)])),Kt=(e,t,r,n)=>{const s=qt(e),o=j(s?e.fields??{}:e,n),i=e.name??e.__name__,a=typeof i=="string"?Ae(i):t;if(a===void 0)throw new f("INTERNAL",`${n}: no document id — expected a \`name\`/\`__name__\` resource path, or a document keyed by its id`);const c=r?.types??{};for(const[l,d]of Object.entries(c)){const u=o[l];if(u!==void 0){if(u!==null&&typeof u!="boolean"&&typeof u!="number"&&typeof u!="string")throw new f("INTERNAL",`${n}.${l}: a \`${d}\` reshape needs a scalar, but this field decoded to an object or array`);o[l]=ve(l,d,u===null?null:String(u))}}return{...o,_id:a}},Zt=async(e,t)=>me(e,t,{authFiles:new Set(t?.auth?.file===void 0?[]:[m(t.auth.file)]),emptyMessage:"holds no .json/.ndjson collection files",matches:r=>oe.test(r),tableNameOf:r=>r.replace(oe,"")},async r=>R(r,{withFileTypes:!0})),Qt=async function*(e){const t=x({crlfDelay:Number.POSITIVE_INFINITY,input:v(e,"utf8")});let r=0;try{for await(const n of t){r+=1;const s=n.trim();if(s.length!==0)try{yield{raw:JSON.parse(s)}}catch(o){throw new f("INTERNAL",`${m(e)} line ${String(r)}: invalid JSON — ${o instanceof Error?o.message:String(o)}`,{cause:o})}}}finally{t.close()}},er=async e=>{const t=await $(e,"utf8");let r;try{r=JSON.parse(t)}catch(n){throw new f("INTERNAL",`${m(e)}: invalid JSON — ${n instanceof Error?n.message:String(n)}`,{cause:n})}if(r!==null&&typeof r=="object"&&Array.isArray(r.documents))return r.documents.map(n=>({raw:n}));if(Array.isArray(r))return r.map(n=>({raw:n}));if(r!==null&&typeof r=="object")return Object.entries(r).map(([n,s])=>({fallbackId:n,raw:s}));throw new f("INTERNAL",`${m(e)}: expected an object, an array, or \`{ documents: [...] }\``)},tr=async function*(e,t){const r=t?.tables?.[e.table],n=e.file.toLowerCase().endsWith(".ndjson")?Qt(e.file):await er(e.file);let s=0;for await(const o of n)yield Kt(o.raw,o.fallbackId,r,`${e.table}[${String(s)}]`),s+=1},rr=async function*(e,t,r,n,s){yield*Ie("firebase",s,t,r,n),yield*ye(e,r,n,o=>tr(o,t))},T=e=>e!==null&&typeof e=="object"&&!Array.isArray(e),M=(e,t)=>{if(!T(e))throw new f("INTERNAL",`${t}: expected a JSON object`);return e},E=(e,t,r)=>{const n=e[t];if(n!==void 0&&typeof n!="string")throw new f("INTERNAL",`${r}: \`${t}\` must be a string`);return n},Re=(e,t,r)=>{const n=e[t];if(n!==void 0&&(!Array.isArray(n)||n.some(s=>typeof s!="string")))throw new f("INTERNAL",`${r}: \`${t}\` must be an array of column names`);return n},nr=(e,t)=>{const r=M(e,t),n=E(r,"file",t),s=E(r,"idColumn",t),o=Re(r,"storageColumns",t),{types:i}=r;if(i!==void 0){if(!T(i))throw new f("INTERNAL",`${t}.types must be an object of column → reshape`);for(const[a,c]of Object.entries(i))if(!wt(c))throw new f("INTERNAL",`${t}.types.${a}: unknown reshape ${JSON.stringify(c)} — expected one of ${be.join(", ")}`)}return{file:n,idColumn:s,storageColumns:o,types:i}},sr=(e,t)=>{if(e!==void 0){if(!T(e))throw new f("INTERNAL",`${t}: \`auth\` must be an object`);for(const r of["file","identitiesFile"])E(e,r,`${t}: auth`)}},or=(e,t)=>{if(e!==void 0){if(!T(e))throw new f("INTERNAL",`${t}: \`tables\` must be an object of table → mapping`);return Object.fromEntries(Object.entries(e).map(([r,n])=>[r,nr(n,`${t}: tables.${r}`)]))}},ir=(e,t)=>{const r=M(e,t),n=E(r,"keyPrefix",t),{auth:s,tables:o}=r;return sr(s,t),{auth:s,keyPrefix:n,tables:or(o,t)}},Oe=e=>y("lunora",`import-${e}.json`),ar=async(e,t,r)=>{const n=Oe(t),s=y(e,n);let o;try{o=await $(s,"utf8")}catch(a){if(a.code==="ENOENT"){r.info(`no ${n} found — every column is copied through untouched (run with --scan to generate one)`);return}throw a}let i;try{i=JSON.parse(o)}catch(a){throw new f("INTERNAL",`${s}: invalid JSON — ${a instanceof Error?a.message:String(a)}`,{cause:a})}return ir(i,s)},cr=8,dr=24*1048576,lr=1e3,ur=/^[\dA-F]{64}$/i,fr=e=>{for(let t=0;t<e.length;t+=1){const r=e.codePointAt(t)??0;if(r<32||r===127)return!1}return!0},hr=/^[\d+/A-Z]{43}=$/i,gr=e=>ur.test(e)?e.toLowerCase():hr.test(e)?Buffer.from(e,"base64").toString("hex"):void 0,pr=(e,t)=>{const r=JSON.parse(e),n=r._id;if(typeof n!="string"||n.length===0||n.includes("/")||n.includes("\\"))throw new f("INTERNAL",`${t}: \`_id\` must be a path-free non-empty string`);if(typeof r.sha256!="string")throw new f("INTERNAL",`${t}: \`sha256\` is missing — re-export with \`npx convex export --include-file-storage\``);const s=gr(r.sha256);if(s===void 0)throw new f("INTERNAL",`${t}: \`sha256\` is neither base16 nor base64 SHA-256 (${r.sha256})`);if(typeof r.size!="number"||!Number.isInteger(r.size)||r.size<0)throw new f("INTERNAL",`${t}: \`size\` must be a non-negative integer`);if(r.contentType!==void 0&&(typeof r.contentType!="string"||!fr(r.contentType)))throw new f("INTERNAL",`${t}: \`contentType\` must be a string with no control characters`);return{contentType:typeof r.contentType=="string"?r.contentType:void 0,id:n,sha256:s,size:r.size}},D=async(e,t,r)=>{const n=[];try{let s=0;for await(const o of F(e,t)){const i=o.trim();s+=1,i.length>0&&n.push(pr(i,`_storage/documents.jsonl line ${String(s)}`))}}catch(s){const o=s instanceof Error?s.message:String(s);throw r.error(`failed to read _storage metadata: ${o}`),s}return n},Le=tt,O=e=>e.bucket===void 0?"":`&bucket=${encodeURIComponent(e.bucket)}`,B=async(e,t)=>{const r=[];let n;do{const s=`${e.baseUrl}${U}?prefix=${encodeURIComponent(t)}&limit=${String(lr)}${n===void 0?"":`&cursor=${encodeURIComponent(n)}`}${O(e)}`,o=await e.fetchImpl(s,{headers:{authorization:`Bearer ${e.token}`},method:"GET"});if(!o.ok){const c=await o.text().catch(()=>"<no body>");throw new f("INTERNAL",`storage list failed (HTTP ${String(o.status)}): ${c}`)}const i=await o.json();r.push(...i.objects??[]);const a=i.truncated===!0?i.cursor:void 0;if(a!==void 0&&a===n)throw new f("INTERNAL","storage list did not advance its cursor — refusing to page forever");n=a}while(n!==void 0);return r},mr=async(e,t,r,n)=>{const s=`${e.baseUrl}${U}?key=${encodeURIComponent(t)}&expectedSha256=${n.sha256}&expectedSize=${String(n.size)}${O(e)}`,o=await e.fetchImpl(s,{body:new Uint8Array(r),headers:{authorization:`Bearer ${e.token}`,"content-type":n.contentType??"application/octet-stream"},method:"PUT"});if(!o.ok){const a=await o.text().catch(()=>"<no body>");throw new f("INTERNAL",`blob upload failed (HTTP ${String(o.status)}): ${a}`)}const i=await o.json();if(i.sha256!==n.sha256)throw new f("INTERNAL",`blob upload verification failed: expected ${n.sha256}, got ${i.sha256??"none"}`);return t},yr=async(e,t)=>(await e.fetchImpl(`${e.baseUrl}${U}?key=${encodeURIComponent(t)}${O(e)}`,{headers:{authorization:`Bearer ${e.token}`},method:"DELETE"}).catch(()=>{}))?.ok===!0,br=async(e,t,r,n,s)=>{const o=`${e.baseUrl}${rt}?key=${encodeURIComponent(t)}&method=PUT&contentType=${encodeURIComponent(n.contentType??"application/octet-stream")}${O(e)}`,i=await e.fetchImpl(o,{headers:{authorization:`Bearer ${e.token}`},method:"GET"});if(!i.ok){const p=await i.text().catch(()=>"<no body>");throw new f("INTERNAL",`blob ${t} is ${String(n.size)} bytes, above the ${String(Le)}-byte verified-upload cap, and no signed PUT URL could be minted (HTTP ${String(i.status)}): ${p}`)}const{url:a}=await i.json(),c=await e.fetchImpl(a,{body:new Uint8Array(r),headers:{"content-type":n.contentType??"application/octet-stream"},method:"PUT"});if(!c.ok){const p=await c.text().catch(()=>"<no body>");throw new f("INTERNAL",`signed PUT failed (HTTP ${String(c.status)}): ${p}`)}const d=(await B(e,t)).find(p=>p.key===t);if(d===void 0)throw new f("INTERNAL",`post-upload verification failed: blob not found at key ${t}`);const u=d.size!==void 0&&d.size!==n.size,h=d.sha256!==void 0&&d.sha256.toLowerCase()!==n.sha256;if(u||h){const p=await yr(e,t);throw new f("INTERNAL",`post-upload verification failed: expected sha256=${n.sha256} size=${String(n.size)}, got sha256=${d.sha256??"none"} size=${String(d.size??"none")}${p?" (the object was removed)":` — AND the object could not be removed: delete ${t} by hand before re-running, or the next run will treat it as already migrated`}`)}const g=[d.size===void 0?"size":void 0,d.sha256===void 0?"sha256":void 0].filter(Boolean);return g.length>0&&s.warn(`blob ${t} went through the signed-PUT path and the host reports no ${g.join(" or ")} for it — that much of the write is unverified`),t},ke=async(e,t,r,n,s)=>{if(r.length!==n.size)throw new f("INTERNAL",`blob ${n.id} is ${String(r.length)} bytes on disk but the export declares ${String(n.size)}`);return r.length<=Le?mr(e,t,r,n):br(e,t,r,n,s)},wr=e=>{const t=[];let r=[],n=0;for(const s of e)r.length>0&&(r.length>=cr||n+s.size>dr)&&(t.push(r),r=[],n=0),r.push(s),n+=s.size;return r.length>0&&t.push(r),t},$r=async(e,t,r,n,s)=>{const o=await D(t,r,s),i=new Map,a=await B(e,n),c=new Map(a.map(u=>[u.key,u])),l=[];for(const u of o){const h=`${n}${u.sha256}`;c.get(h)?.size===u.size?i.set(u.id,h):l.push(u)}s.info(`migrating ${String(l.length)} storage blobs${i.size>0?` (${String(i.size)} already present)`:""}...`);const d=async u=>{try{const h=await mt(t,u.id);i.set(u.id,await ke(e,`${n}${u.sha256}`,h,u,s))}catch(h){const g=h instanceof Error?h.message:String(h);throw s.error(`failed to upload blob ${u.id}: ${g}`),h}};for(const u of wr(l)){const g=(await Promise.allSettled(u.map(p=>d(p)))).find(p=>p.status==="rejected");if(g!==void 0)throw g.reason}return s.success(`migrated ${String(l.length)} storage blobs`),i};async function*Sr(e,t,r){let n=0;for await(const s of F(e,t)){const o=s.trim();if(n+=1,o.length===0)continue;let i;try{i=JSON.parse(o)}catch(a){throw new f("INTERNAL",`${t.table}/documents.jsonl line ${String(n)}: invalid JSON — ${a instanceof Error?a.message:String(a)}`,{cause:a})}r.set(t.table,(r.get(t.table)??0)+1),yield`${JSON.stringify({doc:i,table:t.table})}
6
- `}}async function*vr(e,t,r,n,s){for(const o of t){if(ge(o.table)){o.table===N&&!n&&r.warn(`skipping "${N}" — those rows describe stored files, and their blobs were not migrated. Re-run with --with-storage to upload them and rewrite the references.`);continue}s.has(o.table)||s.set(o.table,0),yield*Sr(e,o,s)}}const Un=["firebase","supabase"],Nr=async(e,t,r)=>{if(e.withStorage===!0&&t==="firebase"&&e.storageDir===void 0)return e.logger.error("--with-storage on Firebase needs --storage-dir — download the bucket first with `gcloud storage cp -r gs://<bucket> <dir>`."),{kind:"invalid"};if(e.table!==void 0)return e.logger.error(`--table cannot be combined with --from ${t} — each row's table comes from its source file.`),{kind:"invalid"};const n=await ar(r,t,e.logger),s=Object.values(n?.tables??{}).some(o=>(o.storageColumns??[]).length>0);return e.verify===!0&&e.withStorage!==!0&&s?(e.logger.error("--verify with `storageColumns` declared requires --with-storage — otherwise every storage path stays unmigrated and only row counts would be checked."),{kind:"invalid"}):t==="supabase"?{kind:"supabase",mapping:n,tables:await Ot(e.file,n)}:{collections:await Zt(e.file,n),kind:"firebase",mapping:n}},Er=async e=>{if(!await _(e.file).then(()=>!0,()=>!1))return!1;for(const[r,n]of[["--scan",e.scan],["--verify",e.verify],["--with-storage",e.withStorage]])if(n===!0)return e.logger.error(`${r} requires a Convex export directory or .zip snapshot — ${e.file} is not one.`),!0;return!1},Tr=async(e,t,r)=>{const n=t.find(o=>o.table===N);if(n===void 0)return r.logger.error("--verify cannot check file references: this export has no `_storage` table, so it was taken without `--include-file-storage`. Re-export with that flag and pass --with-storage, or drop --verify."),!0;const s=await D(e,n,r.logger);return s.length>0?(r.logger.error(`--verify on an export carrying ${String(s.length)} stored file(s) requires --with-storage — otherwise every file reference stays unmigrated and only row counts would be checked.`),!0):!1},Ir=async(e,t)=>{if(e.from==="supabase"||e.from==="firebase")return Nr(e,e.from,t);const r=await ft(e.file),n=r===void 0?void 0:await pt(r);return r!==void 0&&n===void 0?(e.logger.error(`${e.file} is a ${r.kind==="zip"?".zip":"directory"} but holds no <table>/documents.jsonl — expected a \`npx convex export --path\` snapshot, or pass an NDJSON file.`),{kind:"invalid"}):r===void 0||n===void 0?await Er(e)?{kind:"invalid"}:{kind:"ndjson"}:e.table!==void 0?(e.logger.error("--table cannot be combined with a Convex export directory — each row's table comes from its source directory."),{kind:"invalid"}):e.verify===!0&&e.withStorage!==!0&&await Tr(r,n,e)?{kind:"invalid"}:{kind:"convex",snapshot:r,tables:n}},A=y("lunora","import-convex.json"),Ar=(e,t)=>{const r=M(e,t),n=E(r,"keyPrefix",t),s=r.storageColumns;if(s!==void 0){if(!T(s))throw new f("INTERNAL",`${t}: \`storageColumns\` must be an object of table → column names`);for(const o of Object.keys(s))Re(s,o,`${t}: storageColumns`)}return{keyPrefix:n,storageColumns:s}},_r=async(e,t)=>{const r=y(e,A);let n;try{n=await $(r,"utf8")}catch(o){if(o.code==="ENOENT"){t.info(`no ${A} found — rewriting only self-describing { $storage } refs (run with --scan to generate one)`);return}throw o}let s;try{s=JSON.parse(n)}catch(o){throw new f("INTERNAL",`${r}: invalid JSON — ${o instanceof Error?o.message:String(o)}`,{cause:o})}return Ar(s,r)},Rr=(e,t,r)=>{try{return JSON.parse(e)}catch(n){throw new f("INTERNAL",`${t}/documents.jsonl line ${String(r)}: invalid JSON — ${n instanceof Error?n.message:String(n)}`,{cause:n})}},Or=async(e,t,r)=>{const n=new Map([...r].map(i=>[i,i])),s=[];let o=0;for await(const i of F(e,t)){const a=i.trim();if(o+=1,a.length===0)continue;const{ambiguous:c}=pe(Rr(a,t.table,o),n,t.table);for(const{column:l}of c)s.includes(l)||s.push(l)}return s},Lr=async(e,t,r)=>{const n={};for(const s of t){if(ge(s.table))continue;const o=await Or(e,s,r);o.length>0&&(n[s.table]=o)}return n},kr=async(e,t,r)=>{const n=y(t,A),s=`${JSON.stringify(e,void 0,4)}
5
+ `),t.set("account",(t.get("account")??0)+1)}if(n.length>0)throw new f("INTERNAL",`auth import found ${String(n.length)} duplicate email(s), which would merge distinct users: ${n.slice(0,10).join("; ")}${n.length>10?" …":""}`);return s},z=(e,t)=>y(e,m(t)),Te=async e=>{const t=await $(e,"utf8");return et(t,{cast:Ne,columns:!0,skipEmptyLines:!0})},Dt=async(e,t)=>{const r=new Map;if(t===void 0)return r;for(const n of await Te(z(e,t))){const s=n.user_id;typeof s=="string"&&r.set(s,[...r.get(s)??[],n])}return r},Bt=async(e,t)=>{const r=t.auth?.file;if(r===void 0)return[];const n=await Te(z(e,r)),s=await Dt(e,t.auth?.identitiesFile);return n.map(o=>Ft(o,s.get(o.id??"")??[]))},Jt=async(e,t)=>{const r=t.auth?.file;if(r===void 0)return[];const n=z(e,r);let s;try{s=JSON.parse(await $(n,"utf8"))}catch(i){throw new f("INTERNAL",`${m(n)}: invalid JSON — ${i instanceof Error?i.message:String(i)}`,{cause:i})}const o=Array.isArray(s)?s:s.users??[];if(!Array.isArray(o))throw new f("INTERNAL",`${m(n)}: expected \`{ users: [...] }\` from \`firebase auth:export\`, or a bare array`);return o.map(i=>zt(i))},Ie=async function*(e,t,r,n,s){if(r?.auth?.file===void 0)return;const o=e==="supabase"?await Bt(t,r):await Jt(t,r);n.info(`auth: ${String(o.length)} user(s) — passwords are never migrated; users reset via "forgot password"`);for(const i of Mt(o,s))yield i},oe=/\.(?:nd)?json$/i,Ae=e=>{const t=e.split("/").filter(r=>r.length>0);return t[t.length-1]??e},Vt=e=>{const t=Number(e.seconds??0),r=e.nanos??0;return Number.isFinite(t)&&Number.isFinite(r)?t*1e3+Math.floor(r/1e6):Number.NaN},Ht=(e,t)=>{const r=Array.isArray(e)?e:e.data;if(!Array.isArray(r))throw new f("INTERNAL",`${t}: \`bytesValue\` ${JSON.stringify(e)} is neither base64 nor a byte array`);return Buffer.from(r).toString("base64")},Gt=(e,t)=>{const r=typeof e=="string"?Date.parse(e):Vt(e);if(Number.isNaN(r))throw new f("INTERNAL",`${t}: \`timestampValue\` ${JSON.stringify(e)} is neither an RFC-3339 string nor a \`{ seconds, nanos }\` protobuf timestamp`);return r},Wt=(e,t)=>typeof e=="string"?e:Ht(e,t),_e=(e,t)=>{if("nullValue"in e)return null;if(e.stringValue!==void 0)return e.stringValue;if(e.booleanValue!==void 0)return e.booleanValue;if(e.integerValue!==void 0){const r=String(e.integerValue),n=Number(r);return Number.isSafeInteger(n)?n:r}if(e.doubleValue!==void 0)return Number(e.doubleValue);if(e.timestampValue!==void 0)return Gt(e.timestampValue,t);if(e.bytesValue!==void 0)return Wt(e.bytesValue,t);if(e.geoPointValue!==void 0)return{latitude:e.geoPointValue.latitude??0,longitude:e.geoPointValue.longitude??0};if(e.referenceValue!==void 0)return Ae(e.referenceValue);if(e.arrayValue!==void 0)return(e.arrayValue.values??[]).map((r,n)=>_e(r,`${t}[${String(n)}]`));if(e.mapValue!==void 0)return j(e.mapValue.fields??{},t);throw new f("INTERNAL",`${t}: unrecognised Firestore value ${JSON.stringify(e).slice(0,80)}`)},Yt=new Set(["createTime","fields","name","readTime","updateTime"]),Xt=e=>{if(typeof e!="object"||e===null||Array.isArray(e))return!1;const t=Object.keys(e);return t.length===1&&t[0]?.endsWith("Value")===!0},qt=e=>{if(typeof e.name=="string")return!0;const{fields:t}=e;return typeof t!="object"||t===null?!1:Object.keys(e).every(r=>Yt.has(r))&&!Xt(t)},j=(e,t)=>Object.fromEntries(Object.entries(e).map(([r,n])=>[r,_e(n,`${t}.${r}`)])),Kt=(e,t,r,n)=>{const s=qt(e),o=j(s?e.fields??{}:e,n),i=e.name??e.__name__,a=typeof i=="string"?Ae(i):t;if(a===void 0)throw new f("INTERNAL",`${n}: no document id — expected a \`name\`/\`__name__\` resource path, or a document keyed by its id`);const c=r?.types??{};for(const[d,l]of Object.entries(c)){const u=o[d];if(u!==void 0){if(u!==null&&typeof u!="boolean"&&typeof u!="number"&&typeof u!="string")throw new f("INTERNAL",`${n}.${d}: a \`${l}\` reshape needs a scalar, but this field decoded to an object or array`);o[d]=ve(d,l,u===null?null:String(u))}}return{...o,_id:a}},Zt=async(e,t)=>me(e,t,{authFiles:new Set(t?.auth?.file===void 0?[]:[m(t.auth.file)]),emptyMessage:"holds no .json/.ndjson collection files",matches:r=>oe.test(r),tableNameOf:r=>r.replace(oe,"")},async r=>R(r,{withFileTypes:!0})),Qt=async function*(e){const t=x({crlfDelay:Number.POSITIVE_INFINITY,input:v(e,"utf8")});let r=0;try{for await(const n of t){r+=1;const s=n.trim();if(s.length!==0)try{yield{raw:JSON.parse(s)}}catch(o){throw new f("INTERNAL",`${m(e)} line ${String(r)}: invalid JSON — ${o instanceof Error?o.message:String(o)}`,{cause:o})}}}finally{t.close()}},er=async e=>{const t=await $(e,"utf8");let r;try{r=JSON.parse(t)}catch(n){throw new f("INTERNAL",`${m(e)}: invalid JSON — ${n instanceof Error?n.message:String(n)}`,{cause:n})}if(r!==null&&typeof r=="object"&&Array.isArray(r.documents))return r.documents.map(n=>({raw:n}));if(Array.isArray(r))return r.map(n=>({raw:n}));if(r!==null&&typeof r=="object")return Object.entries(r).map(([n,s])=>({fallbackId:n,raw:s}));throw new f("INTERNAL",`${m(e)}: expected an object, an array, or \`{ documents: [...] }\``)},tr=async function*(e,t){const r=t?.tables?.[e.table],n=e.file.toLowerCase().endsWith(".ndjson")?Qt(e.file):await er(e.file);let s=0;for await(const o of n)yield Kt(o.raw,o.fallbackId,r,`${e.table}[${String(s)}]`),s+=1},rr=async function*(e,t,r,n,s){yield*Ie("firebase",s,t,r,n),yield*ye(e,r,n,o=>tr(o,t))},T=e=>e!==null&&typeof e=="object"&&!Array.isArray(e),M=(e,t)=>{if(!T(e))throw new f("INTERNAL",`${t}: expected a JSON object`);return e},E=(e,t,r)=>{const n=e[t];if(n!==void 0&&typeof n!="string")throw new f("INTERNAL",`${r}: \`${t}\` must be a string`);return n},Re=(e,t,r)=>{const n=e[t];if(n!==void 0&&(!Array.isArray(n)||n.some(s=>typeof s!="string")))throw new f("INTERNAL",`${r}: \`${t}\` must be an array of column names`);return n},nr=(e,t)=>{const r=M(e,t),n=E(r,"file",t),s=E(r,"idColumn",t),o=Re(r,"storageColumns",t),{types:i}=r;if(i!==void 0){if(!T(i))throw new f("INTERNAL",`${t}.types must be an object of column → reshape`);for(const[a,c]of Object.entries(i))if(!wt(c))throw new f("INTERNAL",`${t}.types.${a}: unknown reshape ${JSON.stringify(c)} — expected one of ${be.join(", ")}`)}return{file:n,idColumn:s,storageColumns:o,types:i}},sr=(e,t)=>{if(e!==void 0){if(!T(e))throw new f("INTERNAL",`${t}: \`auth\` must be an object`);for(const r of["file","identitiesFile"])E(e,r,`${t}: auth`)}},or=(e,t)=>{if(e!==void 0){if(!T(e))throw new f("INTERNAL",`${t}: \`tables\` must be an object of table → mapping`);return Object.fromEntries(Object.entries(e).map(([r,n])=>[r,nr(n,`${t}: tables.${r}`)]))}},ir=(e,t)=>{const r=M(e,t),n=E(r,"keyPrefix",t),{auth:s,tables:o}=r;return sr(s,t),{auth:s,keyPrefix:n,tables:or(o,t)}},Oe=e=>y("lunora",`import-${e}.json`),ar=async(e,t,r)=>{const n=Oe(t),s=y(e,n);let o;try{o=await $(s,"utf8")}catch(a){if(a.code==="ENOENT"){r.info(`no ${n} found — every column is copied through untouched (run with --scan to generate one)`);return}throw a}let i;try{i=JSON.parse(o)}catch(a){throw new f("INTERNAL",`${s}: invalid JSON — ${a instanceof Error?a.message:String(a)}`,{cause:a})}return ir(i,s)},cr=8,dr=24*1048576,lr=1e3,ur=/^[\dA-F]{64}$/i,fr=e=>{for(let t=0;t<e.length;t+=1){const r=e.codePointAt(t)??0;if(r<32||r===127)return!1}return!0},hr=/^[\d+/A-Z]{43}=$/i,gr=e=>ur.test(e)?e.toLowerCase():hr.test(e)?Buffer.from(e,"base64").toString("hex"):void 0,pr=(e,t)=>{const r=JSON.parse(e),n=r._id;if(typeof n!="string"||n.length===0||n.includes("/")||n.includes("\\"))throw new f("INTERNAL",`${t}: \`_id\` must be a path-free non-empty string`);if(typeof r.sha256!="string")throw new f("INTERNAL",`${t}: \`sha256\` is missing — re-export with \`npx convex export --include-file-storage\``);const s=gr(r.sha256);if(s===void 0)throw new f("INTERNAL",`${t}: \`sha256\` is neither base16 nor base64 SHA-256 (${r.sha256})`);if(typeof r.size!="number"||!Number.isInteger(r.size)||r.size<0)throw new f("INTERNAL",`${t}: \`size\` must be a non-negative integer`);if(r.contentType!==void 0&&(typeof r.contentType!="string"||!fr(r.contentType)))throw new f("INTERNAL",`${t}: \`contentType\` must be a string with no control characters`);return{contentType:typeof r.contentType=="string"?r.contentType:void 0,id:n,sha256:s,size:r.size}},D=async(e,t,r)=>{const n=[];try{let s=0;for await(const o of F(e,t)){const i=o.trim();s+=1,i.length>0&&n.push(pr(i,`_storage/documents.jsonl line ${String(s)}`))}}catch(s){const o=s instanceof Error?s.message:String(s);throw r.error(`failed to read _storage metadata: ${o}`),s}return n},Le=tt,O=e=>e.bucket===void 0?"":`&bucket=${encodeURIComponent(e.bucket)}`,B=async(e,t)=>{const r=[];let n;do{const s=`${e.baseUrl}${U}?prefix=${encodeURIComponent(t)}&limit=${String(lr)}${n===void 0?"":`&cursor=${encodeURIComponent(n)}`}${O(e)}`,o=await e.fetchImpl(s,{headers:{authorization:`Bearer ${e.token}`},method:"GET"});if(!o.ok){const c=await o.text().catch(()=>"<no body>");throw new f("INTERNAL",`storage list failed (HTTP ${String(o.status)}): ${c}`)}const i=await o.json();r.push(...i.objects??[]);const a=i.truncated===!0?i.cursor:void 0;if(a!==void 0&&a===n)throw new f("INTERNAL","storage list did not advance its cursor — refusing to page forever");n=a}while(n!==void 0);return r},mr=async(e,t,r,n)=>{const s=`${e.baseUrl}${U}?key=${encodeURIComponent(t)}&expectedSha256=${n.sha256}&expectedSize=${String(n.size)}${O(e)}`,o=await e.fetchImpl(s,{body:new Uint8Array(r),headers:{authorization:`Bearer ${e.token}`,"content-type":n.contentType??"application/octet-stream"},method:"PUT"});if(!o.ok){const a=await o.text().catch(()=>"<no body>");throw new f("INTERNAL",`blob upload failed (HTTP ${String(o.status)}): ${a}`)}const i=await o.json();if(i.sha256!==n.sha256)throw new f("INTERNAL",`blob upload verification failed: expected ${n.sha256}, got ${i.sha256??"none"}`);return t},yr=async(e,t)=>(await e.fetchImpl(`${e.baseUrl}${U}?key=${encodeURIComponent(t)}${O(e)}`,{headers:{authorization:`Bearer ${e.token}`},method:"DELETE"}).catch(()=>{}))?.ok===!0,br=async(e,t,r,n,s)=>{const o=`${e.baseUrl}${rt}?key=${encodeURIComponent(t)}&method=PUT&contentType=${encodeURIComponent(n.contentType??"application/octet-stream")}${O(e)}`,i=await e.fetchImpl(o,{headers:{authorization:`Bearer ${e.token}`},method:"GET"});if(!i.ok){const p=await i.text().catch(()=>"<no body>");throw new f("INTERNAL",`blob ${t} is ${String(n.size)} bytes, above the ${String(Le)}-byte verified-upload cap, and no signed PUT URL could be minted (HTTP ${String(i.status)}): ${p}`)}const{url:a}=await i.json(),c=await e.fetchImpl(a,{body:new Uint8Array(r),headers:{"content-type":n.contentType??"application/octet-stream"},method:"PUT"});if(!c.ok){const p=await c.text().catch(()=>"<no body>");throw new f("INTERNAL",`signed PUT failed (HTTP ${String(c.status)}): ${p}`)}const l=(await B(e,t)).find(p=>p.key===t);if(l===void 0)throw new f("INTERNAL",`post-upload verification failed: blob not found at key ${t}`);const u=l.size!==void 0&&l.size!==n.size,h=l.sha256!==void 0&&l.sha256.toLowerCase()!==n.sha256;if(u||h){const p=await yr(e,t);throw new f("INTERNAL",`post-upload verification failed: expected sha256=${n.sha256} size=${String(n.size)}, got sha256=${l.sha256??"none"} size=${String(l.size??"none")}${p?" (the object was removed)":` — AND the object could not be removed: delete ${t} by hand before re-running, or the next run will treat it as already migrated`}`)}const g=[l.size===void 0?"size":void 0,l.sha256===void 0?"sha256":void 0].filter(Boolean);return g.length>0&&s.warn(`blob ${t} went through the signed-PUT path and the host reports no ${g.join(" or ")} for it — that much of the write is unverified`),t},ke=async(e,t,r,n,s)=>{if(r.length!==n.size)throw new f("INTERNAL",`blob ${n.id} is ${String(r.length)} bytes on disk but the export declares ${String(n.size)}`);return r.length<=Le?mr(e,t,r,n):br(e,t,r,n,s)},wr=e=>{const t=[];let r=[],n=0;for(const s of e)r.length>0&&(r.length>=cr||n+s.size>dr)&&(t.push(r),r=[],n=0),r.push(s),n+=s.size;return r.length>0&&t.push(r),t},$r=async(e,t,r,n,s)=>{const o=await D(t,r,s),i=new Map,a=await B(e,n),c=new Map(a.map(u=>[u.key,u])),d=[];for(const u of o){const h=`${n}${u.sha256}`;c.get(h)?.size===u.size?i.set(u.id,h):d.push(u)}s.info(`migrating ${String(d.length)} storage blobs${i.size>0?` (${String(i.size)} already present)`:""}...`);const l=async u=>{try{const h=await mt(t,u.id);i.set(u.id,await ke(e,`${n}${u.sha256}`,h,u,s))}catch(h){const g=h instanceof Error?h.message:String(h);throw s.error(`failed to upload blob ${u.id}: ${g}`),h}};for(const u of wr(d)){const g=(await Promise.allSettled(u.map(p=>l(p)))).find(p=>p.status==="rejected");if(g!==void 0)throw g.reason}return s.success(`migrated ${String(d.length)} storage blobs`),i};async function*Sr(e,t,r){let n=0;for await(const s of F(e,t)){const o=s.trim();if(n+=1,o.length===0)continue;let i;try{i=JSON.parse(o)}catch(a){throw new f("INTERNAL",`${t.table}/documents.jsonl line ${String(n)}: invalid JSON — ${a instanceof Error?a.message:String(a)}`,{cause:a})}r.set(t.table,(r.get(t.table)??0)+1),yield`${JSON.stringify({doc:i,table:t.table})}
6
+ `}}async function*vr(e,t,r,n,s){for(const o of t){if(ge(o.table)){o.table===N&&!n&&r.warn(`skipping "${N}" — those rows describe stored files, and their blobs were not migrated. Re-run with --with-storage to upload them and rewrite the references.`);continue}s.has(o.table)||s.set(o.table,0),yield*Sr(e,o,s)}}const Un=["firebase","supabase"],Nr=async(e,t,r)=>{if(e.withStorage===!0&&t==="firebase"&&e.storageDir===void 0)return e.logger.error("--with-storage on Firebase needs --storage-dir — download the bucket first with `gcloud storage cp -r gs://<bucket> <dir>`."),{kind:"invalid"};if(e.table!==void 0)return e.logger.error(`--table cannot be combined with --from ${t} — each row's table comes from its source file.`),{kind:"invalid"};const n=await ar(r,t,e.logger),s=Object.values(n?.tables??{}).some(o=>(o.storageColumns??[]).length>0);return e.verify===!0&&e.withStorage!==!0&&s?(e.logger.error("--verify with `storageColumns` declared requires --with-storage — otherwise every storage path stays unmigrated and only row counts would be checked."),{kind:"invalid"}):t==="supabase"?{kind:"supabase",mapping:n,tables:await Ot(e.file,n)}:{collections:await Zt(e.file,n),kind:"firebase",mapping:n}},Er=async e=>{if(!await _(e.file).then(()=>!0,()=>!1))return!1;for(const[r,n]of[["--scan",e.scan],["--verify",e.verify],["--with-storage",e.withStorage]])if(n===!0)return e.logger.error(`${r} requires a Convex export directory or .zip snapshot — ${e.file} is not one.`),!0;return!1},Tr=async(e,t,r)=>{const n=t.find(o=>o.table===N);if(n===void 0)return r.logger.error("--verify cannot check file references: this export has no `_storage` table, so it was taken without `--include-file-storage`. Re-export with that flag and pass --with-storage, or drop --verify."),!0;const s=await D(e,n,r.logger);return s.length>0?(r.logger.error(`--verify on an export carrying ${String(s.length)} stored file(s) requires --with-storage — otherwise every file reference stays unmigrated and only row counts would be checked.`),!0):!1},Ir=async(e,t)=>{if(e.from==="supabase"||e.from==="firebase")return Nr(e,e.from,t);const r=await ft(e.file),n=r===void 0?void 0:await pt(r);return r!==void 0&&n===void 0?(e.logger.error(`${e.file} is a ${r.kind==="zip"?".zip":"directory"} but holds no <table>/documents.jsonl — expected a \`npx convex export --path\` snapshot, or pass an NDJSON file.`),{kind:"invalid"}):r===void 0||n===void 0?await Er(e)?{kind:"invalid"}:{kind:"ndjson"}:e.table!==void 0?(e.logger.error("--table cannot be combined with a Convex export directory — each row's table comes from its source directory."),{kind:"invalid"}):e.verify===!0&&e.withStorage!==!0&&await Tr(r,n,e)?{kind:"invalid"}:{kind:"convex",snapshot:r,tables:n}},A=y("lunora","import-convex.json"),Ar=(e,t)=>{const r=M(e,t),n=E(r,"keyPrefix",t),s=r.storageColumns;if(s!==void 0){if(!T(s))throw new f("INTERNAL",`${t}: \`storageColumns\` must be an object of table → column names`);for(const o of Object.keys(s))Re(s,o,`${t}: storageColumns`)}return{keyPrefix:n,storageColumns:s}},_r=async(e,t)=>{const r=y(e,A);let n;try{n=await $(r,"utf8")}catch(o){if(o.code==="ENOENT"){t.info(`no ${A} found — rewriting only self-describing { $storage } refs (run with --scan to generate one)`);return}throw o}let s;try{s=JSON.parse(n)}catch(o){throw new f("INTERNAL",`${r}: invalid JSON — ${o instanceof Error?o.message:String(o)}`,{cause:o})}return Ar(s,r)},Rr=(e,t,r)=>{try{return JSON.parse(e)}catch(n){throw new f("INTERNAL",`${t}/documents.jsonl line ${String(r)}: invalid JSON — ${n instanceof Error?n.message:String(n)}`,{cause:n})}},Or=async(e,t,r)=>{const n=new Map([...r].map(i=>[i,i])),s=[];let o=0;for await(const i of F(e,t)){const a=i.trim();if(o+=1,a.length===0)continue;const{ambiguous:c}=pe(Rr(a,t.table,o),n,t.table);for(const{column:d}of c)s.includes(d)||s.push(d)}return s},Lr=async(e,t,r)=>{const n={};for(const s of t){if(ge(s.table))continue;const o=await Or(e,s,r);o.length>0&&(n[s.table]=o)}return n},kr=async(e,t,r)=>{const n=y(t,A),s=`${JSON.stringify(e,void 0,4)}
7
7
  `;await P(y(t,"lunora"),{recursive:!0});try{await le(n,s,{encoding:"utf8",flag:"wx"}),r.success(`wrote candidate mapping to ${n} — review it, then re-run without --scan`)}catch(o){if(o.code!=="EEXIST")throw o;r.warn(`${n} already exists — leaving it untouched. Candidate mapping:`),r.info(s)}},xr=async(e,t,r,n)=>{const s=t.find(c=>c.table===N);if(s===void 0){n.error("no `_storage` table in this export — re-export with `npx convex export --include-file-storage`");return}const o=await D(e,s,n),i=new Set(o.map(c=>c.id));n.info(`found ${String(i.size)} storage ids`);const a={keyPrefix:"",storageColumns:await Lr(e,t,i)};return await kr(a,r,n),a},S=20,Cr=(e,t,r)=>{let n=0;if(r.conflicts===0)for(const[s,o]of t){const i=r.inserted[s]??0;i<o&&(n+=1,e.error(`verify: ${s} inserted ${String(i)} of ${String(o)} source rows (${String(o-i)} missing)`))}else{const s=[...t.values()].reduce((i,a)=>i+a,0),o=Object.values(r.inserted).reduce((i,a)=>i+a,0)+r.conflicts;o<s&&(n+=1,e.error(`verify: ${String(o)} of ${String(s)} source rows accounted for across all tables (${String(s-o)} missing; ${String(r.conflicts)} already present)`))}return n>0?e.error(`verify: ${String(n)} row-parity check(s) failed`):e.success("verify: all source rows accounted for"),n},ie=(e,t,r)=>{const n=new Set;for(const s of t){const o=`${s.table} ${s.column} ${s.storageId}`;n.has(o)||(n.add(o),n.size<=S&&e.warn(r(s)))}n.size>S&&e.warn(`… and ${String(n.size-S)} more`)},jr=(e,t,r)=>(e.info(`storage refs: ${String(t.rewritten)} rewritten, ${String(t.unmigrated.length)} unmigrated, ${String(t.ambiguous.length)} ambiguous`),ie(e,t.unmigrated,n=>`unmigrated storage reference ${n.table}.${n.column}: ${n.storageId} has no exported blob — re-export with \`npx convex export --include-file-storage\``),ie(e,t.ambiguous,n=>`unrewritten storage id in ${n.table}.${n.column}: ${n.storageId} — if that column holds storage references, add it to ${A} and re-import`),r&&t.unmigrated.length>0?(e.error(`verify: ${String(t.unmigrated.length)} storage reference(s) resolved to no migrated blob`),!0):!1),Pr=(e,t,r)=>{for(const n of[...t].slice(0,S))e.warn(`storage path never transferred: ${n} — left as-is`);return t.size>S&&e.warn(`… and ${String(t.size-S)} more untransferred storage paths`),r&&t.size>0},ae=200,Ur=/^\\x[\dA-Fa-f]*$/,Fr=/^[+-]?\d+$/,zr=/^[+-]?\d+\.\d+$/,Mr=/^\{.*\}$/,Dr=/^\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}/,Br=[["bytea-base64",e=>Ur.test(e)],["timestamp-ms",e=>Dr.test(e)],[e=>e.some(t=>!Number.isSafeInteger(Number(t)))?"int8-string":void 0,e=>Fr.test(e)],["boolean",e=>e==="f"||e==="t"],["number",e=>zr.test(e)],[e=>e.every(t=>Mr.test(t))?"text-array":"json",e=>e.startsWith("{")||e.startsWith("[")]],Jr=e=>{if(e.length!==0){for(const[t,r]of Br)if(e.every(n=>r(n)))return typeof t=="function"?t(e):t}},Vr=(e,t)=>{for(const[r,n]of Object.entries(e)){if(n.length===0)continue;const s=t.get(r);s===void 0?t.set(r,[n]):s.push(n)}},Hr=async e=>{const t=new Map,r=v(e).pipe(he({columns:!0,skipEmptyLines:!0,toLine:ae+1}));let n=0;for await(const o of r)if(Vr(o,t),n+=1,n>=ae)break;r.destroy();const s={};for(const[o,i]of t){const a=Jr(i);a!==void 0&&(s[o]=a)}return s},xe=async(e,t,r,n)=>{const s=Oe(r),o=y(t,s),i=`${JSON.stringify(e,void 0,4)}
8
8
  `;await P(ue(o),{recursive:!0});try{await le(o,i,{encoding:"utf8",flag:"wx"}),n.success(`wrote candidate mapping to ${o} — review the inferred types, then re-run without --scan`)}catch(a){if(a.code!=="EEXIST")throw a;n.warn(`${o} already exists — leaving it untouched. Candidate mapping:`),n.info(i)}},Gr=async(e,t,r)=>{const n={};for(const o of e){const i=await Hr(o.file);r.info(`${m(o.file)} → ${o.table}: ${String(Object.keys(i).length)} column(s) need a reshape`),n[o.table]={file:m(o.file),idColumn:"id",...Object.keys(i).length>0?{types:i}:{}}}const s={keyPrefix:"",tables:n};return await xe(s,t,"supabase",r),s},Wr=async(e,t,r)=>{const n={};for(const o of e)n[o.table]={file:m(o.file),storageColumns:[]};r.info(`found ${String(e.length)} collection(s) — Firestore values are self-describing, so only storage columns need declaring`);const s={keyPrefix:"",tables:n};return await xe(s,t,"firebase",r),s},J=e=>y("lunora",`.import-storage-${e}.ndjson`),Yr=async(e,t,r)=>{const n=new Map;let s;try{s=await $(y(e,J(t)),"utf8")}catch(i){if(i.code==="ENOENT")return n;throw i}let o=0;for(const i of s.split(`
9
9
  `)){const a=i.trim();if(a.length!==0)try{const c=JSON.parse(a);typeof c.path=="string"&&typeof c.key=="string"?n.set(c.path,c):o+=1}catch{o+=1}}return n.size>0&&r.info(`resuming: ${String(n.size)} object(s) already transferred${o>0?` (${String(o)} unreadable checkpoint line(s) ignored)`:""}`),n},Xr=async(e,t,r)=>{const n=y(e,J(t));await P(ue(n),{recursive:!0}),await He(n,`${JSON.stringify(r)}
10
- `,"utf8")},Ce=100,qr=e=>e.split("/").map(t=>encodeURIComponent(t)).join("/"),Kr=async(e,t,r,n,s)=>{const o=await r(`${e.url}/storage/v1/object/list/${encodeURIComponent(t)}`,{body:JSON.stringify({limit:Ce,offset:s,prefix:n}),headers:{authorization:`Bearer ${e.serviceKey}`,"content-type":"application/json"},method:"POST"});if(!o.ok){const i=await o.text().catch(()=>"<no body>");throw new f("INTERNAL",`Supabase storage list failed for bucket ${t} (HTTP ${String(o.status)}): ${i}`)}return await o.json()},je=async(e,t,r,n="")=>{const s=[];let o=0;for(;;){const i=await Kr(e,t,r,n,o);for(const a of i){const c=n===""?a.name:`${n}/${a.name}`;a.id===null||a.id===void 0?s.push(...await je(e,t,r,c)):s.push({contentType:a.metadata?.mimetype,name:c})}if(i.length<Ce)return s;o+=i.length}},Zr=async(e,t,r)=>{const n=await t(`${e.url}/storage/v1/bucket`,{headers:{authorization:`Bearer ${e.serviceKey}`},method:"GET"});if(!n.ok){const i=await n.text().catch(()=>"<no body>");throw new f("INTERNAL",`Supabase bucket list failed (HTTP ${String(n.status)}): ${i} — check the project URL and that the key is the service-role key, not the anon key`)}const s=await n.json(),o=[];for(const i of s){const a=await je(e,i.name,t);r.info(`supabase bucket ${i.name}: ${String(a.length)} object(s)`);for(const c of a){const l=`${i.name}/${c.name}`;o.push({contentType:c.contentType,bytes:async()=>{const d=await t(`${e.url}/storage/v1/object/${encodeURIComponent(i.name)}/${qr(c.name)}`,{headers:{authorization:`Bearer ${e.serviceKey}`},method:"GET"});if(!d.ok){const u=await d.text().catch(()=>"<no body>");throw new f("INTERNAL",`Supabase download failed for ${l} (HTTP ${String(d.status)}): ${u}`)}if(d.arrayBuffer===void 0)throw new f("INTERNAL","the fetch implementation cannot read response bytes, which the storage transfer requires");return Buffer.from(await d.arrayBuffer())},path:l})}}return o},Qr=async e=>{const t=k(e),r=[],n=async s=>{const o=await R(s,{withFileTypes:!0}).catch(()=>{});if(o===void 0)throw new f("INTERNAL",`${e} is not a readable directory — download the bucket first with \`gcloud storage cp -r gs://<bucket> <dir>\``);for(const i of o){const a=k(s,i.name);if(!fe(t,a))throw new f("INTERNAL",`${i.name} resolves outside ${e} — refusing to upload it`);i.isDirectory()?await n(a):i.isFile()&&r.push({bytes:async()=>$(a),path:Ye(t,a).split(Xe).join("/")})}};return await n(t),r},en=async(e,t,r,n,s)=>{const o=await t.bytes(),i=st("sha256").update(o).digest("hex"),a=`${r.keyPrefix}${i}`;if(n.get(a)?.size!==o.length){const c={contentType:t.contentType,id:t.path,sha256:i,size:o.length};await ke(e,a,o,c,s)}return await Xr(r.cwd,r.source,{key:a,path:t.path,size:o.length}),a},tn=(e,t)=>{const r=e>500?100:25;return n=>{if(n===e||n%r===0){const s=e===0?100:Math.round(n/e*100);t.info(`transferred ${String(n)}/${String(e)} object(s) (${String(s)}%)`)}}},ce=async(e,t,r,n)=>{const s=new Map,o=await Yr(r.cwd,r.source,n),i=await B(e,r.keyPrefix),a=new Map(i.map(d=>[d.key,d]));o.size>0&&i.length===0&&n.warn(`the checkpoint records ${String(o.size)} transferred object(s) but the target holds none under \`${r.keyPrefix}\` — re-transferring (a different deployment, a wiped bucket, or a changed keyPrefix)`);const c=tn(t.length,n);let l=0;n.info(`transferring ${String(t.length)} object(s) to R2...`);for(const d of t){const u=o.get(d.path);if(u!==void 0&&a.has(u.key)){s.set(d.path,u.key),l+=1,c(l);continue}try{const h=await en(e,d,r,a,n);s.set(d.path,h)}catch(h){const g=h instanceof Error?h.message:String(h);throw n.error(`failed transferring ${d.path} after ${String(l)} object(s): ${g}`),n.error(`progress is saved — re-run the same command to continue from here (delete ${J(r.source)} to start over)`),h}l+=1,c(l)}return n.success(`transferred ${String(t.length)} object(s) to R2`),s},de=/^\/+/,rn=/\/storage\/v1\/object\/(?:public\/|sign\/|authenticated\/)?/,nn=/[#?]/u,sn=e=>{const t=new Map(e),r=new Map;for(const[n,s]of e){const o=n.indexOf("/");if(o===-1)continue;const i=n.slice(o+1);i.length===0||e.has(i)||r.set(i,r.has(i)?void 0:s)}for(const[n,s]of r)s!==void 0&&t.set(n,s);return t},on=e=>{const t=rn.exec(e);if(t===null)return;const r=e.slice(t.index+t[0].length).split(nn)[0]??"";if(r.length!==0)try{return decodeURIComponent(r)}catch{return r}},an=(e,t)=>{const r=t.get(e);if(r!==void 0)return r;const n=e.replace(de,""),s=t.get(n);if(s!==void 0)return s;const o=on(e);return o===void 0?void 0:t.get(o)??t.get(o.replace(de,""))},cn=500,dn=9e5,ln=async e=>{if(e.prod&&e.url===void 0){e.logger.error("--prod requires an explicit --url (refusing to import to the implicit localhost worker)");return}if(e.prod&&e.yes!==!0){e.logger.error("import --prod bulk-writes production. Re-run with --yes to confirm.");return}const t=We(e.url,e.logger,e.cwd);if(t===void 0)return;const{token:r}=Ge({cwd:e.cwd??process.cwd(),token:e.token,url:t});if(!r){e.logger.error("admin token required — pass --token, set LUNORA_ADMIN_TOKEN, or add it to .dev.vars (local targets only)");return}try{const s=await _(e.file);if(!s.isFile()&&!s.isDirectory()){e.logger.error(`not a file or directory: ${e.file}`);return}}catch(s){const o=s instanceof Error?s.message:String(s);e.logger.error(`failed to stat ${e.file}: ${o}`);return}const n=e.fetchImpl??globalThis.fetch;if(typeof n!="function")throw new TypeError("no fetch implementation available — pass fetchImpl or run on Node >= 18");return{baseUrl:t,fetchImpl:n,requestUrl:`${t}${nt}`,token:r}},un=(e,t,r)=>({conflicts:e.conflicts,errors:e.errors,inserted:e.inserted,received:e.received,...t===void 0?{}:{storage:{ambiguous:r.ambiguous,blobs:t.size,rewritten:r.rewritten,unmigrated:r.unmigrated}},...e.warnings.length>0?{warnings:e.warnings}:{}}),fn=(e,t,r,n)=>{switch(e.kind){case"convex":return vr(e.snapshot,e.tables,t.logger,r,n);case"firebase":return rr(e.collections,e.mapping,t.logger,n,t.file);case"supabase":return xt(e.tables,e.mapping,t.logger,n,t.file);default:return v(t.file,{encoding:"utf8"})}},hn=async(e,t,r)=>{switch(e.kind){case"convex":return xr(e.snapshot,e.tables,t,r);case"firebase":return Wr(e.collections,t,r);case"supabase":return Gr(e.tables,t,r);default:{r.error("--scan needs a Convex, Supabase, or Firebase source.");return}}},gn=async(e,t,r,n)=>{const s=t.mapping?.keyPrefix??"";if(r.storageDir!==void 0)return ce(e,await Qr(r.storageDir),{cwd:n,keyPrefix:s,source:t.kind},r.logger);if(t.kind==="firebase"){r.logger.error("--with-storage needs --storage-dir for a Firebase source: download the bucket first (`gcloud storage cp -r gs://<bucket> ./storage`), then point --storage-dir at it.");return}const o=process.env.SUPABASE_URL,i=process.env.SUPABASE_SERVICE_ROLE_KEY;if(o===void 0||i===void 0){r.logger.error("--with-storage needs SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY in the environment (the service-role key, not the anon key), or --storage-dir pointing at an already-downloaded bucket.");return}let a=o.length;for(;a>0&&o[a-1]==="/";)a-=1;const c=o.slice(0,a);if(!c.startsWith("https://")){r.logger.error(`SUPABASE_URL must be https:// — refusing to send the service-role key over ${c.split(":")[0]??"an unknown scheme"}.`);return}const l=await Zr({serviceKey:i,url:c},e.fetchImpl,r.logger);return ce(e,l,{cwd:n,keyPrefix:s,source:t.kind},r.logger)},pn=(e,t,r,n,s)=>{const o=n.mapping?.tables?.[t]?.storageColumns??[];if(o.length===0)return e;const i={...e};for(const a of o){const c=i[a];if(typeof c!="string"||c.length===0)continue;const l=an(c,r);l===void 0?s.add(`${t}.${a}: ${c}`):i[a]=l}return i},mn=e=>{const t=e.source.kind==="supabase"||e.source.kind==="firebase"?e.source:void 0,{transferredPaths:r}=e,n=r===void 0||t===void 0?void 0:sn(r);return bt({remapDocument:n===void 0||t===void 0?void 0:(s,o)=>pn(s,o,n,t,e.unresolvedPaths),report:e.report,storageColumns:e.storageColumns,storageIdMap:e.storageIdMap,table:e.table})},yn=async(e,t,r,n)=>{let s="",o=0;const i=async a=>{o+=1;const c=t(a,o);c!==void 0&&await r.push(c)};try{for await(const a of e){s+=typeof a=="string"?a:a.toString("utf8");let c=s.indexOf(`
10
+ `,"utf8")},Ce=100,qr=e=>e.split("/").map(t=>encodeURIComponent(t)).join("/"),Kr=async(e,t,r,n,s)=>{const o=await r(`${e.url}/storage/v1/object/list/${encodeURIComponent(t)}`,{body:JSON.stringify({limit:Ce,offset:s,prefix:n}),headers:{authorization:`Bearer ${e.serviceKey}`,"content-type":"application/json"},method:"POST"});if(!o.ok){const i=await o.text().catch(()=>"<no body>");throw new f("INTERNAL",`Supabase storage list failed for bucket ${t} (HTTP ${String(o.status)}): ${i}`)}return await o.json()},je=async(e,t,r,n="")=>{const s=[];let o=0;for(;;){const i=await Kr(e,t,r,n,o);for(const a of i){const c=n===""?a.name:`${n}/${a.name}`;a.id===null||a.id===void 0?s.push(...await je(e,t,r,c)):s.push({contentType:a.metadata?.mimetype,name:c})}if(i.length<Ce)return s;o+=i.length}},Zr=async(e,t,r)=>{const n=await t(`${e.url}/storage/v1/bucket`,{headers:{authorization:`Bearer ${e.serviceKey}`},method:"GET"});if(!n.ok){const i=await n.text().catch(()=>"<no body>");throw new f("INTERNAL",`Supabase bucket list failed (HTTP ${String(n.status)}): ${i} — check the project URL and that the key is the service-role key, not the anon key`)}const s=await n.json(),o=[];for(const i of s){const a=await je(e,i.name,t);r.info(`supabase bucket ${i.name}: ${String(a.length)} object(s)`);for(const c of a){const d=`${i.name}/${c.name}`;o.push({contentType:c.contentType,bytes:async()=>{const l=await t(`${e.url}/storage/v1/object/${encodeURIComponent(i.name)}/${qr(c.name)}`,{headers:{authorization:`Bearer ${e.serviceKey}`},method:"GET"});if(!l.ok){const u=await l.text().catch(()=>"<no body>");throw new f("INTERNAL",`Supabase download failed for ${d} (HTTP ${String(l.status)}): ${u}`)}if(l.arrayBuffer===void 0)throw new f("INTERNAL","the fetch implementation cannot read response bytes, which the storage transfer requires");return Buffer.from(await l.arrayBuffer())},path:d})}}return o},Qr=async e=>{const t=k(e),r=[],n=async s=>{const o=await R(s,{withFileTypes:!0}).catch(()=>{});if(o===void 0)throw new f("INTERNAL",`${e} is not a readable directory — download the bucket first with \`gcloud storage cp -r gs://<bucket> <dir>\``);for(const i of o){const a=k(s,i.name);if(!fe(t,a))throw new f("INTERNAL",`${i.name} resolves outside ${e} — refusing to upload it`);i.isDirectory()?await n(a):i.isFile()&&r.push({bytes:async()=>$(a),path:Ye(t,a).split(Xe).join("/")})}};return await n(t),r},en=async(e,t,r,n,s)=>{const o=await t.bytes(),i=st("sha256").update(o).digest("hex"),a=`${r.keyPrefix}${i}`;if(n.get(a)?.size!==o.length){const c={contentType:t.contentType,id:t.path,sha256:i,size:o.length};await ke(e,a,o,c,s)}return await Xr(r.cwd,r.source,{key:a,path:t.path,size:o.length}),a},tn=(e,t)=>{const r=e>500?100:25;return n=>{if(n===e||n%r===0){const s=e===0?100:Math.round(n/e*100);t.info(`transferred ${String(n)}/${String(e)} object(s) (${String(s)}%)`)}}},ce=async(e,t,r,n)=>{const s=new Map,o=await Yr(r.cwd,r.source,n),i=await B(e,r.keyPrefix),a=new Map(i.map(l=>[l.key,l]));o.size>0&&i.length===0&&n.warn(`the checkpoint records ${String(o.size)} transferred object(s) but the target holds none under \`${r.keyPrefix}\` — re-transferring (a different deployment, a wiped bucket, or a changed keyPrefix)`);const c=tn(t.length,n);let d=0;n.info(`transferring ${String(t.length)} object(s) to R2...`);for(const l of t){const u=o.get(l.path);if(u!==void 0&&a.has(u.key)){s.set(l.path,u.key),d+=1,c(d);continue}try{const h=await en(e,l,r,a,n);s.set(l.path,h)}catch(h){const g=h instanceof Error?h.message:String(h);throw n.error(`failed transferring ${l.path} after ${String(d)} object(s): ${g}`),n.error(`progress is saved — re-run the same command to continue from here (delete ${J(r.source)} to start over)`),h}d+=1,c(d)}return n.success(`transferred ${String(t.length)} object(s) to R2`),s},de=/^\/+/,rn=/\/storage\/v1\/object\/(?:public\/|sign\/|authenticated\/)?/,nn=/[#?]/u,sn=e=>{const t=new Map(e),r=new Map;for(const[n,s]of e){const o=n.indexOf("/");if(o===-1)continue;const i=n.slice(o+1);i.length===0||e.has(i)||r.set(i,r.has(i)?void 0:s)}for(const[n,s]of r)s!==void 0&&t.set(n,s);return t},on=e=>{const t=rn.exec(e);if(t===null)return;const r=e.slice(t.index+t[0].length).split(nn)[0]??"";if(r.length!==0)try{return decodeURIComponent(r)}catch{return r}},an=(e,t)=>{const r=t.get(e);if(r!==void 0)return r;const n=e.replace(de,""),s=t.get(n);if(s!==void 0)return s;const o=on(e);return o===void 0?void 0:t.get(o)??t.get(o.replace(de,""))},cn=500,dn=9e5,ln=async e=>{if(e.prod&&e.url===void 0){e.logger.error("--prod requires an explicit --url (refusing to import to the implicit localhost worker)");return}if(e.prod&&e.yes!==!0){e.logger.error("import --prod bulk-writes production. Re-run with --yes to confirm.");return}const t=We(e.url,e.logger,e.cwd);if(t===void 0)return;const{token:r}=Ge({cwd:e.cwd??process.cwd(),token:e.token,url:t});if(!r){e.logger.error("admin token required — pass --token, set LUNORA_ADMIN_TOKEN, or add it to .dev.vars (local targets only)");return}try{const s=await _(e.file);if(!s.isFile()&&!s.isDirectory()){e.logger.error(`not a file or directory: ${e.file}`);return}}catch(s){const o=s instanceof Error?s.message:String(s);e.logger.error(`failed to stat ${e.file}: ${o}`);return}const n=e.fetchImpl??globalThis.fetch;if(typeof n!="function")throw new TypeError("no fetch implementation available — pass fetchImpl or run on Node >= 18");return{baseUrl:t,fetchImpl:n,requestUrl:`${t}${nt}`,token:r}},un=(e,t,r)=>({conflicts:e.conflicts,errors:e.errors,inserted:e.inserted,received:e.received,...t===void 0?{}:{storage:{ambiguous:r.ambiguous,blobs:t.size,rewritten:r.rewritten,unmigrated:r.unmigrated}},...e.warnings.length>0?{warnings:e.warnings}:{}}),fn=(e,t,r,n)=>{switch(e.kind){case"convex":return vr(e.snapshot,e.tables,t.logger,r,n);case"firebase":return rr(e.collections,e.mapping,t.logger,n,t.file);case"supabase":return xt(e.tables,e.mapping,t.logger,n,t.file);default:return v(t.file,{encoding:"utf8"})}},hn=async(e,t,r)=>{switch(e.kind){case"convex":return xr(e.snapshot,e.tables,t,r);case"firebase":return Wr(e.collections,t,r);case"supabase":return Gr(e.tables,t,r);default:{r.error("--scan needs a Convex, Supabase, or Firebase source.");return}}},gn=async(e,t,r,n)=>{const s=t.mapping?.keyPrefix??"";if(r.storageDir!==void 0)return ce(e,await Qr(r.storageDir),{cwd:n,keyPrefix:s,source:t.kind},r.logger);if(t.kind==="firebase"){r.logger.error("--with-storage needs --storage-dir for a Firebase source: download the bucket first (`gcloud storage cp -r gs://<bucket> ./storage`), then point --storage-dir at it.");return}const o=process.env.SUPABASE_URL,i=process.env.SUPABASE_SERVICE_ROLE_KEY;if(o===void 0||i===void 0){r.logger.error("--with-storage needs SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY in the environment (the service-role key, not the anon key), or --storage-dir pointing at an already-downloaded bucket.");return}let a=o.length;for(;a>0&&o[a-1]==="/";)a-=1;const c=o.slice(0,a);if(!c.startsWith("https://")){r.logger.error(`SUPABASE_URL must be https:// — refusing to send the service-role key over ${c.split(":")[0]??"an unknown scheme"}.`);return}const d=await Zr({serviceKey:i,url:c},e.fetchImpl,r.logger);return ce(e,d,{cwd:n,keyPrefix:s,source:t.kind},r.logger)},pn=(e,t,r,n,s)=>{const o=n.mapping?.tables?.[t]?.storageColumns??[];if(o.length===0)return e;const i={...e};for(const a of o){const c=i[a];if(typeof c!="string"||c.length===0)continue;const d=an(c,r);d===void 0?s.add(`${t}.${a}: ${c}`):i[a]=d}return i},mn=e=>{const t=e.source.kind==="supabase"||e.source.kind==="firebase"?e.source:void 0,{transferredPaths:r}=e,n=r===void 0||t===void 0?void 0:sn(r);return bt({remapDocument:n===void 0||t===void 0?void 0:(s,o)=>pn(s,o,n,t,e.unresolvedPaths),report:e.report,storageColumns:e.storageColumns,storageIdMap:e.storageIdMap,table:e.table})},yn=async(e,t,r,n)=>{let s="",o=0;const i=async a=>{o+=1;const c=t(a,o);c!==void 0&&await r.push(c)};try{for await(const a of e){s+=typeof a=="string"?a:a.toString("utf8");let c=s.indexOf(`
11
11
  `);for(;c!==-1;)await i(s.slice(0,c)),s=s.slice(c+1),c=s.indexOf(`
12
- `)}s.length>0&&await i(s),await r.flush();return}catch(a){return n.error(`import failed part-way through: ${a instanceof Error?a.message:String(a)}`),n.error("the rows below had already been written — re-run the same command to resume (existing rows conflict rather than duplicate)"),a}},bn=async(e,t,r,n)=>{const s=await _r(r,n),o=t.tables.find(a=>a.table===N);if(o===void 0){n.error("--with-storage requires a Convex export with a `_storage` metadata table — re-export with `npx convex export --include-file-storage`.");return}const i=await $r(e,t.snapshot,o,s?.keyPrefix??"",n);return n.info(`storage map: ${String(i.size)} blobs mapped`),{mapping:s,storageIdMap:i}},wn=async(e,t,r,n)=>{if(r.withStorage!==!0)return{};if(t.kind==="convex")return bn(e,t,n,r.logger);if(t.kind!=="supabase"&&t.kind!=="firebase")return{};try{const s=await gn(e,t,r,n);return s===void 0?void 0:{transferredPaths:s}}catch{r.logger.error("no rows were imported — fix the transfer and re-run; it will resume where it stopped");return}},$n=(e,t)=>{for(const s of t.warnings)e.warn(s);const r=t.received-t.insertedTotal-t.conflicts-t.errorCount;r>0&&e.warn(`${String(r)} of ${String(t.received)} rows were neither inserted, conflicted, nor reported as errors`);const n=`imported ${String(t.insertedTotal)} of ${String(t.received)} rows (${String(t.conflicts)} conflicts, ${String(t.errorCount)} errors)`;t.failed?e.error(n):e.success(n)},Fn=async e=>{const t=e.cwd??process.cwd(),r=await Ir(e,t);if(r.kind==="invalid")return{body:void 0,code:1,inserted:0};if(e.scan===!0)return{body:void 0,code:await hn(r,t,e.logger)===void 0?1:0,inserted:0};const n=await ln(e);if(n===void 0)return{body:void 0,code:1,inserted:0};const{baseUrl:s,fetchImpl:o,requestUrl:i,token:a}=n,c=e.batchSize??cn,l=await wn({baseUrl:s,fetchImpl:o,token:a},r,e,t);if(l===void 0)return{body:void 0,code:1,inserted:0};const{mapping:d,storageIdMap:u,transferredPaths:h}=l,g=new Set,p=d?.storageColumns,w={ambiguous:[],rewritten:0,unmigrated:[]};e.logger.info(r.kind==="convex"?`POST ${i} -> import Convex export ${e.file} (${String(r.tables.length)} tables)`:`POST ${i} -> import ${e.file}`);const V=new Map,Pe=fn(r,e,u!==void 0,V),L=yt({batchSize:c,fetchImpl:o,maxBatchBytes:dn,requestUrl:i,token:a}),Ue=mn({report:w,source:r,storageColumns:p,storageIdMap:u,table:e.table,transferredPaths:h,unresolvedPaths:g}),H=await yn(Pe,Ue,L,e.logger),{conflicts:G,errors:W,inserted:Y,received:Fe,warnings:ze}=L.totals,Me=e.verify===!0&&H===void 0?Cr(e.logger,V,{conflicts:G,inserted:Y}):0,De=u!==void 0&&jr(e.logger,w,e.verify===!0),Be=Pr(e.logger,g,e.verify===!0),X=Object.values(Y).reduce((Z,Je)=>Z+Je,0),q=un(L.totals,u,w),K=H!==void 0||W.length>0||Me>0||De||Be;return e.logger.info(JSON.stringify(q,void 0,2)),$n(e.logger,{conflicts:G,errorCount:W.length,failed:K,insertedTotal:X,received:Fe,warnings:ze}),{body:q,code:K?1:0,inserted:X}};export{cn as D,Un as I,Le as M,O as b,B as l,Fn as r,ke as u};
12
+ `)}s.length>0&&await i(s),await r.flush();return}catch(a){return n.error(`import failed part-way through: ${a instanceof Error?a.message:String(a)}`),n.error("the rows below had already been written — re-run the same command to resume (existing rows conflict rather than duplicate)"),a}},bn=async(e,t,r,n)=>{const s=await _r(r,n),o=t.tables.find(a=>a.table===N);if(o===void 0){n.error("--with-storage requires a Convex export with a `_storage` metadata table — re-export with `npx convex export --include-file-storage`.");return}const i=await $r(e,t.snapshot,o,s?.keyPrefix??"",n);return n.info(`storage map: ${String(i.size)} blobs mapped`),{mapping:s,storageIdMap:i}},wn=async(e,t,r,n)=>{if(r.withStorage!==!0)return{};if(t.kind==="convex")return bn(e,t,n,r.logger);if(t.kind!=="supabase"&&t.kind!=="firebase")return{};try{const s=await gn(e,t,r,n);return s===void 0?void 0:{transferredPaths:s}}catch{r.logger.error("no rows were imported — fix the transfer and re-run; it will resume where it stopped");return}},$n=(e,t)=>{for(const s of t.warnings)e.warn(s);const r=t.received-t.insertedTotal-t.conflicts-t.errorCount;r>0&&e.warn(`${String(r)} of ${String(t.received)} rows were neither inserted, conflicted, nor reported as errors`);const n=`imported ${String(t.insertedTotal)} of ${String(t.received)} rows (${String(t.conflicts)} conflicts, ${String(t.errorCount)} errors)`;t.failed?e.error(n):e.success(n)},Fn=async e=>{const t=e.cwd??process.cwd(),r=await Ir(e,t);if(r.kind==="invalid")return{body:void 0,code:1,inserted:0};if(e.scan===!0)return{body:void 0,code:await hn(r,t,e.logger)===void 0?1:0,inserted:0};const n=await ln(e);if(n===void 0)return{body:void 0,code:1,inserted:0};const{baseUrl:s,fetchImpl:o,requestUrl:i,token:a}=n,c=e.batchSize??cn,d=await wn({baseUrl:s,fetchImpl:o,token:a},r,e,t);if(d===void 0)return{body:void 0,code:1,inserted:0};const{mapping:l,storageIdMap:u,transferredPaths:h}=d,g=new Set,p=l?.storageColumns,w={ambiguous:[],rewritten:0,unmigrated:[]};e.logger.info(r.kind==="convex"?`POST ${i} -> import Convex export ${e.file} (${String(r.tables.length)} tables)`:`POST ${i} -> import ${e.file}`);const V=new Map,Pe=fn(r,e,u!==void 0,V),L=yt({batchSize:c,fetchImpl:o,maxBatchBytes:dn,requestUrl:i,token:a}),Ue=mn({report:w,source:r,storageColumns:p,storageIdMap:u,table:e.table,transferredPaths:h,unresolvedPaths:g}),H=await yn(Pe,Ue,L,e.logger),{conflicts:G,errors:W,inserted:Y,received:Fe,warnings:ze}=L.totals,Me=e.verify===!0&&H===void 0?Cr(e.logger,V,{conflicts:G,inserted:Y}):0,De=u!==void 0&&jr(e.logger,w,e.verify===!0),Be=Pr(e.logger,g,e.verify===!0),X=Object.values(Y).reduce((Z,Je)=>Z+Je,0),q=un(L.totals,u,w),K=H!==void 0||W.length>0||Me>0||De||Be;return e.logger.info(JSON.stringify(q,void 0,2)),$n(e.logger,{conflicts:G,errorCount:W.length,failed:K,insertedTotal:X,received:Fe,warnings:ze}),{body:q,code:K?1:0,inserted:X}};export{cn as D,Un as I,Le as M,O as b,B as l,Fn as r,ke as u};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lunora/cli",
3
- "version": "1.0.0-alpha.179",
3
+ "version": "1.0.0-alpha.180",
4
4
  "description": "The Lunora CLI: init, dev, deploy, codegen, migrate, seed, doctor, insights, logs, registry, and the rest of the project commands",
5
5
  "keywords": [
6
6
  "agent-skills",
@@ -52,16 +52,16 @@
52
52
  },
53
53
  "dependencies": {
54
54
  "@bomb.sh/tab": "0.0.22",
55
- "@lunora/advisor": "1.0.0-alpha.84",
56
- "@lunora/bindings": "1.0.0-alpha.32",
57
- "@lunora/codegen": "1.0.0-alpha.118",
58
- "@lunora/config": "1.0.0-alpha.149",
55
+ "@lunora/advisor": "1.0.0-alpha.85",
56
+ "@lunora/bindings": "1.0.0-alpha.33",
57
+ "@lunora/codegen": "1.0.0-alpha.119",
58
+ "@lunora/config": "1.0.0-alpha.150",
59
59
  "@lunora/container": "1.0.0-alpha.32",
60
- "@lunora/d1": "1.0.0-alpha.83",
60
+ "@lunora/d1": "1.0.0-alpha.84",
61
61
  "@lunora/errors": "1.0.0-alpha.22",
62
- "@lunora/mcp": "1.0.0-alpha.80",
63
- "@lunora/runtime": "1.0.0-alpha.68",
64
- "@lunora/seed": "1.0.0-alpha.77",
62
+ "@lunora/mcp": "1.0.0-alpha.81",
63
+ "@lunora/runtime": "1.0.0-alpha.69",
64
+ "@lunora/seed": "1.0.0-alpha.78",
65
65
  "@visulima/cerebro": "3.0.6",
66
66
  "@visulima/error": "6.1.0",
67
67
  "@visulima/fs": "6.0.0",