feat: init sourcemap-uploader

This commit is contained in:
ShiKhu 2021-05-03 17:17:26 +02:00
parent b5fc3bfefd
commit 8d5bef140f
12 changed files with 1373 additions and 0 deletions

View file

@ -0,0 +1,26 @@
{
"root": true,
"parserOptions": {
"ecmaVersion": 2018
},
"globals": {
"require": true,
"module": true,
"console": true,
"Promise": true,
"Buffer": true
},
"plugins": [
"prettier"
],
"extends": [
"eslint:recommended",
"plugin:prettier/recommended"
],
"rules": {
"prettier/prettier": ["error", {
"singleQuote": true,
"trailingComma": "all"
}]
}
}

3
sourcemap-uploader/.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
node_modules
npm-debug.log
.cache

View file

@ -0,0 +1,2 @@
.eslintrc.js
.cache

View file

@ -0,0 +1,19 @@
Copyright (c) 2021 OpenReplay.com <support@openreplay.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View file

@ -0,0 +1,34 @@
# sourcemaps-uploader
A NPM module to upload your JS sourcemaps files to [OpenReplay](https://openreplay.com/).
## Installation
```
npm i @openreplay/sourcemap-uploader -D
```
## CLI
Upload sourcemap for one file
```
sourcemap-uploader -k API_KEY -p PROJECT_KEY file -m ./dist/index.js.map -u https://openreplay.com/index.js
```
Upload all sourcemaps in the directory. The path will be appended to the base url
```
sourcemap-uploader -k API_KEY -p PROJECT_KEY dir -m ./dist -u https://openreplay.com/
```
## NPM
There are two functions inside `index.js` of the package
```
uploadFile(api_key, project_key, sourcemap_file_path, js_file_url)
uploadDir(api_key, project_key, sourcemap_dir_path, js_dir_url)
```
Both functions return Promise.

68
sourcemap-uploader/cli.js Executable file
View file

@ -0,0 +1,68 @@
#!/usr/bin/env node
'use strict';
const { ArgumentParser } = require('argparse');
const { version, description } = require('./package.json');
const { uploadFile, uploadDir } = require('./index.js');
const parser = new ArgumentParser({
version,
description,
});
parser.addArgument(['-k', '--api-key'], {
help: 'API key',
required: true,
});
parser.addArgument(['-p', '-i', '--project-key'], { // -i is depricated
help: 'Project Key',
required: true,
});
parser.addArgument(['-s', '--server'], {
help: 'OpenReplay API server URL for upload',
});
const subparsers = parser.addSubparsers({
title: 'commands',
dest: 'command',
});
const file = subparsers.addParser('file');
file.addArgument(['-m', '--sourcemap-file-path'], {
help: 'Local path to the sourcemap file',
required: true,
});
file.addArgument(['-u', '--js-file-url'], {
help: 'URL to the minified js file',
required: true,
});
const dir = subparsers.addParser('dir');
dir.addArgument(['-m', '--sourcemap-dir-path'], {
help: 'Dir with the sourcemap files',
required: true,
});
dir.addArgument(['-u', '--js-dir-url'], {
help: 'Base URL where the corresponding dir will be placed',
required: true,
});
const { command, api_key, project_key, server, ...args } = parser.parseArgs();
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)
)
.then((uploadedFiles) =>
console.log(`Sourcemap${uploadedFiles.length > 1 ? "s" : ""} successfully uploaded! (${uploadedFiles.length} files)\n`
+ uploadedFiles.join("\t\n")
)
)
.catch(e => console.error(`Sourcemap Uploader: ${e}`));

View file

@ -0,0 +1,14 @@
const readFile = require('./lib/readFile.js'),
readDir = require('./lib/readDir.js'),
uploadSourcemaps = require('./lib/uploadSourcemaps.js');
module.exports = {
async uploadFile(api_key, project_key, sourcemap_file_path, js_file_url) {
const sourcemap = await readFile(sourcemap_file_path, js_file_url);
return uploadSourcemaps(api_key, project_key, [sourcemap]);
},
async uploadDir(api_key, project_key, sourcemap_dir_path, js_dir_url) {
const sourcemaps = await readDir(sourcemap_dir_path, js_dir_url);
return uploadSourcemaps(api_key, project_key, sourcemaps);
},
};

View file

@ -0,0 +1,17 @@
const glob = require('glob-promise');
const readFile = require('./readFile');
module.exports = (sourcemap_dir_path, js_dir_url) => {
sourcemap_dir_path = (sourcemap_dir_path + '/').replace(/\/+/g, '/');
js_dir_url = (js_dir_url + '/').replace(/\/+/g, '/');
return glob(sourcemap_dir_path + '**/*.map').then(sourcemap_file_paths =>
Promise.all(
sourcemap_file_paths.map(sourcemap_file_path =>
readFile(
sourcemap_file_path,
js_dir_url + sourcemap_file_path.slice(sourcemap_dir_path.length, -4),
),
),
),
);
};

View file

@ -0,0 +1,6 @@
const fs = require('fs').promises;
module.exports = (sourcemap_file_path, js_file_url) =>
fs.readFile(sourcemap_file_path, 'utf8').then(body => {
return { sourcemap_file_path, js_file_url, body };
});

View file

@ -0,0 +1,68 @@
const https = require('https');
const getUploadURLs = (api_key, project_key, js_file_urls) =>
new Promise((resolve, reject) => {
const pathPrefix = (global.SERVER.pathname + "/").replace(/\/+/g, '/');
const req = https.request(
{
method: 'PUT',
hostname: global.SERVER.host,
path: pathPrefix + `${project_key}/sourcemaps/`,
headers: { Authorization: api_key, 'Content-Type': 'application/json' },
},
res => {
if (res.statusCode === 403) {
reject("Authorisation rejected. Please, check your API_KEY and/or PROJECT_KEY.")
return
} else if (res.statusCode !== 200) {
reject("Server Error. Please, contact OpenReplay support.");
return;
}
let data = '';
res.on('data', s => (data += s));
res.on('end', () => resolve(JSON.parse(data).data));
},
);
req.on('error', reject);
req.write(JSON.stringify({ URL: js_file_urls }));
req.end();
});
const uploadSourcemap = (upload_url, sourcemap) =>
new Promise((resolve, reject) => {
body = Buffer.from(JSON.stringify(sourcemap.body));
const req = https.request(
upload_url,
{
method: 'PUT',
headers: {
'Content-Length': body.length,
'Content-Type': 'application/json',
},
},
res => {
if (res.statusCode !== 200) {
reject("Unable to upload. Please, contact OpenReplay support.");
return;
}
resolve(sourcemap.sourcemap_file_path)
//res.on('end', resolve);
},
);
req.on('error', reject);
req.write(body);
req.end();
});
module.exports = (api_key, project_key, sourcemaps) =>
getUploadURLs(
api_key,
project_key,
sourcemaps.map(({ js_file_url }) => js_file_url),
).then(upload_urls =>
Promise.all(
upload_urls.map((upload_url, i) =>
uploadSourcemap(upload_url, sourcemaps[i])
),
),
);

1091
sourcemap-uploader/package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,25 @@
{
"name": "@openreplay/sourcemap-uploader",
"version": "3.0.1",
"description": "NPM module to upload your JS sourcemaps files to OpenReplay",
"bin": "cli.js",
"main": "index.js",
"scripts": {
"lint": "eslint . --ext .js --fix"
},
"author": "Alex Tsokurov",
"contributors": [
"Aleksandr K <alex@openreplay.com>"
],
"license": "MIT",
"devDependencies": {
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-prettier": "^3.1.2",
"prettier": "^1.19.1"
},
"dependencies": {
"argparse": "^1.0.10",
"glob-promise": "^3.4.0"
}
}