Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InternalFieldCount assertion error #131

Closed
john30 opened this issue Jun 25, 2018 · 5 comments
Closed

InternalFieldCount assertion error #131

john30 opened this issue Jun 25, 2018 · 5 comments

Comments

@john30
Copy link

john30 commented Jun 25, 2018

when using any of time or strings.time from https://github.com/JCMais/node-libcurl/blob/develop/api.md#node-libcurlcurlfileinfo--object during e.g. CHUNK_BGN_FUNCTION, an assertion error is thrown under Windows showing a MSVC++ RT lib dialog with this content:

Assertion failed!

Program: ...modules\node-libcurl\lib\binding\node_libcurl.node
File: ...\nan_obj..._wrap.h
Line: 33
Expression: object->InternalFieldCount() > 0

btw: running node 9.4

@JCMais
Copy link
Owner

JCMais commented Jun 27, 2018

Will look into that. Thanks for reporting.

@JCMais JCMais added the bug label Jun 27, 2018
@john30
Copy link
Author

john30 commented Jul 1, 2018

actually this seems to be problematic only with the pre-built gyp files. it does not occur after a build from source (guess the assertions are skipped in release build?)

@JCMais
Copy link
Owner

JCMais commented Aug 13, 2018

The assert are still there, do you remember the node.js version you were using?

If you could share a piece of code that has this problem it would be really great, I could not reproduce it on my end.

@john30
Copy link
Author

john30 commented Sep 26, 2018

as mentioned in the post:

btw: running node 9.4

@JCMais
Copy link
Owner

JCMais commented May 20, 2019

Working on prerelease of v2 node-libcurl@next

Sample code:

/**
 * Example showing how to download files from a FTP server
 * using custom wildcard pattern matching
 * Based on the ftp-wildcard example
 */
const path = require('path')
const util = require('util')
const fs = require('fs')

const {
  Curl,
  CurlChunk,
  CurlFileType,
  CurlFnMatchFunc,
  Easy,
} = require('../dist')

// Using the Easy interface because currently there is an issue
//  when using libcurl with wildcard matching on the multi interface
//  https://github.com/curl/curl/issues/800
const handle = new Easy()
const url = 'ftp://speedtest.tele2.net/*.zip'

// object to be used to share data between callbacks
const data = {
  output: null,
}

handle.setOpt(Curl.option.URL, url)
handle.setOpt(Curl.option.VERBOSE, 1)
handle.setOpt(Curl.option.WILDCARDMATCH, true)
handle.setOpt(Curl.option.FNMATCH_FUNCTION, fnMatch)
handle.setOpt(Curl.option.CHUNK_BGN_FUNCTION, fileIsComing)
handle.setOpt(Curl.option.CHUNK_END_FUNCTION, filesIsDownloaded)

handle.setOpt(Curl.option.WRITEFUNCTION, (buff, nmemb, size) => {
  let written = 0

  if (data.output) {
    written = fs.writeSync(data.output, buff, 0, nmemb * size)
  } else {
    /* listing output */
    process.stdout.write(buff.toString())
    written = size * nmemb
  }

  return written
})

// Functions globStringToRegex and pregQuote from: http://stackoverflow.com/a/13818704/710693

function globStringToRegex(str) {
  return new RegExp(
    pregQuote(str)
      .replace(/\\\*/g, '.*')
      .replace(/\\\?/g, '.'),
    'g',
  )
}

function pregQuote(str, delimiter) {
  // http://kevin.vanzonneveld.net
  // +   original by: booeyOH
  // +   improved by: Ates Goral (http://magnetiq.com)
  // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // +   bugfixed by: Onno Marsman
  // +   improved by: Brett Zamir (http://brett-zamir.me)
  // *     example 1: preg_quote("$40");
  // *     returns 1: '\$40'
  // *     example 2: preg_quote("*RRRING* Hello?");
  // *     returns 2: '\*RRRING\* Hello\?'
  // *     example 3: preg_quote("\\.+*?[^]$(){}=!<>|:");
  // *     returns 3: '\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:'
  return (str + '').replace(
    new RegExp(
      '[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + (delimiter || '') + '-]',
      'g',
    ),
    '\\$&',
  )
}

/**
 * Use our own logic to make the wildcard matching.
 *
 * Here we are just changing from the default libcurl logic
 *  to use one equivalent using javascript RegExp.
 *
 * @param {String} pattern
 * @param {String} string
 * @returns {number}
 */
function fnMatch(pattern, string) {
  const regex = new RegExp(globStringToRegex(pattern), 'g')

  return string.match(regex) ? CurlFnMatchFunc.Match : CurlFnMatchFunc.NoMatch
}

/**
 * @param {module:node-libcurl~CurlFileInfo} fileInfo
 * @param {Number} remains Number of entries remaining
 * @returns {Number}
 */
function fileIsComing(fileInfo, remains) {
  process.stdout.write(
    util.format(
      'Remaining entries: %d / Current: %s / Size: %d - ',
      remains,
      fileInfo.fileName,
      fileInfo.size,
    ),
  )

  console.log('fileInfo object:', { fileInfo })

  switch (fileInfo.fileType) {
    case CurlFileType.Directory:
      console.log(' DIR')
      break
    case CurlFileType.File:
      console.log(' FILE')
      break
    default:
      console.log(' OTHER')
      break
  }

  if (fileInfo.fileType === CurlFileType.File) {
    /* do not transfer files > 1MB */
    if (fileInfo.size > 1024 * 1024) {
      console.log('SKIPPED')
      return CurlChunk.BgnFuncSkip
    }

    data.output = fs.openSync(path.join(process.cwd(), fileInfo.fileName), 'w+')

    if (!data.output) {
      return CurlChunk.BgnFuncFail
    }
  } else {
    console.log('SKIPPED')
    return CurlChunk.BgnFuncSkip
  }

  return CurlChunk.BgnFuncOk
}

function filesIsDownloaded() {
  if (data.output) {
    console.log('DOWNLOADED')
    fs.closeSync(data.output)
    data.output = null
  }

  return CurlChunk.EndFuncOk
}

handle.perform()

@JCMais JCMais closed this as completed May 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants