@stacksjs/storage 0.75.445 → 0.75.450

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
@@ -249,3 +249,8 @@ For casual chit-chat with others using this package:
249
249
  The MIT License (MIT). Please see [LICENSE](https://github.com/stacksjs/stacks/tree/main/LICENSE.md) for more information.
250
250
 
251
251
  Made with 💙
252
+
253
+ Memory storage preserves content and metadata when moving a file to its own
254
+ normalized path. A missing source still throws. Aborted stream uploads cancel
255
+ pending reads without publishing or replacing existing contents. All consumed
256
+ stream readers release their locks on success, error and abort.
@@ -1 +1 @@
1
- import{Buffer}from"node:buffer";import{mimeFromExtension}from"../mime-from-extension";import{createDirectoryListing,normalizeExpiryToDate}from"../types";export class InMemoryStorageAdapter{files;directories;constructor(){this.files=new Map;this.directories=new Set;this.directories.add("")}normalizePath(path){return path.replace(/^\/+/,"").replace(/\/+$/,"")}getDirectoryPath(path){const parts=path.split("/").filter(Boolean);parts.pop();return parts.join("/")}async contentsToUint8Array(contents){if(typeof contents==="string")return new TextEncoder().encode(contents);else if(contents instanceof Buffer)return new Uint8Array(contents);else if(contents instanceof Uint8Array)return new Uint8Array(contents);else{if(typeof contents.getReader!=="function")throw TypeError("[storage/memory] contents must be a web-standard ReadableStream (with .getReader()), not a Node stream.Readable. Convert via Readable.toWeb(nodeStream) before passing.");const reader=contents.getReader(),chunks=[];while(!0){const{done,value}=await reader.read();if(done)break;if(value)chunks.push(value)}const totalLength=chunks.reduce((sum,chunk)=>sum+chunk.length,0),result=new Uint8Array(totalLength);let offset=0;for(const chunk of chunks){result.set(chunk,offset);offset+=chunk.length}return result}}async write(path,contents){const normalized=this.normalizePath(path),dirPath=this.getDirectoryPath(normalized);if(dirPath)await this.createDirectory(dirPath);const data=await this.contentsToUint8Array(contents),lastModified=Date.now(),mimeType=this.detectMimeType(normalized);this.files.set(normalized,{contents:data,visibility:"private",mimeType,lastModified});return{path:normalized,size:data.length,contentType:mimeType,lastModified}}async read(path){const normalized=this.normalizePath(path),file=this.files.get(normalized);if(!file)throw Error(`File not found: ${path}`);return new Uint8Array(file.contents)}async getStream(path,_options){const normalized=this.normalizePath(path),file=this.files.get(normalized);if(!file)throw Error(`File not found: ${path}`);const bytes=new Uint8Array(file.contents);return new ReadableStream({start(controller){controller.enqueue(bytes);controller.close()}})}async putStream(path,stream,options){const normalized=this.normalizePath(path),dirPath=this.getDirectoryPath(normalized);if(dirPath)await this.createDirectory(dirPath);const chunks=[],reader=stream.getReader(),abort=options?.signal;try{while(!0){if(abort?.aborted)throw Error("aborted");const{done,value}=await reader.read();if(done)break;if(value)chunks.push(value)}}finally{try{reader.releaseLock()}catch{}}const totalLength=chunks.reduce((sum,c)=>sum+c.length,0),data=new Uint8Array(totalLength);let offset=0;for(const c of chunks){data.set(c,offset);offset+=c.length}const lastModified=Date.now(),mimeType=options?.contentType??this.detectMimeType(normalized);this.files.set(normalized,{contents:data,visibility:"private",mimeType,lastModified});return{path:normalized,size:data.length,contentType:mimeType,lastModified}}async readToString(path){const data=await this.read(path);return new TextDecoder().decode(data)}async readToBuffer(path){const data=await this.read(path);return Buffer.from(data)}async readToUint8Array(path){return await this.read(path)}async deleteFile(path){this.files.delete(this.normalizePath(path))}async deleteDirectory(path){const normalized=this.normalizePath(path),prefix=normalized?`${normalized}/`:"";for(const filePath of this.files.keys())if(filePath===normalized||filePath.startsWith(prefix))this.files.delete(filePath);for(const dir of this.directories)if(dir===normalized||dir.startsWith(prefix))this.directories.delete(dir)}async createDirectory(path){const normalized=this.normalizePath(path);if(!normalized)return;const parts=normalized.split("/").filter(Boolean);let current="";for(const part of parts){current=current?`${current}/${part}`:part;this.directories.add(current)}}async moveFile(from,to){const normalizedFrom=this.normalizePath(from),normalizedTo=this.normalizePath(to),file=this.files.get(normalizedFrom);if(!file)throw Error(`File not found: ${from}`);const toDir=this.getDirectoryPath(normalizedTo);if(toDir)await this.createDirectory(toDir);this.files.set(normalizedTo,{...file,lastModified:Date.now()});this.files.delete(normalizedFrom)}async copyFile(from,to){const normalizedFrom=this.normalizePath(from),normalizedTo=this.normalizePath(to),file=this.files.get(normalizedFrom);if(!file)throw Error(`File not found: ${from}`);const toDir=this.getDirectoryPath(normalizedTo);if(toDir)await this.createDirectory(toDir);const contentsCopy=new Uint8Array(file.contents);this.files.set(normalizedTo,{...file,contents:contentsCopy,lastModified:Date.now()})}async stat(path){const normalized=this.normalizePath(path),file=this.files.get(normalized);if(file)return{path:normalized,type:"file",visibility:file.visibility,size:file.contents.length,lastModified:file.lastModified,mimeType:file.mimeType};if(this.directories.has(normalized)||normalized==="")return{path:normalized,type:"directory",visibility:"private",size:0,lastModified:Date.now()};throw Error(`Path not found: ${path}`)}list(path,options={}){return this.createAsyncIterator(path,options.deep||!1)}async*createAsyncIterator(path,deep){const normalized=this.normalizePath(path),prefix=normalized?`${normalized}/`:"",entries=[],seen=new Set;for(const[filePath]of this.files)if(filePath.startsWith(prefix)||prefix===""){const relativePath=prefix?filePath.slice(prefix.length):filePath;if(!deep){if(relativePath.split("/").filter(Boolean).length===1)entries.push({path:filePath,type:"file"})}else entries.push({path:filePath,type:"file"})}for(const dir of this.directories)if((dir.startsWith(prefix)||prefix==="")&&dir!==normalized){const relativePath=prefix?dir.slice(prefix.length):dir;if(!deep){if(relativePath.split("/").filter(Boolean).length===1&&!seen.has(dir)){entries.push({path:dir,type:"directory"});seen.add(dir)}}else if(!seen.has(dir)){entries.push({path:dir,type:"directory"});seen.add(dir)}}yield*createDirectoryListing(entries)}async changeVisibility(path,visibility){const normalized=this.normalizePath(path),file=this.files.get(normalized);if(!file)throw Error(`File not found: ${path}`);file.visibility=visibility}async visibility(path){const normalized=this.normalizePath(path),file=this.files.get(normalized);if(!file)throw Error(`File not found: ${path}`);return file.visibility}async fileExists(path){const normalized=this.normalizePath(path);return this.files.has(normalized)}async directoryExists(path){const normalized=this.normalizePath(path);return this.directories.has(normalized)||normalized===""}async publicUrl(path,options={}){return`${(options.domain||process.env.APP_URL||"http://localhost").replace(/\/$/,"")}/${this.normalizePath(path)}`}async temporaryUrl(path,options){const expiry=normalizeExpiryToDate(options.expiresIn);return`http://localhost/temp/${Buffer.from(`${path}:${expiry.getTime()}`).toString("base64url")}`}async signedUrl(_path,_options){throw Error("[storage/memory] signedUrl is not supported on the in-memory adapter - switch to local or s3 disk for signed URL generation.")}async checksum(path,options={}){const algorithm=options.algorithm||"sha256",data=await this.readToUint8Array(path),hasher=new Bun.CryptoHasher(algorithm);hasher.update(data);return hasher.digest("hex")}async mimeType(path,_options={}){const normalized=this.normalizePath(path),file=this.files.get(normalized);if(!file)throw Error(`File not found: ${path}`);return file.mimeType}detectMimeType(path){return mimeFromExtension(path)}async lastModified(path){return(await this.stat(path)).lastModified}async fileSize(path){return(await this.stat(path)).size}clear(){this.files.clear();this.directories.clear();this.directories.add("")}}export function createMemoryStorage(){return new InMemoryStorageAdapter}
1
+ import{Buffer}from"node:buffer";import{mimeFromExtension}from"../mime-from-extension";import{createDirectoryListing,normalizeExpiryToDate}from"../types";export class InMemoryStorageAdapter{files;directories;constructor(){this.files=new Map;this.directories=new Set;this.directories.add("")}normalizePath(path){return path.replace(/^\/+/,"").replace(/\/+$/,"")}getDirectoryPath(path){const parts=path.split("/").filter(Boolean);parts.pop();return parts.join("/")}async contentsToUint8Array(contents,signal){signal?.throwIfAborted();if(typeof contents==="string")return new TextEncoder().encode(contents);else if(contents instanceof Buffer)return new Uint8Array(contents);else if(contents instanceof Uint8Array)return new Uint8Array(contents);else{if(typeof contents.getReader!=="function")throw TypeError("[storage/memory] contents must be a web-standard ReadableStream (with .getReader()), not a Node stream.Readable. Convert via Readable.toWeb(nodeStream) before passing.");const reader=contents.getReader(),chunks=[],abort=()=>{reader.cancel(signal?.reason).catch(()=>{})};signal?.addEventListener("abort",abort,{once:!0});try{if(signal?.aborted)abort();while(!0){signal?.throwIfAborted();const{done,value}=await reader.read();signal?.throwIfAborted();if(done)break;if(value)chunks.push(new Uint8Array(value))}}finally{signal?.removeEventListener("abort",abort);reader.releaseLock()}const totalLength=chunks.reduce((sum,chunk)=>sum+chunk.length,0),result=new Uint8Array(totalLength);let offset=0;for(const chunk of chunks){result.set(chunk,offset);offset+=chunk.length}return result}}async write(path,contents){const normalized=this.normalizePath(path),dirPath=this.getDirectoryPath(normalized);if(dirPath)await this.createDirectory(dirPath);const data=await this.contentsToUint8Array(contents),lastModified=Date.now(),mimeType=this.detectMimeType(normalized);this.files.set(normalized,{contents:data,visibility:"private",mimeType,lastModified});return{path:normalized,size:data.length,contentType:mimeType,lastModified}}async read(path){const normalized=this.normalizePath(path),file=this.files.get(normalized);if(!file)throw Error(`File not found: ${path}`);return new Uint8Array(file.contents)}async getStream(path,_options){const normalized=this.normalizePath(path),file=this.files.get(normalized);if(!file)throw Error(`File not found: ${path}`);const bytes=new Uint8Array(file.contents);return new ReadableStream({start(controller){controller.enqueue(bytes);controller.close()}})}async putStream(path,stream,options){const normalized=this.normalizePath(path),dirPath=this.getDirectoryPath(normalized);if(dirPath)await this.createDirectory(dirPath);const data=await this.contentsToUint8Array(stream,options?.signal);options?.signal?.throwIfAborted();const lastModified=Date.now(),mimeType=options?.contentType??this.detectMimeType(normalized);this.files.set(normalized,{contents:data,visibility:"private",mimeType,lastModified});return{path:normalized,size:data.length,contentType:mimeType,lastModified}}async readToString(path){const data=await this.read(path);return new TextDecoder().decode(data)}async readToBuffer(path){const data=await this.read(path);return Buffer.from(data)}async readToUint8Array(path){return await this.read(path)}async deleteFile(path){this.files.delete(this.normalizePath(path))}async deleteDirectory(path){const normalized=this.normalizePath(path),prefix=normalized?`${normalized}/`:"";for(const filePath of this.files.keys())if(filePath===normalized||filePath.startsWith(prefix))this.files.delete(filePath);for(const dir of this.directories)if(dir===normalized||dir.startsWith(prefix))this.directories.delete(dir)}async createDirectory(path){const normalized=this.normalizePath(path);if(!normalized)return;const parts=normalized.split("/").filter(Boolean);let current="";for(const part of parts){current=current?`${current}/${part}`:part;this.directories.add(current)}}async moveFile(from,to){const normalizedFrom=this.normalizePath(from),normalizedTo=this.normalizePath(to),file=this.files.get(normalizedFrom);if(!file)throw Error(`File not found: ${from}`);if(normalizedFrom===normalizedTo)return;const toDir=this.getDirectoryPath(normalizedTo);if(toDir)await this.createDirectory(toDir);this.files.set(normalizedTo,{...file,lastModified:Date.now()});this.files.delete(normalizedFrom)}async copyFile(from,to){const normalizedFrom=this.normalizePath(from),normalizedTo=this.normalizePath(to),file=this.files.get(normalizedFrom);if(!file)throw Error(`File not found: ${from}`);const toDir=this.getDirectoryPath(normalizedTo);if(toDir)await this.createDirectory(toDir);const contentsCopy=new Uint8Array(file.contents);this.files.set(normalizedTo,{...file,contents:contentsCopy,lastModified:Date.now()})}async stat(path){const normalized=this.normalizePath(path),file=this.files.get(normalized);if(file)return{path:normalized,type:"file",visibility:file.visibility,size:file.contents.length,lastModified:file.lastModified,mimeType:file.mimeType};if(this.directories.has(normalized)||normalized==="")return{path:normalized,type:"directory",visibility:"private",size:0,lastModified:Date.now()};throw Error(`Path not found: ${path}`)}list(path,options={}){return this.createAsyncIterator(path,options.deep||!1)}async*createAsyncIterator(path,deep){const normalized=this.normalizePath(path),prefix=normalized?`${normalized}/`:"",entries=[],seen=new Set;for(const[filePath]of this.files)if(filePath.startsWith(prefix)||prefix===""){const relativePath=prefix?filePath.slice(prefix.length):filePath;if(!deep){if(relativePath.split("/").filter(Boolean).length===1)entries.push({path:filePath,type:"file"})}else entries.push({path:filePath,type:"file"})}for(const dir of this.directories)if((dir.startsWith(prefix)||prefix==="")&&dir!==normalized){const relativePath=prefix?dir.slice(prefix.length):dir;if(!deep){if(relativePath.split("/").filter(Boolean).length===1&&!seen.has(dir)){entries.push({path:dir,type:"directory"});seen.add(dir)}}else if(!seen.has(dir)){entries.push({path:dir,type:"directory"});seen.add(dir)}}yield*createDirectoryListing(entries)}async changeVisibility(path,visibility){const normalized=this.normalizePath(path),file=this.files.get(normalized);if(!file)throw Error(`File not found: ${path}`);file.visibility=visibility}async visibility(path){const normalized=this.normalizePath(path),file=this.files.get(normalized);if(!file)throw Error(`File not found: ${path}`);return file.visibility}async fileExists(path){const normalized=this.normalizePath(path);return this.files.has(normalized)}async directoryExists(path){const normalized=this.normalizePath(path);return this.directories.has(normalized)||normalized===""}async publicUrl(path,options={}){return`${(options.domain||process.env.APP_URL||"http://localhost").replace(/\/$/,"")}/${this.normalizePath(path)}`}async temporaryUrl(path,options){const expiry=normalizeExpiryToDate(options.expiresIn);return`http://localhost/temp/${Buffer.from(`${path}:${expiry.getTime()}`).toString("base64url")}`}async signedUrl(_path,_options){throw Error("[storage/memory] signedUrl is not supported on the in-memory adapter - switch to local or s3 disk for signed URL generation.")}async checksum(path,options={}){const algorithm=options.algorithm||"sha256",data=await this.readToUint8Array(path),hasher=new Bun.CryptoHasher(algorithm);hasher.update(data);return hasher.digest("hex")}async mimeType(path,_options={}){const normalized=this.normalizePath(path),file=this.files.get(normalized);if(!file)throw Error(`File not found: ${path}`);return file.mimeType}detectMimeType(path){return mimeFromExtension(path)}async lastModified(path){return(await this.stat(path)).lastModified}async fileSize(path){return(await this.stat(path)).size}clear(){this.files.clear();this.directories.clear();this.directories.add("")}}export function createMemoryStorage(){return new InMemoryStorageAdapter}
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.445",
5
+ "version": "0.75.450",
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.445",
65
- "@stacksjs/cli": "0.75.445",
66
- "@stacksjs/config": "0.75.445",
67
- "@stacksjs/env": "0.75.445",
68
- "@stacksjs/error-handling": "0.75.445",
69
- "@stacksjs/logging": "0.75.445",
70
- "@stacksjs/path": "0.75.445",
71
- "@stacksjs/strings": "0.75.445",
64
+ "@stacksjs/arrays": "0.75.450",
65
+ "@stacksjs/cli": "0.75.450",
66
+ "@stacksjs/config": "0.75.450",
67
+ "@stacksjs/env": "0.75.450",
68
+ "@stacksjs/error-handling": "0.75.450",
69
+ "@stacksjs/logging": "0.75.450",
70
+ "@stacksjs/path": "0.75.450",
71
+ "@stacksjs/strings": "0.75.450",
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.445",
77
+ "@stacksjs/types": "0.75.450",
78
78
  "better-dx": "^0.2.24"
79
79
  }
80
80
  }