-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhttp.ts
More file actions
159 lines (142 loc) · 4.09 KB
/
http.ts
File metadata and controls
159 lines (142 loc) · 4.09 KB
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
import axios from 'axios'
import { PassThrough } from 'stream'
import StreamTree, { WritableStreamTree } from 'tree-stream'
import {
AppendOptions,
CreateOptions,
EnsureDirectoryOptions,
FileStatus,
FileSystem,
GetFileStatusOptions,
OpenReadableFileOptions,
OpenWritableFileOptions,
ReadDirectoryOptions,
RemoveDirectoryOptions,
ReplaceFileOptions,
} from './fs'
import { openNullReadable } from './stream'
import { zlib } from './util'
/**
* HTTP [[FileSystem]] implemented with `axios`.
*/
export class HTTPFileSystem extends FileSystem {
constructor(public options?: Record<string, any>) {
super()
}
/** @inheritDoc */
async readDirectory(_urlText: string, _options?: ReadDirectoryOptions) {
return []
}
/** @inheritDoc */
async readDirectoryStream(_urlText: string, _options?: ReadDirectoryOptions) {
return openNullReadable()
}
/** @inheritDoc */
async ensureDirectory(_urlText: string, _options?: EnsureDirectoryOptions) {
return true
}
/** @inheritDoc */
async removeDirectory(_urlText: string, _options?: RemoveDirectoryOptions) {
return true
}
/** @inheritDoc */
async fileExists(url: string) {
try {
await axios({ ...this.options, url, method: 'head' })
} catch {
return false
}
return true
}
/** @inheritDoc */
async getFileStatus(url: string, _options?: GetFileStatusOptions) {
const res = await axios({ ...this.options, url, method: 'head' })
return {
url,
modified: new Date(res.headers['last-modified']),
inode: 0,
size: res.headers['content-length'] ?? 0,
version: 0,
extra: { headers: res.headers },
}
}
/** @inheritDoc */
async openReadableFile(url: string, options?: OpenReadableFileOptions) {
const headers = { 'Accept-Encoding': 'gzip', ...this.options?.headers }
if (options?.byteLength != null) {
const offset = options.byteOffset || 0
headers.range = `bytes=${offset}-${offset + options.byteLength - 1}`
}
const res = await axios({
...this.options,
url,
method: 'get',
headers,
...options?.extra,
responseType: 'stream',
})
if (options?.extra && options.extraOutput) options.extra.headers = res.headers
let stream = StreamTree.readable(res.data)
if (res.headers['content-type'] === 'application/gzip' || url.endsWith('.gz')) {
stream = stream.pipe(zlib.createGunzip())
}
return stream
}
/** @inheritDoc */
async openWritableFile(url: string, options?: OpenWritableFileOptions) {
const passThrough = new PassThrough()
const headers = { ...this.options?.headers }
if (options?.contentType) headers['Content-Type'] = options.contentType
axios({ ...this.options, url, method: 'post', data: passThrough })
let stream = StreamTree.writable(passThrough)
if (url.endsWith('.gz')) stream = stream.pipeFrom(zlib.createGzip())
return stream
}
/** @inheritDoc */
async createFile(
_urlText: string,
_createCallback?: (stream: WritableStreamTree) => Promise<boolean>,
_options?: CreateOptions
) {
return false
}
/** @inheritDoc */
async removeFile(url: string) {
try {
await axios({ ...this.options, url, method: 'delete' })
} catch {
return false
}
return true
}
/** @inheritDoc */
async queueRemoveFile(_urlText: string) {
return false
}
/** @inheritDoc */
async copyFile(_sourceUrlText: string, _destUrlText: string) {
return false
}
/** @inheritDoc */
async moveFile(_sourceUrlText: string, _destUrlText: string) {
return false
}
/** @inheritDoc */
async replaceFile(
_urlText: string,
_writeCallback: (stream: WritableStreamTree) => Promise<boolean>,
_options?: ReplaceFileOptions
): Promise<boolean> {
return false
}
/** @inheritDoc */
async appendToFile(
_urlText: string,
_writeCallback: (stream: WritableStreamTree) => Promise<boolean>,
_createCallback?: (stream: WritableStreamTree) => Promise<boolean>,
_createOptions?: CreateOptions,
_appendOptions?: AppendOptions
): Promise<FileStatus | null> {
return null
}
}