1
- const { GraphQLScalarType, Kind, GraphQLError } = require ( 'graphql' )
1
+ const { GraphQLScalarType, Kind } = require ( 'graphql' )
2
2
const { getGraphQLValueError } = require ( './util' )
3
3
4
4
const ERROR_NON_STRING_VALUE = 'Binary cannot represent non string value'
5
- const ERROR_NON_BASE64_OR_BASE64URL = 'Binary values must be base64 or base64url encoded and normalized strings'
6
5
7
- const _normalizeBase64 = value => ( Buffer . isBuffer ( value ) ? value : Buffer . from ( value , 'base64' ) ) . toString ( 'base64' )
8
-
9
- const _validateBase64String = ( value , buffer , valueNode ) => {
10
- const base64Value = _toBase64 ( value )
11
- const normalized = _normalizeBase64 ( buffer )
12
- if ( _stripPadding ( base64Value ) !== _stripPadding ( normalized ) || base64Value . length > normalized . length )
13
- throw new GraphQLError ( ERROR_NON_BASE64_OR_BASE64URL , valueNode )
6
+ const serialize = value => {
7
+ // Normalize to base64url string
8
+ const buffer = Buffer . isBuffer ( value ) ? value : Buffer . from ( value , 'base64' )
9
+ const base64url = buffer . toString ( 'base64url' )
10
+ // Buffer base64url encoding does not have padding by default -> add it
11
+ return base64url . padEnd ( Math . ceil ( base64url . length / 4 ) * 4 , '=' )
14
12
}
15
13
16
- const _toBase64 = value => value . replace ( / _ / g, '/' ) . replace ( / - / g, '+' )
17
- const _toBase64Url = value => value . replace ( / \/ / g, '_' ) . replace ( / \+ / g, '-' )
18
- const _stripPadding = value => value . replace ( / = / g, '' )
19
-
20
- const serialize = outputValue => _toBase64Url ( _normalizeBase64 ( outputValue ) )
21
-
22
14
const parseValue = inputValue => {
23
15
if ( typeof inputValue !== 'string' ) throw getGraphQLValueError ( ERROR_NON_STRING_VALUE , inputValue )
24
16
25
- const buffer = Buffer . from ( inputValue , 'base64' )
26
- _validateBase64String ( inputValue , buffer )
27
-
28
- return buffer
17
+ return Buffer . from ( inputValue , 'base64' )
29
18
}
30
19
31
20
const parseLiteral = valueNode => {
@@ -36,10 +25,7 @@ const parseLiteral = valueNode => {
36
25
// WORKAROUND: value could have already been parsed to a Buffer, necessary because of manual parsing in enrich AST
37
26
if ( Buffer . isBuffer ( value ) ) return value
38
27
39
- const buffer = Buffer . from ( value , 'base64' )
40
- _validateBase64String ( value , buffer , valueNode )
41
-
42
- return buffer
28
+ return Buffer . from ( value , 'base64' )
43
29
}
44
30
45
31
module . exports = new GraphQLScalarType ( {
0 commit comments