1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
class WpmModule {
constructor() {
this.commands = {};
this.commandsMeta = {
'wpm': { argType: 'enum', enumFn: '_listSubcommands' },
};
this.terminal = null;
this.indexUrl = 'https://webx11.duckdns.org/spaces/newos4/packages.json';
this.index = null;
this.installed = this.loadInstalled();
}
getHelpInfo() {
return { 'wpm': 'wpm <install|list|search|update|remove|info> [...] — Web Package Manager for installing and managing modules' };
}
loadInstalled() {
try {
const data = localStorage.getItem('webx11_wpm_installed');
return data ? JSON.parse(data) : {};
} catch (e) { return {}; }
}
saveInstalled() {
localStorage.setItem('webx11_wpm_installed', JSON.stringify(this.installed));
}
// ── Catalog normalisation ───────────────────────────────────────────────
// packages.json is hand-edited JSON and its field names have drifted: `kanban`
// shipped with `description` while the other 31 entries use `desc`
// (RPT-20261109-123907-5a6273ab), and an entry can be missing `category` /
// `commands` entirely. Reading pkg.desc.toLowerCase() straight out of the index
// threw a TypeError out of `wpm search` for EVERY keyword (the bad entry is only
// reached mid-loop), so all catalog reads go through _pkg()/_packageMap().
// `desc` wins when both keys exist; values are coerced so .toLowerCase() is safe.
_pkg(name, raw) {
const src = (raw && typeof raw === 'object') ? raw : {};
const str = (v) => (v === undefined || v === null) ? '' : String(v);
let commands = src.commands;
if (typeof commands === 'string') commands = [commands];
if (!Array.isArray(commands)) commands = [];
return Object.assign({}, src, {
name: str(src.name) || str(name),
version: str(src.version) || '?',
desc: str(src.desc) || str(src.description),
category: str(src.category),
url: str(src.url),
commands: commands,
});
}
_packageMap(idx) {
const pkgs = (idx && idx.packages && typeof idx.packages === 'object') ? idx.packages : {};
const out = {};
Object.keys(pkgs).forEach(k => { out[k] = this._pkg(k, pkgs[k]); });
return out;
}
registerCommands() {
this.commands['wpm'] = (args) => this.handle(args);
}
handle(args) {
if (!args[0]) return this.terminal.print('Usage: wpm install <pkg>, wpm list, wpm search <kw>, wpm update, wpm remove <pkg>, wpm info <pkg>', 'warning');
const sub = args[0].toLowerCase();
if (sub === 'install') this.install(args[1]);
else if (sub === 'list') this.list();
else if (sub === 'search') this.search(args.slice(1).join(' '));
else if (sub === 'update') this.update();
else if (sub === 'remove') this.remove(args[1]);
else if (sub === 'info') this.info(args[1]);
else this.terminal.print(`Unknown: ${sub}`, 'error');
}
async loadIndex() {
if (this.index) return this.index;
try {
this.terminal.print('Fetching package index...', 'info');
const res = await fetch(this.indexUrl);
if (!res.ok) throw new Error('HTTP ' + res.status);
this.index = await res.json();
return this.index;
} catch (e) {
this.terminal.print(`Failed to load index: ${e.message}`, 'error');
return null;
}
}
async install(name) {
if (!name) return this.terminal.print('Usage: wpm install <package>', 'warning');
const idx = await this.loadIndex();
if (!idx) return;
const raw = idx.packages && idx.packages[name];
if (!raw) return this.terminal.print(`Package "${name}" not found. Try: wpm search <keyword>`, 'error');
const pkg = this._pkg(name, raw);
if (this.installed[name]) return this.terminal.print(`Package "${name}" already installed. Use wpm update to reinstall.`, 'warning');
this.terminal.print(`Installing ${pkg.name} v${pkg.version}...`, 'info');
this.terminal.print(` ${pkg.desc}`, 'info');
// P0: SRI — fetch source, compute SHA-256, verify integrity before loading
try {
const res = await fetch(pkg.url);
if (!res.ok) throw new Error('HTTP ' + res.status);
const source = await res.text();
// Compute SHA-256 via Web Crypto API
const encoder = new TextEncoder();
const data = encoder.encode(source);
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
const hashHex = Array.from(new Uint8Array(hashBuffer))
.map(b => b.toString(16).padStart(2, '0')).join('');
// Verify if package declares a hash
if (pkg.hash) {
if (hashHex !== pkg.hash) {
this.terminal.print(`❌ INTEGRITY CHECK FAILED: hash mismatch!`, 'error');
this.terminal.print(` Expected: ${pkg.hash}`, 'error');
this.terminal.print(` Got: ${hashHex}`, 'error');
this.terminal.print(` Package may have been tampered with. Aborting.`, 'error');
return;
}
this.terminal.print(`✅ Integrity verified (SHA-256: ${hashHex.substring(0, 16)}...)`, 'success');
} else {
this.terminal.print(`⚠ No hash declared. Computed SHA-256: ${hashHex.substring(0, 24)}...`, 'warning');
}
// Load the verified source
const success = await this.terminal.moduleManager.loadModuleFromURL(pkg.url);
if (success) {
this.installed[name] = { version: pkg.version, installed: new Date().toISOString(), hash: hashHex };
this.saveInstalled();
this.terminal.print(`"${name}" installed successfully!`, 'success');
}
} catch (e) {
this.terminal.print(`Install failed: ${e.message}`, 'error');
}
}
async list() {
const idx = await this.loadIndex();
if (!idx) return;
const names = Object.keys(this.installed);
this.terminal.print('=== Installed Packages ===', 'info');
if (names.length === 0) {
this.terminal.print(' None. Use: wpm install <pkg>', 'info');
} else {
names.forEach(name => {
const inst = this.installed[name];
this.terminal.print(` ${name} v${inst.version} (${inst.installed.split('T')[0]})`);
});
}
this.terminal.print('\n=== Available Packages ===', 'info');
const pkgs = this._packageMap(idx);
Object.keys(pkgs).sort().forEach(name => {
const pkg = pkgs[name];
const installed = this.installed[name] ? ' ✅' : '';
this.terminal.print(` ${name} - ${pkg.desc}${installed}`);
});
}
async search(keyword) {
if (!keyword) return this.terminal.print('Usage: wpm search <keyword>', 'warning');
const idx = await this.loadIndex();
if (!idx) return;
const kw = String(keyword).toLowerCase();
const results = [];
Object.entries(this._packageMap(idx)).forEach(([name, pkg]) => {
if (name.toLowerCase().includes(kw) || pkg.desc.toLowerCase().includes(kw) || pkg.category.toLowerCase().includes(kw)) {
results.push({ name, ...pkg });
}
});
if (results.length === 0) return this.terminal.print(`No packages matching "${keyword}"`, 'info');
this.terminal.print(`=== Search: "${keyword}" (${results.length} results) ===`, 'info');
results.forEach(r => {
const installed = this.installed[r.name] ? ' [installed]' : '';
this.terminal.print(` ${r.name} [${r.category}] - ${r.desc}${installed}`);
});
this.terminal.print('Use: wpm install <name> to install', 'info');
}
async update() {
this.index = null;
await this.loadIndex();
if (!this.index) return;
this.terminal.print('Index refreshed.', 'success');
const outdated = [];
const pkgs = this._packageMap(this.index);
Object.keys(this.installed).forEach(name => {
const pkg = pkgs[name];
if (pkg && pkg.version !== this.installed[name].version) {
outdated.push(name);
}
});
if (outdated.length > 0) {
this.terminal.print(`Outdated packages: ${outdated.join(', ')}. Reinstall with "wpm install <name>"`, 'warning');
}
}
async remove(name) {
if (!name) return this.terminal.print('Usage: wpm remove <package>', 'warning');
if (!this.installed[name]) return this.terminal.print(`Package "${name}" is not installed via wpm`, 'warning');
const idx = await this.loadIndex();
const raw = idx && idx.packages ? idx.packages[name] : null;
const className = raw ? this._pkg(name, raw).name.replace(/\s+/g, '') + 'Module' : null;
if (className && this.terminal.moduleManager.unregisterModule(className)) {
this.terminal.print(`Module ${className} unregistered`, 'info');
}
delete this.installed[name];
this.saveInstalled();
this.terminal.print(`Package "${name}" removed. If module persists, use: removemod <ClassName>`, 'success');
}
async info(name) {
if (!name) return this.terminal.print('Usage: wpm info <package>', 'warning');
const idx = await this.loadIndex();
if (!idx) return;
const raw = idx.packages && idx.packages[name];
if (!raw) return this.terminal.print(`Package "${name}" not found`, 'error');
const pkg = this._pkg(name, raw);
this.terminal.print(`=== ${pkg.name} v${pkg.version} ===`, 'info');
this.terminal.print(`Description: ${pkg.desc}`);
this.terminal.print(`Category: ${pkg.category}`);
this.terminal.print(`Commands: ${pkg.commands.join(', ')}`);
this.terminal.print(`URL: ${pkg.url}`);
const installed = this.installed[name];
if (installed) {
this.terminal.print(`Installed: ${installed.installed}`, 'success');
if (installed.hash) this.terminal.print(`Hash: ${installed.hash.substring(0, 24)}...`, 'info');
}
else this.terminal.print('Not installed. Use: wpm install ' + name, 'info');
}
// ── Enum helpers for autocomplete ──
_listSubcommands() {
return ['install', 'list', 'search', 'update', 'remove', 'info'];
}
}
if (typeof window.webx11 !== 'undefined' && window.webx11) {
webx11.moduleManager.registerModule(new WpmModule(), 'https://webx11.duckdns.org/spaces/newos4/modules/ext/wpm.js');
}
|