0.2.0 • Published 3 years ago

@sz-md/postoffice v0.2.0

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

@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"
}