@@ -1522,3 +1522,181 @@ describe(`getStore`, () => {
15221522 )
15231523 } )
15241524} )
1525+
1526+ describe ( 'Region configuration' , ( ) => {
1527+ describe ( 'With `experimentalRegion: "auto"`' , ( ) => {
1528+ test ( 'The client sends a `region=auto` parameter to API calls' , async ( ) => {
1529+ const mockStore = new MockFetch ( )
1530+ . get ( {
1531+ headers : { authorization : `Bearer ${ apiToken } ` } ,
1532+ response : new Response ( JSON . stringify ( { url : signedURL } ) ) ,
1533+ url : `https://api.netlify.com/api/v1/blobs/${ siteID } /deploy:${ deployID } /${ key } ?region=auto` ,
1534+ } )
1535+ . get ( {
1536+ response : new Response ( value ) ,
1537+ url : signedURL ,
1538+ } )
1539+ . get ( {
1540+ headers : { authorization : `Bearer ${ apiToken } ` } ,
1541+ response : new Response ( JSON . stringify ( { url : signedURL } ) ) ,
1542+ url : `https://api.netlify.com/api/v1/blobs/${ siteID } /deploy:${ deployID } /${ key } ?region=auto` ,
1543+ } )
1544+ . get ( {
1545+ response : new Response ( value ) ,
1546+ url : signedURL ,
1547+ } )
1548+
1549+ globalThis . fetch = mockStore . fetch
1550+
1551+ const deployStore = getDeployStore ( { deployID, siteID, token : apiToken , experimentalRegion : 'auto' } )
1552+
1553+ const string = await deployStore . get ( key )
1554+ expect ( string ) . toBe ( value )
1555+
1556+ const stream = await deployStore . get ( key , { type : 'stream' } )
1557+ expect ( await streamToString ( stream as unknown as NodeJS . ReadableStream ) ) . toBe ( value )
1558+
1559+ expect ( mockStore . fulfilled ) . toBeTruthy ( )
1560+ } )
1561+
1562+ test ( 'Throws when used with `edgeURL`' , async ( ) => {
1563+ const mockRegion = 'us-east-2'
1564+ const mockToken = 'some-token'
1565+ const mockStore = new MockFetch ( )
1566+ . get ( {
1567+ headers : { authorization : `Bearer ${ mockToken } ` } ,
1568+ response : new Response ( value ) ,
1569+ url : `${ edgeURL } /region:${ mockRegion } /${ siteID } /deploy:${ deployID } /${ key } ` ,
1570+ } )
1571+ . get ( {
1572+ headers : { authorization : `Bearer ${ mockToken } ` } ,
1573+ response : new Response ( value ) ,
1574+ url : `${ edgeURL } /region:${ mockRegion } /${ siteID } /deploy:${ deployID } /${ key } ` ,
1575+ } )
1576+
1577+ globalThis . fetch = mockStore . fetch
1578+
1579+ expect ( ( ) =>
1580+ getDeployStore ( { deployID, edgeURL, siteID, token : mockToken , experimentalRegion : 'auto' } ) ,
1581+ ) . toThrowError ( )
1582+ expect ( mockStore . fulfilled ) . toBeFalsy ( )
1583+ } )
1584+ } )
1585+
1586+ describe ( 'With `experimentalRegion: "context"`' , ( ) => {
1587+ test ( 'Adds a `region` parameter to API calls with the value set in the context' , async ( ) => {
1588+ const mockStore = new MockFetch ( )
1589+ . get ( {
1590+ headers : { authorization : `Bearer ${ apiToken } ` } ,
1591+ response : new Response ( JSON . stringify ( { url : signedURL } ) ) ,
1592+ url : `https://api.netlify.com/api/v1/blobs/${ siteID } /deploy:${ deployID } /${ key } ?region=us-east-1` ,
1593+ } )
1594+ . get ( {
1595+ response : new Response ( value ) ,
1596+ url : signedURL ,
1597+ } )
1598+ . get ( {
1599+ headers : { authorization : `Bearer ${ apiToken } ` } ,
1600+ response : new Response ( JSON . stringify ( { url : signedURL } ) ) ,
1601+ url : `https://api.netlify.com/api/v1/blobs/${ siteID } /deploy:${ deployID } /${ key } ?region=us-east-1` ,
1602+ } )
1603+ . get ( {
1604+ response : new Response ( value ) ,
1605+ url : signedURL ,
1606+ } )
1607+
1608+ const context = {
1609+ deployID,
1610+ siteID,
1611+ primaryRegion : 'us-east-1' ,
1612+ token : apiToken ,
1613+ }
1614+
1615+ env . NETLIFY_BLOBS_CONTEXT = Buffer . from ( JSON . stringify ( context ) ) . toString ( 'base64' )
1616+
1617+ globalThis . fetch = mockStore . fetch
1618+
1619+ const deployStore = getDeployStore ( { experimentalRegion : 'context' } )
1620+
1621+ const string = await deployStore . get ( key )
1622+ expect ( string ) . toBe ( value )
1623+
1624+ const stream = await deployStore . get ( key , { type : 'stream' } )
1625+ expect ( await streamToString ( stream as unknown as NodeJS . ReadableStream ) ) . toBe ( value )
1626+
1627+ expect ( mockStore . fulfilled ) . toBeTruthy ( )
1628+ } )
1629+
1630+ test ( 'Adds a `region:` segment to the edge URL path with the value set in the context' , async ( ) => {
1631+ const mockRegion = 'us-east-2'
1632+ const mockToken = 'some-token'
1633+ const mockStore = new MockFetch ( )
1634+ . get ( {
1635+ headers : { authorization : `Bearer ${ mockToken } ` } ,
1636+ response : new Response ( value ) ,
1637+ url : `${ edgeURL } /region:${ mockRegion } /${ siteID } /deploy:${ deployID } /${ key } ` ,
1638+ } )
1639+ . get ( {
1640+ headers : { authorization : `Bearer ${ mockToken } ` } ,
1641+ response : new Response ( value ) ,
1642+ url : `${ edgeURL } /region:${ mockRegion } /${ siteID } /deploy:${ deployID } /${ key } ` ,
1643+ } )
1644+
1645+ globalThis . fetch = mockStore . fetch
1646+
1647+ const context = {
1648+ deployID,
1649+ edgeURL,
1650+ primaryRegion : mockRegion ,
1651+ siteID,
1652+ token : mockToken ,
1653+ }
1654+
1655+ env . NETLIFY_BLOBS_CONTEXT = Buffer . from ( JSON . stringify ( context ) ) . toString ( 'base64' )
1656+
1657+ globalThis . fetch = mockStore . fetch
1658+
1659+ const deployStore = getDeployStore ( { experimentalRegion : 'context' } )
1660+
1661+ const string = await deployStore . get ( key )
1662+ expect ( string ) . toBe ( value )
1663+
1664+ const stream = await deployStore . get ( key , { type : 'stream' } )
1665+ expect ( await streamToString ( stream as unknown as NodeJS . ReadableStream ) ) . toBe ( value )
1666+
1667+ expect ( mockStore . fulfilled ) . toBeTruthy ( )
1668+ } )
1669+
1670+ test ( 'Throws an error when there is no region set in the context' , async ( ) => {
1671+ const mockRegion = 'us-east-2'
1672+ const mockToken = 'some-token'
1673+ const mockStore = new MockFetch ( )
1674+ . get ( {
1675+ headers : { authorization : `Bearer ${ mockToken } ` } ,
1676+ response : new Response ( value ) ,
1677+ url : `${ edgeURL } /region:${ mockRegion } /${ siteID } /deploy:${ deployID } /${ key } ` ,
1678+ } )
1679+ . get ( {
1680+ headers : { authorization : `Bearer ${ mockToken } ` } ,
1681+ response : new Response ( value ) ,
1682+ url : `${ edgeURL } /region:${ mockRegion } /${ siteID } /deploy:${ deployID } /${ key } ` ,
1683+ } )
1684+
1685+ globalThis . fetch = mockStore . fetch
1686+
1687+ const context = {
1688+ deployID,
1689+ edgeURL,
1690+ siteID,
1691+ token : mockToken ,
1692+ }
1693+
1694+ env . NETLIFY_BLOBS_CONTEXT = Buffer . from ( JSON . stringify ( context ) ) . toString ( 'base64' )
1695+
1696+ globalThis . fetch = mockStore . fetch
1697+
1698+ expect ( ( ) => getDeployStore ( { experimentalRegion : 'context' } ) ) . toThrowError ( )
1699+ expect ( mockStore . fulfilled ) . toBeFalsy ( )
1700+ } )
1701+ } )
1702+ } )
0 commit comments