@@ -9,23 +9,16 @@ import {
99} from 'graphql' ;
1010
1111type BufferJson = { type : 'Buffer' ; data : number [ ] } ;
12- const base64Validator =
13- / ^ (?: [ A - Z a - z 0 - 9 + \/ ] { 4 } ) * (?: [ A - Z a - z 0 - 9 + \/ ] { 2 } = = | [ A - Z a - z 0 - 9 + \/ ] { 3 } = ) ? $ / ;
12+ const base64Validator = / ^ (?: [ A - Z a - z 0 - 9 + \/ ] { 4 } ) * (?: [ A - Z a - z 0 - 9 + \/ ] { 2 } = = | [ A - Z a - z 0 - 9 + \/ ] { 3 } = ) ? $ / ;
1413function hexValidator ( value : string ) {
1514 // Ensure that any leading 0 is removed from the hex string to avoid false negatives.
1615 const sanitizedValue = value . charAt ( 0 ) === '0' ? value . slice ( 1 ) : value ;
1716 // For larger strings, we run into issues with MAX_SAFE_INTEGER, so split the string
1817 // into smaller pieces to avoid this issue.
1918 if ( value . length > 8 ) {
2019 let parsedString = '' ;
21- for (
22- let startIndex = 0 , endIndex = 8 ;
23- startIndex < value . length ;
24- startIndex += 8 , endIndex += 8
25- ) {
26- parsedString += parseInt ( value . slice ( startIndex , endIndex ) , 16 ) . toString (
27- 16 ,
28- ) ;
20+ for ( let startIndex = 0 , endIndex = 8 ; startIndex < value . length ; startIndex += 8 , endIndex += 8 ) {
21+ parsedString += parseInt ( value . slice ( startIndex , endIndex ) , 16 ) . toString ( 16 ) ;
2922 }
3023 return parsedString === sanitizedValue ;
3124 }
@@ -34,19 +27,13 @@ function hexValidator(value: string) {
3427
3528function validate ( value : Buffer | string | BufferJson ) {
3629 if ( typeof value !== 'string' && ! ( value instanceof global . Buffer ) ) {
37- throw new TypeError (
38- `Value is not an instance of Buffer: ${ JSON . stringify ( value ) } ` ,
39- ) ;
30+ throw new TypeError ( `Value is not an instance of Buffer: ${ JSON . stringify ( value ) } ` ) ;
4031 }
4132 if ( typeof value === 'string' ) {
4233 const isBase64 = base64Validator . test ( value ) ;
4334 const isHex = hexValidator ( value ) ;
4435 if ( ! isBase64 && ! isHex ) {
45- throw new TypeError (
46- `Value is not a valid base64 or hex encoded string: ${ JSON . stringify (
47- value ,
48- ) } `,
49- ) ;
36+ throw new TypeError ( `Value is not a valid base64 or hex encoded string: ${ JSON . stringify ( value ) } ` ) ;
5037 }
5138 return global . Buffer . from ( value , isHex ? 'hex' : 'base64' ) ;
5239 }
@@ -57,25 +44,13 @@ function validate(value: Buffer | string | BufferJson) {
5744function parseObject ( ast : ObjectValueNode ) {
5845 const key = ast . fields [ 0 ] . value ;
5946 const value = ast . fields [ 1 ] . value ;
60- if (
61- ast . fields . length === 2 &&
62- key . kind === Kind . STRING &&
63- key . value === 'Buffer' &&
64- value . kind === Kind . LIST
65- ) {
66- return global . Buffer . from (
67- value . values . map ( ( astValue : IntValueNode ) => parseInt ( astValue . value ) ) ,
68- ) ;
47+ if ( ast . fields . length === 2 && key . kind === Kind . STRING && key . value === 'Buffer' && value . kind === Kind . LIST ) {
48+ return global . Buffer . from ( value . values . map ( ( astValue : IntValueNode ) => parseInt ( astValue . value ) ) ) ;
6949 }
70- throw new TypeError (
71- `Value is not a JSON representation of Buffer: ${ print ( ast ) } ` ,
72- ) ;
50+ throw new TypeError ( `Value is not a JSON representation of Buffer: ${ print ( ast ) } ` ) ;
7351}
7452
75- export const GraphQLByteConfig : GraphQLScalarTypeConfig <
76- Buffer | string | BufferJson ,
77- Buffer
78- > = /*#__PURE__*/ {
53+ export const GraphQLByteConfig : GraphQLScalarTypeConfig < Buffer | string | BufferJson , Buffer > = /*#__PURE__*/ {
7954 name : 'Byte' ,
8055 description : 'The `Byte` scalar type represents byte value as a Buffer' ,
8156 serialize : validate ,
@@ -87,15 +62,16 @@ export const GraphQLByteConfig: GraphQLScalarTypeConfig<
8762 case Kind . OBJECT :
8863 return parseObject ( ast ) ;
8964 default :
90- throw new TypeError (
91- `Can only parse base64 or hex encoded strings as Byte, but got a: ${ ast . kind } ` ,
92- ) ;
65+ throw new TypeError ( `Can only parse base64 or hex encoded strings as Byte, but got a: ${ ast . kind } ` ) ;
9366 }
9467 } ,
9568 extensions : {
9669 codegenScalarType : 'Buffer | string' ,
70+ jsonSchema : {
71+ type : 'string' ,
72+ format : 'byte' ,
73+ } ,
9774 } ,
9875} ;
9976
100- export const GraphQLByte : GraphQLScalarType =
101- /*#__PURE__*/ new GraphQLScalarType ( GraphQLByteConfig ) ;
77+ export const GraphQLByte : GraphQLScalarType = /*#__PURE__*/ new GraphQLScalarType ( GraphQLByteConfig ) ;
0 commit comments