Skip to content

Commit 8c29cec

Browse files
gobengoAlan Shaw
andauthored
feat: api maintenance message is a FeatureHasBeenSunsetError when env.MODE is READ_ONLY (#2346)
Motivation: * make sure the maintenance message is more helpful once we use the READ_ONLY mode to sunset writes --------- Co-authored-by: Alan Shaw <alan.shaw@protocol.ai>
1 parent a234e87 commit 8c29cec

6 files changed

Lines changed: 38 additions & 10 deletions

File tree

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
]
4747
},
4848
"engines": {
49-
"node": "16.x",
49+
"node": "18.x",
5050
"npm": ">=7.x"
5151
}
5252
}

packages/api/src/errors.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,24 @@ export class MaintenanceError extends Error {
193193
}
194194
MaintenanceError.CODE = 'ERROR_MAINTENANCE'
195195

196+
export class FeatureHasBeenSunsetError extends Error {
197+
/**
198+
* @param {string} reason
199+
*/
200+
constructor (reason) {
201+
super(reason)
202+
this.name = 'FeatureHasBeenSunset'
203+
/**
204+
* The 410 (Gone) status code indicates that access to the target resource
205+
* is no longer available at the origin server and that this condition is likely
206+
* to be permanent.
207+
*/
208+
this.status = 410 // Gone
209+
this.code = FeatureHasBeenSunsetError.CODE
210+
}
211+
}
212+
FeatureHasBeenSunsetError.CODE = 'ERROR_FEATURE_HAS_BEEN_SUNSET'
213+
196214
export class PSAErrorInvalidData extends PinningServiceApiError {
197215
/**
198216
* @param {string} message

packages/api/src/maintenance.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HTTPError, MaintenanceError } from './errors.js'
1+
import { FeatureHasBeenSunsetError, HTTPError, MaintenanceError } from './errors.js'
22
import { getTokenFromRequest } from './auth.js'
33

44
/**
@@ -50,10 +50,10 @@ export function withMode (mode) {
5050
* @returns {Response|undefined}
5151
*/
5252
return (request, env, ctx) => {
53-
const enabled = () => {
54-
const currentMode = env.MODE
55-
const currentModeBits = modeBits(currentMode)
53+
const currentMode = env.MODE
54+
const currentModeBits = modeBits(currentMode)
5655

56+
const enabled = () => {
5757
return modeBits(mode).every((bit, i) => {
5858
if (bit === '-') {
5959
return true
@@ -78,6 +78,10 @@ export function withMode (mode) {
7878

7979
// Not enabled, use maintenance handler.
8080
if (!enabled() && !modeSkip()) {
81+
const isAfterSunsetStart = env.NEXT_PUBLIC_W3UP_LAUNCH_SUNSET_START ? (env.NEXT_PUBLIC_W3UP_LAUNCH_SUNSET_START < new Date().toISOString()) : false
82+
if (isAfterSunsetStart && (currentMode === READ_ONLY)) {
83+
throw new FeatureHasBeenSunsetError('This API feature has been sunset, and is no longer available. To continue uploading, use the new web3.storage API: https://web3.storage/docs.')
84+
}
8185
return maintenanceHandler()
8286
}
8387
}

packages/api/test/maintenance.spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ describe('maintenance middleware', () => {
6262
assert.throws(() => block(() => { }, {
6363
MODE: NO_READ_OR_WRITE
6464
}), /API undergoing maintenance/)
65+
66+
// after product sunset, READ_ONLY means FeatureHasBeenSunset
67+
assert.throws(() => block(() => { }, {
68+
MODE: READ_ONLY,
69+
NEXT_PUBLIC_W3UP_LAUNCH_SUNSET_START: (new Date(0)).toISOString()
70+
}), /FeatureHasBeenSunset/)
6571
})
6672

6773
it('should bypass maintenance mode with a allowed token', async () => {

packages/website/components/w3up-launch.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ export const W3upMigrationRecommendationCopy = ({ sunsetStartDate }) => {
1919
const sunsetDateFormatter = new Intl.DateTimeFormat(undefined, { dateStyle: 'long' });
2020
return (
2121
<>
22-
This web3.storage product will sunset on {sunsetDateFormatter.format(sunsetStartDate)}. We recommend migrating
23-
your usage of web3.storage to the new web3.storage.
22+
This web3.storage product sunset for new uploads on {sunsetDateFormatter.format(sunsetStartDate)}. To continue
23+
uploading, migrate to the new web3.storage API.
2424
<br />
2525
<a href={createNewAccountHref}>Click here to create a new account</a> and&nbsp;
2626
<a href={learnWhatsNewHref}>here to read about what’s awesome</a> about the new web3.storage experience.

0 commit comments

Comments
 (0)