-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
diff endpoints + added version in ServerInfo (#235)
- Loading branch information
Showing
15 changed files
with
320 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ type ServerInfo { | |
roles: [Role]! | ||
scopes: [Scope]! | ||
inviteOnly: Boolean | ||
version: String | ||
} | ||
|
||
""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
'use strict' | ||
const appRoot = require( 'app-root-path' ) | ||
const { contextMiddleware, validateScopes, authorizeResolver } = require( `${appRoot}/modules/shared` ) | ||
|
||
const { getStream } = require( '../services/streams' ) | ||
|
||
module.exports = { | ||
async validatePermissionsReadStream( streamId, req ) { | ||
const stream = await getStream( { streamId: streamId, userId: req.context.userId } ) | ||
|
||
if ( !stream ) { | ||
return { result: false, status: 404 } | ||
} | ||
|
||
if ( !stream.isPublic && req.context.auth === false ) { | ||
return { result: false, status: 401 } | ||
} | ||
|
||
if ( !stream.isPublic ) { | ||
try { | ||
await validateScopes( req.context.scopes, 'streams:read' ) | ||
} catch ( err ) { | ||
return { result: false, status: 401 } | ||
} | ||
|
||
try { | ||
await authorizeResolver( req.context.userId, streamId, 'stream:reviewer' ) | ||
} catch ( err ) { | ||
return { result: false, status: 401 } | ||
} | ||
} | ||
|
||
return { result: true, status: 200 } | ||
}, | ||
|
||
async validatePermissionsWriteStream( streamId, req ) { | ||
if ( !req.context || !req.context.auth ) { | ||
return { result: false, status: 401 } | ||
} | ||
|
||
try { | ||
await validateScopes( req.context.scopes, 'streams:write' ) | ||
} catch ( err ) { | ||
return { result: false, status: 401 } | ||
} | ||
|
||
try { | ||
await authorizeResolver( req.context.userId, streamId, 'stream:contributor' ) | ||
} catch ( err ) { | ||
return { result: false, status: 401 } | ||
} | ||
|
||
return { result: true, status: 200 } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
'use strict' | ||
const zlib = require( 'zlib' ) | ||
const Busboy = require( 'busboy' ) | ||
const debug = require( 'debug' ) | ||
const appRoot = require( 'app-root-path' ) | ||
const cors = require( 'cors' ) | ||
|
||
const { matomoMiddleware } = require( `${appRoot}/logging/matomoHelper` ) | ||
const { contextMiddleware, validateScopes, authorizeResolver } = require( `${appRoot}/modules/shared` ) | ||
const { validatePermissionsReadStream } = require( './authUtils' ) | ||
|
||
const { getObjectsStream } = require( '../services/objects' ) | ||
|
||
module.exports = ( app ) => { | ||
|
||
app.options( '/api/getobjects/:streamId', cors() ) | ||
app.post( '/api/getobjects/:streamId', cors(), contextMiddleware, matomoMiddleware, async ( req, res ) => { | ||
let hasStreamAccess = await validatePermissionsReadStream( req.params.streamId, req ) | ||
if ( !hasStreamAccess.result ) { | ||
return res.status( hasStreamAccess.status ).end() | ||
} | ||
|
||
let childrenList = JSON.parse( req.body.objects ) | ||
|
||
let simpleText = req.headers.accept === 'text/plain' | ||
|
||
let dbStream = await getObjectsStream( { streamId: req.params.streamId, objectIds: childrenList } ) | ||
|
||
let currentChunkSize = 0 | ||
let maxChunkSize = 50000 | ||
let chunk = simpleText ? '' : [ ] | ||
let isFirst = true | ||
|
||
|
||
res.writeHead( 200, { 'Content-Encoding': 'gzip', 'Content-Type': simpleText ? 'text/plain' : 'application/json' } ) | ||
|
||
const gzip = zlib.createGzip( ) | ||
|
||
if ( !simpleText ) gzip.write( '[' ) | ||
|
||
// helper func to flush the gzip buffer | ||
const writeBuffer = ( addTrailingComma ) => { | ||
// console.log( `writing buff ${currentChunkSize}` ) | ||
if ( simpleText ) { | ||
gzip.write( chunk ) | ||
} else { | ||
gzip.write( chunk.join( ',' ) ) | ||
if ( addTrailingComma ) { | ||
gzip.write( ',' ) | ||
} | ||
} | ||
gzip.flush( ) | ||
chunk = simpleText ? '' : [ ] | ||
} | ||
|
||
let k = 0 | ||
let requestDropped = false | ||
dbStream.on( 'data', row => { | ||
try { | ||
let data = JSON.stringify( row.data ) | ||
currentChunkSize += Buffer.byteLength( data, 'utf8' ) | ||
if ( simpleText ) { | ||
chunk += `${row.data.id}\t${data}\n` | ||
} else { | ||
chunk.push( data ) | ||
} | ||
if ( currentChunkSize >= maxChunkSize ) { | ||
currentChunkSize = 0 | ||
writeBuffer( true ) | ||
} | ||
k++ | ||
} catch ( e ) { | ||
requestDropped = true | ||
debug( 'speckle:error' )( `'Failed to find object, or object is corrupted.' ${req.params.objectId}` ) | ||
return | ||
} | ||
} ) | ||
|
||
dbStream.on( 'error', err => { | ||
debug( 'speckle:error' )( `Error in streaming object children for ${req.params.objectId}: ${err}` ) | ||
requestDropped = true | ||
return | ||
} ) | ||
|
||
dbStream.on( 'end', ( ) => { | ||
if ( currentChunkSize !== 0 ) { | ||
writeBuffer( false ) | ||
if ( !simpleText ) gzip.write( ']' ) | ||
} | ||
gzip.end( ) | ||
} ) | ||
|
||
// 🚬 | ||
gzip.pipe( res ) | ||
} ) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict' | ||
const zlib = require( 'zlib' ) | ||
const Busboy = require( 'busboy' ) | ||
const debug = require( 'debug' ) | ||
const appRoot = require( 'app-root-path' ) | ||
|
||
const { matomoMiddleware } = require( `${appRoot}/logging/matomoHelper` ) | ||
const { contextMiddleware } = require( `${appRoot}/modules/shared` ) | ||
const { validatePermissionsWriteStream } = require( './authUtils' ) | ||
|
||
const { hasObjects } = require( '../services/objects' ) | ||
|
||
module.exports = ( app ) => { | ||
app.post( '/api/diff/:streamId', contextMiddleware, matomoMiddleware, async ( req, res ) => { | ||
let hasStreamAccess = await validatePermissionsWriteStream( req.params.streamId, req ) | ||
if ( !hasStreamAccess.result ) { | ||
return res.status( hasStreamAccess.status ).end() | ||
} | ||
|
||
let objectList = JSON.parse( req.body.objects ) | ||
|
||
let response = await hasObjects( { streamId: req.params.streamId, objectIds: objectList } ) | ||
// console.log(response) | ||
res.writeHead( 200, { 'Content-Encoding': 'gzip', 'Content-Type': 'application/json' } ) | ||
const gzip = zlib.createGzip( ) | ||
gzip.write( JSON.stringify( response ) ) | ||
gzip.flush( ) | ||
gzip.end( ) | ||
gzip.pipe( res ) | ||
} ) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.