-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
658 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
dist | ||
lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
node_modules | ||
*.log* | ||
dist | ||
lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
function getProxy (name: string, overrides: any = {}): any { | ||
const fn = function () { } | ||
fn.prototype.name = name | ||
const props: any = {} | ||
return new Proxy(fn, { | ||
get (_target, prop) { | ||
if (prop === 'caller') { return null } | ||
if (prop === '__createMock__') { return getProxy } | ||
if (prop in overrides) { return overrides[prop] } | ||
// @ts-ignore | ||
return (props[prop] = props[prop] || getProxy(`${name}.${prop.toString()}`)) | ||
}, | ||
apply (_target, _this, _args) { | ||
return getProxy(`${name}()`) | ||
}, | ||
construct (_target, _args, _newT) { | ||
return getProxy(`[${name}]`) as Object | ||
}, | ||
enumerate (_target) { | ||
return [] | ||
} | ||
}) | ||
} | ||
|
||
export default getProxy('mock') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default () => {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
export const METHODS = [ | ||
'ACL', 'BIND', 'CHECKOUT', | ||
'CONNECT', 'COPY', 'DELETE', | ||
'GET', 'HEAD', 'LINK', | ||
'LOCK', 'M-SEARCH', 'MERGE', | ||
'MKACTIVITY', 'MKCALENDAR', 'MKCOL', | ||
'MOVE', 'NOTIFY', 'OPTIONS', | ||
'PATCH', 'POST', 'PRI', | ||
'PROPFIND', 'PROPPATCH', 'PURGE', | ||
'PUT', 'REBIND', 'REPORT', | ||
'SEARCH', 'SOURCE', 'SUBSCRIBE', | ||
'TRACE', 'UNBIND', 'UNLINK', | ||
'UNLOCK', 'UNSUBSCRIBE' | ||
] | ||
|
||
export const STATUS_CODES = { | ||
100: 'Continue', | ||
101: 'Switching Protocols', | ||
102: 'Processing', | ||
103: 'Early Hints', | ||
200: 'OK', | ||
201: 'Created', | ||
202: 'Accepted', | ||
203: 'Non-Authoritative Information', | ||
204: 'No Content', | ||
205: 'Reset Content', | ||
206: 'Partial Content', | ||
207: 'Multi-Status', | ||
208: 'Already Reported', | ||
226: 'IM Used', | ||
300: 'Multiple Choices', | ||
301: 'Moved Permanently', | ||
302: 'Found', | ||
303: 'See Other', | ||
304: 'Not Modified', | ||
305: 'Use Proxy', | ||
307: 'Temporary Redirect', | ||
308: 'Permanent Redirect', | ||
400: 'Bad Request', | ||
401: 'Unauthorized', | ||
402: 'Payment Required', | ||
403: 'Forbidden', | ||
404: 'Not Found', | ||
405: 'Method Not Allowed', | ||
406: 'Not Acceptable', | ||
407: 'Proxy Authentication Required', | ||
408: 'Request Timeout', | ||
409: 'Conflict', | ||
410: 'Gone', | ||
411: 'Length Required', | ||
412: 'Precondition Failed', | ||
413: 'Payload Too Large', | ||
414: 'URI Too Long', | ||
415: 'Unsupported Media Type', | ||
416: 'Range Not Satisfiable', | ||
417: 'Expectation Failed', | ||
418: "I'm a Teapot", | ||
421: 'Misdirected Request', | ||
422: 'Unprocessable Entity', | ||
423: 'Locked', | ||
424: 'Failed Dependency', | ||
425: 'Too Early', | ||
426: 'Upgrade Required', | ||
428: 'Precondition Required', | ||
429: 'Too Many Requests', | ||
431: 'Request Header Fields Too Large', | ||
451: 'Unavailable For Legal Reasons', | ||
500: 'Internal Server Error', | ||
501: 'Not Implemented', | ||
502: 'Bad Gateway', | ||
503: 'Service Unavailable', | ||
504: 'Gateway Timeout', | ||
505: 'HTTP Version Not Supported', | ||
506: 'Variant Also Negotiates', | ||
507: 'Insufficient Storage', | ||
508: 'Loop Detected', | ||
509: 'Bandwidth Limit Exceeded', | ||
510: 'Not Extended', | ||
511: 'Network Authentication Required' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './consts' | ||
export * from './request' | ||
export * from './response' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import type http from 'http' | ||
import { Socket } from 'src/node/net/socket' | ||
import { Readable } from 'src/node/stream/readable' | ||
import { rawHeaders } from 'src/utils' | ||
|
||
// Docs: https://nodejs.org/api/http.html#http_class_http_incomingmessage | ||
// Implementation: https://github.com/nodejs/node/blob/master/lib/_http_incoming.js | ||
|
||
export class IncomingMessage extends Readable implements http.IncomingMessage { | ||
public aborted: boolean = false; | ||
public httpVersion: string = '1.1' | ||
public httpVersionMajor: number = 1 | ||
public httpVersionMinor: number = 1 | ||
public complete: boolean = true | ||
public connection: Socket | ||
public socket: Socket | ||
public headers: http.IncomingHttpHeaders = {} | ||
public trailers = {} | ||
public method: string = 'GET' | ||
public url: string = '/' | ||
public statusCode: number = 200 | ||
public statusMessage: string = '' | ||
|
||
readable: boolean = false | ||
|
||
constructor (socket?: Socket) { | ||
super() | ||
this.socket = this.connection = socket || new Socket() | ||
} | ||
|
||
get rawHeaders () { | ||
return rawHeaders(this.headers) | ||
} | ||
|
||
get rawTrailers () { | ||
return [] | ||
} | ||
|
||
setTimeout (_msecs: number, _callback?: () => void) { | ||
return this | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import type http from 'http' | ||
import type { Socket } from 'net' | ||
import { Callback, HeadersObject } from 'src/types' | ||
import { Writable } from 'src/node/stream/writable' | ||
|
||
// Docs: https://nodejs.org/api/http.html#http_class_http_serverresponse | ||
// Implementation: https://github.com/nodejs/node/blob/master/lib/_http_outgoing.js | ||
|
||
export class ServerResponse extends Writable implements http.ServerResponse { | ||
statusCode: number = 200; | ||
statusMessage: string = ''; | ||
upgrading: boolean = false; | ||
chunkedEncoding: boolean = false; | ||
shouldKeepAlive: boolean = false; | ||
useChunkedEncodingByDefault: boolean = false; | ||
sendDate: boolean = false; | ||
finished: boolean = false; | ||
headersSent: boolean = false; | ||
connection: Socket | null = null; | ||
socket: Socket | null = null; | ||
req: http.IncomingMessage | ||
|
||
_headers: HeadersObject = {} | ||
|
||
constructor (req: http.IncomingMessage) { | ||
super() | ||
this.req = req | ||
} | ||
|
||
assignSocket (socket: Socket): void { | ||
// @ts-ignore | ||
socket._httpMessage = this | ||
// socket.on('close', onServerResponseClose) | ||
this.socket = socket | ||
this.connection = socket | ||
this.emit('socket', socket) | ||
this._flush() | ||
} | ||
|
||
_flush () { | ||
this.flushHeaders() | ||
} | ||
|
||
detachSocket (_socket: Socket): void { | ||
} | ||
|
||
writeContinue (_callback?: Callback): void { | ||
} | ||
|
||
writeHead (statusCode: number, | ||
arg1?: string | http.OutgoingHttpHeaders | http.OutgoingHttpHeader[], | ||
arg2?: http.OutgoingHttpHeaders | http.OutgoingHttpHeader[]) { | ||
if (statusCode) { | ||
this.statusCode = statusCode | ||
} | ||
if (typeof arg1 === 'string') { | ||
this.statusMessage = arg1 | ||
arg1 = undefined | ||
} | ||
const headers = arg2 || arg1 | ||
if (headers) { | ||
if (Array.isArray(headers)) { | ||
// TODO: OutgoingHttpHeader[] | ||
} else { | ||
for (const key in headers) { | ||
// @ts-ignore | ||
this.setHeader(key, headers[key]) | ||
} | ||
} | ||
} | ||
this.headersSent = true | ||
return this | ||
} | ||
|
||
writeProcessing (): void { | ||
} | ||
|
||
setTimeout (_msecs: number, _callback?: Callback): this { | ||
return this | ||
} | ||
|
||
setHeader (name: string, value: number | string | ReadonlyArray<string>): void { | ||
this._headers[name.toLowerCase()] = value + '' | ||
} | ||
|
||
getHeader (name: string): number | string | string[] | undefined { | ||
return this._headers[name.toLowerCase()] | ||
} | ||
|
||
getHeaders (): http.OutgoingHttpHeaders { | ||
return this._headers | ||
} | ||
|
||
getHeaderNames (): string[] { | ||
return Object.keys(this._headers) | ||
} | ||
|
||
hasHeader (name: string): boolean { | ||
return name.toLowerCase() in this._headers | ||
} | ||
|
||
removeHeader (name: string): void { | ||
delete this._headers[name.toLowerCase()] | ||
} | ||
|
||
addTrailers (_headers: http.OutgoingHttpHeaders | ReadonlyArray<[string, string]>): void { | ||
} | ||
|
||
flushHeaders (): void { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './http' | ||
export * from './net' | ||
export * from './stream' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './socket' |
Oops, something went wrong.