Skip to content

Commit

Permalink
Copy assets from appropriate directory for kbn-monaco (#178669)
Browse files Browse the repository at this point in the history
## Summary

Closes #178448

Per the configuration in kibana, Monaco by default will attempt to load
the specific worker for each language using the public path record
defined on `window.__kbnPublicPath__` [see
here](https://github.com/elastic/kibana/blob/main/packages/kbn-monaco/src/register_globals.ts#L42)
but an error occurs whilst attempting to request the script at the url
we expect it to be at because it doesn't exist there, as seen here;
<img width="1071" alt="Screenshot 2024-03-13 at 17 21 36"
src="https://github.com/elastic/kibana/assets/7893459/012dd841-ba5c-4744-bb32-708bc6efc976">

Ideally this works but with the current setup for CDN assets it doesn't
map 1:1 to how the assets are expected, we should be copying from the
directory `target_workers` to match the same definition for [building
packages

](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/build_packages_task.ts#L111-L113),
similar to what we have
[here](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/create_cdn_assets_task.ts#L67-L74)
which is what this PR introduces.

The hypothesis above is verifiable by attempting to request the same
resource on the same build of the deployment where the error is
reported, with the `target_workers` path prepended, like so
https://kibana.estccdn.com/552e8adcca05/bundles/kbn-monaco/target_workers/json.editor.worker.js
we get the file.

<!-- ### Checklist

Delete any items that are not applicable to this PR.

- [ ] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)
- [ ]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [ ] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
- [ ] [Flaky Test
Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was
used on any tests changed
- [ ] Any UI touched in this PR is usable by keyboard only (learn more
about [keyboard accessibility](https://webaim.org/techniques/keyboard/))
- [ ] Any UI touched in this PR does not create any new axe failures
(run axe in browser:
[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),
[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))
- [ ] If a plugin configuration key changed, check if it needs to be
allowlisted in the cloud and added to the [docker
list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)
- [ ] This renders correctly on smaller devices using a responsive
layout. (You can test this [in your
browser](https://www.browserstack.com/guide/responsive-testing-on-local-server))
- [ ] This was checked for [cross-browser
compatibility](https://www.elastic.co/support/matrix#matrix_browsers)


### Risk Matrix

Delete this section if it is not applicable to this PR.

Before closing this PR, invite QA, stakeholders, and other developers to
identify risks that should be tested prior to the change/feature
release.

When forming the risk matrix, consider some of the following examples
and how they may potentially impact the change:

| Risk | Probability | Severity | Mitigation/Notes |

|---------------------------|-------------|----------|-------------------------|
| Multiple Spaces&mdash;unexpected behavior in non-default Kibana Space.
| Low | High | Integration tests will verify that all features are still
supported in non-default Kibana Space and when user switches between
spaces. |
| Multiple nodes&mdash;Elasticsearch polling might have race conditions
when multiple Kibana nodes are polling for the same tasks. | High | Low
| Tasks are idempotent, so executing them multiple times will not result
in logical error, but will degrade performance. To test for this case we
add plenty of unit tests around this logic and document manual testing
procedure. |
| Code should gracefully handle cases when feature X or plugin Y are
disabled. | Medium | High | Unit tests will verify that any feature flag
or plugin combination still results in our service operational. |
| [See more potential risk
examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) |


### For maintainers

- [ ] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)
-->

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
eokoneyo and kibanamachine authored Mar 21, 2024
1 parent fa9c0dc commit bb54184
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 1 deletion.
25 changes: 25 additions & 0 deletions NOTICE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
Kibana source code with Kibana X-Pack source code
Copyright 2012-2024 Elasticsearch B.V.

---
Adapted from remote-web-worker, which was available under a "MIT" license.

MIT License (MIT)

Copyright (c) 2022 Jan Nicklas

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

---
Pretty handling of logarithmic axes.
Copyright (c) 2007-2014 IOLA and Ole Laursen.
Expand Down
15 changes: 15 additions & 0 deletions packages/kbn-test/src/jest/setup/polyfills.jsdom.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,18 @@ global.Blob = require('blob-polyfill').Blob;
if (!global.hasOwnProperty('ResizeObserver')) {
global.ResizeObserver = require('resize-observer-polyfill');
}

if (!global.hasOwnProperty('Worker')) {
class Worker {
constructor(stringUrl) {
this.url = stringUrl;
this.onmessage = () => {};
}

postMessage(msg) {
this.onmessage(msg);
}
}

global.Worker = Worker;
}
82 changes: 82 additions & 0 deletions packages/kbn-ui-shared-deps-src/src/polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,85 @@ if (typeof window.Event === 'object') {

require('whatwg-fetch');
require('symbol-observable');

/* @notice
*
* Adapted from remote-web-worker, which was available under a "MIT" license.
*
* MIT License (MIT)
*
* Copyright (c) 2022 Jan Nicklas
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
typeof window !== 'undefined' &&
// eslint-disable-next-line no-global-assign
(Worker = ((BaseWorker) =>
class Worker extends BaseWorker {
constructor(url, options) {
let scriptUrl = String(url);
let objectURLRef;

try {
const isCrossOrigin = (URLObject) =>
URLObject.protocol === 'https:' && URLObject.origin !== window.location.origin;

scriptUrl = isCrossOrigin(new URL(scriptUrl)) // to bootstrap the actual script to work around the same origin policy.
? (objectURLRef = URL.createObjectURL(
new Blob(
[
// Replace the `importScripts` function with
// a patched version that will resolve relative URLs
// to the remote script URL.
//
// Without a patched `importScripts` Webpack 5 generated worker chunks will fail with the following error:
//
// Uncaught (in promise) DOMException: Failed to execute 'importScripts' on 'WorkerGlobalScope':
// The script at 'http://some.domain/worker.1e0e1e0e.js' failed to load.
//
// For minification, the inlined variable names are single letters:
// i = original importScripts
// a = arguments
// u = URL
`importScripts=((i)=>(...a)=>i(...a.map((u)=>''+new URL(u,"${scriptUrl}"))))(importScripts);importScripts("${scriptUrl}");`,
],
{
type: 'text/javascript',
}
)
))
: scriptUrl;
} catch {
// provided url doesn't match the expectation for URL constructor, it will be used as is
}

super(scriptUrl, options);

this.addEventListener(
'message',
function revokeURL() {
if (objectURLRef) {
URL.revokeObjectURL(objectURLRef);
objectURLRef = null;
}
},
{ once: true }
);
}
})(Worker));
5 changes: 4 additions & 1 deletion src/dev/build/tasks/create_cdn_assets_task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ export const CreateCdnAssets: Task = {
resolve(buildSource, 'node_modules/@kbn/core/target/public'),
resolve(bundles, 'core')
);
await copyAll(resolve(buildSource, 'node_modules/@kbn/monaco'), resolve(bundles, 'kbn-monaco'));
await copyAll(
resolve(buildSource, 'node_modules/@kbn/monaco/target_workers'),
resolve(bundles, 'kbn-monaco')
);

// packages/core/apps/core-apps-server-internal/src/core_app.ts
await copyAll(
Expand Down

0 comments on commit bb54184

Please sign in to comment.