@ethersphere/bee-js 7.1.1 → 7.1.2
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 +16 -0
- package/dist/cjs/utils/tar-writer.browser.js +1 -1
- package/dist/cjs/utils/tar-writer.js +1 -1
- package/dist/cjs/utils/tar.browser.js +26 -17
- package/dist/cjs/utils/tar.js +44 -20
- package/dist/index.browser.min.js +1 -1
- package/dist/index.browser.min.js.map +1 -1
- package/dist/mjs/utils/tar-writer.browser.js +1 -1
- package/dist/mjs/utils/tar-writer.js +1 -1
- package/dist/mjs/utils/tar.browser.js +26 -17
- package/dist/mjs/utils/tar.js +43 -20
- package/dist/types/utils/tar.browser.d.ts +1 -1
- package/dist/types/utils/tar.d.ts +1 -1
- package/package.json +1 -1
|
@@ -2,7 +2,7 @@ import { fileArrayBuffer } from "./file.js";
|
|
|
2
2
|
export async function writeTar(collection, tarStream) {
|
|
3
3
|
for (const item of collection) {
|
|
4
4
|
if (item.file) {
|
|
5
|
-
|
|
5
|
+
tarStream.beginFile(item.path, item.file.size);
|
|
6
6
|
await tarStream.appendFile(new Uint8Array(await fileArrayBuffer(item.file)));
|
|
7
7
|
await tarStream.endFile();
|
|
8
8
|
} else {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { createReadStream } from 'fs';
|
|
2
2
|
export async function writeTar(collection, tarStream) {
|
|
3
3
|
for (const item of collection) {
|
|
4
|
-
|
|
4
|
+
tarStream.beginFile(item.path, item.size);
|
|
5
5
|
if (item.fsPath) {
|
|
6
6
|
const stream = createReadStream(item.fsPath);
|
|
7
7
|
for await (const chunk of stream) {
|
|
@@ -11,7 +11,7 @@ export class TarStream {
|
|
|
11
11
|
return newAcc;
|
|
12
12
|
});
|
|
13
13
|
}
|
|
14
|
-
|
|
14
|
+
beginFile(path, size) {
|
|
15
15
|
const header = createHeader(path, size);
|
|
16
16
|
this.pieces.push(header);
|
|
17
17
|
this.currentFileSize = 0;
|
|
@@ -21,38 +21,47 @@ export class TarStream {
|
|
|
21
21
|
this.currentFileSize += data.length;
|
|
22
22
|
}
|
|
23
23
|
async endFile() {
|
|
24
|
-
const padding = 512 - this.currentFileSize % 512;
|
|
25
|
-
|
|
24
|
+
const padding = this.currentFileSize % 512 === 0 ? 0 : 512 - this.currentFileSize % 512;
|
|
25
|
+
if (padding > 0) {
|
|
26
|
+
this.pieces.push(new Uint8Array(padding));
|
|
27
|
+
}
|
|
26
28
|
}
|
|
27
29
|
async end() {
|
|
28
30
|
this.pieces.push(createEndOfArchive());
|
|
29
31
|
}
|
|
30
32
|
}
|
|
31
33
|
function createHeader(path, size) {
|
|
32
|
-
const header = new Uint8Array(512); // Initialize header with zeros
|
|
33
34
|
const encoder = new TextEncoder();
|
|
34
35
|
function writeToBuffer(str, offset, length) {
|
|
35
36
|
const bytes = encoder.encode(str);
|
|
36
37
|
header.set(bytes.slice(0, length), offset);
|
|
37
38
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
writeToBuffer(
|
|
43
|
-
|
|
44
|
-
writeToBuffer('
|
|
45
|
-
|
|
46
|
-
writeToBuffer('
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
39
|
+
// Initialize header with zeros
|
|
40
|
+
const header = new Uint8Array();
|
|
41
|
+
header.fill(0, 0, 512);
|
|
42
|
+
// File name, truncated to 100 characters if necessary
|
|
43
|
+
writeToBuffer(path.slice(0, 100).padEnd(100, '\0'), 0, 100);
|
|
44
|
+
// File mode (octal) and null-terminated
|
|
45
|
+
writeToBuffer('0000777\0', 100, 8);
|
|
46
|
+
// UID and GID (octal) and null-terminated
|
|
47
|
+
writeToBuffer('0001750\0', 108, 8); // UID
|
|
48
|
+
writeToBuffer('0001750\0', 116, 8); // GID
|
|
49
|
+
// File size in octal (11 chars) and null-terminated
|
|
50
|
+
writeToBuffer(size.toString(8).padStart(11, '0') + '\0', 124, 12);
|
|
51
|
+
// Modification time in octal and null-terminated
|
|
52
|
+
const modTime = Math.floor(new Date().getTime() / 1000);
|
|
53
|
+
writeToBuffer(modTime.toString(8).padStart(11, '0') + '\0', 136, 12);
|
|
54
|
+
// Checksum placeholder (8 spaces)
|
|
55
|
+
writeToBuffer(' ', 148, 8);
|
|
56
|
+
// Typeflag (normal file)
|
|
57
|
+
writeToBuffer('0', 156, 1);
|
|
58
|
+
// USTAR magic and version
|
|
59
|
+
writeToBuffer('ustar\0\0', 257, 8);
|
|
50
60
|
// Calculate checksum
|
|
51
61
|
let checksum = 0;
|
|
52
62
|
for (let i = 0; i < 512; i++) {
|
|
53
63
|
checksum += header[i];
|
|
54
64
|
}
|
|
55
|
-
// Write checksum
|
|
56
65
|
writeToBuffer(checksum.toString(8).padStart(6, '0') + '\0 ', 148, 8);
|
|
57
66
|
return header;
|
|
58
67
|
}
|
package/dist/mjs/utils/tar.js
CHANGED
|
@@ -4,42 +4,65 @@ export class TarStream {
|
|
|
4
4
|
this.output = new PassThrough();
|
|
5
5
|
this.currentFileSize = 0;
|
|
6
6
|
}
|
|
7
|
-
|
|
7
|
+
beginFile(path, size) {
|
|
8
8
|
const header = createHeader(path, size);
|
|
9
9
|
this.output.write(header);
|
|
10
10
|
this.currentFileSize = 0;
|
|
11
11
|
}
|
|
12
12
|
async appendFile(data) {
|
|
13
13
|
return new Promise(resolve => {
|
|
14
|
-
this.output.write(data
|
|
14
|
+
if (!this.output.write(data)) {
|
|
15
|
+
this.output.once('drain', () => {
|
|
16
|
+
resolve();
|
|
17
|
+
});
|
|
18
|
+
} else {
|
|
15
19
|
resolve();
|
|
16
|
-
}
|
|
20
|
+
}
|
|
17
21
|
this.currentFileSize += data.length;
|
|
18
22
|
});
|
|
19
23
|
}
|
|
20
24
|
async endFile() {
|
|
21
|
-
const padding = 512 - this.currentFileSize % 512;
|
|
22
|
-
|
|
25
|
+
const padding = this.currentFileSize % 512 === 0 ? 0 : 512 - this.currentFileSize % 512;
|
|
26
|
+
if (padding > 0) {
|
|
27
|
+
this.output.write(Buffer.alloc(padding, 0));
|
|
28
|
+
}
|
|
23
29
|
}
|
|
24
30
|
async end() {
|
|
25
|
-
|
|
26
|
-
|
|
31
|
+
return new Promise(resolve => {
|
|
32
|
+
this.output.write(createEndOfArchive());
|
|
33
|
+
this.output.end(() => {
|
|
34
|
+
resolve();
|
|
35
|
+
});
|
|
36
|
+
});
|
|
27
37
|
}
|
|
28
38
|
}
|
|
29
39
|
function createHeader(path, size) {
|
|
30
|
-
|
|
31
|
-
header.
|
|
32
|
-
|
|
33
|
-
header.write('
|
|
34
|
-
|
|
35
|
-
header.write(
|
|
36
|
-
|
|
37
|
-
header.write('
|
|
38
|
-
header.write('0',
|
|
39
|
-
|
|
40
|
-
header.write(
|
|
41
|
-
|
|
42
|
-
|
|
40
|
+
// Initialize header with zeros
|
|
41
|
+
const header = Buffer.alloc(512, 0);
|
|
42
|
+
// File name, truncated to 100 characters if necessary
|
|
43
|
+
header.write(path.slice(0, 100).padEnd(100, '\0'), 0, 100);
|
|
44
|
+
// File mode (octal) and null-terminated
|
|
45
|
+
header.write('0000777\0', 100, 8);
|
|
46
|
+
// UID and GID (octal) and null-terminated
|
|
47
|
+
header.write('0001750\0', 108, 8); // UID
|
|
48
|
+
header.write('0001750\0', 116, 8); // GID
|
|
49
|
+
// File size in octal (11 chars) and null-terminated
|
|
50
|
+
header.write(size.toString(8).padStart(11, '0') + '\0', 124, 12);
|
|
51
|
+
// Modification time in octal and null-terminated
|
|
52
|
+
const modTime = Math.floor(new Date().getTime() / 1000);
|
|
53
|
+
header.write(modTime.toString(8).padStart(11, '0') + '\0', 136, 12);
|
|
54
|
+
// Checksum placeholder (8 spaces)
|
|
55
|
+
header.write(' ', 148, 8);
|
|
56
|
+
// Typeflag (normal file)
|
|
57
|
+
header.write('0', 156, 1);
|
|
58
|
+
// USTAR magic and version
|
|
59
|
+
header.write('ustar\0\0', 257, 8);
|
|
60
|
+
// Calculate checksum
|
|
61
|
+
let checksum = 0;
|
|
62
|
+
for (let i = 0; i < 512; i++) {
|
|
63
|
+
checksum += header[i];
|
|
64
|
+
}
|
|
65
|
+
header.write(checksum.toString(8).padStart(6, '0') + '\0 ', 148, 8);
|
|
43
66
|
return header;
|
|
44
67
|
}
|
|
45
68
|
function createEndOfArchive() {
|
|
@@ -2,7 +2,7 @@ export declare class TarStream {
|
|
|
2
2
|
pieces: Uint8Array[];
|
|
3
3
|
currentFileSize: number;
|
|
4
4
|
get output(): Uint8Array;
|
|
5
|
-
beginFile(path: string, size: number):
|
|
5
|
+
beginFile(path: string, size: number): void;
|
|
6
6
|
appendFile(data: Uint8Array): Promise<void>;
|
|
7
7
|
endFile(): Promise<void>;
|
|
8
8
|
end(): Promise<void>;
|
|
@@ -3,7 +3,7 @@ import { PassThrough } from 'stream';
|
|
|
3
3
|
export declare class TarStream {
|
|
4
4
|
output: PassThrough;
|
|
5
5
|
currentFileSize: number;
|
|
6
|
-
beginFile(path: string, size: number):
|
|
6
|
+
beginFile(path: string, size: number): void;
|
|
7
7
|
appendFile(data: Uint8Array): Promise<void>;
|
|
8
8
|
endFile(): Promise<void>;
|
|
9
9
|
end(): Promise<void>;
|