Skip to content

Commit

Permalink
fix: handle response erroring and invalid content types (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Dec 26, 2023
1 parent 75182af commit ee37a98
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 51 deletions.
10 changes: 6 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ jobs:
CI:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: bvm/[email protected]
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- name: Test
run: deno test --allow-net
- name: Get tag version
Expand All @@ -14,10 +16,10 @@ jobs:
run: echo ::set-output name=TAG_VERSION::${GITHUB_REF/refs\/tags\//}
- uses: actions/setup-node@v2
with:
node-version: '16.x'
node-version: '18.x'
registry-url: 'https://registry.npmjs.org'
- name: npm build
run: deno run -A --no-check ./scripts/build_npm.ts ${{steps.get_tag_version.outputs.TAG_VERSION}}
run: deno task build:npm ${{steps.get_tag_version.outputs.TAG_VERSION}}
- name: npm publish
if: startsWith(github.ref, 'refs/tags/')
env:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.vscode
npm
deno.lock
5 changes: 0 additions & 5 deletions .vscode/settings.json

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2020-2022 David Sherret
Copyright (c) 2020-2023 David Sherret

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
9 changes: 0 additions & 9 deletions bvm.json

This file was deleted.

8 changes: 8 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"tasks": {
"build:npm": "deno run -A ./scripts/build_npm.ts"
},
"exclude": [
"npm"
]
}
15 changes: 3 additions & 12 deletions dprint.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
{
"typescript": {
"indentWidth": 2
},
"json": {
"indentWidth": 2
},
"markdown": {
},
"includes": ["**/*.{ts,tsx,js,jsx,cjs,mjs,json,md}"],
"excludes": [
"**/node_modules",
"**/*-lock.json"
],
"plugins": [
"https://plugins.dprint.dev/typescript-0.74.0.wasm",
"https://plugins.dprint.dev/json-0.15.6.wasm",
"https://plugins.dprint.dev/markdown-0.14.1.wasm"
"https://plugins.dprint.dev/typescript-0.88.7.wasm",
"https://plugins.dprint.dev/json-0.19.1.wasm",
"https://plugins.dprint.dev/markdown-0.16.3.wasm"
]
}
36 changes: 19 additions & 17 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,39 +86,41 @@ export function createImportObject(): WebAssembly.Imports {
}

export interface ResponseLike {
status: number;
arrayBuffer(): Promise<BufferSource>;
text(): Promise<string>;
headers: {
get(name: string): string | null;
};
}

/**
* Creates a formatter from the specified streaming source.
* @remarks This is the most efficient way to create a formatter.
* @param response - The streaming source to create the formatter from.
*/
export function createStreaming(
response: Promise<ResponseLike>,
export async function createStreaming(
responsePromise: Promise<ResponseLike> | ResponseLike,
): Promise<Formatter> {
if (typeof WebAssembly.instantiateStreaming === "function") {
const response = await responsePromise;
if (response.status !== 200) {
throw new Error(
`Unexpected status code: ${response.status}\n${await response.text()}`,
);
}
if (
typeof WebAssembly.instantiateStreaming === "function"
&& response.headers.get("content-type") === "application/wasm"
) {
return WebAssembly
// deno-lint-ignore no-explicit-any
.instantiateStreaming(response as any, createImportObject())
.then((obj) => createFromInstance(obj.instance));
} else {
// fallback for node.js
return getArrayBuffer()
// fallback for node.js or when the content type isn't application/wasm
return response.arrayBuffer()
.then((buffer) => createFromBuffer(buffer));
}

function getArrayBuffer() {
if (isResponse(response)) {
return response.arrayBuffer();
} else {
return response.then((response) => response.arrayBuffer());
}

function isResponse(response: unknown): response is ResponseLike {
return (response as Response).arrayBuffer != null;
}
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion mod_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals } from "https://deno.land/std@0.113.0/testing/asserts.ts";
import { assertEquals } from "https://deno.land/std@0.210.0/assert/mod.ts";
import { createFromBuffer, createStreaming, Formatter, GlobalConfiguration } from "./mod.ts";

Deno.test("it should create streaming", async () => {
Expand Down
6 changes: 4 additions & 2 deletions scripts/build_npm.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { build } from "https://deno.land/x/dnt@0.14.0/mod.ts";
import { build } from "https://deno.land/x/dnt@0.39.0/mod.ts";

await build({
entryPoints: ["mod.ts"],
typeCheck: true,
test: true,
outDir: "./npm",
shims: {
Expand All @@ -17,6 +16,9 @@ await build({
},
}],
},
compilerOptions: {
lib: ["ES2021", "DOM"],
},
package: {
name: "@dprint/formatter",
version: Deno.args[0],
Expand Down

0 comments on commit ee37a98

Please sign in to comment.