Skip to content

Commit

Permalink
[update] run ipfs daemon in stellar-runtime
Browse files Browse the repository at this point in the history
- spawn `jsipfs daemon` as child_process
    - signal 11 failure when run jsipfs node as nwjs-0.48.1 nodejs context
    - jsipfs module does not expose http-api server launcher
        - http-api server implementation(src/http) for jsipfs command only
- NOTE: cannot access localhost with IpfsHttpClient from nwjs
    - unknown reason
  • Loading branch information
bellbind committed Sep 7, 2020
1 parent 504b15e commit ab4719e
Show file tree
Hide file tree
Showing 10 changed files with 1,145 additions and 1,383 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# added for this project
repo-*/

# Logs
logs
*.log
Expand Down
11 changes: 7 additions & 4 deletions modules/boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ win.showDevTools();

// NOTE: require's path is relative from package.json, not this file
const server = window.nw.require("./nodejs/server.cjs");
//const server = window.nw.require("./nodejs/spawn.cjs"); //WORKAROUND: 0.47.3
//const server = window.nw.require("./nodejs/spawn-server.cjs"); //WORKAROUND: 0.47.3
//const ipfs = window.nw.require("./nodejs/ipfs.cjs"); // SIGNAL 11 on 0.48.1
const ipfs = window.nw.require("./nodejs/spawn-ipfs.cjs"); //WORKAROUND: 0.48.1

const createSandboxView = (state, sandbox, url = "") => {
const view = document.createElement("div");
Expand Down Expand Up @@ -82,6 +84,8 @@ const createSandboxView = (state, sandbox, url = "") => {

const main = async () => {
await server.start(info.serverPort);
info.ipfs = await ipfs.start("./repo-ipfs");
console.log("IPFS info", JSON.stringify(info.ipfs));
const state = {
sandboxes: [],
storage: await Storage.open(),
Expand All @@ -91,16 +95,15 @@ const main = async () => {
(async () => {
try {
await Promise.all(state.sandboxes.map(sandbox => sandbox.stop()));
await server.stop();
await Promise.all([ipfs.stop(), server.stop()]);
win.close(true);
} catch (error) {
console.error(error);
//win.close(true);
}
})();
});



// load from storage
const sandboxPromises = [];
const sandboxList = document.createElement("div");
Expand Down
22 changes: 22 additions & 0 deletions nodejs/ipfs.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//This code is not used for stellar-runtime
//NOTE: js-ipfs-0.50.1 has Http Api code,
// but is not exposed except `jsipfs` command (src/cli/bin.js,daemon.js).

const Ipfs = require("ipfs");

let node;
exports.start = async (repo = "./repo-ipfs") => {
//NOTE: ipfs config is in repo/config file
if (!node) node = await Ipfs.create({
repo, relay: {enabled: true, hop: {enabled: true, active: true}},
});
const version = (await node.version()).version;
const id = (await node.id()).id;
console.debug(`IPFS version: ${version}`);
console.debug(`Peer ID: ${id}`);
//console.debug(`Peer ID: ${JSON.stringify(await node.id())}`);
return {version, id};
};
exports.stop = () => {
if (node) node.stop();
};
11 changes: 11 additions & 0 deletions nodejs/run-ipfs.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const ipfs = require("./ipfs.cjs");
process.on("SIGHUP", () => {
ipfs.stop();
});
(async () => {
const repo = process.argv[2];
const info = await ipfs.start(repo);
try {
process.send(`${JSON.stringify(info)}`);
} catch (err) {}
})();
File renamed without changes.
35 changes: 35 additions & 0 deletions nodejs/spawn-ipfs.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const {spawn} = require("child_process");
const IpfsHttpClient = require("ipfs-http-client");

let proc = null;
exports.start = repo => new Promise((f, r) => {
console.log(repo);
if (!repo) return;
//proc = spawn("npx", ["jsipfs", "daemon"], {
proc = spawn("node", ["./node_modules/.bin/jsipfs", "daemon"], {
env: Object.assign({"IPFS_PATH": repo}, process.env),
stdio: [0, 1, 2],
});
waitHttpApi().then(f, r);
});

const waitHttpApi = async () => {
const node = IpfsHttpClient("http://127.0.0.1:5002");
for (let i = 0; i < 10; i++) {
try {
const id = (await node.id()).id;
const version = (await node.version()).version;
return {id, version};
} catch (err) {
await new Promise(f => setTimeout(f, 1000));
}
}
throw Error();
};

exports.stop = () => {
if (proc) {
proc.kill(`SIGHUP`);
proc = null;
}
};
2 changes: 1 addition & 1 deletion nodejs/spawn.cjs → nodejs/spawn-server.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const {spawn} = require("child_process");
let proc = null;
exports.start = async (port = 8000) => {
if (proc) return;
proc = spawn("node", ["./nodejs/run.cjs", port], {
proc = spawn("node", ["./nodejs/run-server.cjs", port], {
stdio: [0, 1, 2],
});
await new Promise(f => setTimeout(f, 1000)); //wait to start up web server
Expand Down
Loading

0 comments on commit ab4719e

Please sign in to comment.