22
33/**
44 * @fileOverview
5- * TUS Protocol 1.0.0 Server Implementation.
5+ * TUS Protocol Server Implementation.
66 *
77 * @author Ben Stahl <[email protected] > 88 */
9-
109const http = require ( 'http' ) ;
11- const DataStore = require ( './stores/DataStore' ) ;
1210
13- const OptionsHandler = require ( './handlers/OptionsHandler' ) ;
14- const PostHandler = require ( './handlers/PostHandler' ) ;
11+ const DataStore = require ( './stores/DataStore' ) ;
1512const HeadHandler = require ( './handlers/HeadHandler' ) ;
13+ const OptionsHandler = require ( './handlers/OptionsHandler' ) ;
1614const PatchHandler = require ( './handlers/PatchHandler' ) ;
17- const TUS_RESUMABLE = require ( './constants' ) . TUS_RESUMABLE ;
15+ const PostHandler = require ( './handlers/PostHandler' ) ;
1816const RequestValidator = require ( './validators/RequestValidator' ) ;
17+
1918const EXPOSED_HEADERS = require ( './constants' ) . EXPOSED_HEADERS ;
19+ const TUS_RESUMABLE = require ( './constants' ) . TUS_RESUMABLE ;
2020
2121class TusServer {
2222
2323 constructor ( ) {
24+ // Any handlers assigned to this object with the method as the key
25+ // will be used to repond to those requests. They get set/re-set
26+ // when a datastore is assigned to the server.
2427 this . handlers = { } ;
2528 }
2629
@@ -33,7 +36,8 @@ class TusServer {
3336 }
3437
3538 /**
36- * Ensure store is a DataStore and add file create API handler
39+ * Assign a datastore to this server, and re-set the handlers to use that
40+ * data store when doing file operations.
3741 *
3842 * @param {DataStore } store Store for uploaded files
3943 */
@@ -45,7 +49,13 @@ class TusServer {
4549 this . _datastore = store ;
4650
4751 this . handlers = {
48- GET : { } , // Alow implemenation to have full handler control
52+ // GET handlers should be written in the implementations
53+ // eg.
54+ // const server = new tus.Server();
55+ // server.get('/', (req, res) => { ... });
56+ GET : { } ,
57+
58+ // These methods are handled under the tus protocol
4959 HEAD : new HeadHandler ( store ) ,
5060 OPTIONS : new OptionsHandler ( store ) ,
5161 PATCH : new PatchHandler ( store ) ,
@@ -62,32 +72,40 @@ class TusServer {
6272 * @param {Function } callback Request listener
6373 */
6474 get ( path , callback ) {
75+
76+ // Add this handler callback to the GET method handler list.
6577 this . handlers . GET [ path ] = callback ;
6678 }
6779
6880 /**
69- * Main server requestListener, the function which is
70- * automatically added to the 'request' event.
81+ * Main server requestListener, invoked on every 'request' event.
7182 *
7283 * @param {object } req http.incomingMessage
7384 * @param {object } res http.ServerResponse
7485 * @return {ServerResponse }
7586 */
7687 handle ( req , res ) {
7788 console . info ( `[TusServer] handle: ${ req . method } ${ req . url } ` ) ;
89+
7890 // Allow overriding the HTTP method. The reason for this is
7991 // that some libraries/environments to not support PATCH and
8092 // DELETE requests, e.g. Flash in a browser and parts of Java
8193 if ( req . headers [ 'x-http-method-override' ] ) {
8294 req . method = req . headers [ 'x-http-method-override' ] . toUpperCase ( ) ;
8395 }
8496
97+
8598 if ( req . method === 'GET' ) {
99+
100+ // Check if this url has been added to allow GET requests, with an
101+ // appropriate callback to handle the request
86102 if ( ! ( req . url in this . handlers . GET ) ) {
87103 res . writeHead ( 404 , { } ) ;
88104 res . write ( 'Not found\n' ) ;
89105 return res . end ( ) ;
90106 }
107+
108+ // invoke the callback
91109 return this . handlers . GET [ req . url ] ( req , res ) ;
92110 }
93111
@@ -100,7 +118,7 @@ class TusServer {
100118 return res . end ( 'Tus-Resumable Required\n' ) ;
101119 }
102120
103- // Validate all other headers
121+ // Validate all required headers to adhere to the tus protocol
104122 const invalid_headers = [ ] ;
105123 for ( const header_name in req . headers ) {
106124 if ( req . method === 'OPTIONS' ) {
@@ -114,17 +132,18 @@ class TusServer {
114132 }
115133
116134 if ( invalid_headers . length > 0 ) {
135+ // The request was not configured to the tus protocol
117136 res . writeHead ( 412 , { } , 'Precondition Failed' ) ;
118137 return res . end ( `Invalid ${ invalid_headers . join ( ' ' ) } \n` ) ;
119138 }
120139
140+ // Enable CORS
141+ res . setHeader ( 'Access-Control-Expose-Headers' , EXPOSED_HEADERS ) ;
121142 if ( req . headers . origin ) {
122143 res . setHeader ( 'Access-Control-Allow-Origin' , req . headers . origin ) ;
123144 }
124145
125- res . setHeader ( 'Access-Control-Expose-Headers' , EXPOSED_HEADERS ) ;
126-
127- // Handle POST, HEAD, PATCH, OPTIONS requests
146+ // Invoke the handler for the method requested
128147 if ( this . handlers [ req . method ] ) {
129148 return this . handlers [ req . method ] . send ( req , res ) ;
130149 }
0 commit comments