Skip to content

Commit

Permalink
Fleet decode sourcemap response (#5635)
Browse files Browse the repository at this point in the history
* decode the fleet sourcemap response

sourceMap is a field in the json response
  • Loading branch information
stuartnelson3 authored Jul 9, 2021
1 parent e6f801a commit cd8b93e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
3 changes: 2 additions & 1 deletion beater/beater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"compress/zlib"
"context"
"errors"
"fmt"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -317,7 +318,7 @@ func TestFleetStoreUsed(t *testing.T) {
called = true
wr := zlib.NewWriter(w)
defer wr.Close()
wr.Write([]byte(test.ValidSourcemap))
wr.Write([]byte(fmt.Sprintf(`{"sourceMap":%s}`, test.ValidSourcemap)))
}))
defer ts.Close()

Expand Down
10 changes: 4 additions & 6 deletions sourcemap/fleet_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
package sourcemap

import (
"bytes"
"compress/zlib"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"time"
Expand Down Expand Up @@ -130,10 +129,9 @@ func (f fleetStore) fetch(ctx context.Context, name, version, path string) (stri
return "", err
}

buf := new(bytes.Buffer)
if _, err := io.Copy(buf, r); err != nil {
var m map[string]json.RawMessage
if err := json.NewDecoder(r).Decode(&m); err != nil {
return "", err
}

return buf.String(), nil
return string(m["sourceMap"]), nil
}
11 changes: 6 additions & 5 deletions sourcemap/fleet_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/elastic/apm-server/beater/config"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestFleetFetch(t *testing.T) {
Expand All @@ -36,7 +37,6 @@ func TestFleetFetch(t *testing.T) {
name = "webapp"
version = "1.0.0"
path = "/my/path/to/bundle.js.map"
wantRes = "sourcemap response"
c = http.DefaultClient
sourceMapPath = "/api/fleet/artifact"
)
Expand All @@ -48,7 +48,7 @@ func TestFleetFetch(t *testing.T) {
// zlib compress
wr := zlib.NewWriter(w)
defer wr.Close()
wr.Write([]byte(wantRes))
wr.Write([]byte(resp))
}))
defer ts.Close()

Expand All @@ -71,9 +71,10 @@ func TestFleetFetch(t *testing.T) {
assert.NoError(t, err)

gotRes, err := fb.fetch(context.Background(), name, version, path)
assert.NoError(t, err)

assert.Equal(t, wantRes, gotRes)
require.NoError(t, err)

assert.Contains(t, gotRes, "webpack:///bundle.js")
assert.True(t, hasAuth)
}

var resp = "{\"serviceName\":\"web-app\",\"serviceVersion\":\"1.0.0\",\"bundleFilepath\":\"/test/e2e/general-usecase/bundle.js.map\",\"sourceMap\":{\"version\":3,\"sources\":[\"webpack:///bundle.js\",\"\",\"webpack:///./scripts/index.js\",\"webpack:///./index.html\",\"webpack:///./scripts/app.js\"],\"names\":[\"modules\",\"__webpack_require__\",\"moduleId\",\"installedModules\",\"exports\",\"module\",\"id\",\"loaded\",\"call\",\"m\",\"c\",\"p\",\"foo\",\"console\",\"log\",\"foobar\"],\"mappings\":\"CAAS,SAAUA,GCInB,QAAAC,GAAAC,GAGA,GAAAC,EAAAD,GACA,MAAAC,GAAAD,GAAAE,OAGA,IAAAC,GAAAF,EAAAD,IACAE,WACAE,GAAAJ,EACAK,QAAA,EAUA,OANAP,GAAAE,GAAAM,KAAAH,EAAAD,QAAAC,IAAAD,QAAAH,GAGAI,EAAAE,QAAA,EAGAF,EAAAD,QAvBA,GAAAD,KAqCA,OATAF,GAAAQ,EAAAT,EAGAC,EAAAS,EAAAP,EAGAF,EAAAU,EAAA,GAGAV,EAAA,KDMM,SAASI,EAAQD,EAASH,GE3ChCA,EAAA,GAEAA,EAAA,GAEAW,OFmDM,SAASP,EAAQD,EAASH,GGxDhCI,EAAAD,QAAAH,EAAAU,EAAA,cH8DM,SAASN,EAAQD,GI9DvB,QAAAQ,KACAC,QAAAC,IAAAC,QAGAH\",\"file\":\"bundle.js\",\"sourcesContent\":[\"/******/ (function(modules) { // webpackBootstrap\\n/******/ \\t// The module cache\\n/******/ \\tvar installedModules = {};\\n/******/\\n/******/ \\t// The require function\\n/******/ \\tfunction __webpack_require__(moduleId) {\\n/******/\\n/******/ \\t\\t// Check if module is in cache\\n/******/ \\t\\tif(installedModules[moduleId])\\n/******/ \\t\\t\\treturn installedModules[moduleId].exports;\\n/******/\\n/******/ \\t\\t// Create a new module (and put it into the cache)\\n/******/ \\t\\tvar module = installedModules[moduleId] = {\\n/******/ \\t\\t\\texports: {},\\n/******/ \\t\\t\\tid: moduleId,\\n/******/ \\t\\t\\tloaded: false\\n/******/ \\t\\t};\\n/******/\\n/******/ \\t\\t// Execute the module function\\n/******/ \\t\\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\\n/******/\\n/******/ \\t\\t// Flag the module as loaded\\n/******/ \\t\\tmodule.loaded = true;\\n/******/\\n/******/ \\t\\t// Return the exports of the module\\n/******/ \\t\\treturn module.exports;\\n/******/ \\t}\\n/******/\\n/******/\\n/******/ \\t// expose the modules object (__webpack_modules__)\\n/******/ \\t__webpack_require__.m = modules;\\n/******/\\n/******/ \\t// expose the module cache\\n/******/ \\t__webpack_require__.c = installedModules;\\n/******/\\n/******/ \\t// __webpack_public_path__\\n/******/ \\t__webpack_require__.p = \\\"\\\";\\n/******/\\n/******/ \\t// Load entry module and return exports\\n/******/ \\treturn __webpack_require__(0);\\n/******/ })\\n/************************************************************************/\\n/******/ ([\\n/* 0 */\\n/***/ function(module, exports, __webpack_require__) {\\n\\n\\t// Webpack\\n\\t__webpack_require__(1)\\n\\t\\n\\t__webpack_require__(2)\\n\\t\\n\\tfoo()\\n\\n\\n/***/ },\\n/* 1 */\\n/***/ function(module, exports, __webpack_require__) {\\n\\n\\tmodule.exports = __webpack_require__.p + \\\"index.html\\\"\\n\\n/***/ },\\n/* 2 */\\n/***/ function(module, exports) {\\n\\n\\tfunction foo() {\\n\\t console.log(foobar)\\n\\t}\\n\\t\\n\\tfoo()\\n\\n\\n/***/ }\\n/******/ ]);\\n\\n\\n/** WEBPACK FOOTER **\\n ** bundle.js\\n **/\",\" \\t// The module cache\\n \\tvar installedModules = {};\\n\\n \\t// The require function\\n \\tfunction __webpack_require__(moduleId) {\\n\\n \\t\\t// Check if module is in cache\\n \\t\\tif(installedModules[moduleId])\\n \\t\\t\\treturn installedModules[moduleId].exports;\\n\\n \\t\\t// Create a new module (and put it into the cache)\\n \\t\\tvar module = installedModules[moduleId] = {\\n \\t\\t\\texports: {},\\n \\t\\t\\tid: moduleId,\\n \\t\\t\\tloaded: false\\n \\t\\t};\\n\\n \\t\\t// Execute the module function\\n \\t\\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\\n\\n \\t\\t// Flag the module as loaded\\n \\t\\tmodule.loaded = true;\\n\\n \\t\\t// Return the exports of the module\\n \\t\\treturn module.exports;\\n \\t}\\n\\n\\n \\t// expose the modules object (__webpack_modules__)\\n \\t__webpack_require__.m = modules;\\n\\n \\t// expose the module cache\\n \\t__webpack_require__.c = installedModules;\\n\\n \\t// __webpack_public_path__\\n \\t__webpack_require__.p = \\\"\\\";\\n\\n \\t// Load entry module and return exports\\n \\treturn __webpack_require__(0);\\n\\n\\n\\n/** WEBPACK FOOTER **\\n ** webpack/bootstrap 6002740481c9666b0d38\\n **/\",\"// Webpack\\nrequire('../index.html')\\n\\nrequire('./app')\\n\\nfoo()\\n\\n\\n\\n/*****************\\n ** WEBPACK FOOTER\\n ** ./scripts/index.js\\n ** module id = 0\\n ** module chunks = 0\\n **/\",\"module.exports = __webpack_public_path__ + \\\"index.html\\\"\\n\\n\\n/*****************\\n ** WEBPACK FOOTER\\n ** ./index.html\\n ** module id = 1\\n ** module chunks = 0\\n **/\",\"function foo() {\\n console.log(foobar)\\n}\\n\\nfoo()\\n\\n\\n\\n/*****************\\n ** WEBPACK FOOTER\\n ** ./scripts/app.js\\n ** module id = 2\\n ** module chunks = 0\\n **/\"],\"sourceRoot\":\"\"}}"
3 changes: 2 additions & 1 deletion sourcemap/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ func TestConcurrentFetch(t *testing.T) {
version = "1.0.0"
path = "/my/path/to/bundle.js.map"
c = http.DefaultClient
res = fmt.Sprintf(`{"sourceMap":%s}`, test.ValidSourcemap)

errsLeft = tc.errWant
)
Expand All @@ -249,7 +250,7 @@ func TestConcurrentFetch(t *testing.T) {
}
wr := zlib.NewWriter(w)
defer wr.Close()
wr.Write([]byte(test.ValidSourcemap))
wr.Write([]byte(res))
}))
defer ts.Close()

Expand Down

0 comments on commit cd8b93e

Please sign in to comment.