Skip to content

Commit 44e6014

Browse files
author
Vladimir Enchev
committed
initial commit
1 parent 782c390 commit 44e6014

File tree

7 files changed

+1183
-0
lines changed

7 files changed

+1183
-0
lines changed

CrossPlatformModules.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,10 @@
288288
</TypeScriptCompile>
289289
<TypeScriptCompile Include="es-collections.d.ts" />
290290
<TypeScriptCompile Include="es6-promise.d.ts" />
291+
<TypeScriptCompile Include="fetch\body.ts" />
292+
<TypeScriptCompile Include="fetch\fetch.ts" />
293+
<TypeScriptCompile Include="fetch\headers.ts" />
294+
<TypeScriptCompile Include="fetch\webidl.d.ts" />
291295
<TypeScriptCompile Include="file-system\file-name-resolver.d.ts" />
292296
<TypeScriptCompile Include="file-system\file-name-resolver.ts">
293297
<DependentUpon>file-name-resolver.d.ts</DependentUpon>
@@ -1670,6 +1674,8 @@
16701674
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
16711675
</Content>
16721676
<Content Include="apps\action-bar-demo\package.json" />
1677+
<Content Include="fetch\package.json" />
1678+
<Content Include="fetch\README.md" />
16731679
<None Include="js-libs\esprima\LICENSE.BSD" />
16741680
<Content Include="source-control.md" />
16751681
<Content Include="ui\segmented-bar\package.json">

fetch/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Isomorphic Fetch Implementation
2+
3+
## status
4+
5+
WIP
6+
7+
## motivation
8+
9+
implementation of [fetch API](https://fetch.spec.whatwg.org/) in pure javascript.
10+
polyfill for browser, and implemnt for node.js.
11+
make network http access isomorphic.
12+
13+
14+
## License
15+
16+
The MIT License (MIT)
17+
Copyright (c) 2013 Jxck

fetch/body.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// https://fetch.spec.whatwg.org/#json
2+
type object = JSON;
3+
4+
type body = Object; // byte stream
5+
6+
// https://fetch.spec.whatwg.org/#bodyinit
7+
type BodyInit = Blob | BufferSource | FormData | URLSearchParams | USVString
8+
9+
// https://fetch.spec.whatwg.org/#body
10+
interface IBody {
11+
// readonly property
12+
bodyUsed: boolean;
13+
14+
// method
15+
arrayBuffer(): Promise<ArrayBuffer>;
16+
blob(): Promise<Blob>;
17+
formData(): Promise<FormData>;
18+
json(): Promise<JSON>;
19+
text(): Promise<USVString>;
20+
};
21+
22+
// https://fetch.spec.whatwg.org/#body
23+
class Body implements IBody {
24+
private _bodyUsed: boolean;
25+
private _body: body;
26+
private _usedFlag: boolean;
27+
private _mimeType: string;
28+
29+
get bodyUsed(): boolean {
30+
return this._bodyUsed;
31+
}
32+
33+
get body(): body {
34+
return this._body;
35+
}
36+
37+
get usedFlag(): boolean {
38+
return this._usedFlag;
39+
}
40+
41+
get mimeType(): string {
42+
return this._mimeType;
43+
}
44+
45+
consumeBody(_type: string): any {
46+
// step 1
47+
var p = new Promise((resolve, reject) => {
48+
// step 2
49+
if (this._bodyUsed == true) {
50+
return reject(new TypeError("body was already used"));
51+
}
52+
53+
// step 3
54+
this._bodyUsed = true;
55+
56+
// step 3-1
57+
var stream = this._body;
58+
59+
// step 3-2
60+
if (stream == null) {
61+
stream = [];
62+
}
63+
64+
// step 3-3
65+
// TODO: Let bytes be the result of reading from stream until it returns end-of-stream.
66+
var bytes;
67+
68+
// step 4
69+
// TODO: implement me
70+
switch(_type) {
71+
case "ArrayBuffer":
72+
case "Blob":
73+
case "FormData":
74+
case "JSON":
75+
case "text":
76+
}
77+
});
78+
return p;
79+
}
80+
81+
arrayBuffer(): Promise<ArrayBuffer> {
82+
return this.consumeBody("ArrayBuffer");
83+
}
84+
85+
blob(): Promise<Blob> {
86+
return this.consumeBody("Blob");
87+
}
88+
89+
formData(): Promise<FormData> {
90+
return this.consumeBody("FormData");
91+
}
92+
93+
json(): Promise<JSON> {
94+
return this.consumeBody("JSON");
95+
}
96+
97+
text(): Promise<USVString> {
98+
return this.consumeBody("text");
99+
}
100+
}

0 commit comments

Comments
 (0)