remove js-ipfs in electron app, eslint to dev deps (#123)
- add ipfs:// protocol handlers, not enabled for now - use browser-side auto-js-ipfs for loading from ipfs even in electron app - deps: bump wabac.js to 2.13.0 - update CHANGES Co-authored-by: Ilya Kreymer <ikreymer@gmail.com>
This commit is contained in:
co-authored by
Ilya Kreymer
parent
43122425e5
commit
f1df02ea3c
@@ -0,0 +1,44 @@
|
||||
import { protocol, session } from "electron";
|
||||
|
||||
import fetchToHandler from "fetch-to-electron-protocol-handler";
|
||||
import makeIPFSFetch from "js-ipfs-fetch";
|
||||
import * as IPFS from "ipfs-core";
|
||||
|
||||
export const PROTOCOL_PERMISSIONS = {
|
||||
standard: true,
|
||||
secure: true,
|
||||
allowServiceWorkers: true,
|
||||
supportFetchAPI: true,
|
||||
bypassCSP: false,
|
||||
corsEnabled: true,
|
||||
stream: true
|
||||
};
|
||||
|
||||
export function registerPrivileges(privileges = PROTOCOL_PERMISSIONS) {
|
||||
protocol.registerSchemesAsPrivileged([
|
||||
{scheme: "ipfs", privileges},
|
||||
{scheme: "ipns", privileges},
|
||||
{scheme: "ipld", privileges}
|
||||
]);
|
||||
}
|
||||
|
||||
export function registerHandler(ipfsRepoPath) {
|
||||
const handler = createHandler(ipfsRepoPath);
|
||||
protocol.registerStreamProtocol("ipfs", handler);
|
||||
protocol.registerStreamProtocol("ipns", handler);
|
||||
protocol.registerStreamProtocol("ipld", handler);
|
||||
}
|
||||
|
||||
export function createHandler (ipfsRepoPath) {
|
||||
const {handler} = fetchToHandler(async () => {
|
||||
const ipfs = await IPFS.create({
|
||||
repo: ipfsRepoPath
|
||||
});
|
||||
|
||||
const fetch = await makeIPFSFetch({ ipfs });
|
||||
|
||||
return fetch;
|
||||
}, session.defaultSession);
|
||||
|
||||
return handler;
|
||||
}
|
||||
+13
-36
@@ -9,7 +9,7 @@ import path from "path";
|
||||
import fs from "fs";
|
||||
|
||||
import { ArchiveResponse, Rewriter } from "@webrecorder/wabac/src/rewrite";
|
||||
import { IPFSClient } from "@webrecorder/wabac/src/ipfs";
|
||||
import {create as createIPFS} from "auto-js-ipfs";
|
||||
|
||||
import { PassThrough, Readable } from "stream";
|
||||
|
||||
@@ -27,33 +27,6 @@ const REPLAY_PREFIX = STATIC_PREFIX + "w/";
|
||||
|
||||
const URL_RX = /([^/]+)\/([\d]+)(?:\w\w_)?\/(.*)$/;
|
||||
|
||||
|
||||
let IPFS = null;
|
||||
|
||||
// ============================================================================
|
||||
class NativeIPFSClient extends IPFSClient
|
||||
{
|
||||
constructor(repoPath) {
|
||||
super();
|
||||
this.repoPath = repoPath;
|
||||
}
|
||||
|
||||
async _doInitIPFS() {
|
||||
if (!IPFS) {
|
||||
IPFS = require("ipfs-core");
|
||||
}
|
||||
|
||||
this.ipfs = await IPFS.create({
|
||||
repo: this.repoPath,
|
||||
init: {emptyRepo: true},
|
||||
//preload: {enabled: false},
|
||||
});
|
||||
|
||||
this.resetGC();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ============================================================================
|
||||
class ElectronReplayApp
|
||||
{
|
||||
@@ -145,11 +118,6 @@ class ElectronReplayApp
|
||||
app.setPath("userData", path.join(app.getPath("appData"), this.profileName));
|
||||
}
|
||||
|
||||
const ipfsRepoPath = path.join(app.getPath("userData"), "js-ipfs");
|
||||
console.log("ipfs path", ipfsRepoPath);
|
||||
|
||||
this.ipfsClient = new NativeIPFSClient(ipfsRepoPath);
|
||||
|
||||
app.on("will-finish-launching", () => {
|
||||
app.on("open-file", (event, filePath) => {
|
||||
this.openNextFile = filePath;
|
||||
@@ -185,6 +153,9 @@ class ElectronReplayApp
|
||||
}
|
||||
|
||||
onAppReady() {
|
||||
const ipfsRepoPath = path.join(app.getPath("userData"), "js-ipfs");
|
||||
console.log("ipfs path", ipfsRepoPath);
|
||||
|
||||
this.checkUpdates();
|
||||
|
||||
this.screenSize = screen.getPrimaryDisplay().workAreaSize;
|
||||
@@ -293,13 +264,16 @@ class ElectronReplayApp
|
||||
return;
|
||||
|
||||
} else if (ipfsCID) {
|
||||
const ipfsURL = `ipfs://${ipfsCID}}`;
|
||||
|
||||
console.log("ipfs serve:", ipfsCID);
|
||||
|
||||
await this.ipfsClient.initIPFS();
|
||||
// TODO: Pass in config?
|
||||
this.ipfsClient = await createIPFS();
|
||||
|
||||
console.log("inited");
|
||||
|
||||
let size = await this.ipfsClient.getFileSize(ipfsCID);
|
||||
let size = await this.ipfsClient.getSize(ipfsURL);
|
||||
|
||||
console.log("got size", size);
|
||||
|
||||
@@ -314,7 +288,10 @@ class ElectronReplayApp
|
||||
if (request.method === "GET") {
|
||||
const offset = start || 0;
|
||||
const length = end ? end - start + 1 : size;
|
||||
data = Readable.from(await this.ipfsClient.cat(ipfsCID, {offset, length}));
|
||||
data = Readable.from(await this.ipfsClient.get(ipfsURL, {
|
||||
start: offset,
|
||||
end: offset +length
|
||||
}));
|
||||
}
|
||||
|
||||
callback({statusCode, headers, data});
|
||||
|
||||
+1
-11
@@ -1,5 +1,5 @@
|
||||
import { LitElement, html, css } from "lit";
|
||||
import { wrapCss, IS_APP } from "./misc";
|
||||
import { wrapCss } from "./misc";
|
||||
|
||||
import prettyBytes from "pretty-bytes";
|
||||
|
||||
@@ -163,16 +163,6 @@ You can select a file to upload from the main page by clicking the 'Choose File.
|
||||
source = this.loadInfo;
|
||||
break;
|
||||
|
||||
case "ipfs":
|
||||
if (IS_APP) {
|
||||
// eslint-disable-next-line no-undef
|
||||
const url = new URL(__APP_FILE_SERVE_PREFIX__);
|
||||
const hash = sourceUrl.split("#", 1)[0];
|
||||
url.searchParams.set("ipfs", hash.slice("ipfs://".length));
|
||||
source = {sourceUrl, loadUrl: url.href};
|
||||
}
|
||||
break;
|
||||
|
||||
case "proxy":
|
||||
sourceUrl = "proxy:" + sourceUrl.slice("proxy://".length);
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user