Skip to content

Commit

Permalink
swarm support
Browse files Browse the repository at this point in the history
  • Loading branch information
alsakhaev committed Nov 6, 2019
1 parent 3317c3c commit eab2fda
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/background/moduleStorages/moduleStorage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Storage } from './storage';
import { HttpModuleStorage } from './httpModuleStorage';
import { SwarmModuleStorage } from './swarmModuleStorage';

export class StorageAggregator implements Storage {

Expand All @@ -13,9 +14,10 @@ export class StorageAggregator implements Storage {
private _chooseStorage(protocol: string): Storage {
switch (protocol) {
case "http:":
return new HttpModuleStorage();
case "https:":
return new HttpModuleStorage();
case "bzz:":
return new SwarmModuleStorage();
default:
throw new Error("Unsupported protocol");
}
Expand Down
15 changes: 15 additions & 0 deletions src/background/moduleStorages/swarmModuleStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Storage as ModuleStorage } from './storage';

export class SwarmModuleStorage implements ModuleStorage {
public async getResource(uri: string): Promise<ArrayBuffer> {
const response = await fetch("https://swarm-gateways.net/" + uri);

if (!response.ok) {
throw new Error(`HttpStorage can't load resource by URI ${uri}`);
}

const buffer = await response.arrayBuffer();

return buffer;
}
}
3 changes: 2 additions & 1 deletion src/background/registries/registryAggregator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Registry } from './registry';
import { DevRegistry } from './devRegistry';
import { TestRegistry } from './testRegistry';
import GlobalConfigService from '../services/globalConfigService';
import { gt } from 'semver';

export class RegistryAggregator implements Registry {
private _registries: Registry[] = [];
Expand All @@ -16,7 +17,7 @@ export class RegistryAggregator implements Registry {
registryVersions.forEach(v => !versions.includes(v) && versions.push(v));
}

return versions;
return versions.sort((a, b) => gt(a, b) ? 1 : -1);;
}

async resolveToUri(name: string, branch: string, version: string): Promise<string[]> {
Expand Down
4 changes: 2 additions & 2 deletions src/background/registries/testRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ export class TestRegistry implements Registry {
const response = await fetch(`${this.endpointUrl}/registry/get-versions?name=${name}&branch=${branch}`);
const json = await response.json();
const versions = json.data;
// ToDo: SORT
return versions;
}

public async resolveToUri(name: string, branch: string, version: string): Promise<string[]> {
const response = await fetch(`${this.endpointUrl}/registry/resolve-to-uri?name=${name}&branch=${branch}&version=${version}`);
const json = await response.json();
// ToDo: fix it
const uris = json.data.map(uri => `${this.endpointUrl}/storage/${uri}`);
const uris = json.data;
return uris;
}

Expand Down
1 change: 0 additions & 1 deletion src/background/utils/moduleManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export default class ModuleManager {
const manifestBufferArray = await this._storage.getResource(manfiestUri);
const manifestJson = String.fromCharCode.apply(null, new Uint8Array(manifestBufferArray));
const manifest: Manifest = JSON.parse(manifestJson);
manifest.dist = new URL(manifest.dist, manfiestUri).href; // ToDo: fix it?
return manifest;
}

Expand Down
2 changes: 1 addition & 1 deletion src/inpage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { init } from './injector'

var observer = new MutationObserver(() => {
if (document.body) {
init().catch((err) => console.error("[Dapplet Extension] " + err));
init(); //.catch((err) => console.error("[Dapplet Extension] " + err));
observer.disconnect();
}
});
Expand Down

0 comments on commit eab2fda

Please sign in to comment.