@sz-md/postoffice

0.2.0 • Public • Published

@sz-md/postoffice

JSON Spec:

{
	"requestId": "<client-request-id>",
	"request": "Request Body"
}

{
	"originRequestId": "<client-request-id>",
	"response": "Response body"
}

API:

.onrequest = function(body) {} // request handler
send()
request() // returns promise
rejectPendingRequests()

Client:

const PostOffice = require("@sz-sw/postoffice")
const NodeTCPSocket = require("@sz-md/node-tcp-socket")

async function main() {
	const instance = await PostOffice.create()
	const socket = await NodeTCPSocket.create({
		host: "localhost",
		port: 1337
	})

	socket.on("data", instance.receiveBytes)
	instance.sendBytes = socket.write

	console.log(
		await instance.sendRequest("request…")
	)
}
main()

Server:

const PostOffice = require("@sz-sw/postoffice")
const instance = await PostOffice.create()

connection.on("data", instance.receiveBytes)
instance.sendBytes = connection.write.bind(connection)

instance.onrequest = function(body) {
	return "My answer is: 42"
}

Readme

Keywords

none

Package Sidebar

Install

npm i @sz-md/postoffice

Weekly Downloads

5

Version

0.2.0

License

MIT

Unpacked Size

15.7 kB

Total Files

16

Last publish

Collaborators

  • sz-coder