Skip to content

Commit 9958fc9

Browse files
committed
Add memory store - Fix tests
1 parent 233aae3 commit 9958fc9

4 files changed

Lines changed: 80 additions & 31 deletions

File tree

test/crypto/crypto-handshake.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
"use strict"
22

3-
const fs = require("fs")
4-
const path = require("path")
5-
63
const crypto_data = {
74
"secio": require("zeronet-crypto/secio"),
85
"tls-rsa": require("zeronet-crypto/tls")
96
}
7+
108
const cryptos = Object.keys(crypto_data).map(c => {
119
return {
1210
name: c,
@@ -16,6 +14,7 @@ const cryptos = Object.keys(crypto_data).map(c => {
1614
const Swarm = require("zeronet-swarm")
1715

1816
const multiaddr = require("multiaddr")
17+
const memstore = require("zeronet-storage-memory")
1918

2019
let swarm
2120

@@ -29,7 +28,8 @@ cryptos.forEach(crypto => {
2928
server: {
3029
host: "127.0.0.1",
3130
port: 25335
32-
}
31+
},
32+
storage: new memstore()
3333
}, err => {
3434
if (err) return cb(err)
3535

test/index.js

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,12 @@ require("app-module-path").addPath(path.join(__dirname, ".."))
55

66
const PeerId = require("peer-id")
77

8-
const common = require("../zeronet-common")
9-
const swarm = require("../zeronet-swarm")
10-
118
before(function (cb) {
129
this.timeout(5000)
1310
PeerId.create((e, id) => {
1411
if (e) return cb(e)
15-
1612
global.id = id
17-
18-
const listenSwarm = new swarm({
19-
id,
20-
server: {
21-
host: "0.0.0.0",
22-
port: 15543
23-
},
24-
server6: {
25-
host: "::",
26-
port: 15543
27-
}
28-
}, err => {
29-
if (err) return cb(err)
30-
31-
const dialSwarm = new swarm({
32-
id
33-
}, err => {
34-
if (err) return cb(err)
35-
})
36-
37-
return cb()
38-
39-
})
13+
cb()
4014
})
4115
})
4216

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"use strict"
2+
3+
function FSMock(store) {
4+
const self = this
5+
self.exists = (file, cb) => cb(null, !!store[file])
6+
self.readFile = (file, cb) => store[file] ? cb(null, store[file]) : cb(new Error("ENOTFOUND: " + file))
7+
self.writeFile = (file, data, cb) => cb(null, store[file] = data)
8+
self.unlink = (file, cb) => store[file] ? cb(null, delete store[file]) : cb(new Error("ENOTFOUND: " + file))
9+
}
10+
11+
module.exports = function ZeroNetStorageFS() {
12+
//store stuff in memory
13+
14+
const self = this
15+
16+
let json, file, fs, jfs
17+
18+
const getPath = (a, b) => a + "/" + b
19+
20+
self.file = {
21+
exists: (zite, version, inner_path, cb) => fs.exists(getPath(zite, inner_path), res => cb(null, res)),
22+
read: (zite, version, inner_path, cb) => fs.readFile(getPath(zite, inner_path), cb),
23+
write: (zite, version, inner_path, cb) => fs.readFile(getPath(zite, inner_path), cb),
24+
remove: (zite, version, inner_path, cb) => fs.unlink(getPath(zite, inner_path), cb)
25+
}
26+
27+
self.json = {
28+
exists: (key, cb) => jfs.exists(getPath("json", key), res => cb(null, res)),
29+
read: (key, cb) => jfs.readFile(getPath("json", key), cb),
30+
write: (key, data, cb) => jfs.writeFile(getPath("json", key), data, cb),
31+
remove: (key, cb) => jfs.unlink(getPath("json", key), cb)
32+
}
33+
34+
self.start = cb => {
35+
json = {}
36+
jfs = new FSMock(json)
37+
file = {}
38+
fs = new FSMock(json)
39+
cb()
40+
}
41+
42+
self.stop = cb => {
43+
json = null
44+
jfs = null
45+
file = null
46+
fs = null
47+
cb()
48+
}
49+
50+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "zeronet-storage-memory",
3+
"version": "0.0.1-alpha13pre",
4+
"description": "ZeroNet filesystem memory storage module",
5+
"main": "lib/index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [
10+
"zeronet",
11+
"storage",
12+
"memory"
13+
],
14+
"author": "Maciej Krüger <mkg20001@gmail.com>",
15+
"license": "GPL-3.0",
16+
"repository": {
17+
"type": "git",
18+
"url": "git+https://github.com/mkg20001/zeronet-js.git"
19+
},
20+
"bugs": {
21+
"url": "https://github.com/mkg20001/zeronet-js/issues"
22+
},
23+
"homepage": "https://github.com/mkg20001/zeronet-js#readme",
24+
"dependencies": {}
25+
}

0 commit comments

Comments
 (0)