Browse Source

made changes to remove the warning about 'sys' being renamed 'util' and changes to allow Cross Origin Resource Sharing in the node.js server

paul laptop 12 years ago
parent
commit
4824d1c388
3 changed files with 518 additions and 450 deletions
  1. 3 3
      bin/nodecompile.js
  2. 37 14
      server/FileServer.st
  3. 478 433
      server/server.js

+ 3 - 3
bin/nodecompile.js

@@ -2,7 +2,7 @@
 // The arguments variable is a series of .st filenames and category names.
 // If it is a .st file we import it, if it is a category name we export it
 // as aCategoryName.js.
-var sys = require('sys'), fs = require('fs');
+var util = require('util'), fs = require('fs');
 
 // Only care about our arguments, strip away node, all.js and debug flag.
 var arguments = process.argv.splice(4);
@@ -19,11 +19,11 @@ if (suffix == "no-silly-suffix") {
 // If it ends with .st, import it, otherwise export category as .js
 arguments.forEach(function(val, index, array) {
   if (/\.st/.test(val)) {
-    sys.puts("Reading file " + val);
+    util.puts("Reading file " + val);
     code = fs.readFileSync(val, "utf8");
     smalltalk.Importer._new()._import_(code._stream());
   } else {
-    sys.puts("Exporting " + (deploy ? "(debug + deploy)" : "(debug)") + " category "
+    util.puts("Exporting " + (deploy ? "(debug + deploy)" : "(debug)") + " category "
 		+ val + " as " + val + suffix + ".js" + (deploy ? " and " + val + suffix + ".deploy.js" : ""));
     fs.writeFileSync(val + suffix + ".js", smalltalk.Exporter._new()._exportPackage_(val));
     if (deploy) {

+ 37 - 14
server/FileServer.st

@@ -1,6 +1,6 @@
 Object subclass: #FileServer
-	instanceVariableNames: 'path http fs url port basePath sys'
-	category: 'FileServer'!
+	instanceVariableNames: 'path http fs url port basePath util'
+	package: 'FileServer'!
 
 !FileServer methodsFor: 'accessing'!
 
@@ -23,7 +23,7 @@ initialize
 	path := self require: 'path'.
 	http := self require: 'http'.
 	fs := self require: 'fs'.
-	sys := self require: 'sys'.
+	util := self require: 'util'.
 	url := self require: 'url'
 ! !
 
@@ -48,9 +48,14 @@ respondNotFoundTo: aResponse
 !
 
 handleRequest: aRequest respondTo: aResponse
+
 	aRequest method = 'PUT'
-		ifTrue: [self handlePUTRequest: aRequest respondTo: aResponse]
-		ifFalse: [self handleGETRequest: aRequest respondTo: aResponse]
+		ifTrue: [self handlePUTRequest: aRequest respondTo: aResponse].
+	aRequest method = 'GET'
+		ifTrue:[self handleGETRequest: aRequest respondTo: aResponse].
+	aRequest method = 'OPTIONS'
+		ifTrue:[self handleOPTIONSRequest: aRequest respondTo: aResponse]
+
 !
 
 handleGETRequest: aRequest respondTo: aResponse
@@ -64,15 +69,32 @@ handleGETRequest: aRequest respondTo: aResponse
 !
 
 handlePUTRequest: aRequest respondTo: aResponse
-	| stream |
-	stream := fs createWriteStream: '.', aRequest url.
+	|stream |
+	stream := fs createWriteStream: '.' , aRequest url.
+        aRequest setEncoding: 'utf8'.
+        <
+		aRequest.on("data", function (chunk) {
+			stream.write(chunk);
+		});
+		aRequest.on("end", function () {
+			stream.end();
+			aResponse.writeHead(200, "OK", {
+			'Content-Type': 'text/plain',
+			'Access-Control-Allow-Origin': '*'
+       			});
+       			aResponse.end();
+	        })
+	>
 
-	aRequest setEncoding: 'utf8'.
-	aRequest on: #data do: [:data | stream write: data].
+!
 
-	aRequest on: #end do: [
-		stream end.
-		self respondOKTo: aResponse]
+handleOPTIONSRequest: aRequest respondTo: aResponse
+	aResponse writeHead: 200 options: #{'Access-Control-Allow-Origin' -> '*'.
+					'Access-Control-Allow-Methods' -> 'GET, PUT, POST, DELETE, OPTIONS'.
+					'Access-Control-Allow-Headers' -> 'Content-Type, Accept'.
+					'Content-Length' -> 0.
+					'Access-Control-Max-Age' -> 10}.
+	aResponse end
 !
 
 respondFileNamed: aFilename to: aResponse
@@ -102,8 +124,9 @@ respondInternalErrorTo: aResponse
 
 respondOKTo: aResponse
 	aResponse 
-		writeHead: 200 options: #{'Content-Type' -> 'text/plain'};
-		end
+		writeHead: 200 options: #{'Content-Type' -> 'text/plain'. 'Access-Control-Allow-Origin' -> '*'}.
+	aResponse end.
+					
 ! !
 
 !FileServer methodsFor: 'starting'!

File diff suppressed because it is too large
+ 478 - 433
server/server.js


Some files were not shown because too many files changed in this diff