
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
sourcemap-codec
Advanced tools
The sourcemap-codec npm package is designed for encoding and decoding mappings between original and generated code as represented in a source map. It provides efficient operations for working with the VLQ (Variable-Length Quantity) encoding used in source maps, making it easier to manipulate and understand the relationships between source and transformed code.
Encoding source map mappings
This feature allows you to encode an array of source map mappings into a compact string using VLQ encoding. The example demonstrates encoding mappings for two lines of code.
const { encode } = require('sourcemap-codec');
const mappings = [
[ [ 0, 0, 0, 0 ], [ 1, 0, 1, 17 ] ], // First line mappings
[ [ 0, 0, 1, 0 ] ] // Second line mappings
];
const encodedMappings = encode(mappings);
Decoding source map mappings
This feature enables the decoding of a VLQ-encoded string representing source map mappings back into a more understandable array format. The example shows how to decode a string into mappings for two lines.
const { decode } = require('sourcemap-codec');
const encodedMappings = 'AAAA,CAAC;AACA';
const mappings = decode(encodedMappings);
The source-map package provides functionalities to generate and consume source maps. It offers a more comprehensive set of features compared to sourcemap-codec, including the ability to add and retrieve mappings, but it might be heavier for projects only needing encoding and decoding.
source-map-js is a fork of the original source-map library, intended to provide similar functionalities with some performance improvements and bug fixes. Like source-map, it is more feature-rich but also more complex than sourcemap-codec.
Encode/decode the mappings
property of a sourcemap.
Sourcemaps are difficult to generate and manipulate, because the mappings
property – the part that actually links the generated code back to the original source – is encoded using an obscure method called Variable-length quantity. On top of that, each segment in the mapping contains offsets rather than absolute indices, which means that you can't look at a segment in isolation – you have to understand the whole sourcemap.
This package makes the process slightly easier.
npm install sourcemap-codec
import { encode, decode } from 'sourcemap-codec';
var decoded = decode( ';EAEEA,EAAE,EAAC,CAAE;ECQY,UACC' );
assert.deepEqual( decoded, [
// the first line (of the generated code) has no mappings,
// as shown by the starting semi-colon (which separates lines)
[],
// the second line contains four (comma-separated) segments
[
// segments are encoded as you'd expect:
// [ generatedCodeColumn, sourceIndex, sourceCodeLine, sourceCodeColumn, nameIndex ]
// i.e. the first segment begins at column 2, and maps back to the second column
// of the second line (both zero-based) of the 0th source, and uses the 0th
// name in the `map.names` array
[ 2, 0, 2, 2, 0 ],
// the remaining segments are 4-length rather than 5-length,
// because they don't map a name
[ 4, 0, 2, 4 ],
[ 6, 0, 2, 5 ],
[ 7, 0, 2, 7 ]
],
// the final line contains two segments
[
[ 2, 1, 10, 19 ],
[ 12, 1, 11, 20 ]
]
]);
var encoded = encode( decoded );
assert.equal( encoded, ';EAEEA,EAAE,EAAC,CAAE;ECQY,UACC' );
MIT
1.4.8
FAQs
Encode/decode sourcemap mappings
The npm package sourcemap-codec receives a total of 8,610,122 weekly downloads. As such, sourcemap-codec popularity was classified as popular.
We found that sourcemap-codec demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.