@peerbit/blocks-interface 1.2.19-efee9d3 → 1.3.0-3a75d6e

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
@@ -1,3 +1,3 @@
1
- # Direct block
1
+ # Direct block interface
2
2
 
3
- Block swap/share protocol built on top of [Direct Stream](./../direct-stream/README.md)
3
+ Message types for the Block swap/share protocol
@@ -0,0 +1,28 @@
1
+ import * as dagCbor from "@ipld/dag-cbor";
2
+ import { CID } from "multiformats";
3
+ import { type Block } from "multiformats/block";
4
+ import * as raw from "multiformats/codecs/raw";
5
+ import type { MultihashHasher } from "multiformats/hashes/interface";
6
+ export declare const defaultHasher: import("multiformats/dist/src/hashes/hasher").Hasher<"sha2-256", 18>;
7
+ export declare const codecCodes: {
8
+ 113: typeof dagCbor;
9
+ 85: typeof raw;
10
+ };
11
+ export declare const codecMap: {
12
+ raw: typeof raw;
13
+ "dag-cbor": typeof dagCbor;
14
+ };
15
+ export declare const cidifyString: (str: string) => CID;
16
+ export declare const stringifyCid: (cid: any) => string;
17
+ export declare const checkDecodeBlock: (expectedCID: CID | string, bytes: Uint8Array, options: {
18
+ hasher?: any;
19
+ codec?: any;
20
+ }) => Promise<Block<any, any, any, any>>;
21
+ export declare const getBlockValue: <T>(block: Block<T, any, any, any>, links?: string[]) => Promise<T>;
22
+ export declare const prepareBlockWrite: (value: any, codec: any, links?: string[]) => any;
23
+ export declare const createBlock: (value: any, format: string, options?: {
24
+ hasher?: MultihashHasher<number>;
25
+ links?: string[];
26
+ }) => Promise<Block<any, any, any, any>>;
27
+ export declare const calculateRawCid: (bytes: Uint8Array) => Promise<string>;
28
+ //# sourceMappingURL=block.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"block.d.ts","sourceRoot":"","sources":["../../src/block.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,GAAG,EAAE,MA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import { CID } from "multiformats";
3
+ import { base58btc } from "multiformats/bases/base58";
4
+ import { decode, encode } from "multiformats/block";
5
+ import * as raw from "multiformats/codecs/raw";
6
+ import { sha256 } from "multiformats/hashes/sha2";
7
+ const unsupportedCodecError = () => new Error("unsupported codec");
8
+ const defaultBase = base58btc;
9
+ export const defaultHasher = sha256;
10
+ export const codecCodes = {
11
+ [dagCbor.code]: dagCbor,
12
+ [raw.code]: raw,
13
+ };
14
+ export const codecMap = {
15
+ raw,
16
+ "dag-cbor": dagCbor,
17
+ };
18
+ export const cidifyString = (str) => {
19
+ if (!str) {
20
+ return str; // TODO fix types
21
+ }
22
+ return CID.parse(str, defaultBase);
23
+ };
24
+ export const stringifyCid = (cid) => {
25
+ if (!cid || typeof cid === "string") {
26
+ return cid;
27
+ }
28
+ if (cid["/"]) {
29
+ return defaultBase.encode(cid["/"]);
30
+ }
31
+ return cid.toString(defaultBase);
32
+ };
33
+ export const checkDecodeBlock = async (expectedCID, bytes, options) => {
34
+ const cidObject = typeof expectedCID === "string" ? cidifyString(expectedCID) : expectedCID;
35
+ const codec = options.codec || codecCodes[cidObject.code];
36
+ const block = await decode({
37
+ bytes,
38
+ codec,
39
+ hasher: options?.hasher || defaultHasher,
40
+ });
41
+ if (!block.cid.equals(cidObject)) {
42
+ throw new Error("CID does not match");
43
+ }
44
+ return block;
45
+ };
46
+ export const getBlockValue = async (block, links) => {
47
+ if (block.cid.code === dagCbor.code) {
48
+ const value = block.value;
49
+ links = links || [];
50
+ links.forEach((prop) => {
51
+ if (value[prop]) {
52
+ value[prop] = Array.isArray(value[prop])
53
+ ? value[prop].map(stringifyCid)
54
+ : stringifyCid(value[prop]);
55
+ }
56
+ });
57
+ return value;
58
+ }
59
+ if (block.cid.code === raw.code) {
60
+ return block.value;
61
+ }
62
+ throw new Error("Unsupported");
63
+ };
64
+ export const prepareBlockWrite = (value, codec, links) => {
65
+ if (!codec)
66
+ throw unsupportedCodecError();
67
+ if (codec.code === dagCbor.code) {
68
+ links = links || [];
69
+ links.forEach((prop) => {
70
+ if (value[prop]) {
71
+ value[prop] = Array.isArray(value[prop])
72
+ ? value[prop].map(cidifyString)
73
+ : cidifyString(value[prop]);
74
+ }
75
+ });
76
+ }
77
+ return value;
78
+ };
79
+ export const createBlock = async (value, format, options) => {
80
+ const codec = codecMap[format];
81
+ value = prepareBlockWrite(value, codec, options?.links);
82
+ const block = await encode({
83
+ value,
84
+ codec,
85
+ hasher: options?.hasher || defaultHasher,
86
+ });
87
+ return block;
88
+ };
89
+ export const calculateRawCid = async (bytes) => {
90
+ const block = await createBlock(bytes, "raw");
91
+ return stringifyCid(block.cid);
92
+ };
93
+ //# sourceMappingURL=block.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"block.js","sourceRoot":"","sources":["../../src/block.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AACnC,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AACtD,OAAO,EAAc,MAAM,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAChE,OAAO,KAAK,GAAG,MAAM,yBAAyB,CAAC;AAE/C,OAAO,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAC;AAElD,MAAM,qBAAqB,GAAG,GAAG,EAAE,CAAC,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC;AAEnE,MAAM,WAAW,GAAG,SAAS,CAAC;AAE9B,MAAM,CAAC,MAAM,aAAa,GAAG,MAAM,CAAC;AAEpC,MAAM,CAAC,MAAM,UAAU,GAAG;IACzB,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,OAAO;IACvB,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,GAAG;CACf,CAAC;AACF,MAAM,CAAC,MAAM,QAAQ,GAAG;IACvB,GAAG;IACH,UAAU,EAAE,OAAO;CACnB,CAAC;AAEF,MAAM,CAAC,MAAM,Y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C,IAAI,CAAC,CAAC;oBACvC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,YAAY,CAAC;oBAC/B,CAAC,CAAC,YAAY,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;YAC9B,CAAC;QACF,CAAC,CAAC,CAAC;IACJ,CAAC;IACD,OAAO,KAAK,CAAC;AACd,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,WAAW,GAAG,KAAK,EAC/B,KAAU,EACV,MAAc,EACd,OAGC,EACoC,EAAE;IACvC,MAAM,KAAK,GAAI,QAAgB,CAAC,MAAM,CAAC,CAAC;IACxC,KAAK,GAAG,iBAAiB,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,CAAC,CAAC;IACxD,MAAM,KAAK,GAAG,MAAM,MAAM,CAAC;QAC1B,KAAK;QACL,KAAK;QACL,MAAM,EAAE,OAAO,EAAE,MAAM,IAAI,aAAa;KACxC,CAAC,CAAC;IACH,OAAO,KAAkC,CAAC;AAC3C,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,eAAe,GAAG,KAAK,EAAE,KAAiB,EAAE,EAAE;IAC1D,MAAM,KAAK,GAAG,MAAM,WAAW,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IAC9C,OAAO,YAAY,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;AAChC,CAAC,CAAC"}
@@ -1,5 +1,6 @@
1
1
  import { type WaitForPeer } from "@peerbit/stream-interface";
2
2
  export type GetOptions = {
3
+ signal?: AbortSignal;
3
4
  timeout?: number;
4
5
  replicate?: boolean;
5
6
  from?: string[];
@@ -15,6 +16,7 @@ export interface Blocks extends WaitForPeer {
15
16
  rm(cid: string): MaybePromise<void>;
16
17
  iterator(): AsyncGenerator<[string, Uint8Array], void, void>;
17
18
  size(): MaybePromise<number>;
19
+ persisted(): MaybePromise<boolean>;
18
20
  }
19
- export {};
21
+ export { cidifyString, stringifyCid, createBlock, getBlockValue, calculateRawCid, checkDecodeBlock, codecCodes, defaultHasher, codecMap, } from "./block.js";
20
22
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAE7D,MAAM,MAAM,UAAU,GAAG;IACxB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;CAChB,CAAC;AACF,MAAM,MAAM,UAAU,GAAG;IACxB,OAAO,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,KAAK,YAAY,CAAC,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;AAEtC,MAAM,WAAW,MAAO,SAAQ,WAAW;IAC1C,GAAG,CAAC,KAAK,EAAE,UAAU,GAAG,YAAY,CAAC,MAAM,CAAC,CAAC;IAC7C,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,YAAY,CAAC,OAAO,CAAC,CAAC;IACxC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,UAAU,GAAG,YAAY,CAAC,UAAU,GAAG,SAAS,CAAC,CAAC;IAC7E,EAAE,CAAC,GAAG,EAAE,MAAM,GAAG,YAAY,CAAC,IAAI,CAAC,CAAC;IACpC,QAAQ,IAAI,cAAc,CAAC,CAAC,MAAM,EAAE,UAAU,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;IAC7D,IAAI,IAAI,YAAY,CAAC,MAAM,CAAC,CAAC;CAC7B"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAE7D,MAAM,MAAM,UAAU,GAAG;IACxB,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;CAChB,CAAC;AACF,MAAM,MAAM,UAAU,GAAG;IACxB,OAAO,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,KAAK,YAAY,CAAC,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;AAEtC,MAAM,WAAW,MAAO,SAAQ,WAAW;IAC1C,GAAG,CAAC,KAAK,EAAE,UAAU,GAAG,YAAY,CAAC,MAAM,CAAC,CAAC;IAC7C,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,YAAY,CAAC,OAAO,CAAC,CAAC;IACxC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,UAAU,GAAG,YAAY,CAAC,UAAU,GAAG,SAAS,CAAC,CAAC;IAC7E,EAAE,CAAC,GAAG,EAAE,MAAM,GAAG,YAAY,CAAC,IAAI,CAAC,CAAC;IACpC,QAAQ,IAAI,cAAc,CAAC,CAAC,MAAM,EAAE,UAAU,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;IAC7D,IAAI,IAAI,YAAY,CAAC,MAAM,CAAC,CAAC;IAC7B,SAAS,IAAI,YAAY,CAAC,OAAO,CAAC,CAAC;CACnC;AAED,OAAO,EACN,YAAY,EACZ,YAAY,EACZ,WAAW,EACX,aAAa,EACb,eAAe,EACf,gBAAgB,EAChB,UAAU,EACV,aAAa,EACb,QAAQ,GACR,MAAM,YAAY,CAAC"}
package/dist/src/index.js CHANGED
@@ -1,2 +1,3 @@
1
1
  import {} from "@peerbit/stream-interface";
2
+ export { cidifyString, stringifyCid, createBlock, getBlockValue, calculateRawCid, checkDecodeBlock, codecCodes, defaultHasher, codecMap, } from "./block.js";
2
3
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,MAAM,2BAA2B,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,MAAM,2BAA2B,CAAC;AAwB7D,OAAO,EACN,YAAY,EACZ,YAAY,EACZ,WAAW,EACX,aAAa,EACb,eAAe,EACf,gBAAgB,EAChB,UAAU,EACV,aAAa,EACb,QAAQ,GACR,MAAM,YAAY,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peerbit/blocks-interface",
3
- "version": "1.2.19-efee9d3",
3
+ "version": "1.3.0-3a75d6e",
4
4
  "description": "Block store streaming",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -35,7 +35,7 @@
35
35
  }
36
36
  },
37
37
  "eslintConfig": {
38
- "extends": "ipfs",
38
+ "extends": "peerbit",
39
39
  "parserOptions": {
40
40
  "project": true,
41
41
  "sourceType": "module"
@@ -52,7 +52,8 @@
52
52
  "scripts": {
53
53
  "clean": "aegir clean",
54
54
  "build": "aegir build --no-bundle",
55
- "test": ""
55
+ "test": "",
56
+ "lint": "aegir lint"
56
57
  },
57
58
  "engines": {
58
59
  "node": ">=16.15.1"
@@ -74,11 +75,11 @@
74
75
  "dao.xyz"
75
76
  ],
76
77
  "devDependencies": {
77
- "@peerbit/libp2p-test-utils": "2.1.3-efee9d3"
78
+ "@peerbit/libp2p-test-utils": "2.1.4-3a75d6e"
78
79
  },
79
80
  "dependencies": {
80
- "@dao-xyz/borsh": "^5.2.2",
81
- "@peerbit/crypto": "2.2.0-efee9d3",
82
- "@peerbit/stream-interface": "5.0.2-efee9d3"
81
+ "@dao-xyz/borsh": "^5.2.3",
82
+ "@peerbit/crypto": "2.3.0-3a75d6e",
83
+ "@peerbit/stream-interface": "5.0.3-3a75d6e"
83
84
  }
84
85
  }
package/src/block.ts ADDED
@@ -0,0 +1,119 @@
1
+ import * as dagCbor from "@ipld/dag-cbor";
2
+ import { CID } from "multiformats";
3
+ import { base58btc } from "multiformats/bases/base58";
4
+ import { type Block, decode, encode } from "multiformats/block";
5
+ import * as raw from "multiformats/codecs/raw";
6
+ import type { MultihashHasher } from "multiformats/hashes/interface";
7
+ import { sha256 } from "multiformats/hashes/sha2";
8
+
9
+ const unsupportedCodecError = () => new Error("unsupported codec");
10
+
11
+ const defaultBase = base58btc;
12
+
13
+ export const defaultHasher = sha256;
14
+
15
+ export const codecCodes = {
16
+ [dagCbor.code]: dagCbor,
17
+ [raw.code]: raw,
18
+ };
19
+ export const codecMap = {
20
+ raw,
21
+ "dag-cbor": dagCbor,
22
+ };
23
+
24
+ export const cidifyString = (str: string): CID => {
25
+ if (!str) {
26
+ return str as any as CID; // TODO fix types
27
+ }
28
+
29
+ return CID.parse(str, defaultBase);
30
+ };
31
+
32
+ export const stringifyCid = (cid: any): string => {
33
+ if (!cid || typeof cid === "string") {
34
+ return cid;
35
+ }
36
+
37
+ if (cid["/"]) {
38
+ return defaultBase.encode(cid["/"]);
39
+ }
40
+ return cid.toString(defaultBase);
41
+ };
42
+
43
+ export const checkDecodeBlock = async (
44
+ expectedCID: CID | string,
45
+ bytes: Uint8Array,
46
+ options: { hasher?: any; codec?: any },
47
+ ): Promise<Block<any, any, any, any>> => {
48
+ const cidObject =
49
+ typeof expectedCID === "string" ? cidifyString(expectedCID) : expectedCID;
50
+ const codec = options.codec || (codecCodes as any)[cidObject.code];
51
+ const block = await decode({
52
+ bytes,
53
+ codec,
54
+ hasher: options?.hasher || defaultHasher,
55
+ });
56
+ if (!block.cid.equals(cidObject)) {
57
+ throw new Error("CID does not match");
58
+ }
59
+ return block as Block<any, any, any, 1>;
60
+ };
61
+ export const getBlockValue = async <T>(
62
+ block: Block<T, any, any, any>,
63
+ links?: string[],
64
+ ): Promise<T> => {
65
+ if (block.cid.code === dagCbor.code) {
66
+ const value = block.value as any;
67
+ links = links || [];
68
+ links.forEach((prop) => {
69
+ if (value[prop]) {
70
+ value[prop] = Array.isArray(value[prop])
71
+ ? value[prop].map(stringifyCid)
72
+ : stringifyCid(value[prop]);
73
+ }
74
+ });
75
+ return value;
76
+ }
77
+ if (block.cid.code === raw.code) {
78
+ return block.value as T;
79
+ }
80
+ throw new Error("Unsupported");
81
+ };
82
+
83
+ export const prepareBlockWrite = (value: any, codec: any, links?: string[]) => {
84
+ if (!codec) throw unsupportedCodecError();
85
+ if (codec.code === dagCbor.code) {
86
+ links = links || [];
87
+ links.forEach((prop) => {
88
+ if (value[prop]) {
89
+ value[prop] = Array.isArray(value[prop])
90
+ ? value[prop].map(cidifyString)
91
+ : cidifyString(value[prop]);
92
+ }
93
+ });
94
+ }
95
+ return value;
96
+ };
97
+
98
+ export const createBlock = async (
99
+ value: any,
100
+ format: string,
101
+ options?: {
102
+ hasher?: MultihashHasher<number>;
103
+ links?: string[];
104
+ },
105
+ ): Promise<Block<any, any, any, any>> => {
106
+ const codec = (codecMap as any)[format];
107
+ value = prepareBlockWrite(value, codec, options?.links);
108
+ const block = await encode({
109
+ value,
110
+ codec,
111
+ hasher: options?.hasher || defaultHasher,
112
+ });
113
+ return block as Block<any, any, any, any>;
114
+ };
115
+
116
+ export const calculateRawCid = async (bytes: Uint8Array) => {
117
+ const block = await createBlock(bytes, "raw");
118
+ return stringifyCid(block.cid);
119
+ };
package/src/index.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { type WaitForPeer } from "@peerbit/stream-interface";
2
2
 
3
3
  export type GetOptions = {
4
+ signal?: AbortSignal;
4
5
  timeout?: number;
5
6
  replicate?: boolean;
6
7
  from?: string[];
@@ -18,4 +19,17 @@ export interface Blocks extends WaitForPeer {
18
19
  rm(cid: string): MaybePromise<void>;
19
20
  iterator(): AsyncGenerator<[string, Uint8Array], void, void>;
20
21
  size(): MaybePromise<number>;
22
+ persisted(): MaybePromise<boolean>;
21
23
  }
24
+
25
+ export {
26
+ cidifyString,
27
+ stringifyCid,
28
+ createBlock,
29
+ getBlockValue,
30
+ calculateRawCid,
31
+ checkDecodeBlock,
32
+ codecCodes,
33
+ defaultHasher,
34
+ codecMap,
35
+ } from "./block.js";