Strict mode

merge-requests/3/merge
Nato Boram 2020-07-08 03:42:42 -04:00
parent 2799e4135b
commit fe7308e6b7
No known key found for this signature in database
GPG Key ID: 478E3C64BF88AFFA
6 changed files with 49 additions and 32 deletions

View File

@ -8,6 +8,9 @@
"schematics": { "schematics": {
"@schematics/angular:component": { "@schematics/angular:component": {
"style": "scss" "style": "scss"
},
"@schematics/angular:application": {
"strict": true
} }
}, },
"root": "", "root": "",

View File

@ -15,6 +15,7 @@
"publish:ipfs": "yarn run build:ipfs && ipfs add --recursive --chunker=buzhash --cid-version=1 dist/angular" "publish:ipfs": "yarn run build:ipfs && ipfs add --recursive --chunker=buzhash --cid-version=1 dist/angular"
}, },
"private": false, "private": false,
"sideEffects": false,
"dependencies": { "dependencies": {
"@angular/animations": "~10.0.2", "@angular/animations": "~10.0.2",
"@angular/cdk": "~10.0.1", "@angular/cdk": "~10.0.1",

View File

@ -0,0 +1,4 @@
export enum Protocol {
IPFS = 'ipfs',
IPNS = 'ipns',
}

View File

@ -2,6 +2,7 @@ import { HttpErrorResponse } from '@angular/common/http';
import { Component, OnInit, ViewChild } from '@angular/core'; import { Component, OnInit, ViewChild } from '@angular/core';
import { MatTable, MatTableDataSource } from '@angular/material/table'; import { MatTable, MatTableDataSource } from '@angular/material/table';
import { Subscription } from 'rxjs'; import { Subscription } from 'rxjs';
import { Protocol } from '../enums/protocol.enum';
import { GatewayService } from '../services/gateway.service'; import { GatewayService } from '../services/gateway.service';
@Component({ @Component({
@ -11,65 +12,63 @@ import { GatewayService } from '../services/gateway.service';
}) })
export class PagesComponent implements OnInit { export class PagesComponent implements OnInit {
gateways: string[]; @ViewChild(MatTable) matTable!: MatTable<Result>;
ipfs: string;
ipns: string;
dataSource: MatTableDataSource<Result>;
displayedColumns = [
'error',
'gateway',
];
subscriptions: Subscription[] = [];
@ViewChild(MatTable) matTable: MatTable<Result>; gateways!: string[];
ipfs = '';
ipns = '';
readonly dataSource = new MatTableDataSource<Result>([]);
readonly displayedColumns: ['error', 'gateway'] = ['error', 'gateway'];
readonly subscriptions: Subscription[] = [];
constructor( constructor(
private readonly gatewayService: GatewayService private readonly gatewayService: GatewayService
) { } ) { }
ngOnInit(): void { ngOnInit(): void {
this.dataSource = new MatTableDataSource([]);
this.gatewayService.list().subscribe((gateways): void => { this.gateways = gateways; }); this.gatewayService.list().subscribe((gateways): void => { this.gateways = gateways; });
} }
cacheIPFS(): void { cacheIPFS(): void {
this.cache('ipfs', this.ipfs); this.cache(Protocol.IPFS, this.ipfs);
} }
cacheIPNS(): void { cacheIPNS(): void {
this.cache('ipns', this.ipns); this.cache(Protocol.IPNS, this.ipns);
} }
cache(type: string, hash: string): void { cache(protocol: Protocol, hashpath: string): void {
// Clear subscriptions
while (this.subscriptions.length) { while (this.subscriptions.length) {
const sub = this.subscriptions.pop(); const sub = this.subscriptions.pop();
if (!sub.closed) { if (sub && !sub.closed) {
sub.unsubscribe(); sub.unsubscribe();
} }
} }
// Clear table
this.dataSource.data = []; this.dataSource.data = [];
this.matTable.renderRows(); this.matTable.renderRows();
console.clear(); console.clear();
this.gateways.forEach((gateway): void => { this.gateways.forEach((gateway): void => {
this.subscriptions.push( this.subscriptions.push(
this.gatewayService.get(gateway, type, hash).subscribe((): void => { this.gatewayService.get(gateway, protocol, hashpath).subscribe((): void => {
this.dataSource.data.push({ gateway: this.gatewayService.url(gateway, type, hash), error: null }); this.dataSource.data.push({ gateway: this.gatewayService.url(gateway, protocol, hashpath), error: null });
this.matTable.renderRows(); this.matTable.renderRows();
}, (error: HttpErrorResponse): void => { }, (error: HttpErrorResponse): void => {
this.dataSource.data.push({ gateway: this.gatewayService.url(gateway, type, hash), error }); this.dataSource.data.push({ gateway: this.gatewayService.url(gateway, protocol, hashpath), error });
this.matTable.renderRows(); this.matTable.renderRows();
}) })
); );
}); });
} }
} }
interface Result { interface Result {
gateway: string; gateway: string;
error: HttpErrorResponse; error: HttpErrorResponse | null;
} }

View File

@ -2,6 +2,7 @@ import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { environment } from '../../environments/environment'; import { environment } from '../../environments/environment';
import { Protocol } from '../enums/protocol.enum';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@ -13,22 +14,27 @@ export class GatewayService {
) { } ) { }
list(): Observable<string[]> { list(): Observable<string[]> {
return this.http.get<string[]>( if (environment && environment.base_href !== '/') {
environment.base_href && environment.base_href !== '/' return this.http.get<string[]>(`${environment.base_href}/assets/json/gateways.json`);
? `${environment.base_href}/assets/json/gateways.json`
: `${document.querySelector('base').href}/assets/json/gateways.json`
);
} }
get(gateway: string, type: string, hash: string): Observable<Blob> { const base = document.querySelector('base');
return this.http.get<Blob>(`${this.url(gateway, type, hash)}#x-ipfs-companion-no-redirect`, { if (base) {
return this.http.get<string[]>(`${base.href}/assets/json/gateways.json`);
}
throw new Error('Couldn\'t find environment nor base.')
}
get(gateway: string, protocol: Protocol, hashpath: string): Observable<Blob> {
return this.http.get<Blob>(`${this.url(gateway, protocol, hashpath)}#x-ipfs-companion-no-redirect`, {
responseType: 'blob' as 'json' responseType: 'blob' as 'json'
}); });
} }
url(gateway: string, type: string, hashpath: string): string { url(gateway: string, protocol: Protocol, hashpath: string): string {
const splits = hashpath.split('/'); const splits: string[] = hashpath.split('/');
const url = gateway.replace(':type', type).replace(':hash', splits.shift()); const url: string = gateway.replace(':type', protocol).replace(':hash', splits.shift() || '');
return splits.length ? [url, splits.join('/')].join('/') : url; return splits.length ? [url, splits.join('/')].join('/') : url;
} }

View File

@ -17,10 +17,14 @@
"lib": [ "lib": [
"es2018", "es2018",
"dom" "dom"
] ],
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"strict": true
}, },
"angularCompilerOptions": { "angularCompilerOptions": {
"fullTemplateTypeCheck": true, "fullTemplateTypeCheck": true,
"strictInjectionParameters": true "strictInjectionParameters": true,
"strictTemplates": true
} }
} }