@srcmap/codec 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +67 -0
  2. package/index.d.ts +19 -0
  3. package/index.js +112 -0
  4. package/package.json +13 -10
package/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # @srcmap/codec
2
+
3
+ [![npm](https://img.shields.io/npm/v/@srcmap/codec.svg)](https://www.npmjs.com/package/@srcmap/codec)
4
+ [![CI](https://github.com/BartWaardenburg/srcmap/actions/workflows/ci.yml/badge.svg)](https://github.com/BartWaardenburg/srcmap/actions/workflows/ci.yml)
5
+ [![Coverage](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/BartWaardenburg/srcmap/badges/coverage.json)](https://github.com/BartWaardenburg/srcmap/actions/workflows/coverage.yml)
6
+
7
+ High-performance VLQ source map codec powered by Rust via [NAPI](https://napi.rs).
8
+
9
+ Drop-in replacement for [`@jridgewell/sourcemap-codec`](https://github.com/jridgewell/sourcemap-codec). Encodes and decodes source map `mappings` strings as specified in [ECMA-426](https://tc39.es/ecma426/).
10
+
11
+ ## Install
12
+
13
+ ```bash
14
+ npm install @srcmap/codec
15
+ ```
16
+
17
+ Prebuilt binaries are available for:
18
+ - macOS (x64, arm64)
19
+ - Linux (x64, arm64, glibc + musl)
20
+ - Windows (x64)
21
+
22
+ ## Usage
23
+
24
+ ```js
25
+ import { decode, encode } from '@srcmap/codec';
26
+
27
+ const decoded = decode('AAAA;AACA,EAAE');
28
+ // [
29
+ // [[0, 0, 0, 0]],
30
+ // [[0, 0, 1, 0], [2, 0, 0, 2]]
31
+ // ]
32
+
33
+ const encoded = encode(decoded);
34
+ // 'AAAA;AACA,EAAE'
35
+ ```
36
+
37
+ ## API
38
+
39
+ ### `decode(mappings: string): number[][][]`
40
+
41
+ Decode a VLQ-encoded mappings string into an array of lines, each containing an array of segments. Each segment is an array of 1, 4, or 5 numbers.
42
+
43
+ ### `encode(mappings: number[][][]): string`
44
+
45
+ Encode decoded mappings back into a VLQ string.
46
+
47
+ ### Segment format
48
+
49
+ | Fields | Meaning |
50
+ |--------|---------|
51
+ | `[genCol]` | Generated column only |
52
+ | `[genCol, srcIdx, origLine, origCol]` | With source mapping |
53
+ | `[genCol, srcIdx, origLine, origCol, nameIdx]` | With source mapping and name |
54
+
55
+ ## Compatibility
56
+
57
+ API-compatible with `@jridgewell/sourcemap-codec` — same function signatures, same output format. Can be used as a drop-in replacement.
58
+
59
+ ## Part of [srcmap](https://github.com/BartWaardenburg/srcmap)
60
+
61
+ High-performance source map tooling written in Rust. See also:
62
+ - [`@srcmap/sourcemap`](https://www.npmjs.com/package/@srcmap/sourcemap) - Source map parser (NAPI)
63
+ - [`@srcmap/sourcemap-wasm`](https://www.npmjs.com/package/@srcmap/sourcemap-wasm) - Source map parser (WASM)
64
+
65
+ ## License
66
+
67
+ MIT
package/index.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Decode a VLQ-encoded source map mappings string.
3
+ *
4
+ * Returns an array of lines, each containing an array of segments.
5
+ * Each segment is an array of 1, 4, or 5 numbers.
6
+ *
7
+ * Compatible with `@jridgewell/sourcemap-codec` decode().
8
+ */
9
+ export declare function decode(mappings: string): number[][][]
10
+
11
+ /**
12
+ * Encode decoded source map mappings back into a VLQ string.
13
+ *
14
+ * Takes an array of lines, each containing an array of segments.
15
+ * Each segment should be an array of 1, 4, or 5 numbers.
16
+ *
17
+ * Compatible with `@jridgewell/sourcemap-codec` encode().
18
+ */
19
+ export declare function encode(mappings: number[][][]): string
package/index.js ADDED
@@ -0,0 +1,112 @@
1
+ /* auto-generated NAPI-RS loader */
2
+ const { existsSync, readFileSync } = require('fs')
3
+ const { join } = require('path')
4
+ const { platform, arch } = process
5
+
6
+ let nativeBinding = null
7
+
8
+ const triples = {
9
+ 'darwin-arm64': 'srcmap-codec.darwin-arm64.node',
10
+ 'darwin-x64': 'srcmap-codec.darwin-x64.node',
11
+ 'linux-x64-gnu': 'srcmap-codec.linux-x64-gnu.node',
12
+ 'linux-x64-musl': 'srcmap-codec.linux-x64-musl.node',
13
+ 'linux-arm64-gnu': 'srcmap-codec.linux-arm64-gnu.node',
14
+ 'linux-arm64-musl': 'srcmap-codec.linux-arm64-musl.node',
15
+ 'win32-x64-msvc': 'srcmap-codec.win32-x64-msvc.node',
16
+ }
17
+
18
+ function getTripleKey() {
19
+ const platformArch = `${platform}-${arch}`
20
+ if (platform === 'linux') {
21
+ const { familySync } = require('detect-libc')
22
+ const libc = familySync() === 'musl' ? 'musl' : 'gnu'
23
+ return `${platformArch}-${libc}`
24
+ }
25
+ return platformArch
26
+ }
27
+
28
+ const tripleKey = getTripleKey()
29
+ const bindingFile = triples[tripleKey]
30
+
31
+ if (bindingFile) {
32
+ const bindingPath = join(__dirname, bindingFile)
33
+ if (existsSync(bindingPath)) {
34
+ nativeBinding = require(bindingPath)
35
+ } else {
36
+ try {
37
+ nativeBinding = require(`@srcmap/codec-${tripleKey}`)
38
+ } catch {
39
+ throw new Error(`Failed to load native binding for ${tripleKey}. File not found: ${bindingPath}`)
40
+ }
41
+ }
42
+ } else {
43
+ throw new Error(`Unsupported platform: ${tripleKey}`)
44
+ }
45
+
46
+ module.exports.decode = nativeBinding.decode
47
+ module.exports.encode = nativeBinding.encode
48
+
49
+ // Experimental: JSON string approach
50
+ module.exports.decodeJson = function decodeJson(mappings) {
51
+ return JSON.parse(nativeBinding.decodeJson(mappings))
52
+ }
53
+ module.exports.encodeJson = function encodeJson(mappings) {
54
+ return nativeBinding.encodeJson(JSON.stringify(mappings))
55
+ }
56
+
57
+ // Experimental: Packed buffer approach
58
+ module.exports.decodeBuf = function decodeBuf(mappings) {
59
+ const buf = nativeBinding.decodeBuf(mappings)
60
+ const i32 = new Int32Array(buf.buffer, buf.byteOffset, buf.byteLength >> 2)
61
+ let pos = 0
62
+
63
+ const nLines = i32[pos++]
64
+ const lineSegCounts = new Array(nLines)
65
+ for (let i = 0; i < nLines; i++) {
66
+ lineSegCounts[i] = i32[pos++]
67
+ }
68
+
69
+ const result = new Array(nLines)
70
+ for (let i = 0; i < nLines; i++) {
71
+ const nSegs = lineSegCounts[i]
72
+ const line = new Array(nSegs)
73
+ for (let j = 0; j < nSegs; j++) {
74
+ const nFields = i32[pos++]
75
+ const seg = new Array(nFields)
76
+ for (let k = 0; k < nFields; k++) {
77
+ seg[k] = i32[pos++]
78
+ }
79
+ line[j] = seg
80
+ }
81
+ result[i] = line
82
+ }
83
+ return result
84
+ }
85
+ module.exports.encodeBuf = function encodeBuf(mappings) {
86
+ // Pack nested arrays into flat i32 buffer
87
+ let totalInts = 1 + mappings.length // n_lines + seg counts
88
+ for (const line of mappings) {
89
+ for (const seg of line) {
90
+ totalInts += 1 + seg.length // n_fields + values
91
+ }
92
+ }
93
+
94
+ const buf = Buffer.alloc(totalInts * 4)
95
+ const i32 = new Int32Array(buf.buffer, buf.byteOffset, totalInts)
96
+ let pos = 0
97
+
98
+ i32[pos++] = mappings.length
99
+ for (const line of mappings) {
100
+ i32[pos++] = line.length
101
+ }
102
+ for (const line of mappings) {
103
+ for (const seg of line) {
104
+ i32[pos++] = seg.length
105
+ for (const val of seg) {
106
+ i32[pos++] = val
107
+ }
108
+ }
109
+ }
110
+
111
+ return nativeBinding.encodeBuf(buf)
112
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@srcmap/codec",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "High-performance VLQ source map codec powered by Rust",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -26,16 +26,17 @@
26
26
  "license": "MIT",
27
27
  "files": [
28
28
  "index.js",
29
- "index.d.ts"
29
+ "index.d.ts",
30
+ "README.md"
30
31
  ],
31
32
  "optionalDependencies": {
32
- "@srcmap/codec-darwin-x64": "0.1.2",
33
- "@srcmap/codec-darwin-arm64": "0.1.2",
34
- "@srcmap/codec-linux-x64-gnu": "0.1.2",
35
- "@srcmap/codec-linux-x64-musl": "0.1.2",
36
- "@srcmap/codec-linux-arm64-gnu": "0.1.2",
37
- "@srcmap/codec-linux-arm64-musl": "0.1.2",
38
- "@srcmap/codec-win32-x64-msvc": "0.1.2"
33
+ "@srcmap/codec-darwin-x64": "0.1.3",
34
+ "@srcmap/codec-darwin-arm64": "0.1.3",
35
+ "@srcmap/codec-linux-x64-gnu": "0.1.3",
36
+ "@srcmap/codec-linux-x64-musl": "0.1.3",
37
+ "@srcmap/codec-linux-arm64-gnu": "0.1.3",
38
+ "@srcmap/codec-linux-arm64-musl": "0.1.3",
39
+ "@srcmap/codec-win32-x64-msvc": "0.1.3"
39
40
  },
40
41
  "devDependencies": {
41
42
  "@napi-rs/cli": "^3.0.0"
@@ -44,12 +45,14 @@
44
45
  "artifacts": "napi artifacts",
45
46
  "build": "napi build --release --platform",
46
47
  "build:debug": "napi build --platform",
48
+ "test": "node --test __tests__/codec.test.mjs",
49
+ "test:coverage": "mkdir -p coverage && node --test --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=coverage/lcov.info --test-reporter=spec --test-reporter-destination=stdout __tests__/codec.test.mjs",
47
50
  "prepublishOnly": "napi prepublish -t npm",
48
51
  "version": "napi version"
49
52
  },
50
53
  "repository": {
51
54
  "type": "git",
52
- "url": "https://github.com/BartWaardenburg/srcmap"
55
+ "url": "git+https://github.com/BartWaardenburg/srcmap.git"
53
56
  },
54
57
  "keywords": [
55
58
  "sourcemap",