Permalink
Browse files

made changes to remove the warning about 'sys' being renamed 'util' a…

…nd changes to allow Cross Origin Resource Sharing in the node.js server
  • Loading branch information...
1 parent 6acabb8 commit 4824d1c38860d52403c2f97a3d3a08356619a18e @pdebruic pdebruic committed with Herbert Vojčík Apr 17, 2012
Showing with 8,756 additions and 5,827 deletions.
  1. +3 −3 bin/nodecompile.js
  2. +37 −14 server/FileServer.st
  3. +8,716 −5,810 server/server.js
View
6 bin/nodecompile.js 100644 → 100755
@@ -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) {
View
51 server/FileServer.st 100644 → 100755
@@ -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'!
Oops, something went wrong.

0 comments on commit 4824d1c

Please sign in to comment.