Commit f07ccc07 authored by Fabien Amarger's avatar Fabien Amarger

feat: [resource] Remove remote/inline resources

All resources are now remote resources.
parent f11734693b0
Pipeline #13797 failed with stages
in 58 seconds
......@@ -19,15 +19,7 @@
******************************************************************************/
import * as chai from "chai";
import {
ViewRegistry,
ViewRegistrySourceKind,
ViewRegistrySourceRemote,
ViewDescriptor,
ViewResourceRemote,
ViewResourceLocation,
loadRegistry
} from "./view-def";
import { ViewRegistry, ViewDescriptor, loadRegistry } from "./view-def";
import mock from "xhr-mock";
import { fail } from "assert";
......@@ -42,11 +34,10 @@ describe("@logilab/libview/view-def", () => {
descriptors: {},
sources: [
{
kind: ViewRegistrySourceKind.remote,
name: "source 1",
uri: "http://example.com/source"
} as ViewRegistrySourceRemote
]
resource: { uri: "http://example.com/source" },
},
],
};
let descriptors: ViewDescriptor[] = [
{
......@@ -57,14 +48,13 @@ describe("@logilab/libview/view-def", () => {
resourceCss: [],
resourceJs: [],
resourceMain: {
location: ViewResourceLocation.remote,
uri: "http://example.com/view1"
} as ViewResourceRemote
}
uri: "http://example.com/view1",
},
},
];
mock.get("http://example.com/source", {
status: 200,
body: JSON.stringify(descriptors)
body: JSON.stringify(descriptors),
});
let target = await loadRegistry(registry);
expect(target.sources.length).equals(registry.sources.length);
......
......@@ -18,40 +18,12 @@
* with CubicWeb. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
/**
* The kind of location for the resource of a view
*/
export enum ViewResourceLocation {
inline = "inline",
remote = "remote"
}
/**
* The base interface for the resource of a view
*/
export interface ViewResource {
/**
* The kind of location of a resource for a view
*/
location: ViewResourceLocation;
}
/**
* An inlined resource for a view
*/
export interface ViewResourceInline extends ViewResource {
/**
* The content for the resource
*/
content: string;
}
/**
* A remote resource for a view
*/
export interface ViewResourceRemote extends ViewResource {
/**
* The URI to the resource's content
* The uri to the resource's content
*/
uri: string;
}
......@@ -97,46 +69,15 @@ export interface ViewDescriptors {
[id: string]: ViewDescriptor | undefined;
}
/**
* Kinds of sources for view descriptors for a registry
*/
export enum ViewRegistrySourceKind {
inline = "inline",
remote = "remote"
}
/**
* A source of view descriptors for a registry
*/
export interface ViewRegistrySource {
/**
* The kind of source
*/
kind: ViewRegistrySourceKind;
/**
* The name of the source
*/
name: string;
}
/**
* A source of view descriptors with the descriptors inlined within it
*/
export interface ViewRegistrySourceInline extends ViewRegistrySource {
/**
* The inlined descriptors
*/
descriptors: ViewDescriptor[];
}
/**
* A remote source of view descriptors
*/
export interface ViewRegistrySourceRemote extends ViewRegistrySource {
/**
* The uri to fetch the descriptors at
*/
uri: string;
resource: ViewResource;
}
/**
......@@ -146,50 +87,30 @@ export interface ViewRegistrySourceRemote extends ViewRegistrySource {
function fetchDescriptors(
source: ViewRegistrySource
): Promise<ViewDescriptor[]> {
if (source.kind === ViewRegistrySourceKind.inline) {
return new Promise<ViewDescriptor[]>(
(
resolve: (result: ViewDescriptor[]) => void,
reject: (reason: string) => void
) => {
resolve((source as ViewRegistrySourceInline).descriptors);
}
);
} else if (source.kind === ViewRegistrySourceKind.remote) {
return new Promise<ViewDescriptor[]>(
(
resolve: (result: ViewDescriptor[]) => void,
reject: (reason: string) => void
) => {
let xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState === 4) {
if (xmlHttp.status < 200 || xmlHttp.status >= 300) {
reject("HTTP error: " + xmlHttp.status);
return;
}
if (xmlHttp.responseText.length === 0) {
reject("Empty result");
return;
}
resolve(JSON.parse(xmlHttp.responseText));
return new Promise<ViewDescriptor[]>(
(
resolve: (result: ViewDescriptor[]) => void,
reject: (reason: string) => void
) => {
let xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState === 4) {
if (xmlHttp.status < 200 || xmlHttp.status >= 300) {
reject("HTTP error: " + xmlHttp.status);
return;
}
};
xmlHttp.open("GET", (source as ViewRegistrySourceRemote).uri, true);
xmlHttp.setRequestHeader("Accept", "application/json");
xmlHttp.send();
}
);
} else {
return new Promise<ViewDescriptor[]>(
(
resolve: (result: ViewDescriptor[]) => void,
reject: (reason: string) => void
) => {
reject("Invalid source");
}
);
}
if (xmlHttp.responseText.length === 0) {
reject("Empty result");
return;
}
resolve(JSON.parse(xmlHttp.responseText));
}
};
xmlHttp.open("GET", source.resource.uri, true);
xmlHttp.setRequestHeader("Accept", "application/json");
xmlHttp.send();
}
);
}
/**
......@@ -222,9 +143,9 @@ function loadDescriptors(
.catch((reason: string) => {
console.log(
"Failed to fetch descriptor from source " +
source.name +
": " +
reason
source.name +
": " +
reason
);
onPartFinished();
});
......@@ -292,50 +213,41 @@ export function getResourceContent(
): Promise<string> {
return new Promise<string>(
(resolve: (result: string) => void, reject: (reason: any) => void) => {
if (resource.location === ViewResourceLocation.inline) {
resolve((resource as ViewResourceInline).content);
} else if (resource.location === ViewResourceLocation.remote) {
let uri = (resource as ViewResourceRemote).uri;
if (cache.hasOwnProperty(uri)) {
resolve(cache[uri]);
return;
}
let xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState === 4) {
if (xmlHttp.status === 0 && uri.startsWith("http://")) {
// try https
getResourceContent(cache, {
location: ViewResourceLocation.remote,
uri: "https:" + uri.substring("http:".length)
} as ViewResourceRemote)
.then((result: string) => {
resolve(result);
})
.catch((reason: any) => {
reject("Failed to fetch: " + reason);
});
return;
}
if (xmlHttp.status < 200 || xmlHttp.status >= 300) {
reject("HTTP error: " + xmlHttp.status);
return;
}
cache[uri] = xmlHttp.responseText;
resolve(xmlHttp.responseText);
}
};
xmlHttp.open("GET", uri, true);
xmlHttp.setRequestHeader(
"Accept",
"text/plain, application/javascript, text/css"
);
xmlHttp.send();
} else {
reject(
"Cannot fetch content of resource with location: " + resource.location
);
let uri = resource.uri;
if (cache.hasOwnProperty(uri)) {
resolve(cache[uri]);
return;
}
let xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState === 4) {
if (xmlHttp.status === 0 && uri.startsWith("http://")) {
// try https
getResourceContent(cache, {
uri: resource.uri
})
.then((result: string) => {
resolve(result);
})
.catch((reason: any) => {
reject("Failed to fetch: " + reason);
});
return;
}
if (xmlHttp.status < 200 || xmlHttp.status >= 300) {
reject("HTTP error: " + xmlHttp.status);
return;
}
cache[uri] = xmlHttp.responseText;
resolve(xmlHttp.responseText);
}
};
xmlHttp.open("GET", uri, true);
xmlHttp.setRequestHeader(
"Accept",
"text/plain, application/javascript, text/css"
);
xmlHttp.send();
}
);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment