From 80ef18fa44253c27c6545f14e65013c4881249ab Mon Sep 17 00:00:00 2001 From: "Chakhsu.Lau" Date: Sun, 3 Dec 2023 21:27:50 +0800 Subject: [PATCH] chore: use standard code type with prettierrc --- .editorconfig | 3 --- .prettierrc | 18 ++++++++++++++++-- README.md | 4 ++-- README_CN.md | 4 ++-- src/index.ts | 6 +++--- src/proxy/clientProxy.ts | 6 +++--- src/proxy/serverProxy.ts | 12 ++++++------ test/benchmark/server-grpcity.js | 4 ++-- test/benchmark/server-grpcjs.js | 2 +- test/client.js | 2 +- test/index.test.js | 8 ++++---- test/server.js | 10 +++++----- test/stream/client-v2.js | 2 +- test/stream/client.js | 6 +++--- test/stream/server-v2.js | 2 +- test/stream/server.js | 6 +++--- types/index.js | 8 ++++---- types/proxy/clientProxy.js | 30 +++++++++++++++--------------- types/proxy/serverProxy.js | 28 ++++++++++++++-------------- types/util/compose.js | 2 +- types/util/iterator.js | 4 ++-- 21 files changed, 89 insertions(+), 78 deletions(-) delete mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 76a93c0..0000000 --- a/.editorconfig +++ /dev/null @@ -1,3 +0,0 @@ -[*] -indent_style = space -indent_size = 2 diff --git a/.prettierrc b/.prettierrc index 62c31f6..d1df35f 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,7 +1,21 @@ { + "arrowParens": "always", + "bracketSameLine": false, + "bracketSpacing": true, + "embeddedLanguageFormatting": "auto", + "endOfLine": "lf", + "htmlWhitespaceSensitivity": "css", + "insertPragma": false, + "jsxSingleQuote": true, + "printWidth": 80, + "proseWrap": "preserve", + "quoteProps": "as-needed", + "requirePragma": false, "semi": false, + "singleAttributePerLine": false, "singleQuote": true, + "tabWidth": 2, "trailingComma": "none", - "arrowParens": "avoid", - "proseWrap": "always" + "useTabs": false, + "vueIndentScriptAndStyle": false } diff --git a/README.md b/README.md index d7aa909..55f77cf 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ class Greeter { } } -const start = async addr => { +const start = async (addr) => { await loader.init() const server = loader.initServer() @@ -123,7 +123,7 @@ Finally, create `client.js` and write the following code in it: ```js import loader from './loader.js' -const start = async addr => { +const start = async (addr) => { await loader.init() await loader.initClients({ diff --git a/README_CN.md b/README_CN.md index 6d3fc95..46730c7 100644 --- a/README_CN.md +++ b/README_CN.md @@ -91,7 +91,7 @@ class Greeter { } } -const start = async addr => { +const start = async (addr) => { await loader.init() const server = loader.initServer() @@ -111,7 +111,7 @@ start('127.0.0.1:9099') ```js import loader from './loader.js' -const start = async addr => { +const start = async (addr) => { await loader.init() await loader.initClients({ diff --git a/src/index.ts b/src/index.ts index e34915e..f7002e4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -65,7 +65,7 @@ class GrpcLoader { this._appName = appName loadOptions.includeDirs = this._protoFiles - .map(p => p.location) + .map((p) => p.location) .concat(loadOptions.includeDirs || []) const files = this._protoFiles.reduce((result, p) => { if (p.files && p.files.length > 0) { @@ -115,7 +115,7 @@ class GrpcLoader { } const serviceNames = Object.keys(services) - serviceNames.forEach(name => { + serviceNames.forEach((name) => { const isDefaultClient = true const addr = _.isString(services[name]) ? services[name] @@ -346,7 +346,7 @@ class GrpcLoader { if (typeof initialValues === 'object') { Object.entries(initialValues).forEach(([key, value]: [string, any]) => { if (Array.isArray(value)) { - value.map(v => meta.add(key, _.isString(v) ? v : Buffer.from(v))) + value.map((v) => meta.add(key, _.isString(v) ? v : Buffer.from(v))) } else { meta.add(key, _.isString(value) ? value : Buffer.from(value)) } diff --git a/src/proxy/clientProxy.ts b/src/proxy/clientProxy.ts index 19e3097..eb42dbb 100644 --- a/src/proxy/clientProxy.ts +++ b/src/proxy/clientProxy.ts @@ -160,7 +160,7 @@ class ClientProxy { call.writeAll = (messages: any[]) => { if (Array.isArray(messages)) { - messages.forEach(message => { + messages.forEach((message) => { call.write(message) }) } @@ -264,7 +264,7 @@ class ClientProxy { call.writeAll = (messages: any[]) => { if (Array.isArray(messages)) { - messages.forEach(message => { + messages.forEach((message) => { call.write(message) }) } @@ -318,7 +318,7 @@ class ClientProxy { const prototype = Object.getPrototypeOf(client) const methodNames: any = Object.keys(prototype) - .filter(key => prototype[key] && prototype[key].path) + .filter((key) => prototype[key] && prototype[key].path) .reduce((names: any, key) => { names[key.toUpperCase()] = prototype[key].path return names diff --git a/src/proxy/serverProxy.ts b/src/proxy/serverProxy.ts index d5ef36f..8d4c014 100644 --- a/src/proxy/serverProxy.ts +++ b/src/proxy/serverProxy.ts @@ -55,7 +55,7 @@ class ServerProxy { } await new Promise((resolve, reject) => { - this._server!.tryShutdown(err => { + this._server!.tryShutdown((err) => { if (err) { reject(err) } else { @@ -106,7 +106,7 @@ class ServerProxy { const service = this._loader.service(name) const options: any = { exclude, inherit, _implementationType: {} } - Object.keys(service).forEach(key => { + Object.keys(service).forEach((key) => { const { requestStream, responseStream } = service[key] options._implementationType[service[key].originalName] = { requestStream, @@ -135,14 +135,14 @@ class ServerProxy { ) if (args.length === 1) { if (Array.isArray(args[0])) { - args[0].forEach(fn => { + args[0].forEach((fn) => { this._use(fn) }) } else { this._use(args[0]) } } else { - args.forEach(fn => { + args.forEach((fn) => { this._use(fn) }) } @@ -298,7 +298,7 @@ class ServerProxy { call.writeAll = (messages: any[]) => { if (Array.isArray(messages)) { - messages.forEach(message => { + messages.forEach((message) => { call.write(message) }) } @@ -327,7 +327,7 @@ class ServerProxy { call.writeAll = (messages: any[]) => { if (Array.isArray(messages)) { - messages.forEach(message => { + messages.forEach((message) => { call.write(message) }) } diff --git a/test/benchmark/server-grpcity.js b/test/benchmark/server-grpcity.js index 30625bc..8bdb980 100755 --- a/test/benchmark/server-grpcity.js +++ b/test/benchmark/server-grpcity.js @@ -2,12 +2,12 @@ const GrpcLoader = require('../../types') const path = require('path') const implementation = { - sayHello: async call => { + sayHello: async (call) => { return { message: 'Hello ' + call.request.name } } } -const start = async addr => { +const start = async (addr) => { const loader = new GrpcLoader({ location: path.resolve(__dirname, './'), files: ['helloworld.proto'] diff --git a/test/benchmark/server-grpcjs.js b/test/benchmark/server-grpcjs.js index f5d892a..3653cee 100644 --- a/test/benchmark/server-grpcjs.js +++ b/test/benchmark/server-grpcjs.js @@ -18,7 +18,7 @@ const implementation = { } } -const start = addr => { +const start = (addr) => { const server = new grpc.Server() server.addService(helloProto.Greeter.service, implementation) server.bindAsync(addr, grpc.ServerCredentials.createInsecure(), () => { diff --git a/test/client.js b/test/client.js index c516c43..92f174b 100755 --- a/test/client.js +++ b/test/client.js @@ -2,7 +2,7 @@ const GrpcLoader = require('../types') const path = require('path') const fs = require('fs') -const start = async addr => { +const start = async (addr) => { const loader = new GrpcLoader({ location: path.resolve(__dirname, 'protos'), files: ['test/helloworld/helloworld.proto'] diff --git a/test/index.test.js b/test/index.test.js index 599ab5f..3ec08ed 100755 --- a/test/index.test.js +++ b/test/index.test.js @@ -17,7 +17,7 @@ describe('Grpc Loader', () => { } if (metadata.get('x-long-delay').length > 0) { - await new Promise(resolve => setTimeout(resolve, 1000 * 10)) + await new Promise((resolve) => setTimeout(resolve, 1000 * 10)) } expect(this).to.be.an('object') @@ -41,7 +41,7 @@ describe('Grpc Loader', () => { const server = loader.initServer() const servicers = [new Greeter()] - await Promise.all(servicers.map(async s => s.init(server))) + await Promise.all(servicers.map(async (s) => s.init(server))) const addr = { host: '127.0.0.1', port: 12305 } await server.listen(addr) @@ -105,7 +105,7 @@ describe('Grpc Loader', () => { const server = loader.initServer() const servicers = [new Greeter()] - await Promise.all(servicers.map(async s => s.init(server))) + await Promise.all(servicers.map(async (s) => s.init(server))) const addr = { host: '127.0.0.1', port: 12306 } await server.listen(addr) @@ -138,7 +138,7 @@ describe('Grpc Loader', () => { } ) - call.on('metadata', metadata => { + call.on('metadata', (metadata) => { expect(metadata.get('x-cache-control')) .to.be.an('array') .deep.eq(['max-age=100']) diff --git a/test/server.js b/test/server.js index e019d05..86f8f34 100755 --- a/test/server.js +++ b/test/server.js @@ -2,7 +2,7 @@ const GrpcLoader = require('../types') const path = require('path') const fs = require('fs') -const timeout = ms => { +const timeout = (ms) => { return new Promise((resolve, reject) => setTimeout(resolve, ms)) } @@ -24,7 +24,7 @@ class Greeter { } if (metadata.get('x-long-delay').length > 0) { - await new Promise(resolve => setTimeout(resolve, 1000 * 10)) + await new Promise((resolve) => setTimeout(resolve, 1000 * 10)) } await timeout(1000) this.count++ @@ -54,7 +54,7 @@ class Hellor { } if (metadata.get('x-long-delay').length > 0) { - await new Promise(resolve => setTimeout(resolve, 1000 * 10)) + await new Promise((resolve) => setTimeout(resolve, 1000 * 10)) } return { message: `hello, ${call.request.name || 'world'}` } @@ -83,7 +83,7 @@ const middlewareB = async (ctx, next) => { console.log('middlewareB: 2', ctx, endTime, endTime - beginTime) } -const start = async addr => { +const start = async (addr) => { const loader = new GrpcLoader({ location: path.resolve(__dirname, 'protos'), files: ['test/helloworld/helloworld.proto'] @@ -100,7 +100,7 @@ const start = async addr => { // server.addMiddleware(middlewareB) const servicers = [new Greeter(), new Hellor()] - await Promise.all(servicers.map(async s => s.init(server))) + await Promise.all(servicers.map(async (s) => s.init(server))) const credentials = server.makeServerCredentials( fs.readFileSync(path.resolve(__dirname, 'certs/ca.crt')), diff --git a/test/stream/client-v2.js b/test/stream/client-v2.js index 201c6d2..2628e5c 100644 --- a/test/stream/client-v2.js +++ b/test/stream/client-v2.js @@ -1,7 +1,7 @@ const GrpcLoader = require('../../types') const path = require('path') -const start = async addr => { +const start = async (addr) => { const loader = new GrpcLoader({ location: path.resolve(__dirname, './'), files: ['stream.proto'] diff --git a/test/stream/client.js b/test/stream/client.js index df94db7..1a8a66f 100644 --- a/test/stream/client.js +++ b/test/stream/client.js @@ -1,7 +1,7 @@ const GrpcLoader = require('../../types') const path = require('path') -const start = async addr => { +const start = async (addr) => { const loader = new GrpcLoader({ location: path.resolve(__dirname, './'), files: ['stream.proto'] @@ -53,7 +53,7 @@ const start = async addr => { const serverStreamHelloCall = client.call.serverStreamHello({ message: 'Hello! How are you?' }) - serverStreamHelloCall.on('data', chunk => { + serverStreamHelloCall.on('data', (chunk) => { console.log(chunk) }) serverStreamHelloCall.on('end', () => { @@ -66,7 +66,7 @@ const start = async addr => { mutualStreamHelloCall.write({ message: 'How are you?' }) mutualStreamHelloCall.write({ message: 'other thing x' }) - mutualStreamHelloCall.on('data', data => { + mutualStreamHelloCall.on('data', (data) => { console.log(data) if (data.message === 'delay 1s') { mutualStreamHelloCall.write({ message: 'ok, I known you delay 1s' }) diff --git a/test/stream/server-v2.js b/test/stream/server-v2.js index 9ff4615..3f3b628 100644 --- a/test/stream/server-v2.js +++ b/test/stream/server-v2.js @@ -59,7 +59,7 @@ class Stream { } } -const start = async addr => { +const start = async (addr) => { const loader = new GrpcLoader({ location: path.resolve(__dirname, './'), files: ['stream.proto'] diff --git a/test/stream/server.js b/test/stream/server.js index 33d26a5..dca7890 100644 --- a/test/stream/server.js +++ b/test/stream/server.js @@ -16,7 +16,7 @@ class Stream { metadata.add('x-timestamp-server', 'received=' + new Date().toISOString()) call.sendMetadata(metadata) - call.on('data', data => { + call.on('data', (data) => { console.log(data) }) call.on('end', () => { @@ -41,7 +41,7 @@ class Stream { call.sendMetadata(metadata) call.write({ message: 'emmm...' }) - call.on('data', chunk => { + call.on('data', (chunk) => { console.log(chunk.message) if (chunk.message === 'Hello!') { call.write({ message: 'Hello too.' }) @@ -63,7 +63,7 @@ class Stream { } } -const start = async addr => { +const start = async (addr) => { const loader = new GrpcLoader({ location: path.resolve(__dirname, './'), files: ['stream.proto'] diff --git a/types/index.js b/types/index.js index d494151..e7cb453 100644 --- a/types/index.js +++ b/types/index.js @@ -103,7 +103,7 @@ class GrpcLoader { this._packagePrefix = packagePrefix this._appName = appName loadOptions.includeDirs = this._protoFiles - .map(p => p.location) + .map((p) => p.location) .concat(loadOptions.includeDirs || []) const files = this._protoFiles.reduce((result, p) => { if (p.files && p.files.length > 0) { @@ -145,7 +145,7 @@ class GrpcLoader { await this.init() } const serviceNames = Object.keys(services) - serviceNames.forEach(name => { + serviceNames.forEach((name) => { const isDefaultClient = true const addr = _.isString(services[name]) ? services[name] @@ -213,7 +213,7 @@ class GrpcLoader { return found } } - const descriptor = this.type(name).fileDescriptorProtos.map(proto => + const descriptor = this.type(name).fileDescriptorProtos.map((proto) => Descriptor.FileDescriptorProto.decode(proto) ) root = protobuf.Root.fromDescriptor({ file: descriptor }) @@ -348,7 +348,7 @@ class GrpcLoader { if (typeof initialValues === 'object') { Object.entries(initialValues).forEach(([key, value]) => { if (Array.isArray(value)) { - value.map(v => meta.add(key, _.isString(v) ? v : Buffer.from(v))) + value.map((v) => meta.add(key, _.isString(v) ? v : Buffer.from(v))) } else { meta.add(key, _.isString(value) ? value : Buffer.from(value)) } diff --git a/types/proxy/clientProxy.js b/types/proxy/clientProxy.js index f8d862c..80b9009 100644 --- a/types/proxy/clientProxy.js +++ b/types/proxy/clientProxy.js @@ -118,10 +118,10 @@ class ClientProxy { result.response = response }) const call = func.apply(client, argumentsList) - call.on('metadata', metadata => { + call.on('metadata', (metadata) => { result.metadata = metadata }) - call.on('status', status => { + call.on('status', (status) => { result.status = status resolve(result) }) @@ -151,9 +151,9 @@ class ClientProxy { result.response = response }) const call = func.apply(client, argumentsList) - call.writeAll = messages => { + call.writeAll = (messages) => { if (Array.isArray(messages)) { - messages.forEach(message => { + messages.forEach((message) => { call.write(message) }) } @@ -161,10 +161,10 @@ class ClientProxy { call.writeEnd = async () => { call.end() await new Promise((resolve, reject) => { - call.on('metadata', metadata => { + call.on('metadata', (metadata) => { result.metadata = metadata }) - call.on('status', status => { + call.on('status', (status) => { result.status = status resolve() }) @@ -189,15 +189,15 @@ class ClientProxy { ;[metadata, options] = this._prepareMetadata(metadata, options, basicMeta) options = this._setDeadline(options, defaultOptions, basicMeta) const call = func.apply(client, [request, metadata, options]) - call.on('error', err => { + call.on('error', (err) => { throw this._handlerError(err, basicMeta) }) const result = {} call.readAll = () => { - call.on('metadata', metadata => { + call.on('metadata', (metadata) => { result.metadata = metadata }) - call.on('status', status => { + call.on('status', (status) => { result.status = status }) return (0, iterator_1.default)(call, 'data', { @@ -225,23 +225,23 @@ class ClientProxy { ;[metadata, options] = this._prepareMetadata(metadata, options, basicMeta) options = this._setDeadline(options, defaultOptions, basicMeta) const call = func.apply(client, [metadata, options]) - call.writeAll = messages => { + call.writeAll = (messages) => { if (Array.isArray(messages)) { - messages.forEach(message => { + messages.forEach((message) => { call.write(message) }) } } call.writeEnd = call.end - call.on('error', err => { + call.on('error', (err) => { throw this._handlerError(err, basicMeta) }) const result = {} call.readAll = () => { - call.on('metadata', metadata => { + call.on('metadata', (metadata) => { result.metadata = metadata }) - call.on('status', status => { + call.on('status', (status) => { result.status = status }) return (0, iterator_1.default)(call, 'data', { @@ -266,7 +266,7 @@ class ClientProxy { defaultOptions.timeout = defaultOptions.timeout || 1000 * 10 const prototype = Object.getPrototypeOf(client) const methodNames = Object.keys(prototype) - .filter(key => prototype[key] && prototype[key].path) + .filter((key) => prototype[key] && prototype[key].path) .reduce((names, key) => { names[key.toUpperCase()] = prototype[key].path return names diff --git a/types/proxy/serverProxy.js b/types/proxy/serverProxy.js index 2bb4476..87c2589 100644 --- a/types/proxy/serverProxy.js +++ b/types/proxy/serverProxy.js @@ -96,7 +96,7 @@ class ServerProxy { return } await new Promise((resolve, reject) => { - this._server.tryShutdown(err => { + this._server.tryShutdown((err) => { if (err) { reject(err) } else { @@ -133,7 +133,7 @@ class ServerProxy { addService(name, implementation, { exclude = [], inherit } = {}) { const service = this._loader.service(name) const options = { exclude, inherit, _implementationType: {} } - Object.keys(service).forEach(key => { + Object.keys(service).forEach((key) => { const { requestStream, responseStream } = service[key] options._implementationType[service[key].originalName] = { requestStream, @@ -156,14 +156,14 @@ class ServerProxy { ) if (args.length === 1) { if (Array.isArray(args[0])) { - args[0].forEach(fn => { + args[0].forEach((fn) => { this._use(fn) }) } else { this._use(args[0]) } } else { - args.forEach(fn => { + args.forEach((fn) => { this._use(fn) }) } @@ -255,7 +255,7 @@ class ServerProxy { const handleResponse = async () => { ctx.response = await target[key](call) } - await composeFunc(ctx, handleResponse).catch(err => { + await composeFunc(ctx, handleResponse).catch((err) => { callback(this._createInternalErrorStatus(err)) }) callback(null, ctx.response) @@ -274,7 +274,7 @@ class ServerProxy { const handleResponse = async () => { ctx.response = await target[key](call) } - await composeFunc(ctx, handleResponse).catch(err => { + await composeFunc(ctx, handleResponse).catch((err) => { callback(this._createInternalErrorStatus(err)) }) callback(null, ctx.response) @@ -282,11 +282,11 @@ class ServerProxy { } } _callServerStreamProxyMethod(target, key, composeFunc) { - return call => { + return (call) => { const ctx = this._createContext(call) - call.writeAll = messages => { + call.writeAll = (messages) => { if (Array.isArray(messages)) { - messages.forEach(message => { + messages.forEach((message) => { call.write(message) }) } @@ -296,7 +296,7 @@ class ServerProxy { const handleResponse = async () => { await target[key](call) } - await composeFunc(ctx, handleResponse).catch(err => { + await composeFunc(ctx, handleResponse).catch((err) => { call.destroy(this._createInternalErrorStatus(err)) }) call.end() @@ -304,11 +304,11 @@ class ServerProxy { } } _callDuplexStreamProxyMethod(target, key, composeFunc) { - return call => { + return (call) => { const ctx = this._createContext(call) - call.writeAll = messages => { + call.writeAll = (messages) => { if (Array.isArray(messages)) { - messages.forEach(message => { + messages.forEach((message) => { call.write(message) }) } @@ -322,7 +322,7 @@ class ServerProxy { const handleResponse = async () => { await target[key](call) } - await composeFunc(ctx, handleResponse).catch(err => { + await composeFunc(ctx, handleResponse).catch((err) => { call.destroy(this._createInternalErrorStatus(err)) }) call.end() diff --git a/types/util/compose.js b/types/util/compose.js index 1440015..2869694 100644 --- a/types/util/compose.js +++ b/types/util/compose.js @@ -10,7 +10,7 @@ exports.compose = void 0 * @return {Function} * @api public */ -const compose = middleware => { +const compose = (middleware) => { if (!Array.isArray(middleware)) throw new TypeError('Middleware stack must be an array!') for (const fn of middleware) { diff --git a/types/util/iterator.js b/types/util/iterator.js index 9db9132..ceae6ac 100644 --- a/types/util/iterator.js +++ b/types/util/iterator.js @@ -1,7 +1,7 @@ 'use strict' Object.defineProperty(exports, '__esModule', { value: true }) const symbolAsyncIterator = Symbol.asyncIterator || '@@asyncIterator' -const normalizeEmitter = emitter => { +const normalizeEmitter = (emitter) => { const addListener = emitter.on || emitter.addListener || emitter.addEventListener const removeListener = @@ -14,7 +14,7 @@ const normalizeEmitter = emitter => { removeListener: removeListener.bind(emitter) } } -const toArray = value => (Array.isArray(value) ? value : [value]) +const toArray = (value) => (Array.isArray(value) ? value : [value]) exports.default = (emitter, event, options) => { if (typeof options === 'function') { options = { filter: options }