@stacksjs/storage 0.75.468 → 0.75.473

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.
@@ -1,6 +1,7 @@
1
1
  import { Buffer } from 'node:buffer';
2
2
  import { copyFile } from 'node:fs/promises';
3
3
  import type { ChecksumOptions, DirectoryListing, FileContents, GetStreamOptions, ListOptions, MimeTypeOptions, PublicUrlOptions, PutResult, PutStreamOptions, SignedUrlOptions, StatEntry, StorageAdapter, StorageAdapterConfig, TemporaryUrlOptions, Visibility } from '../types';
4
+ import type { DirectoryPage, DirectoryPageOptions } from '../types';
4
5
  export declare function createBunStorage(config?: StorageAdapterConfig): BunStorageAdapter;
5
6
  export declare class BunStorageAdapter implements StorageAdapter {
6
7
  constructor(config?: StorageAdapterConfig);
@@ -17,6 +18,7 @@ export declare class BunStorageAdapter implements StorageAdapter {
17
18
  moveFile(from: string, to: string): Promise<void>;
18
19
  copyFile(from: string, to: string): Promise<void>;
19
20
  stat(path: string): Promise<StatEntry>;
21
+ listPage(path: string, options?: DirectoryPageOptions): Promise<DirectoryPage>;
20
22
  list(path: string, options?: ListOptions): DirectoryListing;
21
23
  changeVisibility(path: string, vis: Visibility): Promise<void>;
22
24
  visibility(path: string): Promise<Visibility>;
@@ -1 +1 @@
1
- import{Buffer}from"node:buffer";import{file,write as bunWrite}from"bun";import{chmod,copyFile,lstat,mkdir,rename,rm,unlink}from"node:fs/promises";import{dirname,join,relative}from"node:path";import{createDirectoryListing}from"../types";import{createSignedStorageToken}from"../signed-url";import{publicUrlFor,resolveStoragePath,signedUrlBase,writeAtomically}from"../filesystem-common";export class BunStorageAdapter{root;url;disk;constructor(config={}){this.root=config.root||process.cwd();this.url=config.url;this.disk=config.disk}resolvePath(path){return resolveStoragePath(this.root,path)}async write(path,contents){const fullPath=this.resolvePath(path);if(!(typeof contents==="string"||contents instanceof Uint8Array)){if(typeof contents.getReader!=="function")throw TypeError("[storage/bun] contents must be a web-standard ReadableStream (with .getReader()), not a Node stream.Readable. Convert via Readable.toWeb(nodeStream) before passing.")}await writeAtomically(fullPath,async(temporary)=>{if(typeof contents==="string"||contents instanceof Uint8Array)await bunWrite(temporary,contents);else await bunWrite(temporary,new Response(contents))});const written=file(fullPath);return{path,size:written.size,lastModified:written.lastModified}}async read(path){const fullPath=this.resolvePath(path),bunFile=file(fullPath);if(!await bunFile.exists())throw Error(`File not found: ${path}`);return await bunFile.arrayBuffer().then((buf)=>new Uint8Array(buf))}async getStream(path,_options){const fullPath=this.resolvePath(path),bunFile=file(fullPath);if(!await bunFile.exists())throw Error(`File not found: ${path}`);return bunFile.stream()}async putStream(path,stream,options){const fullPath=this.resolvePath(path),signal=options?.signal;signal?.throwIfAborted();await writeAtomically(fullPath,async(temporary)=>{const source=signal?stream.pipeThrough(new TransformStream,{signal}):stream;await bunWrite(temporary,new Response(source))});const written=file(fullPath);return{path,size:written.size,contentType:options?.contentType,lastModified:written.lastModified}}async readToString(path){const fullPath=this.resolvePath(path),bunFile=file(fullPath);if(!await bunFile.exists())throw Error(`File not found: ${path}`);return await bunFile.text()}async readToBuffer(path){const fullPath=this.resolvePath(path),bunFile=file(fullPath);if(!await bunFile.exists())throw Error(`File not found: ${path}`);const arrayBuffer=await bunFile.arrayBuffer();return Buffer.from(arrayBuffer)}async readToUint8Array(path){const fullPath=this.resolvePath(path),bunFile=file(fullPath);if(!await bunFile.exists())throw Error(`File not found: ${path}`);const arrayBuffer=await bunFile.arrayBuffer();return new Uint8Array(arrayBuffer)}async deleteFile(path){const fullPath=this.resolvePath(path);await unlink(fullPath).catch((error)=>{if(error.code!=="ENOENT")throw error})}async deleteDirectory(path){const fullPath=this.resolvePath(path);await rm(fullPath,{recursive:!0,force:!0})}async createDirectory(path){const fullPath=this.resolvePath(path);await mkdir(fullPath,{recursive:!0})}async moveFile(from,to){const fromPath=this.resolvePath(from),toPath=this.resolvePath(to);await mkdir(dirname(toPath),{recursive:!0});await rename(fromPath,toPath)}async copyFile(from,to){const fromPath=this.resolvePath(from),toPath=this.resolvePath(to);await mkdir(dirname(toPath),{recursive:!0});await copyFile(fromPath,toPath)}async stat(path){const fullPath=this.resolvePath(path),bunFile=file(fullPath);if(!await bunFile.exists())throw Error(`File not found: ${path}`);const stats=await Bun.file(fullPath).stat(),isDir=stats.isDirectory();return{path,type:isDir?"directory":"file",visibility:await this.visibility(path),size:isDir?0:stats.size,lastModified:stats.mtime?.getTime()||Date.now(),mimeType:isDir?void 0:bunFile.type}}list(path,options={}){const fullPath=this.resolvePath(path);return this.createAsyncIterator(fullPath,options.deep||!1)}async*createAsyncIterator(dirPath,deep){const entries=[];try{const glob=new Bun.Glob(deep?"**/*":"*");for await(const entry of glob.scan({cwd:dirPath,onlyFiles:!1})){const fullEntryPath=join(dirPath,entry),stats=await Bun.file(fullEntryPath).stat();entries.push({path:relative(this.root,fullEntryPath),type:stats.isDirectory()?"directory":"file"})}}catch(error){return}yield*createDirectoryListing(entries)}async changeVisibility(path,vis){const fullPath=this.resolvePath(path),isDir=(await lstat(fullPath)).isDirectory();await chmod(fullPath,vis==="public"?isDir?493:420:isDir?448:384)}async visibility(path){const fullPath=this.resolvePath(path);return(await lstat(fullPath)).mode&511&4?"public":"private"}async fileExists(path){const fullPath=this.resolvePath(path);return await file(fullPath).exists()}async directoryExists(path){const fullPath=this.resolvePath(path);try{return(await Bun.file(fullPath).stat()).isDirectory()}catch{return!1}}async publicUrl(path,options={}){return publicUrlFor(path,{domain:options.domain,diskUrl:this.url})}async temporaryUrl(path,options){return await this.signedUrl(path,{expiresIn:options.expiresIn})}async signedUrl(path,options){const token=createSignedStorageToken(path,{...options,disk:options.disk??this.disk});return`${signedUrlBase(options.baseUrl)}/__storage/${encodeURIComponent(path)}?token=${token}`}async checksum(path,options={}){const algorithm=options.algorithm||"sha256",fullPath=this.resolvePath(path),bunFile=file(fullPath);if(!await bunFile.exists())throw Error(`File not found: ${path}`);const hasher=new Bun.CryptoHasher(algorithm),arrayBuffer=await bunFile.arrayBuffer();hasher.update(new Uint8Array(arrayBuffer));return hasher.digest("hex")}async mimeType(path,_options={}){const fullPath=this.resolvePath(path),bunFile=file(fullPath);if(!await bunFile.exists())throw Error(`File not found: ${path}`);return bunFile.type||"application/octet-stream"}async lastModified(path){return(await this.stat(path)).lastModified}async fileSize(path){return(await this.stat(path)).size}}export function createBunStorage(config={}){return new BunStorageAdapter(config)}
1
+ import{localDirectoryPage}from"../directory-page";import{Buffer}from"node:buffer";import{file,write as bunWrite}from"bun";import{chmod,copyFile,lstat,mkdir,rename,rm,unlink}from"node:fs/promises";import{dirname,join,relative}from"node:path";import{createDirectoryListing}from"../types";import{createSignedStorageToken}from"../signed-url";import{publicUrlFor,resolveStoragePath,signedUrlBase,writeAtomically}from"../filesystem-common";export class BunStorageAdapter{root;url;disk;constructor(config={}){this.root=config.root||process.cwd();this.url=config.url;this.disk=config.disk}resolvePath(path){return resolveStoragePath(this.root,path)}async write(path,contents){const fullPath=this.resolvePath(path);if(!(typeof contents==="string"||contents instanceof Uint8Array)){if(typeof contents.getReader!=="function")throw TypeError("[storage/bun] contents must be a web-standard ReadableStream (with .getReader()), not a Node stream.Readable. Convert via Readable.toWeb(nodeStream) before passing.")}await writeAtomically(fullPath,async(temporary)=>{if(typeof contents==="string"||contents instanceof Uint8Array)await bunWrite(temporary,contents);else await bunWrite(temporary,new Response(contents))});const written=file(fullPath);return{path,size:written.size,lastModified:written.lastModified}}async read(path){const fullPath=this.resolvePath(path),bunFile=file(fullPath);if(!await bunFile.exists())throw Error(`File not found: ${path}`);return await bunFile.arrayBuffer().then((buf)=>new Uint8Array(buf))}async getStream(path,_options){const fullPath=this.resolvePath(path),bunFile=file(fullPath);if(!await bunFile.exists())throw Error(`File not found: ${path}`);return bunFile.stream()}async putStream(path,stream,options){const fullPath=this.resolvePath(path),signal=options?.signal;signal?.throwIfAborted();await writeAtomically(fullPath,async(temporary)=>{const source=signal?stream.pipeThrough(new TransformStream,{signal}):stream;await bunWrite(temporary,new Response(source))});const written=file(fullPath);return{path,size:written.size,contentType:options?.contentType,lastModified:written.lastModified}}async readToString(path){const fullPath=this.resolvePath(path),bunFile=file(fullPath);if(!await bunFile.exists())throw Error(`File not found: ${path}`);return await bunFile.text()}async readToBuffer(path){const fullPath=this.resolvePath(path),bunFile=file(fullPath);if(!await bunFile.exists())throw Error(`File not found: ${path}`);const arrayBuffer=await bunFile.arrayBuffer();return Buffer.from(arrayBuffer)}async readToUint8Array(path){const fullPath=this.resolvePath(path),bunFile=file(fullPath);if(!await bunFile.exists())throw Error(`File not found: ${path}`);const arrayBuffer=await bunFile.arrayBuffer();return new Uint8Array(arrayBuffer)}async deleteFile(path){const fullPath=this.resolvePath(path);await unlink(fullPath).catch((error)=>{if(error.code!=="ENOENT")throw error})}async deleteDirectory(path){const fullPath=this.resolvePath(path);await rm(fullPath,{recursive:!0,force:!0})}async createDirectory(path){const fullPath=this.resolvePath(path);await mkdir(fullPath,{recursive:!0})}async moveFile(from,to){const fromPath=this.resolvePath(from),toPath=this.resolvePath(to);await mkdir(dirname(toPath),{recursive:!0});await rename(fromPath,toPath)}async copyFile(from,to){const fromPath=this.resolvePath(from),toPath=this.resolvePath(to);await mkdir(dirname(toPath),{recursive:!0});await copyFile(fromPath,toPath)}async stat(path){const fullPath=this.resolvePath(path),bunFile=file(fullPath);if(!await bunFile.exists())throw Error(`File not found: ${path}`);const stats=await Bun.file(fullPath).stat(),isDir=stats.isDirectory();return{path,type:isDir?"directory":"file",visibility:await this.visibility(path),size:isDir?0:stats.size,lastModified:stats.mtime?.getTime()||Date.now(),mimeType:isDir?void 0:bunFile.type}}listPage(path,options={}){return localDirectoryPage(this.resolvePath(path),path,options)}list(path,options={}){const fullPath=this.resolvePath(path);return this.createAsyncIterator(fullPath,options.deep||!1)}async*createAsyncIterator(dirPath,deep){const entries=[];try{const glob=new Bun.Glob(deep?"**/*":"*");for await(const entry of glob.scan({cwd:dirPath,onlyFiles:!1})){const fullEntryPath=join(dirPath,entry),stats=await Bun.file(fullEntryPath).stat();entries.push({path:relative(this.root,fullEntryPath),type:stats.isDirectory()?"directory":"file"})}}catch(error){return}yield*createDirectoryListing(entries)}async changeVisibility(path,vis){const fullPath=this.resolvePath(path),isDir=(await lstat(fullPath)).isDirectory();await chmod(fullPath,vis==="public"?isDir?493:420:isDir?448:384)}async visibility(path){const fullPath=this.resolvePath(path);return(await lstat(fullPath)).mode&511&4?"public":"private"}async fileExists(path){const fullPath=this.resolvePath(path);return await file(fullPath).exists()}async directoryExists(path){const fullPath=this.resolvePath(path);try{return(await Bun.file(fullPath).stat()).isDirectory()}catch{return!1}}async publicUrl(path,options={}){return publicUrlFor(path,{domain:options.domain,diskUrl:this.url})}async temporaryUrl(path,options){return await this.signedUrl(path,{expiresIn:options.expiresIn})}async signedUrl(path,options){const token=createSignedStorageToken(path,{...options,disk:options.disk??this.disk});return`${signedUrlBase(options.baseUrl)}/__storage/${encodeURIComponent(path)}?token=${token}`}async checksum(path,options={}){const algorithm=options.algorithm||"sha256",fullPath=this.resolvePath(path),bunFile=file(fullPath);if(!await bunFile.exists())throw Error(`File not found: ${path}`);const hasher=new Bun.CryptoHasher(algorithm),arrayBuffer=await bunFile.arrayBuffer();hasher.update(new Uint8Array(arrayBuffer));return hasher.digest("hex")}async mimeType(path,_options={}){const fullPath=this.resolvePath(path),bunFile=file(fullPath);if(!await bunFile.exists())throw Error(`File not found: ${path}`);return bunFile.type||"application/octet-stream"}async lastModified(path){return(await this.stat(path)).lastModified}async fileSize(path){return(await this.stat(path)).size}}export function createBunStorage(config={}){return new BunStorageAdapter(config)}
@@ -1,6 +1,7 @@
1
1
  import { Buffer } from 'node:buffer';
2
2
  import { copyFile, stat } from 'node:fs/promises';
3
3
  import type { ChecksumOptions, DirectoryListing, FileContents, GetStreamOptions, ListOptions, MimeTypeOptions, PublicUrlOptions, PutResult, PutStreamOptions, SignedUrlOptions, StatEntry, StorageAdapter, StorageAdapterConfig, TemporaryUrlOptions, Visibility } from '../types';
4
+ import type { DirectoryPage, DirectoryPageOptions } from '../types';
4
5
  /**
5
6
  * Create a local storage adapter instance
6
7
  */
@@ -23,6 +24,7 @@ export declare class LocalStorageAdapter implements StorageAdapter {
23
24
  moveFile(from: string, to: string): Promise<void>;
24
25
  copyFile(from: string, to: string): Promise<void>;
25
26
  stat(path: string): Promise<StatEntry>;
27
+ listPage(path: string, options?: DirectoryPageOptions): Promise<DirectoryPage>;
26
28
  list(path: string, options?: ListOptions): DirectoryListing;
27
29
  changeVisibility(path: string, vis: Visibility): Promise<void>;
28
30
  visibility(path: string): Promise<Visibility>;
@@ -1 +1 @@
1
- import{Buffer}from"node:buffer";import{createReadStream,createWriteStream}from"node:fs";import{access,chmod,constants,copyFile,lstat,mkdir,readdir,readFile,rename,rm,stat,unlink,writeFile}from"node:fs/promises";import{basename,dirname,join,relative}from"node:path";import{Readable}from"node:stream";import{pipeline}from"node:stream/promises";import{createDirectoryListing}from"../types";import{createSignedStorageToken}from"../signed-url";import{publicUrlFor,resolveStoragePath,signedUrlBase,writeAtomically}from"../filesystem-common";export class LocalStorageAdapter{root;url;disk;constructor(config={}){this.root=config.root||process.cwd();this.url=config.url;this.disk=config.disk}resolvePath(path){return resolveStoragePath(this.root,path)}async write(path,contents){const fullPath=this.resolvePath(path);await writeAtomically(fullPath,async(temporary)=>{if(typeof contents==="string")await writeFile(temporary,contents,"utf8");else if(contents instanceof Uint8Array)await writeFile(temporary,contents);else{const nodeReadable=Readable.fromWeb(contents);await pipeline(nodeReadable,createWriteStream(temporary))}});const st=await stat(fullPath);return{path,size:st.size,lastModified:st.mtimeMs}}async read(path){const fullPath=this.resolvePath(path);return await readFile(fullPath)}async getStream(path,options){const fullPath=this.resolvePath(path);await access(fullPath,constants.R_OK);const nodeStream=createReadStream(fullPath,{signal:options?.signal});return Readable.toWeb(nodeStream)}async putStream(path,stream,options){const fullPath=this.resolvePath(path);await writeAtomically(fullPath,async(temporary)=>{const nodeReadable=Readable.fromWeb(stream);await pipeline(nodeReadable,createWriteStream(temporary),{signal:options?.signal})});const st=await stat(fullPath);return{path,size:st.size,contentType:options?.contentType,lastModified:st.mtimeMs}}async readToString(path){const fullPath=this.resolvePath(path);return await readFile(fullPath,"utf8")}async readToBuffer(path){const fullPath=this.resolvePath(path);return await readFile(fullPath)}async readToUint8Array(path){const fullPath=this.resolvePath(path),buffer=await readFile(fullPath);return new Uint8Array(buffer)}async deleteFile(path){const fullPath=this.resolvePath(path);await unlink(fullPath).catch((error)=>{if(error.code!=="ENOENT")throw error})}async deleteDirectory(path){const fullPath=this.resolvePath(path);await rm(fullPath,{recursive:!0,force:!0})}async createDirectory(path){const fullPath=this.resolvePath(path);await mkdir(fullPath,{recursive:!0})}async moveFile(from,to){const fromPath=this.resolvePath(from),toPath=this.resolvePath(to),toDir=dirname(toPath);await mkdir(toDir,{recursive:!0});await rename(fromPath,toPath)}async copyFile(from,to){const fromPath=this.resolvePath(from),toPath=this.resolvePath(to),toDir=dirname(toPath);await mkdir(toDir,{recursive:!0});await copyFile(fromPath,toPath)}async stat(path){const fullPath=this.resolvePath(path),stats=await lstat(fullPath);return{path,type:stats.isDirectory()?"directory":"file",visibility:await this.visibility(path),size:stats.size,lastModified:stats.mtimeMs,mimeType:stats.isFile()?await this.detectMimeType(fullPath):void 0}}list(path,options={}){return this.createAsyncIterator(path,options.deep||!1)}async*createAsyncIterator(path,deep){const fullPath=this.resolvePath(path);try{await access(fullPath,constants.R_OK)}catch{return}const entries=await this.readDirectoryRecursive(fullPath,deep);yield*createDirectoryListing(entries)}async readDirectoryRecursive(dirPath,deep){const entries=[];try{const items=await readdir(dirPath,{withFileTypes:!0});for(const item of items){const itemPath=join(dirPath,item.name),relativePath=relative(this.root,itemPath);entries.push({path:relativePath,type:item.isDirectory()?"directory":"file"});if(deep&&item.isDirectory()){const subEntries=await this.readDirectoryRecursive(itemPath,!0);entries.push(...subEntries)}}}catch(error){if(error?.code!=="EACCES"&&error?.code!=="EPERM")throw error}return entries}async changeVisibility(path,vis){const fullPath=this.resolvePath(path),isDir=(await lstat(fullPath)).isDirectory();await chmod(fullPath,vis==="public"?isDir?493:420:isDir?448:384)}async visibility(path){const fullPath=this.resolvePath(path);return(await lstat(fullPath)).mode&511&4?"public":"private"}async fileExists(path){const fullPath=this.resolvePath(path);try{return(await lstat(fullPath)).isFile()}catch{return!1}}async directoryExists(path){const fullPath=this.resolvePath(path);try{return(await lstat(fullPath)).isDirectory()}catch{return!1}}async publicUrl(path,options={}){return publicUrlFor(path,{domain:options.domain,diskUrl:this.url})}async temporaryUrl(path,options){return await this.signedUrl(path,{expiresIn:options.expiresIn})}async signedUrl(path,options){const token=createSignedStorageToken(path,{...options,disk:options.disk??this.disk});return`${signedUrlBase(options.baseUrl)}/__storage/${encodeURIComponent(path)}?token=${token}`}async checksum(path,options={}){const algorithm=options.algorithm||"sha256",fullPath=this.resolvePath(path),content=await readFile(fullPath),hasher=new Bun.CryptoHasher(algorithm);hasher.update(content);return hasher.digest("hex")}async mimeType(path,options={}){const fullPath=this.resolvePath(path);return await this.detectMimeType(fullPath)}async detectMimeType(filePath){const ext=basename(filePath).split(".").pop()?.toLowerCase();return{txt:"text/plain",html:"text/html",css:"text/css",js:"application/javascript",json:"application/json",xml:"application/xml",pdf:"application/pdf",zip:"application/zip",jpg:"image/jpeg",jpeg:"image/jpeg",png:"image/png",gif:"image/gif",svg:"image/svg+xml",mp4:"video/mp4",mp3:"audio/mpeg",wav:"audio/wav"}[ext||""]||"application/octet-stream"}async lastModified(path){return(await this.stat(path)).lastModified}async fileSize(path){return(await this.stat(path)).size}}export function createLocalStorage(config={}){return new LocalStorageAdapter(config)}
1
+ import{localDirectoryPage}from"../directory-page";import{Buffer}from"node:buffer";import{createReadStream,createWriteStream}from"node:fs";import{access,chmod,constants,copyFile,lstat,mkdir,readdir,readFile,rename,rm,stat,unlink,writeFile}from"node:fs/promises";import{basename,dirname,join,relative}from"node:path";import{Readable}from"node:stream";import{pipeline}from"node:stream/promises";import{createDirectoryListing}from"../types";import{createSignedStorageToken}from"../signed-url";import{publicUrlFor,resolveStoragePath,signedUrlBase,writeAtomically}from"../filesystem-common";export class LocalStorageAdapter{root;url;disk;constructor(config={}){this.root=config.root||process.cwd();this.url=config.url;this.disk=config.disk}resolvePath(path){return resolveStoragePath(this.root,path)}async write(path,contents){const fullPath=this.resolvePath(path);await writeAtomically(fullPath,async(temporary)=>{if(typeof contents==="string")await writeFile(temporary,contents,"utf8");else if(contents instanceof Uint8Array)await writeFile(temporary,contents);else{const nodeReadable=Readable.fromWeb(contents);await pipeline(nodeReadable,createWriteStream(temporary))}});const st=await stat(fullPath);return{path,size:st.size,lastModified:st.mtimeMs}}async read(path){const fullPath=this.resolvePath(path);return await readFile(fullPath)}async getStream(path,options){const fullPath=this.resolvePath(path);await access(fullPath,constants.R_OK);const nodeStream=createReadStream(fullPath,{signal:options?.signal});return Readable.toWeb(nodeStream)}async putStream(path,stream,options){const fullPath=this.resolvePath(path);await writeAtomically(fullPath,async(temporary)=>{const nodeReadable=Readable.fromWeb(stream);await pipeline(nodeReadable,createWriteStream(temporary),{signal:options?.signal})});const st=await stat(fullPath);return{path,size:st.size,contentType:options?.contentType,lastModified:st.mtimeMs}}async readToString(path){const fullPath=this.resolvePath(path);return await readFile(fullPath,"utf8")}async readToBuffer(path){const fullPath=this.resolvePath(path);return await readFile(fullPath)}async readToUint8Array(path){const fullPath=this.resolvePath(path),buffer=await readFile(fullPath);return new Uint8Array(buffer)}async deleteFile(path){const fullPath=this.resolvePath(path);await unlink(fullPath).catch((error)=>{if(error.code!=="ENOENT")throw error})}async deleteDirectory(path){const fullPath=this.resolvePath(path);await rm(fullPath,{recursive:!0,force:!0})}async createDirectory(path){const fullPath=this.resolvePath(path);await mkdir(fullPath,{recursive:!0})}async moveFile(from,to){const fromPath=this.resolvePath(from),toPath=this.resolvePath(to),toDir=dirname(toPath);await mkdir(toDir,{recursive:!0});await rename(fromPath,toPath)}async copyFile(from,to){const fromPath=this.resolvePath(from),toPath=this.resolvePath(to),toDir=dirname(toPath);await mkdir(toDir,{recursive:!0});await copyFile(fromPath,toPath)}async stat(path){const fullPath=this.resolvePath(path),stats=await lstat(fullPath);return{path,type:stats.isDirectory()?"directory":"file",visibility:await this.visibility(path),size:stats.size,lastModified:stats.mtimeMs,mimeType:stats.isFile()?await this.detectMimeType(fullPath):void 0}}listPage(path,options={}){return localDirectoryPage(this.resolvePath(path),path,options)}list(path,options={}){return this.createAsyncIterator(path,options.deep||!1)}async*createAsyncIterator(path,deep){const fullPath=this.resolvePath(path);try{await access(fullPath,constants.R_OK)}catch{return}const entries=await this.readDirectoryRecursive(fullPath,deep);yield*createDirectoryListing(entries)}async readDirectoryRecursive(dirPath,deep){const entries=[];try{const items=await readdir(dirPath,{withFileTypes:!0});for(const item of items){const itemPath=join(dirPath,item.name),relativePath=relative(this.root,itemPath);entries.push({path:relativePath,type:item.isDirectory()?"directory":"file"});if(deep&&item.isDirectory()){const subEntries=await this.readDirectoryRecursive(itemPath,!0);entries.push(...subEntries)}}}catch(error){if(error?.code!=="EACCES"&&error?.code!=="EPERM")throw error}return entries}async changeVisibility(path,vis){const fullPath=this.resolvePath(path),isDir=(await lstat(fullPath)).isDirectory();await chmod(fullPath,vis==="public"?isDir?493:420:isDir?448:384)}async visibility(path){const fullPath=this.resolvePath(path);return(await lstat(fullPath)).mode&511&4?"public":"private"}async fileExists(path){const fullPath=this.resolvePath(path);try{return(await lstat(fullPath)).isFile()}catch{return!1}}async directoryExists(path){const fullPath=this.resolvePath(path);try{return(await lstat(fullPath)).isDirectory()}catch{return!1}}async publicUrl(path,options={}){return publicUrlFor(path,{domain:options.domain,diskUrl:this.url})}async temporaryUrl(path,options){return await this.signedUrl(path,{expiresIn:options.expiresIn})}async signedUrl(path,options){const token=createSignedStorageToken(path,{...options,disk:options.disk??this.disk});return`${signedUrlBase(options.baseUrl)}/__storage/${encodeURIComponent(path)}?token=${token}`}async checksum(path,options={}){const algorithm=options.algorithm||"sha256",fullPath=this.resolvePath(path),content=await readFile(fullPath),hasher=new Bun.CryptoHasher(algorithm);hasher.update(content);return hasher.digest("hex")}async mimeType(path,options={}){const fullPath=this.resolvePath(path);return await this.detectMimeType(fullPath)}async detectMimeType(filePath){const ext=basename(filePath).split(".").pop()?.toLowerCase();return{txt:"text/plain",html:"text/html",css:"text/css",js:"application/javascript",json:"application/json",xml:"application/xml",pdf:"application/pdf",zip:"application/zip",jpg:"image/jpeg",jpeg:"image/jpeg",png:"image/png",gif:"image/gif",svg:"image/svg+xml",mp4:"video/mp4",mp3:"audio/mpeg",wav:"audio/wav"}[ext||""]||"application/octet-stream"}async lastModified(path){return(await this.stat(path)).lastModified}async fileSize(path){return(await this.stat(path)).size}}export function createLocalStorage(config={}){return new LocalStorageAdapter(config)}
@@ -1,5 +1,6 @@
1
1
  import { Buffer } from 'node:buffer';
2
2
  import type { ChecksumOptions, DirectoryListing, FileContents, GetStreamOptions, ListOptions, MimeTypeOptions, PresignedUploadPolicy, PresignedUploadPolicyOptions, PresignedUploadUrl, PresignedUploadUrlOptions, PublicUrlOptions, PutResult, PutStreamOptions, SignedUrlOptions, StatEntry, StorageAdapter, StorageAdapterConfig, TemporaryUrlOptions, Visibility } from '../types';
3
+ import type { DirectoryPage, DirectoryPageOptions } from '../types';
3
4
  import type { S3Client } from '@stacksjs/ts-cloud';
4
5
  /**
5
6
  * Build ts-cloud `S3ClientOptions` from adapter config, or `undefined` for a
@@ -67,6 +68,7 @@ export declare class S3StorageAdapter implements StorageAdapter {
67
68
  moveFile(from: string, to: string): Promise<void>;
68
69
  copyFile(from: string, to: string): Promise<void>;
69
70
  stat(path: string): Promise<StatEntry>;
71
+ listPage(path: string, options?: DirectoryPageOptions): Promise<DirectoryPage>;
70
72
  list(path: string, options?: ListOptions): DirectoryListing;
71
73
  changeVisibility(path: string, vis: Visibility): Promise<void>;
72
74
  visibility(path: string): Promise<Visibility>;
@@ -1 +1 @@
1
- import{Buffer}from"node:buffer";import{extensionForContentType,mimeFromExtension}from"../mime-from-extension";import{normalizeExpiryToMilliseconds}from"../types";import{sanitizePresignedDir,sanitizePresignedFilename}from"../path-sanitize";import{signS3PresignedPost}from"../s3-presigned-post";import process from"node:process";const S3_MIN_PART_SIZE=5242880,S3_MAX_PART_SIZE=5368709120;function clampPartSize(requested){if(!Number.isFinite(requested))return S3_MIN_PART_SIZE;return Math.max(S3_MIN_PART_SIZE,Math.min(Math.floor(requested),S3_MAX_PART_SIZE))}class ChunkBuffer{chunks=[];total=0;constructor(_partSize){}get length(){return this.total}push(c){this.chunks.push(c);this.total+=c.length}take(n){const out=new Uint8Array(n);let written=0;while(written<n&&this.chunks.length>0){const head=this.chunks[0],need=n-written;if(head.length<=need){out.set(head,written);written+=head.length;this.chunks.shift()}else{out.set(head.subarray(0,need),written);this.chunks[0]=head.subarray(need);written+=need}}this.total-=n;return out}flush(){const out=new Uint8Array(this.total);let off=0;for(const c of this.chunks){out.set(c,off);off+=c.length}this.chunks=[];this.total=0;return out}}async function isSettled(p){const sentinel=Symbol("pending");return await Promise.race([p.then(()=>"settled",()=>"settled"),Promise.resolve(sentinel)])!==sentinel}export function resolveS3ClientOptions(config){const options={};if(config.endpoint)options.endpoint=config.endpoint.replace(/^https:\/\//i,"").replace(/\/+$/,"");if(config.usePathStyleEndpoint)options.forcePathStyle=!0;if(config.credentials)options.credentials=config.credentials;return Object.keys(options).length>0?options:void 0}export class S3StorageAdapter{_client;_clientPromise=null;bucket;prefix;region;endpoint;usePathStyleEndpoint;publicBaseUrl;credentials;constructor(client,config){this._client=client;this.bucket=config.bucket||"";this.prefix=config.prefix||"";this.region=config.region||"us-east-1";this.credentials=config.credentials;this.endpoint=config.endpoint;this.usePathStyleEndpoint=config.usePathStyleEndpoint;this.publicBaseUrl=config.url?.replace(/\/+$/,"");if(!this.bucket)throw Error("S3 bucket name is required")}async getClient(){if(this._client)return this._client;if(!this._clientPromise)this._clientPromise=import("@stacksjs/ts-cloud").then((cloud)=>{this._client=new cloud.S3Client(this.region,void 0,resolveS3ClientOptions({endpoint:this.endpoint,usePathStyleEndpoint:this.usePathStyleEndpoint,credentials:this.credentials}));return this._client});return this._clientPromise}resolveCredentials(){if(this.credentials?.accessKeyId&&this.credentials.secretAccessKey)return this.credentials;const accessKeyId=process.env.AWS_ACCESS_KEY_ID,secretAccessKey=process.env.AWS_SECRET_ACCESS_KEY,sessionToken=process.env.AWS_SESSION_TOKEN;if(!accessKeyId||!secretAccessKey)throw Error("[storage/s3] presignedUploadPolicy requires AWS credentials - pass them via S3DiskConfig.credentials or set AWS_ACCESS_KEY_ID + AWS_SECRET_ACCESS_KEY.");return{accessKeyId,secretAccessKey,sessionToken}}prefixPath(path){if(!this.prefix)return path;return`${this.prefix}/${path}`.replace(/\/+/g,"/")}stripPrefix(path){if(!this.prefix)return path;const prefixWithSlash=`${this.prefix}/`;return path.startsWith(prefixWithSlash)?path.slice(prefixWithSlash.length):path}async contentsToBuffer(contents){if(typeof contents==="string")return Buffer.from(contents,"utf8");else if(contents instanceof Buffer)return contents;else if(contents instanceof Uint8Array)return Buffer.from(contents);else{if(typeof contents.getReader!=="function")throw TypeError("[storage/s3] 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)}return Buffer.concat(chunks.map((c)=>Buffer.from(c)))}}async write(path,contents){const key=this.prefixPath(path),body=await this.contentsToBuffer(contents),contentType=this.detectMimeType(path);await(await this.getClient()).putObject({bucket:this.bucket,key,body,contentType});return{path,size:body.length,contentType,lastModified:Date.now()}}async read(path){const key=this.prefixPath(path),response=await(await this.getClient()).getObjectBytes(this.bucket,key);if(!response?.body)throw Error(`Failed to read file: ${path}`);return Buffer.from(response.body)}async getStream(path,_options){const key=this.prefixPath(path),buf=await(await this.getClient()).getObjectBuffer(this.bucket,key);if(!buf)throw Error(`Failed to read file: ${path}`);const bytes=new Uint8Array(buf);return new ReadableStream({start(controller){controller.enqueue(bytes);controller.close()}})}async putStream(path,stream,options){const key=this.prefixPath(path),contentType=options?.contentType??this.detectMimeType(path),partSize=clampPartSize(options?.partSize??5242880),concurrency=Math.max(1,Math.min(options?.concurrency??4,100)),maxRetries=Math.max(0,options?.maxRetries??3),signal=options?.signal,reader=stream.getReader();let firstChunk=null,firstDone=!1;{const buf=new ChunkBuffer(partSize);while(!firstDone&&buf.length<partSize){if(signal?.aborted){try{reader.releaseLock()}catch{}throw Error("aborted")}const{value,done}=await reader.read();if(done){firstDone=!0;break}if(value)buf.push(value)}firstChunk=buf.flush()}if(firstDone){try{reader.releaseLock()}catch{}await(await this.getClient()).putObject({bucket:this.bucket,key,body:Buffer.from(firstChunk),contentType});return{path,size:firstChunk.length,contentType,lastModified:Date.now()}}const{UploadId:uploadId}=await(await this.getClient()).createMultipartUpload(this.bucket,key,{contentType}),completedParts=[];let totalBytes=0,partNumber=1;const inflight=[],uploadOne=async(body,n)=>{let attempt=0;while(!0){if(signal?.aborted)throw Error("aborted");try{const{ETag}=await(await this.getClient()).uploadPart(this.bucket,key,uploadId,n,Buffer.from(body));completedParts.push({PartNumber:n,ETag});totalBytes+=body.length;return}catch(err){if(attempt>=maxRetries)throw err;attempt+=1}}};try{inflight.push(uploadOne(firstChunk,partNumber++));firstChunk=null;const buf=new ChunkBuffer(partSize);while(!0){if(signal?.aborted)throw Error("aborted");const{value,done}=await reader.read();if(done)break;if(value)buf.push(value);while(buf.length>=partSize){const part=buf.take(partSize);if(inflight.length>=concurrency){await Promise.race(inflight.map((p,i)=>p.then(()=>i)));for(let i=inflight.length-1;i>=0;i--)if(await isSettled(inflight[i]))inflight.splice(i,1)}inflight.push(uploadOne(part,partNumber++))}}try{reader.releaseLock()}catch{}const tail=buf.flush();if(tail.length>0)inflight.push(uploadOne(tail,partNumber++));await Promise.all(inflight);completedParts.sort((a,b)=>a.PartNumber-b.PartNumber);await(await this.getClient()).completeMultipartUpload(this.bucket,key,uploadId,completedParts);return{path,size:totalBytes,contentType,lastModified:Date.now()}}catch(err){try{await(await this.getClient()).abortMultipartUpload(this.bucket,key,uploadId)}catch{}throw err}}async readToString(path){const key=this.prefixPath(path),response=await(await this.getClient()).getObject(this.bucket,key);if(!response)throw Error(`Failed to read file: ${path}`);return response}async readToBuffer(path){return await this.read(path)}async readToUint8Array(path){const buffer=await this.readToBuffer(path);return new Uint8Array(buffer)}async deleteFile(path){const key=this.prefixPath(path);await(await this.getClient()).deleteObject(this.bucket,key)}async deleteDirectory(path){const prefix=this.prefixPath(path).replace(/\/+$/,""),normalizedPrefix=prefix?`${prefix}/`:void 0,keys=(await(await this.getClient()).listAllObjects({bucket:this.bucket,prefix:normalizedPrefix})).map((obj)=>obj.Key).filter((k)=>typeof k==="string");if(keys.length===0)return;await(await this.getClient()).deleteObjects(this.bucket,keys)}async createDirectory(_path){}async moveFile(from,to){await this.copyFile(from,to);await this.deleteFile(from)}async copyFile(from,to){const fromKey=this.prefixPath(from),toKey=this.prefixPath(to);await(await this.getClient()).copyObject({sourceBucket:this.bucket,sourceKey:fromKey,destinationBucket:this.bucket,destinationKey:toKey})}async stat(path){const key=this.prefixPath(path),result=await(await this.getClient()).headObject(this.bucket,key);if(!result)throw Error(`File not found: ${path}`);return{path,type:"file",visibility:"private",size:result.ContentLength||0,lastModified:result.LastModified?new Date(result.LastModified).getTime():Date.now(),mimeType:result.ContentType}}list(path,options={}){return this.createAsyncIterator(path,options.deep||!1)}async*createAsyncIterator(path,deep){const prefix=this.prefixPath(path).replace(/\/+$/,""),normalizedPrefix=prefix?`${prefix}/`:void 0;if(deep){const objects=await(await this.getClient()).listAllObjects({bucket:this.bucket,prefix:normalizedPrefix});for(const obj of objects)yield{path:this.stripPrefix(obj.Key),type:"file"}}else{let continuationToken;do{const result=await(await this.getClient()).listObjects({bucket:this.bucket,prefix:normalizedPrefix,delimiter:"/",continuationToken});for(const obj of result.objects||[]){if(obj.Key===normalizedPrefix)continue;yield{path:this.stripPrefix(obj.Key),type:"file"}}for(const folder of result.commonPrefixes||[])yield{path:this.stripPrefix(folder.replace(/\/$/,"")),type:"directory"};continuationToken=result.nextContinuationToken}while(continuationToken)}}async changeVisibility(path,vis){const key=this.prefixPath(path),acl=vis==="public"?"public-read":"private";await(await this.getClient()).putObjectAcl(this.bucket,key,acl)}async visibility(path){const key=this.prefixPath(path),grant=(await(await this.getClient()).getObjectAcl(this.bucket,key))?.AccessControlList?.Grant;return(grant===void 0?[]:Array.isArray(grant)?grant:[grant]).some((g)=>g.Grantee?.URI==="http://acs.amazonaws.com/groups/global/AllUsers"&&(g.Permission==="READ"||g.Permission==="FULL_CONTROL"))?"public":"private"}async fileExists(path){const key=this.prefixPath(path);try{return!!await(await this.getClient()).headObject(this.bucket,key)}catch(error){if(!error.message?.includes("404")&&!error.message?.includes("NoSuchKey")&&!error.message?.includes("NotFound"))console.debug(`[s3] Unexpected error checking file existence for ${path}: ${error.message}`);return!1}}async directoryExists(path){const prefix=this.prefixPath(path);return((await(await this.getClient()).listObjects({bucket:this.bucket,prefix:`${prefix}/`,maxKeys:1})).objects||[]).length>0}async publicUrl(path,options={}){const key=this.prefixPath(path),base=options.domain?.replace(/\/+$/,"")??this.publicBaseUrl;if(base)return`${base}/${key}`;if(this.endpoint)return`${this.endpointBaseUrl()}/${key}`;return`https://${this.bucket}.s3.${this.region}.amazonaws.com/${key}`}endpointBaseUrl(){const endpoint=this.endpoint.replace(/\/+$/,""),withScheme=/^https?:\/\//.test(endpoint)?endpoint:`https://${endpoint}`;if(this.usePathStyleEndpoint)return`${withScheme}/${this.bucket}`;const url=new URL(withScheme);url.hostname=`${this.bucket}.${url.hostname}`;return url.origin}async temporaryUrl(path,options){const key=this.prefixPath(path),expiresIn=Math.floor(normalizeExpiryToMilliseconds(options.expiresIn)/1000),MIN_EXPIRY=60,MAX_EXPIRY=604800;if(!Number.isFinite(expiresIn)||expiresIn<MIN_EXPIRY||expiresIn>MAX_EXPIRY)throw RangeError(`[storage/s3] temporaryUrl expiresIn must be between 60s and 7 days (got ${expiresIn}s)`);return await(await this.getClient()).getSignedUrl({bucket:this.bucket,key,expiresIn,operation:"getObject"})}async signedUrl(path,options){return this.temporaryUrl(path,{expiresIn:options.expiresIn})}async presignedUploadUrl(options){if(!options.contentType)throw Error("[storage/s3] presignedUploadUrl requires `contentType` - S3 signs against the exact header.");const expiresIn=Math.floor(options.expiresIn),MIN_EXPIRY=60,MAX_EXPIRY=604800;if(!Number.isFinite(expiresIn)||expiresIn<MIN_EXPIRY||expiresIn>MAX_EXPIRY)throw RangeError(`[storage/s3] presignedUploadUrl expiresIn must be between 60s and 7 days (got ${expiresIn}s)`);const safeDir=sanitizePresignedDir(options.dir),safeFilename=options.filename!==void 0?sanitizePresignedFilename(options.filename):`${crypto.randomUUID().replace(/-/g,"")}${this.extensionForContentType(options.contentType)}`,path=safeDir?`${safeDir}/${safeFilename}`:safeFilename,key=this.prefixPath(path);return{url:await(await this.getClient()).getSignedUrl({bucket:this.bucket,key,expiresIn,operation:"putObject"}),path,key,contentType:options.contentType,maxBytes:options.maxBytes}}async presignedUploadPolicy(options){const credentials=this.resolveCredentials(),scopedKey=typeof options.key==="string"?this.prefixPath(options.key):{startsWith:this.prefixPath(options.key.startsWith)};return signS3PresignedPost({bucket:this.bucket,region:this.region,credentials,key:scopedKey,contentType:options.contentType,contentLengthRange:options.contentLengthRange,acl:options.acl,expiresIn:options.expiresIn,fields:options.fields})}extensionForContentType(contentType){return extensionForContentType(contentType)}async checksum(path,options={}){const algorithm=options.algorithm||"sha256",content=await this.readToUint8Array(path),hasher=new Bun.CryptoHasher(algorithm);hasher.update(content);return hasher.digest("hex")}async mimeType(path,_options={}){return(await this.stat(path)).mimeType||this.detectMimeType(path)}detectMimeType(path){return mimeFromExtension(path)}async lastModified(path){return(await this.stat(path)).lastModified}async fileSize(path){return(await this.stat(path)).size}}export function createS3Storage(client,config){return new S3StorageAdapter(client,config)}
1
+ import{directoryPageSize}from"../directory-page";import{Buffer}from"node:buffer";import{extensionForContentType,mimeFromExtension}from"../mime-from-extension";import{normalizeExpiryToMilliseconds}from"../types";import{sanitizePresignedDir,sanitizePresignedFilename}from"../path-sanitize";import{signS3PresignedPost}from"../s3-presigned-post";import process from"node:process";const S3_MIN_PART_SIZE=5242880,S3_MAX_PART_SIZE=5368709120;function clampPartSize(requested){if(!Number.isFinite(requested))return S3_MIN_PART_SIZE;return Math.max(S3_MIN_PART_SIZE,Math.min(Math.floor(requested),S3_MAX_PART_SIZE))}class ChunkBuffer{chunks=[];total=0;constructor(_partSize){}get length(){return this.total}push(c){this.chunks.push(c);this.total+=c.length}take(n){const out=new Uint8Array(n);let written=0;while(written<n&&this.chunks.length>0){const head=this.chunks[0],need=n-written;if(head.length<=need){out.set(head,written);written+=head.length;this.chunks.shift()}else{out.set(head.subarray(0,need),written);this.chunks[0]=head.subarray(need);written+=need}}this.total-=n;return out}flush(){const out=new Uint8Array(this.total);let off=0;for(const c of this.chunks){out.set(c,off);off+=c.length}this.chunks=[];this.total=0;return out}}async function isSettled(p){const sentinel=Symbol("pending");return await Promise.race([p.then(()=>"settled",()=>"settled"),Promise.resolve(sentinel)])!==sentinel}export function resolveS3ClientOptions(config){const options={};if(config.endpoint)options.endpoint=config.endpoint.replace(/^https:\/\//i,"").replace(/\/+$/,"");if(config.usePathStyleEndpoint)options.forcePathStyle=!0;if(config.credentials)options.credentials=config.credentials;return Object.keys(options).length>0?options:void 0}export class S3StorageAdapter{_client;_clientPromise=null;bucket;prefix;region;endpoint;usePathStyleEndpoint;publicBaseUrl;credentials;constructor(client,config){this._client=client;this.bucket=config.bucket||"";this.prefix=config.prefix||"";this.region=config.region||"us-east-1";this.credentials=config.credentials;this.endpoint=config.endpoint;this.usePathStyleEndpoint=config.usePathStyleEndpoint;this.publicBaseUrl=config.url?.replace(/\/+$/,"");if(!this.bucket)throw Error("S3 bucket name is required")}async getClient(){if(this._client)return this._client;if(!this._clientPromise)this._clientPromise=import("@stacksjs/ts-cloud").then((cloud)=>{this._client=new cloud.S3Client(this.region,void 0,resolveS3ClientOptions({endpoint:this.endpoint,usePathStyleEndpoint:this.usePathStyleEndpoint,credentials:this.credentials}));return this._client});return this._clientPromise}resolveCredentials(){if(this.credentials?.accessKeyId&&this.credentials.secretAccessKey)return this.credentials;const accessKeyId=process.env.AWS_ACCESS_KEY_ID,secretAccessKey=process.env.AWS_SECRET_ACCESS_KEY,sessionToken=process.env.AWS_SESSION_TOKEN;if(!accessKeyId||!secretAccessKey)throw Error("[storage/s3] presignedUploadPolicy requires AWS credentials - pass them via S3DiskConfig.credentials or set AWS_ACCESS_KEY_ID + AWS_SECRET_ACCESS_KEY.");return{accessKeyId,secretAccessKey,sessionToken}}prefixPath(path){if(!this.prefix)return path;return`${this.prefix}/${path}`.replace(/\/+/g,"/")}stripPrefix(path){if(!this.prefix)return path;const prefixWithSlash=`${this.prefix}/`;return path.startsWith(prefixWithSlash)?path.slice(prefixWithSlash.length):path}async contentsToBuffer(contents){if(typeof contents==="string")return Buffer.from(contents,"utf8");else if(contents instanceof Buffer)return contents;else if(contents instanceof Uint8Array)return Buffer.from(contents);else{if(typeof contents.getReader!=="function")throw TypeError("[storage/s3] 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)}return Buffer.concat(chunks.map((c)=>Buffer.from(c)))}}async write(path,contents){const key=this.prefixPath(path),body=await this.contentsToBuffer(contents),contentType=this.detectMimeType(path);await(await this.getClient()).putObject({bucket:this.bucket,key,body,contentType});return{path,size:body.length,contentType,lastModified:Date.now()}}async read(path){const key=this.prefixPath(path),response=await(await this.getClient()).getObjectBytes(this.bucket,key);if(!response?.body)throw Error(`Failed to read file: ${path}`);return Buffer.from(response.body)}async getStream(path,_options){const key=this.prefixPath(path),buf=await(await this.getClient()).getObjectBuffer(this.bucket,key);if(!buf)throw Error(`Failed to read file: ${path}`);const bytes=new Uint8Array(buf);return new ReadableStream({start(controller){controller.enqueue(bytes);controller.close()}})}async putStream(path,stream,options){const key=this.prefixPath(path),contentType=options?.contentType??this.detectMimeType(path),partSize=clampPartSize(options?.partSize??5242880),concurrency=Math.max(1,Math.min(options?.concurrency??4,100)),maxRetries=Math.max(0,options?.maxRetries??3),signal=options?.signal,reader=stream.getReader();let firstChunk=null,firstDone=!1;{const buf=new ChunkBuffer(partSize);while(!firstDone&&buf.length<partSize){if(signal?.aborted){try{reader.releaseLock()}catch{}throw Error("aborted")}const{value,done}=await reader.read();if(done){firstDone=!0;break}if(value)buf.push(value)}firstChunk=buf.flush()}if(firstDone){try{reader.releaseLock()}catch{}await(await this.getClient()).putObject({bucket:this.bucket,key,body:Buffer.from(firstChunk),contentType});return{path,size:firstChunk.length,contentType,lastModified:Date.now()}}const{UploadId:uploadId}=await(await this.getClient()).createMultipartUpload(this.bucket,key,{contentType}),completedParts=[];let totalBytes=0,partNumber=1;const inflight=[],uploadOne=async(body,n)=>{let attempt=0;while(!0){if(signal?.aborted)throw Error("aborted");try{const{ETag}=await(await this.getClient()).uploadPart(this.bucket,key,uploadId,n,Buffer.from(body));completedParts.push({PartNumber:n,ETag});totalBytes+=body.length;return}catch(err){if(attempt>=maxRetries)throw err;attempt+=1}}};try{inflight.push(uploadOne(firstChunk,partNumber++));firstChunk=null;const buf=new ChunkBuffer(partSize);while(!0){if(signal?.aborted)throw Error("aborted");const{value,done}=await reader.read();if(done)break;if(value)buf.push(value);while(buf.length>=partSize){const part=buf.take(partSize);if(inflight.length>=concurrency){await Promise.race(inflight.map((p,i)=>p.then(()=>i)));for(let i=inflight.length-1;i>=0;i--)if(await isSettled(inflight[i]))inflight.splice(i,1)}inflight.push(uploadOne(part,partNumber++))}}try{reader.releaseLock()}catch{}const tail=buf.flush();if(tail.length>0)inflight.push(uploadOne(tail,partNumber++));await Promise.all(inflight);completedParts.sort((a,b)=>a.PartNumber-b.PartNumber);await(await this.getClient()).completeMultipartUpload(this.bucket,key,uploadId,completedParts);return{path,size:totalBytes,contentType,lastModified:Date.now()}}catch(err){try{await(await this.getClient()).abortMultipartUpload(this.bucket,key,uploadId)}catch{}throw err}}async readToString(path){const key=this.prefixPath(path),response=await(await this.getClient()).getObject(this.bucket,key);if(!response)throw Error(`Failed to read file: ${path}`);return response}async readToBuffer(path){return await this.read(path)}async readToUint8Array(path){const buffer=await this.readToBuffer(path);return new Uint8Array(buffer)}async deleteFile(path){const key=this.prefixPath(path);await(await this.getClient()).deleteObject(this.bucket,key)}async deleteDirectory(path){const prefix=this.prefixPath(path).replace(/\/+$/,""),normalizedPrefix=prefix?`${prefix}/`:void 0,keys=(await(await this.getClient()).listAllObjects({bucket:this.bucket,prefix:normalizedPrefix})).map((obj)=>obj.Key).filter((k)=>typeof k==="string");if(keys.length===0)return;await(await this.getClient()).deleteObjects(this.bucket,keys)}async createDirectory(_path){}async moveFile(from,to){await this.copyFile(from,to);await this.deleteFile(from)}async copyFile(from,to){const fromKey=this.prefixPath(from),toKey=this.prefixPath(to);await(await this.getClient()).copyObject({sourceBucket:this.bucket,sourceKey:fromKey,destinationBucket:this.bucket,destinationKey:toKey})}async stat(path){const key=this.prefixPath(path),result=await(await this.getClient()).headObject(this.bucket,key);if(!result)throw Error(`File not found: ${path}`);return{path,type:"file",visibility:"private",size:result.ContentLength||0,lastModified:result.LastModified?new Date(result.LastModified).getTime():Date.now(),mimeType:result.ContentType}}async listPage(path,options={}){const prefix=this.prefixPath(path).replace(/\/+$/,""),result=await(await this.getClient()).listObjects({bucket:this.bucket,prefix:prefix?`${prefix}/`:void 0,delimiter:"/",maxKeys:directoryPageSize(options),continuationToken:options.cursor||void 0});return{entries:[...(result.objects||[]).filter((obj)=>obj.Key!==`${prefix}/`).map((obj)=>({path:this.stripPrefix(obj.Key),type:"file"})),...(result.commonPrefixes||[]).map((folder)=>({path:this.stripPrefix(folder.replace(/\/$/,"")),type:"directory"}))],nextCursor:result.nextContinuationToken||null}}list(path,options={}){return this.createAsyncIterator(path,options.deep||!1)}async*createAsyncIterator(path,deep){const prefix=this.prefixPath(path).replace(/\/+$/,""),normalizedPrefix=prefix?`${prefix}/`:void 0;if(deep){const objects=await(await this.getClient()).listAllObjects({bucket:this.bucket,prefix:normalizedPrefix});for(const obj of objects)yield{path:this.stripPrefix(obj.Key),type:"file"}}else{let continuationToken;do{const result=await(await this.getClient()).listObjects({bucket:this.bucket,prefix:normalizedPrefix,delimiter:"/",continuationToken});for(const obj of result.objects||[]){if(obj.Key===normalizedPrefix)continue;yield{path:this.stripPrefix(obj.Key),type:"file"}}for(const folder of result.commonPrefixes||[])yield{path:this.stripPrefix(folder.replace(/\/$/,"")),type:"directory"};continuationToken=result.nextContinuationToken}while(continuationToken)}}async changeVisibility(path,vis){const key=this.prefixPath(path),acl=vis==="public"?"public-read":"private";await(await this.getClient()).putObjectAcl(this.bucket,key,acl)}async visibility(path){const key=this.prefixPath(path),grant=(await(await this.getClient()).getObjectAcl(this.bucket,key))?.AccessControlList?.Grant;return(grant===void 0?[]:Array.isArray(grant)?grant:[grant]).some((g)=>g.Grantee?.URI==="http://acs.amazonaws.com/groups/global/AllUsers"&&(g.Permission==="READ"||g.Permission==="FULL_CONTROL"))?"public":"private"}async fileExists(path){const key=this.prefixPath(path);try{return!!await(await this.getClient()).headObject(this.bucket,key)}catch(error){if(!error.message?.includes("404")&&!error.message?.includes("NoSuchKey")&&!error.message?.includes("NotFound"))console.debug(`[s3] Unexpected error checking file existence for ${path}: ${error.message}`);return!1}}async directoryExists(path){const prefix=this.prefixPath(path);return((await(await this.getClient()).listObjects({bucket:this.bucket,prefix:`${prefix}/`,maxKeys:1})).objects||[]).length>0}async publicUrl(path,options={}){const key=this.prefixPath(path),base=options.domain?.replace(/\/+$/,"")??this.publicBaseUrl;if(base)return`${base}/${key}`;if(this.endpoint)return`${this.endpointBaseUrl()}/${key}`;return`https://${this.bucket}.s3.${this.region}.amazonaws.com/${key}`}endpointBaseUrl(){const endpoint=this.endpoint.replace(/\/+$/,""),withScheme=/^https?:\/\//.test(endpoint)?endpoint:`https://${endpoint}`;if(this.usePathStyleEndpoint)return`${withScheme}/${this.bucket}`;const url=new URL(withScheme);url.hostname=`${this.bucket}.${url.hostname}`;return url.origin}async temporaryUrl(path,options){const key=this.prefixPath(path),expiresIn=Math.floor(normalizeExpiryToMilliseconds(options.expiresIn)/1000),MIN_EXPIRY=60,MAX_EXPIRY=604800;if(!Number.isFinite(expiresIn)||expiresIn<MIN_EXPIRY||expiresIn>MAX_EXPIRY)throw RangeError(`[storage/s3] temporaryUrl expiresIn must be between 60s and 7 days (got ${expiresIn}s)`);return await(await this.getClient()).getSignedUrl({bucket:this.bucket,key,expiresIn,operation:"getObject"})}async signedUrl(path,options){return this.temporaryUrl(path,{expiresIn:options.expiresIn})}async presignedUploadUrl(options){if(!options.contentType)throw Error("[storage/s3] presignedUploadUrl requires `contentType` - S3 signs against the exact header.");const expiresIn=Math.floor(options.expiresIn),MIN_EXPIRY=60,MAX_EXPIRY=604800;if(!Number.isFinite(expiresIn)||expiresIn<MIN_EXPIRY||expiresIn>MAX_EXPIRY)throw RangeError(`[storage/s3] presignedUploadUrl expiresIn must be between 60s and 7 days (got ${expiresIn}s)`);const safeDir=sanitizePresignedDir(options.dir),safeFilename=options.filename!==void 0?sanitizePresignedFilename(options.filename):`${crypto.randomUUID().replace(/-/g,"")}${this.extensionForContentType(options.contentType)}`,path=safeDir?`${safeDir}/${safeFilename}`:safeFilename,key=this.prefixPath(path);return{url:await(await this.getClient()).getSignedUrl({bucket:this.bucket,key,expiresIn,operation:"putObject"}),path,key,contentType:options.contentType,maxBytes:options.maxBytes}}async presignedUploadPolicy(options){const credentials=this.resolveCredentials(),scopedKey=typeof options.key==="string"?this.prefixPath(options.key):{startsWith:this.prefixPath(options.key.startsWith)};return signS3PresignedPost({bucket:this.bucket,region:this.region,credentials,key:scopedKey,contentType:options.contentType,contentLengthRange:options.contentLengthRange,acl:options.acl,expiresIn:options.expiresIn,fields:options.fields})}extensionForContentType(contentType){return extensionForContentType(contentType)}async checksum(path,options={}){const algorithm=options.algorithm||"sha256",content=await this.readToUint8Array(path),hasher=new Bun.CryptoHasher(algorithm);hasher.update(content);return hasher.digest("hex")}async mimeType(path,_options={}){return(await this.stat(path)).mimeType||this.detectMimeType(path)}detectMimeType(path){return mimeFromExtension(path)}async lastModified(path){return(await this.stat(path)).lastModified}async fileSize(path){return(await this.stat(path)).size}}export function createS3Storage(client,config){return new S3StorageAdapter(client,config)}
@@ -0,0 +1,7 @@
1
+ import type { DirectoryPage, DirectoryPageOptions } from './types';
2
+ export declare function directoryPageSize(options: DirectoryPageOptions): number;
3
+ /** Shallow pages only. Cookies are invalidated on any directory mutation, never silently skipped. */
4
+ export declare function localDirectoryPage(fullPath: string, path: string, options?: DirectoryPageOptions): Promise<DirectoryPage>;
5
+ export declare class DirectoryListingChangedError extends Error {
6
+ constructor();
7
+ }
@@ -0,0 +1 @@
1
+ import{stat}from"node:fs/promises";import{join}from"node:path";import{Buffer}from"node:buffer";export class DirectoryListingChangedError extends Error{constructor(){super("The directory changed during inventory. Restart this directory with a fresh cursor.");this.name="DirectoryListingChangedError"}}export function directoryPageSize(options){const size=options.limit??100;if(!Number.isInteger(size)||size<1||size>1000)throw TypeError("A directory page limit must be an integer from 1 to 1000");return size}export async function localDirectoryPage(fullPath,path,options={}){const limit=directoryPageSize(options);if(process.platform!=="darwin"&&process.platform!=="linux")throw Error("Durable local directory pages require a POSIX host");let before;try{before=await stat(fullPath,{bigint:!0})}catch(error){if(error.code==="ENOENT"&&!options.cursor)return{entries:[],nextCursor:null};if(error.code==="ENOENT")throw new DirectoryListingChangedError;throw error}if(!before.isDirectory())throw Error("Directory pages require a directory");const identity=[fullPath,before.dev,before.ino,before.mtimeNs,before.ctimeNs].join(":");let block=0n,skip=0;if(options.cursor){let decoded;try{if(options.cursor.length>8192)throw TypeError("Invalid directory cursor");decoded=JSON.parse(Buffer.from(options.cursor,"base64url").toString())}catch{throw TypeError("Invalid directory cursor")}if(decoded?.v!==1||typeof decoded.block!=="string"||!/^\d{1,19}$/.test(decoded.block)||BigInt(decoded.block)>9223372036854775807n||!Number.isInteger(decoded.skip)||decoded.skip<0||decoded.skip>32768)throw TypeError("Invalid directory cursor");if(decoded.identity!==identity)throw new DirectoryListingChangedError;block=BigInt(decoded.block);skip=decoded.skip}const{dlopen,FFIType,toArrayBuffer}=await import("bun:ffi"),darwin=process.platform==="darwin",errnoName=darwin?"__error":"__errno_location",readName=darwin?"__getdirentries64":"getdents64",libc=dlopen(darwin?"/usr/lib/libSystem.B.dylib":"libc.so.6",{open:{args:[FFIType.cstring,FFIType.i32],returns:FFIType.i32},lseek:{args:[FFIType.i32,FFIType.i64,FFIType.i32],returns:FFIType.i64},[readName]:{args:darwin?[FFIType.i32,FFIType.ptr,FFIType.u64,FFIType.ptr]:[FFIType.i32,FFIType.ptr,FFIType.u64],returns:FFIType.i64},close:{args:[FFIType.i32],returns:FFIType.i32},[errnoName]:{args:[],returns:FFIType.ptr}}),api=libc.symbols;let descriptor=-1;try{const errno=new Int32Array(toArrayBuffer(api[errnoName](),0,4));descriptor=api.open(Buffer.from(`${fullPath}\x00`),0);if(descriptor<0)throw Error(`Cannot open directory for inventory (errno ${errno[0]})`);if(BigInt(api.lseek(descriptor,block,0))<0n)throw Error(`Cannot resume directory inventory (errno ${errno[0]})`);const entries=[],buffer=new Uint8Array(32768),view=new DataView(buffer.buffer),base=new BigInt64Array(1);let ended=!1,scanned=0;while(scanned<limit){const size=Number(darwin?api[readName](descriptor,buffer,buffer.length,base):api[readName](descriptor,buffer,buffer.length));if(size<0)throw Error(`Cannot read directory for inventory (errno ${errno[0]})`);if(skip>size||!size&&skip)throw TypeError("Invalid directory cursor boundary");if(!size){ended=!0;break}let offset=0;while(offset<size&&scanned<limit){const length=view.getUint16(offset+16,!0),nameOffset=darwin?21:19;if(length<=nameOffset||offset+length>size)throw Error("Invalid native directory entry");const next=offset+length;if(offset<skip){if(next>skip)throw TypeError("Invalid directory cursor boundary");offset=next;continue}scanned++;const nameBytes=buffer.subarray(offset+nameOffset,next),end=nameBytes.indexOf(0);if(end<0)throw Error("Invalid native directory name");const name=Buffer.from(nameBytes.subarray(0,end)).toString(),kind=buffer[offset+(darwin?20:18)];offset=next;if(name==="."||name==="..")continue;if(kind===4||kind===8)entries.push({path:join(path,name),type:kind===4?"directory":"file"});else if(kind===0){const{lstat}=await import("node:fs/promises"),info=await lstat(join(fullPath,name));if(info.isFile()||info.isDirectory())entries.push({path:join(path,name),type:info.isDirectory()?"directory":"file"})}}skip=offset;if(offset===size){block=BigInt(api.lseek(descriptor,0n,1));if(block<0n)throw Error(`Cannot checkpoint directory inventory (errno ${errno[0]})`);skip=0}}const after=await stat(fullPath,{bigint:!0});if([fullPath,after.dev,after.ino,after.mtimeNs,after.ctimeNs].join(":")!==identity)throw new DirectoryListingChangedError;return{entries,nextCursor:ended?null:Buffer.from(JSON.stringify({v:1,identity,block:String(block),skip})).toString("base64url")}}finally{if(descriptor>=0)api.close(descriptor);libc.close()}}
package/dist/index.d.ts CHANGED
@@ -65,3 +65,4 @@ export { detectMimeFromMagicBytes, verifyUploadedMime } from './mime-verify';
65
65
  // and just need the policy-signing logic; the Storage facade wraps
66
66
  // it as `presignedUploadPolicy()`.
67
67
  export { signS3PresignedPost } from './s3-presigned-post';
68
+ export { DirectoryListingChangedError } from './directory-page';
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- export*from"./copy";export*from"./delete";export*from"./files";export*from"./bounded-text-file";export*from"./completed-artifact-directory";export*from"./folders";export*from"./fs";export*from"./temporary-workspaces";export*from"./glob";export*from"./hash";export*from"./helpers";export* as storage from"./storage";export*from"./zip";export{ensureBucket,ensureConfiguredBuckets}from"./provision";export*from"./adapters";export*from"./types";export*from"./drivers";export{serveFile}from"./static-serve";export{Storage,StorageManager}from"./facade";export{UploadedFile,uploadedFile,uploadedFiles}from"./uploaded-file";export{azureDisk,backblazeDisk,configFromEnv,filebaseDisk,gcsDisk,hetznerDisk,localDisk,r2Disk,s3Disk}from"./types/filesystem";export{clearRevokedSignedStorageTokens,createSignedStorageToken,isSignedStorageTokenRevoked,revokeSignedStorageToken,verifySignedStorageToken}from"./signed-url";export{parseDiskPath,PathSanitizeError,sanitizePresignedDir,sanitizePresignedFilename}from"./path-sanitize";export{fileInfo,fileInfoFromBytes,fileKind,isPreviewable,resolveMime}from"./file-info";export{detectMimeFromMagicBytes,verifyUploadedMime}from"./mime-verify";export{signS3PresignedPost}from"./s3-presigned-post";
1
+ export*from"./copy";export*from"./delete";export*from"./files";export*from"./bounded-text-file";export*from"./completed-artifact-directory";export*from"./folders";export*from"./fs";export*from"./temporary-workspaces";export*from"./glob";export*from"./hash";export*from"./helpers";export* as storage from"./storage";export*from"./zip";export{ensureBucket,ensureConfiguredBuckets}from"./provision";export*from"./adapters";export*from"./types";export*from"./drivers";export{serveFile}from"./static-serve";export{Storage,StorageManager}from"./facade";export{UploadedFile,uploadedFile,uploadedFiles}from"./uploaded-file";export{azureDisk,backblazeDisk,configFromEnv,filebaseDisk,gcsDisk,hetznerDisk,localDisk,r2Disk,s3Disk}from"./types/filesystem";export{clearRevokedSignedStorageTokens,createSignedStorageToken,isSignedStorageTokenRevoked,revokeSignedStorageToken,verifySignedStorageToken}from"./signed-url";export{parseDiskPath,PathSanitizeError,sanitizePresignedDir,sanitizePresignedFilename}from"./path-sanitize";export{fileInfo,fileInfoFromBytes,fileKind,isPreviewable,resolveMime}from"./file-info";export{detectMimeFromMagicBytes,verifyUploadedMime}from"./mime-verify";export{signS3PresignedPost}from"./s3-presigned-post";export{DirectoryListingChangedError}from"./directory-page";
package/dist/types.d.ts CHANGED
@@ -86,6 +86,9 @@ export declare interface DirectoryListing extends AsyncIterable<DirectoryEntry>
86
86
  /**
87
87
  * Options for listing directories
88
88
  */
89
+ /** A bounded shallow page. Persist the opaque cursor only after processing the whole page. */
90
+ export declare interface DirectoryPageOptions { cursor?: string | null, limit?: number }
91
+ export declare interface DirectoryPage { entries: DirectoryEntry[], nextCursor: string | null }
89
92
  export declare interface ListOptions {
90
93
  deep?: boolean
91
94
  }
@@ -205,6 +208,7 @@ export declare interface StorageAdapter {
205
208
  copyFile(from: string, to: string): Promise<void>
206
209
  stat(path: string): Promise<StatEntry>
207
210
  list(path: string, options?: ListOptions): DirectoryListing
211
+ listPage?(path: string, options?: DirectoryPageOptions): Promise<DirectoryPage>
208
212
  changeVisibility(path: string, visibility: Visibility): Promise<void>
209
213
  visibility(path: string): Promise<Visibility>
210
214
  fileExists(path: string): Promise<boolean>
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.468",
5
+ "version": "0.75.473",
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.468",
65
- "@stacksjs/cli": "0.75.468",
66
- "@stacksjs/config": "0.75.468",
67
- "@stacksjs/env": "0.75.468",
68
- "@stacksjs/error-handling": "0.75.468",
69
- "@stacksjs/logging": "0.75.468",
70
- "@stacksjs/path": "0.75.468",
71
- "@stacksjs/strings": "0.75.468",
72
- "@stacksjs/ts-cloud": "^0.16.91",
64
+ "@stacksjs/arrays": "0.75.473",
65
+ "@stacksjs/cli": "0.75.473",
66
+ "@stacksjs/config": "0.75.473",
67
+ "@stacksjs/env": "0.75.473",
68
+ "@stacksjs/error-handling": "0.75.473",
69
+ "@stacksjs/logging": "0.75.473",
70
+ "@stacksjs/path": "0.75.473",
71
+ "@stacksjs/strings": "0.75.473",
72
+ "@stacksjs/ts-cloud": "^0.16.92",
73
73
  "@stacksjs/ts-xml": "^0.1.0",
74
74
  "ts-images": "^0.2.33"
75
75
  },
76
76
  "devDependencies": {
77
- "@stacksjs/types": "0.75.468",
77
+ "@stacksjs/types": "0.75.473",
78
78
  "better-dx": "^0.2.24"
79
79
  }
80
80
  }