@libp2p/multistream-select
Advanced tools
Comparing version 3.0.0 to 3.1.0
@@ -18,4 +18,4 @@ import { PROTOCOL_ID } from './constants.js'; | ||
} | ||
export { select } from './select.js'; | ||
export { select, lazySelect } from './select.js'; | ||
export { handle } from './handle.js'; | ||
//# sourceMappingURL=index.d.ts.map |
import { PROTOCOL_ID } from './constants.js'; | ||
export { PROTOCOL_ID }; | ||
export { select } from './select.js'; | ||
export { select, lazySelect } from './select.js'; | ||
export { handle } from './handle.js'; | ||
//# sourceMappingURL=index.js.map |
import type { Duplex } from 'it-stream-types'; | ||
import type { Uint8ArrayList } from 'uint8arraylist'; | ||
import { Uint8ArrayList } from 'uint8arraylist'; | ||
import type { ByteArrayInit, ByteListInit, ProtocolStream } from './index.js'; | ||
export declare function select(stream: Duplex<Uint8Array>, protocols: string | string[], options: ByteArrayInit): Promise<ProtocolStream<Uint8Array>>; | ||
export declare function select(stream: Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array>, protocols: string | string[], options?: ByteListInit): Promise<ProtocolStream<Uint8ArrayList, Uint8ArrayList | Uint8Array>>; | ||
/** | ||
* Lazily negotiates a protocol. | ||
* | ||
* It *does not* block writes waiting for the other end to respond. Instead, it | ||
* simply assumes the negotiation went successfully and starts writing data. | ||
* | ||
* Use when it is known that the receiver supports the desired protocol. | ||
*/ | ||
export declare function lazySelect(stream: Duplex<Uint8Array>, protocol: string): ProtocolStream<Uint8Array>; | ||
export declare function lazySelect(stream: Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array>, protocol: string): ProtocolStream<Uint8ArrayList, Uint8ArrayList | Uint8Array>; | ||
//# sourceMappingURL=select.d.ts.map |
@@ -7,2 +7,6 @@ import { logger } from '@libp2p/logger'; | ||
import { PROTOCOL_ID } from './index.js'; | ||
import { Uint8ArrayList } from 'uint8arraylist'; | ||
import { pushable } from 'it-pushable'; | ||
import merge from 'it-merge'; | ||
import { reader } from 'it-reader'; | ||
const log = logger('libp2p:mss:select'); | ||
@@ -46,2 +50,47 @@ export async function select(stream, protocols, options = {}) { | ||
} | ||
export function lazySelect(stream, protocol) { | ||
// This is a signal to write the multistream headers if the consumer tries to | ||
// read from the source | ||
const negotiateTrigger = pushable(); | ||
let negotiated = false; | ||
return { | ||
stream: { | ||
sink: async (source) => await stream.sink((async function* () { | ||
let first = true; | ||
for await (const chunk of merge(source, negotiateTrigger)) { | ||
if (first) { | ||
first = false; | ||
negotiated = true; | ||
negotiateTrigger.end(); | ||
const p1 = uint8ArrayFromString(PROTOCOL_ID); | ||
const p2 = uint8ArrayFromString(protocol); | ||
const list = new Uint8ArrayList(multistream.encode(p1), multistream.encode(p2)); | ||
if (chunk.length > 0) | ||
list.append(chunk); | ||
yield* list; | ||
} | ||
else { | ||
yield chunk; | ||
} | ||
} | ||
})()), | ||
source: (async function* () { | ||
if (!negotiated) | ||
negotiateTrigger.push(new Uint8Array()); | ||
const byteReader = reader(stream.source); | ||
let response = await multistream.readString(byteReader); | ||
if (response === PROTOCOL_ID) { | ||
response = await multistream.readString(byteReader); | ||
} | ||
if (response !== protocol) { | ||
throw errCode(new Error('protocol selection failed'), 'ERR_UNSUPPORTED_PROTOCOL'); | ||
} | ||
for await (const chunk of byteReader) { | ||
yield* chunk; | ||
} | ||
})() | ||
}, | ||
protocol | ||
}; | ||
} | ||
//# sourceMappingURL=select.js.map |
{ | ||
"name": "@libp2p/multistream-select", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "JavaScript implementation of multistream-select", | ||
@@ -152,2 +152,3 @@ "license": "Apache-2.0 OR MIT", | ||
"it-length-prefixed": "^8.0.2", | ||
"it-merge": "^1.0.4", | ||
"it-pipe": "^2.0.3", | ||
@@ -159,3 +160,3 @@ "it-pushable": "^3.0.0", | ||
"uint8arraylist": "^2.3.1", | ||
"uint8arrays": "^3.0.0" | ||
"uint8arrays": "^4.0.2" | ||
}, | ||
@@ -162,0 +163,0 @@ "devDependencies": { |
@@ -24,3 +24,3 @@ import { PROTOCOL_ID } from './constants.js' | ||
export { select } from './select.js' | ||
export { select, lazySelect } from './select.js' | ||
export { handle } from './handle.js' |
@@ -8,3 +8,6 @@ import { logger } from '@libp2p/logger' | ||
import type { Duplex } from 'it-stream-types' | ||
import type { Uint8ArrayList } from 'uint8arraylist' | ||
import { Uint8ArrayList } from 'uint8arraylist' | ||
import { pushable } from 'it-pushable' | ||
import merge from 'it-merge' | ||
import { reader } from 'it-reader' | ||
import type { ByteArrayInit, ByteListInit, MultistreamSelectInit, ProtocolStream } from './index.js' | ||
@@ -62,1 +65,54 @@ | ||
} | ||
/** | ||
* Lazily negotiates a protocol. | ||
* | ||
* It *does not* block writes waiting for the other end to respond. Instead, it | ||
* simply assumes the negotiation went successfully and starts writing data. | ||
* | ||
* Use when it is known that the receiver supports the desired protocol. | ||
*/ | ||
export function lazySelect (stream: Duplex<Uint8Array>, protocol: string): ProtocolStream<Uint8Array> | ||
export function lazySelect (stream: Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array>, protocol: string): ProtocolStream<Uint8ArrayList, Uint8ArrayList | Uint8Array> | ||
export function lazySelect (stream: Duplex<any>, protocol: string): ProtocolStream<any> { | ||
// This is a signal to write the multistream headers if the consumer tries to | ||
// read from the source | ||
const negotiateTrigger = pushable() | ||
let negotiated = false | ||
return { | ||
stream: { | ||
sink: async source => await stream.sink((async function * () { | ||
let first = true | ||
for await (const chunk of merge(source, negotiateTrigger)) { | ||
if (first) { | ||
first = false | ||
negotiated = true | ||
negotiateTrigger.end() | ||
const p1 = uint8ArrayFromString(PROTOCOL_ID) | ||
const p2 = uint8ArrayFromString(protocol) | ||
const list = new Uint8ArrayList(multistream.encode(p1), multistream.encode(p2)) | ||
if (chunk.length > 0) list.append(chunk) | ||
yield * list | ||
} else { | ||
yield chunk | ||
} | ||
} | ||
})()), | ||
source: (async function * () { | ||
if (!negotiated) negotiateTrigger.push(new Uint8Array()) | ||
const byteReader = reader(stream.source) | ||
let response = await multistream.readString(byteReader) | ||
if (response === PROTOCOL_ID) { | ||
response = await multistream.readString(byteReader) | ||
} | ||
if (response !== protocol) { | ||
throw errCode(new Error('protocol selection failed'), 'ERR_UNSUPPORTED_PROTOCOL') | ||
} | ||
for await (const chunk of byteReader) { | ||
yield * chunk | ||
} | ||
})() | ||
}, | ||
protocol | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
45690
497
0
15
+ Addedit-merge@^1.0.4
+ Addedfast-fifo@1.3.2(transitive)
+ Addedit-merge@1.0.4(transitive)
+ Addedit-pushable@1.4.2(transitive)
- Removedmultiformats@9.9.0(transitive)
- Removeduint8arrays@3.1.1(transitive)
Updateduint8arrays@^4.0.2