Merge pull request #290 from cloudflare/sven/nodeinfo-cors

Add CORS header to NodeInfo endpoints
pull/291/head
Sven Sauleau 2023-02-15 15:41:13 +00:00 zatwierdzone przez GitHub
commit 3a5bb32daa
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
7 zmienionych plików z 20 dodań i 5 usunięć

Wyświetl plik

@ -39,17 +39,21 @@ export function internalServerError(): Response {
}
export function statusNotFound(id: string): Response {
return generateErrorResponse('Resource not found', 404, `Status "${id}" not found`)
return resourceNotFound('status', id)
}
export function mediaNotFound(id: string): Response {
return generateErrorResponse('Resource not found', 404, `Media "${id}" not found`)
return resourceNotFound('media', id)
}
export function tagNotFound(tag: string): Response {
return generateErrorResponse('Resource not found', 404, `Tag "${tag}" not found`)
return resourceNotFound('tag', tag)
}
export function exceededLimit(detail: string): Response {
return generateErrorResponse('Limit exceeded', 400, detail)
}
export function resourceNotFound(name: string, id: string): Response {
return generateErrorResponse('Resource not found', 404, `${name} "${id}" not found`)
}

Wyświetl plik

@ -1,7 +1,7 @@
import { createSubscription } from '../../src/mastodon/subscription'
import { createPerson } from 'wildebeest/backend/src/activitypub/actors'
import { strict as assert } from 'node:assert/strict'
import { makeDB, createTestClient, generateVAPIDKeys } from '../utils'
import { makeDB, createTestClient, generateVAPIDKeys, assertCORS } from '../utils'
import * as subscription from 'wildebeest/functions/api/v1/push/subscription'
const userKEK = 'test_kek21'
@ -18,6 +18,7 @@ describe('Mastodon APIs', () => {
const res = await subscription.handleGetRequest(db, req, connectedActor, client.id, vapidKeys)
assert.equal(res.status, 404)
assertCORS(res)
})
test('get existing subscription', async () => {

Wyświetl plik

@ -2,6 +2,7 @@ import { strict as assert } from 'node:assert/strict'
import * as nodeinfo_21 from 'wildebeest/functions/nodeinfo/2.1'
import * as nodeinfo_20 from 'wildebeest/functions/nodeinfo/2.0'
import * as nodeinfo from 'wildebeest/functions/.well-known/nodeinfo'
import { assertCORS } from './utils'
const domain = 'example.com'
@ -9,6 +10,7 @@ describe('NodeInfo', () => {
test('well-known returns links', async () => {
const res = await nodeinfo.handleRequest(domain)
assert.equal(res.status, 200)
assertCORS(res)
const data = await res.json<any>()
assert.equal(data.links.length, 2)
@ -17,6 +19,7 @@ describe('NodeInfo', () => {
test('expose NodeInfo version 2.0', async () => {
const res = await nodeinfo_20.handleRequest()
assert.equal(res.status, 200)
assertCORS(res)
const data = await res.json<any>()
assert.equal(data.version, '2.0')
@ -25,6 +28,7 @@ describe('NodeInfo', () => {
test('expose NodeInfo version 2.1', async () => {
const res = await nodeinfo_21.handleRequest()
assert.equal(res.status, 200)
assertCORS(res)
const data = await res.json<any>()
assert.equal(data.version, '2.1')

Wyświetl plik

@ -1,6 +1,8 @@
import type { Env } from 'wildebeest/backend/src/types/env'
import { cors } from 'wildebeest/backend/src/utils/cors'
const headers = {
...cors(),
'content-type': 'application/json',
'cache-control': 'max-age=259200, public',
}

Wyświetl plik

@ -38,7 +38,7 @@ export async function handleGetRequest(
const subscription = await getSubscription(db, connectedActor, client)
if (subscription === null) {
return new Response('', { status: 404 })
return errors.resourceNotFound('subscription', clientId)
}
const vapidKey = VAPIDPublicKey(vapidKeys)

Wyświetl plik

@ -1,7 +1,9 @@
import type { Env } from 'wildebeest/backend/src/types/env'
import { WILDEBEEST_VERSION } from 'wildebeest/config/versions'
import { cors } from 'wildebeest/backend/src/utils/cors'
const headers = {
...cors(),
'content-type': 'application/json',
'cache-control': 'max-age=259200, public',
}

Wyświetl plik

@ -1,7 +1,9 @@
import type { Env } from 'wildebeest/backend/src/types/env'
import { cors } from 'wildebeest/backend/src/utils/cors'
import { WILDEBEEST_VERSION } from 'wildebeest/config/versions'
const headers = {
...cors(),
'content-type': 'application/json',
'cache-control': 'max-age=259200, public',
}