Skip to content

Commit

Permalink
Add support for serving esm files directly from unpkg (#152)
Browse files Browse the repository at this point in the history
The following changes have been made:
* utils getEntry()
  allow a ${v} placeholder which supports passing the version to
  unpkg and converts a "master" branch name to the "latest" version
* render
  support an "esm" type that serves the files directly
* render_code
  bypass typescript file annotation for raw files (implied by "esm")
  • Loading branch information
alexjeffburke authored and ry committed Sep 16, 2019
1 parent f812031 commit e9d6979
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
23 changes: 23 additions & 0 deletions src/app_test.js
Expand Up @@ -11,6 +11,7 @@ exports.tests = async function tests() {
assert.deepEqual(proxy("/x/install/foo/bar.js"), {
entry: {
name: "install",
type: "github",
raw: { type: "github", owner: "denoland", repo: "deno_install" },
url: "https://raw.githubusercontent.com/denoland/deno_install/master/",
repo: "https://github.com/denoland/deno_install"
Expand All @@ -20,13 +21,35 @@ exports.tests = async function tests() {
assert.deepEqual(proxy("/x/install@v0.1.2/foo/bar.js"), {
entry: {
name: "install",
type: "github",
raw: { type: "github", owner: "denoland", repo: "deno_install" },
url: "https://raw.githubusercontent.com/denoland/deno_install/v0.1.2/",
repo: "https://github.com/denoland/deno_install"
},
path: "foo/bar.js"
});

database.__test_version = {
type: "esm",
url: "https://unpkg.com/__test_version@${v}/",
repo: "https://github.com/__test_versionorg/repo"
};
assert.deepEqual(proxy("/x/__test_version@master/file.js"), {
entry: {
name: "__test_version",
type: "esm",
raw: {
type: "esm",
url: "https://unpkg.com/__test_version@${v}/",
repo: "https://github.com/__test_versionorg/repo"
},
url: "https://unpkg.com/__test_version@latest/",
repo: "https://github.com/__test_versionorg/repo"
},
path: "file.js"
});
delete database.__test_version;

let event = require("./testdata/req1.json");
const context = require("./testdata/context1.json");
let counter = 0;
Expand Down
8 changes: 8 additions & 0 deletions src/render.js
Expand Up @@ -43,6 +43,14 @@ module.exports = async function render(
} else {
return response.success(transpiled, "application/javascript");
}
} else if (entry.type === "esm") {
if (showHTMLVersion) {
return require("./render_code")(pathname, content, entry, {
raw: true
});
} else {
return response.success(content, "application/javascript");
}
} else if (showHTMLVersion) {
// Accept header present, this is a web browser, so display something
// pretty.
Expand Down
7 changes: 4 additions & 3 deletions src/render_code.js
Expand Up @@ -8,8 +8,8 @@ const { transformModuleSpecifier } = require("./transpile_code");
const { annotate } = require("./analyze_code");
const renderBreadcrumbs = require("./breadcrumbs");

function getLines(pathname, code) {
if (pathname.endsWith(".ts") || pathname.endsWith(".js")) {
function getLines(pathname, code, opts) {
if (!opts.raw && (pathname.endsWith(".ts") || pathname.endsWith(".js"))) {
try {
return {
err: null,
Expand All @@ -31,7 +31,8 @@ function getLines(pathname, code) {
module.exports = function renderCode(pathname, code, entry, opts = {}) {
const url = `https://deno.land${pathname}`;

const { err, lines: escapedLines } = getLines(pathname, code);
const { err, lines: escapedLines } = getLines(pathname, code, opts);

const lineNumberedCode = escapedLines
.map((content, i) => {
const line = i + 1;
Expand Down
16 changes: 15 additions & 1 deletion src/utils.js
Expand Up @@ -28,18 +28,32 @@ exports.walkAST = function walkAST(node, cb) {
*/
exports.getEntry = function getEntry(name, branch = "master") {
const rawEntry = DATABASE[name];
if (rawEntry.type === "url") {
if (!rawEntry) {
return null;
} else if (rawEntry.type === "url") {
return {
name,
raw: rawEntry,
type: "url",
url: rawEntry.url.replace(/\$\{b}/, branch),
repo: rawEntry.repo.replace(/\$\{b}/, branch)
};
}
if (rawEntry.type === "esm") {
const version = branch === "master" ? "latest" : branch;
return {
name,
raw: rawEntry,
type: "esm",
url: rawEntry.url.replace(/\$\{v}/, version),
repo: rawEntry.repo.replace(/\$\{v}/, version)
};
}
if (rawEntry.type === "github") {
return {
name,
raw: rawEntry,
type: "github",
url: `https://raw.githubusercontent.com/${rawEntry.owner}/${
rawEntry.repo
}/${branch}${rawEntry.path || "/"}`,
Expand Down

0 comments on commit e9d6979

Please sign in to comment.