Skip to content

Commit 80009dc

Browse files
committed
test: node listener
1 parent 9f94072 commit 80009dc

File tree

5 files changed

+102
-5
lines changed

5 files changed

+102
-5
lines changed

examples/src/index.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ function fileIterator(file: File): AsyncIterable<Uint8Array> {
3131
}
3232

3333
return new Promise((resolve, reject) => {
34-
const chunk = file.slice(index, MAX_CHUNK_SIZE);
35-
index += MAX_CHUNK_SIZE;
34+
const chunk = file.slice(index, (index += MAX_CHUNK_SIZE));
3635

3736
const reader = new global.FileReader();
3837

@@ -144,12 +143,11 @@ function App() {
144143
client.store,
145144
{
146145
cidVersion: 1,
147-
maxChunkSize: 256 * 1024,
148146
rawLeaves: true,
149147
wrapWithDirectory: true,
150148
}
151149
)) {
152-
console.log(chunk.path, chunk.cid);
150+
console.log(chunk);
153151
if (chunk.path === "") {
154152
setUproot(chunk.cid);
155153
}

package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,28 @@
33
"version": "0.1.0",
44
"description": "JS implementation of GraphSync and DataTransfer protocols",
55
"main": "dist/src/index.js",
6+
"files": [
7+
"src",
8+
"dist"
9+
],
610
"scripts": {
711
"build": "aegir build",
812
"test": "aegir test",
13+
"test:node": "npm run test -- -t node",
914
"check": "aegir check"
1015
},
1116
"author": "Myel <thomas@myel.dev>",
1217
"license": "MIT",
18+
"engines": {
19+
"node": ">=16.0.0",
20+
"npm": ">=7.0.0"
21+
},
22+
"repository": {
23+
"type": "git",
24+
"url": "git+https://github.com/myelnet/js-graphsync.git"
25+
},
1326
"devDependencies": {
27+
"@chainsafe/libp2p-noise": "5.0.0",
1428
"@types/bl": "^5.0.2",
1529
"@types/mime": "^2.0.3",
1630
"@types/uuid": "^8.3.4",
@@ -20,6 +34,8 @@
2034
"ipfs-unixfs-importer": "^9.0.6",
2135
"it-pair": "^1.0.0",
2236
"libp2p-interfaces": "^2.0.1",
37+
"libp2p-mplex": "^0.10.5",
38+
"libp2p-tcp": "^0.17.2",
2339
"prettier": "^2.6.1"
2440
},
2541
"dependencies": {
@@ -42,5 +58,9 @@
4258
"uuid": "^8.3.2",
4359
"varint": "^6.0.0",
4460
"varint-decoder": "^1.0.0"
61+
},
62+
"types": "dist/src/index.d.ts",
63+
"publishConfig": {
64+
"access": "public"
4565
}
4666
}

src/messages.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export function newRequest(
140140
req.ext = ext;
141141
}
142142
return lp.encode.single(
143-
new Buffer(
143+
Buffer.from(
144144
dagCBOR.encode<GraphSyncMessageRoot>({
145145
gs2: {
146146
req: [req],

test/graphsync.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {importer} from "ipfs-unixfs-importer";
88
import {resolve, unixfsPathSelector} from "../src/resolver";
99

1010
describe("graphsync", () => {
11+
// this test mocks libp2p so it can run in the browser.
1112
it("e2e", async () => {
1213
const store1 = new MemoryBlockstore();
1314
const net1 = new MockLibp2p(

test/listening.node.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import {expect} from "aegir/utils/chai.js";
2+
import {create as createLibp2p, Libp2p} from "libp2p";
3+
import PeerId from "peer-id";
4+
import Mplex from "libp2p-mplex";
5+
import TCP from "libp2p-tcp";
6+
import {NOISE} from "@chainsafe/libp2p-noise";
7+
import {MemoryBlockstore} from "blockstore-core/memory";
8+
import {importer} from "ipfs-unixfs-importer";
9+
import {GraphSync} from "../src/graphsync";
10+
import {unixfsPathSelector, resolve} from "../src/resolver";
11+
import {concatChunkIterator} from "./mock-libp2p";
12+
13+
async function createNode(): Promise<Libp2p> {
14+
const node = await createLibp2p({
15+
addresses: {
16+
listen: ["/ip4/0.0.0.0/tcp/0"],
17+
},
18+
modules: {
19+
transport: [TCP],
20+
connEncryption: [NOISE],
21+
streamMuxer: [Mplex],
22+
},
23+
});
24+
await node.start();
25+
return node;
26+
}
27+
28+
describe("listening", () => {
29+
it("reified transfer", async () => {
30+
const store1 = new MemoryBlockstore();
31+
const net1 = await createNode();
32+
33+
const first = new Uint8Array(5 * 256);
34+
const second = new Uint8Array(3 * 256);
35+
36+
// chunk and dagify it then get the root cid
37+
let cid;
38+
for await (const chunk of importer(
39+
[
40+
{path: "first", content: first},
41+
{path: "second", content: second},
42+
],
43+
store1,
44+
{
45+
cidVersion: 1,
46+
maxChunkSize: 256,
47+
rawLeaves: true,
48+
wrapWithDirectory: true,
49+
}
50+
)) {
51+
if (chunk.path === "") {
52+
cid = chunk.cid;
53+
}
54+
}
55+
56+
if (!cid) {
57+
throw new Error("failed to add DAG");
58+
}
59+
60+
const store2 = new MemoryBlockstore();
61+
const net2 = await createNode();
62+
63+
net2.peerStore.addressBook.add(net1.peerId, net1.multiaddrs);
64+
65+
const provider = new GraphSync(net1, store1);
66+
provider.start();
67+
68+
const client = new GraphSync(net2, store2);
69+
client.start();
70+
const {root, selector} = unixfsPathSelector(cid.toString() + "/first");
71+
72+
const request = client.request(root, selector);
73+
request.open(net1.peerId);
74+
75+
const buf = await concatChunkIterator(resolve(cid, selector, request));
76+
expect(buf).to.deep.equal(first);
77+
});
78+
});

0 commit comments

Comments
 (0)