@pistonite/pure 0.24.0 → 0.24.2
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.
- package/package.json +3 -3
- package/src/fs/FsSave.ts +28 -17
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pistonite/pure",
|
|
3
|
-
"version": "0.24.
|
|
3
|
+
"version": "0.24.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Pure TypeScript libraries for my projects",
|
|
6
6
|
"homepage": "https://github.com/Pistonite/pure",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"denque": "2.1.0"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"vitest": "^3.
|
|
34
|
-
"mono-dev": "0.
|
|
33
|
+
"vitest": "^3.1.1",
|
|
34
|
+
"mono-dev": "0.1.1"
|
|
35
35
|
}
|
|
36
36
|
}
|
package/src/fs/FsSave.ts
CHANGED
|
@@ -1,10 +1,19 @@
|
|
|
1
|
+
import { fsFail, type FsVoid } from "./FsError.ts";
|
|
2
|
+
|
|
1
3
|
/** Save (download) a file using Blob */
|
|
2
|
-
export function fsSave(content: string | Uint8Array, filename: string) {
|
|
4
|
+
export function fsSave(content: string | Uint8Array, filename: string): FsVoid {
|
|
3
5
|
const blob = new Blob([content], {
|
|
4
6
|
// maybe lying, but should be fine
|
|
5
7
|
type: "text/plain;charset=utf-8",
|
|
6
8
|
});
|
|
7
|
-
|
|
9
|
+
|
|
10
|
+
try {
|
|
11
|
+
saveAs(blob, filename);
|
|
12
|
+
return {};
|
|
13
|
+
} catch (e) {
|
|
14
|
+
console.error(e);
|
|
15
|
+
return { err: fsFail("save failed") };
|
|
16
|
+
}
|
|
8
17
|
}
|
|
9
18
|
|
|
10
19
|
// The following code is adopted from
|
|
@@ -45,16 +54,17 @@ type SaveAsFnOptions = {
|
|
|
45
54
|
* source : http://purl.eligrey.com/github/FileSaver.js
|
|
46
55
|
*/
|
|
47
56
|
|
|
57
|
+
// adoption note: this is now ECMA standard - https://github.com/tc39/proposal-global
|
|
48
58
|
// The one and only way of getting global scope in all environments
|
|
49
59
|
// https://stackoverflow.com/q/3277182/1008999
|
|
50
|
-
const _global =
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
60
|
+
// const _global =
|
|
61
|
+
// typeof window === "object" && window.window === window
|
|
62
|
+
// ? window
|
|
63
|
+
// : typeof self === "object" && self.self === self
|
|
64
|
+
// ? self
|
|
65
|
+
// : typeof global === "object" && global.global === global
|
|
66
|
+
// ? global
|
|
67
|
+
// : this;
|
|
58
68
|
|
|
59
69
|
const download = (url: string | URL, name?: string, opts?: SaveAsFnOptions) => {
|
|
60
70
|
const xhr = new XMLHttpRequest();
|
|
@@ -97,16 +107,16 @@ const corsEnabled = (url: string | URL) => {
|
|
|
97
107
|
// adoption note: converted to check at call time, no need to
|
|
98
108
|
// do this check at boot load
|
|
99
109
|
const saveAs: SaveAsFn = (blob, name?, opts?) => {
|
|
100
|
-
|
|
110
|
+
// adoption note: this is likely to throw if window is not defined.. ?
|
|
111
|
+
if (typeof window !== "object" || window !== globalThis) {
|
|
101
112
|
// probably in some web worker
|
|
102
|
-
console.warn("saveAs is not supported in this environment");
|
|
103
113
|
return;
|
|
104
114
|
}
|
|
105
115
|
// Detect WebView inside a native macOS app by ruling out all browsers
|
|
106
116
|
// We just need to check for 'Safari' because all other browsers (besides Firefox) include that too
|
|
107
117
|
// https://www.whatismybrowser.com/guides/the-latest-user-agent/macos
|
|
108
118
|
const isMacOSWebView =
|
|
109
|
-
|
|
119
|
+
globalThis.navigator &&
|
|
110
120
|
/Macintosh/.test(navigator.userAgent) &&
|
|
111
121
|
/AppleWebKit/.test(navigator.userAgent) &&
|
|
112
122
|
!/Safari/.test(navigator.userAgent);
|
|
@@ -114,7 +124,7 @@ const saveAs: SaveAsFn = (blob, name?, opts?) => {
|
|
|
114
124
|
name = name || "download";
|
|
115
125
|
|
|
116
126
|
if ("download" in HTMLAnchorElement.prototype && !isMacOSWebView) {
|
|
117
|
-
const URL =
|
|
127
|
+
const URL = globalThis.URL || globalThis.webkitURL;
|
|
118
128
|
// Namespace is used to prevent conflict w/ Chrome Poper Blocker extension (Issue #561)
|
|
119
129
|
const a = document.createElementNS(
|
|
120
130
|
"http://www.w3.org/1999/xhtml",
|
|
@@ -172,9 +182,10 @@ const saveAs: SaveAsFn = (blob, name?, opts?) => {
|
|
|
172
182
|
}
|
|
173
183
|
|
|
174
184
|
const force = blob.type === "application/octet-stream";
|
|
185
|
+
// adoption note: add any
|
|
175
186
|
const isSafari =
|
|
176
|
-
/constructor/i.test((
|
|
177
|
-
(
|
|
187
|
+
/constructor/i.test((globalThis as any).HTMLElement) ||
|
|
188
|
+
(globalThis as any).safari;
|
|
178
189
|
const isChromeIOS = /CriOS\/[\d]+/.test(navigator.userAgent);
|
|
179
190
|
|
|
180
191
|
if (
|
|
@@ -197,7 +208,7 @@ const saveAs: SaveAsFn = (blob, name?, opts?) => {
|
|
|
197
208
|
};
|
|
198
209
|
reader.readAsDataURL(blob);
|
|
199
210
|
} else {
|
|
200
|
-
const URL =
|
|
211
|
+
const URL = globalThis.URL || globalThis.webkitURL;
|
|
201
212
|
const url = URL.createObjectURL(blob);
|
|
202
213
|
if (popup) {
|
|
203
214
|
popup.location = url;
|