]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/server-commands/videos/streaming-playlists-command.ts
Increase request retry interval
[github/Chocobozzz/PeerTube.git] / shared / server-commands / videos / streaming-playlists-command.ts
1 import { wait } from '@shared/core-utils'
2 import { HttpStatusCode } from '@shared/models'
3 import { unwrapBody, unwrapBodyOrDecodeToJSON, unwrapTextOrDecode } from '../requests'
4 import { AbstractCommand, OverrideCommandOptions } from '../shared'
5
6 export class StreamingPlaylistsCommand extends AbstractCommand {
7
8 async get (options: OverrideCommandOptions & {
9 url: string
10
11 videoFileToken?: string
12 reinjectVideoFileToken?: boolean
13
14 withRetry?: boolean // default false
15 currentRetry?: number
16 }): Promise<string> {
17 const { videoFileToken, reinjectVideoFileToken, withRetry = false, currentRetry = 1 } = options
18
19 try {
20 const result = await unwrapTextOrDecode(this.getRawRequest({
21 ...options,
22
23 url: options.url,
24 query: {
25 videoFileToken,
26 reinjectVideoFileToken
27 },
28 implicitToken: false,
29 defaultExpectedStatus: HttpStatusCode.OK_200
30 }))
31
32 return result
33 } catch (err) {
34 if (!withRetry || currentRetry > 10) throw err
35
36 await wait(250)
37
38 return this.get({
39 ...options,
40
41 withRetry,
42 currentRetry: currentRetry + 1
43 })
44 }
45 }
46
47 async getFragmentedSegment (options: OverrideCommandOptions & {
48 url: string
49 range?: string
50
51 withRetry?: boolean // default false
52 currentRetry?: number
53 }) {
54 const { withRetry = false, currentRetry = 1 } = options
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) {
69 if (!withRetry || currentRetry > 10) throw err
70
71 await wait(250)
72
73 return this.getFragmentedSegment({
74 ...options,
75
76 withRetry,
77 currentRetry: currentRetry + 1
78 })
79 }
80 }
81
82 async getSegmentSha256 (options: OverrideCommandOptions & {
83 url: string
84
85 withRetry?: boolean // default false
86 currentRetry?: number
87 }) {
88 const { withRetry = false, currentRetry = 1 } = options
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) {
102 if (!withRetry || currentRetry > 10) throw err
103
104 await wait(250)
105
106 return this.getSegmentSha256({
107 ...options,
108
109 withRetry,
110 currentRetry: currentRetry + 1
111 })
112 }
113 }
114 }