Skip to content

Commit 346cf7c

Browse files
committed
chore: header construcor and get method but not good code
1 parent e85a8db commit 346cf7c

File tree

7 files changed

+158
-34
lines changed

7 files changed

+158
-34
lines changed

examples/atob.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ const validBase64 = "YQ==";
22
console.log("validBase64: ", atob(validBase64)); // "a"
33

44
const validBase64Multiple = "SGVsbG8sIEFuZHJvbWVkYSE=";
5-
console.log("validBase64Multiple: ", atob(validBase64Multiple)); // "Hello, Andromeda!"
5+
console.log("validBase64Multiple: ", atob(validBase64Multiple)); // "Hello, Andromeda!"

examples/headers.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
const myHeaders = new Headers({
2-
accept: "application/json",
3-
});
1+
const httpHeaders = {
2+
"Content-Type": "image/jpeg",
3+
"X-My-Custom-Header": "Zeke are cool",
4+
};
5+
const myHeaders = new Headers(httpHeaders);
6+
7+
console.log("myHeaders", myHeaders.get("Content-Type")); // image/jpeg
8+
console.log("myHeaders", myHeaders.get("X-My-Custom-Header")); // Zeke are cool
49

510
// // Append a header to the headers object.
611
// myHeaders.append("user-agent", "Deno Deploy");
@@ -15,4 +20,3 @@ const myHeaders = new Headers({
1520
// method: "POST",
1621
// headers: myHeaders,
1722
// });
18-

examples/url.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,18 @@
33
const baseUrl = "https://developer.mozilla.org";
44

55
const A = new URL("/", baseUrl); // 'https://developer.mozilla.org/'
6-
console.log(A)
6+
console.log(A);
77

88
const B = new URL(baseUrl); // 'https://developer.mozilla.org/'
9-
console.log(B)
9+
console.log(B);
1010

1111
console.log(new URL("en-US/docs", B)); // 'https://developer.mozilla.org/en-US/docs'
1212

13-
1413
const D = new URL("/en-US/docs", B); // 'https://developer.mozilla.org/en-US/docs'
15-
console.log(D)
14+
console.log(D);
1615

1716
console.log(new URL("/en-US/docs", D)); // 'https://developer.mozilla.org/en-US/docs'
1817

1918
console.log(new URL("/en-US/docs", A)); // 'https://developer.mozilla.org/en-US/docs'
2019

21-
new URL("/en-US/docs", "https://developer.mozilla.org/fr-FR/toto"); // 'https://developer.mozilla.org/en-US/docs'
20+
new URL("/en-US/docs", "https://developer.mozilla.org/fr-FR/toto"); // 'https://developer.mozilla.org/en-US/docs'

namespace/mod.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -298,20 +298,20 @@ function alert(message: string) {
298298
}
299299

300300
/**
301-
* Takes the input data, in the form of a Unicode string containing only characters in the range U+0000 to U+00FF,
302-
* each representing a binary byte with values 0x00 to 0xFF respectively, and converts it to its base64 representation,
301+
* Takes the input data, in the form of a Unicode string containing only characters in the range U+0000 to U+00FF,
302+
* each representing a binary byte with values 0x00 to 0xFF respectively, and converts it to its base64 representation,
303303
* which it returns.
304304
*/
305305
function btoa(input: string): string {
306306
return internal_btoa(input);
307307
}
308308

309309
/**
310-
* Takes the input data, in the form of a Unicode string containing base64-encoded binary data,
311-
* decodes it, and returns a string consisting of characters in the range U+0000 to U+00FF,
312-
* each representing a binary byte with values 0x00 to 0xFF respectively,
310+
* Takes the input data, in the form of a Unicode string containing base64-encoded binary data,
311+
* decodes it, and returns a string consisting of characters in the range U+0000 to U+00FF,
312+
* each representing a binary byte with values 0x00 to 0xFF respectively,
313313
* corresponding to that binary data.
314314
*/
315315
function atob(input: string): string {
316316
return internal_atob(input);
317-
}
317+
}
Lines changed: 124 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,128 @@
11
class Headers {
22
constructor(init = undefined) {
3-
console.log('init', init)
3+
// TODO: webidl
4+
// this.brand = brand;
45
// @ts-ignore - this is a hack to make the URL object work
6+
this.guard = "none";
7+
// @ts-ignore - this is a hack to make the URL object work
8+
this.headerList = [];
9+
fillHeaders(this, init);
10+
}
11+
12+
get(name) {
13+
// @ts-ignore - this is a hack to make the URL object work
14+
return getHeader(this.headerList, name);
15+
}
16+
}
17+
18+
function fillHeaders(headers, object) {
19+
if (Array.isArray(object)) {
20+
for (let i = 0; i < object.length; ++i) {
21+
const header = object[i];
22+
if (header.length !== 2) {
23+
throw new TypeError(
24+
`Invalid header: length must be 2, but is ${header.length}`,
25+
);
26+
}
27+
appendHeader(headers, header[0], header[1]);
28+
}
29+
} else {
30+
for (const key in object) {
31+
if (!Object.hasOwn(object, key)) {
32+
continue;
33+
}
34+
appendHeader(headers, key, object[key]);
35+
}
36+
}
37+
}
38+
39+
/**
40+
* @param {string} s
41+
* @returns {string}
42+
*/
43+
function byteLowerCase(s) {
44+
// NOTE: correct since all callers convert to ByteString first
45+
// TODO(@AaronO): maybe prefer a ByteString_Lower webidl converter
46+
return s;
47+
}
48+
49+
// https://fetch.spec.whatwg.org/#concept-headers-append
50+
function appendHeader(headers, name, value) {
51+
// 1.
52+
value = normalizeHeaderValue(value);
53+
54+
// 2.
55+
// if (!checkHeaderNameForHttpTokenCodePoint(name)) {
56+
// throw new TypeError(`Invalid header name: "${name}"`);
57+
// }
58+
// if (!checkForInvalidValueChars(value)) {
59+
// throw new TypeError(`Invalid header value: "${value}"`);
60+
// }
61+
62+
// 3
63+
if (headers.guard == "immutable") {
64+
throw new TypeError("Cannot change header: headers are immutable");
65+
}
66+
67+
// 7.
68+
const list = headers.headerList;
69+
const lowercaseName = byteLowerCase(name);
70+
for (let i = 0; i < list.length; i++) {
71+
if (byteLowerCase(list[i][0]) === lowercaseName) {
72+
name = list[i][0];
73+
break;
74+
}
75+
}
76+
list.push([name, value]);
77+
}
78+
79+
/**
80+
* @param {string} potentialValue
81+
* @returns {string}
82+
*/
83+
function normalizeHeaderValue(potentialValue) {
84+
return httpTrim(potentialValue);
85+
}
86+
87+
// TODO: move to web
88+
function isHttpWhitespace(char) {
89+
switch (char) {
90+
case "\u0009":
91+
case "\u000A":
92+
case "\u000D":
93+
case "\u0020":
94+
return true;
95+
default:
96+
return false;
97+
}
98+
}
99+
100+
// const HTTP_BETWEEN_WHITESPACE = new SafeRegExp(
101+
// `^[${HTTP_WHITESPACE_MATCHER}]*(.*?)[${HTTP_WHITESPACE_MATCHER}]*$`,
102+
// );
103+
// TODO: move to web
104+
function httpTrim(s) {
105+
if (!isHttpWhitespace(s[0]) && !isHttpWhitespace(s[s.length - 1])) {
106+
return s;
107+
}
108+
// return String.prototype.match(s, HTTP_BETWEEN_WHITESPACE)?.[1] ?? "";
109+
// TODO: implement to nova RegExp
110+
return s;
111+
}
112+
113+
// https://fetch.spec.whatwg.org/#concept-header-list-get
114+
function getHeader(list, name) {
115+
const lowercaseName = byteLowerCase(name);
116+
const entries = [];
117+
for (let i = 0; i < list.length; i++) {
118+
if (byteLowerCase(list[i][0]) === lowercaseName) {
119+
entries.push(list[i][1]);
120+
}
121+
}
122+
123+
if (entries.length === 0) {
124+
return null;
125+
} else {
126+
return entries.join(entries, "\x2C\x20");
5127
}
6-
}
128+
}

runtime/src/ext/url/mod.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
// deno-lint-ignore-file no-unused-vars
22
class URL {
3-
constructor(url: string, base?: string) {
4-
// @ts-ignore - this is a hack to make the URL object work
5-
this.url = url;
6-
// @ts-ignore - this is a hack to make the Base URL object work
7-
this.base = base;
8-
// @ts-ignore - this is a hack to make the URL object work
9-
this.serialized = base
10-
? internal_url_parse(url, base)
11-
: internal_url_parse_no_base(url);
12-
}
3+
constructor(url: string, base?: string) {
4+
// @ts-ignore - this is a hack to make the URL object work
5+
this.url = url;
6+
// @ts-ignore - this is a hack to make the Base URL object work
7+
this.base = base;
8+
// @ts-ignore - this is a hack to make the URL object work
9+
this.serialized = base
10+
? internal_url_parse(url, base)
11+
: internal_url_parse_no_base(url);
12+
}
1313

14-
toString() {
15-
// @ts-ignore - this is a hack to make the URL object work
16-
return this.serialized;
17-
}
14+
toString() {
15+
// @ts-ignore - this is a hack to make the URL object work
16+
return this.serialized;
17+
}
1818
}

types/internals.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ declare function internal_url_parse_no_base(url: string): string;
9898
*/
9999
declare function internal_btoa(input: string): string;
100100
/**
101-
*
102101
* The `internal_atob` function decodes a string in base64.
103102
*/
104-
declare function internal_atob(input: string): string;
103+
declare function internal_atob(input: string): string;

0 commit comments

Comments
 (0)