Skip to content

Commit

Permalink
Deno 2 support (#860)
Browse files Browse the repository at this point in the history
* Deno 2 support

Signed-off-by: Marcos Candeia <[email protected]>

* Remove unused bench

Signed-off-by: Marcos Candeia <[email protected]>

* Support deno 2 githubactions

Signed-off-by: Marcos Candeia <[email protected]>

* Replace to frozen usages

Signed-off-by: Marcos Candeia <[email protected]>

---------

Signed-off-by: Marcos Candeia <[email protected]>
  • Loading branch information
mcandeia authored Oct 15, 2024
1 parent a93ae57 commit f2432d7
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 118 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ jobs:
path: |
/home/runner/.deno
/home/runner/.cache/deno/deps/https/deno.land
- uses: denoland/setup-deno@v1
- uses: denoland/setup-deno@v2
with:
deno-version: v1.x
deno-version: v2.x
- name: Build Deno Module
run: deno run -A --lock=deno.lock --lock-write --reload mod.ts
run: deno run -A --lock=deno.lock --frozen=false --reload mod.ts
- name: fetch any uncached dependencies
run: |
deno cache --lock=deno.lock --lock-write --reload=file: ./mod.ts
deno cache --lock=deno.lock --frozen=false --reload=file: ./mod.ts
- run: |
deno test --lock=deno.lock --lock-write -A .
deno test --lock=deno.lock --frozen=false -A .
- run: |
deno bench --lock=deno.lock --lock-write -A .
deno bench --lock=deno.lock --frozen=false -A .
- name: check lint
continue-on-error: true
run: |
Expand Down
11 changes: 4 additions & 7 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,14 @@ jobs:
steps:
- uses: actions/checkout@v4

- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- name: Publish package
run: deno publish
run: npx jsr publish

- name: Publish script package
run: cd scripts/ && deno publish
run: cd scripts/ && npx jsr publish

- name: Publish dev package
run: cd dev/ && deno publish
run: cd dev/ && npx jsr publish

- name: Set up QEMU
uses: docker/setup-qemu-action@v3
Expand Down Expand Up @@ -105,4 +102,4 @@ jobs:
if [ -n "$LATEST" ]; then
echo "::set-output name=image_builder_latest::$REGISTRY/$REPOSITORY:$IMAGE_TAG_LATEST"
fi
echo "::set-output name=image_builder_commit::$REGISTRY/$REPOSITORY:$IMAGE_TAG_COMMIT"
echo "::set-output name=image_builder_commit::$REGISTRY/$REPOSITORY:$IMAGE_TAG_COMMIT"
4 changes: 2 additions & 2 deletions components/section.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ interface BoundaryState {
}

export class ErrorBoundary extends Component<BoundaryProps, BoundaryState> {
state = { error: null };
override state = { error: null };

static getDerivedStateFromError(error: Error) {
static override getDerivedStateFromError(error: Error) {
if (error instanceof HttpError) {
throw error;
}
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"component": "deno eval 'import \"deco/scripts/component.ts\"'",
"order_imports": "deno eval 'import \"deco/scripts/order_imports.ts\"'",
"release": "deno run -A ./scripts/release.ts",
"check": "deno fmt && deno check live.ts && deno --unstable-http test -A . && deno bench -A .",
"check": "deno fmt && deno check live.ts && deno test --unstable-http -A . && deno bench -A .",
"cache_clean": "rm deno.lock; deno cache -r live.ts",
"info_json": "deno info --json live.ts > deps.json",
"detective": "deno run -A https://deno.land/x/detective/detective.ts deps.json",
Expand Down
1 change: 1 addition & 0 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type {
Attributes,
BatchObservableResult,
Context,
Exception,
Link,
Meter,
ObservableCounter,
Expand Down
3 changes: 2 additions & 1 deletion engine/importmap/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export interface ImportMap {
export const safeImportResolve = (specifier: string): string => {
try {
return import.meta.resolve(specifier);
} catch (err) {
} catch (_err) {
const err = _err as { message?: string };
const msg = err?.message;
if (typeof msg === "string") {
const match = msg.match(JSR_ERROR_REGEX);
Expand Down
28 changes: 0 additions & 28 deletions engine/schema/schemeable.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
import { dirname, join } from "@std/path";

import { fromFileUrl, toFileUrl } from "@std/path";
import {
findSchemeableFromNode,
type Schemeable as _Schemeable,
} from "https://denopkg.com/deco-cx/[email protected]/engine/schema/transform.ts";
import { denoDoc } from "https://denopkg.com/deco-cx/[email protected]/engine/schema/utils.ts";
import { ImportMapBuilder } from "../importmap/builder.ts";
import { parsePath } from "./parser.ts";
import { type Schemeable, typeNameToSchemeable } from "./transform.ts";
Expand Down Expand Up @@ -79,18 +74,6 @@ const getSchemeableDenoAST = async (
});
};

const getSchemeableDenoDoc = async (
path: string,
name: string,
): Promise<_Schemeable | undefined> => {
const ast = await denoDoc(path);
const nodeTypeRef = ast.find((node) => node.name === name);
if (!nodeTypeRef) {
return undefined;
}
return await findSchemeableFromNode(nodeTypeRef, [path, ast], new Map());
};

Deno.bench(
"transform to schemeable using deno_ast",
{ group: "schema_gen", baseline: true },
Expand All @@ -102,14 +85,3 @@ Deno.bench(
}));
},
);

Deno.bench(
"transform to schemeable using denodoc",
{ group: "schema_gen" },
async () => {
localStorage.clear();
await Promise.all(testCases.map(({ path, typeName }) => {
return getSchemeableDenoDoc(path, typeName);
}));
},
);
42 changes: 2 additions & 40 deletions observability/otel/instrumentation/deno-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/
import {
type Attributes,
type BatchObservableResult,
InstrumentationBase,
type InstrumentationConfig,
type ObservableCounter,
Expand All @@ -30,19 +29,6 @@ export class DenoRuntimeInstrumentation extends InstrumentationBase {

protected init() {}

private gatherOpenResources = (x: ObservableResult<Attributes>) => {
for (
const entry of Object
.values(Deno.resources())
.reduce<Map<string, number>>(
(acc, x) => (acc.set(x, 1 + (acc.get(x) ?? 0)), acc),
new Map(),
)
) {
x.observe(entry[1], { "deno.resource.type": entry[0] });
}
};

private gatherMemoryUsage = (x: ObservableResult<Attributes>) => {
const usage = Deno.memoryUsage();
x.observe(usage.rss, { "deno.memory.type": "rss" });
Expand All @@ -51,21 +37,7 @@ export class DenoRuntimeInstrumentation extends InstrumentationBase {
x.observe(usage.external, { "deno.memory.type": "external" });
};

private gatherOps = (x: BatchObservableResult<Attributes>) => {
for (const [op, data] of Object.entries(Deno.metrics().ops)) {
if (data.opsDispatched == 0) continue;
x.observe(this.metrics.dispatchedCtr, data.opsDispatched, {
"deno.op": op,
});
x.observe(
this.metrics.inflightCtr,
data.opsDispatched - data.opsCompleted,
{ "deno.op": op },
);
}
};

enable() {
override enable() {
this.metrics ??= {
openResources: this.meter
.createObservableUpDownCounter("deno.open_resources", {
Expand All @@ -88,20 +60,10 @@ export class DenoRuntimeInstrumentation extends InstrumentationBase {
}),
};

this.metrics.openResources.addCallback(this.gatherOpenResources);
this.metrics.memoryUsage.addCallback(this.gatherMemoryUsage);
this.meter.addBatchObservableCallback(this.gatherOps, [
this.metrics.dispatchedCtr,
this.metrics.inflightCtr,
]);
}

disable() {
this.metrics.openResources.removeCallback(this.gatherOpenResources);
override disable() {
this.metrics.memoryUsage.removeCallback(this.gatherMemoryUsage);
this.meter.removeBatchObservableCallback(this.gatherOps, [
this.metrics.dispatchedCtr,
this.metrics.inflightCtr,
]);
}
}
23 changes: 0 additions & 23 deletions observability/probes/resources.ts

This file was deleted.

4 changes: 2 additions & 2 deletions runtime/caches/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ValueType } from "../../deps.ts";
import { type Exception, ValueType } from "../../deps.ts";
import { tracer } from "../../observability/otel/config.ts";
import { meter } from "../../observability/otel/metrics.ts";

Expand Down Expand Up @@ -38,7 +38,7 @@ export const withInstrumentation = (
});
return isMatch;
} catch (err) {
span.recordException(err);
span.recordException(err as Exception);
throw err;
} finally {
span.end();
Expand Down
3 changes: 2 additions & 1 deletion runtime/caches/fileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ function createFileSystemCache(): CacheStorage {
const filePath = `${FILE_SYSTEM_CACHE_DIRECTORY}/${key}`;
const fileContent = await Deno.readFile(filePath);
return fileContent;
} catch (err) {
} catch (_err) {
const err = _err as { code?: string };
// Error code different for file/dir not found
// The file won't be found in cases where it's not cached
if (err.code !== "ENOENT") {
Expand Down
7 changes: 4 additions & 3 deletions runtime/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { HTTPException } from "@hono/hono/http-exception";
import { DECO_MATCHER_HEADER_QS } from "../blocks/matcher.ts";
import { Context } from "../deco.ts";
import { getCookies, SpanStatusCode } from "../deps.ts";
import { Exception, getCookies, SpanStatusCode } from "../deps.ts";
import { startObserve } from "../observability/http.ts";
import { logger } from "../observability/mod.ts";
import { HttpError } from "../runtime/errors.ts";
Expand Down Expand Up @@ -77,7 +77,8 @@ export const createHandler = <TManifest extends AppManifest = AppManifest>(
async (ctx, next) => {
try {
return await handler(ctx, next);
} catch (err) {
} catch (_err) {
const err = _err as { stack?: string; message?: string };
if (err instanceof HttpError) {
return err.resp;
}
Expand Down Expand Up @@ -278,7 +279,7 @@ export const middlewareFor = <TAppManifest extends AppManifest = AppManifest>(
try {
await next();
} catch (e) {
span.recordException(e);
span.recordException(e as Exception);
throw e;
} finally {
const status = ctx.res?.status ?? 500;
Expand Down
3 changes: 0 additions & 3 deletions runtime/middlewares/liveness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { medianLatencyChecker } from "../../observability/probes/medianLatency.t
import { memoryChecker } from "../../observability/probes/memory.ts";
import { reqCountChecker } from "../../observability/probes/reqCount.ts";
import { reqInflightChecker } from "../../observability/probes/reqInflight.ts";
import { resourcesChecker } from "../../observability/probes/resources.ts";
import { uptimeChecker } from "../../observability/probes/uptime.ts";
import type { DecoMiddleware } from "../middleware.ts";

Expand All @@ -21,7 +20,6 @@ export interface Metrics {
};
mem: Deno.MemoryUsage;
sys: Deno.SystemMemoryInfo;
resources: Deno.ResourceMap;
}

export interface LiveChecker<TValue = number> {
Expand Down Expand Up @@ -125,5 +123,4 @@ export const liveness = buildHandler(
reqCountChecker,
medianLatencyChecker,
reqInflightChecker,
resourcesChecker,
);
12 changes: 11 additions & 1 deletion utils/patched_fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,18 @@ import { RequestContext } from "../deco.ts";
// Monkey patch fetch so we can have global cancelation token
const fetcher = globalThis.fetch;

type RequestInitWithSignal = (RequestInit | globalThis.RequestInit) & {
signal?: AbortSignal;
};
const hasSignal = (
init: RequestInitWithSignal | unknown,
): init is RequestInitWithSignal => {
return init !== null && typeof init === "object" && ("signal" in init);
};
globalThis.fetch = (input, init) => {
const signal = init?.signal || RequestContext.signal;
const signal = hasSignal(init)
? init.signal || RequestContext.signal
: RequestContext.signal;

signal?.throwIfAborted?.();

Expand Down

0 comments on commit f2432d7

Please sign in to comment.