@furystack/rest-service 13.0.0 → 14.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +124 -0
- package/README.md +47 -28
- package/esm/actions/error-action.d.ts +7 -2
- package/esm/actions/error-action.d.ts.map +1 -1
- package/esm/actions/error-action.js +7 -2
- package/esm/actions/error-action.js.map +1 -1
- package/esm/actions/get-current-user.d.ts +1 -4
- package/esm/actions/get-current-user.d.ts.map +1 -1
- package/esm/actions/get-current-user.js +1 -4
- package/esm/actions/get-current-user.js.map +1 -1
- package/esm/actions/get-current-user.spec.js +10 -6
- package/esm/actions/get-current-user.spec.js.map +1 -1
- package/esm/actions/is-authenticated.d.ts +3 -4
- package/esm/actions/is-authenticated.d.ts.map +1 -1
- package/esm/actions/is-authenticated.js +3 -4
- package/esm/actions/is-authenticated.js.map +1 -1
- package/esm/actions/is-authenticated.spec.js +10 -6
- package/esm/actions/is-authenticated.spec.js.map +1 -1
- package/esm/actions/logout-action.spec.js +11 -9
- package/esm/actions/logout-action.spec.js.map +1 -1
- package/esm/actions/logout.d.ts +2 -6
- package/esm/actions/logout.d.ts.map +1 -1
- package/esm/actions/logout.js +3 -7
- package/esm/actions/logout.js.map +1 -1
- package/esm/actions/not-found-action.d.ts +1 -3
- package/esm/actions/not-found-action.d.ts.map +1 -1
- package/esm/actions/not-found-action.js +1 -3
- package/esm/actions/not-found-action.js.map +1 -1
- package/esm/actions/password-login-action.d.ts +5 -12
- package/esm/actions/password-login-action.d.ts.map +1 -1
- package/esm/actions/password-login-action.js +6 -13
- package/esm/actions/password-login-action.js.map +1 -1
- package/esm/actions/password-login-action.spec.js +23 -28
- package/esm/actions/password-login-action.spec.js.map +1 -1
- package/esm/authenticate.d.ts +5 -11
- package/esm/authenticate.d.ts.map +1 -1
- package/esm/authenticate.js +6 -12
- package/esm/authenticate.js.map +1 -1
- package/esm/authenticate.spec.js +41 -33
- package/esm/authenticate.spec.js.map +1 -1
- package/esm/authentication-providers/authentication-provider.d.ts +17 -0
- package/esm/authentication-providers/authentication-provider.d.ts.map +1 -1
- package/esm/authentication-providers/basic-auth-provider.d.ts +7 -0
- package/esm/authentication-providers/basic-auth-provider.d.ts.map +1 -1
- package/esm/authentication-providers/basic-auth-provider.js +7 -0
- package/esm/authentication-providers/basic-auth-provider.js.map +1 -1
- package/esm/authentication-providers/cookie-auth-provider.d.ts.map +1 -1
- package/esm/authentication-providers/cookie-auth-provider.js +8 -0
- package/esm/authentication-providers/cookie-auth-provider.js.map +1 -1
- package/esm/authorize.d.ts +5 -17
- package/esm/authorize.d.ts.map +1 -1
- package/esm/authorize.js +5 -17
- package/esm/authorize.js.map +1 -1
- package/esm/authorize.spec.js +34 -29
- package/esm/authorize.spec.js.map +1 -1
- package/esm/endpoint-generators/create-delete-endpoint.d.ts +6 -11
- package/esm/endpoint-generators/create-delete-endpoint.d.ts.map +1 -1
- package/esm/endpoint-generators/create-delete-endpoint.js +7 -11
- package/esm/endpoint-generators/create-delete-endpoint.js.map +1 -1
- package/esm/endpoint-generators/create-delete-endpoint.spec.js +14 -21
- package/esm/endpoint-generators/create-delete-endpoint.spec.js.map +1 -1
- package/esm/endpoint-generators/create-get-collection-endpoint.d.ts +11 -11
- package/esm/endpoint-generators/create-get-collection-endpoint.d.ts.map +1 -1
- package/esm/endpoint-generators/create-get-collection-endpoint.js +13 -12
- package/esm/endpoint-generators/create-get-collection-endpoint.js.map +1 -1
- package/esm/endpoint-generators/create-get-collection-endpoint.spec.js +43 -70
- package/esm/endpoint-generators/create-get-collection-endpoint.spec.js.map +1 -1
- package/esm/endpoint-generators/create-get-entity-endpoint.d.ts +6 -11
- package/esm/endpoint-generators/create-get-entity-endpoint.d.ts.map +1 -1
- package/esm/endpoint-generators/create-get-entity-endpoint.js +7 -11
- package/esm/endpoint-generators/create-get-entity-endpoint.js.map +1 -1
- package/esm/endpoint-generators/create-get-entity-endpoint.spec.js +16 -33
- package/esm/endpoint-generators/create-get-entity-endpoint.spec.js.map +1 -1
- package/esm/endpoint-generators/create-get-openapi-document-action.d.ts +1 -1
- package/esm/endpoint-generators/create-get-openapi-document-action.d.ts.map +1 -1
- package/esm/endpoint-generators/create-get-openapi-document-action.js +1 -1
- package/esm/endpoint-generators/create-get-openapi-document-action.js.map +1 -1
- package/esm/endpoint-generators/create-get-schema-action.d.ts +1 -1
- package/esm/endpoint-generators/create-get-schema-action.d.ts.map +1 -1
- package/esm/endpoint-generators/create-get-schema-action.js.map +1 -1
- package/esm/endpoint-generators/create-patch-endpoint.d.ts +7 -13
- package/esm/endpoint-generators/create-patch-endpoint.d.ts.map +1 -1
- package/esm/endpoint-generators/create-patch-endpoint.js +8 -13
- package/esm/endpoint-generators/create-patch-endpoint.js.map +1 -1
- package/esm/endpoint-generators/create-patch-endpoint.spec.js +10 -17
- package/esm/endpoint-generators/create-patch-endpoint.spec.js.map +1 -1
- package/esm/endpoint-generators/create-post-endpoint.d.ts +7 -13
- package/esm/endpoint-generators/create-post-endpoint.d.ts.map +1 -1
- package/esm/endpoint-generators/create-post-endpoint.js +8 -13
- package/esm/endpoint-generators/create-post-endpoint.js.map +1 -1
- package/esm/endpoint-generators/create-post-endpoint.spec.js +41 -14
- package/esm/endpoint-generators/create-post-endpoint.spec.js.map +1 -1
- package/esm/endpoint-generators/utils.d.ts +17 -2
- package/esm/endpoint-generators/utils.d.ts.map +1 -1
- package/esm/endpoint-generators/utils.js +33 -20
- package/esm/endpoint-generators/utils.js.map +1 -1
- package/esm/get-schema-from-api.d.ts +1 -1
- package/esm/get-schema-from-api.d.ts.map +1 -1
- package/esm/get-schema-from-api.js +1 -0
- package/esm/get-schema-from-api.js.map +1 -1
- package/esm/get-schema-from-api.spec.js.map +1 -1
- package/esm/helpers.d.ts +54 -88
- package/esm/helpers.d.ts.map +1 -1
- package/esm/helpers.js +90 -92
- package/esm/helpers.js.map +1 -1
- package/esm/helpers.spec.d.ts +2 -1
- package/esm/helpers.spec.d.ts.map +1 -1
- package/esm/helpers.spec.js +41 -38
- package/esm/helpers.spec.js.map +1 -1
- package/esm/http-authentication-settings.d.ts +56 -12
- package/esm/http-authentication-settings.d.ts.map +1 -1
- package/esm/http-authentication-settings.js +27 -29
- package/esm/http-authentication-settings.js.map +1 -1
- package/esm/http-server-pool.d.ts +64 -0
- package/esm/http-server-pool.d.ts.map +1 -0
- package/esm/http-server-pool.js +95 -0
- package/esm/http-server-pool.js.map +1 -0
- package/esm/http-server-pool.spec.d.ts +2 -0
- package/esm/http-server-pool.spec.d.ts.map +1 -0
- package/esm/http-server-pool.spec.js +193 -0
- package/esm/http-server-pool.spec.js.map +1 -0
- package/esm/http-user-context.d.ts +34 -44
- package/esm/http-user-context.d.ts.map +1 -1
- package/esm/http-user-context.js +75 -95
- package/esm/http-user-context.js.map +1 -1
- package/esm/http-user-context.spec.d.ts +8 -2
- package/esm/http-user-context.spec.d.ts.map +1 -1
- package/esm/http-user-context.spec.js +226 -332
- package/esm/http-user-context.spec.js.map +1 -1
- package/esm/identity-cache-keys.d.ts +28 -0
- package/esm/identity-cache-keys.d.ts.map +1 -0
- package/esm/identity-cache-keys.js +13 -0
- package/esm/identity-cache-keys.js.map +1 -0
- package/esm/identity-cache-keys.spec.d.ts +2 -0
- package/esm/identity-cache-keys.spec.d.ts.map +1 -0
- package/esm/identity-cache-keys.spec.js +21 -0
- package/esm/identity-cache-keys.spec.js.map +1 -0
- package/esm/identity-event-bus.d.ts +102 -0
- package/esm/identity-event-bus.d.ts.map +1 -0
- package/esm/identity-event-bus.js +151 -0
- package/esm/identity-event-bus.js.map +1 -0
- package/esm/identity-event-bus.spec.d.ts +2 -0
- package/esm/identity-event-bus.spec.d.ts.map +1 -0
- package/esm/identity-event-bus.spec.js +392 -0
- package/esm/identity-event-bus.spec.js.map +1 -0
- package/esm/index.d.ts +9 -4
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +9 -4
- package/esm/index.js.map +1 -1
- package/esm/login-response-strategy.d.ts +11 -10
- package/esm/login-response-strategy.d.ts.map +1 -1
- package/esm/login-response-strategy.js +7 -6
- package/esm/login-response-strategy.js.map +1 -1
- package/esm/login-response-strategy.spec.js +25 -29
- package/esm/login-response-strategy.spec.js.map +1 -1
- package/esm/openapi/generate-openapi-document.spec.js.map +1 -1
- package/esm/proxy-manager.spec.js +152 -176
- package/esm/proxy-manager.spec.js.map +1 -1
- package/esm/proxy-runtime.d.ts +27 -0
- package/esm/proxy-runtime.d.ts.map +1 -0
- package/esm/proxy-runtime.js +45 -0
- package/esm/proxy-runtime.js.map +1 -0
- package/esm/proxy-runtime.spec.d.ts +2 -0
- package/esm/proxy-runtime.spec.d.ts.map +1 -0
- package/esm/proxy-runtime.spec.js +39 -0
- package/esm/proxy-runtime.spec.js.map +1 -0
- package/esm/request-action-implementation.d.ts +25 -0
- package/esm/request-action-implementation.d.ts.map +1 -1
- package/esm/request-action-implementation.js +9 -0
- package/esm/request-action-implementation.js.map +1 -1
- package/esm/rest-api-runtime.d.ts +76 -0
- package/esm/rest-api-runtime.d.ts.map +1 -0
- package/esm/rest-api-runtime.js +131 -0
- package/esm/rest-api-runtime.js.map +1 -0
- package/esm/rest-api-runtime.spec.d.ts +2 -0
- package/esm/rest-api-runtime.spec.d.ts.map +1 -0
- package/esm/rest-api-runtime.spec.js +58 -0
- package/esm/rest-api-runtime.spec.js.map +1 -0
- package/esm/rest-service.integration.spec.d.ts +1 -1
- package/esm/rest-service.integration.spec.d.ts.map +1 -1
- package/esm/rest-service.integration.spec.js +57 -80
- package/esm/rest-service.integration.spec.js.map +1 -1
- package/esm/rest.integration.test.js +26 -84
- package/esm/rest.integration.test.js.map +1 -1
- package/esm/server-telemetry.d.ts +49 -0
- package/esm/server-telemetry.d.ts.map +1 -0
- package/esm/server-telemetry.js +25 -0
- package/esm/server-telemetry.js.map +1 -0
- package/esm/server-telemetry.spec.d.ts +2 -0
- package/esm/server-telemetry.spec.d.ts.map +1 -0
- package/esm/server-telemetry.spec.js +52 -0
- package/esm/server-telemetry.spec.js.map +1 -0
- package/esm/static-server-manager.spec.js +60 -125
- package/esm/static-server-manager.spec.js.map +1 -1
- package/esm/static-site-runtime.d.ts +21 -0
- package/esm/static-site-runtime.d.ts.map +1 -0
- package/esm/static-site-runtime.js +48 -0
- package/esm/static-site-runtime.js.map +1 -0
- package/esm/static-site-runtime.spec.d.ts +2 -0
- package/esm/static-site-runtime.spec.d.ts.map +1 -0
- package/esm/static-site-runtime.spec.js +30 -0
- package/esm/static-site-runtime.spec.js.map +1 -0
- package/esm/user-resolution-cache.d.ts +65 -0
- package/esm/user-resolution-cache.d.ts.map +1 -0
- package/esm/user-resolution-cache.js +101 -0
- package/esm/user-resolution-cache.js.map +1 -0
- package/esm/user-resolution-cache.spec.d.ts +2 -0
- package/esm/user-resolution-cache.spec.d.ts.map +1 -0
- package/esm/user-resolution-cache.spec.js +220 -0
- package/esm/user-resolution-cache.spec.js.map +1 -0
- package/esm/user-store.d.ts +39 -0
- package/esm/user-store.d.ts.map +1 -0
- package/esm/user-store.js +62 -0
- package/esm/user-store.js.map +1 -0
- package/esm/user-store.spec.d.ts +2 -0
- package/esm/user-store.spec.d.ts.map +1 -0
- package/esm/user-store.spec.js +72 -0
- package/esm/user-store.spec.js.map +1 -0
- package/esm/validate.d.ts +13 -12
- package/esm/validate.d.ts.map +1 -1
- package/esm/validate.integration.spec.js +27 -28
- package/esm/validate.integration.spec.js.map +1 -1
- package/esm/validate.js +12 -0
- package/esm/validate.js.map +1 -1
- package/package.json +14 -12
- package/src/actions/error-action.ts +7 -3
- package/src/actions/get-current-user.spec.ts +11 -10
- package/src/actions/get-current-user.ts +1 -4
- package/src/actions/is-authenticated.spec.ts +12 -7
- package/src/actions/is-authenticated.ts +3 -4
- package/src/actions/logout-action.spec.ts +12 -12
- package/src/actions/logout.ts +3 -7
- package/src/actions/not-found-action.ts +1 -3
- package/src/actions/password-login-action.spec.ts +30 -30
- package/src/actions/password-login-action.ts +6 -13
- package/src/authenticate.spec.ts +45 -51
- package/src/authenticate.ts +6 -12
- package/src/authentication-providers/authentication-provider.ts +17 -0
- package/src/authentication-providers/basic-auth-provider.ts +7 -0
- package/src/authentication-providers/cookie-auth-provider.ts +8 -0
- package/src/authorize.spec.ts +37 -39
- package/src/authorize.ts +6 -17
- package/src/endpoint-generators/create-delete-endpoint.spec.ts +16 -24
- package/src/endpoint-generators/create-delete-endpoint.ts +11 -17
- package/src/endpoint-generators/create-get-collection-endpoint.spec.ts +45 -78
- package/src/endpoint-generators/create-get-collection-endpoint.ts +17 -19
- package/src/endpoint-generators/create-get-entity-endpoint.spec.ts +17 -33
- package/src/endpoint-generators/create-get-entity-endpoint.ts +11 -17
- package/src/endpoint-generators/create-get-openapi-document-action.ts +2 -2
- package/src/endpoint-generators/create-get-schema-action.ts +1 -1
- package/src/endpoint-generators/create-patch-endpoint.spec.ts +11 -18
- package/src/endpoint-generators/create-patch-endpoint.ts +12 -23
- package/src/endpoint-generators/create-post-endpoint.spec.ts +45 -14
- package/src/endpoint-generators/create-post-endpoint.ts +13 -23
- package/src/endpoint-generators/utils.ts +36 -28
- package/src/get-schema-from-api.spec.ts +1 -1
- package/src/get-schema-from-api.ts +2 -1
- package/src/helpers.spec.ts +51 -41
- package/src/helpers.ts +140 -109
- package/src/http-authentication-settings.ts +78 -24
- package/src/http-server-pool.spec.ts +220 -0
- package/src/http-server-pool.ts +169 -0
- package/src/http-user-context.spec.ts +246 -378
- package/src/http-user-context.ts +122 -92
- package/src/identity-cache-keys.spec.ts +24 -0
- package/src/identity-cache-keys.ts +31 -0
- package/src/identity-event-bus.spec.ts +285 -0
- package/src/identity-event-bus.ts +230 -0
- package/src/index.ts +9 -4
- package/src/login-response-strategy.spec.ts +32 -31
- package/src/login-response-strategy.ts +14 -12
- package/src/openapi/generate-openapi-document.spec.ts +2 -2
- package/src/proxy-manager.spec.ts +158 -188
- package/src/proxy-runtime.spec.ts +50 -0
- package/src/proxy-runtime.ts +75 -0
- package/src/request-action-implementation.ts +25 -0
- package/src/rest-api-runtime.spec.ts +77 -0
- package/src/rest-api-runtime.ts +204 -0
- package/src/rest-service.integration.spec.ts +65 -85
- package/src/rest.integration.test.ts +27 -87
- package/src/server-telemetry.spec.ts +56 -0
- package/src/server-telemetry.ts +43 -0
- package/src/static-server-manager.spec.ts +63 -151
- package/src/static-site-runtime.spec.ts +39 -0
- package/src/static-site-runtime.ts +75 -0
- package/src/user-resolution-cache.spec.ts +241 -0
- package/src/user-resolution-cache.ts +164 -0
- package/src/user-store.spec.ts +86 -0
- package/src/user-store.ts +69 -0
- package/src/validate.integration.spec.ts +40 -35
- package/src/validate.ts +14 -12
- package/esm/api-manager.d.ts +0 -104
- package/esm/api-manager.d.ts.map +0 -1
- package/esm/api-manager.js +0 -179
- package/esm/api-manager.js.map +0 -1
- package/esm/proxy-manager.d.ts +0 -53
- package/esm/proxy-manager.d.ts.map +0 -1
- package/esm/proxy-manager.js +0 -84
- package/esm/proxy-manager.js.map +0 -1
- package/esm/server-manager.d.ts +0 -48
- package/esm/server-manager.d.ts.map +0 -1
- package/esm/server-manager.js +0 -100
- package/esm/server-manager.js.map +0 -1
- package/esm/static-server-manager.d.ts +0 -19
- package/esm/static-server-manager.d.ts.map +0 -1
- package/esm/static-server-manager.js +0 -65
- package/esm/static-server-manager.js.map +0 -1
- package/src/api-manager.ts +0 -304
- package/src/proxy-manager.ts +0 -102
- package/src/server-manager.ts +0 -135
- package/src/static-server-manager.ts +0 -90
package/esm/helpers.spec.js
CHANGED
|
@@ -1,52 +1,46 @@
|
|
|
1
|
-
import { InMemoryStore, User
|
|
1
|
+
import { InMemoryStore, User as UserModel } from '@furystack/core';
|
|
2
2
|
import { getPort } from '@furystack/core/port-generator';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { PasswordCredential, PasswordResetToken, usePasswordPolicy } from '@furystack/security';
|
|
3
|
+
import { createInjector } from '@furystack/inject';
|
|
4
|
+
import { PasswordCredential, PasswordCredentialStore, PasswordResetToken, PasswordResetTokenStore, usePasswordPolicy, } from '@furystack/security';
|
|
6
5
|
import { usingAsync } from '@furystack/utils';
|
|
7
6
|
import { describe, expect, it } from 'vitest';
|
|
8
|
-
import { ApiManager } from './api-manager.js';
|
|
9
7
|
import { useHttpAuthentication, useProxy, useRestService, useStaticFiles } from './helpers.js';
|
|
10
8
|
import { HttpAuthenticationSettings } from './http-authentication-settings.js';
|
|
9
|
+
import { HttpServerPoolToken } from './http-server-pool.js';
|
|
11
10
|
import { DefaultSession } from './models/default-session.js';
|
|
12
|
-
import {
|
|
13
|
-
import { StaticServerManager } from './static-server-manager.js';
|
|
11
|
+
import { SessionStore, UserStore } from './user-store.js';
|
|
14
12
|
const setupAuthStores = (i) => {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const repo = getRepository(i);
|
|
20
|
-
repo.createDataSet(User, 'username');
|
|
21
|
-
repo.createDataSet(DefaultSession, 'sessionId');
|
|
22
|
-
repo.createDataSet(PasswordCredential, 'userName');
|
|
23
|
-
repo.createDataSet(PasswordResetToken, 'token');
|
|
13
|
+
i.bind(UserStore, () => new InMemoryStore({ model: UserModel, primaryKey: 'username' }));
|
|
14
|
+
i.bind(SessionStore, () => new InMemoryStore({ model: DefaultSession, primaryKey: 'sessionId' }));
|
|
15
|
+
i.bind(PasswordCredentialStore, () => new InMemoryStore({ model: PasswordCredential, primaryKey: 'userName' }));
|
|
16
|
+
i.bind(PasswordResetTokenStore, () => new InMemoryStore({ model: PasswordResetToken, primaryKey: 'token' }));
|
|
24
17
|
usePasswordPolicy(i);
|
|
25
18
|
};
|
|
26
|
-
describe('
|
|
19
|
+
describe('rest-service setup helpers', () => {
|
|
27
20
|
describe('useHttpAuthentication', () => {
|
|
28
|
-
it('
|
|
29
|
-
await usingAsync(
|
|
21
|
+
it('binds HttpAuthenticationSettings on the injector', async () => {
|
|
22
|
+
await usingAsync(createInjector(), async (i) => {
|
|
30
23
|
setupAuthStores(i);
|
|
31
24
|
useHttpAuthentication(i);
|
|
32
|
-
|
|
25
|
+
const settings = i.get(HttpAuthenticationSettings);
|
|
26
|
+
expect(settings).toBeDefined();
|
|
33
27
|
});
|
|
34
28
|
});
|
|
35
|
-
it('
|
|
36
|
-
await usingAsync(
|
|
29
|
+
it('registers basic-auth and cookie-auth providers by default', async () => {
|
|
30
|
+
await usingAsync(createInjector(), async (i) => {
|
|
37
31
|
setupAuthStores(i);
|
|
38
32
|
useHttpAuthentication(i);
|
|
39
|
-
const settings = i.
|
|
33
|
+
const settings = i.get(HttpAuthenticationSettings);
|
|
40
34
|
const providerNames = settings.authenticationProviders.map((p) => p.name);
|
|
41
35
|
expect(providerNames).toContain('basic-auth');
|
|
42
36
|
expect(providerNames).toContain('cookie-auth');
|
|
43
37
|
});
|
|
44
38
|
});
|
|
45
|
-
it('
|
|
46
|
-
await usingAsync(
|
|
39
|
+
it('omits the basic-auth provider when enableBasicAuth is false', async () => {
|
|
40
|
+
await usingAsync(createInjector(), async (i) => {
|
|
47
41
|
setupAuthStores(i);
|
|
48
42
|
useHttpAuthentication(i, { enableBasicAuth: false });
|
|
49
|
-
const settings = i.
|
|
43
|
+
const settings = i.get(HttpAuthenticationSettings);
|
|
50
44
|
const providerNames = settings.authenticationProviders.map((p) => p.name);
|
|
51
45
|
expect(providerNames).not.toContain('basic-auth');
|
|
52
46
|
expect(providerNames).toContain('cookie-auth');
|
|
@@ -54,35 +48,44 @@ describe('Injector extensions', () => {
|
|
|
54
48
|
});
|
|
55
49
|
});
|
|
56
50
|
describe('useRestService()', () => {
|
|
57
|
-
it('
|
|
58
|
-
await usingAsync(
|
|
51
|
+
it('acquires a pooled HTTP server and attaches a serverApi', async () => {
|
|
52
|
+
await usingAsync(createInjector(), async (i) => {
|
|
59
53
|
const port = getPort();
|
|
60
|
-
await useRestService({ injector: i, api: {}, root: '/', port });
|
|
61
|
-
expect(
|
|
54
|
+
const serverApi = await useRestService({ injector: i, api: {}, root: '/', port });
|
|
55
|
+
expect(serverApi).toBeDefined();
|
|
56
|
+
const pool = i.get(HttpServerPoolToken);
|
|
57
|
+
const record = await pool.acquire({ port });
|
|
58
|
+
expect(record.apis).toContain(serverApi);
|
|
62
59
|
});
|
|
63
60
|
});
|
|
64
61
|
});
|
|
65
62
|
describe('useStaticFiles()', () => {
|
|
66
|
-
it('
|
|
67
|
-
await usingAsync(
|
|
63
|
+
it('attaches a static-file serverApi to the pooled HTTP server', async () => {
|
|
64
|
+
await usingAsync(createInjector(), async (i) => {
|
|
68
65
|
const port = getPort();
|
|
69
|
-
await useStaticFiles({ injector: i, baseUrl: '/', path: '.', port });
|
|
70
|
-
expect(
|
|
66
|
+
const serverApi = await useStaticFiles({ injector: i, baseUrl: '/', path: '.', port });
|
|
67
|
+
expect(serverApi).toBeDefined();
|
|
68
|
+
const pool = i.get(HttpServerPoolToken);
|
|
69
|
+
const record = await pool.acquire({ port });
|
|
70
|
+
expect(record.apis).toContain(serverApi);
|
|
71
71
|
});
|
|
72
72
|
});
|
|
73
73
|
});
|
|
74
74
|
describe('useProxy()', () => {
|
|
75
|
-
it('
|
|
76
|
-
await usingAsync(
|
|
75
|
+
it('attaches a proxy serverApi to the pooled HTTP server', async () => {
|
|
76
|
+
await usingAsync(createInjector(), async (i) => {
|
|
77
77
|
const sourcePort = getPort();
|
|
78
78
|
const targetPort = getPort();
|
|
79
|
-
await useProxy({
|
|
79
|
+
const serverApi = await useProxy({
|
|
80
80
|
injector: i,
|
|
81
81
|
sourceBaseUrl: '/api',
|
|
82
82
|
targetBaseUrl: `http://localhost:${targetPort}`,
|
|
83
83
|
sourcePort,
|
|
84
84
|
});
|
|
85
|
-
expect(
|
|
85
|
+
expect(serverApi).toBeDefined();
|
|
86
|
+
const pool = i.get(HttpServerPoolToken);
|
|
87
|
+
const record = await pool.acquire({ port: sourcePort });
|
|
88
|
+
expect(record.apis).toContain(serverApi);
|
|
86
89
|
});
|
|
87
90
|
});
|
|
88
91
|
});
|
package/esm/helpers.spec.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.spec.js","sourceRoot":"","sources":["../src/helpers.spec.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"helpers.spec.js","sourceRoot":"","sources":["../src/helpers.spec.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,IAAI,IAAI,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAClE,OAAO,EAAE,OAAO,EAAE,MAAM,gCAAgC,CAAA;AACxD,OAAO,EAAE,cAAc,EAAiB,MAAM,mBAAmB,CAAA;AACjE,OAAO,EACL,kBAAkB,EAClB,uBAAuB,EACvB,kBAAkB,EAClB,uBAAuB,EACvB,iBAAiB,GAClB,MAAM,qBAAqB,CAAA;AAC5B,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAC7C,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAA;AAC7C,OAAO,EAAE,qBAAqB,EAAE,QAAQ,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAC9F,OAAO,EAAE,0BAA0B,EAAE,MAAM,mCAAmC,CAAA;AAC9E,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAA;AAC3D,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAA;AAC5D,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAEzD,MAAM,eAAe,GAAG,CAAC,CAAW,EAAE,EAAE;IACtC,CAAC,CAAC,IAAI,CAAC,SAAS,EAAE,GAAG,EAAE,CAAC,IAAI,aAAa,CAAC,EAAE,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,UAAU,EAAE,CAAC,CAAC,CAAA;IACxF,CAAC,CAAC,IAAI,CAAC,YAAY,EAAE,GAAG,EAAE,CAAC,IAAI,aAAa,CAAC,EAAE,KAAK,EAAE,cAAc,EAAE,UAAU,EAAE,WAAW,EAAE,CAAC,CAAC,CA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BAC7C,MAAM,UAAU,GAAG,OAAO,EAAE,CAAA;gBAC5B,MAAM,UAAU,GAAG,OAAO,EAAE,CAAA;gBAC5B,MAAM,SAAS,GAAG,MAAM,QAAQ,CAAC;oBAC/B,QAAQ,EAAE,CAAC;oBACX,aAAa,EAAE,MAAM;oBACrB,aAAa,EAAE,oBAAoB,UAAU,EAAE;oBAC/C,UAAU;iBACX,CAAC,CAAA;gBACF,MAAM,CAAC,SAAS,CAAC,CAAC,WAAW,EAAE,CAAA;gBAC/B,MAAM,IAAI,GAAG,CAAC,CAAC,GAAG,CAAC,mBAAmB,CAAC,CAAA;gBACvC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC,CAAA;gBACvD,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,SAAS,CAAC,SAAS,CAAC,CAAA;YAC1C,CAAC,CAAC,CAAA;QACJ,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
|
|
@@ -1,22 +1,66 @@
|
|
|
1
|
-
import { User } from '@furystack/core';
|
|
2
|
-
import
|
|
1
|
+
import type { User } from '@furystack/core';
|
|
2
|
+
import { type Token } from '@furystack/inject';
|
|
3
|
+
import type { DataSetToken } from '@furystack/repository';
|
|
3
4
|
import type { AuthenticationProvider } from './authentication-providers/authentication-provider.js';
|
|
4
|
-
import { DefaultSession } from './models/default-session.js';
|
|
5
|
+
import type { DefaultSession } from './models/default-session.js';
|
|
5
6
|
/**
|
|
6
|
-
*
|
|
7
|
+
* HTTP-authentication settings — rebound by {@link useHttpAuthentication}
|
|
8
|
+
* during application setup and consumed by {@link HttpUserContext},
|
|
9
|
+
* cookie-login strategies and OpenAPI generators.
|
|
10
|
+
*
|
|
11
|
+
* `userDataSet` / `sessionDataSet` are token references — consumers resolve
|
|
12
|
+
* them through the injector at the appropriate scope. `authenticationProviders`
|
|
13
|
+
* is an ordered list walked by {@link HttpUserContext} on every
|
|
14
|
+
* unauthenticated request.
|
|
7
15
|
*/
|
|
8
|
-
export
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
16
|
+
export interface HttpAuthenticationSettings {
|
|
17
|
+
/** Data set token for user lookups. */
|
|
18
|
+
userDataSet: DataSetToken<User, 'username'>;
|
|
19
|
+
/** Data set token for session lookups. */
|
|
20
|
+
sessionDataSet: DataSetToken<DefaultSession, 'sessionId'>;
|
|
21
|
+
/** Cookie name used by the built-in cookie-auth provider. */
|
|
13
22
|
cookieName: string;
|
|
23
|
+
/** Whether the built-in Basic Auth provider is installed by {@link useHttpAuthentication}. */
|
|
14
24
|
enableBasicAuth: boolean;
|
|
15
25
|
/**
|
|
16
|
-
* Ordered list of authentication providers. Populated
|
|
17
|
-
* and extended by
|
|
18
|
-
*
|
|
26
|
+
* Ordered list of authentication providers. Populated during
|
|
27
|
+
* {@link useHttpAuthentication} and extended by plugins such as
|
|
28
|
+
* `useJwtAuthentication`.
|
|
19
29
|
*/
|
|
20
30
|
authenticationProviders: AuthenticationProvider[];
|
|
31
|
+
/**
|
|
32
|
+
* TTL applied to the user-resolution cache. Every authenticated request
|
|
33
|
+
* runs at most one provider chain walk per cache key per TTL window;
|
|
34
|
+
* subsequent calls within the window return the cached `User`.
|
|
35
|
+
*
|
|
36
|
+
* Bounds the staleness window for out-of-band identity changes
|
|
37
|
+
* (session invalidation on a sibling instance, role mutation in storage,
|
|
38
|
+
* etc.) when the application is deployed across multiple nodes.
|
|
39
|
+
*
|
|
40
|
+
* Set to `0` to disable caching entirely. When omitted, the default
|
|
41
|
+
* {@link DEFAULT_USER_CACHE_TTL_MS} is used by `UserResolutionCache`.
|
|
42
|
+
*/
|
|
43
|
+
userCacheTtlMs?: number;
|
|
44
|
+
/**
|
|
45
|
+
* Maximum number of entries in the user-resolution cache before LRU
|
|
46
|
+
* eviction kicks in. When omitted, the default
|
|
47
|
+
* {@link DEFAULT_USER_CACHE_CAPACITY} is used by `UserResolutionCache`.
|
|
48
|
+
*/
|
|
49
|
+
userCacheCapacity?: number;
|
|
21
50
|
}
|
|
51
|
+
/** Default TTL applied to the user-resolution cache (30 seconds). */
|
|
52
|
+
export declare const DEFAULT_USER_CACHE_TTL_MS = 30000;
|
|
53
|
+
/** Default LRU capacity ceiling for the user-resolution cache. */
|
|
54
|
+
export declare const DEFAULT_USER_CACHE_CAPACITY = 10000;
|
|
55
|
+
/**
|
|
56
|
+
* Returns a fresh copy of the default {@link HttpAuthenticationSettings}.
|
|
57
|
+
* Used as the starting point inside {@link useHttpAuthentication}.
|
|
58
|
+
*/
|
|
59
|
+
export declare const defaultHttpAuthenticationSettings: () => HttpAuthenticationSettings;
|
|
60
|
+
/**
|
|
61
|
+
* DI token carrying the current {@link HttpAuthenticationSettings}. Rebind
|
|
62
|
+
* via {@link useHttpAuthentication} (preferred) or directly through
|
|
63
|
+
* {@link Injector.bind} for advanced scenarios.
|
|
64
|
+
*/
|
|
65
|
+
export declare const HttpAuthenticationSettings: Token<HttpAuthenticationSettings, 'singleton'>;
|
|
22
66
|
//# sourceMappingURL=http-authentication-settings.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http-authentication-settings.d.ts","sourceRoot":"","sources":["../src/http-authentication-settings.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAA;
|
|
1
|
+
{"version":3,"file":"http-authentication-settings.d.ts","sourceRoot":"","sources":["../src/http-authentication-settings.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAA;AAE3C,OAAO,EAAiB,KAAK,KAAK,EAAE,MAAM,mBAAmB,CAAA;AAC7D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAA;AACzD,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,uDAAuD,CAAA;AAGnG,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAA;AAGjE;;;;;;;;;GASG;AACH,MAAM,WAAW,0BAA0B;IACzC,uCAAuC;IACvC,WAAW,EAAE,YAAY,CAAC,IAAI,EAAE,UAAU,CAAC,CAAA;IAC3C,0CAA0C;IAC1C,cAAc,EAAE,YAAY,CAAC,cAAc,EAAE,WAAW,CAAC,CAAA;IACzD,6DAA6D;IAC7D,UAAU,EAAE,MAAM,CAAA;IAClB,8FAA8F;IAC9F,eAAe,EAAE,OAAO,CAAA;IACxB;;;;OAIG;IACH,uBAAuB,EAAE,sBAAsB,EAAE,CAAA;IACjD;;;;;;;;;;;OAWG;IACH,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB;;;;OAIG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAA;CAC3B;AAED,qEAAqE;AACrE,eAAO,MAAM,yBAAyB,QAAS,CAAA;AAE/C,kEAAkE;AAClE,eAAO,MAAM,2BAA2B,QAAS,CAAA;AAEjD;;;GAGG;AACH,eAAO,MAAM,iCAAiC,QAAO,0BAQnD,CAAA;AAEF;;;;GAIG;AACH,eAAO,MAAM,0BAA0B,EAAE,KAAK,CAAC,0BAA0B,EAAE,WAAW,CAIpF,CAAA"}
|
|
@@ -1,32 +1,30 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
import { User } from '@furystack/core';
|
|
8
|
-
import { Injectable } from '@furystack/inject';
|
|
9
|
-
import { getDataSetFor } from '@furystack/repository';
|
|
10
|
-
import { DefaultSession } from './models/default-session.js';
|
|
1
|
+
import { defineService } from '@furystack/inject';
|
|
2
|
+
import { SessionDataSet, UserDataSet } from './user-store.js';
|
|
3
|
+
/** Default TTL applied to the user-resolution cache (30 seconds). */
|
|
4
|
+
export const DEFAULT_USER_CACHE_TTL_MS = 30_000;
|
|
5
|
+
/** Default LRU capacity ceiling for the user-resolution cache. */
|
|
6
|
+
export const DEFAULT_USER_CACHE_CAPACITY = 10_000;
|
|
11
7
|
/**
|
|
12
|
-
*
|
|
8
|
+
* Returns a fresh copy of the default {@link HttpAuthenticationSettings}.
|
|
9
|
+
* Used as the starting point inside {@link useHttpAuthentication}.
|
|
13
10
|
*/
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
HttpAuthenticationSettings =
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
11
|
+
export const defaultHttpAuthenticationSettings = () => ({
|
|
12
|
+
userDataSet: UserDataSet,
|
|
13
|
+
sessionDataSet: SessionDataSet,
|
|
14
|
+
cookieName: 'fss',
|
|
15
|
+
enableBasicAuth: true,
|
|
16
|
+
authenticationProviders: [],
|
|
17
|
+
userCacheTtlMs: DEFAULT_USER_CACHE_TTL_MS,
|
|
18
|
+
userCacheCapacity: DEFAULT_USER_CACHE_CAPACITY,
|
|
19
|
+
});
|
|
20
|
+
/**
|
|
21
|
+
* DI token carrying the current {@link HttpAuthenticationSettings}. Rebind
|
|
22
|
+
* via {@link useHttpAuthentication} (preferred) or directly through
|
|
23
|
+
* {@link Injector.bind} for advanced scenarios.
|
|
24
|
+
*/
|
|
25
|
+
export const HttpAuthenticationSettings = defineService({
|
|
26
|
+
name: 'furystack/rest-service/HttpAuthenticationSettings',
|
|
27
|
+
lifetime: 'singleton',
|
|
28
|
+
factory: () => defaultHttpAuthenticationSettings(),
|
|
29
|
+
});
|
|
32
30
|
//# sourceMappingURL=http-authentication-settings.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http-authentication-settings.js","sourceRoot":"","sources":["../src/http-authentication-settings.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"http-authentication-settings.js","sourceRoot":"","sources":["../src/http-authentication-settings.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAc,MAAM,mBAAmB,CAAA;AAM7D,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAA;AAgD7D,qEAAqE;AACrE,MAAM,CAAC,MAAM,yBAAyB,GAAG,MAAM,CAAA;AAE/C,kEAAkE;AAClE,MAAM,CAAC,MAAM,2BAA2B,GAAG,MAAM,CAAA;AAEjD;;;GAGG;AACH,MAAM,CAAC,MAAM,iCAAiC,GAAG,GAA+B,EAAE,CAAC,CAAC;IAClF,WAAW,EAAE,WAAW;IACxB,cAAc,EAAE,cAAc;IAC9B,UAAU,EAAE,KAAK;IACjB,eAAe,EAAE,IAAI;IACrB,uBAAuB,EAAE,EAAE;IAC3B,cAAc,EAAE,yBAAyB;IACzC,iBAAiB,EAAE,2BAA2B;CAC/C,CAAC,CAAA;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAmD,aAAa,CAAC;IACtG,IAAI,EAAE,mDAAmD;IACzD,QAAQ,EAAE,WAAW;IACrB,OAAO,EAAE,GAAG,EAAE,CAAC,iCAAiC,EAAE;CACnD,CAAC,CAAA"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { type Token } from '@furystack/inject';
|
|
2
|
+
import type { IncomingMessage, Server, ServerResponse } from 'http';
|
|
3
|
+
import type { Duplex } from 'stream';
|
|
4
|
+
/**
|
|
5
|
+
* Options accepted by {@link HttpServerPool.acquire}.
|
|
6
|
+
*/
|
|
7
|
+
export interface ServerOptions {
|
|
8
|
+
hostName?: string;
|
|
9
|
+
port: number;
|
|
10
|
+
}
|
|
11
|
+
/** Payload passed to every {@link ServerApi.onRequest} invocation. */
|
|
12
|
+
export interface OnRequest {
|
|
13
|
+
req: IncomingMessage;
|
|
14
|
+
res: ServerResponse;
|
|
15
|
+
}
|
|
16
|
+
/** Payload passed to every {@link ServerApi.onUpgrade} invocation. */
|
|
17
|
+
export interface OnUpgrade {
|
|
18
|
+
req: IncomingMessage;
|
|
19
|
+
socket: Duplex;
|
|
20
|
+
head: Buffer;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Contract every server-side consumer (REST, static sites, proxies,
|
|
24
|
+
* websockets) implements. Pushed into {@link ServerRecord.apis} by
|
|
25
|
+
* `useRestService` / `useStaticFiles` / `useProxy` / websocket wiring.
|
|
26
|
+
*/
|
|
27
|
+
export interface ServerApi {
|
|
28
|
+
shouldExec: (options: OnRequest) => boolean;
|
|
29
|
+
onRequest: (options: OnRequest) => Promise<void>;
|
|
30
|
+
onUpgrade?: (options: OnUpgrade) => Promise<void>;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Shared-port registry entry. Multiple {@link ServerApi}s attached to the
|
|
34
|
+
* same `port`/`hostName` share a single `http.Server` via the pool and
|
|
35
|
+
* dispatch to the first matching `shouldExec`.
|
|
36
|
+
*/
|
|
37
|
+
export interface ServerRecord {
|
|
38
|
+
server: Server;
|
|
39
|
+
apis: ServerApi[];
|
|
40
|
+
}
|
|
41
|
+
export declare const DEFAULT_HOST = "localhost";
|
|
42
|
+
/**
|
|
43
|
+
* Public surface of the {@link HttpServerPoolToken}. Acquires shared
|
|
44
|
+
* `http.Server` instances keyed by url. Consumers push their
|
|
45
|
+
* {@link ServerApi}s onto the returned record's `apis` array.
|
|
46
|
+
*
|
|
47
|
+
* Internal to the rest-service package — application code interacts with
|
|
48
|
+
* the pool indirectly through `useRestService` / `useStaticFiles` /
|
|
49
|
+
* `useProxy`.
|
|
50
|
+
*/
|
|
51
|
+
export interface HttpServerPool {
|
|
52
|
+
/**
|
|
53
|
+
* Returns the {@link ServerRecord} for the given options, opening a new
|
|
54
|
+
* `http.Server` on first request and returning the cached instance on
|
|
55
|
+
* subsequent requests to the same url.
|
|
56
|
+
*/
|
|
57
|
+
acquire(options: ServerOptions): Promise<ServerRecord>;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* DI token for the {@link HttpServerPool}. Scoped so each test / application
|
|
61
|
+
* scope has its own set of HTTP servers and sockets.
|
|
62
|
+
*/
|
|
63
|
+
export declare const HttpServerPoolToken: Token<HttpServerPool, 'scoped'>;
|
|
64
|
+
//# sourceMappingURL=http-server-pool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http-server-pool.d.ts","sourceRoot":"","sources":["../src/http-server-pool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,KAAK,KAAK,EAAE,MAAM,mBAAmB,CAAA;AAC7D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,MAAM,CAAA;AAGnE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAGpC;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,IAAI,EAAE,MAAM,CAAA;CACb;AAED,sEAAsE;AACtE,MAAM,WAAW,SAAS;IACxB,GAAG,EAAE,eAAe,CAAA;IACpB,GAAG,EAAE,cAAc,CAAA;CACpB;AAED,sEAAsE;AACtE,MAAM,WAAW,SAAS;IACxB,GAAG,EAAE,eAAe,CAAA;IACpB,MAAM,EAAE,MAAM,CAAA;IACd,IAAI,EAAE,MAAM,CAAA;CACb;AAED;;;;GAIG;AACH,MAAM,WAAW,SAAS;IACxB,UAAU,EAAE,CAAC,OAAO,EAAE,SAAS,KAAK,OAAO,CAAA;IAC3C,SAAS,EAAE,CAAC,OAAO,EAAE,SAAS,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;IAChD,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,SAAS,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;CAClD;AAED;;;;GAIG;AACH,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,MAAM,CAAA;IACd,IAAI,EAAE,SAAS,EAAE,CAAA;CAClB;AAED,eAAO,MAAM,YAAY,cAAc,CAAA;AAIvC;;;;;;;;GAQG;AACH,MAAM,WAAW,cAAc;IAC7B;;;;OAIG;IACH,OAAO,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,YAAY,CAAC,CAAA;CACvD;AAED;;;GAGG;AACH,eAAO,MAAM,mBAAmB,EAAE,KAAK,CAAC,cAAc,EAAE,QAAQ,CA6F9D,CAAA"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { defineService } from '@furystack/inject';
|
|
2
|
+
import { createServer } from 'http';
|
|
3
|
+
import { ServerTelemetryToken } from './server-telemetry.js';
|
|
4
|
+
export const DEFAULT_HOST = 'localhost';
|
|
5
|
+
const buildHostUrl = (options) => `http://${options.hostName ?? DEFAULT_HOST}:${options.port}`;
|
|
6
|
+
/**
|
|
7
|
+
* DI token for the {@link HttpServerPool}. Scoped so each test / application
|
|
8
|
+
* scope has its own set of HTTP servers and sockets.
|
|
9
|
+
*/
|
|
10
|
+
export const HttpServerPoolToken = defineService({
|
|
11
|
+
name: 'furystack/rest-service/HttpServerPool',
|
|
12
|
+
lifetime: 'scoped',
|
|
13
|
+
factory: ({ inject, onDispose }) => {
|
|
14
|
+
const telemetry = inject(ServerTelemetryToken);
|
|
15
|
+
const servers = new Map();
|
|
16
|
+
const pendingCreates = new Map();
|
|
17
|
+
const openedSockets = new Set();
|
|
18
|
+
const trackConnection = (socket) => {
|
|
19
|
+
openedSockets.add(socket);
|
|
20
|
+
socket.once('close', () => openedSockets.delete(socket));
|
|
21
|
+
};
|
|
22
|
+
const createRecord = async (url, options) => {
|
|
23
|
+
const apis = [];
|
|
24
|
+
const server = createServer((req, res) => {
|
|
25
|
+
const apiMatch = apis.find((api) => api.shouldExec({ req, res }));
|
|
26
|
+
if (apiMatch) {
|
|
27
|
+
apiMatch.onRequest({ req, res }).catch((error) => {
|
|
28
|
+
telemetry.emit('onRequestFailed', [error, req, res]);
|
|
29
|
+
});
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
res.destroy();
|
|
33
|
+
});
|
|
34
|
+
server.on('upgrade', (req, socket, head) => {
|
|
35
|
+
const apiMatch = apis.find((api) => api.shouldExec({ req, res: {} }));
|
|
36
|
+
if (apiMatch?.onUpgrade) {
|
|
37
|
+
apiMatch.onUpgrade({ req, socket, head }).catch((error) => {
|
|
38
|
+
telemetry.emit('onRequestFailed', [error, req, {}]);
|
|
39
|
+
socket.destroy();
|
|
40
|
+
});
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
socket.destroy();
|
|
44
|
+
});
|
|
45
|
+
server.on('connection', trackConnection);
|
|
46
|
+
try {
|
|
47
|
+
await new Promise((resolve, reject) => {
|
|
48
|
+
server.on('listening', () => resolve());
|
|
49
|
+
server.on('error', (err) => reject(err));
|
|
50
|
+
server.listen(options.port, options.hostName);
|
|
51
|
+
});
|
|
52
|
+
const record = { server, apis };
|
|
53
|
+
servers.set(url, record);
|
|
54
|
+
telemetry.emit('onServerListening', { url, port: options.port, hostName: options.hostName });
|
|
55
|
+
return record;
|
|
56
|
+
}
|
|
57
|
+
finally {
|
|
58
|
+
pendingCreates.delete(url);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
onDispose(async () => {
|
|
62
|
+
await Promise.allSettled([...pendingCreates.values()]);
|
|
63
|
+
openedSockets.forEach((socket) => socket.destroy());
|
|
64
|
+
await Promise.allSettled([...servers.entries()].map(([url, record]) => new Promise((resolve, reject) => {
|
|
65
|
+
record.server.close((err) => {
|
|
66
|
+
if (err) {
|
|
67
|
+
reject(err);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
telemetry.emit('onServerClosed', { url });
|
|
71
|
+
resolve();
|
|
72
|
+
});
|
|
73
|
+
record.server.off('connection', trackConnection);
|
|
74
|
+
})));
|
|
75
|
+
servers.clear();
|
|
76
|
+
});
|
|
77
|
+
return {
|
|
78
|
+
acquire: async (options) => {
|
|
79
|
+
const url = buildHostUrl(options);
|
|
80
|
+
const existing = servers.get(url);
|
|
81
|
+
if (existing) {
|
|
82
|
+
return existing;
|
|
83
|
+
}
|
|
84
|
+
const pending = pendingCreates.get(url);
|
|
85
|
+
if (pending) {
|
|
86
|
+
return pending;
|
|
87
|
+
}
|
|
88
|
+
const createPromise = createRecord(url, options);
|
|
89
|
+
pendingCreates.set(url, createPromise);
|
|
90
|
+
return createPromise;
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
//# sourceMappingURL=http-server-pool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http-server-pool.js","sourceRoot":"","sources":["../src/http-server-pool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAc,MAAM,mBAAmB,CAAA;AAE7D,OAAO,EAAE,YAAY,EAAE,MAAM,MAAM,CAAA;AAGnC,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAA;AA4C5D,MAAM,CAAC,MAAM,YAAY,GAAG,WAAW,CAAA;AAEvC,MAAM,YAAY,GAAG,CAAC,OAAsB,EAAU,EAAE,CAAC,UAAU,OAAO,CAAC,QAAQ,IAAI,YAAY,IAAI,OAAO,CAAC,IAAI,EAAE,CAAA;AAoBrH;;;GAGG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAoC,aAAa,CAAC;IAChF,IAAI,EAAE,uCAAuC;IAC7C,QAAQ,EAAE,QAAQ;IAClB,OAAO,EAAE,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,EAAkB,EAAE;QACjD,MAAM,SAAS,GAAG,MAAM,CAAC,oBAAoB,CAAC,CAAA;QAC9C,MAAM,OAAO,GAAG,IAAI,GAAG,EAAwB,CAAA;QAC/C,MAAM,cAAc,GAAG,IAAI,GAAG,EAAiC,CAAA;QAC/D,MAAM,aAAa,GAAG,IAAI,GAAG,EAAU,CAAA;QAEvC,MAAM,eAAe,GAAG,CAAC,MAAc,EAAQ,EAAE;YAC/C,aAAa,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;YACzB,MAAM,CAAC,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