@octane-xplat/files 0.11.0 → 0.11.1

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.
@@ -0,0 +1,144 @@
1
+ import { assertSafeFileName, concatBytes, exactBuffer, fileTooLarge, throwIfAborted } from "./file-bytes.js";
2
+ import { desktopHost } from "@octane-xplat/platform/host/web";
3
+ //#region src/files.web.ts
4
+ var objectBlobs = /* @__PURE__ */ new Map();
5
+ function mintObjectUrl(blob) {
6
+ const uri = URL.createObjectURL(blob);
7
+ objectBlobs.set(uri, blob);
8
+ return uri;
9
+ }
10
+ function chooseFiles(accept, multiple) {
11
+ return new Promise((resolve) => {
12
+ const input = document.createElement("input");
13
+ input.type = "file";
14
+ input.accept = accept;
15
+ input.multiple = multiple;
16
+ input.onchange = () => {
17
+ const refs = [];
18
+ for (const file of Array.from(input.files ?? [])) refs.push({
19
+ name: file.name,
20
+ uri: mintObjectUrl(file)
21
+ });
22
+ resolve(refs);
23
+ };
24
+ input.oncancel = () => resolve([]);
25
+ input.click();
26
+ });
27
+ }
28
+ async function fetchBytes(uri, opts) {
29
+ const response = await fetch(uri, { signal: opts?.signal });
30
+ if (!response.ok) throw new Error(`Unable to read file: ${uri}`);
31
+ if (opts?.maxBytes == null) return new Uint8Array(await response.arrayBuffer());
32
+ const reader = response.body?.getReader();
33
+ if (!reader) {
34
+ const bytes = new Uint8Array(await response.arrayBuffer());
35
+ if (bytes.byteLength > opts.maxBytes) throw fileTooLarge(uri, opts.maxBytes);
36
+ return bytes;
37
+ }
38
+ const chunks = [];
39
+ let total = 0;
40
+ try {
41
+ while (true) {
42
+ const { done, value } = await reader.read();
43
+ if (done) break;
44
+ total += value.byteLength;
45
+ if (total > opts.maxBytes) {
46
+ await reader.cancel().catch(() => {});
47
+ throw fileTooLarge(uri, opts.maxBytes);
48
+ }
49
+ chunks.push(value);
50
+ }
51
+ } finally {
52
+ reader.releaseLock();
53
+ }
54
+ return concatBytes(chunks, total);
55
+ }
56
+ var files = {
57
+ async pickMultiple(accept = "*/*", _opts) {
58
+ return chooseFiles(accept, true);
59
+ },
60
+ async pick(accept = "*/*", opts) {
61
+ const host = desktopHost();
62
+ if (host && await host.supports("files", "pick")) return host.files.pick(accept, { startingFolder: opts?.startingFolder });
63
+ return (await chooseFiles(accept, false))[0] ?? null;
64
+ },
65
+ async readText(ref) {
66
+ const host = desktopHost();
67
+ if (host && await host.supports("files", "readText")) {
68
+ const text = await host.files.readText(ref.uri);
69
+ if (text === null) throw new Error(`Unable to read host file: ${ref.uri}`);
70
+ return text;
71
+ }
72
+ return (await fetch(ref.uri)).text();
73
+ },
74
+ async readBytes(ref, opts) {
75
+ throwIfAborted(opts?.signal);
76
+ const blob = objectBlobs.get(ref.uri);
77
+ if (blob) {
78
+ if (opts?.maxBytes != null && blob.size > opts.maxBytes) throw fileTooLarge(ref.uri, opts.maxBytes);
79
+ const bytes = new Uint8Array(await blob.arrayBuffer());
80
+ throwIfAborted(opts?.signal);
81
+ return bytes;
82
+ }
83
+ return fetchBytes(ref.uri, opts);
84
+ },
85
+ /** Desktop hosts write the selected path; plain web starts a download. */
86
+ async writeText(name, text) {
87
+ assertSafeFileName(name);
88
+ const host = desktopHost();
89
+ if (host && await host.supports("files", "writeText")) {
90
+ const file = await host.files.writeText(name, text);
91
+ if (!file) throw new Error("Host file save was cancelled");
92
+ return file;
93
+ }
94
+ const uri = mintObjectUrl(new Blob([text], { type: "text/plain" }));
95
+ const a = document.createElement("a");
96
+ a.href = uri;
97
+ a.download = name;
98
+ a.click();
99
+ return {
100
+ name,
101
+ uri
102
+ };
103
+ },
104
+ /** The desktop host contract is text-only, so bytes always download. */
105
+ async writeBytes(name, bytes) {
106
+ assertSafeFileName(name);
107
+ const uri = mintObjectUrl(new Blob([exactBuffer(bytes)], { type: "application/octet-stream" }));
108
+ const a = document.createElement("a");
109
+ a.href = uri;
110
+ a.download = name;
111
+ a.click();
112
+ return {
113
+ name,
114
+ uri
115
+ };
116
+ },
117
+ async export(name, bytes, opts) {
118
+ assertSafeFileName(name);
119
+ const picker = window.showSaveFilePicker;
120
+ if (typeof picker !== "function") return "unavailable";
121
+ const extension = name.includes(".") ? `.${name.split(".").pop()}` : null;
122
+ const types = opts?.mimeType && extension ? [{ accept: { [opts.mimeType]: [extension] } }] : void 0;
123
+ try {
124
+ const writable = await (await picker.call(window, {
125
+ suggestedName: name,
126
+ types
127
+ })).createWritable();
128
+ await writable.write(bytes);
129
+ await writable.close();
130
+ return "saved";
131
+ } catch (error) {
132
+ if (error?.name === "AbortError") return "cancelled";
133
+ throw error;
134
+ }
135
+ },
136
+ release(ref) {
137
+ if (ref.uri.startsWith("blob:")) {
138
+ objectBlobs.delete(ref.uri);
139
+ URL.revokeObjectURL(ref.uri);
140
+ }
141
+ }
142
+ };
143
+ //#endregion
144
+ export { files };
@@ -0,0 +1,3 @@
1
+ import { FileInput } from "./FileInput.js";
2
+ import { files } from "./files.js";
3
+ export { FileInput, files };
@@ -0,0 +1,8 @@
1
+ {
2
+ "version": 1,
3
+ "buildId": "2929db87-50ff-4e14-869f-567470f2d2ad",
4
+ "mode": "production",
5
+ "capabilities": {
6
+ "independentHydration": false
7
+ }
8
+ }
File without changes
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@octane-xplat/files",
3
- "version": "0.11.0",
3
+ "version": "0.11.1",
4
4
  "description": "Native + web file pick and read/write for Octane xplat — matching web + NativeScript implementations",
5
5
  "keywords": [
6
6
  "android",
@@ -22,6 +22,7 @@
22
22
  "directory": "packages/files"
23
23
  },
24
24
  "files": [
25
+ "dist",
25
26
  "src",
26
27
  "types"
27
28
  ],
@@ -32,24 +33,64 @@
32
33
  "types": "./types/index.macos.d.ts",
33
34
  "default": "./src/index.macos.ts"
34
35
  },
36
+ "linux": "./dist/linux/index.js",
35
37
  "types": "./types/index.d.ts",
36
- "default": "./src/index.ts"
38
+ "web": "./dist/web/index.js",
39
+ "native": "./dist/native/index.js",
40
+ "default": "./dist/native/index.js"
37
41
  },
38
- "./*": "./src/*"
42
+ "./*": {
43
+ "linux": "./dist/linux/*.js",
44
+ "types": "./src/*.ts",
45
+ "web": "./dist/web/*.js",
46
+ "native": "./dist/native/*.js",
47
+ "default": "./dist/native/*.js"
48
+ },
49
+ "./*.ts": {
50
+ "types": "./src/*.ts",
51
+ "default": "./dist/native/*.js"
52
+ },
53
+ "./*.web.ts": {
54
+ "types": "./src/*.web.ts",
55
+ "default": "./dist/web/*.js"
56
+ },
57
+ "./*.macos.ts": "./src/*.macos.ts",
58
+ "./*.linux.ts": {
59
+ "types": "./src/*.linux.ts",
60
+ "default": "./dist/linux/*.js"
61
+ },
62
+ "./*.tsrx": {
63
+ "types": "./src/*.tsrx",
64
+ "default": "./dist/native/*.js"
65
+ },
66
+ "./*.web.tsrx": {
67
+ "types": "./src/*.web.tsrx",
68
+ "default": "./dist/web/*.js"
69
+ },
70
+ "./*.macos.tsrx": "./src/*.macos.tsrx",
71
+ "./*.linux.tsrx": {
72
+ "types": "./src/*.linux.tsrx",
73
+ "default": "./dist/linux/*.js"
74
+ },
75
+ "./*.web": "./dist/web/*.js",
76
+ "./*.macos": "./src/*.macos.ts",
77
+ "./*.linux": "./dist/linux/*.js"
39
78
  },
40
79
  "publishConfig": {
41
80
  "access": "public"
42
81
  },
43
82
  "dependencies": {
44
83
  "@nativescript-community/ui-document-picker": "1.1.29",
45
- "@octane-xplat/platform": "0.11.0"
84
+ "@octane-xplat/platform": "0.11.1"
46
85
  },
47
86
  "devDependencies": {
48
87
  "@nativescript-community/octane": "0.2.4",
49
88
  "@nativescript/core": "9.1.2",
50
- "@nativescript/types": "9.0.0",
51
- "@octane-xplat/ui": "0.11.0",
52
- "octane": "0.6.3"
89
+ "@nativescript/types": "9.1.4",
90
+ "@octane-xplat/ui": "0.11.1",
91
+ "octane": "0.6.3",
92
+ "@octanejs/vite-plugin": "0.1.61",
93
+ "vite": "8.3.0"
53
94
  },
54
95
  "peerDependencies": {
55
96
  "@nativescript-community/octane": ">=0.2.1 <1",
@@ -72,6 +113,7 @@
72
113
  }
73
114
  },
74
115
  "scripts": {
75
- "test:packed": "node tests/packed-consumer.mjs"
116
+ "test:packed": "node tests/packed-consumer.mjs",
117
+ "build": "vite build && vite build --mode native && vite build --mode linux && node ../../scripts/check-native-dist.mjs"
76
118
  }
77
119
  }