@rest-vir/run-service 1.4.0 → 1.5.1
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/dist/handle-request/handle-cors.js +5 -1
- package/dist/handle-request/handle-endpoint.js +4 -2
- package/dist/handle-request/handle-route.js +4 -1
- package/dist/handle-request/handle-search-params.js +3 -1
- package/dist/handle-request/handle-web-socket.js +16 -1
- package/dist/handle-request/pre-handler.js +4 -1
- package/dist/start-service/start-service.js +4 -1
- package/dist/test/test-service.js +21 -0
- package/package.json +14 -14
|
@@ -63,7 +63,11 @@ function buildStandardCorsHeaders(matchedOrigin, customHeaders) {
|
|
|
63
63
|
};
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
|
-
const accessControlMaxAgeValue = String(convertDuration({
|
|
66
|
+
const accessControlMaxAgeValue = String(convertDuration({
|
|
67
|
+
hours: 1,
|
|
68
|
+
}, {
|
|
69
|
+
seconds: true,
|
|
70
|
+
}).seconds);
|
|
67
71
|
const contentLengthHeaders = {
|
|
68
72
|
/**
|
|
69
73
|
* Safari (and potentially other browsers) need content-length 0 for 204 or it will hang waiting
|
|
@@ -37,7 +37,7 @@ export async function handleEndpointRequest({ endpoint, request, response, attac
|
|
|
37
37
|
if (!endpointResult.statusCode) {
|
|
38
38
|
throw new RestVirHandlerError(endpoint, 'Missing response status code.');
|
|
39
39
|
}
|
|
40
|
-
if (isErrorHttpStatus(endpointResult.statusCode)) {
|
|
40
|
+
else if (isErrorHttpStatus(endpointResult.statusCode)) {
|
|
41
41
|
endpoint.service.logger.error(new RestVirHandlerError(endpoint, `Endpoint implementation returned error status: ${endpointResult.statusCode}`, endpointResult.statusCode));
|
|
42
42
|
return {
|
|
43
43
|
statusCode: endpointResult.statusCode,
|
|
@@ -50,7 +50,9 @@ export async function handleEndpointRequest({ endpoint, request, response, attac
|
|
|
50
50
|
throw new RestVirHandlerError(endpoint, 'Got response data but none was expected.');
|
|
51
51
|
}
|
|
52
52
|
if (!endpoint.bypassResponseValidation) {
|
|
53
|
-
assertValidShape(endpointResult.responseData, endpoint.responseDataShape, {
|
|
53
|
+
assertValidShape(endpointResult.responseData, endpoint.responseDataShape, {
|
|
54
|
+
allowExtraKeys: true,
|
|
55
|
+
}, 'invalid response data');
|
|
54
56
|
}
|
|
55
57
|
return {
|
|
56
58
|
headers: {
|
|
@@ -73,7 +73,10 @@ export async function handleRoute({ webSocket, request, response, route, attachI
|
|
|
73
73
|
catch (error) {
|
|
74
74
|
if (error instanceof RejectRequestError) {
|
|
75
75
|
assert.isDefined(response, 'no response object');
|
|
76
|
-
await handleHandlerOutput({
|
|
76
|
+
await handleHandlerOutput({
|
|
77
|
+
statusCode: error.httpStatus,
|
|
78
|
+
body: error.responseErrorMessage,
|
|
79
|
+
}, response);
|
|
77
80
|
return;
|
|
78
81
|
}
|
|
79
82
|
route.service.logger.error(ensureError(error));
|
|
@@ -15,7 +15,9 @@ export function handleSearchParams({ request, route, }) {
|
|
|
15
15
|
const shape = route.searchParamsShape;
|
|
16
16
|
const validationError = shape
|
|
17
17
|
? wrapInTry(() => {
|
|
18
|
-
assertValidShape(searchParams, shape, {
|
|
18
|
+
assertValidShape(searchParams, shape, {
|
|
19
|
+
allowExtraKeys: true,
|
|
20
|
+
});
|
|
19
21
|
return undefined;
|
|
20
22
|
})
|
|
21
23
|
: undefined;
|
|
@@ -15,6 +15,19 @@ export async function handleWebSocketRequest({ attachId, request, implementedWeb
|
|
|
15
15
|
const restVirContext = request.restVirContext?.[attachId];
|
|
16
16
|
assert.isDefined(restVirContext, 'restVirContext is not defined');
|
|
17
17
|
const webSocket = overwriteWebSocketMethods(implementedWebSocket, wsWebSocket, WebSocketLocation.OnHost);
|
|
18
|
+
/**
|
|
19
|
+
* `Duplexify`-based sockets (used by `@fastify/websocket`'s `injectWS` for testing) don't emit
|
|
20
|
+
* `'close'` after `end()` like real TCP sockets do. The `ws` library relies on the socket
|
|
21
|
+
* `'close'` event to fire `emitClose()` and complete the close handshake. Destroying the socket
|
|
22
|
+
* when it finishes writing triggers `'close'` promptly. For real TCP sockets this is
|
|
23
|
+
* effectively a no-op since they close naturally after the FIN handshake.
|
|
24
|
+
*/
|
|
25
|
+
const rawSocket = wsWebSocket._socket;
|
|
26
|
+
rawSocket?.on('finish', () => {
|
|
27
|
+
if (!rawSocket.destroyed) {
|
|
28
|
+
rawSocket.destroy();
|
|
29
|
+
}
|
|
30
|
+
});
|
|
18
31
|
const webSocketCallbackParams = {
|
|
19
32
|
context: restVirContext.context,
|
|
20
33
|
headers: request.headers,
|
|
@@ -40,7 +53,9 @@ export async function handleWebSocketRequest({ attachId, request, implementedWeb
|
|
|
40
53
|
const stringRawMessage = String(rawMessage);
|
|
41
54
|
message = parseJsonWithUndefined(stringRawMessage);
|
|
42
55
|
if (implementedWebSocket.messageFromClientShape) {
|
|
43
|
-
assertValidShape(message, implementedWebSocket.messageFromClientShape, {
|
|
56
|
+
assertValidShape(message, implementedWebSocket.messageFromClientShape, {
|
|
57
|
+
allowExtraKeys: true,
|
|
58
|
+
}, 'Invalid message send shape.');
|
|
44
59
|
}
|
|
45
60
|
else if (message) {
|
|
46
61
|
throw new Error(`Did not expect any data from the client but got ${stringify(message)}.`);
|
|
@@ -74,7 +74,10 @@ export async function preHandler({ request, response, service, server, attachId,
|
|
|
74
74
|
};
|
|
75
75
|
}
|
|
76
76
|
attachedRestVirContext.requestData = requestData;
|
|
77
|
-
const searchParams = handleSearchParams({
|
|
77
|
+
const searchParams = handleSearchParams({
|
|
78
|
+
request,
|
|
79
|
+
route,
|
|
80
|
+
});
|
|
78
81
|
if (!('data' in searchParams)) {
|
|
79
82
|
return handleHandlerOutputWithoutSending(searchParams, response);
|
|
80
83
|
}
|
|
@@ -80,7 +80,10 @@ async function startServer(service, { host, port }, fastifyPlugins) {
|
|
|
80
80
|
await attachService(server, service, {
|
|
81
81
|
throwErrorsForExternalHandling: false,
|
|
82
82
|
});
|
|
83
|
-
await server.listen({
|
|
83
|
+
await server.listen({
|
|
84
|
+
port,
|
|
85
|
+
host,
|
|
86
|
+
});
|
|
84
87
|
return {
|
|
85
88
|
host,
|
|
86
89
|
port,
|
|
@@ -188,6 +188,27 @@ export async function testExistingServer(server, service, options = {}) {
|
|
|
188
188
|
}
|
|
189
189
|
: {}))
|
|
190
190
|
: new WebSocket(webSocketUrl, protocols);
|
|
191
|
+
if (webSocketOrigin == undefined) {
|
|
192
|
+
/**
|
|
193
|
+
* `injectWS` creates a `ws` WebSocket with `_closeTimeout = undefined`.
|
|
194
|
+
* `setTimeout(fn, undefined)` fires immediately (0ms), which destroys the
|
|
195
|
+
* socket before the close frame can be sent, preventing the server-side close
|
|
196
|
+
* event from firing. Set a reasonable fallback timeout.
|
|
197
|
+
*
|
|
198
|
+
* Additionally, `injectWS` uses `Duplexify` streams which don't emit `'close'`
|
|
199
|
+
* after `end()` like real TCP sockets do. The `ws` library relies on the socket
|
|
200
|
+
* `'close'` event to complete the close handshake. Destroying the socket when
|
|
201
|
+
* it finishes writing triggers `'close'` promptly instead of waiting for the
|
|
202
|
+
* full close timeout.
|
|
203
|
+
*/
|
|
204
|
+
const wsInternal = webSocket;
|
|
205
|
+
wsInternal._closeTimeout = 5000;
|
|
206
|
+
wsInternal._socket?.on('finish', () => {
|
|
207
|
+
if (!wsInternal._socket?.destroyed) {
|
|
208
|
+
wsInternal._socket?.destroy();
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
}
|
|
191
212
|
const finalized = await finalizeWebSocket(webSocketDefinition, webSocket, listeners, WebSocketLocation.OnClient);
|
|
192
213
|
if (webSocketOrigin == undefined) {
|
|
193
214
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rest-vir/run-service",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.1",
|
|
4
4
|
"description": "Run a service defined by @rest-vir/define-service and implemented by @rest-vir/implement-service.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"rest",
|
|
@@ -39,31 +39,31 @@
|
|
|
39
39
|
"test:update": "npm test update"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@augment-vir/assert": "^31.
|
|
43
|
-
"@augment-vir/common": "^31.
|
|
44
|
-
"@augment-vir/node": "^31.
|
|
42
|
+
"@augment-vir/assert": "^31.68.1",
|
|
43
|
+
"@augment-vir/common": "^31.68.1",
|
|
44
|
+
"@augment-vir/node": "^31.68.1",
|
|
45
45
|
"@fastify/compress": "^8.3.1",
|
|
46
46
|
"@fastify/websocket": "^11.2.0",
|
|
47
|
-
"@rest-vir/define-service": "^1.
|
|
48
|
-
"@rest-vir/implement-service": "^1.
|
|
47
|
+
"@rest-vir/define-service": "^1.5.1",
|
|
48
|
+
"@rest-vir/implement-service": "^1.5.1",
|
|
49
49
|
"cluster-vir": "^1.0.1",
|
|
50
|
-
"date-vir": "^8.1
|
|
51
|
-
"fastify": "^5.
|
|
50
|
+
"date-vir": "^8.2.1",
|
|
51
|
+
"fastify": "^5.8.2",
|
|
52
52
|
"light-my-request": "^6.6.0",
|
|
53
53
|
"portfinder": "^1.0.38",
|
|
54
|
-
"type-fest": "^5.
|
|
54
|
+
"type-fest": "^5.4.4",
|
|
55
55
|
"url-vir": "^2.1.7"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
|
-
"@augment-vir/test": "^31.
|
|
59
|
-
"@fastify/multipart": "^9.
|
|
58
|
+
"@augment-vir/test": "^31.68.1",
|
|
59
|
+
"@fastify/multipart": "^9.4.0",
|
|
60
60
|
"@types/connect": "^3.4.38",
|
|
61
|
-
"@types/node": "^25.0
|
|
61
|
+
"@types/node": "^25.3.0",
|
|
62
62
|
"@types/ws": "^8.18.1",
|
|
63
|
-
"c8": "^
|
|
63
|
+
"c8": "^11.0.0",
|
|
64
64
|
"istanbul-smart-text-reporter": "^1.1.5",
|
|
65
65
|
"markdown-code-example-inserter": "^3.0.3",
|
|
66
|
-
"object-shape-tester": "^6.
|
|
66
|
+
"object-shape-tester": "^6.11.0"
|
|
67
67
|
},
|
|
68
68
|
"peerDependencies": {
|
|
69
69
|
"@augment-vir/test": ">=30",
|