@spacemesh/svm-codec
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -1,37 +0,24 @@ | ||
export namespace svmCodec { | ||
export { isInitialized }; | ||
export { init }; | ||
export { call }; | ||
export { wasmBufferAlloc }; | ||
export { wasmBufferFree }; | ||
export { wasmBufferLength }; | ||
export { wasmBufferDataSlice }; | ||
export { loadWasmBufferDataAsString }; | ||
export { loadWasmBufferError }; | ||
export { loadWasmBuffer }; | ||
export { copyToWasmBufferData }; | ||
export { newWasmBuffer }; | ||
export { loadWasmBufferDataAsJson }; | ||
export { encodeInput }; | ||
export { decodeInput }; | ||
export { OK_MARKER }; | ||
export { ERR_MARKER }; | ||
export declare const OK_MARKER = 1; | ||
export declare const ERR_MARKER = 0; | ||
export declare function init(code: BufferSource): Promise<void>; | ||
export declare function isInitialized(): boolean; | ||
export interface Spawn { | ||
version: number; | ||
template: string; | ||
name: string; | ||
ctor_name: string; | ||
calldata: Uint8Array; | ||
} | ||
declare function isInitialized(): boolean; | ||
declare function init(code: any): Promise<void>; | ||
declare function call(funcName: any, buf: any): any; | ||
declare function wasmBufferAlloc(length: any): any; | ||
declare function wasmBufferFree(buf: any): any; | ||
declare function wasmBufferLength(buf: any): any; | ||
declare function wasmBufferDataSlice(buf: any, offset: any, length: any): Uint8Array; | ||
declare function loadWasmBufferDataAsString(buf: any): string; | ||
declare function loadWasmBufferError(buf: any): string; | ||
declare function loadWasmBuffer(buf: any): any; | ||
declare function copyToWasmBufferData(buf: any, data: any): void; | ||
declare function newWasmBuffer(object: any): any; | ||
declare function loadWasmBufferDataAsJson(buf: any): any; | ||
declare function encodeInput(object: any): any; | ||
declare function decodeInput(encodedData: any): any; | ||
declare const OK_MARKER: 1; | ||
declare const ERR_MARKER: 0; | ||
export {}; | ||
export interface Call { | ||
version: number; | ||
target: string; | ||
func_name: string; | ||
verifydata: Uint8Array; | ||
calldata: Uint8Array; | ||
} | ||
export declare function encodeSpawn(data: Spawn): Uint8Array; | ||
export declare function decodeSpawn(data: Uint8Array): JSON; | ||
export declare function encodeCall(data: Call): Uint8Array; | ||
export declare function decodeCall(bytes: Uint8Array): JSON; | ||
export declare function encodeInput(object: any): JSON; | ||
export declare function decodeInput(encodedData: any): JSON; |
"use strict"; | ||
const OK_MARKER = 1; | ||
const ERR_MARKER = 0; | ||
let codec = null; | ||
const assert = require("assert"); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.decodeInput = exports.encodeInput = exports.decodeCall = exports.encodeCall = exports.decodeSpawn = exports.encodeSpawn = exports.isInitialized = exports.init = exports.ERR_MARKER = exports.OK_MARKER = void 0; | ||
exports.OK_MARKER = 1; | ||
exports.ERR_MARKER = 0; | ||
let codec; | ||
// Init the library with the binary content of a .wasm file. | ||
@@ -12,5 +13,98 @@ // This function must be called once per client prior to use of any other package functions. | ||
} | ||
exports.init = init; | ||
// Returns true iff library was initialized with Wasm code | ||
function isInitialized() { | ||
return (codec !== null); | ||
} | ||
exports.isInitialized = isInitialized; | ||
// Encodes the provided spawn app data | ||
function encodeSpawn(data) { | ||
const buf = newWasmBuffer(data); | ||
const result = call("wasm_encode_spawn", buf); | ||
const len = wasmBufferLength(result); | ||
const slice = wasmBufferDataSlice(result, 0, len); | ||
let errMessage = ""; | ||
if (slice[0] !== exports.OK_MARKER) { | ||
errMessage = loadWasmBufferError(result); | ||
} | ||
wasmBufferFree(buf); | ||
wasmBufferFree(result); | ||
if (errMessage.length > 0) { | ||
throw errMessage; | ||
} | ||
return slice.slice(1); | ||
} | ||
exports.encodeSpawn = encodeSpawn; | ||
// Decodes the provided svm encoded spwan data | ||
function decodeSpawn(data) { | ||
const buf = newWasmBuffer({ data: binToString(data) }); | ||
const result = call("wasm_decode_spawn", buf); | ||
const json = loadWasmBufferDataAsJson(result); | ||
wasmBufferFree(buf); | ||
wasmBufferFree(result); | ||
return json; | ||
} | ||
exports.decodeSpawn = decodeSpawn; | ||
// Encodes the provided call account data | ||
function encodeCall(data) { | ||
const buf = newWasmBuffer(data); | ||
const result = call("wasm_encode_call", buf); | ||
const len = wasmBufferLength(result); | ||
const slice = wasmBufferDataSlice(result, 0, len); | ||
let errMessage = ""; | ||
if (slice[0] !== exports.OK_MARKER) { | ||
errMessage = loadWasmBufferError(result); | ||
} | ||
wasmBufferFree(buf); | ||
wasmBufferFree(result); | ||
if (errMessage.length > 0) { | ||
throw errMessage; | ||
} | ||
return slice.slice(1); | ||
} | ||
exports.encodeCall = encodeCall; | ||
// Decodes the svm encoded call account data | ||
function decodeCall(bytes) { | ||
const buf = newWasmBuffer({ data: binToString(bytes) }); | ||
const result = call("wasm_decode_call", buf); | ||
const json = loadWasmBufferDataAsJson(result); | ||
wasmBufferFree(buf); | ||
wasmBufferFree(result); | ||
return json; | ||
} | ||
exports.decodeCall = decodeCall; | ||
// Encodes data provided in object and returns the encoded data in a json object | ||
function encodeInput(object) { | ||
const buf = newWasmBuffer(object); | ||
const result = call("wasm_encode_inputdata", buf); | ||
const encoded = loadWasmBufferDataAsJson(result); | ||
wasmBufferFree(buf); | ||
wasmBufferFree(result); | ||
return encoded; | ||
} | ||
exports.encodeInput = encodeInput; | ||
// Decode svm data provided in encodedData value and returns a json object of the data | ||
function decodeInput(encodedData) { | ||
const buf = newWasmBuffer(encodedData); | ||
const result = call("wasm_decode_inputdata", buf); | ||
const json = loadWasmBufferDataAsJson(result); | ||
wasmBufferFree(buf); | ||
wasmBufferFree(result); | ||
return json; | ||
} | ||
exports.decodeInput = decodeInput; | ||
// Encodes binary provided binary data as a hex binary string (without an 0x prefix) | ||
function binToString(array) { | ||
let result = ""; | ||
for (const b of array) { | ||
let s = b.toString(16); | ||
// padding | ||
if (s.length < 2) { | ||
s = "0" + s; | ||
} | ||
result += s; | ||
} | ||
return result; | ||
} | ||
///// Internal help functions below | ||
// Call an svm_codec function with the provided buffer. Returns result buffer. | ||
@@ -38,5 +132,5 @@ function call(funcName, buf) { | ||
function copyToWasmBufferData(buf, data) { | ||
let ptr = wasmBufferDataPtr(buf); | ||
let memory = codec.exports.memory.buffer; | ||
let view = new Uint8Array(memory); | ||
const ptr = wasmBufferDataPtr(buf); | ||
const memory = codec.exports.memory.buffer; | ||
const view = new Uint8Array(memory); | ||
view.set([...data], ptr); | ||
@@ -46,3 +140,3 @@ } | ||
function wasmBufferDataSlice(buf, offset, length) { | ||
let ptr = wasmBufferDataPtr(buf); | ||
const ptr = wasmBufferDataPtr(buf); | ||
const memory = codec.exports.memory.buffer; | ||
@@ -58,18 +152,15 @@ const view = new Uint8Array(memory); | ||
const buf = wasmBufferAlloc(bytes.length); | ||
assert.strictEqual(bytes.length, wasmBufferLength(buf)); | ||
if (bytes.length !== wasmBufferLength(buf)) { | ||
throw new Error("svm codec error: unexpected buf length"); | ||
} | ||
copyToWasmBufferData(buf, bytes); | ||
return buf; | ||
} | ||
// Returns a json object of a provided wasm buffer | ||
function loadWasmBuffer(buf) { | ||
let length = wasmBufferLength(buf); | ||
const slice = wasmBufferDataSlice(buf, 0, length); | ||
const string = new TextDecoder().decode(slice); | ||
return JSON.parse(string); | ||
} | ||
// Returns a utf-8 string representation of the data in an svm_codec buffer | ||
function loadWasmBufferDataAsString(buf) { | ||
let length = wasmBufferLength(buf); | ||
const length = wasmBufferLength(buf); | ||
const slice = wasmBufferDataSlice(buf, 0, length); | ||
assert.strictEqual(slice[0], OK_MARKER); | ||
if (slice[0] !== exports.OK_MARKER) { | ||
throw loadWasmBufferError(buf); | ||
} | ||
return new TextDecoder().decode(slice.slice(1)); | ||
@@ -80,10 +171,7 @@ } | ||
function loadWasmBufferDataAsJson(buf) { | ||
let length = wasmBufferLength(buf); | ||
const length = wasmBufferLength(buf); | ||
const slice = wasmBufferDataSlice(buf, 0, length); | ||
if (slice[0] === ERR_MARKER) { | ||
const msg = loadWasmBufferError(buf); | ||
console.warn(msg); | ||
throw msg; | ||
if (slice[0] === exports.ERR_MARKER) { | ||
throw loadWasmBufferError(buf); | ||
} | ||
assert.strictEqual(slice[0], OK_MARKER); | ||
const string = loadWasmBufferDataAsString(buf); | ||
@@ -94,44 +182,9 @@ return JSON.parse(string); | ||
function loadWasmBufferError(buf) { | ||
let length = wasmBufferLength(buf); | ||
const length = wasmBufferLength(buf); | ||
const slice = wasmBufferDataSlice(buf, 0, length); | ||
// assert.strictEqual(slice[0], ERR_MARKER); | ||
if (slice[0] !== exports.ERR_MARKER) { | ||
return "wasm buffer doesn't have an error."; | ||
} | ||
return new TextDecoder().decode(slice.slice(1)); | ||
} | ||
// Encodes data provided in josn object and returns the encoded data in a json object | ||
function encodeInput(object) { | ||
const buf = newWasmBuffer(object); | ||
const result = call("wasm_encode_inputdata", buf); | ||
const encoded = loadWasmBufferDataAsJson(result); | ||
wasmBufferFree(buf); | ||
wasmBufferFree(result); | ||
return encoded; | ||
} | ||
// Decode svm data provided in encodedData json object and returns a json object of the decoded data | ||
function decodeInput(encodedData) { | ||
const buf = newWasmBuffer(encodedData); | ||
const result = call("wasm_decode_inputdata", buf); | ||
const json = loadWasmBufferDataAsJson(result); | ||
wasmBufferFree(buf); | ||
wasmBufferFree(result); | ||
return json; | ||
} | ||
module.exports.svmCodec = { | ||
isInitialized, | ||
init, | ||
call, | ||
wasmBufferAlloc, | ||
wasmBufferFree, | ||
wasmBufferLength, | ||
wasmBufferDataSlice, | ||
loadWasmBufferDataAsString, | ||
loadWasmBufferError, | ||
loadWasmBuffer, | ||
copyToWasmBufferData, | ||
newWasmBuffer, | ||
loadWasmBufferDataAsJson, | ||
encodeInput, | ||
decodeInput, | ||
OK_MARKER, | ||
ERR_MARKER | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@spacemesh/svm-codec", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"collaborators": [ | ||
@@ -48,2 +48,3 @@ "Aviv Eyal <aviveyal07@gmail.com>" | ||
"eslint-plugin-jest": "^24.5.0", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"jest": "^27.2.4", | ||
@@ -50,0 +51,0 @@ "ts-jest": "^27.0.5", |
## svm-codec npm package | ||
A public npm package providing the Spacemesh svm-codec for browser and node.js apps. | ||
A [public npm package](https://www.npmjs.com/package/@spacemesh/svm-codec) providing the Spacemesh svm-codec for browser and node.js apps. | ||
@@ -31,10 +31,11 @@ ### Setup | ||
```TypeScript | ||
import {svmCodec} from "@spacemesh/svm-codec" | ||
import fs from 'fs'; | ||
import Path from 'path'; | ||
const fs = require('fs'); | ||
const Path = require('path'); | ||
import * as SvmCodec from '@spacemesh/svm-codec'; | ||
const path = Path.resolve(__dirname, 'svm_codec.wasm'); | ||
const code = fs.readFileSync(path); | ||
await svmCodec.init(code); | ||
await SvmCodec.init(code); | ||
@@ -50,5 +51,6 @@ // call svmCodec functions... | ||
```TypeScript | ||
import {svmCodec} from "@spacemesh/svm-codec"; | ||
import * as SvmCodec from '@spacemesh/svm-codec'; | ||
const code = .... // load data from svm_codec.wasm here... | ||
await svmCodec.init(code); | ||
await SvmCodec.init(code); | ||
@@ -60,5 +62,3 @@ // call svmCodec functions.... | ||
### Known Issues | ||
- No CI yet. | ||
- No integration with svm-codec releases is implemented yet. | ||
- No tight types in Typescript typings yet. | ||
- integration with svm-codec releases is NYI. | ||
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17317
207
11
1