Skip to content

Commit 29ccd6b

Browse files
committed
refactor streaming
1 parent 4307999 commit 29ccd6b

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

src/adapters/controllers/get-models.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
import { Readable, Transform } from 'stream'
33
import getContent from './get-content'
44

5+
function isStream (models) {
6+
return (
7+
models?.length > 0 &&
8+
(models[0] instanceof Readable || models[0] instanceof Transform)
9+
)
10+
}
11+
512
/**
613
*
714
* @param {import("../use-cases/list-models").listModels} listModels
@@ -17,13 +24,7 @@ export default function getModelsFactory (listModels) {
1724
writable: httpRequest.res
1825
})
1926

20-
if (
21-
models?.length > 0 &&
22-
(models[0] instanceof Readable || models[0] instanceof Transform)
23-
) {
24-
httpRequest.stream = true
25-
return
26-
}
27+
if (isStream()) return
2728

2829
const { content, contentType } = getContent(httpRequest, models)
2930

src/adapters/controllers/http-adapter.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/**
1717
* @param {httpController} controller
1818
*/
19-
export default function buildCallback(controller) {
19+
export default function buildCallback (controller) {
2020
/**
2121
*/
2222
return async (req, res) => {
@@ -29,7 +29,7 @@ export default function buildCallback(controller) {
2929
path: req.path,
3030
res: res,
3131
headers: req.headers,
32-
log(func) {
32+
log (func) {
3333
console.info({
3434
function: func,
3535
ip: httpRequest.ip,
@@ -44,7 +44,6 @@ export default function buildCallback(controller) {
4444

4545
return controller(httpRequest)
4646
.then(httpResponse => {
47-
if (httpRequest.stream) return
4847
if (httpResponse.headers) {
4948
res.set(httpResponse.headers)
5049
}
@@ -55,4 +54,4 @@ export default function buildCallback(controller) {
5554
res.status(500).send({ error: 'An unkown error occurred.', e })
5655
)
5756
}
58-
}
57+
}

src/domain/datasource-factory.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,10 @@ const DsCoreExtensions = superclass =>
171171
*/
172172
async list (options) {
173173
try {
174-
if (options?.query.__count) return this.count()
175-
if (options?.query.__cached) return this.listSync(options.query)
174+
if (options?.query?.__count) return this.count()
175+
if (options?.query?.__cached) return this.listSync(options.query)
176176

177-
const opts = { ...options, streamRequested: options.writable }
177+
const opts = { ...options, streamRequested: options?.writable }
178178
const list = [await super.list(opts)].flat()
179179

180180
if (list.length < 1) return []

src/domain/use-cases/list-configs.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,27 @@ export default function listConfigsFactory ({
2525
const poolName =
2626
typeof query.poolName === 'string' ? query.poolName.toUpperCase() : null
2727

28+
const domain = models.getModelSpec(modelName)?.domain
29+
2830
const configTypes = {
2931
data: async () =>
3032
modelName && isMainThread
3133
? threadpools
3234
.getThreadPool(modelName)
3335
.runJob(listConfigs.name, query, modelName)
3436
: modelName && poolName
35-
? await datasources.getSharedDataSource(poolName).list()
37+
? await datasources.getSharedDataSource(poolName, domain).list()
3638
: modelName
37-
? await datasources.getSharedDataSource(modelName).list()
39+
? await datasources.getSharedDataSource(modelName, domain).list()
3840
: await Promise.all(
3941
datasources.listDataSources().map(async dsName => ({
4042
dsName,
41-
...(await datasources.getSharedDataSource(dsName).count())
43+
...(await datasources
44+
.getSharedDataSource(
45+
dsName,
46+
models.getModelSpec(modelName)?.domain
47+
)
48+
.count())
4249
}))
4350
),
4451

0 commit comments

Comments
 (0)