docs.sheetjs.com/docz/docs/03-demos/09-cloud/19-deno.md

112 lines
3.1 KiB
Markdown
Raw Normal View History

2023-06-06 10:01:12 +00:00
---
title: Deno Deploy
pagination_prev: demos/local/index
pagination_next: demos/extensions/index
---
import current from '/version.js';
import CodeBlock from '@theme/CodeBlock';
Deno Deploy offers "Serverless Functions" powered by Deno.
The [Deno installation](/docs/getting-started/installation/deno) instructions
apply to Deno Deploy scripts.
:::warning
Deno Deploy does not offer any sort of temporary file access in functions.
This breaks web frameworks that use the filesystem in body parsing.
:::
:::caution
When the demo was last tested, Deno Deploy required a GitHub account.
:::
## Supported Frameworks
When the demo was last tested, the `drash` server framework used an in-memory
approach for parsing POST request bodies.
### Parsing Data
When files are submitted via HTTP POST, the `bodyParam` method can fetch data.
The `content` property of the returned object can be parsed with `XLSX.read`.
The following example assumes the file is submitted at field name `file`:
<CodeBlock language="ts">{`\
// @deno-types="https://cdn.sheetjs.com/xlsx-${current}/package/types/index.d.ts"
import { read, utils } from 'https://cdn.sheetjs.com/xlsx-${current}/package/xlsx.mjs';
2023-08-18 20:39:12 +00:00
import * as Drash from "https://cdn.jsdelivr.net/gh/drashland/drash@v2.8.0/mod.ts";
2023-06-06 10:01:12 +00:00
\n\
class SheetJSResource extends Drash.Resource {
public paths = ["/"];
\n\
public POST(request: Drash.Request, response: Drash.Response) {
// highlight-start
/* get data from body */
const file = request.bodyParam<Drash.Types.BodyFile>("file");
/* parse */
var wb = read(file.content, {type: "buffer", dense: true});
// highlight-end
/* generate HTML from first worksheet */
return response.html(utils.sheet_to_html(wb.Sheets[wb.SheetNames[0]]));
}
}`}
</CodeBlock>
## Demo
:::note
This demo was last tested on 2023 June 05. The service <https://s2c.sheetjs.com>
was implemented using this exact sequence.
:::
1) Register and Sign in.
2) Click "New Project" to create a new Project. In the next screen, look for the
"Hello World" sample and click the corresponding "Fork" button.
3) Download [`s2c.ts`](pathname:///deno/s2c.ts). Open with a text editor and
copy the contents into the playground editor (left pane).
4) Click "Save and Deploy".
### Testing
5) Download the test file <https://sheetjs.com/pres.xlsx>
6) In the browser window, click "Choose File" and select the downloaded file.
Click "Submit" and the page will show the contents in a HTML TABLE.
7) Click the "Fullscreen" icon in the top-right corner of the page window.
8) Open a terminal window and download <https://sheetjs.com/pres.numbers>:
```bash
curl -LO https://sheetjs.com/pres.numbers
```
9) Copy the first `curl` line from the page and run in the terminal. For
example, if the deployment is `clean-badger-69`, the command would be
```bash
curl -X POST -F"file=@pres.numbers" https://clean-badger-69.deno.dev/
```
The output will be an HTML table
10) Copy the second `curl` line from the page and run in the terminal. For
example, if the deployment is `clean-badger-69`, the command would be
```bash
curl -X POST -F"file=@pres.numbers" -F"type=csv" https://clean-badger-69.deno.dev/
```
The output will be CSV.