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

fix: update deps #7

Merged
merged 2 commits into from
Feb 16, 2022
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
24 changes: 14 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,26 @@
"name": "it-handshake",
"version": "3.0.0",
"description": "Create handshakes for binary protocols with iterable streams",
"homepage": "https://github.com/jacobheun/it-handshake#readme",
"author": "Jacob Heun",
"license": "Apache-2.0 OR MIT",
"homepage": "https://github.com/jacobheun/it-handshake#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/jacobheun/it-handshake.git"
},
"bugs": {
"url": "https://github.com/jacobheun/it-handshake/issues"
},
"keywords": [
"async",
"handshake",
"iterable",
"iterator"
],
"engines": {
"node": ">=16.0.0",
"npm": ">=7.0.0"
},
"type": "module",
"types": "./dist/src/index.d.ts",
"files": [
Expand Down Expand Up @@ -128,7 +139,7 @@
"dependencies": {
"it-map": "^1.0.6",
"it-pushable": "^2.0.1",
"it-reader": "^4.0.2",
"it-reader": "^5.0.0",
"it-stream-types": "^1.0.4",
"p-defer": "^4.0.0"
},
Expand All @@ -138,12 +149,5 @@
"it-pair": "^2.0.2",
"it-pipe": "^2.0.2",
"uint8arrays": "^3.0.0"
},
"author": "Jacob Heun",
"keywords": [
"async",
"iterable",
"iterator",
"handshake"
]
}
}
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface Handshake {
stream: Duplex<Uint8Array>
rest: () => Source<Uint8Array>
write: (data: Uint8Array) => void
read: () => Promise<Uint8Array>
read: () => Promise<Uint8Array | undefined>
}

// Convert a duplex stream into a reader and writer and rest stream
Expand Down Expand Up @@ -53,7 +53,10 @@ export function handshake (stream: Duplex<Uint8Array>): Handshake {
write: writer.push,
read: async () => {
const res = await source.next()
return res.value.slice()

if (res.value != null) {
return res.value.slice()
}
}
}
}
22 changes: 11 additions & 11 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ describe('handshake', () => {

iShake.write(uint8ArrayFromString('hello'))
let message = await rShake.read()
expect(message.slice()).to.eql(uint8ArrayFromString('hello'))
expect(message).to.eql(uint8ArrayFromString('hello'))
rShake.write(uint8ArrayFromString('hi'))
rShake.rest()
message = await iShake.read()
expect(message.slice()).to.eql(uint8ArrayFromString('hi'))
expect(message).to.eql(uint8ArrayFromString('hi'))
iShake.rest()

const buffer = uint8ArrayFromString('more data')
void pipe(
rShake.stream,
(source) => (async function * () {
for await (const message of source) {
expect(message.slice()).to.eql(buffer)
expect(message).to.eql(buffer)
yield message
}
})(),
Expand All @@ -43,23 +43,23 @@ describe('handshake', () => {

iShake.write(uint8ArrayFromString('hello'))
let message = await rShake.read()
expect(message.slice()).to.eql(uint8ArrayFromString('hello'))
expect(message).to.eql(uint8ArrayFromString('hello'))
rShake.write(uint8ArrayFromString('hi'))
rShake.rest()
message = await iShake.read()
expect(message.slice()).to.eql(uint8ArrayFromString('hi'))
expect(message).to.eql(uint8ArrayFromString('hi'))
iShake.rest()

const iShake2 = handshake(iShake.stream)
const rShake2 = handshake(rShake.stream)

iShake2.write(uint8ArrayFromString('ready?'))
message = await rShake2.read()
expect(message.slice()).to.eql(uint8ArrayFromString('ready?'))
expect(message).to.eql(uint8ArrayFromString('ready?'))
rShake2.write(uint8ArrayFromString('yes!'))
rShake2.rest()
message = await iShake2.read()
expect(message.slice()).to.eql(uint8ArrayFromString('yes!'))
expect(message).to.eql(uint8ArrayFromString('yes!'))
iShake2.rest()

void pipe(
Expand Down Expand Up @@ -87,10 +87,10 @@ describe('handshake', () => {
// handshake before the responder finishes
iShake.write(uint8ArrayFromString('hello'))
message = await rShake.read()
expect(message.slice()).to.eql(uint8ArrayFromString('hello'))
expect(message).to.eql(uint8ArrayFromString('hello'))
rShake.write(uint8ArrayFromString('hi'))
message = await iShake.read()
expect(message.slice()).to.eql(uint8ArrayFromString('hi'))
expect(message).to.eql(uint8ArrayFromString('hi'))
iShake.rest()

const iShake2 = handshake(iShake.stream)
Expand All @@ -100,11 +100,11 @@ describe('handshake', () => {
const rShake2 = handshake(rShake.stream)

message = await rShake2.read()
expect(message.slice()).to.eql(uint8ArrayFromString('ready?'))
expect(message).to.eql(uint8ArrayFromString('ready?'))
rShake2.write(uint8ArrayFromString('yes!'))
rShake2.rest()
message = await iShake2.read()
expect(message.slice()).to.eql(uint8ArrayFromString('yes!'))
expect(message).to.eql(uint8ArrayFromString('yes!'))
iShake2.rest()

void pipe(
Expand Down