fsd-fs 0.9.0 → 0.10.0
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 +1 -1
- package/lib/index.js +30 -32
- package/package.json +7 -9
package/README.md
CHANGED
package/lib/index.js
CHANGED
|
@@ -6,14 +6,9 @@ const Path = require("path");
|
|
|
6
6
|
const fs = require("fs");
|
|
7
7
|
const isStream = require("is-stream");
|
|
8
8
|
const _glob = require("glob");
|
|
9
|
-
const _rimraf = require("rimraf");
|
|
10
|
-
const _cpr = require("cpr");
|
|
11
9
|
const mapLimit = require("async/mapLimit");
|
|
12
10
|
const Debugger = require("debug");
|
|
13
|
-
const url_1 = require("url");
|
|
14
11
|
const glob = util.promisify(_glob);
|
|
15
|
-
const rimraf = util.promisify(_rimraf);
|
|
16
|
-
const cpr = util.promisify(_cpr);
|
|
17
12
|
const debug = Debugger('fsd-fs');
|
|
18
13
|
async function getStat(path) {
|
|
19
14
|
try {
|
|
@@ -68,19 +63,19 @@ class FSAdapter {
|
|
|
68
63
|
}
|
|
69
64
|
async createWriteStream(path, options) {
|
|
70
65
|
debug('createWriteStream %s', path);
|
|
71
|
-
let p
|
|
66
|
+
let p;
|
|
72
67
|
if (path.startsWith('task://')) {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
p = Path.join(this._options.
|
|
68
|
+
p = Path.join(this._options.tmpdir, path.replace('task://', ''));
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
p = Path.join(this._options.root, path);
|
|
77
72
|
}
|
|
78
73
|
return fs.createWriteStream(p, options);
|
|
79
74
|
}
|
|
80
75
|
async unlink(path) {
|
|
81
76
|
debug('unlink %s', path);
|
|
82
77
|
let p = Path.join(this._options.root, path);
|
|
83
|
-
await
|
|
78
|
+
await fs.promises.rm(p, { recursive: true, force: true });
|
|
84
79
|
}
|
|
85
80
|
async mkdir(path, recursive) {
|
|
86
81
|
debug('mkdir %s', path);
|
|
@@ -122,9 +117,7 @@ class FSAdapter {
|
|
|
122
117
|
let to = Path.join(root, dest);
|
|
123
118
|
if (!(await getStat(from)))
|
|
124
119
|
throw new Error(`source file '${path}' is not exists!`);
|
|
125
|
-
|
|
126
|
-
throw new Error(`dest file '${dest}' is already exists!`);
|
|
127
|
-
await cpr(from, to);
|
|
120
|
+
await fs.promises.cp(from, to, { recursive: true, force: true });
|
|
128
121
|
}
|
|
129
122
|
async rename(path, dest) {
|
|
130
123
|
debug('rename %s to %s', path, dest);
|
|
@@ -163,18 +156,17 @@ class FSAdapter {
|
|
|
163
156
|
}
|
|
164
157
|
async initMultipartUpload(path, partCount) {
|
|
165
158
|
debug('initMultipartUpload %s, partCount: %d', path, partCount);
|
|
166
|
-
let taskId = `upload-${Math.random().toString().
|
|
159
|
+
let taskId = `upload-${Math.random().toString().substring(2)}-`;
|
|
167
160
|
let tasks = [];
|
|
168
161
|
for (let i = 1; i <= partCount; i += 1) {
|
|
169
|
-
tasks.push(`task://${taskId}${i}
|
|
162
|
+
tasks.push(`task://${taskId}${i}`);
|
|
170
163
|
}
|
|
171
164
|
return tasks;
|
|
172
165
|
}
|
|
173
166
|
async writePart(path, partTask, data) {
|
|
174
167
|
debug('writePart %s, task: %s', path, partTask);
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
throw new Error('Invalid part pathname');
|
|
168
|
+
if (!partTask.startsWith('task://'))
|
|
169
|
+
throw new Error('Invalid part task id');
|
|
178
170
|
let writeStream = await this.createWriteStream(partTask);
|
|
179
171
|
await new Promise((resolve, reject) => {
|
|
180
172
|
data.pipe(writeStream).on('close', resolve).on('error', reject);
|
|
@@ -183,25 +175,31 @@ class FSAdapter {
|
|
|
183
175
|
}
|
|
184
176
|
async completeMultipartUpload(path, parts) {
|
|
185
177
|
debug('completeMultipartUpload %s', path);
|
|
186
|
-
let
|
|
178
|
+
let partPaths = [];
|
|
187
179
|
for (let part of parts) {
|
|
188
180
|
if (!part.startsWith('part://'))
|
|
189
181
|
throw new Error(`${part} is not a part file`);
|
|
190
|
-
let
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
if (info.pathname !== path)
|
|
194
|
-
throw new Error(`Invalid part link: ${part} for path: ${path}`);
|
|
195
|
-
let file = Path.join(this._options.tmpdir, info.hostname);
|
|
196
|
-
if (!(await getStat(file)))
|
|
182
|
+
let partPath = Path.join(this._options.tmpdir, part.replace('part://', ''));
|
|
183
|
+
let stat = await getStat(partPath);
|
|
184
|
+
if (!stat)
|
|
197
185
|
throw new Error(`part file ${part} is not exists`);
|
|
198
|
-
|
|
186
|
+
partPaths.push({ file: partPath, size: stat.size });
|
|
199
187
|
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
188
|
+
let p = Path.join(this._options.root, path);
|
|
189
|
+
let start = 0;
|
|
190
|
+
for (let info of partPaths) {
|
|
191
|
+
let writeStream = fs.createWriteStream(p, {
|
|
192
|
+
flags: 'a',
|
|
193
|
+
start
|
|
194
|
+
});
|
|
195
|
+
let stream = fs.createReadStream(info.file);
|
|
196
|
+
await new Promise((resolve, reject) => {
|
|
197
|
+
stream.pipe(writeStream).on('close', resolve).on('error', reject);
|
|
198
|
+
});
|
|
199
|
+
start += info.size;
|
|
200
|
+
writeStream.close();
|
|
203
201
|
}
|
|
204
|
-
|
|
202
|
+
partPaths.forEach((info) => fs.promises.rm(info.file, { force: true }));
|
|
205
203
|
}
|
|
206
204
|
}
|
|
207
205
|
exports.default = FSAdapter;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fsd-fs",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "File system adapter for fsd",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -8,16 +8,14 @@
|
|
|
8
8
|
"build": "tsc",
|
|
9
9
|
"prepublish": "npm run build"
|
|
10
10
|
},
|
|
11
|
-
"repository": "https://github.com/
|
|
12
|
-
"author": "Liang <liang@
|
|
11
|
+
"repository": "https://github.com/liangxingchen/fsd/tree/master/packages/fsd-fs",
|
|
12
|
+
"author": "Liang <liang@miaomo.cc> (https://github.com/liangxingchen)",
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"dependencies": {
|
|
15
15
|
"async": "*",
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"is-stream": "^2.0.1",
|
|
20
|
-
"rimraf": "^3.0.2"
|
|
16
|
+
"debug": "^4.3.4",
|
|
17
|
+
"glob": "^7.2.0",
|
|
18
|
+
"is-stream": "^2.0.1"
|
|
21
19
|
},
|
|
22
|
-
"gitHead": "
|
|
20
|
+
"gitHead": "9820fd7263b6791a38e5568396bfac0f2d3e37e9"
|
|
23
21
|
}
|