-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_util.ts
205 lines (176 loc) · 5.11 KB
/
_util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import {
assert,
base64,
crypto,
ERR_STREAM_ALREADY_FINISHED,
ERR_STREAM_DESTROYED,
is,
Stats,
statSync,
} from "./deps.ts";
import type {
Context,
DigestAlgorithm,
Entity,
FileInfo,
State,
VirtualFileInfo,
VirtualFsFile,
} from "./mod.d.ts";
export { assert, is };
export const ETAG_EMPTY = `"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk="`;
export const ETAG_LENGTH = 27, ETAG_RADIX = 16, ETAG_MTIME_LENGTH = 12;
import type { DecodedEntityTag, DecodedStatTag } from "./mod.d.ts";
export function decodeStatTag(value: string): Partial<DecodedStatTag> {
const [size, time] = value.split("-").map((n: string | number) => (
n = parseInt(n as string, ETAG_RADIX) as number, is.nan(n) ? 0 : n
));
const mtime = new Date(time);
return { size, mtime, hash: undefined };
}
export function decodeEntityTag(value: string): Partial<DecodedEntityTag> {
const [length, hash] = value.split("-");
let size = parseInt(length, ETAG_RADIX);
if (is.nan(size)) size = 0;
return { size, hash, mtime: undefined };
}
/** @internal */
export function digestSync(
entity: Entity,
DigestAlgorithm: DigestAlgorithm = "SHA-1",
): string {
/** @internal */
function validateEntity(entity: Entity): Uint8Array {
if (is.uint8Array(entity) || is.arrayBuffer(entity)) {
return entity as Uint8Array;
} else if (is.string(entity)) {
// encode as (utf-8) Uint8Array
return new TextEncoder().encode(entity);
}
assert(
!is.nullOrUndefined(entity),
"Entity cannot be null or undefined!",
entity,
);
return entity as any;
}
const toHex = (v: number) => v.toString(16).padStart(2, "0");
const buffer = validateEntity(entity);
const hash = crypto.subtle.digestSync(DigestAlgorithm, buffer);
return base64.encode(Array.from(new Uint8Array(hash), toHex).join(""));
}
export function digest(
entity: Entity,
DigestAlgorithm: DigestAlgorithm = "SHA-1",
): Promise<string> {
return Promise.resolve(digestSync(entity, DigestAlgorithm));
}
/** @internal */
export function isFileInfo(obj: any): obj is FileInfo | Stats {
if (is.function(Stats) && obj instanceof Stats) {
return true;
}
// 🦆 🦆 🦆
// deno-fmt-ignore
return obj
&& is.nonEmptyObject(obj)
&& "ino" in obj
&& "size" in obj
&& "mtime" in obj
&& "ctime" in obj
&& is.any(is.number, obj.ino, obj.size)
&& is.any([is.date, is.string, is.number], obj.mtime)
&& is.number((obj as any).size);
}
/** @internal */
export function fstatSync<
T extends VirtualFsFile["rid"] | VirtualFsFile,
>(file: T): VirtualFileInfo;
/** @internal */
export function fstatSync(file: Deno.FsFile): FileInfo;
/** @internal */
export function fstatSync(file: string | URL | Stats): Partial<FileInfo>;
/** @internal */
export function fstatSync(file: any): any | undefined {
// scaffolding for virtual fs support in the future
if ("body" in file && "meta" in file) {
return file.meta;
}
if (is.function(Deno.fstatSync)) {
if (is.directInstanceOf(file, Deno.FsFile) || is.number(file.rid)) {
return Deno.fstatSync(file.rid);
}
}
if (is.urlInstance(file) || is.string(file)) {
return statSync(file);
}
if (file instanceof Stats) {
return { ...file };
}
return undefined;
}
/** @internal */
export function fstat(
file:
| (string | URL | Stats)
| Deno.FsFile
| (number | VirtualFsFile)
| FileInfo,
): Promise<ReturnType<typeof fstatSync>> {
try {
return Promise.resolve(fstatSync(file as any));
} catch (err) {
return Promise.reject(err);
}
}
/**
* For a given Context, try to determine the response body entity that an ETag
* can be calculated from.
*/
/** @internal */
export async function getEntity<C extends Context<S>, S = State>(
ctx: C,
): Promise<Entity>;
/** @internal */
export async function getEntity<T extends Response | BodyInit>(
res: T,
): Promise<Entity>;
/** @internal */
export async function getEntity(ctx: any): Promise<any> {
try {
const getContextBody = (ctx: any) => {
if (("response" in ctx) && is.response(ctx.response)) {
if ((ctx as Context).response.bodyUsed) {
throw new ERR_STREAM_ALREADY_FINISHED(
"getEntity(ctx.response.arrayBuffer)",
);
}
return (ctx as Context).response.arrayBuffer()!;
} else if (is.response(ctx)) {
if ((ctx as Response).bodyUsed) {
throw new ERR_STREAM_ALREADY_FINISHED("getEntity(ctx.arrayBuffer)");
}
return (ctx as Response).arrayBuffer()!;
} else if (is.directInstanceOf(ctx, Deno.FsFile) || isFileInfo(ctx)) {
return fstatSync(ctx as any);
}
};
const body = await getContextBody(ctx);
if (is.uint8Array(body) || is.arrayBuffer(body)) {
return body;
} else if (!is.nullOrUndefined(body) && is.nonEmptyObject(body)) {
try {
return JSON.stringify(body);
} catch (err) {
console.error(err);
throw new ERR_STREAM_DESTROYED("getEntity(ctx)");
}
}
return undefined;
} catch (cause) {
throw new TypeError(
`Unable to determine entity body from given context!`,
{ cause },
);
}
}