Permalink
Browse files

Port to JavaScript

  • Loading branch information...
1 parent b256a3e commit 42e105c2b55b34150ca3777113b0a3af0a9b7066 @isaacs isaacs committed Apr 24, 2012
View
@@ -1,31 +0,0 @@
-## OSX
-.DS_Store
-
-# Thumbnails
-._*
-
-# Files that might appear on external disk
-.Spotlight-V100
-.Trashes
-
-## vim
-.*.sw[a-z]
-*.un~
-Session.vim
-
-## Node
-lib-cov
-*.seed
-*.log
-*.csv
-*.dat
-*.out
-*.pid
-*.gz
-
-pids
-logs
-results
-
-node_modules
-npm-debug.log
View
@@ -1,27 +0,0 @@
-Negotiator = require('../lib/negotiator').Negotiator
-http = require('http')
-
-representations =
- 'text/html': '<h1>Hello world!</h1>'
- 'text/plain': 'Hello World!'
- 'application/json': JSON.stringify({hello: 'world!'})
-
-availableMediaTypes = (key for key, val of representations)
-
-server = http.createServer (req, res) ->
- negotiator = new Negotiator(req)
-
- console.log "Accept: #{req.headers['accept']}"
- console.log "Preferred: #{negotiator.preferredMediaTypes()}"
- console.log "Possible: #{negotiator.preferredMediaTypes(availableMediaTypes)}"
- mediaType = negotiator.preferredMediaType(availableMediaTypes)
- console.log "Selected: #{mediaType}"
-
- if mediaType
- res.writeHead(200, {'Content-Type': mediaType})
- res.end(representations[mediaType])
- else
- res.writeHead(406)
- res.end()
-
-server.listen(8080)
View
@@ -0,0 +1,47 @@
+(function() {
+ var Negotiator, availableMediaTypes, http, key, representations, server, val;
+
+ Negotiator = require('../lib/negotiator').Negotiator;
+
+ http = require('http');
+
+ representations = {
+ 'text/html': '<h1>Hello world!</h1>',
+ 'text/plain': 'Hello World!',
+ 'application/json': JSON.stringify({
+ hello: 'world!'
+ })
+ };
+
+ availableMediaTypes = (function() {
+ var _results;
+ _results = [];
+ for (key in representations) {
+ val = representations[key];
+ _results.push(key);
+ }
+ return _results;
+ })();
+
+ server = http.createServer(function(req, res) {
+ var mediaType, negotiator;
+ negotiator = new Negotiator(req);
+ console.log("Accept: " + req.headers['accept']);
+ console.log("Preferred: " + (negotiator.preferredMediaTypes()));
+ console.log("Possible: " + (negotiator.preferredMediaTypes(availableMediaTypes)));
+ mediaType = negotiator.preferredMediaType(availableMediaTypes);
+ console.log("Selected: " + mediaType);
+ if (mediaType) {
+ res.writeHead(200, {
+ 'Content-Type': mediaType
+ });
+ return res.end(representations[mediaType]);
+ } else {
+ res.writeHead(406);
+ return res.end();
+ }
+ });
+
+ server.listen(8080);
+
+}).call(this);
View
@@ -1,31 +0,0 @@
-Negotiator = require('../lib/negotiator').Negotiator
-http = require('http')
-
-Buffer = require('buffer').Buffer
-Iconv = require('iconv').Iconv
-
-iconv = new Iconv('UTF-8', 'ISO-8859-1')
-message = "ë"
-messages =
- 'utf-8': message
- 'iso-8859-1': iconv.convert(new Buffer(message))
-
-availableCharsets = (key for key, val of messages)
-
-server = http.createServer (req, res) ->
- negotiator = new Negotiator(req )
-
- console.log "Accept-Charset: #{req.headers['accept-charset']}"
- console.log "Preferred: #{negotiator.preferredCharsets()}"
- console.log "Possible: #{negotiator.preferredCharsets(availableCharsets)}"
- charset = negotiator.preferredCharset(availableCharsets)
- console.log "Selected: #{charset}"
-
- if charset
- res.writeHead(200, {'Content-Type': "text/html; charset=#{charset}"})
- res.end(messages[charset])
- else
- res.writeHead(406)
- res.end()
-
-server.listen(8080)
View
@@ -0,0 +1,52 @@
+(function() {
+ var Buffer, Iconv, Negotiator, availableCharsets, http, iconv, key, message, messages, server, val;
+
+ Negotiator = require('../lib/negotiator').Negotiator;
+
+ http = require('http');
+
+ Buffer = require('buffer').Buffer;
+
+ Iconv = require('iconv').Iconv;
+
+ iconv = new Iconv('UTF-8', 'ISO-8859-1');
+
+ message = "ë";
+
+ messages = {
+ 'utf-8': message,
+ 'iso-8859-1': iconv.convert(new Buffer(message))
+ };
+
+ availableCharsets = (function() {
+ var _results;
+ _results = [];
+ for (key in messages) {
+ val = messages[key];
+ _results.push(key);
+ }
+ return _results;
+ })();
+
+ server = http.createServer(function(req, res) {
+ var charset, negotiator;
+ negotiator = new Negotiator(req);
+ console.log("Accept-Charset: " + req.headers['accept-charset']);
+ console.log("Preferred: " + (negotiator.preferredCharsets()));
+ console.log("Possible: " + (negotiator.preferredCharsets(availableCharsets)));
+ charset = negotiator.preferredCharset(availableCharsets);
+ console.log("Selected: " + charset);
+ if (charset) {
+ res.writeHead(200, {
+ 'Content-Type': "text/html; charset=" + charset
+ });
+ return res.end(messages[charset]);
+ } else {
+ res.writeHead(406);
+ return res.end();
+ }
+ });
+
+ server.listen(8080);
+
+}).call(this);
View
@@ -1,31 +0,0 @@
-Negotiator = require('../lib/negotiator').Negotiator
-http = require('http')
-
-gbuf = require('gzip-buffer')
-
-messages =
- identity: 'Hello World'
-
-gbuf.gzip messages.identity, (zipped) ->
- messages.gzip = zipped
-
- availableEncodings = (key for key, val of messages)
- console.log availableEncodings
-
- server = http.createServer (req, res) ->
- negotiator = new Negotiator(req)
-
- console.log "Accept-Encoding: #{req.headers['accept-encoding']}"
- console.log "Preferred: #{negotiator.preferredEncodings()}"
- console.log "Possible: #{negotiator.preferredEncodings(availableEncodings)}"
- encoding = negotiator.preferredEncoding(availableEncodings)
- console.log "Selected: #{encoding}"
-
- if encoding
- res.writeHead(200, {'Content-Encoding': encoding})
- res.end(messages[encoding])
- else
- res.writeHead(406)
- res.end()
-
- server.listen(8080)
View
@@ -0,0 +1,48 @@
+(function() {
+ var Negotiator, gbuf, http, messages;
+
+ Negotiator = require('../lib/negotiator').Negotiator;
+
+ http = require('http');
+
+ gbuf = require('gzip-buffer');
+
+ messages = {
+ identity: 'Hello World'
+ };
+
+ gbuf.gzip(messages.identity, function(zipped) {
+ var availableEncodings, key, server, val;
+ messages.gzip = zipped;
+ availableEncodings = (function() {
+ var _results;
+ _results = [];
+ for (key in messages) {
+ val = messages[key];
+ _results.push(key);
+ }
+ return _results;
+ })();
+ console.log(availableEncodings);
+ server = http.createServer(function(req, res) {
+ var encoding, negotiator;
+ negotiator = new Negotiator(req);
+ console.log("Accept-Encoding: " + req.headers['accept-encoding']);
+ console.log("Preferred: " + (negotiator.preferredEncodings()));
+ console.log("Possible: " + (negotiator.preferredEncodings(availableEncodings)));
+ encoding = negotiator.preferredEncoding(availableEncodings);
+ console.log("Selected: " + encoding);
+ if (encoding) {
+ res.writeHead(200, {
+ 'Content-Encoding': encoding
+ });
+ return res.end(messages[encoding]);
+ } else {
+ res.writeHead(406);
+ return res.end();
+ }
+ });
+ return server.listen(8080);
+ });
+
+}).call(this);
View
@@ -1,26 +0,0 @@
-Negotiator = require('../lib/negotiator').Negotiator
-http = require('http')
-
-messages =
- es: "¡Hola Mundo!",
- en: "Hello World!"
-
-availableLanguages = (key for key, val of messages)
-
-server = http.createServer (req, res) ->
- negotiator = new Negotiator(req )
-
- console.log "Accept-Language: #{req.headers['accept-language']}"
- console.log "Preferred: #{negotiator.preferredLanguages()}"
- console.log "Possible: #{negotiator.preferredLanguages(availableLanguages)}"
- language = negotiator.preferredLanguage(availableLanguages)
- console.log "Selected: #{language}"
-
- if language
- res.writeHead(200, {'Content-Language': language})
- res.end(messages[language])
- else
- res.writeHead(406)
- res.end()
-
-server.listen(8080)
View
@@ -0,0 +1,44 @@
+(function() {
+ var Negotiator, availableLanguages, http, key, messages, server, val;
+
+ Negotiator = require('../lib/negotiator').Negotiator;
+
+ http = require('http');
+
+ messages = {
+ es: "¡Hola Mundo!",
+ en: "Hello World!"
+ };
+
+ availableLanguages = (function() {
+ var _results;
+ _results = [];
+ for (key in messages) {
+ val = messages[key];
+ _results.push(key);
+ }
+ return _results;
+ })();
+
+ server = http.createServer(function(req, res) {
+ var language, negotiator;
+ negotiator = new Negotiator(req);
+ console.log("Accept-Language: " + req.headers['accept-language']);
+ console.log("Preferred: " + (negotiator.preferredLanguages()));
+ console.log("Possible: " + (negotiator.preferredLanguages(availableLanguages)));
+ language = negotiator.preferredLanguage(availableLanguages);
+ console.log("Selected: " + language);
+ if (language) {
+ res.writeHead(200, {
+ 'Content-Language': language
+ });
+ return res.end(messages[language]);
+ } else {
+ res.writeHead(406);
+ return res.end();
+ }
+ });
+
+ server.listen(8080);
+
+}).call(this);
View
@@ -1,43 +0,0 @@
-_ = require('underscore')
-
-parseAcceptCharset = (accept) ->
- acceptableCharsets = accept.split(',').map (e) -> parseCharset(e.trim())
- _.select(acceptableCharsets, (e) -> e && e.q > 0)
-
-parseCharset = (s) ->
- match = /^\s*(\S+?)\s*(?:;(.*))?$/.exec(s)
- if !match
- null
- else
- charset = match[1]
- params = {}
- q = 1
- if match[2]
- for p in match[2].split(';').map((s) -> s.trim().split('='))
- params[p[0]] = p[1]
- if params.q?
- q = parseFloat(params.q)
- delete params.q
- {charset: charset, q: q}
-
-getCharsetPriority = (charset, accepted) ->
- specs = accepted.map (a) -> specify(charset, a)
- specs = _.select(specs, (x) -> x)
- _.max(specs, (spec) -> spec.q)?.q || 0
-
-specify = (charset, spec) ->
- spec if spec.charset == '*' || spec.charset == charset
-
-preferredCharsets = (accept, provided) ->
- accept = parseAcceptCharset(accept || '')
- if provided
- _.chain(provided)
- .map((type) -> [type, getCharsetPriority(type, accept)])
- .select((pair) -> pair[1] > 0)
- .sortBy((pair) -> pair[1] * -1)
- .map((pair) -> pair[0])
- .value()
- else
- accept.map((type) -> type.charset)
-
-exports.preferredCharsets = preferredCharsets
Oops, something went wrong.

0 comments on commit 42e105c

Please sign in to comment.