@capturebridge/sdk 0.12.0 → 0.14.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.
Files changed (46) hide show
  1. package/CHANGELOG.md +93 -58
  2. package/Camera.js +63 -63
  3. package/CanonCamera.js +25 -25
  4. package/CaptureBridge.js +160 -160
  5. package/CapturePlugin.js +68 -68
  6. package/Common.js +258 -252
  7. package/Device.js +588 -595
  8. package/ICAO.js +86 -86
  9. package/IFace.js +237 -237
  10. package/ImageSource.js +110 -110
  11. package/Logs.js +129 -129
  12. package/MockCamera.js +17 -17
  13. package/Plugin.js +299 -299
  14. package/README.md +3 -3
  15. package/Scanner.js +55 -55
  16. package/SignatureTablet.js +86 -108
  17. package/SignatureTabletWidgets.js +164 -164
  18. package/StreamingCapturePlugin.js +129 -129
  19. package/TopazSigGem.js +68 -25
  20. package/Verifone.js +239 -192
  21. package/VerifoneControls.js +484 -0
  22. package/Version.js +7 -7
  23. package/WindowsScanner.js +25 -25
  24. package/package.json +1 -1
  25. package/examples/canon.html +0 -20
  26. package/examples/canon.js +0 -44
  27. package/examples/canon_getframe.html +0 -20
  28. package/examples/canon_getframe.js +0 -46
  29. package/examples/canon_lan.html +0 -20
  30. package/examples/canon_lan.js +0 -46
  31. package/examples/canon_minimal.html +0 -7
  32. package/examples/canon_minimal.js +0 -27
  33. package/examples/empty.jpg +0 -0
  34. package/examples/icao.html +0 -7
  35. package/examples/icao.jpg +0 -0
  36. package/examples/icao.js +0 -80
  37. package/examples/mockcamera.html +0 -20
  38. package/examples/mockcamera.js +0 -39
  39. package/examples/mockcamera_getframe.html +0 -20
  40. package/examples/mockcamera_getframe.js +0 -40
  41. package/examples/mockcamera_minimal.html +0 -7
  42. package/examples/mockcamera_minimal.js +0 -21
  43. package/examples/verifone_signature.html +0 -7
  44. package/examples/verifone_signature.js +0 -14
  45. package/examples/windows_scanner_minimal.html +0 -8
  46. package/examples/windows_scanner_minimal.js +0 -19
package/CapturePlugin.js CHANGED
@@ -1,68 +1,68 @@
1
- import Plugin from './Plugin.js'
2
- import {
3
- BASE_URL,
4
- DATAURL_JPEG,
5
- DEFAULT_CAPTURE_OPT,
6
- blobToBuffer
7
- } from './Common.js'
8
-
9
- /**
10
- * CapturePlugin
11
- * @classdesc Class for working with a plugin that acquires high resolution
12
- * images from a device.
13
- * @extends Plugin
14
- * @see {@link Camera}
15
- * @see {@link SignatureTablet}
16
- * @see {@link Scanner}
17
- */
18
- class CapturePlugin extends Plugin {
19
- /**
20
- * Instantiate a CapturePlugin
21
- * @constructor
22
- * @param {string} [baseURL] - Protocol, domain, and port for the service.
23
- * @param {string} pluginId - The Id of the desired plugin to use for this
24
- * camera instance. The plugin must support the `start_feed`, `stop_feed`,
25
- * `devices`, and `capture` methods.
26
- * @example
27
- * const capture = new CapturePlugin('capture_camera_canon')
28
- */
29
- constructor (plugin, baseUrl = BASE_URL) {
30
- super(plugin, baseUrl)
31
- }
32
-
33
- /**
34
- * Take a full capture
35
- * @param {CaptureOptions} [captureOpt] - Additional options for capturing an
36
- * image.
37
- * @param {string|object} [deviceOpt] - Take the capture using either a
38
- * specific Device ID or a Device Object. The default is the first available
39
- * device.
40
- *
41
- * @async
42
- * @example
43
- * // Capture an image as an ArrayBuffer and add it to an img tag appended to
44
- * // the document's body.
45
- * const img = document.createElement('img')
46
- * img.src = await capture.fullCapture()
47
- * document.body.appendChild(img)
48
- */
49
- async fullCapture (captureOpt = {}, deviceOpt) {
50
- const device = await this.setupDevice(deviceOpt)
51
- const mergedOpt = Object.assign({}, DEFAULT_CAPTURE_OPT, captureOpt)
52
- switch (mergedOpt.kind) {
53
- case 'objecturl':
54
- return URL.createObjectURL(await device.captureAsBlob(mergedOpt))
55
- case 'blob':
56
- return await device.captureAsBlob(mergedOpt)
57
- case 'buffer':
58
- return await blobToBuffer(await device.captureAsBlob(mergedOpt))
59
- case 'base64':
60
- return await device.captureAsBase64(mergedOpt)
61
- case 'dataurl':
62
- return DATAURL_JPEG + (await device.captureAsBase64(mergedOpt))
63
- }
64
- throw new Error(`Unknown image response kind: ${mergedOpt.kind}`)
65
- }
66
- }
67
-
68
- export default CapturePlugin
1
+ import Plugin from './Plugin.js'
2
+ import {
3
+ BASE_URL,
4
+ DATAURL_JPEG,
5
+ DEFAULT_CAPTURE_OPT,
6
+ blobToBuffer
7
+ } from './Common.js'
8
+
9
+ /**
10
+ * CapturePlugin
11
+ * @classdesc Class for working with a plugin that acquires high resolution
12
+ * images from a device.
13
+ * @extends Plugin
14
+ * @see {@link Camera}
15
+ * @see {@link SignatureTablet}
16
+ * @see {@link Scanner}
17
+ */
18
+ class CapturePlugin extends Plugin {
19
+ /**
20
+ * Instantiate a CapturePlugin
21
+ * @constructor
22
+ * @param {string} [baseURL] - Protocol, domain, and port for the service.
23
+ * @param {string} pluginId - The Id of the desired plugin to use for this
24
+ * camera instance. The plugin must support the `start_feed`, `stop_feed`,
25
+ * `devices`, and `capture` methods.
26
+ * @example
27
+ * const capture = new CapturePlugin('capture_camera_canon')
28
+ */
29
+ constructor (plugin, baseUrl = BASE_URL) {
30
+ super(plugin, baseUrl)
31
+ }
32
+
33
+ /**
34
+ * Take a full capture
35
+ * @param {CaptureOptions} [captureOpt] - Additional options for capturing an
36
+ * image.
37
+ * @param {string|object} [deviceOpt] - Take the capture using either a
38
+ * specific Device ID or a Device Object. The default is the first available
39
+ * device.
40
+ *
41
+ * @async
42
+ * @example
43
+ * // Capture an image as an ArrayBuffer and add it to an img tag appended to
44
+ * // the document's body.
45
+ * const img = document.createElement('img')
46
+ * img.src = await capture.fullCapture()
47
+ * document.body.appendChild(img)
48
+ */
49
+ async fullCapture (captureOpt = {}, deviceOpt) {
50
+ const device = await this.setupDevice(deviceOpt)
51
+ const mergedOpt = Object.assign({}, DEFAULT_CAPTURE_OPT, captureOpt)
52
+ switch (mergedOpt.kind) {
53
+ case 'objecturl':
54
+ return URL.createObjectURL(await device.captureAsBlob(mergedOpt))
55
+ case 'blob':
56
+ return await device.captureAsBlob(mergedOpt)
57
+ case 'buffer':
58
+ return await blobToBuffer(await device.captureAsBlob(mergedOpt))
59
+ case 'base64':
60
+ return await device.captureAsBase64(mergedOpt)
61
+ case 'dataurl':
62
+ return DATAURL_JPEG + (await device.captureAsBase64(mergedOpt))
63
+ }
64
+ throw new Error(`Unknown image response kind: ${mergedOpt.kind}`)
65
+ }
66
+ }
67
+
68
+ export default CapturePlugin
package/Common.js CHANGED
@@ -1,252 +1,258 @@
1
- /**
2
- * @summary Default URL for API requests.
3
- * @constant
4
- * @type {string}
5
- */
6
- export const DEFAULT_URL = 'https://local.capturebridge.net:9001'
7
-
8
- /**
9
- * @summary Base URL for API requests.
10
- * @constant
11
- * @type {string}
12
- * @default {@link https://local.capturebridge.net:9001}
13
- */
14
- export const BASE_URL = window?.location?.origin ?? DEFAULT_URL
15
-
16
- /**
17
- * Common {@link https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API|Fetch}
18
- * options for making
19
- * {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST|POST}
20
- * requests to the API.
21
- * @constant
22
- * @type {object}
23
- */
24
- export const POST = {
25
- mode: 'cors',
26
- method: 'post'
27
- }
28
-
29
- /**
30
- * @summary
31
- * Common {@link https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API|Fetch}
32
- * options for making
33
- * {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE|DELETE}
34
- * requests to the API.
35
- * @constant
36
- * @type {object}
37
- */
38
- export const DELETE = {
39
- mode: 'cors',
40
- method: 'delete'
41
- }
42
-
43
- /**
44
- * @summary Default options for stream operations
45
- * @constant
46
- * @property {number} rotate - Angle, in degrees to rotate the stream. Must be
47
- * a value from -359 to 359. A value outside of that range, or a 0 will perform
48
- * no rotation.
49
- * @typedef {object} StreamOptions
50
- */
51
- export const DEFAULT_STREAM_OPT = {
52
- rotate: 0
53
- }
54
-
55
- /**
56
- * @typedef {string} CaptureResponseKind
57
- * @summary Specifies the desired return type for a captured object. Valid
58
- * values are `objecturl`, `blob`, `buffer`, `base64`, or `dataurl`.
59
- *
60
- * - `objecturl` will load the captured object into an
61
- * {@link https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL_static|ObjectURL}
62
- * that can be set directly on an img tag's src attribute. Note that these URLs
63
- * should be {@link https://developer.mozilla.org/en-US/docs/Web/API/URL/revokeObjectURL_static|revoked}
64
- * when they are no longer needed.
65
- *
66
- * - `buffer` will load the image into an
67
- * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer|ArrayBuffer}
68
- *
69
- * - `blob` will return a
70
- * {@link https://developer.mozilla.org/en-US/docs/Web/API/Blob|Blob} that can
71
- * be used with a
72
- * {@link https://developer.mozilla.org/en-US/docs/Web/API/FileReader|FileReader}.
73
- *
74
- * - `base64` will return the image contents as a
75
- * {@link https://en.wikipedia.org/wiki/Base64|Base64} encoded string that is
76
- * suitable for use with JSON serialization.
77
- *
78
- * - `dataurl` will return a
79
- * {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs|Data URL}
80
- * that can be set on an img tag's src attribute or loaded as a document
81
- * location.
82
- *
83
- */
84
-
85
- /**
86
- * @typedef {string} CaptureResponseFormat
87
- * @summary The image format to be returned.
88
- *
89
- * Valid values:
90
- *
91
- * - `jpg`
92
- * - `tiff`
93
- *
94
- * If not provided, a plugin or device-specific format will be returned.
95
- *
96
- * **Note:**
97
- * - Some plugins have configuration options for specifying the return format,
98
- * such configuration options should be used instead of this parameter to
99
- * prevent unecessary transcoding.
100
- * - Web browsers are not typically capable of displaying TIFF images.
101
- *
102
- */
103
-
104
- /**
105
- * @summary Default options for capture operations.
106
- * @constant
107
- * @property {number?} rotate - Angle, in degrees to rotate the captured object.
108
- * Must be a value from -359 to 359. A value of 0 will perform no rotation.
109
- * @property {boolean?} base64 - Return the captured object as a base64 encoded
110
- * string. The default behavior is to return the image as binary data.
111
- * @property {CaptureResponseKind} kind - Specifies how to return the captured
112
- * object. Some SDK methods that are designed to only return a particular type,
113
- * such as {@link Device#frameAsBase64}, ignore this parameter. This parameter
114
- * is also not sent in API requests, it is used internally by the SDK for
115
- * constructing the appropriate response type.
116
- * @property {CaptureResponseFormat} format - The image format to be returned.
117
- *
118
- * @typedef {object} CaptureOptions
119
- */
120
- export const DEFAULT_CAPTURE_OPT = {
121
- rotate: 0,
122
- base64: false,
123
- format: null,
124
- kind: 'objecturl'
125
- }
126
-
127
- /**
128
- * @summary `data:` prefix for JPEG
129
- * {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs|Data URLs}
130
- * @constant
131
- * @type {string}
132
- */
133
- export const DATAURL_JPEG = 'data:image/jpeg;base64,'
134
-
135
- /**
136
- * @summary `data:` prefix for PNG
137
- * {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs|Data URLs}
138
- * @constant
139
- * @type {string}
140
- */
141
- export const DATAURL_PNG = 'data:image/png;base64,'
142
-
143
- /**
144
- * @summary For a given base64 string, convert it to the appropriate Data URL
145
- * @constant
146
- * @type {string}
147
- */
148
- export const base64ToDataURL = base64 => {
149
- const magicBytes = atob(base64.slice(0, 6))
150
- const byteArray = new Uint8Array(magicBytes.length)
151
- for (let i = 0; i < magicBytes.length; i++) {
152
- byteArray[i] = magicBytes.charCodeAt(i)
153
- }
154
-
155
- // Check for JPEG magic number (FF D8 FF)
156
- if (byteArray[0] === 0xFF && byteArray[1] === 0xD8 && byteArray[2] === 0xFF) {
157
- return DATAURL_JPEG + base64
158
- }
159
-
160
- // Check for PNG magic number (89 50 4E 47)
161
- if (byteArray[0] === 0x89 && byteArray[1] === 0x50 && byteArray[2] === 0x4E && byteArray[3] === 0x47) {
162
- return DATAURL_PNG + base64
163
- }
164
-
165
- throw new Error('Unknown image format provided in Base64 string')
166
- }
167
-
168
- /**
169
- * An Async/Await version of
170
- * {@link https://developer.mozilla.org/en-US/docs/Web/API/setTimeout|setTimeout}
171
- * @private
172
- */
173
- export const timeout = millis => {
174
- return new Promise(resolve => setTimeout(resolve, millis))
175
- }
176
-
177
- /**
178
- * Convert a Blob to a Buffer
179
- * @private
180
- */
181
- export const blobToBuffer = blob => {
182
- return new Promise(resolve => {
183
- const reader = new window.FileReader()
184
- reader.onload = event => resolve(event.target.result)
185
- reader.readAsArrayBuffer(blob)
186
- })
187
- }
188
-
189
- /**
190
- * Convert a Blob to a Base64 String
191
- * @private
192
- */
193
- export const blobToBase64 = blob => {
194
- return new Promise(resolve => {
195
- const reader = new window.FileReader()
196
- reader.onload = event => resolve(event.target.result.split(',')[1])
197
- reader.readAsDataURL(blob)
198
- })
199
- }
200
-
201
- /**
202
- * Helper function for classes to easily add callback style support to their
203
- * async/await methods.
204
- * @private
205
- */
206
- export const asyncToCallback = (thisValue, asyncFn, callback, ...params) => {
207
- (async () => {
208
- try {
209
- const result = await asyncFn.call(thisValue, ...(params.length ? params : []))
210
- callback(null, result)
211
- } catch (ex) {
212
- callback(ex)
213
- }
214
- })()
215
- }
216
-
217
- /**
218
- * Helper function to throw if an http response was a error.
219
- * @private
220
- */
221
- export const checkResponse = async (response) => {
222
- if (!response.ok) {
223
- const error = await response.json()
224
- const msg = error.message || error.summary || error.code || 'Unexpected error'
225
- const ex = new Error(msg)
226
- ex.code = error.code || 500
227
- ex.summary = error.summary || 'Unexpected error'
228
- throw ex
229
- }
230
- }
231
-
232
- /**
233
- * Helper function to convert a key/value map to a string, removing any items
234
- * that are null or undefined (instead of converting them to a string which
235
- * URLSearchParams().toString() will do.)
236
- * @private
237
- */
238
- export const urlParamsToString = (params) => {
239
- const filtered = {}
240
- Object.keys(params).forEach(key => {
241
- if (params[key] !== null && params[key] !== undefined) {
242
- filtered[key] = params[key]
243
- }
244
- })
245
- return new URLSearchParams(filtered).toString()
246
- }
247
-
248
- /**
249
- * Allows passing in a string for the response type of capture methods
250
- * @private
251
- */
252
- export const DEPRECATION_01 = true
1
+ /**
2
+ * @summary Default URL for API requests.
3
+ * @constant
4
+ * @type {string}
5
+ */
6
+ export const DEFAULT_URL = 'https://local.capturebridge.net:9001'
7
+
8
+ /**
9
+ * @summary Base URL for API requests.
10
+ * @constant
11
+ * @type {string}
12
+ * @default {@link https://local.capturebridge.net:9001}
13
+ */
14
+ export const BASE_URL = window?.location?.origin ?? DEFAULT_URL
15
+
16
+ /**
17
+ * Common {@link https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API|Fetch}
18
+ * options for making
19
+ * {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST|POST}
20
+ * requests to the API.
21
+ * @constant
22
+ * @type {object}
23
+ */
24
+ export const POST = {
25
+ mode: 'cors',
26
+ method: 'post'
27
+ }
28
+
29
+ /**
30
+ * @summary
31
+ * Common {@link https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API|Fetch}
32
+ * options for making
33
+ * {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE|DELETE}
34
+ * requests to the API.
35
+ * @constant
36
+ * @type {object}
37
+ */
38
+ export const DELETE = {
39
+ mode: 'cors',
40
+ method: 'delete'
41
+ }
42
+
43
+ /**
44
+ * @summary Default options for stream operations
45
+ * @constant
46
+ * @property {number} rotate - Angle, in degrees to rotate the stream. Must be
47
+ * a value from -359 to 359. A value outside of that range, or a 0 will perform
48
+ * no rotation.
49
+ * @typedef {object} StreamOptions
50
+ */
51
+ export const DEFAULT_STREAM_OPT = {
52
+ rotate: 0
53
+ }
54
+
55
+ /**
56
+ * @typedef {string} CaptureResponseKind
57
+ * @summary Specifies the desired return type for a captured object. Valid
58
+ * values are `objecturl`, `blob`, `buffer`, `base64`, or `dataurl`.
59
+ *
60
+ * - `objecturl` will load the captured object into an
61
+ * {@link https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL_static|ObjectURL}
62
+ * that can be set directly on an img tag's src attribute. Note that these URLs
63
+ * should be {@link https://developer.mozilla.org/en-US/docs/Web/API/URL/revokeObjectURL_static|revoked}
64
+ * when they are no longer needed.
65
+ *
66
+ * - `buffer` will load the image into an
67
+ * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer|ArrayBuffer}
68
+ *
69
+ * - `blob` will return a
70
+ * {@link https://developer.mozilla.org/en-US/docs/Web/API/Blob|Blob} that can
71
+ * be used with a
72
+ * {@link https://developer.mozilla.org/en-US/docs/Web/API/FileReader|FileReader}.
73
+ *
74
+ * - `base64` will return the image contents as a
75
+ * {@link https://en.wikipedia.org/wiki/Base64|Base64} encoded string that is
76
+ * suitable for use with JSON serialization.
77
+ *
78
+ * - `dataurl` will return a
79
+ * {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs|Data URL}
80
+ * that can be set on an img tag's src attribute or loaded as a document
81
+ * location.
82
+ *
83
+ */
84
+
85
+ /**
86
+ * @typedef {string} CaptureResponseFormat
87
+ * @summary The image format to be returned.
88
+ *
89
+ * Valid values:
90
+ *
91
+ * - `jpg`
92
+ * - `tiff`
93
+ *
94
+ * If not provided, a plugin or device-specific format will be returned.
95
+ *
96
+ * **Note:**
97
+ * - Some plugins have configuration options for specifying the return format,
98
+ * such configuration options should be used instead of this parameter to
99
+ * prevent unecessary transcoding.
100
+ * - Web browsers are not typically capable of displaying TIFF images.
101
+ *
102
+ */
103
+
104
+ /**
105
+ * @summary Default options for capture operations.
106
+ * @constant
107
+ * @property {number?} rotate - Angle, in degrees to rotate the captured object.
108
+ * Must be a value from -359 to 359. A value of 0 will perform no rotation.
109
+ * @property {boolean?} base64 - Return the captured object as a base64 encoded
110
+ * string. The default behavior is to return the image as binary data.
111
+ * @property {CaptureResponseKind} kind - Specifies how to return the captured
112
+ * object. Some SDK methods that are designed to only return a particular type,
113
+ * such as {@link Device#frameAsBase64}, ignore this parameter. This parameter
114
+ * is also not sent in API requests, it is used internally by the SDK for
115
+ * constructing the appropriate response type.
116
+ * @property {CaptureResponseFormat} format - The image format to be returned.
117
+ *
118
+ * @typedef {object} CaptureOptions
119
+ */
120
+ export const DEFAULT_CAPTURE_OPT = {
121
+ rotate: 0,
122
+ base64: false,
123
+ format: null,
124
+ kind: 'objecturl'
125
+ }
126
+
127
+ /**
128
+ * @summary `data:` prefix for JPEG
129
+ * {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs|Data URLs}
130
+ * @constant
131
+ * @type {string}
132
+ */
133
+ export const DATAURL_JPEG = 'data:image/jpeg;base64,'
134
+
135
+ /**
136
+ * @summary `data:` prefix for PNG
137
+ * {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs|Data URLs}
138
+ * @constant
139
+ * @type {string}
140
+ */
141
+ export const DATAURL_PNG = 'data:image/png;base64,'
142
+
143
+ /**
144
+ * @summary For a given base64 string, convert it to the appropriate Data URL
145
+ * @constant
146
+ * @type {string}
147
+ */
148
+ export const base64ToDataURL = base64 => {
149
+ const magicBytes = atob(base64.slice(0, 6))
150
+ const byteArray = new Uint8Array(magicBytes.length)
151
+ for (let i = 0; i < magicBytes.length; i++) {
152
+ byteArray[i] = magicBytes.charCodeAt(i)
153
+ }
154
+
155
+ // Check for JPEG magic number (FF D8 FF)
156
+ if (byteArray[0] === 0xFF && byteArray[1] === 0xD8 && byteArray[2] === 0xFF) {
157
+ return DATAURL_JPEG + base64
158
+ }
159
+
160
+ // Check for PNG magic number (89 50 4E 47)
161
+ if (byteArray[0] === 0x89 && byteArray[1] === 0x50 && byteArray[2] === 0x4E && byteArray[3] === 0x47) {
162
+ return DATAURL_PNG + base64
163
+ }
164
+
165
+ throw new Error('Unknown image format provided in Base64 string')
166
+ }
167
+
168
+ /**
169
+ * An Async/Await version of
170
+ * {@link https://developer.mozilla.org/en-US/docs/Web/API/setTimeout|setTimeout}
171
+ * @private
172
+ */
173
+ export const timeout = millis => {
174
+ return new Promise(resolve => setTimeout(resolve, millis))
175
+ }
176
+
177
+ /**
178
+ * Convert a Blob to a Buffer
179
+ * @private
180
+ */
181
+ export const blobToBuffer = blob => {
182
+ return new Promise(resolve => {
183
+ const reader = new window.FileReader()
184
+ reader.onload = event => resolve(event.target.result)
185
+ reader.readAsArrayBuffer(blob)
186
+ })
187
+ }
188
+
189
+ /**
190
+ * Convert a Blob to a Base64 String
191
+ * @private
192
+ */
193
+ export const blobToBase64 = blob => {
194
+ return new Promise(resolve => {
195
+ const reader = new window.FileReader()
196
+ reader.onload = event => resolve(event.target.result.split(',')[1])
197
+ reader.readAsDataURL(blob)
198
+ })
199
+ }
200
+
201
+ /**
202
+ * Helper function for classes to easily add callback style support to their
203
+ * async/await methods.
204
+ * @private
205
+ */
206
+ export const asyncToCallback = (thisValue, asyncFn, callback, ...params) => {
207
+ (async () => {
208
+ try {
209
+ const result = await asyncFn.call(thisValue, ...(params.length ? params : []))
210
+ callback(null, result)
211
+ } catch (ex) {
212
+ callback(ex)
213
+ }
214
+ })()
215
+ }
216
+
217
+ /**
218
+ * Helper function to throw if an http response was a error.
219
+ * @private
220
+ */
221
+ export const checkResponse = async (response) => {
222
+ if (!response.ok) {
223
+ const error = await response.json()
224
+ const msg = error.message || error.summary || error.code || 'Unexpected error'
225
+ const ex = new Error(msg)
226
+ ex.code = error.code || 500
227
+ ex.summary = error.summary || 'Unexpected error'
228
+ throw ex
229
+ }
230
+ }
231
+
232
+ /**
233
+ * Helper function to convert a key/value map to a string, removing any items
234
+ * that are null or undefined (instead of converting them to a string which
235
+ * URLSearchParams().toString() will do.)
236
+ * @private
237
+ */
238
+ export const urlParamsToString = (params) => {
239
+ const filtered = {}
240
+ Object.keys(params).forEach(key => {
241
+ if (params[key] !== null && params[key] !== undefined) {
242
+ filtered[key] = params[key]
243
+ }
244
+ })
245
+ return new URLSearchParams(filtered).toString()
246
+ }
247
+
248
+ /**
249
+ * Allows passing in a string for the response type of capture methods
250
+ * @private
251
+ */
252
+ export const DEPRECATION_01 = true
253
+
254
+ /**
255
+ * Allows passing in a boolean to clear the screen for display methods
256
+ * @private
257
+ */
258
+ export const DEPRECATION_02 = true