npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

mega-mock

v0.4.2

Published

mega-mock mocks MEGA API servers

Downloads

71

Readme

mega-mock

mega-mock mocks MEGA API servers. It's intended to be used to test MEGA clients.

Supported features

  • Logging into (version 1) MEGA accounts
  • Getting basic account info
  • Listing files from accounts
  • Listing files from shared folders
  • Uploading files
  • Downloading files
  • Creating and sharing folders
  • Updating attributes
  • Deleting files

Usage

CLI

  • Install it using npm install mega-mock (or npm install -g mega-mock)
  • Run it using npx mega-mock (or mega-mock)
  • Configure MEGA to use http://localhost:3000/ as gateway
  • Log in into mock@test with password mock
  • Use MEGA as usual
  • Stop server by using Ctrl+C

It will create a "mega-mock-data" folder. Uploaded files will be stored named as their handlers. Server state is stored in "state.json" when it stops. Temporary files, used on upload, are also stored in this folder and may remain if some error happens. Those files can be used during tests in order to check if clients tested are working as expected.

Module

Install it using npm install mega-mock then run the following:

const megamock = require('mega-mock')

const server = megamock({
  dataFolder: 'path to the data folder', // required
  state: {} // initial server state
})

// It returns a instance of http.Server
// (it uses zeit/micro internally)
server.listen(3000, '0.0.0.0')

// Current state is exposed in the `.state` property
server.state.users // a Map of uh => {files, shares}
server.state.shares // a Map of share => {handler, uh}
server.state.loginData // a Map of uh => {login data}
server.state.uploadStates // a Map of id => [file parts]

To make implementation simpler "uh" is used as the internal user identifier. Initial server state is normalized by casting data of each property using new Map.

When using the module the data folder isn't created automatically. Also no users are registered: you can register users by following the "new account registration" instructions below.

Account registration

The account login flow uses RSA encryption, so to simplify implementation the server use pre-generated data. This data can be generated by opening MEGA website and running this code in the console:

;(async function () {
  // Change those as you want
  let email = 'mock@test'
  let password = 'mock'

  const derivedKey = prepare_key_pw(password)
  const derivedAes = new sjcl.cipher.aes(derivedKey)
  const uh = stringhash(email, derivedAes)

  // Default key is "mock", you can change it
  const accountKey = [109, 111, 99, 107]
  const u_k_aes = new sjcl.cipher.aes(accountKey)

  // To keep implementation simple the uh is used as the internal user id,
  // also the server reads it from the sid when handling API requests
  const testSid = (uh + '_megamock'.repeat(4)).substr(0, 43)
  const rsakey = await new Promise(resolve => {
    const w = new Worker('/keygen.js')
    w.onmessage = function (e) {
      w.terminate()
      resolve(e.data)
    }
    const workerSeed = new Uint8Array(256)
    asmCrypto.getRandomValues(workerSeed)
    w.postMessage([2048, 257, workerSeed])
  })

  console.log('Run server.state.loginData.set(%s, %s)', JSON.stringify(uh),
    JSON.stringify({
      csid: base64urlencode(crypto_rsaencrypt(base64urldecode(testSid), rsakey)),
      privk: a32_to_base64(encrypt_key(u_k_aes, str_to_a32(crypto_encodeprivkey(rsakey)))),
      k: a32_to_base64(encrypt_key(derivedAes, accountKey))
  }))
}())

By now only version 1 accounts are supported, that's why the code above uses prepare_key_pw.