@unboundcx/sdk 4.0.7 → 4.0.8
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/package.json +1 -1
- package/services/storage.js +71 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unboundcx/sdk",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.8",
|
|
4
4
|
"description": "Official JavaScript SDK for the Unbound API - A comprehensive toolkit for integrating with Unbound's communication, AI, and data management services",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
package/services/storage.js
CHANGED
|
@@ -87,6 +87,69 @@ export class StorageService {
|
|
|
87
87
|
return commonTypes[ext] || 'application/octet-stream';
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
+
// Private helper to detect if a value is a Node Readable or Web ReadableStream
|
|
91
|
+
_isStreamLike(value) {
|
|
92
|
+
if (!value || typeof value !== 'object') return false;
|
|
93
|
+
if (typeof value.pipe === 'function') return true; // Node Readable
|
|
94
|
+
if (typeof value.getReader === 'function') return true; // Web ReadableStream
|
|
95
|
+
if (typeof value[Symbol.asyncIterator] === 'function') return true;
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Private helper to create a streaming multipart body for Node.js.
|
|
100
|
+
// Returns { body: ReadableStream, headers } for fetch() with duplex: 'half'.
|
|
101
|
+
// Byte-identical framing to _createNodeFormData — emitted in chunks so the full
|
|
102
|
+
// file never has to sit in memory.
|
|
103
|
+
_createNodeFormDataStream(fileStream, fileName, formFields) {
|
|
104
|
+
const boundary = `----formdata-${Date.now()}-${Math.random()
|
|
105
|
+
.toString(36)
|
|
106
|
+
.slice(2)}`;
|
|
107
|
+
const CRLF = '\r\n';
|
|
108
|
+
const contentType = this._getContentType(fileName);
|
|
109
|
+
|
|
110
|
+
const header =
|
|
111
|
+
`--${boundary}${CRLF}` +
|
|
112
|
+
`Content-Disposition: form-data; name="files"; filename="${
|
|
113
|
+
fileName || 'file'
|
|
114
|
+
}"${CRLF}` +
|
|
115
|
+
`Content-Type: ${contentType}${CRLF}${CRLF}`;
|
|
116
|
+
|
|
117
|
+
let tail = '';
|
|
118
|
+
for (const [name, value] of formFields) {
|
|
119
|
+
tail += `${CRLF}--${boundary}${CRLF}Content-Disposition: form-data; name="${name}"${CRLF}${CRLF}${value}`;
|
|
120
|
+
}
|
|
121
|
+
tail += `${CRLF}--${boundary}--${CRLF}`;
|
|
122
|
+
|
|
123
|
+
const body = new ReadableStream({
|
|
124
|
+
async start(controller) {
|
|
125
|
+
try {
|
|
126
|
+
controller.enqueue(Buffer.from(header, 'utf8'));
|
|
127
|
+
for await (const chunk of fileStream) {
|
|
128
|
+
controller.enqueue(
|
|
129
|
+
Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk),
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
controller.enqueue(Buffer.from(tail, 'utf8'));
|
|
133
|
+
controller.close();
|
|
134
|
+
} catch (err) {
|
|
135
|
+
controller.error(err);
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
cancel(reason) {
|
|
139
|
+
if (fileStream && typeof fileStream.destroy === 'function') {
|
|
140
|
+
fileStream.destroy(reason);
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
return {
|
|
146
|
+
body,
|
|
147
|
+
headers: {
|
|
148
|
+
'content-type': `multipart/form-data; boundary=${boundary}`,
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
90
153
|
// Private helper to create FormData for Node.js environment
|
|
91
154
|
_createNodeFormData(file, fileName, formFields) {
|
|
92
155
|
const boundary = `----formdata-${Date.now()}-${Math.random().toString(36)}`;
|
|
@@ -234,7 +297,13 @@ export class StorageService {
|
|
|
234
297
|
// Default behavior: Use fetch via sdk._fetch
|
|
235
298
|
let formData, headers;
|
|
236
299
|
|
|
237
|
-
if (isNode) {
|
|
300
|
+
if (isNode && this._isStreamLike(file)) {
|
|
301
|
+
// Streaming path — file is piped chunk-by-chunk, no full-buffer copy.
|
|
302
|
+
// Caller is responsible for creating a fresh stream per retry.
|
|
303
|
+
const result = this._createNodeFormDataStream(file, fileName, formFields);
|
|
304
|
+
formData = result.body;
|
|
305
|
+
headers = result.headers;
|
|
306
|
+
} else if (isNode) {
|
|
238
307
|
const result = this._createNodeFormData(file, fileName, formFields);
|
|
239
308
|
formData = result.formData;
|
|
240
309
|
headers = result.headers;
|
|
@@ -381,7 +450,7 @@ Response:
|
|
|
381
450
|
/**
|
|
382
451
|
* Upload a file to storage with optional format conversion
|
|
383
452
|
* @param {Object} config - Configuration object
|
|
384
|
-
* @param {Object} config.file - File content
|
|
453
|
+
* @param {Object} config.file - File content: Buffer, File, Node Readable stream, or Web ReadableStream. Streams upload chunk-by-chunk (no full-buffer copy) — recommended for files > ~100 MB. For retries, create a fresh stream per attempt. REQUIRED
|
|
385
454
|
* @param {string} [config.classification='generic'] - File classification (e.g., 'fax', 'files', 'generic')
|
|
386
455
|
* @param {string} [config.folder] - Folder path for organizing files
|
|
387
456
|
* @param {string} [config.fileName] - Original file name
|