Skip to content

Commit 53e9414

Browse files
author
ozelot379
committed
- Browser support
- `ArrayBuffer` and `Uint8Array` support
1 parent c66ec33 commit 53e9414

15 files changed

Lines changed: 168 additions & 449 deletions

‎src/Converter/MetadataConverter.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import AbstractConverter from "./AbstractConverter";
22
import DeleteConverter from "./DeleteConverter";
3+
import path from "path";
34
import uuid from "uuid/v4";
45

56
/**
@@ -50,7 +51,7 @@ class MetadataConverter extends AbstractConverter {
5051
"format_version": 1,
5152
"header": {
5253
"description": mcmeta.pack.description,
53-
"name": await this.output.input.name(),
54+
"name": path.parse(this.output.input.filename).name,
5455
"platform_locked": false,
5556
"uuid": uuid_header,
5657
"version": [0, 0, 1]

‎src/Input/AbstractInput.js‎

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,28 @@ class AbstractInput {
77
/**
88
* AbstractInput constructor
99
*
10-
* @param {string|Buffer} input
10+
* @param {string|Buffer|ArrayBuffer|Uint8Array} input
11+
* @param {string} filename
1112
*
1213
* @throws {Error}
1314
*/
14-
constructor(input) {
15+
constructor(input, filename) {
1516
if (this.constructor === AbstractInput) {
1617
throw new Error("Can't instantiate abstract class!");
1718
}
1819

1920
/**
20-
* @type {string|Buffer}
21+
* @type {string|Buffer|ArrayBuffer|Uint8Array}
2122
*
2223
* @protected
2324
*/
2425
this.input = input;
25-
}
26-
27-
/**
28-
* @returns {Promise<string>}
29-
*
30-
* @throws {Error}
31-
*
32-
* @abstract
33-
*/
34-
async name() {
35-
26+
/**
27+
* @type {string}
28+
*
29+
* @protected
30+
*/
31+
this.filename = filename;
3632
}
3733
}
3834

‎src/Input/BufferInput.js‎

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,10 @@
1-
import ZipInput from "./ZipInput";
2-
import path from "path";
1+
import AbstractInput from "./AbstractInput";
32

43
/**
54
* Class BufferInput
65
*/
7-
class BufferInput extends ZipInput {
8-
/**
9-
* @inheritDoc
10-
*
11-
* @param {string} filename
12-
*/
13-
constructor(input, filename = "") {
14-
super(input);
6+
class BufferInput extends AbstractInput {
157

16-
/**
17-
* @type {string}
18-
*
19-
* @protected
20-
*/
21-
this.filename = filename;
22-
}
23-
24-
/**
25-
* @inheritDoc
26-
*/
27-
async name() {
28-
return path.parse(this.filename).name;
29-
}
308
}
319

3210
export default BufferInput;

‎src/Input/FolderInput.js‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
import AbstractInput from "./AbstractInput";
2-
import path from "path";
32

43
/**
54
* Class FolderInput
65
*/
76
class FolderInput extends AbstractInput {
8-
/**
9-
* @inheritDoc
10-
*/
11-
async name() {
12-
return path.basename(this.input);
13-
}
7+
148
}
159

1610
export default FolderInput;

‎src/Input/ZipInput.js‎

Lines changed: 0 additions & 16 deletions
This file was deleted.

‎src/Input/index.js‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@ import AbstractInput from "./AbstractInput";
22
import BufferInput from "./BufferInput";
33
import FolderInput from "./FolderInput";
44
import fs from "fs-extra";
5+
import isBuffer from "./../Output/isBuffer";
56
import MetadataConverter from "../Converter/MetadataConverter";
67
import path from "path";
7-
import ZipInput from "./ZipInput";
88

99
/**
10-
* @param {string|Buffer|Array} input
10+
* @param {string|Buffer|ArrayBuffer|Uint8Array|Array} input
1111
*
1212
* @returns {Promise<AbstractInput>}
1313
*
1414
* @throws {Error}
1515
*/
1616
async function detectInput(input) {
17-
if (Buffer.isBuffer(input)) {
17+
if (isBuffer(input)) {
1818
return new BufferInput(input);
1919
}
2020

2121
if (Array.isArray(input)) {
22-
if (Buffer.isBuffer(input[0])) {
22+
if (isBuffer(input[0])) {
2323
return new BufferInput(input[0], input[1]);
2424
}
2525

@@ -30,6 +30,10 @@ async function detectInput(input) {
3030
throw new Error(`Invalid string input!`);
3131
}
3232

33+
if (!fs) {
34+
throw new Error(`Browser only supports Buffer input!`);
35+
}
36+
3337
if (!fs.existsSync(input)) {
3438
throw new Error(`The input ${input} does not exists!`);
3539
}
@@ -39,15 +43,15 @@ async function detectInput(input) {
3943
throw new Error(`Invalid folder input ${input} - Missing ${MetadataConverter.PACK_MCMETA}!`);
4044
}
4145

42-
return new FolderInput(input);
46+
return new FolderInput(input, input);
4347
}
4448

4549

4650
const ext = path.extname(input).toLowerCase().substr(1);
4751

4852
switch (ext) {
4953
case "zip":
50-
return new ZipInput(input);
54+
return new BufferInput(await fs.readFile(input), input);
5155

5256
default:
5357
throw new Error(`The input ${input} is no zip archive or folder!`);

‎src/Output/AbstractOutput.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class AbstractOutput {
1010
/**
1111
* AbstractOutput constructor
1212
*
13-
* @param {string|Buffer} output
13+
* @param {string|Buffer|ArrayBuffer|Uint8Array} output
1414
* @param {AbstractInput} input
1515
* @param {Log} log
1616
*
@@ -22,7 +22,7 @@ class AbstractOutput {
2222
}
2323

2424
/**
25-
* @type {string|Buffer}
25+
* @type {string|Buffer|ArrayBuffer|Uint8Array}
2626
*
2727
* @protected
2828
*/
@@ -53,7 +53,7 @@ class AbstractOutput {
5353
}
5454

5555
/**
56-
* @returns {Promise<string|Buffer>}
56+
* @returns {Promise<string|Buffer|ArrayBuffer|Uint8Array>}
5757
*
5858
* @throws {Error}
5959
*

‎src/Output/BufferOutput.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class BufferOutput extends ZipOutput {
1010
async generate() {
1111
this.log.log(`Pack output`);
1212

13-
return this.generateZip();
13+
return this.generateZip(Buffer.isBuffer(this.output) ? undefined : this.output.constructor.name.toLowerCase());
1414
}
1515
}
1616

‎src/Output/FolderOutput.js‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import AbstractOutput from "./AbstractOutput";
2-
import extractZip from "extract-zip";
2+
import BufferInput from "../Input/BufferInput";
33
import FolderInput from "../Input/FolderInput";
44
import fs from "fs-extra";
5+
import JSZip from "jszip";
56
import path from "path";
6-
import util from "util";
7-
import ZipInput from "../Input/ZipInput";
8-
9-
const extractZipPromise = util.promisify(extractZip);
107

118
/**
129
* Class FolderOutput
@@ -17,10 +14,21 @@ class FolderOutput extends AbstractOutput {
1714
*/
1815
async init() {
1916
switch (true) {
20-
case (this.input instanceof ZipInput):
17+
case (this.input instanceof BufferInput):
2118
this.log.log(`Extract input`);
2219

23-
return extractZipPromise(this.input.input, {dir: path.resolve(this.output)});
20+
let zip = new JSZip();
21+
zip = await zip.loadAsync(this.input.input);
22+
for (const entry of Object.values(zip.files)) {
23+
if (entry.dir) {
24+
if (!fs.existsSync(this.p(entry.name))) {
25+
await fs.mkdirs(this.p(entry.name)); // Empty folders
26+
}
27+
} else {
28+
await this.write(entry.name, await zip.file(entry.name).async("nodebuffer"));
29+
}
30+
}
31+
break;
2432

2533
case (this.input instanceof FolderInput):
2634
this.log.log(`Copy input`);

‎src/Output/ZipOutput.js‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import AbstractOutput from "./AbstractOutput";
2+
import BufferInput from "../Input/BufferInput";
23
import FolderInput from "../Input/FolderInput";
34
import fs from "fs-extra";
45
import JSZip from "jszip";
56
import readdirp from "readdirp";
6-
import ZipInput from "../Input/ZipInput";
77

88
/**
99
* Class ZipOutput
@@ -21,10 +21,10 @@ class ZipOutput extends AbstractOutput {
2121
this.zip = new JSZip();
2222

2323
switch (true) {
24-
case (this.input instanceof ZipInput):
24+
case (this.input instanceof BufferInput):
2525
this.log.log(`Read input`);
2626

27-
this.zip = await this.zip.loadAsync(Buffer.isBuffer(this.input.input) ? this.input.input : await fs.readFile(this.input.input));
27+
this.zip = await this.zip.loadAsync(this.input.input);
2828
break;
2929

3030
case (this.input instanceof FolderInput):
@@ -60,13 +60,15 @@ class ZipOutput extends AbstractOutput {
6060
}
6161

6262
/**
63+
* @param {string} type
64+
*
6365
* @returns {Promise<Buffer>}
6466
*
6567
* @protected
6668
*/
67-
async generateZip() {
69+
async generateZip(type = "nodebuffer") {
6870
return this.zip.generateAsync({
69-
type: "nodebuffer"
71+
type
7072
})
7173
}
7274

0 commit comments

Comments
 (0)