Initial commit

This commit is contained in:
abrendan
2023-11-30 14:15:19 +00:00
commit e4599df811
5457 changed files with 500139 additions and 0 deletions

97
node_modules/server/plugins/parser/index.js generated vendored Normal file
View File

@@ -0,0 +1,97 @@
// Parser plugin
// Get the raw request and transform it into something usable
// Examples: ctx.body, ctx.files, etc
const join = require('../../src/join');
const modern = require('../../src/modern');
const plugin = {
name: 'parser',
options: {
body: {
type: [Object, Boolean],
default: { extended: true },
extend: true
},
json: {
type: [Object, Boolean],
default: {}
},
text: {
type: Object,
default: {}
},
data: {
type: Object,
default: {}
},
cookie: {
type: Object,
default: {}
},
method: {
type: [Object, String, Boolean],
default: [
'X-HTTP-Method',
'X-HTTP-Method-Override',
'X-Method-Override',
'_method'
],
// Coerce it into an Array if it is not already
clean: value => typeof value === 'string' ? [value] : value
}
},
// It is populated in "init()" right now:
before: [
ctx => {
if (!ctx.options.parser.method) return;
return join(ctx.options.parser.method.map(one => {
return modern(require('method-override')(one));
}))(ctx);
},
ctx => {
if (!ctx.options.parser.body) return;
const body = require('body-parser').urlencoded(ctx.options.parser.body);
return modern(body)(ctx);
},
// JSON parser
ctx => {
if (!ctx.options.parser.json) return;
const json = require('body-parser').json(ctx.options.parser.json);
return modern(json)(ctx);
},
// Text parser
ctx => {
if (!ctx.options.parser.text) return;
const text = require('body-parser').text(ctx.options.parser.text);
return modern(text)(ctx);
},
// Data parser
ctx => {
if (!ctx.options.parser.data) return;
const data = require('express-data-parser')(ctx.options.parser.data);
return modern(data)(ctx);
},
// Cookie parser
ctx => {
if (!ctx.options.parser.cookie) return;
const cookie = require('cookie-parser')(
ctx.options.secret,
ctx.options.parser.cookie
);
return modern(cookie)(ctx);
},
// Add a reference from ctx.req.body to the ctx.data and an alias
ctx => {
ctx.data = ctx.body;
},
]
};
module.exports = plugin;

121
node_modules/server/plugins/parser/integration.test.js generated vendored Normal file
View File

@@ -0,0 +1,121 @@
// External libraries used
const { cookie } = require('server/reply');
const run = require('server/test/run');
const fs = require('fs');
run.options = { security: false };
// Local helpers and data
const logo = fs.createReadStream(__dirname + '/../../test/logo.png');
const content = ctx => ctx.headers['content-type'];
describe('Default modules', () => {
it('bodyParser', async () => {
const mid = ctx => {
expect(ctx.data).toEqual(ctx.req.body);
expect(ctx.data).toBeDefined();
expect(ctx.data.hello).toBe('世界');
expect(content(ctx)).toBe('application/x-www-form-urlencoded');
return 'Hello 世界';
};
const res = await run(mid).post('/', { form: 'hello=世界' });
expect(res.body).toBe('Hello 世界');
});
it('dataParser', async () => {
const mid = ctx => ctx.files.logo;
const res = await run(mid).post('/', { formData: { logo } });
expect(res.body.name).toBe('logo.png');
expect(res.body.type).toBe('image/png');
expect(res.body.size).toBe(30587);
});
// It can *set* cookies from the server()
// TODO: it can *get* cookies from the server()
it('cookieParser', async () => {
const mid = () => cookie('place', '世界').send('Hello 世界');
const res = await run(mid).post('/', { body: { place: '世界' } });
const cookies = res.headers['set-cookie'].join();
expect(cookies).toMatch('place=%E4%B8%96%E7%95%8C');
});
// Change the method to the specified one
it('method-override through header', async () => {
const mid = ctx => {
expect(ctx.method).toBe('PUT');
expect(ctx.originalMethod).toBe('POST');
return 'Hello 世界';
};
const headers = { 'X-HTTP-Method-Override': 'PUT' };
const res = await run(mid).post('/', { headers });
expect(res.body).toBe('Hello 世界');
});
// Change the method to the specified one
it('override-method works with a string', async () => {
const mid = ctx => {
expect(ctx.method).toBe('PUT');
expect(ctx.originalMethod).toBe('POST');
return 'Hello 世界';
};
const headers = { 'X-HTTP-Method-Override': 'PUT' };
const res = await run({ parser: {
method: 'X-HTTP-Method-Override'
} }, mid).post('/', { headers });
expect(res.body).toBe('Hello 世界');
});
// Change the method to the specified one
it('override-method works with an array', async () => {
const mid = ctx => {
expect(ctx.method).toBe('PUT');
expect(ctx.originalMethod).toBe('POST');
return 'Hello 世界';
};
const headers = { 'X-HTTP-Method-Override': 'PUT' };
const res = await run({ parser: {
method: ['X-HTTP-Method-Override']
} }, mid).post('/', { headers });
expect(res.body).toBe('Hello 世界');
});
// TODO: check more options
});
describe('Cancel parts through options', () => {
it('can cancel bodyParser', async () => {
const options = { parser: { body: false } };
const mid = ctx => {
expect(ctx.body).toEqual({});
expect(ctx.headers['content-type']).toBe('application/x-www-form-urlencoded');
return 'Hello 世界';
};
const res = await run(options, mid).post('/', { form: 'hello=世界' });
expect(res.body).toBe('Hello 世界');
});
it('can cancel jsonParser', async () => {
const mid = ctx => {
expect(ctx.data).toEqual(ctx.req.body);
expect(ctx.data).toEqual({});
expect(content(ctx)).toBe('application/json');
return 'Hello 世界';
};
const res = await run({ parser: { json: false }}, mid).post('/', { body: { hello: '世界' }});
expect(res.body).toBe('Hello 世界');
});
// TODO: check all others can be cancelled
});