From 6da01bf53d520e552c7ae61a47f8d3d9e7fdc1ad Mon Sep 17 00:00:00 2001 From: shiyuhang <1136742008@qq.com> Date: Wed, 6 Mar 2024 12:17:01 +0800 Subject: [PATCH] fix lint --- .../cloudflare/Cloudflare-Workers.mdx | 353 +++++++++--------- 1 file changed, 168 insertions(+), 185 deletions(-) diff --git a/website/docs/examples/cloudflare/Cloudflare-Workers.mdx b/website/docs/examples/cloudflare/Cloudflare-Workers.mdx index 22f14af75c..7423783b03 100644 --- a/website/docs/examples/cloudflare/Cloudflare-Workers.mdx +++ b/website/docs/examples/cloudflare/Cloudflare-Workers.mdx @@ -21,29 +21,29 @@ Before you try the steps in this article, you need to prepare the following thin 2. To authenticate Wrangler, run wrangler login: - ``` - wrangler login - ``` + ``` + wrangler login + ``` 3. Use Wrangler to create a project: - ``` - wrangler init mysql2-cloudflare -y - ``` + ``` + wrangler init mysql2-cloudflare -y + ``` ## Step 2: Install node-mysql2 1. Enter your project directory: - ``` - cd mysql2-cloudflare - ``` + ``` + cd mysql2-cloudflare + ``` 2. Install the node-mysql2 with npm: - ``` - npm install mysql2 - ``` + ``` + npm install mysql2 + ``` This adds the node-mysql2 dependency in `package.json`. @@ -53,149 +53,136 @@ Develop your code in the `src/index.ts`. Here are some examples: 1. local mysql example - ```ts - - import { createConnection } from 'mysql2'; - - async function sleep(ms) { - return new Promise(resolve => setTimeout(resolve, ms)); - } - export default { - async fetch( - request: Request, - env: Env, - ctx: ExecutionContext - ): Promise { - let result - const connection = createConnection( - { - host: '127.0.0.1', - port: 3306, - user: 'user', - password: 'password', - useStaticParser:true - }); - connection.query( - 'show databases', - function(err, rows, fields) { - if (err) { - throw err - } - result = rows - } - ); - await sleep(2000); - return new Response(JSON.stringify(result)) - }, - }; - ``` +```ts +import { createConnection } from 'mysql2'; + +async function sleep(ms) { + return new Promise((resolve) => setTimeout(resolve, ms)); +} +export default { + async fetch( + request: Request, + env: Env, + ctx: ExecutionContext + ): Promise { + let result; + const connection = createConnection({ + host: '127.0.0.1', + port: 3306, + user: 'user', + password: 'password', + useStaticParser: true, + }); + connection.query('show databases', function (err, rows, fields) { + if (err) { + throw err; + } + result = rows; + }); + await sleep(2000); + return new Response(JSON.stringify(result)); + }, +}; +``` 2. TiDB Serverless example with TLS: - ```ts - import { createConnection } from 'mysql2'; - - async function sleep(ms) { - return new Promise(resolve => setTimeout(resolve, ms)); - } - export default { - async fetch( - request: Request, - env: Env, - ctx: ExecutionContext - ): Promise { - let result - const connection = createConnection( - { - host: 'gateway01.ap-southeast-1.prod.aws.tidbcloud.com', - port: 4000, - user: 'your_user', - password: 'your_password', - database: 'test', - ssl: { - minVersion: 'TLSv1.2', - rejectUnauthorized: true - }, - useStaticParser:true - }); - connection.query( - 'show databases', - function(err, rows, fields) { - if (err) { - throw err - } - result = rows - } - ); - await sleep(2000); - return new Response(JSON.stringify(result)) - }, - }; - ``` +```ts +import { createConnection } from 'mysql2'; + +async function sleep(ms) { + return new Promise((resolve) => setTimeout(resolve, ms)); +} +export default { + async fetch( + request: Request, + env: Env, + ctx: ExecutionContext + ): Promise { + let result; + const connection = createConnection({ + host: 'gateway01.ap-southeast-1.prod.aws.tidbcloud.com', + port: 4000, + user: 'your_user', + password: 'your_password', + database: 'test', + ssl: { + minVersion: 'TLSv1.2', + rejectUnauthorized: true, + }, + useStaticParser: true, + }); + connection.query('show databases', function (err, rows, fields) { + if (err) { + throw err; + } + result = rows; + }); + await sleep(2000); + return new Response(JSON.stringify(result)); + }, +}; +``` 3. PlanetScale example with TLS: - ```ts - import { createConnection } from 'mysql2'; - - async function sleep(ms) { - return new Promise(resolve => setTimeout(resolve, ms)); - } - - export default { - async fetch( - request: Request, - env: Env, - ctx: ExecutionContext - ): Promise { - let result - const connection = createConnection( - { - host: 'aws.connect.psdb.cloud', - port: 3306, - user: 'your_user', - password: 'your_password', - database: 'test', - ssl: { - minVersion: 'TLSv1.2', - rejectUnauthorized: true - }, - useStaticParser:true - }); - connection.query( - 'show databases', - function(err, rows, fields) { - if (err) { - throw err - } - result = rows - } - ); - await sleep(2000); - return new Response(JSON.stringify(result)) - }, - }; - ``` +```ts +import { createConnection } from 'mysql2'; + +async function sleep(ms) { + return new Promise((resolve) => setTimeout(resolve, ms)); +} + +export default { + async fetch( + request: Request, + env: Env, + ctx: ExecutionContext + ): Promise { + let result; + const connection = createConnection({ + host: 'aws.connect.psdb.cloud', + port: 3306, + user: 'your_user', + password: 'your_password', + database: 'test', + ssl: { + minVersion: 'TLSv1.2', + rejectUnauthorized: true, + }, + useStaticParser: true, + }); + connection.query('show databases', function (err, rows, fields) { + if (err) { + throw err; + } + result = rows; + }); + await sleep(2000); + return new Response(JSON.stringify(result)); + }, +}; +``` ## Step 4: Test locally 1. Add `node_compat = true` in the `wrangler.toml` to make Cloudflare Workers compatible with node dependencies. - ``` - vim wrangler.toml - ``` +``` +vim wrangler.toml +``` 2. Upgrade wrangler - ``` - npm install wrangler@3.22.1 --save-dev - ``` +``` +npm install wrangler@3.22.1 --save-dev +``` 2. Run locally - ``` - wrangler dev - ``` +``` +wrangler dev +``` ## Step 5: Deploy @@ -207,62 +194,58 @@ If you want to develop the corresponding feature. Here is a simaple example of t 1. enter to node-mysql2 - ``` - cd node-mysql2 - ``` +``` +cd node-mysql2 +``` 2. vim wrangler.toml - ``` - name = "mysql2-cloudflare" - main = "src/worker.js" - compatibility_date = "2023-08-21" - node_compat = true - ``` +``` +name = "mysql2-cloudflare" +main = "src/worker.js" +compatibility_date = "2023-08-21" +node_compat = true +``` 3. add worker.js - ``` - mkdir src - cd src - vim workers.js - ``` +``` +mkdir src +cd src +vim workers.js +``` 4. write your test code inside worker.js - ```js - const { createConnection } = require('../index'); - async function sleep(ms) { - return new Promise(resolve => setTimeout(resolve, ms)); - } - export default { - async fetch(request, env, ctx) { - let result - const connection = createConnection( - { - host: '127.0.0.1', - port: 3306, - user: 'test', - password: 'password', - useStaticParser:true - }); - connection.query( - 'show databases', - function(err, rows, fields) { - if (err) { - throw err - } - result = rows - } - ); - await sleep(2000); - return new Response(JSON.stringify(result)) - } - }; - ``` +```js +const { createConnection } = require('../index'); +async function sleep(ms) { + return new Promise((resolve) => setTimeout(resolve, ms)); +} +export default { + async fetch(request, env, ctx) { + let result; + const connection = createConnection({ + host: '127.0.0.1', + port: 3306, + user: 'test', + password: 'password', + useStaticParser: true, + }); + connection.query('show databases', function (err, rows, fields) { + if (err) { + throw err; + } + result = rows; + }); + await sleep(2000); + return new Response(JSON.stringify(result)); + }, +}; +``` 5. Test locally - ``` - npx wrangler dev - ``` +``` +npx wrangler dev +```