
Product
Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain
Socket Firewall is a free tool that blocks malicious packages at install time, giving developers proactive protection against rising supply chain attacks.
@abcnews/hash-codec
Advanced tools
A JavaScript library for encoding and decoding values to strings with validation support. This library provides various codecs and utilities for data transformation and serialization.
A JavaScript library for encoding and decoding values to strings with validation support. This library provides various codecs and utilities for data transformation and serialization.
npm install hash-codec
This library works well with @abcnews/alternating-case-to-object (ACTO), but isn't prescriptive about it.
For this reason the library takes an object as an input and returns an object as an output. Since libraries like svelte-scrollyteller automatically decode ACTO objects, we don't attempt to handle that in the main schema functionality. So you may sometimes need to use both libraries in conjunction.
import { encodeSchema, decodeSchema } from 'hash-codec';
// Define a schema with proper type definitions and keys
const schema = {
version: {
// Number types are rounded to the nearest whole number.
type: 'number',
key: 'ver'
},
name: {
// String types are free text. You must sanitise these yourself,
// especially if passing them to ACTO encode.
type: 'string',
key: 'n'
},
name: {
// String type, encoded as base36. Safe to pass to ACTO without
// any sanitisation
type: 'base36string',
key: 'n'
},
isActive: {
// Boolean type, true or false.
type: 'boolean',
key: 'ia',
defaultValue: false
},
status: {
// Enum values are translated into an integer for the matching array index.
// Be careful to keep your arrays in a consistent order, or your outputs
// will change.
type: 'enum',
key: 's',
defaultValue: 'pending',
values: ['pending', 'active', 'inactive']
},
customData: {
// A custom codec. Specify an encode and decode functino of your own to
// sanitise/encode data for this field.
type: 'custom',
key: 'cd',
codec: {
encode: (data) => customEncode(data),
decode: (encoded) => customDecode(encoded)
}
}
};
// Encode data
const encoded = encodeSchema({name: 'grug', age: 99}, schema);
// Decode data
const decoded = decodeSchema(encoded, schema);
import { getRleCodec, getBinaryCodec } from 'hash-codec';
// Run-Length Encoding
const rleDelineator = 'q';
const rleCodec = getRleCodec({ delineator: rleDelineator });
const encoded = rle.encode('aaaaaaaabaaaxyzaaaaa');
// a8qba3xyza5
const decoded = rle.decode(encoded);
// Binary encoding
const binary = getBinaryCodec();
const binaryData = binary.encode([true,false,false,false,true,true]);
const original = binary.decode(binaryData);
Codecs can be used as-is in the schema:
const schema = {
myField: {
key:'x',
type: 'custom',
codec: rleCodec,
}
}
You can write a codec of your own, as long as it has an encode() and decode() function. These may be synchronous or async.
const twoDecimalPointCodec = { encode: num => Math.round(num*100), decode: string => Number(string)/100 }
The svelte store is useful for builders and contexts where you want to sync the data to and from a hash in the URL bar. This is probably not useful as a production-facing utility.
import { makeSvelteStore } from 'hash-codec';
const store = makeSvelteStore(initialData, schema);
# Install dependencies
npm install
# Run tests
npm test
FAQs
A JavaScript library for encoding and decoding values to strings with validation support. This library provides various codecs and utilities for data transformation and serialization.
We found that @abcnews/hash-codec demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
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.
Product
Socket Firewall is a free tool that blocks malicious packages at install time, giving developers proactive protection against rising supply chain attacks.
Research
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.
Research
A malicious package uses a QR code as steganography in an innovative technique.