@karpeleslab/klbfw 0.2.11 → 0.2.13
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/index.d.ts +2 -0
- package/index.js +1 -0
- package/package.json +1 -1
- package/rest.js +55 -0
- package/upload.js +23 -6
package/index.d.ts
CHANGED
|
@@ -32,6 +32,7 @@ declare function setCookie(name: string, value: string, expires?: Date | number,
|
|
|
32
32
|
declare function rest(name: string, verb: string, params?: Record<string, any>, context?: Record<string, any>): Promise<any>;
|
|
33
33
|
declare function rest_get(name: string, params?: Record<string, any>): Promise<any>; // Backward compatibility
|
|
34
34
|
declare function restGet(name: string, params?: Record<string, any>): Promise<any>;
|
|
35
|
+
declare function restSSE(name: string, method: 'GET', params?: Record<string, any>, context?: Record<string, any>): EventSource;
|
|
35
36
|
|
|
36
37
|
// Upload module types
|
|
37
38
|
interface UploadOptions {
|
|
@@ -75,6 +76,7 @@ export {
|
|
|
75
76
|
rest,
|
|
76
77
|
rest_get,
|
|
77
78
|
restGet,
|
|
79
|
+
restSSE,
|
|
78
80
|
upload,
|
|
79
81
|
getI18N,
|
|
80
82
|
trimPrefix
|
package/index.js
CHANGED
|
@@ -42,6 +42,7 @@ module.exports.setCookie = cookies.setCookie;
|
|
|
42
42
|
module.exports.rest = rest.rest;
|
|
43
43
|
module.exports.rest_get = rest.rest_get; // Backward compatibility
|
|
44
44
|
module.exports.restGet = rest.restGet; // New camelCase name
|
|
45
|
+
module.exports.restSSE = rest.restSSE;
|
|
45
46
|
|
|
46
47
|
// Upload module exports
|
|
47
48
|
module.exports.upload = upload.upload;
|
package/package.json
CHANGED
package/rest.js
CHANGED
|
@@ -153,9 +153,64 @@ const restGet = (name, params) => {
|
|
|
153
153
|
});
|
|
154
154
|
};
|
|
155
155
|
|
|
156
|
+
/**
|
|
157
|
+
* Creates a Server-Sent Events (SSE) connection to a REST API endpoint
|
|
158
|
+
* @param {string} name - API endpoint name
|
|
159
|
+
* @param {string} method - HTTP method (must be GET)
|
|
160
|
+
* @param {Object} params - Request parameters
|
|
161
|
+
* @param {Object} context - Context object with additional parameters
|
|
162
|
+
* @returns {EventSource} EventSource instance for the SSE connection
|
|
163
|
+
*/
|
|
164
|
+
const restSSE = (name, method, params, context) => {
|
|
165
|
+
// EventSource only supports GET requests
|
|
166
|
+
if (method !== 'GET') {
|
|
167
|
+
throw new Error('EventSource only supports GET method');
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// EventSource only works in browsers
|
|
171
|
+
if (typeof EventSource === 'undefined') {
|
|
172
|
+
throw new Error('EventSource is not supported in this environment');
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (!internal.checkSupport()) {
|
|
176
|
+
throw new Error('Environment not supported');
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
params = params || {};
|
|
180
|
+
context = context || {};
|
|
181
|
+
|
|
182
|
+
// Add timezone data if in browser
|
|
183
|
+
if (typeof window !== 'undefined') {
|
|
184
|
+
context['t'] = internal.getTimezoneData();
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Build URL with authentication and context
|
|
188
|
+
let callUrl = internal.buildRestUrl(name, true, context);
|
|
189
|
+
|
|
190
|
+
// Add params to the URL
|
|
191
|
+
if (params) {
|
|
192
|
+
const glue = callUrl.indexOf('?') === -1 ? '?' : '&';
|
|
193
|
+
if (typeof params === 'string') {
|
|
194
|
+
callUrl += glue + '_=' + encodeURIComponent(params);
|
|
195
|
+
} else {
|
|
196
|
+
callUrl += glue + '_=' + encodeURIComponent(JSON.stringify(params));
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Create and return EventSource instance
|
|
201
|
+
// Note: EventSource doesn't support custom headers directly,
|
|
202
|
+
// but authentication is handled via URL parameters or cookies
|
|
203
|
+
const eventSource = new EventSource(callUrl, {
|
|
204
|
+
withCredentials: true
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
return eventSource;
|
|
208
|
+
};
|
|
209
|
+
|
|
156
210
|
// Export new camelCase API
|
|
157
211
|
module.exports.rest = rest;
|
|
158
212
|
module.exports.restGet = restGet;
|
|
213
|
+
module.exports.restSSE = restSSE;
|
|
159
214
|
|
|
160
215
|
// Backward compatibility
|
|
161
216
|
module.exports.rest_get = restGet;
|
package/upload.js
CHANGED
|
@@ -459,7 +459,7 @@ module.exports.upload = (function () {
|
|
|
459
459
|
params.filename = up.file.name;
|
|
460
460
|
params.size = up.file.size;
|
|
461
461
|
params.lastModified = up.file.lastModified / 1000;
|
|
462
|
-
params.type = up.file.type;
|
|
462
|
+
params.type = up.file.type || "application/octet-stream";
|
|
463
463
|
|
|
464
464
|
// Initialize upload with the server
|
|
465
465
|
rest.rest(up.path, "POST", params, up.context)
|
|
@@ -497,7 +497,7 @@ module.exports.upload = (function () {
|
|
|
497
497
|
"POST",
|
|
498
498
|
"uploads=",
|
|
499
499
|
"",
|
|
500
|
-
{"Content-Type": up.file.type, "X-Amz-Acl": "private"},
|
|
500
|
+
{"Content-Type": up.file.type || "application/octet-stream", "X-Amz-Acl": "private"},
|
|
501
501
|
up.context
|
|
502
502
|
)
|
|
503
503
|
.then(response => response.text())
|
|
@@ -609,9 +609,18 @@ module.exports.upload = (function () {
|
|
|
609
609
|
up.context
|
|
610
610
|
)
|
|
611
611
|
.then(response => {
|
|
612
|
+
// Verify the response is successful
|
|
613
|
+
if (!response.ok) {
|
|
614
|
+
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
615
|
+
}
|
|
612
616
|
// Store ETag for this part (needed for completion)
|
|
613
|
-
|
|
614
|
-
|
|
617
|
+
const etag = response.headers.get("ETag");
|
|
618
|
+
// Read response body to ensure request completed
|
|
619
|
+
return response.text().then(() => etag);
|
|
620
|
+
})
|
|
621
|
+
.then(etag => {
|
|
622
|
+
up.b[partNumber] = etag;
|
|
623
|
+
|
|
615
624
|
// Update progress and continue processing
|
|
616
625
|
sendProgress();
|
|
617
626
|
upload.run();
|
|
@@ -629,7 +638,7 @@ module.exports.upload = (function () {
|
|
|
629
638
|
function uploadPutPart(up, partNumber, startByte, data) {
|
|
630
639
|
// Set up headers
|
|
631
640
|
const headers = {
|
|
632
|
-
"Content-Type": up.file.type
|
|
641
|
+
"Content-Type": up.file.type || "application/octet-stream"
|
|
633
642
|
};
|
|
634
643
|
|
|
635
644
|
// Add Content-Range header for multipart PUT
|
|
@@ -645,9 +654,17 @@ module.exports.upload = (function () {
|
|
|
645
654
|
headers: headers,
|
|
646
655
|
})
|
|
647
656
|
.then(response => {
|
|
657
|
+
// Verify the response is successful
|
|
658
|
+
if (!response.ok) {
|
|
659
|
+
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
660
|
+
}
|
|
661
|
+
// Read response body to ensure request completed
|
|
662
|
+
return response.text();
|
|
663
|
+
})
|
|
664
|
+
.then(() => {
|
|
648
665
|
// Mark part as done
|
|
649
666
|
up.b[partNumber] = "done";
|
|
650
|
-
|
|
667
|
+
|
|
651
668
|
// Update progress and continue processing
|
|
652
669
|
sendProgress();
|
|
653
670
|
upload.run();
|