holepunch-hop 0.4.4 → 0.4.8
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/.vscode/settings.json +3 -0
- package/package.json +11 -5
- package/src/index.js +45 -10
- package/src/{peers.js → network/peers.js} +507 -138
- package/src/{bees.js → storage/bees.js} +47 -37
- package/src/{drive.js → storage/drive.js} +2 -3
- package/src/{fileParser.js → storage/fileParser.js} +1 -1
- package/test/datacommands/files/data/jan3-bitcoin.csv +17 -0
- package/test/datacommands/files/large-csv.test.js +7 -0
- package/test/datacommands/files/small-csv.test.js +76 -0
- package/test/datacommands/ledger/save-get-ledger.test.js +117 -0
- package/test/datacommands/results/save-get-results.test.js +122 -0
- package/test/holepunch-initiate.test.js +47 -0
- package/test/multipers/ten-peers-network.test.js +437 -0
- package/test/setup-bee-holepunch.test.js +45 -0
- package/test/setup-holepunch.test.js +14 -13
- package/test/threepers/peer3-geninvite-after.test.js +439 -0
- package/test/threepers/three-peers.test.js +159 -0
- package/test/threepers/two-then-three.test.js +434 -0
- package/test/twopeers/peerClient-Server.test.js +243 -0
- package/test/twopeers/reconnect-peers.test.js +257 -0
- package/test/twopeers/reconnect-serverthen-conerr.test.js +304 -0
- package/test/twopeers/reconnect-then-conerr.test.js +309 -0
- package/test/twopeers/two-peer-one-disconnect.test.js +162 -0
- package/test/twopeers/two-peer-server-disconnect.test.js +167 -0
- package/vitest.config.js +8 -0
- /package/src/{kbledger.js → ledger/kbledger.js} +0 -0
|
@@ -22,6 +22,7 @@ class HyperBee extends EventEmitter {
|
|
|
22
22
|
this.liveBees = {}
|
|
23
23
|
this.confirmPubLibList = {}
|
|
24
24
|
this.repPublicHolder = {}
|
|
25
|
+
this.activeBees = []
|
|
25
26
|
}
|
|
26
27
|
|
|
27
28
|
/**
|
|
@@ -113,6 +114,7 @@ class HyperBee extends EventEmitter {
|
|
|
113
114
|
keyEncoding: 'utf-8', // can be set to undefined (binary), utf-8, ascii or and abstract-encoding
|
|
114
115
|
valueEncoding: 'json' // same options as above
|
|
115
116
|
})
|
|
117
|
+
|
|
116
118
|
await this.dbBentocues.ready()
|
|
117
119
|
beePubkeys.push({store:'bentocues', privacy: 'public', pubkey: b4a.toString(core7.key, 'hex')})
|
|
118
120
|
// open the cues library
|
|
@@ -181,40 +183,7 @@ class HyperBee extends EventEmitter {
|
|
|
181
183
|
startBeePubkey.data = beePubkeys
|
|
182
184
|
this.liveBees = startBeePubkey
|
|
183
185
|
this.wsocket.send(JSON.stringify(startBeePubkey))
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* save pair in keystore public network library
|
|
188
|
-
* @method savePubliclibrary
|
|
189
|
-
*
|
|
190
|
-
*/
|
|
191
|
-
savePubliclibrary = async function (refContract) {
|
|
192
|
-
let beeSave = await this.dbPublicLibrary.put(refContract.data.hash, refContract.data.contract)
|
|
193
|
-
// go query the key are return the info. to ensure data save asplanned.
|
|
194
|
-
let saveCheck = await this.getPublicLibrary(refContract.data.hash)
|
|
195
|
-
let returnMessage = {}
|
|
196
|
-
returnMessage.stored = true
|
|
197
|
-
returnMessage.type = refContract.reftype
|
|
198
|
-
returnMessage.key = saveCheck.key
|
|
199
|
-
returnMessage.contract = saveCheck.value
|
|
200
|
-
return returnMessage
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* save pair in keystore db
|
|
206
|
-
* @method savePeerLibrary
|
|
207
|
-
*
|
|
208
|
-
*/
|
|
209
|
-
savePeerLibrary = async function (refContract) {
|
|
210
|
-
await this.dbPeerLibrary.put(refContract.data.hash, refContract.data.contract)
|
|
211
|
-
let saveCheck = await this.getPeerLibrary(refContract.data.hash)
|
|
212
|
-
let returnMessage = {}
|
|
213
|
-
returnMessage.stored = true
|
|
214
|
-
returnMessage.type = refContract.reftype
|
|
215
|
-
returnMessage.key = saveCheck.key
|
|
216
|
-
returnMessage.contract = saveCheck.value
|
|
217
|
-
return returnMessage
|
|
186
|
+
this.activeBees = beePubkeys
|
|
218
187
|
}
|
|
219
188
|
|
|
220
189
|
/**
|
|
@@ -698,6 +667,42 @@ class HyperBee extends EventEmitter {
|
|
|
698
667
|
return nodeData
|
|
699
668
|
}
|
|
700
669
|
|
|
670
|
+
//** */ public library **//
|
|
671
|
+
|
|
672
|
+
/**
|
|
673
|
+
* save pair in keystore public network library
|
|
674
|
+
* @method savePubliclibrary
|
|
675
|
+
*
|
|
676
|
+
*/
|
|
677
|
+
savePubliclibrary = async function (refContract) {
|
|
678
|
+
await this.dbPublicLibrary.put(refContract.data.hash, refContract.data.contract)
|
|
679
|
+
// go query the key are return the info. to ensure data save asplanned.
|
|
680
|
+
let saveCheck = await this.getPublicLibrary(refContract.data.hash)
|
|
681
|
+
let returnMessage = {}
|
|
682
|
+
returnMessage.stored = true
|
|
683
|
+
returnMessage.type = refContract.reftype
|
|
684
|
+
returnMessage.key = saveCheck.key
|
|
685
|
+
returnMessage.contract = saveCheck.value
|
|
686
|
+
return returnMessage
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
|
|
690
|
+
/**
|
|
691
|
+
* save pair in keystore db
|
|
692
|
+
* @method savePeerLibrary
|
|
693
|
+
*
|
|
694
|
+
*/
|
|
695
|
+
savePeerLibrary = async function (refContract) {
|
|
696
|
+
await this.dbPeerLibrary.put(refContract.data.hash, refContract.data.contract)
|
|
697
|
+
let saveCheck = await this.getPeerLibrary(refContract.data.hash)
|
|
698
|
+
let returnMessage = {}
|
|
699
|
+
returnMessage.stored = true
|
|
700
|
+
returnMessage.type = refContract.reftype
|
|
701
|
+
returnMessage.key = saveCheck.key
|
|
702
|
+
returnMessage.contract = saveCheck.value
|
|
703
|
+
return returnMessage
|
|
704
|
+
}
|
|
705
|
+
|
|
701
706
|
/**
|
|
702
707
|
* lookup specific result UUID
|
|
703
708
|
* @method getPublicLibrary
|
|
@@ -890,9 +895,14 @@ class HyperBee extends EventEmitter {
|
|
|
890
895
|
for await (const { key, value } of nodeData) {
|
|
891
896
|
resData.push({ key, value })
|
|
892
897
|
}
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
898
|
+
// data received?
|
|
899
|
+
if (resData.length > 0) {
|
|
900
|
+
this.repPublicHolder[dataIn.discoverykey] = resData
|
|
901
|
+
// notify peer repliate complete, ask if want save
|
|
902
|
+
this.emit('publib-replicate-notification', { data: { text: 'public library replication complete', publib: dataIn.discoverykey }})
|
|
903
|
+
} else {
|
|
904
|
+
this.emit('publib-replicate-notification', { data: { text: 'no data received', publib: dataIn.discoverykey }})
|
|
905
|
+
}
|
|
896
906
|
}
|
|
897
907
|
|
|
898
908
|
/**
|
|
@@ -13,7 +13,7 @@ import fs from 'fs'
|
|
|
13
13
|
import Hyperdrive from 'hyperdrive'
|
|
14
14
|
import b4a from 'b4a'
|
|
15
15
|
import Fileparser from './fileParser.js'
|
|
16
|
-
import SqliteAdapter from '
|
|
16
|
+
import SqliteAdapter from '../adapters/sqliteDatabase.js'
|
|
17
17
|
import csv from 'csv-parser'
|
|
18
18
|
import { DateTime, Interval } from 'luxon'
|
|
19
19
|
import * as chrono from 'chrono-node'
|
|
@@ -54,12 +54,11 @@ class HypDrive extends EventEmitter {
|
|
|
54
54
|
// One for a file metadata Hyperbee, and one for a content Hypercore
|
|
55
55
|
this.drive = new Hyperdrive(this.core)
|
|
56
56
|
await this.drive.ready()
|
|
57
|
-
|
|
58
57
|
let startDrivePubkey = {}
|
|
59
58
|
startDrivePubkey.type = 'account'
|
|
60
59
|
startDrivePubkey.action = 'drive-pubkey'
|
|
61
60
|
startDrivePubkey.data = b4a.toString(this.drive.key, 'hex')
|
|
62
|
-
this.wsocket.send(JSON.stringify(startDrivePubkey))
|
|
61
|
+
// this.wsocket.send(JSON.stringify(startDrivePubkey))
|
|
63
62
|
}
|
|
64
63
|
|
|
65
64
|
|
|
@@ -18,7 +18,7 @@ import events from 'events'
|
|
|
18
18
|
import axios from 'axios'
|
|
19
19
|
import csv from 'csv-parser'
|
|
20
20
|
import crypto from 'crypto'
|
|
21
|
-
import TimeConvert from '
|
|
21
|
+
import TimeConvert from '../adapters/timeConvertor.js'
|
|
22
22
|
import { DateTime, Interval } from 'luxon'
|
|
23
23
|
import * as chrono from 'chrono-node'
|
|
24
24
|
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
datetime,tz_cd,price,volume,source,site_no
|
|
2
|
+
2009-01-03 01:30,PST,0,0,bitcoin,0
|
|
3
|
+
2010-01-03 01:30,PST,0,0,bitcoin,0
|
|
4
|
+
2011-01-03 01:30,PST,0.3,14300,bitcoin,0
|
|
5
|
+
2012-01-03 01:30,PST,4.9,125170,bitcoin,0
|
|
6
|
+
2013-01-03 01:30,PST,13.4,18030,bitcoin,0
|
|
7
|
+
2014-01-03 01:30,PST,884.3,9710,bitcoin,0
|
|
8
|
+
2015-01-03 01:30,PST,287.1,47370,bitcoin,0
|
|
9
|
+
2016-01-03 01:30,PST,430.7,54830,bitcoin,0
|
|
10
|
+
2017-01-03 01:30,PST,1033.3,547900,bitcoin,0
|
|
11
|
+
2018-01-03 01:31,PST,15156.6,106540,bitcoin,0
|
|
12
|
+
2019-01-03 01:32,PST,3780.1,468450,bitcoin,0
|
|
13
|
+
2020-01-03 01:33,PST,7343.1,938290,bitcoin,0
|
|
14
|
+
2021-01-03 01:34,PST,32958.9,155210,bitcoin,0
|
|
15
|
+
2022-01-03 01:35,PST,46430.2,41060,bitcoin,0
|
|
16
|
+
2023-01-03 01:36,PST,16674.2,178730,bitcoin,0
|
|
17
|
+
2024-01-03 01:37,PST,42836.1,117650,bitcoin,0
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
|
2
|
+
import Corestore from 'corestore'
|
|
3
|
+
import fs from 'fs'
|
|
4
|
+
import path from 'path'
|
|
5
|
+
import os from 'os'
|
|
6
|
+
import Hyperswarm from 'hyperswarm'
|
|
7
|
+
import goodbye from 'graceful-goodbye'
|
|
8
|
+
import b4a from 'b4a'
|
|
9
|
+
import DriveWorker from '../../../src/storage/drive.js'
|
|
10
|
+
|
|
11
|
+
// Get the path to the test data directory
|
|
12
|
+
const testDataPath = path.join(__dirname, 'data')
|
|
13
|
+
const testCSVPath = path.join(testDataPath, 'jan3-bitcoin.csv')
|
|
14
|
+
|
|
15
|
+
describe('CSV File Operations Tests', () => {
|
|
16
|
+
let store
|
|
17
|
+
let swarm
|
|
18
|
+
let clientDrive
|
|
19
|
+
|
|
20
|
+
beforeEach(async () => {
|
|
21
|
+
|
|
22
|
+
store = new Corestore(os.homedir() + '/.test-file-store')
|
|
23
|
+
swarm = new Hyperswarm()
|
|
24
|
+
// make replication possible
|
|
25
|
+
swarm.on('connection', conn => store.replicate(conn))
|
|
26
|
+
goodbye(() => swarm.destroy())
|
|
27
|
+
|
|
28
|
+
// Create HypDrive instance
|
|
29
|
+
clientDrive = new DriveWorker(store, swarm)
|
|
30
|
+
|
|
31
|
+
// Initialize drive
|
|
32
|
+
await clientDrive.setupHyperdrive()
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
afterEach(async () => {
|
|
36
|
+
// Clean up
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
it('should save and retrieve small CSV file', async () => {
|
|
40
|
+
// 1. Read the actual CSV file
|
|
41
|
+
const csvContent = fs.readFileSync(testCSVPath, 'utf-8')
|
|
42
|
+
const csvFileName = path.basename(testCSVPath)
|
|
43
|
+
|
|
44
|
+
// 2. Save CSV file
|
|
45
|
+
await clientDrive.hyperdriveFilesave('text/csv', csvFileName, csvContent)
|
|
46
|
+
|
|
47
|
+
// 3. Verify file is saved
|
|
48
|
+
const clientFileContent = await clientDrive.drive.get('text/csv/' + csvFileName)
|
|
49
|
+
expect(clientFileContent.toString()).toBe(csvContent)
|
|
50
|
+
|
|
51
|
+
// 4. Verify file metadata
|
|
52
|
+
const fileInfo = await clientDrive.drive.entry('text/csv/' + csvFileName)
|
|
53
|
+
expect(fileInfo).toBeDefined()
|
|
54
|
+
expect(fileInfo.value.blob).toBeDefined()
|
|
55
|
+
expect(fileInfo.value.blob.byteLength).toBe(csvContent.length)
|
|
56
|
+
|
|
57
|
+
// 5. Verify file structure (headers)
|
|
58
|
+
const fileLines = csvContent.split('\n')
|
|
59
|
+
const headers = fileLines[0].split(',')
|
|
60
|
+
expect(headers.length).toBeGreaterThan(0)
|
|
61
|
+
expect(headers[0]).toBeDefined()
|
|
62
|
+
|
|
63
|
+
// 6. Verify specific 2015 entry
|
|
64
|
+
const entry2015 = fileLines[7] // 7th line (0-indexed)
|
|
65
|
+
expect(entry2015).toBe('2015-01-03 01:30,PST,287.1,47370,bitcoin,0')
|
|
66
|
+
|
|
67
|
+
// 7. Verify specific values in 2015 entry
|
|
68
|
+
const entryValues = entry2015.split(',')
|
|
69
|
+
expect(entryValues[0]).toBe('2015-01-03 01:30')
|
|
70
|
+
expect(entryValues[1]).toBe('PST')
|
|
71
|
+
expect(entryValues[2]).toBe('287.1')
|
|
72
|
+
expect(entryValues[3]).toBe('47370')
|
|
73
|
+
expect(entryValues[4]).toBe('bitcoin')
|
|
74
|
+
expect(entryValues[5]).toBe('0')
|
|
75
|
+
})
|
|
76
|
+
})
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
|
|
2
|
+
import Corestore from 'corestore'
|
|
3
|
+
import Hyperswarm from 'hyperswarm'
|
|
4
|
+
import Hyperbee from 'hyperbee'
|
|
5
|
+
import b4a from 'b4a'
|
|
6
|
+
import hashObject from 'object-hash'
|
|
7
|
+
import path from 'path'
|
|
8
|
+
import os from 'os'
|
|
9
|
+
import goodbye from 'graceful-goodbye'
|
|
10
|
+
import BeeWorker from '../../../src/storage/bees.js'
|
|
11
|
+
|
|
12
|
+
// Get the path to the test data directory
|
|
13
|
+
const testDataPath = path.join(__dirname, 'data')
|
|
14
|
+
|
|
15
|
+
describe('Hyperbee Ledger Tests', () => {
|
|
16
|
+
let store
|
|
17
|
+
let swarm
|
|
18
|
+
let bee
|
|
19
|
+
|
|
20
|
+
beforeAll(async () => {
|
|
21
|
+
console.log('start of before each')
|
|
22
|
+
store = new Corestore(os.homedir() + '/.test-ledger-store')
|
|
23
|
+
swarm = new Hyperswarm()
|
|
24
|
+
// make replication possible
|
|
25
|
+
swarm.on('connection', conn => store.replicate(conn))
|
|
26
|
+
goodbye(() => swarm.destroy())
|
|
27
|
+
|
|
28
|
+
// Additional wait for full initialization
|
|
29
|
+
await new Promise((resolve) => setTimeout(resolve, 4000))
|
|
30
|
+
console.log('wait afoter')
|
|
31
|
+
// Create BeeWorker instance
|
|
32
|
+
bee = new BeeWorker(store, swarm)
|
|
33
|
+
// await bee.setupHyperbee() vitest have issues starting so many core at once
|
|
34
|
+
const core5 = store.get({ name: 'kbledger' })
|
|
35
|
+
bee.dbKBledger = new Hyperbee(core5, {
|
|
36
|
+
keyEncoding: 'utf-8',
|
|
37
|
+
valueEncoding: 'json'
|
|
38
|
+
})
|
|
39
|
+
await bee.dbKBledger.ready()
|
|
40
|
+
await new Promise((resolve) => setTimeout(resolve, 2000))
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
afterAll(async () => {
|
|
44
|
+
console.log('Cleaning up test environment')
|
|
45
|
+
// await swarm.destroy()
|
|
46
|
+
await bee.dbKBledger.close()
|
|
47
|
+
bee = null
|
|
48
|
+
store = null
|
|
49
|
+
// swarm = null
|
|
50
|
+
}, 20000) // 20 second timeout for cleanup
|
|
51
|
+
|
|
52
|
+
it('should perform basic Hyperbee operations', async () => {
|
|
53
|
+
// Test data
|
|
54
|
+
console.log('start of test')
|
|
55
|
+
const testHash = 'test-hash'
|
|
56
|
+
const testData = { message: 'Hello Hyperbee!' }
|
|
57
|
+
|
|
58
|
+
// 1. Put data
|
|
59
|
+
await bee.dbKBledger.put(testHash, testData)
|
|
60
|
+
|
|
61
|
+
// 2. Get data
|
|
62
|
+
const result = await bee.dbKBledger.get(testHash)
|
|
63
|
+
expect(result.value).toEqual(testData)
|
|
64
|
+
|
|
65
|
+
// 3. Batch operations with 32-byte keys
|
|
66
|
+
let res1 = { bbid: '1212121212', data: { value: [1, 2, 3] }}
|
|
67
|
+
let res2 = { bbid: '3232323232', data: { value: [4, 5, 6] }}
|
|
68
|
+
const batch = bee.dbKBledger.batch()
|
|
69
|
+
const key1 = hashObject(res1)
|
|
70
|
+
const key2 = hashObject(res2)
|
|
71
|
+
await batch.put(key1, res1)
|
|
72
|
+
await batch.put(key2, res2)
|
|
73
|
+
await batch.flush()
|
|
74
|
+
|
|
75
|
+
// 4. CreateReadStream
|
|
76
|
+
const stream = bee.dbKBledger.createReadStream()
|
|
77
|
+
const entries = []
|
|
78
|
+
for await (const entry of stream) {
|
|
79
|
+
entries.push(entry)
|
|
80
|
+
}
|
|
81
|
+
expect(entries.length).toBeGreaterThan(0)
|
|
82
|
+
|
|
83
|
+
// 5. Check version
|
|
84
|
+
expect(bee.dbKBledger.version).toBeGreaterThan(0)
|
|
85
|
+
|
|
86
|
+
// 6. Verify batch operations
|
|
87
|
+
const result1 = await bee.dbKBledger.get(key1)
|
|
88
|
+
expect(result1.key).toEqual(key1)
|
|
89
|
+
const result2 = await bee.dbKBledger.get(key2)
|
|
90
|
+
expect(result2.key).toEqual(key2)
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
it('should handle key value operations', async () => {
|
|
94
|
+
// Test data
|
|
95
|
+
console.log('start of test2')
|
|
96
|
+
let res1 = { bbid: '1212121212', data: { value: [1, 2, 3] }}
|
|
97
|
+
let res2 = { bbid: '3232323232', data: { value: [4, 5, 6] }}
|
|
98
|
+
const key1 = hashObject(res1)
|
|
99
|
+
const key2 = hashObject(res2)
|
|
100
|
+
|
|
101
|
+
// Put multiple values
|
|
102
|
+
await bee.dbKBledger.put(key1, res1)
|
|
103
|
+
await bee.dbKBledger.put(key2, res2)
|
|
104
|
+
|
|
105
|
+
// Get values
|
|
106
|
+
const result1 = await bee.dbKBledger.get(key1)
|
|
107
|
+
const result2 = await bee.dbKBledger.get(key2)
|
|
108
|
+
expect(result1.value.data.value[0]).toEqual(1)
|
|
109
|
+
expect(result2.value.data.value[0]).toEqual(4)
|
|
110
|
+
|
|
111
|
+
// Delete value
|
|
112
|
+
await bee.dbKBledger.del(key1)
|
|
113
|
+
const deletedResult = await bee.dbKBledger.get(key1)
|
|
114
|
+
expect(deletedResult).toBeNull()
|
|
115
|
+
console.log('end of test2')
|
|
116
|
+
})
|
|
117
|
+
})
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
|
|
2
|
+
import Corestore from 'corestore'
|
|
3
|
+
import Hyperswarm from 'hyperswarm'
|
|
4
|
+
import Hyperbee from 'hyperbee'
|
|
5
|
+
import b4a from 'b4a'
|
|
6
|
+
import hashObject from 'object-hash'
|
|
7
|
+
import path from 'path'
|
|
8
|
+
import os from 'os'
|
|
9
|
+
import goodbye from 'graceful-goodbye'
|
|
10
|
+
import BeeWorker from '../../../src/storage/bees.js'
|
|
11
|
+
|
|
12
|
+
// Get the path to the test data directory
|
|
13
|
+
const testDataPath = path.join(__dirname, 'data')
|
|
14
|
+
|
|
15
|
+
describe('Hyperbee Operations Tests', () => {
|
|
16
|
+
let store
|
|
17
|
+
let swarm
|
|
18
|
+
let bee
|
|
19
|
+
let core4
|
|
20
|
+
|
|
21
|
+
beforeAll(async () => {
|
|
22
|
+
console.log('start of before each')
|
|
23
|
+
store = new Corestore(os.homedir() + '/.test-bee-store')
|
|
24
|
+
swarm = new Hyperswarm()
|
|
25
|
+
// make replication possible
|
|
26
|
+
swarm.on('connection', conn => store.replicate(conn))
|
|
27
|
+
goodbye(() => swarm.destroy())
|
|
28
|
+
|
|
29
|
+
// Additional wait for full initialization
|
|
30
|
+
await new Promise((resolve) => setTimeout(resolve, 4000))
|
|
31
|
+
console.log('wait afoter')
|
|
32
|
+
// Create BeeWorker instance
|
|
33
|
+
bee = new BeeWorker(store, swarm)
|
|
34
|
+
// await bee.setupHyperbee() vitest have issues starting so many core at once
|
|
35
|
+
core4 = store.get({ name: 'hopresults' })
|
|
36
|
+
bee.dbHOPresults = new Hyperbee(core4, {
|
|
37
|
+
keyEncoding: 'utf-8', // can be set to undefined (binary), utf-8, ascii or and abstract-encoding
|
|
38
|
+
valueEncoding: 'json' // same options as above
|
|
39
|
+
})
|
|
40
|
+
await bee.dbHOPresults.ready()
|
|
41
|
+
await new Promise((resolve) => setTimeout(resolve, 2000))
|
|
42
|
+
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
afterAll(async () => {
|
|
46
|
+
console.log('Cleaning up test environment')
|
|
47
|
+
// await swarm.destroy()
|
|
48
|
+
await bee.dbHOPresults.close()
|
|
49
|
+
await core4.close()
|
|
50
|
+
bee = null
|
|
51
|
+
store = null
|
|
52
|
+
//swarm = null
|
|
53
|
+
core4 = null
|
|
54
|
+
}, 20000) // 20 second timeout for cleanup
|
|
55
|
+
|
|
56
|
+
it('should perform basic Hyperbee operations', async () => {
|
|
57
|
+
// Test data
|
|
58
|
+
console.log('start of test')
|
|
59
|
+
const testHash = 'test-hash'
|
|
60
|
+
const testData = { message: 'Hello Hyperbee!' }
|
|
61
|
+
|
|
62
|
+
// 1. Put data
|
|
63
|
+
await bee.dbHOPresults.put(testHash, testData)
|
|
64
|
+
|
|
65
|
+
// 2. Get data
|
|
66
|
+
const result = await bee.dbHOPresults.get(testHash)
|
|
67
|
+
expect(result.value).toEqual(testData)
|
|
68
|
+
|
|
69
|
+
// 3. Batch operations with 32-byte keys
|
|
70
|
+
let res1 = { bbid: '1212121212', data: { value: [1, 2, 3] }}
|
|
71
|
+
let res2 = { bbid: '3232323232', data: { value: [4, 5, 6] }}
|
|
72
|
+
const batch = bee.dbHOPresults.batch()
|
|
73
|
+
const key1 = hashObject(res1)
|
|
74
|
+
const key2 = hashObject(res2)
|
|
75
|
+
await batch.put(key1, res1)
|
|
76
|
+
await batch.put(key2, res2)
|
|
77
|
+
await batch.flush()
|
|
78
|
+
|
|
79
|
+
// 4. CreateReadStream
|
|
80
|
+
const stream = bee.dbHOPresults.createReadStream()
|
|
81
|
+
const entries = []
|
|
82
|
+
for await (const entry of stream) {
|
|
83
|
+
entries.push(entry)
|
|
84
|
+
}
|
|
85
|
+
expect(entries.length).toBeGreaterThan(0)
|
|
86
|
+
|
|
87
|
+
// 5. Check version
|
|
88
|
+
expect(bee.dbHOPresults.version).toBeGreaterThan(0)
|
|
89
|
+
|
|
90
|
+
// 6. Verify batch operations
|
|
91
|
+
const result1 = await bee.dbHOPresults.get(key1)
|
|
92
|
+
expect(result1.key).toEqual(key1)
|
|
93
|
+
const result2 = await bee.dbHOPresults.get(key2)
|
|
94
|
+
expect(result2.key).toEqual(key2)
|
|
95
|
+
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
it('should handle key value operations', async () => {
|
|
99
|
+
// Test data
|
|
100
|
+
console.log('start of test2')
|
|
101
|
+
let res1 = { bbid: '1212121212', data: { value: [1, 2, 3] }}
|
|
102
|
+
let res2 = { bbid: '3232323232', data: { value: [4, 5, 6] }}
|
|
103
|
+
const key1 = hashObject(res1)
|
|
104
|
+
const key2 = hashObject(res2)
|
|
105
|
+
|
|
106
|
+
// Put multiple values
|
|
107
|
+
await bee.dbHOPresults.put(key1, res1)
|
|
108
|
+
await bee.dbHOPresults.put(key2, res2)
|
|
109
|
+
|
|
110
|
+
// Get values
|
|
111
|
+
const result1 = await bee.dbHOPresults.get(key1)
|
|
112
|
+
const result2 = await bee.dbHOPresults.get(key2)
|
|
113
|
+
expect(result1.value.data.value[0]).toEqual(1)
|
|
114
|
+
expect(result2.value.data.value[0]).toEqual(4)
|
|
115
|
+
|
|
116
|
+
// Delete value
|
|
117
|
+
await bee.dbHOPresults.del(key1)
|
|
118
|
+
const deletedResult = await bee.dbHOPresults.get(key1)
|
|
119
|
+
expect(deletedResult).toBeNull()
|
|
120
|
+
console.log('end of test2')
|
|
121
|
+
})
|
|
122
|
+
})
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
|
|
2
|
+
import { spawn } from 'child_process'
|
|
3
|
+
import path from 'path'
|
|
4
|
+
import HolepunchWorker from '../src/index.js'
|
|
5
|
+
|
|
6
|
+
// Set global test timeout to 10 seconds
|
|
7
|
+
const testTimeout = 10000;
|
|
8
|
+
|
|
9
|
+
describe('holepunch initialization', () => {
|
|
10
|
+
let hopProcess
|
|
11
|
+
let holepunch
|
|
12
|
+
|
|
13
|
+
beforeAll(async () => {
|
|
14
|
+
// Start HOP server
|
|
15
|
+
const baseHOPStepsUp = path.join(__dirname, '..')
|
|
16
|
+
hopProcess = spawn('npm', ['run', 'start'], { stdio: 'inherit', cwd: baseHOPStepsUp })
|
|
17
|
+
|
|
18
|
+
// Wait for server to start
|
|
19
|
+
await new Promise((resolve) => setTimeout(resolve, 3000))
|
|
20
|
+
|
|
21
|
+
// Initialize HolepunchWorker
|
|
22
|
+
holepunch = new HolepunchWorker('.test-instance')
|
|
23
|
+
await holepunch.startHolepunch()
|
|
24
|
+
// Additional wait for full initialization
|
|
25
|
+
await new Promise((resolve) => setTimeout(resolve, 2000))
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
afterAll(async () => {
|
|
29
|
+
// Clean up HolepunchWorker
|
|
30
|
+
// Stop HOP server
|
|
31
|
+
if (hopProcess) {
|
|
32
|
+
hopProcess.kill()
|
|
33
|
+
}
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it('should have initialized store', () => {
|
|
37
|
+
expect(holepunch.store).toBeDefined()
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it('should have initialized swarm', () => {
|
|
41
|
+
expect(holepunch.swarm).toBeDefined()
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it('should have initialized BeeData', () => {
|
|
45
|
+
expect(holepunch.BeeData).toBeDefined()
|
|
46
|
+
})
|
|
47
|
+
})
|