From 81f58e3ac7b0a5f8447cc97af36dc7f130bdc6fa Mon Sep 17 00:00:00 2001 From: Akinwale Ariwodola Date: Tue, 17 Mar 2020 22:57:27 +0100 Subject: [PATCH 1/2] add connectionStringOverride parameter for resolve and claim_search --- dist/bundle.es.js | 25 +++++++++++++------------ src/lbry.js | 14 ++++++++------ src/redux/actions/claims.js | 13 +++++++------ 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/dist/bundle.es.js b/dist/bundle.es.js index de8a54b..42313e3 100644 --- a/dist/bundle.es.js +++ b/dist/bundle.es.js @@ -937,9 +937,9 @@ const Lbry = { version: () => daemonCallWithResult('version', {}), // Claim fetching and manipulation - resolve: params => daemonCallWithResult('resolve', params), + resolve: (params, connectionStringOverride = null) => daemonCallWithResult('resolve', params, connectionStringOverride), get: params => daemonCallWithResult('get', params), - claim_search: params => daemonCallWithResult('claim_search', params), + claim_search: (params, connectionStringOverride = null) => daemonCallWithResult('claim_search', params, connectionStringOverride), claim_list: params => daemonCallWithResult('claim_list', params), channel_create: params => daemonCallWithResult('channel_create', params), channel_update: params => daemonCallWithResult('channel_update', params), @@ -1039,7 +1039,7 @@ function checkAndParse(response) { }); } -function apiCall(method, params, resolve, reject) { +function apiCall(method, params, resolve, reject, connectionStringOverride = null) { const counter = new Date().getTime(); const options = { method: 'POST', @@ -1052,7 +1052,8 @@ function apiCall(method, params, resolve, reject) { }) }; - return fetch(Lbry.daemonConnectionString + '?m=' + method, options).then(checkAndParse).then(response => { + const connectionString = connectionStringOverride ? connectionStringOverride : Lbry.daemonConnectionString; + return fetch(connectionString + '?m=' + method, options).then(checkAndParse).then(response => { const error = response.error || response.result && response.result.error; if (error) { @@ -1062,11 +1063,11 @@ function apiCall(method, params, resolve, reject) { }).catch(reject); } -function daemonCallWithResult(name, params = {}) { +function daemonCallWithResult(name, params = {}, connectionStringOverride = null) { return new Promise((resolve, reject) => { apiCall(name, params, result => { resolve(result); - }, reject); + }, reject, connectionStringOverride); }); } @@ -2870,7 +2871,7 @@ function batchActions(...actions) { var _extends$5 = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; -function doResolveUris(uris, returnCachedClaims = false) { +function doResolveUris(uris, returnCachedClaims = false, connectionStringOverride = null) { return (dispatch, getState) => { const normalizedUris = uris.map(normalizeURI); const state = getState(); @@ -2896,7 +2897,7 @@ function doResolveUris(uris, returnCachedClaims = false) { const resolveInfo = {}; - lbryProxy.resolve({ urls: urisToResolve }).then(result => { + lbryProxy.resolve({ urls: urisToResolve }, connectionStringOverride).then(result => { Object.entries(result).forEach(([uri, uriResolveInfo]) => { const fallbackResolveInfo = { stream: null, @@ -2936,8 +2937,8 @@ function doResolveUris(uris, returnCachedClaims = false) { }; } -function doResolveUri(uri) { - return doResolveUris([uri]); +function doResolveUri(uri, connectionStringOverride = null) { + return doResolveUris([uri], connectionStringOverride); } function doFetchClaimListMine(page = 1, pageSize = 99999, resolve = true) { @@ -3223,7 +3224,7 @@ function doClaimSearch(options = { no_totals: true, page_size: 10, page: 1 -}) { +}, connectionStringOverride = null) { const query = createNormalizedClaimSearchKey(options); return dispatch => { dispatch({ @@ -3259,7 +3260,7 @@ function doClaimSearch(options = { }); }; - lbryProxy.claim_search(options).then(success, failure); + lbryProxy.claim_search(options, connectionStringOverride).then(success, failure); }; } diff --git a/src/lbry.js b/src/lbry.js index 146149d..ba41147 100644 --- a/src/lbry.js +++ b/src/lbry.js @@ -73,9 +73,9 @@ const Lbry: LbryTypes = { version: () => daemonCallWithResult('version', {}), // Claim fetching and manipulation - resolve: params => daemonCallWithResult('resolve', params), + resolve: (params, connectionStringOverride = null) => daemonCallWithResult('resolve', params, connectionStringOverride), get: params => daemonCallWithResult('get', params), - claim_search: params => daemonCallWithResult('claim_search', params), + claim_search: (params, connectionStringOverride = null) => daemonCallWithResult('claim_search', params, connectionStringOverride), claim_list: params => daemonCallWithResult('claim_list', params), channel_create: params => daemonCallWithResult('channel_create', params), channel_update: params => daemonCallWithResult('channel_update', params), @@ -178,7 +178,7 @@ function checkAndParse(response) { }); } -export function apiCall(method: string, params: ?{}, resolve: Function, reject: Function) { +export function apiCall(method: string, params: ?{}, resolve: Function, reject: Function, connectionStringOverride = null) { const counter = new Date().getTime(); const options = { method: 'POST', @@ -191,7 +191,8 @@ export function apiCall(method: string, params: ?{}, resolve: Function, reject: }), }; - return fetch(Lbry.daemonConnectionString + '?m=' + method, options) + const connectionString = connectionStringOverride ? connectionStringOverride : Lbry.daemonConnectionString; + return fetch(connectionString + '?m=' + method, options) .then(checkAndParse) .then(response => { const error = response.error || (response.result && response.result.error); @@ -204,7 +205,7 @@ export function apiCall(method: string, params: ?{}, resolve: Function, reject: .catch(reject); } -function daemonCallWithResult(name: string, params: ?{} = {}) { +function daemonCallWithResult(name: string, params: ?{} = {}, connectionStringOverride = null) { return new Promise((resolve, reject) => { apiCall( name, @@ -212,7 +213,8 @@ function daemonCallWithResult(name: string, params: ?{} = {}) { result => { resolve(result); }, - reject + reject, + connectionStringOverride ); }); } diff --git a/src/redux/actions/claims.js b/src/redux/actions/claims.js index de18fd9..b20f67c 100644 --- a/src/redux/actions/claims.js +++ b/src/redux/actions/claims.js @@ -15,7 +15,7 @@ import { creditsToString } from 'util/format-credits'; import { batchActions } from 'util/batch-actions'; import { createNormalizedClaimSearchKey } from 'util/claim'; -export function doResolveUris(uris: Array, returnCachedClaims: boolean = false) { +export function doResolveUris(uris: Array, returnCachedClaims: boolean = false, connectionStringOverride: string = null) { return (dispatch: Dispatch, getState: GetState) => { const normalizedUris = uris.map(normalizeURI); const state = getState(); @@ -47,7 +47,7 @@ export function doResolveUris(uris: Array, returnCachedClaims: boolean = }, } = {}; - Lbry.resolve({ urls: urisToResolve }).then((result: ResolveResponse) => { + Lbry.resolve({ urls: urisToResolve }, connectionStringOverride).then((result: ResolveResponse) => { Object.entries(result).forEach(([uri, uriResolveInfo]) => { const fallbackResolveInfo = { stream: null, @@ -90,8 +90,8 @@ export function doResolveUris(uris: Array, returnCachedClaims: boolean = }; } -export function doResolveUri(uri: string) { - return doResolveUris([uri]); +export function doResolveUri(uri: string, connectionStringOverride: string = null) { + return doResolveUris([uri], connectionStringOverride); } export function doFetchClaimListMine( @@ -426,7 +426,8 @@ export function doClaimSearch( no_totals: true, page_size: 10, page: 1, - } + }, + connectionStringOverride: string = null ) { const query = createNormalizedClaimSearchKey(options); return (dispatch: Dispatch) => { @@ -463,7 +464,7 @@ export function doClaimSearch( }); }; - Lbry.claim_search(options).then(success, failure); + Lbry.claim_search(options, connectionStringOverride).then(success, failure); }; } From 69ffd110dbf3633e5847f61f008751edec033017 Mon Sep 17 00:00:00 2001 From: Akinwale Ariwodola Date: Wed, 18 Mar 2020 18:11:03 +0100 Subject: [PATCH 2/2] update type --- dist/bundle.es.js | 26 ++++++++++++++------------ dist/flow-typed/Lbry.js | 2 ++ flow-typed/Lbry.js | 2 ++ src/lbry.js | 17 ++++++++++------- src/redux/actions/claims.js | 13 ++++++------- 5 files changed, 34 insertions(+), 26 deletions(-) diff --git a/dist/bundle.es.js b/dist/bundle.es.js index 42313e3..b538929 100644 --- a/dist/bundle.es.js +++ b/dist/bundle.es.js @@ -884,6 +884,8 @@ const Lbry = { isConnected: false, connectPromise: null, daemonConnectionString: 'http://localhost:5279', + alternateConnectionString: '', + methodsUsingAlternateConnectionString: [], apiRequestHeaders: { 'Content-Type': 'application/json-rpc' }, // Allow overriding daemon connection string (e.g. to `/api/proxy` for lbryweb) @@ -937,9 +939,9 @@ const Lbry = { version: () => daemonCallWithResult('version', {}), // Claim fetching and manipulation - resolve: (params, connectionStringOverride = null) => daemonCallWithResult('resolve', params, connectionStringOverride), + resolve: params => daemonCallWithResult('resolve', params), get: params => daemonCallWithResult('get', params), - claim_search: (params, connectionStringOverride = null) => daemonCallWithResult('claim_search', params, connectionStringOverride), + claim_search: params => daemonCallWithResult('claim_search', params), claim_list: params => daemonCallWithResult('claim_list', params), channel_create: params => daemonCallWithResult('channel_create', params), channel_update: params => daemonCallWithResult('channel_update', params), @@ -1039,7 +1041,7 @@ function checkAndParse(response) { }); } -function apiCall(method, params, resolve, reject, connectionStringOverride = null) { +function apiCall(method, params, resolve, reject) { const counter = new Date().getTime(); const options = { method: 'POST', @@ -1052,7 +1054,7 @@ function apiCall(method, params, resolve, reject, connectionStringOverride = nul }) }; - const connectionString = connectionStringOverride ? connectionStringOverride : Lbry.daemonConnectionString; + const connectionString = Lbry.methodsUsingAlternateConnectionString.includes(method) ? Lbry.alternateConnectionString : Lbry.daemonConnectionString; return fetch(connectionString + '?m=' + method, options).then(checkAndParse).then(response => { const error = response.error || response.result && response.result.error; @@ -1063,11 +1065,11 @@ function apiCall(method, params, resolve, reject, connectionStringOverride = nul }).catch(reject); } -function daemonCallWithResult(name, params = {}, connectionStringOverride = null) { +function daemonCallWithResult(name, params = {}) { return new Promise((resolve, reject) => { apiCall(name, params, result => { resolve(result); - }, reject, connectionStringOverride); + }, reject); }); } @@ -2871,7 +2873,7 @@ function batchActions(...actions) { var _extends$5 = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; -function doResolveUris(uris, returnCachedClaims = false, connectionStringOverride = null) { +function doResolveUris(uris, returnCachedClaims = false) { return (dispatch, getState) => { const normalizedUris = uris.map(normalizeURI); const state = getState(); @@ -2897,7 +2899,7 @@ function doResolveUris(uris, returnCachedClaims = false, connectionStringOverrid const resolveInfo = {}; - lbryProxy.resolve({ urls: urisToResolve }, connectionStringOverride).then(result => { + lbryProxy.resolve({ urls: urisToResolve }).then(result => { Object.entries(result).forEach(([uri, uriResolveInfo]) => { const fallbackResolveInfo = { stream: null, @@ -2937,8 +2939,8 @@ function doResolveUris(uris, returnCachedClaims = false, connectionStringOverrid }; } -function doResolveUri(uri, connectionStringOverride = null) { - return doResolveUris([uri], connectionStringOverride); +function doResolveUri(uri) { + return doResolveUris([uri]); } function doFetchClaimListMine(page = 1, pageSize = 99999, resolve = true) { @@ -3224,7 +3226,7 @@ function doClaimSearch(options = { no_totals: true, page_size: 10, page: 1 -}, connectionStringOverride = null) { +}) { const query = createNormalizedClaimSearchKey(options); return dispatch => { dispatch({ @@ -3260,7 +3262,7 @@ function doClaimSearch(options = { }); }; - lbryProxy.claim_search(options, connectionStringOverride).then(success, failure); + lbryProxy.claim_search(options).then(success, failure); }; } diff --git a/dist/flow-typed/Lbry.js b/dist/flow-typed/Lbry.js index ba88d27..9f43627 100644 --- a/dist/flow-typed/Lbry.js +++ b/dist/flow-typed/Lbry.js @@ -221,6 +221,8 @@ declare type LbryTypes = { connectPromise: ?Promise, connect: () => void, daemonConnectionString: string, + alternateConnectionString: string, + methodsUsingAlternateConnectionString: Array, apiRequestHeaders: { [key: string]: string }, setDaemonConnectionString: string => void, setApiHeader: (string, string) => void, diff --git a/flow-typed/Lbry.js b/flow-typed/Lbry.js index ba88d27..9f43627 100644 --- a/flow-typed/Lbry.js +++ b/flow-typed/Lbry.js @@ -221,6 +221,8 @@ declare type LbryTypes = { connectPromise: ?Promise, connect: () => void, daemonConnectionString: string, + alternateConnectionString: string, + methodsUsingAlternateConnectionString: Array, apiRequestHeaders: { [key: string]: string }, setDaemonConnectionString: string => void, setApiHeader: (string, string) => void, diff --git a/src/lbry.js b/src/lbry.js index ba41147..33b57c0 100644 --- a/src/lbry.js +++ b/src/lbry.js @@ -11,6 +11,8 @@ const Lbry: LbryTypes = { isConnected: false, connectPromise: null, daemonConnectionString: 'http://localhost:5279', + alternateConnectionString: '', + methodsUsingAlternateConnectionString: [], apiRequestHeaders: { 'Content-Type': 'application/json-rpc' }, // Allow overriding daemon connection string (e.g. to `/api/proxy` for lbryweb) @@ -73,9 +75,9 @@ const Lbry: LbryTypes = { version: () => daemonCallWithResult('version', {}), // Claim fetching and manipulation - resolve: (params, connectionStringOverride = null) => daemonCallWithResult('resolve', params, connectionStringOverride), + resolve: params => daemonCallWithResult('resolve', params), get: params => daemonCallWithResult('get', params), - claim_search: (params, connectionStringOverride = null) => daemonCallWithResult('claim_search', params, connectionStringOverride), + claim_search: params => daemonCallWithResult('claim_search', params), claim_list: params => daemonCallWithResult('claim_list', params), channel_create: params => daemonCallWithResult('channel_create', params), channel_update: params => daemonCallWithResult('channel_update', params), @@ -178,7 +180,7 @@ function checkAndParse(response) { }); } -export function apiCall(method: string, params: ?{}, resolve: Function, reject: Function, connectionStringOverride = null) { +export function apiCall(method: string, params: ?{}, resolve: Function, reject: Function) { const counter = new Date().getTime(); const options = { method: 'POST', @@ -191,7 +193,9 @@ export function apiCall(method: string, params: ?{}, resolve: Function, reject: }), }; - const connectionString = connectionStringOverride ? connectionStringOverride : Lbry.daemonConnectionString; + const connectionString = Lbry.methodsUsingAlternateConnectionString.includes(method) + ? Lbry.alternateConnectionString + : Lbry.daemonConnectionString; return fetch(connectionString + '?m=' + method, options) .then(checkAndParse) .then(response => { @@ -205,7 +209,7 @@ export function apiCall(method: string, params: ?{}, resolve: Function, reject: .catch(reject); } -function daemonCallWithResult(name: string, params: ?{} = {}, connectionStringOverride = null) { +function daemonCallWithResult(name: string, params: ?{} = {}) { return new Promise((resolve, reject) => { apiCall( name, @@ -213,8 +217,7 @@ function daemonCallWithResult(name: string, params: ?{} = {}, connectionStringOv result => { resolve(result); }, - reject, - connectionStringOverride + reject ); }); } diff --git a/src/redux/actions/claims.js b/src/redux/actions/claims.js index b20f67c..de18fd9 100644 --- a/src/redux/actions/claims.js +++ b/src/redux/actions/claims.js @@ -15,7 +15,7 @@ import { creditsToString } from 'util/format-credits'; import { batchActions } from 'util/batch-actions'; import { createNormalizedClaimSearchKey } from 'util/claim'; -export function doResolveUris(uris: Array, returnCachedClaims: boolean = false, connectionStringOverride: string = null) { +export function doResolveUris(uris: Array, returnCachedClaims: boolean = false) { return (dispatch: Dispatch, getState: GetState) => { const normalizedUris = uris.map(normalizeURI); const state = getState(); @@ -47,7 +47,7 @@ export function doResolveUris(uris: Array, returnCachedClaims: boolean = }, } = {}; - Lbry.resolve({ urls: urisToResolve }, connectionStringOverride).then((result: ResolveResponse) => { + Lbry.resolve({ urls: urisToResolve }).then((result: ResolveResponse) => { Object.entries(result).forEach(([uri, uriResolveInfo]) => { const fallbackResolveInfo = { stream: null, @@ -90,8 +90,8 @@ export function doResolveUris(uris: Array, returnCachedClaims: boolean = }; } -export function doResolveUri(uri: string, connectionStringOverride: string = null) { - return doResolveUris([uri], connectionStringOverride); +export function doResolveUri(uri: string) { + return doResolveUris([uri]); } export function doFetchClaimListMine( @@ -426,8 +426,7 @@ export function doClaimSearch( no_totals: true, page_size: 10, page: 1, - }, - connectionStringOverride: string = null + } ) { const query = createNormalizedClaimSearchKey(options); return (dispatch: Dispatch) => { @@ -464,7 +463,7 @@ export function doClaimSearch( }); }; - Lbry.claim_search(options, connectionStringOverride).then(success, failure); + Lbry.claim_search(options).then(success, failure); }; }