@backstage/backend-openapi-utils 0.6.8-next.2 → 0.6.9-next.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 +18 -0
- package/dist/proxy/setup.cjs.js +27 -3
- package/dist/proxy/setup.cjs.js.map +1 -1
- package/dist/testUtils.cjs.js +8 -6
- package/dist/testUtils.cjs.js.map +1 -1
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @backstage/backend-openapi-utils
|
|
2
2
|
|
|
3
|
+
## 0.6.9-next.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies
|
|
8
|
+
- @backstage/errors@1.3.1-next.0
|
|
9
|
+
- @backstage/backend-plugin-api@1.9.1-next.0
|
|
10
|
+
- @backstage/types@1.2.2
|
|
11
|
+
|
|
12
|
+
## 0.6.8
|
|
13
|
+
|
|
14
|
+
### Patch Changes
|
|
15
|
+
|
|
16
|
+
- 59dd9b9: Fixes a memory leak during `wrapServer` where stopped servers weren't removed from the clean up list.
|
|
17
|
+
- Updated dependencies
|
|
18
|
+
- @backstage/backend-plugin-api@1.9.0
|
|
19
|
+
- @backstage/errors@1.3.0
|
|
20
|
+
|
|
3
21
|
## 0.6.8-next.2
|
|
4
22
|
|
|
5
23
|
### Patch Changes
|
package/dist/proxy/setup.cjs.js
CHANGED
|
@@ -58,12 +58,36 @@ class Proxy {
|
|
|
58
58
|
await this.validator.initialize(`${url}/openapi.json`);
|
|
59
59
|
this.express.server = server;
|
|
60
60
|
}
|
|
61
|
-
stop() {
|
|
61
|
+
async stop() {
|
|
62
62
|
if (Object.keys(this.#openRequests).length > 0) {
|
|
63
63
|
throw new Error("There are still open requests");
|
|
64
64
|
}
|
|
65
|
-
this.server
|
|
66
|
-
|
|
65
|
+
if (!this.express.server) {
|
|
66
|
+
throw new Error(
|
|
67
|
+
"Proxy server was not initialized with an express server"
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
const results = await Promise.allSettled([
|
|
71
|
+
this.server.stop(),
|
|
72
|
+
// If this isn't expressly closed, it will cause a jest memory leak warning.
|
|
73
|
+
new Promise((resolve, reject) => {
|
|
74
|
+
this.express.server?.close((err) => {
|
|
75
|
+
if (err) {
|
|
76
|
+
reject(err);
|
|
77
|
+
} else {
|
|
78
|
+
resolve(void 0);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
})
|
|
82
|
+
]);
|
|
83
|
+
if (results.some((result) => result.status === "rejected")) {
|
|
84
|
+
const errors = results.filter(
|
|
85
|
+
(result) => result.status === "rejected"
|
|
86
|
+
).map((result) => result.reason);
|
|
87
|
+
throw new Error(
|
|
88
|
+
`Failed to stop proxy server: ${errors.map((e) => e.message).join(", ")}`
|
|
89
|
+
);
|
|
90
|
+
}
|
|
67
91
|
}
|
|
68
92
|
get url() {
|
|
69
93
|
return this.server.proxyEnv.HTTP_PROXY;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setup.cjs.js","sources":["../../src/proxy/setup.ts"],"sourcesContent":["/*\n * Copyright 2024 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport * as mockttp from 'mockttp';\nimport { OpenApiProxyValidator } from '../schema/validation';\nimport getPort from 'get-port';\nimport { Server } from 'node:http';\n\nexport class Proxy {\n server: mockttp.Mockttp;\n #openRequests: Record<string, mockttp.CompletedRequest> = {};\n requestResponsePairs = new Map<\n mockttp.CompletedRequest,\n mockttp.CompletedResponse\n >();\n validator: OpenApiProxyValidator;\n public forwardTo: { port: number } = { port: 0 };\n express: { server: Server | undefined } = { server: undefined };\n constructor() {\n this.server = mockttp.getLocal();\n this.validator = new OpenApiProxyValidator();\n }\n\n async setup() {\n await this.server.start();\n this.forwardTo.port = await getPort();\n this.server\n .forAnyRequest()\n .thenForwardTo(`http://localhost:${this.forwardTo.port}`);\n await this.server.on('request', request => {\n this.#openRequests[request.id] = request;\n });\n await this.server.on('response', response => {\n const request = this.#openRequests[response.id];\n if (request) {\n this.requestResponsePairs.set(request, response);\n }\n delete this.#openRequests[response.id];\n this.validator.validate(request, response);\n });\n }\n\n async initialize(url: string, server: Server) {\n await this.validator.initialize(`${url}/openapi.json`);\n this.express.server = server;\n }\n\n stop() {\n if (Object.keys(this.#openRequests).length > 0) {\n throw new Error('There are still open requests');\n }\n this.server.stop()
|
|
1
|
+
{"version":3,"file":"setup.cjs.js","sources":["../../src/proxy/setup.ts"],"sourcesContent":["/*\n * Copyright 2024 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport * as mockttp from 'mockttp';\nimport { OpenApiProxyValidator } from '../schema/validation';\nimport getPort from 'get-port';\nimport { Server } from 'node:http';\n\nexport class Proxy {\n server: mockttp.Mockttp;\n #openRequests: Record<string, mockttp.CompletedRequest> = {};\n requestResponsePairs = new Map<\n mockttp.CompletedRequest,\n mockttp.CompletedResponse\n >();\n validator: OpenApiProxyValidator;\n public forwardTo: { port: number } = { port: 0 };\n express: { server: Server | undefined } = { server: undefined };\n constructor() {\n this.server = mockttp.getLocal();\n this.validator = new OpenApiProxyValidator();\n }\n\n async setup() {\n await this.server.start();\n this.forwardTo.port = await getPort();\n this.server\n .forAnyRequest()\n .thenForwardTo(`http://localhost:${this.forwardTo.port}`);\n await this.server.on('request', request => {\n this.#openRequests[request.id] = request;\n });\n await this.server.on('response', response => {\n const request = this.#openRequests[response.id];\n if (request) {\n this.requestResponsePairs.set(request, response);\n }\n delete this.#openRequests[response.id];\n this.validator.validate(request, response);\n });\n }\n\n async initialize(url: string, server: Server) {\n await this.validator.initialize(`${url}/openapi.json`);\n this.express.server = server;\n }\n\n async stop() {\n if (Object.keys(this.#openRequests).length > 0) {\n throw new Error('There are still open requests');\n }\n if (!this.express.server) {\n throw new Error(\n 'Proxy server was not initialized with an express server',\n );\n }\n const results = await Promise.allSettled([\n this.server.stop(),\n\n // If this isn't expressly closed, it will cause a jest memory leak warning.\n new Promise((resolve, reject) => {\n this.express.server?.close(err => {\n if (err) {\n reject(err);\n } else {\n resolve(undefined);\n }\n });\n }),\n ]);\n if (results.some(result => result.status === 'rejected')) {\n const errors = results\n .filter(\n (result): result is PromiseRejectedResult =>\n result.status === 'rejected',\n )\n .map(result => result.reason);\n throw new Error(\n `Failed to stop proxy server: ${errors.map(e => e.message).join(', ')}`,\n );\n }\n }\n\n get url() {\n return this.server.proxyEnv.HTTP_PROXY;\n }\n}\n"],"names":["mockttp","OpenApiProxyValidator","getPort"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqBO,MAAM,KAAA,CAAM;AAAA,EACjB,MAAA;AAAA,EACA,gBAA0D,EAAC;AAAA,EAC3D,oBAAA,uBAA2B,GAAA,EAGzB;AAAA,EACF,SAAA;AAAA,EACO,SAAA,GAA8B,EAAE,IAAA,EAAM,CAAA,EAAE;AAAA,EAC/C,OAAA,GAA0C,EAAE,MAAA,EAAQ,MAAA,EAAU;AAAA,EAC9D,WAAA,GAAc;AACZ,IAAA,IAAA,CAAK,MAAA,GAASA,mBAAQ,QAAA,EAAS;AAC/B,IAAA,IAAA,CAAK,SAAA,GAAY,IAAIC,gCAAA,EAAsB;AAAA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,10 @@
|
|
|
2
2
|
|
|
3
3
|
var setup = require('./proxy/setup.cjs.js');
|
|
4
4
|
|
|
5
|
-
const proxiesToCleanup =
|
|
5
|
+
const proxiesToCleanup = /* @__PURE__ */ new Set();
|
|
6
6
|
async function wrapServer(app) {
|
|
7
7
|
const proxy = new setup.Proxy();
|
|
8
|
-
proxiesToCleanup.
|
|
8
|
+
proxiesToCleanup.add(proxy);
|
|
9
9
|
await proxy.setup();
|
|
10
10
|
const server = app.listen(proxy.forwardTo.port);
|
|
11
11
|
await proxy.initialize(`http://localhost:${proxy.forwardTo.port}`, server);
|
|
@@ -20,10 +20,12 @@ function registerHooks() {
|
|
|
20
20
|
return;
|
|
21
21
|
}
|
|
22
22
|
registered = true;
|
|
23
|
-
afterAll(() => {
|
|
24
|
-
|
|
25
|
-
proxy.stop()
|
|
26
|
-
|
|
23
|
+
afterAll(async () => {
|
|
24
|
+
const stopPromises = Array.from(proxiesToCleanup).map(
|
|
25
|
+
(proxy) => proxy.stop()
|
|
26
|
+
);
|
|
27
|
+
await Promise.allSettled(stopPromises);
|
|
28
|
+
proxiesToCleanup.clear();
|
|
27
29
|
});
|
|
28
30
|
}
|
|
29
31
|
registerHooks();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"testUtils.cjs.js","sources":["../src/testUtils.ts"],"sourcesContent":["/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { Express } from 'express';\nimport { Server } from 'node:http';\nimport { Proxy } from './proxy/setup';\n\nconst proxiesToCleanup: Proxy
|
|
1
|
+
{"version":3,"file":"testUtils.cjs.js","sources":["../src/testUtils.ts"],"sourcesContent":["/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { Express } from 'express';\nimport { Server } from 'node:http';\nimport { Proxy } from './proxy/setup';\n\nconst proxiesToCleanup: Set<Proxy> = new Set();\n\n/**\n * !!! THIS CURRENTLY ONLY SUPPORTS SUPERTEST !!!\n * Setup a server with a custom OpenAPI proxy. This proxy will capture all requests and responses and make sure they\n * conform to the spec.\n * @param app - express server, needed to ensure we have the correct ports for the proxy.\n * @returns - a configured HTTP server that should be used with supertest.\n * @public\n */\nexport async function wrapServer(app: Express): Promise<Server> {\n const proxy = new Proxy();\n proxiesToCleanup.add(proxy);\n await proxy.setup();\n\n const server = app.listen(proxy.forwardTo.port);\n await proxy.initialize(`http://localhost:${proxy.forwardTo.port}`, server);\n\n return { ...server, address: () => new URL(proxy.url) } as any;\n}\n\nlet registered = false;\nfunction registerHooks() {\n if (typeof afterAll !== 'function' || typeof beforeAll !== 'function') {\n return;\n }\n if (registered) {\n return;\n }\n registered = true;\n\n afterAll(async () => {\n const stopPromises = Array.from(proxiesToCleanup).map(proxy =>\n proxy.stop(),\n );\n await Promise.allSettled(stopPromises);\n proxiesToCleanup.clear();\n });\n}\n\nregisterHooks();\n\n/**\n * !!! THIS CURRENTLY ONLY SUPPORTS SUPERTEST !!!\n * Running against supertest, we need some way to hit the optic proxy. This ensures that\n * that happens at runtime when in the context of a `yarn optic capture` command.\n * @param app - Express router that would be passed to supertest's `request`.\n * @returns A wrapper around the express router (or the router untouched) that still works with supertest.\n * @public\n */\nexport const wrapInOpenApiTestServer = (app: Express): Server | Express => {\n if (process.env.OPTIC_PROXY) {\n const server = app.listen(+process.env.PORT!);\n return {\n ...server,\n address: () => new URL(process.env.OPTIC_PROXY!),\n } as any;\n }\n return app;\n};\n"],"names":["Proxy"],"mappings":";;;;AAmBA,MAAM,gBAAA,uBAAmC,GAAA,EAAI;AAU7C,eAAsB,WAAW,GAAA,EAA+B;AAC9D,EAAA,MAAM,KAAA,GAAQ,IAAIA,WAAA,EAAM;AACxB,EAAA,gBAAA,CAAiB,IAAI,KAAK,CAAA;AAC1B,EAAA,MAAM,MAAM,KAAA,EAAM;AAElB,EAAA,MAAM,MAAA,GAAS,GAAA,CAAI,MAAA,CAAO,KAAA,CAAM,UAAU,IAAI,CAAA;AAC9C,EAAA,MAAM,MAAM,UAAA,CAAW,CAAA,iBAAA,EAAoB,MAAM,SAAA,CAAU,IAAI,IAAI,MAAM,CAAA;AAEzE,EAAA,OAAO,EAAE,GAAG,MAAA,EAAQ,OAAA,EAAS,MAAM,IAAI,GAAA,CAAI,KAAA,CAAM,GAAG,CAAA,EAAE;AACxD;AAEA,IAAI,UAAA,GAAa,KAAA;AACjB,SAAS,aAAA,GAAgB;AACvB,EAAA,IAAI,OAAO,QAAA,KAAa,UAAA,IAAc,OAAO,cAAc,UAAA,EAAY;AACrE,IAAA;AAAA,EACF;AACA,EAAA,IAAI,UAAA,EAAY;AACd,IAAA;AAAA,EACF;AACA,EAAA,UAAA,GAAa,IAAA;AAEb,EAAA,QAAA,CAAS,YAAY;AACnB,IAAA,MAAM,YAAA,GAAe,KAAA,CAAM,IAAA,CAAK,gBAAgB,CAAA,CAAE,GAAA;AAAA,MAAI,CAAA,KAAA,KACpD,MAAM,IAAA;AAAK,KACb;AACA,IAAA,MAAM,OAAA,CAAQ,WAAW,YAAY,CAAA;AACrC,IAAA,gBAAA,CAAiB,KAAA,EAAM;AAAA,EACzB,CAAC,CAAA;AACH;AAEA,aAAA,EAAc;AAUP,MAAM,uBAAA,GAA0B,CAAC,GAAA,KAAmC;AACzE,EAAA,IAAI,OAAA,CAAQ,IAAI,WAAA,EAAa;AAC3B,IAAA,MAAM,SAAS,GAAA,CAAI,MAAA,CAAO,CAAC,OAAA,CAAQ,IAAI,IAAK,CAAA;AAC5C,IAAA,OAAO;AAAA,MACL,GAAG,MAAA;AAAA,MACH,SAAS,MAAM,IAAI,GAAA,CAAI,OAAA,CAAQ,IAAI,WAAY;AAAA,KACjD;AAAA,EACF;AACA,EAAA,OAAO,GAAA;AACT;;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@backstage/backend-openapi-utils",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.9-next.0",
|
|
4
4
|
"description": "OpenAPI typescript support.",
|
|
5
5
|
"backstage": {
|
|
6
6
|
"role": "node-library"
|
|
@@ -54,8 +54,8 @@
|
|
|
54
54
|
},
|
|
55
55
|
"dependencies": {
|
|
56
56
|
"@apidevtools/swagger-parser": "^10.1.0",
|
|
57
|
-
"@backstage/backend-plugin-api": "1.9.
|
|
58
|
-
"@backstage/errors": "1.3.
|
|
57
|
+
"@backstage/backend-plugin-api": "1.9.1-next.0",
|
|
58
|
+
"@backstage/errors": "1.3.1-next.0",
|
|
59
59
|
"@backstage/types": "1.2.2",
|
|
60
60
|
"@types/express": "^4.17.6",
|
|
61
61
|
"@types/express-serve-static-core": "^4.17.5",
|
|
@@ -71,8 +71,8 @@
|
|
|
71
71
|
"openapi3-ts": "^3.1.2"
|
|
72
72
|
},
|
|
73
73
|
"devDependencies": {
|
|
74
|
-
"@backstage/cli": "0.36.
|
|
75
|
-
"@backstage/test-utils": "1.7.
|
|
74
|
+
"@backstage/cli": "0.36.2-next.0",
|
|
75
|
+
"@backstage/test-utils": "1.7.18-next.0",
|
|
76
76
|
"msw": "^1.0.0",
|
|
77
77
|
"supertest": "^7.0.0"
|
|
78
78
|
}
|