source: trip-planner-front/node_modules/convert-source-map/index.js@ b738035

Last change on this file since b738035 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 4.0 KB
Line 
1'use strict';
2var fs = require('fs');
3var path = require('path');
4var SafeBuffer = require('safe-buffer');
5
6Object.defineProperty(exports, 'commentRegex', {
7 get: function getCommentRegex () {
8 return /^\s*\/(?:\/|\*)[@#]\s+sourceMappingURL=data:(?:application|text)\/json;(?:charset[:=]\S+?;)?base64,(?:.*)$/mg;
9 }
10});
11
12Object.defineProperty(exports, 'mapFileCommentRegex', {
13 get: function getMapFileCommentRegex () {
14 // Matches sourceMappingURL in either // or /* comment styles.
15 return /(?:\/\/[@#][ \t]+sourceMappingURL=([^\s'"`]+?)[ \t]*$)|(?:\/\*[@#][ \t]+sourceMappingURL=([^\*]+?)[ \t]*(?:\*\/){1}[ \t]*$)/mg;
16 }
17});
18
19
20function decodeBase64(base64) {
21 return (SafeBuffer.Buffer.from(base64, 'base64') || "").toString();
22}
23
24function stripComment(sm) {
25 return sm.split(',').pop();
26}
27
28function readFromFileMap(sm, dir) {
29 // NOTE: this will only work on the server since it attempts to read the map file
30
31 var r = exports.mapFileCommentRegex.exec(sm);
32
33 // for some odd reason //# .. captures in 1 and /* .. */ in 2
34 var filename = r[1] || r[2];
35 var filepath = path.resolve(dir, filename);
36
37 try {
38 return fs.readFileSync(filepath, 'utf8');
39 } catch (e) {
40 throw new Error('An error occurred while trying to read the map file at ' + filepath + '\n' + e);
41 }
42}
43
44function Converter (sm, opts) {
45 opts = opts || {};
46
47 if (opts.isFileComment) sm = readFromFileMap(sm, opts.commentFileDir);
48 if (opts.hasComment) sm = stripComment(sm);
49 if (opts.isEncoded) sm = decodeBase64(sm);
50 if (opts.isJSON || opts.isEncoded) sm = JSON.parse(sm);
51
52 this.sourcemap = sm;
53}
54
55Converter.prototype.toJSON = function (space) {
56 return JSON.stringify(this.sourcemap, null, space);
57};
58
59Converter.prototype.toBase64 = function () {
60 var json = this.toJSON();
61 return (SafeBuffer.Buffer.from(json, 'utf8') || "").toString('base64');
62};
63
64Converter.prototype.toComment = function (options) {
65 var base64 = this.toBase64();
66 var data = 'sourceMappingURL=data:application/json;charset=utf-8;base64,' + base64;
67 return options && options.multiline ? '/*# ' + data + ' */' : '//# ' + data;
68};
69
70// returns copy instead of original
71Converter.prototype.toObject = function () {
72 return JSON.parse(this.toJSON());
73};
74
75Converter.prototype.addProperty = function (key, value) {
76 if (this.sourcemap.hasOwnProperty(key)) throw new Error('property "' + key + '" already exists on the sourcemap, use set property instead');
77 return this.setProperty(key, value);
78};
79
80Converter.prototype.setProperty = function (key, value) {
81 this.sourcemap[key] = value;
82 return this;
83};
84
85Converter.prototype.getProperty = function (key) {
86 return this.sourcemap[key];
87};
88
89exports.fromObject = function (obj) {
90 return new Converter(obj);
91};
92
93exports.fromJSON = function (json) {
94 return new Converter(json, { isJSON: true });
95};
96
97exports.fromBase64 = function (base64) {
98 return new Converter(base64, { isEncoded: true });
99};
100
101exports.fromComment = function (comment) {
102 comment = comment
103 .replace(/^\/\*/g, '//')
104 .replace(/\*\/$/g, '');
105
106 return new Converter(comment, { isEncoded: true, hasComment: true });
107};
108
109exports.fromMapFileComment = function (comment, dir) {
110 return new Converter(comment, { commentFileDir: dir, isFileComment: true, isJSON: true });
111};
112
113// Finds last sourcemap comment in file or returns null if none was found
114exports.fromSource = function (content) {
115 var m = content.match(exports.commentRegex);
116 return m ? exports.fromComment(m.pop()) : null;
117};
118
119// Finds last sourcemap comment in file or returns null if none was found
120exports.fromMapFileSource = function (content, dir) {
121 var m = content.match(exports.mapFileCommentRegex);
122 return m ? exports.fromMapFileComment(m.pop(), dir) : null;
123};
124
125exports.removeComments = function (src) {
126 return src.replace(exports.commentRegex, '');
127};
128
129exports.removeMapFileComments = function (src) {
130 return src.replace(exports.mapFileCommentRegex, '');
131};
132
133exports.generateMapFileComment = function (file, options) {
134 var data = 'sourceMappingURL=' + file;
135 return options && options.multiline ? '/*# ' + data + ' */' : '//# ' + data;
136};
Note: See TracBrowser for help on using the repository browser.