@@ -98,10 +98,119 @@ const fetchTmpl = `
9898* This file is a generated Typescript file for GRPC Gateway, DO NOT MODIFY
9999*/
100100
101+ /**
102+ * base64 encoder and decoder
103+ * Copied and adapted from https://github.com/protobufjs/protobuf.js/blob/master/lib/base64/index.js
104+ */
105+ // Base64 encoding table
106+ const b64 = new Array(64);
107+
108+ // Base64 decoding table
109+ const s64 = new Array(123);
110+
111+ // 65..90, 97..122, 48..57, 43, 47
112+ for (let i = 0; i < 64;)
113+ s64[b64[i] = i < 26 ? i + 65 : i < 52 ? i + 71 : i < 62 ? i - 4 : i - 59 | 43] = i++;
114+
115+ export function b64Encode(buffer: Uint8Array, start: number, end: number): string {
116+ let parts: string[] = null;
117+ const chunk = [];
118+ let i = 0, // output index
119+ j = 0, // goto index
120+ t; // temporary
121+ while (start < end) {
122+ const b = buffer[start++];
123+ switch (j) {
124+ case 0:
125+ chunk[i++] = b64[b >> 2];
126+ t = (b & 3) << 4;
127+ j = 1;
128+ break;
129+ case 1:
130+ chunk[i++] = b64[t | b >> 4];
131+ t = (b & 15) << 2;
132+ j = 2;
133+ break;
134+ case 2:
135+ chunk[i++] = b64[t | b >> 6];
136+ chunk[i++] = b64[b & 63];
137+ j = 0;
138+ break;
139+ }
140+ if (i > 8191) {
141+ (parts || (parts = [])).push(String.fromCharCode.apply(String, chunk));
142+ i = 0;
143+ }
144+ }
145+ if (j) {
146+ chunk[i++] = b64[t];
147+ chunk[i++] = 61;
148+ if (j === 1)
149+ chunk[i++] = 61;
150+ }
151+ if (parts) {
152+ if (i)
153+ parts.push(String.fromCharCode.apply(String, chunk.slice(0, i)));
154+ return parts.join("");
155+ }
156+ return String.fromCharCode.apply(String, chunk.slice(0, i));
157+ }
158+
159+ const invalidEncoding = "invalid encoding";
160+
161+ export function b64Decode(s: string): Uint8Array {
162+ const buffer = [];
163+ let offset = 0;
164+ let j = 0, // goto index
165+ t; // temporary
166+ for (let i = 0; i < s.length;) {
167+ let c = s.charCodeAt(i++);
168+ if (c === 61 && j > 1)
169+ break;
170+ if ((c = s64[c]) === undefined)
171+ throw Error(invalidEncoding);
172+ switch (j) {
173+ case 0:
174+ t = c;
175+ j = 1;
176+ break;
177+ case 1:
178+ buffer[offset++] = t << 2 | (c & 48) >> 4;
179+ t = c;
180+ j = 2;
181+ break;
182+ case 2:
183+ buffer[offset++] = (t & 15) << 4 | (c & 60) >> 2;
184+ t = c;
185+ j = 3;
186+ break;
187+ case 3:
188+ buffer[offset++] = (t & 3) << 6 | c;
189+ j = 0;
190+ break;
191+ }
192+ }
193+ if (j === 1)
194+ throw Error(invalidEncoding);
195+ return new Uint8Array(buffer);
196+ }
197+
198+ function b64Test(s: string): boolean {
199+ return /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(s);
200+ }
201+
101202export interface InitReq extends RequestInit {
102203 pathPrefix?: string
103204}
104205
206+ export function replacer(key: any, value: any): any {
207+ if(value && value.constructor === Uint8Array) {
208+ return b64Encode(value, 0, value.length);
209+ }
210+
211+ return value;
212+ }
213+
105214export function fetchReq<I, O>(path: string, init?: InitReq): Promise<O> {
106215 const {pathPrefix, ...req} = init || {}
107216
@@ -399,9 +508,9 @@ func buildInitReq(method data.Method) string {
399508 m := `method: "` + httpMethod + `"`
400509 fields := []string {m }
401510 if method .HTTPRequestBody == nil || * method .HTTPRequestBody == "*" {
402- fields = append (fields , "body: JSON.stringify(req)" )
511+ fields = append (fields , "body: JSON.stringify(req, fm.replacer )" )
403512 } else if * method .HTTPRequestBody != "" {
404- fields = append (fields , `body: JSON.stringify(req["` + * method .HTTPRequestBody + `"])` )
513+ fields = append (fields , `body: JSON.stringify(req["` + * method .HTTPRequestBody + `"], fm.replacer )` )
405514 }
406515
407516 return strings .Join (fields , ", " )
0 commit comments