bleam 0.0.6 → 0.0.8

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 (86) hide show
  1. package/dist/ai.cjs +3984 -25
  2. package/dist/ai.d.cts +503 -20
  3. package/dist/ai.d.ts +503 -20
  4. package/dist/ai.js +3946 -26
  5. package/dist/animated.cjs +1 -1
  6. package/dist/animated.js +1 -1
  7. package/dist/app.cjs +92 -0
  8. package/dist/app.js +92 -0
  9. package/dist/cli.cjs +653 -86
  10. package/dist/cli.d.cts +89 -1
  11. package/dist/cli.d.ts +89 -1
  12. package/dist/cli.js +654 -88
  13. package/dist/{config-ClDGHNb3.d.ts → config-Cms0rvqg.d.ts} +1 -0
  14. package/dist/{config-D3gPaVMb.d.cts → config-CufOVJV3.d.cts} +1 -0
  15. package/dist/config.d.cts +1 -1
  16. package/dist/config.d.ts +1 -1
  17. package/dist/crypto.d.cts +0 -2
  18. package/dist/crypto.d.ts +1 -1
  19. package/dist/dev-protocol.d.cts +14 -1
  20. package/dist/dev-protocol.d.ts +14 -1
  21. package/dist/files-Bo7h9fik.cjs +468 -0
  22. package/dist/files-Ds1wT8C2.js +445 -0
  23. package/dist/files-DwA7pzr3.d.cts +99 -0
  24. package/dist/files-VrkQlKIT.d.ts +99 -0
  25. package/dist/files.cjs +7 -0
  26. package/dist/files.d.cts +2 -0
  27. package/dist/files.d.ts +2 -0
  28. package/dist/files.js +4 -0
  29. package/dist/index.d.cts +1 -1
  30. package/dist/index.d.ts +1 -1
  31. package/dist/native-sqlite-xcGdamRD.js +64 -0
  32. package/dist/native-sqlite-yQLD5s9i.cjs +66 -0
  33. package/dist/platform.cjs +27 -0
  34. package/dist/platform.d.cts +39 -0
  35. package/dist/platform.d.ts +39 -0
  36. package/dist/platform.js +21 -0
  37. package/dist/runtime.d.cts +1 -6
  38. package/dist/runtime.d.ts +1 -6
  39. package/dist/{schema-ipsECAso.d.ts → schema-DOOjfXvs.d.ts} +1 -1
  40. package/dist/{schema-BOuUHcqo.d.cts → schema-ENSMX_1t.d.cts} +1 -1
  41. package/dist/schema.cjs +1 -1
  42. package/dist/schema.d.cts +1 -1
  43. package/dist/schema.d.ts +1 -1
  44. package/dist/schema.js +1 -1
  45. package/dist/state-Bx0VlTlO.cjs +852 -0
  46. package/dist/state-CAwe-Vw1.js +767 -0
  47. package/dist/state.cjs +17 -763
  48. package/dist/state.d.cts +107 -77
  49. package/dist/state.d.ts +107 -77
  50. package/dist/state.js +4 -745
  51. package/dist/ui-1WepaMS4.d.cts +92 -0
  52. package/dist/ui-Ce-pnUUA.d.ts +92 -0
  53. package/dist/ui.cjs +286 -35
  54. package/dist/ui.d.cts +2 -30
  55. package/dist/ui.d.ts +2 -30
  56. package/dist/ui.js +283 -36
  57. package/dist/window.cjs +596 -0
  58. package/dist/window.d.cts +63 -0
  59. package/dist/window.d.ts +63 -0
  60. package/dist/window.js +589 -0
  61. package/package.json +16 -14
  62. package/templates/basic/app/index.tsx +8 -5
  63. package/templates/foundation-models/app/index.tsx +32 -0
  64. package/templates/foundation-models/app.config.ts +5 -0
  65. package/templates/image-generation/app/index.tsx +39 -0
  66. package/templates/image-generation/app.config.ts +5 -0
  67. package/templates/state/app/index.tsx +30 -0
  68. package/templates/state/app.config.ts +5 -0
  69. package/dist/chrome.cjs +0 -265
  70. package/dist/chrome.d.cts +0 -19
  71. package/dist/chrome.d.ts +0 -19
  72. package/dist/chrome.js +0 -262
  73. package/dist/fs.cjs +0 -13
  74. package/dist/fs.d.cts +0 -8
  75. package/dist/fs.d.ts +0 -8
  76. package/dist/fs.js +0 -12
  77. package/dist/native-macos-D6p2SmUj.js +0 -20
  78. package/dist/native-macos-DLwo_ii9.cjs +0 -31
  79. package/dist/native-runtime-CbPzrs8j.d.cts +0 -33
  80. package/dist/native-runtime-DxHSrsTX.d.ts +0 -33
  81. package/dist/native.cjs +0 -135
  82. package/dist/native.d.cts +0 -34
  83. package/dist/native.d.ts +0 -34
  84. package/dist/native.js +0 -135
  85. /package/dist/{schema-DVmoD9JL.js → schema-Bo5Jvqus.js} +0 -0
  86. /package/dist/{schema-bxeeCyLn.cjs → schema-rQ13mrpD.cjs} +0 -0
@@ -12,6 +12,7 @@ interface BleamConfig {
12
12
  version?: string;
13
13
  buildNumber?: string;
14
14
  bundleIdentifier?: string;
15
+ appGroups?: string[];
15
16
  teamId?: string;
16
17
  appearance?: BleamAppearance;
17
18
  }
@@ -12,6 +12,7 @@ interface BleamConfig {
12
12
  version?: string;
13
13
  buildNumber?: string;
14
14
  bundleIdentifier?: string;
15
+ appGroups?: string[];
15
16
  teamId?: string;
16
17
  appearance?: BleamAppearance;
17
18
  }
package/dist/config.d.cts CHANGED
@@ -1,2 +1,2 @@
1
- import { a as HexColor, c as defineConfig, i as BleamUserInterfaceStyle, n as BleamConfig, o as appConfig, r as BleamInternalExpoConfig, s as createExpoConfig, t as BleamAppearance } from "./config-D3gPaVMb.cjs";
1
+ import { a as HexColor, c as defineConfig, i as BleamUserInterfaceStyle, n as BleamConfig, o as appConfig, r as BleamInternalExpoConfig, s as createExpoConfig, t as BleamAppearance } from "./config-CufOVJV3.cjs";
2
2
  export { BleamAppearance, BleamConfig, BleamInternalExpoConfig, BleamUserInterfaceStyle, HexColor, appConfig, createExpoConfig, defineConfig };
package/dist/config.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- import { a as HexColor, c as defineConfig, i as BleamUserInterfaceStyle, n as BleamConfig, o as appConfig, r as BleamInternalExpoConfig, s as createExpoConfig, t as BleamAppearance } from "./config-ClDGHNb3.js";
1
+ import { a as HexColor, c as defineConfig, i as BleamUserInterfaceStyle, n as BleamConfig, o as appConfig, r as BleamInternalExpoConfig, s as createExpoConfig, t as BleamAppearance } from "./config-Cms0rvqg.js";
2
2
  export { BleamAppearance, BleamConfig, BleamInternalExpoConfig, BleamUserInterfaceStyle, HexColor, appConfig, createExpoConfig, defineConfig };
package/dist/crypto.d.cts CHANGED
@@ -1,5 +1,3 @@
1
- import "./native-runtime-CbPzrs8j.cjs";
2
-
3
1
  //#region src/crypto.d.ts
4
2
  interface BleamCrypto {
5
3
  randomUUID(): string;
package/dist/crypto.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import "./native-runtime-DxHSrsTX.js";
1
+ import "@nativescript/react-native";
2
2
 
3
3
  //#region src/crypto.d.ts
4
4
  interface BleamCrypto {
@@ -10,7 +10,20 @@ interface BleamManifest {
10
10
  dev: boolean;
11
11
  }
12
12
  declare const BLEAM_MANIFEST_PATH = "/bleam/manifest.json";
13
+ type BleamDevLogLevel = 'debug' | 'info' | 'warn' | 'error';
14
+ type BleamDevLogSource = 'cli' | 'metro' | 'runtime' | 'js' | 'native' | 'xpc';
15
+ interface BleamDevLogEvent {
16
+ type: 'log';
17
+ id: string;
18
+ time: string;
19
+ source: BleamDevLogSource;
20
+ level: BleamDevLogLevel;
21
+ category?: string;
22
+ message: string;
23
+ details?: unknown;
24
+ jobId?: string;
25
+ }
13
26
  declare function createBleamDevBundleUrl(origin: string): string;
14
27
  declare function isBleamManifest(value: unknown): value is BleamManifest;
15
28
  //#endregion
16
- export { BLEAM_MANIFEST_PATH, BleamManifest, BleamPlatform, createBleamDevBundleUrl, isBleamManifest };
29
+ export { BLEAM_MANIFEST_PATH, BleamDevLogEvent, BleamDevLogLevel, BleamDevLogSource, BleamManifest, BleamPlatform, createBleamDevBundleUrl, isBleamManifest };
@@ -10,7 +10,20 @@ interface BleamManifest {
10
10
  dev: boolean;
11
11
  }
12
12
  declare const BLEAM_MANIFEST_PATH = "/bleam/manifest.json";
13
+ type BleamDevLogLevel = 'debug' | 'info' | 'warn' | 'error';
14
+ type BleamDevLogSource = 'cli' | 'metro' | 'runtime' | 'js' | 'native' | 'xpc';
15
+ interface BleamDevLogEvent {
16
+ type: 'log';
17
+ id: string;
18
+ time: string;
19
+ source: BleamDevLogSource;
20
+ level: BleamDevLogLevel;
21
+ category?: string;
22
+ message: string;
23
+ details?: unknown;
24
+ jobId?: string;
25
+ }
13
26
  declare function createBleamDevBundleUrl(origin: string): string;
14
27
  declare function isBleamManifest(value: unknown): value is BleamManifest;
15
28
  //#endregion
16
- export { BLEAM_MANIFEST_PATH, BleamManifest, BleamPlatform, createBleamDevBundleUrl, isBleamManifest };
29
+ export { BLEAM_MANIFEST_PATH, BleamDevLogEvent, BleamDevLogLevel, BleamDevLogSource, BleamManifest, BleamPlatform, createBleamDevBundleUrl, isBleamManifest };
@@ -0,0 +1,468 @@
1
+ const require_native_runtime = require('./native-runtime-CsXnXkQn.cjs');
2
+
3
+ //#region src/native-files.ts
4
+ const downloads = /* @__PURE__ */ new Map();
5
+ let session;
6
+ let sessionDelegate;
7
+ let downloadRetainer;
8
+ function settlePicker(callback) {
9
+ setTimeout(callback, 0);
10
+ }
11
+ function retainer() {
12
+ downloadRetainer ??= require_native_runtime.ensureNativeScript().createRetainer();
13
+ return downloadRetainer;
14
+ }
15
+ function fileUri$1(path) {
16
+ return path.startsWith("file://") ? path : `file://${path}`;
17
+ }
18
+ function nsString(value) {
19
+ return NSString.stringWithString(value);
20
+ }
21
+ function fileManager() {
22
+ require_native_runtime.ensureNativeScript();
23
+ return NSFileManager.defaultManager;
24
+ }
25
+ function ensureDirectory(path) {
26
+ fileManager().createDirectoryAtPathWithIntermediateDirectoriesAttributesError(path, true, null, null);
27
+ }
28
+ function parentDirectory(path) {
29
+ return nsString(path).stringByDeletingLastPathComponent;
30
+ }
31
+ function appGroupIdentifier() {
32
+ return String(NSBundle.mainBundle.objectForInfoDictionaryKey("AppGroupIdentifier") ?? "group.dev.bleam.app");
33
+ }
34
+ function containerPath() {
35
+ const url = fileManager().containerURLForSecurityApplicationGroupIdentifier(appGroupIdentifier());
36
+ if (!url?.path) throw new Error("Shared container is unavailable");
37
+ return String(url.path);
38
+ }
39
+ function searchPath(directory) {
40
+ const value = NSSearchPathForDirectoriesInDomains(directory, NSSearchPathDomainMask.UserDomain, true)?.objectAtIndex(0);
41
+ if (typeof value !== "string") throw new Error("Native directory unavailable");
42
+ return value;
43
+ }
44
+ function nativeDirectories() {
45
+ const shared = containerPath();
46
+ return {
47
+ cache: searchPath(NSSearchPathDirectory.Caches),
48
+ documents: searchPath(NSSearchPathDirectory.Document),
49
+ models: nsString(shared).stringByAppendingPathComponent("Models"),
50
+ shared,
51
+ temporary: NSTemporaryDirectory()
52
+ };
53
+ }
54
+ function nativeStat(path) {
55
+ if (!fileManager().fileExistsAtPath(path)) return {
56
+ exists: false,
57
+ type: "missing",
58
+ path,
59
+ uri: fileUri$1(path)
60
+ };
61
+ const attributes = fileManager().attributesOfItemAtPathError(path, null);
62
+ const size = Number(attributes?.objectForKey(NSFileSize) ?? void 0);
63
+ const createdAt = attributes?.objectForKey(NSFileCreationDate);
64
+ const modifiedAt = attributes?.objectForKey(NSFileModificationDate);
65
+ return {
66
+ exists: true,
67
+ type: attributes?.objectForKey(NSFileType) === NSFileTypeDirectory ? "directory" : "file",
68
+ path,
69
+ uri: fileUri$1(path),
70
+ size: Number.isFinite(size) ? size : void 0,
71
+ createdAt: createdAt ? createdAt.timeIntervalSince1970 * 1e3 : void 0,
72
+ modifiedAt: modifiedAt ? modifiedAt.timeIntervalSince1970 * 1e3 : void 0
73
+ };
74
+ }
75
+ async function nativeMkdir(path, recursive) {
76
+ fileManager().createDirectoryAtPathWithIntermediateDirectoriesAttributesError(path, recursive, null, null);
77
+ }
78
+ async function nativeRemove(path, recursive) {
79
+ const stat = nativeStat(path);
80
+ if (!stat.exists) return;
81
+ if (stat.type === "directory" && !recursive) {
82
+ const items = fileManager().contentsOfDirectoryAtPathError(path, null);
83
+ if (Number(items?.count ?? 0) > 0) throw new Error(`Directory is not empty: ${path}`);
84
+ }
85
+ fileManager().removeItemAtPathError(path, null);
86
+ }
87
+ async function nativeReadText(path) {
88
+ return String(NSString.stringWithContentsOfFileEncodingError(path, NSUTF8StringEncoding, null));
89
+ }
90
+ async function nativeWriteText(path, contents, atomic) {
91
+ ensureDirectory(parentDirectory(path));
92
+ if (!nsString(contents).writeToFileAtomicallyEncodingError(path, atomic, NSUTF8StringEncoding, null)) throw new Error(`Failed to write file: ${path}`);
93
+ }
94
+ function taskId(task) {
95
+ return Number(task.taskIdentifier);
96
+ }
97
+ function downloadSession() {
98
+ if (session) return session;
99
+ sessionDelegate = require_native_runtime.ensureNativeScript().createDelegate("NSURLSessionDataDelegate", {
100
+ URLSessionDataTaskDidReceiveResponseCompletionHandler(_session, task, response, completion) {
101
+ const state = downloads.get(taskId(task));
102
+ if (!state) {
103
+ completion(NSURLSessionResponseDisposition.Cancel);
104
+ return;
105
+ }
106
+ const http = response;
107
+ if (http.statusCode && (http.statusCode < 200 || http.statusCode >= 300)) {
108
+ completion(NSURLSessionResponseDisposition.Cancel);
109
+ state.output.close();
110
+ retainer().release(state.output);
111
+ state.reject(/* @__PURE__ */ new Error(`Download failed: HTTP ${http.statusCode}`));
112
+ downloads.delete(taskId(task));
113
+ return;
114
+ }
115
+ state.output.open();
116
+ completion(NSURLSessionResponseDisposition.Allow);
117
+ },
118
+ URLSessionDataTaskDidReceiveData(_session, task, data) {
119
+ const state = downloads.get(taskId(task));
120
+ if (!state) return;
121
+ const bytes = data.bytes;
122
+ const written = state.output.writeMaxLength(bytes, data.length);
123
+ if (written < 0) {
124
+ task.cancel();
125
+ state.output.close();
126
+ retainer().release(state.output);
127
+ state.reject(/* @__PURE__ */ new Error(`Failed to write download: ${state.path}`));
128
+ downloads.delete(taskId(task));
129
+ return;
130
+ }
131
+ state.writtenBytes += written;
132
+ state.onProgress?.({
133
+ id: state.id,
134
+ path: state.path,
135
+ bytesWritten: state.writtenBytes,
136
+ totalBytes: state.expectedBytes
137
+ });
138
+ },
139
+ URLSessionTaskDidCompleteWithError(_session, task, error) {
140
+ const state = downloads.get(taskId(task));
141
+ if (!state) return;
142
+ downloads.delete(taskId(task));
143
+ state.output.close();
144
+ retainer().release(state.output);
145
+ if (error) {
146
+ state.reject(error);
147
+ return;
148
+ }
149
+ if (state.expectedBytes !== void 0 && state.writtenBytes !== state.expectedBytes) {
150
+ state.reject(/* @__PURE__ */ new Error(`Downloaded ${state.writtenBytes} bytes, expected ${state.expectedBytes}`));
151
+ return;
152
+ }
153
+ if (!state.temporaryPath || state.temporaryPath === state.path) {
154
+ state.resolve(nativeStat(state.path));
155
+ return;
156
+ }
157
+ fileManager().removeItemAtPathError(state.path, null);
158
+ if (!fileManager().moveItemAtPathToPathError(state.temporaryPath, state.path, null)) {
159
+ state.reject(/* @__PURE__ */ new Error(`Failed to move download into place: ${state.path}`));
160
+ return;
161
+ }
162
+ state.resolve(nativeStat(state.path));
163
+ }
164
+ }, { retainer: retainer() });
165
+ const configuration = NSURLSessionConfiguration.defaultSessionConfiguration;
166
+ session = NSURLSession.sessionWithConfigurationDelegateDelegateQueue(configuration, sessionDelegate, null);
167
+ return session;
168
+ }
169
+ function nativeCancelDownload(id) {
170
+ for (const state of downloads.values()) if (state.id === id) state.task?.cancel();
171
+ }
172
+ async function nativeDownload(request) {
173
+ ensureDirectory(parentDirectory(request.destination));
174
+ const url = NSURL.URLWithString(request.url);
175
+ if (!url) throw new Error(`Invalid download URL: ${request.url}`);
176
+ const temporaryPath = request.atomic ? `${request.destination}.download-${request.id}` : request.destination;
177
+ fileManager().removeItemAtPathError(temporaryPath, null);
178
+ const output = NSOutputStream.outputStreamToFileAtPathAppend(temporaryPath, false);
179
+ if (!output) throw new Error(`Failed to create output stream: ${temporaryPath}`);
180
+ retainer().retain(output);
181
+ const nativeRequest = NSMutableURLRequest.requestWithURL(url);
182
+ for (const [key, value] of Object.entries(request.headers ?? {})) nativeRequest.setValueForHTTPHeaderField(value, key);
183
+ return new Promise((resolve, reject) => {
184
+ const task = downloadSession().dataTaskWithRequest(nativeRequest);
185
+ const state = {
186
+ id: request.id,
187
+ path: request.destination,
188
+ temporaryPath,
189
+ expectedBytes: request.expectedBytes,
190
+ writtenBytes: 0,
191
+ output,
192
+ task,
193
+ resolve,
194
+ reject,
195
+ onProgress: request.onProgress
196
+ };
197
+ downloads.set(taskId(task), state);
198
+ task.resume();
199
+ });
200
+ }
201
+ function topVisibleViewController(root = UIApplication.sharedApplication.keyWindow?.rootViewController) {
202
+ let current = root ?? null;
203
+ while (current?.presentedViewController) current = current.presentedViewController;
204
+ const selected = current?.selectedViewController;
205
+ if (selected) return topVisibleViewController(selected);
206
+ const visible = current?.visibleViewController;
207
+ if (visible) return topVisibleViewController(visible);
208
+ return current;
209
+ }
210
+ function copyPickedFile(url, destination) {
211
+ ensureDirectory(destination);
212
+ const name = String(url.lastPathComponent ?? `picked-${Date.now()}`);
213
+ const target = nsString(destination).stringByAppendingPathComponent(name);
214
+ fileManager().removeItemAtPathError(target, null);
215
+ if (!fileManager().copyItemAtURLToURLError(url, NSURL.fileURLWithPath(target), null)) throw new Error(`Failed to copy picked file: ${name}`);
216
+ return nativeStat(target);
217
+ }
218
+ async function nativePick(request) {
219
+ return new Promise((resolve, reject) => {
220
+ require_native_runtime.ensureNativeScript().runOnUI(() => {
221
+ const pickerTypes = request.type === "image" ? ["public.image"] : ["public.item"];
222
+ const picker = UIDocumentPickerViewController.alloc().initWithDocumentTypesInMode(pickerTypes, UIDocumentPickerMode.Import);
223
+ picker.allowsMultipleSelection = request.multiple;
224
+ require_native_runtime.ensureNativeScript().createDelegate("UIDocumentPickerDelegate", {
225
+ documentPickerDidPickDocumentsAtURLs(controller, urls) {
226
+ try {
227
+ const values = [];
228
+ const count = Number(urls.count ?? 0);
229
+ for (let index = 0; index < count; index += 1) values.push(copyPickedFile(urls.objectAtIndex(index), request.destination));
230
+ controller.dismissViewControllerAnimatedCompletion(true, () => null);
231
+ settlePicker(() => resolve(values));
232
+ } catch (error) {
233
+ settlePicker(() => reject(error));
234
+ }
235
+ },
236
+ documentPickerWasCancelled(controller) {
237
+ controller.dismissViewControllerAnimatedCompletion(true, () => null);
238
+ settlePicker(() => resolve([]));
239
+ }
240
+ }, { assignTo: {
241
+ object: picker,
242
+ property: "delegate"
243
+ } });
244
+ const presenter = topVisibleViewController();
245
+ if (!presenter) {
246
+ settlePicker(() => reject(/* @__PURE__ */ new Error("No view controller available for file picker")));
247
+ return;
248
+ }
249
+ presenter.presentViewControllerAnimatedCompletion(picker, true, () => null);
250
+ });
251
+ });
252
+ }
253
+
254
+ //#endregion
255
+ //#region src/files.ts
256
+ const paths = {
257
+ get cache() {
258
+ return nativeDirectories().cache;
259
+ },
260
+ get documents() {
261
+ return nativeDirectories().documents;
262
+ },
263
+ get models() {
264
+ return nativeDirectories().models;
265
+ },
266
+ get shared() {
267
+ return nativeDirectories().shared;
268
+ },
269
+ get temporary() {
270
+ return nativeDirectories().temporary;
271
+ }
272
+ };
273
+ let directoryPromise;
274
+ async function directories() {
275
+ directoryPromise ??= Promise.resolve(nativeDirectories());
276
+ return directoryPromise;
277
+ }
278
+ function joinPath(...parts) {
279
+ const [first, ...rest] = parts.map((part) => typeof part === "string" ? part : part.path);
280
+ if (!first) throw new Error("Expected path");
281
+ return rest.reduce((path, part) => {
282
+ const next = part.replace(/^\/+/, "");
283
+ if (!next) return path;
284
+ return `${path.replace(/\/+$/, "")}/${next}`;
285
+ }, first);
286
+ }
287
+ function fileUri(path) {
288
+ return path.startsWith("file://") ? path : `file://${path}`;
289
+ }
290
+ function downloadId() {
291
+ return `download-${Date.now()}-${Math.random().toString(36).slice(2)}`;
292
+ }
293
+ function statFromNative(value) {
294
+ if (!value || typeof value !== "object") throw new Error("Invalid native file stat");
295
+ const record = value;
296
+ const path = stringValue(record.path, "path");
297
+ const type = stringValue(record.type, "type");
298
+ if (type !== "file" && type !== "directory" && type !== "missing") throw new Error(`Invalid native file type: ${type}`);
299
+ return {
300
+ exists: Boolean(record.exists),
301
+ type,
302
+ path,
303
+ uri: stringValue(record.uri, "uri"),
304
+ size: optionalNumber(record.size),
305
+ createdAt: optionalNumber(record.createdAt),
306
+ modifiedAt: optionalNumber(record.modifiedAt)
307
+ };
308
+ }
309
+ function stringValue(value, name) {
310
+ if (typeof value !== "string") throw new Error(`Expected ${name}`);
311
+ return value;
312
+ }
313
+ function optionalNumber(value) {
314
+ return typeof value === "number" && Number.isFinite(value) ? value : void 0;
315
+ }
316
+ function progressFromNative(value) {
317
+ const totalBytes = optionalNumber(value.totalBytes);
318
+ return {
319
+ id: value.id,
320
+ path: value.path,
321
+ bytesWritten: value.bytesWritten,
322
+ totalBytes,
323
+ fraction: totalBytes && totalBytes > 0 ? value.bytesWritten / totalBytes : void 0
324
+ };
325
+ }
326
+ function pickDestinationPath(destination) {
327
+ return typeof destination === "string" ? destination : destination.path;
328
+ }
329
+ function pickedFilesFromNative(value) {
330
+ if (!Array.isArray(value)) throw new Error("Invalid native pick result");
331
+ return value.map((item) => {
332
+ return new FileRef(statFromNative(item).path);
333
+ });
334
+ }
335
+ var FileRef = class {
336
+ uri;
337
+ constructor(path) {
338
+ this.path = path;
339
+ this.uri = fileUri(path);
340
+ }
341
+ async exists() {
342
+ return (await this.stat()).type === "file";
343
+ }
344
+ async stat() {
345
+ return statFromNative(nativeStat(this.path));
346
+ }
347
+ async text() {
348
+ const value = await nativeReadText(this.path);
349
+ if (typeof value !== "string") throw new Error("Expected native file text");
350
+ return value;
351
+ }
352
+ async json() {
353
+ return JSON.parse(await this.text());
354
+ }
355
+ async write(contents, options = {}) {
356
+ await nativeWriteText(this.path, contents, options.atomic ?? true);
357
+ }
358
+ async delete() {
359
+ await nativeRemove(this.path, false);
360
+ }
361
+ download(url, options = {}) {
362
+ return new DownloadTask(url, this.path, options);
363
+ }
364
+ };
365
+ var DirectoryRef = class DirectoryRef {
366
+ uri;
367
+ constructor(path) {
368
+ this.path = path;
369
+ this.uri = fileUri(path);
370
+ }
371
+ file(...parts) {
372
+ return new FileRef(joinPath(this.path, ...parts));
373
+ }
374
+ directory(...parts) {
375
+ return new DirectoryRef(joinPath(this.path, ...parts));
376
+ }
377
+ async exists() {
378
+ return (await this.stat()).type === "directory";
379
+ }
380
+ async stat() {
381
+ return statFromNative(nativeStat(this.path));
382
+ }
383
+ async create(options = {}) {
384
+ await nativeMkdir(this.path, options.recursive ?? true);
385
+ }
386
+ async delete(options = {}) {
387
+ await nativeRemove(this.path, options.recursive ?? true);
388
+ }
389
+ };
390
+ var DownloadTask = class {
391
+ id;
392
+ result;
393
+ onAbort = () => {
394
+ this.cancel();
395
+ };
396
+ constructor(url, destination, options = {}) {
397
+ this.url = url;
398
+ this.destination = destination;
399
+ this.options = options;
400
+ this.id = options.id ?? downloadId();
401
+ options.signal?.addEventListener("abort", this.onAbort, { once: true });
402
+ this.result = this.start().finally(() => {
403
+ options.signal?.removeEventListener("abort", this.onAbort);
404
+ });
405
+ }
406
+ async cancel() {
407
+ nativeCancelDownload(this.id);
408
+ }
409
+ async start() {
410
+ return new FileRef(statFromNative(await nativeDownload({
411
+ id: this.id,
412
+ url: this.url,
413
+ destination: this.destination,
414
+ headers: this.options.headers,
415
+ expectedBytes: this.options.expectedBytes,
416
+ atomic: this.options.atomic ?? true,
417
+ resume: this.options.resume ?? true,
418
+ onProgress: (event) => {
419
+ if (event.id === this.id) this.options.onProgress?.(progressFromNative(event));
420
+ }
421
+ })).path);
422
+ }
423
+ };
424
+ const files = {
425
+ paths,
426
+ async directories() {
427
+ return directories();
428
+ },
429
+ async pick(options) {
430
+ return pickedFilesFromNative(await nativePick({
431
+ type: options.type ?? "any",
432
+ multiple: options.multiple ?? false,
433
+ destination: pickDestinationPath(options.destination)
434
+ }));
435
+ },
436
+ file(...parts) {
437
+ return new FileRef(joinPath(...parts));
438
+ },
439
+ directory(...parts) {
440
+ return new DirectoryRef(joinPath(...parts));
441
+ }
442
+ };
443
+
444
+ //#endregion
445
+ Object.defineProperty(exports, 'DirectoryRef', {
446
+ enumerable: true,
447
+ get: function () {
448
+ return DirectoryRef;
449
+ }
450
+ });
451
+ Object.defineProperty(exports, 'DownloadTask', {
452
+ enumerable: true,
453
+ get: function () {
454
+ return DownloadTask;
455
+ }
456
+ });
457
+ Object.defineProperty(exports, 'FileRef', {
458
+ enumerable: true,
459
+ get: function () {
460
+ return FileRef;
461
+ }
462
+ });
463
+ Object.defineProperty(exports, 'files', {
464
+ enumerable: true,
465
+ get: function () {
466
+ return files;
467
+ }
468
+ });