@aneuhold/core-ts-api-lib 2.0.3 → 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,20 +9,35 @@ export type DOFunctionInput = object;
9
9
  */
10
10
  export type DOFunctionOutput = object;
11
11
  /**
12
- * Raw input to a Digital Ocean function will always have a data property
13
- * which is a string. This string must be parsed using EJSON.parse.
12
+ * Raw input to a Digital Ocean function. This is with the expectation that
13
+ * `web: raw` is set in the `project.yml` file.
14
14
  */
15
15
  export interface DOFunctionRawInput {
16
- /**
17
- * There are many other properties in the `http` object, but only the body
18
- * property is used.
19
- */
20
16
  http: {
21
17
  /**
22
- * The body property needs to be parsed with atob, then put through some
23
- * other processing.
18
+ * The body to be parsed or deserialized.
24
19
  */
25
20
  body: string;
21
+ headers: {
22
+ accept: string;
23
+ 'accept-encoding': string;
24
+ /**
25
+ * This is important.
26
+ */
27
+ 'content-type': string;
28
+ host: string;
29
+ 'user-agent': string;
30
+ 'x-forwarded-for': string;
31
+ 'x-forwarded-proto': string;
32
+ 'x-request-id': string;
33
+ };
34
+ /**
35
+ * This needs to be used to determine if the body is base64 encoded.
36
+ */
37
+ isBase64Encoded: boolean;
38
+ method: string;
39
+ path: string;
40
+ queryString: string;
26
41
  };
27
42
  }
28
43
  /**
@@ -36,8 +51,14 @@ export interface DOFunctionRawOutput {
36
51
  * The body is a base64 encoded string.
37
52
  */
38
53
  body: string;
54
+ statusCode: number;
39
55
  headers: {
40
- 'Content-Type': 'application/octet-stream';
56
+ /**
57
+ * This is different than what gets passed in. This is on purpose. DO
58
+ * functions changes the header name when being passed in, but not when
59
+ * being passed out.
60
+ */
61
+ 'Content-Type': string;
41
62
  };
42
63
  }
43
64
  /**
@@ -48,10 +69,6 @@ export interface DOFunctionCallOutput<TOutput extends DOFunctionOutput> {
48
69
  errors: string[];
49
70
  data: TOutput;
50
71
  }
51
- /**
52
- * An abstract class that can be extended to define a Digital Ocean
53
- * function.
54
- */
55
72
  /**
56
73
  * An abstract class representing a Digital Ocean Function.
57
74
  *
@@ -82,11 +99,11 @@ export default abstract class DOFunction<TInput extends DOFunctionInput, TOutput
82
99
  */
83
100
  call(input: TInput): Promise<DOFunctionCallOutput<TOutput>>;
84
101
  /**
85
- * Decodes an {@link ArrayBuffer} into a {@link DOFunctionCallOutput}.
102
+ * Decodes the response from the Digital Ocean function.
86
103
  *
87
- * @param buffer - The buffer to decode.
104
+ * @param response - The response to decode.
88
105
  * @returns The decoded output.
89
106
  */
90
- private decodeArrayBuffer;
107
+ private decodeResponse;
91
108
  }
92
109
  //# sourceMappingURL=DOFunction.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"DOFunction.d.ts","sourceRoot":"./src/","sources":["services/DOFunctionService/DOFunction.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC;AAErC;;;;GAIG;AACH,MAAM,MAAM,gBAAgB,GAAG,MAAM,CAAC;AAEtC;;;GAGG;AACH,MAAM,WAAW,kBAAkB;IACjC;;;OAGG;IACH,IAAI,EAAE;QACJ;;;WAGG;QACH,IAAI,EAAE,MAAM,CAAC;KACd,CAAC;CACH;AAED;;;;;GAKG;AACH,MAAM,WAAW,mBAAmB;IAClC;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE;QACP,cAAc,EAAE,0BAA0B,CAAC;KAC5C,CAAC;CACH;AAED;;GAEG;AACH,MAAM,WAAW,oBAAoB,CAAC,OAAO,SAAS,gBAAgB;IACpE,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,IAAI,EAAE,OAAO,CAAC;CACf;AAED;;;GAGG;AACH;;;;;GAKG;AACH,MAAM,CAAC,OAAO,CAAC,QAAQ,OAAO,UAAU,CACtC,MAAM,SAAS,eAAe,EAC9B,OAAO,SAAS,gBAAgB;IAEhC;;OAEG;IACH,SAAS,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC;IAEvB,SAAS,CAAC,YAAY,EAAE,MAAM,CAAC;IAE/B,SAAS;IAIT;;;;OAIG;IACH,MAAM,CAAC,GAAG,EAAE,MAAM;IAIlB;;;;;;;;OAQG;IACG,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,oBAAoB,CAAC,OAAO,CAAC,CAAC;IAgBjE;;;;;OAKG;IACH,OAAO,CAAC,iBAAiB;CAM1B"}
1
+ {"version":3,"file":"DOFunction.d.ts","sourceRoot":"./src/","sources":["services/DOFunctionService/DOFunction.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC;AAErC;;;;GAIG;AACH,MAAM,MAAM,gBAAgB,GAAG,MAAM,CAAC;AAEtC;;;GAGG;AACH,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE;QACJ;;WAEG;QACH,IAAI,EAAE,MAAM,CAAC;QACb,OAAO,EAAE;YACP,MAAM,EAAE,MAAM,CAAC;YACf,iBAAiB,EAAE,MAAM,CAAC;YAC1B;;eAEG;YACH,cAAc,EAAE,MAAM,CAAC;YACvB,IAAI,EAAE,MAAM,CAAC;YACb,YAAY,EAAE,MAAM,CAAC;YACrB,iBAAiB,EAAE,MAAM,CAAC;YAC1B,mBAAmB,EAAE,MAAM,CAAC;YAC5B,cAAc,EAAE,MAAM,CAAC;SACxB,CAAC;QACF;;WAEG;QACH,eAAe,EAAE,OAAO,CAAC;QACzB,MAAM,EAAE,MAAM,CAAC;QACf,IAAI,EAAE,MAAM,CAAC;QACb,WAAW,EAAE,MAAM,CAAC;KACrB,CAAC;CACH;AAED;;;;;GAKG;AACH,MAAM,WAAW,mBAAmB;IAClC;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE;QACP;;;;WAIG;QACH,cAAc,EAAE,MAAM,CAAC;KACxB,CAAC;CACH;AAED;;GAEG;AACH,MAAM,WAAW,oBAAoB,CAAC,OAAO,SAAS,gBAAgB;IACpE,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,IAAI,EAAE,OAAO,CAAC;CACf;AAED;;;;;GAKG;AACH,MAAM,CAAC,OAAO,CAAC,QAAQ,OAAO,UAAU,CACtC,MAAM,SAAS,eAAe,EAC9B,OAAO,SAAS,gBAAgB;IAEhC;;OAEG;IACH,SAAS,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC;IAEvB,SAAS,CAAC,YAAY,EAAE,MAAM,CAAC;IAE/B,SAAS;IAIT;;;;OAIG;IACH,MAAM,CAAC,GAAG,EAAE,MAAM;IAIlB;;;;;;;;OAQG;IACG,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,oBAAoB,CAAC,OAAO,CAAC,CAAC;IAgBjE;;;;;OAKG;YACW,cAAc;CAO7B"}
@@ -1,8 +1,4 @@
1
1
  import { BSON } from 'bson';
2
- /**
3
- * An abstract class that can be extended to define a Digital Ocean
4
- * function.
5
- */
6
2
  /**
7
3
  * An abstract class representing a Digital Ocean Function.
8
4
  *
@@ -39,26 +35,27 @@ export default class DOFunction {
39
35
  if (!this.url) {
40
36
  throw new Error(`${this.functionName} URL is not set`);
41
37
  }
42
- const result = await fetch(this.url, {
38
+ const body = Buffer.from(BSON.serialize(input)).toString('base64');
39
+ const response = await fetch(this.url, {
43
40
  method: 'POST',
44
41
  headers: {
42
+ Connection: 'keep-alive',
45
43
  'Content-Type': 'application/octet-stream'
46
44
  },
47
- // It isn't clear why this works by itself. It comes in to the function
48
- // as a base64 string.
49
- body: BSON.serialize(input)
45
+ body: body
50
46
  });
51
- return this.decodeArrayBuffer(await result.arrayBuffer());
47
+ return await this.decodeResponse(response);
52
48
  }
53
49
  /**
54
- * Decodes an {@link ArrayBuffer} into a {@link DOFunctionCallOutput}.
50
+ * Decodes the response from the Digital Ocean function.
55
51
  *
56
- * @param buffer - The buffer to decode.
52
+ * @param response - The response to decode.
57
53
  * @returns The decoded output.
58
54
  */
59
- decodeArrayBuffer(buffer) {
60
- const bytes = new Uint8Array(buffer);
61
- return BSON.deserialize(bytes);
55
+ async decodeResponse(response) {
56
+ const bodyText = await response.text();
57
+ const buffer = Buffer.from(bodyText, 'base64');
58
+ return BSON.deserialize(buffer);
62
59
  }
63
60
  }
64
61
  //# sourceMappingURL=DOFunction.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"DOFunction.js","sourceRoot":"./src/","sources":["services/DOFunctionService/DOFunction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAyD5B;;;GAGG;AACH;;;;;GAKG;AACH,MAAM,CAAC,OAAO,OAAgB,UAAU;IAItC;;OAEG;IACO,GAAG,CAAU;IAEb,YAAY,CAAS;IAE/B;QACE,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC;IAC5C,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,GAAW;QAChB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;IACjB,CAAC;IAED;;;;;;;;OAQG;IACH,KAAK,CAAC,IAAI,CAAC,KAAa;QACtB,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC;YACd,MAAM,IAAI,KAAK,CAAC,GAAG,IAAI,CAAC,YAAY,iBAAiB,CAAC,CAAC;QACzD,CAAC;QACD,MAAM,MAAM,GAAG,MAAM,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE;YACnC,MAAM,EAAE,MAAM;YACd,OAAO,EAAE;gBACP,cAAc,EAAE,0BAA0B;aAC3C;YACD,uEAAuE;YACvE,sBAAsB;YACtB,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC;SAC5B,CAAC,CAAC;QACH,OAAO,IAAI,CAAC,iBAAiB,CAAC,MAAM,MAAM,CAAC,WAAW,EAAE,CAAC,CAAC;IAC5D,CAAC;IAED;;;;;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B,MAAM,QAAQ,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;QACvC,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;QAC/C,OAAO,IAAI,CAAC,WAAW,CAAC,MAAM,CAAkC,CAAC;IACnE,CAAC;CACF"}
@@ -13,20 +13,35 @@ export type DOFunctionInput = object;
13
13
  export type DOFunctionOutput = object;
14
14
 
15
15
  /**
16
- * Raw input to a Digital Ocean function will always have a data property
17
- * which is a string. This string must be parsed using EJSON.parse.
16
+ * Raw input to a Digital Ocean function. This is with the expectation that
17
+ * `web: raw` is set in the `project.yml` file.
18
18
  */
19
19
  export interface DOFunctionRawInput {
20
- /**
21
- * There are many other properties in the `http` object, but only the body
22
- * property is used.
23
- */
24
20
  http: {
25
21
  /**
26
- * The body property needs to be parsed with atob, then put through some
27
- * other processing.
22
+ * The body to be parsed or deserialized.
28
23
  */
29
24
  body: string;
25
+ headers: {
26
+ accept: string;
27
+ 'accept-encoding': string;
28
+ /**
29
+ * This is important.
30
+ */
31
+ 'content-type': string;
32
+ host: string;
33
+ 'user-agent': string;
34
+ 'x-forwarded-for': string;
35
+ 'x-forwarded-proto': string;
36
+ 'x-request-id': string;
37
+ };
38
+ /**
39
+ * This needs to be used to determine if the body is base64 encoded.
40
+ */
41
+ isBase64Encoded: boolean;
42
+ method: string;
43
+ path: string;
44
+ queryString: string;
30
45
  };
31
46
  }
32
47
 
@@ -41,8 +56,14 @@ export interface DOFunctionRawOutput {
41
56
  * The body is a base64 encoded string.
42
57
  */
43
58
  body: string;
59
+ statusCode: number;
44
60
  headers: {
45
- 'Content-Type': 'application/octet-stream';
61
+ /**
62
+ * This is different than what gets passed in. This is on purpose. DO
63
+ * functions changes the header name when being passed in, but not when
64
+ * being passed out.
65
+ */
66
+ 'Content-Type': string;
46
67
  };
47
68
  }
48
69
 
@@ -55,10 +76,6 @@ export interface DOFunctionCallOutput<TOutput extends DOFunctionOutput> {
55
76
  data: TOutput;
56
77
  }
57
78
 
58
- /**
59
- * An abstract class that can be extended to define a Digital Ocean
60
- * function.
61
- */
62
79
  /**
63
80
  * An abstract class representing a Digital Ocean Function.
64
81
  *
@@ -102,28 +119,29 @@ export default abstract class DOFunction<
102
119
  if (!this.url) {
103
120
  throw new Error(`${this.functionName} URL is not set`);
104
121
  }
105
- const result = await fetch(this.url, {
122
+ const body = Buffer.from(BSON.serialize(input)).toString('base64');
123
+ const response = await fetch(this.url, {
106
124
  method: 'POST',
107
125
  headers: {
126
+ Connection: 'keep-alive',
108
127
  'Content-Type': 'application/octet-stream'
109
128
  },
110
- // It isn't clear why this works by itself. It comes in to the function
111
- // as a base64 string.
112
- body: BSON.serialize(input)
129
+ body: body
113
130
  });
114
- return this.decodeArrayBuffer(await result.arrayBuffer());
131
+ return await this.decodeResponse(response);
115
132
  }
116
133
 
117
134
  /**
118
- * Decodes an {@link ArrayBuffer} into a {@link DOFunctionCallOutput}.
135
+ * Decodes the response from the Digital Ocean function.
119
136
  *
120
- * @param buffer - The buffer to decode.
137
+ * @param response - The response to decode.
121
138
  * @returns The decoded output.
122
139
  */
123
- private decodeArrayBuffer(
124
- buffer: ArrayBuffer
125
- ): DOFunctionCallOutput<TOutput> {
126
- const bytes = new Uint8Array(buffer);
127
- return BSON.deserialize(bytes) as DOFunctionCallOutput<TOutput>;
140
+ private async decodeResponse(
141
+ response: Response
142
+ ): Promise<DOFunctionCallOutput<TOutput>> {
143
+ const bodyText = await response.text();
144
+ const buffer = Buffer.from(bodyText, 'base64');
145
+ return BSON.deserialize(buffer) as DOFunctionCallOutput<TOutput>;
128
146
  }
129
147
  }
@@ -39,7 +39,8 @@ export default class DOFunctionService {
39
39
  */
40
40
  private static deserializeInput;
41
41
  /**
42
- * Serializes the output object into a base64 string.
42
+ * Serializes the output object into a base64 string. This could be updated
43
+ * to support other serialization methods in the future.
43
44
  *
44
45
  * @param output - The output object to serialize.
45
46
  * @returns The serialized output as a base64 string.
@@ -1 +1 @@
1
- {"version":3,"file":"DOFunctionService.d.ts","sourceRoot":"./src/","sources":["services/DOFunctionService/DOFunctionService.ts"],"names":[],"mappings":"AACA,OAAO,EACL,oBAAoB,EACpB,eAAe,EACf,gBAAgB,EAChB,kBAAkB,EAClB,mBAAmB,EACpB,MAAM,iBAAiB,CAAC;AACzB,OAAO,iBAAiB,MAAM,kCAAkC,CAAC;AACjE,OAAO,gBAAgB,MAAM,iCAAiC,CAAC;AAC/D,OAAO,gBAAgB,MAAM,iCAAiC,CAAC;AAE/D;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,iBAAiB;IACpC;;OAEG;IACH,MAAM,CAAC,iBAAiB,EAAE,iBAAiB,CAAmC;IAE9E;;OAEG;IACH,MAAM,CAAC,gBAAgB,EAAE,gBAAgB,CAAkC;IAE3E;;OAEG;IACH,MAAM,CAAC,gBAAgB,EAAE,gBAAgB,CAAkC;IAE3E;;;;;;;;;;;OAWG;WACU,gBAAgB,CAC3B,MAAM,SAAS,eAAe,EAC9B,OAAO,SAAS,gBAAgB,EAEhC,QAAQ,EAAE,kBAAkB,EAC5B,OAAO,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,oBAAoB,CAAC,OAAO,CAAC,CAAC,GACjE,OAAO,CAAC,mBAAmB,CAAC;IAyB/B;;;;;OAKG;IACH,OAAO,CAAC,MAAM,CAAC,gBAAgB;IAY/B;;;;;OAKG;IACH,OAAO,CAAC,MAAM,CAAC,eAAe;CAM/B"}
1
+ {"version":3,"file":"DOFunctionService.d.ts","sourceRoot":"./src/","sources":["services/DOFunctionService/DOFunctionService.ts"],"names":[],"mappings":"AACA,OAAO,EACL,oBAAoB,EACpB,eAAe,EACf,gBAAgB,EAChB,kBAAkB,EAClB,mBAAmB,EACpB,MAAM,iBAAiB,CAAC;AACzB,OAAO,iBAAiB,MAAM,kCAAkC,CAAC;AACjE,OAAO,gBAAgB,MAAM,iCAAiC,CAAC;AAC/D,OAAO,gBAAgB,MAAM,iCAAiC,CAAC;AAE/D;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,iBAAiB;IACpC;;OAEG;IACH,MAAM,CAAC,iBAAiB,EAAE,iBAAiB,CAAmC;IAE9E;;OAEG;IACH,MAAM,CAAC,gBAAgB,EAAE,gBAAgB,CAAkC;IAE3E;;OAEG;IACH,MAAM,CAAC,gBAAgB,EAAE,gBAAgB,CAAkC;IAE3E;;;;;;;;;;;OAWG;WACU,gBAAgB,CAC3B,MAAM,SAAS,eAAe,EAC9B,OAAO,SAAS,gBAAgB,EAEhC,QAAQ,EAAE,kBAAkB,EAC5B,OAAO,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,oBAAoB,CAAC,OAAO,CAAC,CAAC,GACjE,OAAO,CAAC,mBAAmB,CAAC;IA0B/B;;;;;OAKG;IACH,OAAO,CAAC,MAAM,CAAC,gBAAgB;IA2B/B;;;;;;OAMG;IACH,OAAO,CAAC,MAAM,CAAC,eAAe;CAM/B"}
@@ -34,6 +34,7 @@ export default class DOFunctionService {
34
34
  const input = this.deserializeInput(rawInput);
35
35
  const rawOutput = {
36
36
  body: '',
37
+ statusCode: 200,
37
38
  headers: {
38
39
  'Content-Type': 'application/octet-stream'
39
40
  }
@@ -62,23 +63,38 @@ export default class DOFunctionService {
62
63
  * @returns The deserialized input object.
63
64
  */
64
65
  static deserializeInput(rawInput) {
65
- const binaryString = atob(rawInput.http.body);
66
- const len = binaryString.length;
67
- const bytes = new Uint8Array(len);
68
- for (let i = 0; i < len; i += 1) {
69
- bytes[i] = binaryString.charCodeAt(i);
66
+ const { http } = rawInput;
67
+ const { body, isBase64Encoded, headers } = http;
68
+ let decodedBody;
69
+ if (isBase64Encoded) {
70
+ decodedBody = Buffer.from(body, 'base64');
70
71
  }
71
- return BSON.deserialize(bytes);
72
+ else {
73
+ decodedBody = Buffer.from(body, 'utf8');
74
+ }
75
+ // Determine if the incoming content type is BSON
76
+ const isBson = headers['content-type'] === 'application/octet-stream';
77
+ let requestData;
78
+ if (isBson) {
79
+ // Deserialize BSON data
80
+ requestData = BSON.deserialize(decodedBody);
81
+ }
82
+ else {
83
+ // Parse JSON data
84
+ requestData = JSON.parse(decodedBody.toString('utf8'));
85
+ }
86
+ return requestData;
72
87
  }
73
88
  /**
74
- * Serializes the output object into a base64 string.
89
+ * Serializes the output object into a base64 string. This could be updated
90
+ * to support other serialization methods in the future.
75
91
  *
76
92
  * @param output - The output object to serialize.
77
93
  * @returns The serialized output as a base64 string.
78
94
  */
79
95
  static serializeOutput(output) {
80
- const uInt8Array = BSON.serialize(output);
81
- return Buffer.from(uInt8Array).toString('base64');
96
+ const bsonBuffer = BSON.serialize(output);
97
+ return Buffer.from(bsonBuffer).toString('base64');
82
98
  }
83
99
  }
84
100
  //# sourceMappingURL=DOFunctionService.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"DOFunctionService.js","sourceRoot":"./src/","sources":["services/DOFunctionService/DOFunctionService.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAQ5B,OAAO,iBAAiB,MAAM,kCAAkC,CAAC;AACjE,OAAO,gBAAgB,MAAM,iCAAiC,CAAC;AAC/D,OAAO,gBAAgB,MAAM,iCAAiC,CAAC;AAE/D;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,iBAAiB;IACpC;;OAEG;IACH,MAAM,CAAC,iBAAiB,GAAsB,iBAAiB,CAAC,WAAW,EAAE,CAAC;IAE9E;;OAEG;IACH,MAAM,CAAC,gBAAgB,GAAqB,gBAAgB,CAAC,WAAW,EAAE,CAAC;IAE3E;;OAEG;IACH,MAAM,CAAC,gBAAgB,GAAqB,gBAAgB,CAAC,WAAW,EAAE,CAAC;IAE3E;;;;;;;;;;;OAWG;IACH,MAAM,CAAC,KAAK,CAAC,gBAAgB,CAI3B,QAA4B,EAC5B,OAAkE;QAElE,MAAM,KAAK,GAAW,IAAI,CAAC,gBAAgB,CAAC,QAAQ,CAAC,CAAC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AAC,IAAI,CAAC,UAAU,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;IACpD,CAAC"}
@@ -51,6 +51,7 @@ export default class DOFunctionService {
51
51
  const input: TInput = this.deserializeInput(rawInput);
52
52
  const rawOutput: DOFunctionRawOutput = {
53
53
  body: '',
54
+ statusCode: 200,
54
55
  headers: {
55
56
  'Content-Type': 'application/octet-stream'
56
57
  }
@@ -81,17 +82,33 @@ export default class DOFunctionService {
81
82
  private static deserializeInput<TInput extends DOFunctionInput>(
82
83
  rawInput: DOFunctionRawInput
83
84
  ): TInput {
84
- const binaryString = atob(rawInput.http.body);
85
- const len = binaryString.length;
86
- const bytes = new Uint8Array(len);
87
- for (let i = 0; i < len; i += 1) {
88
- bytes[i] = binaryString.charCodeAt(i);
85
+ const { http } = rawInput;
86
+ const { body, isBase64Encoded, headers } = http;
87
+
88
+ let decodedBody: Buffer;
89
+ if (isBase64Encoded) {
90
+ decodedBody = Buffer.from(body, 'base64');
91
+ } else {
92
+ decodedBody = Buffer.from(body, 'utf8');
93
+ }
94
+
95
+ // Determine if the incoming content type is BSON
96
+ const isBson = headers['content-type'] === 'application/octet-stream';
97
+ let requestData: TInput;
98
+ if (isBson) {
99
+ // Deserialize BSON data
100
+ requestData = BSON.deserialize(decodedBody) as TInput;
101
+ } else {
102
+ // Parse JSON data
103
+ requestData = JSON.parse(decodedBody.toString('utf8')) as TInput;
89
104
  }
90
- return BSON.deserialize(bytes) as TInput;
105
+
106
+ return requestData;
91
107
  }
92
108
 
93
109
  /**
94
- * Serializes the output object into a base64 string.
110
+ * Serializes the output object into a base64 string. This could be updated
111
+ * to support other serialization methods in the future.
95
112
  *
96
113
  * @param output - The output object to serialize.
97
114
  * @returns The serialized output as a base64 string.
@@ -99,7 +116,7 @@ export default class DOFunctionService {
99
116
  private static serializeOutput<TOutput extends DOFunctionOutput>(
100
117
  output: DOFunctionCallOutput<TOutput>
101
118
  ): string {
102
- const uInt8Array = BSON.serialize(output);
103
- return Buffer.from(uInt8Array).toString('base64');
119
+ const bsonBuffer = BSON.serialize(output);
120
+ return Buffer.from(bsonBuffer).toString('base64');
104
121
  }
105
122
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@aneuhold/core-ts-api-lib",
3
3
  "author": "Anton G. Neuhold Jr.",
4
4
  "license": "MIT",
5
- "version": "2.0.3",
5
+ "version": "2.0.4",
6
6
  "description": "A library for interacting with the backend and defining the backend API for personal projects.",
7
7
  "packageManager": "yarn@4.5.1",
8
8
  "type": "module",