hydrousdb 3.5.4 → 3.5.5
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/README.md +17 -5
- package/dist/index.cjs +62 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +62 -4
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -150,18 +150,76 @@ var HttpClient = class {
|
|
|
150
150
|
*/
|
|
151
151
|
async putToSignedUrl(signedUrl, data, mimeType, onProgress) {
|
|
152
152
|
const body = data;
|
|
153
|
-
if (typeof XMLHttpRequest !== "undefined"
|
|
153
|
+
if (typeof XMLHttpRequest !== "undefined") {
|
|
154
154
|
await new Promise((resolve, reject) => {
|
|
155
155
|
const xhr = new XMLHttpRequest();
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
156
|
+
if (typeof onProgress === "function") {
|
|
157
|
+
xhr.upload.onprogress = (e) => {
|
|
158
|
+
if (e.lengthComputable) onProgress(Math.round(e.loaded / e.total * 100));
|
|
159
|
+
};
|
|
160
|
+
}
|
|
159
161
|
xhr.onload = () => xhr.status >= 200 && xhr.status < 300 ? resolve() : reject(new Error(`GCS upload failed: ${xhr.status}`));
|
|
160
162
|
xhr.onerror = () => reject(new Error("GCS upload network error"));
|
|
161
163
|
xhr.open("PUT", signedUrl);
|
|
162
164
|
xhr.setRequestHeader("Content-Type", mimeType);
|
|
163
165
|
xhr.send(body);
|
|
164
166
|
});
|
|
167
|
+
} else if (typeof process !== "undefined" && process.versions && process.versions.node) {
|
|
168
|
+
const https = await import('https');
|
|
169
|
+
const { URL } = await import('url');
|
|
170
|
+
let buffer;
|
|
171
|
+
if (typeof Buffer !== "undefined" && Buffer.isBuffer(body)) {
|
|
172
|
+
buffer = body;
|
|
173
|
+
} else if (body instanceof ArrayBuffer) {
|
|
174
|
+
buffer = Buffer.from(body);
|
|
175
|
+
} else if (body instanceof Uint8Array) {
|
|
176
|
+
buffer = Buffer.from(body.buffer, body.byteOffset, body.byteLength);
|
|
177
|
+
} else if (typeof Blob !== "undefined" && body instanceof Blob) {
|
|
178
|
+
const arrayBuffer = await body.arrayBuffer();
|
|
179
|
+
buffer = Buffer.from(arrayBuffer);
|
|
180
|
+
} else {
|
|
181
|
+
buffer = Buffer.from(String(body));
|
|
182
|
+
}
|
|
183
|
+
const urlObj = new URL(signedUrl);
|
|
184
|
+
const options = {
|
|
185
|
+
method: "PUT",
|
|
186
|
+
hostname: urlObj.hostname,
|
|
187
|
+
path: urlObj.pathname + urlObj.search,
|
|
188
|
+
headers: {
|
|
189
|
+
"Content-Type": mimeType,
|
|
190
|
+
"Content-Length": String(buffer.length)
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
await new Promise((resolve, reject) => {
|
|
194
|
+
const req = https.request(options, (res) => {
|
|
195
|
+
if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) {
|
|
196
|
+
resolve();
|
|
197
|
+
} else {
|
|
198
|
+
reject(new Error(`GCS upload failed: ${res.statusCode}`));
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
req.on("error", (err) => {
|
|
202
|
+
reject(err);
|
|
203
|
+
});
|
|
204
|
+
const totalBytes = buffer.length;
|
|
205
|
+
let writtenBytes = 0;
|
|
206
|
+
const chunkSize = 64 * 1024;
|
|
207
|
+
const writeChunk = () => {
|
|
208
|
+
if (writtenBytes < totalBytes) {
|
|
209
|
+
const nextChunkSize = Math.min(chunkSize, totalBytes - writtenBytes);
|
|
210
|
+
const chunk = buffer.subarray(writtenBytes, writtenBytes + nextChunkSize);
|
|
211
|
+
req.write(chunk);
|
|
212
|
+
writtenBytes += nextChunkSize;
|
|
213
|
+
if (typeof onProgress === "function") {
|
|
214
|
+
onProgress(Math.round(writtenBytes / totalBytes * 100));
|
|
215
|
+
}
|
|
216
|
+
setImmediate(writeChunk);
|
|
217
|
+
} else {
|
|
218
|
+
req.end();
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
writeChunk();
|
|
222
|
+
});
|
|
165
223
|
} else {
|
|
166
224
|
const res = await fetch(signedUrl, {
|
|
167
225
|
method: "PUT",
|