What is the equivalent of INET_ATON() in mongodb? I am using nodejs with mongodb so if a equivalent in nodejs is avaliable than it is good enough.
// ip example: 192.168.2.1
function inet_aton(ip){
// split into octets
var a = ip.split('.');
var buffer = new ArrayBuffer(4);
var dv = new DataView(buffer);
for(var i = 0; i < 4; i++){
dv.setUint8(i, a[i]);
}
return(dv.getUint32(0));
}
// num example: 3232236033
function inet_ntoa(num){
var nbuffer = new ArrayBuffer(4);
var ndv = new DataView(nbuffer);
ndv.setUint32(0, num);
var a = new Array();
for(var i = 0; i < 4; i++){
a[i] = ndv.getUint8(i);
}
return a.join('.');
}
http://rolfrost.de/ipjs.html
Here's someone else's solution for converting an IP address from a dotted-decimal string to a 32-bit number:
function dot2num(dot)
{
var d = dot.split('.');
return ((((((+d[0])*256)+(+d[1]))*256)+(+d[2]))*256)+(+d[3]);
}
From here: IP-addresses stored as int results in overflow?
function inet_aton(ip){
var a = new Array();
a = ip.split('.');
return((a[0] << 24) >>> 0) + ((a[1] << 16) >>> 0) + ((a[2] << 8) >>> 0) + (a[3] >>> 0);
}
function inet_ntoa(n){
var a = ((n >> 24) & 0xFF) >>> 0;
var b = ((n >> 16) & 0xFF) >>> 0;
var c = ((n >> 8) & 0xFF) >>> 0;
var d = (n & 0xFF) >>> 0;
return(a + "." + b + "." + c + "." + d);
}
as of my site see below/above, compatible with older browsers.
Here's a version of the ntoa function in Javascript using typed arrays.
function ntoa(ipInt) {
var buffer = new ArrayBuffer(4);
var uint8View = new Uint8Array(buffer);
var uint32View = new Uint32Array(buffer);
uint32View[0] = ipInt;
var ipSegments = [];
for (var i = 0; i < uint8View.length; i ++) {
ipSegments.push(uint8View[i]);
}
return ipSegments.join(".");
}
Related
I am using a Javascript to generate wireguard keypairs but it's browser faced so i removed the window objects and have one more issue that prevents creation of private key.
the issue is this line of code that i cannot run in nodejs:
function generatePresharedKey() {
var privateKey = new Uint8Array(32);
var crypto = require('crypto');
crypto.getRandomValues(privateKey);
return privateKey;
}
this is the error i get
crypto.getRandomValues(privateKey);
^
TypeError: crypto.getRandomValues is not a function
if i try to call getRandomValues using require it says that it cannot find module. var getRandomValues = require('get-random-values');
How to do i import it ? npm install get-random-values doesn't help.
any advice ?
fiddle here :
/*! SPDX-License-Identifier: GPL-2.0
*
* Copyright (C) 2015-2020 Jason A. Donenfeld <Jason#zx2c4.com>. All Rights Reserved.
*/
function gf(init) {
var r = new Float64Array(16);
if (init) {
for (var i = 0; i < init.length; ++i)
r[i] = init[i];
}
return r;
}
function pack(o, n) {
var b, m = gf(), t = gf();
for (var i = 0; i < 16; ++i)
t[i] = n[i];
carry(t);
carry(t);
carry(t);
for (var j = 0; j < 2; ++j) {
m[0] = t[0] - 0xffed;
for (var i = 1; i < 15; ++i) {
m[i] = t[i] - 0xffff - ((m[i - 1] >> 16) & 1);
m[i - 1] &= 0xffff;
}
m[15] = t[15] - 0x7fff - ((m[14] >> 16) & 1);
b = (m[15] >> 16) & 1;
m[14] &= 0xffff;
cswap(t, m, 1 - b);
}
for (var i = 0; i < 16; ++i) {
o[2 * i] = t[i] & 0xff;
o[2 * i + 1] = t[i] >> 8;
}
}
function carry(o) {
var c;
for (var i = 0; i < 16; ++i) {
o[(i + 1) % 16] += (i < 15 ? 1 : 38) * Math.floor(o[i] / 65536);
o[i] &= 0xffff;
}
}
function cswap(p, q, b) {
var t, c = ~(b - 1);
for (var i = 0; i < 16; ++i) {
t = c & (p[i] ^ q[i]);
p[i] ^= t;
q[i] ^= t;
}
}
function add(o, a, b) {
for (var i = 0; i < 16; ++i)
o[i] = (a[i] + b[i]) | 0;
}
function subtract(o, a, b) {
for (var i = 0; i < 16; ++i)
o[i] = (a[i] - b[i]) | 0;
}
function multmod(o, a, b) {
var t = new Float64Array(31);
for (var i = 0; i < 16; ++i) {
for (var j = 0; j < 16; ++j)
t[i + j] += a[i] * b[j];
}
for (var i = 0; i < 15; ++i)
t[i] += 38 * t[i + 16];
for (var i = 0; i < 16; ++i)
o[i] = t[i];
carry(o);
carry(o);
}
function invert(o, i) {
var c = gf();
for (var a = 0; a < 16; ++a)
c[a] = i[a];
for (var a = 253; a >= 0; --a) {
multmod(c, c, c);
if (a !== 2 && a !== 4)
multmod(c, c, i);
}
for (var a = 0; a < 16; ++a)
o[a] = c[a];
}
function clamp(z) {
z[31] = (z[31] & 127) | 64;
z[0] &= 248;
}
function generatePublicKey(privateKey) {
var r, z = new Uint8Array(32);
var a = gf([1]),
b = gf([9]),
c = gf(),
d = gf([1]),
e = gf(),
f = gf(),
_121665 = gf([0xdb41, 1]),
_9 = gf([9]);
for (var i = 0; i < 32; ++i)
z[i] = privateKey[i];
clamp(z);
for (var i = 254; i >= 0; --i) {
r = (z[i >>> 3] >>> (i & 7)) & 1;
cswap(a, b, r);
cswap(c, d, r);
add(e, a, c);
subtract(a, a, c);
add(c, b, d);
subtract(b, b, d);
multmod(d, e, e);
multmod(f, a, a);
multmod(a, c, a);
multmod(c, b, e);
add(e, a, c);
subtract(a, a, c);
multmod(b, a, a);
subtract(c, d, f);
multmod(a, c, _121665);
add(a, a, d);
multmod(c, c, a);
multmod(a, d, f);
multmod(d, b, _9);
multmod(b, e, e);
cswap(a, b, r);
cswap(c, d, r);
}
invert(c, c);
multmod(a, a, c);
pack(z, a);
return z;
}
function generatePresharedKey() {
var privateKey = new Uint8Array(32);
//var crypto = require('crypto').randomBytes;
//var getRandomValues = require('get-random-values');
//crypto.getRandomValues(privateKey);
const webcrypto = require('crypto').webcrypto;
webcrypto.getRandomValues(privateKey);
return privateKey;
}
function generatePrivateKey() {
var privateKey = generatePresharedKey();
clamp(privateKey);
return privateKey;
}
function encodeBase64(dest, src) {
var input = Uint8Array.from([(src[0] >> 2) & 63, ((src[0] << 4) | (src[1] >> 4)) & 63, ((src[1] << 2) | (src[2] >> 6)) & 63, src[2] & 63]);
for (var i = 0; i < 4; ++i)
dest[i] = input[i] + 65 +
(((25 - input[i]) >> 8) & 6) -
(((51 - input[i]) >> 8) & 75) -
(((61 - input[i]) >> 8) & 15) +
(((62 - input[i]) >> 8) & 3);
}
function keyToBase64(key) {
var i, base64 = new Uint8Array(44);
for (i = 0; i < 32 / 3; ++i)
encodeBase64(base64.subarray(i * 4), key.subarray(i * 3));
encodeBase64(base64.subarray(i * 4), Uint8Array.from([key[i * 3 + 0], key[i * 3 + 1], 0]));
base64[43] = 61;
return String.fromCharCode.apply(null, base64);
}
function generateKeypair() {
var privateKey = generatePrivateKey();
var publicKey = generatePublicKey(privateKey);
return {
publicKey: keyToBase64(publicKey),
privateKey: keyToBase64(privateKey)
};
}
function doSomething() {
const keypair = generateKeypair()
var m = JSON.stringify(keypair)
var op = JSON.parse(m)
console.log(keypair)
//console.log(op.publicKey)
document.getElementById("demo").innerHTML = op.publicKey + "</br>" + op.privateKey;
}
doSomething();
Class: Crypto
Added in: v15.0.0
Calling require('crypto').webcrypto returns an instance of the Crypto
class. Crypto is a singleton that provides access to the remainder of
the crypto API.
Example:
const privateKey = new Uint8Array(32);
const webcrypto = require('crypto').webcrypto;
webcrypto.getRandomValues(privateKey);
Result:
÷ÆVY{ñÕÓ»ÃVíA0²†xò¥x´ü^18
You need to understand getRandomValues is on window.crypto that means it works on browser. To make it work on Node.js you need to install get-random-values
npm i get-random-values
In your module add this:
const getRandomValues = require('get-random-values'),
array = new Uint8Array(10);
getRandomValues(array);
console.log(getRandomValues(array));
So I am using this npm package: node-stl
And its working great. However the regexp syntax, mathematics and geometrical calculations are somewhat confusing to me. Especially all at the same time.
Basically what I want to achieve is to extend the script to calculate the bounding box of the STL.
Here is the main file that calculates the volume and weight of the STL being parsed/read.
var fs = require('fs');
// Vertex
function Vertex (v1,v2,v3) {
this.v1 = Number(v1);
this.v2 = Number(v2);
this.v3 = Number(v3);
}
// Vertex Holder
function VertexHolder (vertex1,vertex2,vertex3) {
this.vert1 = vertex1;
this.vert2 = vertex2;
this.vert3 = vertex3;
}
// transforming a Node.js Buffer into a V8 array buffer
function _toArrayBuffer (buffer) {
var
ab = new ArrayBuffer(buffer.length),
view = new Uint8Array(ab);
for (var i = 0; i < buffer.length; ++i) {
view[i] = buffer[i];
}
return ab;
}
// calculation of the triangle volume
// source: http://stackoverflow.com/questions/6518404/how-do-i-calculate-the-volume-of-an-object-stored-in-stl-files
function _triangleVolume (vertexHolder) {
var
v321 = Number(vertexHolder.vert3.v1 * vertexHolder.vert2.v2 * vertexHolder.vert1.v3),
v231 = Number(vertexHolder.vert2.v1 * vertexHolder.vert3.v2 * vertexHolder.vert1.v3),
v312 = Number(vertexHolder.vert3.v1 * vertexHolder.vert1.v2 * vertexHolder.vert2.v3),
v132 = Number(vertexHolder.vert1.v1 * vertexHolder.vert3.v2 * vertexHolder.vert2.v3),
v213 = Number(vertexHolder.vert2.v1 * vertexHolder.vert1.v2 * vertexHolder.vert3.v3),
v123 = Number(vertexHolder.vert1.v1 * vertexHolder.vert2.v2 * vertexHolder.vert3.v3);
return Number(1.0/6.0)*(-v321 + v231 + v312 - v132 - v213 + v123);
}
// parsing an STL ASCII string
function _parseSTLString (stl) {
var totalVol = 0;
// yes, this is the regular expression, matching the vertexes
// it was kind of tricky but it is fast and does the job
var vertexes = stl.match(/facet\s+normal\s+([-+]?\b(?:[0-9]*\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\b)\s+([-+]?\b(?:[0-9]*\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\b)\s+([-+]?\b(?:[0-9]*\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\b)\s+outer\s+loop\s+vertex\s+([-+]?\b(?:[0-9]*\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\b)\s+([-+]?\b(?:[0-9]*\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\b)\s+([-+]?\b(?:[0-9]*\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\b)\s+vertex\s+([-+]?\b(?:[0-9]*\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\b)\s+([-+]?\b(?:[0-9]*\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\b)\s+([-+]?\b(?:[0-9]*\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\b)\s+vertex\s+([-+]?\b(?:[0-9]*\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\b)\s+([-+]?\b(?:[0-9]*\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\b)\s+([-+]?\b(?:[0-9]*\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\b)\s+endloop\s+endfacet/g);
vertexes.forEach(function (vert) {
var preVertexHolder = new VertexHolder();
vert.match(/vertex\s+([-+]?\b(?:[0-9]*\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\b)\s+([-+]?\b(?:[0-9]*\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\b)\s+([-+]?\b(?:[0-9]*\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\b)\s/g).forEach(function (vertex, i) {
var tempVertex = vertex.replace('vertex', '').match(/[-+]?[0-9]*\.?[0-9]+/g);
var preVertex = new Vertex(tempVertex[0],tempVertex[1],tempVertex[2]);
preVertexHolder['vert'+(i+1)] = preVertex;
});
var partVolume = _triangleVolume(preVertexHolder);
totalVol += Number(partVolume);
})
var volumeTotal = Math.abs(totalVol)/1000;
return {
volume: volumeTotal, // cubic cm
weight: volumeTotal * 1.04 // gm
}
}
// parsing an STL Binary File
// (borrowed some code from here: https://github.com/mrdoob/three.js/blob/master/examples/js/loaders/STLLoader.js)
function _parseSTLBinary (buf) {
buf = _toArrayBuffer(buf);
var
headerLength = 80,
dataOffset = 84,
faceLength = 12*4 + 2,
le = true; // is little-endian
var
dvTriangleCount = new DataView(buf, headerLength, 4),
numTriangles = dvTriangleCount.getUint32(0, le),
totalVol = 0;
for (var i = 0; i < numTriangles; i++) {
var
dv = new DataView(buf, dataOffset + i*faceLength, faceLength),
normal = new Vertex(dv.getFloat32(0, le), dv.getFloat32(4, le), dv.getFloat32(8, le)),
vertHolder = new VertexHolder();
for(var v = 3; v < 12; v+=3) {
var vert = new Vertex(dv.getFloat32(v*4, le), dv.getFloat32((v+1)*4, le), dv.getFloat32( (v+2)*4, le ) );
vertHolder['vert'+(v/3)] = vert;
}
totalVol += _triangleVolume(vertHolder);
}
var volumeTotal = Math.abs(totalVol)/1000;
return {
volume: volumeTotal, // cubic cm
weight: volumeTotal * 1.04 // gm
}
}
// NodeStl
// =======
// > var stl = NodeStl(__dirname + '/myCool.stl');
// > console.log(stl.volume + 'cm^3');
// > console.log(stl.weight + 'gm');
function NodeStl (stlPath) {
var
buf = fs.readFileSync(stlPath),
isAscii = true;
for (var i=0, len=buf.length; i<len; i++) {
if (buf[i] > 127) { isAscii=false; break; }
}
if (isAscii)
return _parseSTLString(buf.toString());
else
return _parseSTLBinary(buf);
}
module.exports = NodeStl;
If anyone could help me with this it would be great. I know and it feels like it simple. That I just need to know max/min of the different directions(x,y,z) and could then calculate the bounding box.
But I do not understand what the max/min for x,y and z is here. Please answer if you have an idea.
I've made a new branch https://github.com/johannesboyne/node-stl/tree/boundingbox could you please verify whether the applied algorithm works?
Best,
Johannes
Edit: If the branch is stable -> works I'll push it into v.0.1.0 (don't know why it is still 0.0.1)
How to get the headers of the given Excel file in node xlsx (https://www.npmjs.com/package/xlsx) module?
Did the following in xlsx v0.16.9
const workbookHeaders = xlsx.readFile(filePath, { sheetRows: 1 });
const columnsArray = xlsx.utils.sheet_to_json(workbookHeaders.Sheets[sheetName], { header: 1 })[0];
As I could find, there's no exposed method to get headers of the Excel file from the module. So I copied few functions (With all respect to author. https://github.com/SheetJS/js-xlsx) from their source code and make that work with few changes.
function getHeaders(sheet){
var header=0, offset = 1;
var hdr=[];
var o = {};
if (sheet == null || sheet["!ref"] == null) return [];
var range = o.range !== undefined ? o.range : sheet["!ref"];
var r;
if (o.header === 1) header = 1;
else if (o.header === "A") header = 2;
else if (Array.isArray(o.header)) header = 3;
switch (typeof range) {
case 'string':
r = safe_decode_range(range);
break;
case 'number':
r = safe_decode_range(sheet["!ref"]);
r.s.r = range;
break;
default:
r = range;
}
if (header > 0) offset = 0;
var rr = XLSX.utils.encode_row(r.s.r);
var cols = new Array(r.e.c - r.s.c + 1);
for (var C = r.s.c; C <= r.e.c; ++C) {
cols[C] = XLSX.utils.encode_col(C);
var val = sheet[cols[C] + rr];
switch (header) {
case 1:
hdr.push(C);
break;
case 2:
hdr.push(cols[C]);
break;
case 3:
hdr.push(o.header[C - r.s.c]);
break;
default:
if (val === undefined) continue;
hdr.push(XLSX.utils.format_cell(val));
}
}
return hdr;
}
function safe_decode_range(range) {
var o = {s:{c:0,r:0},e:{c:0,r:0}};
var idx = 0, i = 0, cc = 0;
var len = range.length;
for(idx = 0; i < len; ++i) {
if((cc=range.charCodeAt(i)-64) < 1 || cc > 26) break;
idx = 26*idx + cc;
}
o.s.c = --idx;
for(idx = 0; i < len; ++i) {
if((cc=range.charCodeAt(i)-48) < 0 || cc > 9) break;
idx = 10*idx + cc;
}
o.s.r = --idx;
if(i === len || range.charCodeAt(++i) === 58) { o.e.c=o.s.c; o.e.r=o.s.r; return o; }
for(idx = 0; i != len; ++i) {
if((cc=range.charCodeAt(i)-64) < 1 || cc > 26) break;
idx = 26*idx + cc;
}
o.e.c = --idx;
for(idx = 0; i != len; ++i) {
if((cc=range.charCodeAt(i)-48) < 0 || cc > 9) break;
idx = 10*idx + cc;
}
o.e.r = --idx;
return o;
}
Call getHeaders function by passing the Work Sheet will return the headers array of the excel sheet.
Have a look here:
https://www.npmjs.com/package/xlsx if we go through the documentation. It says that we need to pass options By default, sheet_to_json scans the first row and uses the values as headers. With the header: 1 option, the function exports an array of arrays of values.
So the whole code goes like this:
const data = e.target.result;
const workbook = XLSX.read(data, { type: "array" });
console.log(workbook);
const firstSheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[firstSheetName];
const options = { header: 1 };
const sheetData2 = XLSX.utils.sheet_to_json(worksheet, options);
const header = sheetData2.shift();
console.log(header); //you should get your header right here
Where e.target comes from your input.
i am working in smart card development. i have created MF (Master file), DF (dedicated File), EF (Elementary file) in smart card. EF file is used to store the data. This EF may be transparent file or record oriented file. i have written the data to transparent file using 00D1000008 540100 5303 010203 this command.i am also try to write the record oriented file using 00DD000008 540100 5303 010203 this command. but i got the error (6700 error code) wrong length. i need the solution to write the smart card EF record oriented file. please guide me.
Screen shot:
My code:
i have used winscard.dll
private void button_Transmit_Click(object sender, EventArgs e)
{
Status.Text = "";
byte[] baData = null;
string sClass = textBox_Class.Text;
string sIns = textBox_CLA.Text;
string sP1 = textBox_P1.Text;
string sP2 = textBox_P2.Text;
string sP3 = textBox_P3.Text;
sP3 = sP3.ToUpper();
int k1 = 70;
string sData = textBox1.Text;
byte bP1 = 0;
byte bP2 = 0;
byte bP3 = 0;
byte bClass = byte.Parse(sClass, NumberStyles.AllowHexSpecifier);
byte bIns = byte.Parse(sIns, NumberStyles.AllowHexSpecifier);
if (sP1 != "" && sP1 != "#")
bP1 = byte.Parse(sP1, NumberStyles.AllowHexSpecifier);
if (sP2 != "" && sP2 != "#")
bP2 = byte.Parse(sP2, NumberStyles.AllowHexSpecifier);
int integer = int.Parse(sP3, NumberStyles.AllowHexSpecifier);
byte bLe = (byte)k1;
if (integer != 0 && sData.Length != 0)
{
baData = new byte[integer];
for (int nJ = 0; nJ < sData.Length; nJ += 2)
baData[nJ / 2] = byte.Parse(sData.Substring(nJ, 2), NumberStyles.AllowHexSpecifier);
bLe = 0;
}
UInt32 m_nProtocol = (uint)PROTOCOL.Undefined;
uint RecvLength = 0;
byte[] ApduBuffer = null;
IntPtr ApduResponse = IntPtr.Zero;
SCard_IO_Request ioRequest = new SCard_IO_Request();
ioRequest.m_dwProtocol = m_nProtocol;
ioRequest.m_cbPciLength = 8;
if (baData == null)
{
ApduBuffer = new byte[4 + ((bLe != 0) ? 1 : 0)];
if (bLe != 0)
{
ApduBuffer[4] = (byte)bLe;
}
}
else
{
if (textBox1.Text.Length > 8)
{
ApduBuffer = new byte[5 + baData.Length];
Buffer.BlockCopy(baData, 0, ApduBuffer, 5, baData.Length);
ApduBuffer[4] = (byte)(baData.Length);
}
//read binary
else
{
ApduBuffer = new byte[5 + baData.Length + 1];
Buffer.BlockCopy(baData, 0, ApduBuffer, 5, baData.Length);
ApduBuffer[4] = (byte)(baData.Length);
ApduBuffer[5 + baData.Length] = 255;
}
}
ApduBuffer[0] = bClass;
ApduBuffer[1] = bIns;
ApduBuffer[2] = bP1;
ApduBuffer[3] = bP2;
m_nLastError = SCardTransmit(scard.m_hCard, ref ioRequest, ApduBuffer, (uint)ApduBuffer.Length, ref ioRequest, ApduResponse, ref RecvLength);
textBox2.Text = "";
byte[] caReadersData = new byte[RecvLength];
if (m_nLastError == 0)
{
ApduResponse = Marshal.AllocHGlobal((int)RecvLength);
if (m_nLastError == 0)
{
m_nLastError = SCardTransmit(scard.m_hCard, ref ioRequest, ApduBuffer, (uint)ApduBuffer.Length, ref ioRequest, ApduResponse, ref RecvLength);
if (RecvLength > 2)
{
for (int nI = 0; nI < RecvLength - 2; nI++)
{
caReadersData[nI] = Marshal.ReadByte(ApduResponse, nI);
//kl[nI] = Marshal.ReadByte(ApduResponse, nI);
//result = string.Format("{0:X02}", caReadersData[nI]);
//Status.Text += string.Format("{0:X02}", caReadersData[nI]) + " ";
textBox2.Text += string.Format("{0:X02}", caReadersData[nI]) + " ";
//result = Status.Text;
}
}
else
{
for (int nI = 0; nI < RecvLength; nI++)
{
caReadersData[nI] = Marshal.ReadByte(ApduResponse, nI);
Status.Text += string.Format("{0:X02}", caReadersData[nI]) + " ";
}
}
}
}
Marshal.FreeHGlobal(ApduResponse);
}
Edit:
READ COMMAND is working fine. see the screen shot
It seems that you use an undefined coding of P2 with the odd instruction UPDATE RECORD command. Also, you have to specify the record number in P1. For the three least significant bits use
100-Replace
101-Logical AND
110-Logical OR
111-Logical XOR
If you aim for writing / updating complete records of small length, you might consider using the commands with even instruction. Then you can drop the offset DO (0x54) and only transmit the complete record (value of the discretionary data DO (0x53)).
I have code like this:
for (var i in data) {
for (var j in data[i]) {
for (var k in data[i][j]) {
db.data.insert({i:i, j:j, k:k}, emptyCallback);
}
}
}
but I run out of memory because it's queuing up all the inserts. How can I make the for loop pause until the insert is complete?
I've tried pushing all the records to an array to insert later, but then the array gets too big and again I run out of memory.
you need to iterate to next key each time callback is called, something like this:
function get_first_keys(data, keys)
{
var ki = Object.keys(data);
for (var i = keys[0]; i < ki.length; ++i)
{
var kj = Object.keys(data[ki[i]]);
for (var j = keys[1]; j < kj.length; ++j)
{
var kk = Object.keys(data[ki[i]][kj[j]]);
for (var k = keys[2]; k < kk.length; ++k)
{
return [i, j, k];
}
}
}
}
function get_next_keys(data, keys)
{
var i = keys[0];
var j = keys[1];
var k = keys[2];
var ki = Object.keys(data);
var kj = Object.keys(data[ki[i]]);
var kk = Object.keys(data[ki[i]][kj[j]]);
if (k + 1 < kk.length)
return [i, j, k + 1];
if (j + 1 < kj.length)
return get_first_keys(data, [i, j+1, 0]);
if (i + 1 < ki.length)
return get_first_keys(data, [i+1, 0, 0]);
return;
}
var current_keys = get_first_keys(data, [0, 0, 0]);
function insert()
{
if (!current_keys)
return;
key = {};
key.i = Object.keys(data)[current_keys[0]];
key.j = Object.keys(data[key.i])[current_keys[1]];
key.k = Object.keys(data[key.j])[current_keys[2]];
db.data.insert(key, insert);
current_keys = get_next_keys(data, current_keys);
}
insert();
The simple answer is to do it recursively: do the next insert in the callback rather than passing an empty callback.