app: ipfs loading improvements:

use IPFSClient from wabac.js
app: remove # from ipfs paths sent to app
preload interface made more extensible
This commit is contained in:
Ilya Kreymer
2020-12-09 22:46:16 -08:00
parent dfdebe9f69
commit cd40979625
7 changed files with 72 additions and 78 deletions
+1
View File
@@ -2,6 +2,7 @@
v1.3.3 v1.3.3
- UI: Component customization improvements, update to latest wabac.js - UI: Component customization improvements, update to latest wabac.js
- Loading: Improvements for IPFS loading, fix loading with hashtag #filename= in Electron app
v1.3.2 v1.3.2
- UI: Show navbar on all pages, include breadcrumbs with collection title current page - UI: Show navbar on all pages, include breadcrumbs with collection title current page
+10 -4
View File
@@ -10,18 +10,22 @@ const dbs = {};
const loader = new CollectionLoader(); const loader = new CollectionLoader();
async function getDB(name) { async function getColl(name) {
if (!dbs[name]) { if (!dbs[name]) {
dbs[name] = await loader.loadColl(name); dbs[name] = await loader.loadColl(name);
console.log(dbs[name]); await dbs[name].initing;
} }
return dbs[name].store; return dbs[name];
}
async function getDB(name) {
const coll = await getColl(name);
return coll.store;
} }
async function getResponse(event, request, coll, ts, channel) { async function getResponse(event, request, coll, ts, channel) {
const db = await getDB(coll); const db = await getDB(coll);
await db.initing;
const req = {request, url: request.url, timestamp: ts}; const req = {request, url: request.url, timestamp: ts};
@@ -41,6 +45,8 @@ async function getResponse(event, request, coll, ts, channel) {
ipcRenderer.on('getresponse', getResponse); ipcRenderer.on('getresponse', getResponse);
export { getDB, getColl, loader };
+27 -42
View File
@@ -7,6 +7,7 @@ import path from 'path';
import fs from 'fs'; import fs from 'fs';
import { ArchiveResponse, Rewriter } from '@webrecorder/wabac/src/rewrite'; import { ArchiveResponse, Rewriter } from '@webrecorder/wabac/src/rewrite';
import { IPFSClient } from '@webrecorder/wabac/src/ipfs';
import { PassThrough, Readable } from 'stream'; import { PassThrough, Readable } from 'stream';
@@ -21,9 +22,26 @@ const REPLAY_PREFIX = STATIC_PREFIX + "wabac/";
const URL_RX = /([^\/]+)\/([\d]+)(?:\w\w_)?\/(.*)$/; const URL_RX = /([^\/]+)\/([\d]+)(?:\w\w_)?\/(.*)$/;
let IPFS = null; let IPFS = null;
let ipfs = null;
let initingIPFS = null; // ============================================================================
class NativeIPFSClient extends IPFSClient
{
async _doInitIPFS() {
if (!IPFS) {
IPFS = require("ipfs-core");
}
this.ipfs = await IPFS.create({
//repo: "/tmp/test-ipfs"
init: {emptyRepo: true},
//preload: {enabled: false},
});
this.resetGC();
}
}
// ============================================================================ // ============================================================================
@@ -48,6 +66,8 @@ class ElectronReplayApp
this.openNextFile = null; this.openNextFile = null;
this.screenSize = {width: 1024, height: 768}; this.screenSize = {width: 1024, height: 768};
this.ipfsClient = new NativeIPFSClient();
} }
get mainWindowWebPreferences() { get mainWindowWebPreferences() {
@@ -138,38 +158,7 @@ class ElectronReplayApp
}); });
} }
static async doInitIPFS() {
if (!IPFS) {
IPFS = require("ipfs-core");
}
ipfs = await IPFS.create({
//repo: "/tmp/test-ipfs"
init: {emptyRepo: true},
//preload: {enabled: false},
});
}
async initIPFS() {
if (!ipfs) {
try {
if (!initingIPFS) {
initingIPFS = ElectronReplayApp.doInitIPFS();
}
await initingIPFS;
} catch (e) {
console.warn(e);
}
}
return ipfs;
}
onAppReady() { onAppReady() {
//ElectronReplayApp.doInitIPFS();
this.screenSize = screen.getPrimaryDisplay().workAreaSize; this.screenSize = screen.getPrimaryDisplay().workAreaSize;
app.on('web-contents-created', (event, contents) => { app.on('web-contents-created', (event, contents) => {
@@ -276,16 +265,12 @@ class ElectronReplayApp
} else if (ipfsCID) { } else if (ipfsCID) {
console.log("ipfs serve:", ipfsCID); console.log("ipfs serve:", ipfsCID);
const ipfs = await this.initIPFS(); await this.ipfsClient.initIPFS();
let size = 0; let size = await this.ipfsClient.getFileSize(ipfsCID);
for await (const file of ipfs.get(ipfsCID, {timeout: 20000, preload: false})) { if (size === null) {
if (file.type !== "file") { return this.notFound(ipfsCID, callback);
return this.notFound(ipfsCID, callback);
}
size = file.size;
break;
} }
const {statusCode, start, end} = this.parseRange(reqHeaders, headers, size); const {statusCode, start, end} = this.parseRange(reqHeaders, headers, size);
@@ -295,7 +280,7 @@ class ElectronReplayApp
if (request.method === "GET") { if (request.method === "GET") {
const offset = start || 0; const offset = start || 0;
const length = end ? end - start + 1 : size; const length = end ? end - start + 1 : size;
data = Readable.from(ipfs.cat(ipfsCID, {offset, length})); data = Readable.from(this.ipfsClient.cat(ipfsCID, {offset, length}));
} }
callback({statusCode, headers, data}); callback({statusCode, headers, data});
+2 -1
View File
@@ -134,7 +134,8 @@ You can select a file to upload from the main page by clicking the \'Choose File
case "ipfs": case "ipfs":
if (IS_APP) { if (IS_APP) {
const url = new URL(__APP_FILE_SERVE_PREFIX__); const url = new URL(__APP_FILE_SERVE_PREFIX__);
url.searchParams.set("ipfs", sourceUrl.slice("ipfs://".length)); const hash = sourceUrl.split("#", 1)[0];
url.searchParams.set("ipfs", hash.slice("ipfs://".length));
source = {sourceUrl, loadUrl: url.href}; source = {sourceUrl, loadUrl: url.href};
} }
break; break;
+17 -17
View File
File diff suppressed because one or more lines are too long
+12 -12
View File
File diff suppressed because one or more lines are too long
+3 -2
View File
@@ -47,7 +47,8 @@ const electronMainConfig = (env, argv) => {
plugins: [ plugins: [
new webpack.DefinePlugin({ new webpack.DefinePlugin({
__APP_FILE_SERVE_PREFIX__ : JSON.stringify(APP_FILE_SERVE_PREFIX), __APP_FILE_SERVE_PREFIX__ : JSON.stringify(APP_FILE_SERVE_PREFIX),
__HELPER_PROXY__ : JSON.stringify(HELPER_PROXY) __HELPER_PROXY__ : JSON.stringify(HELPER_PROXY),
__IPFS_CORE_URL__: JSON.stringify("")
}), }),
new webpack.BannerPlugin(BANNER_TEXT), new webpack.BannerPlugin(BANNER_TEXT),
new CopyPlugin({ new CopyPlugin({
@@ -78,7 +79,7 @@ const electronPreloadConfig = (env, argv) => {
// this needs to be defined, but not actually used, as electron app uses // this needs to be defined, but not actually used, as electron app uses
// ipfs-core from node // ipfs-core from node
new webpack.DefinePlugin({ new webpack.DefinePlugin({
__IPFS_CORE_URL__: JSON.stringify(IPFS_CORE_URL) __IPFS_CORE_URL__: JSON.stringify("")
}), }),
] ]
} }