pax_global_header00006660000000000000000000000064132623601760014520gustar00rootroot0000000000000052 comment=56cff5ed29f9d9fdf0f70728edc669bda83d95c7 golang-gopkg-sourcemap.v1-1.0.5/000077500000000000000000000000001326236017600164205ustar00rootroot00000000000000golang-gopkg-sourcemap.v1-1.0.5/.travis.yml000066400000000000000000000004371326236017600205350ustar00rootroot00000000000000sudo: false language: go go: - 1.6 - 1.7 - tip matrix: allow_failures: - go: tip install: - mkdir -p $HOME/gopath/src/gopkg.in - mv $HOME/gopath/src/github.com/go-sourcemap/sourcemap $HOME/gopath/src/gopkg.in/sourcemap.v1 - cd $HOME/gopath/src/gopkg.in/sourcemap.v1 golang-gopkg-sourcemap.v1-1.0.5/LICENSE000066400000000000000000000024631326236017600174320ustar00rootroot00000000000000Copyright (c) 2016 The github.com/go-sourcemap/sourcemap Contributors. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. golang-gopkg-sourcemap.v1-1.0.5/Makefile000066400000000000000000000000701326236017600200550ustar00rootroot00000000000000all: go test ./... go test ./... -short -race go vet golang-gopkg-sourcemap.v1-1.0.5/README.md000066400000000000000000000013551326236017600177030ustar00rootroot00000000000000# Source Maps consumer for Golang [![Build Status](https://travis-ci.org/go-sourcemap/sourcemap.svg?branch=v1)](https://travis-ci.org/go-sourcemap/sourcemap) ## Installation Install: go get gopkg.in/sourcemap.v1 ## Quickstart ```go func ExampleParse() { mapURL := "http://code.jquery.com/jquery-2.0.3.min.map" resp, err := http.Get(mapURL) if err != nil { panic(err) } defer resp.Body.Close() b, err := ioutil.ReadAll(resp.Body) if err != nil { panic(err) } smap, err := sourcemap.Parse(mapURL, b) if err != nil { panic(err) } line, column := 5, 6789 file, fn, line, col, ok := smap.Source(line, column) fmt.Println(file, fn, line, col, ok) // Output: http://code.jquery.com/jquery-2.0.3.js apply 4360 27 true } ``` golang-gopkg-sourcemap.v1-1.0.5/base64vlq/000077500000000000000000000000001326236017600202275ustar00rootroot00000000000000golang-gopkg-sourcemap.v1-1.0.5/base64vlq/base64_vlq.go000066400000000000000000000027211326236017600225260ustar00rootroot00000000000000package base64vlq import ( "io" ) const encodeStd = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" const ( vlqBaseShift = 5 vlqBase = 1 << vlqBaseShift vlqBaseMask = vlqBase - 1 vlqSignBit = 1 vlqContinuationBit = vlqBase ) var decodeMap [256]byte func init() { for i := 0; i < len(encodeStd); i++ { decodeMap[encodeStd[i]] = byte(i) } } func toVLQSigned(n int) int { if n < 0 { return -n<<1 + 1 } return n << 1 } func fromVLQSigned(n int) int { isNeg := n&vlqSignBit != 0 n >>= 1 if isNeg { return -n } return n } type Encoder struct { w io.ByteWriter } func NewEncoder(w io.ByteWriter) *Encoder { return &Encoder{ w: w, } } func (enc Encoder) Encode(n int) error { n = toVLQSigned(n) for digit := vlqContinuationBit; digit&vlqContinuationBit != 0; { digit = n & vlqBaseMask n >>= vlqBaseShift if n > 0 { digit |= vlqContinuationBit } err := enc.w.WriteByte(encodeStd[digit]) if err != nil { return err } } return nil } type Decoder struct { r io.ByteReader } func NewDecoder(r io.ByteReader) *Decoder { return &Decoder{ r: r, } } func (dec Decoder) Decode() (n int, err error) { shift := uint(0) for continuation := true; continuation; { c, err := dec.r.ReadByte() if err != nil { return 0, err } c = decodeMap[c] continuation = c&vlqContinuationBit != 0 n += int(c&vlqBaseMask) << shift shift += vlqBaseShift } return fromVLQSigned(n), nil } golang-gopkg-sourcemap.v1-1.0.5/base64vlq/base64_vlq_test.go000066400000000000000000000017411326236017600235660ustar00rootroot00000000000000package base64vlq_test import ( "bytes" "testing" "gopkg.in/sourcemap.v1/base64vlq" ) func TestEncodeDecode(t *testing.T) { buf := new(bytes.Buffer) enc := base64vlq.NewEncoder(buf) dec := base64vlq.NewDecoder(buf) for n := -1000; n < 1000; n++ { if err := enc.Encode(n); err != nil { panic(err) } } for n := -1000; n < 1000; n++ { nn, err := dec.Decode() if err != nil { panic(err) } if nn != n { t.Errorf("%d != %d", nn, n) } } } func BenchmarkEncode(b *testing.B) { buf := new(bytes.Buffer) enc := base64vlq.NewEncoder(buf) b.ResetTimer() for i := 0; i < b.N; i++ { if err := enc.Encode(1000); err != nil { panic(err) } } } func BenchmarkEncodeDecode(b *testing.B) { buf := new(bytes.Buffer) enc := base64vlq.NewEncoder(buf) dec := base64vlq.NewDecoder(buf) b.ResetTimer() for i := 0; i < b.N; i++ { if err := enc.Encode(1000); err != nil { panic(err) } _, err := dec.Decode() if err != nil { panic(err) } } } golang-gopkg-sourcemap.v1-1.0.5/bench_test.go000066400000000000000000000007461326236017600210740ustar00rootroot00000000000000package sourcemap_test import ( "testing" "gopkg.in/sourcemap.v1" ) func BenchmarkParse(b *testing.B) { for i := 0; i < b.N; i++ { _, err := sourcemap.Parse(jqSourceMapURL, jqSourceMapBytes) if err != nil { b.Fatal(err) } } } func BenchmarkSource(b *testing.B) { smap, err := sourcemap.Parse(jqSourceMapURL, jqSourceMapBytes) if err != nil { b.Fatal(err) } b.ResetTimer() for i := 0; i < b.N; i++ { for j := 0; j < 10; j++ { smap.Source(j, 100*j) } } } golang-gopkg-sourcemap.v1-1.0.5/consumer.go000066400000000000000000000045341326236017600206100ustar00rootroot00000000000000package sourcemap import ( "encoding/json" "fmt" "net/url" "path" "sort" "strconv" ) type Consumer struct { sourceRootURL *url.URL smap *sourceMap mappings []mapping } func Parse(mapURL string, b []byte) (*Consumer, error) { smap := new(sourceMap) err := json.Unmarshal(b, smap) if err != nil { return nil, err } if smap.Version != 3 { return nil, fmt.Errorf( "sourcemap: got version=%d, but only 3rd version is supported", smap.Version, ) } var sourceRootURL *url.URL if smap.SourceRoot != "" { u, err := url.Parse(smap.SourceRoot) if err != nil { return nil, err } if u.IsAbs() { sourceRootURL = u } } else if mapURL != "" { u, err := url.Parse(mapURL) if err != nil { return nil, err } if u.IsAbs() { u.Path = path.Dir(u.Path) sourceRootURL = u } } mappings, err := parseMappings(smap.Mappings) if err != nil { return nil, err } // Free memory. smap.Mappings = "" return &Consumer{ sourceRootURL: sourceRootURL, smap: smap, mappings: mappings, }, nil } func (c *Consumer) File() string { return c.smap.File } func (c *Consumer) Source(genLine, genCol int) (source, name string, line, col int, ok bool) { i := sort.Search(len(c.mappings), func(i int) bool { m := &c.mappings[i] if m.genLine == genLine { return m.genCol >= genCol } return m.genLine >= genLine }) // Mapping not found. if i == len(c.mappings) { return } match := &c.mappings[i] // Fuzzy match. if match.genLine > genLine || match.genCol > genCol { if i == 0 { return } match = &c.mappings[i-1] } if match.sourcesInd >= 0 { source = c.absSource(c.smap.Sources[match.sourcesInd]) } if match.namesInd >= 0 { v := c.smap.Names[match.namesInd] switch v := v.(type) { case string: name = v case float64: name = strconv.FormatFloat(v, 'f', -1, 64) default: name = fmt.Sprint(v) } } line = match.sourceLine col = match.sourceCol ok = true return } func (c *Consumer) absSource(source string) string { if path.IsAbs(source) { return source } if u, err := url.Parse(source); err == nil && u.IsAbs() { return source } if c.sourceRootURL != nil { u := *c.sourceRootURL u.Path = path.Join(c.sourceRootURL.Path, source) return u.String() } if c.smap.SourceRoot != "" { return path.Join(c.smap.SourceRoot, source) } return source } golang-gopkg-sourcemap.v1-1.0.5/consumer_test.go000066400000000000000000000126761326236017600216550ustar00rootroot00000000000000package sourcemap_test import ( "fmt" "io/ioutil" "net/http" "strings" "testing" "gopkg.in/sourcemap.v1" ) const jqSourceMapURL = "http://code.jquery.com/jquery-2.0.3.min.map" var jqSourceMapBytes []byte func init() { resp, err := http.Get(jqSourceMapURL) if err != nil { panic(err) } defer resp.Body.Close() jqSourceMapBytes, err = ioutil.ReadAll(resp.Body) if err != nil { panic(err) } } type sourceMapTest struct { genLine int genCol int wantedSource string wantedName string wantedLine int wantedCol int } func (test *sourceMapTest) String() string { return fmt.Sprintf("line=%d col=%d in file=%s", test.genLine, test.genCol, test.wantedSource) } func (test *sourceMapTest) assert(t *testing.T, smap *sourcemap.Consumer) { source, name, line, col, ok := smap.Source(test.genLine, test.genCol) if !ok { if test.wantedSource == "" && test.wantedName == "" && test.wantedLine == 0 && test.wantedCol == 0 { return } t.Fatalf("Source not found for %s", test) } if source != test.wantedSource { t.Fatalf("file: got %q, wanted %q (%s)", source, test.wantedSource, test) } if name != test.wantedName { t.Fatalf("func: got %q, wanted %q (%s)", name, test.wantedName, test) } if line != test.wantedLine { t.Fatalf("line: got %d, wanted %d (%s)", line, test.wantedLine, test) } if col != test.wantedCol { t.Fatalf("column: got %d, wanted %d (%s)", col, test.wantedCol, test) } } func TestSourceMap(t *testing.T) { smap, err := sourcemap.Parse("", []byte(sourceMapJSON)) if err != nil { t.Fatal(err) } tests := []*sourceMapTest{ {1, 1, "/the/root/one.js", "", 1, 1}, {1, 5, "/the/root/one.js", "", 1, 5}, {1, 9, "/the/root/one.js", "", 1, 11}, {1, 18, "/the/root/one.js", "bar", 1, 21}, {1, 21, "/the/root/one.js", "", 2, 3}, {1, 28, "/the/root/one.js", "baz", 2, 10}, {1, 32, "/the/root/one.js", "bar", 2, 14}, {2, 1, "/the/root/two.js", "", 1, 1}, {2, 5, "/the/root/two.js", "", 1, 5}, {2, 9, "/the/root/two.js", "", 1, 11}, {2, 18, "/the/root/two.js", "n", 1, 21}, {2, 21, "/the/root/two.js", "", 2, 3}, {2, 28, "/the/root/two.js", "n", 2, 10}, // Fuzzy match. {1, 20, "/the/root/one.js", "bar", 1, 21}, {1, 30, "/the/root/one.js", "baz", 2, 10}, {2, 12, "/the/root/two.js", "", 1, 11}, } for _, test := range tests { test.assert(t, smap) } _, _, _, _, ok := smap.Source(3, 0) if ok { t.Fatal("source must not exist") } } func TestSourceRootURL(t *testing.T) { jsonStr := sourceMapJSON jsonStr = strings.Replace(jsonStr, "/the/root", "http://the/root", 1) jsonStr = strings.Replace(jsonStr, "one.js", "../one.js", 1) smap, err := sourcemap.Parse("", []byte(jsonStr)) if err != nil { t.Fatal(err) } tests := []*sourceMapTest{ {1, 1, "http://the/one.js", "", 1, 1}, {2, 1, "http://the/root/two.js", "", 1, 1}, } for _, test := range tests { test.assert(t, smap) } } func TestEmptySourceRootURL(t *testing.T) { jsonStr := sourceMapJSON jsonStr = strings.Replace(jsonStr, "/the/root", "", 1) jsonStr = strings.Replace(jsonStr, "one.js", "../one.js", 1) smap, err := sourcemap.Parse("http://the/root/app.min.map", []byte(jsonStr)) if err != nil { t.Fatal(err) } tests := []*sourceMapTest{ {1, 1, "http://the/one.js", "", 1, 1}, {2, 1, "http://the/root/two.js", "", 1, 1}, } for _, test := range tests { test.assert(t, smap) } } func TestAbsSourceURL(t *testing.T) { jsonStr := sourceMapJSON jsonStr = strings.Replace(jsonStr, "/the/root", "", 1) jsonStr = strings.Replace(jsonStr, "one.js", "http://the/root/one.js", 1) jsonStr = strings.Replace(jsonStr, "two.js", "/another/root/two.js", 1) testAbsSourceURL(t, "", jsonStr) testAbsSourceURL(t, "http://path/to/map", jsonStr) } func testAbsSourceURL(t *testing.T, mapURL, jsonStr string) { smap, err := sourcemap.Parse(mapURL, []byte(jsonStr)) if err != nil { t.Fatal(err) } tests := []*sourceMapTest{ {1, 1, "http://the/root/one.js", "", 1, 1}, {2, 1, "/another/root/two.js", "", 1, 1}, } for _, test := range tests { test.assert(t, smap) } } func TestJQuerySourceMap(t *testing.T) { smap, err := sourcemap.Parse(jqSourceMapURL, jqSourceMapBytes) if err != nil { t.Fatal(err) } tests := []*sourceMapTest{ {1, 1, "", "", 0, 0}, {4, 0, "", "", 0, 0}, {4, 1, "http://code.jquery.com/jquery-2.0.3.js", "", 14, 0}, {4, 10, "http://code.jquery.com/jquery-2.0.3.js", "window", 14, 11}, {5, 6789, "http://code.jquery.com/jquery-2.0.3.js", "apply", 4360, 27}, {5, 10006, "http://code.jquery.com/jquery-2.0.3.js", "apply", 4676, 8}, {4, 553, "http://code.jquery.com/jquery-2.0.3.js", "ready", 93, 9}, {999999, 0, "", "", 0, 0}, } for _, test := range tests { test.assert(t, smap) } } // This is a test mapping which maps functions from two different files // (one.js and two.js) to a minified generated source. // // Here is one.js: // // ONE.foo = function (bar) { // return baz(bar); // }; // // Here is two.js: // // TWO.inc = function (n) { // return n + 1; // }; // // And here is the generated code (min.js): // // ONE.foo=function(a){return baz(a);}; // TWO.inc=function(a){return a+1;}; var genCode = `exports.testGeneratedCode = "ONE.foo=function(a){return baz(a);}; TWO.inc=function(a){return a+1;};` var sourceMapJSON = `{ "version": 3, "file": "min.js", "names": ["bar", "baz", "n"], "sources": ["one.js", "two.js"], "sourceRoot": "/the/root", "mappings": "CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA" }` golang-gopkg-sourcemap.v1-1.0.5/example_test.go000066400000000000000000000011261326236017600214410ustar00rootroot00000000000000package sourcemap_test import ( "fmt" "io/ioutil" "net/http" "gopkg.in/sourcemap.v1" ) func ExampleParse() { mapURL := "http://code.jquery.com/jquery-2.0.3.min.map" resp, err := http.Get(mapURL) if err != nil { panic(err) } defer resp.Body.Close() b, err := ioutil.ReadAll(resp.Body) if err != nil { panic(err) } smap, err := sourcemap.Parse(mapURL, b) if err != nil { panic(err) } line, column := 5, 6789 file, fn, line, col, ok := smap.Source(line, column) fmt.Println(file, fn, line, col, ok) // Output: http://code.jquery.com/jquery-2.0.3.js apply 4360 27 true } golang-gopkg-sourcemap.v1-1.0.5/sourcemap.go000066400000000000000000000052301326236017600207450ustar00rootroot00000000000000package sourcemap // import "gopkg.in/sourcemap.v1" import ( "io" "strings" "gopkg.in/sourcemap.v1/base64vlq" ) type fn func(m *mappings) (fn, error) type sourceMap struct { Version int `json:"version"` File string `json:"file"` SourceRoot string `json:"sourceRoot"` Sources []string `json:"sources"` Names []interface{} `json:"names"` Mappings string `json:"mappings"` } type mapping struct { genLine int genCol int sourcesInd int sourceLine int sourceCol int namesInd int } type mappings struct { rd *strings.Reader dec *base64vlq.Decoder hasName bool value mapping values []mapping } func parseMappings(s string) ([]mapping, error) { rd := strings.NewReader(s) m := &mappings{ rd: rd, dec: base64vlq.NewDecoder(rd), } m.value.genLine = 1 m.value.sourceLine = 1 err := m.parse() if err != nil { return nil, err } return m.values, nil } func (m *mappings) parse() error { next := parseGenCol for { c, err := m.rd.ReadByte() if err == io.EOF { m.pushValue() return nil } if err != nil { return err } switch c { case ',': m.pushValue() next = parseGenCol case ';': m.pushValue() m.value.genLine++ m.value.genCol = 0 next = parseGenCol default: err := m.rd.UnreadByte() if err != nil { return err } next, err = next(m) if err != nil { return err } } } } func parseGenCol(m *mappings) (fn, error) { n, err := m.dec.Decode() if err != nil { return nil, err } m.value.genCol += n return parseSourcesInd, nil } func parseSourcesInd(m *mappings) (fn, error) { n, err := m.dec.Decode() if err != nil { return nil, err } m.value.sourcesInd += n return parseSourceLine, nil } func parseSourceLine(m *mappings) (fn, error) { n, err := m.dec.Decode() if err != nil { return nil, err } m.value.sourceLine += n return parseSourceCol, nil } func parseSourceCol(m *mappings) (fn, error) { n, err := m.dec.Decode() if err != nil { return nil, err } m.value.sourceCol += n return parseNamesInd, nil } func parseNamesInd(m *mappings) (fn, error) { n, err := m.dec.Decode() if err != nil { return nil, err } m.hasName = true m.value.namesInd += n return parseGenCol, nil } func (m *mappings) pushValue() { if m.value.sourceLine == 1 && m.value.sourceCol == 0 { return } if m.hasName { m.values = append(m.values, m.value) m.hasName = false } else { m.values = append(m.values, mapping{ genLine: m.value.genLine, genCol: m.value.genCol, sourcesInd: m.value.sourcesInd, sourceLine: m.value.sourceLine, sourceCol: m.value.sourceCol, namesInd: -1, }) } }