feat(xo.getAllObjects): implement ndjson handler (#2500)
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { forEach } from 'lodash'
|
||||
|
||||
import { streamToBuffer } from '../utils'
|
||||
|
||||
// ===================================================================
|
||||
@@ -30,8 +32,18 @@ exportConfig.permission = 'admin'
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
export function getAllObjects ({ filter, limit }) {
|
||||
return this.getObjects({ filter, limit })
|
||||
function handleGetAllObjects (req, res, { filter, limit }) {
|
||||
forEach(this.getObjects({ filter, limit }), object => {
|
||||
res.write(JSON.stringify(object))
|
||||
res.write('\n')
|
||||
})
|
||||
res.end()
|
||||
}
|
||||
|
||||
export function getAllObjects ({ filter, limit, ndjson = false }) {
|
||||
return ndjson
|
||||
? this.registerHttpRequest(handleGetAllObjects, { filter, limit }).then($getFrom => ({ $getFrom }))
|
||||
: this.getObjects({ filter, limit })
|
||||
}
|
||||
|
||||
getAllObjects.permission = ''
|
||||
|
||||
@@ -22,6 +22,7 @@ import { lastly, reflect, tap } from 'promise-toolbox'
|
||||
import { forbiddenOperation, noHostsAvailable } from 'xo-common/api-errors'
|
||||
|
||||
import _ from '../intl'
|
||||
import fetch, { post } from '../fetch'
|
||||
import invoke from '../invoke'
|
||||
import logError from '../log-error'
|
||||
import renderXoItem from '../render-xo-item'
|
||||
@@ -29,7 +30,6 @@ import store from 'store'
|
||||
import { alert, chooseAction, confirm } from '../modal'
|
||||
import { error, info, success } from '../notification'
|
||||
import { getObject } from 'selectors'
|
||||
import { post } from '../fetch'
|
||||
import { noop, resolveId, resolveIds } from '../utils'
|
||||
import {
|
||||
connected,
|
||||
@@ -127,9 +127,33 @@ export const connectStore = store => {
|
||||
xo.on('authenticated', () => {
|
||||
store.dispatch(signedIn(xo.user))
|
||||
|
||||
_call('xo.getAllObjects').then(objects =>
|
||||
store.dispatch(updateObjects(objects))
|
||||
)
|
||||
_call('xo.getAllObjects', { ndjson: true })
|
||||
.then(({ $getFrom }) => fetch($getFrom))
|
||||
.then(response => response.text())
|
||||
.then(data => {
|
||||
const objects = Object.create(null)
|
||||
|
||||
const { length } = data
|
||||
let i = 0
|
||||
while (i < length) {
|
||||
let j = data.indexOf('\n', i)
|
||||
|
||||
// no final \n
|
||||
if (j === -1) {
|
||||
j = length
|
||||
}
|
||||
|
||||
// non empty line
|
||||
if (j !== i) {
|
||||
const object = JSON.parse(data.slice(i, j))
|
||||
objects[object.id] = object
|
||||
}
|
||||
|
||||
i = j + 1
|
||||
}
|
||||
|
||||
store.dispatch(updateObjects(objects))
|
||||
})
|
||||
})
|
||||
xo.on('notification', notification => {
|
||||
if (notification.method !== 'all') {
|
||||
|
||||
Reference in New Issue
Block a user