@simplysm/storage 13.0.98 → 13.0.99
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 +72 -109
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @simplysm/storage
|
|
2
2
|
|
|
3
|
-
Storage Module (node)
|
|
3
|
+
Simplysm Package - Storage Module (node). Provides FTP, FTPS, and SFTP storage client implementations with a factory for managed connections.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -14,123 +14,90 @@ npm install @simplysm/storage
|
|
|
14
14
|
|
|
15
15
|
| API | Type | Description |
|
|
16
16
|
|-----|------|-------------|
|
|
17
|
-
| `
|
|
18
|
-
| `
|
|
19
|
-
| `
|
|
20
|
-
| `
|
|
17
|
+
| `StorageConnConfig` | interface | Storage connection configuration |
|
|
18
|
+
| `FileInfo` | interface | File/directory entry information |
|
|
19
|
+
| `StorageClient` | interface | Storage client interface |
|
|
20
|
+
| `StorageProtocol` | type | Protocol type (`"ftp" \| "ftps" \| "sftp"`) |
|
|
21
21
|
|
|
22
|
-
###
|
|
22
|
+
### Clients
|
|
23
23
|
|
|
24
24
|
| API | Type | Description |
|
|
25
25
|
|-----|------|-------------|
|
|
26
|
-
| `
|
|
26
|
+
| `FtpStorageClient` | class | FTP/FTPS storage client (uses basic-ftp) |
|
|
27
|
+
| `SftpStorageClient` | class | SFTP storage client (uses ssh2-sftp-client) |
|
|
27
28
|
|
|
28
|
-
###
|
|
29
|
+
### Factory
|
|
29
30
|
|
|
30
31
|
| API | Type | Description |
|
|
31
32
|
|-----|------|-------------|
|
|
32
|
-
| `
|
|
33
|
-
| `SftpStorageClient` | class | SFTP storage client (ssh2-sftp-client) |
|
|
33
|
+
| `StorageFactory` | class | Storage client factory with managed connections |
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
### `StorageProtocol`
|
|
36
38
|
|
|
37
39
|
```typescript
|
|
38
|
-
type StorageProtocol = "ftp" | "ftps" | "sftp"
|
|
40
|
+
type StorageProtocol = "ftp" | "ftps" | "sftp"
|
|
39
41
|
```
|
|
40
42
|
|
|
41
|
-
|
|
43
|
+
### `StorageConnConfig`
|
|
42
44
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
```
|
|
45
|
+
| Field | Type | Description |
|
|
46
|
+
|-------|------|-------------|
|
|
47
|
+
| `host` | `string` | Server hostname |
|
|
48
|
+
| `port` | `number?` | Server port |
|
|
49
|
+
| `user` | `string?` | Username |
|
|
50
|
+
| `password` | `string?` | Password (SFTP: if omitted, uses SSH agent + key file) |
|
|
51
51
|
|
|
52
|
-
|
|
52
|
+
### `FileInfo`
|
|
53
53
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
```
|
|
54
|
+
| Field | Type | Description |
|
|
55
|
+
|-------|------|-------------|
|
|
56
|
+
| `name` | `string` | File or directory name |
|
|
57
|
+
| `isFile` | `boolean` | Whether the entry is a file |
|
|
60
58
|
|
|
61
|
-
|
|
59
|
+
### `StorageClient`
|
|
62
60
|
|
|
63
|
-
|
|
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
|
-
```
|
|
61
|
+
Interface implemented by both `FtpStorageClient` and `SftpStorageClient`.
|
|
77
62
|
|
|
78
|
-
|
|
63
|
+
| Method | Signature | Description |
|
|
64
|
+
|--------|-----------|-------------|
|
|
65
|
+
| `connect` | `(config: StorageConnConfig) => Promise<void>` | Connect to server |
|
|
66
|
+
| `mkdir` | `(dirPath: string) => Promise<void>` | Create directory (recursive) |
|
|
67
|
+
| `rename` | `(fromPath: string, toPath: string) => Promise<void>` | Rename file/directory |
|
|
68
|
+
| `list` | `(dirPath: string) => Promise<FileInfo[]>` | List directory contents |
|
|
69
|
+
| `readFile` | `(filePath: string) => Promise<Bytes>` | Read file |
|
|
70
|
+
| `exists` | `(filePath: string) => Promise<boolean>` | Check existence |
|
|
71
|
+
| `put` | `(localPathOrBuffer: string \| Bytes, storageFilePath: string) => Promise<void>` | Upload file (local path or byte data) |
|
|
72
|
+
| `uploadDir` | `(fromPath: string, toPath: string) => Promise<void>` | Upload entire directory |
|
|
73
|
+
| `remove` | `(filePath: string) => Promise<void>` | Delete file |
|
|
74
|
+
| `close` | `() => Promise<void>` | Close connection |
|
|
79
75
|
|
|
80
|
-
|
|
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
|
-
```
|
|
76
|
+
### `FtpStorageClient`
|
|
89
77
|
|
|
90
|
-
|
|
78
|
+
Implements `StorageClient` using FTP/FTPS protocol via the `basic-ftp` library.
|
|
91
79
|
|
|
92
|
-
|
|
80
|
+
| Method | Signature | Description |
|
|
81
|
+
|--------|-----------|-------------|
|
|
82
|
+
| `constructor` | `(secure?: boolean)` | Create client (`true` for FTPS, `false` for FTP) |
|
|
93
83
|
|
|
94
|
-
|
|
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
|
-
```
|
|
84
|
+
All `StorageClient` methods are implemented. Using `StorageFactory.connect` is recommended over direct usage.
|
|
109
85
|
|
|
110
|
-
|
|
86
|
+
### `SftpStorageClient`
|
|
111
87
|
|
|
112
|
-
|
|
88
|
+
Implements `StorageClient` using SFTP protocol via the `ssh2-sftp-client` library. Supports password authentication and SSH agent + key file authentication.
|
|
113
89
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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
|
-
```
|
|
90
|
+
All `StorageClient` methods are implemented. Using `StorageFactory.connect` is recommended over direct usage.
|
|
91
|
+
|
|
92
|
+
### `StorageFactory`
|
|
128
93
|
|
|
129
|
-
|
|
94
|
+
| Method | Signature | Description |
|
|
95
|
+
|--------|-----------|-------------|
|
|
96
|
+
| `connect` | `<R>(type: StorageProtocol, config: StorageConnConfig, fn: (storage: StorageClient) => R \| Promise<R>) => Promise<R>` | Connect, execute callback, auto-close |
|
|
130
97
|
|
|
131
98
|
## Usage Examples
|
|
132
99
|
|
|
133
|
-
###
|
|
100
|
+
### Using StorageFactory (recommended)
|
|
134
101
|
|
|
135
102
|
```typescript
|
|
136
103
|
import { StorageFactory } from "@simplysm/storage";
|
|
@@ -140,40 +107,36 @@ await StorageFactory.connect("sftp", {
|
|
|
140
107
|
user: "deploy",
|
|
141
108
|
password: "secret",
|
|
142
109
|
}, async (storage) => {
|
|
143
|
-
await storage.
|
|
144
|
-
await storage.
|
|
110
|
+
await storage.uploadDir("./dist", "/var/www/app");
|
|
111
|
+
const files = await storage.list("/var/www/app");
|
|
145
112
|
});
|
|
146
|
-
// Connection is automatically closed
|
|
147
113
|
```
|
|
148
114
|
|
|
149
|
-
###
|
|
115
|
+
### Using client directly
|
|
150
116
|
|
|
151
117
|
```typescript
|
|
152
|
-
import {
|
|
153
|
-
|
|
154
|
-
const
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
for (const file of files) {
|
|
163
|
-
// file.name, file.isFile
|
|
118
|
+
import { FtpStorageClient } from "@simplysm/storage";
|
|
119
|
+
|
|
120
|
+
const client = new FtpStorageClient(true); // FTPS
|
|
121
|
+
await client.connect({ host: "ftp.example.com", user: "admin", password: "pass" });
|
|
122
|
+
try {
|
|
123
|
+
await client.put("./build.zip", "/releases/build.zip");
|
|
124
|
+
const exists = await client.exists("/releases/build.zip");
|
|
125
|
+
} finally {
|
|
126
|
+
await client.close();
|
|
164
127
|
}
|
|
165
128
|
```
|
|
166
129
|
|
|
167
|
-
###
|
|
130
|
+
### SFTP with SSH key authentication
|
|
168
131
|
|
|
169
132
|
```typescript
|
|
170
133
|
import { StorageFactory } from "@simplysm/storage";
|
|
171
134
|
|
|
172
|
-
|
|
173
|
-
|
|
135
|
+
// Omit password to use SSH agent + ~/.ssh/id_ed25519
|
|
136
|
+
await StorageFactory.connect("sftp", {
|
|
137
|
+
host: "example.com",
|
|
174
138
|
user: "deploy",
|
|
175
|
-
password: "secret",
|
|
176
139
|
}, async (storage) => {
|
|
177
|
-
await storage.
|
|
140
|
+
await storage.put(fileBytes, "/data/upload.bin");
|
|
178
141
|
});
|
|
179
142
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@simplysm/storage",
|
|
3
|
-
"version": "13.0.
|
|
3
|
+
"version": "13.0.99",
|
|
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.
|
|
24
|
+
"@simplysm/core-common": "13.0.99"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@types/ssh2-sftp-client": "^9.0.6"
|