@karpeleslab/klbfw 0.1.7 → 0.1.10

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/fw-wrapper.js CHANGED
@@ -5,7 +5,7 @@ module.exports.getLocale = () => (typeof FW !== "undefined") ? FW.Locale : "en-U
5
5
  module.exports.getPath = () => (typeof FW !== "undefined") ? FW.path : window.location.pathname;
6
6
  module.exports.getHostname = () => (typeof FW !== "undefined") ? FW.hostname : window.location.hostname;
7
7
  module.exports.getCurrency = () => (typeof FW !== "undefined") ? FW.Context.c : "USD";
8
- module.exports.getContext = () => (typeof FW !== "undefined") ? FW.Context : {};
8
+ module.exports.getContext = () => (typeof FW !== "undefined") ? Object.assign({}, FW.Context) : {};
9
9
  module.exports.setContext = (k, v) => { if (typeof FW !== "undefined") FW.Context[k] = v; };
10
10
  module.exports.getToken = () => (typeof FW !== "undefined") ? FW.token : undefined;
11
11
  module.exports.getRegistry = () => (typeof FW !== "undefined") ? FW.Registry : undefined;
package/internal.js CHANGED
@@ -43,7 +43,7 @@ function rest_url(path, with_token, context) {
43
43
  if (fwWrapper.getCallUrlPrefix()) call_url = fwWrapper.getCallUrlPrefix() + call_url;
44
44
 
45
45
  // copy context, proceed with overload then add to url
46
- var ctx_final = Object.assign({}, fwWrapper.getContext());
46
+ var ctx_final = fwWrapper.getContext();
47
47
  for (var i in context) ctx_final[i] = context[i];
48
48
  for (var i in ctx_final) {
49
49
  if (i == "_") continue;
@@ -132,7 +132,7 @@ function responseParse(response, resolve, reject) {
132
132
  function (json) {
133
133
  // check for gtag
134
134
  if ((json.gtag) && (typeof window !== "undefined") && (window.gtag)) {
135
- json.gtag.map((item) => window.gtag.apply(null, item));
135
+ json.gtag.map(function (item) { window.gtag.apply(null, item); });
136
136
  }
137
137
  // check for result
138
138
  if (json.result != "success" && json.result != "redirect") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@karpeleslab/klbfw",
3
- "version": "0.1.7",
3
+ "version": "0.1.10",
4
4
  "description": "Frontend Framework",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/rest.js CHANGED
@@ -7,7 +7,7 @@ const fwWrapper = require('./fw-wrapper');
7
7
  module.exports.rest = (name, verb, params, context) => {
8
8
  if (typeof __platformAsyncRest !== "undefined") {
9
9
  context = context || {};
10
- var ctx_final = Object.assign({}, fwWrapper.getContext());
10
+ var ctx_final = fwWrapper.getContext();
11
11
  for (var i in context) ctx_final[i] = context[i];
12
12
  return __platformAsyncRest(name, verb, params, ctx_final);
13
13
  }
package/upload.js CHANGED
@@ -117,35 +117,55 @@ module.exports.upload = (function () {
117
117
  params["type"] = up.file.type;
118
118
 
119
119
  rest.rest(up.path, "POST", params, up.context).then(function (res) {
120
- if (!res["data"]["Cloud_Aws_Bucket_Upload__"]) {
121
- // invalid data
122
- up.reject();
123
- delete upload_running[up.up_id];
124
- upload_failed.push(up);
120
+ // Method 1: aws signed multipart upload
121
+ if (res["data"]["Cloud_Aws_Bucket_Upload__"]) {
122
+ up.info = res["data"]; // contains stuff like Bucket_Endpoint, Key, etc
123
+
124
+ // ok we are ready to upload - this will initiate an upload
125
+ awsReq(up.info, "POST", "uploads=", "", {"Content-Type": up.file.type, "X-Amz-Acl": "private"}, up.context)
126
+ .then(response => response.text())
127
+ .then(str => (new DOMParser()).parseFromString(str, "text/xml"))
128
+ .then(dom => dom.querySelector('UploadId').innerHTML)
129
+ .then(function (uploadId) {
130
+ up.uploadId = uploadId;
131
+
132
+ // ok, let's compute block size so we know how many parts we need to send
133
+ var fsize = up.file.size;
134
+ var bsize = Math.ceil(fsize / 10000); // we want ~10k parts
135
+ if (bsize < 5242880) bsize = 5242880; // minimum block size = 5MB
136
+
137
+ up.method = 'aws';
138
+ up.bsize = bsize;
139
+ up.blocks = Math.ceil(fsize / bsize);
140
+ up.b = {};
141
+ up['status'] = 'uploading';
142
+ upload.run();
143
+ }).catch(res => failure(up, res))
125
144
  return;
126
145
  }
146
+ // Method 2: PUT requests
147
+ if (res["data"]["PUT"]) {
148
+ var fsize = up.file.size;
149
+ var bsize = fsize; // upload file in a single block
150
+ if (res["data"]["Blocksize"]) {
151
+ // this upload target supports multipart PUT upload
152
+ bsize = res["data"]["Blocksize"]; // multipart upload
153
+ }
127
154
 
128
- up.info = res["data"]; // contains stuff like Bucket_Endpoint, Key, etc
129
-
130
- // ok we are ready to upload - this will initiate an upload
131
- awsReq(up.info, "POST", "uploads=", "", {"Content-Type": up.file.type, "X-Amz-Acl": "private"}, up.context)
132
- .then(response => response.text())
133
- .then(str => (new DOMParser()).parseFromString(str, "text/xml"))
134
- .then(dom => dom.querySelector('UploadId').innerHTML)
135
- .then(function (uploadId) {
136
- up.uploadId = uploadId;
137
-
138
- // ok, let's compute block size so we know how many parts we need to send
139
- var fsize = up.file.size;
140
- var bsize = Math.ceil(fsize / 10000); // we want ~10k parts
141
- if (bsize < 5242880) bsize = 5242880; // minimum block size = 5MB
142
-
143
- up.bsize = bsize;
144
- up.blocks = Math.ceil(fsize / bsize);
145
- up.b = {};
146
- up['status'] = 'uploading';
147
- upload.run();
148
- }).catch(res => failure(up, res))
155
+ up.info = res["data"];
156
+ up.method = 'put';
157
+ up.bsize = bsize;
158
+ up.blocks = Math.ceil(fsize / bsize);
159
+ up.b = {};
160
+ up['status'] = 'uploading';
161
+ upload.run();
162
+ return;
163
+ }
164
+ // invalid data
165
+ up.reject();
166
+ delete upload_running[up.up_id];
167
+ upload_failed.push(up);
168
+ return;
149
169
  })
150
170
  .catch(res => failure(up, res));
151
171
  }
@@ -185,12 +205,36 @@ module.exports.upload = (function () {
185
205
 
186
206
  var reader = new FileReader();
187
207
  reader.addEventListener("loadend", function () {
188
- awsReq(up.info, "PUT", "partNumber=" + (partno + 1) + "&uploadId=" + up.uploadId, reader.result, null, up.context)
189
- .then(function (response) {
190
- up.b[partno] = response.headers.get("ETag");
208
+ switch(up.method) {
209
+ case 'aws':
210
+ awsReq(up.info, "PUT", "partNumber=" + (partno + 1) + "&uploadId=" + up.uploadId, reader.result, null, up.context)
211
+ .then(function (response) {
212
+ up.b[partno] = response.headers.get("ETag");
213
+ sendprogress();
214
+ upload.run();
215
+ }).catch(res => failure(up, res));
216
+ break;
217
+ case 'put':
218
+ let headers = {};
219
+ headers["Content-Type"] = up.file.type;
220
+ if (up.blocks > 1) {
221
+ // add Content-Range header
222
+ // Content-Range: bytes start-end/*
223
+ const end = start + reader.result.byteLength - 1; // inclusive
224
+ headers["Content-Range"] = "bytes "+start+"-"+end+"/*";
225
+ }
226
+
227
+ fetch(up.info["PUT"], {
228
+ method: "PUT",
229
+ body: reader.result,
230
+ headers: headers,
231
+ }).then(function (response) {
232
+ up.b[partno] = "done";
191
233
  sendprogress();
192
234
  upload.run();
193
235
  }).catch(res => failure(up, res));
236
+ break;
237
+ }
194
238
  });
195
239
 
196
240
  reader.addEventListener("error", function (e) {
@@ -221,19 +265,34 @@ module.exports.upload = (function () {
221
265
  up["done"] = d;
222
266
 
223
267
  if (p == 0) {
224
- // complete, see https://docs.aws.amazon.com/AmazonS3/latest/API/mpUploadComplete.html
225
- up["status"] = "validating";
226
- var xml = "<CompleteMultipartUpload>";
227
- for (var i = 0; i < up.blocks; i++) {
228
- xml += "<Part><PartNumber>" + (i + 1) + "</PartNumber><ETag>" + up.b[i] + "</ETag></Part>";
229
- }
230
- xml += "</CompleteMultipartUpload>";
231
- awsReq(up.info, "POST", "uploadId=" + up.uploadId, xml, null, up.context)
232
- .then(response => response.text())
233
- .then(function (r) {
234
- // if success, need to call finalize
235
- rest.rest("Cloud/Aws/Bucket/Upload/" + up.info.Cloud_Aws_Bucket_Upload__ + ":handleComplete", "POST", {}, up.context).then(function (ares) {
236
- // SUCCESS!
268
+ switch(up.method) {
269
+ case 'aws':
270
+ // complete, see https://docs.aws.amazon.com/AmazonS3/latest/API/mpUploadComplete.html
271
+ up["status"] = "validating";
272
+ var xml = "<CompleteMultipartUpload>";
273
+ for (var i = 0; i < up.blocks; i++) {
274
+ xml += "<Part><PartNumber>" + (i + 1) + "</PartNumber><ETag>" + up.b[i] + "</ETag></Part>";
275
+ }
276
+ xml += "</CompleteMultipartUpload>";
277
+ awsReq(up.info, "POST", "uploadId=" + up.uploadId, xml, null, up.context)
278
+ .then(response => response.text())
279
+ .then(function (r) {
280
+ // if success, need to call finalize
281
+ rest.rest("Cloud/Aws/Bucket/Upload/" + up.info.Cloud_Aws_Bucket_Upload__ + ":handleComplete", "POST", {}, up.context).then(function (ares) {
282
+ // SUCCESS!
283
+ up["status"] = "complete";
284
+ up["final"] = ares["data"];
285
+ sendprogress();
286
+ up.resolve(up);
287
+ delete upload_running[up.up_id];
288
+ upload.run();
289
+ }).catch(res => failure(up, res));
290
+ }).catch(res => failure(up, res));
291
+ break;
292
+ case 'put':
293
+ // complete, directly call handleComplete
294
+ rest.rest(up.info.Complete, "POST", {}, up.context).then(function (ares) {
295
+ // success!
237
296
  up["status"] = "complete";
238
297
  up["final"] = ares["data"];
239
298
  sendprogress();
@@ -241,7 +300,8 @@ module.exports.upload = (function () {
241
300
  delete upload_running[up.up_id];
242
301
  upload.run();
243
302
  }).catch(res => failure(up, res));
244
- }).catch(res => failure(up, res));
303
+ break;
304
+ }
245
305
  }
246
306
  }
247
307