@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 CHANGED
@@ -15,6 +15,22 @@
15
15
 
16
16
  > Intended to be used with Bee version 2.1.0.
17
17
 
18
+ ## Quick start
19
+
20
+ Start a Swarm project using TypeScript:
21
+
22
+ ```sh
23
+ npm init swarm-app@latest my-dapp node-ts
24
+ ```
25
+
26
+ or using Vite and TypeScript:
27
+
28
+ ```sh
29
+ npm init swarm-app@latest my-dapp vite-tsx
30
+ ```
31
+
32
+ Supported types are `node`, `node-esm`, `node-ts` and `vite-tsx`. Replace `my-dapp` with your project name.
33
+
18
34
  ## Install
19
35
 
20
36
  ```sh
@@ -5,7 +5,7 @@ const file_1 = require("./file");
5
5
  async function writeTar(collection, tarStream) {
6
6
  for (const item of collection) {
7
7
  if (item.file) {
8
- await tarStream.beginFile(item.path, item.file.size);
8
+ tarStream.beginFile(item.path, item.file.size);
9
9
  await tarStream.appendFile(new Uint8Array(await (0, file_1.fileArrayBuffer)(item.file)));
10
10
  await tarStream.endFile();
11
11
  }
@@ -4,7 +4,7 @@ exports.writeTar = void 0;
4
4
  const fs_1 = require("fs");
5
5
  async function writeTar(collection, tarStream) {
6
6
  for (const item of collection) {
7
- await tarStream.beginFile(item.path, item.size);
7
+ tarStream.beginFile(item.path, item.size);
8
8
  if (item.fsPath) {
9
9
  const stream = (0, fs_1.createReadStream)(item.fsPath);
10
10
  for await (const chunk of stream) {
@@ -14,7 +14,7 @@ class TarStream {
14
14
  return newAcc;
15
15
  });
16
16
  }
17
- async beginFile(path, size) {
17
+ beginFile(path, size) {
18
18
  const header = createHeader(path, size);
19
19
  this.pieces.push(header);
20
20
  this.currentFileSize = 0;
@@ -24,8 +24,10 @@ class TarStream {
24
24
  this.currentFileSize += data.length;
25
25
  }
26
26
  async endFile() {
27
- const padding = 512 - (this.currentFileSize % 512);
28
- this.pieces.push(new Uint8Array(padding));
27
+ const padding = this.currentFileSize % 512 === 0 ? 0 : 512 - (this.currentFileSize % 512);
28
+ if (padding > 0) {
29
+ this.pieces.push(new Uint8Array(padding));
30
+ }
29
31
  }
30
32
  async end() {
31
33
  this.pieces.push(createEndOfArchive());
@@ -33,30 +35,37 @@ class TarStream {
33
35
  }
34
36
  exports.TarStream = TarStream;
35
37
  function createHeader(path, size) {
36
- const header = new Uint8Array(512); // Initialize header with zeros
37
38
  const encoder = new TextEncoder();
38
39
  function writeToBuffer(str, offset, length) {
39
40
  const bytes = encoder.encode(str);
40
41
  header.set(bytes.slice(0, length), offset);
41
42
  }
42
- writeToBuffer(path, 0, 100); // File name
43
- writeToBuffer('0000777', 100, 8); // File mode
44
- writeToBuffer('0001750', 108, 8); // UID
45
- writeToBuffer('0001750', 116, 8); // GID
46
- writeToBuffer(size.toString(8).padStart(11, '0') + ' ', 124, 12); // File size
47
- writeToBuffer(Math.floor(Date.now() / 1000).toString(8) + ' ', 136, 12); // Mod time
48
- writeToBuffer(' ', 148, 8); // Checksum placeholder
49
- writeToBuffer('0', 156, 1); // Typeflag
50
- writeToBuffer('ustar ', 257, 8); // Magic and version
51
- for (let i = 345; i < 512; i++) {
52
- header[i] = 0; // Fill remaining with zeros
53
- }
43
+ // Initialize header with zeros
44
+ const header = new Uint8Array();
45
+ header.fill(0, 0, 512);
46
+ // File name, truncated to 100 characters if necessary
47
+ writeToBuffer(path.slice(0, 100).padEnd(100, '\0'), 0, 100);
48
+ // File mode (octal) and null-terminated
49
+ writeToBuffer('0000777\0', 100, 8);
50
+ // UID and GID (octal) and null-terminated
51
+ writeToBuffer('0001750\0', 108, 8); // UID
52
+ writeToBuffer('0001750\0', 116, 8); // GID
53
+ // File size in octal (11 chars) and null-terminated
54
+ writeToBuffer(size.toString(8).padStart(11, '0') + '\0', 124, 12);
55
+ // Modification time in octal and null-terminated
56
+ const modTime = Math.floor(new Date().getTime() / 1000);
57
+ writeToBuffer(modTime.toString(8).padStart(11, '0') + '\0', 136, 12);
58
+ // Checksum placeholder (8 spaces)
59
+ writeToBuffer(' ', 148, 8);
60
+ // Typeflag (normal file)
61
+ writeToBuffer('0', 156, 1);
62
+ // USTAR magic and version
63
+ writeToBuffer('ustar\0\0', 257, 8);
54
64
  // Calculate checksum
55
65
  let checksum = 0;
56
66
  for (let i = 0; i < 512; i++) {
57
67
  checksum += header[i];
58
68
  }
59
- // Write checksum
60
69
  writeToBuffer(checksum.toString(8).padStart(6, '0') + '\0 ', 148, 8);
61
70
  return header;
62
71
  }
@@ -7,43 +7,67 @@ class TarStream {
7
7
  this.output = new stream_1.PassThrough();
8
8
  this.currentFileSize = 0;
9
9
  }
10
- async beginFile(path, size) {
10
+ beginFile(path, size) {
11
11
  const header = createHeader(path, size);
12
12
  this.output.write(header);
13
13
  this.currentFileSize = 0;
14
14
  }
15
15
  async appendFile(data) {
16
16
  return new Promise(resolve => {
17
- this.output.write(data, () => {
17
+ if (!this.output.write(data)) {
18
+ this.output.once('drain', () => {
19
+ resolve();
20
+ });
21
+ }
22
+ else {
18
23
  resolve();
19
- });
24
+ }
20
25
  this.currentFileSize += data.length;
21
26
  });
22
27
  }
23
28
  async endFile() {
24
- const padding = 512 - (this.currentFileSize % 512);
25
- this.output.write(Buffer.alloc(padding, 0));
29
+ const padding = this.currentFileSize % 512 === 0 ? 0 : 512 - (this.currentFileSize % 512);
30
+ if (padding > 0) {
31
+ this.output.write(Buffer.alloc(padding, 0));
32
+ }
26
33
  }
27
34
  async end() {
28
- this.output.write(createEndOfArchive());
29
- this.output.end();
35
+ return new Promise(resolve => {
36
+ this.output.write(createEndOfArchive());
37
+ this.output.end(() => {
38
+ resolve();
39
+ });
40
+ });
30
41
  }
31
42
  }
32
43
  exports.TarStream = TarStream;
33
44
  function createHeader(path, size) {
34
- const header = Buffer.alloc(512, 0); // Initialize header with zeros
35
- header.write(path, 0, 100); // File name
36
- header.write('0000777', 100, 8); // File mode
37
- header.write('0001750', 108, 8); // UID
38
- header.write('0001750', 116, 8); // GID
39
- header.write(size.toString(8).padStart(11, '0') + ' ', 124, 12); // File size
40
- header.write(Math.floor(new Date().getTime() / 1000).toString(8) + ' ', 136, 12); // Mod time
41
- header.write(' ', 148, 8); // Checksum placeholder
42
- header.write('0', 156, 1); // Typeflag
43
- header.write('ustar ', 257, 8); // Magic and version
44
- header.write('0'.repeat(8 * 12), 345, 8 * 12); // Fill remaining with zeros
45
- const checksum = header.reduce((sum, elem) => sum + elem, 0);
46
- header.write(checksum.toString(8).padStart(6, '0') + '\0 ', 148, 8); // Write checksum
45
+ // Initialize header with zeros
46
+ const header = Buffer.alloc(512, 0);
47
+ // File name, truncated to 100 characters if necessary
48
+ header.write(path.slice(0, 100).padEnd(100, '\0'), 0, 100);
49
+ // File mode (octal) and null-terminated
50
+ header.write('0000777\0', 100, 8);
51
+ // UID and GID (octal) and null-terminated
52
+ header.write('0001750\0', 108, 8); // UID
53
+ header.write('0001750\0', 116, 8); // GID
54
+ // File size in octal (11 chars) and null-terminated
55
+ header.write(size.toString(8).padStart(11, '0') + '\0', 124, 12);
56
+ // Modification time in octal and null-terminated
57
+ const modTime = Math.floor(new Date().getTime() / 1000);
58
+ header.write(modTime.toString(8).padStart(11, '0') + '\0', 136, 12);
59
+ // Checksum placeholder (8 spaces)
60
+ header.write(' ', 148, 8);
61
+ // Typeflag (normal file)
62
+ header.write('0', 156, 1);
63
+ // USTAR magic and version
64
+ header.write('ustar\0\0', 257, 8);
65
+ // Calculate checksum
66
+ let checksum = 0;
67
+ for (let i = 0; i < 512; i++) {
68
+ checksum += header[i];
69
+ }
70
+ header.write(checksum.toString(8).padStart(6, '0') + '\0 ', 148, 8);
47
71
  return header;
48
72
  }
49
73
  function createEndOfArchive() {