This repository has been archived by the owner on Oct 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
socket-fetch.html
151 lines (129 loc) · 5.72 KB
/
socket-fetch.html
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
<link rel="import" href="../cookie-parser/cookie-parser.html">
<script src="../zlib/bin/zlib_and_gzip.min.js"></script>
<!-- <script src="../uri.js/src/URI.min.js"></script> -->
<!-- <script src="../cookie-parser/cookie-parser.js"></script> -->
<script src="../crypto-js/crypto-js.js"></script>
<script src="auth.js"></script>
<script src="auth.basic.js"></script>
<script src="auth.digest.js"></script>
<script src="auth.ntlm.js"></script>
<script src="app.event.js"></script>
<script src="app.request.js"></script>
<script src="app.response.js"></script>
<script src="app.fetch.js"></script>
<script src="http.parser.js"></script>
<script src="import-location.js"></script>
<!--
# Socket fetch
The HTTP client transport based on [chrome.sockets.tcp] API.
## Getting started
Bower the library
```
bower install --save advanced-rest-client/socket-fetch
```
And import it into your project using web components.
```html
<link rel="import" href="bower-components/socket-fetch/socket-fetch.html">
```
Now you can use following classes:
* ArcEventTarget
* ArcEventSource
* ArcRequest
* ArcResponse
* SocketFetch
* HttpParser
The Arc prefix comes from [Advanced Rest Client] project.
## usage
Basically you need a resource you want to fetch:
```
var url = 'http://www.google.com';
```
You can set up some headers if you wish.
This implementation will not set any default headers so if you don't do it the request will not contain them.
```
var headers = {
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding':'gzip, deflate, sdch',
'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2652.0 Safari/537.36'
};
```
By default the implementation will use `GET` method and will follow redirects.
```
var init = {
'method': 'GET',
'headers': new Headers(headers),
'redirect': 'follow'
};
```
And finally you just need to fetch the resource:
```
var connection = new SocketFetch(url, opts);
connection.fetch()
.then((response) => {
if (response.ok) {
response.text().then((result) => {
console.log('Fetch result', result);
})
.catch((cause) => {
console.error('Error during fetch', cause);
});
}
})
.catch((cause) => {
console.error('Error during fetch', cause);
});
```
If you want to send data use `body` property in `init` object:
```
var payload = JSON.stringify({
'test': 'request'
});
var headers = {
'Content-Type': 'application/json',
'Content-Length': payload.length
};
var init = {
'method': 'POST',
'headers': new Headers(headers),
'body': payload
};
```
You can send body of type of Blob, BufferSource, FormData, URLSearchParams, or String.
## ArcRequest
The ArcRequest class is similar to JavaScript's Request class. You can initialize it the same way as regular Request class.
### Initialization
Initialize as Request object. The first parameter can be `String`, `Request` or `ArcRequest` object.
Second argument may have following options:
| Name | Type | Description | Default |
| --- | --- | --- | --- |
| method | String | The request method, e.g., GET, POST. | `GET` |
| headers | Headers or Object | Any headers you want to add to your request, contained within a Headers object or an object literal with key value pairs. Note that the library will not generate default headers. If none headers are passed it will not send headers in the request. | _none_ |
| body | Blob, BufferSource, FormData, URLSearchParams, or String | Any body that you want to add to your request. Note that the body will be removed if the request's method is either `GET` or `HEAD` | _none_ |
| redirect | String | The redirect mode to use: follow or error. If follow is set the result will contain redairect information. | `follow` |
| timeout | Number | A number of milliseconds for connection timeout. Note that the timer run at the moment when connection was established. | _none_ |
## Bonus in ArcResponse
Regular Response object will not contain all headers in the response. ArcResponse class however will return all received headers from the server - even the prohibited ones. Other properties and methods are inherited from the Response object.
Additionally the ArcResponse will contain two custom fields:
* redirects {Set<ArcResponse>} - A list of responses that lead to redirection
* stats {Set<Object>} - Some stats about the request and response. It is the same as `timings` object in HAR 1.2 specification.
## Import scripts (web workers)
This library uses web workers. Sometimes it is necessary to change import path of the library.
By default the script will look in the '/' path for web workers. However bower or combined scripts
may have been placed in different location so `SocketFetchOptions.importUrl` should be set to
the real path to locate a file.
The decompress worker uses Zlib and Gzip library. It has hardcoded script import to '../zlib/bin/zlib_and_gzip.min.js'. It means that the script will be included from this path relatively to the web worker file location.
In most cases the element will be placed in `bower_components` or `components` folder with other bower elements so it resolve paths correctly. However, if you concat JS files or move the element somewhere else it may be the problem.
In this case you need to move decompression libraries accordingly.
```
/path/to/file/%s
```
Keep the %s. The script will replace it with corresponding file name.
When importing the library as a web component it will guess the correct location of the file and will set up it for you. However if you have difficulties with paths feel free to set up paths on your own.
[chrome.sockets.tcp]: https://developer.chrome.com/apps/sockets_tcp
[Advanced Rest Client]: https://github.com/jarrodek/ChromeRestClient
-->
<script>
Polymer({
is: 'socket-fetch'
});
</script>