Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added typescript lambda support #2

Closed
wants to merge 3 commits into from

Conversation

Meesayen
Copy link

This adds support for Typescript lambdas via a new bin now-we-test-ts which automatically injects ts-node/register as a node require hook.

ts-node and typescript will be necessary to run .ts lambdas, but they have been added as peerDependencies in case the user doesn't need them.

I'll leave some comments on the changes to help the reviews.

Fixes: #1

@codecov-io
Copy link

codecov-io commented Dec 28, 2018

Codecov Report

Merging #2 into master will decrease coverage by 0.37%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master       #2      +/-   ##
==========================================
- Coverage    98.9%   98.52%   -0.38%     
==========================================
  Files           3        3              
  Lines          91       68      -23     
  Branches        9        9              
==========================================
- Hits           90       67      -23     
  Misses          1        1
Impacted Files Coverage Δ
lib/commands/serve.js 98.33% <100%> (+0.08%) ⬆️
lib/simulator.js
lib/lambda.js 100% <0%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8075b4f...9c43b79. Read the comment docs.

@@ -11,6 +11,8 @@ const clearModule = require('clear-module')

const { createError } = micro

const lamdbaMap = new Map()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created a map which will cache imported lambdas upon usage. This was necessary to fix a problem I encountered when requiring a lambda that was importing and using firebase.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide me with the full firebase related error? We can't really allow for module cache reusing for this would break the concept of self-enclosed, no-shared-context of lambda executions. I tried something with child processes, but it didn't work so far. Maybe we can clear a broader module scope on clearModule.match(...) to ensure anything needed by the lambda gets cleared out and re-loaded...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the error output of the second time you'd request a lambda using firebase:

Error: Module did not self-register.
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:718:18)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (/[REDACTED]/node_modules/grpc/src/grpc_extension.js:32:13)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)

Not really helpful. My only quick thought was that the module being required multiple times was breaking some firebase internal module relationships.

Maybe yes, a broader clean up of modules is the key.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Meesayen do you mind to test your firebase related code using branch fs/broader-cache-cleanup?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I couldn't have a look at this lately.
Here's what I'll do, I'll get rid of the caching part on this PR, and unblock the basic typescript support. The firebase issue can wait (firebase doesn't work when deployed on Now anyway...)

@@ -32,7 +34,7 @@ class ServeCommand extends Command {
const { builds = [], routes = [] } = require(nowConfigPath)

// find all node lambdas
const nodeBuilds = builds.filter(({ use }) => use === '@now/node')
const nodeBuilds = builds.filter(({ use }) => /^@now\/node@?/.test(use))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment, Typescript support on Now v2 is only possible via the canary version of @now/node, hence the check for the optional @ (i.e. @now/node@canary will be the builder for some time)

)
let pathname = urls
.map(
url =>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can probably be simplified, but in short, we only match Typescript lambdas if they end with .ts explicitly, when requested. Either manually or via a routes alias in now.json.

@AlexGeb
Copy link

AlexGeb commented Jan 8, 2019

Thank you for this guys, I'm looking forward to using this new feature !

typeof entrypoint === 'string' ? require(entrypoint) : entrypoint

// Supporting both Typescript and ESM type of imports
if (lambda.default) lambda = lambda.default
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a bit sloppy, as it can break when importing a CJS module such as:

module.exports = {
  default: {...},
  fn() {}
}

but since we're dealing with Lambdas, I'd expect that each module will only export exactly one function.

@Meesayen Meesayen closed this Nov 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TypeScript support
4 participants