Skip to content

Commit

Permalink
Added metadata reading API endpoint (#28)
Browse files Browse the repository at this point in the history
This implements an '/api/packages/metadata' which reads the global
package manifest as well as the user's in VFS.

Currently only system based VFS adapters is supported.
  • Loading branch information
andersevenrud committed Apr 10, 2020
1 parent 1d12093 commit 8fd89c8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ const readOrDefault = filename => fs.existsSync(filename)
? fs.readJsonSync(filename)
: [];

/**
* @typedef InstallPackageOptions
* @param {boolean} system
* @param {object} [auth]
*/

/**
* OS.js Package Management
*/
Expand Down Expand Up @@ -133,6 +139,36 @@ class Packages {
}, 500);
}

/**
* Installs a package from given url
* @param {string} url
* @param {InstallPackageOptions} options
*/
async installPackage(url, options) {
throw new Error('Not implemented yet');
}

/**
* Reads package manifests
* @return {Package[]} List of packages
*/
async readPackageManifests(user) {
const {realpath} = this.core.make('osjs/vfs');
const {manifestFile} = this.options;
const homePath = await realpath('home:/.packages/metadata.json', user);

const systemManifest = await readOrDefault(manifestFile);
const userManifest = await readOrDefault(homePath);

return [
...systemManifest,
...userManifest.map(pkg => Object.assign({}, pkg, {
_user: true,
server: null
}))
];
}

/**
* Loads package data
* @param {string} filename Filename
Expand Down
20 changes: 20 additions & 0 deletions src/providers/packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,35 @@ class PackageServiceProvider extends ServiceProvider {
});
}

depends() {
return [
'osjs/express'
];
}

provides() {
return [
'osjs/packages'
];
}

init() {
const {routeAuthenticated} = this.core.make('osjs/express');

this.core.singleton('osjs/packages', () => this.packages);

routeAuthenticated('GET', '/api/packages/manifest', (req, res) => {
this.packages.readPackageManifests(req.session.user)
.then(json => res.json(json))
.catch(error => res.status(400).json({error}));
});

routeAuthenticated('POST', '/api/packages/install', (req, res) => {
this.packages.installPackage(req.body.url, req.body.options)
.then(() => res.json({success: true}))
.catch(error => res.status(400).json({error}));
});

return this.packages.init();
}

Expand Down

0 comments on commit 8fd89c8

Please sign in to comment.