@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.
@@ -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
- await tarStream.beginFile(item.path, item.file.size);
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
- await tarStream.beginFile(item.path, item.size);
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
- async beginFile(path, size) {
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
- this.pieces.push(new Uint8Array(padding));
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
- writeToBuffer(path, 0, 100); // File name
39
- writeToBuffer('0000777', 100, 8); // File mode
40
- writeToBuffer('0001750', 108, 8); // UID
41
- writeToBuffer('0001750', 116, 8); // GID
42
- writeToBuffer(size.toString(8).padStart(11, '0') + ' ', 124, 12); // File size
43
- writeToBuffer(Math.floor(Date.now() / 1000).toString(8) + ' ', 136, 12); // Mod time
44
- writeToBuffer(' ', 148, 8); // Checksum placeholder
45
- writeToBuffer('0', 156, 1); // Typeflag
46
- writeToBuffer('ustar ', 257, 8); // Magic and version
47
- for (let i = 345; i < 512; i++) {
48
- header[i] = 0; // Fill remaining with zeros
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
  }
@@ -4,42 +4,65 @@ export class TarStream {
4
4
  this.output = new PassThrough();
5
5
  this.currentFileSize = 0;
6
6
  }
7
- async beginFile(path, size) {
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
- this.output.write(Buffer.alloc(padding, 0));
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
- this.output.write(createEndOfArchive());
26
- this.output.end();
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
- const header = Buffer.alloc(512, 0); // Initialize header with zeros
31
- header.write(path, 0, 100); // File name
32
- header.write('0000777', 100, 8); // File mode
33
- header.write('0001750', 108, 8); // UID
34
- header.write('0001750', 116, 8); // GID
35
- header.write(size.toString(8).padStart(11, '0') + ' ', 124, 12); // File size
36
- header.write(Math.floor(new Date().getTime() / 1000).toString(8) + ' ', 136, 12); // Mod time
37
- header.write(' ', 148, 8); // Checksum placeholder
38
- header.write('0', 156, 1); // Typeflag
39
- header.write('ustar ', 257, 8); // Magic and version
40
- header.write('0'.repeat(8 * 12), 345, 8 * 12); // Fill remaining with zeros
41
- const checksum = header.reduce((sum, elem) => sum + elem, 0);
42
- header.write(checksum.toString(8).padStart(6, '0') + '\0 ', 148, 8); // Write checksum
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): Promise<void>;
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): Promise<void>;
6
+ beginFile(path: string, size: number): void;
7
7
  appendFile(data: Uint8Array): Promise<void>;
8
8
  endFile(): Promise<void>;
9
9
  end(): Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ethersphere/bee-js",
3
- "version": "7.1.1",
3
+ "version": "7.1.2",
4
4
  "description": "Javascript client for Bee",
5
5
  "keywords": [
6
6
  "bee",