From be1192f0d325d6557095ecbcd1fc9a02bcb828cf Mon Sep 17 00:00:00 2001 From: TomDo1234 Date: Wed, 3 Apr 2024 18:22:39 +1100 Subject: [PATCH] Done with worker doc --- docz/docs/03-demos/30-cloud/14-cloudflare.md | 53 ++++++++++++++++++-- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/docz/docs/03-demos/30-cloud/14-cloudflare.md b/docz/docs/03-demos/30-cloud/14-cloudflare.md index c75e295..292cbe0 100644 --- a/docz/docs/03-demos/30-cloud/14-cloudflare.md +++ b/docz/docs/03-demos/30-cloud/14-cloudflare.md @@ -113,11 +113,58 @@ free requests per day and 10 milliseconds of CPU time per invocation. ::: -1) If you do not have an account, create a new account[^7]. +1) If you do not have an account, create a new Cloudflare account[^7]. -### Setting up the Project and Testing +#### Setting up the Project and Testing -[The Cloudflare Docs provide a simple step by step guide to setup, develop, test, build and deploy your project](https://developers.cloudflare.com/workers/get-started/guide/) +We will be using the C3 CLI which means we do not have to interact with the Cloudflare dashboard as much. + +2) Create a Worker project with C3 CLI. It will ask you to authenticate into cloudflare. It will also give you some options but for this example we will be using JS, + +```bash +npm create cloudflare@latest +``` + +3) Install Required Dependencies + +```bash +npm i --save https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz @aws-sdk/client-s3@3.540.0 +``` + + +4) Navigate to src/index.js, then replace the default `async fetch` function there with this + +```js +const XLSX = require("xlsx"); +export default { + async fetch(request, env, ctx) { + const wb = XLSX.read("S,h,e,e,t,J,S\n5,4,3,3,7,9,5", {type: "binary"}); + /* write to XLSX file in a NodeJS Buffer */ + const data = XLSX.write(wb, {type: "buffer", bookType: "xlsx"}); + return new Response(data,{ + headers: { + "Content-Disposition": 'attachment; filename="SheetJSCFWorker.xlsx"' + } + }) + } +} +``` + +5) Start the development server + +```bash +npx wrangler dev +``` +Then open the browser at the port wrangler opened, it should download a file called SheetJSCFWorker.xlsx with some test data in it + +6) Deploy the worker and configure live enviromnent secrets + +```bash +npx wrangler deploy +``` + +7) Go to your Cloudflare dashboard, go to Workers & Pages, click on your worker, then go to Settings -> Triggers and then click on your worker route, +it should download SheetJSCFWorker.xlsx with the same test data ## Cloudflare R2