@heyputer/puter.js 2.1.15 → 2.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/dist/puter.cjs +2 -2
  2. package/index.d.ts +0 -2
  3. package/package.json +2 -3
  4. package/src/index.js +100 -82
  5. package/src/lib/utils.js +42 -55
  6. package/src/modules/AI.js +8 -204
  7. package/src/modules/Apps.js +42 -11
  8. package/src/modules/Auth.js +6 -5
  9. package/src/modules/Debug.js +4 -4
  10. package/src/modules/Drivers.js +12 -17
  11. package/src/modules/FileSystem/index.js +9 -24
  12. package/src/modules/Hosting.js +5 -4
  13. package/src/modules/KV.js +67 -11
  14. package/src/modules/OS.js +6 -5
  15. package/src/modules/Perms.js +4 -3
  16. package/src/modules/PuterDialog.js +2 -2
  17. package/src/modules/UI.js +44 -28
  18. package/src/modules/UsageLimitDialog.js +208 -0
  19. package/types/modules/ai.d.ts +0 -10
  20. package/types/modules/apps.d.ts +24 -15
  21. package/types/modules/auth.d.ts +3 -1
  22. package/types/modules/filesystem.d.ts +0 -2
  23. package/types/modules/kv.d.ts +10 -0
  24. package/types/modules/networking.d.ts +1 -1
  25. package/types/modules/os.d.ts +2 -7
  26. package/types/modules/ui.d.ts +10 -7
  27. package/types/modules/util.d.ts +0 -1
  28. package/types/modules/workers.d.ts +9 -10
  29. package/types/puter.d.ts +1 -7
  30. package/src/lib/filesystem/APIFS.js +0 -65
  31. package/src/lib/filesystem/CacheFS.js +0 -243
  32. package/src/lib/filesystem/PostMessageFS.js +0 -40
  33. package/src/lib/filesystem/definitions.js +0 -40
  34. package/src/modules/Threads.js +0 -72
  35. package/src/services/APIAccess.js +0 -46
  36. package/src/services/FSRelay.js +0 -20
  37. package/src/services/Filesystem.js +0 -137
  38. package/src/services/NoPuterYet.js +0 -20
  39. package/src/services/XDIncoming.js +0 -44
  40. package/types/modules/threads.d.ts +0 -27
@@ -1,26 +1,25 @@
1
1
  export interface WorkerInfo {
2
2
  name: string;
3
3
  url: string;
4
- file_path?: string;
5
- file_uid?: string;
6
- created_at?: string;
7
- [key: string]: unknown;
4
+ file_path: string;
5
+ file_uid: string;
6
+ created_at: string;
8
7
  }
9
8
 
10
9
  export interface WorkerDeployment {
11
10
  success: boolean;
12
11
  url: string;
13
- errors?: unknown[];
14
- [key: string]: unknown;
12
+ errors?: string[];
15
13
  }
16
14
 
17
15
  export class WorkersHandler {
18
- constructor (authToken?: string);
19
-
20
16
  create (workerName: string, filePath: string, appName?: string): Promise<WorkerDeployment>;
21
17
  delete (workerName: string): Promise<boolean>;
22
18
  exec (request: RequestInfo | URL, init?: RequestInit): Promise<Response>;
23
- get (workerName: string): Promise<WorkerInfo>;
19
+ get (workerName: string): Promise<WorkerInfo | undefined>;
24
20
  list (): Promise<WorkerInfo[]>;
25
- getLoggingHandle (workerName: string): Promise<EventTarget & { close: () => void }>;
21
+ getLoggingHandle (workerName: string): Promise<EventTarget & {
22
+ close: () => void;
23
+ onLog: (event: MessageEvent) => void;
24
+ }>;
26
25
  }
package/types/puter.d.ts CHANGED
@@ -10,14 +10,11 @@ import type { KV } from './modules/kv.d.ts';
10
10
  import type { Networking } from './modules/networking.d.ts';
11
11
  import type { OS } from './modules/os.d.ts';
12
12
  import type { Perms } from './modules/perms.d.ts';
13
- import type Threads from './modules/threads.d.ts';
14
13
  import type { UI } from './modules/ui.d.ts';
15
14
  import type Util from './modules/util.d.ts';
16
15
  import type { WorkersHandler } from './modules/workers.d.ts';
17
16
  import type { APICallLogger, APILoggingConfig, PuterEnvironment, ToolSchema } from './shared.d.ts';
18
17
 
19
- export interface NetAPI extends Networking {}
20
-
21
18
  export interface PuterArgs {
22
19
  [key: string]: unknown;
23
20
  }
@@ -55,7 +52,6 @@ export class Puter {
55
52
  ui: UI;
56
53
  hosting: Hosting;
57
54
  kv: KV;
58
- threads: Threads;
59
55
  perms: Perms;
60
56
  drivers: Drivers;
61
57
  debug: Debug;
@@ -67,13 +63,11 @@ export class Puter {
67
63
  [key: string]: unknown;
68
64
  };
69
65
 
70
- net: NetAPI;
66
+ net: Networking;
71
67
  workers: WorkersHandler;
72
68
 
73
69
  static FSItem: typeof FSItem;
74
70
 
75
- constructor();
76
-
77
71
  setAuthToken(authToken: string): void;
78
72
  resetAuthToken(): void;
79
73
  setAPIOrigin(APIOrigin: string): void;
@@ -1,65 +0,0 @@
1
- import * as utils from '../utils.js';
2
- import putility from '@heyputer/putility';
3
- import { TeePromise } from '@heyputer/putility/src/libs/promise.js';
4
- import getAbsolutePathForApp from '../../modules/FileSystem/utils/getAbsolutePathForApp.js';
5
- import { TFilesystem } from './definitions.js';
6
-
7
- export class PuterAPIFilesystem extends putility.AdvancedBase {
8
- constructor ({ api_info }) {
9
- super();
10
- this.api_info = api_info;
11
- }
12
-
13
- static IMPLEMENTS = {
14
- [TFilesystem]: {
15
- stat: async function (options) {
16
- this.ensure_auth_();
17
- const tp = new TeePromise();
18
-
19
- const xhr = new utils.initXhr('/stat', this.api_info.APIOrigin, undefined, 'post', 'text/plain;actually=json');
20
- utils.setupXhrEventHandlers(xhr, undefined, undefined, tp.resolve.bind(tp), tp.reject.bind(tp));
21
-
22
- let dataToSend = {};
23
- if ( options.uid !== undefined ) {
24
- dataToSend.uid = options.uid;
25
- } else if ( options.path !== undefined ) {
26
- // If dirPath is not provided or it's not starting with a slash, it means it's a relative path
27
- // in that case, we need to prepend the app's root directory to it
28
- dataToSend.path = getAbsolutePathForApp(options.path);
29
- }
30
-
31
- dataToSend.return_subdomains = options.returnSubdomains;
32
- dataToSend.return_permissions = options.returnPermissions;
33
- dataToSend.return_versions = options.returnVersions;
34
- dataToSend.return_size = options.returnSize;
35
- dataToSend.auth_token = this.api_info.authToken;
36
-
37
- xhr.send(JSON.stringify(dataToSend));
38
-
39
- return await tp;
40
- },
41
- readdir: async function (options) {
42
- this.ensure_auth_();
43
- const tp = new TeePromise();
44
-
45
- const xhr = new utils.initXhr('/readdir', this.api_info.APIOrigin, undefined, 'post', 'text/plain;actually=json');
46
- utils.setupXhrEventHandlers(xhr, undefined, undefined, tp.resolve.bind(tp), tp.reject.bind(tp));
47
-
48
- xhr.send(JSON.stringify({ path: getAbsolutePathForApp(options.path), auth_token: this.api_info.authToken }));
49
-
50
- return await tp;
51
- },
52
- },
53
- };
54
-
55
- ensure_auth_ () {
56
- // TODO: remove reference to global 'puter'; get 'env' via context
57
- if ( !this.api_info.authToken && puter.env === 'web' ) {
58
- try {
59
- this.ui.authenticateWithPuter();
60
- } catch (e) {
61
- throw new Error('Authentication failed.');
62
- }
63
- }
64
- }
65
- }
@@ -1,243 +0,0 @@
1
- import putility from '@heyputer/putility';
2
- import { RWLock } from '@heyputer/putility/src/libs/promise.js';
3
- import { ProxyFilesystem, TFilesystem } from './definitions.js';
4
- import { uuidv4 } from '../utils.js';
5
-
6
- export const ROOT_UUID = '00000000-0000-0000-0000-000000000000';
7
- const TTL = 5 * 1000;
8
-
9
- export class CacheFS extends putility.AdvancedBase {
10
- static PROPERTIES = {
11
- assocs_path_: () => ({}),
12
- assocs_uuid_: () => ({}),
13
- entries: () => ({}),
14
- };
15
-
16
- get_entry_ei (external_identifier) {
17
- if ( Array.isArray(external_identifier) ) {
18
- for ( const ei of external_identifier ) {
19
- const entry = this.get_entry_ei(ei);
20
- if ( entry ) return entry;
21
- }
22
- return;
23
- }
24
-
25
- console.log('GET ENTRY EI', external_identifier);
26
-
27
- const internal_identifier =
28
- this.assocs_path_[external_identifier] ||
29
- this.assocs_uuid_[external_identifier] ||
30
- external_identifier;
31
-
32
- if ( ! internal_identifier ) {
33
- return;
34
- }
35
- return this.entries[internal_identifier];
36
- }
37
-
38
- add_entry ({ id } = {}) {
39
- const internal_identifier = id ?? uuidv4();
40
- const entry = {
41
- id: internal_identifier,
42
- stat_has: {},
43
- stat_exp: 0,
44
- locks: {
45
- stat: new RWLock(),
46
- members: new RWLock(),
47
- },
48
- };
49
- this.entries[internal_identifier] = entry;
50
- return entry;
51
- }
52
-
53
- assoc_path (path, internal_identifier) {
54
- console.log('ASSOC PATH', path, internal_identifier);
55
- this.assocs_path_[path] = internal_identifier;
56
- }
57
-
58
- assoc_uuid (uuid, internal_identifier) {
59
- if ( uuid === internal_identifier ) return;
60
- this.assocs_uuid_[uuid] = internal_identifier;
61
- }
62
-
63
- }
64
-
65
- export class CachedFilesystem extends ProxyFilesystem {
66
- constructor (o) {
67
- super(o);
68
- // this.cacheFS = cacheFS;
69
- this.cacheFS = new CacheFS();
70
- }
71
- static IMPLEMENTS = {
72
- [TFilesystem]: {
73
- stat: async function (o) {
74
- let cent = this.cacheFS.get_entry_ei(o.path ?? o.uid);
75
-
76
- const modifiers = [
77
- 'subdomains',
78
- 'permissions',
79
- 'versions',
80
- 'size',
81
- ];
82
-
83
- let values_requested = {};
84
- for ( const mod of modifiers ) {
85
- const optionsKey = `return${
86
- mod.charAt(0).toUpperCase()
87
- }${mod.slice(1)}`;
88
- if ( ! o[optionsKey] ) continue;
89
- values_requested[mod] = true;
90
- }
91
-
92
- const satisfactory_cache = cent => {
93
- for ( const mod of modifiers ) {
94
- if ( ! values_requested[mod] ) continue;
95
- if ( ! cent.stat_has[mod] ) {
96
- return false;
97
- }
98
- }
99
- return true;
100
- };
101
-
102
- let cached_stat;
103
- if ( cent && cent.stat && cent.stat_exp > Date.now() ) {
104
- const l = await cent.locks.stat.rlock();
105
- if ( satisfactory_cache(cent) ) {
106
- cached_stat = cent.stat;
107
- }
108
- l.unlock();
109
- }
110
-
111
- if ( cached_stat ) {
112
- console.log('CACHE HIT');
113
- return cached_stat;
114
- }
115
- console.log('CACHE MISS');
116
-
117
- let l;
118
- if ( cent ) {
119
- l = await cent.locks.stat.wlock();
120
- }
121
-
122
- console.log('DOING THE STAT', o);
123
- const entry = await this.delegate.stat(o);
124
-
125
- // We might have new information to identify a relevant cache entry
126
- let cent_replaced = !!cent;
127
- cent = this.cacheFS.get_entry_ei([entry.uid, entry.path]);
128
- if ( cent ) {
129
- if ( cent_replaced ) l.unlock();
130
- l = await cent.locks.stat.wlock();
131
- }
132
-
133
- if ( ! cent ) {
134
- cent = this.cacheFS.add_entry({ id: entry.uid });
135
- this.cacheFS.assoc_path(entry.path, cent.id);
136
- this.cacheFS.assoc_uuid(entry.uid, cent.id);
137
-
138
- l = await cent.locks.stat.wlock();
139
- }
140
-
141
- cent.stat = entry;
142
- cent.stat_has = { ...values_requested };
143
- // TODO: increase cache TTL once invalidation works
144
- cent.stat_exp = Date.now() + TTL;
145
-
146
- l.unlock();
147
-
148
- console.log('RETRUNING THE ENTRY', entry);
149
- return entry;
150
- },
151
- readdir: async function (o) {
152
- let cent = this.cacheFS.get_entry_ei([o.path, o.uid]);
153
-
154
- console.log('CENT', cent, o);
155
- let stats = null;
156
- if ( cent && cent.members && cent.members_exp > Date.now() ) {
157
- console.log('MEMBERS', cent.members);
158
- stats = [];
159
- const l = await cent.locks.stat.rlock();
160
-
161
- for ( const id of cent.members ) {
162
- const member = this.cacheFS.get_entry_ei(id);
163
- if ( !member || !member.stat || member.stat_exp <= Date.now() ) {
164
- console.log('NO MEMBER OR STAT', member);
165
- stats = null;
166
- break;
167
- }
168
- console.log('member', member);
169
- if ( !o.no_assocs && !member.stat_has.subdomains ) {
170
- stats = null;
171
- break;
172
- }
173
- if ( !o.no_assocs && !member.stat_has.apps ) {
174
- stats = null;
175
- break;
176
- }
177
- if ( !o.no_thumbs && !member.stat_has.thumbnail ) {
178
- stats = null;
179
- break;
180
- }
181
- console.log('PUSHING', member.stat);
182
-
183
- stats.push(member.stat);
184
- }
185
-
186
- l.unlock();
187
- }
188
-
189
- console.log('STATS????', stats);
190
- if ( stats ) {
191
- return stats;
192
- }
193
-
194
- let l;
195
- if ( cent ) {
196
- l = await cent.locks.members.wlock();
197
- }
198
-
199
- const entries = await this.delegate.readdir(o);
200
- if ( ! cent ) {
201
- cent = this.cacheFS.add_entry(o.uid ? { id: o.uid } : {});
202
- if ( o.path ) this.cacheFS.assoc_path(o.path, cent.id);
203
- l = await cent.locks.members.wlock();
204
- }
205
-
206
- let cent_ids = [];
207
- for ( const entry of entries ) {
208
- let entry_cent = this.cacheFS.get_entry_ei([entry.path, entry.uid]);
209
- if ( ! entry_cent ) {
210
- entry_cent = this.cacheFS.add_entry({ id: entry.uid });
211
- this.cacheFS.assoc_path(entry.path, entry.uid);
212
- }
213
- cent_ids.push(entry_cent.id);
214
- // TODO: update_stat_ is not implemented
215
- // this.cacheFS.update_stat_(entry_cent, entry, {
216
- // subdomains: ! o.no_assocs,
217
- // apps: ! o.no_assocs,
218
- // thumbnail: ! o.no_thumbs,
219
- // });
220
- entry_cent.stat = entry;
221
- entry_cent.stat_has = {
222
- subdomains: !o.no_assocs,
223
- apps: !o.no_assocs,
224
- thumbnail: !o.no_thumbs,
225
- };
226
- entry_cent.stat_exp = Date.now() + 1000 * 3;
227
- }
228
-
229
- cent.members = [];
230
- for ( const id of cent_ids ) {
231
- cent.members.push(id);
232
- }
233
- cent.members_exp = Date.now() + TTL;
234
-
235
- l.unlock();
236
-
237
- console.log('CACHE ENTRY?', cent);
238
-
239
- return entries;
240
- },
241
- },
242
- };
243
- }
@@ -1,40 +0,0 @@
1
- import putility from '@heyputer/putility';
2
- import { TFilesystem } from './definitions.js';
3
-
4
- const example = {
5
- 'id': 'f485f1ba-de07-422c-8c4b-c2da057d4a44',
6
- 'uid': 'f485f1ba-de07-422c-8c4b-c2da057d4a44',
7
- 'is_dir': true,
8
- 'immutable': true,
9
- 'name': 'Test',
10
- };
11
-
12
- export class PostMessageFilesystem extends putility.AdvancedBase {
13
- constructor ({ rpc, messageTarget }) {
14
- super();
15
- this.rpc = rpc;
16
- this.messageTarget = messageTarget;
17
- }
18
- static IMPLEMENTS = {
19
- [TFilesystem]: {
20
- stat: async function (o) {
21
- return example;
22
- },
23
- readdir: async function (o) {
24
- const tp = new putility.libs.promise.TeePromise();
25
- const $callback = this.rpc.registerCallback((result) => {
26
- tp.resolve(result);
27
- });
28
- // return [example];
29
- this.messageTarget.postMessage({
30
- $: 'puter-fs',
31
- $callback,
32
- op: 'readdir',
33
- args: o,
34
- }, '*');
35
-
36
- return await tp;
37
- },
38
- },
39
- };
40
- }
@@ -1,40 +0,0 @@
1
- import putility from '@heyputer/putility';
2
-
3
- export const TFilesystem = 'TFilesystem';
4
-
5
- // TODO: UNUSED (eventually putility will support these definitions)
6
- // This is here so that the idea is not forgotten.
7
- export const IFilesystem = {
8
- methods: {
9
- stat: {
10
- parameters: {
11
- path: {
12
- alias: 'uid',
13
- },
14
- },
15
- },
16
- },
17
-
18
- };
19
-
20
- export class ProxyFilesystem extends putility.AdvancedBase {
21
- static PROPERTIES = {
22
- delegate: () => {
23
- },
24
- };
25
- // TODO: constructor implied by properties
26
- constructor ({ delegate }) {
27
- super();
28
- this.delegate = delegate;
29
- }
30
- static IMPLEMENTS = {
31
- [TFilesystem]: {
32
- stat: async function (o) {
33
- return this.delegate.stat(o);
34
- },
35
- readdir: async function (o) {
36
- return this.delegate.readdir(o);
37
- },
38
- },
39
- };
40
- }
@@ -1,72 +0,0 @@
1
- import { RequestError } from '../lib/RequestError.js';
2
-
3
- export default class Threads {
4
- constructor (context) {
5
- this.authToken = context.authToken;
6
- this.APIOrigin = context.APIOrigin;
7
- }
8
- setAuthToken (authToken) {
9
- this.authToken = authToken;
10
- }
11
- setAPIOrigin (APIOrigin) {
12
- this.APIOrigin = APIOrigin;
13
- }
14
- async req_ (method, route, body) {
15
- const resp = await fetch(this.APIOrigin + route, {
16
- method,
17
- headers: {
18
- Authorization: `Bearer ${this.authToken}`,
19
- ...(body ? { 'Content-Type': 'application/json' } : {}),
20
- },
21
- ...(body ? { body: JSON.stringify(body) } : {}),
22
- });
23
- if ( ! resp.ok ) {
24
- const resp_data = await resp.json();
25
- const err = new RequestError(resp_data.message);
26
- err.response = resp_data;
27
- throw err;
28
- }
29
- return await resp.json();
30
- }
31
-
32
- async create (spec, parent) {
33
- if ( typeof spec === 'string' ) spec = { text: spec };
34
- return await this.req_('POST', '/threads/create', {
35
- ...spec,
36
- ...(parent ? { parent } : {}),
37
- });
38
- }
39
-
40
- async edit (uid, spec = {}) {
41
- if ( typeof spec === 'string' ) spec = { text: spec };
42
- await this.req_('PUT', `/threads/edit/${ encodeURIComponent(uid)}`, {
43
- ...spec,
44
- });
45
- }
46
-
47
- async delete (uid) {
48
- await this.req_('DELETE', `/threads/${ encodeURIComponent(uid)}`);
49
- }
50
-
51
- async list (uid, page, options) {
52
- return await this.req_('POST',
53
- `/threads/list/${ encodeURIComponent(uid) }/${ page}`,
54
- options ?? {});
55
- }
56
-
57
- async subscribe (uid, callback) {
58
- puter.fs.socket.emit('thread.sub-request', { uid });
59
-
60
- // socket.io, which we use unfortunatelly, doesn't handle
61
- // wildcard events, so we have to just put them all here.
62
- const events = [
63
- 'post', 'edit', 'delete', 'child-edit', 'child-delete',
64
- ];
65
-
66
- for ( const event of events ) {
67
- puter.fs.socket.on(`thread.${event}`, (data) => {
68
- if ( data.subscription === uid ) callback(event, data);
69
- });
70
- }
71
- }
72
- }
@@ -1,46 +0,0 @@
1
- import putility from '@heyputer/putility';
2
-
3
- const { TTopics } = putility.traits;
4
-
5
- /**
6
- * Manages the auth token and origin used to communicate with
7
- * Puter's API
8
- */
9
- export class APIAccessService extends putility.concepts.Service {
10
- static TOPICS = ['update'];
11
-
12
- static PROPERTIES = {
13
- auth_token: {
14
- post_set (v) {
15
- this.as(TTopics).pub('update');
16
- },
17
- },
18
- api_origin: {
19
- post_set () {
20
- this.as(TTopics).pub('update');
21
- },
22
- },
23
- };
24
-
25
- // TODO: inconsistent! Update all dependents.
26
- get_api_info () {
27
- const self = this;
28
- const o = {};
29
- [
30
- ['auth_token', 'auth_token'],
31
- ['authToken', 'auth_token'],
32
- ['APIOrigin', 'api_origin'],
33
- ['api_origin', 'api_origin'],
34
- ].forEach(([k1, k2]) => {
35
- Object.defineProperty(o, k1, {
36
- get () {
37
- return self[k2];
38
- },
39
- set (v) {
40
- return self;
41
- },
42
- });
43
- });
44
- return o;
45
- }
46
- }
@@ -1,20 +0,0 @@
1
- import putility from '@heyputer/putility';
2
-
3
- const example = {
4
- 'id': 'f485f1ba-de07-422c-8c4b-c2da057d4a44',
5
- 'uid': 'f485f1ba-de07-422c-8c4b-c2da057d4a44',
6
- 'is_dir': true,
7
- 'immutable': true,
8
- 'name': 'FromParentWindow',
9
- };
10
-
11
- export class FSRelayService extends putility.concepts.Service {
12
- async _init () {
13
- const services = this._.context.services;
14
- const util = this._.context.util;
15
- const svc_xdIncoming = services.get('xd-incoming');
16
- svc_xdIncoming.register_tagged_listener('puter-fs', event => {
17
- util.rpc.send(event.source, event.data.$callback, [example]);
18
- });
19
- }
20
- }