Skip to content

Commit 708f8ec

Browse files
committed
Added metadata reading API endpoint (#28)
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.
1 parent aaef734 commit 708f8ec

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/packages.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ const readOrDefault = filename => fs.existsSync(filename)
4141
? fs.readJsonSync(filename)
4242
: [];
4343

44+
/**
45+
* @typedef InstallPackageOptions
46+
* @param {boolean} system
47+
* @param {object} [auth]
48+
*/
49+
4450
/**
4551
* OS.js Package Management
4652
*/
@@ -133,6 +139,36 @@ class Packages {
133139
}, 500);
134140
}
135141

142+
/**
143+
* Installs a package from given url
144+
* @param {string} url
145+
* @param {InstallPackageOptions} options
146+
*/
147+
async installPackage(url, options) {
148+
throw new Error('Not implemented yet');
149+
}
150+
151+
/**
152+
* Reads package manifests
153+
* @return {Package[]} List of packages
154+
*/
155+
async readPackageManifests(user) {
156+
const {realpath} = this.core.make('osjs/vfs');
157+
const {manifestFile} = this.options;
158+
const homePath = await realpath('home:/.packages/metadata.json', user);
159+
160+
const systemManifest = await readOrDefault(manifestFile);
161+
const userManifest = await readOrDefault(homePath);
162+
163+
return [
164+
...systemManifest,
165+
...userManifest.map(pkg => Object.assign({}, pkg, {
166+
_user: true,
167+
server: null
168+
}))
169+
];
170+
}
171+
136172
/**
137173
* Loads package data
138174
* @param {string} filename Filename

src/providers/packages.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,35 @@ class PackageServiceProvider extends ServiceProvider {
5555
});
5656
}
5757

58+
depends() {
59+
return [
60+
'osjs/express'
61+
];
62+
}
63+
5864
provides() {
5965
return [
6066
'osjs/packages'
6167
];
6268
}
6369

6470
init() {
71+
const {routeAuthenticated} = this.core.make('osjs/express');
72+
6573
this.core.singleton('osjs/packages', () => this.packages);
6674

75+
routeAuthenticated('GET', '/api/packages/manifest', (req, res) => {
76+
this.packages.readPackageManifests(req.session.user)
77+
.then(json => res.json(json))
78+
.catch(error => res.status(400).json({error}));
79+
});
80+
81+
routeAuthenticated('POST', '/api/packages/install', (req, res) => {
82+
this.packages.installPackage(req.body.url, req.body.options)
83+
.then(() => res.json({success: true}))
84+
.catch(error => res.status(400).json({error}));
85+
});
86+
6787
return this.packages.init();
6888
}
6989

0 commit comments

Comments
 (0)