Ephemeral Socket Service
This express middleware allows you to accept incoming HTTP POST requests that will be piped through to later HTTP GET requests.
Why would I want to?
The server acts as a file sharing service but the data is never stored on the server. Instead, the incoming HTTP POST is accepted and then stalled until the HTTP GET request is made. In order to know what URL to use for the HTTP GET request, we write a partial response to the HTTP POST but do not close the stream.
- Create an express server.
- Attach this middleware.
- Respond to events on middleware to create your service.
Here is how it might look:
# Create express serverexpress = require 'express'app = express# Initialize middlewareservice = require'ephemeral-socket-service'attachapp# Respond to eventsserviceon 'socket:opened'socketreswriteHead200socketreswrite"Ephemeral socket open with ID "serviceon 'socket:end'socketres?write'\n\nUpload complete. Socket deleted.\n'socketres?endserviceon 'socket:timeout'socketres?write'\n\nTimed out. No one requested your socket.\n'socketres?endserviceon 'error:not-found'resstatus404send'Not found'# Serveappset'port'9000server = applistenappget'port'->consolelog'Listening on %s'serveraddressport
EphemeralSocketService.attach() takes an option hash as its second argument. The available options are:
||Time (in seconds) that a socket may stay open before it times out|
|maxUploadBytes||2^30 (1 GB)||Set the max number of bytes allowed to be piped through a socket. HTTP POSTs will be rejected with a 413 status code if they exceed this limit|
||RECOMMENDED if you are using reservations. If defined with a string like "http://localhost:9000", this will restrict reservations to that specific referer.|
||An object for creating the socket IDs. See