Skip to content

Commit

Permalink
Modernize Deno implementation and prepare for JSR
Browse files Browse the repository at this point in the history
Updates everything to be compatible with Deno v2 and prepares for
publishing the packages to the [JSR](https://jsr.io) instead of
the HTTPS based imports common before the JSR.

The JSR doesn't allow for complex inferred types.[^1]
Therefore, prior `z.infer<Schema>` types have been replaced with
explicit interfaces.

Instead of the pattern of having all dependencies defined in a `deps.ts`,
it is now common practice (and requirement for the JSR) to have a `deno.json`,
which acts in a similar way to the `package.json` in NodeJS. It also serves
as an import map for dependencies, pinning their versions.

Note that while it's possible to omit the `jsr:@scope/package` and just write
`@scope/package` with this, to keep samples copy-pasteable without having to
copy the import map, imports in the samples still use the "fully qualified"
import, even though the respective versions are then pinned down using the
`deno.json` file.

In the `Dockerfile`s for the samples as well as the `docker-compose.yml` for
development in the root `backend-deno` folder, the Deno base image was upgraded
to Deno v2.

The new NATS version doesn't use (or provide) the codec functions anymore.[^2]
Therefore, the version upgrade also means that we no longer provide these as
re-exported functions.

Now, any publishing method also allows to pass a `string`, meaning one can just
use something like:

```ts
nc.publish(JSON.stringify({}));
```

Also, the `Msg` interface (any message received by NATS) now offers convenience
methods for retrieving the payload as either `json()` or a `string()`.

- JetStream is no longer included and has to be installed as `jsr:@nats-io/jetstream`;
- KV (a key-value-storage) is no longer included, but available as `jsr:@nats-io/kv`.

See <https://github.com/nats-io/nats.js/blob/e8520523a9b5316f436c44ef05ba2e50c4f6e60a/migration.md>
for a full list of changes / migration guide.

In `ensureMinimalConfig()` (which gets called by `startService()`), the function
now throws an error instead of just printing it to the console and running
`Deno.exit(1)`. This fixes various tests that failed with the upgraded Deno version.

Note that I was unable to find the change that introduced the inabilities of tests to
handle `Deno.exit(1)`. I just know that it worked previously, didn't anymore, and will
now, with this change, work once again.

[^1]: Cf. <https://jsr.io/docs/about-slow-types>
[^2]: Cf. <https://github.com/nats-io/nats.js/blob/e8520523a9b5316f436c44ef05ba2e50c4f6e60a/migration.md#changes-in-nats-base-client>
  • Loading branch information
pklaschka committed Dec 6, 2024
1 parent f57c0ff commit 3c66657
Show file tree
Hide file tree
Showing 20 changed files with 270 additions and 111 deletions.
2 changes: 1 addition & 1 deletion backend-deno/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This library provides a framework for building Telestion services in Deno.
## Usage

```typescript
import {startService} from 'https://deno.land/x/telestion/mod.ts';
import {startService} from 'jsr:@wuespace/telestion';

const {nc} = await startService();
```
Expand Down
3 changes: 0 additions & 3 deletions backend-deno/cucumber/deps.ts

This file was deleted.

2 changes: 1 addition & 1 deletion backend-deno/cucumber/step-registry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolve } from "./deps.ts";
import { resolve } from "@std/path";

/**
* A step definition
Expand Down
4 changes: 2 additions & 2 deletions backend-deno/cucumber/steps/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { startService } from "../../mod.ts";
import { startService } from "jsr:@wuespace/telestion";
import { Given, Then } from "../step-registry.ts";
import { assertEquals } from "./deps.ts";
import { assertEquals } from "@std/assert";

Given('I have an environment variable named {string} with value {string}', (_ctx, key, value) => {
Deno.env.set(key, value);
Expand Down
5 changes: 0 additions & 5 deletions backend-deno/cucumber/steps/deps.ts

This file was deleted.

3 changes: 2 additions & 1 deletion backend-deno/cucumber/steps/nats.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ConnectionOptions, NatsConnection } from "@nats-io/nats-core";
import { Given, Then } from "../step-registry.ts";
import { ConnectionOptions, NatsConnection, assert, assertEquals } from "./deps.ts";
import { assert, assertEquals } from "@std/assert";

/**
* A mock NATS client that can be used to test services that use NATS.
Expand Down
4 changes: 2 additions & 2 deletions backend-deno/cucumber/steps/start.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { startService } from "../../mod.ts";
import { startService } from "jsr:@wuespace/telestion";
import { Then, When } from "../step-registry.ts";
import { assert, assertRejects } from "./deps.ts";
import { assert, assertRejects } from "@std/assert";

When("I start the service", async (ctx) => {
if (!ctx.nats) {
Expand Down
4 changes: 3 additions & 1 deletion backend-deno/cucumber/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
* MIT License (MIT)
*/

import { AssertionError, parseArgs, resolve } from "./deps.ts";
import { parseArgs } from "@std/cli";
import { getStep, importStepDefinitions } from "./step-registry.ts";
import { resolve } from "@std/path";
import { AssertionError } from "@std/assert";

/// Determine steps and features folder from command line arguments

Expand Down
23 changes: 23 additions & 0 deletions backend-deno/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "@wuespace/telestion",
"license": "MIT",
"version": "1.0.0-alpha.4",
"exports": "./mod.ts",
"publish": {
"include": [
"mod.ts",
"README.md",
"LICENSE"
]
},
"imports": {
"@nats-io/nats-core": "jsr:@nats-io/[email protected]",
"@nats-io/transport-deno": "jsr:@nats-io/[email protected]",
"@std/assert": "jsr:@std/assert@^1.0.8",
"@std/cli": "jsr:@std/cli@^1.0.7",
"@std/jsonc": "jsr:@std/jsonc@^1.0.1",
"@std/path": "jsr:@std/path@^1.0.8",
"jsr:@wuespace/telestion": "./mod.ts",
"zod": "npm:zod@^3.23.8"
}
}
117 changes: 117 additions & 0 deletions backend-deno/deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions backend-deno/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
services:
backend-deno:
image: denoland/deno:1.39.0
command: [ "test", "--allow-all", "/app/cucumber/test.ts", "--",
image: denoland/deno:2.1.2
working_dir: /app
command: [ "test", "--allow-all", "./cucumber/test.ts", "--",
"--features", "/features",
"--steps", "/app/cucumber/steps", ]
volumes:
Expand Down
Loading

0 comments on commit 3c66657

Please sign in to comment.