@stacksjs/storage 0.75.451 → 0.75.452

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/README.md CHANGED
@@ -254,3 +254,8 @@ Memory storage preserves content and metadata when moving a file to its own
254
254
  normalized path. A missing source still throws. Aborted stream uploads cancel
255
255
  pending reads without publishing or replacing existing contents. All consumed
256
256
  stream readers release their locks on success, error and abort.
257
+
258
+ Recursive file deletion unlinks symlinks without following targets. File listings
259
+ skip root and descendant links. Symlink ancestors below cwd/temp anchors reject
260
+ before traversal; platform aliases above those anchors are accepted.
261
+ Config updates copy own enumerable fields and preserve special keys as data.
package/dist/files.d.ts CHANGED
@@ -31,7 +31,9 @@ export declare function doesNotExist(path: string): boolean;
31
31
  export declare function hasFiles(folder: string): boolean;
32
32
  export declare function hasComponents(): boolean;
33
33
  export declare function hasFunctions(): boolean;
34
+ /** Delete files and link entries, never the contents of a symlink target. */
34
35
  export declare function deleteFiles(dir: string, exclude?: string[]): void;
36
+ /** List ordinary files without following root or descendant symlink entries. */
35
37
  export declare function getFiles(dir: string, exclude?: string[]): string[];
36
38
  export declare function put(path: string, contents: string): void;
37
39
  export declare function get(path: string): Promise<string>;
package/dist/files.js CHANGED
@@ -1 +1 @@
1
- import{contains}from"@stacksjs/arrays";import{log}from"@stacksjs/logging";import{dirname,join,path as p}from"@stacksjs/path";import{detectIndent,detectNewline}from"@stacksjs/strings";import{createFolder,isFolder}from"./folders";import{existsSync,fs}from"./fs";export async function readJsonFile(name,cwd){const file=await readTextFile(name,cwd);let data;try{data=JSON.parse(file.data)}catch(error){throw Error(`Failed to parse JSON file "${name}": ${error.message}`)}const indent=detectIndent(file.data).indent,newline=detectNewline(file.data);return{...file,data,indent,newline}}export async function readPackageJson(name,cwd){return(await readJsonFile(name,cwd)).data}export async function writeFile(path,data){if(typeof path==="string"){const dirPath=dirname(path);if(!await existsSync(dirPath))await createFolder(dirPath);return await Bun.write(Bun.file(path),data)}return await Bun.write(path,data)}export async function writeJsonFile(file){let json=JSON.stringify(file.data,void 0,file.indent);if(file.newline)json+=file.newline;return writeTextFile({...file,data:json})}export function readTextFile(name,cwd){return new Promise((resolve,reject)=>{let filePath;if(cwd)filePath=join(cwd,name);else filePath=name;fs.readFile(filePath,"utf8",(err,text)=>{if(err)reject(err);else resolve({path:filePath,data:text})})})}export async function writeTextFile(file){return await Bun.write(file.path,file.data)}function isFile(path){return fs.existsSync(path)}export function doesExist(path){return isFile(path)||isFolder(path)}export function doesNotExist(path){return!isFile(path)&&!isFolder(path)}export function hasFiles(folder){try{return fs.readdirSync(folder).length>0}catch(err){log.debug(`Error reading folder: ${folder}`,err);return!1}}export function hasComponents(){return hasFiles(p.componentsPath())}export function hasFunctions(){return hasFiles(p.functionsPath())}export function deleteFiles(dir,exclude=[]){if(fs.existsSync(dir))fs.readdirSync(dir).forEach((file)=>{const p=join(dir,file);if(fs.statSync(p).isDirectory())if(fs.readdirSync(p).length===0)fs.rmSync(p,{recursive:!0,force:!0});else deleteFiles(p,exclude);else if(!contains(p,exclude))fs.rmSync(p)})}export function getFiles(dir,exclude=[]){let results=[];fs.readdirSync(dir).forEach((file)=>{file=join(dir,file);if(fs.statSync(file).isDirectory())results=results.concat(getFiles(file,exclude));else if(!contains(file,exclude))results.push(file)});return results}export function put(path,contents){const dirPath=dirname(path);if(!fs.existsSync(dirPath))fs.mkdirSync(dirPath,{recursive:!0});fs.writeFileSync(path,contents,"utf-8")}export async function get(path){return Bun.file(path).text()}export const files={readJsonFile,readPackageJson,readTextFile,writeJsonFile,writeTextFile,hasFiles,hasComponents,hasFunctions,deleteFiles,getFiles,put,get};
1
+ import{contains}from"@stacksjs/arrays";import{log}from"@stacksjs/logging";import{path as p}from"@stacksjs/path";import{dirname,join,resolve,relative,isAbsolute,parse,sep}from"node:path";import{tmpdir}from"node:os";import process from"node:process";import{detectIndent,detectNewline}from"@stacksjs/strings";import{createFolder,isFolder}from"./folders";import{existsSync,fs}from"./fs";export async function readJsonFile(name,cwd){const file=await readTextFile(name,cwd);let data;try{data=JSON.parse(file.data)}catch(error){throw Error(`Failed to parse JSON file "${name}": ${error.message}`)}const indent=detectIndent(file.data).indent,newline=detectNewline(file.data);return{...file,data,indent,newline}}export async function readPackageJson(name,cwd){return(await readJsonFile(name,cwd)).data}export async function writeFile(path,data){if(typeof path==="string"){const dirPath=dirname(path);if(!await existsSync(dirPath))await createFolder(dirPath);return await Bun.write(Bun.file(path),data)}return await Bun.write(path,data)}export async function writeJsonFile(file){let json=JSON.stringify(file.data,void 0,file.indent);if(file.newline)json+=file.newline;return writeTextFile({...file,data:json})}export function readTextFile(name,cwd){return new Promise((resolve,reject)=>{let filePath;if(cwd)filePath=join(cwd,name);else filePath=name;fs.readFile(filePath,"utf8",(err,text)=>{if(err)reject(err);else resolve({path:filePath,data:text})})})}export async function writeTextFile(file){return await Bun.write(file.path,file.data)}function isFile(path){return fs.existsSync(path)}export function doesExist(path){return isFile(path)||isFolder(path)}export function doesNotExist(path){return!isFile(path)&&!isFolder(path)}export function hasFiles(folder){try{return fs.readdirSync(folder).length>0}catch(err){log.debug(`Error reading folder: ${folder}`,err);return!1}}export function hasComponents(){return hasFiles(p.componentsPath())}export function hasFunctions(){return hasFiles(p.functionsPath())}function traversalRoot(directory){const root=resolve(directory),candidates=[process.cwd(),tmpdir(),...process.platform==="win32"?[]:["/tmp"]].flatMap((anchor)=>{try{return[resolve(anchor),fs.realpathSync(anchor)]}catch{return[resolve(anchor)]}}),contained=(anchor)=>{const part=relative(anchor,root);return part===""||!isAbsolute(part)&&part!==".."&&!part.startsWith(`..${sep}`)},anchor=candidates.filter(contained).sort((a,b)=>b.length-a.length)[0]??parse(root).root;let current=anchor;for(const part of root===anchor?[]:relative(anchor,dirname(root)).split(sep).filter(Boolean)){current=join(current,part);if(fs.lstatSync(current).isSymbolicLink())throw Error(`Cannot traverse a symlink ancestor: ${current}`)}return root}export function deleteFiles(dir,exclude=[]){if(contains(dir,exclude))return;let root;try{root=traversalRoot(dir)}catch(error){if(error.code==="ENOENT")return;throw error}const walk=(path,display)=>{if(contains(display,exclude))return;let stat;try{stat=fs.lstatSync(path)}catch(error){if(error.code==="ENOENT")return;throw error}if(!stat.isDirectory()){fs.unlinkSync(path);return}for(const name of fs.readdirSync(path)){const entry=join(path,name),shown=join(display,name);if(contains(shown,exclude))continue;if(fs.lstatSync(entry).isDirectory()&&fs.readdirSync(entry).length===0)fs.rmdirSync(entry);else walk(entry,shown)}};walk(root,dir)}export function getFiles(dir,exclude=[]){const root=traversalRoot(dir);if(fs.lstatSync(root).isSymbolicLink()||contains(dir,exclude))return[];const results=[],walk=(path,display)=>{for(const name of fs.readdirSync(path)){const file=join(path,name),shown=join(display,name);if(contains(shown,exclude))continue;const stat=fs.lstatSync(file);if(stat.isSymbolicLink())continue;if(stat.isDirectory())walk(file,shown);else if(stat.isFile())results.push(shown)}};walk(root,dir);return results}export function put(path,contents){const dirPath=dirname(path);if(!fs.existsSync(dirPath))fs.mkdirSync(dirPath,{recursive:!0});fs.writeFileSync(path,contents,"utf-8")}export async function get(path){return Bun.file(path).text()}export const files={readJsonFile,readPackageJson,readTextFile,writeJsonFile,writeTextFile,hasFiles,hasComponents,hasFunctions,deleteFiles,getFiles,put,get};
package/dist/helpers.js CHANGED
@@ -1 +1 @@
1
- var __dirname="";import{fileURLToPath}from"node:url";import{dirname}from"@stacksjs/path";import{fs}from"./fs";export const _dirname=typeof __dirname<"u"?__dirname:dirname(fileURLToPath(import.meta.url));export function updateConfigFile(filePath,newConfig){return new Promise((resolve,reject)=>{let config;try{config=JSON.parse(fs.readFileSync(filePath,"utf8"))}catch(error){reject(Error(`Failed to parse config file "${filePath}": ${error.message}`));return}for(const key in newConfig)config[key]=newConfig[key];try{fs.writeFileSync(filePath,JSON.stringify(config,null,2));resolve()}catch(error){reject(error)}})}export const helpers={_dirname,updateConfigFile};
1
+ var __dirname="";import{fileURLToPath}from"node:url";import{dirname}from"@stacksjs/path";import{fs}from"./fs";export const _dirname=typeof __dirname<"u"?__dirname:dirname(fileURLToPath(import.meta.url));export function updateConfigFile(filePath,newConfig){return new Promise((resolve,reject)=>{let config;try{config=JSON.parse(fs.readFileSync(filePath,"utf8"))}catch(error){reject(Error(`Failed to parse config file "${filePath}": ${error.message}`));return}for(const[key,value]of Object.entries(newConfig))Object.defineProperty(config,key,{value,enumerable:!0,writable:!0,configurable:!0});try{fs.writeFileSync(filePath,JSON.stringify(config,null,2));resolve()}catch(error){reject(error)}})}export const helpers={_dirname,updateConfigFile};
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@stacksjs/storage",
3
3
  "type": "module",
4
4
  "sideEffects": false,
5
- "version": "0.75.451",
5
+ "version": "0.75.452",
6
6
  "description": "The Stacks file system.",
7
7
  "author": "Chris Breuer",
8
8
  "contributors": [
@@ -61,20 +61,20 @@
61
61
  "prepublishOnly": "bun run build"
62
62
  },
63
63
  "dependencies": {
64
- "@stacksjs/arrays": "0.75.451",
65
- "@stacksjs/cli": "0.75.451",
66
- "@stacksjs/config": "0.75.451",
67
- "@stacksjs/env": "0.75.451",
68
- "@stacksjs/error-handling": "0.75.451",
69
- "@stacksjs/logging": "0.75.451",
70
- "@stacksjs/path": "0.75.451",
71
- "@stacksjs/strings": "0.75.451",
64
+ "@stacksjs/arrays": "0.75.452",
65
+ "@stacksjs/cli": "0.75.452",
66
+ "@stacksjs/config": "0.75.452",
67
+ "@stacksjs/env": "0.75.452",
68
+ "@stacksjs/error-handling": "0.75.452",
69
+ "@stacksjs/logging": "0.75.452",
70
+ "@stacksjs/path": "0.75.452",
71
+ "@stacksjs/strings": "0.75.452",
72
72
  "@stacksjs/ts-cloud": "^0.16.91",
73
73
  "@stacksjs/ts-xml": "^0.1.0",
74
74
  "ts-images": "^0.2.23"
75
75
  },
76
76
  "devDependencies": {
77
- "@stacksjs/types": "0.75.451",
77
+ "@stacksjs/types": "0.75.452",
78
78
  "better-dx": "^0.2.24"
79
79
  }
80
80
  }