apache-js-workers
Advanced tools
Comparing version 0.0.14 to 0.0.16
24
index.js
@@ -45,17 +45,19 @@ "use strict"; | ||
this.statusCode = 200; | ||
this.headers = {}; | ||
} | ||
Res.prototype.addHeaders = function (headers) { | ||
for (var name in headers) { | ||
var value = headers[name]; | ||
worker_threads_1.parentPort.postMessage({ type: 'set-header', name: name, value: value }); | ||
} | ||
}; | ||
Res.prototype.writeHead = function (statusCode, headers) { | ||
this.addHeaders(headers); | ||
this.statusCode = statusCode; | ||
worker_threads_1.parentPort.postMessage({ type: 'set-status-code', statusCode: statusCode }); | ||
}; | ||
Res.prototype.write = function (body) { | ||
worker_threads_1.parentPort.postMessage({ | ||
type: 'write', | ||
body: body | ||
}); | ||
worker_threads_1.parentPort.postMessage({ type: 'write', body: body }); | ||
}; | ||
Res.prototype.send = function (body) { | ||
worker_threads_1.parentPort.postMessage({ | ||
type: 'response', | ||
statusCode: this.statusCode, | ||
headers: this.headers, | ||
body: body | ||
}); | ||
worker_threads_1.parentPort.postMessage({ type: 'response', body: body }); | ||
}; | ||
@@ -62,0 +64,0 @@ return Res; |
62
index.ts
@@ -29,49 +29,59 @@ import { workerData, parentPort } from 'worker_threads' | ||
export type PostMessage = ResponsePostMessage | WritePostMessage | LogPostMessage | ||
export type PostMessage = ResponseMessage | WriteMessage | LogMessage | SetHeaderMessage | SetStatusCodeMessage | ||
export interface ResponsePostMessage { | ||
export interface ResponseMessage { | ||
type: 'response' | ||
statusCode: number | ||
headers: { | ||
[ keys: string ]: string | ||
} | ||
body: any | ||
} | ||
export interface WritePostMessage { | ||
type: 'write', | ||
export interface WriteMessage { | ||
type: 'write' | ||
body: any | ||
} | ||
export interface LogPostMessage { | ||
type: 'log', | ||
level: LogLevel, | ||
export interface LogMessage { | ||
type: 'log' | ||
level: LogLevel | ||
message: string | ||
} | ||
export interface SetHeaderMessage { | ||
type: 'set-header' | ||
name: string | ||
value: string | ||
} | ||
export interface SetStatusCodeMessage { | ||
type: 'set-status-code' | ||
statusCode: number | ||
} | ||
export class Res { | ||
statusCode: number | ||
headers: { | ||
[ keys: string ]: string | ||
} | ||
constructor() { | ||
this.statusCode = 200 | ||
this.headers = {} | ||
} | ||
addHeaders(headers: { [ keys: string ]: string }) { | ||
for (let name in headers) { | ||
const value = headers[name] | ||
parentPort.postMessage({ type: 'set-header', name, value } as SetHeaderMessage) | ||
} | ||
} | ||
writeHead(statusCode: number, headers: { [ keys: string ]: string }) { | ||
this.addHeaders(headers) | ||
this.statusCode = statusCode | ||
parentPort.postMessage({ type: 'set-status-code', statusCode } as SetStatusCodeMessage) | ||
} | ||
write(body: any) { | ||
parentPort.postMessage({ | ||
type: 'write', | ||
body | ||
} as WritePostMessage) | ||
parentPort.postMessage({ type: 'write', body } as WriteMessage) | ||
} | ||
send(body?: any) { | ||
parentPort.postMessage({ | ||
type: 'response', | ||
statusCode: this.statusCode, | ||
headers: this.headers, | ||
body | ||
} as ResponsePostMessage) | ||
parentPort.postMessage({ type: 'response', body } as ResponseMessage) | ||
} | ||
@@ -87,3 +97,3 @@ } | ||
parentPort.postMessage({ type: 'log', level, message } as LogPostMessage) | ||
parentPort.postMessage({ type: 'log', level, message } as LogMessage) | ||
} |
{ | ||
"name": "apache-js-workers", | ||
"version": "0.0.14", | ||
"version": "0.0.16", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6361
159