diff --git a/sourcemap-uploader/README.md b/sourcemap-uploader/README.md index e0a42a530..cfca77dd3 100644 --- a/sourcemap-uploader/README.md +++ b/sourcemap-uploader/README.md @@ -10,28 +10,31 @@ npm i -D @openreplay/sourcemap-uploader ## CLI -Upload sourcemap for one file: +### Upload a sourcemap for one file: ``` sourcemap-uploader -s https://opnereplay.mycompany.com/api -k API_KEY -p PROJECT_KEY file -m ./dist/index.js.map -u https://myapp.com/index.js ``` -Upload all sourcemaps in a given directory. The URL must correspond to the root where you upload JS files from the directory. In other words, if you have your `app-42.js` along with the `app-42.js.map` in the `./build` folder and then want to upload it to your OpenReplay instance so it can be reachable through the link `https://myapp.com/static/app-42.js`, then the command should be like: +### Upload all sourcemaps in a given directory. +The URL must correspond to the root where you upload JS files from the directory. In other words, if you have your `app-42.js` along with the `app-42.js.map` in the `./build` folder and then want to upload it to your OpenReplay instance so it can be reachable through the link `https://myapp.com/static/app-42.js`, then the command should be like: ``` sourcemap-uploader -s https://opnereplay.mycompany.com/api -k API_KEY -p PROJECT_KEY dir -m ./build -u https://myapp.com/static ``` -- Use `-s` (`--server`) to specify the URL of your OpenReplay instance (make to append it with /api) +- Use `-s` (`--server`) to specify the URL of your OpenReplay instance (append it with /api). +**Do not use this parameter if you use SaaS version of the OpenRplay** + - Use `-v` (`--verbose`) to see the logs. ## NPM -There are two functions inside `index.js` of the package: +There are two functions you can export from the package: ``` -uploadFile(api_key, project_key, sourcemap_file_path, js_file_url) -uploadDir(api_key, project_key, sourcemap_dir_path, js_dir_url) +uploadFile(api_key, project_key, sourcemap_file_path, js_file_url, [server]) +uploadDir(api_key, project_key, sourcemap_dir_path, js_dir_url, [server]) ``` -Both functions return Promise. +Both functions return Promise with a result value to be the list of files for which sourcemaps were uploaded. diff --git a/sourcemap-uploader/cli.js b/sourcemap-uploader/cli.js index 253a72ce5..9dd3f16f3 100755 --- a/sourcemap-uploader/cli.js +++ b/sourcemap-uploader/cli.js @@ -58,15 +58,9 @@ const { command, api_key, project_key, server, verbose, ...args } = parser.parse global._VERBOSE = !!verbose; -try { - global.SERVER = new URL(server || "https://api.openreplay.com"); -} catch (e) { - console.error(`Sourcemap Uploader: server URL parse error. ${e}`) -} - (command === 'file' - ? uploadFile(api_key, project_key, args.sourcemap_file_path, args.js_file_url) - : uploadDir(api_key, project_key, args.sourcemap_dir_path, args.js_dir_url) + ? uploadFile(api_key, project_key, args.sourcemap_file_path, args.js_file_url, server) + : uploadDir(api_key, project_key, args.sourcemap_dir_path, args.js_dir_url, server) ) .then((sourceFiles) => sourceFiles.length > 0 diff --git a/sourcemap-uploader/index.js b/sourcemap-uploader/index.js index ad16b9ccd..7c3ee7aab 100644 --- a/sourcemap-uploader/index.js +++ b/sourcemap-uploader/index.js @@ -3,12 +3,12 @@ const readFile = require('./lib/readFile.js'), uploadSourcemaps = require('./lib/uploadSourcemaps.js'); module.exports = { - async uploadFile(api_key, project_key, sourcemap_file_path, js_file_url) { + async uploadFile(api_key, project_key, sourcemap_file_path, js_file_url, server) { const sourcemap = await readFile(sourcemap_file_path, js_file_url); - return uploadSourcemaps(api_key, project_key, [sourcemap]); + return uploadSourcemaps(api_key, project_key, [sourcemap], server); }, - async uploadDir(api_key, project_key, sourcemap_dir_path, js_dir_url) { + async uploadDir(api_key, project_key, sourcemap_dir_path, js_dir_url, server) { const sourcemaps = await readDir(sourcemap_dir_path, js_dir_url); - return uploadSourcemaps(api_key, project_key, sourcemaps); + return uploadSourcemaps(api_key, project_key, sourcemaps, server); }, }; diff --git a/sourcemap-uploader/lib/uploadSourcemaps.js b/sourcemap-uploader/lib/uploadSourcemaps.js index da4a57912..8da34ecdc 100644 --- a/sourcemap-uploader/lib/uploadSourcemaps.js +++ b/sourcemap-uploader/lib/uploadSourcemaps.js @@ -1,15 +1,22 @@ const https = require('https'); -const getUploadURLs = (api_key, project_key, js_file_urls) => +const getUploadURLs = (api_key, project_key, js_file_urls, server) => new Promise((resolve, reject) => { if (js_file_urls.length === 0) { resolve([]); } - const pathPrefix = (global.SERVER.pathname + "/").replace(/\/+/g, '/'); + let serverURL; + try { + serverURL = new URL(server); + } catch(e) { + return reject(`Failed to parse server URL "${server}".`) + } + + const pathPrefix = (serverURL.pathname + "/").replace(/\/+/g, '/'); const options = { method: 'PUT', - hostname: global.SERVER.host, + hostname: serverURL.host, path: pathPrefix + `${project_key}/sourcemaps/`, headers: { Authorization: api_key, 'Content-Type': 'application/json' }, } @@ -74,11 +81,12 @@ const uploadSourcemap = (upload_url, body) => req.end(); }); -module.exports = (api_key, project_key, sourcemaps) => +module.exports = (api_key, project_key, sourcemaps, server) => getUploadURLs( api_key, project_key, sourcemaps.map(({ js_file_url }) => js_file_url), + server || "https://api.openreplay.com", ).then(upload_urls => Promise.all( upload_urls.map((upload_url, i) => diff --git a/sourcemap-uploader/package.json b/sourcemap-uploader/package.json index b68c656e1..4a1d20181 100644 --- a/sourcemap-uploader/package.json +++ b/sourcemap-uploader/package.json @@ -1,6 +1,6 @@ { "name": "@openreplay/sourcemap-uploader", - "version": "3.0.5", + "version": "3.0.6", "description": "NPM module to upload your JS sourcemaps files to OpenReplay", "bin": "cli.js", "main": "index.js",