@contello/uploader 8.21.0 → 8.21.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/README.md +88 -0
- package/dist/index.cjs +211 -0
- package/dist/index.d.cts +122 -0
- package/dist/index.d.ts +122 -132
- package/dist/index.js +155 -512
- package/package.json +18 -25
- package/dist/index.umd.cjs +0 -3
package/dist/index.js
CHANGED
|
@@ -1,540 +1,183 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
if (e) return e.call(r);
|
|
20
|
-
if (r && typeof r.length == "number") return {
|
|
21
|
-
next: function() {
|
|
22
|
-
return r && n >= r.length && (r = void 0), { value: r && r[n++], done: !r };
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
throw new TypeError(t ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
26
|
-
}
|
|
27
|
-
function O(r, t) {
|
|
28
|
-
var e = typeof Symbol == "function" && r[Symbol.iterator];
|
|
29
|
-
if (!e) return r;
|
|
30
|
-
var n = e.call(r), o, i = [], u;
|
|
31
|
-
try {
|
|
32
|
-
for (; (t === void 0 || t-- > 0) && !(o = n.next()).done; ) i.push(o.value);
|
|
33
|
-
} catch (c) {
|
|
34
|
-
u = { error: c };
|
|
35
|
-
} finally {
|
|
36
|
-
try {
|
|
37
|
-
o && !o.done && (e = n.return) && e.call(n);
|
|
38
|
-
} finally {
|
|
39
|
-
if (u) throw u.error;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
return i;
|
|
43
|
-
}
|
|
44
|
-
function _(r, t, e) {
|
|
45
|
-
if (e || arguments.length === 2) for (var n = 0, o = t.length, i; n < o; n++)
|
|
46
|
-
(i || !(n in t)) && (i || (i = Array.prototype.slice.call(t, 0, n)), i[n] = t[n]);
|
|
47
|
-
return r.concat(i || Array.prototype.slice.call(t));
|
|
48
|
-
}
|
|
49
|
-
function d(r) {
|
|
50
|
-
return typeof r == "function";
|
|
51
|
-
}
|
|
52
|
-
function B(r) {
|
|
53
|
-
var t = function(n) {
|
|
54
|
-
Error.call(n), n.stack = new Error().stack;
|
|
55
|
-
}, e = r(t);
|
|
56
|
-
return e.prototype = Object.create(Error.prototype), e.prototype.constructor = e, e;
|
|
57
|
-
}
|
|
58
|
-
var g = B(function(r) {
|
|
59
|
-
return function(e) {
|
|
60
|
-
r(this), this.message = e ? e.length + ` errors occurred during unsubscription:
|
|
61
|
-
` + e.map(function(n, o) {
|
|
62
|
-
return o + 1 + ") " + n.toString();
|
|
63
|
-
}).join(`
|
|
64
|
-
`) : "", this.name = "UnsubscriptionError", this.errors = e;
|
|
65
|
-
};
|
|
66
|
-
});
|
|
67
|
-
function j(r, t) {
|
|
68
|
-
if (r) {
|
|
69
|
-
var e = r.indexOf(t);
|
|
70
|
-
0 <= e && r.splice(e, 1);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
var A = function() {
|
|
74
|
-
function r(t) {
|
|
75
|
-
this.initialTeardown = t, this.closed = !1, this._parentage = null, this._finalizers = null;
|
|
76
|
-
}
|
|
77
|
-
return r.prototype.unsubscribe = function() {
|
|
78
|
-
var t, e, n, o, i;
|
|
79
|
-
if (!this.closed) {
|
|
80
|
-
this.closed = !0;
|
|
81
|
-
var u = this._parentage;
|
|
82
|
-
if (u)
|
|
83
|
-
if (this._parentage = null, Array.isArray(u))
|
|
84
|
-
try {
|
|
85
|
-
for (var c = F(u), s = c.next(); !s.done; s = c.next()) {
|
|
86
|
-
var l = s.value;
|
|
87
|
-
l.remove(this);
|
|
88
|
-
}
|
|
89
|
-
} catch (f) {
|
|
90
|
-
t = { error: f };
|
|
91
|
-
} finally {
|
|
92
|
-
try {
|
|
93
|
-
s && !s.done && (e = c.return) && e.call(c);
|
|
94
|
-
} finally {
|
|
95
|
-
if (t) throw t.error;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
else
|
|
99
|
-
u.remove(this);
|
|
100
|
-
var a = this.initialTeardown;
|
|
101
|
-
if (d(a))
|
|
102
|
-
try {
|
|
103
|
-
a();
|
|
104
|
-
} catch (f) {
|
|
105
|
-
i = f instanceof g ? f.errors : [f];
|
|
106
|
-
}
|
|
107
|
-
var p = this._finalizers;
|
|
108
|
-
if (p) {
|
|
109
|
-
this._finalizers = null;
|
|
110
|
-
try {
|
|
111
|
-
for (var y = F(p), b = y.next(); !b.done; b = y.next()) {
|
|
112
|
-
var m = b.value;
|
|
113
|
-
try {
|
|
114
|
-
L(m);
|
|
115
|
-
} catch (f) {
|
|
116
|
-
i = i ?? [], f instanceof g ? i = _(_([], O(i)), O(f.errors)) : i.push(f);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
} catch (f) {
|
|
120
|
-
n = { error: f };
|
|
121
|
-
} finally {
|
|
122
|
-
try {
|
|
123
|
-
b && !b.done && (o = y.return) && o.call(y);
|
|
124
|
-
} finally {
|
|
125
|
-
if (n) throw n.error;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
if (i)
|
|
130
|
-
throw new g(i);
|
|
131
|
-
}
|
|
132
|
-
}, r.prototype.add = function(t) {
|
|
133
|
-
var e;
|
|
134
|
-
if (t && t !== this)
|
|
135
|
-
if (this.closed)
|
|
136
|
-
L(t);
|
|
137
|
-
else {
|
|
138
|
-
if (t instanceof r) {
|
|
139
|
-
if (t.closed || t._hasParent(this))
|
|
140
|
-
return;
|
|
141
|
-
t._addParent(this);
|
|
142
|
-
}
|
|
143
|
-
(this._finalizers = (e = this._finalizers) !== null && e !== void 0 ? e : []).push(t);
|
|
144
|
-
}
|
|
145
|
-
}, r.prototype._hasParent = function(t) {
|
|
146
|
-
var e = this._parentage;
|
|
147
|
-
return e === t || Array.isArray(e) && e.includes(t);
|
|
148
|
-
}, r.prototype._addParent = function(t) {
|
|
149
|
-
var e = this._parentage;
|
|
150
|
-
this._parentage = Array.isArray(e) ? (e.push(t), e) : e ? [e, t] : t;
|
|
151
|
-
}, r.prototype._removeParent = function(t) {
|
|
152
|
-
var e = this._parentage;
|
|
153
|
-
e === t ? this._parentage = null : Array.isArray(e) && j(e, t);
|
|
154
|
-
}, r.prototype.remove = function(t) {
|
|
155
|
-
var e = this._finalizers;
|
|
156
|
-
e && j(e, t), t instanceof r && t._removeParent(this);
|
|
157
|
-
}, r.EMPTY = function() {
|
|
158
|
-
var t = new r();
|
|
159
|
-
return t.closed = !0, t;
|
|
160
|
-
}(), r;
|
|
161
|
-
}();
|
|
162
|
-
A.EMPTY;
|
|
163
|
-
function J(r) {
|
|
164
|
-
return r instanceof A || r && "closed" in r && d(r.remove) && d(r.add) && d(r.unsubscribe);
|
|
165
|
-
}
|
|
166
|
-
function L(r) {
|
|
167
|
-
d(r) ? r() : r.unsubscribe();
|
|
168
|
-
}
|
|
169
|
-
var M = {
|
|
170
|
-
Promise: void 0
|
|
171
|
-
}, V = {
|
|
172
|
-
setTimeout: function(r, t) {
|
|
173
|
-
for (var e = [], n = 2; n < arguments.length; n++)
|
|
174
|
-
e[n - 2] = arguments[n];
|
|
175
|
-
return setTimeout.apply(void 0, _([r, t], O(e)));
|
|
176
|
-
},
|
|
177
|
-
clearTimeout: function(r) {
|
|
178
|
-
return clearTimeout(r);
|
|
179
|
-
},
|
|
180
|
-
delegate: void 0
|
|
181
|
-
};
|
|
182
|
-
function q(r) {
|
|
183
|
-
V.setTimeout(function() {
|
|
184
|
-
throw r;
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
function W() {
|
|
188
|
-
}
|
|
189
|
-
function Y(r) {
|
|
190
|
-
r();
|
|
191
|
-
}
|
|
192
|
-
var T = function(r) {
|
|
193
|
-
P(t, r);
|
|
194
|
-
function t(e) {
|
|
195
|
-
var n = r.call(this) || this;
|
|
196
|
-
return n.isStopped = !1, e ? (n.destination = e, J(e) && e.add(n)) : n.destination = N, n;
|
|
197
|
-
}
|
|
198
|
-
return t.create = function(e, n, o) {
|
|
199
|
-
return new w(e, n, o);
|
|
200
|
-
}, t.prototype.next = function(e) {
|
|
201
|
-
this.isStopped || this._next(e);
|
|
202
|
-
}, t.prototype.error = function(e) {
|
|
203
|
-
this.isStopped || (this.isStopped = !0, this._error(e));
|
|
204
|
-
}, t.prototype.complete = function() {
|
|
205
|
-
this.isStopped || (this.isStopped = !0, this._complete());
|
|
206
|
-
}, t.prototype.unsubscribe = function() {
|
|
207
|
-
this.closed || (this.isStopped = !0, r.prototype.unsubscribe.call(this), this.destination = null);
|
|
208
|
-
}, t.prototype._next = function(e) {
|
|
209
|
-
this.destination.next(e);
|
|
210
|
-
}, t.prototype._error = function(e) {
|
|
211
|
-
try {
|
|
212
|
-
this.destination.error(e);
|
|
213
|
-
} finally {
|
|
214
|
-
this.unsubscribe();
|
|
215
|
-
}
|
|
216
|
-
}, t.prototype._complete = function() {
|
|
217
|
-
try {
|
|
218
|
-
this.destination.complete();
|
|
219
|
-
} finally {
|
|
220
|
-
this.unsubscribe();
|
|
221
|
-
}
|
|
222
|
-
}, t;
|
|
223
|
-
}(A), C = function() {
|
|
224
|
-
function r(t) {
|
|
225
|
-
this.partialObserver = t;
|
|
226
|
-
}
|
|
227
|
-
return r.prototype.next = function(t) {
|
|
228
|
-
var e = this.partialObserver;
|
|
229
|
-
if (e.next)
|
|
230
|
-
try {
|
|
231
|
-
e.next(t);
|
|
232
|
-
} catch (n) {
|
|
233
|
-
S(n);
|
|
234
|
-
}
|
|
235
|
-
}, r.prototype.error = function(t) {
|
|
236
|
-
var e = this.partialObserver;
|
|
237
|
-
if (e.error)
|
|
238
|
-
try {
|
|
239
|
-
e.error(t);
|
|
240
|
-
} catch (n) {
|
|
241
|
-
S(n);
|
|
242
|
-
}
|
|
243
|
-
else
|
|
244
|
-
S(t);
|
|
245
|
-
}, r.prototype.complete = function() {
|
|
246
|
-
var t = this.partialObserver;
|
|
247
|
-
if (t.complete)
|
|
248
|
-
try {
|
|
249
|
-
t.complete();
|
|
250
|
-
} catch (e) {
|
|
251
|
-
S(e);
|
|
252
|
-
}
|
|
253
|
-
}, r;
|
|
254
|
-
}(), w = function(r) {
|
|
255
|
-
P(t, r);
|
|
256
|
-
function t(e, n, o) {
|
|
257
|
-
var i = r.call(this) || this, u;
|
|
258
|
-
return d(e) || !e ? u = {
|
|
259
|
-
next: e ?? void 0,
|
|
260
|
-
error: n ?? void 0,
|
|
261
|
-
complete: o ?? void 0
|
|
262
|
-
} : u = e, i.destination = new C(u), i;
|
|
263
|
-
}
|
|
264
|
-
return t;
|
|
265
|
-
}(T);
|
|
266
|
-
function S(r) {
|
|
267
|
-
q(r);
|
|
268
|
-
}
|
|
269
|
-
function D(r) {
|
|
270
|
-
throw r;
|
|
271
|
-
}
|
|
272
|
-
var N = {
|
|
273
|
-
closed: !0,
|
|
274
|
-
next: W,
|
|
275
|
-
error: D,
|
|
276
|
-
complete: W
|
|
277
|
-
}, X = function() {
|
|
278
|
-
return typeof Symbol == "function" && Symbol.observable || "@@observable";
|
|
279
|
-
}();
|
|
280
|
-
function G(r) {
|
|
281
|
-
return r;
|
|
282
|
-
}
|
|
283
|
-
function K(r) {
|
|
284
|
-
return r.length === 0 ? G : r.length === 1 ? r[0] : function(e) {
|
|
285
|
-
return r.reduce(function(n, o) {
|
|
286
|
-
return o(n);
|
|
287
|
-
}, e);
|
|
288
|
-
};
|
|
289
|
-
}
|
|
290
|
-
var $ = function() {
|
|
291
|
-
function r(t) {
|
|
292
|
-
t && (this._subscribe = t);
|
|
293
|
-
}
|
|
294
|
-
return r.prototype.lift = function(t) {
|
|
295
|
-
var e = new r();
|
|
296
|
-
return e.source = this, e.operator = t, e;
|
|
297
|
-
}, r.prototype.subscribe = function(t, e, n) {
|
|
298
|
-
var o = this, i = Z(t) ? t : new w(t, e, n);
|
|
299
|
-
return Y(function() {
|
|
300
|
-
var u = o, c = u.operator, s = u.source;
|
|
301
|
-
i.add(c ? c.call(i, s) : s ? o._subscribe(i) : o._trySubscribe(i));
|
|
302
|
-
}), i;
|
|
303
|
-
}, r.prototype._trySubscribe = function(t) {
|
|
304
|
-
try {
|
|
305
|
-
return this._subscribe(t);
|
|
306
|
-
} catch (e) {
|
|
307
|
-
t.error(e);
|
|
308
|
-
}
|
|
309
|
-
}, r.prototype.forEach = function(t, e) {
|
|
310
|
-
var n = this;
|
|
311
|
-
return e = z(e), new e(function(o, i) {
|
|
312
|
-
var u = new w({
|
|
313
|
-
next: function(c) {
|
|
314
|
-
try {
|
|
315
|
-
t(c);
|
|
316
|
-
} catch (s) {
|
|
317
|
-
i(s), u.unsubscribe();
|
|
318
|
-
}
|
|
319
|
-
},
|
|
320
|
-
error: i,
|
|
321
|
-
complete: o
|
|
322
|
-
});
|
|
323
|
-
n.subscribe(u);
|
|
324
|
-
});
|
|
325
|
-
}, r.prototype._subscribe = function(t) {
|
|
326
|
-
var e;
|
|
327
|
-
return (e = this.source) === null || e === void 0 ? void 0 : e.subscribe(t);
|
|
328
|
-
}, r.prototype[X] = function() {
|
|
329
|
-
return this;
|
|
330
|
-
}, r.prototype.pipe = function() {
|
|
331
|
-
for (var t = [], e = 0; e < arguments.length; e++)
|
|
332
|
-
t[e] = arguments[e];
|
|
333
|
-
return K(t)(this);
|
|
334
|
-
}, r.prototype.toPromise = function(t) {
|
|
335
|
-
var e = this;
|
|
336
|
-
return t = z(t), new t(function(n, o) {
|
|
337
|
-
var i;
|
|
338
|
-
e.subscribe(function(u) {
|
|
339
|
-
return i = u;
|
|
340
|
-
}, function(u) {
|
|
341
|
-
return o(u);
|
|
342
|
-
}, function() {
|
|
343
|
-
return n(i);
|
|
344
|
-
});
|
|
345
|
-
});
|
|
346
|
-
}, r.create = function(t) {
|
|
347
|
-
return new r(t);
|
|
348
|
-
}, r;
|
|
349
|
-
}();
|
|
350
|
-
function z(r) {
|
|
351
|
-
var t;
|
|
352
|
-
return (t = r ?? M.Promise) !== null && t !== void 0 ? t : Promise;
|
|
353
|
-
}
|
|
354
|
-
function Q(r) {
|
|
355
|
-
return r && d(r.next) && d(r.error) && d(r.complete);
|
|
356
|
-
}
|
|
357
|
-
function Z(r) {
|
|
358
|
-
return r && r instanceof T || Q(r) && J(r);
|
|
359
|
-
}
|
|
360
|
-
function rr(r) {
|
|
361
|
-
return d(r?.lift);
|
|
362
|
-
}
|
|
363
|
-
function tr(r) {
|
|
364
|
-
return function(t) {
|
|
365
|
-
if (rr(t))
|
|
366
|
-
return t.lift(function(e) {
|
|
367
|
-
try {
|
|
368
|
-
return r(e, this);
|
|
369
|
-
} catch (n) {
|
|
370
|
-
this.error(n);
|
|
371
|
-
}
|
|
372
|
-
});
|
|
373
|
-
throw new TypeError("Unable to lift unknown Observable type");
|
|
374
|
-
};
|
|
375
|
-
}
|
|
376
|
-
function er(r, t, e, n, o) {
|
|
377
|
-
return new nr(r, t, e, n, o);
|
|
378
|
-
}
|
|
379
|
-
var nr = function(r) {
|
|
380
|
-
P(t, r);
|
|
381
|
-
function t(e, n, o, i, u, c) {
|
|
382
|
-
var s = r.call(this, e) || this;
|
|
383
|
-
return s.onFinalize = u, s.shouldUnsubscribe = c, s._next = n ? function(l) {
|
|
384
|
-
try {
|
|
385
|
-
n(l);
|
|
386
|
-
} catch (a) {
|
|
387
|
-
e.error(a);
|
|
388
|
-
}
|
|
389
|
-
} : r.prototype._next, s._error = i ? function(l) {
|
|
390
|
-
try {
|
|
391
|
-
i(l);
|
|
392
|
-
} catch (a) {
|
|
393
|
-
e.error(a);
|
|
394
|
-
} finally {
|
|
395
|
-
this.unsubscribe();
|
|
396
|
-
}
|
|
397
|
-
} : r.prototype._error, s._complete = o ? function() {
|
|
398
|
-
try {
|
|
399
|
-
o();
|
|
400
|
-
} catch (l) {
|
|
401
|
-
e.error(l);
|
|
402
|
-
} finally {
|
|
403
|
-
this.unsubscribe();
|
|
404
|
-
}
|
|
405
|
-
} : r.prototype._complete, s;
|
|
406
|
-
}
|
|
407
|
-
return t.prototype.unsubscribe = function() {
|
|
408
|
-
var e;
|
|
409
|
-
if (!this.shouldUnsubscribe || this.shouldUnsubscribe()) {
|
|
410
|
-
var n = this.closed;
|
|
411
|
-
r.prototype.unsubscribe.call(this), !n && ((e = this.onFinalize) === null || e === void 0 || e.call(this));
|
|
412
|
-
}
|
|
413
|
-
}, t;
|
|
414
|
-
}(T), or = B(function(r) {
|
|
415
|
-
return function() {
|
|
416
|
-
r(this), this.name = "EmptyError", this.message = "no elements in sequence";
|
|
417
|
-
};
|
|
418
|
-
});
|
|
419
|
-
function ir(r, t) {
|
|
420
|
-
return new Promise(function(e, n) {
|
|
421
|
-
var o = new w({
|
|
422
|
-
next: function(i) {
|
|
423
|
-
e(i), o.unsubscribe();
|
|
424
|
-
},
|
|
425
|
-
error: n,
|
|
426
|
-
complete: function() {
|
|
427
|
-
n(new or());
|
|
428
|
-
}
|
|
429
|
-
});
|
|
430
|
-
r.subscribe(o);
|
|
431
|
-
});
|
|
432
|
-
}
|
|
433
|
-
function sr(r, t) {
|
|
434
|
-
return tr(function(e, n) {
|
|
435
|
-
var o = 0;
|
|
436
|
-
e.subscribe(er(n, function(i) {
|
|
437
|
-
return r.call(t, i, o++) && n.next(i);
|
|
438
|
-
}));
|
|
439
|
-
});
|
|
440
|
-
}
|
|
441
|
-
function ur({ url: r, project: t, token: e, chunkSize: n }, o, i, u) {
|
|
442
|
-
const c = { ...i ?? {}, projectRef: t };
|
|
443
|
-
return new $((s) => {
|
|
444
|
-
const l = new URL(r), a = new WebSocket(
|
|
445
|
-
`${l.protocol === "https:" ? "wss" : "ws"}://${l.host}/api/v1/assets/ws`,
|
|
1
|
+
// src/upload-metadata.ts
|
|
2
|
+
var UploadAssetRetentionPolicy = /* @__PURE__ */ ((UploadAssetRetentionPolicy2) => {
|
|
3
|
+
UploadAssetRetentionPolicy2["retain"] = "retain";
|
|
4
|
+
UploadAssetRetentionPolicy2["deleteIfNotUsed"] = "deleteIfNotUsed";
|
|
5
|
+
return UploadAssetRetentionPolicy2;
|
|
6
|
+
})(UploadAssetRetentionPolicy || {});
|
|
7
|
+
|
|
8
|
+
// src/uploader.ts
|
|
9
|
+
import { filter, firstValueFrom } from "rxjs";
|
|
10
|
+
|
|
11
|
+
// src/implementations/ws.ts
|
|
12
|
+
import { Observable } from "rxjs";
|
|
13
|
+
function uploadViaWebSocket({ url, project, token, chunkSize }, file, meta, options) {
|
|
14
|
+
const metadata = { ...meta ?? {}, projectRef: project };
|
|
15
|
+
return new Observable((obs) => {
|
|
16
|
+
const parsedUrl = new URL(url);
|
|
17
|
+
const ws = new WebSocket(
|
|
18
|
+
`${parsedUrl.protocol === "https:" ? "wss" : "ws"}://${parsedUrl.host}/api/v1/assets/ws`,
|
|
446
19
|
"contello-file-upload-v1"
|
|
447
|
-
)
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
20
|
+
);
|
|
21
|
+
const reader = typeof FileReader !== "undefined" ? new FileReader() : null;
|
|
22
|
+
let offset = 0;
|
|
23
|
+
let done = false;
|
|
24
|
+
let ackReceived = false;
|
|
25
|
+
ws.onopen = async () => {
|
|
26
|
+
const initFrame = {
|
|
451
27
|
type: "init",
|
|
452
28
|
metadata: {
|
|
453
|
-
...
|
|
454
|
-
name:
|
|
455
|
-
mimeType:
|
|
456
|
-
size:
|
|
29
|
+
...metadata,
|
|
30
|
+
name: file.name,
|
|
31
|
+
mimeType: file.type,
|
|
32
|
+
size: file.size
|
|
457
33
|
},
|
|
458
|
-
token
|
|
34
|
+
token
|
|
459
35
|
};
|
|
460
|
-
|
|
461
|
-
function
|
|
462
|
-
const
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
|
|
36
|
+
ws.send(JSON.stringify(initFrame));
|
|
37
|
+
function readSlice() {
|
|
38
|
+
const slice = file.slice(offset, offset + chunkSize);
|
|
39
|
+
if (reader) {
|
|
40
|
+
reader.readAsArrayBuffer(slice);
|
|
41
|
+
} else {
|
|
42
|
+
slice.arrayBuffer().then((buffer) => {
|
|
43
|
+
handleArrayBuffer(buffer);
|
|
44
|
+
}).catch((err) => {
|
|
45
|
+
obs.error(err);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
468
48
|
}
|
|
469
|
-
function
|
|
470
|
-
|
|
49
|
+
function markAsDone() {
|
|
50
|
+
reader?.abort();
|
|
51
|
+
ws.send(JSON.stringify({ type: "done" }));
|
|
52
|
+
}
|
|
53
|
+
function startUpload() {
|
|
54
|
+
if (ackReceived) {
|
|
55
|
+
readSlice();
|
|
56
|
+
}
|
|
471
57
|
}
|
|
472
|
-
function
|
|
473
|
-
|
|
58
|
+
function handleArrayBuffer(buffer) {
|
|
59
|
+
if (ws.readyState === WebSocket.OPEN) {
|
|
60
|
+
ws.send(buffer);
|
|
61
|
+
offset += buffer.byteLength;
|
|
62
|
+
if (offset < file.size) {
|
|
63
|
+
readSlice();
|
|
64
|
+
} else {
|
|
65
|
+
markAsDone();
|
|
66
|
+
}
|
|
67
|
+
} else {
|
|
68
|
+
obs.error(new Error("WebSocket is closed"));
|
|
69
|
+
reader?.abort();
|
|
70
|
+
}
|
|
474
71
|
}
|
|
475
|
-
|
|
476
|
-
|
|
72
|
+
if (reader) {
|
|
73
|
+
reader.onload = () => {
|
|
74
|
+
if (reader.result && reader.result instanceof ArrayBuffer) {
|
|
75
|
+
handleArrayBuffer(reader.result);
|
|
76
|
+
} else {
|
|
77
|
+
markAsDone();
|
|
78
|
+
}
|
|
79
|
+
};
|
|
477
80
|
}
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
m = !0, H();
|
|
81
|
+
ws.onmessage = (event) => {
|
|
82
|
+
const message = JSON.parse(event.data);
|
|
83
|
+
if (message.type === "ack") {
|
|
84
|
+
ackReceived = true;
|
|
85
|
+
startUpload();
|
|
484
86
|
return;
|
|
485
87
|
}
|
|
486
|
-
if (
|
|
487
|
-
return
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
88
|
+
if (message.type === "progress") {
|
|
89
|
+
return obs.next({ progress: message.bytesReceived / file.size * 100 });
|
|
90
|
+
}
|
|
91
|
+
if (message.type === "done") {
|
|
92
|
+
if (message.id) {
|
|
93
|
+
done = true;
|
|
94
|
+
obs.next({ id: message.id });
|
|
95
|
+
obs.complete();
|
|
96
|
+
} else {
|
|
97
|
+
obs.error(new Error("No asset id received"));
|
|
98
|
+
}
|
|
99
|
+
return ws.close();
|
|
100
|
+
}
|
|
101
|
+
obs.error(new Error(`WebSocket message with unknown type ${JSON.stringify(message)}`));
|
|
102
|
+
ws.close();
|
|
491
103
|
};
|
|
492
|
-
}, a.onerror = (E) => s.error(E), a.onclose = () => {
|
|
493
|
-
b || s.error(new Error("Connection closed"));
|
|
494
104
|
};
|
|
495
|
-
|
|
496
|
-
|
|
105
|
+
ws.onerror = (error) => obs.error(error);
|
|
106
|
+
ws.onclose = () => {
|
|
107
|
+
if (!done) {
|
|
108
|
+
obs.error(new Error("Connection closed"));
|
|
109
|
+
}
|
|
497
110
|
};
|
|
498
|
-
|
|
499
|
-
|
|
111
|
+
const abortHandler = () => {
|
|
112
|
+
if (!done) {
|
|
113
|
+
reader?.abort();
|
|
114
|
+
ws.close();
|
|
115
|
+
obs.error(new Error("Upload aborted"));
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
options?.abort?.addEventListener("abort", abortHandler);
|
|
119
|
+
return () => {
|
|
120
|
+
options?.abort?.removeEventListener("abort", abortHandler);
|
|
121
|
+
ws.close();
|
|
500
122
|
};
|
|
501
123
|
});
|
|
502
124
|
}
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
125
|
+
|
|
126
|
+
// src/implementations/xhr.ts
|
|
127
|
+
import { Observable as Observable2 } from "rxjs";
|
|
128
|
+
function uploadViaXhr({ url, project, token }, file, meta, options) {
|
|
129
|
+
return new Observable2((obs) => {
|
|
130
|
+
const data = new FormData();
|
|
131
|
+
data.append("metadata", JSON.stringify({ ...meta ?? {}, projectRef: project }));
|
|
132
|
+
data.append("file", file);
|
|
133
|
+
const xhr = new XMLHttpRequest();
|
|
134
|
+
xhr.setRequestHeader("Authorization", `Bearer ${token}`);
|
|
135
|
+
xhr.open("POST", `${url}/api/v1/assets`, true);
|
|
136
|
+
xhr.upload.onprogress = async (event) => {
|
|
137
|
+
if (event.lengthComputable) {
|
|
138
|
+
obs.next({ progress: event.loaded / event.total * 100 });
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
xhr.onload = async () => {
|
|
142
|
+
if (xhr.status >= 200 && xhr.status < 300) {
|
|
143
|
+
const { id } = JSON.parse(xhr.responseText);
|
|
144
|
+
obs.next({ id });
|
|
145
|
+
return obs.complete();
|
|
514
146
|
}
|
|
515
|
-
|
|
516
|
-
}
|
|
147
|
+
obs.error(new Error(`${xhr.status}: ${xhr.statusText}`));
|
|
148
|
+
};
|
|
149
|
+
xhr.onerror = (error) => obs.error(error);
|
|
150
|
+
xhr.send(data);
|
|
151
|
+
options?.abort?.addEventListener("abort", () => xhr.abort());
|
|
152
|
+
return () => xhr.abort();
|
|
517
153
|
});
|
|
518
154
|
}
|
|
519
|
-
|
|
155
|
+
|
|
156
|
+
// src/uploader.ts
|
|
157
|
+
var ContelloUploader = class {
|
|
520
158
|
params;
|
|
521
|
-
constructor(
|
|
159
|
+
constructor(params) {
|
|
522
160
|
this.params = {
|
|
523
|
-
url:
|
|
524
|
-
project:
|
|
525
|
-
token:
|
|
526
|
-
transport:
|
|
527
|
-
chunkSize:
|
|
161
|
+
url: params.url,
|
|
162
|
+
project: params.project,
|
|
163
|
+
token: params.token,
|
|
164
|
+
transport: params.transport || "ws",
|
|
165
|
+
chunkSize: params.chunkSize || 4 * 1024 * 1024
|
|
528
166
|
};
|
|
529
167
|
}
|
|
530
|
-
|
|
531
|
-
|
|
168
|
+
/** Uploads a file and resolves with the asset ID once complete. */
|
|
169
|
+
async upload(file, meta, options) {
|
|
170
|
+
return firstValueFrom(this.uploadWithEvents(file, meta, options).pipe(filter((r) => "id" in r)));
|
|
532
171
|
}
|
|
533
|
-
|
|
534
|
-
|
|
172
|
+
/** Uploads a file and returns an Observable emitting progress events and the final asset ID. */
|
|
173
|
+
uploadWithEvents(file, meta, options) {
|
|
174
|
+
if (this.params.transport === "http") {
|
|
175
|
+
return uploadViaXhr(this.params, file, meta, options);
|
|
176
|
+
}
|
|
177
|
+
return uploadViaWebSocket(this.params, file, meta, options);
|
|
535
178
|
}
|
|
536
|
-
}
|
|
179
|
+
};
|
|
537
180
|
export {
|
|
538
|
-
|
|
539
|
-
|
|
181
|
+
ContelloUploader,
|
|
182
|
+
UploadAssetRetentionPolicy
|
|
540
183
|
};
|