hydrousdb 3.5.3 → 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/dist/index.d.cts CHANGED
@@ -278,6 +278,7 @@ interface UploadOptions {
278
278
  overwrite?: boolean;
279
279
  mimeType?: string;
280
280
  expiresInSeconds?: number;
281
+ onProgress?: (percent: number) => void;
281
282
  }
282
283
  interface UploadResult {
283
284
  path: string;
package/dist/index.d.ts CHANGED
@@ -278,6 +278,7 @@ interface UploadOptions {
278
278
  overwrite?: boolean;
279
279
  mimeType?: string;
280
280
  expiresInSeconds?: number;
281
+ onProgress?: (percent: number) => void;
281
282
  }
282
283
  interface UploadResult {
283
284
  path: string;
package/dist/index.mjs CHANGED
@@ -149,19 +149,77 @@ var HttpClient = class {
149
149
  * Uses XHR in browsers for onprogress support; fetch in Node.
150
150
  */
151
151
  async putToSignedUrl(signedUrl, data, mimeType, onProgress) {
152
- const body = data instanceof Blob ? data : data instanceof Uint8Array ? data.buffer : data;
153
- if (typeof XMLHttpRequest !== "undefined" && typeof onProgress === "function") {
152
+ const body = data;
153
+ if (typeof XMLHttpRequest !== "undefined") {
154
154
  await new Promise((resolve, reject) => {
155
155
  const xhr = new XMLHttpRequest();
156
- xhr.upload.onprogress = (e) => {
157
- if (e.lengthComputable) onProgress(Math.round(e.loaded / e.total * 100));
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",
@@ -1061,8 +1119,32 @@ var StorageManager = class {
1061
1119
  * ```
1062
1120
  */
1063
1121
  async upload(data, path, options = {}) {
1064
- const { isPublic = false, overwrite = false, mimeType } = options;
1122
+ const { isPublic = false, overwrite = false, mimeType, onProgress } = options;
1065
1123
  const mime = mimeType ?? guessMimeType(path);
1124
+ let size = 0;
1125
+ if (data instanceof Blob) {
1126
+ size = data.size;
1127
+ } else if (data instanceof Uint8Array || data instanceof ArrayBuffer) {
1128
+ size = data.byteLength;
1129
+ } else if (typeof Buffer !== "undefined" && Buffer.isBuffer(data)) {
1130
+ size = data.length;
1131
+ }
1132
+ const useDirectUpload = onProgress !== void 0 || size > 5 * 1024 * 1024;
1133
+ if (useDirectUpload) {
1134
+ const { uploadUrl, path: cleanPath } = await this.getUploadUrl({
1135
+ path,
1136
+ mimeType: mime,
1137
+ size,
1138
+ isPublic,
1139
+ overwrite
1140
+ });
1141
+ await this.uploadToSignedUrl(uploadUrl, data, mime, onProgress);
1142
+ return this.confirmUpload({
1143
+ path: cleanPath,
1144
+ mimeType: mime,
1145
+ isPublic
1146
+ });
1147
+ }
1066
1148
  const formData = new FormData();
1067
1149
  const blob = data instanceof Blob ? data : new Blob([data], { type: mime });
1068
1150
  formData.append("file", blob, path.split("/").pop() ?? "file");