pulumi/sdk/nodejs
Will Jones cb8277d6a4
Try and avoid serializing `runtime/resource.js` (#17247)
Several Pulumi NodeJS SDKs support passing NodeJS closures directly to
resources in lieu of e.g. source files that must be managed separately
on the file system. The example below, for instance, shows how a NodeJS
closure can be used to declare an AWS Lambda that responds to objects
being created in an S3 bucket:

```typescript
const bucket = new aws.s3.Bucket("my-bucket")

bucket.onObjectCreated("listener", () => {
  console.log("An object was created in the S3 bucket!")
})
```

Under the hood, Pulumi serializes the passed closure to a piece of code
and takes care of packaging that code appropriately for use as a Lambda
function. This serialization is non-trivial: for packaged code to work,
Pulumi must be careful to capture and serialize the transitive
dependency graph of the closure passed -- the functions and values that
the closure depends on, as well as the dependencies of those
dependencies, and so on.

This change fixes a regression whereby code that transitively depends on
the Pulumi SDK no longer serializes to working code. Let's modify the
above example to use `@pulumi/pulumi` types and values:

```typescript
const bucket = new aws.s3.Bucket("my-bucket")

const task = new awsx.ecs.FargateTaskDefinition("task", {
  container: {
    name: "container",
    image: "node",
  },
})

bucket.onObjectCreated("listener", () => {
  console.log(task.taskDefinition.get())
})
```

When this code is serialized, it captures parts of the Pulumi SDK, since
`task.taskDefinition.get()` involves SDK classes and their members. The
Pulumi SDK is marked as a `deploymentOnlyModule`, meaning that it is not
intended to be captured in its entirety, since it is not expected to
work in its entirety in such a context. However, there may be select
parts of the SDK (or other `deploymentOnlyModule`s) that _do_ work and
so the serialization code tries to grab just the referenced values.

One such referenced value is the `Resource.getProvider` method, which
was recently refactored to use a helper method, `pkgFromType`, defined
in the `runtime/resource` module. This causes the serializer to generate
a `require("@pulumi/pulumi/runtime/resource")` call, which fails since
the remainder of the SDK has not been captured.

It might be possible to fix this "properly" with a better or more
considered approach to serialization in the presence of
`deploymentOnlyModule`, but in the interests of fixing the regression
this commit takes the lazier option. Since `sdk/nodejs/resource` and
`sdk/nodejs/runtime/resource` _already_ depend on each other cyclically
(this is permitted by NodeJS), we just move the `pkgFromType` helper
function (which is used by `resource` and `runtime/resource`) to
`resource` instead. Thus, when we serialize `resource`, we don't
generate the import to `runtime/resource`.

In theory, moving the function like this could create the same issue in
the opposite direction (whereby the use of `runtime/resource` generates
a bad `require` of `resource`). However, it feels even less likely that
`pulumi.runtime.*` functions should be supported inside serializable
closures, and the only indirect caller of the majority of
`runtime/resource`'s exports _is_ `resource` (the others being
`Callable`s, `settings`, and `rpc`, which again feel like they have no
business being in a serialized closure). As a result, this is hopefully
safe. If it turns out not to be, duplicating the function would be the
next step to take.

Fixes #14671
2024-09-12 16:27:53 +00:00
..
asset Improve TypeDocs for `sdk/nodejs` (#16622) 2024-07-15 09:05:28 +00:00
automation add the attach-debugger flag to automation API (#17175) 2024-09-05 11:19:01 +00:00
cmd Changelog and go.mod updates for v3.132.0 (#17241) 2024-09-12 00:01:48 +00:00
dist Fix typo in PP script 2019-08-06 17:35:52 -07:00
dynamic Improve TypeDocs for `sdk/nodejs` (#16622) 2024-07-15 09:05:28 +00:00
iterable Improve TypeDocs for `sdk/nodejs` (#16622) 2024-07-15 09:05:28 +00:00
log Improve TypeDocs for `sdk/nodejs` (#16622) 2024-07-15 09:05:28 +00:00
npm Enable goheader rule and add missing license headers (#15473) 2024-09-09 12:05:45 +00:00
proto Send resource name and type as well as URN to providers (#17177) 2024-09-06 09:29:37 +00:00
provider Improve TypeDocs for `sdk/nodejs` (#16622) 2024-07-15 09:05:28 +00:00
queryable Improve TypeDocs for `sdk/nodejs` (#16622) 2024-07-15 09:05:28 +00:00
runtime Try and avoid serializing `runtime/resource.js` (#17247) 2024-09-12 16:27:53 +00:00
tests Use @types/node matching the ts version for integration tests (#17185) 2024-09-06 09:21:47 +00:00
tests_with_mocks Fix NodeJS SDK mocking of custom resource reads (#16542) 2024-07-01 16:05:30 +00:00
types Replace deprecated read-package-tree with @npmcli/arborist (#15503) 2024-02-26 18:40:28 +00:00
vendor Vendor TypeScript and ts-node (#15622) 2024-04-10 15:26:37 +00:00
.eslintrc.js Vendor TypeScript and ts-node (#15622) 2024-04-10 15:26:37 +00:00
.gitignore Vendor TypeScript and ts-node (#15622) 2024-04-10 15:26:37 +00:00
Makefile Replace Rome with Biome in the NodeJS SDK (#16456) 2024-06-24 11:14:56 +00:00
README.md fix some links 2023-05-19 19:39:26 -07:00
biome.json Replace Rome with Biome in the NodeJS SDK (#16456) 2024-06-24 11:14:56 +00:00
config.ts Replace Rome with Biome in the NodeJS SDK (#16456) 2024-06-24 11:14:56 +00:00
errors.ts Improve TypeDocs for `sdk/nodejs` (#16622) 2024-07-15 09:05:28 +00:00
index.ts Removing x namespace from go/python/nodejs automation packages (#6518) 2021-04-14 19:32:18 +01:00
invoke.ts implement invoke transforms for NodeJS (#16688) 2024-07-18 08:41:39 +00:00
metadata.ts Improve TypeDocs for `sdk/nodejs` (#16622) 2024-07-15 09:05:28 +00:00
output.ts Updated nodejs and python sdk docs on outputs (#16885) 2024-09-09 08:11:25 +00:00
package.json Freeze v3.132.0 (#17234) 2024-09-11 22:00:05 +00:00
resource.ts Try and avoid serializing `runtime/resource.js` (#17247) 2024-09-12 16:27:53 +00:00
stackReference.ts Improve TypeDocs for `sdk/nodejs` (#16622) 2024-07-15 09:05:28 +00:00
tsconfig.json Revert "Implement `dependsOn` for invokes in the NodeJS SDK (#16560)" (#16642) 2024-07-12 10:16:55 +00:00
tsutils.ts Improve TypeDocs for `sdk/nodejs` (#16622) 2024-07-15 09:05:28 +00:00
typescript-shim.ts Vendor TypeScript and ts-node (#15622) 2024-04-10 15:26:37 +00:00
utils.ts Improve TypeDocs for `sdk/nodejs` (#16622) 2024-07-15 09:05:28 +00:00
version.ts Freeze v3.132.0 (#17234) 2024-09-11 22:00:05 +00:00
yarn.lock Bump NodeJS SDK dependencies (#17214) 2024-09-10 09:23:20 +00:00

README.md

Pulumi Node.js SDK

The Pulumi Node.js SDK lets you write cloud programs in JavaScript.

Installation

Using npm:

$ npm install --save @pulumi/pulumi

Using yarn:

$ yarn add @pulumi/pulumi

This SDK is meant for use with the Pulumi CLI. Visit Pulumi's Download & Install to install the CLI.

Building and Testing

For anybody who wants to build from source, here is how you do it.

Prerequisites

This SDK uses Node.js and we support any of the Current, Active and Maintenance LTS versions. We support both NPM and Yarn for package management.

At the moment, we only support building on macOS and Linux, where standard GNU tools like make are available.

Make Targets

To build the SDK, simply run make from the root directory (where this README lives, at sdk/nodejs/ from the repo's root). This will build the code, run tests, and install the package and its supporting artifacts.

At the moment, for local development, we install everything into $HOME/.dev-pulumi. You will want this on your $PATH.

The tests will verify that everything works, but feel free to try running pulumi preview and/or pulumi up from the examples/minimal/ directory. Remember to run tsc first, since pulumi expects JavaScript, not TypeScript.