]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/server-commands/videos/streaming-playlists-command.ts
Increase request retry interval
[github/Chocobozzz/PeerTube.git] / shared / server-commands / videos / streaming-playlists-command.ts
CommitLineData
34aa316f 1import { wait } from '@shared/core-utils'
c0e8b12e 2import { HttpStatusCode } from '@shared/models'
34aa316f 3import { unwrapBody, unwrapBodyOrDecodeToJSON, unwrapTextOrDecode } from '../requests'
57f879a5
C
4import { AbstractCommand, OverrideCommandOptions } from '../shared'
5
6export class StreamingPlaylistsCommand extends AbstractCommand {
7
34aa316f 8 async get (options: OverrideCommandOptions & {
57f879a5 9 url: string
71e3e879
C
10
11 videoFileToken?: string
12 reinjectVideoFileToken?: boolean
13
34aa316f
C
14 withRetry?: boolean // default false
15 currentRetry?: number
d102de1b 16 }): Promise<string> {
644391be 17 const { videoFileToken, reinjectVideoFileToken, withRetry = false, currentRetry = 1 } = options
57f879a5 18
34aa316f
C
19 try {
20 const result = await unwrapTextOrDecode(this.getRawRequest({
21 ...options,
22
23 url: options.url,
71e3e879
C
24 query: {
25 videoFileToken,
26 reinjectVideoFileToken
27 },
34aa316f
C
28 implicitToken: false,
29 defaultExpectedStatus: HttpStatusCode.OK_200
30 }))
31
32 return result
33 } catch (err) {
a63943fe 34 if (!withRetry || currentRetry > 10) throw err
34aa316f 35
c106db14 36 await wait(250)
34aa316f
C
37
38 return this.get({
39 ...options,
40
41 withRetry,
42 currentRetry: currentRetry + 1
43 })
44 }
57f879a5
C
45 }
46
5170f492 47 async getFragmentedSegment (options: OverrideCommandOptions & {
57f879a5
C
48 url: string
49 range?: string
5170f492
C
50
51 withRetry?: boolean // default false
52 currentRetry?: number
57f879a5 53 }) {
644391be 54 const { withRetry = false, currentRetry = 1 } = options
5170f492
C
55
56 try {
57 const result = await unwrapBody<Buffer>(this.getRawRequest({
58 ...options,
59
60 url: options.url,
61 range: options.range,
62 implicitToken: false,
63 responseType: 'application/octet-stream',
64 defaultExpectedStatus: HttpStatusCode.OK_200
65 }))
66
67 return result
68 } catch (err) {
a63943fe 69 if (!withRetry || currentRetry > 10) throw err
5170f492 70
c106db14 71 await wait(250)
5170f492
C
72
73 return this.getFragmentedSegment({
74 ...options,
75
76 withRetry,
77 currentRetry: currentRetry + 1
78 })
79 }
57f879a5
C
80 }
81
5170f492 82 async getSegmentSha256 (options: OverrideCommandOptions & {
57f879a5 83 url: string
5170f492
C
84
85 withRetry?: boolean // default false
86 currentRetry?: number
57f879a5 87 }) {
644391be 88 const { withRetry = false, currentRetry = 1 } = options
5170f492
C
89
90 try {
91 const result = await unwrapBodyOrDecodeToJSON<{ [ id: string ]: string }>(this.getRawRequest({
92 ...options,
93
94 url: options.url,
95 contentType: 'application/json',
96 implicitToken: false,
97 defaultExpectedStatus: HttpStatusCode.OK_200
98 }))
99
100 return result
101 } catch (err) {
a63943fe 102 if (!withRetry || currentRetry > 10) throw err
5170f492 103
c106db14 104 await wait(250)
5170f492
C
105
106 return this.getSegmentSha256({
107 ...options,
108
109 withRetry,
110 currentRetry: currentRetry + 1
111 })
112 }
57f879a5
C
113 }
114}