Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mpwrapper

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mpwrapper

Monkey Patch Wrapper that support multiple wrapping and unwrapping

  • 0.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-78.57%
Maintainers
1
Weekly downloads
 
Created
Source

mpwrapper - Monkey Patch Wrapper

NPM Published Version Apache License

Monkey Patch Wrapper that support multiple wrapping and unwrapping.

Installation

npm install --save mpwrapper

Usage in node.js

const https = require('https');
const mpWrapper = require('mpwrapper');
const wrapper1 = mpWrapper.wrap(https, 'get', (original) => {
  return function () {
    console.log('before request 1');
    const result = original.apply(this, arguments);
    console.log('after request 1');
    return result;
  };
});

const wrapper2 = mpWrapper.wrap(https, 'get', (original) => {
  return function () {
    console.log('before request 2');
    const result = original.apply(this, arguments);
    console.log('after request 2');
    return result;
  };
});

function getData(callback) {
  const url = 'https://raw.githubusercontent.com/obecny/mpwrapper/master/package.json';
  https.get(url, (response) => {
    let data = '';
    response.on('data', (chunk) => {
      data += chunk;
    });
    response.on('end', () => {
      const info = JSON.parse(data);
      console.log(`${info.name} - ${info.version}`);
      callback();
    });
  });
}

getData(()=> {
  wrapper1.unwrap();
  getData(()=> {
    wrapper2.unwrap();
    getData(()=> {
      console.log('done');
    });
  });
});

// before request 1
// before request 2
// after request 2
// after request 1
// mpwrapper - 0.1.0
// before request 2
// after request 2
// mpwrapper - 0.1.0
// mpwrapper - 0.1.0
// done

Usage in browser

import * as mpWrapper from 'mpwrapper';
const wrapper1 = mpWrapper.wrap(XMLHttpRequest.prototype, 'open', (original) => {
  return function () {
    console.log('before request 1');
    const result = original.apply(this, arguments);
    console.log('after request 1');
    return result;
  };
});

const wrapper2 = mpWrapper.wrap(XMLHttpRequest.prototype, 'open', (original) => {
  return function () {
    console.log('before request 2');
    const result = original.apply(this, arguments);
    console.log('after request 2');
    return result;
  };
});

function getData(callback) {
  const url = 'https://raw.githubusercontent.com/obecny/mpwrapper/master/package.json';
  const req = new XMLHttpRequest();
  req.open('GET', url, true);
  req.send();
  req.onload = function () {
    const info = JSON.parse(req.responseText);
    console.log(`${info.name} - ${info.version}`);
    callback();
  };
}

getData(()=> {
  wrapper1.unwrap();
  getData(()=> {
    wrapper2.unwrap();
    getData(()=> {
      console.log('done');
    });
  });
});

// before request 1
// before request 2
// after request 2
// after request 1
// mpwrapper - 0.1.0
// before request 2
// after request 2
// mpwrapper - 0.1.0
// mpwrapper - 0.1.0
// done

License

Apache 2.0 - See LICENSE for more information.

Keywords

FAQs

Package last updated on 19 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc