Coder Social home page Coder Social logo

azure-functions-deno-worker's Introduction

Deno for Azure Functions

           @@@@@@@@@@@,         
       @@@@@@@@@@@@@@@@@@@                        %%%%%%
     @@@@@@        @@@@@@@@@@                    %%%%%%
   @@@@@ @  @           *@@@@@              @   %%%%%%    @
   @@@                    @@@@@           @@   %%%%%%      @@
  @@@@@                   @@@@@        @@@    %%%%%%%%%%%    @@@
  @@@@@@@@@@@@@@@          @@@@      @@      %%%%%%%%%%        @@
   @@@@@@@@@@@@@@          @@@@        @@         %%%%       @@
    @@@@@@@@@@@@@@         @@@           @@      %%%       @@
     @@@@@@@@@@@@@         @               @@    %%      @@
       @@@@@@@@@@@                              %%
            @@@@@@@                             %

Overview

This is a worker that lets you run Deno on Azure Functions. It is implemented as an Azure Functions Custom Handler and runs on the Azure Functions Consumption (serverless) plan.

The project includes a CLI denofunc to make it easy to create, run, and deploy your Deno Azure Functions apps.

3 commands to get started

# initialize function app
denofunc init

# run function app locally
denofunc start

# deploy the app
denofunc publish $functionAppName [--slot $slotName] [--allow-run] [--allow-write]

For more information, try the quickstart below.

Programming model

All Azure Functions triggers and bindings (including custom bindings) are supported.

In this simplified programming model, each function is a single file. Here are a couple of examples:

Check out the new project template for the entire app structure.

Getting started - building a Deno function app

Requirements

Codespaces

You can also get a preconfigured, cloud-based dev environment from Codespaces:

Install the denofunc CLI

To help create, run, and deploy a Deno for Azure Functions app, you need to install the denofunc CLI. denofunc wraps the Azure Functions Core Tools (func) and is used for generating artifacts required to run/deploy the app.

To install the CLI, run the following Deno command.

deno install --allow-run --allow-read --allow-write --allow-net --unstable --force \
    --name=denofunc https://raw.githubusercontent.com/anthonychu/azure-functions-deno-worker/v0.9.0/denofunc.ts

Confirm it is installed correctly:

denofunc --help

Create and run an app locally

  1. Create and change into an empty folder.

  2. Initialize the project:

    denofunc init

    A few of the files that are important to know about:

  3. Run the app locally:

    denofunc start

    The Azure Functions Core Tools (func) is then called to run the function app.

    Note: A folder is automatically generated for the hello_world function containing a file named function.json that is used by the Azure Functions runtime to load the function (they are ignored in .gitnore).

  4. Open the URL displayed on the screen (http://localhost:7071/api/hello_world) to run the function.

  5. Ctrl-C to stop the app.

Deploy the app to Azure

Now that you've run the function app locally, it's time to deploy it to Azure!

  1. Configure some variables (examples are in bash):

    region=centralus # any region where Linux Azure Functions are available
    resourceGroupName=<resource_group_name>
    functionAppName=<function_app_name>
    storageName=<storage_name> # must be between 3 and 24 characters in length and may contain numbers and lowercase letters only.
  2. If you are not authenticated with the Azure CLI, log in.

    # Log in to the Azure CLI
    az login

    This might not work in some environments (e.g. Codespaces). Try az login --use-device-code instead.

  3. Run these Azure CLI commands to create and configure the function app:

    # Create resource group
    az group create -l $region -n $resourceGroupName
    
    # Create storage account needed by function app
    az storage account create -n $storageName -l $region -g $resourceGroupName --sku Standard_LRS
    
    # Create function app (also works on Windows)
    az functionapp create -n $functionAppName --storage-account $storageName \
        --consumption-plan-location $region -g $resourceGroupName \
        --functions-version 3 --runtime dotnet --os-type Linux
  4. Deploy the app:

    denofunc publish $functionAppName

    Prior to deployment, denofunc tool will download the Deno Linux binary matching your locally installed version of deno that is included with the deployment package.

  5. The deployment output will print out the URL of the deployed function. Open to the URL to run your function.

🎉 Congratulations!

You've deployed your first Azure Functions app in Deno! 🦕


Disclaimer: This is a community open source project. No official support is provided by Microsoft.

azure-functions-deno-worker's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

azure-functions-deno-worker's Issues

Queue trigger function cannot work on [email protected]

Queue trigger function with [email protected] raises TypeError: Cannot read property 'name' of undefined.
image

It seems that body.value at mod.ts#L55 may return a Promise object due to this (breaking) PR in [email protected].
As a result, isHttpPassthrough at mod.ts#L120 is always handled as true.

I think the minimum change is following at mod.ts#L114:

-          let parsedBody: any = parseBody(body);
+          let parsedBody: any = await parseBody(body);

But it should be tested.

Cannot install with Deno 1.4.x

There are some following errors when installing denofunc with deno 1.4.x.

TS1371 [ERROR]: This import is never used as a value and must use 'import type' because the 'importsNotUsedAsValues' is set to 'error'.

image

As long as I know, a workaround for this is to use --no-check flag on installing (need to update README.md) or to update dependencies, especially [email protected].

Could you please look into this?

NPM resolution error: Junction/SymLink error on Azure Functions

While we are not using the worker in this repo specifically, we are using the deno executable from a Azure Consumption Function.

When resolving NPM packages, im receiving this error

2023-09-22T02:59:35.118 [Error] �[0m�[1m�[31merror�[0m: Failed creating junction and fallback symlink in node_modules 
folder.Access is denied. (os error 5)A required privilege is not held by the client. (os error 1314), symlink 
'D:\DWASFiles\Sites\mawsFnPlaceholder105_f_v4_x64\Temp\z3969c07-10c7-424d-a8b8-
e9f01fd66e72\node_modules\.deno\@[email protected]\node_modules\@azure\abort-controller' -> 
'D:\DWASFiles\Sites\mawsFnPlaceholder105_f_v4_x64\Temp\z3969c07-10c7-424d-a8b8-
e9f01fd66e72\node_modules\.deno\@[email protected]\node_modules\@azure\abort-controller'

It seems that Deno uses Junctions/Symlinks when installing the NPM package references. However, creation of symlinks is not allowed in AzureWebApps. https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox#symbolic-link-creation

Is there away to prevent the symlink creationg during npm install? Has anyone using Deno with Azure Functions encountered this issue before? I assume the same scenario could play out when using the azure functions deno worker if there are npm specifiers in the custom handler, but I have not tested that yet.

Add Windows support

denofunc seems to be having trouble resolving path to func.

https://github.com/anthonychu/azure-functions-deno-worker/blob/ccf2640/denofunc.ts#L120

> denofunc start
Generating functions...
Cleaning function folders...
Deleting folder hello_world.
Generating function folders...
Generating file hello_world/function.json.
Running command: deno bundle --unstable worker.ts worker.bundle.js
Bundling file:///D:/git/temp/20200513-test-deno/worker.ts
Emitting bundle to "worker.bundle.js"
1564236 bytes emmited.
Starting Azure Functions Core Tools: func start
error: Uncaught NotFound: The system cannot find the file specified. (os error 2)
    at unwrapResponse ($deno$/ops/dispatch_json.ts:43:11)
    at Object.sendSync ($deno$/ops/dispatch_json.ts:72:10)
    at Object.run ($deno$/ops/process.ts:45:10)
    at Object.run ($deno$/process.ts:118:15)
    at runFunc (denofunc.ts:125:23)
    at async denofunc.ts:18:5

Also, chmod doesn't work. Need to investigate if this is needed on Windows to turn on execute bit for the deno binary to be deployed properly. Not needed once this fix is fully deployed to Azure: Azure/azure-functions-host#5696

https://github.com/anthonychu/azure-functions-deno-worker/blob/ccf2640/denofunc.ts#L63

Cannot get `url` from context in HTTP trigger

When trying to access the context.req.url property in an HTTP trigger function, VSCode shows TS error as the following screenshot.

image

As the following documenation says, the req object has url property that means the actual requested URL.
https://docs.microsoft.com/en-us/azure/azure-functions/functions-custom-handlers#examples
image

The url property is defined in the interface HttpRequest but is commented out currently.

// /**
// * Request URL.
// */
// url: string;

This cause the above TS error.

There might be some reasons why this is commented out but I'm not sure.
@anthonychu do you have any concerns if this comment out is removed and the url is defined?

Combination of [email protected] and [email protected] do not work properly

[email protected] (introduced by PR #35) causes the following error on denofunc 0.7.0 and deno v1.11.x .

Error:

$ denofunc
Check https://raw.githubusercontent.com/anthonychu/azure-functions-deno-worker/v0.7.0/denofunc.ts
error: TS2339 [ERROR]: Property 'operator' does not exist on type 'never'.
    if (high!.operator === comp || high!.operator === ecomp) {
              ~~~~~~~~
    at https://deno.land/x/[email protected]/mod.ts:1733:15

TS2339 [ERROR]: Property 'operator' does not exist on type 'never'.
    if (high!.operator === comp || high!.operator === ecomp) {
                                         ~~~~~~~~
    at https://deno.land/x/[email protected]/mod.ts:1733:42

TS2339 [ERROR]: Property 'operator' does not exist on type 'never'.
      (!low!.operator || low!.operator === comp) &&
             ~~~~~~~~
    at https://deno.land/x/[email protected]/mod.ts:1740:14

TS2339 [ERROR]: Property 'operator' does not exist on type 'never'.
      (!low!.operator || low!.operator === comp) &&
                              ~~~~~~~~
    at https://deno.land/x/[email protected]/mod.ts:1740:31

TS2339 [ERROR]: Property 'semver' does not exist on type 'never'.
      ltefn(version, low!.semver)
                          ~~~~~~
    at https://deno.land/x/[email protected]/mod.ts:1741:27

TS2339 [ERROR]: Property 'operator' does not exist on type 'never'.
    } else if (low!.operator === ecomp && ltfn(version, low!.semver)) {
                    ~~~~~~~~
    at https://deno.land/x/[email protected]/mod.ts:1744:21

TS2339 [ERROR]: Property 'semver' does not exist on type 'never'.
    } else if (low!.operator === ecomp && ltfn(version, low!.semver)) {
                                                             ~~~~~~
    at https://deno.land/x/[email protected]/mod.ts:1744:62

Environment:

$ deno --version
deno 1.11.5 (release, x86_64-unknown-linux-gnu)
v8 9.1.269.35
typescript 4.3.2

Doesn't install on Windows 10

Running the standard install:

deno install --allow-run --allow-read --allow-write --allow-net --unstable --force --name=denofunc https://deno.land/x/azure_functions/denofunc.ts

I get the following error on Windows 10 in Powershell.

Download https://deno.land/x/oak/pathToRegExp.ts
error: TS2322 [ERROR]: Type 'string' is not assignable to type 'boolean'.
  return srcArray.reduce(

    at https://deno.land/[email protected]/fs/_util.ts:20:3

Route parameters in the binding don't appear to work

I was experimenting with this. After running denofunc init and I changed the hello_world file like below. As far as I can tell, route parameters aren't passed into the function. Am I missing something?

import { AzureFunctionsContext } from "../deps.ts";

async function handler(context: AzureFunctionsContext) {
  context.res = {
    status: 200,
    body: `${context.bindingData.category}/${context.bindingData.id}`
  };
}

export default {
  handler,

  // Name of the function
  name: "hello_world",
  metadata: {
    bindings: [{
      type: "httpTrigger",
      name: "req",
      direction: "in",
      methods: ["get"],
      route: "products/{category:alpha}/{id:int?}"
    }, {
      type: "http",
      name: "res",
      direction: "out"
    }]
  }
  // By default, it's an HTTP function. For other functions, add a `metadata` property
  // with the contents of function.json that describes the trigger and bindings.
  // https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-example
};

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.