Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: api maintenance message is a FeatureHasBeenSunsetError when env.MODE is READ_ONLY #2346

Merged
merged 7 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: the 'api' package CI already uses node 18 to test and doesn't test on 16 anyway

"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
Loading