@stacksjs/desktop-build 0.75.661 → 0.75.663

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.
@@ -9,12 +9,25 @@ export declare function desktopAgentPaths(appName: string, home?: string): Deskt
9
9
  /** Take the lock, or return false when another live copy of the app holds it. A lock left by a crash is taken over. */
10
10
  export declare function acquireInstanceLock(lockFile: string, pid?: number): boolean;
11
11
  export declare function releaseInstanceLock(lockFile: string, pid?: number): void;
12
+ /**
13
+ * `text` with every `session=` query value replaced.
14
+ *
15
+ * The window's start URL carries the session token, and that token is the
16
+ * session cookie itself, so a log line holding it is a credential to the
17
+ * app's local agent for as long as the app runs (stacksjs/stacks#3371).
18
+ */
19
+ export declare function redactSessionTokens(text: string): string;
12
20
  /**
13
21
  * Launched from Finder there is no terminal, and what the app prints would go
14
22
  * nowhere: send it to a log file instead. From a terminal the developer keeps
15
23
  * stdout and stderr. Returns whether it redirected.
24
+ *
25
+ * The file is readable by its owner only, holds no session token, and is
26
+ * rotated to `<file>.1` once it passes `maxBytes` (5 MB), keeping one previous
27
+ * file. It used to be world-readable, unbounded, and to record the window's
28
+ * `?session=` URL as Craft echoed it (stacksjs/stacks#3371).
16
29
  */
17
- export declare function logToFileWhenDetached(logFile: string, isTTY?: boolean): boolean;
30
+ export declare function logToFileWhenDetached(logFile: string, isTTY?: boolean, options?: { maxBytes?: number }): boolean;
18
31
  /** The manifest next to a bundled executable (`Contents/Resources`), or the one `STACKS_DESKTOP_AGENT_MANIFEST` names. */
19
32
  export declare function findAgentManifest(execPath?: string, env?: NodeJS.ProcessEnv): string | null;
20
33
  export declare function readAgentManifest(file: string): DesktopAgentManifest;
@@ -1,5 +1,5 @@
1
- import{appendFileSync,existsSync,mkdirSync,readFileSync,rmSync,writeFileSync}from"node:fs";import{homedir}from"node:os";import{dirname,isAbsolute,join,resolve}from"node:path";import process from"node:process";import{fileURLToPath}from"node:url";import{format}from"node:util";import{startDesktopAgent}from"./agent";export const AGENT_MANIFEST="desktop-agent.json";export const AGENT_MANIFEST_ENV="STACKS_DESKTOP_AGENT_MANIFEST";export const AGENT_DATA_DIR_ENV="STACKS_DESKTOP_DATA_DIR";export const AGENT_RESOURCES_ENV="STACKS_DESKTOP_RESOURCES";export function desktopResource(name,env=process.env){try{const resources=JSON.parse(env[AGENT_RESOURCES_ENV]??"{}");return typeof resources[name]==="string"?resources[name]:undefined}catch{return}}export function desktopAgentPaths(appName,home=homedir()){const dataDir=join(home,"Library","Application Support",appName);return{dataDir,portFile:join(dataDir,"agent.port"),lockFile:join(dataDir,"agent.pid"),logFile:join(home,"Library","Logs",`${appName}.log`)}}export function acquireInstanceLock(lockFile,pid=process.pid){mkdirSync(dirname(lockFile),{recursive:true});if(existsSync(lockFile)){const holder=Number(readFileSync(lockFile,"utf8").trim());if(holder&&holder!==pid){try{process.kill(holder,0);return false}catch(error){if(error.code==="EPERM")return false}}}writeFileSync(lockFile,String(pid));return true}export function releaseInstanceLock(lockFile,pid=process.pid){try{if(readFileSync(lockFile,"utf8").trim()===String(pid))rmSync(lockFile)}catch{}}export function logToFileWhenDetached(logFile,isTTY=Boolean(process.stdout.isTTY)){if(isTTY)return false;mkdirSync(dirname(logFile),{recursive:true});for(const level of["log","info","warn","error","debug"]){console[level]=(...args)=>{try{appendFileSync(logFile,`${new Date().toISOString()} ${level==="log"?"":`[${level}] `}${format(...args)}
2
- `)}catch{}}}return true}export function findAgentManifest(execPath=process.execPath,env=process.env){if(env[AGENT_MANIFEST_ENV])return resolve(env[AGENT_MANIFEST_ENV]);const bundled=join(dirname(execPath),"..","Resources",AGENT_MANIFEST);return existsSync(bundled)?bundled:null}export function readAgentManifest(file){const manifest=JSON.parse(readFileSync(file,"utf8"));const base=dirname(file);const at=(path)=>isAbsolute(path)?path:resolve(base,path);return{...manifest,host:at(manifest.host),nativeBundle:at(manifest.nativeBundle),...manifest.staticDir?{staticDir:at(manifest.staticDir)}:{},...manifest.resources?{resources:Object.fromEntries(Object.entries(manifest.resources).map(([name,path])=>[name,at(path)]))}:{}}}export function agentHostCommand(manifest,agentUrl){return[manifest.host,"--api-url",agentUrl,"--native-bundle",manifest.nativeBundle,...manifest.hostArguments]}export async function stacksAgentHandler(options){try{const{overridesReady}=await import("@stacksjs/config");await overridesReady}catch(error){console.warn(`[agent] config not loaded: ${error instanceof Error?error.message:String(error)}`)}if(options.appRoutes){try{const{injectGlobalAutoImports}=await import("@stacksjs/server");await injectGlobalAutoImports()}catch(error){console.warn(`[agent] models and framework globals not injected: ${error instanceof Error?error.message:String(error)}`)}}const{provideActions,provideMiddleware,route,serverResponse}=await import("@stacksjs/router");if(options.actions)provideActions(options.actions);if(options.middleware)provideMiddleware(options.middleware);const module=options.entry?await options.entry():undefined;if(typeof module?.default==="function")return module.default;if(options.appRoutes){const fromProject=(request)=>serverResponse(request);return fromProject}for(const file of options.routes??[]){if(file.prefix||file.middleware?.length)await route.group({prefix:file.prefix,middleware:file.middleware?.length?file.middleware:undefined},async()=>{await file.load()});else await file.load()}const bundled=(request)=>route.handleRequest(request);return bundled}export function renderAgentEntry(options={}){const launcher=options.launcherModule??fileURLToPath(import.meta.url);const entry=options.agentEntry?`() => import(${JSON.stringify(options.agentEntry)})`:"undefined";const routes=options.routes?.length?`[
1
+ import{appendFileSync,chmodSync,existsSync,mkdirSync,readFileSync,renameSync,rmSync,statSync,writeFileSync}from"node:fs";import{homedir}from"node:os";import{dirname,isAbsolute,join,resolve}from"node:path";import process from"node:process";import{fileURLToPath}from"node:url";import{format}from"node:util";import{startDesktopAgent}from"./agent";export const AGENT_MANIFEST="desktop-agent.json";export const AGENT_MANIFEST_ENV="STACKS_DESKTOP_AGENT_MANIFEST";export const AGENT_DATA_DIR_ENV="STACKS_DESKTOP_DATA_DIR";export const AGENT_RESOURCES_ENV="STACKS_DESKTOP_RESOURCES";export function desktopResource(name,env=process.env){try{const resources=JSON.parse(env[AGENT_RESOURCES_ENV]??"{}");return typeof resources[name]==="string"?resources[name]:undefined}catch{return}}export function desktopAgentPaths(appName,home=homedir()){const dataDir=join(home,"Library","Application Support",appName);return{dataDir,portFile:join(dataDir,"agent.port"),lockFile:join(dataDir,"agent.pid"),logFile:join(home,"Library","Logs",`${appName}.log`)}}export function acquireInstanceLock(lockFile,pid=process.pid){mkdirSync(dirname(lockFile),{recursive:true});if(existsSync(lockFile)){const holder=Number(readFileSync(lockFile,"utf8").trim());if(holder&&holder!==pid){try{process.kill(holder,0);return false}catch(error){if(error.code==="EPERM")return false}}}writeFileSync(lockFile,String(pid));return true}export function releaseInstanceLock(lockFile,pid=process.pid){try{if(readFileSync(lockFile,"utf8").trim()===String(pid))rmSync(lockFile)}catch{}}const DETACHED_LOG_MAX_BYTES=5*1024*1024;export function redactSessionTokens(text){return text.replace(/([?&]session=)[^&#\s"'`]+/g,"$1[redacted]")}export function logToFileWhenDetached(logFile,isTTY=Boolean(process.stdout.isTTY),options={}){if(isTTY)return false;const maxBytes=options.maxBytes??DETACHED_LOG_MAX_BYTES;mkdirSync(dirname(logFile),{recursive:true,mode:448});let size=0;try{size=statSync(logFile).size;chmodSync(logFile,384)}catch{}const rotate=()=>{try{renameSync(logFile,`${logFile}.1`)}catch{}size=0};if(size>maxBytes)rotate();for(const level of["log","info","warn","error","debug"]){console[level]=(...args)=>{try{const line=redactSessionTokens(`${new Date().toISOString()} ${level==="log"?"":`[${level}] `}${format(...args)}
2
+ `);const bytes=Buffer.byteLength(line);if(size>0&&size+bytes>maxBytes)rotate();appendFileSync(logFile,line,{mode:384});size+=bytes}catch{}}}return true}export function findAgentManifest(execPath=process.execPath,env=process.env){if(env[AGENT_MANIFEST_ENV])return resolve(env[AGENT_MANIFEST_ENV]);const bundled=join(dirname(execPath),"..","Resources",AGENT_MANIFEST);return existsSync(bundled)?bundled:null}export function readAgentManifest(file){const manifest=JSON.parse(readFileSync(file,"utf8"));const base=dirname(file);const at=(path)=>isAbsolute(path)?path:resolve(base,path);return{...manifest,host:at(manifest.host),nativeBundle:at(manifest.nativeBundle),...manifest.staticDir?{staticDir:at(manifest.staticDir)}:{},...manifest.resources?{resources:Object.fromEntries(Object.entries(manifest.resources).map(([name,path])=>[name,at(path)]))}:{}}}export function agentHostCommand(manifest,agentUrl){return[manifest.host,"--api-url",agentUrl,"--native-bundle",manifest.nativeBundle,...manifest.hostArguments]}export async function stacksAgentHandler(options){try{const{overridesReady}=await import("@stacksjs/config");await overridesReady}catch(error){console.warn(`[agent] config not loaded: ${error instanceof Error?error.message:String(error)}`)}if(options.appRoutes){try{const{injectGlobalAutoImports}=await import("@stacksjs/server");await injectGlobalAutoImports()}catch(error){console.warn(`[agent] models and framework globals not injected: ${error instanceof Error?error.message:String(error)}`)}}const{provideActions,provideMiddleware,route,serverResponse}=await import("@stacksjs/router");if(options.actions)provideActions(options.actions);if(options.middleware)provideMiddleware(options.middleware);const module=options.entry?await options.entry():undefined;if(typeof module?.default==="function")return module.default;if(options.appRoutes){const fromProject=(request)=>serverResponse(request);return fromProject}for(const file of options.routes??[]){if(file.prefix||file.middleware?.length)await route.group({prefix:file.prefix,middleware:file.middleware?.length?file.middleware:undefined},async()=>{await file.load()});else await file.load()}const bundled=(request)=>route.handleRequest(request);return bundled}export function renderAgentEntry(options={}){const launcher=options.launcherModule??fileURLToPath(import.meta.url);const entry=options.agentEntry?`() => import(${JSON.stringify(options.agentEntry)})`:"undefined";const routes=options.routes?.length?`[
3
3
  ${options.routes.map((file)=>` { load: () => import(${JSON.stringify(file.file)})${file.prefix?`, prefix: ${JSON.stringify(file.prefix)}`:""}${file.middleware?.length?`, middleware: ${JSON.stringify(file.middleware)}`:""} },`).join(`
4
4
  `)}
5
5
  ]`:"[]";const actionEntries=Object.entries(options.actions??{});const actions=actionEntries.length?`{
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@stacksjs/desktop-build",
3
3
  "type": "module",
4
4
  "sideEffects": false,
5
- "version": "0.75.661",
5
+ "version": "0.75.663",
6
6
  "description": "Build and launch Craft desktop applications for Stacks",
7
7
  "author": "Chris Breuer",
8
8
  "contributors": [
@@ -73,17 +73,17 @@
73
73
  "prepublishOnly": "bun run build"
74
74
  },
75
75
  "dependencies": {
76
- "@stacksjs/config": "0.75.661",
77
- "@stacksjs/router": "0.75.661",
78
- "@stacksjs/server": "0.75.661",
79
- "@stacksjs/storage": "0.75.661",
76
+ "@stacksjs/config": "0.75.663",
77
+ "@stacksjs/router": "0.75.663",
78
+ "@stacksjs/server": "0.75.663",
79
+ "@stacksjs/storage": "0.75.663",
80
80
  "craft-native": ">=0.0.505"
81
81
  },
82
82
  "peerDependencies": {
83
- "@stacksjs/config": "0.75.661",
84
- "@stacksjs/router": "0.75.661",
85
- "@stacksjs/server": "0.75.661",
86
- "@stacksjs/storage": "0.75.661"
83
+ "@stacksjs/config": "0.75.663",
84
+ "@stacksjs/router": "0.75.663",
85
+ "@stacksjs/server": "0.75.663",
86
+ "@stacksjs/storage": "0.75.663"
87
87
  },
88
88
  "peerDependenciesMeta": {
89
89
  "@stacksjs/config": {
@@ -100,7 +100,7 @@
100
100
  }
101
101
  },
102
102
  "devDependencies": {
103
- "@stacksjs/build": "0.75.661",
103
+ "@stacksjs/build": "0.75.663",
104
104
  "better-dx": "^0.2.39"
105
105
  }
106
106
  }