-
Notifications
You must be signed in to change notification settings - Fork 57
Pre-fetching of the default editor CDN resources #1192
Changes from all commits
67568ba
270fd0e
aeffa37
67c2b9a
518620c
46b1c53
bf3e34a
c0e2c72
487247d
9a202a9
db32c3f
547d00f
3e0f807
b6a5c32
8594506
ded8b24
353045f
9a69f58
f4e2ec0
d57be5e
68e1099
da0589e
02a8a37
5f86c23
31a5699
9123db6
25337fa
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,155 @@ | ||
/* | ||
* Copyright (c) 2016-2018 Red Hat, Inc. | ||
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. minor: 2019 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. seems it's not supported by the license checker for now |
||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
'use strict'; | ||
import {CheBranding} from '../branding/che-branding.factory'; | ||
|
||
const IDE_FETCHER_CALLBACK_ID = 'cheIdeFetcherCallback'; | ||
|
||
/** | ||
* Provides a way to download IDE .js and then cache it before trying to load the IDE | ||
* @author Florent Benoit | ||
* @author Oleksii Orel | ||
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. you're now an author :-) 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. added |
||
* @author David Festal | ||
*/ | ||
export class CheIdeFetcher { | ||
|
||
static $inject = ['$log', '$http', '$window', 'cheBranding']; | ||
|
||
private $log: ng.ILogService; | ||
private $http: ng.IHttpService; | ||
private $window: ng.IWindowService; | ||
private cheBranding: CheBranding; | ||
private userAgent: string; | ||
|
||
/** | ||
* Default constructor that is using resource injection | ||
*/ | ||
constructor($log: ng.ILogService, $http: ng.IHttpService, $window: ng.IWindowService, cheBranding: CheBranding) { | ||
this.$log = $log; | ||
this.$http = $http; | ||
this.$window = $window; | ||
this.cheBranding = cheBranding; | ||
|
||
this.userAgent = this.getUserAgent(); | ||
|
||
const callback = () => { | ||
this.findMappingFile(); | ||
this.cheBranding.unregisterCallback(IDE_FETCHER_CALLBACK_ID); | ||
}; | ||
this.cheBranding.registerCallback(IDE_FETCHER_CALLBACK_ID, callback.bind(this)); | ||
} | ||
|
||
/** | ||
* Gets user agent. | ||
* @returns {string} | ||
*/ | ||
getUserAgent(): string { | ||
const userAgent = this.$window.navigator.userAgent.toLowerCase(); | ||
const docMode = (<any>this.$window.document).documentMode; | ||
|
||
if (userAgent.indexOf('webkit') !== -1) { | ||
return 'safari'; | ||
} else if (userAgent.indexOf('msie') !== -1) { | ||
if (docMode >= 10 && docMode < 11) { | ||
return 'ie10'; | ||
} else if (docMode >= 9 && docMode < 11) { | ||
return 'ie9'; | ||
} else if (docMode >= 8 && docMode < 11) { | ||
return 'ie8'; | ||
} | ||
} else if (userAgent.indexOf('gecko') !== -1) { | ||
return 'gecko1_8'; | ||
} | ||
|
||
return 'unknown'; | ||
} | ||
|
||
/** | ||
* Finds mapping file. | ||
*/ | ||
findMappingFile(): void { | ||
// get the content of the compilation mapping file | ||
const randVal = Math.floor((Math.random() * 1000000) + 1); | ||
const resourcesPath = this.cheBranding.getIdeResourcesPath(); | ||
if (!resourcesPath) { | ||
this.$log.log('Unable to get IDE resources path'); | ||
return; | ||
} | ||
|
||
const fileMappingUrl = `${resourcesPath}compilation-mappings.txt?uid=${randVal}`; | ||
|
||
this.$http.get(fileMappingUrl).then((response: {data: any}) => { | ||
if (!response || !response.data) { | ||
return; | ||
} | ||
let urlToLoad = this.getIdeUrlMappingFile(response.data); | ||
// load the url | ||
if (angular.isDefined(urlToLoad)) { | ||
this.$log.log('Preloading IDE javascript', urlToLoad); | ||
this.$http.get(urlToLoad, { cache: true}); | ||
} else { | ||
this.$log.error('Unable to find the IDE javascript file to cache'); | ||
} | ||
}, (error: any) => { | ||
this.$log.log('unable to find compilation mapping file', error); | ||
}); | ||
|
||
this.$http.get('/api/cdn-support/paths', {cache: false}).then((response: {data: any}) => { | ||
garagatyi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!response || !response.data) { | ||
return; | ||
} | ||
const data = response.data; | ||
data.forEach((entry) => { | ||
let urlToLoad = entry.cdn; | ||
// load the url | ||
if (angular.isDefined(urlToLoad)) { | ||
this.$log.log('Preloading Theia resource', urlToLoad); | ||
const link = document.createElement("link"); | ||
link.rel='prefetch'; | ||
link.href=urlToLoad; | ||
document.head.appendChild(link); | ||
} else { | ||
this.$log.error('Unable to find the Theia resource file to cache'); | ||
} | ||
}); | ||
}, (error: any) => { | ||
this.$log.log('Unable to find Theia CDN resources to cache', error); | ||
}); | ||
} | ||
|
||
/** | ||
* Gets URL of mapping file. | ||
* @param data {string} | ||
* @returns {string} | ||
*/ | ||
getIdeUrlMappingFile(data: string): string { | ||
let mappingFileUrl: string; | ||
let javascriptFileName: string; | ||
const mappings = data.split(new RegExp('^\\n', 'gm')); | ||
const isPasses = mappings.some((mapping: string) => { | ||
const subMappings = mapping.split('\n'); | ||
const userAgent = subMappings.find((subMapping: string) => { | ||
return subMapping.startsWith('user.agent '); | ||
}).split(' ')[1]; | ||
javascriptFileName = subMappings.find((subMapping: string) => { | ||
return subMapping.endsWith('.cache.js'); | ||
}); | ||
return javascriptFileName && userAgent && this.userAgent === userAgent; | ||
}); | ||
if (isPasses && javascriptFileName) { | ||
mappingFileUrl = this.cheBranding.getIdeResourcesPath() + javascriptFileName; | ||
} | ||
|
||
return mappingFileUrl; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
|
||
Copyright (c) 2016-2018 Red Hat, Inc. | ||
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. 2019 🎉 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. I fear it's not supported by the license checker for now |
||
This program and the accompanying materials are made | ||
available under the terms of the Eclipse Public License 2.0 | ||
which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
|
||
SPDX-License-Identifier: EPL-2.0 | ||
|
||
Contributors: | ||
Red Hat, Inc. - initial API and implementation | ||
|
||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<artifactId>fabric8-ide-plugins-parent</artifactId> | ||
<groupId>com.redhat.che</groupId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
<artifactId>fabric8-cdn-support</artifactId> | ||
<name>Fabric8 IDE :: Plugins :: CDN Support</name> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-core</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-databind</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.dataformat</groupId> | ||
<artifactId>jackson-dataformat-yaml</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.guava</groupId> | ||
<artifactId>guava</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.inject</groupId> | ||
<artifactId>guice</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.inject</groupId> | ||
<artifactId>javax.inject</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.ws.rs</groupId> | ||
<artifactId>javax.ws.rs-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.eclipse.che.core</groupId> | ||
<artifactId>che-core-api-core</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.eclipse.che.core</groupId> | ||
<artifactId>che-core-api-workspace</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.eclipse.che.core</groupId> | ||
<artifactId>che-core-api-workspace-shared</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.eclipse.che.core</groupId> | ||
<artifactId>che-core-commons-annotations</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.eclipse.che.core</groupId> | ||
<artifactId>che-core-commons-inject</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>javax.servlet-api</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>ch.qos.logback</groupId> | ||
<artifactId>logback-classic</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.eclipse.che.core</groupId> | ||
<artifactId>che-core-db</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.hamcrest</groupId> | ||
<artifactId>hamcrest-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-testng</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.testng</groupId> | ||
<artifactId>testng</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
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.
Could we include an example value here? I don't know offhand what the full identifier would look like.
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.
example added