diff options
Diffstat (limited to 'shared/core-utils')
-rw-r--r-- | shared/core-utils/common/date.ts (renamed from shared/core-utils/miscs/date.ts) | 47 | ||||
-rw-r--r-- | shared/core-utils/common/index.ts | 6 | ||||
-rw-r--r-- | shared/core-utils/common/miscs.ts (renamed from shared/core-utils/miscs/miscs.ts) | 10 | ||||
-rw-r--r-- | shared/core-utils/common/promises.ts | 12 | ||||
-rw-r--r-- | shared/core-utils/common/regexp.ts | 5 | ||||
-rw-r--r-- | shared/core-utils/common/types.ts (renamed from shared/core-utils/miscs/types.ts) | 0 | ||||
-rw-r--r-- | shared/core-utils/common/url.ts | 130 | ||||
-rw-r--r-- | shared/core-utils/index.ts | 3 | ||||
-rw-r--r-- | shared/core-utils/logs/index.ts | 1 | ||||
-rw-r--r-- | shared/core-utils/logs/logs.ts | 25 | ||||
-rw-r--r-- | shared/core-utils/miscs/http-error-codes.ts | 364 | ||||
-rw-r--r-- | shared/core-utils/miscs/http-methods.ts | 21 | ||||
-rw-r--r-- | shared/core-utils/miscs/index.ts | 5 | ||||
-rw-r--r-- | shared/core-utils/plugins/hooks.ts | 2 |
14 files changed, 201 insertions, 430 deletions
diff --git a/shared/core-utils/miscs/date.ts b/shared/core-utils/common/date.ts index 4f92f758f..3e4a3c08c 100644 --- a/shared/core-utils/miscs/date.ts +++ b/shared/core-utils/common/date.ts | |||
@@ -43,6 +43,49 @@ function isLastWeek (d: Date) { | |||
43 | return getDaysDifferences(now, d) <= 7 | 43 | return getDaysDifferences(now, d) <= 7 |
44 | } | 44 | } |
45 | 45 | ||
46 | function timeToInt (time: number | string) { | ||
47 | if (!time) return 0 | ||
48 | if (typeof time === 'number') return time | ||
49 | |||
50 | const reg = /^((\d+)[h:])?((\d+)[m:])?((\d+)s?)?$/ | ||
51 | const matches = time.match(reg) | ||
52 | |||
53 | if (!matches) return 0 | ||
54 | |||
55 | const hours = parseInt(matches[2] || '0', 10) | ||
56 | const minutes = parseInt(matches[4] || '0', 10) | ||
57 | const seconds = parseInt(matches[6] || '0', 10) | ||
58 | |||
59 | return hours * 3600 + minutes * 60 + seconds | ||
60 | } | ||
61 | |||
62 | function secondsToTime (seconds: number, full = false, symbol?: string) { | ||
63 | let time = '' | ||
64 | |||
65 | if (seconds === 0 && !full) return '0s' | ||
66 | |||
67 | const hourSymbol = (symbol || 'h') | ||
68 | const minuteSymbol = (symbol || 'm') | ||
69 | const secondsSymbol = full ? '' : 's' | ||
70 | |||
71 | const hours = Math.floor(seconds / 3600) | ||
72 | if (hours >= 1) time = hours + hourSymbol | ||
73 | else if (full) time = '0' + hourSymbol | ||
74 | |||
75 | seconds %= 3600 | ||
76 | const minutes = Math.floor(seconds / 60) | ||
77 | if (minutes >= 1 && minutes < 10 && full) time += '0' + minutes + minuteSymbol | ||
78 | else if (minutes >= 1) time += minutes + minuteSymbol | ||
79 | else if (full) time += '00' + minuteSymbol | ||
80 | |||
81 | seconds %= 60 | ||
82 | if (seconds >= 1 && seconds < 10 && full) time += '0' + seconds + secondsSymbol | ||
83 | else if (seconds >= 1) time += seconds + secondsSymbol | ||
84 | else if (full) time += '00' | ||
85 | |||
86 | return time | ||
87 | } | ||
88 | |||
46 | // --------------------------------------------------------------------------- | 89 | // --------------------------------------------------------------------------- |
47 | 90 | ||
48 | export { | 91 | export { |
@@ -51,7 +94,9 @@ export { | |||
51 | isThisMonth, | 94 | isThisMonth, |
52 | isToday, | 95 | isToday, |
53 | isLastMonth, | 96 | isLastMonth, |
54 | isLastWeek | 97 | isLastWeek, |
98 | timeToInt, | ||
99 | secondsToTime | ||
55 | } | 100 | } |
56 | 101 | ||
57 | // --------------------------------------------------------------------------- | 102 | // --------------------------------------------------------------------------- |
diff --git a/shared/core-utils/common/index.ts b/shared/core-utils/common/index.ts new file mode 100644 index 000000000..0908ff981 --- /dev/null +++ b/shared/core-utils/common/index.ts | |||
@@ -0,0 +1,6 @@ | |||
1 | export * from './date' | ||
2 | export * from './miscs' | ||
3 | export * from './regexp' | ||
4 | export * from './promises' | ||
5 | export * from './types' | ||
6 | export * from './url' | ||
diff --git a/shared/core-utils/miscs/miscs.ts b/shared/core-utils/common/miscs.ts index 4780ca922..bc65dc338 100644 --- a/shared/core-utils/miscs/miscs.ts +++ b/shared/core-utils/common/miscs.ts | |||
@@ -20,14 +20,6 @@ function compareSemVer (a: string, b: string) { | |||
20 | return segmentsA.length - segmentsB.length | 20 | return segmentsA.length - segmentsB.length |
21 | } | 21 | } |
22 | 22 | ||
23 | function isPromise (value: any) { | ||
24 | return value && typeof value.then === 'function' | ||
25 | } | ||
26 | |||
27 | function isCatchable (value: any) { | ||
28 | return value && typeof value.catch === 'function' | ||
29 | } | ||
30 | |||
31 | function sortObjectComparator (key: string, order: 'asc' | 'desc') { | 23 | function sortObjectComparator (key: string, order: 'asc' | 'desc') { |
32 | return (a: any, b: any) => { | 24 | return (a: any, b: any) => { |
33 | if (a[key] < b[key]) { | 25 | if (a[key] < b[key]) { |
@@ -45,7 +37,5 @@ function sortObjectComparator (key: string, order: 'asc' | 'desc') { | |||
45 | export { | 37 | export { |
46 | randomInt, | 38 | randomInt, |
47 | compareSemVer, | 39 | compareSemVer, |
48 | isPromise, | ||
49 | isCatchable, | ||
50 | sortObjectComparator | 40 | sortObjectComparator |
51 | } | 41 | } |
diff --git a/shared/core-utils/common/promises.ts b/shared/core-utils/common/promises.ts new file mode 100644 index 000000000..7ef9d60b6 --- /dev/null +++ b/shared/core-utils/common/promises.ts | |||
@@ -0,0 +1,12 @@ | |||
1 | function isPromise (value: any) { | ||
2 | return value && typeof value.then === 'function' | ||
3 | } | ||
4 | |||
5 | function isCatchable (value: any) { | ||
6 | return value && typeof value.catch === 'function' | ||
7 | } | ||
8 | |||
9 | export { | ||
10 | isPromise, | ||
11 | isCatchable | ||
12 | } | ||
diff --git a/shared/core-utils/common/regexp.ts b/shared/core-utils/common/regexp.ts new file mode 100644 index 000000000..59eb87eb6 --- /dev/null +++ b/shared/core-utils/common/regexp.ts | |||
@@ -0,0 +1,5 @@ | |||
1 | export const uuidRegex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}' | ||
2 | |||
3 | export function removeFragmentedMP4Ext (path: string) { | ||
4 | return path.replace(/-fragmented.mp4$/i, '') | ||
5 | } | ||
diff --git a/shared/core-utils/miscs/types.ts b/shared/core-utils/common/types.ts index bd2a97b98..bd2a97b98 100644 --- a/shared/core-utils/miscs/types.ts +++ b/shared/core-utils/common/types.ts | |||
diff --git a/shared/core-utils/common/url.ts b/shared/core-utils/common/url.ts new file mode 100644 index 000000000..52ed247c4 --- /dev/null +++ b/shared/core-utils/common/url.ts | |||
@@ -0,0 +1,130 @@ | |||
1 | import { Video, VideoPlaylist } from '../../models' | ||
2 | import { secondsToTime } from './date' | ||
3 | |||
4 | function buildPlaylistLink (playlist: Pick<VideoPlaylist, 'shortUUID'>, base?: string) { | ||
5 | return (base ?? window.location.origin) + buildPlaylistWatchPath(playlist) | ||
6 | } | ||
7 | |||
8 | function buildPlaylistWatchPath (playlist: Pick<VideoPlaylist, 'shortUUID'>) { | ||
9 | return '/w/p/' + playlist.shortUUID | ||
10 | } | ||
11 | |||
12 | function buildVideoWatchPath (video: Pick<Video, 'shortUUID'>) { | ||
13 | return '/w/' + video.shortUUID | ||
14 | } | ||
15 | |||
16 | function buildVideoLink (video: Pick<Video, 'shortUUID'>, base?: string) { | ||
17 | return (base ?? window.location.origin) + buildVideoWatchPath(video) | ||
18 | } | ||
19 | |||
20 | function buildPlaylistEmbedPath (playlist: Pick<VideoPlaylist, 'uuid'>) { | ||
21 | return '/video-playlists/embed/' + playlist.uuid | ||
22 | } | ||
23 | |||
24 | function buildPlaylistEmbedLink (playlist: Pick<VideoPlaylist, 'uuid'>, base?: string) { | ||
25 | return (base ?? window.location.origin) + buildPlaylistEmbedPath(playlist) | ||
26 | } | ||
27 | |||
28 | function buildVideoEmbedPath (video: Pick<Video, 'uuid'>) { | ||
29 | return '/videos/embed/' + video.uuid | ||
30 | } | ||
31 | |||
32 | function buildVideoEmbedLink (video: Pick<Video, 'uuid'>, base?: string) { | ||
33 | return (base ?? window.location.origin) + buildVideoEmbedPath(video) | ||
34 | } | ||
35 | |||
36 | function decorateVideoLink (options: { | ||
37 | url: string | ||
38 | |||
39 | startTime?: number | ||
40 | stopTime?: number | ||
41 | |||
42 | subtitle?: string | ||
43 | |||
44 | loop?: boolean | ||
45 | autoplay?: boolean | ||
46 | muted?: boolean | ||
47 | |||
48 | // Embed options | ||
49 | title?: boolean | ||
50 | warningTitle?: boolean | ||
51 | controls?: boolean | ||
52 | peertubeLink?: boolean | ||
53 | }) { | ||
54 | const { url } = options | ||
55 | |||
56 | const params = generateParams(window.location.search) | ||
57 | |||
58 | if (options.startTime !== undefined && options.startTime !== null) { | ||
59 | const startTimeInt = Math.floor(options.startTime) | ||
60 | params.set('start', secondsToTime(startTimeInt)) | ||
61 | } | ||
62 | |||
63 | if (options.stopTime) { | ||
64 | const stopTimeInt = Math.floor(options.stopTime) | ||
65 | params.set('stop', secondsToTime(stopTimeInt)) | ||
66 | } | ||
67 | |||
68 | if (options.subtitle) params.set('subtitle', options.subtitle) | ||
69 | |||
70 | if (options.loop === true) params.set('loop', '1') | ||
71 | if (options.autoplay === true) params.set('autoplay', '1') | ||
72 | if (options.muted === true) params.set('muted', '1') | ||
73 | if (options.title === false) params.set('title', '0') | ||
74 | if (options.warningTitle === false) params.set('warningTitle', '0') | ||
75 | if (options.controls === false) params.set('controls', '0') | ||
76 | if (options.peertubeLink === false) params.set('peertubeLink', '0') | ||
77 | |||
78 | return buildUrl(url, params) | ||
79 | } | ||
80 | |||
81 | function decoratePlaylistLink (options: { | ||
82 | url: string | ||
83 | |||
84 | playlistPosition?: number | ||
85 | }) { | ||
86 | const { url } = options | ||
87 | |||
88 | const params = generateParams(window.location.search) | ||
89 | |||
90 | if (options.playlistPosition) params.set('playlistPosition', '' + options.playlistPosition) | ||
91 | |||
92 | return buildUrl(url, params) | ||
93 | } | ||
94 | |||
95 | // --------------------------------------------------------------------------- | ||
96 | |||
97 | export { | ||
98 | buildPlaylistLink, | ||
99 | buildVideoLink, | ||
100 | |||
101 | buildVideoWatchPath, | ||
102 | buildPlaylistWatchPath, | ||
103 | |||
104 | buildPlaylistEmbedPath, | ||
105 | buildVideoEmbedPath, | ||
106 | |||
107 | buildPlaylistEmbedLink, | ||
108 | buildVideoEmbedLink, | ||
109 | |||
110 | decorateVideoLink, | ||
111 | decoratePlaylistLink | ||
112 | } | ||
113 | |||
114 | function buildUrl (url: string, params: URLSearchParams) { | ||
115 | let hasParams = false | ||
116 | params.forEach(() => { hasParams = true }) | ||
117 | |||
118 | if (hasParams) return url + '?' + params.toString() | ||
119 | |||
120 | return url | ||
121 | } | ||
122 | |||
123 | function generateParams (url: string) { | ||
124 | const params = new URLSearchParams(window.location.search) | ||
125 | // Unused parameters in embed | ||
126 | params.delete('videoId') | ||
127 | params.delete('resume') | ||
128 | |||
129 | return params | ||
130 | } | ||
diff --git a/shared/core-utils/index.ts b/shared/core-utils/index.ts index 42d7cab1d..66d50ef93 100644 --- a/shared/core-utils/index.ts +++ b/shared/core-utils/index.ts | |||
@@ -1,7 +1,6 @@ | |||
1 | export * from './abuse' | 1 | export * from './abuse' |
2 | export * from './common' | ||
2 | export * from './i18n' | 3 | export * from './i18n' |
3 | export * from './logs' | ||
4 | export * from './miscs' | ||
5 | export * from './plugins' | 4 | export * from './plugins' |
6 | export * from './renderer' | 5 | export * from './renderer' |
7 | export * from './users' | 6 | export * from './users' |
diff --git a/shared/core-utils/logs/index.ts b/shared/core-utils/logs/index.ts deleted file mode 100644 index ceb5d7a7f..000000000 --- a/shared/core-utils/logs/index.ts +++ /dev/null | |||
@@ -1 +0,0 @@ | |||
1 | export * from './logs' | ||
diff --git a/shared/core-utils/logs/logs.ts b/shared/core-utils/logs/logs.ts deleted file mode 100644 index d0996cf55..000000000 --- a/shared/core-utils/logs/logs.ts +++ /dev/null | |||
@@ -1,25 +0,0 @@ | |||
1 | import { stat } from 'fs-extra' | ||
2 | |||
3 | async function mtimeSortFilesDesc (files: string[], basePath: string) { | ||
4 | const promises = [] | ||
5 | const out: { file: string, mtime: number }[] = [] | ||
6 | |||
7 | for (const file of files) { | ||
8 | const p = stat(basePath + '/' + file) | ||
9 | .then(stats => { | ||
10 | if (stats.isFile()) out.push({ file, mtime: stats.mtime.getTime() }) | ||
11 | }) | ||
12 | |||
13 | promises.push(p) | ||
14 | } | ||
15 | |||
16 | await Promise.all(promises) | ||
17 | |||
18 | out.sort((a, b) => b.mtime - a.mtime) | ||
19 | |||
20 | return out | ||
21 | } | ||
22 | |||
23 | export { | ||
24 | mtimeSortFilesDesc | ||
25 | } | ||
diff --git a/shared/core-utils/miscs/http-error-codes.ts b/shared/core-utils/miscs/http-error-codes.ts deleted file mode 100644 index b2fbdfc5a..000000000 --- a/shared/core-utils/miscs/http-error-codes.ts +++ /dev/null | |||
@@ -1,364 +0,0 @@ | |||
1 | /** | ||
2 | * Hypertext Transfer Protocol (HTTP) response status codes. | ||
3 | * @see {@link https://en.wikipedia.org/wiki/List_of_HTTP_status_codes} | ||
4 | * | ||
5 | * WebDAV and other codes useless with regards to PeerTube are not listed. | ||
6 | */ | ||
7 | export enum HttpStatusCode { | ||
8 | |||
9 | /** | ||
10 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.2.1 | ||
11 | * | ||
12 | * The server has received the request headers and the client should proceed to send the request body | ||
13 | * (in the case of a request for which a body needs to be sent; for example, a POST request). | ||
14 | * Sending a large request body to a server after a request has been rejected for inappropriate headers would be inefficient. | ||
15 | * To have a server check the request's headers, a client must send Expect: 100-continue as a header in its initial request | ||
16 | * and receive a 100 Continue status code in response before sending the body. The response 417 Expectation Failed indicates | ||
17 | * the request should not be continued. | ||
18 | */ | ||
19 | CONTINUE_100 = 100, | ||
20 | |||
21 | /** | ||
22 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.2.2 | ||
23 | * | ||
24 | * This code is sent in response to an Upgrade request header by the client, and indicates the protocol the server is switching too. | ||
25 | */ | ||
26 | SWITCHING_PROTOCOLS_101 = 101, | ||
27 | |||
28 | /** | ||
29 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.3.1 | ||
30 | * | ||
31 | * Standard response for successful HTTP requests. The actual response will depend on the request method used: | ||
32 | * GET: The resource has been fetched and is transmitted in the message body. | ||
33 | * HEAD: The entity headers are in the message body. | ||
34 | * POST: The resource describing the result of the action is transmitted in the message body. | ||
35 | * TRACE: The message body contains the request message as received by the server | ||
36 | */ | ||
37 | OK_200 = 200, | ||
38 | |||
39 | /** | ||
40 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.3.2 | ||
41 | * | ||
42 | * The request has been fulfilled, resulting in the creation of a new resource, typically after a PUT. | ||
43 | */ | ||
44 | CREATED_201 = 201, | ||
45 | |||
46 | /** | ||
47 | * The request has been accepted for processing, but the processing has not been completed. | ||
48 | * The request might or might not be eventually acted upon, and may be disallowed when processing occurs. | ||
49 | */ | ||
50 | ACCEPTED_202 = 202, | ||
51 | |||
52 | /** | ||
53 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.3.5 | ||
54 | * | ||
55 | * There is no content to send for this request, but the headers may be useful. | ||
56 | * The user-agent may update its cached headers for this resource with the new ones. | ||
57 | */ | ||
58 | NO_CONTENT_204 = 204, | ||
59 | |||
60 | /** | ||
61 | * The server successfully processed the request, but is not returning any content. | ||
62 | * Unlike a 204 response, this response requires that the requester reset the document view. | ||
63 | */ | ||
64 | RESET_CONTENT_205 = 205, | ||
65 | |||
66 | /** | ||
67 | * The server is delivering only part of the resource (byte serving) due to a range header sent by the client. | ||
68 | * The range header is used by HTTP clients to enable resuming of interrupted downloads, | ||
69 | * or split a download into multiple simultaneous streams. | ||
70 | */ | ||
71 | PARTIAL_CONTENT_206 = 206, | ||
72 | |||
73 | /** | ||
74 | * Indicates multiple options for the resource from which the client may choose (via agent-driven content negotiation). | ||
75 | * For example, this code could be used to present multiple video format options, | ||
76 | * to list files with different filename extensions, or to suggest word-sense disambiguation. | ||
77 | */ | ||
78 | MULTIPLE_CHOICES_300 = 300, | ||
79 | |||
80 | /** | ||
81 | * This and all future requests should be directed to the given URI. | ||
82 | */ | ||
83 | MOVED_PERMANENTLY_301 = 301, | ||
84 | |||
85 | /** | ||
86 | * This is an example of industry practice contradicting the standard. | ||
87 | * The HTTP/1.0 specification (RFC 1945) required the client to perform a temporary redirect | ||
88 | * (the original describing phrase was "Moved Temporarily"), but popular browsers implemented 302 | ||
89 | * with the functionality of a 303 See Other. Therefore, HTTP/1.1 added status codes 303 and 307 | ||
90 | * to distinguish between the two behaviours. However, some Web applications and frameworks | ||
91 | * use the 302 status code as if it were the 303. | ||
92 | */ | ||
93 | FOUND_302 = 302, | ||
94 | |||
95 | /** | ||
96 | * SINCE HTTP/1.1 | ||
97 | * The response to the request can be found under another URI using a GET method. | ||
98 | * When received in response to a POST (or PUT/DELETE), the client should presume that | ||
99 | * the server has received the data and should issue a redirect with a separate GET message. | ||
100 | */ | ||
101 | SEE_OTHER_303 = 303, | ||
102 | |||
103 | /** | ||
104 | * Official Documentation @ https://tools.ietf.org/html/rfc7232#section-4.1 | ||
105 | * | ||
106 | * Indicates that the resource has not been modified since the version specified by the request headers | ||
107 | * `If-Modified-Since` or `If-None-Match`. | ||
108 | * In such case, there is no need to retransmit the resource since the client still has a previously-downloaded copy. | ||
109 | */ | ||
110 | NOT_MODIFIED_304 = 304, | ||
111 | |||
112 | /** | ||
113 | * SINCE HTTP/1.1 | ||
114 | * In this case, the request should be repeated with another URI; however, future requests should still use the original URI. | ||
115 | * In contrast to how 302 was historically implemented, the request method is not allowed to be changed when reissuing the | ||
116 | * original request. | ||
117 | * For example, a POST request should be repeated using another POST request. | ||
118 | */ | ||
119 | TEMPORARY_REDIRECT_307 = 307, | ||
120 | |||
121 | /** | ||
122 | * The request and all future requests should be repeated using another URI. | ||
123 | * 307 and 308 parallel the behaviors of 302 and 301, but do not allow the HTTP method to change. | ||
124 | * So, for example, submitting a form to a permanently redirected resource may continue smoothly. | ||
125 | */ | ||
126 | PERMANENT_REDIRECT_308 = 308, | ||
127 | |||
128 | /** | ||
129 | * The server cannot or will not process the request due to an apparent client error | ||
130 | * (e.g., malformed request syntax, too large size, invalid request message framing, or deceptive request routing). | ||
131 | */ | ||
132 | BAD_REQUEST_400 = 400, | ||
133 | |||
134 | /** | ||
135 | * Official Documentation @ https://tools.ietf.org/html/rfc7235#section-3.1 | ||
136 | * | ||
137 | * Similar to 403 Forbidden, but specifically for use when authentication is required and has failed or has not yet | ||
138 | * been provided. The response must include a `WWW-Authenticate` header field containing a challenge applicable to the | ||
139 | * requested resource. See Basic access authentication and Digest access authentication. 401 semantically means | ||
140 | * "unauthenticated",i.e. the user does not have the necessary credentials. | ||
141 | */ | ||
142 | UNAUTHORIZED_401 = 401, | ||
143 | |||
144 | /** | ||
145 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.2 | ||
146 | * | ||
147 | * Reserved for future use. The original intention was that this code might be used as part of some form of digital | ||
148 | * cash or micro payment scheme, but that has not happened, and this code is not usually used. | ||
149 | * Google Developers API uses this status if a particular developer has exceeded the daily limit on requests. | ||
150 | */ | ||
151 | PAYMENT_REQUIRED_402 = 402, | ||
152 | |||
153 | /** | ||
154 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.3 | ||
155 | * | ||
156 | * The client does not have access rights to the content, i.e. they are unauthorized, so server is rejecting to | ||
157 | * give proper response. Unlike 401, the client's identity is known to the server. | ||
158 | */ | ||
159 | FORBIDDEN_403 = 403, | ||
160 | |||
161 | /** | ||
162 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.6.2 | ||
163 | * | ||
164 | * The requested resource could not be found but may be available in the future. | ||
165 | * Subsequent requests by the client are permissible. | ||
166 | */ | ||
167 | NOT_FOUND_404 = 404, | ||
168 | |||
169 | /** | ||
170 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.5 | ||
171 | * | ||
172 | * A request method is not supported for the requested resource; | ||
173 | * for example, a GET request on a form that requires data to be presented via POST, or a PUT request on a read-only resource. | ||
174 | */ | ||
175 | METHOD_NOT_ALLOWED_405 = 405, | ||
176 | |||
177 | /** | ||
178 | * The requested resource is capable of generating only content not acceptable according to the Accept headers sent in the request. | ||
179 | */ | ||
180 | NOT_ACCEPTABLE_406 = 406, | ||
181 | |||
182 | /** | ||
183 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.7 | ||
184 | * | ||
185 | * This response is sent on an idle connection by some servers, even without any previous request by the client. | ||
186 | * It means that the server would like to shut down this unused connection. This response is used much more since | ||
187 | * some browsers, like Chrome, Firefox 27+, or IE9, use HTTP pre-connection mechanisms to speed up surfing. Also | ||
188 | * note that some servers merely shut down the connection without sending this message. | ||
189 | * | ||
190 | * @ | ||
191 | */ | ||
192 | REQUEST_TIMEOUT_408 = 408, | ||
193 | |||
194 | /** | ||
195 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.8 | ||
196 | * | ||
197 | * Indicates that the request could not be processed because of conflict in the request, | ||
198 | * such as an edit conflict between multiple simultaneous updates. | ||
199 | * | ||
200 | * @see HttpStatusCode.UNPROCESSABLE_ENTITY_422 to denote a disabled feature | ||
201 | */ | ||
202 | CONFLICT_409 = 409, | ||
203 | |||
204 | /** | ||
205 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.9 | ||
206 | * | ||
207 | * Indicates that the resource requested is no longer available and will not be available again. | ||
208 | * This should be used when a resource has been intentionally removed and the resource should be purged. | ||
209 | * Upon receiving a 410 status code, the client should not request the resource in the future. | ||
210 | * Clients such as search engines should remove the resource from their indices. | ||
211 | * Most use cases do not require clients and search engines to purge the resource, and a "404 Not Found" may be used instead. | ||
212 | */ | ||
213 | GONE_410 = 410, | ||
214 | |||
215 | /** | ||
216 | * The request did not specify the length of its content, which is required by the requested resource. | ||
217 | */ | ||
218 | LENGTH_REQUIRED_411 = 411, | ||
219 | |||
220 | /** | ||
221 | * The server does not meet one of the preconditions that the requester put on the request. | ||
222 | */ | ||
223 | PRECONDITION_FAILED_412 = 412, | ||
224 | |||
225 | /** | ||
226 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.11 | ||
227 | * | ||
228 | * The request is larger than the server is willing or able to process ; the server might close the connection | ||
229 | * or return an Retry-After header field. | ||
230 | * Previously called "Request Entity Too Large". | ||
231 | */ | ||
232 | PAYLOAD_TOO_LARGE_413 = 413, | ||
233 | |||
234 | /** | ||
235 | * The URI provided was too long for the server to process. Often the result of too much data being encoded as a | ||
236 | * query-string of a GET request, in which case it should be converted to a POST request. | ||
237 | * Called "Request-URI Too Long" previously. | ||
238 | */ | ||
239 | URI_TOO_LONG_414 = 414, | ||
240 | |||
241 | /** | ||
242 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.13 | ||
243 | * | ||
244 | * The request entity has a media type which the server or resource does not support. | ||
245 | * For example, the client uploads an image as image/svg+xml, but the server requires that images use a different format. | ||
246 | */ | ||
247 | UNSUPPORTED_MEDIA_TYPE_415 = 415, | ||
248 | |||
249 | /** | ||
250 | * The client has asked for a portion of the file (byte serving), but the server cannot supply that portion. | ||
251 | * For example, if the client asked for a part of the file that lies beyond the end of the file. | ||
252 | * Called "Requested Range Not Satisfiable" previously. | ||
253 | */ | ||
254 | RANGE_NOT_SATISFIABLE_416 = 416, | ||
255 | |||
256 | /** | ||
257 | * The server cannot meet the requirements of the `Expect` request-header field. | ||
258 | */ | ||
259 | EXPECTATION_FAILED_417 = 417, | ||
260 | |||
261 | /** | ||
262 | * Official Documentation @ https://tools.ietf.org/html/rfc2324 | ||
263 | * | ||
264 | * This code was defined in 1998 as one of the traditional IETF April Fools' jokes, in RFC 2324, Hyper Text Coffee Pot Control Protocol, | ||
265 | * and is not expected to be implemented by actual HTTP servers. The RFC specifies this code should be returned by | ||
266 | * teapots requested to brew coffee. This HTTP status is used as an Easter egg in some websites, including PeerTube instances ;-). | ||
267 | */ | ||
268 | I_AM_A_TEAPOT_418 = 418, | ||
269 | |||
270 | /** | ||
271 | * Official Documentation @ https://tools.ietf.org/html/rfc2518#section-10.3 | ||
272 | * | ||
273 | * The request was well-formed but was unable to be followed due to semantic errors. | ||
274 | * The server understands the content type of the request entity (hence a 415 (Unsupported Media Type) status code is inappropriate), | ||
275 | * and the syntax of the request entity is correct (thus a 400 (Bad Request) status code is inappropriate) but was unable to process | ||
276 | * the contained instructions. For example, this error condition may occur if an JSON request body contains well-formed (i.e., | ||
277 | * syntactically correct), but semantically erroneous, JSON instructions. | ||
278 | * | ||
279 | * Can also be used to denote disabled features (akin to disabled syntax). | ||
280 | * | ||
281 | * @see HttpStatusCode.UNSUPPORTED_MEDIA_TYPE_415 if the `Content-Type` was not supported. | ||
282 | * @see HttpStatusCode.BAD_REQUEST_400 if the request was not parsable (broken JSON, XML) | ||
283 | */ | ||
284 | UNPROCESSABLE_ENTITY_422 = 422, | ||
285 | |||
286 | /** | ||
287 | * Official Documentation @ https://tools.ietf.org/html/rfc4918#section-11.3 | ||
288 | * | ||
289 | * The resource that is being accessed is locked. WebDAV-specific but used by some HTTP services. | ||
290 | * | ||
291 | * @deprecated use `If-Match` / `If-None-Match` instead | ||
292 | * @see {@link https://evertpot.com/http/423-locked} | ||
293 | */ | ||
294 | LOCKED_423 = 423, | ||
295 | |||
296 | /** | ||
297 | * Official Documentation @ https://tools.ietf.org/html/rfc6585#section-4 | ||
298 | * | ||
299 | * The user has sent too many requests in a given amount of time. Intended for use with rate-limiting schemes. | ||
300 | */ | ||
301 | TOO_MANY_REQUESTS_429 = 429, | ||
302 | |||
303 | /** | ||
304 | * Official Documentation @ https://tools.ietf.org/html/rfc6585#section-5 | ||
305 | * | ||
306 | * The server is unwilling to process the request because either an individual header field, | ||
307 | * or all the header fields collectively, are too large. | ||
308 | */ | ||
309 | REQUEST_HEADER_FIELDS_TOO_LARGE_431 = 431, | ||
310 | |||
311 | /** | ||
312 | * Official Documentation @ https://tools.ietf.org/html/rfc7725 | ||
313 | * | ||
314 | * A server operator has received a legal demand to deny access to a resource or to a set of resources | ||
315 | * that includes the requested resource. The code 451 was chosen as a reference to the novel Fahrenheit 451. | ||
316 | */ | ||
317 | UNAVAILABLE_FOR_LEGAL_REASONS_451 = 451, | ||
318 | |||
319 | /** | ||
320 | * A generic error message, given when an unexpected condition was encountered and no more specific message is suitable. | ||
321 | */ | ||
322 | INTERNAL_SERVER_ERROR_500 = 500, | ||
323 | |||
324 | /** | ||
325 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.6.2 | ||
326 | * | ||
327 | * The server either does not recognize the request method, or it lacks the ability to fulfill the request. | ||
328 | * Usually this implies future availability (e.g., a new feature of a web-service API). | ||
329 | */ | ||
330 | NOT_IMPLEMENTED_501 = 501, | ||
331 | |||
332 | /** | ||
333 | * The server was acting as a gateway or proxy and received an invalid response from the upstream server. | ||
334 | */ | ||
335 | BAD_GATEWAY_502 = 502, | ||
336 | |||
337 | /** | ||
338 | * The server is currently unavailable (because it is overloaded or down for maintenance). | ||
339 | * Generally, this is a temporary state. | ||
340 | */ | ||
341 | SERVICE_UNAVAILABLE_503 = 503, | ||
342 | |||
343 | /** | ||
344 | * The server was acting as a gateway or proxy and did not receive a timely response from the upstream server. | ||
345 | */ | ||
346 | GATEWAY_TIMEOUT_504 = 504, | ||
347 | |||
348 | /** | ||
349 | * The server does not support the HTTP protocol version used in the request | ||
350 | */ | ||
351 | HTTP_VERSION_NOT_SUPPORTED_505 = 505, | ||
352 | |||
353 | /** | ||
354 | * Official Documentation @ https://tools.ietf.org/html/rfc2518#section-10.6 | ||
355 | * | ||
356 | * The 507 (Insufficient Storage) status code means the method could not be performed on the resource because the | ||
357 | * server is unable to store the representation needed to successfully complete the request. This condition is | ||
358 | * considered to be temporary. If the request which received this status code was the result of a user action, | ||
359 | * the request MUST NOT be repeated until it is requested by a separate user action. | ||
360 | * | ||
361 | * @see HttpStatusCode.PAYLOAD_TOO_LARGE_413 for quota errors | ||
362 | */ | ||
363 | INSUFFICIENT_STORAGE_507 = 507, | ||
364 | } | ||
diff --git a/shared/core-utils/miscs/http-methods.ts b/shared/core-utils/miscs/http-methods.ts deleted file mode 100644 index 1cfa458b9..000000000 --- a/shared/core-utils/miscs/http-methods.ts +++ /dev/null | |||
@@ -1,21 +0,0 @@ | |||
1 | /** HTTP request method to indicate the desired action to be performed for a given resource. */ | ||
2 | export enum HttpMethod { | ||
3 | /** The CONNECT method establishes a tunnel to the server identified by the target resource. */ | ||
4 | CONNECT = 'CONNECT', | ||
5 | /** The DELETE method deletes the specified resource. */ | ||
6 | DELETE = 'DELETE', | ||
7 | /** The GET method requests a representation of the specified resource. Requests using GET should only retrieve data. */ | ||
8 | GET = 'GET', | ||
9 | /** The HEAD method asks for a response identical to that of a GET request, but without the response body. */ | ||
10 | HEAD = 'HEAD', | ||
11 | /** The OPTIONS method is used to describe the communication options for the target resource. */ | ||
12 | OPTIONS = 'OPTIONS', | ||
13 | /** The PATCH method is used to apply partial modifications to a resource. */ | ||
14 | PATCH = 'PATCH', | ||
15 | /** The POST method is used to submit an entity to the specified resource */ | ||
16 | POST = 'POST', | ||
17 | /** The PUT method replaces all current representations of the target resource with the request payload. */ | ||
18 | PUT = 'PUT', | ||
19 | /** The TRACE method performs a message loop-back test along the path to the target resource. */ | ||
20 | TRACE = 'TRACE' | ||
21 | } | ||
diff --git a/shared/core-utils/miscs/index.ts b/shared/core-utils/miscs/index.ts deleted file mode 100644 index 251df1de2..000000000 --- a/shared/core-utils/miscs/index.ts +++ /dev/null | |||
@@ -1,5 +0,0 @@ | |||
1 | export * from './date' | ||
2 | export * from './miscs' | ||
3 | export * from './types' | ||
4 | export * from './http-error-codes' | ||
5 | export * from './http-methods' | ||
diff --git a/shared/core-utils/plugins/hooks.ts b/shared/core-utils/plugins/hooks.ts index 5405e0529..92cb5ad68 100644 --- a/shared/core-utils/plugins/hooks.ts +++ b/shared/core-utils/plugins/hooks.ts | |||
@@ -1,5 +1,5 @@ | |||
1 | import { HookType } from '../../models/plugins/hook-type.enum' | 1 | import { HookType } from '../../models/plugins/hook-type.enum' |
2 | import { isCatchable, isPromise } from '../miscs/miscs' | 2 | import { isCatchable, isPromise } from '../common/promises' |
3 | 3 | ||
4 | function getHookType (hookName: string) { | 4 | function getHookType (hookName: string) { |
5 | if (hookName.startsWith('filter:')) return HookType.FILTER | 5 | if (hookName.startsWith('filter:')) return HookType.FILTER |