-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate /translations
route to core
#83280
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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. | ||
*/ | ||
|
||
import { IRouter } from '../../http'; | ||
import { registerTranslationsRoute } from './translations'; | ||
|
||
export const registerRoutes = ({ router, locale }: { router: IRouter; locale: string }) => { | ||
registerTranslationsRoute(router, locale); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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. | ||
*/ | ||
|
||
import { createHash } from 'crypto'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { schema } from '@kbn/config-schema'; | ||
import { IRouter } from '../../http'; | ||
|
||
interface TranslationCache { | ||
translations: string; | ||
hash: string; | ||
} | ||
|
||
export const registerTranslationsRoute = (router: IRouter, locale: string) => { | ||
let translationCache: TranslationCache; | ||
|
||
router.get( | ||
{ | ||
path: '/translations/{locale}.json', | ||
validate: { | ||
params: schema.object({ | ||
locale: schema.string(), | ||
}), | ||
}, | ||
options: { | ||
authRequired: false, | ||
}, | ||
}, | ||
(ctx, req, res) => { | ||
if (req.params.locale.toLowerCase() !== locale.toLowerCase()) { | ||
return res.notFound({ | ||
body: `Unknown locale: ${req.params.locale}`, | ||
}); | ||
} | ||
if (!translationCache) { | ||
const translations = JSON.stringify(i18n.getTranslation()); | ||
const hash = createHash('sha1').update(translations).digest('hex'); | ||
translationCache = { | ||
translations, | ||
hash, | ||
}; | ||
} | ||
return res.ok({ | ||
headers: { | ||
'content-type': 'application/json', | ||
'cache-control': 'must-revalidate', | ||
etag: translationCache.hash, | ||
}, | ||
body: translationCache.translations, | ||
}); | ||
} | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,9 +131,6 @@ export class Server { | |
await ensureValidConfiguration(this.configService, legacyConfigSetup); | ||
} | ||
|
||
// setup i18n prior to any other service, to have translations ready | ||
const i18nServiceSetup = await this.i18n.setup({ pluginPaths }); | ||
|
||
const contextServiceSetup = this.context.setup({ | ||
// We inject a fake "legacy plugin" with dependencies on every plugin so that legacy plugins: | ||
// 1) Can access context from any KP plugin | ||
|
@@ -149,6 +146,9 @@ export class Server { | |
context: contextServiceSetup, | ||
}); | ||
|
||
// setup i18n prior to any other service, to have translations ready | ||
const i18nServiceSetup = await this.i18n.setup({ http: httpSetup, pluginPaths }); | ||
Comment on lines
+149
to
+150
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Had to move this after the |
||
|
||
const capabilitiesSetup = this.capabilities.setup({ http: httpSetup }); | ||
|
||
const elasticsearchServiceSetup = await this.elasticsearch.setup({ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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. | ||
*/ | ||
import expect from '@kbn/expect'; | ||
import { FtrProviderContext } from '../../ftr_provider_context'; | ||
|
||
export default function ({ getService }: FtrProviderContext) { | ||
const supertest = getService('supertest'); | ||
|
||
describe('compression', () => { | ||
it(`uses compression when there isn't a referer`, async () => { | ||
Comment on lines
+22
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extracted / converted to TS from |
||
await supertest | ||
.get('/app/kibana') | ||
.set('accept-encoding', 'gzip') | ||
.then((response) => { | ||
expect(response.header).to.have.property('content-encoding', 'gzip'); | ||
}); | ||
}); | ||
|
||
it(`uses compression when there is a whitelisted referer`, async () => { | ||
await supertest | ||
.get('/app/kibana') | ||
.set('accept-encoding', 'gzip') | ||
.set('referer', 'https://some-host.com') | ||
.then((response) => { | ||
expect(response.header).to.have.property('content-encoding', 'gzip'); | ||
}); | ||
}); | ||
|
||
it(`doesn't use compression when there is a non-whitelisted referer`, async () => { | ||
await supertest | ||
.get('/app/kibana') | ||
.set('accept-encoding', 'gzip') | ||
.set('referer', 'https://other.some-host.com') | ||
.then((response) => { | ||
expect(response.header).not.to.have.property('content-encoding'); | ||
}); | ||
}); | ||
}); | ||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed with @restrry, we ideally should use
cache-control
instead ofetag
here in dist mode to avoid an unnecessary roundtrip. However as this requires some refactoring to be able to add a path prefix,suffix or query param to invalidate the request when the kibana version is bumped (as we do with static assets), it will be done at a later time. I will create an issue for that before the current PR got merged.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created #83409