This repository has been archived by the owner on May 11, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
179 changed files
with
2,346 additions
and
14,717 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
#!/usr/bin/env node | ||
|
||
const fs = require('fs') | ||
const path = require('path') | ||
const os = require('os') | ||
|
||
const { mkdirp } = require('../lib/n3h-common') | ||
const tweetlog = require('../lib/tweetlog') | ||
const log = tweetlog('n3h-bin') | ||
|
||
const { buildLogHandler } = require('../lib/file-logger') | ||
const { N3hNode, N3hHackMode, N3hMock } = require('../lib/index') | ||
const { unhandledRejection } = require('../lib/n3h-common') | ||
unhandledRejection.strict() | ||
|
||
async function main () { | ||
const workDir = 'N3H_WORK_DIR' in process.env | ||
? process.env.N3H_WORK_DIR | ||
: path.resolve(path.join( | ||
os.homedir(), '.n3h')) | ||
|
||
// Move into working directory? | ||
await mkdirp(workDir) | ||
process.chdir(workDir) | ||
|
||
const logDir = path.join(workDir, 'logs') | ||
await mkdirp(logDir) | ||
|
||
const logHandler = buildLogHandler({ | ||
dir: logDir | ||
}) | ||
|
||
// setup logging | ||
tweetlog.set('t') // enable trace logging | ||
tweetlog.listen(logHandler) | ||
|
||
const quietMode = 'N3H_QUIET' in process.env | ||
if (!quietMode) { | ||
tweetlog.listen((level, tag, ...args) => { | ||
args = args.map(a => { | ||
if (a instanceof Error) { | ||
return a.stack || a.toString() | ||
} else if (typeof a === 'object') { | ||
return JSON.stringify(a) | ||
} | ||
return a.toString() | ||
}).join(' ') | ||
console.error(`(${tag}) [${level}] ${args}`) | ||
}) | ||
} | ||
|
||
let rawConfigData = null | ||
|
||
try { | ||
rawConfigData = JSON.parse((await _tryReadStdin()).toString()) | ||
log.w('got stdin config!', rawConfigData) | ||
} catch (e) { | ||
log.w('could not read stdin', e) | ||
} | ||
|
||
if (!rawConfigData) { | ||
try { | ||
rawConfigData = JSON.parse(fs.readFileSync(path.resolve('n3h-config.json'))) | ||
} catch (e) { | ||
log.w('failed to load config', e) | ||
} | ||
} | ||
|
||
let mode = 'HACK' | ||
|
||
// mode from config | ||
if (rawConfigData && 'mode' in rawConfigData) { | ||
mode = rawConfigData.mode | ||
} | ||
|
||
// environment mode override | ||
if ('N3H_MODE' in process.env) { | ||
mode = process.env.N3H_MODE | ||
} | ||
|
||
var n3hNode = null | ||
|
||
let terminated = false | ||
const terminate = async () => { | ||
if (terminated) { | ||
return | ||
} | ||
try { | ||
if (n3hNode) { | ||
await n3hNode.destroy() | ||
} | ||
log.i('n3h exited cleanly') | ||
await logHandler.cleanup() | ||
process.exit(0) | ||
} catch (e) { | ||
try { | ||
await logHandler.cleanup() | ||
} catch (e) { /* pass */ } | ||
log.e(e.stack || e.toString()) | ||
process.exit(1) | ||
} | ||
} | ||
|
||
process.on('SIGINT', terminate) | ||
process.on('SIGTERM', terminate) | ||
process.on('unhandledRejection', err => { | ||
log.e(err) | ||
terminate() | ||
}) | ||
|
||
log.i('executing mode ' + mode) | ||
switch(mode) { | ||
case 'MOCK': | ||
n3hNode = await new N3hMock(workDir, rawConfigData) | ||
break | ||
case 'HACK': | ||
n3hNode = await new N3hHackMode(workDir, rawConfigData) | ||
break | ||
default: | ||
log.e('so called "real" mode disabled while running POC on hackmode / mockmode') | ||
process.exit(1) | ||
//n3hNode = await N3hNode.constructDefault(workDir, rawConfigData) | ||
//break | ||
} | ||
|
||
await n3hNode.run() | ||
} | ||
|
||
main().then(() => {}, (err) => { | ||
log.e(err) | ||
process.exit(1) | ||
}) | ||
|
||
/** | ||
* read from stdin until closed | ||
* timeout if not read in 4 seconds | ||
*/ | ||
async function _tryReadStdin () { | ||
const timeout = (new Error('timeout')).stack | ||
return new Promise((resolve, reject) => { | ||
const timer = setTimeout(() => { | ||
reject(timeout) | ||
}, 4000) | ||
|
||
log.w('attempting read stdin') | ||
process.stdin.resume() | ||
|
||
let data = Buffer.alloc(0) | ||
process.stdin.on('data', chunk => { | ||
data = Buffer.concat([data, chunk]) | ||
}) | ||
process.stdin.on('end', () => { | ||
clearTimeout(timer) | ||
resolve(data) | ||
}) | ||
}) | ||
} |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...ode/lib/p2p-backend-hackmode-peer.test.js → ...ackmode/p2p-backend-hackmode-peer.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/hc-dpki/lib/keypair.test.js → lib/hc-dpki/keypair.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.