fleetmap-reports 2.0.207 → 2.0.209

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fleetmap-reports",
3
- "version": "2.0.207",
3
+ "version": "2.0.209",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -23,6 +23,7 @@
23
23
  "jspdf": "^2.5.1",
24
24
  "jspdf-autotable": "3.8.1",
25
25
  "moment": "2.30.1",
26
+ "piscina": "^4.9.2",
26
27
  "stream-chain": "^3.4.0",
27
28
  "stream-json": "^1.9.1",
28
29
  "traccar-api": "^1.0.5",
@@ -1,10 +1,8 @@
1
1
  const automaticReports = require('../automaticReports')
2
2
  const { convertFromUTC, isClientSide, convertFromLocal } = require('./utils')
3
- const { parser } = require('stream-json')
4
- const { streamArray } = require('stream-json/streamers/StreamArray')
5
- const { chain } = require('stream-chain')
6
- const { pick } = require('stream-json/filters/Pick')
7
-
3
+ const path = require('path')
4
+ const Piscina = require('piscina')
5
+ const piscina = new Piscina()
8
6
  async function getRoute (traccar, from, to, devices) {
9
7
  const devicesToSlice = devices.slice()
10
8
 
@@ -96,28 +94,15 @@ async function getItems (traccar, u, _chunk, counter, devicesPerRequest, ignoreP
96
94
  }
97
95
  }
98
96
 
99
- async function streamJson (traccar, u) {
100
- const now = new Date()
101
- console.log(traccar.axios.defaults.baseURL, u)
102
- const response = await traccar.axios.get(u, {
103
- responseType: 'stream',
104
- jar: traccar.cookieJar,
105
- withCredentials: true
106
- })
107
-
108
- const result = { route: [] }
109
- const requestId = response.headers['x-amzn-requestid']
110
- console.log(u, 'x-amzn-requestid', requestId, 'took', new Date() - now, 'ms', 'streaming...')
111
- return new Promise((resolve, reject) => {
112
- const pipeline = chain([
113
- response.data,
114
- parser(),
115
- pick({ filter: 'route' }),
116
- streamArray() // Stream each item in the array
117
- ])
118
- pipeline.on('data', ({ value }) => result.route.push(value))
119
- pipeline.on('end', () => resolve(result))
120
- pipeline.on('error', reject)
97
+ function streamJson (traccar, u) {
98
+ const cookie = traccar.axios.defaults.headers?.cookie || traccar.axios.defaults.headers?.common?.cookie
99
+ return piscina.run({
100
+ url: traccar.axios.defaults.baseURL + u,
101
+ cookie
102
+ },
103
+ {
104
+ filename: path.resolve(__dirname, './traccar.worker.js'),
105
+ maxThreads: require('os').cpus().length
121
106
  })
122
107
  }
123
108
 
@@ -0,0 +1,31 @@
1
+ const { chain } = require('stream-chain')
2
+ const { parser } = require('stream-json')
3
+ const { pick } = require('stream-json/filters/Pick')
4
+ const { streamArray } = require('stream-json/streamers/StreamArray')
5
+ const { Readable } = require('stream')
6
+
7
+ module.exports = async ({ url, cookie }) => {
8
+ const now = new Date()
9
+ console.log(url)
10
+ const response = await fetch(url, {
11
+ headers: { cookie }
12
+ })
13
+ if (!response.ok) {
14
+ throw new Error(`Fetch failed: ${response.status} ${response.statusText}`)
15
+ }
16
+ console.log(url, 'took', new Date() - now, 'ms', 'streaming...')
17
+ const nodeStream = Readable.fromWeb(response.body)
18
+
19
+ const result = { route: [] }
20
+ return new Promise((resolve, reject) => {
21
+ const pipeline = chain([
22
+ nodeStream,
23
+ parser(),
24
+ pick({ filter: 'route' }),
25
+ streamArray() // Stream each item in the array
26
+ ])
27
+ pipeline.on('data', ({ value }) => result.route.push(value))
28
+ pipeline.on('end', () => resolve(result))
29
+ pipeline.on('error', reject)
30
+ })
31
+ }