-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from comake/version/0.7.0
Version/0.7.0
- Loading branch information
Showing
22 changed files
with
738 additions
and
260 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"@context": "https://linkedsoftwaredependencies.org/bundles/npm/@comake/solid-on-rails/^0.0.0/components/context.jsonld", | ||
"@graph": [ | ||
{ | ||
"@id": "urn:solid-on-rails:default:ServerConfigurator", | ||
"@type": "ParallelHandler", | ||
"handlers": [ | ||
{ | ||
"comment": "Handles all request events from the server.", | ||
"@id": "urn:solid-on-rails:default:HandlerServerConfigurator", | ||
"@type": "HandlerServerConfigurator", | ||
"handler": { "@id": "urn:solid-on-rails:default:HttpHandler" }, | ||
"showStackTrace": { "@id": "urn:solid-on-rails:default:variable:showStackTrace" } | ||
}, | ||
{ | ||
"comment": "Handles all WebSocket connections to the server.", | ||
"@id": "urn:solid-on-rails:default:WebSocketServerConfigurator", | ||
"@type": "WebSocketServerConfigurator", | ||
"handler": { | ||
"@id": "urn:solid-on-rails:default:WebSocketHandler", | ||
"@type": "WaterfallHandler", | ||
"handlers": [ | ||
{ | ||
"comment": [ | ||
"This handler is required to prevent Components.js issues with arrays.", | ||
"This might be fixed in the next Components.js release after which this can be removed." | ||
], | ||
"@type": "UnsupportedAsyncHandler" | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
{ | ||
"@context": "https://linkedsoftwaredependencies.org/bundles/npm/@comake/solid-on-rails/^0.0.0/components/context.jsonld", | ||
"import": [ | ||
"files-sor:config/server/server-factory/configurator/default.json" | ||
], | ||
"@graph": [ | ||
{ | ||
"comment": "Creates a server that supports HTTP requests.", | ||
"@id": "urn:solid-on-rails:default:ServerFactory", | ||
"@type": "BaseHttpServerFactory", | ||
"handler": { "@id": "urn:solid-on-rails:default:HttpHandler" }, | ||
"options_showStackTrace": { "@id": "urn:solid-on-rails:default:variable:showStackTrace" } | ||
"@type": "BaseServerFactory", | ||
"configurator": { "@id": "urn:solid-on-rails:default:ServerConfigurator" } | ||
} | ||
] | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type { WebSocket } from 'ws'; | ||
import { AsyncHandler } from '../../util/handlers/AsyncHandler'; | ||
import type { HttpRequest } from '../HttpRequest'; | ||
|
||
export interface WebSocketHandlerInput { | ||
webSocket: WebSocket; | ||
upgradeRequest: HttpRequest; | ||
} | ||
|
||
/** | ||
* A handler to support requests trying to open a WebSocket connection. | ||
*/ | ||
export abstract class WebSocketHandler extends AsyncHandler<WebSocketHandlerInput> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import type { Server, IncomingMessage, ServerResponse } from 'http'; | ||
import type { HttpHandler } from '../../http/handler/HttpHandler'; | ||
import { getLoggerFor } from '../../logging/LogUtil'; | ||
import { isError } from '../../util/errors/ErrorUtil'; | ||
import { guardStream } from '../../util/GuardedStream'; | ||
import { ServerConfigurator } from './ServerConfigurator'; | ||
|
||
/** | ||
* A {@link ServerConfigurator} that attaches an {@link HttpHandler} to the `request` event of a {@link Server}. | ||
* All incoming requests will be sent to the provided handler. | ||
* Failsafes are added to make sure a valid response is sent in case something goes wrong. | ||
* | ||
* The `showStackTrace` parameter can be used to add stack traces to error outputs. | ||
*/ | ||
export class HandlerServerConfigurator extends ServerConfigurator { | ||
protected readonly logger = getLoggerFor(this); | ||
protected readonly errorLogger = (error: Error): void => { | ||
this.logger.error(`Request error: ${error.message}`); | ||
}; | ||
|
||
/** The main HttpHandler */ | ||
private readonly handler: HttpHandler; | ||
private readonly showStackTrace: boolean; | ||
|
||
public constructor(handler: HttpHandler, showStackTrace = false) { | ||
super(); | ||
this.handler = handler; | ||
this.showStackTrace = showStackTrace; | ||
} | ||
|
||
public async handle(server: Server): Promise<void> { | ||
server.on('request', | ||
async(request: IncomingMessage, response: ServerResponse): Promise<void> => { | ||
try { | ||
this.logger.info(`Received ${request.method} request for ${request.url}`); | ||
const guardedRequest = guardStream(request); | ||
guardedRequest.on('error', this.errorLogger); | ||
await this.handler.handleSafe({ request: guardedRequest, response }); | ||
} catch (error: unknown) { | ||
const errMsg = this.createErrorMessage(error); | ||
this.logger.error(errMsg); | ||
if (response.headersSent) { | ||
response.end(); | ||
} else { | ||
response.setHeader('Content-Type', 'text/plain; charset=utf-8'); | ||
response.writeHead(500).end(errMsg); | ||
} | ||
} finally { | ||
if (!response.headersSent) { | ||
response.writeHead(404).end(); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Creates a readable error message based on the error and the `showStackTrace` parameter. | ||
*/ | ||
private createErrorMessage(error: unknown): string { | ||
if (!isError(error)) { | ||
return `Unknown error: ${error}.\n`; | ||
} | ||
if (this.showStackTrace && error.stack) { | ||
return `${error.stack}\n`; | ||
} | ||
return `${error.name}: ${error.message}\n`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import type { Server } from 'http'; | ||
import { AsyncHandler } from '../../util/handlers/AsyncHandler'; | ||
|
||
/** | ||
* Configures a {@link Server} by attaching listeners for specific events. | ||
*/ | ||
export abstract class ServerConfigurator extends AsyncHandler<Server> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import type { IncomingMessage, Server } from 'http'; | ||
import type { Socket } from 'net'; | ||
import type { WebSocket } from 'ws'; | ||
import { WebSocketServer } from 'ws'; | ||
import type { WebSocketHandler } from '../../http/handler/WebSocketHandler'; | ||
import { getLoggerFor } from '../../logging/LogUtil'; | ||
import { createErrorMessage } from '../../util/errors/ErrorUtil'; | ||
import { guardStream } from '../../util/GuardedStream'; | ||
import { ServerConfigurator } from './ServerConfigurator'; | ||
|
||
/** | ||
* {@link ServerConfigurator} that adds WebSocket functionality to an existing {@link Server}. | ||
* | ||
* Listens for WebSocket requests and sends them to its handler. | ||
*/ | ||
export class WebSocketServerConfigurator extends ServerConfigurator { | ||
protected readonly logger = getLoggerFor(this); | ||
|
||
private readonly handler: WebSocketHandler; | ||
|
||
public constructor(handler: WebSocketHandler) { | ||
super(); | ||
this.handler = handler; | ||
} | ||
|
||
public async handle(server: Server): Promise<void> { | ||
const webSocketServer = new WebSocketServer({ noServer: true }); | ||
server.on('upgrade', (upgradeRequest: IncomingMessage, socket: Socket, head: Buffer): void => { | ||
webSocketServer.handleUpgrade(upgradeRequest, socket, head, async(webSocket: WebSocket): Promise<void> => { | ||
try { | ||
await this.handler.handleSafe({ upgradeRequest: guardStream(upgradeRequest), webSocket }); | ||
} catch (error: unknown) { | ||
this.logger.error(`Something went wrong handling a WebSocket connection: ${createErrorMessage(error)}`); | ||
webSocket.send(`There was an error opening this WebSocket: ${createErrorMessage(error)}`); | ||
webSocket.close(); | ||
} | ||
}); | ||
}); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.