Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Network module #571

Merged
merged 2 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions api/network.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
*
*
* @module Network
*
* Provides a higher level API over the stream-relay protocol.
*
* @see {@link https://socketsupply.co/guides/#p2p-guide}
*
*/
import sugar from './stream-relay/sugar.js'
import { Cache, sha256, Encryption, NAT } from './stream-relay/index.js'
import events from './events.js'
import dgram from './dgram.js'

const network = sugar(dgram, events)

export { network, Cache, sha256, Encryption, NAT }
export default network
195 changes: 0 additions & 195 deletions api/peer.js

This file was deleted.

38 changes: 18 additions & 20 deletions api/stream-relay/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,15 @@ export class Cache {
return this.data.size
}

toJSON () {
return [...this.data.entries()]
}

/**
* Inserts a `CacheEntry` value `v` into the cache at key `k`.
* @param {string} k
* @param {CacheEntry} v
* @return {Promise<boolean>}
* @return {boolean}
*/
async insert (k, v) {
insert (k, v) {
if (v.type !== PacketPublish.type) return false
if (this.has(k)) return true
if (this.has(k)) return false

if (this.data.size === this.maxSize) {
const oldest = [...this.data.values()].sort(this.siblingResolver).pop()
Expand All @@ -104,23 +100,24 @@ export class Cache {
v.timestamp = Date.now()

this.data.set(k, v)
if (this.onInsert) this.onInsert(k, v)
return true
}

/**
* Gets a `CacheEntry` value at key `k`.
* @param {string} k
* @return {Promise<CacheEntry?>}
* @return {CacheEntry?}
*/
async get (k) {
get (k) {
return this.data.get(k)
}

/**
* @param {string} k
* @return {Promise<boolean>}
* @return {boolean}
*/
async delete (k) {
delete (k) {
if (!this.has(k)) return false
this.data.delete(k)
return true
Expand All @@ -139,20 +136,20 @@ export class Cache {
* Composes an indexed packet into a new `Packet`
* @param {Packet} packet
*/
async compose (packet) {
if (packet.previousId && packet?.index > 0) packet = await this.get(packet.previousId)
if (!packet) return null
async compose (packet, source = this.data) {
let previous = packet

if (packet?.index > 0) previous = source.get(packet.previousId)
if (!previous) return null

const { meta, size, indexes } = packet.message
const { meta, size, indexes } = previous.message

// follow the chain to get the buffers in order
const bufs = [...this.data.values()]
.filter(p => p.previousId === packet.packetId)
const bufs = [...source.values()]
.filter(p => p.previousId === previous.packetId)
.sort((a, b) => a.index - b.index)

if (!indexes || bufs.length < indexes) {
return null
}
if (!indexes || bufs.length < indexes) return null

// sort by index, concat and then hash, the original should match
const messages = bufs.map(p => p.message)
Expand Down Expand Up @@ -220,6 +217,7 @@ export class Cache {

// partition the cache into children
for (const key of this.data.keys()) {
if (!key || !key.slice) continue
if (prefix.length && !key.startsWith(prefix)) continue
const hex = key.slice(prefix.length, prefix.length + 1)
children[parseInt(hex, 16)].push(key)
Expand Down
Loading
Loading