Skip to content

Commit

Permalink
feat: api maintenance message is a FeatureHasBeenSunsetError when env…
Browse files Browse the repository at this point in the history
….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>
  • Loading branch information
gobengo and Alan Shaw authored Jan 9, 2024
1 parent a234e87 commit 8c29cec
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 10 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
]
},
"engines": {
"node": "16.x",
"node": "18.x",
"npm": ">=7.x"
}
}
18 changes: 18 additions & 0 deletions packages/api/src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,24 @@ export class MaintenanceError extends Error {
}
MaintenanceError.CODE = 'ERROR_MAINTENANCE'

export class FeatureHasBeenSunsetError extends Error {
/**
* @param {string} reason
*/
constructor (reason) {
super(reason)
this.name = 'FeatureHasBeenSunset'
/**
* The 410 (Gone) status code indicates that access to the target resource
* is no longer available at the origin server and that this condition is likely
* to be permanent.
*/
this.status = 410 // Gone
this.code = FeatureHasBeenSunsetError.CODE
}
}
FeatureHasBeenSunsetError.CODE = 'ERROR_FEATURE_HAS_BEEN_SUNSET'

export class PSAErrorInvalidData extends PinningServiceApiError {
/**
* @param {string} message
Expand Down
12 changes: 8 additions & 4 deletions packages/api/src/maintenance.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HTTPError, MaintenanceError } from './errors.js'
import { FeatureHasBeenSunsetError, HTTPError, MaintenanceError } from './errors.js'
import { getTokenFromRequest } from './auth.js'

/**
Expand Down Expand Up @@ -50,10 +50,10 @@ export function withMode (mode) {
* @returns {Response|undefined}
*/
return (request, env, ctx) => {
const enabled = () => {
const currentMode = env.MODE
const currentModeBits = modeBits(currentMode)
const currentMode = env.MODE
const currentModeBits = modeBits(currentMode)

const enabled = () => {
return modeBits(mode).every((bit, i) => {
if (bit === '-') {
return true
Expand All @@ -78,6 +78,10 @@ export function withMode (mode) {

// Not enabled, use maintenance handler.
if (!enabled() && !modeSkip()) {
const isAfterSunsetStart = env.NEXT_PUBLIC_W3UP_LAUNCH_SUNSET_START ? (env.NEXT_PUBLIC_W3UP_LAUNCH_SUNSET_START < new Date().toISOString()) : false
if (isAfterSunsetStart && (currentMode === READ_ONLY)) {
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.')
}
return maintenanceHandler()
}
}
Expand Down
6 changes: 6 additions & 0 deletions packages/api/test/maintenance.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ describe('maintenance middleware', () => {
assert.throws(() => block(() => { }, {
MODE: NO_READ_OR_WRITE
}), /API undergoing maintenance/)

// after product sunset, READ_ONLY means FeatureHasBeenSunset
assert.throws(() => block(() => { }, {
MODE: READ_ONLY,
NEXT_PUBLIC_W3UP_LAUNCH_SUNSET_START: (new Date(0)).toISOString()
}), /FeatureHasBeenSunset/)
})

it('should bypass maintenance mode with a allowed token', async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/website/components/w3up-launch.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export const W3upMigrationRecommendationCopy = ({ sunsetStartDate }) => {
const sunsetDateFormatter = new Intl.DateTimeFormat(undefined, { dateStyle: 'long' });
return (
<>
This web3.storage product will sunset on {sunsetDateFormatter.format(sunsetStartDate)}. We recommend migrating
your usage of web3.storage to the new web3.storage.
This web3.storage product sunset for new uploads on {sunsetDateFormatter.format(sunsetStartDate)}. To continue
uploading, migrate to the new web3.storage API.
<br />
<a href={createNewAccountHref}>Click here to create a new account</a> and&nbsp;
<a href={learnWhatsNewHref}>here to read about what’s awesome</a> about the new web3.storage experience.
Expand Down

0 comments on commit 8c29cec

Please sign in to comment.