mirror of
https://github.com/abrendan/MicDropMessages.git
synced 2025-08-25 14:02:03 +02:00
Initial commit
This commit is contained in:
3
node_modules/server/reply/cookie.js
generated
vendored
Normal file
3
node_modules/server/reply/cookie.js
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
const Reply = require('./reply');
|
||||
|
||||
module.exports = (...args) => new Reply('cookie', ...args);
|
3
node_modules/server/reply/download.js
generated
vendored
Normal file
3
node_modules/server/reply/download.js
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
const Reply = require('./reply');
|
||||
|
||||
module.exports = (...args) => new Reply('download', ...args);
|
3
node_modules/server/reply/end.js
generated
vendored
Normal file
3
node_modules/server/reply/end.js
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
const Reply = require('./reply');
|
||||
|
||||
module.exports = (...args) => new Reply('end', ...args);
|
3
node_modules/server/reply/file.js
generated
vendored
Normal file
3
node_modules/server/reply/file.js
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
const Reply = require('./reply');
|
||||
|
||||
module.exports = (...args) => new Reply('file', ...args);
|
3
node_modules/server/reply/header.js
generated
vendored
Normal file
3
node_modules/server/reply/header.js
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
const Reply = require('./reply');
|
||||
|
||||
module.exports = (...args) => new Reply('header', ...args);
|
14
node_modules/server/reply/index.js
generated
vendored
Normal file
14
node_modules/server/reply/index.js
generated
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
module.exports = {
|
||||
cookie: require('./cookie' ),
|
||||
download: require('./download'),
|
||||
end: require('./end' ),
|
||||
file: require('./file' ),
|
||||
header: require('./header' ),
|
||||
json: require('./json' ),
|
||||
jsonp: require('./jsonp' ),
|
||||
redirect: require('./redirect'),
|
||||
render: require('./render' ),
|
||||
send: require('./send' ),
|
||||
status: require('./status' ),
|
||||
type: require('./type' )
|
||||
};
|
241
node_modules/server/reply/integration.test.js
generated
vendored
Normal file
241
node_modules/server/reply/integration.test.js
generated
vendored
Normal file
@@ -0,0 +1,241 @@
|
||||
const server = require('server');
|
||||
const run = require('server/test/run');
|
||||
const {
|
||||
cookie, download, end, file, header, json,
|
||||
jsonp, redirect, render, send, status, type
|
||||
} = require('.');
|
||||
|
||||
const logo = process.cwd() + '/test/logo.png';
|
||||
|
||||
describe('reply', () => {
|
||||
describe('cookie', () => {
|
||||
it('sets the cookie with name, value', async () => {
|
||||
const mid = () => cookie('hello', 'world').end();
|
||||
const res = await run(mid).get('/');
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.headers['set-cookie'][0]).toMatch(/hello\=world/);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('download', () => {
|
||||
it('can download a file with an absolute path', async () => {
|
||||
const mid = async () => download(logo, 'logo.png');
|
||||
const res = await run(mid).get('/');
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.headers['content-disposition']).toBe('attachment; filename="logo.png"');
|
||||
expect(res.headers['content-type']).toBe('image/png');
|
||||
});
|
||||
|
||||
it('can download a file with a relative path', async () => {
|
||||
const mid = async () => download('test/logo.png', 'logo.png');
|
||||
const res = await run(mid).get('/');
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.headers['content-type']).toBe('image/png');
|
||||
});
|
||||
|
||||
it('needs a path', async () => {
|
||||
const mid = async () => download();
|
||||
const res = await run(mid).get('/');
|
||||
expect(res.status).toBe(500);
|
||||
expect(res.body).toMatch(/first argument/);
|
||||
});
|
||||
|
||||
it('the file has to exist', async () => {
|
||||
const mid = async () => download('blabla.png', 'bla.png');
|
||||
const res = await run(mid).get('/');
|
||||
expect(res.status).toBe(500);
|
||||
expect(res.body).toMatch(/does not exist/);
|
||||
});
|
||||
|
||||
it('needs a name', async () => {
|
||||
const mid = async () => download(logo);
|
||||
const res = await run(mid).get('/');
|
||||
expect(res.status).toBe(500);
|
||||
expect(res.body).toMatch(/second argument/);
|
||||
});
|
||||
|
||||
it('needs nothing else', async () => {
|
||||
const mid = async () => download(logo, 'logo.png', 'should not be here');
|
||||
const res = await run(mid).get('/');
|
||||
expect(res.status).toBe(500);
|
||||
expect(res.body).toMatch(/two arguments/);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
describe('end', () => {
|
||||
it('can end a request', async () => {
|
||||
const mid = async () => end();
|
||||
const res = await run(mid).get('/');
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
describe('file', () => {
|
||||
it('can download a file with an absolute path', async () => {
|
||||
const mid = async () => file(logo, 'logo.png');
|
||||
const res = await run(mid).get('/');
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.headers['content-disposition']).toBe(undefined);
|
||||
expect(res.headers['content-type']).toBe('image/png');
|
||||
});
|
||||
|
||||
it('can download a file with a relative path', async () => {
|
||||
const mid = async () => file('test/logo.png', 'logo.png');
|
||||
const res = await run(mid).get('/');
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.headers['content-type']).toBe('image/png');
|
||||
});
|
||||
|
||||
it('needs a path', async () => {
|
||||
const mid = async () => file();
|
||||
const res = await run(mid).get('/');
|
||||
expect(res.status).toBe(500);
|
||||
expect(res.body).toMatch(/first argument/);
|
||||
});
|
||||
|
||||
it('the file has to exist', async () => {
|
||||
const mid = async () => file('blabla.png', 'bla.png');
|
||||
const res = await run(mid).get('/');
|
||||
expect(res.status).toBe(500);
|
||||
expect(res.body).toMatch(/does not exist/);
|
||||
});
|
||||
|
||||
it('needs nothing else', async () => {
|
||||
const mid = async () => file(logo, 'logo.png', 'should not be here');
|
||||
const res = await run(mid).get('/');
|
||||
expect(res.status).toBe(500);
|
||||
expect(res.body).toMatch(/two arguments/);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
describe('header', () => {
|
||||
it('can send some headers', async () => {
|
||||
const mid = async () => header('hello', 'world').end();
|
||||
const res = await run(mid).get('/');
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.headers['hello']).toBe('world');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
describe('json', () => {
|
||||
it('answers json', async () => {
|
||||
const mid = () => json([0, 1, 'a']);
|
||||
const res = await run(mid).get('/');
|
||||
expect(res.rawBody).toBe('[0,1,"a"]');
|
||||
expect(res.headers['content-type']).toMatch(/application\/json/);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
describe('jsonp', () => {
|
||||
it('answers jsonp', async () => {
|
||||
const mid = () => jsonp([0, 1, 'a']);
|
||||
const res = await run(mid).get('/?callback=callback');
|
||||
expect(res.body).toMatch('callback([0,1,"a"])');
|
||||
expect(res.headers['content-type']).toMatch('text/javascript');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
describe('redirect', () => {
|
||||
it('send the correct headers', async () => {
|
||||
const mid = (ctx) => ctx.url === '/'
|
||||
? redirect('/login') : ctx.url === '/login' ? 'Redirected' : 'Error';
|
||||
const res = await run(mid).get('/');
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body).toBe('Redirected');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
describe('render', () => {
|
||||
it('renders a demo file', async () => {
|
||||
const mid = (ctx) => render('index.hbs');
|
||||
const res = await run({ views: 'test/views' }, mid).get('/');
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body).toMatch(/\<h1\>Hello world\<\/h1\>/);
|
||||
});
|
||||
|
||||
it('requires to specify a file', async () => {
|
||||
const mid = (ctx) => render();
|
||||
const res = await run({ views: 'test/views' }, mid).get('/');
|
||||
expect(res.status).toBe(500);
|
||||
});
|
||||
|
||||
it('does not expect many things', async () => {
|
||||
const mid = (ctx) => render('index.hbs', {}, 'should not be here');
|
||||
const res = await run({ views: 'test/views' }, mid).get('/');
|
||||
expect(res.status).toBe(500);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
describe('send', () => {
|
||||
it('simple send answer', async () => {
|
||||
const mid = () => send('Hello 世界');
|
||||
const res = await run(mid).get('/');
|
||||
expect(res.body).toBe('Hello 世界');
|
||||
});
|
||||
|
||||
it('does not allow to send the full context', async () => {
|
||||
const mid = ctx => send(ctx);
|
||||
const res = await run(mid).get('/');
|
||||
expect(res.status).toBe(500);
|
||||
expect(res.body).toMatch(/send the context/);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
describe('status', () => {
|
||||
it('can change the status', async () => {
|
||||
const mid = (ctx) => status(505).end();
|
||||
const res = await run(mid).get('/');
|
||||
expect(res.status).toBe(505);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
describe('type', () => {
|
||||
it('can set the response type', async () => {
|
||||
const mid = (ctx) => type('png').send('Hello');
|
||||
const res = await run(mid).get('/');
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.headers['content-type']).toBe('image/png; charset=utf-8');
|
||||
});
|
||||
});
|
||||
});
|
3
node_modules/server/reply/json.js
generated
vendored
Normal file
3
node_modules/server/reply/json.js
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
const Reply = require('./reply');
|
||||
|
||||
module.exports = (...args) => new Reply('json', ...args);
|
3
node_modules/server/reply/jsonp.js
generated
vendored
Normal file
3
node_modules/server/reply/jsonp.js
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
const Reply = require('./reply');
|
||||
|
||||
module.exports = (...args) => new Reply('jsonp', ...args);
|
3
node_modules/server/reply/redirect.js
generated
vendored
Normal file
3
node_modules/server/reply/redirect.js
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
const Reply = require('./reply');
|
||||
|
||||
module.exports = (...args) => new Reply('redirect', ...args);
|
3
node_modules/server/reply/render.js
generated
vendored
Normal file
3
node_modules/server/reply/render.js
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
const Reply = require('./reply');
|
||||
|
||||
module.exports = (...args) => new Reply('render', ...args);
|
181
node_modules/server/reply/reply.js
generated
vendored
Normal file
181
node_modules/server/reply/reply.js
generated
vendored
Normal file
@@ -0,0 +1,181 @@
|
||||
const path = require('path');
|
||||
const fs = require('mz/fs');
|
||||
|
||||
|
||||
const Reply = function (name, ...args) {
|
||||
this.stack = [];
|
||||
this[name](...args);
|
||||
return this;
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reply.prototype.cookie = function (...args) {
|
||||
this.stack.push(ctx => {
|
||||
ctx.res.cookie(...args);
|
||||
});
|
||||
return this;
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reply.prototype.download = function (...args) {
|
||||
|
||||
// Guard clauses
|
||||
if (args.length < 1) {
|
||||
throw new Error('download() expects a path as the first argument');
|
||||
}
|
||||
|
||||
if (args.length < 2) {
|
||||
throw new Error('download() expects a filename as the second argument');
|
||||
}
|
||||
|
||||
if (args.length > 2) {
|
||||
throw new Error('download() only expects two arguments, path and filename. The rest of them will be ignored');
|
||||
}
|
||||
|
||||
let [file, opts] = args;
|
||||
if (!path.isAbsolute(file)) {
|
||||
file = path.resolve(process.cwd(), file);
|
||||
}
|
||||
|
||||
this.stack.push(async ctx => {
|
||||
if (!await fs.exists(file)) {
|
||||
throw new Error(`The file "${file}" does not exist. Make sure that you set an absolute path or a relative path to the root of your project`);
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
ctx.res.download(file, opts, err => err ? reject(err) : resolve());
|
||||
});
|
||||
});
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reply.prototype.end = function () {
|
||||
this.stack.push(ctx => {
|
||||
ctx.res.end();
|
||||
});
|
||||
return this;
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reply.prototype.file = function (...args) {
|
||||
|
||||
// Guard clauses
|
||||
if (args.length < 1) {
|
||||
throw new Error('file() expects a path as the first argument');
|
||||
}
|
||||
|
||||
if (args.length > 2) {
|
||||
throw new Error(`file() only expects two arguments, the path and options, but ${args.length} were provided.`);
|
||||
}
|
||||
|
||||
let [file, opts = {}] = args;
|
||||
if (!path.isAbsolute(file)) {
|
||||
file = path.resolve(process.cwd(), file);
|
||||
}
|
||||
|
||||
this.stack.push(async ctx => {
|
||||
if (!await fs.exists(file)) {
|
||||
throw new Error(`The file "${file}" does not exist. Make sure that you set an absolute path or a relative path to the root of your project`);
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
ctx.res.sendFile(file, opts, err => err ? reject(err) : resolve());
|
||||
});
|
||||
});
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reply.prototype.header = function (...args) {
|
||||
this.stack.push(ctx => {
|
||||
ctx.res.header(...args);
|
||||
});
|
||||
return this;
|
||||
};
|
||||
|
||||
Reply.prototype.json = function (...args) {
|
||||
this.stack.push(ctx => {
|
||||
ctx.res.json(...args);
|
||||
});
|
||||
return this;
|
||||
};
|
||||
|
||||
Reply.prototype.jsonp = function (...args) {
|
||||
this.stack.push(ctx => {
|
||||
ctx.res.jsonp(...args);
|
||||
});
|
||||
return this;
|
||||
};
|
||||
|
||||
Reply.prototype.redirect = function (...args) {
|
||||
this.stack.push(ctx => {
|
||||
ctx.res.redirect(...args);
|
||||
});
|
||||
return this;
|
||||
};
|
||||
|
||||
Reply.prototype.render = function (...args) {
|
||||
|
||||
// Guard clauses
|
||||
if (args.length < 1) {
|
||||
throw new Error('file() expects a path');
|
||||
}
|
||||
|
||||
if (args.length > 2) {
|
||||
throw new Error('file() expects a path and options but nothing else');
|
||||
}
|
||||
|
||||
let [file, opts = {}] = args;
|
||||
|
||||
this.stack.push(ctx => new Promise((resolve, reject) => {
|
||||
// Note: if callback is provided, it does not send() automatically
|
||||
const cb = (err, html) => err ? reject(err) : resolve(ctx.res.send(html));
|
||||
ctx.res.render(file, opts, cb);
|
||||
}));
|
||||
return this;
|
||||
};
|
||||
|
||||
Reply.prototype.send = function (...args) {
|
||||
|
||||
// If we are trying to send the context
|
||||
if (args[0] && args[0].close && args[0].close instanceof Function) {
|
||||
throw new Error('Never send the context, request or response as those are a security risk');
|
||||
}
|
||||
|
||||
this.stack.push(ctx => {
|
||||
ctx.res.send(...args);
|
||||
});
|
||||
return this;
|
||||
};
|
||||
|
||||
Reply.prototype.status = function (...args) {
|
||||
this.stack.push(ctx => {
|
||||
// In case there is no response, it'll respond with the status
|
||||
ctx.res.explicitStatus = true;
|
||||
ctx.res.status(...args);
|
||||
});
|
||||
return this;
|
||||
};
|
||||
|
||||
Reply.prototype.type = function (...args) {
|
||||
this.stack.push(ctx => {
|
||||
ctx.res.type(...args);
|
||||
});
|
||||
return this;
|
||||
};
|
||||
|
||||
Reply.prototype.exec = async function (ctx) {
|
||||
for (let cb of this.stack) {
|
||||
await cb(ctx);
|
||||
}
|
||||
this.stack = [];
|
||||
};
|
||||
|
||||
// This will make that the first time a function is called it starts a new stack
|
||||
module.exports = Reply;
|
3
node_modules/server/reply/send.js
generated
vendored
Normal file
3
node_modules/server/reply/send.js
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
const Reply = require('./reply');
|
||||
|
||||
module.exports = (...args) => new Reply('send', ...args);
|
3
node_modules/server/reply/status.js
generated
vendored
Normal file
3
node_modules/server/reply/status.js
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
const Reply = require('./reply');
|
||||
|
||||
module.exports = (...args) => new Reply('status', ...args);
|
3
node_modules/server/reply/type.js
generated
vendored
Normal file
3
node_modules/server/reply/type.js
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
const Reply = require('./reply');
|
||||
|
||||
module.exports = (...args) => new Reply('type', ...args);
|
67
node_modules/server/reply/unit.test.js
generated
vendored
Normal file
67
node_modules/server/reply/unit.test.js
generated
vendored
Normal file
@@ -0,0 +1,67 @@
|
||||
const reply = require('.');
|
||||
|
||||
|
||||
|
||||
describe('reply', () => {
|
||||
|
||||
it('loads the main reply', () => {
|
||||
expect(reply).toEqual(require('server').reply);
|
||||
expect(reply).toEqual(require('server/reply'));
|
||||
});
|
||||
|
||||
it('has the right methods defined', () => {
|
||||
expect(reply.cookie ).toEqual(jasmine.any(Function));
|
||||
expect(reply.download).toEqual(jasmine.any(Function));
|
||||
expect(reply.end ).toEqual(jasmine.any(Function));
|
||||
expect(reply.file ).toEqual(jasmine.any(Function));
|
||||
expect(reply.header ).toEqual(jasmine.any(Function));
|
||||
expect(reply.json ).toEqual(jasmine.any(Function));
|
||||
expect(reply.jsonp ).toEqual(jasmine.any(Function));
|
||||
expect(reply.redirect).toEqual(jasmine.any(Function));
|
||||
expect(reply.render ).toEqual(jasmine.any(Function));
|
||||
expect(reply.send ).toEqual(jasmine.any(Function));
|
||||
expect(reply.status ).toEqual(jasmine.any(Function));
|
||||
expect(reply.type ).toEqual(jasmine.any(Function));
|
||||
});
|
||||
|
||||
it('can load all the methods manually', () => {
|
||||
expect(require('server/reply/cookie' )).toEqual(reply.cookie);
|
||||
expect(require('server/reply/download')).toEqual(reply.download);
|
||||
expect(require('server/reply/end' )).toEqual(reply.end);
|
||||
expect(require('server/reply/file' )).toEqual(reply.file);
|
||||
expect(require('server/reply/header' )).toEqual(reply.header);
|
||||
expect(require('server/reply/json' )).toEqual(reply.json);
|
||||
expect(require('server/reply/jsonp' )).toEqual(reply.jsonp);
|
||||
expect(require('server/reply/redirect')).toEqual(reply.redirect);
|
||||
expect(require('server/reply/render' )).toEqual(reply.render);
|
||||
expect(require('server/reply/send' )).toEqual(reply.send);
|
||||
expect(require('server/reply/status' )).toEqual(reply.status);
|
||||
expect(require('server/reply/type' )).toEqual(reply.type);
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
describe('reply: instances instead of global', () => {
|
||||
it('adds a method to the stack', () => {
|
||||
const mock = reply.file('./index.js');
|
||||
expect(mock.stack.length).toEqual(1);
|
||||
const inst = reply.file('./index.js');
|
||||
expect(inst.stack.length).toEqual(1);
|
||||
|
||||
// Do not touch the global
|
||||
expect(mock.stack.length).toEqual(1);
|
||||
});
|
||||
|
||||
it('adds several methods correctly', () => {
|
||||
const mock = reply.file('./index.js');
|
||||
expect(mock.stack.length).toEqual(1);
|
||||
const inst = reply.file('./index.js').file('./whatever.js');
|
||||
expect(inst.stack.length).toEqual(2);
|
||||
|
||||
// Do not touch the global
|
||||
expect(mock.stack.length).toEqual(1);
|
||||
});
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user