req.on('end', function(string){
		var downloadedCrc = crc.crc32( data );
		var original = fs.readFileSync( file, { encoding: 'ascii' } );
		var originalCrc = crc.crc32( original );
		downloadedCrc.should.equal( originalCrc );
		done();
	});
Example #2
0
		.on('end', function() {
			sql.query("SET FOREIGN_KEY_CHECKS = 0");
			console.log('Move '+ fileList.length +' files...')
			for (var i = 0; i < fileList.length; i++) {
				var current = fileList[i];
				if(fs.existsSync(current.file)) {
					var outPath = path.join(outDir, current.genre.replace(/\//g, '\\'));
					if(!fs.existsSync(outPath)) fs.mkdirSync(outPath);
					outPath = path.join(outPath, current.album.replace(/\//g, '\\'));
					if(!fs.existsSync(outPath)) fs.mkdirSync(outPath);
					outPath = path.join(outPath, (current.disk+'-'+current.track+'-'+current.title+path.extname(current.file)).replace(/\//g, '\\'));
					if(outPath != current.file) {
						console.log('Move: #'+ (i+1) +' '+ current.file);
						fs.renameSync(current.file, outPath);
						var albumId = crc.crc32(current.album + path.dirname(outPath));
						var trackId = crc.crc32(outPath);
						
						sql.query("UPDATE music LEFT JOIN album ON music.album_id = album.album_id " +
								"SET id=:id, file=:outFile, albumId=:albumId WHERE id=:oldID", {
							id: trackId,
							albumId: albumId,
							outFile: outPath.replace(/\'/g, "\'"),
							oldID: current.id
						});
					} 
				} else {
					console.log(current.file +' does not exist, skipping.');
				}
			}
			console.log('Updating database...');
			sql.query("SET FOREIGN_KEY_CHECKS = 1");
			sql.end();
		});	
Example #3
0
 function assertFailIfDownloadedFileIsNotTheSameThatTheSource(ctx, file) {
     var to = ctx.getFileDestinationPath(file);
     var from = path.join(process.cwd(), ctx.getFileSourcePath(file));
     var crc32To = crc.crc32(fs.readFileSync(to)).toString(16);
     var crc32From = crc.crc32(fs.readFileSync(from)).toString(16);
     if (crc32To !== crc32From) {
         assert.fail();
     }
 }
  // perform validation on supplied fields { field1: value1, field2: value2, ... }
  // RETURN: validationState - a hash containing an aggregation of all our validation results
  //                           suitable to determine if React needs to re-generate
  //         NOTE: As an alternative, if a reactComp is supplied (in constructor), 
  //               then AlmondJoi can coordinate React re-renders automatically, and
  //               you do not have to interpret validationState.
  validate(fields) {
    
    const joiResult = Joi.validate(fields, 
                                    this._joiSchema,
                                    { // TODO: provide AlmondJoi client hooks to change these options
                                      abortEarly: false, // give us all the errors at once (both for all fields and multiple errors per field)
                                    });

    // post process joiResult
    // ... clear all prior messages
    let allProminentMsgs = [];
    for (const field of this.fields) {
      this.cntl[field].detailedMsgs = [];
    }
    // ... retain new joiResult, and accumulate our validationState
    let validationState   = crc.crc32("AlmondJoi:validationState");
    let firstFieldInError = null;
    if (joiResult.error) { // something was invalid
      for (const detail of joiResult.error.details) {
        const path = detail.path;
        if (!firstFieldInError) {
          firstFieldInError = path;
        }
        if (this.cntl[path].beingValidated) {
          this.cntl[path].detailedMsgs.push(detail.message);
          if (this.cntl[path].detailedMsgs.length === 1) // allProminentMsgs only contain the first msg per field
            allProminentMsgs.push( this.cntl[path].consolidatedMsg || detail.message);
          validationState = crc.crc32(detail.message, validationState);
        }
      }
    }
    validationState = validationState.toString(16);
    _firstFieldInError.set(this, firstFieldInError);
    _allProminentMsgs.set(this, allProminentMsgs);

    // as a convenience, optionally coordinate React re-renders
    if (this._reactComp &&                         // ... if our optional React Component has been supplied,
      validationState !== this._validationState) { //     and our validaion has changed,
      this._reactComp.forceUpdate();               // ask React to force an update (to visualize the errors)
    }

    // retain our current validation state
    this._validationState = validationState;

    // that's all folks :-)
    // console.log("validationState: " + validationState);
    return validationState
  }
Example #5
0
   channel.receiving = function(err, packet, cbChan)
   {
     tick()
 //    //console.log("We got a packet on the dfu-stm32 channel\n");
     // any responce is next frame
     if(packet && fd)
     {
       var json = {at:offset};
       var body = new Buffer(1024);
       var readLength = fs.readSync(fd,body,0,size,offset);
       if (readLength < 1024) {
         body.length = readLength;
       }
       offset += readLength;
       //buf.copy(body,0,0,body.length);
       checksum = crc.crc32(body,checksum);
       // if at the end, include total checksum
       if(readLength != size)
       {
         json.done = checksum;
         json.end = true;
         fs.closeSync(fd);
         fd = undefined
       }
       channel.send({json:json,body:body});
     }else{
       if (type === "bootloader")
         dfuRun(link, firmware, type, tick).then((r) => res(r)).catch((er) => rej(er));
       else
         res();
     }
     cbChan();
   }
 function hash(sess) {
   return crc(JSON.stringify(sess, function(key, val) {
     if (key !== 'cookie') {
       return val;
     }
   }));
 }
Example #7
0
app.post('/twilio', function (req, res) {
  var startTime = Date.now();
  var initialText;
  if (req.body === undefined) {
    res.send(400);
    return;
  }

  initialText = req.body.Body;
  if (initialText === undefined) {
    res.send(400);
    return;
  }

  var to = req.body.To || '';
  // Mask the user's phone number
  var from = '0';
  if (req.body.From !== undefined) {
    from = crc.hex32(crc.crc32(req.body.From));
  }

  // Record information for the log.
  var logEntry = logger.makeEntry(from);
  logEntry.data.message = initialText;

  console.log('\nInbound message info:');
  console.log(JSON.stringify({ from: from, to: to, body: initialText }));

  smsflow.respondToSms(initialText, from, logEntry)
  .then(function (message) {
    var twiML = twilio.sms(message);
    twilio.sendTwiML(res, twiML);

    logEntry.data.responseCount = twilio.countMessages(twiML);

    // Log the recorded data to the logger web service
    logEntry.send();

    console.log('Outbound message info:');
    console.log(twiML);
    console.log('Message length: ' + message.length);
    console.log('Processing time: ' + (Date.now() - startTime));
  })
  .fail(function (reason) {
    console.log(reason);
    console.log(reason.stack);

    var twiML = twilio.sms(Strings.GenericFailMessage);
    twilio.sendTwiML(res, twiML);

    logEntry.data.error = true;

    // Log the recorded data to the logger web service
    logEntry.send();

    console.log('Outbound message info:');
    console.log(twiML);
    console.log('Processing time: ' + (Date.now() - startTime));
  });
});
Example #8
0
function getFileHash(file) {
	if (!hashLookup[file]) {
		hashLookup[file] = crc.crc32(fs.readFileSync(file));
	}

	return hashLookup[file];
}
Example #9
0
   channel.receiving = function(err, packet, cbChan)
   {
 //    //console.log("We got a packet on the dfu-stm32 channel\n");
     // any responce is next frame
     if(packet && fd)
     {
       tick();
       var json = {at:offset};
       var body = new Buffer(1024);
       var readLength = fs.readSync(fd,body,0,size,offset);
       if (readLength < 1024) {
         body.length = readLength;
       }
 //      //console.log("dfu-stm32 chunk:", body);
       offset += readLength;
       //buf.copy(body,0,0,body.length);
       checksum = crc.crc32(body,checksum);
       // if at the end, include total checksum
       if(readLength != size)
       {
         json.done = checksum;
         json.end = true;
         fs.closeSync(fd);
         fd = undefined
       }
       //process.stdout.write(".")
 //      //console.log('\n\ndfu-stm32 frame',json,readLength, body);
       channel.send({json:json,body:body});
     }else{
       tick();
       //console.log("dfu-stm32 done, wait for reset...")
         res()
     }
     cbChan();
   }
Example #10
0
var copyFile = function (original, newname, callback) {
  // First get the CRC of the file before we move it
  var thisFileCRC = crc.crc32(fs.readFileSync(original)).toString(16);

  // Setup the reader and writer streams
  var readf = fs.createReadStream(original);
  var writef = fs.createWriteStream(newname);

  // When the piping is finished, do some logic.
  readf.on('end', function () {
    var newCrc = crc.crc32(fs.readFileSync(newname)).toString(16);
    // Check to see if they match, if not delete and report
    if (thisFileCRC !== newCrc) {
      errorFile.write(new Date() + ' - [CRC Mismatch]: ' + filename + '\n');
      console.log('[CRC Mismatch]: ' + original);
      errorCount++;
      callback('Copy Failure');
    }
    else {
      successCount++;
      callback();
    }
  });

  // Actually copy over the file via fancy Node piping
  readf.pipe(writef);
};
Example #11
0
      fs.readFile(resTypePlatform.sourceFilePath, function(err, buf) {
        if (err) {
          deferred.reject('Error reading ' + resTypePlatform.sourceFilePath);

        } else {
          try {
            sourceFiles[resTypePlatform.sourceFilePath].imageId = crc.crc32(buf).toString(16);

            var resImages = _.filter(images, function(image) {
              return image.resType === resType;
            });

            resImages.forEach(function(image) {
              if (image.platform === resTypePlatform.platform) {
                image.sourceFilePath = resTypePlatform.sourceFilePath;

                var sourceFile = sourceFiles[image.sourceFilePath];
                var tmpFilename = sourceFile.imageId + '-' + image.width + 'x' + image.height + '.png';

                image.imageId = sourceFile.imageId;
                image.tmpPath = path.join(tmpDir, tmpFilename);

                if (resType === 'splash') {
                  if (!((image.width >= image.height && generateLandscape) ||
                        (image.height >= image.width && generatePortrait))) {
                    image.skip = true;
                    return;
                  }
                }

                if (Settings.cacheImages && fs.existsSync(image.tmpPath)) {
                  log.info(image.resType + ' ' + image.platform + ' ' + image.name +
                           ' (' + image.width + 'x' + image.height + ') from cache');

                } else {
                  loadCachedSourceImageData(sourceFile);

                  if (sourceFile.cachedData && !sourceFile.cachedData.vector &&
                      (sourceFile.cachedData.width < image.width || sourceFile.cachedData.height < image.height)) {
                    image.skip = true;
                    log.error(image.resType + ' ' + image.platform + ' ' + image.name +
                              ' (' + image.width + 'x' + image.height + ') skipped, source image ' +
                              sourceFile.filename + ' (' + sourceFile.cachedData.width + 'x' +
                              sourceFile.cachedData.height + ') too small');

                  } else {
                    sourceFile.upload = true;
                    generateQueue.push(image);
                  }
                }
              }
            });
            deferred.resolve();

          } catch (e) {
            deferred.reject('Error loading ' + resTypePlatform.sourceFilePath + ' md5: ' + e);
          }
        }
      });
Example #12
0
function weakhash(buf) {
  if (buf.length === 0) {
    // fast-path empty
    return '0-0'
  }

  return buf.length.toString(16) + '-' + crc(buf).toString(16)
}
Example #13
0
CRC32Stream.prototype._transform = function(chunk, encoding, callback) {
  if (chunk) {
    this.checksum = crc32(chunk, this.checksum);
    this.rawSize += chunk.length;
  }

  callback(null, chunk);
};
Example #14
0
DeflateCRC32Stream.prototype.write = function(chunk, enc, cb) {
  if (chunk) {
    this.checksum = crc32(chunk, this.checksum);
    this.rawSize += chunk.length;
  }

  return zlib.DeflateRaw.prototype.write.call(this, chunk, enc, cb);
};
  write(chunk, enc, cb) {
    if (chunk) {
      this.checksum = crc32(chunk, this.checksum);
      this.rawSize += chunk.length;
    }

    return super.write(chunk, enc, cb);
  }
Example #16
0
function chunk(type, data) {
  var length = positiveNumberToBytes(data.length, 4);
  var typeAndData = Buffer.concat([new Buffer(type, 'binary'), data]);
  return Buffer.concat([
      length,
      typeAndData,
      positiveNumberToBytes(crc(typeAndData), 4)
  ]);
}
Example #17
0
var defRoute = function(session, msg, context, cb) {
  var list = context.getServersByType(msg.serverType);
  if (!list || !list.length) {
    cb(new Error('can not find server info for type:' + msg.serverType));
    return;
  }
  var uid = session ? (session.uid || '') : '';
  var index = Math.abs(crc.crc32(uid.toString())) % list.length;
  utils.invokeCallback(cb, null, list[index].id);
};
Example #18
0
function hash(sess) {
  return crc(JSON.stringify(sess, function (key, val) {
    // ignore sess.cookie property
    if (this === sess && key === 'cookie') {
      return
    }

    return val
  }))
}
Example #19
0
app.post('/tropo', function (req, res) {
  var startTime = Date.now();
  var tropo = new tropowebapi.TropoWebAPI();
  res.setHeader('Content-Type', 'application/json');

  var session = req.body.session;
  if (req.body.session === undefined) {
    res.send(400);
    return;
  }
  if (session.from === undefined) {
    res.send(400);
    return;
  }

  var to;
  if (session.to) {
    to = session.to.id || '';
  } else {
    to = '';
  }
  // Mask the user's phone number
  var from = '0';
  if (session.from.id !== undefined) {
    from = crc.hex32(crc.crc32(session.from.id));
  }
  var initialText = session.initialText.trim();

  var logEntry = logger.makeEntry(from);

  console.log('\nInbound message info:');
  console.log(JSON.stringify({ from: from, to: to, body: initialText }));

  smsflow.respondToSms(initialText, from, logEntry)
  .then(function (message) {
    tropo.say(message);
    var jsonOut = tropowebapi.TropoJSON(tropo);
    console.log('Outbound message info:');
    console.log(jsonOut);
    console.log('Message length: ' + message.length);
    console.log('Processing time: ' + (Date.now() - startTime));
    res.send(jsonOut);
  })
  .fail(function (reason) {
    console.log(reason);
    console.log(reason.stack);
    tropo.say(Strings.GenericFailMessage);
    var jsonOut = tropowebapi.TropoJSON(tropo);
    console.log('Outbound message info:');
    console.log(jsonOut);
    console.log('Processing time: ' + (Date.now() - startTime));
    res.send(jsonOut);
  });
});
Example #20
0
exports.wetag = function wetag(body, encoding){
  if (body.length === 0) {
    // fast-path empty body
    return 'W/"0-0"'
  }

  var buf = Buffer.isBuffer(body)
    ? body
    : new Buffer(body, encoding)
  var len = buf.length
  return 'W/"' + len.toString(16) + '-' + crc(buf) + '"'
};
Example #21
0
  hashingShingles : function(shingles, callback) {
    var hashes = [];
    for(var i = 0, n = 1; i < n; i++) {
      var hashedArr = [];
      for(var j = 0, k = shingles.length; j < k; j++) {
        hashedArr.push(crc.crc32(shingles[j]));
      }
      hashes.push(hashedArr);
    }

    callback(hashes);
  },
Example #22
0
 function getRandomDataCRCs(nelems, options) {
   let i = 0;
   const elements = {};
   while (i < nelems) {
     const data = generateRandomData(options);
     const id = crc32(data).toString(16);
     if (elements.hasOwnProperty(id)) {
       throw new Error(`Got duplicated element`);
     }
     elements[id] = data.length;
     i++;
   }
   return elements;
 }
Example #23
0
 readf.on('end', function () {
   var newCrc = crc.crc32(fs.readFileSync(newname)).toString(16);
   // Check to see if they match, if not delete and report
   if (thisFileCRC !== newCrc) {
     errorFile.write(new Date() + ' - [CRC Mismatch]: ' + filename + '\n');
     console.log('[CRC Mismatch]: ' + original);
     errorCount++;
     callback('Copy Failure');
   }
   else {
     successCount++;
     callback();
   }
 });
Example #24
0
    AssetsInliner.prototype.convertSVGFile = function(src) {
        if (this.svgs[src]) return Promise(this.svgs[src]);

        var that = this;
        var hash = crc.crc32(src).toString(16);

        // Create new file
        return this.createNewFile(DEFAULT_ASSETS_FOLDER, hash + '.png')
        .then(function(filename) {
            that.svgs[src] = filename;

            return imagesUtil.convertSVGToPNG(src, that.resolve(filename))
            .thenResolve(filename);
        });
    };
          result.body.vehicle.forEach(function(item) {
           var vehicle = item['$'];
           if(vehicle && vehicle.id) {
             var firebaseId = crc.crc32(vehicle.id + vehicle.routeTag);
             vehicle = traverseAndCoerce(vehicle);
	     vehicle.ts = (Date.now() / 1000) - vehicle.secsSinceReport;
	     vehicle.vtype = 'FJLMNXKT'.indexOf(vehicle.routeTag) > -1 ? 'train' : 'bus'; 
	     firebusRef.child(agency).child(firebaseId).set(vehicle);
           }
	   else {
             console.log("bad vehicle ->");
	     console.log(vehicle);
	   }
           i++;
          });
Example #26
0
 zlib.deflate(scanlinesBuffer, function(err, buffer){
   var IDATData = Buffer.concat([
     buffer,
     positiveNumberToBytes(crc(scanlinesBuffer), 4)
   ]);
   var IDATChunk = chunk('IDAT', IDATData);
   callback(Buffer.concat([
     fileSignature,
     IHDRChunk,
     metadataChunks,
     htMlChunk,
     IDATChunk,
     IENDChunk
   ]));
 });
Example #27
0
    AssetsInliner.prototype.convertSVGBuffer = function(buf) {
        var that = this;
        var hash = crc.crc32(buf).toString(16);

        // Already converted?
        if (this.inlineSvgs[hash]) return Promise(this.inlineSvgs[hash]);

        return this.createNewFile(DEFAULT_ASSETS_FOLDER, hash + '.png')
        .then(function(filename) {
            that.inlineSvgs[hash] = filename;

            return imagesUtil.convertSVGBufferToPNG(buf, that.resolve(filename))
            .thenResolve(filename);
        });
    };
                results.forEach(function(el) {
                    j++;
                    var k = {"keyword_id": key.id, "position": j, "domain": crc.crc32(self.parseURL(el.doc.domain)), timedAt: self.currentDate()};

                    self.logKeyword(k, function(err, status) {
                        if(err) {
                            return cb(err.message, null);
                        } else {
                            if(i == total) {
                                cb(null, "done");
                            }
                            i++;
                        }
                    })
                });
Example #29
0
    it('should get bucket from query', function (done) {
      var buckets = {
        a: 0,
        b: 10
      };
      var app = App({
        buckets: buckets
      });
      var crcstr = crc(JSON.stringify(buckets));

      request(app)
      .get('/path?abtest=a')
      .set('cookie', 'abtest=b:' + crcstr)
      .expect(200)
      .expect('a', done);
    });
Example #30
0
prototype._transform = function (blob, _, callback) {
  if (!Buffer.isBuffer(blob)) {
    var error = new Error('object not a Buffer or String')
    error.object = blob
    callback(error)
  } else {
    var buffer = new Buffer(CRC_BYTES + LENGTH_BYTES + blob.length)
    // 1. CRC-32 of the blob.
    buffer.writeUInt32BE(crc(blob))
    // 2. Byte length of the blob.
    buffer.writeUInt32BE(blob.length, CRC_BYTES)
    // 3. The blob.
    blob.copy(buffer, CRC_BYTES + LENGTH_BYTES)
    callback(null, buffer)
  }
}