From c81d612c7015ba3cf92b41ed138fa502abd03a5f Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Tue, 30 Oct 2018 11:54:20 -0600 Subject: [PATCH 1/2] feat: moved some useful tools to common --- src/core/client/embed/StreamEmbed.ts | 10 +++------- src/core/client/embed/tsconfig.json | 6 +++++- src/core/client/embed/utils/index.ts | 2 -- .../embed => common}/utils/ensureEndSlash.spec.ts | 0 .../{client/embed => common}/utils/ensureEndSlash.ts | 0 .../embed => common}/utils/ensureNoEndSlash.spec.ts | 0 .../{client/embed => common}/utils/ensureNoEndSlash.ts | 0 src/core/common/utils/index.ts | 2 ++ .../middleware/passport/strategies/oidc/discover.ts | 5 +++-- 9 files changed, 13 insertions(+), 12 deletions(-) rename src/core/{client/embed => common}/utils/ensureEndSlash.spec.ts (100%) rename src/core/{client/embed => common}/utils/ensureEndSlash.ts (100%) rename src/core/{client/embed => common}/utils/ensureNoEndSlash.spec.ts (100%) rename src/core/{client/embed => common}/utils/ensureNoEndSlash.ts (100%) diff --git a/src/core/client/embed/StreamEmbed.ts b/src/core/client/embed/StreamEmbed.ts index 6f89de186..58aac27fa 100644 --- a/src/core/client/embed/StreamEmbed.ts +++ b/src/core/client/embed/StreamEmbed.ts @@ -1,6 +1,9 @@ import { EventEmitter2 } from "eventemitter2"; import qs from "query-string"; +import ensureNoEndSlash from "talk-common/utils/ensureNoEndSlash"; +import urls from "talk-framework/helpers/urls"; + import { Decorator, withAutoHeight, @@ -15,13 +18,6 @@ import PymControl, { defaultPymControlFactory, PymControlFactory, } from "./PymControl"; -import { ensureNoEndSlash } from "./utils"; - -// This is importing the url helper from the framework using a relative path -// import because the ts paths are not configured to use the framework for this -// target. -// TODO: (wyattjoh) replace with import from framework when we include it in the config. -import urls from "../framework/helpers/urls"; export interface StreamEmbedConfig { storyID?: string; diff --git a/src/core/client/embed/tsconfig.json b/src/core/client/embed/tsconfig.json index ce6279923..3229d8a13 100644 --- a/src/core/client/embed/tsconfig.json +++ b/src/core/client/embed/tsconfig.json @@ -3,7 +3,11 @@ "compilerOptions": { "lib": ["dom", "es5"], "types": ["jest", "node"], - "paths": {} + "baseUrl": "./", + "paths": { + "talk-common/*": ["../../common/*"], + "talk-framework/*": ["../framework/*"] + } }, "include": [ "./**/*", diff --git a/src/core/client/embed/utils/index.ts b/src/core/client/embed/utils/index.ts index 18b20360b..13c551665 100644 --- a/src/core/client/embed/utils/index.ts +++ b/src/core/client/embed/utils/index.ts @@ -1,6 +1,4 @@ export { default as buildURL } from "./buildURL"; -export { default as ensureEndSlash } from "./ensureEndSlash"; -export { default as ensureNoEndSlash } from "./ensureNoEndSlash"; export { default as startsWith } from "./startsWith"; export { default as prefixStorage } from "./prefixStorage"; export { default as parseHashQuery } from "./parseHashQuery"; diff --git a/src/core/client/embed/utils/ensureEndSlash.spec.ts b/src/core/common/utils/ensureEndSlash.spec.ts similarity index 100% rename from src/core/client/embed/utils/ensureEndSlash.spec.ts rename to src/core/common/utils/ensureEndSlash.spec.ts diff --git a/src/core/client/embed/utils/ensureEndSlash.ts b/src/core/common/utils/ensureEndSlash.ts similarity index 100% rename from src/core/client/embed/utils/ensureEndSlash.ts rename to src/core/common/utils/ensureEndSlash.ts diff --git a/src/core/client/embed/utils/ensureNoEndSlash.spec.ts b/src/core/common/utils/ensureNoEndSlash.spec.ts similarity index 100% rename from src/core/client/embed/utils/ensureNoEndSlash.spec.ts rename to src/core/common/utils/ensureNoEndSlash.spec.ts diff --git a/src/core/client/embed/utils/ensureNoEndSlash.ts b/src/core/common/utils/ensureNoEndSlash.ts similarity index 100% rename from src/core/client/embed/utils/ensureNoEndSlash.ts rename to src/core/common/utils/ensureNoEndSlash.ts diff --git a/src/core/common/utils/index.ts b/src/core/common/utils/index.ts index 4d6df9fc5..dade47431 100644 --- a/src/core/common/utils/index.ts +++ b/src/core/common/utils/index.ts @@ -3,3 +3,5 @@ export { default as animationFrame } from "./animationFrame"; export { default as pascalCase } from "./pascalCase"; export { default as oncePerFrame } from "./oncePerFrame"; export { default as isBeforeDate } from "./isBeforeDate"; +export { default as ensureEndSlash } from "./ensureEndSlash"; +export { default as ensureNoEndSlash } from "./ensureNoEndSlash"; diff --git a/src/core/server/app/middleware/passport/strategies/oidc/discover.ts b/src/core/server/app/middleware/passport/strategies/oidc/discover.ts index 3fe4679a6..844969bd3 100644 --- a/src/core/server/app/middleware/passport/strategies/oidc/discover.ts +++ b/src/core/server/app/middleware/passport/strategies/oidc/discover.ts @@ -1,6 +1,8 @@ import fetch from "node-fetch"; import { URL } from "url"; +import { ensureNoEndSlash } from "talk-common/utils"; + /** * Configuration that Talk is expecting. */ @@ -35,8 +37,7 @@ export async function discover( // Any provider MUST provide a .well-known url that is JSON parsable based // on the issuer: https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfig const configurationURL = - issuer.origin + - issuer.pathname.replace(/\/$/, "") + + ensureNoEndSlash(issuer.origin + issuer.pathname) + "/.well-known/openid-configuration"; const res = await fetch(configurationURL); From 625ef9cbeea141e87b2d7e0758fb9d31431627c1 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Thu, 1 Nov 2018 20:28:09 +0100 Subject: [PATCH 2/2] refactor: deduplicate embed utils --- .../embed/decorators/withSetCommentID.ts | 2 +- src/core/client/embed/utils/buildURL.spec.ts | 18 -------------- src/core/client/embed/utils/buildURL.ts | 17 ------------- src/core/client/embed/utils/index.ts | 2 -- .../client/embed/utils/parseHashQuery.spec.ts | 24 ------------------- src/core/client/embed/utils/parseHashQuery.ts | 5 ---- 6 files changed, 1 insertion(+), 67 deletions(-) delete mode 100644 src/core/client/embed/utils/buildURL.spec.ts delete mode 100644 src/core/client/embed/utils/buildURL.ts delete mode 100644 src/core/client/embed/utils/parseHashQuery.spec.ts delete mode 100644 src/core/client/embed/utils/parseHashQuery.ts diff --git a/src/core/client/embed/decorators/withSetCommentID.ts b/src/core/client/embed/decorators/withSetCommentID.ts index b7f0ddc9e..c656fca8a 100644 --- a/src/core/client/embed/decorators/withSetCommentID.ts +++ b/src/core/client/embed/decorators/withSetCommentID.ts @@ -1,6 +1,6 @@ import qs from "query-string"; -import { buildURL } from "../utils"; +import { buildURL } from "talk-framework/utils"; import { Decorator } from "./types"; function getCurrentCommentID() { diff --git a/src/core/client/embed/utils/buildURL.spec.ts b/src/core/client/embed/utils/buildURL.spec.ts deleted file mode 100644 index 07e4e2ddc..000000000 --- a/src/core/client/embed/utils/buildURL.spec.ts +++ /dev/null @@ -1,18 +0,0 @@ -import buildURL from "./buildURL"; - -it("should default to window.location", () => { - const url = buildURL(); - expect(url).toBe("http://localhost/"); -}); - -it("should build from parameters", () => { - const url = buildURL({ - protocol: "https", - hostname: "hostname", - port: "8080", - pathname: "/pathname", - search: "search", - hash: "#hash", - }); - expect(url).toBe("https//hostname:8080/pathname?search#hash"); -}); diff --git a/src/core/client/embed/utils/buildURL.ts b/src/core/client/embed/utils/buildURL.ts deleted file mode 100644 index 77353427f..000000000 --- a/src/core/client/embed/utils/buildURL.ts +++ /dev/null @@ -1,17 +0,0 @@ -export default function buildURL({ - protocol = window.location.protocol, - hostname = window.location.hostname, - port = window.location.port, - pathname = window.location.pathname, - search = window.location.search, - hash = window.location.hash, -} = {}) { - if (search && search[0] !== "?") { - search = `?${search}`; - } else if (search === "?") { - search = ""; - } - return `${protocol}//${hostname}${ - port ? `:${port}` : "" - }${pathname}${search}${hash}`; -} diff --git a/src/core/client/embed/utils/index.ts b/src/core/client/embed/utils/index.ts index 13c551665..72a2c8889 100644 --- a/src/core/client/embed/utils/index.ts +++ b/src/core/client/embed/utils/index.ts @@ -1,4 +1,2 @@ -export { default as buildURL } from "./buildURL"; export { default as startsWith } from "./startsWith"; export { default as prefixStorage } from "./prefixStorage"; -export { default as parseHashQuery } from "./parseHashQuery"; diff --git a/src/core/client/embed/utils/parseHashQuery.spec.ts b/src/core/client/embed/utils/parseHashQuery.spec.ts deleted file mode 100644 index c90e0be9e..000000000 --- a/src/core/client/embed/utils/parseHashQuery.spec.ts +++ /dev/null @@ -1,24 +0,0 @@ -import parseHashQuery from "./parseHashQuery"; - -it("should parse hash", () => { - const testCases: Array<[string, ReturnType]> = [ - [ - "#commentID=comment-id", - { - commentID: "comment-id", - }, - ], - [ - "#commentID=comment-id&storyURL=story-url", - { - commentID: "comment-id", - storyURL: "story-url", - }, - ], - ["#", {}], - ["", {}], - ]; - testCases.forEach(tc => { - expect(parseHashQuery(tc[0])).toEqual(tc[1]); - }); -}); diff --git a/src/core/client/embed/utils/parseHashQuery.ts b/src/core/client/embed/utils/parseHashQuery.ts deleted file mode 100644 index daf5f5a5a..000000000 --- a/src/core/client/embed/utils/parseHashQuery.ts +++ /dev/null @@ -1,5 +0,0 @@ -import qs from "query-string"; - -export default function parseQueryHash(hash: string): Record { - return qs.parse(hash); -}