Skip to content

Commit d72c475

Browse files
committed
Add support for returning UInt8Array
1 parent 593f73e commit d72c475

File tree

5 files changed

+41
-25
lines changed

5 files changed

+41
-25
lines changed

src/driver/BaseDriver.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,11 @@ export abstract class BaseDriver {
132132
const shouldTransform = (this.useClassTransformer && result != null) // transform only if enabled and value exist
133133
&& result instanceof Object // don't transform primitive types (string/number/boolean)
134134
&& !(
135-
result instanceof Buffer // don't transform binary data
135+
result instanceof Uint8Array // don't transform binary data
136136
||
137137
result.pipe instanceof Function // don't transform streams
138138
);
139-
139+
140140
// transform result if needed
141141
if (shouldTransform) {
142142
const options = action.responseClassTransformOptions || this.classToPlainTransformOptions;

src/driver/express/ExpressDriver.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,11 @@ export class ExpressDriver extends BaseDriver {
294294
}
295295
options.next();
296296
}
297-
else if (result instanceof Buffer) {
298-
options.response.send(result);
297+
else if (result instanceof Buffer) { // check if it's binary data (Buffer)
298+
options.response.end(result, "binary");
299+
}
300+
else if (result instanceof Uint8Array) { // check if it's binary data (typed array)
301+
options.response.end(Buffer.from(result as any), "binary");
299302
}
300303
else if (result.pipe instanceof Function) {
301304
result.pipe(options.response);

src/driver/koa/KoaDriver.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,7 @@ export class KoaDriver extends BaseDriver {
223223
} else if (action.successHttpCode) {
224224
options.response.status = action.successHttpCode;
225225
}
226-
227-
// apply http headers
228-
Object.keys(action.headers).forEach(name => {
229-
options.response.set(name, action.headers[name]);
230-
});
231-
226+
232227
if (action.redirect) { // if redirect is set then do it
233228
if (typeof result === "string") {
234229
options.response.redirect(result);
@@ -237,17 +232,12 @@ export class KoaDriver extends BaseDriver {
237232
} else {
238233
options.response.redirect(action.redirect);
239234
}
240-
241-
return options.next();
242-
243235
} else if (action.renderedTemplate) { // if template is set then render it // TODO: not working in koa
244236
const renderOptions = result && result instanceof Object ? result : {};
245-
237+
246238
this.koa.use(async function (ctx: any, next: any) {
247239
await ctx.render(action.renderedTemplate, renderOptions);
248240
});
249-
250-
return options.next();
251241
}
252242
else if (result === undefined) { // throw NotFoundError on undefined response
253243
const notFoundError = new NotFoundError();
@@ -270,18 +260,24 @@ export class KoaDriver extends BaseDriver {
270260
} else {
271261
options.response.status = 204;
272262
}
273-
274-
return options.next();
263+
}
264+
else if (result instanceof Uint8Array) { // check if it's binary data (typed array)
265+
options.response.body = Buffer.from(result as any);
275266
}
276267
else { // send regular result
277268
if (result instanceof Object) {
278269
options.response.body = result;
279270
} else {
280271
options.response.body = result;
281272
}
282-
283-
return options.next();
284273
}
274+
275+
// apply http headers
276+
Object.keys(action.headers).forEach(name => {
277+
options.response.set(name, action.headers[name]);
278+
});
279+
280+
return options.next();
285281
}
286282

287283
/**

test/functional/action-params.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ describe("action parameters", () => {
412412
});
413413
assertRequest([3002], "get", "state", response => {
414414
expect(response).to.be.status(200);
415-
expect(response).to.have.header("content-type", "application/json; charset=utf-8");
415+
expect(response).to.have.header("content-type", "application/json");
416416
expect(response.body.username).to.be.equal("pleerock");
417417
});
418418
assertRequest([3002], "get", "state/username", response => {

test/functional/special-result-send.spec.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {assertRequest} from "./test-utils";
77
import {InterceptorInterface} from "../../src/InterceptorInterface";
88
import {Interceptor} from "../../src/decorator/Interceptor";
99
import {UseInterceptor} from "../../src/decorator/UseInterceptor";
10-
import {Controller} from "../../src/decorator/Controller";
10+
import {JsonController} from "../../src/decorator/JsonController";
1111
import {Get} from "../../src/decorator/Get";
1212
import {Action} from "../../src/Action";
1313
import {ContentType} from "../../src/decorator/ContentType";
@@ -23,19 +23,27 @@ describe("special result value treatment", () => {
2323
// reset metadata args storage
2424
getMetadataArgsStorage().reset();
2525

26-
@Controller()
26+
@JsonController()
2727
class HandledController {
2828

2929
@Get("/stream")
3030
@ContentType("text/plain")
3131
getStream() {
3232
return createReadStream(path.resolve(__dirname, "../../../../test/resources/sample-text-file.txt"));
3333
}
34-
34+
3535
@Get("/buffer")
36+
@ContentType("application/octet-stream")
3637
getBuffer() {
3738
return new Buffer(rawData);
3839
}
40+
41+
@Get("/array")
42+
@ContentType("application/octet-stream")
43+
getUIntArray() {
44+
return new Uint8Array(rawData);
45+
}
46+
3947
}
4048

4149
});
@@ -56,11 +64,20 @@ describe("special result value treatment", () => {
5664
});
5765
});
5866

59-
describe("should send raw binary data", () => {
67+
describe("should send raw binary data from Buffer", () => {
6068
assertRequest([3001, 3002], "get", "buffer", response => {
6169
expect(response).to.be.status(200);
6270
expect(response).to.have.header("content-type", "application/octet-stream");
6371
expect(response.body).to.be.equal(new Buffer(rawData).toString());
6472
});
6573
});
74+
75+
describe("should send raw binary data from UIntArray", () => {
76+
assertRequest([3001, 3002], "get", "array", response => {
77+
expect(response).to.be.status(200);
78+
expect(response).to.have.header("content-type", "application/octet-stream");
79+
expect(response.body).to.be.equal(Buffer.from(rawData).toString());
80+
});
81+
});
82+
6683
});

0 commit comments

Comments
 (0)