@mojojs/core 0.4.7 → 0.5.3
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.
- package/lib/body.d.ts +5 -4
- package/lib/body.js +4 -0
- package/lib/context.d.ts +2 -2
- package/lib/core.d.ts +1 -0
- package/lib/core.js +1 -0
- package/lib/mime.js +2 -1
- package/lib/plugins/yaml-config.d.ts +4 -0
- package/lib/plugins/yaml-config.js +21 -0
- package/lib/renderer.js +5 -0
- package/lib/user-agent/test.d.ts +1 -0
- package/lib/user-agent/test.js +5 -0
- package/lib/user-agent.js +6 -0
- package/package.json +9 -8
package/lib/body.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
/// <reference types="busboy" />
|
|
3
2
|
/// <reference types="cheerio" />
|
|
4
3
|
import type { JSONValue } from './types.js';
|
|
5
4
|
import type { IncomingHttpHeaders, IncomingMessage } from 'http';
|
|
6
5
|
import { Params } from './body/params.js';
|
|
6
|
+
import Busboy from 'busboy';
|
|
7
7
|
declare type BusboyFile = [string, NodeJS.ReadableStream, string, string, string];
|
|
8
8
|
interface FileUpload {
|
|
9
9
|
fieldname: string;
|
|
@@ -18,8 +18,8 @@ export declare class Body {
|
|
|
18
18
|
constructor(stream: IncomingMessage);
|
|
19
19
|
[Symbol.asyncIterator](): AsyncIterable<Buffer>;
|
|
20
20
|
buffer(): Promise<Buffer>;
|
|
21
|
-
files(options?:
|
|
22
|
-
form(options?:
|
|
21
|
+
files(options?: Busboy.BusboyConfig): AsyncIterableIterator<FileUpload>;
|
|
22
|
+
form(options?: Busboy.BusboyConfig): Promise<Params>;
|
|
23
23
|
get(name: string): string | undefined;
|
|
24
24
|
get headers(): IncomingHttpHeaders;
|
|
25
25
|
html(): Promise<cheerio.Root>;
|
|
@@ -28,8 +28,9 @@ export declare class Body {
|
|
|
28
28
|
pipe(writer: NodeJS.WritableStream): Promise<void>;
|
|
29
29
|
text(charset?: BufferEncoding): Promise<string>;
|
|
30
30
|
xml(): Promise<cheerio.Root>;
|
|
31
|
+
yaml(): Promise<unknown>;
|
|
31
32
|
_consumeBody(): Promise<Uint8Array[]>;
|
|
32
|
-
_formIterator(options?:
|
|
33
|
+
_formIterator(options?: Busboy.BusboyConfig): AsyncIterableIterator<BusboyFile>;
|
|
33
34
|
_isForm(): boolean;
|
|
34
35
|
get _params(): Params;
|
|
35
36
|
}
|
package/lib/body.js
CHANGED
|
@@ -2,6 +2,7 @@ import { on } from 'events';
|
|
|
2
2
|
import { Params } from './body/params.js';
|
|
3
3
|
import Busboy from 'busboy';
|
|
4
4
|
import cheerio from 'cheerio';
|
|
5
|
+
import yaml from 'js-yaml';
|
|
5
6
|
export class Body {
|
|
6
7
|
constructor(stream) {
|
|
7
8
|
this._form = undefined;
|
|
@@ -63,6 +64,9 @@ export class Body {
|
|
|
63
64
|
async xml() {
|
|
64
65
|
return cheerio.load(await this.buffer(), { xmlMode: true });
|
|
65
66
|
}
|
|
67
|
+
async yaml() {
|
|
68
|
+
return yaml.load((await this.buffer()).toString());
|
|
69
|
+
}
|
|
66
70
|
async _consumeBody() {
|
|
67
71
|
const chunks = [];
|
|
68
72
|
return await new Promise((resolve, reject) => {
|
package/lib/context.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
/// <reference types="busboy" />
|
|
3
2
|
import type { App } from './app.js';
|
|
4
3
|
import type { ChildLogger } from './logger.js';
|
|
5
4
|
import type { Plan } from './router/plan.js';
|
|
@@ -8,6 +7,7 @@ import type { UserAgent } from './user-agent.js';
|
|
|
8
7
|
import type { WebSocket } from './websocket.js';
|
|
9
8
|
import type Path from '@mojojs/path';
|
|
10
9
|
import type { ValidateFunction } from 'ajv';
|
|
10
|
+
import type { BusboyConfig } from 'busboy';
|
|
11
11
|
import type http from 'http';
|
|
12
12
|
import EventEmitter from 'events';
|
|
13
13
|
import { Params } from './body/params.js';
|
|
@@ -45,7 +45,7 @@ declare class Context extends EventEmitter {
|
|
|
45
45
|
get isWebSocket(): boolean;
|
|
46
46
|
json(fn: WebSocketHandler): this;
|
|
47
47
|
get models(): Record<string, any>;
|
|
48
|
-
params(options?:
|
|
48
|
+
params(options?: BusboyConfig): Promise<Params>;
|
|
49
49
|
plain(fn: WebSocketHandler): this;
|
|
50
50
|
redirectTo(target: string, options?: {
|
|
51
51
|
status?: number;
|
package/lib/core.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ import { App } from './app.js';
|
|
|
8
8
|
export declare const version: any;
|
|
9
9
|
export default function mojo(options?: AppOptions): App;
|
|
10
10
|
export { default as jsonConfigPlugin } from './plugins/json-config.js';
|
|
11
|
+
export { default as yamlConfigPlugin } from './plugins/yaml-config.js';
|
|
11
12
|
export { Logger } from './logger.js';
|
|
12
13
|
export { Server } from './server.js';
|
|
13
14
|
export { Session } from './session.js';
|
package/lib/core.js
CHANGED
|
@@ -19,6 +19,7 @@ export default function mojo(options) {
|
|
|
19
19
|
}
|
|
20
20
|
// "Professor: These old Doomsday devices are dangerously unstable. I'll rest easier not knowing where they are."
|
|
21
21
|
export { default as jsonConfigPlugin } from './plugins/json-config.js';
|
|
22
|
+
export { default as yamlConfigPlugin } from './plugins/yaml-config.js';
|
|
22
23
|
export { Logger } from './logger.js';
|
|
23
24
|
export { Server } from './server.js';
|
|
24
25
|
export { Session } from './session.js';
|
package/lib/mime.js
CHANGED
|
@@ -2,7 +2,8 @@ import mime from 'mime-types';
|
|
|
2
2
|
const COMMON = {
|
|
3
3
|
html: 'text/html; charset=utf-8',
|
|
4
4
|
json: 'application/json; charset=utf-8',
|
|
5
|
-
txt: 'text/plain; charset=utf-8'
|
|
5
|
+
txt: 'text/plain; charset=utf-8',
|
|
6
|
+
yaml: 'text/yaml; charset=utf-8'
|
|
6
7
|
};
|
|
7
8
|
export class Mime {
|
|
8
9
|
detect(accepts) {
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import Path from '@mojojs/path';
|
|
2
|
+
import yaml from 'js-yaml';
|
|
3
|
+
export default function yamlConfigPlugin(app, options) {
|
|
4
|
+
const filename = new Path(options.file ?? 'config.yml');
|
|
5
|
+
const filePath = filename.isAbsolute() ? filename : app.home.child(filename.toString());
|
|
6
|
+
const log = app.log;
|
|
7
|
+
if (filePath.existsSync()) {
|
|
8
|
+
Object.assign(app.config, yaml.load(filePath.readFileSync().toString()));
|
|
9
|
+
log.trace(`Config file "${filePath.toString()}" loaded`);
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
log.trace(`Config file "${filePath.toString()}" does not exist`);
|
|
13
|
+
}
|
|
14
|
+
const fileParts = filePath.toString().split('.');
|
|
15
|
+
fileParts.splice(-1, 0, app.mode);
|
|
16
|
+
const modeFilePath = new Path(fileParts.join('.'));
|
|
17
|
+
if (modeFilePath.existsSync()) {
|
|
18
|
+
Object.assign(app.config, yaml.load(modeFilePath.readFileSync().toString()));
|
|
19
|
+
log.trace(`Mode specific config file "${filePath.toString()}" loaded`);
|
|
20
|
+
}
|
|
21
|
+
}
|
package/lib/renderer.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import Path from '@mojojs/path';
|
|
2
|
+
import yaml from 'js-yaml';
|
|
2
3
|
export class Renderer {
|
|
3
4
|
constructor() {
|
|
4
5
|
this.defaultEngine = 'ejs';
|
|
@@ -29,6 +30,10 @@ export class Renderer {
|
|
|
29
30
|
}
|
|
30
31
|
return { output: Buffer.from(JSON.stringify(options.json)), format: options.format ?? 'json' };
|
|
31
32
|
}
|
|
33
|
+
if (options.yaml !== undefined) {
|
|
34
|
+
log.trace('Rendering YAML response');
|
|
35
|
+
return { output: Buffer.from(yaml.dump(options.yaml)), format: options.format ?? 'yaml' };
|
|
36
|
+
}
|
|
32
37
|
if (options.engine !== undefined) {
|
|
33
38
|
const engine = this.engines[options.engine];
|
|
34
39
|
if (engine === undefined)
|
package/lib/user-agent/test.d.ts
CHANGED
|
@@ -49,6 +49,7 @@ export declare class TestUserAgent extends MockUserAgent {
|
|
|
49
49
|
typeLike(regex: RegExp): this;
|
|
50
50
|
websocketOk(url: string | URL, options?: UserAgentWebSocketOptions): Promise<void>;
|
|
51
51
|
get ws(): WebSocket;
|
|
52
|
+
yamlIs(value: any): this;
|
|
52
53
|
get _cheerio(): cheerio.Root;
|
|
53
54
|
_prepareTap(tap: Tap.Tap): void;
|
|
54
55
|
_requestOk(skip: SkipFunction, method: string, url: string | URL, options?: UserAgentRequestOptions): Promise<this>;
|
package/lib/user-agent/test.js
CHANGED
|
@@ -2,6 +2,7 @@ import assert from 'assert/strict';
|
|
|
2
2
|
import { on } from 'events';
|
|
3
3
|
import { MockUserAgent } from './mock.js';
|
|
4
4
|
import cheerio from 'cheerio';
|
|
5
|
+
import yaml from 'js-yaml';
|
|
5
6
|
import StackUtils from 'stack-utils';
|
|
6
7
|
export class TestUserAgent extends MockUserAgent {
|
|
7
8
|
constructor(options = {}) {
|
|
@@ -141,6 +142,10 @@ export class TestUserAgent extends MockUserAgent {
|
|
|
141
142
|
throw new Error('No active WebSocket connection');
|
|
142
143
|
return ws;
|
|
143
144
|
}
|
|
145
|
+
yamlIs(value) {
|
|
146
|
+
this.assert('same', [yaml.load(this.body.toString()), value], 'YAML body is equal', this.yamlIs);
|
|
147
|
+
return this;
|
|
148
|
+
}
|
|
144
149
|
get _cheerio() {
|
|
145
150
|
if (this._dom === undefined)
|
|
146
151
|
this._dom = cheerio.load(this.body ?? '');
|
package/lib/user-agent.js
CHANGED
|
@@ -6,6 +6,7 @@ import { format, URL } from 'url';
|
|
|
6
6
|
import { UserAgentResponse } from './user-agent/response.js';
|
|
7
7
|
import { WebSocket } from './websocket.js';
|
|
8
8
|
import FormData from 'form-data';
|
|
9
|
+
import yaml from 'js-yaml';
|
|
9
10
|
import tough from 'tough-cookie';
|
|
10
11
|
import WS from 'ws';
|
|
11
12
|
class UserAgent extends EventEmitter {
|
|
@@ -100,6 +101,11 @@ class UserAgent extends EventEmitter {
|
|
|
100
101
|
filtered.headers['Content-Type'] = 'application/json';
|
|
101
102
|
filtered.body = JSON.stringify(filtered.json);
|
|
102
103
|
}
|
|
104
|
+
else if (filtered.yaml !== undefined) {
|
|
105
|
+
if (filtered.headers['Content-Type'] === undefined)
|
|
106
|
+
filtered.headers['Content-Type'] = 'text/yaml';
|
|
107
|
+
filtered.body = yaml.dump(filtered.yaml);
|
|
108
|
+
}
|
|
103
109
|
else if (filtered.form !== undefined) {
|
|
104
110
|
if (filtered.headers['Content-Type'] === undefined) {
|
|
105
111
|
filtered.headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mojojs/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.3",
|
|
4
4
|
"description": "Real-time web framework",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"web",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"build:lint": "npm run build && npm run lint",
|
|
26
26
|
"build:test": "npm run build && npm test",
|
|
27
27
|
"build:watch": "npm run build -- --watch",
|
|
28
|
-
"clean": "rm -
|
|
28
|
+
"clean": "rm -rf tsconfig.tsbuildinfo test/support/ts/full-app/tsconfig.tsbuildinfo lib test/support/ts/full-app/lib",
|
|
29
29
|
"coverage": "tap --node-arg='--no-warnings' --node-arg='--experimental-loader' --node-arg='@istanbuljs/esm-loader-hook' test/*.js",
|
|
30
30
|
"coverage:ci": "npm run coverage -- --no-check-coverage && tap --no-check-coverage --coverage-report=text-lcov > .lcov.info",
|
|
31
31
|
"lint": "eslint \"test/*.js\" \"test/support/js/**/*.js\" \"test/support/ts/**/src/**/*.ts\" \"src/**/*.ts\" \"src/*.ts\"",
|
|
@@ -49,10 +49,11 @@
|
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"@istanbuljs/esm-loader-hook": "^=0.1.2",
|
|
52
|
-
"@types/busboy": "^0.
|
|
52
|
+
"@types/busboy": "^0.3.1",
|
|
53
53
|
"@types/cheerio": "^0.22.29",
|
|
54
54
|
"@types/cookie": "^0.4.0",
|
|
55
55
|
"@types/ejs": "^3.0.6",
|
|
56
|
+
"@types/js-yaml": "^4.0.3",
|
|
56
57
|
"@types/lru-cache": "^5.1.0",
|
|
57
58
|
"@types/mime-types": "^2.1.0",
|
|
58
59
|
"@types/node": "^16.0.0",
|
|
@@ -61,16 +62,15 @@
|
|
|
61
62
|
"@types/tap": "^15.0.3",
|
|
62
63
|
"@types/tough-cookie": "^4.0.0",
|
|
63
64
|
"@types/ws": "^7.4.5",
|
|
64
|
-
"@typescript-eslint/eslint-plugin": "^
|
|
65
|
+
"@typescript-eslint/eslint-plugin": "^5.1.0",
|
|
65
66
|
"autocannon": "^7.3.0",
|
|
66
67
|
"concurrently": "^6.2.0",
|
|
67
|
-
"eslint": "^
|
|
68
|
+
"eslint": "^8.1.0",
|
|
68
69
|
"eslint-config-prettier": "^8.3.0",
|
|
69
70
|
"eslint-plugin-import": "^2.23.4",
|
|
70
71
|
"eslint-plugin-node": "^11.1.0",
|
|
71
|
-
"eslint-plugin-prettier": "^
|
|
72
|
-
"
|
|
73
|
-
"prettier": "2.3.2",
|
|
72
|
+
"eslint-plugin-prettier": "^4.0.0",
|
|
73
|
+
"prettier": "2.4.1",
|
|
74
74
|
"typescript": "^=4.3.2"
|
|
75
75
|
},
|
|
76
76
|
"engines": {
|
|
@@ -86,6 +86,7 @@
|
|
|
86
86
|
"ejs": "^3.1.6",
|
|
87
87
|
"escape-string-regexp": "^5.0.0",
|
|
88
88
|
"form-data": "^4.0.0",
|
|
89
|
+
"js-yaml": "^4.1.0",
|
|
89
90
|
"lru-cache": "^6.0.0",
|
|
90
91
|
"mime-types": "^2.1.30",
|
|
91
92
|
"nopt": "^5.0.0",
|