Skip to content
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

Feature/refactor server #289

Merged
merged 3 commits into from
Apr 23, 2021
Merged

Feature/refactor server #289

merged 3 commits into from
Apr 23, 2021

Conversation

just-at-uber
Copy link
Contributor

@just-at-uber just-at-uber commented Apr 23, 2021

This PR has no functional changes, only syntax / readability changes to help future work with ES support. Since a lot of files have been created to split apart helpers into single files, each file needs a license header which adds to the lines-of-code added.

Changed

  • moved helper functions into separate files for better readability
  • moved require (imports) to top of files to match import compatibility in the future
  • moved most constants into separate constants file

@@ -1,304 +0,0 @@
// Copyright (c) 2017-2021 Uber Technologies Inc.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

file moved to server/middleware/tchannel-client/index.js

// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

const momentToLong = require('./moment-to-long');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved helper functions into separate files for better readability


const Long = require('long');

const momentToLong = m => Long.fromValue(m.unix()).mul(1000000000);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved helper functions into separate files for better readability

app.webpackConfig = require('../webpack.config');
const webpackConfig = require('../webpack.config');
const tchannelClient = require('./middleware/tchannel-client');
const router = require('./router');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved require (imports) to top of file to match import compatibility in the future

.use(
require('koa-compress')({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved require (imports) to top of files to match import compatibility in the future

filter: contentType => !contentType.startsWith('text/event-stream'),
})
)
.use(require('./middleware/tchannel-client'))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved require (imports) to top of files to match import compatibility in the future

.use(
useWebpack
? koaWebpack({
compiler,
dev: { stats: { colors: true } },
hot: { port: process.env.TEST_RUN ? 8082 : 8081 },
})
: require('koa-static')(staticRoot)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved require (imports) to top of files to match import compatibility in the future

let compiler;

if (useWebpack) {
const Webpack = require('webpack');

koaWebpack = require('koa-webpack');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved require (imports) to top of files to match import compatibility in the future

timeout: REQUEST_TIMEOUT,
retryFlags: REQUEST_RETRY_FLAGS,
retryLimit: REQUEST_RETRY_LIMIT,
};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved most constants into separate constants file

// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved helper functions into separate files for better readability

// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved helper functions into separate files for better readability

// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved helper functions into separate files for better readability

// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved helper functions into separate files for better readability

// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved helper functions into separate files for better readability

// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved helper functions into separate files for better readability

// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved helper functions into separate files for better readability

// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved helper functions into separate files for better readability

// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved helper functions into separate files for better readability

// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved helper functions into separate files for better readability

// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved helper functions into separate files for better readability

// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved helper functions into separate files for better readability

// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved helper functions into separate files for better readability

withWorkflowExecution,
withVerboseWorkflowExecution,
withDomainPagingAndWorkflowExecution,
} = require('./helpers');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved helper functions into separate files for better readability

@@ -0,0 +1,128 @@
// Copyright (c) 2017-2021 Uber Technologies Inc.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved from server/middleware/tchannel-client.js

const moment = require('moment');

const featureFlags = require('./feature-flags.json');
const { momentToLong } = require('./helpers');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved helper functions into separate files for better readability

@just-at-uber just-at-uber marked this pull request as ready for review April 23, 2021 17:27
@just-at-uber just-at-uber requested a review from a team April 23, 2021 17:27
app = new Koa(),
router = require('./routes');
const path = require('path');
const Koa = require('koa');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be lowecase like the rest of consts here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'll update. casing ultimately has no difference in javascript but its good to keep to convention.

Copy link
Contributor Author

@just-at-uber just-at-uber Apr 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the code more closely, the code actually calls new on Koa. this would indicate that this is a class (or constructor). Typically in javascript we notate classes vs functions by using first capital letter. So i think in this particular case the casing is correct.

You can look at this convention here:
https://stackoverflow.com/questions/1564398/javascript-method-naming-lowercase-vs-uppercase/1564489#:~:text=5%20Answers&text=A%20popular%20convention%20in%20Javascript,mistakenly%20called%20%22classes%22).&text=Anything%20that's%20not%20a%20constructor%20usually%20starts%20with%20lowercase%20and%20is%20camelCased.


const tchannelAsThrift = TChannelAsThrift({
channel: cadenceChannel,
entryPoint: path.join(__dirname, '../../../idl/cadence.thrift'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has two level upwards path (../..) compared to previous change. Is this intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its because its path has now moved two levels deeper from where the code was located earlier.
server/middleware/tchannel-client.js => server/middleware/tchannel-client/helpers/make-channel.js


const get = require('lodash.get');

const withDomainPaging = ctx => body => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curiuos, how should ctx => body =>be read? Is lambda to lambda? I assume two arguments would be specified as (ctx, body) =>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currying functions, so a function returns a function waiting to be executed before proceeding.
In this case ctx is prepped into scope as previously this was just accessed from outside the function. body onwards will be executed by koa server when the route is called and will pass the body automatically when calling the request handler.
Sorry if this isn't clear enough.

some articles on currying in javascript:
https://javascript.info/currying-partials
https://blog.benestudio.co/currying-in-javascript-es6-540d2ad09400

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can compare the old version of the function within the removed code server/middleware/tchannel-client.js for more context.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, currying, nice functional stuff :)

Copy link

@mkolodezny mkolodezny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@just-at-uber just-at-uber merged commit ebb847d into master Apr 23, 2021
@just-at-uber just-at-uber deleted the feature/refactor-server branch April 23, 2021 18:28
@just-at-uber just-at-uber mentioned this pull request May 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants