Skip to content

Commit ee37a98

Browse files
authored
fix: handle response erroring and invalid content types (#10)
1 parent 75182af commit ee37a98

File tree

10 files changed

+44
-51
lines changed

10 files changed

+44
-51
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ jobs:
44
CI:
55
runs-on: ubuntu-latest
66
steps:
7-
- uses: actions/checkout@v2
8-
- uses: bvm/gh-action@v1.1.0
7+
- uses: actions/checkout@v4
8+
- uses: denoland/setup-deno@v1
9+
with:
10+
deno-version: v1.x
911
- name: Test
1012
run: deno test --allow-net
1113
- name: Get tag version
@@ -14,10 +16,10 @@ jobs:
1416
run: echo ::set-output name=TAG_VERSION::${GITHUB_REF/refs\/tags\//}
1517
- uses: actions/setup-node@v2
1618
with:
17-
node-version: '16.x'
19+
node-version: '18.x'
1820
registry-url: 'https://registry.npmjs.org'
1921
- name: npm build
20-
run: deno run -A --no-check ./scripts/build_npm.ts ${{steps.get_tag_version.outputs.TAG_VERSION}}
22+
run: deno task build:npm ${{steps.get_tag_version.outputs.TAG_VERSION}}
2123
- name: npm publish
2224
if: startsWith(github.ref, 'refs/tags/')
2325
env:

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
.vscode
12
npm
3+
deno.lock

.vscode/settings.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2020-2022 David Sherret
3+
Copyright (c) 2020-2023 David Sherret
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

bvm.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

deno.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"tasks": {
3+
"build:npm": "deno run -A ./scripts/build_npm.ts"
4+
},
5+
"exclude": [
6+
"npm"
7+
]
8+
}

dprint.json

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
11
{
2-
"typescript": {
3-
"indentWidth": 2
4-
},
5-
"json": {
6-
"indentWidth": 2
7-
},
8-
"markdown": {
9-
},
10-
"includes": ["**/*.{ts,tsx,js,jsx,cjs,mjs,json,md}"],
112
"excludes": [
123
"**/node_modules",
134
"**/*-lock.json"
145
],
156
"plugins": [
16-
"https://plugins.dprint.dev/typescript-0.74.0.wasm",
17-
"https://plugins.dprint.dev/json-0.15.6.wasm",
18-
"https://plugins.dprint.dev/markdown-0.14.1.wasm"
7+
"https://plugins.dprint.dev/typescript-0.88.7.wasm",
8+
"https://plugins.dprint.dev/json-0.19.1.wasm",
9+
"https://plugins.dprint.dev/markdown-0.16.3.wasm"
1910
]
2011
}

mod.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -86,39 +86,41 @@ export function createImportObject(): WebAssembly.Imports {
8686
}
8787

8888
export interface ResponseLike {
89+
status: number;
8990
arrayBuffer(): Promise<BufferSource>;
91+
text(): Promise<string>;
92+
headers: {
93+
get(name: string): string | null;
94+
};
9095
}
9196

9297
/**
9398
* Creates a formatter from the specified streaming source.
9499
* @remarks This is the most efficient way to create a formatter.
95100
* @param response - The streaming source to create the formatter from.
96101
*/
97-
export function createStreaming(
98-
response: Promise<ResponseLike>,
102+
export async function createStreaming(
103+
responsePromise: Promise<ResponseLike> | ResponseLike,
99104
): Promise<Formatter> {
100-
if (typeof WebAssembly.instantiateStreaming === "function") {
105+
const response = await responsePromise;
106+
if (response.status !== 200) {
107+
throw new Error(
108+
`Unexpected status code: ${response.status}\n${await response.text()}`,
109+
);
110+
}
111+
if (
112+
typeof WebAssembly.instantiateStreaming === "function"
113+
&& response.headers.get("content-type") === "application/wasm"
114+
) {
101115
return WebAssembly
102116
// deno-lint-ignore no-explicit-any
103117
.instantiateStreaming(response as any, createImportObject())
104118
.then((obj) => createFromInstance(obj.instance));
105119
} else {
106-
// fallback for node.js
107-
return getArrayBuffer()
120+
// fallback for node.js or when the content type isn't application/wasm
121+
return response.arrayBuffer()
108122
.then((buffer) => createFromBuffer(buffer));
109123
}
110-
111-
function getArrayBuffer() {
112-
if (isResponse(response)) {
113-
return response.arrayBuffer();
114-
} else {
115-
return response.then((response) => response.arrayBuffer());
116-
}
117-
118-
function isResponse(response: unknown): response is ResponseLike {
119-
return (response as Response).arrayBuffer != null;
120-
}
121-
}
122124
}
123125

124126
/**

mod_test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assertEquals } from "https://deno.land/std@0.113.0/testing/asserts.ts";
1+
import { assertEquals } from "https://deno.land/std@0.210.0/assert/mod.ts";
22
import { createFromBuffer, createStreaming, Formatter, GlobalConfiguration } from "./mod.ts";
33

44
Deno.test("it should create streaming", async () => {

scripts/build_npm.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { build } from "https://deno.land/x/dnt@0.14.0/mod.ts";
1+
import { build } from "https://deno.land/x/dnt@0.39.0/mod.ts";
22

33
await build({
44
entryPoints: ["mod.ts"],
5-
typeCheck: true,
65
test: true,
76
outDir: "./npm",
87
shims: {
@@ -17,6 +16,9 @@ await build({
1716
},
1817
}],
1918
},
19+
compilerOptions: {
20+
lib: ["ES2021", "DOM"],
21+
},
2022
package: {
2123
name: "@dprint/formatter",
2224
version: Deno.args[0],

0 commit comments

Comments
 (0)