-
Notifications
You must be signed in to change notification settings - Fork 0
support for multiple apps #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aaronshaf
wants to merge
2
commits into
master
Choose a base branch
from
multipleapps3
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -61,4 +61,6 @@ typings/ | |
| package-lock.json | ||
| yarn.lock | ||
|
|
||
| pub-archive | ||
| pub-archive | ||
| applications/* | ||
| NOTES.md | ||
This file contains hidden or 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 @@ | ||
| public/vendor/ |
This file contains hidden or 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 |
|---|---|---|
| @@ -1 +1,5 @@ | ||
| {"semi": false, "singleQuote": true, "bracketSpacing": true} | ||
| { | ||
| "semi": false, | ||
| "singleQuote": true, | ||
| "bracketSpacing": true | ||
| } |
This file contains hidden or 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,68 @@ | ||
| const DatArchive = require('node-dat-archive') | ||
| const { lstatSync, readdirSync } = require('fs') | ||
| const { join, basename } = require('path') | ||
| const mkdirp = require('mkdirp') | ||
|
|
||
| const APPLICATIONS_DIR = join(__dirname, 'applications') | ||
|
|
||
| const isDirectory = source => lstatSync(source).isDirectory() | ||
|
|
||
| const getDirectories = source => | ||
| readdirSync(source) | ||
| .map(name => join(source, name)) | ||
| .filter(isDirectory) | ||
| .map(name => basename(name)) | ||
| .filter(name => name.length === 64) | ||
|
|
||
| mkdirp.sync(APPLICATIONS_DIR) | ||
|
|
||
| const archives = new Map() | ||
|
|
||
| exports.getApplicationPubs = () => archives | ||
|
|
||
| exports.createApplicationPub = async applicationKey => { | ||
| if (archives.has(applicationKey)) { | ||
| return archives.get(applicationKey) | ||
| } | ||
|
|
||
| const localPath = join(APPLICATIONS_DIR, applicationKey, 'pub') | ||
|
|
||
| mkdirp.sync(localPath) | ||
|
|
||
| const archive = await DatArchive.create({ | ||
| localPath, | ||
| title: `Known archives for application: dat://${applicationKey}`, | ||
| description: 'Pub archive for application', | ||
| datOptions: { latest: true } | ||
| }) | ||
|
|
||
| // wait for archive to be ready for use | ||
| await archive._loadPromise | ||
|
|
||
| await loadAllArchives() | ||
|
|
||
| return archive | ||
| } | ||
|
|
||
| async function loadAllArchives() { | ||
| const dirs = getDirectories(APPLICATIONS_DIR).filter( | ||
| applicationKey => archives.has(applicationKey) === false | ||
| ) | ||
|
|
||
| for (let applicationKey of dirs) { | ||
| const archive = await DatArchive.load({ | ||
| localPath: join(APPLICATIONS_DIR, applicationKey, 'pub'), | ||
| datOptions: { latest: true } | ||
| }) | ||
|
|
||
| if (archive == null) { | ||
| throw new Error( | ||
| `Error when creating or loading archive for ${applicationKey}` | ||
| ) | ||
| } | ||
|
|
||
| archives.set(applicationKey, archive) | ||
| } | ||
| } | ||
|
|
||
| exports.loadAllArchives = loadAllArchives | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,102 +1,14 @@ | ||
| const DatArchive = require('node-dat-archive') | ||
| const fastify = require('fastify')({ logger: true }) | ||
| const mkdirp = require('dat-mkdirp') | ||
| const datPathExists = require('dat-path-exists') | ||
| const { createServer } = require('./server') | ||
|
|
||
| const IS_DAT = /^dat:\/\/.+/ | ||
| async function main() { | ||
| const fastify = await createServer() | ||
|
|
||
| function createServer(archive) { | ||
| fastify.get('/', async () => { | ||
| return { | ||
| url: archive.url | ||
| } | ||
| }) | ||
|
|
||
| fastify.post( | ||
| '/', | ||
| { | ||
| schema: { | ||
| body: { | ||
| type: 'object', | ||
| required: ['url', 'application'], | ||
| properties: { | ||
| url: { | ||
| type: 'string' | ||
| }, | ||
| application: { | ||
| type: 'string' | ||
| } | ||
| } | ||
| }, | ||
| response: { | ||
| 200: { | ||
| type: 'object', | ||
| properties: { | ||
| location: { | ||
| type: 'string' | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| async (request) => { | ||
| const newURL = request.body.url | ||
| const application = request.body.application | ||
|
|
||
| if (!IS_DAT.exec(newURL)) throw new Error('Invalid url') | ||
|
|
||
| if (!IS_DAT.exec(application)) throw new Error('Invalid application url') | ||
|
|
||
| const newArchive = new DatArchive(newURL, { | ||
| datOptions: { latest: true } | ||
| }) | ||
|
|
||
| const proofLocation = `/.well-known/dat-pubs/${archive.url.slice(6)}.json` | ||
| try { | ||
| const file = await newArchive.readFile(proofLocation) | ||
| const proofData = JSON.parse(file) | ||
|
|
||
| if (!proofData || !Array.isArray(proofData.applications)) { | ||
| throw new Error('Invalid format for proof file') | ||
| } | ||
|
|
||
| if (proofData.applications.indexOf(application) === -1) { | ||
| throw new Error(`Missing application (${application}) in proof list`) | ||
| } | ||
|
|
||
| const recordLocation = `/${application.slice(6)}/${newURL.slice( | ||
| 6 | ||
| )}.json` | ||
|
|
||
| const alreadyExists = await datPathExists(recordLocation, archive) | ||
|
|
||
| if (alreadyExists) throw new Error('Already registered') | ||
|
|
||
| const recordData = JSON.stringify({}) | ||
|
|
||
| await mkdirp(`/${application.slice(6)}`, archive) | ||
|
|
||
| await archive.writeFile(recordLocation, recordData) | ||
|
|
||
| return { | ||
| location: `${archive.url}${recordLocation}` | ||
| } | ||
| } catch (e) { | ||
| const err = new Error( | ||
| `${ | ||
| e.message | ||
| }. Could not find a valid proof at '${newURL}${proofLocation}'` | ||
| ) | ||
| err.statusCode = 403 | ||
| throw err | ||
| } | ||
| } | ||
| ) | ||
|
|
||
| return fastify | ||
| try { | ||
| await fastify.listen(process.env.PORT || 3000, '0.0.0.0') | ||
| } catch (err) { | ||
| fastify.log.error(err) | ||
| process.exit(1) | ||
| } | ||
| } | ||
|
|
||
| module.exports = { | ||
| createServer | ||
| } | ||
| main() |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be configurable by the user.