@sendsafely/sendsafely 1.1.0 → 1.1.1

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/Dropzone.js CHANGED
@@ -1,120 +1,120 @@
1
- const URL = require('url').URL;
2
- const Window = require('window');
3
- const window = new Window();
4
- const $ = require('jquery')(window);
5
- const XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
6
-
7
- /**
8
- * @param eventHandler
9
- * @param {string} url
10
- * @param {string} dropzoneId
11
- * @param {string} requestAPI
12
- * @constructor
13
- */
14
- function AnonymousRequest(eventHandler, url, dropzoneId, requestAPI) {
15
- const myself = this;
16
-
17
- this.apiPrefix = '/drop-zone/v2.0';
18
- this.url = url;
19
- this.dropzoneId = dropzoneId;
20
- this.eventHandler = eventHandler;
21
- this.requestAPI = requestAPI;
22
-
23
- this.sendRequest = function (requestType, messageData, a_sync) {
24
-
25
- if (typeof a_sync === 'undefined') {
26
- a_sync = true;
27
- }
28
-
29
- return $.ajax({
30
- url: myself.url + myself.apiPrefix + requestType.url,
31
- type: requestType.HTTPMethod,
32
- timeout: 25000,
33
- data: messageData == null ? null : JSON.stringify(messageData),
34
- contentType: requestType.mimetype,
35
- headers: {
36
- 'ss-api-key': myself.dropzoneId,
37
- 'ss-request-api' : myself.requestAPI
38
- },
39
- crossDomain: true,
40
- async: a_sync,
41
- retryCount: 2, //Need to Implement.
42
- });
43
- };
44
-
45
- this.getHTTPSOptionForFileUpload = function (uri, method, messageData, boundary, isEC2Proxy) {
46
- let headers = {
47
- 'Content-Type': 'multipart/form-data; boundary=' + boundary,
48
- 'ss-api-key': myself.dropzoneId,
49
- 'ss-request-api' : myself.requestAPI
50
- };
51
- let url = new URL(myself.url + myself.apiPrefix + uri);
52
-
53
- if (!isEC2Proxy) {
54
- headers = {};
55
- url = new URL(uri);
56
- }
57
-
58
- return {
59
- hostname: url.hostname,
60
- port: url.port,
61
- path: url.pathname + url.search,
62
- headers: headers,
63
- method: method,
64
- };
65
- };
66
-
67
- this.getHTTPObjForFileUpload = function (uri, messageData, boundary, a_sync) {
68
- const xhr = new XMLHttpRequest();
69
- const url = myself.url + myself.apiPrefix + uri;
70
-
71
- xhr.open('POST', url, a_sync);
72
-
73
- xhr.setRequestHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
74
- xhr.setRequestHeader('ss-api-key', myself.dropzoneId);
75
- xhr.setRequestHeader('ss-request-api', myself.requestAPI);
76
-
77
- return xhr;
78
- };
79
-
80
- /**
81
- * Function used to deal with Errors, and callbacks for AJAX Requests.
82
- * Progress callback cannot be done when async is false.
83
- * @ignore
84
- * @param {promise} ajax AJAX Promise
85
- * @param {function} error_callback Function is called when there is an error with the function or when there is an
86
- * error in the response.
87
- * @param {function} success_callback Function is called when data is received from the server with no errors.
88
- * @param {function} progress_callback Function is called when the data is being uploaded.
89
- */
90
- this.processAjaxData = function (ajax, success_callback) {
91
- ajax.fail(function (xhr, status, error) {
92
- // Wrap the error to a format we recognize.
93
- myself.eventHandler.raiseError(this.NETWORK_ERROR, error.message);
94
- })
95
- .done(function (data) {
96
- if (typeof data == 'string') {
97
- data = JSON.parse(data);
98
- }
99
- if (data.response === 'SUCCESS') {
100
- if (success_callback !== undefined) {
101
- success_callback(data);
102
- }
103
- } else {
104
- myself.eventHandler.raiseError(data.response, data.message);
105
- }
106
- });
107
- };
108
-
109
- this.extend = function (a, b) {
110
- for (const key in b) {
111
- if (b.hasOwnProperty(key)) {
112
- a[key] = b[key];
113
- }
114
- }
115
- return a;
116
- };
117
- }
118
-
119
-
120
- module.exports = {AnonymousRequest};
1
+ const URL = require('url').URL;
2
+ const Window = require('window');
3
+ const window = new Window();
4
+ const $ = require('jquery')(window);
5
+ const XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
6
+
7
+ /**
8
+ * @param eventHandler
9
+ * @param {string} url
10
+ * @param {string} dropzoneId
11
+ * @param {string} requestAPI
12
+ * @constructor
13
+ */
14
+ function AnonymousRequest(eventHandler, url, dropzoneId, requestAPI) {
15
+ const myself = this;
16
+
17
+ this.apiPrefix = '/drop-zone/v2.0';
18
+ this.url = url;
19
+ this.dropzoneId = dropzoneId;
20
+ this.eventHandler = eventHandler;
21
+ this.requestAPI = requestAPI;
22
+
23
+ this.sendRequest = function (requestType, messageData, a_sync) {
24
+
25
+ if (typeof a_sync === 'undefined') {
26
+ a_sync = true;
27
+ }
28
+
29
+ return $.ajax({
30
+ url: myself.url + myself.apiPrefix + requestType.url,
31
+ type: requestType.HTTPMethod,
32
+ timeout: 25000,
33
+ data: messageData == null ? null : JSON.stringify(messageData),
34
+ contentType: requestType.mimetype,
35
+ headers: {
36
+ 'ss-api-key': myself.dropzoneId,
37
+ 'ss-request-api' : myself.requestAPI
38
+ },
39
+ crossDomain: true,
40
+ async: a_sync,
41
+ retryCount: 2, //Need to Implement.
42
+ });
43
+ };
44
+
45
+ this.getHTTPSOptionForFileUpload = function (uri, method, messageData, boundary, isEC2Proxy) {
46
+ let headers = {
47
+ 'Content-Type': 'multipart/form-data; boundary=' + boundary,
48
+ 'ss-api-key': myself.dropzoneId,
49
+ 'ss-request-api' : myself.requestAPI
50
+ };
51
+ let url = new URL(myself.url + myself.apiPrefix + uri);
52
+
53
+ if (!isEC2Proxy) {
54
+ headers = {};
55
+ url = new URL(uri);
56
+ }
57
+
58
+ return {
59
+ hostname: url.hostname,
60
+ port: url.port,
61
+ path: url.pathname + url.search,
62
+ headers: headers,
63
+ method: method,
64
+ };
65
+ };
66
+
67
+ this.getHTTPObjForFileUpload = function (uri, messageData, boundary, a_sync) {
68
+ const xhr = new XMLHttpRequest();
69
+ const url = myself.url + myself.apiPrefix + uri;
70
+
71
+ xhr.open('POST', url, a_sync);
72
+
73
+ xhr.setRequestHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
74
+ xhr.setRequestHeader('ss-api-key', myself.dropzoneId);
75
+ xhr.setRequestHeader('ss-request-api', myself.requestAPI);
76
+
77
+ return xhr;
78
+ };
79
+
80
+ /**
81
+ * Function used to deal with Errors, and callbacks for AJAX Requests.
82
+ * Progress callback cannot be done when async is false.
83
+ * @ignore
84
+ * @param {promise} ajax AJAX Promise
85
+ * @param {function} error_callback Function is called when there is an error with the function or when there is an
86
+ * error in the response.
87
+ * @param {function} success_callback Function is called when data is received from the server with no errors.
88
+ * @param {function} progress_callback Function is called when the data is being uploaded.
89
+ */
90
+ this.processAjaxData = function (ajax, success_callback) {
91
+ ajax.fail(function (xhr, status, error) {
92
+ // Wrap the error to a format we recognize.
93
+ myself.eventHandler.raiseError(this.NETWORK_ERROR, error.message);
94
+ })
95
+ .done(function (data) {
96
+ if (typeof data == 'string') {
97
+ data = JSON.parse(data);
98
+ }
99
+ if (data.response === 'SUCCESS') {
100
+ if (success_callback !== undefined) {
101
+ success_callback(data);
102
+ }
103
+ } else {
104
+ myself.eventHandler.raiseError(data.response, data.message);
105
+ }
106
+ });
107
+ };
108
+
109
+ this.extend = function (a, b) {
110
+ for (const key in b) {
111
+ if (b.hasOwnProperty(key)) {
112
+ a[key] = b[key];
113
+ }
114
+ }
115
+ return a;
116
+ };
117
+ }
118
+
119
+
120
+ module.exports = {AnonymousRequest};
package/FileUtil.js ADDED
@@ -0,0 +1,110 @@
1
+ const fs = require('fs');
2
+ const path = require("path");
3
+
4
+ function FileUtil(param) {
5
+
6
+ if(param === undefined || typeof param !== 'object') {
7
+ throw new Error('FileUtil: Invalid parameters');
8
+ }
9
+
10
+ if(!param.hasOwnProperty('filePath')) {
11
+ throw new Error('FileUtil: filePath is needed');
12
+ }
13
+
14
+ if(!param.hasOwnProperty('callback')) {
15
+ throw new Error('FileUtil: callback is needed');
16
+ }
17
+
18
+ if(param.hasOwnProperty('callback') && typeof param.callback !== 'function') {
19
+ throw new Error('FileUtil: callback must be a function');
20
+ }
21
+
22
+ let myself = this;
23
+ this.SEGMENT_SIZE = 2621440;
24
+ this.filePath = param.filePath;
25
+ this.callback = param.callback;
26
+ this.file = {size: 0, name: '', totalParts: 0};
27
+ this.readableStream = fs.createReadStream(myself.filePath);
28
+ this.reading = false;
29
+ this.eof = false;
30
+ this.data = [];
31
+ this.tempSize = 0;
32
+
33
+ this.init = function() {
34
+ return new Promise(function(resolve) {
35
+ fs.stat(myself.filePath, (err, stats) => {
36
+ if (err) {
37
+ throw new Error('FileUtil: File does not exist, ' + myself.filePath);
38
+ } else {
39
+ myself.file.size = stats.size;
40
+ myself.file.name = path.basename(myself.filePath);
41
+
42
+ if(myself.file.size > (myself.SEGMENT_SIZE/4)) {
43
+ myself.file.totalParts = Math.ceil((myself.file.size-(myself.SEGMENT_SIZE/4))/myself.SEGMENT_SIZE);
44
+ } else {
45
+ myself.file.totalParts = 1;
46
+ }
47
+
48
+ resolve(myself.file);
49
+
50
+ myself.readableStream.on('readable', function() {
51
+ // keep reading chunk until it reaches SEGMENT_SIZE
52
+ if(myself.reading && !myself.eof) {
53
+ processChunk();
54
+ }
55
+ });
56
+
57
+ myself.readableStream.on('end', function() {
58
+ // done reading file
59
+ if(!myself.eof) {
60
+ myself.eof = true;
61
+ callback(true);
62
+ }
63
+ });
64
+ }
65
+ });
66
+
67
+ });
68
+ }
69
+
70
+ this.read = function() {
71
+ if(!myself.reading && !myself.eof) {
72
+ myself.reading = true;
73
+ processChunk();
74
+ }
75
+ }
76
+
77
+ function processChunk() {
78
+ if(myself.tempSize === myself.SEGMENT_SIZE) {
79
+ // callback when data size reaches SEGMENT_SIZE
80
+ callback(false);
81
+ } else {
82
+ let chunk = myself.readableStream.read();
83
+ if(chunk !== null) {
84
+ myself.data.push(new Uint8Array(chunk));
85
+ myself.tempSize += chunk.length;
86
+ }
87
+ }
88
+ }
89
+
90
+ function callback(isComplete) {
91
+ myself.tempSize = 0;
92
+ myself.reading = false;
93
+ myself.callback({data: concatenate(myself.data), complete: isComplete});
94
+ myself.data = [];
95
+ }
96
+
97
+ function concatenate(arrays) {
98
+ var totalLength = arrays.reduce(function(total, arr) {
99
+ return total + arr.length
100
+ }, 0);
101
+ var result = new Uint8Array(totalLength);
102
+ arrays.reduce(function(offset, arr){
103
+ result.set(arr, offset);
104
+ return offset + arr.length;
105
+ }, 0);
106
+ return result;
107
+ }
108
+ }
109
+
110
+ module.exports = {FileUtil};
package/README.md CHANGED
@@ -1,35 +1,35 @@
1
-
2
- # SendSafely JavaScript API for Node.js
3
-
4
- The SendSafely JavaScript API for Node.js lets you integrate SendSafely secure data transfer capabilities directly into your Node.js application.
5
-
6
- ## Quickstart
7
- The example below shows you how to install the package and use it as a CommonJS module.
8
-
9
- Install with npm
10
- ```console
11
- npm install @sendsafely/sendsafely
12
- ```
13
-
14
- Include the SendSafely class to start making your API calls.
15
-
16
- ```javascript
17
- var SendSafely = require('@sendsafely/sendsafely');
18
- //var sendSafely = new SendSafely("https://demo.sendsafely.com", "exAPIkeyxyz", "exAPIsecretxyz");
19
- var sendSafely = new SendSafely("https://ORGANIZATION_HOST", "API_KEY", "API_SECRET");
20
-
21
- sendSafely.verifyCredentials(function(email) {
22
- console.log("Connected to SendSafely as user " + email);
23
- });
24
-
25
- ```
26
-
27
- *You will need to generate your own API_KEY and API_SECRET from the API Keys section of your Profile page when logged into your SendSafely portal.*
28
-
29
- ## Examples
30
- Please refer to our [Developer Website](https://sendsafely.github.io) to familiarize yourself with the core SendSafely API and common operations. See our [examples](https://github.com/SendSafely/JavaScript-Node-Client-API/tree/master/examples/CreateNewPackage) in GitHub for working examples of how to use the SendSafely JavaScript API for Node.js.
31
-
32
- ## Support
33
- For support, please contact support@sendsafely.com.
34
-
35
-
1
+
2
+ # SendSafely JavaScript API for Node.js
3
+
4
+ The SendSafely JavaScript API for Node.js lets you integrate SendSafely secure data transfer capabilities directly into your Node.js application.
5
+
6
+ ## Quickstart
7
+ The example below shows you how to install the package and use it as a CommonJS module.
8
+
9
+ Install with npm
10
+ ```console
11
+ npm install @sendsafely/sendsafely
12
+ ```
13
+
14
+ Include the SendSafely class to start making your API calls.
15
+
16
+ ```javascript
17
+ var SendSafely = require('@sendsafely/sendsafely');
18
+ //var sendSafely = new SendSafely("https://demo.sendsafely.com", "exAPIkeyxyz", "exAPIsecretxyz");
19
+ var sendSafely = new SendSafely("https://ORGANIZATION_HOST", "API_KEY", "API_SECRET");
20
+
21
+ sendSafely.verifyCredentials(function(email) {
22
+ console.log("Connected to SendSafely as user " + email);
23
+ });
24
+
25
+ ```
26
+
27
+ *You will need to generate your own API_KEY and API_SECRET from the API Keys section of your Profile page when logged into your SendSafely portal.*
28
+
29
+ ## Examples
30
+ Please refer to our [Developer Website](https://sendsafely.github.io) to familiarize yourself with the core SendSafely API and common operations. See our [examples](https://github.com/SendSafely/JavaScript-Node-Client-API/tree/master/examples/CreateNewPackage) in GitHub for working examples of how to use the SendSafely JavaScript API for Node.js.
31
+
32
+ ## Support
33
+ For support, please contact support@sendsafely.com.
34
+
35
+