oauth2-mock-server
Advanced tools
Comparing version
@@ -7,2 +7,12 @@ # Changelog | ||
## [8.1.0](https://github.com/axa-group/oauth2-mock-server/compare/v8.0.1...v8.1.0) — 2025-06-06 | ||
### Added | ||
- Export `HttpServer` and `OAuth2Service` (reported in [#344](https://github.com/axa-group/oauth2-mock-server/issues/344) by [jraoult](https://github.com/jraoult)) | ||
### Changed | ||
- Update dependencies | ||
## [8.0.1](https://github.com/axa-group/oauth2-mock-server/compare/v8.0.0...v8.0.1) — 2025-05-28 | ||
@@ -9,0 +19,0 @@ |
@@ -1,2 +0,2 @@ | ||
export { E as Events, J as JWKStore, O as OAuth2Issuer, a as OAuth2Server } from './shared/oauth2-server.js'; | ||
export { E as Events, H as HttpServer, J as JWKStore, O as OAuth2Issuer, a as OAuth2Server, b as OAuth2Service } from './shared/oauth2-server.js'; | ||
import 'node:fs'; | ||
@@ -3,0 +3,0 @@ import 'node:http'; |
@@ -5,3 +5,6 @@ #!/usr/bin/env node | ||
import path from 'node:path'; | ||
import { s as shift, r as readJsonFromFile, a as OAuth2Server, b as assertIsString } from './shared/oauth2-server.js'; | ||
import { s as shift, r as readJsonFromFile, a as OAuth2Server, c as assertIsString } from './shared/oauth2-server.js'; | ||
import 'node:http'; | ||
import 'node:https'; | ||
import 'node:net'; | ||
import 'node:crypto'; | ||
@@ -11,10 +14,7 @@ import 'node:assert'; | ||
import 'node:events'; | ||
import 'node:fs'; | ||
import 'node:http'; | ||
import 'node:https'; | ||
import 'node:net'; | ||
import 'is-plain-obj'; | ||
import 'express'; | ||
import 'cors'; | ||
import 'basic-auth'; | ||
import 'node:fs'; | ||
import 'is-plain-obj'; | ||
@@ -21,0 +21,0 @@ const __filename = fileURLToPath(import.meta.url); |
@@ -144,2 +144,70 @@ import { readFileSync } from 'node:fs'; | ||
class HttpServer { | ||
#server; | ||
#isSecured; | ||
constructor(requestListener, options) { | ||
this.#isSecured = false; | ||
if (options?.key && options.cert) { | ||
this.#server = createServer(options, requestListener); | ||
this.#isSecured = true; | ||
} | ||
else { | ||
this.#server = createServer$1(requestListener); | ||
} | ||
} | ||
get listening() { | ||
return this.#server.listening; | ||
} | ||
address() { | ||
if (!this.listening) { | ||
throw new Error('Server is not started.'); | ||
} | ||
const address = this.#server.address(); | ||
assertIsAddressInfo(address); | ||
return address; | ||
} | ||
async start(port, host) { | ||
if (this.listening) { | ||
throw new Error('Server has already been started.'); | ||
} | ||
return new Promise((resolve, reject) => { | ||
this.#server | ||
.listen(port, host) | ||
.on('listening', resolve) | ||
.on('error', reject); | ||
}); | ||
} | ||
async stop() { | ||
if (!this.listening) { | ||
throw new Error('Server is not started.'); | ||
} | ||
return new Promise((resolve, reject) => { | ||
this.#server.close((err) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
buildIssuerUrl(host, port) { | ||
const url = new URL(`${this.#isSecured ? 'https' : 'http'}://localhost:${port.toString()}`); | ||
if (host && !coversLocalhost(host)) { | ||
url.hostname = host.includes(':') ? `[${host}]` : host; | ||
} | ||
return url.origin; | ||
} | ||
} | ||
const coversLocalhost = (address) => { | ||
switch (isIP(address)) { | ||
case 4: | ||
return address === '0.0.0.0' || address.startsWith('127.'); | ||
case 6: | ||
return address === '::' || address === '::1'; | ||
default: | ||
return false; | ||
} | ||
}; | ||
const generateRandomKid = () => { | ||
@@ -310,70 +378,2 @@ return randomBytes(40).toString('hex'); | ||
class HttpServer { | ||
#server; | ||
#isSecured; | ||
constructor(requestListener, options) { | ||
this.#isSecured = false; | ||
if (options?.key && options.cert) { | ||
this.#server = createServer(options, requestListener); | ||
this.#isSecured = true; | ||
} | ||
else { | ||
this.#server = createServer$1(requestListener); | ||
} | ||
} | ||
get listening() { | ||
return this.#server.listening; | ||
} | ||
address() { | ||
if (!this.listening) { | ||
throw new Error('Server is not started.'); | ||
} | ||
const address = this.#server.address(); | ||
assertIsAddressInfo(address); | ||
return address; | ||
} | ||
async start(port, host) { | ||
if (this.listening) { | ||
throw new Error('Server has already been started.'); | ||
} | ||
return new Promise((resolve, reject) => { | ||
this.#server | ||
.listen(port, host) | ||
.on('listening', resolve) | ||
.on('error', reject); | ||
}); | ||
} | ||
async stop() { | ||
if (!this.listening) { | ||
throw new Error('Server is not started.'); | ||
} | ||
return new Promise((resolve, reject) => { | ||
this.#server.close((err) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
buildIssuerUrl(host, port) { | ||
const url = new URL(`${this.#isSecured ? 'https' : 'http'}://localhost:${port.toString()}`); | ||
if (host && !coversLocalhost(host)) { | ||
url.hostname = host.includes(':') ? `[${host}]` : host; | ||
} | ||
return url.origin; | ||
} | ||
} | ||
const coversLocalhost = (address) => { | ||
switch (isIP(address)) { | ||
case 4: | ||
return address === '0.0.0.0' || address.startsWith('127.'); | ||
case 6: | ||
return address === '::' || address === '::1'; | ||
default: | ||
return false; | ||
} | ||
}; | ||
var Events; | ||
@@ -691,2 +691,2 @@ (function (Events) { | ||
export { Events as E, JWKStore as J, OAuth2Issuer as O, OAuth2Server as a, assertIsString as b, readJsonFromFile as r, shift as s }; | ||
export { Events as E, HttpServer as H, JWKStore as J, OAuth2Issuer as O, OAuth2Server as a, OAuth2Service as b, assertIsString as c, readJsonFromFile as r, shift as s }; |
@@ -0,6 +1,6 @@ | ||
import { RequestListener, IncomingMessage } from 'node:http'; | ||
import { AddressInfo } from 'node:net'; | ||
import { ServerOptions } from 'node:https'; | ||
import { JWK as JWK$1 } from 'jose'; | ||
import { EventEmitter } from 'node:events'; | ||
import { AddressInfo } from 'node:net'; | ||
import { RequestListener, IncomingMessage } from 'node:http'; | ||
@@ -135,4 +135,60 @@ interface JWKWithKid extends JWK$1 { | ||
*/ | ||
/** | ||
* HTTP Server library | ||
* @module lib/http-server | ||
*/ | ||
/** | ||
* Provides a restartable wrapper for http.CreateServer(). | ||
*/ | ||
declare class HttpServer { | ||
#private; | ||
/** | ||
* Creates a new instance of HttpServer. | ||
* @param requestListener The function that will handle the server's requests. | ||
* @param options Optional HttpServerOptions to start the server with https. | ||
*/ | ||
constructor(requestListener: RequestListener, options?: HttpServerOptions); | ||
/** | ||
* Returns a value indicating whether or not the server is listening for connections. | ||
* @returns A boolean value indicating whether the server is listening. | ||
*/ | ||
get listening(): boolean; | ||
/** | ||
* Returns the bound address, family name and port where the server is listening, | ||
* or null if the server has not been started. | ||
* @returns The server bound address information. | ||
*/ | ||
address(): AddressInfo; | ||
/** | ||
* Starts the server. | ||
* @param port Port number. If omitted, it will be assigned by the operating system. | ||
* @param host Host name. | ||
* @returns A promise that resolves when the server has been started. | ||
*/ | ||
start(port?: number, host?: string): Promise<void>; | ||
/** | ||
* Stops the server. | ||
* @returns Resolves when the server has been stopped. | ||
*/ | ||
stop(): Promise<void>; | ||
protected buildIssuerUrl(host: string | undefined, port: number): string; | ||
} | ||
/** | ||
* Copyright (c) AXA Assistance France | ||
* | ||
* Licensed under the AXA Assistance France License (the "License"); you | ||
* may not use this file except in compliance with the License. | ||
* A copy of the License can be found in the LICENSE.md file distributed | ||
* together with this file. | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
/** | ||
* Simple JWK store | ||
@@ -242,58 +298,2 @@ */ | ||
/** | ||
* HTTP Server library | ||
* @module lib/http-server | ||
*/ | ||
/** | ||
* Provides a restartable wrapper for http.CreateServer(). | ||
*/ | ||
declare class HttpServer { | ||
#private; | ||
/** | ||
* Creates a new instance of HttpServer. | ||
* @param requestListener The function that will handle the server's requests. | ||
* @param options Optional HttpServerOptions to start the server with https. | ||
*/ | ||
constructor(requestListener: RequestListener, options?: HttpServerOptions); | ||
/** | ||
* Returns a value indicating whether or not the server is listening for connections. | ||
* @returns A boolean value indicating whether the server is listening. | ||
*/ | ||
get listening(): boolean; | ||
/** | ||
* Returns the bound address, family name and port where the server is listening, | ||
* or null if the server has not been started. | ||
* @returns The server bound address information. | ||
*/ | ||
address(): AddressInfo; | ||
/** | ||
* Starts the server. | ||
* @param port Port number. If omitted, it will be assigned by the operating system. | ||
* @param host Host name. | ||
* @returns A promise that resolves when the server has been started. | ||
*/ | ||
start(port?: number, host?: string): Promise<void>; | ||
/** | ||
* Stops the server. | ||
* @returns Resolves when the server has been stopped. | ||
*/ | ||
stop(): Promise<void>; | ||
protected buildIssuerUrl(host: string | undefined, port: number): string; | ||
} | ||
/** | ||
* Copyright (c) AXA Assistance France | ||
* | ||
* Licensed under the AXA Assistance France License (the "License"); you | ||
* may not use this file except in compliance with the License. | ||
* A copy of the License can be found in the LICENSE.md file distributed | ||
* together with this file. | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
/** | ||
* OAuth2 Service library | ||
@@ -404,3 +404,3 @@ * @module lib/oauth2-service | ||
export { Events, JWKStore, OAuth2Issuer, OAuth2Server }; | ||
export { Events, HttpServer, JWKStore, OAuth2Issuer, OAuth2Server, OAuth2Service }; | ||
export type { CodeChallenge, Header, HttpServerOptions, JWK, JwtTransform, MutableRedirectUri, MutableResponse, MutableToken, OAuth2Endpoints, OAuth2EndpointsInput, OAuth2Options, Options, PKCEAlgorithm, Payload, ScopesOrTransform, StatusCodeMutableResponse, TokenBuildOptions, TokenRequest }; |
{ | ||
"name": "oauth2-mock-server", | ||
"version": "8.0.1", | ||
"version": "8.1.0", | ||
"description": "OAuth 2 mock server", | ||
@@ -67,26 +67,27 @@ "type": "module", | ||
"devDependencies": { | ||
"@eslint/js": "^9.26.0", | ||
"@eslint/js": "^9.28.0", | ||
"@rollup/plugin-typescript": "^12.1.2", | ||
"@types/basic-auth": "^1.1.6", | ||
"@types/cors": "^2.8.17", | ||
"@types/express": "^5.0.1", | ||
"@types/node": "^20.17.46", | ||
"@types/express": "^5.0.2", | ||
"@types/node": "^20.17.57", | ||
"@types/supertest": "^6.0.3", | ||
"@typescript-eslint/eslint-plugin": "^8.32.0", | ||
"@typescript-eslint/parser": "^8.32.0", | ||
"@vitest/coverage-v8": "^3.1.3", | ||
"@vitest/eslint-plugin": "^1.1.44", | ||
"eslint": "^9.26.0", | ||
"@typescript-eslint/eslint-plugin": "^8.33.1", | ||
"@typescript-eslint/parser": "^8.33.1", | ||
"@vitest/coverage-v8": "^3.2.1", | ||
"@vitest/eslint-plugin": "^1.2.1", | ||
"eslint": "^9.28.0", | ||
"eslint-config-prettier": "^10.1.3", | ||
"eslint-import-resolver-typescript": "^4.3.4", | ||
"eslint-plugin-import-x": "^4.11.0", | ||
"eslint-plugin-jsdoc": "^50.6.11", | ||
"eslint-plugin-prettier": "^5.4.0", | ||
"eslint-import-resolver-typescript": "^4.4.2", | ||
"eslint-plugin-import-x": "^4.15.0", | ||
"eslint-plugin-jsdoc": "^50.7.1", | ||
"eslint-plugin-prettier": "^5.4.1", | ||
"prettier": "^3.5.3", | ||
"rollup": "^4.9.0", | ||
"rollup": "^4.41.1", | ||
"rollup-plugin-dts": "^6.1.0", | ||
"supertest": "^7.1.0", | ||
"supertest": "^7.1.1", | ||
"tslib": "^2.8.1", | ||
"typescript": "^5.8.3", | ||
"typescript-eslint": "^8.32.0", | ||
"vitest": "^3.1.3" | ||
"typescript-eslint": "^8.33.1", | ||
"vitest": "^3.2.1" | ||
}, | ||
@@ -93,0 +94,0 @@ "overrides": { |
68900
0.65%25
4.17%