@capturebridge/sdk 0.12.1 → 0.14.2

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/ImageSource.js CHANGED
@@ -1,110 +1,110 @@
1
- import { blobToBase64, base64ToDataURL, checkResponse } from './Common.js'
2
-
3
- /**
4
- * ImageSource
5
- * @classdesc Class to handle loading various image formats
6
- *
7
- * @see {@link Probe}
8
- */
9
- export class ImageSource {
10
- source
11
- url
12
- imageData
13
- type
14
- /**
15
- * Instantiate an ImageSource
16
- * @constructor
17
- * @param {string} source - The source of the image data.
18
- * @param {string} [type] - The type of data in the image source, valid values
19
- * are 'url', 'base64', and 'dataurl'. If not provided 'base64' is the default.
20
- *
21
- * @example
22
- * const personPhoto = new ImageSource('/person/123.jpg', '123', 'url')
23
- */
24
- constructor (source, type = 'base64') {
25
- // TODO:
26
- // If the source is typeof object and/or type=object we should do some duck
27
- // typing and accept any of...
28
- // - The result of an ICAO check that contains a cropped image
29
- // - An Image tag (fetch the source)
30
- // - A Blob
31
- // - An Array Buffer
32
- // - An object that has a fullCapture method (call function in .data())
33
- this.type = type
34
- switch (this.type) {
35
- case 'url':
36
- this.url = source
37
- break
38
- case 'base64':
39
- this.imageData = source
40
- break
41
- case 'dataurl':
42
- this.imageData = source.split(',')[1]
43
- break
44
- case undefined:
45
- case null:
46
- default:
47
- throw new Error('Invalid image source type provided')
48
- }
49
- }
50
-
51
- /**
52
- * Fetch the image data and base64 encode it
53
- * @async
54
- * @example
55
- * const personPhoto = new ImageSource("/img/photo.jpg")
56
- * await personPhoto.data()
57
- * console.log(personPhoto.imageData)
58
- */
59
- async data () {
60
- if (!this.imageData && this.url) {
61
- const response = await fetch(this.url)
62
- await checkResponse(response)
63
- const blob = await response.blob()
64
- this.imageData = await blobToBase64(blob)
65
- }
66
- return this.imageData
67
- }
68
-
69
- /**
70
- * Create an <img> element, wait for it to load and return it
71
- * @async
72
- * @example
73
- * // Load the image and draw it on a Canvas
74
- * const img = await imgSrc.toImage()
75
- * const canvas = document.createElement('canvas')
76
- * const ctx = canvas.getContext('2d')
77
- * canvas.width = img.width
78
- * canvas.height = img.height
79
- * ctx.drawImage(img, canvas.width, canvas.height, img.width, img.height)
80
- */
81
- async toImage () {
82
- const img = new window.Image()
83
- const promise = new Promise((resolve, reject) => {
84
- img.onload = (data) => resolve(img)
85
- img.onerror = (err) => reject(err)
86
- })
87
- switch (this.type) {
88
- case 'url':
89
- img.src = this.url
90
- break
91
- case 'base64':
92
- img.src = base64ToDataURL(this.imageData)
93
- break
94
- case 'dataurl':
95
- img.src = this.source
96
- break
97
- case undefined:
98
- case null:
99
- default:
100
- throw new Error('Invalid image source type provided')
101
- }
102
- return promise
103
- }
104
-
105
- toJSON () {
106
- return this.imageData
107
- }
108
- }
109
-
110
- export default ImageSource
1
+ import { blobToBase64, base64ToDataURL, checkResponse } from './Common.js'
2
+
3
+ /**
4
+ * ImageSource
5
+ * @classdesc Class to handle loading various image formats
6
+ *
7
+ * @see {@link Probe}
8
+ */
9
+ export class ImageSource {
10
+ source
11
+ url
12
+ imageData
13
+ type
14
+ /**
15
+ * Instantiate an ImageSource
16
+ * @constructor
17
+ * @param {string} source - The source of the image data.
18
+ * @param {string} [type] - The type of data in the image source, valid values
19
+ * are 'url', 'base64', and 'dataurl'. If not provided 'base64' is the default.
20
+ *
21
+ * @example
22
+ * const personPhoto = new ImageSource('/person/123.jpg', '123', 'url')
23
+ */
24
+ constructor (source, type = 'base64') {
25
+ // TODO:
26
+ // If the source is typeof object and/or type=object we should do some duck
27
+ // typing and accept any of...
28
+ // - The result of an ICAO check that contains a cropped image
29
+ // - An Image tag (fetch the source)
30
+ // - A Blob
31
+ // - An Array Buffer
32
+ // - An object that has a fullCapture method (call function in .data())
33
+ this.type = type
34
+ switch (this.type) {
35
+ case 'url':
36
+ this.url = source
37
+ break
38
+ case 'base64':
39
+ this.imageData = source
40
+ break
41
+ case 'dataurl':
42
+ this.imageData = source.split(',')[1]
43
+ break
44
+ case undefined:
45
+ case null:
46
+ default:
47
+ throw new Error('Invalid image source type provided')
48
+ }
49
+ }
50
+
51
+ /**
52
+ * Fetch the image data and base64 encode it
53
+ * @async
54
+ * @example
55
+ * const personPhoto = new ImageSource("/img/photo.jpg")
56
+ * await personPhoto.data()
57
+ * console.log(personPhoto.imageData)
58
+ */
59
+ async data () {
60
+ if (!this.imageData && this.url) {
61
+ const response = await fetch(this.url)
62
+ await checkResponse(response)
63
+ const blob = await response.blob()
64
+ this.imageData = await blobToBase64(blob)
65
+ }
66
+ return this.imageData
67
+ }
68
+
69
+ /**
70
+ * Create an <img> element, wait for it to load and return it
71
+ * @async
72
+ * @example
73
+ * // Load the image and draw it on a Canvas
74
+ * const img = await imgSrc.toImage()
75
+ * const canvas = document.createElement('canvas')
76
+ * const ctx = canvas.getContext('2d')
77
+ * canvas.width = img.width
78
+ * canvas.height = img.height
79
+ * ctx.drawImage(img, canvas.width, canvas.height, img.width, img.height)
80
+ */
81
+ async toImage () {
82
+ const img = new window.Image()
83
+ const promise = new Promise((resolve, reject) => {
84
+ img.onload = (data) => resolve(img)
85
+ img.onerror = (err) => reject(err)
86
+ })
87
+ switch (this.type) {
88
+ case 'url':
89
+ img.src = this.url
90
+ break
91
+ case 'base64':
92
+ img.src = base64ToDataURL(this.imageData)
93
+ break
94
+ case 'dataurl':
95
+ img.src = this.source
96
+ break
97
+ case undefined:
98
+ case null:
99
+ default:
100
+ throw new Error('Invalid image source type provided')
101
+ }
102
+ return promise
103
+ }
104
+
105
+ toJSON () {
106
+ return this.imageData
107
+ }
108
+ }
109
+
110
+ export default ImageSource
package/Logs.js CHANGED
@@ -1,129 +1,129 @@
1
- import { BASE_URL } from './Common.js'
2
-
3
- export const TRACE = 10
4
- export const DEBUG = 20
5
- export const INFO = 30
6
- export const WARN = 40
7
- export const ERROR = 50
8
- export const FATAL = 60
9
-
10
- /**
11
- * @constant
12
- * @type {Object.<string, number>}
13
- *
14
- */
15
- export const LOG_LEVELS = {
16
- trace: TRACE,
17
- debug: DEBUG,
18
- info: INFO,
19
- warn: WARN,
20
- error: ERROR,
21
- fatal: FATAL
22
- }
23
-
24
- /**
25
- * @classdesc Plugins write
26
- * {@link https://github.com/trentm/node-bunyan?tab=readme-ov-file#log-record-fields|Bunyan}
27
- * formated logs to separate log files. This class provides
28
- * utilities for viewing and following those logs as they're written.
29
- */
30
- export class Logs {
31
- baseUrl
32
- #reader
33
- #controller
34
- /**
35
- * Instantiate a Log Object
36
- * @constructor
37
- * @param {string} [baseURL] - Protocol, domain, and port for the service.
38
- */
39
- constructor (baseUrl = BASE_URL) {
40
- this.baseUrl = baseUrl
41
- }
42
-
43
- /**
44
- * End the log stream and stop following logs.
45
- */
46
- end () {
47
- this.#reader?.cancel()
48
- this.#controller?.abort()
49
- }
50
-
51
- /**
52
- * Add a handler for following logs. Note that each invocation of this method
53
- * opens a connection to the server and holds it open. Web browsers limit the
54
- * number of connections to a single domain. Avoid using this method more than
55
- * once per client.
56
- * @param {string[]} plugins - Follow logs for the provided Plugin IDs.
57
- * @param {string|number} level - Log Level.
58
- * @param {function} callback - Invoked with two arguments. The first argument
59
- * is an Error object (if an error occurred) or null. The second argument is
60
- * an log object.
61
- */
62
- follow (plugins, level, callback) {
63
- let pluginList
64
- if (typeof plugins === 'object' && plugins.length > 0) {
65
- pluginList = plugins.join(',')
66
- } else {
67
- throw new Error('Invalid value provided for plugins argument')
68
- }
69
-
70
- const logLevel = (typeof level === 'number') ? level : LOG_LEVELS[level]
71
-
72
- if (!logLevel) {
73
- throw new Error('Invalid value provided for log level argument')
74
- }
75
-
76
- if (typeof callback !== 'function') {
77
- throw new Error('Invalid value provided for callback argument')
78
- }
79
-
80
- const url = `${this.baseUrl}/plugin/logs/follow?plugins=${pluginList}&level=${logLevel}`
81
-
82
- this.#controller = new AbortController()
83
- const signal = this.#controller.signal
84
-
85
- fetch(url, { signal })
86
- .then(({ body }) => {
87
- let buffer = ''
88
- if (!body) {
89
- return callback(new Error('No response body'))
90
- }
91
- const readData = data => {
92
- if (!data.done) {
93
- callback(null, data.value)
94
- this.#reader.read().then(readData).catch(e => callback(e))
95
- }
96
- }
97
- this.#reader = body
98
- .pipeThrough(new TextDecoderStream())
99
- .pipeThrough(new TransformStream({
100
- transform (chunk, controller) {
101
- buffer += chunk
102
- const parts = buffer.split('\n')
103
- parts.slice(0, -1).forEach(part => controller.enqueue(part))
104
- buffer = parts[parts.length - 1]
105
- },
106
- flush (controller) {
107
- if (buffer) {
108
- controller.enqueue(buffer)
109
- }
110
- }
111
- }))
112
- .pipeThrough(new TransformStream({
113
- transform (chunk, controller) {
114
- controller.enqueue(JSON.parse(chunk))
115
- }
116
- }))
117
- .getReader()
118
- this.#reader
119
- .read()
120
- .then(readData)
121
- .catch(e => callback(e))
122
- }).catch(e => {
123
- // Don't emit an error when aborting the fetch operation
124
- if (e.name !== 'AbortError') {
125
- callback(e)
126
- }
127
- })
128
- }
129
- }
1
+ import { BASE_URL } from './Common.js'
2
+
3
+ export const TRACE = 10
4
+ export const DEBUG = 20
5
+ export const INFO = 30
6
+ export const WARN = 40
7
+ export const ERROR = 50
8
+ export const FATAL = 60
9
+
10
+ /**
11
+ * @constant
12
+ * @type {Object.<string, number>}
13
+ *
14
+ */
15
+ export const LOG_LEVELS = {
16
+ trace: TRACE,
17
+ debug: DEBUG,
18
+ info: INFO,
19
+ warn: WARN,
20
+ error: ERROR,
21
+ fatal: FATAL
22
+ }
23
+
24
+ /**
25
+ * @classdesc Plugins write
26
+ * {@link https://github.com/trentm/node-bunyan?tab=readme-ov-file#log-record-fields|Bunyan}
27
+ * formated logs to separate log files. This class provides
28
+ * utilities for viewing and following those logs as they're written.
29
+ */
30
+ export class Logs {
31
+ baseUrl
32
+ #reader
33
+ #controller
34
+ /**
35
+ * Instantiate a Log Object
36
+ * @constructor
37
+ * @param {string} [baseURL] - Protocol, domain, and port for the service.
38
+ */
39
+ constructor (baseUrl = BASE_URL) {
40
+ this.baseUrl = baseUrl
41
+ }
42
+
43
+ /**
44
+ * End the log stream and stop following logs.
45
+ */
46
+ end () {
47
+ this.#reader?.cancel()
48
+ this.#controller?.abort()
49
+ }
50
+
51
+ /**
52
+ * Add a handler for following logs. Note that each invocation of this method
53
+ * opens a connection to the server and holds it open. Web browsers limit the
54
+ * number of connections to a single domain. Avoid using this method more than
55
+ * once per client.
56
+ * @param {string[]} plugins - Follow logs for the provided Plugin IDs.
57
+ * @param {string|number} level - Log Level.
58
+ * @param {function} callback - Invoked with two arguments. The first argument
59
+ * is an Error object (if an error occurred) or null. The second argument is
60
+ * an log object.
61
+ */
62
+ follow (plugins, level, callback) {
63
+ let pluginList
64
+ if (typeof plugins === 'object' && plugins.length > 0) {
65
+ pluginList = plugins.join(',')
66
+ } else {
67
+ throw new Error('Invalid value provided for plugins argument')
68
+ }
69
+
70
+ const logLevel = (typeof level === 'number') ? level : LOG_LEVELS[level]
71
+
72
+ if (!logLevel) {
73
+ throw new Error('Invalid value provided for log level argument')
74
+ }
75
+
76
+ if (typeof callback !== 'function') {
77
+ throw new Error('Invalid value provided for callback argument')
78
+ }
79
+
80
+ const url = `${this.baseUrl}/plugin/logs/follow?plugins=${pluginList}&level=${logLevel}`
81
+
82
+ this.#controller = new AbortController()
83
+ const signal = this.#controller.signal
84
+
85
+ fetch(url, { signal })
86
+ .then(({ body }) => {
87
+ let buffer = ''
88
+ if (!body) {
89
+ return callback(new Error('No response body'))
90
+ }
91
+ const readData = data => {
92
+ if (!data.done) {
93
+ callback(null, data.value)
94
+ this.#reader.read().then(readData).catch(e => callback(e))
95
+ }
96
+ }
97
+ this.#reader = body
98
+ .pipeThrough(new TextDecoderStream())
99
+ .pipeThrough(new TransformStream({
100
+ transform (chunk, controller) {
101
+ buffer += chunk
102
+ const parts = buffer.split('\n')
103
+ parts.slice(0, -1).forEach(part => controller.enqueue(part))
104
+ buffer = parts[parts.length - 1]
105
+ },
106
+ flush (controller) {
107
+ if (buffer) {
108
+ controller.enqueue(buffer)
109
+ }
110
+ }
111
+ }))
112
+ .pipeThrough(new TransformStream({
113
+ transform (chunk, controller) {
114
+ controller.enqueue(JSON.parse(chunk))
115
+ }
116
+ }))
117
+ .getReader()
118
+ this.#reader
119
+ .read()
120
+ .then(readData)
121
+ .catch(e => callback(e))
122
+ }).catch(e => {
123
+ // Don't emit an error when aborting the fetch operation
124
+ if (e.name !== 'AbortError') {
125
+ callback(e)
126
+ }
127
+ })
128
+ }
129
+ }
package/MockCamera.js CHANGED
@@ -1,17 +1,17 @@
1
- import Camera from './Camera.js'
2
- import { BASE_URL } from './Common.js'
3
-
4
- /**
5
- * MockCamera
6
- * @classdesc Convenience class for instantiating a Mock Camera Object.
7
- * Inherits all methods on the {@link Camera} class.
8
- * @extends Camera
9
- * @see {@link Camera}
10
- */
11
- class MockCamera extends Camera {
12
- constructor (baseUrl = BASE_URL) {
13
- super('capture_camera_mock', baseUrl)
14
- }
15
- }
16
-
17
- export default MockCamera
1
+ import Camera from './Camera.js'
2
+ import { BASE_URL } from './Common.js'
3
+
4
+ /**
5
+ * MockCamera
6
+ * @classdesc Convenience class for instantiating a Mock Camera Object.
7
+ * Inherits all methods on the {@link Camera} class.
8
+ * @extends Camera
9
+ * @see {@link Camera}
10
+ */
11
+ class MockCamera extends Camera {
12
+ constructor (baseUrl = BASE_URL) {
13
+ super('capture_camera_mock', baseUrl)
14
+ }
15
+ }
16
+
17
+ export default MockCamera