diff --git a/.jshintrc b/.jshintrc index 4a29289..53fb51a 100644 --- a/.jshintrc +++ b/.jshintrc @@ -41,6 +41,7 @@ "define": false, "window": false, "atob": true, - "JSON": false + "JSON": false, + "TextDecoder": true } } diff --git a/LICENSE b/LICENSE index 748f42e..789326f 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,7 @@ The MIT License (MIT) -Copyright (c) 2014, 2015, 2016, 2017 Simon Lydell +Copyright (c) 2014, 2015, 2016, 2017, 2018, 2019, 2020 Simon Lydell +Copyright (c) 2019 ZHAO Jinxiang Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/bower.json b/bower.json deleted file mode 100644 index 552663b..0000000 --- a/bower.json +++ /dev/null @@ -1,30 +0,0 @@ -{ - "name": "source-map-resolve", - "version": "0.5.1", - "author": "Simon Lydell", - "license": "MIT", - "description": "Resolve the source map and/or sources for a generated file.", - "keywords": [ - "source map", - "sourcemap", - "source", - "map", - "sourceMappingURL", - "resolve", - "resolver", - "locate", - "locator", - "find", - "finder" - ], - "authors": [ - "Simon Lydell" - ], - "ignore": [ - ".*" - ], - "dependencies": { - "source-map-url": "^0.4.0", - "resolve-url": "^0.2.1" - } -} \ No newline at end of file diff --git a/changelog.md b/changelog.md index d730d36..80fe2e3 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,23 @@ +### Version 0.6.0 (2020-03-21) ### + +- Removed: The browser version. Only Node.js is now supported. +- Improved: Three old, weird, tiny dependencies were inlined, which should + decrease install size. + +### Version 0.5.3 (2019-12-28) ### + +- Fixed: base64 encoded source maps now correctly decodes as utf-8. Previously, + non-ASCII characters could end up garbled. Thanks to ZHAO Jinxiang + (@xiaoxiangmoe)! (Note: This fix does not work in old evironments not + supporting both `TextDecoder` and `Uint8Array`.) +- Improved: Reduced size of the npm package. + +### Version 0.5.2 (2018-05-10) ### + +- Improved: Updated the version range of `atob` to disallow depending on `2.0.3` + which as a [security + vulnerability](https://snyk.io/test/npm/atob/2.0.3?severity=high&severity=medium&severity=low). + ### Version 0.5.1 (2017-10-21) ### - Fixed: URLs are now decoded before being passed to `read` in Node.js. This diff --git a/component.json b/component.json deleted file mode 100644 index c081ca0..0000000 --- a/component.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "name": "source-map-resolve", - "version": "0.5.1", - "author": "Simon Lydell", - "license": "MIT", - "description": "Resolve the source map and/or sources for a generated file.", - "keywords": [ - "source map", - "sourcemap", - "source", - "map", - "sourceMappingURL", - "resolve", - "resolver", - "locate", - "locator", - "find", - "finder" - ], - "repo": "lydell/source-map-resolve", - "main": "source-map-resolve.js", - "scripts": [ - "source-map-resolve.js" - ], - "dependencies": { - "lydell/source-map-url": "~0.4.0", - "lydell/resolve-url": "~0.2.1" - } -} \ No newline at end of file diff --git a/generate-source-map-resolve.js b/generate-source-map-resolve.js deleted file mode 100644 index a37e393..0000000 --- a/generate-source-map-resolve.js +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright 2014, 2017 Simon Lydell -// X11 (“MIT”) Licensed. (See LICENSE.) - -var fs = require("fs") - -var template = fs.readFileSync("source-map-resolve.js.template").toString() -var nodeCode = fs.readFileSync("lib/source-map-resolve-node.js").toString() - -nodeCode = nodeCode - - // Remove leading comments and `require`s. - .replace(/^\s*(?:\/\/.+\s+|var\s+\w+\s*=\s*require\([^)]+\).*\s+)*/, "") - - // Remove `urix`. - .replace(/(\w+)\s*=\s*urix\(\1\)\s*/g, "") - - // Remove `decode-uri-component`. - .replace(/(var readUrl = )decodeUriComponent\(([\w.]+)\)/g, "$1$2") - - // Change `module.exports = {...}` to `return {...}`. - .replace(/module\.exports = (\{[^}]+\})\s*$/, "return $1") - - // Indent. - .replace(/^(?!$)/gm, " ") - -var code = template.replace(/[ \t]*\{\{source-map-resolve-node.js\}\}/, nodeCode) - -fs.writeFileSync("source-map-resolve.js", code) diff --git a/lib/source-map-resolve-node.js b/index.js similarity index 71% rename from lib/source-map-resolve-node.js rename to index.js index f80953d..66bb638 100644 --- a/lib/source-map-resolve-node.js +++ b/index.js @@ -1,13 +1,24 @@ -// Copyright 2014, 2015, 2016, 2017 Simon Lydell -// X11 (“MIT”) Licensed. (See LICENSE.) +var atob = require("atob") +var urlLib = require("url") +var pathLib = require("path") +var decodeUriComponentLib = require("decode-uri-component") -var sourceMappingURL = require("source-map-url") -var resolveUrl = require("./resolve-url") -var decodeUriComponent = require("./decode-uri-component") -var urix = require("urix") -var atob = require("atob") +function resolveUrl(/* ...urls */) { + return Array.prototype.reduce.call(arguments, function(resolved, nextUrl) { + return urlLib.resolve(resolved, nextUrl) + }) +} + +function convertWindowsPath(aPath) { + return pathLib.sep === "\\" ? aPath.replace(/\\/g, "/").replace(/^[a-z]:\/?/i, "/") : aPath +} + +function customDecodeUriComponent(string) { + // `decodeUriComponentLib` turns `+` into ` `, but that's not wanted. + return decodeUriComponentLib(string.replace(/\+/g, "%2B")) +} function callbackAsync(callback, error, result) { setImmediate(function() { callback(error, result) }) @@ -23,7 +34,7 @@ function parseMapToJSON(string, data) { } function readSync(read, url, data) { - var readUrl = decodeUriComponent(url) + var readUrl = customDecodeUriComponent(url) try { return String(read(readUrl)) } catch (error) { @@ -34,6 +45,28 @@ function readSync(read, url, data) { +var innerRegex = /[#@] sourceMappingURL=([^\s'"]*)/ + +var sourceMappingURLRegex = RegExp( + "(?:" + + "/\\*" + + "(?:\\s*\r?\n(?://)?)?" + + "(?:" + innerRegex.source + ")" + + "\\s*" + + "\\*/" + + "|" + + "//(?:" + innerRegex.source + ")" + + ")" + + "\\s*" +) + +function getSourceMappingUrl(code) { + var match = code.match(sourceMappingURLRegex) + return match ? match[1] || match[2] || "" : null +} + + + function resolveSourceMap(code, codeUrl, read, callback) { var mapData try { @@ -44,7 +77,7 @@ function resolveSourceMap(code, codeUrl, read, callback) { if (!mapData || mapData.map) { return callbackAsync(callback, null, mapData) } - var readUrl = decodeUriComponent(mapData.url) + var readUrl = customDecodeUriComponent(mapData.url) read(readUrl, function(error, result) { if (error) { error.sourceMapData = mapData @@ -71,19 +104,56 @@ function resolveSourceMapSync(code, codeUrl, read) { } var dataUriRegex = /^data:([^,;]*)(;[^,;]*)*(?:,(.*))?$/ + +/** + * The media type for JSON text is application/json. + * + * {@link https://tools.ietf.org/html/rfc8259#section-11 | IANA Considerations } + * + * `text/json` is non-standard media type + */ var jsonMimeTypeRegex = /^(?:application|text)\/json$/ +/** + * JSON text exchanged between systems that are not part of a closed ecosystem + * MUST be encoded using UTF-8. + * + * {@link https://tools.ietf.org/html/rfc8259#section-8.1 | Character Encoding} + */ +var jsonCharacterEncoding = "utf-8" + +function base64ToBuf(b64) { + var binStr = atob(b64) + var len = binStr.length + var arr = new Uint8Array(len) + for (var i = 0; i < len; i++) { + arr[i] = binStr.charCodeAt(i) + } + return arr +} + +function decodeBase64String(b64) { + if (typeof TextDecoder === "undefined" || typeof Uint8Array === "undefined") { + return atob(b64) + } + var buf = base64ToBuf(b64); + // Note: `decoder.decode` method will throw a `DOMException` with the + // `"EncodingError"` value when an coding error is found. + var decoder = new TextDecoder(jsonCharacterEncoding, {fatal: true}) + return decoder.decode(buf); +} + function resolveSourceMapHelper(code, codeUrl) { - codeUrl = urix(codeUrl) + codeUrl = convertWindowsPath(codeUrl) - var url = sourceMappingURL.getFrom(code) + var url = getSourceMappingUrl(code) if (!url) { return null } var dataUri = url.match(dataUriRegex) if (dataUri) { - var mimeType = dataUri[1] + var mimeType = dataUri[1] || "text/plain" var lastParameter = dataUri[2] || "" var encoded = dataUri[3] || "" var data = { @@ -93,14 +163,19 @@ function resolveSourceMapHelper(code, codeUrl) { map: encoded } if (!jsonMimeTypeRegex.test(mimeType)) { - var error = new Error("Unuseful data uri mime type: " + (mimeType || "text/plain")) + var error = new Error("Unuseful data uri mime type: " + mimeType) + error.sourceMapData = data + throw error + } + try { + data.map = parseMapToJSON( + lastParameter === ";base64" ? decodeBase64String(encoded) : decodeURIComponent(encoded), + data + ) + } catch (error) { error.sourceMapData = data throw error } - data.map = parseMapToJSON( - lastParameter === ";base64" ? atob(encoded) : decodeURIComponent(encoded), - data - ) return data } @@ -144,7 +219,7 @@ function resolveSources(map, mapUrl, read, options, callback) { result.sourcesContent[index] = sourceContent callbackAsync(done, null) } else { - var readUrl = decodeUriComponent(fullUrl) + var readUrl = customDecodeUriComponent(fullUrl) read(readUrl, function(error, source) { result.sourcesContent[index] = error ? error : String(source) done() @@ -169,7 +244,7 @@ function resolveSourcesSync(map, mapUrl, read, options) { if (typeof sourceContent === "string") { result.sourcesContent[index] = sourceContent } else { - var readUrl = decodeUriComponent(fullUrl) + var readUrl = customDecodeUriComponent(fullUrl) try { result.sourcesContent[index] = String(read(readUrl)) } catch (error) { @@ -186,7 +261,7 @@ var endingSlash = /\/?$/ function resolveSourcesHelper(map, mapUrl, options, fn) { options = options || {} - mapUrl = urix(mapUrl) + mapUrl = convertWindowsPath(mapUrl) var fullUrl var sourceContent var sourceRoot @@ -227,7 +302,7 @@ function resolve(code, codeUrl, read, options, callback) { sourcesRelativeTo: mapUrl, map: null } - var readUrl = decodeUriComponent(mapUrl) + var readUrl = customDecodeUriComponent(mapUrl) read(readUrl, function(error, result) { if (error) { error.sourceMapData = data diff --git a/lib/decode-uri-component.js b/lib/decode-uri-component.js deleted file mode 100644 index c7064ff..0000000 --- a/lib/decode-uri-component.js +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright 2017 Simon Lydell -// X11 (“MIT”) Licensed. (See LICENSE.) - -var decodeUriComponent = require("decode-uri-component") - -function customDecodeUriComponent(string) { - // `decodeUriComponent` turns `+` into ` `, but that's not wanted. - return decodeUriComponent(string.replace(/\+/g, "%2B")) -} - -module.exports = customDecodeUriComponent diff --git a/lib/resolve-url.js b/lib/resolve-url.js deleted file mode 100644 index 2ca8fa9..0000000 --- a/lib/resolve-url.js +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2014 Simon Lydell -// X11 (“MIT”) Licensed. (See LICENSE.) - -var url = require("url") - -function resolveUrl(/* ...urls */) { - return Array.prototype.reduce.call(arguments, function(resolved, nextUrl) { - return url.resolve(resolved, nextUrl) - }) -} - -module.exports = resolveUrl diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..38683da --- /dev/null +++ b/package-lock.json @@ -0,0 +1,541 @@ +{ + "name": "source-map-resolve", + "version": "0.6.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "Base64": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/Base64/-/Base64-1.1.0.tgz", + "integrity": "sha512-qeacf8dvGpf+XAT27ESHMh7z84uRzj/ua2pQdJg483m3bEXv/kVFtDnMgvf70BQGqzbZhR9t6BmASzKvqfJf3Q==", + "dev": true + }, + "atob": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/atob/-/atob-2.1.2.tgz", + "integrity": "sha512-Wm6ukoaOGJi/73p/cl2GvLjTI5JM1k/O14isD73YML8StrH/7/lRFgmg8nICZgD3bZZvjwCGxtMOD3wWNAu8cg==" + }, + "balanced-match": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", + "dev": true + }, + "brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, + "requires": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, + "cli": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/cli/-/cli-1.0.1.tgz", + "integrity": "sha1-IoF1NPJL+klQw01TLUjsvGIbjBQ=", + "dev": true, + "requires": { + "exit": "0.1.2", + "glob": "^7.1.1" + } + }, + "concat-map": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", + "dev": true + }, + "console-browserify": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/console-browserify/-/console-browserify-1.1.0.tgz", + "integrity": "sha1-8CQcRXMKn8YyOyBtvzjtx0HQuxA=", + "dev": true, + "requires": { + "date-now": "^0.1.4" + } + }, + "core-util-is": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", + "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=", + "dev": true + }, + "date-now": { + "version": "0.1.4", + "resolved": "https://registry.npmjs.org/date-now/-/date-now-0.1.4.tgz", + "integrity": "sha1-6vQ5/U1ISK105cx9vvIAZyueNFs=", + "dev": true + }, + "decode-uri-component": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/decode-uri-component/-/decode-uri-component-0.2.0.tgz", + "integrity": "sha1-6zkTMzRYd1y4TNGh+uBiEGu4dUU=" + }, + "deep-equal": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/deep-equal/-/deep-equal-1.1.1.tgz", + "integrity": "sha512-yd9c5AdiqVcR+JjcwUQb9DkhJc8ngNr0MahEBGvDiJw8puWab2yZlh+nkasOnZP+EGTAP6rRp2JzJhJZzvNF8g==", + "dev": true, + "requires": { + "is-arguments": "^1.0.4", + "is-date-object": "^1.0.1", + "is-regex": "^1.0.4", + "object-is": "^1.0.1", + "object-keys": "^1.1.1", + "regexp.prototype.flags": "^1.2.0" + } + }, + "define-properties": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.1.3.tgz", + "integrity": "sha512-3MqfYKj2lLzdMSf8ZIZE/V+Zuy+BgD6f164e8K2w7dgnpKArBDerGYpM46IYYcjnkdPNMjPk9A6VFB8+3SKlXQ==", + "dev": true, + "requires": { + "object-keys": "^1.0.12" + } + }, + "defined": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/defined/-/defined-1.0.0.tgz", + "integrity": "sha1-yY2bzvdWdBiOEQlpFRGZ45sfppM=", + "dev": true + }, + "dom-serializer": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/dom-serializer/-/dom-serializer-0.2.2.tgz", + "integrity": "sha512-2/xPb3ORsQ42nHYiSunXkDjPLBaEj/xTwUO4B7XCZQTRk7EBtTOPaygh10YAAh2OI1Qrp6NWfpAhzswj0ydt9g==", + "dev": true, + "requires": { + "domelementtype": "^2.0.1", + "entities": "^2.0.0" + }, + "dependencies": { + "domelementtype": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/domelementtype/-/domelementtype-2.0.1.tgz", + "integrity": "sha512-5HOHUDsYZWV8FGWN0Njbr/Rn7f/eWSQi1v7+HsUVwXgn8nWWlL64zKDkS0n8ZmQ3mlWOMuXOnR+7Nx/5tMO5AQ==", + "dev": true + }, + "entities": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/entities/-/entities-2.0.0.tgz", + "integrity": "sha512-D9f7V0JSRwIxlRI2mjMqufDrRDnx8p+eEOz7aUM9SuvF8gsBzra0/6tbjl1m8eQHrZlYj6PxqE00hZ1SAIKPLw==", + "dev": true + } + } + }, + "domelementtype": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/domelementtype/-/domelementtype-1.3.1.tgz", + "integrity": "sha512-BSKB+TSpMpFI/HOxCNr1O8aMOTZ8hT3pM3GQ0w/mWRmkhEDSFJkkyzz4XQsBV44BChwGkrDfMyjVD0eA2aFV3w==", + "dev": true + }, + "domhandler": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/domhandler/-/domhandler-2.3.0.tgz", + "integrity": "sha1-LeWaCCLVAn+r/28DLCsloqir5zg=", + "dev": true, + "requires": { + "domelementtype": "1" + } + }, + "domutils": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/domutils/-/domutils-1.5.1.tgz", + "integrity": "sha1-3NhIiib1Y9YQeeSMn3t+Mjc2gs8=", + "dev": true, + "requires": { + "dom-serializer": "0", + "domelementtype": "1" + } + }, + "entities": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/entities/-/entities-1.0.0.tgz", + "integrity": "sha1-sph6o4ITR/zeZCsk/fyeT7cSvyY=", + "dev": true + }, + "es-abstract": { + "version": "1.17.0", + "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.17.0.tgz", + "integrity": "sha512-yYkE07YF+6SIBmg1MsJ9dlub5L48Ek7X0qz+c/CPCHS9EBXfESorzng4cJQjJW5/pB6vDF41u7F8vUhLVDqIug==", + "dev": true, + "requires": { + "es-to-primitive": "^1.2.1", + "function-bind": "^1.1.1", + "has": "^1.0.3", + "has-symbols": "^1.0.1", + "is-callable": "^1.1.5", + "is-regex": "^1.0.5", + "object-inspect": "^1.7.0", + "object-keys": "^1.1.1", + "object.assign": "^4.1.0", + "string.prototype.trimleft": "^2.1.1", + "string.prototype.trimright": "^2.1.1" + } + }, + "es-to-primitive": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/es-to-primitive/-/es-to-primitive-1.2.1.tgz", + "integrity": "sha512-QCOllgZJtaUo9miYBcLChTUaHNjJF3PYs1VidD7AwiEj1kYxKeQTctLAezAOH5ZKRH0g2IgPn6KwB4IT8iRpvA==", + "dev": true, + "requires": { + "is-callable": "^1.1.4", + "is-date-object": "^1.0.1", + "is-symbol": "^1.0.2" + } + }, + "exit": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/exit/-/exit-0.1.2.tgz", + "integrity": "sha1-BjJjj42HfMghB9MKD/8aF8uhzQw=", + "dev": true + }, + "for-each": { + "version": "0.3.3", + "resolved": "https://registry.npmjs.org/for-each/-/for-each-0.3.3.tgz", + "integrity": "sha512-jqYfLp7mo9vIyQf8ykW2v7A+2N4QjeCeI5+Dz9XraiO1ign81wjiH7Fb9vSOWvQfNtmSa4H2RoQTrrXivdUZmw==", + "dev": true, + "requires": { + "is-callable": "^1.1.3" + } + }, + "fs.realpath": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", + "dev": true + }, + "function-bind": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", + "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==", + "dev": true + }, + "glob": { + "version": "7.1.6", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.6.tgz", + "integrity": "sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==", + "dev": true, + "requires": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.4", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + } + }, + "has": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz", + "integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==", + "dev": true, + "requires": { + "function-bind": "^1.1.1" + } + }, + "has-symbols": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.1.tgz", + "integrity": "sha512-PLcsoqu++dmEIZB+6totNFKq/7Do+Z0u4oT0zKOJNl3lYK6vGwwu2hjHs+68OEZbTjiUE9bgOABXbP/GvrS0Kg==", + "dev": true + }, + "htmlparser2": { + "version": "3.8.3", + "resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-3.8.3.tgz", + "integrity": "sha1-mWwosZFRaovoZQGn15dX5ccMEGg=", + "dev": true, + "requires": { + "domelementtype": "1", + "domhandler": "2.3", + "domutils": "1.5", + "entities": "1.0", + "readable-stream": "1.1" + } + }, + "inflight": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", + "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", + "dev": true, + "requires": { + "once": "^1.3.0", + "wrappy": "1" + } + }, + "inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", + "dev": true + }, + "is-arguments": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/is-arguments/-/is-arguments-1.0.4.tgz", + "integrity": "sha512-xPh0Rmt8NE65sNzvyUmWgI1tz3mKq74lGA0mL8LYZcoIzKOzDh6HmrYm3d18k60nHerC8A9Km8kYu87zfSFnLA==", + "dev": true + }, + "is-callable": { + "version": "1.1.5", + "resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.1.5.tgz", + "integrity": "sha512-ESKv5sMCJB2jnHTWZ3O5itG+O128Hsus4K4Qh1h2/cgn2vbgnLSVqfV46AeJA9D5EeeLa9w81KUXMtn34zhX+Q==", + "dev": true + }, + "is-date-object": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/is-date-object/-/is-date-object-1.0.2.tgz", + "integrity": "sha512-USlDT524woQ08aoZFzh3/Z6ch9Y/EWXEHQ/AaRN0SkKq4t2Jw2R2339tSXmwuVoY7LLlBCbOIlx2myP/L5zk0g==", + "dev": true + }, + "is-regex": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.0.5.tgz", + "integrity": "sha512-vlKW17SNq44owv5AQR3Cq0bQPEb8+kF3UKZ2fiZNOWtztYE5i0CzCZxFDwO58qAOWtxdBRVO/V5Qin1wjCqFYQ==", + "dev": true, + "requires": { + "has": "^1.0.3" + } + }, + "is-symbol": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/is-symbol/-/is-symbol-1.0.3.tgz", + "integrity": "sha512-OwijhaRSgqvhm/0ZdAcXNZt9lYdKFpcRDT5ULUuYXPoT794UNOdU+gpT6Rzo7b4V2HUl/op6GqY894AZwv9faQ==", + "dev": true, + "requires": { + "has-symbols": "^1.0.1" + } + }, + "isarray": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", + "integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8=", + "dev": true + }, + "jshint": { + "version": "2.10.3", + "resolved": "https://registry.npmjs.org/jshint/-/jshint-2.10.3.tgz", + "integrity": "sha512-d8AoXcNNYzmm7cdmulQ3dQApbrPYArtVBO6n4xOICe4QsXGNHCAKDcFORzqP52LhK61KX0VhY39yYzCsNq+bxQ==", + "dev": true, + "requires": { + "cli": "~1.0.0", + "console-browserify": "1.1.x", + "exit": "0.1.x", + "htmlparser2": "3.8.x", + "lodash": "~4.17.11", + "minimatch": "~3.0.2", + "shelljs": "0.3.x", + "strip-json-comments": "1.0.x" + } + }, + "lodash": { + "version": "4.17.15", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", + "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==", + "dev": true + }, + "minimatch": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", + "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "dev": true, + "requires": { + "brace-expansion": "^1.1.7" + } + }, + "minimist": { + "version": "1.2.5", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", + "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==", + "dev": true + }, + "object-inspect": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.7.0.tgz", + "integrity": "sha512-a7pEHdh1xKIAgTySUGgLMx/xwDZskN1Ud6egYYN3EdRW4ZMPNEDUTF+hwy2LUC+Bl+SyLXANnwz/jyh/qutKUw==", + "dev": true + }, + "object-is": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/object-is/-/object-is-1.0.2.tgz", + "integrity": "sha512-Epah+btZd5wrrfjkJZq1AOB9O6OxUQto45hzFd7lXGrpHPGE0W1k+426yrZV+k6NJOzLNNW/nVsmZdIWsAqoOQ==", + "dev": true + }, + "object-keys": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.1.1.tgz", + "integrity": "sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA==", + "dev": true + }, + "object.assign": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/object.assign/-/object.assign-4.1.0.tgz", + "integrity": "sha512-exHJeq6kBKj58mqGyTQ9DFvrZC/eR6OwxzoM9YRoGBqrXYonaFyGiFMuc9VZrXf7DarreEwMpurG3dd+CNyW5w==", + "dev": true, + "requires": { + "define-properties": "^1.1.2", + "function-bind": "^1.1.1", + "has-symbols": "^1.0.0", + "object-keys": "^1.0.11" + } + }, + "once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", + "dev": true, + "requires": { + "wrappy": "1" + } + }, + "path-is-absolute": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", + "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", + "dev": true + }, + "path-parse": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.6.tgz", + "integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==", + "dev": true + }, + "readable-stream": { + "version": "1.1.14", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz", + "integrity": "sha1-fPTFTvZI44EwhMY23SB54WbAgdk=", + "dev": true, + "requires": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.1", + "isarray": "0.0.1", + "string_decoder": "~0.10.x" + } + }, + "regexp.prototype.flags": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/regexp.prototype.flags/-/regexp.prototype.flags-1.3.0.tgz", + "integrity": "sha512-2+Q0C5g951OlYlJz6yu5/M33IcsESLlLfsyIaLJaG4FA2r4yP8MvVMJUUP/fVBkSpbbbZlS5gynbEWLipiiXiQ==", + "dev": true, + "requires": { + "define-properties": "^1.1.3", + "es-abstract": "^1.17.0-next.1" + } + }, + "resolve": { + "version": "1.14.1", + "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.14.1.tgz", + "integrity": "sha512-fn5Wobh4cxbLzuHaE+nphztHy43/b++4M6SsGFC2gB8uYwf0C8LcarfCz1un7UTW8OFQg9iNjZ4xpcFVGebDPg==", + "dev": true, + "requires": { + "path-parse": "^1.0.6" + } + }, + "resumer": { + "version": "0.0.0", + "resolved": "https://registry.npmjs.org/resumer/-/resumer-0.0.0.tgz", + "integrity": "sha1-8ej0YeQGS6Oegq883CqMiT0HZ1k=", + "dev": true, + "requires": { + "through": "~2.3.4" + } + }, + "setimmediate": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/setimmediate/-/setimmediate-1.0.5.tgz", + "integrity": "sha1-KQy7Iy4waULX1+qbg3Mqt4VvgoU=", + "dev": true + }, + "shelljs": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/shelljs/-/shelljs-0.3.0.tgz", + "integrity": "sha1-NZbmMHp4FUT1kfN9phg2DzHbV7E=", + "dev": true + }, + "simple-asyncify": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/simple-asyncify/-/simple-asyncify-1.0.0.tgz", + "integrity": "sha1-M+1SWXvkIPcr/krOHYWwqdNu7v0=", + "dev": true + }, + "string.prototype.trim": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/string.prototype.trim/-/string.prototype.trim-1.2.1.tgz", + "integrity": "sha512-MjGFEeqixw47dAMFMtgUro/I0+wNqZB5GKXGt1fFr24u3TzDXCPu7J9Buppzoe3r/LqkSDLDDJzE15RGWDGAVw==", + "dev": true, + "requires": { + "define-properties": "^1.1.3", + "es-abstract": "^1.17.0-next.1", + "function-bind": "^1.1.1" + } + }, + "string.prototype.trimleft": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/string.prototype.trimleft/-/string.prototype.trimleft-2.1.1.tgz", + "integrity": "sha512-iu2AGd3PuP5Rp7x2kEZCrB2Nf41ehzh+goo8TV7z8/XDBbsvc6HQIlUl9RjkZ4oyrW1XM5UwlGl1oVEaDjg6Ag==", + "dev": true, + "requires": { + "define-properties": "^1.1.3", + "function-bind": "^1.1.1" + } + }, + "string.prototype.trimright": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/string.prototype.trimright/-/string.prototype.trimright-2.1.1.tgz", + "integrity": "sha512-qFvWL3/+QIgZXVmJBfpHmxLB7xsUXz6HsUmP8+5dRaC3Q7oKUv9Vo6aMCRZC1smrtyECFsIT30PqBJ1gTjAs+g==", + "dev": true, + "requires": { + "define-properties": "^1.1.3", + "function-bind": "^1.1.1" + } + }, + "string_decoder": { + "version": "0.10.31", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", + "integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ=", + "dev": true + }, + "strip-json-comments": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-1.0.4.tgz", + "integrity": "sha1-HhX7ysl9Pumb8tc7TGVrCCu6+5E=", + "dev": true + }, + "tape": { + "version": "4.12.1", + "resolved": "https://registry.npmjs.org/tape/-/tape-4.12.1.tgz", + "integrity": "sha512-xoK2ariLmdGxqyXhhxfIZlr0czNB8hNJeVQmHN4D7ZyBn30GUoa4q2oM4cX8jNhnj1mtILXn1ugbfxc0tTDKtA==", + "dev": true, + "requires": { + "deep-equal": "~1.1.1", + "defined": "~1.0.0", + "for-each": "~0.3.3", + "function-bind": "~1.1.1", + "glob": "~7.1.6", + "has": "~1.0.3", + "inherits": "~2.0.4", + "is-regex": "~1.0.5", + "minimist": "~1.2.0", + "object-inspect": "~1.7.0", + "resolve": "~1.14.1", + "resumer": "~0.0.0", + "string.prototype.trim": "~1.2.1", + "through": "~2.3.8" + } + }, + "through": { + "version": "2.3.8", + "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", + "integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=", + "dev": true + }, + "wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", + "dev": true + } + } +} diff --git a/package.json b/package.json index 83fff6e..60200f2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "source-map-resolve", - "version": "0.5.1", + "version": "0.6.0", "author": "Simon Lydell", "license": "MIT", "description": "Resolve the source map and/or sources for a generated file.", @@ -18,39 +18,23 @@ "finder" ], "repository": "lydell/source-map-resolve", - "main": "lib/source-map-resolve-node.js", - "browser": "source-map-resolve.js", + "files": [ + "index.js" + ], "scripts": { - "lint": "jshint lib/ test/", - "unit": "node test/source-map-resolve.js && node test/windows.js && node test/read.js", - "test": "npm run lint && npm run unit", - "build": "node generate-source-map-resolve.js" + "lint": "jshint index.js test/", + "unit": "node test/index.js && node test/read.js && node test/windows.js", + "test": "npm run lint && npm run unit" }, "dependencies": { - "decode-uri-component": "^0.2.0", - "source-map-url": "^0.4.0", - "atob": "^2.0.0", - "urix": "^0.1.0", - "resolve-url": "^0.2.1" + "atob": "^2.1.2", + "decode-uri-component": "^0.2.0" }, "devDependencies": { - "tape": "^4.4.0", - "jshint": "~2.9.1", - "setimmediate": "^1.0.4", - "Base64": "^0.3.0", - "simple-asyncify": "^1.0.0" - }, - "testling": { - "files": "test/source-map-resolve.js", - "browsers": [ - "ie/8..latest", - "chrome/latest", - "firefox/latest", - "opera/12", - "opera/latest", - "safari/5", - "iphone/6", - "android-browser/4" - ] + "Base64": "1.1.0", + "jshint": "2.10.3", + "setimmediate": "1.0.5", + "simple-asyncify": "1.0.0", + "tape": "4.12.1" } } diff --git a/readme.md b/readme.md index a6bbd84..4ce571e 100644 --- a/readme.md +++ b/readme.md @@ -1,8 +1,6 @@ Overview [![Build Status](https://travis-ci.org/lydell/source-map-resolve.svg?branch=master)](https://travis-ci.org/lydell/source-map-resolve) ======== -[![browser support](https://ci.testling.com/lydell/source-map-resolve.png)](https://ci.testling.com/lydell/source-map-resolve) - Resolve the source map and/or sources for a generated file. ```js @@ -62,18 +60,7 @@ sourceMapResolve.resolve(code, "/js/foo.js", fs.readFile, function(error, result Installation ============ -- `npm install source-map-resolve` -- `bower install source-map-resolve` -- `component install lydell/source-map-resolve` - -Works with CommonJS, AMD and browser globals, through UMD. - -Note: This module requires `setImmediate` and `atob`. -Use polyfills if needed, such as: - -- -- - +`npm install source-map-resolve` Usage ===== @@ -200,37 +187,7 @@ _you_ give the generated code to the module), it’s up to you to look for such header when you retrieve the file (should the need arise). -Development -=========== - -Tests ------ - -First off, run `npm install` to install testing modules and browser polyfills. - -`npm test` lints the code and runs the test suite in Node.js. - -To run the tests in a browser, run `testling` (`npm install -g testling`) or -`testling -u`. - -x-package.json5 ---------------- - -package.json, component.json and bower.json are all generated from -x-package.json5 by using [`xpkg`]. Only edit x-package.json5, and remember to -run `xpkg` before commiting! - -[`xpkg`]: https://github.com/kof/node-xpkg - -Generating the browser version ------------------------------- - -source-map-resolve.js is generated from source-map-resolve-node.js and -source-map-resolve-template.js. Only edit the two latter files, _not_ -source-map-resolve.js! To generate it, run `npm run build`. - - License ======= -[The X11 (“MIT”) License](LICENSE). +[MIT](LICENSE). diff --git a/source-map-resolve.js b/source-map-resolve.js deleted file mode 100644 index 387fc19..0000000 --- a/source-map-resolve.js +++ /dev/null @@ -1,309 +0,0 @@ -// Copyright 2014, 2015, 2016, 2017 Simon Lydell -// X11 (“MIT”) Licensed. (See LICENSE.) - -// Note: source-map-resolve.js is generated from source-map-resolve-node.js and -// source-map-resolve-template.js. Only edit the two latter files, _not_ -// source-map-resolve.js! - -void (function(root, factory) { - if (typeof define === "function" && define.amd) { - define(["source-map-url", "resolve-url"], factory) - } else if (typeof exports === "object") { - var sourceMappingURL = require("source-map-url") - var resolveUrl = require("resolve-url") - module.exports = factory(sourceMappingURL, resolveUrl) - } else { - root.sourceMapResolve = factory(root.sourceMappingURL, root.resolveUrl) - } -}(this, function(sourceMappingURL, resolveUrl) { - - function callbackAsync(callback, error, result) { - setImmediate(function() { callback(error, result) }) - } - - function parseMapToJSON(string, data) { - try { - return JSON.parse(string.replace(/^\)\]\}'/, "")) - } catch (error) { - error.sourceMapData = data - throw error - } - } - - function readSync(read, url, data) { - var readUrl = url - try { - return String(read(readUrl)) - } catch (error) { - error.sourceMapData = data - throw error - } - } - - - - function resolveSourceMap(code, codeUrl, read, callback) { - var mapData - try { - mapData = resolveSourceMapHelper(code, codeUrl) - } catch (error) { - return callbackAsync(callback, error) - } - if (!mapData || mapData.map) { - return callbackAsync(callback, null, mapData) - } - var readUrl = mapData.url - read(readUrl, function(error, result) { - if (error) { - error.sourceMapData = mapData - return callback(error) - } - mapData.map = String(result) - try { - mapData.map = parseMapToJSON(mapData.map, mapData) - } catch (error) { - return callback(error) - } - callback(null, mapData) - }) - } - - function resolveSourceMapSync(code, codeUrl, read) { - var mapData = resolveSourceMapHelper(code, codeUrl) - if (!mapData || mapData.map) { - return mapData - } - mapData.map = readSync(read, mapData.url, mapData) - mapData.map = parseMapToJSON(mapData.map, mapData) - return mapData - } - - var dataUriRegex = /^data:([^,;]*)(;[^,;]*)*(?:,(.*))?$/ - var jsonMimeTypeRegex = /^(?:application|text)\/json$/ - - function resolveSourceMapHelper(code, codeUrl) { - var url = sourceMappingURL.getFrom(code) - if (!url) { - return null - } - - var dataUri = url.match(dataUriRegex) - if (dataUri) { - var mimeType = dataUri[1] - var lastParameter = dataUri[2] || "" - var encoded = dataUri[3] || "" - var data = { - sourceMappingURL: url, - url: null, - sourcesRelativeTo: codeUrl, - map: encoded - } - if (!jsonMimeTypeRegex.test(mimeType)) { - var error = new Error("Unuseful data uri mime type: " + (mimeType || "text/plain")) - error.sourceMapData = data - throw error - } - data.map = parseMapToJSON( - lastParameter === ";base64" ? atob(encoded) : decodeURIComponent(encoded), - data - ) - return data - } - - var mapUrl = resolveUrl(codeUrl, url) - return { - sourceMappingURL: url, - url: mapUrl, - sourcesRelativeTo: mapUrl, - map: null - } - } - - - - function resolveSources(map, mapUrl, read, options, callback) { - if (typeof options === "function") { - callback = options - options = {} - } - var pending = map.sources ? map.sources.length : 0 - var result = { - sourcesResolved: [], - sourcesContent: [] - } - - if (pending === 0) { - callbackAsync(callback, null, result) - return - } - - var done = function() { - pending-- - if (pending === 0) { - callback(null, result) - } - } - - resolveSourcesHelper(map, mapUrl, options, function(fullUrl, sourceContent, index) { - result.sourcesResolved[index] = fullUrl - if (typeof sourceContent === "string") { - result.sourcesContent[index] = sourceContent - callbackAsync(done, null) - } else { - var readUrl = fullUrl - read(readUrl, function(error, source) { - result.sourcesContent[index] = error ? error : String(source) - done() - }) - } - }) - } - - function resolveSourcesSync(map, mapUrl, read, options) { - var result = { - sourcesResolved: [], - sourcesContent: [] - } - - if (!map.sources || map.sources.length === 0) { - return result - } - - resolveSourcesHelper(map, mapUrl, options, function(fullUrl, sourceContent, index) { - result.sourcesResolved[index] = fullUrl - if (read !== null) { - if (typeof sourceContent === "string") { - result.sourcesContent[index] = sourceContent - } else { - var readUrl = fullUrl - try { - result.sourcesContent[index] = String(read(readUrl)) - } catch (error) { - result.sourcesContent[index] = error - } - } - } - }) - - return result - } - - var endingSlash = /\/?$/ - - function resolveSourcesHelper(map, mapUrl, options, fn) { - options = options || {} - var fullUrl - var sourceContent - var sourceRoot - for (var index = 0, len = map.sources.length; index < len; index++) { - sourceRoot = null - if (typeof options.sourceRoot === "string") { - sourceRoot = options.sourceRoot - } else if (typeof map.sourceRoot === "string" && options.sourceRoot !== false) { - sourceRoot = map.sourceRoot - } - // If the sourceRoot is the empty string, it is equivalent to not setting - // the property at all. - if (sourceRoot === null || sourceRoot === '') { - fullUrl = resolveUrl(mapUrl, map.sources[index]) - } else { - // Make sure that the sourceRoot ends with a slash, so that `/scripts/subdir` becomes - // `/scripts/subdir/`, not `/scripts/`. Pointing to a file as source root - // does not make sense. - fullUrl = resolveUrl(mapUrl, sourceRoot.replace(endingSlash, "/"), map.sources[index]) - } - sourceContent = (map.sourcesContent || [])[index] - fn(fullUrl, sourceContent, index) - } - } - - - - function resolve(code, codeUrl, read, options, callback) { - if (typeof options === "function") { - callback = options - options = {} - } - if (code === null) { - var mapUrl = codeUrl - var data = { - sourceMappingURL: null, - url: mapUrl, - sourcesRelativeTo: mapUrl, - map: null - } - var readUrl = mapUrl - read(readUrl, function(error, result) { - if (error) { - error.sourceMapData = data - return callback(error) - } - data.map = String(result) - try { - data.map = parseMapToJSON(data.map, data) - } catch (error) { - return callback(error) - } - _resolveSources(data) - }) - } else { - resolveSourceMap(code, codeUrl, read, function(error, mapData) { - if (error) { - return callback(error) - } - if (!mapData) { - return callback(null, null) - } - _resolveSources(mapData) - }) - } - - function _resolveSources(mapData) { - resolveSources(mapData.map, mapData.sourcesRelativeTo, read, options, function(error, result) { - if (error) { - return callback(error) - } - mapData.sourcesResolved = result.sourcesResolved - mapData.sourcesContent = result.sourcesContent - callback(null, mapData) - }) - } - } - - function resolveSync(code, codeUrl, read, options) { - var mapData - if (code === null) { - var mapUrl = codeUrl - mapData = { - sourceMappingURL: null, - url: mapUrl, - sourcesRelativeTo: mapUrl, - map: null - } - mapData.map = readSync(read, mapUrl, mapData) - mapData.map = parseMapToJSON(mapData.map, mapData) - } else { - mapData = resolveSourceMapSync(code, codeUrl, read) - if (!mapData) { - return null - } - } - var result = resolveSourcesSync(mapData.map, mapData.sourcesRelativeTo, read, options) - mapData.sourcesResolved = result.sourcesResolved - mapData.sourcesContent = result.sourcesContent - return mapData - } - - - - return { - resolveSourceMap: resolveSourceMap, - resolveSourceMapSync: resolveSourceMapSync, - resolveSources: resolveSources, - resolveSourcesSync: resolveSourcesSync, - resolve: resolve, - resolveSync: resolveSync, - parseMapToJSON: parseMapToJSON - } - -})); diff --git a/source-map-resolve.js.template b/source-map-resolve.js.template deleted file mode 100644 index 813198a..0000000 --- a/source-map-resolve.js.template +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2014, 2015, 2016, 2017 Simon Lydell -// X11 (“MIT”) Licensed. (See LICENSE.) - -// Note: source-map-resolve.js is generated from source-map-resolve-node.js and -// source-map-resolve-template.js. Only edit the two latter files, _not_ -// source-map-resolve.js! - -void (function(root, factory) { - if (typeof define === "function" && define.amd) { - define(["source-map-url", "resolve-url"], factory) - } else if (typeof exports === "object") { - var sourceMappingURL = require("source-map-url") - var resolveUrl = require("resolve-url") - module.exports = factory(sourceMappingURL, resolveUrl) - } else { - root.sourceMapResolve = factory(root.sourceMappingURL, root.resolveUrl) - } -}(this, function(sourceMappingURL, resolveUrl) { - - {{source-map-resolve-node.js}} - -})); diff --git a/test/common.js b/test/common.js index 8616c17..3416179 100644 --- a/test/common.js +++ b/test/common.js @@ -1,10 +1,19 @@ -// Copyright 2014 Simon Lydell -// X11 (“MIT”) Licensed. (See LICENSE.) - -function u(url) { +function u1(url) { return "code\n/*# sourceMappingURL=" + url + " */" } +function u2(url) { + return "code\n//# sourceMappingURL=" + url +} + +function u3(url) { + return "code\n/*\n# sourceMappingURL=" + url + "\n*/" +} + +function u4(url) { + return "code\n/*\n//# sourceMappingURL=" + url + "\n*/" +} + function read(x) { return function() { return x @@ -20,7 +29,10 @@ function identity(x) { } module.exports = { - u: u, + u1: u1, + u2: u2, + u3: u3, + u4: u4, read: read, Throws: Throws, identity: identity diff --git a/test/source-map-resolve.js b/test/index.js similarity index 91% rename from test/source-map-resolve.js rename to test/index.js index f61c006..a66b83d 100644 --- a/test/source-map-resolve.js +++ b/test/index.js @@ -1,10 +1,10 @@ -// Copyright 2014, 2015, 2016, 2017 Simon Lydell -// X11 (“MIT”) Licensed. (See LICENSE.) - var test = require("tape") var asyncify = require("simple-asyncify") var common = require("./common") -var u = common.u +var u1 = common.u1 +var u2 = common.u2 +var u3 = common.u3 +var u4 = common.u4 var read = common.read var Throws = common.Throws var identity = common.identity @@ -61,34 +61,42 @@ var map = { sources: [], names: [] }, + utf8 : { + mappings: "AAAA", + sources: ["foo.js"], + sourcesContent: ["中文😊"], + names: [] + }, empty: {} } map.simpleString = JSON.stringify(map.simple) map.XSSIsafe = ")]}'" + map.simpleString var code = { - fileRelative: u("foo.js.map"), - domainRelative: u("/foo.js.map"), - schemeRelative: u("//foo.org/foo.js.map"), - absolute: u("https://foo.org/foo.js.map"), - dataUri: u("data:application/json," + + fileRelative: u1("foo.js.map"), + domainRelative: u2("/foo.js.map"), + schemeRelative: u3("//foo.org/foo.js.map"), + absolute: u4("https://foo.org/foo.js.map"), + dataUri: u1("data:application/json," + "%7B%22mappings%22%3A%22AAAA%22%2C%22sources%22%3A%5B%22" + "foo.js%22%5D%2C%22names%22%3A%5B%5D%7D"), - base64: u("data:application/json;base64," + - "eyJtYXBwaW5ncyI6IkFBQUEiLCJzb3VyY2VzIjpbImZvby5qcyJdLCJuYW1lcyI6W119"), - dataUriText: u("data:text/json," + + base64: u2("data:application/json;base64," + + "eyJtYXBwaW5ncyI6IkFBQUEiLCJzb3VyY2VzIjpbImZvby5qcyJdLCJzb3VyY2VzQ29udGVudCI6WyLkuK3mlofwn5iKIl0sIm5hbWVzIjpbXX0="), // jshint ignore:line + base64InvalidUtf8: u3("data:application/json;base64,abc"), + dataUriText: u4("data:text/json," + "%7B%22mappings%22%3A%22AAAA%22%2C%22sources%22%3A%5B%22" + "foo.js%22%5D%2C%22names%22%3A%5B%5D%7D"), - dataUriParameter: u("data:application/json;charset=UTF-8;foo=bar," + + dataUriParameter: u1("data:application/json;charset=UTF-8;foo=bar," + "%7B%22mappings%22%3A%22AAAA%22%2C%22sources%22%3A%5B%22" + "foo.js%22%5D%2C%22names%22%3A%5B%5D%7D"), - dataUriNoMime: u("data:,foo"), - dataUriInvalidMime: u("data:text/html,foo"), - dataUriInvalidJSON: u("data:application/json,foo"), - dataUriXSSIsafe: u("data:application/json," + ")%5D%7D%27" + + dataUriNoMime: u2("data:,foo"), + dataUriInvalidMime: u3("data:text/html,foo"), + dataUriInvalidJSON: u4("data:application/json,foo"), + dataUriInvalidCode: u1("data:application/json,%"), + dataUriXSSIsafe: u2("data:application/json," + ")%5D%7D%27" + "%7B%22mappings%22%3A%22AAAA%22%2C%22sources%22%3A%5B%22" + "foo.js%22%5D%2C%22names%22%3A%5B%5D%7D"), - dataUriEmpty: u("data:"), + dataUriEmpty: u3("data:"), noMap: "" } @@ -99,7 +107,7 @@ function testResolveSourceMap(method, sync) { var codeUrl = "http://example.com/a/b/c/foo.js" - t.plan(1 + 12*3 + 6*4) + t.plan(1 + 12*3 + 8*4) t.equal(typeof method, "function", "is a function") @@ -171,14 +179,27 @@ function testResolveSourceMap(method, sync) { t.error(error) t.deepEqual(result, { sourceMappingURL: "data:application/json;base64," + - "eyJtYXBwaW5ncyI6IkFBQUEiLCJzb3VyY2VzIjpbImZvby5qcyJdLCJuYW1lcyI6W119", + "eyJtYXBwaW5ncyI6IkFBQUEiLCJzb3VyY2VzIjpbImZvby5qcyJdLCJzb3VyY2VzQ29udGVudCI6WyLkuK3mlofwn5iKIl0sIm5hbWVzIjpbXX0=", // jshint ignore:line url: null, sourcesRelativeTo: codeUrl, - map: map.simple + map: map.utf8 }, "base64") isAsync() }) + method(code.base64InvalidUtf8, codeUrl, wrap(Throws), function(error, result) { + t.deepEqual(error.sourceMapData, { + sourceMappingURL: "data:application/json;base64,abc", + url: null, + sourcesRelativeTo: codeUrl, + map: "abc" + }, "base64InvalidUtf8 .sourceMapData") + t.ok(error instanceof TypeError && error.message !== "data:application/json;base64,abc", + "base64InvalidUtf8") + t.notOk(result) + isAsync() + }) + method(code.dataUriText, codeUrl, wrap(Throws), function(error, result) { t.error(error) t.deepEqual(result, { @@ -242,6 +263,19 @@ function testResolveSourceMap(method, sync) { isAsync() }) + method(code.dataUriInvalidCode, codeUrl, wrap(Throws), function(error, result) { + t.deepEqual(error.sourceMapData, { + sourceMappingURL: "data:application/json,%", + url: null, + sourcesRelativeTo: codeUrl, + map: "%" + }, "dataUriInvalidCode .sourceMapData") + t.ok(error instanceof URIError && error.message !== "data:application/json,%", + "dataUriInvalidCode") + t.notOk(result) + isAsync() + }) + method(code.dataUriXSSIsafe, codeUrl, wrap(Throws), function(error, result) { t.error(error) t.deepEqual(result, { @@ -599,7 +633,7 @@ function testResolve(method, sync) { var codeUrl = "http://example.com/a/b/c/foo.js" - t.plan(1 + 15*3 + 21*4 + 4) + t.plan(1 + 15*3 + 23*4 + 4) t.equal(typeof method, "function", "is a function") @@ -683,16 +717,29 @@ function testResolve(method, sync) { t.error(error) t.deepEqual(result, { sourceMappingURL: "data:application/json;base64," + - "eyJtYXBwaW5ncyI6IkFBQUEiLCJzb3VyY2VzIjpbImZvby5qcyJdLCJuYW1lcyI6W119", + "eyJtYXBwaW5ncyI6IkFBQUEiLCJzb3VyY2VzIjpbImZvby5qcyJdLCJzb3VyY2VzQ29udGVudCI6WyLkuK3mlofwn5iKIl0sIm5hbWVzIjpbXX0=", // jshint ignore:line url: null, sourcesRelativeTo: codeUrl, - map: map.simple, + map: map.utf8, sourcesResolved: ["http://example.com/a/b/c/foo.js"], - sourcesContent: ["http://example.com/a/b/c/foo.js"] + sourcesContent: ["中文😊"] }, "base64") isAsync() }) + method(code.base64InvalidUtf8, codeUrl, wrap(Throws), function(error, result) { + t.deepEqual(error.sourceMapData, { + sourceMappingURL: "data:application/json;base64,abc", + url: null, + sourcesRelativeTo: codeUrl, + map: "abc" + }, "base64InvalidUtf8 .sourceMapData") + t.ok(error instanceof TypeError && error.message !== "data:application/json;base64,abc", + "base64InvalidUtf8") + t.notOk(result) + isAsync() + }) + method(code.dataUriText, codeUrl, wrapMap(Throws, identity), function(error, result) { t.error(error) t.deepEqual(result, { @@ -760,6 +807,19 @@ function testResolve(method, sync) { isAsync() }) + method(code.dataUriInvalidCode, codeUrl, wrap(Throws), function(error, result) { + t.deepEqual(error.sourceMapData, { + sourceMappingURL: "data:application/json,%", + url: null, + sourcesRelativeTo: codeUrl, + map: "%" + }, "dataUriInvalidCode .sourceMapData") + t.ok(error instanceof URIError && error.message !== "data:application/json,%", + "dataUriInvalidCode") + t.notOk(result) + isAsync() + }) + method(code.dataUriXSSIsafe, codeUrl, wrapMap(Throws, identity), function(error, result) { t.error(error) t.deepEqual(result, { diff --git a/test/read.js b/test/read.js index 6bf2dad..e303630 100644 --- a/test/read.js +++ b/test/read.js @@ -1,10 +1,7 @@ -// Copyright 2017 Simon Lydell -// X11 (“MIT”) Licensed. (See LICENSE.) - var test = require("tape") var asyncify = require("simple-asyncify") var common = require("./common") -var u = common.u +var u1 = common.u1 var sourceMapResolve = require("../") @@ -38,7 +35,7 @@ function testResolveSourceMap(method, sync) { "built files/operators map.json": "{}" }) - method(u(mapUrl), codeUrl, read, function(error) { + method(u1(mapUrl), codeUrl, read, function(error) { t.error(error) }) @@ -93,7 +90,7 @@ function testResolve(method, sync) { "source files/operators:+-<>%.coffee": "source code" }) - method(u(mapUrl), codeUrl, read, function(error) { + method(u1(mapUrl), codeUrl, read, function(error) { t.error(error) }) diff --git a/test/windows.js b/test/windows.js index 611ec7d..5c3219e 100644 --- a/test/windows.js +++ b/test/windows.js @@ -1,11 +1,10 @@ -// Copyright 2014 Simon Lydell -// X11 (“MIT”) Licensed. (See LICENSE.) - var path = require("path") var test = require("tape") var asyncify = require("simple-asyncify") var common = require("./common") -var u = common.u +var u1 = common.u1 +var u2 = common.u2 +var u3 = common.u3 var read = common.read var identity = common.identity @@ -29,7 +28,7 @@ function testResolveSourceMap(method, sync) { var map = {} var readMap = wrap(read(JSON.stringify(map))) - method(u("foo.js.map"), codeUrl, readMap, function(error, result) { + method(u1("foo.js.map"), codeUrl, readMap, function(error, result) { t.error(error) t.deepEqual(result, { sourceMappingURL: "foo.js.map", @@ -39,7 +38,7 @@ function testResolveSourceMap(method, sync) { }) }) - method(u("/foo.js.map"), codeUrl, readMap, function(error, result) { + method(u2("/foo.js.map"), codeUrl, readMap, function(error, result) { t.error(error) t.deepEqual(result, { sourceMappingURL: "/foo.js.map", @@ -49,7 +48,7 @@ function testResolveSourceMap(method, sync) { }) }) - method(u("../foo.js.map"), codeUrl, readMap, function(error, result) { + method(u3("../foo.js.map"), codeUrl, readMap, function(error, result) { t.error(error) t.deepEqual(result, { sourceMappingURL: "../foo.js.map", @@ -122,7 +121,7 @@ function testResolve(method, sync) { } var readMap = wrapMap(read(JSON.stringify(map)), identity) - method(u("foo.js.map"), codeUrl, readMap, function(error, result) { + method(u1("foo.js.map"), codeUrl, readMap, function(error, result) { t.error(error) t.deepEqual(result, { sourceMappingURL: "foo.js.map", @@ -134,7 +133,7 @@ function testResolve(method, sync) { }) }) - method(u("/foo.js.map"), codeUrl, readMap, function(error, result) { + method(u2("/foo.js.map"), codeUrl, readMap, function(error, result) { t.error(error) t.deepEqual(result, { sourceMappingURL: "/foo.js.map", @@ -146,7 +145,7 @@ function testResolve(method, sync) { }) }) - method(u("../foo.js.map"), codeUrl, readMap, function(error, result) { + method(u3("../foo.js.map"), codeUrl, readMap, function(error, result) { t.error(error) t.deepEqual(result, { sourceMappingURL: "../foo.js.map", diff --git a/x-package.json5 b/x-package.json5 deleted file mode 100644 index 957399f..0000000 --- a/x-package.json5 +++ /dev/null @@ -1,83 +0,0 @@ -{ - name: "source-map-resolve", - version: "0.5.1", - author: "Simon Lydell", - license: "MIT", - description: "Resolve the source map and/or sources for a generated file.", - keywords: [ - "source map", - "sourcemap", - "source", - "map", - "sourceMappingURL", - "resolve", - "resolver", - "locate", - "locator", - "find", - "finder" - ], - overlay: { - npm: { - repository: "lydell/source-map-resolve", - main: "lib/source-map-resolve-node.js", - browser: "source-map-resolve.js", - scripts: { - lint: "jshint lib/ test/", - unit: "node test/source-map-resolve.js && node test/windows.js", - test: "npm run lint && npm run unit", - build: "node generate-source-map-resolve.js" - }, - dependencies: { - "source-map-url": "^0.4.0", - "atob": "^2.0.0", - "urix": "^0.1.0", - // resolve-url is a dependency in package.json even though it is not - // needed in Node.js, to allow using npm as a package manager for - // browser projects too. - "resolve-url": "^0.2.1" - }, - devDependencies: { - "tape": "^4.4.0", - "jshint": "~2.9.1", - "setimmediate": "^1.0.4", - "Base64": "^0.3.0", - "simple-asyncify": "^1.0.0" - }, - testling: { - files: "test/source-map-resolve.js", - browsers: [ - "ie/8..latest", - "chrome/latest", - "firefox/latest", - "opera/12", - "opera/latest", - "safari/5", - "iphone/6", - "android-browser/4" - ] - } - }, - component: { - repo: "lydell/source-map-resolve", - main: "source-map-resolve.js", - scripts: [ - "source-map-resolve.js" - ], - dependencies: { - "lydell/source-map-url": "~0.4.0", - "lydell/resolve-url": "~0.2.1" - } - }, - bower: { - authors: ["Simon Lydell"], - ignore: [ - ".*" - ], - dependencies: { - "source-map-url": "^0.4.0", - "resolve-url": "^0.2.1" - } - } - } -}