Comparing version 0.1.0 to 0.2.0
// The erlang.js API | ||
// | ||
module.exports = { "term_to_binary": require('./term_to_binary').term_to_binary | ||
var term_to_binary = require('./term_to_binary'); | ||
module.exports = { "term_to_binary": term_to_binary.term_to_binary | ||
, "optlist_to_binary": term_to_binary.optlist_to_binary | ||
} |
56
lib.js
@@ -1,24 +0,27 @@ | ||
exports.VERSION_MAGIC = 131; | ||
exports.tags = { 'SMALL_INTEGER' : 'a' | ||
, 'INTEGER' : 'b' | ||
, 'FLOAT' : 'c' | ||
, 'ATOM' : 'd' | ||
, 'SMALL_ATOM' : 's' | ||
, 'REFERENCE' : 'e' | ||
, 'NEW_REFERENCE' : 'r' | ||
, 'PORT' : 'f' | ||
, 'NEW_FLOAT' : 'F' | ||
, 'PID' : 'g' | ||
, 'SMALL_TUPLE' : 'h' | ||
, 'LARGE_TUPLE' : 'i' | ||
, 'NIL' : 'j' | ||
, 'STRING' : 'k' | ||
, 'LIST' : 'l' | ||
, 'BINARY' : 'm' | ||
, 'BIT_BINARY' : 'M' | ||
, 'SMALL_BIG' : 'n' | ||
, 'LARGE_BIG' : 'o' | ||
, 'NEW_FUN' : 'p' | ||
, 'EXPORT' : 'q' | ||
, 'FUN' : 'u' | ||
exports.VERSION_MAGIC = 131; // 131 83 | ||
exports.MAX_INTEGER = (1 << 27) - 1; | ||
exports.MIN_INTEGER = -(1 << 27); | ||
exports.tags = { 'SMALL_INTEGER' : 'a' // 97 61 | ||
, 'INTEGER' : 'b' // 98 62 | ||
, 'FLOAT' : 'c' // 99 63 | ||
, 'ATOM' : 'd' // 100 64 | ||
, 'SMALL_ATOM' : 's' // 115 73 | ||
, 'REFERENCE' : 'e' // 101 65 | ||
, 'NEW_REFERENCE' : 'r' // 114 72 | ||
, 'PORT' : 'f' // 102 66 | ||
, 'NEW_FLOAT' : 'F' // 70 46 | ||
, 'PID' : 'g' // 103 67 | ||
, 'SMALL_TUPLE' : 'h' // 104 68 | ||
, 'LARGE_TUPLE' : 'i' // 105 69 | ||
, 'NIL' : 'j' // 106 6a | ||
, 'STRING' : 'k' // 107 6b | ||
, 'LIST' : 'l' // 108 6c | ||
, 'BINARY' : 'm' // 109 6d | ||
, 'BIT_BINARY' : 'M' // 77 4d | ||
, 'SMALL_BIG' : 'n' // 110 6e | ||
, 'LARGE_BIG' : 'o' // 111 6f | ||
, 'NEW_FUN' : 'p' // 112 70 | ||
, 'EXPORT' : 'q' // 113 71 | ||
, 'FUN' : 'u' // 117 75 | ||
} | ||
@@ -41,7 +44,8 @@ | ||
if (value) { | ||
if(to_s(value) === '[object Array]') { | ||
if(to_s(value) === '[object Array]') | ||
s = 'array'; | ||
} else if(to_s(value) === '[object Buffer]') { | ||
else if(to_s(value) === '[object Buffer]') | ||
s = 'buffer'; | ||
} | ||
else if(typeof Buffer === 'function' && value instanceof Buffer) | ||
s = 'buffer'; | ||
} else { | ||
@@ -48,0 +52,0 @@ s = 'null'; |
{ "name": "erlang" | ||
, "version": "0.1.0" | ||
, "author": { "name": "Jason Smith" | ||
, "email": "jhs@couchone.com" } | ||
, "version": "0.2.0" | ||
, "author": { "name": "Iris Couch" | ||
, "email": "jhs@iriscouch.com" } | ||
, "description": "Erlang interoperability with Javascript" | ||
, "homepage": "http://github.com/jhs/erlang.js" | ||
, "homepage": "http://github.com/iriscouch/erlang.js" | ||
, "repository": { "type": "git" | ||
, "url": "git://github.com/jhs/erlang.js" } | ||
, "url": "git://github.com/iriscouch/erlang.js" } | ||
, "engines": [ "node" ] | ||
, "main": "api" | ||
} |
@@ -1,2 +0,2 @@ | ||
var sys = require('sys') | ||
var sys = require('util') | ||
, lib = require('./lib.js') | ||
@@ -20,4 +20,4 @@ ; | ||
this.encode = function(term) { | ||
var encoder = this[lib.typeOf(term)]; | ||
self.encode = function(term) { | ||
var encoder = self[lib.typeOf(term)]; | ||
if(!encoder) | ||
@@ -28,9 +28,11 @@ throw new Error("Do not know how to encode " + lib.typeOf(term) + ': ' + sys.inspect(term)); | ||
this.number = function(x) { | ||
return is_int(x) ? this.int(x) : this.float(x); | ||
self.number = function(x) { | ||
return is_int(x) ? self.int(x) : self.float(x); | ||
} | ||
this.int = function(x) { | ||
self.int = function(x) { | ||
if(x >= 0 && x < 256) | ||
return [lib.tags.SMALL_INTEGER, x]; | ||
else if(lib.MIN_INTEGER <= x && x <= lib.MAX_INTEGER) | ||
return [lib.tags.INTEGER, lib.uint32(x)]; | ||
else | ||
@@ -40,3 +42,3 @@ throw new Error('Unknown integer: ' + x); | ||
this.array = function(x) { | ||
self.array = function(x) { | ||
// Simple array encoding, without worrying about tagging. | ||
@@ -64,3 +66,3 @@ var result = [] | ||
this.object = function(x) { | ||
self.object = function(x) { | ||
if(Object.keys(x).length !== 1) | ||
@@ -88,3 +90,3 @@ throw new Error("Don't know how to process: " + sys.inspect(x)); | ||
this.atom = function(x) { | ||
self.atom = function(x) { | ||
var bytes = new Buffer(x, 'utf8'); | ||
@@ -98,3 +100,3 @@ var result = [ lib.tags.ATOM | ||
this.tuple = function(x) { | ||
self.tuple = function(x) { | ||
var result = []; | ||
@@ -110,3 +112,3 @@ if(x.length < 256) | ||
this.buffer = function(x) { | ||
self.buffer = function(x) { | ||
var result = [lib.tags.BINARY]; | ||
@@ -119,3 +121,3 @@ result.push(lib.uint32(x.length)); | ||
this.string = function(x) { | ||
self.string = function(x) { | ||
var result = []; | ||
@@ -136,3 +138,3 @@ result.push(lib.tags.STRING); | ||
this.boolean = function(x) { | ||
self.boolean = function(x) { | ||
return self.atom(x ? "true" : "false"); | ||
@@ -149,1 +151,64 @@ } | ||
} | ||
// Provide convenience to convert to Erlang opt lists: [{verbose, true}, quiet, etc] | ||
// Array elements must be either: | ||
// 1. String, from 1 to 255 characters of only lower-case alphanumerics -> atom | ||
// 2. A 2-array, first element are strings like #1. If the second element is | ||
// a string like #1, it is converted, otherwise left alone -> {two, tuple} | ||
// | ||
// Booleans are converted to tuples too. | ||
exports.optlist_to_term = optlist_to_term = function(opts) { | ||
var args = Array.prototype.slice.apply(arguments); | ||
if(args.length > 1) | ||
return optlist_to_term(args); | ||
if(typeOf(opts) !== 'array') | ||
throw new Error("Cannot convert to OptList: " + sys.inspect(opts)); | ||
var looks_like_atom = /^[a-z][a-zA-Z0-9@\._]{0,254}$/; | ||
function to_atom(el, opts) { | ||
var type = typeOf(el); | ||
if(type === 'boolean') | ||
return el; | ||
if(type === 'string') { | ||
if(looks_like_atom.test(el)) | ||
return {'atom': el}; | ||
else if(opts && opts.identity) | ||
return el; | ||
throw new Error("Invalid atom: " + el); | ||
} | ||
if(opts && opts.identity) | ||
return el; | ||
throw new Error("Cannot convert to atom: " + sys.inspect(el)); | ||
} | ||
function to_2_tuple(el) { | ||
return {'tuple': [ to_atom(el[0]) | ||
, to_atom(el[1], {identity:true}) ] }; | ||
} | ||
function element_to_opt(el) { | ||
var type = typeOf(el); | ||
if(type === 'string' || type === 'boolean') { | ||
return to_atom(el); | ||
} else if(type === 'array' && el.length === 2) { | ||
return to_2_tuple(el); | ||
} else if(type === 'object' && Object.keys(el).length === 1) { | ||
var key = Object.keys(el)[0]; | ||
return to_2_tuple([key, el[key]]); | ||
} else { | ||
throw new Error("Invalid optlist element: " + sys.inspect(el)); | ||
} | ||
} | ||
return opts.map(function(el) { return element_to_opt(el) }); | ||
} | ||
exports.optlist_to_binary = function() { | ||
return exports.term_to_binary(exports.optlist_to_term.apply(this, arguments)); | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
271
0
10672
7