{"id":13832227,"url":"https://github.com/ewasm/wasm-metering","last_synced_at":"2025-08-22T01:32:39.604Z","repository":{"id":91809571,"uuid":"77086478","full_name":"ewasm/wasm-metering","owner":"ewasm","description":"[ORPHANED] Injects metering into webassembly binaries","archived":false,"fork":false,"pushed_at":"2022-11-09T08:12:51.000Z","size":960,"stargazers_count":68,"open_issues_count":11,"forks_count":15,"subscribers_count":13,"default_branch":"master","last_synced_at":"2024-04-23T18:54:52.267Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ewasm.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-12-21T21:13:36.000Z","updated_at":"2024-06-19T22:50:03.445Z","dependencies_parsed_at":null,"dependency_job_id":"1ea00382-8212-4d30-b8a6-3eb28b4047c5","html_url":"https://github.com/ewasm/wasm-metering","commit_stats":{"total_commits":51,"total_committers":6,"mean_commits":8.5,"dds":0.3529411764705882,"last_synced_commit":"e4c06f34513db4879d48a0b02b172a9e0677f562"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ewasm%2Fwasm-metering","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ewasm%2Fwasm-metering/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ewasm%2Fwasm-metering/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ewasm%2Fwasm-metering/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ewasm","download_url":"https://codeload.github.com/ewasm/wasm-metering/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230547678,"owners_count":18243227,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-04T10:01:56.204Z","updated_at":"2024-12-20T07:07:04.458Z","avatar_url":"https://github.com/ewasm.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# SYNOPSIS \n\n[![NPM Package](https://img.shields.io/npm/v/wasm-metering.svg?style=flat-square)](https://www.npmjs.org/package/wasm-metering)\n[![Build Status](https://img.shields.io/travis/ewasm/wasm-metering.svg?branch=master\u0026style=flat-square)](https://travis-ci.org/ewasm/wasm-metering)\n[![Coverage Status](https://img.shields.io/coveralls/ewasm/wasm-metering.svg?style=flat-square)](https://coveralls.io/r/ewasm/wasm-metering)\n\n[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)  \n\nInjects metering into webassembly binaries. The metering counts computation\ntime for a given program in units of `gas`. The metered wasm binary expects an \nimport that functions as the gas counter. This works for binary version 0x1.\nFor a more detailed description of how this works see [metering.md](https://github.com/ewasm/design/blob/master/metering.md)\n\n# INSTALL\n`npm install wasm-metering`\n\n# USAGE\n```javascript\nconst fs = require('fs')\nconst metering = require('wasm-metering')\n\nconst wasm = fs.readFileSync('fac.wasm')\nconst meteredWasm = metering.meterWASM(wasm, {\n  meterType: 'i32'\n})\n\nconst limit = 90000000\nlet gasUsed = 0\n\nconst mod = WebAssembly.Module(meteredWasm.module)\nconst instance = WebAssembly.Instance(mod, {\n  'metering': {\n    'usegas': (gas) =\u003e {\n      gasUsed += gas\n      if (gasUsed \u003e limit) {\n        throw new Error('out of gas!')\n      }\n    }\n  }\n})\n\nconst result = instance.exports.fac(6)\nconsole.log(`result:${result}, gas used ${gasUsed * 1e-4}`) // result:720, gas used 0.4177\n```\n[Source](./example/index.js)\n\n# API\n## meterJSON\n\n[./index.js:104-224](https://github.com/ewasm/wasm-metering/blob/5ab76de89bc07d0abfaa6d0c776c204a752a0d9d/./index.js#L104-L224 \"Source code on GitHub\")\n\nInjects metering into a JSON output of [wasm2json](https://github.com/ewasm/wasm-json-toolkit#wasm2json)\n\n**Parameters**\n\n-   `json` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the json tobe metered\n-   `opts` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** \n    -   `opts.costTable` **\\[[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)]** the cost table to meter with. See these notes about the default. (optional, default `defaultTable`)\n    -   `opts.moduleStr` **\\[[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)]** the import string for the metering function (optional, default `'metering'`)\n    -   `opts.fieldStr` **\\[[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)]** the field string for the metering function (optional, default `'usegas'`)\n    -   `opts.meterType` **\\[[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)]** the register type that is used to meter. Can be `i64`, `i32`, `f64`, `f32` (optional, default `'i64'`)\n\nReturns **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the metered json\n\n## meterWASM\n\n[./index.js:236-240](https://github.com/ewasm/wasm-metering/blob/5ab76de89bc07d0abfaa6d0c776c204a752a0d9d/./index.js#L236-L240 \"Source code on GitHub\")\n\nInjects metering into a webassembly binary\n\n**Parameters**\n\n-   `json` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the json tobe metered\n-   `opts` **\\[[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)](default {})** \n    -   `opts.costTable` **\\[[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)]** the cost table to meter with. See these notes about the default. (optional, default `defaultTable`)\n    -   `opts.moduleStr` **\\[[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)]** the import string for the metering function (optional, default `'metering'`)\n    -   `opts.fieldStr` **\\[[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)]** the field string for the metering function (optional, default `'usegas'`)\n    -   `opts.meterType` **\\[[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)]** the register type that is used to meter. Can be `i64`, `i32`, `f64`, `f32` (optional, default `'i64'`)\n-   `wasm`  \n\nReturns **[Buffer](https://nodejs.org/api/buffer.html)** \n\n## costTable\n\nThe costTable option defines the cost of each of the operations.\nCost Tables consist of an object whose keys are sections in a wasm binary. \nFor example\n```\nmodule.exports = {\n  'start': 1,\n  'type': {\n    'params': {\n      'DEFAULT': 1\n    },\n    'return_type': {\n      'DEFAULT': 1\n    }\n  },\n  'import': 1,\n  'code': {\n    'locals': {\n      'DEFAULT': 1\n    },\n    'code': {\n      'DEFAULT': 1\n    }\n  },\n  'memory': (entry) =\u003e {\n    return entry.maximum * 10\n  },\n  'data': 5\n}\n\n```\n\nKeys can either map to a function which will be given that section's entries or\nan integer which will be used as the cost for each entry or an object whose\nkeys are matched against the [JSON representation](https://github.com/ewasm/wasm-json-toolkit) of the code.\nThe default cost table used is from [here](https://github.com/ewasm/design/blob/master/determining_wasm_gas_costs.md)\n\nThe cost table can use a special key 'DEFAULT' that will be used as the cost value for any fields in a section that are not defined.\n\n## Initial Cost\nThe Initial cost for instantation for the module is calculated from all the \nsections other than the code section (which is metered at runtime). This information is\nstored as a [custom section](https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md#name-section)\nthat is inserted directly after the preamble. It uses the the name `initCost` and\nits payload contains the initial cost encoded as an unsigned leb128 interger.\n\n# LICENSE\n[MPL-2.0](https://tldrlegal.com/license/mozilla-public-license-2.0-(mpl-2))\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fewasm%2Fwasm-metering","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fewasm%2Fwasm-metering","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fewasm%2Fwasm-metering/lists"}