Skip to content

Commit

Permalink
feat(api-server): allow CORS requests (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
alimd authored Nov 7, 2024
2 parents 1dd092f + 78b89b2 commit 236361c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
32 changes: 30 additions & 2 deletions packages/api-server/src/api-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ export class NanotronApiServer {
this.httpServer.on('clientError', this.handleClientError_);

if (this.config_.healthRoute) {
this._defineHealthRoute();
this.defineHealthRoute_();
}

if (this.config_.allowAllOrigin === true) {
this.defineCorsRoute_();
}
}

Expand Down Expand Up @@ -253,6 +257,10 @@ export class NanotronApiServer {

const connection = new NanotronClientRequest(url, nativeClientRequest, nativeServerResponse, routeOption);

if (this.config_.allowAllOrigin === true) {
connection.serverResponse.headers['access-control-allow-origin'] = '*';
}

if (routeOption === null) {
connection.serverResponse.statusCode = HttpStatusCodes.Error_Client_404_Not_Found;
connection.serverResponse.replyError();
Expand Down Expand Up @@ -284,7 +292,9 @@ export class NanotronApiServer {
// TODO: handled open remained connections.
}

protected _defineHealthRoute(): void {
protected defineHealthRoute_(): void {
this.logger_.logMethod?.('defineHealthRoute_');

this.defineRoute({
method: 'GET',
url: '/health',
Expand All @@ -297,4 +307,22 @@ export class NanotronApiServer {
},
});
}

protected defineCorsRoute_(): void {
this.logger_.logMethod?.('defineCorsRoute_');

this.defineRoute({
method: 'OPTIONS',
matchType: 'startsWith',
url: '/',
handler: function () {
const res = this.serverResponse.raw_;
res.statusCode = HttpStatusCodes.Success_204_No_Content;
res.setHeader('access-control-allow-origin', '*');
res.setHeader('access-control-allow-methods', '*');
res.setHeader('access-control-allow-headers', '*');
res.end();
},
});
}
}
29 changes: 10 additions & 19 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2240,16 +2240,7 @@ __metadata:
languageName: node
linkType: hard

"@types/node@npm:*":
version: 22.8.6
resolution: "@types/node@npm:22.8.6"
dependencies:
undici-types: "npm:~6.19.8"
checksum: 10c0/d3a11f2549234a91a4c5d0ff35ab4bdcb7ba34db4d3f1d189be39b8bd41c19aac98d117150a95a9c5a9d45b1014135477ea240b2b8317c86ae3d3cf1c3b3f8f4
languageName: node
linkType: hard

"@types/node@npm:^22.9.0":
"@types/node@npm:*, @types/node@npm:^22.9.0":
version: 22.9.0
resolution: "@types/node@npm:22.9.0"
dependencies:
Expand Down Expand Up @@ -2943,9 +2934,9 @@ __metadata:
linkType: hard

"caniuse-lite@npm:^1.0.30001669":
version: 1.0.30001676
resolution: "caniuse-lite@npm:1.0.30001676"
checksum: 10c0/53d310d76b5282947c99638a65d7534ac28a80aae1920de085a616ec8ad603358fad67cebacfc0452b1efdea12cce24fd37a50a712d074986b4962110e87d82b
version: 1.0.30001678
resolution: "caniuse-lite@npm:1.0.30001678"
checksum: 10c0/3209cc0f0b9683514916bed676d8f7965cae7faccaccb90f97c11fbdee32cd3f2f3b9cfec388ef400476299c3dd496fb1f8734c31c6199c4799b42813391517f
languageName: node
linkType: hard

Expand Down Expand Up @@ -3299,13 +3290,13 @@ __metadata:
linkType: hard

"cross-spawn@npm:^7.0.0, cross-spawn@npm:^7.0.2, cross-spawn@npm:^7.0.3":
version: 7.0.3
resolution: "cross-spawn@npm:7.0.3"
version: 7.0.4
resolution: "cross-spawn@npm:7.0.4"
dependencies:
path-key: "npm:^3.1.0"
shebang-command: "npm:^2.0.0"
which: "npm:^2.0.1"
checksum: 10c0/5738c312387081c98d69c98e105b6327b069197f864a60593245d64c8089c8a0a744e16349281210d56835bb9274130d825a78b2ad6853ca13cfbeffc0c31750
checksum: 10c0/04f6c70dcbdd156f53073f13730f71160dabb91c8dfbdb24a873f4580ad7ca4b73c062ddfaaa2ba46d0dac433856d0cc0a07ff7173cd5404fefde952e87c9dbf
languageName: node
linkType: hard

Expand Down Expand Up @@ -3529,9 +3520,9 @@ __metadata:
linkType: hard

"electron-to-chromium@npm:^1.5.41":
version: 1.5.50
resolution: "electron-to-chromium@npm:1.5.50"
checksum: 10c0/8b77b18ae833bfe2173e346ac33b8d66b5b5acf0cf5de65df9799f4d482334c938aa0950e4d01391d5fab8994f46c0e9059f4517843e7b8d861f9b0c49eb4c5d
version: 1.5.52
resolution: "electron-to-chromium@npm:1.5.52"
checksum: 10c0/1c85a5710ad21758780b8e067d5f63ed00416dbe93f64bd8937dbfb4ed98cf93d80c471a30daed439cb91a00ff4942ea2628e00a69d56639cc7070e9e8ab2694
languageName: node
linkType: hard

Expand Down

0 comments on commit 236361c

Please sign in to comment.