Click here to Skip to main content
12,508,992 members (57,837 online)
Click here to Skip to main content
Articles » Web Development » Node.js » General » Downloads

Stats

149.6K views
2K downloads
162 bookmarked
Posted

Node.Js And Stuff

, 11 Feb 2013 CPOL
Small demo app using Node.Js/Socket.IO/MongoDB/D3.Js and jQuery.
WebSocketDemo
node_modules
.bin
express
.npmignore
.travis.yml
History.md
lib
router
node_modules
commander
.npmignore
.travis.yml
History.md
lib
Readme.md
connect
.npmignore
lib
middleware
session
public
favicon.ico
icons
page.png
page_add.png
page_attach.png
page_code.png
page_copy.png
page_delete.png
page_edit.png
page_error.png
page_excel.png
page_find.png
page_gear.png
page_go.png
page_green.png
page_key.png
page_lightning.png
page_link.png
page_paintbrush.png
page_paste.png
page_red.png
page_refresh.png
page_save.png
page_white.png
page_white_acrobat.png
page_white_actionscript.png
page_white_add.png
page_white_c.png
page_white_camera.png
page_white_cd.png
page_white_code.png
page_white_code_red.png
page_white_coldfusion.png
page_white_compressed.png
page_white_copy.png
page_white_cplusplus.png
page_white_csharp.png
page_white_cup.png
page_white_database.png
page_white_delete.png
page_white_dvd.png
page_white_edit.png
page_white_error.png
page_white_excel.png
page_white_find.png
page_white_flash.png
page_white_freehand.png
page_white_gear.png
page_white_get.png
page_white_go.png
page_white_h.png
page_white_horizontal.png
page_white_key.png
page_white_lightning.png
page_white_link.png
page_white_magnify.png
page_white_medal.png
page_white_office.png
page_white_paint.png
page_white_paintbrush.png
page_white_paste.png
page_white_php.png
page_white_picture.png
page_white_powerpoint.png
page_white_put.png
page_white_ruby.png
page_white_stack.png
page_white_star.png
page_white_swoosh.png
page_white_text.png
page_white_text_width.png
page_white_tux.png
page_white_vector.png
page_white_visualstudio.png
page_white_width.png
page_white_word.png
page_white_world.png
page_white_wrench.png
page_white_zip.png
page_word.png
page_world.png
node_modules
bytes
.npmignore
History.md
Readme.md
formidable
.npmignore
.travis.yml
benchmark
example
lib
node-gently
example
lib
gently
Readme.md
test
simple
Readme.md
test
fixture
file
http
special-chars-in-filename
info.md
js
integration
legacy
integration
simple
system
unit
tool
pause
.npmignore
History.md
Readme.md
qs
.gitmodules
.npmignore
.travis.yml
History.md
lib
Readme.md
test
mocha.opts
Readme.md
cookie
.npmignore
.travis.yml
README.md
test
mocha.opts
crc
.gitmodules
.npmignore
lib
README.md
test
debug
.npmignore
example
History.md
lib
Readme.md
fresh
.npmignore
Readme.md
methods
mkdirp
.gitignore.orig
.gitignore.rej
.npmignore
.travis.yml
examples
pow.js.orig
pow.js.rej
README.markdown
test
range-parser
.npmignore
History.md
Readme.md
send
.npmignore
History.md
lib
node_modules
mime
README.md
types
mime.types
node.types
Readme.md
Readme.md
jade
.npmignore
jade.md
lib
nodes
node_modules
commander
.npmignore
.travis.yml
History.md
lib
Readme.md
mkdirp
.npmignore
.travis.yml
examples
README.markdown
test
Readme.md
test.jade
testing
head.jade
index.jade
layout.jade
user.jade
mongodb
.travis.yml
CONTRIBUTING.md
lib
mongodb
commands
connection
strategies
gridfs
responses
node_modules
bson
.travis.yml
benchmarks
binding.gyp
build
binding.Makefile
bson.target.mk
config.gypi
Release
.deps
Release
bson.node.d
obj.target
bson
ext
bson.o.d
bson.node
linker.lock
obj.target
bson
ext
bson.o
ext
bson.cc
win32
ia32
bson.node
x64
bson.node
lib
bson
README.md
test
browser
node
data
test_gs_weird_bug.png
tools
tools
jasmine-1.1.0
jasmine_favicon.png
MIT.LICENSE
Readme.md
node-static
.npmignore
benchmark
bin
etc
trainwreck.jpg
examples
lib
node-static
node_modules
colors
ReadMe.md
optimist
.travis.yml
example
node_modules
wordwrap
.npmignore
example
README.markdown
test
README.markdown
test
_
README.md
test
fixtures
integration
socket.io
.npmignore
.travis.yml
benchmarks
History.md
lib
stores
transports
websocket
node_modules
policyfile
.npmignore
doc
examples
lib
README.md
tests
ssl
ssl.crt
ssl.private.key
redis
.npmignore
benches
stress
pubsub
rpushblpop
speed
size-rate.png
changelog.md
examples
lib
parser
README.md
socket.io-client
.npmignore
bin
dist
WebSocketMain.swf
WebSocketMainInsecure.swf
History.md
lib
transports
vendor
web-socket-js
.npmignore
flash-src
build.sh
com
adobe
net
proxies
RFC2817Socket.as
gsolo
encryption
MD5.as
hurlant
crypto
cert
MozillaRootCertificates.as
X509Certificate.as
X509CertificateCollection.as
Crypto.as
hash
HMAC.as
IHash.as
IHMAC.as
MAC.as
MD2.as
MD5.as
SHA1.as
SHA224.as
SHA256.as
SHABase.as
prng
ARC4.as
IPRNG.as
Random.as
TLSPRF.as
rsa
RSAKey.as
symmetric
AESKey.as
aeskey.pl
BlowFishKey.as
CBCMode.as
CFB8Mode.as
CFBMode.as
CTRMode.as
DESKey.as
ECBMode.as
ICipher.as
IMode.as
IPad.as
IStreamCipher.as
ISymmetricKey.as
IVMode.as
NullPad.as
OFBMode.as
PKCS5.as
SimpleIVMode.as
SSLPad.as
TLSPad.as
TripleDESKey.as
XTeaKey.as
tests
AESKeyTest.as
ARC4Test.as
BigIntegerTest.as
BlowFishKeyTest.as
CBCModeTest.as
CFB8ModeTest.as
CFBModeTest.as
CTRModeTest.as
DESKeyTest.as
ECBModeTest.as
HMACTest.as
ITestHarness.as
MD2Test.as
MD5Test.as
OFBModeTest.as
RSAKeyTest.as
SHA1Test.as
SHA224Test.as
SHA256Test.as
TestCase.as
TLSPRFTest.as
TripleDESKeyTest.as
XTeaKeyTest.as
tls
BulkCiphers.as
CipherSuites.as
IConnectionState.as
ISecurityParameters.as
KeyExchanges.as
MACs.as
SSLConnectionState.as
SSLEvent.as
SSLSecurityParameters.as
TLSConfig.as
TLSConnectionState.as
TLSEngine.as
TLSError.as
TLSEvent.as
TLSSecurityParameters.as
TLSSocket.as
TLSSocketEvent.as
TLSTest.as
math
BarrettReduction.as
bi_internal.as
BigInteger.as
ClassicReduction.as
IReduction.as
MontgomeryReduction.as
NullReduction.as
util
ArrayUtil.as
Base64.as
der
ByteString.as
DER.as
IAsn1Type.as
Integer.as
ObjectIdentifier.as
OID.as
PEM.as
PrintableString.as
Sequence.as
Set.as
Type.as
UTCTime.as
Hex.as
Memory.as
IWebSocketLogger.as
WebSocket.as
WebSocketEvent.as
WebSocketMain.as
WebSocketMainInsecure.as
README.md
WebSocketMain.swf
WebSocketMainInsecure.zip
node_modules
.bin
active-x-obfuscator
.npmignore
node_modules
zeparser
.npmignore
Readme.md
uglify-js
.npmignore
lib
package.json~
README.org
test
unit
compress
expected
test
tmp
ws
.npmignore
.travis.yml
bench
binding.gyp
doc
ws.md
examples
fileapi
.npmignore
public
serverstats
public
serverstats-express_3
public
History.md
lib
node_modules
commander
.npmignore
.travis.yml
History.md
lib
Readme.md
options
.npmignore
lib
README.md
test
fixtures
test.conf
tinycolor
.npmignore
README.md
README.md
src
bufferutil.cc
validation.cc
test
fixtures
certificate.pem
key.pem
request.pem
xmlhttprequest
autotest.watchr
example
lib
README.md
tests
README.md
test
node
Readme.md
stylus
lib
convert
functions
index.styl
nodes
stack
visitor
node_modules
cssom
.gitmodules
.npmignore
lib
README.mdown
debug
.npmignore
example
History.md
lib
Readme.md
mkdirp
.npmignore
.travis.yml
examples
README.markdown
test
Readme.md
testing
small.styl
test.styl
public
images
Friend.png
Header.pdn
Header.png
javascripts
jquery-ui-1.9.1.custom
css
ui-lightness
images
ui-bg_diagonals-thick_18_b81900_40x40.png
ui-bg_diagonals-thick_20_666666_40x40.png
ui-bg_flat_10_000000_40x100.png
ui-bg_glass_100_f6f6f6_1x400.png
ui-bg_glass_100_fdf5ce_1x400.png
ui-bg_glass_65_ffffff_1x400.png
ui-bg_gloss-wave_35_f6a828_500x100.png
ui-bg_highlight-soft_100_eeeeee_1x100.png
ui-bg_highlight-soft_75_ffe45c_1x100.png
ui-icons_222222_256x240.png
ui-icons_228ef1_256x240.png
ui-icons_ef8c08_256x240.png
ui-icons_ffd27a_256x240.png
ui-icons_ffffff_256x240.png
js
stylesheets
home.styl
style.styl
routes
views
d3demo.jade
home.jade
layout.jade
var sys = require('util'),
  debug = require('util').debug,
  inspect = require('util').inspect,
  Buffer = require('buffer').Buffer,
  BSON = require('../../ext').BSON,
  Buffer = require('buffer').Buffer,
  BSONJS = require('../../lib/bson/bson').BSON,
  BinaryParser = require('../../lib/bson/binary_parser').BinaryParser,
  Long = require('../../lib/bson/long').Long,
  ObjectID = require('../../lib/bson/bson').ObjectID,
  Binary = require('../../lib/bson/bson').Binary,
  Code = require('../../lib/bson/bson').Code,
  DBRef = require('../../lib/bson/bson').DBRef,
  Symbol = require('../../lib/bson/bson').Symbol,
  Double = require('../../lib/bson/bson').Double,
  MaxKey = require('../../lib/bson/bson').MaxKey,
  MinKey = require('../../lib/bson/bson').MinKey,
  Timestamp = require('../../lib/bson/bson').Timestamp,
  gleak = require('../../tools/gleak'),
  assert = require('assert');

// Parsers
var bsonC = new BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]);
var bsonJS = new BSONJS([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]);

/**
 * Retrieve the server information for the current
 * instance of the db client
 *
 * @ignore
 */
exports.setUp = function(callback) {
  callback();
}

/**
 * Retrieve the server information for the current
 * instance of the db client
 *
 * @ignore
 */
exports.tearDown = function(callback) {
  callback();
}

/**
 * @ignore
 */
exports['Should Correctly Serialize and Deserialize simple edge value'] = function(test) {
  // Simple serialization and deserialization of edge value
  var doc = {doc:0x1ffffffffffffe};
  var simple_string_serialized = bsonC.serialize(doc, false, true);
  assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true));
  assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized));

  var doc = {doc:-0x1ffffffffffffe};
  var simple_string_serialized = bsonC.serialize(doc, false, true);
  assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true));
  assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized));
  test.done();
}

/**
 * @ignore
 */
exports['Should Correctly execute toJSON'] = function(test) {
  var a = Long.fromNumber(10);
  assert.equal(10, a);

  var a = Long.fromNumber(9223372036854775807);
  assert.equal(9223372036854775807, a);

  // Simple serialization and deserialization test for a Single String value
  var doc = {doc:'Serialize'};
  var simple_string_serialized = bsonC.serialize(doc, true, false);

  assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true));
  assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized));
  test.done();
}

/**
 * @ignore
 */
exports['Should Serialize and Deserialize nested document'] = function(test) {
  // Nested doc
  var doc = {a:{b:{c:1}}};
  var simple_string_serialized = bsonC.serialize(doc, false, true);

  assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true));
  assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized));
  test.done();
}

/**
 * @ignore
 */
exports['Simple integer serialization/deserialization test, including testing boundary conditions'] = function(test) {
  var doc = {doc:-1};
  var simple_string_serialized = bsonC.serialize(doc, false, true);
  assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true));
  assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized));

  var doc = {doc:2147483648};
  var simple_string_serialized = bsonC.serialize(doc, false, true);
  assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized));

  var doc = {doc:-2147483648};
  var simple_string_serialized = bsonC.serialize(doc, false, true);
  assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true));
  assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized));
  test.done();
}

/**
 * @ignore
 */
exports['Simple serialization and deserialization test for a Long value'] = function(test) {
  var doc = {doc:Long.fromNumber(9223372036854775807)};
  var simple_string_serialized = bsonC.serialize(doc, false, true);
  assert.deepEqual(simple_string_serialized, bsonJS.serialize({doc:Long.fromNumber(9223372036854775807)}, false, true));
  assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized));

  var doc = {doc:Long.fromNumber(-9223372036854775807)};
  var simple_string_serialized = bsonC.serialize(doc, false, true);
  assert.deepEqual(simple_string_serialized, bsonJS.serialize({doc:Long.fromNumber(-9223372036854775807)}, false, true));
  assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized));
  test.done();
}

/**
 * @ignore
 */
exports['Simple serialization and deserialization for a Float value'] = function(test) {
  var doc = {doc:2222.3333};
  var simple_string_serialized = bsonC.serialize(doc, false, true);
  assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true));
  assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized));

  var doc = {doc:-2222.3333};
  var simple_string_serialized = bsonC.serialize(doc, false, true);
  assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true));
  assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized));
  test.done();
}

/**
 * @ignore
 */
exports['Simple serialization and deserialization for a null value'] = function(test) {
  var doc = {doc:null};
  var simple_string_serialized = bsonC.serialize(doc, false, true);
  assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true));
  assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized));
  test.done();
}

/**
 * @ignore
 */
exports['Simple serialization and deserialization for a boolean value'] = function(test) {
  var doc = {doc:true};
  var simple_string_serialized = bsonC.serialize(doc, false, true);
  assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true));
  assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized));
  test.done();
}

/**
 * @ignore
 */
exports['Simple serialization and deserialization for a date value'] = function(test) {
  var date = new Date();
  var doc = {doc:date};
  var simple_string_serialized = bsonC.serialize(doc, false, true);
  assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true));
  assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized));
  test.done();
}

/**
 * @ignore
 */
exports['Simple serialization and deserialization for a boolean value'] = function(test) {
  var doc = {doc:/abcd/mi};
  var simple_string_serialized = bsonC.serialize(doc, false, true);
  assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true));
  assert.equal(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')).doc.toString(), bsonC.deserialize(simple_string_serialized).doc.toString());

  var doc = {doc:/abcd/};
  var simple_string_serialized = bsonC.serialize(doc, false, true);
  assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true));
  assert.equal(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')).doc.toString(), bsonC.deserialize(simple_string_serialized).doc.toString());
  test.done();
}

/**
 * @ignore
 */
exports['Simple serialization and deserialization for a objectId value'] = function(test) {
  var doc = {doc:new ObjectID()};
  var simple_string_serialized = bsonC.serialize(doc, false, true);
  var doc2 = {doc:ObjectID.createFromHexString(doc.doc.toHexString())};

  assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc2, false, true));
  assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')).doc.toString(), bsonC.deserialize(simple_string_serialized).doc.toString());
  test.done();
}

/**
 * @ignore
 */
exports['Simple serialization and deserialization for a Binary value'] = function(test) {
  var binary = new Binary();
  var string = 'binstring'
  for(var index = 0; index < string.length; index++) { binary.put(string.charAt(index)); }

  var simple_string_serialized = bsonC.serialize({doc:binary}, false, true);
  assert.deepEqual(simple_string_serialized, bsonJS.serialize({doc:binary}, false, true));
  assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')).doc.value(), bsonC.deserialize(simple_string_serialized).doc.value());
  test.done();
}

/**
 * @ignore
 */
exports['Simple serialization and deserialization for a Code value'] = function(test) {
  var code = new Code('this.a > i', {'i': 1});
  var simple_string_serialized_2 = bsonJS.serialize({doc:code}, false, true);
  var simple_string_serialized = bsonC.serialize({doc:code}, false, true);

  assert.deepEqual(simple_string_serialized, simple_string_serialized_2);
  assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized_2, 'binary')).doc.scope, bsonC.deserialize(simple_string_serialized).doc.scope);
  test.done();
}

/**
 * @ignore
 */
exports['Simple serialization and deserialization for an Object'] = function(test) {
  var simple_string_serialized = bsonC.serialize({doc:{a:1, b:{c:2}}}, false, true);
  var simple_string_serialized_2 = bsonJS.serialize({doc:{a:1, b:{c:2}}}, false, true);
  assert.deepEqual(simple_string_serialized, simple_string_serialized_2)
  assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized_2, 'binary')).doc, bsonC.deserialize(simple_string_serialized).doc);

  // Simple serialization and deserialization for an Array
  var simple_string_serialized = bsonC.serialize({doc:[9, 9, 1, 2, 3, 1, 1, 1, 1, 1, 1, 1]}, false, true);
  var simple_string_serialized_2 = bsonJS.serialize({doc:[9, 9, 1, 2, 3, 1, 1, 1, 1, 1, 1, 1]}, false, true);

  assert.deepEqual(simple_string_serialized, simple_string_serialized_2)
  assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized_2, 'binary')).doc, bsonC.deserialize(simple_string_serialized).doc);
  test.done();
}

/**
 * @ignore
 */
exports['Simple serialization and deserialization for a DBRef'] = function(test) {
  var oid = new ObjectID()
  var oid2 = new ObjectID.createFromHexString(oid.toHexString())
  var simple_string_serialized = bsonJS.serialize({doc:new DBRef('namespace', oid2, 'integration_tests_')}, false, true);
  var simple_string_serialized_2 = bsonC.serialize({doc:new DBRef('namespace', oid, 'integration_tests_')}, false, true);

  assert.deepEqual(simple_string_serialized, simple_string_serialized_2)
  // Ensure we have the same values for the dbref
  var object_js = bsonJS.deserialize(new Buffer(simple_string_serialized_2, 'binary'));
  var object_c = bsonC.deserialize(simple_string_serialized);

  assert.equal(object_js.doc.namespace, object_c.doc.namespace);
  assert.equal(object_js.doc.oid.toHexString(), object_c.doc.oid.toHexString());
  assert.equal(object_js.doc.db, object_c.doc.db);
  test.done();
}

/**
 * @ignore
 */
exports['Should correctly deserialize bytes array'] = function(test) {
  // Serialized document
  var bytes = [47,0,0,0,2,110,97,109,101,0,6,0,0,0,80,97,116,116,121,0,16,97,103,101,0,34,0,0,0,7,95,105,100,0,76,100,12,23,11,30,39,8,89,0,0,1,0];
  var serialized_data = '';
  // Convert to chars
  for(var i = 0; i < bytes.length; i++) {
    serialized_data = serialized_data + BinaryParser.fromByte(bytes[i]);
  }
  var object = bsonC.deserialize(new Buffer(serialized_data, 'binary'));
  assert.equal('Patty', object.name)
  assert.equal(34, object.age)
  assert.equal('4c640c170b1e270859000001', object._id.toHexString())
  test.done();
}

/**
 * @ignore
 */
exports['Serialize utf8'] = function(test) {
  var doc = { "name" : "本荘由利地域に洪水警報", "name1" : "öüóőúéáűíÖÜÓŐÚÉÁŰÍ", "name2" : "abcdedede"};
  var simple_string_serialized = bsonC.serialize(doc, false, true);
  var simple_string_serialized2 = bsonJS.serialize(doc, false, true);
  assert.deepEqual(simple_string_serialized, simple_string_serialized2)

  var object = bsonC.deserialize(simple_string_serialized);
  assert.equal(doc.name, object.name)
  assert.equal(doc.name1, object.name1)
  assert.equal(doc.name2, object.name2)
  test.done();
}

/**
 * @ignore
 */
exports['Serialize object with array'] = function(test) {
  var doc = {b:[1, 2, 3]};
  var simple_string_serialized = bsonC.serialize(doc, false, true);
  var simple_string_serialized_2 = bsonJS.serialize(doc, false, true);
  assert.deepEqual(simple_string_serialized, simple_string_serialized_2)

  var object = bsonC.deserialize(simple_string_serialized);
  assert.deepEqual(doc, object)
  test.done();
}

/**
 * @ignore
 */
exports['Test equality of an object ID'] = function(test) {
  var object_id = new ObjectID();
  var object_id_2 = new ObjectID();
  assert.ok(object_id.equals(object_id));
  assert.ok(!(object_id.equals(object_id_2)))
  test.done();
}

/**
 * @ignore
 */
exports['Test same serialization for Object ID'] = function(test) {
  var object_id = new ObjectID();
  var object_id2 = ObjectID.createFromHexString(object_id.toString())
  var simple_string_serialized = bsonJS.serialize({doc:object_id}, false, true);
  var simple_string_serialized_2 = bsonC.serialize({doc:object_id2}, false, true);

  assert.equal(simple_string_serialized_2.length, simple_string_serialized.length);
  assert.deepEqual(simple_string_serialized, simple_string_serialized_2)
  var object = bsonJS.deserialize(new Buffer(simple_string_serialized_2, 'binary'));
  var object2 = bsonC.deserialize(simple_string_serialized);
  assert.equal(object.doc.id, object2.doc.id)
  test.done();
}

/**
 * @ignore
 */
exports['Complex object serialization'] = function(test) {
  // JS Object
  var c1 = { _id: new ObjectID, comments: [], title: 'number 1' };
  var c2 = { _id: new ObjectID, comments: [], title: 'number 2' };
  var doc = {
      numbers: []
    , owners: []
    , comments: [c1, c2]
    , _id: new ObjectID
  };

  var simple_string_serialized = bsonJS.serialize(doc, false, true);

  // C++ Object
  var c1 = { _id: ObjectID.createFromHexString(c1._id.toHexString()), comments: [], title: 'number 1' };
  var c2 = { _id: ObjectID.createFromHexString(c2._id.toHexString()), comments: [], title: 'number 2' };
  var doc = {
      numbers: []
    , owners: []
    , comments: [c1, c2]
    , _id: ObjectID.createFromHexString(doc._id.toHexString())
  };

  var simple_string_serialized_2 = bsonC.serialize(doc, false, true);

  for(var i = 0; i < simple_string_serialized_2.length; i++) {
    // debug(i + "[" + simple_string_serialized_2[i] + "] = [" + simple_string_serialized[i] + "]")
    assert.equal(simple_string_serialized_2[i], simple_string_serialized[i]);
  }

  var doc1 = bsonJS.deserialize(new Buffer(simple_string_serialized_2));
  var doc2 = bsonC.deserialize(new Buffer(simple_string_serialized_2));
  assert.equal(doc._id.id, doc1._id.id)
  assert.equal(doc._id.id, doc2._id.id)
  assert.equal(doc1._id.id, doc2._id.id)

  var doc = {
   _id: 'testid',
    key1: { code: 'test1', time: {start:1309323402727,end:1309323402727}, x:10, y:5 },
    key2: { code: 'test1', time: {start:1309323402727,end:1309323402727}, x:10, y:5 }
  };

  var simple_string_serialized = bsonJS.serialize(doc, false, true);
  var simple_string_serialized_2 = bsonC.serialize(doc, false, true);
  test.done();
}

/**
 * @ignore
 */
exports['Serialize function'] = function(test) {
  var doc = {
   _id: 'testid',
    key1: function() {}
  }

  var simple_string_serialized = bsonJS.serialize(doc, false, true, true);
  var simple_string_serialized_2 = bsonC.serialize(doc, false, true, true);

  // Deserialize the string
  var doc1 = bsonJS.deserialize(new Buffer(simple_string_serialized_2));
  var doc2 = bsonC.deserialize(new Buffer(simple_string_serialized_2));
  assert.equal(doc1.key1.code.toString(), doc2.key1.code.toString())
  test.done();
}

/**
 * @ignore
 */
exports['Serialize document with special operators'] = function(test) {
  var doc =  {"user_id":"4e9fc8d55883d90100000003","lc_status":{"$ne":"deleted"},"owner_rating":{"$exists":false}};
  var simple_string_serialized = bsonJS.serialize(doc, false, true, true);
  var simple_string_serialized_2 = bsonC.serialize(doc, false, true, true);

  // Should serialize to the same value
  assert.equal(simple_string_serialized_2.toString('base64'), simple_string_serialized.toString('base64'))
  var doc1 = bsonJS.deserialize(simple_string_serialized_2);
  var doc2 = bsonC.deserialize(simple_string_serialized);
  assert.deepEqual(doc1, doc2)
  test.done();
}

/**
 * @ignore
 */
exports['Create ObjectID from hex string'] = function(test) {
  // Hex Id
  var hexId = new ObjectID().toString();
  var docJS = {_id: ObjectID.createFromHexString(hexId), 'funds.remaining': {$gte: 1.222}, 'transactions.id': {$ne: ObjectID.createFromHexString(hexId)}};
  var docC = {_id: ObjectID.createFromHexString(hexId), 'funds.remaining': {$gte: 1.222}, 'transactions.id': {$ne: ObjectID.createFromHexString(hexId)}};
  var docJSBin = bsonJS.serialize(docJS, false, true, true);
  var docCBin = bsonC.serialize(docC, false, true, true);
  assert.equal(docCBin.toString('base64'), docJSBin.toString('base64'));
  test.done();
}

/**
 * @ignore
 */
exports['Serialize big complex document'] = function(test) {
  // Complex document serialization
  var doc = {"DateTime": "Tue Nov 40 2011 17:27:55 GMT+0000 (WEST)","isActive": true,"Media": {"URL": "http://videos.sapo.pt/Tc85NsjaKjj8o5aV7Ubb"},"Title": "Lisboa fecha a ganhar 0.19%","SetPosition": 60,"Type": "videos","Thumbnail": [{"URL": "http://rd3.videos.sapo.pt/Tc85NsjaKjj8o5aV7Ubb/pic/320x240","Dimensions": {"Height": 240,"Width": 320}}],"Source": {"URL": "http://videos.sapo.pt","SetID": "1288","SourceID": "http://videos.sapo.pt/tvnet/rss2","SetURL": "http://noticias.sapo.pt/videos/tv-net_1288/","ItemID": "Tc85NsjaKjj8o5aV7Ubb","Name": "SAPO Vídeos"},"Category": "Tec_ciencia","Description": "Lisboa fecha a ganhar 0.19%","GalleryID": new ObjectID("4eea2a634ce8573200000000"),"InternalRefs": {"RegisterDate": "Thu Dec 15 2011 17:12:51 GMT+0000 (WEST)","ChangeDate": "Thu Dec 15 2011 17:12:51 GMT+0000 (WEST)","Hash": 332279244514},"_id": new ObjectID("4eea2a96e52778160000003a")}
  var docJSBin = bsonJS.serialize(doc, false, true, true);
  var docCBin = bsonC.serialize(doc, false, true, true);
  assert.equal(docCBin.toString('base64'), docJSBin.toString('base64'));
  test.done();
}

/**
 * @ignore
 */
exports['Should error out due to 24 characters but not valid hexstring for ObjectID'] = function(test) {
  try {
    var oid = new ObjectID("tttttttttttttttttttttttt");
    test.ok(false);
  } catch(err) {}

  test.done();
}


/**
 * @ignore
 */
exports.noGlobalsLeaked = function(test) {
  var leaks = gleak.detectNew();
  test.equal(0, leaks.length, "global var leak detected: " + leaks.join(', '));
  test.done();
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Sacha Barber
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160927.1 | Last Updated 11 Feb 2013
Article Copyright 2013 by Sacha Barber
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid