1 | var extend = require('deep-extend'); |
||
2 | var curl = require('curl'); // 0.1.4 |
||
3 | var queryString = require('query-string'); // 4.3.4 |
||
4 | var fm = require('front-matter'); |
||
5 | var fs = require('fs-extra'); |
||
6 | var glob = require('glob'); |
||
7 | var path = require('path'); |
||
8 | var through = require('through2'); |
||
9 | var slash = require('slash'); |
||
10 | var jsonfile = require('jsonfile'); |
||
11 | |||
12 | /** |
||
13 | * Patternlibrary 'gulp-connect'/'gulp-browser-sync' middleware hook |
||
14 | * |
||
15 | * @package Patternlibrary |
||
16 | * @namespace Patternlibrary |
||
17 | * @author Björn Bartels <[email protected]> |
||
18 | */ |
||
19 | class Patternlibrary_Middleware { |
||
20 | |||
21 | /** |
||
22 | * Initializes an instance of Patternlibrary middleware hook. |
||
23 | * @constructor |
||
24 | * @param {object} options - Configuration options to use. |
||
25 | * @param {Patternlibrary} patternlibrary - main patternlibrary object reference. |
||
26 | */ |
||
27 | constructor ( options, patternlibrary ) { |
||
28 | |||
29 | this.options = extend(this.defaults, options); |
||
30 | |||
31 | if (typeof patternlibrary == 'object') { |
||
32 | this.$PL = patternlibrary; |
||
33 | } |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * bind Patternlibrary object |
||
38 | * |
||
39 | * @param Patternlibrary the Patternlibrary |
||
40 | * @return self |
||
41 | */ |
||
42 | bind ( patternlibrary ) { |
||
43 | if ( (typeof patternlibrary.patternlibrary_version == 'undefined') ) throw 'invalid patternlibrary object to bind'; |
||
0 ignored issues
–
show
|
|||
44 | |||
45 | if (typeof patternlibrary == 'object') { |
||
46 | this.$PL = patternlibrary; |
||
47 | } |
||
48 | |||
49 | return (this); |
||
50 | } |
||
51 | |||
52 | getRoute ( httpRequest ) { |
||
53 | return ( httpRequest._parsedUrl.pathname.toLowerCase().replace(this.options.basepath, '') ) |
||
54 | } |
||
55 | |||
56 | getMethod ( httpRequest ) { |
||
57 | return ( httpRequest.method.toLowerCase() ) |
||
58 | } |
||
59 | |||
60 | getPatterns () { |
||
61 | return this.$PL.data; |
||
62 | |||
63 | } |
||
64 | |||
65 | getTree () { |
||
66 | return this.$PL.Supercollider.tree; |
||
67 | |||
68 | } |
||
69 | |||
70 | getSearch ( callback ) { |
||
71 | var $PL = this.$PL; |
||
72 | var searchFile = $PL.options.dest+'search.json'; |
||
73 | $PL.Supercollider.buildSearch(searchFile, callback); |
||
74 | } |
||
75 | |||
76 | getHook () { |
||
77 | return ( this.hook.bind(this) ); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * 'gulp-connect'/'gulp-browser-sync' middleware hook |
||
82 | * |
||
83 | * @param Request httpRequest |
||
84 | * @param Response httpResponse |
||
85 | * @param function next |
||
86 | */ |
||
87 | hook (httpRequest, httpResponse, next) { |
||
88 | |||
89 | var $PL = this.$PL, |
||
90 | basePath = this.options.basepath, // '/pl', |
||
0 ignored issues
–
show
|
|||
91 | method = this.getMethod(httpRequest), |
||
92 | route = this.getRoute(httpRequest) |
||
93 | |||
94 | ; |
||
95 | |||
96 | // allow cross-domain ajax/iframe access |
||
97 | httpResponse.setHeader('Access-Control-Allow-Origin', '*'); |
||
98 | |||
99 | switch (method) { |
||
100 | |||
101 | case 'delete' : |
||
102 | case 'put' : |
||
103 | case 'update' : |
||
104 | case 'post' : |
||
105 | |||
106 | switch (route) { |
||
107 | case "some_route" : |
||
108 | // this is to come ... ;) |
||
109 | console.log('some route...:', route); |
||
0 ignored issues
–
show
|
|||
110 | break; |
||
111 | default: |
||
112 | // this is to come ... ;) |
||
113 | console.log('method: ', httpRequest.method.toLowerCase()); |
||
114 | console.log('request: ', httpRequest.url); |
||
115 | next(); |
||
116 | break; |
||
117 | } |
||
118 | |||
119 | break; |
||
120 | |||
121 | case 'get' : |
||
122 | default : |
||
123 | |||
124 | switch (route) { |
||
125 | case '/patterns.json' : |
||
126 | httpResponse.write( ''+JSON.stringify(this.getPatterns())+'' ); |
||
127 | httpResponse.end(); |
||
128 | break; |
||
129 | case '/patternlibrary.json' : |
||
130 | httpResponse.write( ''+JSON.stringify({ |
||
131 | patterns: this.getPatterns(), |
||
132 | tree : this.getTree() |
||
133 | })+'' ); |
||
134 | httpResponse.end(); |
||
135 | break; |
||
136 | |||
137 | case '/search' : |
||
138 | case '/search.json' : |
||
139 | var searchFile = $PL.options.dest+'search.json'; |
||
140 | this.getSearch(function() { |
||
141 | jsonfile.readFile(searchFile, function(err, obj) { |
||
142 | httpResponse.write( ''+JSON.stringify(obj)+'' ); |
||
143 | httpResponse.end(); |
||
144 | }); |
||
145 | }); |
||
146 | break; |
||
147 | |||
148 | case '/proxy' : |
||
149 | case '/emulate' : |
||
150 | case '/emulate.php' : |
||
151 | var query = queryString.parse(httpRequest._parsedUrl.search); |
||
152 | var EmulatorLinkScript = '<script type="text/javascript">' |
||
153 | +'document.domain=document.domain.replace(\'www.\',\'\');' |
||
154 | +'parent.document.getElementById(\'UrlTxt\').innerHTML=\'http://my-application.net/en/user/login\';' |
||
155 | +'function bye() {' |
||
156 | +'if(parent.EmulatorStarted == false){' |
||
157 | +'window.parent.EmulatorUrlBye(this.location.href);' |
||
158 | +'}' |
||
159 | +'}' |
||
160 | +'window.onbeforeunload = bye;' |
||
161 | +'</script>'; |
||
162 | var EmulatorNoContentScript = '<script type="text/javascript">' |
||
0 ignored issues
–
show
|
|||
163 | +'document.domain=document.domain.replace(\'www.\',\'\');' |
||
164 | +'window.parent.EmulatorUrlKO();' |
||
165 | +'</script>'; |
||
166 | |||
167 | curl.get(query.url, {}, function(err, response, body) { |
||
168 | if (err) throw err; |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed.
Loading history...
|
|||
169 | if (body != '') { |
||
170 | body = String(body).replace('<head>','<head><base href="'+query.url+'">'); |
||
171 | httpResponse.write( |
||
172 | String(body).replace('</body>', EmulatorLinkScript+'</body>') |
||
173 | +'' |
||
174 | +"\n" |
||
175 | ); |
||
176 | } else { |
||
177 | httpResponse.write( |
||
178 | String(body).replace('</body>', EmulatorLinkScript+'</body>') |
||
179 | +'' |
||
180 | +"\n" |
||
181 | ); |
||
182 | } |
||
183 | httpResponse.end(); |
||
184 | }); |
||
185 | |||
186 | break; |
||
187 | |||
188 | case '/userscreensize.php' : |
||
189 | httpResponse.write('url: '+JSON.stringify(httpRequest._parsedUrl, null, 4)+"\n" ); |
||
190 | httpResponse.end(); |
||
191 | break; |
||
192 | |||
193 | |||
194 | default: |
||
195 | // no route found... handle over to try to serve file... |
||
196 | //console.log('request: ', httpRequest.url); |
||
197 | next(); |
||
198 | break; |
||
199 | |||
200 | } // switch (route) |
||
201 | |||
202 | break; |
||
203 | |||
204 | } // switch (method) |
||
205 | |||
206 | } |
||
207 | |||
208 | } |
||
209 | |||
210 | Patternlibrary_Middleware.prototype.defaults = require('./../config/default.js'); |
||
211 | |||
212 | module.exports = Patternlibrary_Middleware; |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.