@stacksjs/storage 0.74.40 → 0.74.41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/facade.js +1 -1
- package/package.json +10 -10
package/dist/facade.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{resolve}from"node:path";import process from"node:process";import{filesystems,app as appConfig}from"@stacksjs/config";import{AzureBlobStorageAdapter}from"./adapters/azure";import{createLocalStorage}from"./adapters/local";import{S3StorageAdapter}from"./adapters/s3";import{parseDiskPath}from"./path-sanitize";import{putUploadedFile}from"./put-file";function buildConfig(){const cwd=process.cwd(),rootDir=filesystems.root||cwd,s3Config=filesystems.s3,appUrl=(appConfig?.url||"").replace(/\/$/,""),config={default:filesystems.driver||"local",disks:{local:{driver:"local",root:resolve(rootDir,"storage/app"),visibility:filesystems.defaultVisibility||"private"},public:{driver:"local",root:resolve(rootDir,"public"),url:appUrl||"/",visibility:"public"}}};if(s3Config?.bucket)config.disks.s3={driver:"s3",bucket:s3Config.bucket,region:s3Config.region||"us-east-1",prefix:s3Config.prefix,endpoint:s3Config.endpoint,url:filesystems.publicUrl?.domain,usePathStyleEndpoint
|
|
1
|
+
import{resolve}from"node:path";import process from"node:process";import{filesystems,app as appConfig}from"@stacksjs/config";import{AzureBlobStorageAdapter}from"./adapters/azure";import{createLocalStorage}from"./adapters/local";import{S3StorageAdapter}from"./adapters/s3";import{parseDiskPath}from"./path-sanitize";import{putUploadedFile}from"./put-file";function buildConfig(){const cwd=process.cwd(),rootDir=filesystems.root||cwd,s3Config=filesystems.s3,appUrl=(appConfig?.url||"").replace(/\/$/,""),config={default:filesystems.driver||"local",disks:{local:{driver:"local",root:resolve(rootDir,"storage/app"),visibility:filesystems.defaultVisibility||"private"},public:{driver:"local",root:resolve(rootDir,"public"),url:appUrl||"/",visibility:"public"}}};if(s3Config?.bucket)config.disks.s3={driver:"s3",bucket:s3Config.bucket,region:s3Config.region||"us-east-1",prefix:s3Config.prefix,endpoint:s3Config.endpoint,url:filesystems.publicUrl?.domain,usePathStyleEndpoint:s3Config.usePathStyleEndpoint??!!s3Config.endpoint,visibility:filesystems.defaultVisibility||"private",credentials:s3Config.credentials?{key:s3Config.credentials.accessKeyId,secret:s3Config.credentials.secretAccessKey}:void 0};return config}class StorageManager{_config=null;disks=new Map;customConfig=null;get config(){if(!this._config){const builtConfig=buildConfig();this._config=this.customConfig?{default:this.customConfig.default||builtConfig.default,disks:{...builtConfig.disks,...this.customConfig.disks}}:builtConfig}return this._config}init(config){this.customConfig=config;this._config=null;this.disks.clear();return this}disk(name){const diskName=name||this.config.default;if(this.disks.has(diskName))return this.disks.get(diskName);const diskConfig=this.config.disks[diskName];if(!diskConfig){const available=Object.keys(this.config.disks).join(", ");throw Error(`Disk [${diskName}] is not configured. Available: ${available}.${diskName==="s3"?" Set `filesystems.s3.bucket` in config/filesystems.ts - the s3 disk is only built once a bucket is configured.":name===void 0?" This is the DEFAULT disk, from `filesystems.driver`; it has to name one of the disks above.":""}`)}const adapter=this.createAdapter(diskName,diskConfig);this.disks.set(diskName,adapter);return adapter}createAdapter(name,config){switch(config.driver){case"local":return this.createLocalAdapter(config);case"s3":return this.createS3Adapter(name,config);case"azure":return this.createAzureAdapter(config);default:throw Error(`Unsupported driver: ${config.driver}`)}}createLocalAdapter(config){return createLocalStorage({root:config.root,url:config.url})}createS3Adapter(_name,config){return new S3StorageAdapter(null,{bucket:config.bucket,region:config.region,prefix:config.prefix,endpoint:config.endpoint,usePathStyleEndpoint:config.usePathStyleEndpoint,url:config.url,credentials:config.credentials?{accessKeyId:config.credentials.key,secretAccessKey:config.credentials.secret}:void 0})}createAzureAdapter(config){return new AzureBlobStorageAdapter({account:config.account,accountKey:config.accountKey,sasToken:config.sasToken,container:config.container,prefix:config.prefix,url:config.url,endpoint:config.endpoint})}async put(pathOrFile,contentsOrOpts){if(typeof pathOrFile==="string")return this.disk().write(pathOrFile,contentsOrOpts);return putUploadedFile(this,pathOrFile,contentsOrOpts??{})}async stat(path){return this.disk().stat(path)}async getStream(path,options){const adapter=this.disk();if(typeof adapter.getStream!=="function")throw Error(`[storage] disk '${this.config.default}' does not support getStream - adapter is missing the optional method`);return adapter.getStream(path,options)}async putStream(path,stream,options){const adapter=this.disk();if(typeof adapter.putStream!=="function")throw Error(`[storage] disk '${this.config.default}' does not support putStream - adapter is missing the optional method`);return adapter.putStream(path,stream,options)}async copyAcross(source,dest){const src=parseDiskPath(source),dst=parseDiskPath(dest);if(src.disk===dst.disk){const adapter=this.disk(src.disk);await adapter.copyFile(src.path,dst.path);return adapter.stat(dst.path).then((entry)=>({path:dst.path,size:entry.size,contentType:entry.mimeType,lastModified:entry.lastModified}))}const contents=await this.disk(src.disk).read(src.path);return this.disk(dst.disk).write(dst.path,contents)}async moveAcross(source,dest){const src=parseDiskPath(source),result=await this.copyAcross(source,dest);await this.disk(src.disk).deleteFile(src.path);return result}async get(path){return this.disk().readToString(path)}async exists(path){return this.disk().fileExists(path)}async missing(path){return!await this.exists(path)}async delete(path){return this.disk().deleteFile(path)}async copy(from,to){return this.disk().copyFile(from,to)}async move(from,to){return this.disk().moveFile(from,to)}async url(path){return this.disk().publicUrl(path)}async signedUrl(path,options){const adapter=this.disk();if(typeof adapter.signedUrl!=="function")throw Error(`[storage] disk '${this.config.default}' does not support signedUrl`);return adapter.signedUrl(path,options)}async presignedUploadUrl(options){const adapter=this.disk();if(typeof adapter.presignedUploadUrl!=="function")throw Error(`[storage] disk '${this.config.default}' does not support presignedUploadUrl - only S3-style adapters do. Use \`Storage.put(file, opts)\` for local/proxied uploads.`);return adapter.presignedUploadUrl(options)}async presignedUploadPolicy(options){const adapter=this.disk();if(typeof adapter.presignedUploadPolicy!=="function")throw Error(`[storage] disk '${this.config.default}' does not support presignedUploadPolicy - S3-only. Use \`presignedUploadUrl\` for the PUT-form, or \`Storage.put(file, opts)\` for server-proxied uploads.`);return adapter.presignedUploadPolicy(options)}async size(path){return this.disk().fileSize(path)}async lastModified(path){return this.disk().lastModified(path)}async mimeType(path){return this.disk().mimeType(path)}async checksum(path,algorithm){return this.disk().checksum(path,{algorithm})}async makeDirectory(path){return this.disk().createDirectory(path)}async deleteDirectory(path){return this.disk().deleteDirectory(path)}files(path=""){return this.disk().list(path)}allFiles(path=""){return this.disk().list(path,{deep:!0})}configure(name,config){const currentConfig=this.config;currentConfig.disks[name]=config;this.disks.delete(name);return this}setDefaultDisk(name){if(!this.config.disks[name])throw Error(`Disk [${name}] is not configured`);this.config.default=name;return this}getDiskConfig(name){return this.config.disks[name||this.config.default]}getConfiguredDisks(){return Object.keys(this.config.disks)}getDefaultDisk(){return this.config.default}reset(){this._config=null;this.customConfig=null;this.disks.clear();return this}}export const Storage=new StorageManager;export{StorageManager};
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@stacksjs/storage",
|
|
3
3
|
"type": "module",
|
|
4
4
|
"sideEffects": false,
|
|
5
|
-
"version": "0.74.
|
|
5
|
+
"version": "0.74.41",
|
|
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.74.
|
|
65
|
-
"@stacksjs/cli": "0.74.
|
|
66
|
-
"@stacksjs/config": "0.74.
|
|
67
|
-
"@stacksjs/env": "0.74.
|
|
68
|
-
"@stacksjs/error-handling": "0.74.
|
|
69
|
-
"@stacksjs/logging": "0.74.
|
|
70
|
-
"@stacksjs/path": "0.74.
|
|
71
|
-
"@stacksjs/strings": "0.74.
|
|
64
|
+
"@stacksjs/arrays": "0.74.41",
|
|
65
|
+
"@stacksjs/cli": "0.74.41",
|
|
66
|
+
"@stacksjs/config": "0.74.41",
|
|
67
|
+
"@stacksjs/env": "0.74.41",
|
|
68
|
+
"@stacksjs/error-handling": "0.74.41",
|
|
69
|
+
"@stacksjs/logging": "0.74.41",
|
|
70
|
+
"@stacksjs/path": "0.74.41",
|
|
71
|
+
"@stacksjs/strings": "0.74.41",
|
|
72
72
|
"@stacksjs/ts-cloud": "^0.16.0",
|
|
73
73
|
"@stacksjs/ts-xml": "^0.1.0",
|
|
74
74
|
"ts-images": "^0.2.11"
|
|
75
75
|
},
|
|
76
76
|
"devDependencies": {
|
|
77
|
-
"@stacksjs/types": "0.74.
|
|
77
|
+
"@stacksjs/types": "0.74.41",
|
|
78
78
|
"better-dx": "^0.2.24"
|
|
79
79
|
}
|
|
80
80
|
}
|