Skip to content
This repository was archived by the owner on Jul 3, 2020. It is now read-only.

Commit 51a90e7

Browse files
author
Face Kapow
committed
Update style
1 parent c8d8fd0 commit 51a90e7

73 files changed

Lines changed: 2169 additions & 1003 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.eslintrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ rules:
2929
no-return-assign: 1
3030
new-cap: 1
3131
no-console: 0
32+
wrap-iife:
33+
- 2
34+
- 'inside'
35+
no-use-before-define:
36+
- 2
37+
-
38+
functions: false
39+
classes: true
3240

3341
parserOptions:
3442
ecmaVersion: 6

docs/code-style-exceptions.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ You can (and should) use ES6 `get` and `set` in new APIs, it's already used in v
3737

3838
## Assigning to functions parameters
3939

40-
You cannot *reassign* functions parameters, but you can assign to *properties of* function parameters.
40+
You cannot reassign *functions parameters*, but you can assign to *properties of* function parameters.
4141
```js
4242
// yes
4343
const demo = (myObject) => {
@@ -46,8 +46,8 @@ const demo = (myObject) => {
4646
}
4747

4848
// no
49-
const demo = (myUint8Array) => {
50-
myUint8Array = {
49+
const demo = (myObject) => {
50+
myObject = {
5151
someProperty: 'my value'
5252
};
5353
// ...
@@ -61,3 +61,19 @@ If you get this warning, it's ok:
6161
But this is not:
6262

6363
> Assignment to function parameter 'parameter-name'
64+
65+
## Wrapping an IIFE
66+
67+
The invocation should be *outside* the parentheses, not inside.
68+
69+
```js
70+
// yes
71+
(function() {
72+
// ...
73+
})();
74+
75+
// no
76+
(function() {
77+
78+
}());
79+
```

js/__loader.js

Lines changed: 65 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,27 @@
2121
const cache = {};
2222
const builtinsResolveFromComponents = builtinsResolveFrom.split('/');
2323

24-
const throwError = (err) => { throw err; };
25-
const endsWith = (str, suffix) => str.indexOf(suffix, str.length - suffix.length) !== -1;
24+
function throwError(err) {
25+
throw err;
26+
}
27+
28+
function endsWith(str, suffix) {
29+
return str.indexOf(suffix, str.length - suffix.length) !== -1;
30+
}
2631

27-
const normalizePath = (components) => {
32+
function normalizePath(components) {
2833
const r = [];
2934
for (const p of components) {
3035
if (p === '') {
31-
if (r.length === 0) r.push(p);
36+
if (r.length === 0) {
37+
r.push(p);
38+
}
3239
continue;
3340
}
3441

35-
if (p === '.') continue;
42+
if (p === '.') {
43+
continue;
44+
}
3645

3746
if (p === '..') {
3847
if (r.length > 0) {
@@ -46,18 +55,18 @@
4655
}
4756

4857
return r;
49-
};
58+
}
5059

51-
const loadAsFile = (path) => {
60+
function loadAsFile(path) {
5261
if (existsFileFn(path)) return path;
5362
if (existsFileFn(`${path}.js`)) return `${path}.js`;
5463
if (existsFileFn(`${path}.json`)) return `${path}.json`;
5564
if (existsFileFn(`${path}.node`)) return `${path}.node`;
5665

5766
return null;
58-
};
67+
}
5968

60-
const getPackageMain = (packageJsonFile) => {
69+
function getPackageMain(packageJsonFile) {
6170
const json = readFileFn(packageJsonFile);
6271
let parsed = null;
6372
try {
@@ -74,9 +83,9 @@
7483
}
7584

7685
return parsed.main || 'index.js';
77-
};
86+
}
7887

79-
const loadAsDirectory = (path, ignoreJson) => {
88+
function loadAsDirectory(path, ignoreJson) {
8089
let mainFile = 'index';
8190
let dir = false;
8291
if (!ignoreJson && existsFileFn(`${path}/package.json`)) {
@@ -85,41 +94,55 @@
8594
}
8695

8796
const normalizedPath = normalizePath(path.split('/').concat(mainFile.split('/')));
88-
if (!normalizedPath) return null;
97+
if (!normalizedPath) {
98+
return null;
99+
}
89100

90101
const s = normalizedPath.join('/');
91102
const res = loadAsFile(s);
92-
if (res) return res;
103+
if (res) {
104+
return res;
105+
}
93106

94-
if (dir) return loadAsDirectory(s, true);
107+
if (dir) {
108+
return loadAsDirectory(s, true);
109+
}
95110

96111
return null;
97-
};
112+
}
98113

99-
const loadNodeModules = (dirComponents, parts) => {
114+
function loadNodeModules(dirComponents, parts) {
100115
let count = dirComponents.length;
101116

102117
while (count-- > 0) {
103118
let p = dirComponents.slice(0, count + 1);
104-
if (p.length === 0) continue;
119+
if (p.length === 0) {
120+
continue;
121+
}
105122

106-
if (p[p.length - 1] === 'node_modules') continue;
123+
if (p[p.length - 1] === 'node_modules') {
124+
continue;
125+
}
107126

108127
p.push('node_modules');
109128
p = p.concat(parts);
110129

111130
const normalizedPath = normalizePath(p);
112-
if (!normalizedPath) continue;
131+
if (!normalizedPath) {
132+
continue;
133+
}
113134

114135
const s = normalizedPath.join('/');
115136
const loadedPath = loadAsFile(s) || loadAsDirectory(s, false) || null;
116-
if (loadedPath) return loadedPath;
137+
if (loadedPath) {
138+
return loadedPath;
139+
}
117140
}
118141

119142
return null;
120-
};
143+
}
121144

122-
const resolve = (module, pathOpt = '') => {
145+
function resolve(module, pathOpt = '') {
123146
let path = String(pathOpt);
124147

125148
let resolveFrom = module.dirComponents;
@@ -134,22 +157,24 @@
134157

135158
// starts with ./ ../ or /
136159
if (firstPathComponent === '.' ||
137-
firstPathComponent === '..' ||
138-
firstPathComponent === '') {
139-
const combinedPathComponents = (firstPathComponent === '')
140-
? pathComponents
141-
: resolveFrom.concat(pathComponents);
160+
firstPathComponent === '..' ||
161+
firstPathComponent === '') {
162+
const combinedPathComponents = (firstPathComponent === '') ?
163+
pathComponents :
164+
resolveFrom.concat(pathComponents);
142165

143166
const normalizedPath = normalizePath(combinedPathComponents);
144-
if (!normalizedPath) return null;
167+
if (!normalizedPath) {
168+
return null;
169+
}
145170

146171
const pathStr = normalizedPath.join('/');
147172
const loadedPath = loadAsFile(pathStr) || loadAsDirectory(pathStr, false) || null;
148173
return loadedPath;
149174
}
150175

151176
return loadNodeModules(resolveFrom, pathComponents);
152-
};
177+
}
153178

154179
class Module {
155180
constructor(pathComponents) {
@@ -170,7 +195,9 @@
170195
const pathComponents = resolvedPath.split('/');
171196
const displayPath = resolvedPath;
172197
const cacheKey = pathComponents.join('/');
173-
if (cache[cacheKey]) return cache[cacheKey].exports;
198+
if (cache[cacheKey]) {
199+
return cache[cacheKey].exports;
200+
}
174201

175202
const currentModule = global.module;
176203
module = new Module(pathComponents);
@@ -189,10 +216,8 @@
189216
} else {
190217
/* eslint-disable max-len */
191218
evalScriptFn(
192-
`(function(require,exports,module,__filename,__dirname){
193-
${content}
194-
})((function(m){return function(path){return m.require(path)}})(global.module),global.module.exports,global.module,global.module.filename,global.module.dirname)`,
195-
displayPath);
219+
`(function(require,exports,module,__filename,__dirname){${content}})((function(m){return function(path){return m.require(path)}})(global.module),global.module.exports,global.module,global.module.filename,global.module.dirname)`,
220+
displayPath);
196221
/* eslint-enable max-len */
197222
}
198223

@@ -211,9 +236,13 @@ ${content}
211236
// end
212237

213238
const files = {};
214-
for (const file of __SYSCALL.initrdListFiles()) files[file] = true;
239+
for (const file of __SYSCALL.initrdListFiles()) {
240+
files[file] = true;
241+
}
215242

216-
const fileExists = path => !!files[path];
243+
function fileExists(path) {
244+
return !!files[path];
245+
}
217246

218247
const runtimePackagePath = __SYSCALL.initrdGetKernelIndex().split('/').slice(0, -1).join('/');
219248
const loader = new Loader(fileExists, __SYSCALL.initrdReadFile, __SYSCALL.eval, {

js/component/dns-client/dns-packet.js

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ exports.getQuery = (domain, query) => {
3636
let bufferLength = 17;
3737

3838
const labels = domain.split('.');
39-
for (const label of labels) bufferLength += label.length + 1;
39+
for (const label of labels) {
40+
bufferLength += label.length + 1;
41+
}
4042

4143
const u8 = new Uint8Array(bufferLength);
4244
const view = new DataView(u8.buffer);
@@ -54,7 +56,9 @@ exports.getQuery = (domain, query) => {
5456

5557
for (const label of labels) {
5658
view.setUint8(offset++, label.length);
57-
for (let j = 0; j < label.length; ++j) view.setUint8(offset++, label.charCodeAt(j));
59+
for (let j = 0; j < label.length; ++j) {
60+
view.setUint8(offset++, label.charCodeAt(j));
61+
}
5862
}
5963
view.setUint8(offset++, 0); // null terminator
6064

@@ -64,14 +68,19 @@ exports.getQuery = (domain, query) => {
6468
};
6569

6670
const POINTER_VALUE = 0xc0;
67-
const isPointer = value => (value & POINTER_VALUE) === POINTER_VALUE;
6871

69-
const readHostname = (reader) => {
72+
function isPointer(value) {
73+
return (value & POINTER_VALUE) === POINTER_VALUE;
74+
}
75+
76+
function readHostname(reader) {
7077
let labels = [];
7178

7279
for (let z = reader.getOffset(); z < reader.len; ++z) {
7380
const len = reader.readUint8();
74-
if (len === 0) break;
81+
if (len === 0) {
82+
break;
83+
}
7584

7685
if (isPointer(len)) {
7786
const ptrOffset = ((len - POINTER_VALUE) << 8) + reader.readUint8();
@@ -82,20 +91,24 @@ const readHostname = (reader) => {
8291
break;
8392
} else {
8493
let label = '';
85-
for (let i = 0; i < len; ++i) label += String.fromCharCode(reader.readUint8());
94+
for (let i = 0; i < len; ++i) {
95+
label += String.fromCharCode(reader.readUint8());
96+
}
8697

8798
labels.push(label);
8899
}
89100
}
90101

91102
return labels;
92-
};
103+
}
93104

94105
exports.parseResponse = (u8) => {
95106
const reader = new PacketReader(u8.buffer, u8.byteLength, u8.byteOffset);
96107
const responseRandomId = reader.readUint16();
97108

98-
if (responseRandomId !== randomId) return null;
109+
if (responseRandomId !== randomId) {
110+
return null;
111+
}
99112

100113
/* eslint-disable no-unused-vars */
101114
const flags = reader.readUint16();
@@ -105,7 +118,9 @@ exports.parseResponse = (u8) => {
105118
const additionalCount = reader.readUint16();
106119
/* eslint-enable no-unused-vars */
107120

108-
if (questionsCount !== 1) return null;
121+
if (questionsCount !== 1) {
122+
return null;
123+
}
109124

110125
// Read question
111126
const hostname = readHostname(reader).join('.');
@@ -127,7 +142,9 @@ exports.parseResponse = (u8) => {
127142

128143
switch (recordType) {
129144
case queries.A: // A record
130-
if (rdLen !== 4) return null;
145+
if (rdLen !== 4) {
146+
return null;
147+
}
131148

132149
results.push({
133150
hostname: host,
@@ -149,7 +166,9 @@ exports.parseResponse = (u8) => {
149166
});
150167
break;
151168
default:
152-
for (let b = 0; b < rdLen; ++b) reader.readUint8();
169+
for (let b = 0; b < rdLen; ++b) {
170+
reader.readUint8();
171+
}
153172
break;
154173
}
155174
}

0 commit comments

Comments
 (0)