-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathucan-invocation.js
122 lines (105 loc) · 2.92 KB
/
ucan-invocation.js
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
import * as CAR from '@ucanto/transport/car'
import * as Link from 'multiformats/link'
import { AgentMessage } from '@web3-storage/upload-api'
import {
BadBodyError,
BadContentTypeError,
NoTokenError,
ExpectedBasicStringError,
NoValidTokenError,
} from './errors.js'
export const CONTENT_TYPE = CAR.contentType
/**
* @typedef {import('./types').UcanLogCtx} UcanLogCtx
* @typedef {import('./types').WorkflowCtx} WorkflowCtx
* @typedef {import('./types').ReceiptBlockCtx} ReceiptBlockCtx
* @typedef {import('./types').Workflow} Workflow
* @typedef {import('./types').ReceiptBlock} ReceiptBlock
*/
/**
* @param {import('aws-lambda').APIGatewayProxyEventV2} request
* @param {UcanLogCtx} ctx
*/
export async function processUcanLogRequest(request, ctx) {
const token = getTokenFromRequest(request)
if (token !== ctx.basicAuth) {
throw new NoValidTokenError('invalid Authorization credentials provided')
}
if (!request.body) {
throw new BadBodyError('service requests are required to have body')
}
const bytes = Buffer.from(request.body, 'base64')
const contentType = request.headers['content-type'] || ''
if (contentType === CONTENT_TYPE) {
const request = {
body: bytes,
headers: {
'content-type': contentType,
},
}
const message = await CAR.request.decode(request)
const result = await ctx.agentStore.messages.write({
source: request,
data: message,
index: AgentMessage.index(message)
})
if (result.error) {
throw result.error
}
return result.ok
}
throw new BadContentTypeError()
}
/**
* @param {import('aws-lambda').APIGatewayProxyEventV2} request
*/
function getTokenFromRequest(request) {
const authHeader = request.headers.authorization || ''
if (!authHeader) {
throw new NoTokenError('no Authorization header provided')
}
const token = parseAuthorizationHeader(authHeader)
return token
}
/**
* @param {string} header
*/
function parseAuthorizationHeader(header) {
if (!header.toLowerCase().startsWith('basic ')) {
throw new ExpectedBasicStringError('no basic Authorization header provided')
}
return header.slice(6)
}
/**
* @param {any} value
*/
export const replaceAllLinkValues = (value) => {
// Array with Links?
if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) {
if (Link.isLink(value[i])) {
value[i] = toJSON(value[i])
} else {
replaceAllLinkValues(value[i])
}
}
}
// Object with Links?
else if (typeof value === 'object') {
for (const key of Object.keys(value)) {
if (Link.isLink(value[key])) {
value[key] = toJSON(value[key])
}
replaceAllLinkValues(value[key])
}
}
return value
}
/**
* @template {import('multiformats').UnknownLink} Link
* @param {Link} link
*/
export const toJSON = (link) =>
/** @type {import('@web3-storage/upload-api').LinkJSON<Link>} */ ({
'/': link.toString(),
})