-
Notifications
You must be signed in to change notification settings - Fork 16
Http Methods #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jlacivita
wants to merge
34
commits into
main
Choose a base branch
from
feature/http-methods
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Http Methods #53
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
0f9c289
First pass at HTTP extension methods
jlacivita 97ccdaf
More features
jlacivita 1310a75
Refactor Http module to conform to Transport API
jlacivita 54a100b
Wire up Authenticate API
jlacivita e784330
Remove bespoke Core code from Extensions
jlacivita 7f55ab5
Merge branch 'main' into feature/http-methods
jlacivita 69dedd6
Fix http macro replacement to do all occurences
jlacivita f29fa0c
Remove unused commented code example
jlacivita 6005416
Move config code to template files
jlacivita 88f78d2
Remove unneeded token API
jlacivita 6e4e87d
Clean up Browser.js
jlacivita 1c7a849
Update authorize signature
jlacivita 1010d02
Merge branch 'main' into feature/http-methods
jlacivita bd0443b
Rationalize Transport.send options and transforms params
jlacivita a5482f6
Don't import any transport for `exclude-from-sdk` methods
jlacivita f1797da
Update index.mjs
jlacivita 963dd4e
Fix bug w/ mock server
jlacivita 3626bee
Add distributor to initialize()
jlacivita 6b96020
Move distributor to it's on parameter
jlacivita 9a36b11
Merge branch 'main' into feature/http-methods
jlacivita f7b9d6d
Fix MockProps for real
jlacivita 396dcff
Merge branch 'main' into feature/http-methods
jlacivita eb414f1
Update index.mjs
jlacivita 2a7c1f1
Require capability on all methods
jlacivita 60d62cb
Merge branch 'next' into feature/http-methods
jlacivita 2b0619e
chore: Cherry picked Tree-shaking from extensions branch
jlacivita c13e8fc
chore: Move HTTP Test server to src
jlacivita caad894
chore: Remove Browser.js from jest
jlacivita c4128ec
chore: Move Teardown to src
jlacivita 1f2b299
Merge branch 'next' into feature/http-methods
jlacivita fa63ed1
fix: Rollback #65
jlacivita 49107c1
fix: Remove ability for Extension SDK to set it's own host
jlacivita 18a79ff
Merge branch 'next' into feature/http-methods
jlacivita dde6a6c
fix: Remove distributor from initialize
jlacivita File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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,94 @@ | ||
| import http from 'http' | ||
| import dns from 'dns' | ||
|
|
||
| const lookup = dns.lookup | ||
|
|
||
| const config = (await import(process.env.npm_package_json, { assert: {type: "json" }})).default | ||
| const name = 'firebolt-' + config.name.split('/').pop().split('-').shift() | ||
| const rpcPath = process.env.npm_config_local_prefix + '/dist/' + name + '-open-rpc.json' | ||
| const rpc = (await import(rpcPath, {assert: { type: "json" }})).default | ||
| const endpoint = "http://localhost/" | ||
| const domain = new URL(endpoint).hostname | ||
| const port = parseInt(new URL(endpoint).port) || 80 | ||
| const httpTag = m => m.tags && m.tags.find(t => t.name === 'http') | ||
| const httpPath = m => httpTag(m) && httpTag(m)['x-http-path'] | ||
|
|
||
| console.log(`endpoint: ${endpoint}`) | ||
| console.log(`domain: ${domain}`) | ||
| console.log(`port: ${port}`) | ||
|
|
||
| dns.lookup = function (...args) { | ||
| if (args[0] === domain) { | ||
| console.log(`spoofing domain ${domain} to localhost`) | ||
| return args.pop()(null, '127.0.0.1', 4) | ||
| } | ||
| else { | ||
| return lookup(...args) | ||
| } | ||
| } | ||
|
|
||
| async function setup() { | ||
| return new Promise((resolve, reject) => { | ||
|
|
||
| const process = async (request, response) => { | ||
| const requestPath = request.url.split(endpoint).pop() | ||
| const method = rpc.methods.find(m => { | ||
| const tag = httpTag(m) | ||
| let path = tag && httpPath(m) | ||
|
|
||
| if (path && path.endsWith('/')) { | ||
| path = path.substr(0, path.length-1) | ||
| } | ||
|
|
||
| return path && requestPath.startsWith(path.split("$")[0]) | ||
| }) | ||
|
|
||
| let body | ||
|
|
||
| if (request.method === 'POST') { | ||
| const buffers = []; | ||
|
|
||
| for await (const chunk of request) { | ||
| buffers.push(chunk); | ||
| } | ||
|
|
||
| body = Buffer.concat(buffers).toString(); | ||
| } | ||
|
|
||
| response.setHeader("Content-Type", "application/json") | ||
| response.writeHead(200) | ||
|
|
||
| if (method) { | ||
| response.write(JSON.stringify(method.examples[0].result.value)) | ||
| } | ||
| else if (request.url.endsWith('/account/authenticate')) { | ||
| response.write(JSON.stringify({ | ||
| oat: "OAT", | ||
| bearerToken: "BEARER", | ||
| d: "D" | ||
| })) | ||
| } | ||
| else { | ||
| response.write('{}') | ||
| } | ||
| response.end() | ||
| } | ||
|
|
||
| const server = http.createServer(process); | ||
|
|
||
| global.__firebolt__shutdown__ = () => { | ||
| server.close() | ||
| } | ||
|
|
||
| server.listen(port, () => { | ||
| console.log("Firebolt Server Started") | ||
| resolve() | ||
| }); | ||
| }) | ||
| } | ||
|
|
||
|
|
||
| export default async function(globalConfig, projectConfig) { | ||
| await setup() | ||
| return | ||
| } |
This file contains hidden or 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,4 @@ | ||
| export default async function(globalConfig, projectConfig) { | ||
| await __firebolt__shutdown__() | ||
| return | ||
| } |
This file contains hidden or 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,25 @@ | ||
| const config = {} | ||
|
|
||
| function set(name, value) { | ||
| // if the only argument is an object, copy all of the values in | ||
| if (name && !value && typeof name === 'object') { | ||
| Object.assign(config, name) | ||
| } | ||
| else { | ||
| config[name] = value | ||
| } | ||
| } | ||
|
|
||
| function get(name) { | ||
| if (!name) { | ||
| return JSON.parse(JSON.stringify(config)) | ||
| } | ||
| else { | ||
| return config[name] | ||
| } | ||
| } | ||
|
|
||
| export default { | ||
| get, | ||
| set | ||
| } |
This file contains hidden or 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 hidden or 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 @@ | ||
| import Transport from '../Transport/index.mjs' | ||
|
|
||
| window.__firebolt = window.__firebolt || {} | ||
| const initializers = {} | ||
| const apis = {} | ||
| const queue = {} | ||
| const initialized = [] | ||
| const pending = [] | ||
| const frozen = false | ||
|
|
||
| window.__firebolt.registerExtensionSDK = (id, initializer) => { | ||
| initializers[id] = initializer | ||
|
|
||
| if (queue[id]) { | ||
| initialize(id, queue[id]) | ||
| delete queue[id] | ||
| } | ||
| } | ||
|
|
||
| // Method for handing off platform tokens to extension SDKs | ||
| registerAPI('authorize', (...args) => Transport.send('capabilities', 'authorize', {...args} )) | ||
|
|
||
| function initialize(id, config) { | ||
| if (!frozen) { | ||
| Object.freeze(apis) | ||
| } | ||
| Object.freeze(config) | ||
| if (initializers[id]) { | ||
| const getEndPoint = Transport.send('capabilities', 'endpoint', { id }) | ||
| const init = initializers[id] | ||
| delete initializers[id] | ||
| pending.push(id) | ||
| getEndPoint.then((endpoint) => { | ||
| init(endpoint, config, apis) | ||
| initialized.push(id) | ||
| delete pending[id] | ||
| }) | ||
| } | ||
| else { | ||
| queue[id] = config | ||
| } | ||
| } | ||
|
|
||
| function active() { | ||
| return Object.freeze(initialized.contact()) | ||
| } | ||
|
|
||
| export function registerAPI(name, method) { | ||
| apis[name] = method | ||
| } | ||
|
|
||
| export default { | ||
| initialize, | ||
| active | ||
| } |
This file contains hidden or 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,100 @@ | ||
| let baseUri | ||
|
|
||
| let authApiResolver | ||
| let authApiReady = new Promise( resolve => authApiResolver = resolve ) | ||
|
|
||
|
|
||
| let proxyProvider = (resource, options) => Promise.resolve({ resource, options }) | ||
|
|
||
| const join = (...args) => args.map(a => (''+a).split('/')).flat().filter(a => a!=='').join('/').replace(':/', '://') | ||
|
|
||
| function setEndpoint(endpoint) { | ||
| baseUri = endpoint | ||
| } | ||
|
|
||
| function onAuthorize(callback) { | ||
| if (authApiResolver) { | ||
| authApiResolver(callback) | ||
| authApiResolver = null | ||
| } | ||
| } | ||
|
|
||
| function getAuthorization(grants) { | ||
| return authApiReady.then(authGetter => authGetter(grants)) | ||
| } | ||
|
|
||
|
|
||
| // this gets called syncrhonously by the SDK itself, if needed | ||
| export function proxy(callback) { | ||
| proxyProvider = callback | ||
| } | ||
|
|
||
| function send(module, method, params, http = {}) { | ||
|
|
||
| let path = http.path || `/${module}/${method}` | ||
| const headers = http.headers || {} | ||
| const body = params && JSON.stringify(params) || null | ||
| let query = http.parameters || '' | ||
|
|
||
| Object.entries(params).forEach(([name, value]) => { | ||
| const re = new RegExp('\\${param.' + name + '}', 'g') | ||
| path = path.replace(re, value) | ||
| query = query.replace(re, value) | ||
| Object.keys(headers).forEach(header => { | ||
| headers[header] = headers[header].replace(re, value) | ||
| }) | ||
| }) | ||
|
|
||
| return new Promise((resolve, reject) => { | ||
| const requested = http.uses.map(capability => ({ role: 'use', capability })) | ||
| .concat(http.manages.map(capability => ({ role: 'manage', capability }))) | ||
| .concat(http.provides.map(capability => ({ role: 'provide', capability }))) | ||
|
|
||
| getAuthorization(requested).then((token) => { | ||
| // TODO: this could be a bit more robust... | ||
| Object.assign(headers, { | ||
| 'Authorization': token | ||
| }) | ||
|
|
||
| const resource = join(baseUri, path) + (query ? '?' + query : '') | ||
| const options = { | ||
| method: http.method || 'GET', | ||
| headers: headers | ||
| } | ||
|
|
||
| if (method === 'POST' && body) { | ||
| options.body = body | ||
| } | ||
|
|
||
| proxyProvider(resource, options).then( ({ resource, options }) => { | ||
| console.log(`Fetch ${method} ${resource}`) | ||
| options.body && console.log(options.body) | ||
|
|
||
| fetch(resource, options).then(response => { | ||
| if (response.ok) { | ||
| response.json().then(json => { | ||
|
jlacivita marked this conversation as resolved.
|
||
| resolve(json) | ||
| }, error => { | ||
| reject(error) | ||
| }) | ||
| } | ||
| else { | ||
| reject(new Error(response.status + ': ' + response.statusText)) | ||
| } | ||
| }).catch(error => { | ||
| reject(error) | ||
| }) | ||
| }).catch(error => { | ||
| reject(error) | ||
| }) | ||
| }).catch(error => { | ||
| reject(error) | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| export default { | ||
| send, | ||
| setEndpoint, | ||
| onAuthorize | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might be better to rewrite this as an async function, a lot of nested
.thens going onThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stylistically, I agree.
However, I've been resisting the urge to use
async/awaitbecause we haven't used them yet in the code. I just did a quick search and saw it's used in the new version of Lifecycle.ready() in the Core SDK. Have we tested that code on all of the browsers we care about?