How to use ffi-napi - 10 common examples

To help you get started, we’ve selected a few ffi-napi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github lmangani / nDPIex / nodejs / main.js View on Github external
console.error("usage: node pcap.js /path/to/file.pcap");
    console.error();
    process.exit();
}

/* NDPI Types */

	var voidPtr = exports.voidPtr = ref.refType(ref.types.void);
	var u_char = exports.u_char = Struct({
	  __u_char: ref.types.uchar,
	});
	var u_charPtr = exports.u_charPtr = ref.refType(u_char);
	
	var pcap_t = exports.pcap_t = voidPtr;
	var pcap_tPtr = exports.pcap_tPtr = ref.refType(pcap_t);
	var pcap_handler = exports.pcap_handler = ffi.Function(ref.types.void, [
	  ref.refType(ref.types.uchar),
	  voidPtr,
	  ref.refType(ref.types.uchar),
	]);
	var pcap_handlerPtr = exports.pcap_handlerPtr = ref.refType(pcap_handler);
	
	var uint8_t = exports.uint8_t = voidPtr;
	var uint8_tPtr = exports.uint8_tPtr = ref.refType(uint8_t);


/* callback */ 
	const onProto = function(id, packet) {
		if (id > 0) { console.log("Proto: "+packet+" ("+id+")") }
	}

	var callback = exports.callback = ffi.Function(ref.types.void, [
github lmangani / nDPIex / nodejs / main.js View on Github external
ref.refType(ref.types.uchar),
	  voidPtr,
	  ref.refType(ref.types.uchar),
	]);
	var pcap_handlerPtr = exports.pcap_handlerPtr = ref.refType(pcap_handler);
	
	var uint8_t = exports.uint8_t = voidPtr;
	var uint8_tPtr = exports.uint8_tPtr = ref.refType(uint8_t);


/* callback */ 
	const onProto = function(id, packet) {
		if (id > 0) { console.log("Proto: "+packet+" ("+id+")") }
	}

	var callback = exports.callback = ffi.Function(ref.types.void, [
	  ref.types.int32,
	  ref.refType(uint8_t),
	]);

	var ndpi = ffi.Library('../ndpiexlib.so', {
	  init: [ref.types.void, [
	  ]],
	  setDatalinkType: [ref.types.void, [
	      pcap_tPtr,
	  ]],
	  processPacket: [ref.types.void, [
	    voidPtr,
	    uint8_t,
	  ]],
	  finish: [ref.types.void, [
	  ]],
github lmangani / nDPIex / nodebeat / nodebeat.js View on Github external
]);
	var pcap_handlerPtr = exports.pcap_handlerPtr = ref.refType(pcap_handler);
	
	// PCAP Header
	var pcap_pkthdr = exports.pcap_pkthdr = Struct({
	  'ts_sec': 'long', 
	  'ts_usec': 'long',
	  'incl_len': 'int',
	  'orig_len': 'int'
	});
	
	var pktHdr = exports.pktHdr = new pcap_pkthdr;
	pktHdr = ref.refType(ref.types.void);

	/* NDPI Hook */
	var ndpi = exports.ndpi = new ffi.Library('../ndpiexlib.so', {
	  init: [ref.types.void, [
	  ]],
	  getResults: [ref.types.void, [
	  ]],
	  setDatalinkType: [ref.types.void, [
	      pcap_tPtr,
	  ]],
	  processPacket: [ref.types.void, [
	    voidPtr,
	    uint8_t,
	  ]],
	  finish: [ref.types.void, [
	  ]],
	  addProtocolHandler: [ref.types.void, [
	    callback
	  ]],
github Razviar / mtgap / src / our-active-win / lib / windows.js View on Github external
// Create the struct required to save the window bounds
const Rect = struct({
  left: 'long',
  top: 'long',
  right: 'long',
  bottom: 'long',
});
const RectPointer = refType(Rect);

// Required by QueryFullProcessImageName
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms684880(v=vs.85).aspx
const PROCESS_QUERY_LIMITED_INFORMATION = 0x1000;

// Create FFI declarations for the C++ library and functions needed (User32.dll), using their "Unicode" (UTF-16) version
const user32 = new Library('User32.dll', {
  // https://msdn.microsoft.com/en-us/library/windows/desktop/ms633505(v=vs.85).aspx
  GetForegroundWindow: ['pointer', []],
  // https://msdn.microsoft.com/en-us/library/windows/desktop/ms633522(v=vs.85).aspx
  GetWindowThreadProcessId: ['uint32', ['pointer', 'uint32 *']],
  // Get window bounds function
  // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getwindowrect
  GetWindowRect: ['bool', ['pointer', RectPointer]],
});

function windows() {
  // Windows C++ APIs' functions are declared with capitals, so this rule has to be turned off

  // Get a "handle" of the active window
  const activeWindowHandle = user32.GetForegroundWindow();

  if (isNull(activeWindowHandle)) {
github airgram / airgram / packages / airgram / src / components / TdProxy.ts View on Github external
public constructor ({ command }: { command?: string }) {
    // @formatter:off
    this.client = ffi.Library(
      resolvePath(command || PATH_TO_LIBRARY_FILE),
      {
        td_json_client_create: ['pointer', []],
        td_json_client_send: ['void', ['pointer', 'string']],
        td_json_client_receive: ['string', ['pointer', 'double']],
        td_json_client_execute: ['string', ['pointer', 'string']],
        td_json_client_destroy: ['void', ['pointer']]
      })
    // @formatter:on
  }
github sindresorhus / active-win / lib / windows.js View on Github external
// Create the struct required to save the window bounds
const Rect = struct({
	left: 'long',
	top: 'long',
	right: 'long',
	bottom: 'long'
});
const RectPointer = ref.refType(Rect);

// Required by QueryFullProcessImageName
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms684880(v=vs.85).aspx
const PROCESS_QUERY_LIMITED_INFORMATION = 0x1000;

// Create FFI declarations for the C++ library and functions needed (User32.dll), using their "Unicode" (UTF-16) version
const user32 = new ffi.Library('User32.dll', {
	// https://msdn.microsoft.com/en-us/library/windows/desktop/ms633505(v=vs.85).aspx
	GetForegroundWindow: ['pointer', []],
	// https://msdn.microsoft.com/en-us/library/windows/desktop/ms633520(v=vs.85).aspx
	GetWindowTextW: ['int', ['pointer', 'pointer', 'int']],
	// https://msdn.microsoft.com/en-us/library/windows/desktop/ms633521(v=vs.85).aspx
	GetWindowTextLengthW: ['int', ['pointer']],
	// https://msdn.microsoft.com/en-us/library/windows/desktop/ms633522(v=vs.85).aspx
	GetWindowThreadProcessId: ['uint32', ['pointer', 'uint32 *']],
	// Get window bounds function
	// https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getwindowrect
	GetWindowRect: ['bool', ['pointer', RectPointer]]
});

const SIZE_T = 'uint64';

// https://docs.microsoft.com/en-us/windows/desktop/api/psapi/ns-psapi-_process_memory_counters
github lmangani / nDPIex / nodebeat / nodebeat.js View on Github external
/* APP VARS */

	var voidPtr = exports.voidPtr = ref.refType(ref.types.void);
	var u_char = exports.u_char = Struct({
	  __u_char: ref.types.uchar,
	});
	var u_charPtr = exports.u_charPtr = ref.refType(u_char);
	
	var uint8_t = exports.uint8_t = voidPtr;
	var uint8_tPtr = exports.uint8_tPtr = ref.refType(uint8_t);
	
	// var callbackPtr = ffi.Callback(ref.types.void, [ ref.types.int32, ref.refType(ref.types.uchar) ], onProto);
	// var callbackF = ffi.ForeignFunction(callbackPtr, ref.types.void, [ ref.types.int32, ref.refType(ref.types.uchar) ]);

	var callback = exports.callback = ffi.Function(ref.types.void, [
	  ref.types.int32,
	  ref.refType(ref.types.uchar),
	]);
		
	var pcap_t = exports.pcap_t = voidPtr;
	var pcap_tPtr = exports.pcap_tPtr = ref.refType(pcap_t);
	var pcap_handler = exports.pcap_handler = ffi.Function(ref.types.void, [
	  ref.refType(ref.types.uchar),
	  voidPtr,
	  ref.refType(ref.types.uchar),
	]);
	var pcap_handlerPtr = exports.pcap_handlerPtr = ref.refType(pcap_handler);
	
	// PCAP Header
	var pcap_pkthdr = exports.pcap_pkthdr = Struct({
	  'ts_sec': 'long',
github lmangani / nDPIex / nodebeat / nodebeat.js View on Github external
var u_charPtr = exports.u_charPtr = ref.refType(u_char);
	
	var uint8_t = exports.uint8_t = voidPtr;
	var uint8_tPtr = exports.uint8_tPtr = ref.refType(uint8_t);
	
	// var callbackPtr = ffi.Callback(ref.types.void, [ ref.types.int32, ref.refType(ref.types.uchar) ], onProto);
	// var callbackF = ffi.ForeignFunction(callbackPtr, ref.types.void, [ ref.types.int32, ref.refType(ref.types.uchar) ]);

	var callback = exports.callback = ffi.Function(ref.types.void, [
	  ref.types.int32,
	  ref.refType(ref.types.uchar),
	]);
		
	var pcap_t = exports.pcap_t = voidPtr;
	var pcap_tPtr = exports.pcap_tPtr = ref.refType(pcap_t);
	var pcap_handler = exports.pcap_handler = ffi.Function(ref.types.void, [
	  ref.refType(ref.types.uchar),
	  voidPtr,
	  ref.refType(ref.types.uchar),
	]);
	var pcap_handlerPtr = exports.pcap_handlerPtr = ref.refType(pcap_handler);
	
	// PCAP Header
	var pcap_pkthdr = exports.pcap_pkthdr = Struct({
	  'ts_sec': 'long', 
	  'ts_usec': 'long',
	  'incl_len': 'int',
	  'orig_len': 'int'
	});
	
	var pktHdr = exports.pktHdr = new pcap_pkthdr;
	pktHdr = ref.refType(ref.types.void);
github waitingsong / node-win32-api / packages / win32-api / src / lib / helper.ts View on Github external
settings?: FModel.LoadSettings,
): T {

  const st = parse_settings(settings)

  if (st && st.singleton) {
    let inst = get_inst_by_name(dllName)

    if (! inst) {
      inst = ffi.Library(dllName, gen_api_opts(dllFuncs, fns)) as T
      set_inst_by_name(dllName, inst)
    }
    return inst
  }
  else {
    return ffi.Library(dllName, gen_api_opts(dllFuncs, fns))
  }
}
github waitingsong / node-win32-api / packages / win32-api / src / lib / helper.ts View on Github external
export function load(
  dllName: string,
  dllFuncs: FModel.DllFuncs,
  fns?: FModel.FnName[],
  settings?: FModel.LoadSettings,
): T {

  const st = parse_settings(settings)

  if (st && st.singleton) {
    let inst = get_inst_by_name(dllName)

    if (! inst) {
      inst = ffi.Library(dllName, gen_api_opts(dllFuncs, fns)) as T
      set_inst_by_name(dllName, inst)
    }
    return inst
  }
  else {
    return ffi.Library(dllName, gen_api_opts(dllFuncs, fns))
  }
}

ffi-napi

A foreign function interface (FFI) for Node.js, N-API style

MIT
Latest version published 4 years ago

Package Health Score

56 / 100
Full package analysis