Skip to content

Commit 74e5b42

Browse files
committed
Fix types for TypeScript 5.7 (Deno 2.2)
Deno v2.2 upgraded to TypeScript 5.7. There's a nice writeup of the relevant change in this PR: microsoft/TypeScript#59417 In short, there are now two variants of Uint8Array: Uint8Array<ArrayBuffer> and Uint8Array<SharedArrayBuffer>. And their types have diverged. If you don't specify which one you're returning, you get a union of the two, which is no longer assignment-compatible with ArrayBuffer. Fix our types (and some Deno types) to be explicit there.
1 parent 38cdcdd commit 74e5b42

File tree

10 files changed

+815
-351
lines changed

10 files changed

+815
-351
lines changed

deno.jsonc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nfnitloop/deno-embedder",
3-
"version": "1.6.1",
3+
"version": "2.0.0-alpha-1",
44
"exports": {
55
".": "./src/mod.ts",
66
"./plugins/plugins": "./src/plugins/plugins.ts",
@@ -164,5 +164,5 @@
164164
// Pseudocode doc block example:
165165
"src/helpers/hono.ts"
166166
]
167-
}
167+
},
168168
}

deno.lock

Lines changed: 164 additions & 53 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/hono/deno.lock

Lines changed: 159 additions & 53 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/with-embedder/deno.lock

Lines changed: 169 additions & 53 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/without-embedder/deno.lock

Lines changed: 122 additions & 118 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/deps/std/encoding/base64.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
export * from "jsr:@std/[email protected]/base64"
1+
import {decodeBase64 as upstream} from "jsr:@std/[email protected]/base64"
2+
export { encodeBase64 } from "jsr:@std/[email protected]/base64"
3+
4+
// Fix type. (v1.0 has a breaking change to encoding/decoding.)
5+
export function decodeBase64(value: string): Uint8Array<ArrayBuffer> {
6+
return upstream(value) as Uint8Array<ArrayBuffer>
7+
}
8+

src/embed.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,32 @@ export type ImportMeta = {
2323

2424
const decoder = new TextDecoder()
2525

26+
/** A reference to an embedded file's data and metadata. */
27+
export type FileHandle = {
28+
/** Size of the embedded file in bytes (uncomrpessed/unencoded) */
29+
readonly size: number
30+
31+
/** Returns the raw bytes of the embedded file. */
32+
bytes(): Promise<Uint8Array<ArrayBuffer>>;
33+
34+
/**
35+
* Parse the bytes as utf-8 text.
36+
*/
37+
text(): Promise<string>;
38+
}
39+
2640
/**
2741
* Represents the contents of a file that's been embedded into TypeScript.
2842
*/
29-
export class File {
43+
export class File implements FileHandle {
3044
/** Size of the embedded file in bytes (uncomrpessed/unencoded) */
3145
readonly size: number
3246

3347
/** May be compressed */
34-
#contents: {bytes: Uint8Array, compression: CompressionFormat | undefined }
48+
#contents: {
49+
bytes: Uint8Array<ArrayBuffer>,
50+
compression: CompressionFormat | undefined
51+
}
3552

3653

3754
/** Called (indirectly) by each embedded file. */
@@ -46,7 +63,7 @@ export class File {
4663
}
4764

4865
/** Returns the raw bytes of the embedded file. */
49-
async bytes(): Promise<Uint8Array> {
66+
async bytes(): Promise<Uint8Array<ArrayBuffer>> {
5067
let {bytes, compression} = this.#contents
5168

5269
// Decompress on first use:
@@ -109,7 +126,7 @@ export type CompressionFormat = ConstructorParameters<typeof DecompressionStream
109126
/** Shortcut for `new File(opts)` */
110127
export function F(opts: FileMeta): File { return new File(opts) }
111128

112-
async function decompress(data: Uint8Array, compression: CompressionFormat): Promise<Uint8Array> {
129+
async function decompress(data: Uint8Array, compression: CompressionFormat): Promise<Uint8Array<ArrayBuffer>> {
113130
let input = new Blob([data])
114131
let ds = new DecompressionStream(compression)
115132
let stream = input.stream().pipeThrough(ds)

src/helpers/hono.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,12 @@ export function serveStatic<E extends Env = Env>(
2828
options: Omit<ServeStaticOptions<E>, "root"> & { root: Embeds }
2929
): MiddlewareHandler {
3030
const { root, ...rest } = options;
31-
return function serveStatic(c, next) {
32-
const getContent = async (path: string) => {
33-
try {
34-
const file = await root.get(path);
35-
return file ? file.bytes() : null;
36-
} catch (e) {
37-
console.warn(`${e}`);
38-
return null;
39-
}
40-
};
41-
31+
const getContent = async (path: string): Promise<ArrayBuffer | null> => {
32+
const file = await root.get(path);
33+
if (!file) { return null }
34+
return (await file.bytes()).buffer
35+
};
36+
return function serveStaticImpl(c, next) {
4237
// note: baseServeStatic gives us mime type headers automatically. Nice.
4338
return baseServeStatic({
4439
...rest,

0 commit comments

Comments
 (0)