This repository was archived by the owner on Apr 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain_puter.js
77 lines (70 loc) · 2.69 KB
/
main_puter.js
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
/*
* Copyright (C) 2024 Puter Technologies Inc.
*
* This file is part of Phoenix Shell.
*
* Phoenix Shell is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { Context } from 'contextlink';
import { launchPuterShell } from './puter-shell/main.js';
import { CreateFilesystemProvider } from './platform/puter/filesystem.js';
import { CreateDriversProvider } from './platform/puter/drivers.js';
import { XDocumentPTT } from './pty/XDocumentPTT.js';
import { CreateEnvProvider } from './platform/puter/env.js';
window.main_shell = async () => {
const config = {};
let resolveConfigured = null;
const configured_ = new Promise(rslv => {
resolveConfigured = rslv;
});
window.addEventListener('message', evt => {
if ( evt.source !== window.parent ) return;
if ( evt.data instanceof Uint8Array ) {
return;
}
if ( ! evt.data.hasOwnProperty('$') ) {
console.error(`unrecognized window message`, evt);
return;
}
if ( evt.data.$ !== 'config' ) return;
console.log('received configuration at ANSI shell');
const configValues = { ...evt.data };
delete configValues.$;
for ( const k in configValues ) {
config[k] = configValues[k];
}
resolveConfigured();
});
window.parent.postMessage({ $: 'ready' }, '*');
await configured_;
const puterSDK = globalThis.puter;
if ( config['puter.auth.token'] ) {
await puterSDK.setAuthToken(config['puter.auth.token']);
}
const source_without_trailing_slash =
(config.source && config.source.replace(/\/$/, ''))
|| 'https://api.puter.com';
await puterSDK.setAPIOrigin(source_without_trailing_slash);
const ptt = new XDocumentPTT();
await launchPuterShell(new Context({
ptt,
config, puterSDK,
externs: new Context({ puterSDK }),
platform: new Context({
filesystem: CreateFilesystemProvider({ puterSDK }),
drivers: CreateDriversProvider({ puterSDK }),
env: CreateEnvProvider({ config }),
}),
}));
};