Skip to content

Commit 6b9ca16

Browse files
committed
Setting up CDN
1 parent f873fea commit 6b9ca16

File tree

13 files changed

+3164
-0
lines changed

13 files changed

+3164
-0
lines changed

easycoder/plugins/anagrams.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// eslint-disable-next-line no-unused-vars
2+
const EasyCoder_Anagrams = {
3+
4+
name: `EasyCoder_Anagrams`,
5+
6+
value: {
7+
8+
compile: (compiler) => {
9+
if (compiler.tokenIs(`anagrams`)) {
10+
if (compiler.nextTokenIs(`of`)) {
11+
const value = compiler.getNextValue();
12+
return {
13+
domain: `anagrams`,
14+
type: `getAnagrams`,
15+
value
16+
};
17+
}
18+
}
19+
return null;
20+
},
21+
22+
get: (program, value) => {
23+
switch (value.type) {
24+
case `getAnagrams`:
25+
return {
26+
type: `constant`,
27+
numeric: false,
28+
content: JSON.stringify(AnagramFinder.getAnagrams(program.getValue(value.value), EasyCoder_words))
29+
};
30+
}
31+
return null;
32+
}
33+
},
34+
35+
getHandler: () => {
36+
return null;
37+
},
38+
39+
condition: {
40+
41+
compile: () => {}
42+
}
43+
};
44+
45+
// eslint-disable-next-line no-unused-vars
46+
EasyCoder.domain.anagrams = EasyCoder_Anagrams;

easycoder/plugins/aws.js

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
const EasyCoder_AWS = {
2+
3+
name: `EasyCoder_AWS`,
4+
5+
AWS: {
6+
7+
compile: (compiler) => {
8+
const lino = compiler.getLino();
9+
const request = compiler.nextToken();
10+
switch (request) {
11+
case `get`:
12+
if (compiler.nextIsSymbol(true)) {
13+
const targetRecord = compiler.getSymbolRecord();
14+
if (targetRecord.keyword === `variable`) {
15+
if (compiler.nextTokenIs(`from`)) {
16+
const url = compiler.getNextValue();
17+
let onError = null;
18+
if (compiler.tokenIs(`or`)) {
19+
compiler.next();
20+
onError = compiler.getPc() + 1;
21+
compiler.completeHandler();
22+
}
23+
compiler.addCommand({
24+
domain: `aws`,
25+
keyword: `aws`,
26+
lino,
27+
request: `get`,
28+
target: targetRecord.name,
29+
url,
30+
onError
31+
});
32+
return true;
33+
}
34+
}
35+
}
36+
break;
37+
case `post`:
38+
const value = compiler.getNextValue();
39+
let target = null;
40+
if (compiler.tokenIs(`giving`)) {
41+
if (compiler.nextIsSymbol()) {
42+
const targetRecord = compiler.getSymbolRecord();
43+
if (targetRecord.isVHolder) {
44+
target = targetRecord.name;
45+
compiler.next();
46+
} else {
47+
throw new Error(`'${targetRecord.name}' cannot hold a value`);
48+
}
49+
}
50+
}
51+
onError = null;
52+
if (compiler.tokenIs(`or`)) {
53+
compiler.next();
54+
onError = compiler.getPc() + 1;
55+
compiler.completeHandler();
56+
}
57+
compiler.addCommand({
58+
domain: `aws`,
59+
keyword: `aws`,
60+
lino,
61+
request: `post`,
62+
value,
63+
target,
64+
onError
65+
});
66+
return true;
67+
case `delete`:
68+
if (compiler.nextIsSymbol(true)) {
69+
const targetRecord = compiler.getSymbolRecord();
70+
if (targetRecord.keyword === `variable`) {
71+
let onError = null;
72+
if (compiler.tokenIs(`or`)) {
73+
compiler.next();
74+
onError = compiler.getPc() + 1;
75+
compiler.completeHandler();
76+
}
77+
compiler.next();
78+
compiler.addCommand({
79+
domain: `aws`,
80+
keyword: `aws`,
81+
lino,
82+
request: `delete`,
83+
target: targetRecord.name,
84+
onError
85+
});
86+
return true;
87+
}
88+
}
89+
break;
90+
case `set`:
91+
if (compiler.nextTokenIs(`the`)) {
92+
compiler.next();
93+
}
94+
if (compiler.tokenIs(`url`)) {
95+
if (compiler.nextTokenIs(`to`)) {
96+
const url = compiler.getNextValue();
97+
compiler.addCommand({
98+
domain: `aws`,
99+
keyword: `aws`,
100+
lino,
101+
request: `setUrl`,
102+
url
103+
});
104+
return true;
105+
}
106+
}
107+
return false;
108+
}
109+
return false;
110+
},
111+
112+
run: (program) => {
113+
const createCORSRequest = function (method, url) {
114+
let xhr = new XMLHttpRequest();
115+
if (`withCredentials` in xhr) {
116+
// Most browsers.
117+
xhr.open(method, url, true);
118+
} else if (typeof XDomainRequest != `undefined`) {
119+
// IE8 & IE9
120+
xhr = new XDomainRequest();
121+
xhr.open(method, url);
122+
} else {
123+
// CORS not supported.
124+
xhr = null;
125+
}
126+
return xhr;
127+
};
128+
129+
const command = program[program.pc];
130+
if (command.request === `setUrl`) {
131+
EasyCoder_AWS.url = program.getValue(command.url);
132+
return command.pc + 1;
133+
} else if ([`get`, `post`, `delete`].includes(command.request)) {
134+
const method = command.request.toUpperCase();
135+
const url = `${EasyCoder_AWS.url}${program.getValue(command.url)}`;
136+
const request = createCORSRequest(method, url);
137+
if (!request) {
138+
program.runtimeError(command.lino, `CORS not supported`);
139+
return command.pc + 1;
140+
}
141+
request.setRequestHeader(`Content-Type`, `application/json; charset=UTF-8`);
142+
request.command = command;
143+
switch (command.request) {
144+
case `get`:
145+
case `delete`:
146+
request.send();
147+
break;
148+
case `post`:
149+
const value = program.getValue(command.value);
150+
console.log(`POST to ${EasyCoder_AWS.url}`);
151+
if ([`[`, `{`].includes(value.charAt(0))) {
152+
request.setRequestHeader(`Content-Type`, `application/json; charset=UTF-8`);
153+
// console.log(`value=${value}`);
154+
request.send(value);
155+
} else {
156+
request.setRequestHeader(`Content-Type`, `text/plain; charset=UTF-8`);
157+
// console.log(`value=${program.encode(value)}`);
158+
request.send(program.encode(value));
159+
}
160+
break;
161+
}
162+
163+
request.onload = function () {
164+
var content = request.responseText;
165+
if (content.length > 0 && ![`[`, `{`].includes(content.charAt(0))) {
166+
content = program.decode(content);
167+
// } else {
168+
// content = JSON.parse(content);
169+
}
170+
if (command.target) {
171+
const targetRecord = program.getSymbolRecord(command.target);
172+
targetRecord.value[targetRecord.index] = {
173+
type: `constant`,
174+
numeric: false,
175+
content
176+
};
177+
}
178+
program.run(command.pc + 1);
179+
};
180+
181+
request.onerror = function () {
182+
if (command.onError) {
183+
program.errorMessage = this.responseText;
184+
program.run(command.onError);
185+
} else {
186+
const error = this.responseText;
187+
program.runtimeError(command.lino, error);
188+
}
189+
};
190+
191+
return 0;
192+
}
193+
}
194+
},
195+
196+
getHandler: (name) => {
197+
switch (name) {
198+
case `aws`:
199+
return EasyCoder_AWS.AWS;
200+
default:
201+
return null;
202+
}
203+
},
204+
205+
run: (program) => {
206+
const command = program[program.pc];
207+
const handler = EasyCoder_AWS.getHandler(command.keyword);
208+
if (!handler) {
209+
program.runtimeError(command.lino, `Unknown keyword '${command.keyword}' in 'aws' package`);
210+
}
211+
return handler.run(program);
212+
},
213+
214+
value: {
215+
216+
compile: () => {
217+
return null;
218+
},
219+
220+
get: () => {
221+
return null;
222+
}
223+
},
224+
225+
condition: {
226+
227+
compile: () => {},
228+
229+
test: () => {}
230+
}
231+
};
232+
233+
// eslint-disable-next-line no-unused-vars
234+
EasyCoder.domain.aws = EasyCoder_AWS;

0 commit comments

Comments
 (0)