-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from Financial-Times/use-next-session-proxy
Use next session proxy
- Loading branch information
Showing
17 changed files
with
188 additions
and
283 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
.idea/ | ||
node_modules/ | ||
bower_components/ | ||
test/app/public/ | ||
.eslintrc.js | ||
.editorconfig | ||
.eslintrc.js | ||
|
||
/.idea/ | ||
/bower_components/ | ||
/node_modules/ | ||
/test/app/public/ |
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 |
---|---|---|
@@ -1,10 +1,6 @@ | ||
include n.Makefile | ||
|
||
test: verify | ||
karma start | ||
unit-test: | ||
karma start test/karma.conf.js | ||
|
||
test-app: | ||
rm -rf test/app/public/ | ||
mkdir test/app/public/ | ||
browserify test/app/main.js --debug --transform debowerify > test/app/public/bundle.js | ||
node test/app/server.js | ||
test: verify unit-test |
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 |
---|---|---|
@@ -1,9 +1,3 @@ | ||
machine: | ||
node: | ||
version: 4.4.7 | ||
deployment: | ||
release: | ||
tag: /^v[0-9]+(.[0-9]+)*/ | ||
owner: Financial-Times | ||
commands: | ||
- make npm-publish |
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 |
---|---|---|
@@ -1,55 +1,78 @@ | ||
'use strict'; | ||
const request = require('./src/request'); | ||
const cache = require('./src/cache'); | ||
const promises = {}; | ||
|
||
function uuid (){ | ||
const uuid = cache('uuid') | ||
|
||
if (uuid){ | ||
return Promise.resolve({ | ||
uuid:uuid | ||
}); | ||
import request from './src/request'; | ||
import cache from './src/cache'; | ||
|
||
const requests = {}; | ||
|
||
// DEPRECATED: use the secure session ID, via getSessionId | ||
const getCookie = () => { | ||
return (/FTSession=([^;]+)/.exec(document.cookie) || [null, ''])[1]; | ||
}; | ||
|
||
const getSessionId = () => { | ||
const [, sessionId] = /FTSession_s=([^;]+)/.exec(document.cookie) || []; | ||
return sessionId; | ||
}; | ||
|
||
const getUuid = () => { | ||
const cachedUUID = cache('uuid'); | ||
if (cachedUUID) { | ||
return Promise.resolve({ uuid: cachedUUID }); | ||
} | ||
if (!promises.uuid) { | ||
promises.uuid = request('/uuid').then(function (response){ | ||
cache('uuid', response.uuid); | ||
return response; | ||
}); | ||
|
||
const sessionId = getSessionId(); | ||
if (!sessionId) { | ||
return Promise.resolve({ uuid: undefined }); | ||
} | ||
|
||
return promises.uuid; | ||
} | ||
if (!requests.uuid) { | ||
requests.uuid = request(`/sessions/s/${sessionId}`) | ||
.then(({ uuid } = {}) => { | ||
delete requests.uuid; | ||
if (uuid) { | ||
cache('uuid', uuid); | ||
} | ||
return { uuid }; | ||
}); | ||
} | ||
|
||
return requests.uuid; | ||
}; | ||
|
||
function products () { | ||
const getProducts = () => { | ||
const cachedProducts = cache('products'); | ||
const cachedUUID = cache('uuid'); | ||
|
||
if(cachedProducts && cachedUUID){ | ||
return Promise.resolve({products:cachedProducts, uuid:cachedUUID}); | ||
if (cachedProducts && cachedUUID){ | ||
return Promise.resolve({ products: cachedProducts, uuid: cachedUUID }); | ||
} | ||
|
||
if (!promises.products) { | ||
promises.products = request('/products').then(function (response) { | ||
cache('products', response.products); | ||
cache('uuid', response.uuid); | ||
return response; | ||
}); | ||
if (!requests.products) { | ||
requests.products = request('/products', { credentials: 'include' }) | ||
.then(({ products, uuid } = {}) => { | ||
delete requests.products; | ||
if (products) { | ||
cache('uuid', uuid); | ||
} | ||
if (uuid) { | ||
cache('uuid', uuid); | ||
} | ||
return { products, uuid }; | ||
}); | ||
} | ||
|
||
return promises.products; | ||
} | ||
return requests.products; | ||
}; | ||
|
||
function validate () { | ||
return request('/validate'); | ||
} | ||
// DEPRECATED: use getUuid, will only return a uuid if session is valid | ||
const validate = () => { | ||
return getUuid() | ||
.then(({ uuid }) => uuid ? true : false); | ||
}; | ||
|
||
module.exports = { | ||
uuid : uuid, | ||
validate : validate, | ||
cache : cache, | ||
products: products, | ||
cookie : function () { | ||
return (/FTSession=([^;]+)/.exec(document.cookie) || [null, ''])[1]; | ||
} | ||
export default { | ||
uuid: getUuid, | ||
products: getProducts, | ||
validate, | ||
cache, | ||
cookie: getCookie, | ||
sessionId: getSessionId | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,29 @@ | ||
'use strict'; | ||
|
||
let detailsCache = {}; | ||
|
||
|
||
function cache (name, value) { | ||
if(typeof name === 'object'){ | ||
const cache = (name, value) => { | ||
if (typeof name === 'object'){ | ||
detailsCache = name; | ||
return; | ||
} | ||
|
||
if(typeof name === 'string' && typeof value === 'string') { | ||
if (typeof name === 'string' && typeof value === 'string') { | ||
detailsCache[name] = value; | ||
return; | ||
} | ||
|
||
if(typeof name === 'string' && typeof value === 'undefined') { | ||
if (typeof name === 'string' && typeof value === 'undefined') { | ||
return detailsCache[name] || null; | ||
} | ||
|
||
if(typeof name === 'undefined' && typeof value === 'undefined') { | ||
if (typeof name === 'undefined' && typeof value === 'undefined') { | ||
return detailsCache; | ||
} | ||
|
||
throw new Error('Invalid arguments'); | ||
} | ||
|
||
cache.clear = function () { | ||
cache.clear = () => { | ||
detailsCache = {}; | ||
}; | ||
|
||
module.exports = cache; | ||
export default cache; |
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 |
---|---|---|
@@ -1,38 +1,27 @@ | ||
'use strict'; | ||
|
||
function request (url) { | ||
// if we don't have a session token cookie, don't bother.. | ||
if (document.cookie.indexOf('FTSession=') === -1) { | ||
return Promise.reject(new Error('No session cookie found')); | ||
} | ||
|
||
export default (url, { credentials = 'omit' } = {}) => { | ||
return fetch(`https://session-next.ft.com${url}`, { | ||
credentials:'include', | ||
credentials, | ||
useCorsProxy: true | ||
}).then(function (response) { | ||
if (response.ok){ | ||
return (url === '/validate') ? Promise.resolve(true) : response.json(); | ||
} else { | ||
return (url === '/validate') ? Promise.resolve(false) : Promise.reject(response.status); | ||
} | ||
|
||
}).catch(function (e) { | ||
const message = e.message || ''; | ||
if (message.indexOf('timed out') > -1 || message.indexOf('Network request failed') > -1 || message.indexOf('Not Found') > -1) { | ||
// HTTP timeouts and invalid sessions are a fact of life on the internet. | ||
// We don't want to report this to Sentry. | ||
} else { | ||
}) | ||
.then(response => { | ||
if (response.ok){ | ||
return response.json(); | ||
} else { | ||
return response.text() | ||
.then(text => { | ||
throw new Error(`Next session responded with "${text}" (${response.status})`); | ||
}); | ||
} | ||
}) | ||
.catch(err => { | ||
document.body.dispatchEvent(new CustomEvent('oErrors.log', { | ||
bubbles: true, | ||
detail: { | ||
error: e, | ||
error: err, | ||
info: { | ||
component: 'next-session-client' | ||
} | ||
} | ||
})) | ||
} | ||
}); | ||
})); | ||
}); | ||
} | ||
|
||
module.exports = request; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.