Skip to content

Commit

Permalink
chore: make the code compatible with node 10.0.0
Browse files Browse the repository at this point in the history
- Replace new Buffer() with Buffer.from()
- Work around nodejs/node#20278
- Upgrade to source-map-support@0.5.5
  • Loading branch information
raymondfeng committed Apr 25, 2018
1 parent 49b9a82 commit afe72ad
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('Basic Authentication', () => {
const client = whenIMakeRequestTo(server);
const credential =
users.list.joe.profile.id + ':' + users.list.joe.password;
const hash = new Buffer(credential).toString('base64');
const hash = Buffer.from(credential).toString('base64');
await client
.get('/whoAmI')
.set('Authorization', 'Basic ' + hash)
Expand All @@ -58,7 +58,7 @@ describe('Basic Authentication', () => {
it('returns error for invalid credentials', async () => {
const client = whenIMakeRequestTo(server);
const credential = users.list.Simpson.profile.id + ':' + 'invalid';
const hash = new Buffer(credential).toString('base64');
const hash = Buffer.from(credential).toString('base64');
await client
.get('/whoAmI')
.set('Authorization', 'Basic ' + hash)
Expand Down
11 changes: 7 additions & 4 deletions packages/boot/src/booters/booter-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,13 @@ export function isClass(target: any): target is Constructor<any> {
export function loadClassesFromFiles(files: string[]): Constructor<{}>[] {
const classes: Array<Constructor<{}>> = [];
for (const file of files) {
const data = require(file);
for (const cls of Object.values(data)) {
if (isClass(cls)) {
classes.push(cls);
const moduleObj = require(file);
// WORKAROUND: use `for in` instead of Object.values().
// See https://github.com/nodejs/node/issues/20278
for (const k in moduleObj) {
const exported = moduleObj[k];
if (isClass(exported)) {
classes.push(exported);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"nyc": "^11.7.1",
"prettier": "^1.12.1",
"rimraf": "^2.6.2",
"source-map-support": "^0.5.4",
"source-map-support": "^0.5.5",
"strong-docs": "^1.10.2",
"tslint": "^5.9.1",
"typescript": "^2.8.1"
Expand Down
20 changes: 10 additions & 10 deletions packages/repository/test/unit/type/type.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ describe('types', () => {
describe('buffer', () => {
const bufferType = new types.BufferType();
it('checks isInstance', () => {
expect(bufferType.isInstance(new Buffer([1]))).to.be.true();
expect(bufferType.isInstance(new Buffer('123'))).to.be.true();
expect(bufferType.isInstance(Buffer.from([1]))).to.be.true();
expect(bufferType.isInstance(Buffer.from('123'))).to.be.true();
expect(bufferType.isInstance('str')).to.be.false();
expect(bufferType.isInstance(null)).to.be.true();
expect(bufferType.isInstance(undefined)).to.be.true();
Expand All @@ -248,7 +248,7 @@ describe('types', () => {
expect(bufferType.isCoercible('str')).to.be.true();
expect(bufferType.isCoercible(null)).to.be.true();
expect(bufferType.isCoercible(undefined)).to.be.true();
expect(bufferType.isCoercible(new Buffer('12'))).to.be.true();
expect(bufferType.isCoercible(Buffer.from('12'))).to.be.true();
expect(bufferType.isCoercible([1, 2])).to.be.true();
expect(bufferType.isCoercible({x: 1})).to.be.false();
expect(bufferType.isCoercible(1)).to.be.false();
Expand All @@ -260,11 +260,11 @@ describe('types', () => {
});

it('coerces values', () => {
expect(bufferType.coerce('str').equals(new Buffer('str'))).to.be.true();
expect(bufferType.coerce('str').equals(Buffer.from('str'))).to.be.true();
expect(bufferType.coerce([1]).equals(Buffer.from([1]))).to.be.true();
expect(bufferType.coerce(null)).to.equal(null);
expect(bufferType.coerce(undefined)).to.equal(undefined);
const buf = new Buffer('12');
const buf = Buffer.from('12');
expect(bufferType.coerce(buf)).exactly(buf);
expect(() => bufferType.coerce(1)).to.throw(/Invalid buffer/);
expect(() => bufferType.coerce(new Date())).to.throw(/Invalid buffer/);
Expand All @@ -274,9 +274,9 @@ describe('types', () => {

it('serializes values', () => {
expect(
bufferType.serialize(new Buffer('str'), {encoding: 'utf-8'}),
bufferType.serialize(Buffer.from('str'), {encoding: 'utf-8'}),
).to.eql('str');
expect(bufferType.serialize(new Buffer('str'))).to.eql('c3Ry');
expect(bufferType.serialize(Buffer.from('str'))).to.eql('c3Ry');
expect(bufferType.serialize(null)).null();
expect(bufferType.serialize(undefined)).undefined();
});
Expand All @@ -293,7 +293,7 @@ describe('types', () => {
expect(anyType.isInstance([1, 2])).to.be.true();
expect(anyType.isInstance(1)).to.be.true();
expect(anyType.isInstance(new Date())).to.be.true();
expect(anyType.isInstance(new Buffer('123'))).to.be.true();
expect(anyType.isInstance(Buffer.from('123'))).to.be.true();
});

it('checks isCoercible', () => {
Expand All @@ -305,7 +305,7 @@ describe('types', () => {
expect(anyType.isCoercible(1)).to.be.true();
expect(anyType.isCoercible([1, '2'])).to.be.true();
expect(anyType.isCoercible(new Date())).to.be.true();
expect(anyType.isCoercible(new Buffer('123'))).to.be.true();
expect(anyType.isCoercible(Buffer.from('123'))).to.be.true();
});

it('creates defaultValue', () => {
Expand All @@ -324,7 +324,7 @@ describe('types', () => {
expect(anyType.coerce(1)).to.equal(1);
const date = new Date();
expect(anyType.coerce(date)).to.equal(date);
const buf = new Buffer('12');
const buf = Buffer.from('12');
expect(anyType.coerce(buf)).to.equal(buf);
});

Expand Down

0 comments on commit afe72ad

Please sign in to comment.