@helia/dag-cbor 4.1.0-9993b476 → 4.1.0-e502d45f

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.
@@ -0,0 +1,9 @@
1
+ import { CID } from 'multiformats/cid';
2
+ import type { AddOptions, DAGCBORComponents, DAGCBOR as DAGCBORInterface, GetOptions } from './index.js';
3
+ export declare class DAGCBOR implements DAGCBORInterface {
4
+ private readonly components;
5
+ constructor(components: DAGCBORComponents);
6
+ add(obj: any, options?: AddOptions): Promise<CID>;
7
+ get<T>(cid: CID, options?: GetOptions): Promise<T>;
8
+ }
9
+ //# sourceMappingURL=dag-cbor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dag-cbor.d.ts","sourceRoot":"","sources":["../../src/dag-cbor.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAEtC,OAAO,KAAK,EAAE,UAAU,EAAE,iBAAiB,EAAE,OAAO,IAAI,gBAAgB,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAExG,qBAAa,OAAQ,YAAW,gBAAgB;IAC9C,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAmB;gBAEjC,UAAU,EAAE,iBAAiB;IAIpC,GAAG,CAAE,GAAG,EAAE,GAAG,EAAE,OAAO,GAAE,UAAe,GAAG,OAAO,CAAC,GAAG,CAAC;IAUtD,GAAG,CAAE,CAAC,EAAG,GAAG,EAAE,GAAG,EAAE,OAAO,GAAE,UAAe,GAAG,OAAO,CAAC,CAAC,CAAC;CAS/D"}
@@ -0,0 +1,26 @@
1
+ import { InvalidCodecError } from '@helia/interface';
2
+ import * as codec from '@ipld/dag-cbor';
3
+ import toBuffer from 'it-to-buffer';
4
+ import { CID } from 'multiformats/cid';
5
+ import { sha256 } from 'multiformats/hashes/sha2';
6
+ export class DAGCBOR {
7
+ components;
8
+ constructor(components) {
9
+ this.components = components;
10
+ }
11
+ async add(obj, options = {}) {
12
+ const buf = codec.encode(obj);
13
+ const hash = await (options.hasher ?? sha256).digest(buf);
14
+ const cid = CID.createV1(codec.code, hash);
15
+ await this.components.blockstore.put(cid, buf, options);
16
+ return cid;
17
+ }
18
+ async get(cid, options = {}) {
19
+ if (cid.code !== codec.code) {
20
+ throw new InvalidCodecError('The passed CID had an incorrect codec, it may correspond to a non-DAG-CBOR block');
21
+ }
22
+ const buf = await toBuffer(this.components.blockstore.get(cid, options));
23
+ return codec.decode(buf);
24
+ }
25
+ }
26
+ //# sourceMappingURL=dag-cbor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dag-cbor.js","sourceRoot":"","sources":["../../src/dag-cbor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AACpD,OAAO,KAAK,KAAK,MAAM,gBAAgB,CAAA;AACvC,OAAO,QAAQ,MAAM,cAAc,CAAA;AACnC,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AACtC,OAAO,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAA;AAGjD,MAAM,OAAO,OAAO;IACD,UAAU,CAAmB;IAE9C,YAAa,UAA6B;QACxC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;IAC9B,CAAC;IAED,KAAK,CAAC,GAAG,CAAE,GAAQ,EAAE,UAAsB,EAAE;QAC3C,MAAM,GAAG,GAAG,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;QAC7B,MAAM,IAAI,GAAG,MAAM,CAAC,OAAO,CAAC,MAAM,IAAI,MAAM,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;QACzD,MAAM,GAAG,GAAG,GAAG,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAA;QAE1C,MAAM,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,OAAO,CAAC,CAAA;QAEvD,OAAO,GAAG,CAAA;IACZ,CAAC;IAED,KAAK,CAAC,GAAG,CAAM,GAAQ,EAAE,UAAsB,EAAE;QAC/C,IAAI,GAAG,CAAC,IAAI,KAAK,KAAK,CAAC,IAAI,EAAE,CAAC;YAC5B,MAAM,IAAI,iBAAiB,CAAC,kFAAkF,CAAC,CAAA;QACjH,CAAC;QAED,MAAM,GAAG,GAAG,MAAM,QAAQ,CAAC,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,CAAA;QAExE,OAAO,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;IAC1B,CAAC;CACF"}
@@ -28,17 +28,15 @@ import { CID } from 'multiformats/cid';
28
28
  import type { GetBlockProgressEvents, ProviderOptions, PutBlockProgressEvents } from '@helia/interface/blocks';
29
29
  import type { AbortOptions } from '@libp2p/interface';
30
30
  import type { Blockstore } from 'interface-blockstore';
31
- import type { BlockCodec } from 'multiformats/codecs/interface';
32
31
  import type { MultihashHasher } from 'multiformats/hashes/interface';
33
32
  import type { ProgressOptions } from 'progress-events';
34
33
  export interface DAGCBORComponents {
35
34
  blockstore: Blockstore;
36
35
  }
37
36
  export interface AddOptions extends AbortOptions, ProgressOptions<PutBlockProgressEvents> {
38
- hasher: MultihashHasher;
37
+ hasher?: MultihashHasher;
39
38
  }
40
39
  export interface GetOptions extends AbortOptions, ProgressOptions<GetBlockProgressEvents>, ProviderOptions {
41
- codec: BlockCodec<any, unknown>;
42
40
  }
43
41
  /**
44
42
  * The JSON interface provides a simple and intuitive way to add/get objects
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAIH,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAEtC,OAAO,KAAK,EAAE,sBAAsB,EAAE,eAAe,EAAE,sBAAsB,EAAE,MAAM,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AkB,CAAA;AAEtC,OAAO,KAAK,EAAE,sBAAsB,EAAE,eAAe,EAAE,sBAAsB,EAAE,MAAM,yBAAyB,CAAA;AAC9G,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AACrD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AACtD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAA;AACpE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAEtD,MAAM,WAAW,iBAAiB;IAChC,UAAU,EAAE,UAAU,CAAA;CACvB;AAED,MAAM,WAAW,UAAW,SAAQ,YAAY,EAAE,eAAe,CAAC,sBAAsB,CAAC;IACvF,MAAM,CAAC,EAAE,eAAe,CAAA;CACzB;AAED,MAAM,WAAW,UAAW,SAAQ,YAAY,EAAE,eAAe,CAAC,sBAAsB,CAAC,EAAE,eAAe;CAEzG;AAED;;;GAGG;AACH,MAAM,WAAW,OAAO;IACtB;;;;;;;;;;;;;;;OAeG;IACH,GAAG,CAAC,GAAG,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,UAAU,CAAC,GAAG,OAAO,CAAC,GAAG,CAAC,CAAA;IAE9D;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,UAAU,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAA;CAC5D;AAED;;GAEG;AACH,wBAAgB,OAAO,CAAE,KAAK,EAAE;IAAE,UAAU,EAAE,UAAU,CAAA;CAAE,GAAG,OAAO,CAEnE"}
package/dist/src/index.js CHANGED
@@ -24,31 +24,12 @@
24
24
  * // { hello: 'world' }
25
25
  * ```
26
26
  */
27
- import * as codec from '@ipld/dag-cbor';
28
- import toBuffer from 'it-to-buffer';
29
27
  import { CID } from 'multiformats/cid';
30
- import { sha256 } from 'multiformats/hashes/sha2';
31
- class DefaultDAGCBOR {
32
- components;
33
- constructor(components) {
34
- this.components = components;
35
- }
36
- async add(obj, options = {}) {
37
- const buf = codec.encode(obj);
38
- const hash = await (options.hasher ?? sha256).digest(buf);
39
- const cid = CID.createV1(codec.code, hash);
40
- await this.components.blockstore.put(cid, buf, options);
41
- return cid;
42
- }
43
- async get(cid, options = {}) {
44
- const buf = await toBuffer(this.components.blockstore.get(cid, options));
45
- return codec.decode(buf);
46
- }
47
- }
28
+ import { DAGCBOR as DAGCBORClass } from './dag-cbor.js';
48
29
  /**
49
30
  * Create a {@link DAGCBOR} instance for use with {@link https://github.com/ipfs/helia Helia}
50
31
  */
51
32
  export function dagCbor(helia) {
52
- return new DefaultDAGCBOR(helia);
33
+ return new DAGCBORClass(helia);
53
34
  }
54
35
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,OAAO,KAAK,KAAK,MAAM,gBAAgB,CAAA;AACvC,OAAO,QAAQ,MAAM,cAAc,CAAA;AACnC,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AACtC,OAAO,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAA;AAmEjD,MAAM,cAAc;IACD,UAAU,CAAmB;IAE9C,YAAa,UAA6B;QACxC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;IAC9B,CAAC;IAED,KAAK,CAAC,GAAG,CAAE,GAAQ,EAAE,UAA+B,EAAE;QACpD,MAAM,GAAG,GAAG,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;QAC7B,MAAM,IAAI,GAAG,MAAM,CAAC,OAAO,CAAC,MAAM,IAAI,MAAM,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;QACzD,MAAM,GAAG,GAAG,GAAG,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAA;QAE1C,MAAM,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,OAAO,CAAC,CAAA;QAEvD,OAAO,GAAG,CAAA;IACZ,CAAC;IAED,KAAK,CAAC,GAAG,CAAM,GAAQ,EAAE,UAA+B,EAAE;QACxD,MAAM,GAAG,GAAG,MAAM,QAAQ,CAAC,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,CAAA;QAExE,OAAO,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;IAC1B,CAAC;CACF;AAED;;GAEG;AACH,MAAM,UAAU,OAAO,CAAE,KAAiC;IACxD,OAAO,IAAI,cAAc,CAAC,KAAK,CAAC,CAAA;AAClC,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AACtC,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,MAAM,eAAe,CAAA;AAkEvD;;GAEG;AACH,MAAM,UAAU,OAAO,CAAE,KAAiC;IACxD,OAAO,IAAI,YAAY,CAAC,KAAK,CAAC,CAAA;AAChC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@helia/dag-cbor",
3
- "version": "4.1.0-9993b476",
3
+ "version": "4.1.0-e502d45f",
4
4
  "description": "Add/get IPLD blocks containing dag-cbor with your Helia node",
5
5
  "license": "Apache-2.0 OR MIT",
6
6
  "homepage": "https://github.com/ipfs/helia/tree/main/packages/dag-cbor#readme",
@@ -48,7 +48,7 @@
48
48
  "test:electron-main": "aegir test -t electron-main"
49
49
  },
50
50
  "dependencies": {
51
- "@helia/interface": "5.4.0-9993b476",
51
+ "@helia/interface": "5.4.0-e502d45f",
52
52
  "@ipld/dag-cbor": "^9.2.5",
53
53
  "@libp2p/interface": "^3.0.2",
54
54
  "interface-blockstore": "^6.0.1",
@@ -0,0 +1,34 @@
1
+ import { InvalidCodecError } from '@helia/interface'
2
+ import * as codec from '@ipld/dag-cbor'
3
+ import toBuffer from 'it-to-buffer'
4
+ import { CID } from 'multiformats/cid'
5
+ import { sha256 } from 'multiformats/hashes/sha2'
6
+ import type { AddOptions, DAGCBORComponents, DAGCBOR as DAGCBORInterface, GetOptions } from './index.js'
7
+
8
+ export class DAGCBOR implements DAGCBORInterface {
9
+ private readonly components: DAGCBORComponents
10
+
11
+ constructor (components: DAGCBORComponents) {
12
+ this.components = components
13
+ }
14
+
15
+ async add (obj: any, options: AddOptions = {}): Promise<CID> {
16
+ const buf = codec.encode(obj)
17
+ const hash = await (options.hasher ?? sha256).digest(buf)
18
+ const cid = CID.createV1(codec.code, hash)
19
+
20
+ await this.components.blockstore.put(cid, buf, options)
21
+
22
+ return cid
23
+ }
24
+
25
+ async get <T> (cid: CID, options: GetOptions = {}): Promise<T> {
26
+ if (cid.code !== codec.code) {
27
+ throw new InvalidCodecError('The passed CID had an incorrect codec, it may correspond to a non-DAG-CBOR block')
28
+ }
29
+
30
+ const buf = await toBuffer(this.components.blockstore.get(cid, options))
31
+
32
+ return codec.decode(buf)
33
+ }
34
+ }
package/src/index.ts CHANGED
@@ -25,14 +25,11 @@
25
25
  * ```
26
26
  */
27
27
 
28
- import * as codec from '@ipld/dag-cbor'
29
- import toBuffer from 'it-to-buffer'
30
28
  import { CID } from 'multiformats/cid'
31
- import { sha256 } from 'multiformats/hashes/sha2'
29
+ import { DAGCBOR as DAGCBORClass } from './dag-cbor.js'
32
30
  import type { GetBlockProgressEvents, ProviderOptions, PutBlockProgressEvents } from '@helia/interface/blocks'
33
31
  import type { AbortOptions } from '@libp2p/interface'
34
32
  import type { Blockstore } from 'interface-blockstore'
35
- import type { BlockCodec } from 'multiformats/codecs/interface'
36
33
  import type { MultihashHasher } from 'multiformats/hashes/interface'
37
34
  import type { ProgressOptions } from 'progress-events'
38
35
 
@@ -41,11 +38,11 @@ export interface DAGCBORComponents {
41
38
  }
42
39
 
43
40
  export interface AddOptions extends AbortOptions, ProgressOptions<PutBlockProgressEvents> {
44
- hasher: MultihashHasher
41
+ hasher?: MultihashHasher
45
42
  }
46
43
 
47
44
  export interface GetOptions extends AbortOptions, ProgressOptions<GetBlockProgressEvents>, ProviderOptions {
48
- codec: BlockCodec<any, unknown>
45
+
49
46
  }
50
47
 
51
48
  /**
@@ -95,33 +92,9 @@ export interface DAGCBOR {
95
92
  get<T>(cid: CID, options?: Partial<GetOptions>): Promise<T>
96
93
  }
97
94
 
98
- class DefaultDAGCBOR implements DAGCBOR {
99
- private readonly components: DAGCBORComponents
100
-
101
- constructor (components: DAGCBORComponents) {
102
- this.components = components
103
- }
104
-
105
- async add (obj: any, options: Partial<AddOptions> = {}): Promise<CID> {
106
- const buf = codec.encode(obj)
107
- const hash = await (options.hasher ?? sha256).digest(buf)
108
- const cid = CID.createV1(codec.code, hash)
109
-
110
- await this.components.blockstore.put(cid, buf, options)
111
-
112
- return cid
113
- }
114
-
115
- async get <T> (cid: CID, options: Partial<GetOptions> = {}): Promise<T> {
116
- const buf = await toBuffer(this.components.blockstore.get(cid, options))
117
-
118
- return codec.decode(buf)
119
- }
120
- }
121
-
122
95
  /**
123
96
  * Create a {@link DAGCBOR} instance for use with {@link https://github.com/ipfs/helia Helia}
124
97
  */
125
98
  export function dagCbor (helia: { blockstore: Blockstore }): DAGCBOR {
126
- return new DefaultDAGCBOR(helia)
99
+ return new DAGCBORClass(helia)
127
100
  }