1
1
import { RequestResponseInfo } from "./requestresponseinfo" ;
2
2
3
- import { getCustomRewriter , rewriteDASH , rewriteHLS } from "@webrecorder/wabac" ;
3
+ import {
4
+ getCustomRewriter ,
5
+ rewriteDASH ,
6
+ rewriteHLS ,
7
+ removeRangeAsQuery ,
8
+ } from "@webrecorder/wabac" ;
4
9
5
10
import { Buffer } from "buffer" ;
6
11
@@ -15,6 +20,7 @@ import {
15
20
BEHAVIOR_PAUSED ,
16
21
BEHAVIOR_DONE ,
17
22
} from "./consts" ;
23
+ import { getLocalOption } from "./localstorage" ;
18
24
19
25
// @ts -expect-error - TS2554 - Expected 0 arguments, but got 1.
20
26
const encoder = new TextEncoder ( "utf-8" ) ;
@@ -34,9 +40,26 @@ function sleep(time) {
34
40
return new Promise ( ( resolve ) => setTimeout ( ( ) => resolve ( ) , time ) ) ;
35
41
}
36
42
43
+ type FetchEntry = {
44
+ url : string ;
45
+ headers ?: Headers ;
46
+ rangeReplaced ?: boolean ;
47
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
48
+ sessions ?: any [ ] ;
49
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
50
+ pageInfo ?: any ;
51
+
52
+ rangeRemoved ?: boolean ;
53
+ doRangeCheck ?: boolean ;
54
+ redirectOnly ?: boolean ;
55
+ } ;
56
+
37
57
// ===========================================================================
38
58
class Recorder {
39
- recordStorage = true ;
59
+ archiveStorage = false ;
60
+ archiveCookies = false ;
61
+
62
+ _fetchQueue : FetchEntry [ ] = [ ] ;
40
63
41
64
constructor ( ) {
42
65
// @ts -expect-error - TS2339 - Property 'flatMode' does not exist on type 'Recorder'.
@@ -79,8 +102,7 @@ class Recorder {
79
102
80
103
// @ts -expect-error - TS2339 - Property '_fetchPending' does not exist on type 'Recorder'.
81
104
this . _fetchPending = new Map ( ) ;
82
- // @ts -expect-error - TS2339 - Property '_fetchQueue' does not exist on type 'Recorder'.
83
- this . _fetchQueue = [ ] ;
105
+
84
106
// @ts -expect-error - TS2339 - Property '_fetchUrls' does not exist on type 'Recorder'.
85
107
this . _fetchUrls = new Set ( ) ;
86
108
@@ -128,6 +150,13 @@ class Recorder {
128
150
this . defaultFetchOpts = {
129
151
redirect : "manual" ,
130
152
} ;
153
+
154
+ this . initOpts ( ) ;
155
+ }
156
+
157
+ async initOpts ( ) {
158
+ this . archiveCookies = ( await getLocalOption ( "archiveCookies" ) === "1" ) ;
159
+ this . archiveStorage = ( await getLocalOption ( "archiveStorage" ) === "1" ) ;
131
160
}
132
161
133
162
// @ts -expect-error - TS7006 - Parameter 'autorun' implicitly has an 'any' type.
@@ -860,7 +889,7 @@ class Recorder {
860
889
// @ts -expect-error - TS7006 - Parameter 'url' implicitly has an 'any' type. | TS7006 - Parameter 'sessions' implicitly has an 'any' type.
861
890
handleWindowOpen ( url , sessions ) {
862
891
// @ts -expect-error - TS2339 - Property 'pageInfo' does not exist on type 'Recorder'.
863
- const headers = { Referer : this . pageInfo . url } ;
892
+ const headers = new Headers ( { Referer : this . pageInfo . url } ) ;
864
893
this . doAsyncFetch ( { url, headers, redirectOnly : true } , sessions ) ;
865
894
}
866
895
@@ -1450,8 +1479,12 @@ class Recorder {
1450
1479
//this._fetchPending.set(requestId, pending);
1451
1480
1452
1481
try {
1453
- // @ts -expect-error - TS2339 - Property 'pageInfo' does not exist on type 'Recorder'.
1454
- const data = reqresp . toDBRecord ( reqresp . payload , this . pageInfo ) ;
1482
+ const data = reqresp . toDBRecord (
1483
+ reqresp . payload ,
1484
+ // @ts -expect-error - TS2339 - Property 'pageInfo' does not exist on type 'Recorder'.
1485
+ this . pageInfo ,
1486
+ this . archiveCookies ,
1487
+ ) ;
1455
1488
1456
1489
// top-level URL is a non-GET request
1457
1490
if (
@@ -1513,7 +1546,7 @@ class Recorder {
1513
1546
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1514
1547
async getStorage ( sessions : any ) {
1515
1548
// check if recording storage is allowed
1516
- if ( ! this . recordStorage ) {
1549
+ if ( ! this . archiveStorage ) {
1517
1550
return null ;
1518
1551
}
1519
1552
@@ -1576,7 +1609,7 @@ class Recorder {
1576
1609
1577
1610
reqresp . fillResponseRedirect ( params ) ;
1578
1611
// @ts -expect-error - TS2339 - Property 'pageInfo' does not exist on type 'Recorder'.
1579
- data = reqresp . toDBRecord ( null , this . pageInfo ) ;
1612
+ data = reqresp . toDBRecord ( null , this . pageInfo , this . archiveCookies ) ;
1580
1613
}
1581
1614
1582
1615
reqresp . fillRequest ( params ) ;
@@ -1629,14 +1662,14 @@ class Recorder {
1629
1662
for ( const { value } of params . events ) {
1630
1663
if ( value . indexOf ( '"kLoad"' ) > 0 ) {
1631
1664
const { url } = JSON . parse ( value ) ;
1632
- this . doAsyncFetch ( { url } , sessions ) ;
1665
+ this . doAsyncFetch ( { url, doRangeCheck : true } , sessions ) ;
1633
1666
break ;
1634
1667
}
1635
1668
}
1636
1669
}
1637
1670
1638
1671
// @ts -expect-error - TS7006 - Parameter 'request' implicitly has an 'any' type. | TS7006 - Parameter 'resp' implicitly has an 'any' type.
1639
- async attemptFetchRedirect ( request , resp ) {
1672
+ async attemptFetchRedirect ( request : FetchEntry , resp ) {
1640
1673
if ( request . redirectOnly && resp . type === "opaqueredirect" ) {
1641
1674
const abort = new AbortController ( ) ;
1642
1675
// @ts -expect-error - TS2345 - Argument of type '{ abort: AbortController; }' is not assignable to parameter of type 'RequestInit'.
@@ -1671,11 +1704,19 @@ class Recorder {
1671
1704
}
1672
1705
1673
1706
// @ts -expect-error - TS7006 - Parameter 'request' implicitly has an 'any' type. | TS7006 - Parameter 'sessions' implicitly has an 'any' type.
1674
- doAsyncFetch ( request , sessions ) {
1707
+ doAsyncFetch ( request : FetchEntry , sessions ) {
1675
1708
if ( ! request || ! this . isValidUrl ( request . url ) ) {
1676
1709
return ;
1677
1710
}
1678
1711
1712
+ if ( request . doRangeCheck ) {
1713
+ const url = removeRangeAsQuery ( request . url ) ;
1714
+ if ( url ) {
1715
+ request . url = url ;
1716
+ request . rangeRemoved = true ;
1717
+ }
1718
+ }
1719
+
1679
1720
// @ts -expect-error - TS2339 - Property '_fetchUrls' does not exist on type 'Recorder'.
1680
1721
if ( this . _fetchUrls . has ( request . url ) ) {
1681
1722
console . log ( "Skipping, already fetching: " + request . url ) ;
@@ -1686,15 +1727,13 @@ class Recorder {
1686
1727
request . pageInfo = this . pageInfo ;
1687
1728
request . sessions = sessions ;
1688
1729
1689
- // @ts -expect-error - TS2339 - Property '_fetchQueue' does not exist on type 'Recorder'.
1690
1730
this . _fetchQueue . push ( request ) ;
1691
1731
1692
1732
this . doBackgroundFetch ( ) ;
1693
1733
}
1694
1734
1695
1735
async doBackgroundFetch ( ) {
1696
1736
if (
1697
- // @ts -expect-error - TS2339 - Property '_fetchQueue' does not exist on type 'Recorder'.
1698
1737
! this . _fetchQueue . length ||
1699
1738
// @ts -expect-error - TS2339 - Property '_fetchPending' does not exist on type 'Recorder'.
1700
1739
this . _fetchPending . size >= MAX_CONCURRENT_FETCH ||
@@ -1704,8 +1743,10 @@ class Recorder {
1704
1743
return ;
1705
1744
}
1706
1745
1707
- // @ts -expect-error - TS2339 - Property '_fetchQueue' does not exist on type 'Recorder'.
1708
1746
const request = this . _fetchQueue . shift ( ) ;
1747
+ if ( ! request ) {
1748
+ return ;
1749
+ }
1709
1750
1710
1751
// @ts -expect-error - TS2339 - Property '_fetchUrls' does not exist on type 'Recorder'.
1711
1752
if ( this . _fetchUrls . has ( request . url ) ) {
@@ -1732,11 +1773,9 @@ class Recorder {
1732
1773
// @ts -expect-error - TS2339 - Property 'defaultFetchOpts' does not exist on type 'Recorder'.
1733
1774
const opts = { ...this . defaultFetchOpts } ;
1734
1775
1735
- if ( request . getRequestHeadersDict ) {
1736
- opts . headers = request . getRequestHeadersDict ( ) . headers ;
1737
- opts . headers . delete ( "range" ) ;
1738
- } else if ( request . headers ) {
1776
+ if ( request . headers ) {
1739
1777
opts . headers = request . headers ;
1778
+ opts . headers . delete ( "range" ) ;
1740
1779
}
1741
1780
1742
1781
let resp = await fetch ( request . url , opts ) ;
@@ -1779,8 +1818,12 @@ class Recorder {
1779
1818
// @ts -expect-error - TS2339 - Property 'payload' does not exist on type 'RequestResponseInfo'.
1780
1819
reqresp . payload = new Uint8Array ( payload ) ;
1781
1820
1782
- // @ts -expect-error - TS2339 - Property 'payload' does not exist on type 'RequestResponseInfo'.
1783
- const data = reqresp . toDBRecord ( reqresp . payload , request . pageInfo ) ;
1821
+ const data = reqresp . toDBRecord (
1822
+ // @ts -expect-error - TS2339 - Property 'payload' does not exist on type 'RequestResponseInfo'.
1823
+ reqresp . payload ,
1824
+ request . pageInfo ,
1825
+ this . archiveCookies ,
1826
+ ) ;
1784
1827
1785
1828
if ( data ) {
1786
1829
await this . commitResource ( data , request . pageInfo ) ;
@@ -1813,9 +1856,36 @@ class Recorder {
1813
1856
let payload ;
1814
1857
1815
1858
if ( reqresp . status === 206 ) {
1816
- sleep ( 500 ) . then ( ( ) => this . doAsyncFetch ( reqresp , sessions ) ) ;
1859
+ sleep ( 500 ) . then ( ( ) =>
1860
+ this . doAsyncFetch (
1861
+ {
1862
+ url : reqresp . url ,
1863
+ headers : reqresp . getRequestHeadersDict ( ) . headers ,
1864
+ } ,
1865
+ sessions ,
1866
+ ) ,
1867
+ ) ;
1817
1868
reqresp . payload = null ;
1818
1869
return null ;
1870
+ } else {
1871
+ const changedUrl = removeRangeAsQuery ( reqresp . url ) ;
1872
+
1873
+ if ( changedUrl ) {
1874
+ reqresp . url = changedUrl ;
1875
+ this . removeReqResp ( reqresp . requestId ) ;
1876
+ sleep ( 500 ) . then ( ( ) =>
1877
+ this . doAsyncFetch (
1878
+ {
1879
+ url : changedUrl ,
1880
+ headers : reqresp . getRequestHeadersDict ( ) . headers ,
1881
+ rangeRemoved : true ,
1882
+ } ,
1883
+ sessions ,
1884
+ ) ,
1885
+ ) ;
1886
+ reqresp . payload = null ;
1887
+ return null ;
1888
+ }
1819
1889
}
1820
1890
1821
1891
if ( ! this . noResponseForStatus ( reqresp . status ) ) {
@@ -1888,9 +1958,13 @@ class Recorder {
1888
1958
if ( reqresp . payload ) {
1889
1959
// @ts -expect-error - TS2571 - Object is of type 'unknown'.
1890
1960
console . log ( `Committing Finished ${ id } - ${ reqresp . url } ` ) ;
1891
-
1892
1961
// @ts -expect-error - TS2571 - Object is of type 'unknown'. | TS2571 - Object is of type 'unknown'.
1893
- const data = reqresp . toDBRecord ( reqresp . payload , pageInfo ) ;
1962
+ const data = reqresp . toDBRecord (
1963
+ // @ts -expect-error - TS2571 - Object is of type 'unknown'. | TS2571 - Object is of type 'unknown'.
1964
+ reqresp . payload ,
1965
+ pageInfo ,
1966
+ this . archiveCookies ,
1967
+ ) ;
1894
1968
1895
1969
if ( data ) {
1896
1970
// @ts -expect-error - TS2554 - Expected 2 arguments, but got 1.
0 commit comments