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

@nmg/k8s

v20.0.0

Published

NMG helpers for kubernetes workloads

Downloads

176

Readme

@nmg/k8s

NMG helpers for kubernetes workloads

Installation

$ npm install @nmg/k8s

Usage

Cron:

shouldRun(crontab, timezone)

Returns Boolean if crontab should run

Example
const { cron } = require('@nmg/k8s');
const crontab = '* * * * *';

cron.shouldRun(crontab) ? true : false;

Data Warehouse:

queryDataWarehouse(options, [headers])

Example
const { queryDataWarehouse } = require('@nmg/k8s');
queryDataWarehouse(
  {
    url,
    variables,
    query,
  }, 
  headers
);
Dependent Environment Variables
  • process.env.NMG_K8S_DATA_WAREHOUSE_TOKEN

Email:

sendEmail(send_email_params, [credentials])

Example
const { sendEmail } = require('@nmg/k8s');
sendEmail(
  {
    to,
    subject,
    text,
    html,
    attachments,
  }, 
  { user, password }
); // auth vaules optional
Recommended Environment Variables
  • process.env.NMG_K8S_EMAIL_USER
  • process.env.NMG_K8S_EMAIL_PASSWORD

Encryption:

Encrypt

encrypt(value)

Example
const { encrypt } = require('@nmg/k8s');
encrypt(value);
  • process.env.NMG_K8S_SECRET

Decypt

decrypt(encrypted_value)

Example
const { encrypt } = require('@nmg/k8s');
encrypt(value);
Dependent Environment Variables
  • process.env.NMG_K8S_SECRET

Image:

cropImage(input_filepath, output_filepath, [options])

Example
const { cropImage } = require('@nmg/k8s');
cropImage(
  'path/to/original.png', 
  'path/to/new.png', 
  { color_bands: false }
);

IP:

getIp([debug])

debug: Boolean - if true, will log response status code

Will return an IPv4 address or null if there is an error

Example
const ip_helper = require('@nmg/k8s');
ip_helper.getIp().then((ip) => console.log(ip));

Log:

log(log_params)

Formats a log to show up in nmg-big-data.logging.logs BigQuery table

Example
const { log } = require('@nmg/k8s');
log(
  {
    level,
    error_message,
    message,
  }
);


MongoDB:

connect()

Sets up the connection to the mongo db.

getCollection(collection_name)

Gets the collection object for the name passed in.

create(collection_name, data, account) (recommended)

Creates a collection with data and account information.

insert(collection_name, data)

Inserts raw data into collection.

update(collection_name, data, account)

Updates inserted data.

read(collection_name, options, return_cursor)

Reads from collection based on filter and returns either cursor or data.

remove(collection_name, filter, account)

Removes data from collection based on filter.

disconnect()

Closes the connection to the mongo db.

Example
const MongoDbClient = require('@nmg/k8s');
const client = new MongoDBClient(
  {
    db_name,
    connection_url,
    user,
    password,
    options,
  }
);
const db = await client.connect();
await client.disconnect();
Recommended Environment Variables
  • process.env.MONGODB_DB_NAME
  • process.env.MONGODB_CONNECTION_URL
  • process.env.MONGODB_USER
  • process.env.MONGODB_PASSWORD

Mysql Helper:

setupDB()

Sets the DB connection info and returns it

query(_query)

runs a query and returns the results

getTableStructure(table)

describes the given table

structureToSchema(structure)

writeMysqlRowsToBigQueryJsonFile(table_config, where_mysql, output_filepath, mysql_structure)

Example
const MongoDbClient = require('@nmg/k8s');
setupDB(
  {
    host,
    user,
    password,
    database,
    port,
  }
);
const description = await getTableStructure('table_name');
Recommended Environment Variables
  • process.env.MYSQL_HOST
  • process.env.MYSQL_USER
  • process.env.MYSQL_PASS
  • process.env.MYSQL_NAME
  • process.env.MYSQL_PORT

Netsuite:

netsuite.executeScript(options, [env_overrides])

Execute NetSuite RESTlets using URL

Example
const { netsuite } = require('@nmg/k8s');
const env_overrides = {}
const restlet_response = await netsuite.executeScript(
  {
    url: 'https://5000005.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=999&deploy=1',
    data: {
      saved_search_id: 'customsearch1',
    },
  },
  env_overrides
)

options will be passed to the request({ ...options }) function with the authentication defaults

Recommended Env Variables
  • process.env.NETSUITE_REALM
  • process.env.NETSUITE_CUSTOMER_KEY
  • process.env.NETSUITE_CUSTOMER_SECRET
  • process.env.NETSUITE_TOKEN_KEY
  • process.env.NETSUITE_TOKEN_SECRET

Product Search (soon to be obsolete)

class ProductSearch({ gcs_keyfile })

Example
const { ProductSearch } = require('@nmg/k8s')
const product_search = new ProductSearch({
  keyfile: path.resolve(process.env.GOOGLE_APPLICATION_CREDENTIALS),
})
await product_search.setupCache();

const searches_results = await product_search.multiProductSearch({
  searches: [
    { pn_search: 'GSS25GMHES' },
    { pn_search: 'HBLP651RUC' },
    { pn_search: 'GSS25GGHWW' },
  ],
  num_possible_results: 1,
  cutoff: 0.9,
})
Environment Variables
  • process.env.NMG_K8S_DATA_WAREHOUSE_TOKEN

Queue:

Queue(size)

Example
const { Queue } = require('@nmg/k8s');
const queue = new Queue(10);

for (const page of response.pageRanges) {
  const _page = page;
  console.log(`QUEUE - ${_page.index}`);
  queue.push(
    async () => {
      console.log(`RUN - ${_page.index}`);
      return executeScript({
        ...options,
        page_index: _page.index,
      }).then((page_results) => {
        console.log(`RETURNED - ${_page.index}`)
        results.push(...page_results)
      });
    }
  );
}
queue.start()
await new Promise((resolve, reject) => {
  const resolveIfEmpty = () => {
    if (queue.isEmpty()) {
      resolve();
    }
    else {
      setTimeout(resolveIfEmpty, 100)
    }
  }
  resolveIfEmpty();
})
queue.stop();
}

Rate Limiting:

RateLimit(options)

Example
const { RateLimit } = require('@nmg/k8s');

const rate_limiter = new RateLimiter({
  shouldRequeueOnError(error) {
    if (error.message === 'THIS NEEDS EXPONENTIAL BACKOFF') {
      rate_limiter.exponentiallyBackoff()
    }
    return /(Quota|exponential)/ig.test(error.message)
  }
});
rate_limiter.on('request_processed', () => {
  rate_limiter.resetExponentialBackoff(); //always reset
  console.log(`REMAINING REQUESTS - ${rate_limiter.requests.length}`)
});
rate_limiter.queueRequest(
  async () => {
    return Promise.resolve();
  }
);
rate_limiter.startProcessing();
await new Promise((resolve) => {
  rate_limiter.once('queue_empty', resolve));
}

options

limits = [
  // limit to 3 requests for 1000ms
  {
    type: 'RATE_LIMIT',
    requests: 3,
    requests_timespan: 1000, // milliseconds
  },
  // AND limit to 1000 requests for 100000ms
  {
    type: 'RATE_LIMIT',
    requests: 1000,
    requests_timespan: 100000, // milliseconds
  },
  // AND limit to 5 concurrent requests
  {
    type: 'CONCURRENCY',
    requests: 5,
  },
],
requests = [], // intialize with requests
log = false,
shouldRequeueOnError = null, // return true to retry

constants

returns a list of constants used in NMG projects

Example
const { nmg_constants } = require('@nmg/k8s');

//gets the collection name for member_sale_outs
nmg_constants.collections.member_sale_outs;

Tests

The Jest testing framework has been implemented. To run these tests run: npm run test

Contributing

Automated deploy is set up on the pipelines/npm branch