@pistonite/pure 0.23.1 → 0.24.0

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 (2) hide show
  1. package/package.json +3 -5
  2. package/src/fs/FsSave.ts +204 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pistonite/pure",
3
- "version": "0.23.1",
3
+ "version": "0.24.0",
4
4
  "type": "module",
5
5
  "description": "Pure TypeScript libraries for my projects",
6
6
  "homepage": "https://github.com/Pistonite/pure",
@@ -27,12 +27,10 @@
27
27
  "directory": "packages/pure"
28
28
  },
29
29
  "dependencies": {
30
- "denque": "2.1.0",
31
- "file-saver": "2.0.5"
30
+ "denque": "2.1.0"
32
31
  },
33
32
  "devDependencies": {
34
- "@types/file-saver": "^2.0.7",
35
- "vitest": "^3.0.5",
33
+ "vitest": "^3.0.9",
36
34
  "mono-dev": "0.0.0"
37
35
  }
38
36
  }
package/src/fs/FsSave.ts CHANGED
@@ -1,7 +1,3 @@
1
- // workaround for CommonJS
2
- import pkg from "file-saver";
3
- const { saveAs } = pkg;
4
-
5
1
  /** Save (download) a file using Blob */
6
2
  export function fsSave(content: string | Uint8Array, filename: string) {
7
3
  const blob = new Blob([content], {
@@ -10,3 +6,207 @@ export function fsSave(content: string | Uint8Array, filename: string) {
10
6
  });
11
7
  saveAs(blob, filename);
12
8
  }
9
+
10
+ // The following code is adopted from
11
+ // - https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/file-saver/index.d.ts
12
+ // under this MIT license:
13
+ /*
14
+ This project is licensed under the MIT license.
15
+ Copyrights are respective of each contributor listed at the beginning of each definition file.
16
+
17
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
18
+
19
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ type SaveAsFn = (
25
+ data: Blob | string,
26
+ filename?: string,
27
+ options?: SaveAsFnOptions,
28
+ ) => void;
29
+ type SaveAsFnOptions = {
30
+ autoBom: boolean;
31
+ };
32
+
33
+ /* eslint-disable @typescript-eslint/no-explicit-any */
34
+
35
+ // The following code is vendored from
36
+ // - https://github.com/eligrey/FileSaver.js/blob/master/src/FileSaver.js
37
+ // under this MIT license:
38
+ /*
39
+ * FileSaver.js
40
+ * A saveAs() FileSaver implementation.
41
+ *
42
+ * By Eli Grey, http://eligrey.com
43
+ *
44
+ * License : https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md (MIT)
45
+ * source : http://purl.eligrey.com/github/FileSaver.js
46
+ */
47
+
48
+ // The one and only way of getting global scope in all environments
49
+ // https://stackoverflow.com/q/3277182/1008999
50
+ const _global =
51
+ typeof window === "object" && window.window === window
52
+ ? window
53
+ : typeof self === "object" && self.self === self
54
+ ? self
55
+ : typeof global === "object" && global.global === global
56
+ ? global
57
+ : this;
58
+
59
+ const download = (url: string | URL, name?: string, opts?: SaveAsFnOptions) => {
60
+ const xhr = new XMLHttpRequest();
61
+ xhr.open("GET", url);
62
+ xhr.responseType = "blob";
63
+ xhr.onload = function () {
64
+ saveAs(xhr.response, name, opts);
65
+ };
66
+ xhr.onerror = function () {
67
+ console.error("could not download file");
68
+ };
69
+ xhr.send();
70
+ };
71
+
72
+ const corsEnabled = (url: string | URL) => {
73
+ const xhr = new XMLHttpRequest();
74
+ // use sync to avoid popup blocker
75
+ xhr.open("HEAD", url, false);
76
+ try {
77
+ xhr.send();
78
+ } catch {
79
+ // ignore
80
+ }
81
+ return xhr.status >= 200 && xhr.status <= 299;
82
+ };
83
+
84
+ // adoption note: we drop support for old browsers, and just use click()
85
+ // original note: `a.click()` doesn't work for all browsers (#465)
86
+ // const click = (node: Node) => {
87
+ // try {
88
+ // node.dispatchEvent(new MouseEvent('click'))
89
+ // } catch {
90
+ // const evt = document.createEvent('MouseEvents')
91
+ // evt.initMouseEvent('click', true, true, window, 0, 0, 0, 80,
92
+ // 20, false, false, false, false, 0, null)
93
+ // node.dispatchEvent(evt)
94
+ // }
95
+ // }
96
+
97
+ // adoption note: converted to check at call time, no need to
98
+ // do this check at boot load
99
+ const saveAs: SaveAsFn = (blob, name?, opts?) => {
100
+ if (typeof window !== "object" || window !== _global) {
101
+ // probably in some web worker
102
+ console.warn("saveAs is not supported in this environment");
103
+ return;
104
+ }
105
+ // Detect WebView inside a native macOS app by ruling out all browsers
106
+ // We just need to check for 'Safari' because all other browsers (besides Firefox) include that too
107
+ // https://www.whatismybrowser.com/guides/the-latest-user-agent/macos
108
+ const isMacOSWebView =
109
+ _global.navigator &&
110
+ /Macintosh/.test(navigator.userAgent) &&
111
+ /AppleWebKit/.test(navigator.userAgent) &&
112
+ !/Safari/.test(navigator.userAgent);
113
+ // adoption note: removed blob.name, and pulled this line outside
114
+ name = name || "download";
115
+
116
+ if ("download" in HTMLAnchorElement.prototype && !isMacOSWebView) {
117
+ const URL = _global.URL || _global.webkitURL;
118
+ // Namespace is used to prevent conflict w/ Chrome Poper Blocker extension (Issue #561)
119
+ const a = document.createElementNS(
120
+ "http://www.w3.org/1999/xhtml",
121
+ "a",
122
+ ) as HTMLAnchorElement;
123
+
124
+ a.download = name;
125
+ a.rel = "noopener"; // tabnabbing
126
+
127
+ // TODO: detect chrome extensions & packaged apps
128
+ // a.target = '_blank'
129
+
130
+ if (typeof blob === "string") {
131
+ // Support regular links
132
+ a.href = blob;
133
+ if (a.origin !== location.origin) {
134
+ // adoption note: removed turnery and changed click()
135
+ if (corsEnabled(a.href)) {
136
+ download(blob, name, opts);
137
+ } else {
138
+ a.target = "_blank";
139
+ a.click();
140
+ }
141
+ } else {
142
+ // adoption note: changed click()
143
+ a.click();
144
+ }
145
+ } else {
146
+ // Support blobs
147
+ a.href = URL.createObjectURL(blob);
148
+ // adoption note: not sure why 40s
149
+ setTimeout(() => {
150
+ URL.revokeObjectURL(a.href);
151
+ }, 4e4); // 40s
152
+ // adoption note: changed click()
153
+ setTimeout(() => {
154
+ a.click();
155
+ }, 0);
156
+ }
157
+ return;
158
+ }
159
+
160
+ // adoption note: drop IE support
161
+
162
+ // Fallback to using FileReader and a popup
163
+ // Open a popup immediately do go around popup blocker
164
+ // Mostly only available on user interaction and the fileReader is async so...
165
+ let popup = (window as any).popup || open("", "_blank");
166
+ if (popup) {
167
+ popup.document.title = popup.document.body.innerText = "downloading...";
168
+ }
169
+
170
+ if (typeof blob === "string") {
171
+ return download(blob, name, opts);
172
+ }
173
+
174
+ const force = blob.type === "application/octet-stream";
175
+ const isSafari =
176
+ /constructor/i.test((_global as any).HTMLElement) ||
177
+ (_global as any).safari;
178
+ const isChromeIOS = /CriOS\/[\d]+/.test(navigator.userAgent);
179
+
180
+ if (
181
+ (isChromeIOS || (force && isSafari) || isMacOSWebView) &&
182
+ typeof FileReader !== "undefined"
183
+ ) {
184
+ // Safari doesn't allow downloading of blob URLs
185
+ const reader = new FileReader();
186
+ reader.onloadend = function () {
187
+ let url = reader.result as string;
188
+ url = isChromeIOS
189
+ ? url
190
+ : url.replace(/^data:[^;]*;/, "data:attachment/file;");
191
+ if (popup) {
192
+ popup.location.href = url;
193
+ } else {
194
+ (location as any) = url;
195
+ }
196
+ popup = null; // reverse-tabnabbing #460
197
+ };
198
+ reader.readAsDataURL(blob);
199
+ } else {
200
+ const URL = _global.URL || _global.webkitURL;
201
+ const url = URL.createObjectURL(blob);
202
+ if (popup) {
203
+ popup.location = url;
204
+ } else {
205
+ location.href = url;
206
+ }
207
+ popup = null; // reverse-tabnabbing #460
208
+ setTimeout(function () {
209
+ URL.revokeObjectURL(url);
210
+ }, 4e4); // 40s
211
+ }
212
+ };