Skip to content
This repository has been archived by the owner on May 6, 2020. It is now read-only.

Commit

Permalink
Merge branch 'use-amp-toolbox-cache-url' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
ithinkihaveacat committed Oct 8, 2018
2 parents c8c5d97 + 68f1629 commit f88d4b5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 27 deletions.
4 changes: 4 additions & 0 deletions amp-story/linter/amp-toolbox-cache-url.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module "amp-toolbox-cache-url" {
function createCacheUrl(cacheSuffix: string, url: string): Promise<string>;
export = createCacheUrl;
}
24 changes: 7 additions & 17 deletions amp-story/linter/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// <reference path="probe-image-size.d.ts" />
/// <reference path="amp-toolbox-cache-url.d.ts" />

import {readFileSync} from "fs";
import {resolve, URL} from "url";
Expand All @@ -9,6 +10,7 @@ const validator = require("amphtml-validator").newInstance(
// tslint:disable-next-line:no-var-requires
readFileSync(`${__dirname}/validator.js`).toString(),
);
import createCacheUrl = require("amp-toolbox-cache-url");
import * as cheerio from "cheerio";
import throat = require("throat");

Expand Down Expand Up @@ -279,20 +281,6 @@ function addSourceOrigin(url: string, sourceOrigin: string) {
return format(obj);
}

function buildCacheOrigin(cacheSuffix: string, url: string): string {
// console.log({cacheSuffix, url});
function convertHost(hostname: string) {
return punycode
.toASCII(hostname)
.replace(/\-/g, "--")
.replace(/\./g, "-");
}
const {parse, format} = require("url"); // use old API to work with node 6+
const obj = parse(url);
const cacheHost = `${convertHost(obj.host)}.${cacheSuffix}`;
return `https://${cacheHost}`;
}

function isJson(res: Response): Promise<Response> {
const contentType = (() => {
if (!res.headers) {
Expand Down Expand Up @@ -383,10 +371,12 @@ function canXhrSameOrigin(context: Context, xhrUrl: string) {
.then(PASS, (e: Error) => FAIL(`can't XHR [${xhrUrl}]: ${e.message} [debug: ${curl}]`));
}

function canXhrCache(context: Context, xhrUrl: string, cacheSuffix: string) {
xhrUrl = absoluteUrl(xhrUrl, context.url)!;
async function canXhrCache(context: Context, xhrUrl: string, cacheSuffix: string) {
const sourceOrigin = buildSourceOrigin(context.url);
const origin = buildCacheOrigin(cacheSuffix, context.url);
const url = await createCacheUrl(cacheSuffix, context.url);
const {parse} = require("url"); // use old API to work with node 6+
const obj = parse(url);
const origin = `${obj.protocol}//${obj.host}`;

const headers = Object.assign(
{},
Expand Down
36 changes: 26 additions & 10 deletions amp-story/linter/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions amp-story/linter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"license": "Apache-2.0",
"author": "Michael Stillwell <mjs@beebo.org>",
"dependencies": {
"amp-toolbox-cache-url": "^0.1.0",
"amphtml-validator": "^1.0.23",
"cheerio": "^1.0.0-rc.2",
"debug": "^4.0.1",
Expand Down

0 comments on commit f88d4b5

Please sign in to comment.