@simplysm/storage 13.0.97 → 13.0.98

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.
Files changed (2) hide show
  1. package/README.md +179 -0
  2. package/package.json +2 -2
package/README.md ADDED
@@ -0,0 +1,179 @@
1
+ # @simplysm/storage
2
+
3
+ Storage Module (node) -- FTP, FTPS, and SFTP storage client with a unified interface and factory pattern.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @simplysm/storage
9
+ ```
10
+
11
+ ## API Overview
12
+
13
+ ### Types
14
+
15
+ | API | Type | Description |
16
+ |-----|------|-------------|
17
+ | `StorageProtocol` | type | Protocol type: `"ftp"`, `"ftps"`, `"sftp"` |
18
+ | `StorageConnConfig` | interface | Connection configuration (host, port, user, password) |
19
+ | `FileInfo` | interface | File entry info (`name`, `isFile`) |
20
+ | `StorageClient` | interface | Unified storage client interface |
21
+
22
+ ### Factory
23
+
24
+ | API | Type | Description |
25
+ |-----|------|-------------|
26
+ | `StorageFactory` | class | Factory that creates and manages storage connections |
27
+
28
+ ### Clients
29
+
30
+ | API | Type | Description |
31
+ |-----|------|-------------|
32
+ | `FtpStorageClient` | class | FTP/FTPS storage client (basic-ftp) |
33
+ | `SftpStorageClient` | class | SFTP storage client (ssh2-sftp-client) |
34
+
35
+ ## `StorageProtocol`
36
+
37
+ ```typescript
38
+ type StorageProtocol = "ftp" | "ftps" | "sftp";
39
+ ```
40
+
41
+ ## `StorageConnConfig`
42
+
43
+ ```typescript
44
+ interface StorageConnConfig {
45
+ host: string;
46
+ port?: number;
47
+ user?: string;
48
+ password?: string;
49
+ }
50
+ ```
51
+
52
+ ## `FileInfo`
53
+
54
+ ```typescript
55
+ interface FileInfo {
56
+ name: string;
57
+ isFile: boolean;
58
+ }
59
+ ```
60
+
61
+ ## `StorageClient`
62
+
63
+ ```typescript
64
+ interface StorageClient {
65
+ connect(config: StorageConnConfig): Promise<void>;
66
+ mkdir(dirPath: string): Promise<void>;
67
+ rename(fromPath: string, toPath: string): Promise<void>;
68
+ list(dirPath: string): Promise<FileInfo[]>;
69
+ readFile(filePath: string): Promise<Bytes>;
70
+ exists(filePath: string): Promise<boolean>;
71
+ put(localPathOrBuffer: string | Bytes, storageFilePath: string): Promise<void>;
72
+ uploadDir(fromPath: string, toPath: string): Promise<void>;
73
+ remove(filePath: string): Promise<void>;
74
+ close(): Promise<void>;
75
+ }
76
+ ```
77
+
78
+ ## `StorageFactory`
79
+
80
+ ```typescript
81
+ class StorageFactory {
82
+ static async connect<R>(
83
+ type: StorageProtocol,
84
+ config: StorageConnConfig,
85
+ fn: (storage: StorageClient) => R | Promise<R>,
86
+ ): Promise<R>;
87
+ }
88
+ ```
89
+
90
+ Creates a storage connection, executes the callback, and automatically closes the connection. The connection is closed even if the callback throws an exception. This is the recommended way to use storage clients.
91
+
92
+ ## `FtpStorageClient`
93
+
94
+ ```typescript
95
+ class FtpStorageClient implements StorageClient {
96
+ constructor(secure?: boolean);
97
+ async connect(config: StorageConnConfig): Promise<void>;
98
+ async mkdir(dirPath: string): Promise<void>;
99
+ async rename(fromPath: string, toPath: string): Promise<void>;
100
+ async list(dirPath: string): Promise<FileInfo[]>;
101
+ async readFile(filePath: string): Promise<Bytes>;
102
+ async exists(filePath: string): Promise<boolean>;
103
+ async put(localPathOrBuffer: string | Bytes, storageFilePath: string): Promise<void>;
104
+ async uploadDir(fromPath: string, toPath: string): Promise<void>;
105
+ async remove(filePath: string): Promise<void>;
106
+ close(): Promise<void>;
107
+ }
108
+ ```
109
+
110
+ FTP/FTPS storage client. The `secure` constructor parameter controls FTPS mode. Use `StorageFactory.connect` instead of direct usage for automatic connection lifecycle management.
111
+
112
+ ## `SftpStorageClient`
113
+
114
+ ```typescript
115
+ class SftpStorageClient implements StorageClient {
116
+ async connect(config: StorageConnConfig): Promise<void>;
117
+ async mkdir(dirPath: string): Promise<void>;
118
+ async rename(fromPath: string, toPath: string): Promise<void>;
119
+ async list(dirPath: string): Promise<FileInfo[]>;
120
+ async readFile(filePath: string): Promise<Bytes>;
121
+ async exists(filePath: string): Promise<boolean>;
122
+ async put(localPathOrBuffer: string | Bytes, storageFilePath: string): Promise<void>;
123
+ async uploadDir(fromPath: string, toPath: string): Promise<void>;
124
+ async remove(filePath: string): Promise<void>;
125
+ async close(): Promise<void>;
126
+ }
127
+ ```
128
+
129
+ SFTP storage client. Supports password authentication and SSH key/agent authentication. Use `StorageFactory.connect` instead of direct usage for automatic connection lifecycle management.
130
+
131
+ ## Usage Examples
132
+
133
+ ### Upload files via StorageFactory (recommended)
134
+
135
+ ```typescript
136
+ import { StorageFactory } from "@simplysm/storage";
137
+
138
+ await StorageFactory.connect("sftp", {
139
+ host: "example.com",
140
+ user: "deploy",
141
+ password: "secret",
142
+ }, async (storage) => {
143
+ await storage.mkdir("/var/www/app");
144
+ await storage.put("/local/dist/bundle.js", "/var/www/app/bundle.js");
145
+ });
146
+ // Connection is automatically closed
147
+ ```
148
+
149
+ ### List remote directory
150
+
151
+ ```typescript
152
+ import { StorageFactory } from "@simplysm/storage";
153
+
154
+ const files = await StorageFactory.connect("ftp", {
155
+ host: "files.example.com",
156
+ user: "admin",
157
+ password: "pass",
158
+ }, async (storage) => {
159
+ return await storage.list("/uploads");
160
+ });
161
+
162
+ for (const file of files) {
163
+ // file.name, file.isFile
164
+ }
165
+ ```
166
+
167
+ ### Upload entire directory via FTPS
168
+
169
+ ```typescript
170
+ import { StorageFactory } from "@simplysm/storage";
171
+
172
+ await StorageFactory.connect("ftps", {
173
+ host: "secure.example.com",
174
+ user: "deploy",
175
+ password: "secret",
176
+ }, async (storage) => {
177
+ await storage.uploadDir("/local/dist", "/remote/app");
178
+ });
179
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simplysm/storage",
3
- "version": "13.0.97",
3
+ "version": "13.0.98",
4
4
  "description": "Simplysm Package - Storage Module (node)",
5
5
  "author": "simplysm",
6
6
  "license": "Apache-2.0",
@@ -21,7 +21,7 @@
21
21
  "dependencies": {
22
22
  "basic-ftp": "^5.2.0",
23
23
  "ssh2-sftp-client": "^12.1.0",
24
- "@simplysm/core-common": "13.0.97"
24
+ "@simplysm/core-common": "13.0.98"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@types/ssh2-sftp-client": "^9.0.6"