-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(extract): adds basic support for ESM urls
- Loading branch information
Showing
8 changed files
with
142 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* eslint-disable security/detect-object-injection */ | ||
|
||
/** | ||
* Given a module string returns in an object | ||
* - the module name | ||
* - the protocol (when encoded in the string) | ||
* - the mimeType (when encoded in the string) | ||
* | ||
* See https://nodejs.org/api/esm.html#esm_urls | ||
* | ||
* would've loved to use url.URL here, but that doesn't extract the mime type | ||
* (if there's a default node API that does this I'm all ears) | ||
* | ||
* @param {string} pString | ||
* @returns {any} | ||
*/ | ||
module.exports = function extractModuleAttributes(pString) { | ||
let lReturnValue = { module: pString }; | ||
const lModuleAttributes = pString.match( | ||
// eslint-disable-next-line security/detect-unsafe-regex, unicorn/no-unsafe-regex | ||
/^(node:|file:|data:)?(([^,]+),)?(.+)$/ | ||
); | ||
const lProtocolPosition = 1; | ||
const lMimeTypePosition = 3; | ||
const lModulePosition = 4; | ||
|
||
if (lModuleAttributes) { | ||
lReturnValue.module = lModuleAttributes[lModulePosition]; | ||
if (lModuleAttributes[lProtocolPosition]) { | ||
lReturnValue.protocol = lModuleAttributes[lProtocolPosition]; | ||
} | ||
if (lModuleAttributes[lMimeTypePosition]) { | ||
lReturnValue.mimeType = lModuleAttributes[lMimeTypePosition]; | ||
} | ||
} | ||
return lReturnValue; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const expect = require("chai").expect; | ||
const extractModuleAttributes = require("../../../src/extract/utl/extract-module-attributes"); | ||
|
||
describe("extract/utl/extract-module-attributes", () => { | ||
it("leaves regular module specifications alone", () => { | ||
expect(extractModuleAttributes("protodash")).to.deep.equal({ | ||
module: "protodash", | ||
}); | ||
}); | ||
|
||
it("extracts the protocol if there is one", () => { | ||
expect(extractModuleAttributes("node:fs")).to.deep.equal({ | ||
module: "fs", | ||
protocol: "node:", | ||
}); | ||
}); | ||
|
||
it("leaves things alone the protocol is unknown", () => { | ||
expect(extractModuleAttributes("nod:fs")).to.deep.equal({ | ||
module: "nod:fs", | ||
}); | ||
}); | ||
|
||
it("manages empty strings gracefully", () => { | ||
expect(extractModuleAttributes("")).to.deep.equal({ | ||
module: "", | ||
}); | ||
}); | ||
|
||
it("extracts both protocol and mimeType when they're in the URI", () => { | ||
expect( | ||
extractModuleAttributes("data:application/json,gegevens.json") | ||
).to.deep.equal({ | ||
module: "gegevens.json", | ||
protocol: "data:", | ||
mimeType: "application/json", | ||
}); | ||
}); | ||
|
||
it("handles emtpy mimeTypes gracefulley", () => { | ||
expect(extractModuleAttributes("data:,gegevens.json")).to.deep.equal({ | ||
module: ",gegevens.json", | ||
protocol: "data:", | ||
}); | ||
}); | ||
|
||
it("when protocol separator is mistyped, returns it as part of the module name", () => { | ||
expect( | ||
extractModuleAttributes("data:application/json;gegevens.json") | ||
).to.deep.equal({ | ||
module: "application/json;gegevens.json", | ||
protocol: "data:", | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters