forked from processcomplete/openbadges-bakery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
159 lines (133 loc) · 3.63 KB
/
index.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
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
/*
* openbadges-bakery
* https://github.com/mozilla/openbadges-bakery
*
* Copyright (c) 2012 Mozilla Foundation
* Licensed under the MPL 2.0 license.
*/
const util = require('util');
const request = require('request');
const urlutil = require('url');
const jws = require('jws')
const typeCheck = require('./lib/stream-type-check')
const png = require('./lib/png')
const svg = require('./lib/svg')
module.exports = {
bake: bake,
extract: extract,
debake: debake,
getRemoteAssertion: debake,
getAssertion: debake,
typeCheck: typeCheck,
}
var bakeries = {
'image/png': png.bake,
'image/svg+xml': svg.bake,
'unknown': unknownImageType
}
var extractors = {
'image/png': png.extract,
'image/svg+xml': svg.extract,
'unknown': unknownImageType
}
function unknownImageType(opts, callback) {
return callback(new Error('Unknown/unhandled image type'))
}
function bake(options, callback) {
typeCheck(options.image, function (err, type, stream) {
if (err) return callback(err)
options.image = stream
return bakeries[type](options, callback)
})
}
function extract(imgdata, callback) {
typeCheck(imgdata, function (err, type, stream) {
if (err) return callback(err)
return extractors[type](stream, callback)
})
}
function debake(image, callback) {
function defer(fn) {
return (global.setImmediate || process.nextTick)(fn)
}
extract(image, function (error, data) {
if (error)
return defer(function(){ callback(error) })
var url = data
var assertion
var decoded
// signature?
if ((decoded = jws.decode(data))) {
try {
assertion = JSON.parse(decoded.payload)
return defer(function(){ callback(null, assertion) })
} catch (e) {
return defer(function(){ callback(errors.jsonParse(e)) })
}
}
// assertion?
try {
assertion = JSON.parse(data)
return defer(function(){ callback(null, assertion) })
} catch (_) {}
// fall back to url
request(url, function (error, response, body) {
if (error) {
error = errors.request(error, url)
return callback(error)
}
const status = response.statusCode
const type = response.headers['content-type']
if (status != 200) {
error = (errors[status] || errors.generic)(status, url)
return callback(error)
}
return parseResponse(body, url, callback)
})
})
}
function parseResponse(body, url, callback) {
var error, obj
if (typeof body !== 'string')
return callback(null, body)
try {
obj = JSON.parse(body)
}
catch (original) {
error = errors.jsonParse(original, url)
return callback(error)
}
return callback(null, obj);
}
const errors = {
request: function (original, url) {
var msg = util.format('There was an error initiating the request: %s', original.message);
var error = new Error(msg);
error.code = 'REQUEST_ERROR';
error.original = original;
error.url = url;
return error;
},
generic: function (status, url) {
var msg = util.format('There was a problem retrieving the remote resource (%s)', status);
var error = new Error(msg);
error.code = 'RESOURCE_ERROR';
error.url = url;
error.httpStatusCode = status;
return error;
},
jsonParse: function (original, url) {
var error = new Error('Could not parse JSON');
error.code = 'JSON_PARSE_ERROR';
error.url = url;
error.original = original;
return error;
},
404: function (response, url) {
var error = new Error('Could not get resource (404)');
error.code = 'RESOURCE_NOT_FOUND';
error.url = url;
error.httpStatusCode = 404;
return error;
}
}