[Pkg-javascript-commits] [node-sourcemap-codec] 01/03: New upstream version 1.4.1

Julien Puydt julien.puydt at laposte.net
Thu Mar 15 14:58:35 UTC 2018


This is an automated email from the git hooks/post-receive script.

jpuydt-guest pushed a commit to branch master
in repository node-sourcemap-codec.

commit a71f17a40f76bad6e2718afeb16393b16163f25f
Author: Julien Puydt <julien.puydt at laposte.net>
Date:   Thu Mar 15 15:54:29 2018 +0100

    New upstream version 1.4.1
---
 CHANGELOG.md           |   4 +
 package.json           |   6 +-
 src/sourcemap-codec.ts | 207 ++++++++++++++++++++++++++++---------------------
 3 files changed, 125 insertions(+), 92 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index a656a33..730d578 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,9 @@
 # sourcemap-codec changelog
 
+## 1.4.1
+
+* GO FASTER ([#71](https://github.com/Rich-Harris/sourcemap-codec/pull/71))
+
 ## 1.4.0
 
 * Add TypeScript declarations ([#70](https://github.com/Rich-Harris/sourcemap-codec/pull/70))
diff --git a/package.json b/package.json
index a7c94a7..19d4157 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "sourcemap-codec",
-  "version": "1.4.0",
+  "version": "1.4.1",
   "description": "Encode/decode sourcemap mappings",
   "main": "dist/sourcemap-codec.umd.js",
   "module": "dist/sourcemap-codec.es.js",
@@ -30,9 +30,7 @@
     "url": "https://github.com/Rich-Harris/sourcemap-codec/issues"
   },
   "homepage": "https://github.com/Rich-Harris/sourcemap-codec",
-  "dependencies": {
-    "vlq": "^1.0.0"
-  },
+  "dependencies": {},
   "devDependencies": {
     "codecov.io": "^0.1.6",
     "console-group": "^0.3.3",
diff --git a/src/sourcemap-codec.ts b/src/sourcemap-codec.ts
index 5052a47..f377eba 100644
--- a/src/sourcemap-codec.ts
+++ b/src/sourcemap-codec.ts
@@ -1,121 +1,152 @@
-import { decode as decodeVlq, encode as encodeVlq } from 'vlq';
-
-export type SourceMapSegment = [number] | [number, number, number, number] | [number, number, number, number, number];
+export type SourceMapSegment =
+	| [number]
+	| [number, number, number, number]
+	| [number, number, number, number, number];
 export type SourceMapLine = SourceMapSegment[];
 export type SourceMapMappings = SourceMapLine[];
 
-function decodeSegments ( encodedSegments: string[] ): number[][] {
-	let i = encodedSegments.length;
-	const segments = new Array<number[]>( i );
+const charToInteger: { [charCode: number]: number } = {};
+const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
 
-	while ( i-- ) segments[i] = decodeVlq( encodedSegments[i] );
-	return segments;
+for (let i = 0; i < chars.length; i++) {
+	charToInteger[chars.charCodeAt(i)] = i;
 }
 
-export function decode ( mappings: string ): SourceMapMappings {
-	let sourceFileIndex = 0;   // second field
-	let sourceCodeLine = 0;    // third field
-	let sourceCodeColumn = 0;  // fourth field
-	let nameIndex = 0;         // fifth field
-
-	const lines = mappings.split( ';' );
-	const numLines = lines.length;
-	const decoded = new Array<SourceMapLine>( numLines );
-
-	let i: number;
-	let j: number;
-	let line: string;
-	let generatedCodeColumn: number;
-	let decodedLine: SourceMapLine;
-	let segments: number[][];
-	let segment: number[];
-	let result: SourceMapSegment;
-
-	for ( i = 0; i < numLines; i += 1 ) {
-		line = lines[i];
+export function decode(mappings: string): SourceMapMappings {
+	let generatedCodeColumn = 0; // first field
+	let sourceFileIndex = 0;     // second field
+	let sourceCodeLine = 0;      // third field
+	let sourceCodeColumn = 0;    // fourth field
+	let nameIndex = 0;           // fifth field
+
+	const decoded: SourceMapMappings = [];
+	let line: SourceMapLine = [];
+	let segment: number[] = [];
+
+	for (let i = 0, j = 0, shift = 0, value = 0; i < mappings.length; i++) {
+		const c = mappings.charCodeAt(i);
+
+		if (c === 44) { // ","
+			if (segment.length) line.push(<SourceMapSegment>segment);
+			segment = [];
+			j = 0;
+
+		} else if (c === 59) { // ";"
+			if (segment.length) line.push(<SourceMapSegment>segment);
+			segment = [];
+			j = 0;
+			decoded.push(line);
+			line = [];
+			generatedCodeColumn = 0;
+
+		} else {
+			let integer = charToInteger[c];
+			if (integer === undefined) {
+				throw new Error('Invalid character (' + String.fromCharCode(c) + ')');
+			}
 
-		generatedCodeColumn = 0; // first field - reset each time
-		decodedLine = [];
+			const hasContinuationBit = integer & 32;
 
-		segments = decodeSegments( line.split( ',' ) );
+			integer &= 31;
+			value += integer << shift;
 
-		for ( j = 0; j < segments.length; j += 1 ) {
-			segment = segments[j];
+			if (hasContinuationBit) {
+				shift += 5;
+			} else {
+				const shouldNegate = value & 1;
+				value >>= 1;
 
-			if ( !segment.length ) {
-				break;
-			}
+				const num = shouldNegate ? -value : value;
 
-			generatedCodeColumn += segment[0];
+				if (j == 0) {
+					generatedCodeColumn += num;
+					segment.push(generatedCodeColumn);
 
-			result = [ generatedCodeColumn ];
-			decodedLine.push( result );
+				} else if (j === 1) {
+					sourceFileIndex += num;
+					segment.push(sourceFileIndex);
 
-			if ( segment.length === 1 ) {
-				// only one field!
-				continue;
-			}
+				} else if (j === 2) {
+					sourceCodeLine += num;
+					segment.push(sourceCodeLine);
 
-			sourceFileIndex  += segment[1];
-			sourceCodeLine   += segment[2];
-			sourceCodeColumn += segment[3];
+				} else if (j === 3) {
+					sourceCodeColumn += num;
+					segment.push(sourceCodeColumn);
 
-			result.push( sourceFileIndex, sourceCodeLine, sourceCodeColumn );
+				} else if (j === 4) {
+					nameIndex += num;
+					segment.push(nameIndex);
+				}
 
-			if ( segment.length === 5 ) {
-				nameIndex += segment[4];
-				result.push( nameIndex );
+				j++;
+				value = shift = 0; // reset
 			}
 		}
-
-		decoded[i] = decodedLine;
 	}
 
+	if (segment.length) line.push(<SourceMapSegment>segment);
+	decoded.push(line);
+
 	return decoded;
 }
 
-export function encode ( decoded: SourceMapMappings ): string {
-	const offsets = {
-		generatedCodeColumn: 0,
-		sourceFileIndex: 0,   // second field
-		sourceCodeLine: 0,    // third field
-		sourceCodeColumn: 0,  // fourth field
-		nameIndex: 0          // fifth field
-	};
-
-	return decoded.map( line => {
-		offsets.generatedCodeColumn = 0; // first field - reset each time
-		return line.map( encodeSegment ).join( ',' );
-	}).join( ';' );
-
-	function encodeSegment ( segment: SourceMapSegment ): string {
-		if ( !segment.length ) {
-			return '';
-		}
+export function encode(decoded: SourceMapMappings): string {
+	let sourceFileIndex = 0;  // second field
+	let sourceCodeLine = 0;   // third field
+	let sourceCodeColumn = 0; // fourth field
+	let nameIndex = 0;        // fifth field
+	let mappings = '';
 
-		const result = new Array<number>( segment.length );
+	for (let i = 0; i < decoded.length; i++) {
+		const line = decoded[i];
+		if (i > 0) mappings += ';';
+		if (line.length === 0) continue;
 
-		result[0] = segment[0] - offsets.generatedCodeColumn;
-		offsets.generatedCodeColumn = segment[0];
+		let generatedCodeColumn = 0; // first field
 
-		if ( segment.length === 1 ) {
-			// only one field!
-			return encodeVlq( result );
-		}
+		const lineMappings: string[] = [];
+
+		for (const segment of line) {
+			let segmentMappings = encodeInteger(segment[0] - generatedCodeColumn);
+			generatedCodeColumn = segment[0];
 
-		result[1] = segment[1] - offsets.sourceFileIndex;
-		result[2] = segment[2] - offsets.sourceCodeLine;
-		result[3] = segment[3] - offsets.sourceCodeColumn;
+			if (segment.length > 1) {
+				segmentMappings +=
+					encodeInteger(segment[1] - sourceFileIndex) +
+					encodeInteger(segment[2] - sourceCodeLine) +
+					encodeInteger(segment[3] - sourceCodeColumn);
 
-		offsets.sourceFileIndex  = segment[1];
-		offsets.sourceCodeLine   = segment[2];
-		offsets.sourceCodeColumn = segment[3];
+				sourceFileIndex = segment[1];
+				sourceCodeLine = segment[2];
+				sourceCodeColumn = segment[3];
+			}
+
+			if (segment.length === 5) {
+				segmentMappings += encodeInteger(segment[4] - nameIndex);
+				nameIndex = segment[4];
+			}
 
-		if ( segment.length === 5 ) {
-			result[4] = segment[4] - offsets.nameIndex;
-			offsets.nameIndex = segment[4];
+			lineMappings.push(segmentMappings);
 		}
 
-		return encodeVlq( result );
+		mappings += lineMappings.join(',');
 	}
+
+	return mappings;
+}
+
+function encodeInteger(num: number): string {
+	var result = '';
+	num = num < 0 ? (-num << 1) | 1 : num << 1;
+	do {
+		var clamped = num & 31;
+		num >>= 5;
+		if (num > 0) {
+			clamped |= 32;
+		}
+		result += chars[clamped];
+	} while (num > 0);
+
+	return result;
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-sourcemap-codec.git



More information about the Pkg-javascript-commits mailing list