-
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.
feat(architectura): Added automatic preflight management. (#303)
* feat(architectura): Added automatic preflight management. * feat: rework InitializeOnce and make Server methods non-static * chore(architecutra): Added unit tests for prelight automatic handling. * chore: improve coverage * fix: unused parameter in optional 'abstract' methods was causing errors * chore: update semver --------- Co-authored-by: Zamralik <[email protected]>
- Loading branch information
1 parent
69e3728
commit 5c65551
Showing
21 changed files
with
1,036 additions
and
212 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
1 change: 1 addition & 0 deletions
1
packages/architectura/mock/core/server/definition/interface/_index.mts
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,3 +1,4 @@ | ||
export * from "./mock-request.interface.mjs"; | ||
export * from "./mock-response.interface.mjs"; | ||
export * from "./mock-socket.interface.mjs"; | ||
export * from "./mock-server.interface.mjs"; |
15 changes: 15 additions & 0 deletions
15
packages/architectura/mock/core/server/definition/interface/mock-server.interface.mts
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,15 @@ | ||
import type { SinonStub } from "sinon"; | ||
import type { BaseMockInterface } from "../../../../_index.mjs"; | ||
import type { Server } from "../../../../../src/_index.mjs"; | ||
|
||
interface MockServerInterface extends BaseMockInterface<Server> | ||
{ | ||
stubs: BaseMockInterface<Server>["stubs"] & { | ||
handlePublicAssets: SinonStub; | ||
handleAutomaticPreflight: SinonStub; | ||
handleEndpoints: SinonStub; | ||
finalizeResponse: SinonStub; | ||
}; | ||
} | ||
|
||
export type { MockServerInterface }; |
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,38 @@ | ||
import { stub } from "sinon"; | ||
import { Server, type ServerInstantiationType } from "../../../src/_index.mjs"; | ||
import { type MockServerInterface, baseMock } from "../../_index.mjs"; | ||
|
||
function mockServer(parameters?: ServerInstantiationType): MockServerInterface | ||
{ | ||
const instantiation: ServerInstantiationType = parameters ?? { | ||
https: false, | ||
port: 80, | ||
}; | ||
|
||
// @ts-expect-error: We are mocking a server instance. | ||
const instance: Server = new Server(instantiation); | ||
|
||
const MOCK: MockServerInterface = baseMock({ | ||
instance: instance, | ||
stubs: { | ||
start: stub(instance, "start"), | ||
isHTTPS: stub(instance, "isHTTPS"), | ||
handleError: stub(instance, "handleError"), | ||
requestListener: stub(instance, "requestListener"), | ||
// @ts-expect-error: Private method | ||
handlePublicAssets: stub(instance, "handlePublicAssets"), | ||
// @ts-expect-error: Private method | ||
handleAutomaticPreflight: stub(instance, "handleAutomaticPreflight"), | ||
// @ts-expect-error: Private method | ||
handleEndpoints: stub(instance, "handleEndpoints"), | ||
// @ts-expect-error: Private method | ||
finalizeResponse: stub(instance, "finalizeResponse"), | ||
}, | ||
}); | ||
|
||
MOCK.callThroughAllStubs(); | ||
|
||
return MOCK; | ||
} | ||
|
||
export { mockServer }; |
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
97 changes: 97 additions & 0 deletions
97
packages/architectura/src/core/endpoint/access-control-definition.mts
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,97 @@ | ||
import type { AccessControlDefinitionInstantiationInterface } from "./definition/interface/access-control-definition-instantiation.interface.mjs"; | ||
|
||
class AccessControlDefinition | ||
{ | ||
/** | ||
* Which headers are allowed for this endpoint. | ||
* | ||
* @remarks | ||
* By default, no headers are allowed. | ||
* If you want to allow all headers, set this property to "*". | ||
* Otherwise, list the headers you want to allow. | ||
* This property is used to set the Access-Control-Allow-Headers during automatic preflight. | ||
*/ | ||
protected readonly allowedHeaders: Array<string> | "*" = []; | ||
|
||
/** | ||
* Which origins are allowed for this endpoint. | ||
* | ||
* @remarks | ||
* By default, no origins are allowed. | ||
* If you want to allow all origins, set this property to "*". | ||
* Otherwise, list the origins you want to allow. | ||
* This property is used to set the Access-Control-Allow-Origin during automatic preflight. | ||
*/ | ||
protected readonly allowedOrigins: Array<string> | "*" = []; | ||
|
||
/** | ||
* How long the preflight response can be cached in seconds. | ||
* Defaults to 0. | ||
* | ||
* @remarks | ||
* This property is used to set the Access-Control-Max-Age during automatic preflight. | ||
* If you want to disable caching, set this property to 0. | ||
* Otherwise, set it to the number of seconds you want to cache the preflight response. | ||
* | ||
* Important: If you do not set this property, or set it to 0, the preflight response will not be cached. | ||
* This can lead to a performance penalty, as the preflight request will be sent for every request. | ||
*/ | ||
protected readonly maxAge: number = 0; | ||
|
||
public constructor(parameters: AccessControlDefinitionInstantiationInterface) | ||
{ | ||
this.allowedHeaders = parameters.allowedHeaders; | ||
this.allowedOrigins = parameters.allowedOrigins; | ||
this.maxAge = parameters.maxAge; | ||
} | ||
|
||
/** | ||
* Get the allowed headers. | ||
* | ||
* @sealed | ||
*/ | ||
public getAllowedHeaders(): Array<string> | "*" | ||
{ | ||
return this.allowedHeaders; | ||
} | ||
|
||
/** | ||
* Get the allowed origins. | ||
* | ||
* @sealed | ||
*/ | ||
public getAllowedOrigins(): Array<string> | "*" | ||
{ | ||
return this.allowedOrigins; | ||
} | ||
|
||
/** | ||
* Get the max age. | ||
* | ||
* @sealed | ||
*/ | ||
public getMaxAge(): number | ||
{ | ||
return this.maxAge; | ||
} | ||
|
||
/** | ||
* Generate the preflight headers | ||
* | ||
* @sealed | ||
* @returns The preflight headers. | ||
*/ | ||
public generatePreflightHeaders(): Headers | ||
{ | ||
const headers: Headers = new Headers(); | ||
|
||
headers.set("Access-Control-Allow-Headers", this.allowedHeaders === "*" ? "*" : this.allowedHeaders.join(", ")); | ||
headers.set("Access-Control-Allow-Origin", this.allowedOrigins === "*" ? "*" : this.allowedOrigins.join(", ")); | ||
headers.set("Access-Control-Max-Age", this.maxAge.toString()); | ||
headers.set("Content-Length", "0"); | ||
|
||
return headers; | ||
} | ||
} | ||
|
||
export { AccessControlDefinition }; |
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
8 changes: 8 additions & 0 deletions
8
.../core/endpoint/definition/interface/access-control-definition-instantiation.interface.mts
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,8 @@ | ||
interface AccessControlDefinitionInstantiationInterface | ||
{ | ||
allowedHeaders: Array<string> | "*"; | ||
allowedOrigins: Array<string> | "*"; | ||
maxAge: number; | ||
} | ||
|
||
export type { AccessControlDefinitionInstantiationInterface }; |
3 changes: 3 additions & 0 deletions
3
...architectura/src/core/server/definition/interface/base-server-configuration.interface.mts
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,9 +1,12 @@ | ||
import type { AccessControlDefinition } from "../../../endpoint/access-control-definition.mjs"; | ||
|
||
/** | ||
* Shared properties of server configuration | ||
*/ | ||
interface BaseServerConfigurationInterface | ||
{ | ||
port: number; | ||
defaultAccessControlDefinition?: AccessControlDefinition | undefined; | ||
} | ||
|
||
export type { BaseServerConfigurationInterface }; |
3 changes: 3 additions & 0 deletions
3
...architectura/src/core/server/definition/interface/base-server-instantiation.interface.mts
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,7 +1,10 @@ | ||
import type { AccessControlDefinition } from "../../../endpoint/access-control-definition.mjs"; | ||
|
||
/** @internal */ | ||
interface BaseServerInstantiationInterface | ||
{ | ||
port: number; | ||
defaultAccessControlDefinition?: AccessControlDefinition | undefined; | ||
} | ||
|
||
export type { BaseServerInstantiationInterface }; |
Oops, something went wrong.