Created
July 21, 2020 20:44
-
-
Save dpaez/3008b46139d9bf0c3807056b5d7ed153 to your computer and use it in GitHub Desktop.
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
const P2PCommons = require('@p2pcommons/sdk-js') | |
const tempy = require('tempy')) | |
const baseDir = tempy.directory() | |
console.log(`baseDir is: ${baseDir}`) | |
const commons = new P2PCommons({ verbose: true, baseDir }) | |
process.once('SIGINT', () => commons.destroy()) | |
const consumer = async () => { | |
// GOAL: the sdk will clone the module key | |
const externalContentUrl = process.argv[2] | |
const externalContentVersion = process.argv[3] // optional, module version for checkouts | |
if (!externalContentUrl) { | |
throw new Error('missing argument: module key') | |
} | |
console.log(`cloning module key ${externalContentUrl}`) | |
const { rawJSON: content, dwldHandle } = await commons.clone( | |
externalContentUrl, | |
externalContentVersion | |
) | |
await new Promise(resolve => { | |
// this can be also a put-end event if the SDK is in watch mode | |
dwldHandle.on('end', () => { | |
return resolve() | |
}) | |
}) | |
console.log('content downloaded OK') | |
console.log({ content }) | |
await commons.destroy() | |
} | |
consumer().catch(err => { | |
console.error(err) | |
process.exit(1) | |
}) |
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
const { | |
promises: { writeFile } | |
} = require('fs') | |
const { join } = require('path') | |
const { once } = require('events') | |
const P2PCommons = require('@p2pcommons/sdk-js') | |
const tempy = require('tempy') | |
const commons = new P2PCommons({ | |
baseDir: tempy.directory(), | |
verbose: true, | |
persist: false | |
}) | |
process.once('SIGINT', () => commons.destroy()) | |
;(async () => { | |
await commons.ready() | |
// create some content | |
const { rawJSON, driveWatch } = await commons.init({ | |
type: 'content', | |
title: 'Cool 101', | |
description: 'All the cool content you want to know' | |
}) | |
const key = rawJSON.url.slice('hyper://'.length) | |
console.log('key', key) | |
await writeFile(join(commons.baseDir, key, 'file.txt'), 'hola mundo') | |
await once(driveWatch, 'put-end') | |
await commons.set({ | |
url: rawJSON.url, | |
description: 'All the cool content you want to know and more!!!', | |
main: 'file.txt' | |
}) | |
const { rawJSON: updatedContent, metadata } = await commons.get(rawJSON.url) | |
console.log({ metadata }) | |
console.log({ rawJSON: updatedContent }) | |
console.log('P2PCommons swarming listening...') | |
commons.destroy(true, false) | |
})() |
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
{ | |
"name": "clone-example", | |
"version": "1.0.0", | |
"description": "p2pcommons sdk clone example", | |
"main": "creator.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"@p2pcommons/sdk-js": "^0.6.3", | |
"tempy": "latest" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment