@guanghechen/filepart 1.0.0-alpha.1
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/CHANGELOG.md +15 -0
- package/lib/cjs/index.cjs +97 -0
- package/lib/esm/index.mjs +87 -0
- package/lib/types/index.d.ts +30 -0
- package/package.json +54 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
# 1.0.0-alpha.1 (2023-12-10)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Performance Improvements
|
|
10
|
+
|
|
11
|
+
* 🎨 export const DEFAULT_FILEPART_CODE_PREFIX from @guanghechen/filepart.types ([d6d2b1d](https://github.com/guanghechen/sora/commit/d6d2b1d960a9a02b3315c4cbd32cdc1fa53cccc3))
|
|
12
|
+
* 🎨 export new method 'calcFilePartNamesByCount' ([dbd16b6](https://github.com/guanghechen/sora/commit/dbd16b65610088e38ac84e39d66196e67b33446f))
|
|
13
|
+
* 🎨 make methods return iterator instead of array ([1fddedd](https://github.com/guanghechen/sora/commit/1fddedd8a8de6b21c0431bdacf2a7d8a17bd2c86))
|
|
14
|
+
* 🎨 move filepart related code from @guanghechen/file-split to new package @guanghechen/filepart ([833aa10](https://github.com/guanghechen/sora/commit/833aa10f4eee38b09a375b221a0a763f96d8fd4c))
|
|
15
|
+
* 🎨 refactor virtual file system to support filesplit case ([d520715](https://github.com/guanghechen/sora/commit/d520715e1195169914e9a4563fc8c2b1cd035b27))
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var filepart_types = require('@guanghechen/filepart.types');
|
|
4
|
+
|
|
5
|
+
const isProduction = process.env.NODE_ENV === 'production';
|
|
6
|
+
const prefix = 'Invariant failed';
|
|
7
|
+
function invariant(condition, message) {
|
|
8
|
+
if (condition)
|
|
9
|
+
return;
|
|
10
|
+
if (isProduction)
|
|
11
|
+
throw new Error(prefix);
|
|
12
|
+
if (message == null)
|
|
13
|
+
throw new Error(prefix + ': ');
|
|
14
|
+
throw new Error(prefix + ': ' + (message instanceof Function ? message() : message));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function* calcFilePartItemsBySize(fileSize, partSize) {
|
|
18
|
+
invariant(partSize >= 1 && Number.isInteger(partSize), 'Part size should be a positive integer!');
|
|
19
|
+
if (fileSize <= 0) {
|
|
20
|
+
yield { sid: 1, start: 0, end: 0 };
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (fileSize <= partSize) {
|
|
24
|
+
yield { sid: 1, start: 0, end: fileSize };
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const partTotal = Math.ceil(fileSize / partSize);
|
|
28
|
+
invariant(partTotal > 0, 'Part size is too small!');
|
|
29
|
+
for (let i = 0; i < partTotal; ++i) {
|
|
30
|
+
const part = {
|
|
31
|
+
sid: i + 1,
|
|
32
|
+
start: i * partSize,
|
|
33
|
+
end: i + 1 === partTotal ? fileSize : (i + 1) * partSize,
|
|
34
|
+
};
|
|
35
|
+
yield part;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function* calcFilePartItemsByCount(fileSize, partTotal) {
|
|
39
|
+
invariant(partTotal >= 1 && Number.isInteger(partTotal), 'Total of part should be a positive integer!');
|
|
40
|
+
if (fileSize <= 0) {
|
|
41
|
+
yield { sid: 1, start: 0, end: 0 };
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (partTotal === 1) {
|
|
45
|
+
yield { sid: 1, start: 0, end: fileSize };
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const partSize = Math.ceil(fileSize / partTotal);
|
|
49
|
+
invariant(partSize > 0, 'Part size is too small!');
|
|
50
|
+
for (let i = 0; i < partTotal; ++i) {
|
|
51
|
+
const part = {
|
|
52
|
+
sid: i + 1,
|
|
53
|
+
start: i * partSize,
|
|
54
|
+
end: i + 1 === partTotal ? fileSize : (i + 1) * partSize,
|
|
55
|
+
};
|
|
56
|
+
yield part;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function* calcFilePartNames(parts, partCodePrefix) {
|
|
60
|
+
if (parts.length === 0)
|
|
61
|
+
return;
|
|
62
|
+
if (parts.length === 1) {
|
|
63
|
+
yield '';
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const maxPaddingCount = String(parts.length).length;
|
|
67
|
+
for (const part of parts) {
|
|
68
|
+
const partCode = String(part.sid).padStart(maxPaddingCount, '0');
|
|
69
|
+
const partName = partCodePrefix + partCode;
|
|
70
|
+
yield partName;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function* calcFilePartNamesByCount(partTotal, partCodePrefix) {
|
|
74
|
+
if (partTotal <= 0)
|
|
75
|
+
return;
|
|
76
|
+
if (partTotal === 1) {
|
|
77
|
+
yield '';
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const maxPaddingCount = String(partTotal).length;
|
|
81
|
+
for (let sid = 1; sid <= partTotal; ++sid) {
|
|
82
|
+
const partCode = String(sid).padStart(maxPaddingCount, '0');
|
|
83
|
+
const partName = partCodePrefix + partCode;
|
|
84
|
+
yield partName;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
exports.calcFilePartItemsByCount = calcFilePartItemsByCount;
|
|
89
|
+
exports.calcFilePartItemsBySize = calcFilePartItemsBySize;
|
|
90
|
+
exports.calcFilePartNames = calcFilePartNames;
|
|
91
|
+
exports.calcFilePartNamesByCount = calcFilePartNamesByCount;
|
|
92
|
+
Object.keys(filepart_types).forEach(function (k) {
|
|
93
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
94
|
+
enumerable: true,
|
|
95
|
+
get: function () { return filepart_types[k]; }
|
|
96
|
+
});
|
|
97
|
+
});
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import 'node:fs';
|
|
2
|
+
export * from '@guanghechen/filepart.types';
|
|
3
|
+
|
|
4
|
+
const isProduction = process.env.NODE_ENV === 'production';
|
|
5
|
+
const prefix = 'Invariant failed';
|
|
6
|
+
function invariant(condition, message) {
|
|
7
|
+
if (condition)
|
|
8
|
+
return;
|
|
9
|
+
if (isProduction)
|
|
10
|
+
throw new Error(prefix);
|
|
11
|
+
if (message == null)
|
|
12
|
+
throw new Error(prefix + ': ');
|
|
13
|
+
throw new Error(prefix + ': ' + (message instanceof Function ? message() : message));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function* calcFilePartItemsBySize(fileSize, partSize) {
|
|
17
|
+
invariant(partSize >= 1 && Number.isInteger(partSize), 'Part size should be a positive integer!');
|
|
18
|
+
if (fileSize <= 0) {
|
|
19
|
+
yield { sid: 1, start: 0, end: 0 };
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (fileSize <= partSize) {
|
|
23
|
+
yield { sid: 1, start: 0, end: fileSize };
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const partTotal = Math.ceil(fileSize / partSize);
|
|
27
|
+
invariant(partTotal > 0, 'Part size is too small!');
|
|
28
|
+
for (let i = 0; i < partTotal; ++i) {
|
|
29
|
+
const part = {
|
|
30
|
+
sid: i + 1,
|
|
31
|
+
start: i * partSize,
|
|
32
|
+
end: i + 1 === partTotal ? fileSize : (i + 1) * partSize,
|
|
33
|
+
};
|
|
34
|
+
yield part;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function* calcFilePartItemsByCount(fileSize, partTotal) {
|
|
38
|
+
invariant(partTotal >= 1 && Number.isInteger(partTotal), 'Total of part should be a positive integer!');
|
|
39
|
+
if (fileSize <= 0) {
|
|
40
|
+
yield { sid: 1, start: 0, end: 0 };
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (partTotal === 1) {
|
|
44
|
+
yield { sid: 1, start: 0, end: fileSize };
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const partSize = Math.ceil(fileSize / partTotal);
|
|
48
|
+
invariant(partSize > 0, 'Part size is too small!');
|
|
49
|
+
for (let i = 0; i < partTotal; ++i) {
|
|
50
|
+
const part = {
|
|
51
|
+
sid: i + 1,
|
|
52
|
+
start: i * partSize,
|
|
53
|
+
end: i + 1 === partTotal ? fileSize : (i + 1) * partSize,
|
|
54
|
+
};
|
|
55
|
+
yield part;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function* calcFilePartNames(parts, partCodePrefix) {
|
|
59
|
+
if (parts.length === 0)
|
|
60
|
+
return;
|
|
61
|
+
if (parts.length === 1) {
|
|
62
|
+
yield '';
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const maxPaddingCount = String(parts.length).length;
|
|
66
|
+
for (const part of parts) {
|
|
67
|
+
const partCode = String(part.sid).padStart(maxPaddingCount, '0');
|
|
68
|
+
const partName = partCodePrefix + partCode;
|
|
69
|
+
yield partName;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function* calcFilePartNamesByCount(partTotal, partCodePrefix) {
|
|
73
|
+
if (partTotal <= 0)
|
|
74
|
+
return;
|
|
75
|
+
if (partTotal === 1) {
|
|
76
|
+
yield '';
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const maxPaddingCount = String(partTotal).length;
|
|
80
|
+
for (let sid = 1; sid <= partTotal; ++sid) {
|
|
81
|
+
const partCode = String(sid).padStart(maxPaddingCount, '0');
|
|
82
|
+
const partName = partCodePrefix + partCode;
|
|
83
|
+
yield partName;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export { calcFilePartItemsByCount, calcFilePartItemsBySize, calcFilePartNames, calcFilePartNamesByCount };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { IFilePartItem } from '@guanghechen/filepart.types';
|
|
2
|
+
export * from '@guanghechen/filepart.types';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Generate file part items by part size.
|
|
6
|
+
*
|
|
7
|
+
* @param fileSize
|
|
8
|
+
* @param _partSize
|
|
9
|
+
* @returns
|
|
10
|
+
*/
|
|
11
|
+
declare function calcFilePartItemsBySize(fileSize: number, partSize: number): IterableIterator<IFilePartItem>;
|
|
12
|
+
/**
|
|
13
|
+
* Generate file part items by total of parts.
|
|
14
|
+
*
|
|
15
|
+
* @param filepath
|
|
16
|
+
* @param partTotal
|
|
17
|
+
* @returns
|
|
18
|
+
*/
|
|
19
|
+
declare function calcFilePartItemsByCount(fileSize: number, partTotal: number): IterableIterator<IFilePartItem>;
|
|
20
|
+
/**
|
|
21
|
+
* Calculate names of parts of sourcefile respectively.
|
|
22
|
+
*
|
|
23
|
+
* @param parts
|
|
24
|
+
* @param partCodePrefix
|
|
25
|
+
* @returns
|
|
26
|
+
*/
|
|
27
|
+
declare function calcFilePartNames(parts: ReadonlyArray<Pick<IFilePartItem, 'sid'>>, partCodePrefix: string): IterableIterator<string>;
|
|
28
|
+
declare function calcFilePartNamesByCount(partTotal: number, partCodePrefix: string): IterableIterator<string>;
|
|
29
|
+
|
|
30
|
+
export { calcFilePartItemsByCount, calcFilePartItemsBySize, calcFilePartNames, calcFilePartNamesByCount };
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@guanghechen/filepart",
|
|
3
|
+
"version": "1.0.0-alpha.1",
|
|
4
|
+
"description": "File helper",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "guanghechen",
|
|
7
|
+
"url": "https://github.com/guanghechen/"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/guanghechen/sora/tree/@guanghechen/filepart@1.0.0-alpha.0",
|
|
12
|
+
"directory": "packages/filepart"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://github.com/guanghechen/sora/tree/@guanghechen/filepart@1.0.0-alpha.0/packages/filepart#readme",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"file helper",
|
|
17
|
+
"split file",
|
|
18
|
+
"merge streams"
|
|
19
|
+
],
|
|
20
|
+
"type": "module",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"source": "./src/index.ts",
|
|
24
|
+
"import": "./lib/esm/index.mjs",
|
|
25
|
+
"require": "./lib/cjs/index.cjs",
|
|
26
|
+
"types": "./lib/types/index.d.ts"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"source": "./src/index.ts",
|
|
30
|
+
"main": "./lib/cjs/index.cjs",
|
|
31
|
+
"module": "./lib/esm/index.mjs",
|
|
32
|
+
"types": "./lib/types/index.d.ts",
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"files": [
|
|
35
|
+
"lib/",
|
|
36
|
+
"!lib/**/*.map",
|
|
37
|
+
"package.json",
|
|
38
|
+
"CHANGELOG.md",
|
|
39
|
+
"LICENSE",
|
|
40
|
+
"README.md"
|
|
41
|
+
],
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "../../node_modules/.bin/rimraf lib/ && ../../node_modules/.bin/cross-env NODE_ENV=production ../../node_modules/.bin/rollup -c ../../rollup.config.mjs",
|
|
44
|
+
"prepublishOnly": "yarn build",
|
|
45
|
+
"test": "node --experimental-vm-modules ../../node_modules/.bin/jest --config ../../jest.config.mjs --rootDir ."
|
|
46
|
+
},
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"@guanghechen/filepart.types": "^1.0.0-alpha.1"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@guanghechen/internal": "^1.0.0-alpha.0"
|
|
52
|
+
},
|
|
53
|
+
"gitHead": "77612b01152880c3f7b5c123fb7a2e32d7d43885"
|
|
54
|
+
}
|