44 * @packageDocumentation
55 */
66
7- import { type SkipService , ServiceInstance } from "@skipruntime/core" ;
7+ import { type SkipService } from "@skipruntime/core" ;
88import { controlService , streamingService } from "./rest.js" ;
9+ import type { Express , Request , Response , NextFunction } from "express" ;
10+ import express from "express" ;
911
1012/**
1113 * A running Skip server.
@@ -90,6 +92,7 @@ export type SkipServer = {
9092 * @param options.control_port - Port on which control service will listen.
9193 * @param options.streaming_port - Port on which streaming service will listen.
9294 * @param options.platform - Skip runtime platform to be used to run the service: either `wasm` (the default) or `native`.
95+ * @param options.no_cors - Disable CORS for the streaming endpoint.
9396 * @returns Object to manage the running server.
9497 */
9598export async function runService (
@@ -98,17 +101,18 @@ export async function runService(
98101 streaming_port : number ;
99102 control_port : number ;
100103 platform ?: "wasm" | "native" ;
104+ no_cors ?: boolean ;
101105 } = {
102106 streaming_port : 8080 ,
103107 control_port : 8081 ,
104108 platform : "wasm" ,
109+ no_cors : false ,
105110 } ,
106111) : Promise < SkipServer > {
107- let instance : ServiceInstance ;
112+ let runtime ;
108113 if ( options . platform == "native" ) {
109114 try {
110- const runtime = await import ( "@skipruntime/native" ) ;
111- instance = await runtime . initService ( service ) ;
115+ runtime = await import ( "@skipruntime/native" ) ;
112116 } catch ( e ) {
113117 console . error (
114118 'Error loading Skip runtime for specified "native" platform: ' ,
@@ -117,15 +121,15 @@ export async function runService(
117121 }
118122 } else {
119123 try {
120- const runtime = await import ( "@skipruntime/wasm" ) ;
121- instance = await runtime . initService ( service ) ;
124+ runtime = await import ( "@skipruntime/wasm" ) ;
122125 } catch ( e ) {
123126 console . error (
124127 'Error loading Skip runtime for specified "wasm" platform: ' ,
125128 ) ;
126129 throw e ;
127130 }
128131 }
132+ const instance = await runtime . initService ( service ) ;
129133 const controlHttpServer = controlService ( instance ) . listen (
130134 options . control_port ,
131135 ( ) => {
@@ -134,7 +138,13 @@ export async function runService(
134138 ) ;
135139 } ,
136140 ) ;
137- const streamingHttpServer = streamingService ( instance ) . listen (
141+ const wrapMiddleware = ( app : Express ) => {
142+ if ( options . no_cors ) {
143+ return express ( ) . use ( no_cors ) . use ( app ) ;
144+ }
145+ return app ;
146+ } ;
147+ const streamingHttpServer = wrapMiddleware ( streamingService ( instance ) ) . listen (
138148 options . streaming_port ,
139149 ( ) => {
140150 console . log (
@@ -151,3 +161,16 @@ export async function runService(
151161 } ,
152162 } ;
153163}
164+
165+ function no_cors ( req : Request , res : Response , next : NextFunction ) {
166+ res . header ( "Access-Control-Allow-Credentials" , "true" ) ;
167+ res . header ( "Access-Control-Allow-Methods" , "GET,OPTIONS" ) ;
168+ res . header ( "Access-Control-Allow-Origin" , "*" ) ;
169+ if ( req . method . toUpperCase ( ) == "OPTIONS" ) {
170+ res . statusCode = 204 ;
171+ res . setHeader ( "Content-Length" , "0" ) ;
172+ res . end ( ) ;
173+ } else {
174+ next ( ) ;
175+ }
176+ }
0 commit comments