@ipld/car 5.3.3 → 5.4.0

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
@@ -377,7 +377,7 @@ amount of memory that the runtime can handle.
377
377
  <a name="CarReader__readRaw"></a>
378
378
  ### `async CarReader.readRaw(fd, blockIndex)`
379
379
 
380
- * `fd` `(fs.promises.FileHandle|number)`: A file descriptor from the
380
+ * `fd` `(FileHandle|number)`: A file descriptor from the
381
381
  Node.js `fs` module. Either an integer, from `fs.open()` or a `FileHandle`
382
382
  from `fs.promises.open()`.
383
383
  * `blockIndex` `(BlockIndex)`: An index pointing to the location of the
@@ -780,7 +780,7 @@ upon successful modification.
780
780
  <a name="CarWriter__updateRootsInFile"></a>
781
781
  ### `async CarWriter.updateRootsInFile(fd, roots)`
782
782
 
783
- * `fd` `(fs.promises.FileHandle|number)`: A file descriptor from the
783
+ * `fd` `(FileHandle|number)`: A file descriptor from the
784
784
  Node.js `fs` module. Either an integer, from `fs.open()` or a `FileHandle`
785
785
  from `fs.promises.open()`.
786
786
  * `roots` `(CID[])`: A new list of roots to replace the existing list in
@@ -0,0 +1,28 @@
1
+ /**
2
+ * @description This function is needed not to initialize the `fs.read` on load time. To run in cf workers without polyfill.
3
+ * @param {number} fd
4
+ * @param {Uint8Array} buffer
5
+ * @param {number} offset
6
+ * @param {number} length
7
+ * @param {number} position
8
+ * @returns {Promise<{ bytesRead: number, buffer: Uint8Array }>}
9
+ */
10
+ export function fsread(fd: number, buffer: Uint8Array, offset: number, length: number, position: number): Promise<{
11
+ bytesRead: number;
12
+ buffer: Uint8Array;
13
+ }>;
14
+ /**
15
+ * @description This function is needed not to initialize the `fs.write` on load time. To run in cf workers without polyfill.
16
+ * @param {number} fd
17
+ * @param {Uint8Array} buffer
18
+ * @param {number} offset
19
+ * @param {number} length
20
+ * @param {number} position
21
+ * @returns {Promise<{ bytesRead: number, buffer: Uint8Array }>}
22
+ */
23
+ export function fswrite(fd: number, buffer: Uint8Array, offset: number, length: number, position: number): Promise<{
24
+ bytesRead: number;
25
+ buffer: Uint8Array;
26
+ }>;
27
+ export const hasFS: boolean;
28
+ //# sourceMappingURL=promise-fs-opts.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"promise-fs-opts.d.ts","sourceRoot":"","sources":["../../src/promise-fs-opts.js"],"names":[],"mappings":"AAWA;;;;;;;;GAQG;AACH,2BAPW,MAAM,UACN,UAAU,UACV,MAAM,UACN,MAAM,YACN,MAAM,GACJ,QAAQ;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,UAAU,CAAA;CAAE,CAAC,CAO9D;AAMD;;;;;;;;GAQG;AACH,4BAPW,MAAM,UACN,UAAU,UACV,MAAM,UACN,MAAM,YACN,MAAM,GACJ,QAAQ;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,UAAU,CAAA;CAAE,CAAC,CAO9D;AA1CD,4BAAyB"}
@@ -1,4 +1,9 @@
1
- /// <reference types="node" />
1
+ /**
2
+ * @typedef {import('./api').Block} Block
3
+ * @typedef {import('./api').BlockIndex} BlockIndex
4
+ * @typedef {import('./api').CarReader} CarReaderIface
5
+ * @typedef {import('fs').promises.FileHandle} FileHandle
6
+ */
2
7
  /**
3
8
  * @class
4
9
  * @implements {CarReaderIface}
@@ -16,7 +21,7 @@ export class CarReader extends BrowserCarReader implements CarReaderIface {
16
21
  * @async
17
22
  * @static
18
23
  * @memberof CarReader
19
- * @param {fs.promises.FileHandle | number} fd - A file descriptor from the
24
+ * @param {FileHandle | number} fd - A file descriptor from the
20
25
  * Node.js `fs` module. Either an integer, from `fs.open()` or a `FileHandle`
21
26
  * from `fs.promises.open()`.
22
27
  * @param {BlockIndex} blockIndex - An index pointing to the location of the
@@ -24,12 +29,12 @@ export class CarReader extends BrowserCarReader implements CarReaderIface {
24
29
  * `{cid:CID, blockLength:number, blockOffset:number}`.
25
30
  * @returns {Promise<Block>} A `{ cid:CID, bytes:Uint8Array }` pair.
26
31
  */
27
- static readRaw(fd: fs.promises.FileHandle | number, blockIndex: BlockIndex): Promise<Block>;
32
+ static readRaw(fd: FileHandle | number, blockIndex: BlockIndex): Promise<Block>;
28
33
  }
29
- export const __browser: false;
34
+ export const __browser: boolean;
30
35
  export type Block = import('./api').Block;
31
36
  export type BlockIndex = import('./api').BlockIndex;
32
37
  export type CarReaderIface = import('./api').CarReader;
38
+ export type FileHandle = import('fs').promises.FileHandle;
33
39
  import { CarReader as BrowserCarReader } from './reader-browser.js';
34
- import fs from 'fs';
35
40
  //# sourceMappingURL=reader.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"reader.d.ts","sourceRoot":"","sources":["../../src/reader.js"],"names":[],"mappings":";AAYA;;;GAGG;AACH;IACE;;;;;;;;;;;;;;;;;;;OAmBG;IACH,mBARW,GAAG,QAAQ,CAAC,UAAU,GAAG,MAAM,cAG/B,UAAU,GAGR,QAAQ,KAAK,CAAC,CAiB1B;CACF;AAED,8BAA8B;oBAlDjB,OAAO,OAAO,EAAE,KAAK;yBACrB,OAAO,OAAO,EAAE,UAAU;6BAC1B,OAAO,OAAO,EAAE,SAAS;8CALQ,qBAAqB;eAFpD,IAAI"}
1
+ {"version":3,"file":"reader.d.ts","sourceRoot":"","sources":["../../src/reader.js"],"names":[],"mappings":"AAGA;;;;;GAKG;AAEH;;;GAGG;AACH;IACE;;;;;;;;;;;;;;;;;;;OAmBG;IACH,mBARW,UAAU,GAAG,MAAM,cAGnB,UAAU,GAGR,QAAQ,KAAK,CAAC,CAiB1B;CACF;AAED,gCAA+B;oBAjDlB,OAAO,OAAO,EAAE,KAAK;yBACrB,OAAO,OAAO,EAAE,UAAU;6BAC1B,OAAO,OAAO,EAAE,SAAS;yBACzB,OAAO,IAAI,EAAE,QAAQ,CAAC,UAAU;8CANC,qBAAqB"}
@@ -1,7 +1,7 @@
1
- /// <reference types="node" />
2
1
  /**
3
2
  * @typedef {import('multiformats/cid').CID} CID
4
3
  * @typedef {import('./api').BlockWriter} BlockWriter
4
+ * @typedef {import('fs').promises.FileHandle} FileHandle
5
5
  */
6
6
  /**
7
7
  * @class
@@ -26,7 +26,7 @@ export class CarWriter extends BrowserCarWriter implements BlockWriter {
26
26
  * @async
27
27
  * @static
28
28
  * @memberof CarWriter
29
- * @param {fs.promises.FileHandle | number} fd - A file descriptor from the
29
+ * @param {FileHandle | number} fd - A file descriptor from the
30
30
  * Node.js `fs` module. Either an integer, from `fs.open()` or a `FileHandle`
31
31
  * from `fs.promises.open()`.
32
32
  * @param {CID[]} roots - A new list of roots to replace the existing list in
@@ -35,11 +35,11 @@ export class CarWriter extends BrowserCarWriter implements BlockWriter {
35
35
  * as the existing roots.
36
36
  * @returns {Promise<void>}
37
37
  */
38
- static updateRootsInFile(fd: fs.promises.FileHandle | number, roots: CID[]): Promise<void>;
38
+ static updateRootsInFile(fd: FileHandle | number, roots: CID[]): Promise<void>;
39
39
  }
40
- export const __browser: false;
40
+ export const __browser: boolean;
41
41
  export type CID = import('multiformats/cid').CID;
42
42
  export type BlockWriter = import('./api').BlockWriter;
43
+ export type FileHandle = import('fs').promises.FileHandle;
43
44
  import { CarWriter as BrowserCarWriter } from './writer-browser.js';
44
- import fs from 'fs';
45
45
  //# sourceMappingURL=writer.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"writer.d.ts","sourceRoot":"","sources":["../../src/writer.js"],"names":[],"mappings":";AASA;;;GAGG;AAEH;;;GAGG;AACH;IACE;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;IACH,6BATW,GAAG,QAAQ,CAAC,UAAU,GAAG,MAAM,SAG/B,GAAG,EAAE,GAIH,QAAQ,IAAI,CAAC,CAoCzB;CACF;AAED,8BAA8B;kBAzEjB,OAAO,kBAAkB,EAAE,GAAG;0BAC9B,OAAO,OAAO,EAAE,WAAW;8CAPM,qBAAqB;eAJpD,IAAI"}
1
+ {"version":3,"file":"writer.d.ts","sourceRoot":"","sources":["../../src/writer.js"],"names":[],"mappings":"AAKA;;;;GAIG;AAEH;;;GAGG;AACH;IACE;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;IACH,6BATW,UAAU,GAAG,MAAM,SAGnB,GAAG,EAAE,GAIH,QAAQ,IAAI,CAAC,CAoCzB;CACF;AAED,gCAA+B;kBA1ElB,OAAO,kBAAkB,EAAE,GAAG;0BAC9B,OAAO,OAAO,EAAE,WAAW;yBAC3B,OAAO,IAAI,EAAE,QAAQ,CAAC,UAAU;8CALC,qBAAqB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ipld/car",
3
- "version": "5.3.3",
3
+ "version": "5.4.0",
4
4
  "description": "Content Addressable aRchive format reader and writer",
5
5
  "author": "Rod <rod@vagg.org> (http://r.va.gg/)",
6
6
  "license": "Apache-2.0 OR MIT",
@@ -0,0 +1,46 @@
1
+ import fs from 'fs'
2
+ import { promisify } from 'util'
3
+
4
+ const hasFS = Boolean(fs)
5
+
6
+ export { hasFS }
7
+
8
+ /**
9
+ * @type {any}
10
+ */
11
+ let _fsReadFn
12
+ /**
13
+ * @description This function is needed not to initialize the `fs.read` on load time. To run in cf workers without polyfill.
14
+ * @param {number} fd
15
+ * @param {Uint8Array} buffer
16
+ * @param {number} offset
17
+ * @param {number} length
18
+ * @param {number} position
19
+ * @returns {Promise<{ bytesRead: number, buffer: Uint8Array }>}
20
+ */
21
+ export function fsread (fd, buffer, offset, length, position) {
22
+ if (!_fsReadFn) {
23
+ _fsReadFn = promisify(fs.read)
24
+ }
25
+ return _fsReadFn(fd, buffer, offset, length, position)
26
+ }
27
+
28
+ /**
29
+ * @type {any}
30
+ */
31
+ let _fsWriteFn
32
+ /**
33
+ * @description This function is needed not to initialize the `fs.write` on load time. To run in cf workers without polyfill.
34
+ * @param {number} fd
35
+ * @param {Uint8Array} buffer
36
+ * @param {number} offset
37
+ * @param {number} length
38
+ * @param {number} position
39
+ * @returns {Promise<{ bytesRead: number, buffer: Uint8Array }>}
40
+ */
41
+ export function fswrite (fd, buffer, offset, length, position) {
42
+ if (!_fsWriteFn) {
43
+ _fsWriteFn = promisify(fs.write)
44
+ }
45
+ return _fsWriteFn(fd, buffer, offset, length, position)
46
+ }
package/src/reader.js CHANGED
@@ -1,15 +1,13 @@
1
- import fs from 'fs'
2
- import { promisify } from 'util'
1
+ import { fsread, hasFS } from './promise-fs-opts.js'
3
2
  import { CarReader as BrowserCarReader } from './reader-browser.js'
4
3
 
5
4
  /**
6
5
  * @typedef {import('./api').Block} Block
7
6
  * @typedef {import('./api').BlockIndex} BlockIndex
8
7
  * @typedef {import('./api').CarReader} CarReaderIface
8
+ * @typedef {import('fs').promises.FileHandle} FileHandle
9
9
  */
10
10
 
11
- const fsread = promisify(fs.read)
12
-
13
11
  /**
14
12
  * @class
15
13
  * @implements {CarReaderIface}
@@ -27,7 +25,7 @@ export class CarReader extends BrowserCarReader {
27
25
  * @async
28
26
  * @static
29
27
  * @memberof CarReader
30
- * @param {fs.promises.FileHandle | number} fd - A file descriptor from the
28
+ * @param {FileHandle | number} fd - A file descriptor from the
31
29
  * Node.js `fs` module. Either an integer, from `fs.open()` or a `FileHandle`
32
30
  * from `fs.promises.open()`.
33
31
  * @param {BlockIndex} blockIndex - An index pointing to the location of the
@@ -53,4 +51,4 @@ export class CarReader extends BrowserCarReader {
53
51
  }
54
52
  }
55
53
 
56
- export const __browser = false
54
+ export const __browser = !hasFS
package/src/writer.js CHANGED
@@ -1,15 +1,12 @@
1
- import fs from 'fs'
2
- import { promisify } from 'util'
3
1
  import { readHeader, chunkReader } from './decoder.js'
4
2
  import { createHeader } from './encoder.js'
3
+ import { fsread, fswrite, hasFS } from './promise-fs-opts.js'
5
4
  import { CarWriter as BrowserCarWriter } from './writer-browser.js'
6
5
 
7
- const fsread = promisify(fs.read)
8
- const fswrite = promisify(fs.write)
9
-
10
6
  /**
11
7
  * @typedef {import('multiformats/cid').CID} CID
12
8
  * @typedef {import('./api').BlockWriter} BlockWriter
9
+ * @typedef {import('fs').promises.FileHandle} FileHandle
13
10
  */
14
11
 
15
12
  /**
@@ -35,7 +32,7 @@ export class CarWriter extends BrowserCarWriter {
35
32
  * @async
36
33
  * @static
37
34
  * @memberof CarWriter
38
- * @param {fs.promises.FileHandle | number} fd - A file descriptor from the
35
+ * @param {FileHandle | number} fd - A file descriptor from the
39
36
  * Node.js `fs` module. Either an integer, from `fs.open()` or a `FileHandle`
40
37
  * from `fs.promises.open()`.
41
38
  * @param {CID[]} roots - A new list of roots to replace the existing list in
@@ -81,4 +78,4 @@ export class CarWriter extends BrowserCarWriter {
81
78
  }
82
79
  }
83
80
 
84
- export const __browser = false
81
+ export const __browser = !hasFS