-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathservice.js
More file actions
171 lines (160 loc) · 4.7 KB
/
service.js
File metadata and controls
171 lines (160 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { CID } from 'multiformats'
import { importCar } from './importer.js'
import { Response, Request } from './mock-server.js'
/**
* @param {Request} request
*/
const headers = ({ headers }) => ({
'Access-Control-Allow-Origin': headers.get('origin') || '*',
'Access-Control-Allow-Methods': 'GET,POST,DELETE,OPTIONS',
// Allow all future content Request headers to go back to browser
// such as Authorization (Bearer) or X-Client-Name-Version
'Access-Control-Allow-Headers':
headers.get('Access-Control-Request-Headers') || '',
'Content-Type': 'application/json;charset=UTF-8',
})
/**
* @param {Request} request
*/
const importUpload = async (request) => {
const contentType = request.headers.get('content-type') || ''
const isCar =
contentType.includes('application/car') ||
contentType.includes('application/vnd.ipld.car')
if (!isCar) {
throw new Error(`unexpected content type: ${contentType}`)
}
const content = await request.arrayBuffer()
return await importCar(new Uint8Array(content))
}
/**
* @typedef {{AUTH_TOKEN:string, store: Map<string, any>}} State
* @param {string} [token]
* @param {Map<string, any>} [store]
* @returns {State}
*/
export const init = (
token = Math.random().toString(32).slice(2),
store = new Map()
) => ({
AUTH_TOKEN: token,
store,
})
/**
* @param {Request} request
* @param {State} state
*/
export const handle = async (request, { store, AUTH_TOKEN }) => {
const url = new URL(request.url)
const [_, ...pathParts] = url.pathname.split('/')
const auth = request.headers.get('authorization')
const [, token] = (auth && auth.match(/Bearer (.+)/)) || []
// If preflight
if (request.method === 'OPTIONS') {
return new Response('', { headers: headers(request) })
}
const authorize = () => {
// If not authorized 401
if (token !== AUTH_TOKEN) {
throw Object.assign(new Error('Unauthorized'), { status: 401 })
}
}
try {
switch (`${request.method} /${pathParts.join('/')}`) {
case 'POST /upload/':
case 'POST /upload': {
authorize()
const { cid } = await importUpload(request)
const key = `${token}:${cid}`
if (!store.get(key)) {
const created = new Date()
store.set(key, {
cid: cid.toString(),
deals: [],
pin: {
cid: cid.toString(),
status: 'pinned',
created,
},
created,
})
}
const result = { ok: true, value: { cid: cid.toString() } }
return new Response(JSON.stringify(result), {
headers: headers(request),
})
}
case `GET /check/${pathParts[1]}/`:
case `GET /check/${pathParts[1]}`: {
const cid = CID.parse(pathParts[1] || '')
const value = store.get(`${AUTH_TOKEN}:${cid}`)
if (!value) {
throw Object.assign(new Error(`not found: ${cid}`), { status: 404 })
}
return new Response(
JSON.stringify({
ok: true,
value: {
cid: cid.toString(),
pin: { status: value.pin.status },
deals: value.deals,
},
}),
{
headers: headers(request),
}
)
}
case `GET /${pathParts[0]}/`:
case `GET /${pathParts[0]}`: {
authorize()
const cid = CID.parse(pathParts[0] || '')
const value = store.get(`${token}:${cid}`)
const [status, result] = value
? [200, { ok: true, value }]
: [
404,
{
ok: false,
error: { message: `NFT with a CID ${cid} not found` },
},
]
return new Response(JSON.stringify(result), {
status,
headers: headers(request),
})
}
case `DELETE /${pathParts[0]}/`:
case `DELETE /${pathParts[0]}`: {
authorize()
const cid = CID.parse(pathParts[0] || '')
store.delete(`${token}:${cid}`)
return new Response(JSON.stringify({ ok: true }), {
headers: headers(request),
})
}
default: {
const result = {
ok: false,
error: { message: `No such API endpoint ${url.pathname}` },
}
return new Response(JSON.stringify(result), {
status: 404,
headers: headers(request),
})
}
}
} catch (err) {
const error = /** @type {Error & {status: number}} */ (err)
return new Response(
JSON.stringify({
ok: false,
error: { message: error.message || 'failed to handle request' },
}),
{
status: error.status || 500,
headers: headers(request),
}
)
}
}