@@ -67,6 +67,8 @@ const DIST_FOLDER = join(process.cwd(), 'dist/browser');
6767// Set path fir IIIF viewer.
6868const IIIF_VIEWER = join ( process . cwd ( ) , 'dist/iiif' ) ;
6969
70+ const miradorHtml = join ( IIIF_VIEWER , '/mirador/index.html' ) ;
71+
7072const indexHtml = join ( DIST_FOLDER , 'index.html' ) ;
7173
7274const cookieParser = require ( 'cookie-parser' ) ;
@@ -88,8 +90,10 @@ const _window = domino.createWindow(indexHtml);
8890// The REST server base URL
8991const REST_BASE_URL = environment . rest . ssrBaseUrl || environment . rest . baseUrl ;
9092
93+ const IIIF_ALLOWED_ORIGINS = environment . rest . allowedOrigins || [ ] ;
94+
9195// Assign the DOM window and document objects to the global object
92- ( _window as any ) . screen = { deviceXDPI : 0 , logicalXDPI : 0 } ;
96+ ( _window as any ) . screen = { deviceXDPI : 0 , logicalXDPI : 0 } ;
9397( global as any ) . window = _window ;
9498( global as any ) . document = _window . document ;
9599( global as any ) . navigator = _window . navigator ;
@@ -211,6 +215,35 @@ export function app() {
211215 */
212216 router . use ( '/iiif' , express . static ( IIIF_VIEWER , { index : false } ) ) ;
213217
218+ /*
219+ * Adapt headers to allow embedding of IIIF viewer in authorized pages
220+ */
221+ server . get ( '/iiif/mirador/index.html' , ( req , res ) => {
222+ const referer = req . headers . referer ;
223+
224+ if ( referer && ! referer . startsWith ( '/' ) ) {
225+ try {
226+ const origin = new URL ( referer ) . origin ;
227+ if ( IIIF_ALLOWED_ORIGINS . includes ( origin ) ) {
228+ console . info ( 'Found allowed origin, setting headers for IIIF viewer' ) ;
229+ // CORS header
230+ res . setHeader ( 'Access-Control-Allow-Origin' , origin ) ;
231+ // CSP for iframe embedding
232+ res . setHeader ( 'Content-Security-Policy' , `frame-ancestors ${ origin } ;` ) ;
233+ console . info ( 'Headers have been set ' , res . getHeader ( 'Access-Control-Allow-Origin' ) , res . getHeader ( 'Content-Security-Policy' ) ) ;
234+ }
235+ } catch ( error ) {
236+ console . error ( 'An error occurred setting security headers in response:' , error ) ;
237+ }
238+ }
239+
240+ res . sendFile ( miradorHtml , ( err ) => {
241+ if ( err ) {
242+ res . status ( 500 ) . send ( 'Internal Server Error' ) ;
243+ }
244+ } ) ;
245+ } ) ;
246+
214247 /**
215248 * Checking server status
216249 */
@@ -283,6 +316,10 @@ function serverSideRender(req, res, next, sendToUser: boolean = true) {
283316 ] ,
284317 } )
285318 . then ( ( html ) => {
319+ if ( res . writableEnded || res . headersSent || res . finished ) {
320+ return ;
321+ }
322+
286323 if ( hasValue ( html ) ) {
287324 // Replace REST URL with UI URL
288325 if ( environment . ssr . replaceRestUrl && REST_BASE_URL !== environment . rest . baseUrl ) {
@@ -646,10 +683,10 @@ function start() {
646683 * The callback function to serve client health check requests
647684 */
648685function clientHealthCheck ( req , res ) {
649- const isServerHealthy = true ;
650- if ( isServerHealthy ) {
651- res . status ( 200 ) . json ( { status : 'UP' } ) ;
652- }
686+ const isServerHealthy = true ;
687+ if ( isServerHealthy ) {
688+ res . status ( 200 ) . json ( { status : 'UP' } ) ;
689+ }
653690}
654691
655692/*
@@ -667,6 +704,8 @@ function healthCheck(req, res) {
667704 } ) ;
668705 } ) ;
669706}
707+
708+
670709// Webpack will replace 'require' with '__webpack_require__'
671710// '__non_webpack_require__' is a proxy to Node 'require'
672711// The below code is to ensure that the server is run only when not requiring the bundle.
0 commit comments