AmberCli.st 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. Smalltalk current createPackage: 'AmberCli'!
  2. Object subclass: #AmberCli
  3. instanceVariableNames: ''
  4. package: 'AmberCli'!
  5. !AmberCli class methodsFor: 'commandline'!
  6. commandLineSwitches
  7. "Collect all methodnames from the 'commands' protocol of the class
  8. and select the ones with only one parameter.
  9. Then remove the ':' at the end of the name.
  10. Additionally all uppercase letters are made lowercase and preceded by a '-'.
  11. Example: fallbackPage: becomes --fallback-page.
  12. Return the Array containing the commandline switches."
  13. | switches |
  14. switches := ((self class methodsInProtocol: 'commands') collect: [ :each | each selector]).
  15. switches := switches select: [ :each | each match: '^[^:]*:$'].
  16. switches :=switches collect: [ :each |
  17. (each allButLast replace: '([A-Z])' with: '-$1') asLowercase].
  18. ^switches
  19. !
  20. handleArguments: args
  21. | command |
  22. command := self selectorForCommandLineSwitch: (args first).
  23. args remove: args first.
  24. self perform: command withArguments: (Array with: args).
  25. !
  26. selectorForCommandLineSwitch: aSwitch
  27. "Add ':' at the end
  28. and replace all occurences of a lowercase letter preceded by a '-' with
  29. the Uppercase letter.
  30. Example: fallback-page becomes fallbackPage:"
  31. ^(aSwitch replace: '-[a-z]' with: [ :each | each second asUppercase ]), ':'
  32. ! !
  33. !AmberCli class methodsFor: 'commands'!
  34. help: args
  35. console log: 'Available Commands:'.
  36. self commandLineSwitches do: [ :each | console log: each ]
  37. !
  38. repl: args
  39. ^Repl new createInterface
  40. !
  41. serve: args
  42. ^(FileServer createServerWithArguments: args) start
  43. ! !
  44. !AmberCli class methodsFor: 'startup'!
  45. main
  46. "Main entry point for Amber applications.
  47. Parses commandline arguments and starts the according subprogram."
  48. | args |
  49. args := process argv.
  50. "Remove the first args which contain the path to the node executable and the script file."
  51. args removeFrom: 1 to: 3.
  52. (args isEmpty)
  53. ifTrue: [self help: nil]
  54. ifFalse: [^self handleArguments: args]
  55. ! !
  56. Object subclass: #FileServer
  57. instanceVariableNames: 'path http fs url host port basePath util username password fallbackPage'
  58. package: 'AmberCli'!
  59. !FileServer commentStamp!
  60. I am the Amber Smalltalk FileServer.
  61. My runtime requirement is a functional Node.js executable.
  62. To start a FileServer instance on port `4000` use the following code:
  63. FileServer new start
  64. A parameterized instance can be created with the following code:
  65. FileServer createServerWithArguments: options
  66. Here, `options` is an array of commandline style strings each followed by a value e.g. `#('--port', '6000', '--host', '0.0.0.0')`.
  67. A list of all available parameters can be printed to the commandline by passing `--help` as parameter.
  68. See the `Options` section for further details on how options are mapped to instance methods.
  69. After startup FileServer checks if the directory layout required by Amber is present and logs a warning on absence.
  70. ## Options
  71. Each option is of the form `--some-option-string` which is transformed into a selector of the format `someOptionString:`.
  72. The trailing `--` gets removed, each `-[a-z]` gets transformed into the according uppercase letter, and a `:` is appended to create a selector which takes a single argument.
  73. Afterwards, the selector gets executed on the `FileServer` instance with the value following in the options array as parameter.
  74. ## Adding new commandline parameters
  75. Adding new commandline parameters to `FileServer` is as easy as adding a new single parameter method to the `accessing` protocol.!
  76. !FileServer methodsFor: 'accessing'!
  77. basePath
  78. ^basePath ifNil: ['./']
  79. !
  80. basePath: aString
  81. basePath := aString
  82. !
  83. fallbackPage
  84. ^fallbackPage
  85. !
  86. fallbackPage: aString
  87. fallbackPage := aString
  88. !
  89. host
  90. ^host
  91. !
  92. host: hostname
  93. host := hostname
  94. !
  95. password: aPassword
  96. password := aPassword.
  97. !
  98. port
  99. ^port
  100. !
  101. port: aNumber
  102. port := aNumber
  103. !
  104. username: aUsername
  105. username := aUsername.
  106. ! !
  107. !FileServer methodsFor: 'initialization'!
  108. checkDirectoryLayout
  109. (fs existsSync: self basePath, 'index.html') ifFalse: [
  110. console warn: 'Warning: project directory does not contain index.html'].
  111. (fs existsSync: self basePath, 'st') ifFalse: [
  112. console warn: 'Warning: project directory is missing an "st" directory'].
  113. (fs existsSync: self basePath, 'js') ifFalse: [
  114. console warn: 'Warning: project directory is missing a "js" directory'].
  115. !
  116. initialize
  117. super initialize.
  118. path := self require: 'path'.
  119. http := self require: 'http'.
  120. fs := self require: 'fs'.
  121. util := self require: 'util'.
  122. url := self require: 'url'.
  123. host := self class defaultHost.
  124. port := self class defaultPort.
  125. username := nil.
  126. password := nil.
  127. fallbackPage := nil.
  128. ! !
  129. !FileServer methodsFor: 'private'!
  130. base64Decode: aString
  131. <return (new Buffer(aString, 'base64').toString())>
  132. !
  133. isAuthenticated: aRequest
  134. "Basic HTTP Auth: http://stackoverflow.com/a/5957629/293175
  135. and https://gist.github.com/1686663"
  136. | header token auth parts|
  137. (username isNil and: [password isNil]) ifTrue: [^true].
  138. "get authentication header"
  139. header := (aRequest headers at: 'authorization') ifNil:[''].
  140. (header isEmpty)
  141. ifTrue: [^false]
  142. ifFalse: [
  143. "get authentication token"
  144. token := (header tokenize: ' ') ifNil:[''].
  145. "convert back from base64"
  146. auth := self base64Decode: (token at: 2).
  147. "split token at colon"
  148. parts := auth tokenize: ':'.
  149. ((username = (parts at: 1)) and: [password = (parts at: 2)])
  150. ifTrue: [^true]
  151. ifFalse: [^false]
  152. ].
  153. !
  154. require: aModuleString
  155. "call to the require function"
  156. ^require value: aModuleString
  157. !
  158. writeData: data toFileNamed: aFilename
  159. console log: aFilename
  160. ! !
  161. !FileServer methodsFor: 'request handling'!
  162. handleGETRequest: aRequest respondTo: aResponse
  163. | uri filename |
  164. uri := (url parse: aRequest url) pathname.
  165. filename := path join: self basePath with: uri.
  166. fs exists: filename do: [:aBoolean |
  167. aBoolean
  168. ifFalse: [self respondNotFoundTo: aResponse]
  169. ifTrue: [self respondFileNamed: filename to: aResponse]]
  170. !
  171. handleOPTIONSRequest: aRequest respondTo: aResponse
  172. aResponse writeHead: 200 options: #{'Access-Control-Allow-Origin' -> '*'.
  173. 'Access-Control-Allow-Methods' -> 'GET, PUT, POST, DELETE, OPTIONS'.
  174. 'Access-Control-Allow-Headers' -> 'Content-Type, Accept'.
  175. 'Content-Length' -> 0.
  176. 'Access-Control-Max-Age' -> 10}.
  177. aResponse end
  178. !
  179. handlePUTRequest: aRequest respondTo: aResponse
  180. | file stream |
  181. (self isAuthenticated: aRequest)
  182. ifFalse: [self respondAuthenticationRequiredTo: aResponse. ^nil].
  183. file := '.', aRequest url.
  184. stream := fs createWriteStream: file.
  185. stream on: 'error' do: [:error |
  186. console warn: 'Error creating WriteStream for file ', file.
  187. console warn: ' Did you forget to create the necessary js/ or st/ directory in your project?'.
  188. console warn: ' The exact error is: ', error.
  189. self respondNotCreatedTo: aResponse].
  190. stream on: 'close' do: [
  191. self respondCreatedTo: aResponse].
  192. aRequest setEncoding: 'utf8'.
  193. aRequest on: 'data' do: [:data |
  194. stream write: data].
  195. aRequest on: 'end' do: [
  196. stream writable ifTrue: [stream end]]
  197. !
  198. handleRequest: aRequest respondTo: aResponse
  199. aRequest method = 'PUT'
  200. ifTrue: [self handlePUTRequest: aRequest respondTo: aResponse].
  201. aRequest method = 'GET'
  202. ifTrue:[self handleGETRequest: aRequest respondTo: aResponse].
  203. aRequest method = 'OPTIONS'
  204. ifTrue:[self handleOPTIONSRequest: aRequest respondTo: aResponse]
  205. !
  206. respondAuthenticationRequiredTo: aResponse
  207. aResponse
  208. writeHead: 401 options: #{'WWW-Authenticate' -> 'Basic realm="Secured Developer Area"'};
  209. write: '<html><body>Authentication needed</body></html>';
  210. end.
  211. !
  212. respondCreatedTo: aResponse
  213. aResponse
  214. writeHead: 201 options: #{'Content-Type' -> 'text/plain'. 'Access-Control-Allow-Origin' -> '*'};
  215. end.
  216. !
  217. respondFileNamed: aFilename to: aResponse
  218. | type filename |
  219. filename := aFilename.
  220. (fs statSync: aFilename) isDirectory ifTrue: [
  221. filename := filename, 'index.html'].
  222. fs readFile: filename do: [:ex :file |
  223. ex notNil
  224. ifTrue: [
  225. console log: filename, ' does not exist'.
  226. self respondInternalErrorTo: aResponse]
  227. ifFalse: [
  228. type := self class mimeTypeFor: filename.
  229. type = 'application/javascript'
  230. ifTrue: [ type:=type,';charset=utf-8' ].
  231. aResponse
  232. writeHead: 200 options: #{'Content-Type' -> type};
  233. write: file encoding: 'binary';
  234. end]]
  235. !
  236. respondInternalErrorTo: aResponse
  237. aResponse
  238. writeHead: 500 options: #{'Content-Type' -> 'text/plain'};
  239. write: '500 Internal server error';
  240. end
  241. !
  242. respondNotCreatedTo: aResponse
  243. aResponse
  244. writeHead: 400 options: #{'Content-Type' -> 'text/plain'};
  245. write: 'File could not be created. Did you forget to create the st/js directories on the server?';
  246. end.
  247. !
  248. respondNotFoundTo: aResponse
  249. self fallbackPage isNil ifFalse: [^self respondFileNamed: self fallbackPage to: aResponse].
  250. aResponse
  251. writeHead: 404 options: #{'Content-Type' -> 'text/plain'};
  252. write: '404 Not found';
  253. end
  254. !
  255. respondOKTo: aResponse
  256. aResponse
  257. writeHead: 200 options: #{'Content-Type' -> 'text/plain'. 'Access-Control-Allow-Origin' -> '*'};
  258. end.
  259. ! !
  260. !FileServer methodsFor: 'starting'!
  261. start
  262. "Checks if required directory layout is present (issue warning if not).
  263. Afterwards start the server."
  264. self checkDirectoryLayout.
  265. (http createServer: [:request :response |
  266. self handleRequest: request respondTo: response])
  267. on: 'error' do: [:error | console log: 'Error starting server: ', error];
  268. on: 'listening' do: [console log: 'Starting file server on ', self host, ':', self port asString];
  269. listen: self port host: self host.
  270. !
  271. startOn: aPort
  272. self port: aPort.
  273. self start
  274. ! !
  275. FileServer class instanceVariableNames: 'mimeTypes'!
  276. !FileServer class methodsFor: 'accessing'!
  277. commandLineSwitches
  278. "Collect all methodnames from the 'accessing' protocol
  279. and select the ones with only one parameter.
  280. Then remove the ':' at the end of the name
  281. and add a '--' at the beginning.
  282. Additionally all uppercase letters are made lowercase and preceded by a '-'.
  283. Example: fallbackPage: becomes --fallback-page.
  284. Return the Array containing the commandline switches."
  285. | switches |
  286. switches := ((self methodsInProtocol: 'accessing') collect: [ :each | each selector]).
  287. switches := switches select: [ :each | each match: '^[^:]*:$'].
  288. switches :=switches collect: [ :each |
  289. (each allButLast replace: '([A-Z])' with: '-$1') asLowercase replace: '^([a-z])' with: '--$1' ].
  290. ^switches
  291. !
  292. defaultHost
  293. ^'127.0.0.1'
  294. !
  295. defaultMimeTypes
  296. ^ #{
  297. '%' -> 'application/x-trash'.
  298. '323' -> 'text/h323'.
  299. 'abw' -> 'application/x-abiword'.
  300. 'ai' -> 'application/postscript'.
  301. 'aif' -> 'audio/x-aiff'.
  302. 'aifc' -> 'audio/x-aiff'.
  303. 'aiff' -> 'audio/x-aiff'.
  304. 'alc' -> 'chemical/x-alchemy'.
  305. 'art' -> 'image/x-jg'.
  306. 'asc' -> 'text/plain'.
  307. 'asf' -> 'video/x-ms-asf'.
  308. 'asn' -> 'chemical/x-ncbi-asn1-spec'.
  309. 'aso' -> 'chemical/x-ncbi-asn1-binary'.
  310. 'asx' -> 'video/x-ms-asf'.
  311. 'au' -> 'audio/basic'.
  312. 'avi' -> 'video/x-msvideo'.
  313. 'b' -> 'chemical/x-molconn-Z'.
  314. 'bak' -> 'application/x-trash'.
  315. 'bat' -> 'application/x-msdos-program'.
  316. 'bcpio' -> 'application/x-bcpio'.
  317. 'bib' -> 'text/x-bibtex'.
  318. 'bin' -> 'application/octet-stream'.
  319. 'bmp' -> 'image/x-ms-bmp'.
  320. 'book' -> 'application/x-maker'.
  321. 'bsd' -> 'chemical/x-crossfire'.
  322. 'c' -> 'text/x-csrc'.
  323. 'c++' -> 'text/x-c++src'.
  324. 'c3d' -> 'chemical/x-chem3d'.
  325. 'cac' -> 'chemical/x-cache'.
  326. 'cache' -> 'chemical/x-cache'.
  327. 'cascii' -> 'chemical/x-cactvs-binary'.
  328. 'cat' -> 'application/vnd.ms-pki.seccat'.
  329. 'cbin' -> 'chemical/x-cactvs-binary'.
  330. 'cc' -> 'text/x-c++src'.
  331. 'cdf' -> 'application/x-cdf'.
  332. 'cdr' -> 'image/x-coreldraw'.
  333. 'cdt' -> 'image/x-coreldrawtemplate'.
  334. 'cdx' -> 'chemical/x-cdx'.
  335. 'cdy' -> 'application/vnd.cinderella'.
  336. 'cef' -> 'chemical/x-cxf'.
  337. 'cer' -> 'chemical/x-cerius'.
  338. 'chm' -> 'chemical/x-chemdraw'.
  339. 'chrt' -> 'application/x-kchart'.
  340. 'cif' -> 'chemical/x-cif'.
  341. 'class' -> 'application/java-vm'.
  342. 'cls' -> 'text/x-tex'.
  343. 'cmdf' -> 'chemical/x-cmdf'.
  344. 'cml' -> 'chemical/x-cml'.
  345. 'cod' -> 'application/vnd.rim.cod'.
  346. 'com' -> 'application/x-msdos-program'.
  347. 'cpa' -> 'chemical/x-compass'.
  348. 'cpio' -> 'application/x-cpio'.
  349. 'cpp' -> 'text/x-c++src'.
  350. 'cpt' -> 'image/x-corelphotopaint'.
  351. 'crl' -> 'application/x-pkcs7-crl'.
  352. 'crt' -> 'application/x-x509-ca-cert'.
  353. 'csf' -> 'chemical/x-cache-csf'.
  354. 'csh' -> 'text/x-csh'.
  355. 'csm' -> 'chemical/x-csml'.
  356. 'csml' -> 'chemical/x-csml'.
  357. 'css' -> 'text/css'.
  358. 'csv' -> 'text/comma-separated-values'.
  359. 'ctab' -> 'chemical/x-cactvs-binary'.
  360. 'ctx' -> 'chemical/x-ctx'.
  361. 'cu' -> 'application/cu-seeme'.
  362. 'cub' -> 'chemical/x-gaussian-cube'.
  363. 'cxf' -> 'chemical/x-cxf'.
  364. 'cxx' -> 'text/x-c++src'.
  365. 'dat' -> 'chemical/x-mopac-input'.
  366. 'dcr' -> 'application/x-director'.
  367. 'deb' -> 'application/x-debian-package'.
  368. 'dif' -> 'video/dv'.
  369. 'diff' -> 'text/plain'.
  370. 'dir' -> 'application/x-director'.
  371. 'djv' -> 'image/vnd.djvu'.
  372. 'djvu' -> 'image/vnd.djvu'.
  373. 'dl' -> 'video/dl'.
  374. 'dll' -> 'application/x-msdos-program'.
  375. 'dmg' -> 'application/x-apple-diskimage'.
  376. 'dms' -> 'application/x-dms'.
  377. 'doc' -> 'application/msword'.
  378. 'dot' -> 'application/msword'.
  379. 'dv' -> 'video/dv'.
  380. 'dvi' -> 'application/x-dvi'.
  381. 'dx' -> 'chemical/x-jcamp-dx'.
  382. 'dxr' -> 'application/x-director'.
  383. 'emb' -> 'chemical/x-embl-dl-nucleotide'.
  384. 'embl' -> 'chemical/x-embl-dl-nucleotide'.
  385. 'ent' -> 'chemical/x-pdb'.
  386. 'eps' -> 'application/postscript'.
  387. 'etx' -> 'text/x-setext'.
  388. 'exe' -> 'application/x-msdos-program'.
  389. 'ez' -> 'application/andrew-inset'.
  390. 'fb' -> 'application/x-maker'.
  391. 'fbdoc' -> 'application/x-maker'.
  392. 'fch' -> 'chemical/x-gaussian-checkpoint'.
  393. 'fchk' -> 'chemical/x-gaussian-checkpoint'.
  394. 'fig' -> 'application/x-xfig'.
  395. 'flac' -> 'application/x-flac'.
  396. 'fli' -> 'video/fli'.
  397. 'fm' -> 'application/x-maker'.
  398. 'frame' -> 'application/x-maker'.
  399. 'frm' -> 'application/x-maker'.
  400. 'gal' -> 'chemical/x-gaussian-log'.
  401. 'gam' -> 'chemical/x-gamess-input'.
  402. 'gamin' -> 'chemical/x-gamess-input'.
  403. 'gau' -> 'chemical/x-gaussian-input'.
  404. 'gcd' -> 'text/x-pcs-gcd'.
  405. 'gcf' -> 'application/x-graphing-calculator'.
  406. 'gcg' -> 'chemical/x-gcg8-sequence'.
  407. 'gen' -> 'chemical/x-genbank'.
  408. 'gf' -> 'application/x-tex-gf'.
  409. 'gif' -> 'image/gif'.
  410. 'gjc' -> 'chemical/x-gaussian-input'.
  411. 'gjf' -> 'chemical/x-gaussian-input'.
  412. 'gl' -> 'video/gl'.
  413. 'gnumeric' -> 'application/x-gnumeric'.
  414. 'gpt' -> 'chemical/x-mopac-graph'.
  415. 'gsf' -> 'application/x-font'.
  416. 'gsm' -> 'audio/x-gsm'.
  417. 'gtar' -> 'application/x-gtar'.
  418. 'h' -> 'text/x-chdr'.
  419. 'h++' -> 'text/x-c++hdr'.
  420. 'hdf' -> 'application/x-hdf'.
  421. 'hh' -> 'text/x-c++hdr'.
  422. 'hin' -> 'chemical/x-hin'.
  423. 'hpp' -> 'text/x-c++hdr'.
  424. 'hqx' -> 'application/mac-binhex40'.
  425. 'hs' -> 'text/x-haskell'.
  426. 'hta' -> 'application/hta'.
  427. 'htc' -> 'text/x-component'.
  428. 'htm' -> 'text/html'.
  429. 'html' -> 'text/html'.
  430. 'hxx' -> 'text/x-c++hdr'.
  431. 'ica' -> 'application/x-ica'.
  432. 'ice' -> 'x-conference/x-cooltalk'.
  433. 'ico' -> 'image/x-icon'.
  434. 'ics' -> 'text/calendar'.
  435. 'icz' -> 'text/calendar'.
  436. 'ief' -> 'image/ief'.
  437. 'iges' -> 'model/iges'.
  438. 'igs' -> 'model/iges'.
  439. 'iii' -> 'application/x-iphone'.
  440. 'inp' -> 'chemical/x-gamess-input'.
  441. 'ins' -> 'application/x-internet-signup'.
  442. 'iso' -> 'application/x-iso9660-image'.
  443. 'isp' -> 'application/x-internet-signup'.
  444. 'ist' -> 'chemical/x-isostar'.
  445. 'istr' -> 'chemical/x-isostar'.
  446. 'jad' -> 'text/vnd.sun.j2me.app-descriptor'.
  447. 'jar' -> 'application/java-archive'.
  448. 'java' -> 'text/x-java'.
  449. 'jdx' -> 'chemical/x-jcamp-dx'.
  450. 'jmz' -> 'application/x-jmol'.
  451. 'jng' -> 'image/x-jng'.
  452. 'jnlp' -> 'application/x-java-jnlp-file'.
  453. 'jpe' -> 'image/jpeg'.
  454. 'jpeg' -> 'image/jpeg'.
  455. 'jpg' -> 'image/jpeg'.
  456. 'js' -> 'application/javascript'.
  457. 'kar' -> 'audio/midi'.
  458. 'key' -> 'application/pgp-keys'.
  459. 'kil' -> 'application/x-killustrator'.
  460. 'kin' -> 'chemical/x-kinemage'.
  461. 'kpr' -> 'application/x-kpresenter'.
  462. 'kpt' -> 'application/x-kpresenter'.
  463. 'ksp' -> 'application/x-kspread'.
  464. 'kwd' -> 'application/x-kword'.
  465. 'kwt' -> 'application/x-kword'.
  466. 'latex' -> 'application/x-latex'.
  467. 'lha' -> 'application/x-lha'.
  468. 'lhs' -> 'text/x-literate-haskell'.
  469. 'lsf' -> 'video/x-la-asf'.
  470. 'lsx' -> 'video/x-la-asf'.
  471. 'ltx' -> 'text/x-tex'.
  472. 'lzh' -> 'application/x-lzh'.
  473. 'lzx' -> 'application/x-lzx'.
  474. 'm3u' -> 'audio/x-mpegurl'.
  475. 'm4a' -> 'audio/mpeg'.
  476. 'maker' -> 'application/x-maker'.
  477. 'man' -> 'application/x-troff-man'.
  478. 'mcif' -> 'chemical/x-mmcif'.
  479. 'mcm' -> 'chemical/x-macmolecule'.
  480. 'mdb' -> 'application/msaccess'.
  481. 'me' -> 'application/x-troff-me'.
  482. 'mesh' -> 'model/mesh'.
  483. 'mid' -> 'audio/midi'.
  484. 'midi' -> 'audio/midi'.
  485. 'mif' -> 'application/x-mif'.
  486. 'mm' -> 'application/x-freemind'.
  487. 'mmd' -> 'chemical/x-macromodel-input'.
  488. 'mmf' -> 'application/vnd.smaf'.
  489. 'mml' -> 'text/mathml'.
  490. 'mmod' -> 'chemical/x-macromodel-input'.
  491. 'mng' -> 'video/x-mng'.
  492. 'moc' -> 'text/x-moc'.
  493. 'mol' -> 'chemical/x-mdl-molfile'.
  494. 'mol2' -> 'chemical/x-mol2'.
  495. 'moo' -> 'chemical/x-mopac-out'.
  496. 'mop' -> 'chemical/x-mopac-input'.
  497. 'mopcrt' -> 'chemical/x-mopac-input'.
  498. 'mov' -> 'video/quicktime'.
  499. 'movie' -> 'video/x-sgi-movie'.
  500. 'mp2' -> 'audio/mpeg'.
  501. 'mp3' -> 'audio/mpeg'.
  502. 'mp4' -> 'video/mp4'.
  503. 'mpc' -> 'chemical/x-mopac-input'.
  504. 'mpe' -> 'video/mpeg'.
  505. 'mpeg' -> 'video/mpeg'.
  506. 'mpega' -> 'audio/mpeg'.
  507. 'mpg' -> 'video/mpeg'.
  508. 'mpga' -> 'audio/mpeg'.
  509. 'ms' -> 'application/x-troff-ms'.
  510. 'msh' -> 'model/mesh'.
  511. 'msi' -> 'application/x-msi'.
  512. 'mvb' -> 'chemical/x-mopac-vib'.
  513. 'mxu' -> 'video/vnd.mpegurl'.
  514. 'nb' -> 'application/mathematica'.
  515. 'nc' -> 'application/x-netcdf'.
  516. 'nwc' -> 'application/x-nwc'.
  517. 'o' -> 'application/x-object'.
  518. 'oda' -> 'application/oda'.
  519. 'odb' -> 'application/vnd.oasis.opendocument.database'.
  520. 'odc' -> 'application/vnd.oasis.opendocument.chart'.
  521. 'odf' -> 'application/vnd.oasis.opendocument.formula'.
  522. 'odg' -> 'application/vnd.oasis.opendocument.graphics'.
  523. 'odi' -> 'application/vnd.oasis.opendocument.image'.
  524. 'odm' -> 'application/vnd.oasis.opendocument.text-master'.
  525. 'odp' -> 'application/vnd.oasis.opendocument.presentation'.
  526. 'ods' -> 'application/vnd.oasis.opendocument.spreadsheet'.
  527. 'odt' -> 'application/vnd.oasis.opendocument.text'.
  528. 'ogg' -> 'application/ogg'.
  529. 'old' -> 'application/x-trash'.
  530. 'oth' -> 'application/vnd.oasis.opendocument.text-web'.
  531. 'oza' -> 'application/x-oz-application'.
  532. 'p' -> 'text/x-pascal'.
  533. 'p7r' -> 'application/x-pkcs7-certreqresp'.
  534. 'pac' -> 'application/x-ns-proxy-autoconfig'.
  535. 'pas' -> 'text/x-pascal'.
  536. 'pat' -> 'image/x-coreldrawpattern'.
  537. 'pbm' -> 'image/x-portable-bitmap'.
  538. 'pcf' -> 'application/x-font'.
  539. 'pcf.Z' -> 'application/x-font'.
  540. 'pcx' -> 'image/pcx'.
  541. 'pdb' -> 'chemical/x-pdb'.
  542. 'pdf' -> 'application/pdf'.
  543. 'pfa' -> 'application/x-font'.
  544. 'pfb' -> 'application/x-font'.
  545. 'pgm' -> 'image/x-portable-graymap'.
  546. 'pgn' -> 'application/x-chess-pgn'.
  547. 'pgp' -> 'application/pgp-signature'.
  548. 'pk' -> 'application/x-tex-pk'.
  549. 'pl' -> 'text/x-perl'.
  550. 'pls' -> 'audio/x-scpls'.
  551. 'pm' -> 'text/x-perl'.
  552. 'png' -> 'image/png'.
  553. 'pnm' -> 'image/x-portable-anymap'.
  554. 'pot' -> 'text/plain'.
  555. 'ppm' -> 'image/x-portable-pixmap'.
  556. 'pps' -> 'application/vnd.ms-powerpoint'.
  557. 'ppt' -> 'application/vnd.ms-powerpoint'.
  558. 'prf' -> 'application/pics-rules'.
  559. 'prt' -> 'chemical/x-ncbi-asn1-ascii'.
  560. 'ps' -> 'application/postscript'.
  561. 'psd' -> 'image/x-photoshop'.
  562. 'psp' -> 'text/x-psp'.
  563. 'py' -> 'text/x-python'.
  564. 'pyc' -> 'application/x-python-code'.
  565. 'pyo' -> 'application/x-python-code'.
  566. 'qt' -> 'video/quicktime'.
  567. 'qtl' -> 'application/x-quicktimeplayer'.
  568. 'ra' -> 'audio/x-realaudio'.
  569. 'ram' -> 'audio/x-pn-realaudio'.
  570. 'rar' -> 'application/rar'.
  571. 'ras' -> 'image/x-cmu-raster'.
  572. 'rd' -> 'chemical/x-mdl-rdfile'.
  573. 'rdf' -> 'application/rdf+xml'.
  574. 'rgb' -> 'image/x-rgb'.
  575. 'rm' -> 'audio/x-pn-realaudio'.
  576. 'roff' -> 'application/x-troff'.
  577. 'ros' -> 'chemical/x-rosdal'.
  578. 'rpm' -> 'application/x-redhat-package-manager'.
  579. 'rss' -> 'application/rss+xml'.
  580. 'rtf' -> 'text/rtf'.
  581. 'rtx' -> 'text/richtext'.
  582. 'rxn' -> 'chemical/x-mdl-rxnfile'.
  583. 'sct' -> 'text/scriptlet'.
  584. 'sd' -> 'chemical/x-mdl-sdfile'.
  585. 'sd2' -> 'audio/x-sd2'.
  586. 'sda' -> 'application/vnd.stardivision.draw'.
  587. 'sdc' -> 'application/vnd.stardivision.calc'.
  588. 'sdd' -> 'application/vnd.stardivision.impress'.
  589. 'sdf' -> 'chemical/x-mdl-sdfile'.
  590. 'sdp' -> 'application/vnd.stardivision.impress'.
  591. 'sdw' -> 'application/vnd.stardivision.writer'.
  592. 'ser' -> 'application/java-serialized-object'.
  593. 'sgf' -> 'application/x-go-sgf'.
  594. 'sgl' -> 'application/vnd.stardivision.writer-global'.
  595. 'sh' -> 'text/x-sh'.
  596. 'shar' -> 'application/x-shar'.
  597. 'shtml' -> 'text/html'.
  598. 'sid' -> 'audio/prs.sid'.
  599. 'sik' -> 'application/x-trash'.
  600. 'silo' -> 'model/mesh'.
  601. 'sis' -> 'application/vnd.symbian.install'.
  602. 'sit' -> 'application/x-stuffit'.
  603. 'skd' -> 'application/x-koan'.
  604. 'skm' -> 'application/x-koan'.
  605. 'skp' -> 'application/x-koan'.
  606. 'skt' -> 'application/x-koan'.
  607. 'smf' -> 'application/vnd.stardivision.math'.
  608. 'smi' -> 'application/smil'.
  609. 'smil' -> 'application/smil'.
  610. 'snd' -> 'audio/basic'.
  611. 'spc' -> 'chemical/x-galactic-spc'.
  612. 'spl' -> 'application/x-futuresplash'.
  613. 'src' -> 'application/x-wais-source'.
  614. 'stc' -> 'application/vnd.sun.xml.calc.template'.
  615. 'std' -> 'application/vnd.sun.xml.draw.template'.
  616. 'sti' -> 'application/vnd.sun.xml.impress.template'.
  617. 'stl' -> 'application/vnd.ms-pki.stl'.
  618. 'stw' -> 'application/vnd.sun.xml.writer.template'.
  619. 'sty' -> 'text/x-tex'.
  620. 'sv4cpio' -> 'application/x-sv4cpio'.
  621. 'sv4crc' -> 'application/x-sv4crc'.
  622. 'svg' -> 'image/svg+xml'.
  623. 'svgz' -> 'image/svg+xml'.
  624. 'sw' -> 'chemical/x-swissprot'.
  625. 'swf' -> 'application/x-shockwave-flash'.
  626. 'swfl' -> 'application/x-shockwave-flash'.
  627. 'sxc' -> 'application/vnd.sun.xml.calc'.
  628. 'sxd' -> 'application/vnd.sun.xml.draw'.
  629. 'sxg' -> 'application/vnd.sun.xml.writer.global'.
  630. 'sxi' -> 'application/vnd.sun.xml.impress'.
  631. 'sxm' -> 'application/vnd.sun.xml.math'.
  632. 'sxw' -> 'application/vnd.sun.xml.writer'.
  633. 't' -> 'application/x-troff'.
  634. 'tar' -> 'application/x-tar'.
  635. 'taz' -> 'application/x-gtar'.
  636. 'tcl' -> 'text/x-tcl'.
  637. 'tex' -> 'text/x-tex'.
  638. 'texi' -> 'application/x-texinfo'.
  639. 'texinfo' -> 'application/x-texinfo'.
  640. 'text' -> 'text/plain'.
  641. 'tgf' -> 'chemical/x-mdl-tgf'.
  642. 'tgz' -> 'application/x-gtar'.
  643. 'tif' -> 'image/tiff'.
  644. 'tiff' -> 'image/tiff'.
  645. 'tk' -> 'text/x-tcl'.
  646. 'tm' -> 'text/texmacs'.
  647. 'torrent' -> 'application/x-bittorrent'.
  648. 'tr' -> 'application/x-troff'.
  649. 'ts' -> 'text/texmacs'.
  650. 'tsp' -> 'application/dsptype'.
  651. 'tsv' -> 'text/tab-separated-values'.
  652. 'txt' -> 'text/plain'.
  653. 'udeb' -> 'application/x-debian-package'.
  654. 'uls' -> 'text/iuls'.
  655. 'ustar' -> 'application/x-ustar'.
  656. 'val' -> 'chemical/x-ncbi-asn1-binary'.
  657. 'vcd' -> 'application/x-cdlink'.
  658. 'vcf' -> 'text/x-vcard'.
  659. 'vcs' -> 'text/x-vcalendar'.
  660. 'vmd' -> 'chemical/x-vmd'.
  661. 'vms' -> 'chemical/x-vamas-iso14976'.
  662. 'vor' -> 'application/vnd.stardivision.writer'.
  663. 'vrm' -> 'x-world/x-vrml'.
  664. 'vrml' -> 'x-world/x-vrml'.
  665. 'vsd' -> 'application/vnd.visio'.
  666. 'wad' -> 'application/x-doom'.
  667. 'wav' -> 'audio/x-wav'.
  668. 'wax' -> 'audio/x-ms-wax'.
  669. 'wbmp' -> 'image/vnd.wap.wbmp'.
  670. 'wbxml' -> 'application/vnd.wap.wbxml'.
  671. 'wk' -> 'application/x-123'.
  672. 'wm' -> 'video/x-ms-wm'.
  673. 'wma' -> 'audio/x-ms-wma'.
  674. 'wmd' -> 'application/x-ms-wmd'.
  675. 'wml' -> 'text/vnd.wap.wml'.
  676. 'wmlc' -> 'application/vnd.wap.wmlc'.
  677. 'wmls' -> 'text/vnd.wap.wmlscript'.
  678. 'wmlsc' -> 'application/vnd.wap.wmlscriptc'.
  679. 'wmv' -> 'video/x-ms-wmv'.
  680. 'wmx' -> 'video/x-ms-wmx'.
  681. 'wmz' -> 'application/x-ms-wmz'.
  682. 'wp5' -> 'application/wordperfect5.1'.
  683. 'wpd' -> 'application/wordperfect'.
  684. 'wrl' -> 'x-world/x-vrml'.
  685. 'wsc' -> 'text/scriptlet'.
  686. 'wvx' -> 'video/x-ms-wvx'.
  687. 'wz' -> 'application/x-wingz'.
  688. 'xbm' -> 'image/x-xbitmap'.
  689. 'xcf' -> 'application/x-xcf'.
  690. 'xht' -> 'application/xhtml+xml'.
  691. 'xhtml' -> 'application/xhtml+xml'.
  692. 'xlb' -> 'application/vnd.ms-excel'.
  693. 'xls' -> 'application/vnd.ms-excel'.
  694. 'xlt' -> 'application/vnd.ms-excel'.
  695. 'xml' -> 'application/xml'.
  696. 'xpi' -> 'application/x-xpinstall'.
  697. 'xpm' -> 'image/x-xpixmap'.
  698. 'xsl' -> 'application/xml'.
  699. 'xtel' -> 'chemical/x-xtel'.
  700. 'xul' -> 'application/vnd.mozilla.xul+xml'.
  701. 'xwd' -> 'image/x-xwindowdump'.
  702. 'xyz' -> 'chemical/x-xyz'.
  703. 'zip' -> 'application/zip'.
  704. 'zmt' -> 'chemical/x-mopac-input'.
  705. '~' -> 'application/x-trash'
  706. }
  707. !
  708. defaultPort
  709. ^4000
  710. !
  711. mimeTypeFor: aString
  712. ^self mimeTypes at: (aString replace: '.*[\.]' with: '') ifAbsent: ['text/plain']
  713. !
  714. mimeTypes
  715. ^mimeTypes ifNil: [mimeTypes := self defaultMimeTypes]
  716. !
  717. printHelp
  718. console log: 'Available commandline options are:'.
  719. console log: '--help'.
  720. self commandLineSwitches do: [ :each |
  721. console log: each, ' <parameter>']
  722. !
  723. selectorForCommandLineSwitch: aSwitch
  724. "Remove the trailing '--', add ':' at the end
  725. and replace all occurences of a lowercase letter preceded by a '-' with
  726. the Uppercase letter.
  727. Example: --fallback-page becomes fallbackPage:"
  728. ^((aSwitch replace: '^--' with: '')
  729. replace: '-[a-z]' with: [ :each | each second asUppercase ]), ':'
  730. ! !
  731. !FileServer class methodsFor: 'initialization'!
  732. createServerWithArguments: options
  733. "If options are empty return a default FileServer instance.
  734. If options are given loop through them and set the passed in values
  735. on the FileServer instance.
  736. Commanline options map directly to methods in the 'accessing' protocol
  737. taking one parameter.
  738. Adding a method to this protocol makes it directly settable through
  739. command line options.
  740. "
  741. | server popFront front optionName optionValue switches |
  742. switches := self commandLineSwitches.
  743. server := self new.
  744. options ifEmpty: [^server].
  745. (options size even) ifFalse: [
  746. console log: 'Using default parameters.'.
  747. console log: 'Wrong commandline options or not enough arguments for: ' , options.
  748. console log: 'Use any of the following ones: ', switches.
  749. ^server].
  750. popFront := [:args |
  751. front := args first.
  752. args remove: front.
  753. front].
  754. [options notEmpty] whileTrue: [
  755. optionName := popFront value: options.
  756. optionValue := popFront value: options.
  757. (switches includes: optionName) ifTrue: [
  758. optionName := self selectorForCommandLineSwitch: optionName.
  759. server perform: optionName withArguments: (Array with: optionValue)]
  760. ifFalse: [
  761. console log: optionName, ' is not a valid commandline option'.
  762. console log: 'Use any of the following ones: ', switches ]].
  763. ^server.
  764. !
  765. main
  766. "Main entry point for Amber applications.
  767. Creates and starts a FileServer instance."
  768. | fileServer args |
  769. args := process argv.
  770. "Remove the first args which contain the path to the node executable and the script file."
  771. args removeFrom: 1 to: 3.
  772. args detect: [ :each |
  773. (each = '--help') ifTrue: [FileServer printHelp]]
  774. ifNone: [
  775. fileServer := FileServer createServerWithArguments: args.
  776. ^fileServer start]
  777. ! !
  778. Object subclass: #Repl
  779. instanceVariableNames: 'readline interface util'
  780. package: 'AmberCli'!
  781. !Repl commentStamp!
  782. I am a class representing a REPL (Read Evaluate Print Loop) and provide a command line interface to Amber Smalltalk.
  783. On the prompt you can type Amber statements which will be evaluated after pressing <Enter>.
  784. The evaluation is comparable with executing a 'DoIt' in a workspace.
  785. My runtime requirement is a functional Node.js executable with working Readline support.!
  786. !Repl methodsFor: 'accessing'!
  787. prompt
  788. ^'amber >> '
  789. ! !
  790. !Repl methodsFor: 'actions'!
  791. close
  792. process stdin destroy
  793. !
  794. createInterface
  795. "No completion for now"
  796. interface := readline createInterface: process stdin stdout: process stdout.
  797. interface on: 'line' do: [:buffer | self eval: buffer].
  798. interface on: 'close' do: [self close].
  799. self setPrompt.
  800. interface prompt
  801. !
  802. eval: buffer
  803. | result |
  804. buffer isEmpty ifFalse: [
  805. self try: [
  806. result := Compiler new evaluateExpression: buffer.
  807. Transcript show: result]
  808. catch: [:e |
  809. e isSmalltalkError
  810. ifTrue: [ErrorHandler new handleError: e]
  811. ifFalse: [process stdout write: e jsStack]]].
  812. interface prompt
  813. !
  814. setPrompt
  815. interface setPrompt: self prompt
  816. ! !
  817. !Repl methodsFor: 'initialization'!
  818. initialize
  819. super initialize.
  820. readline := require value: 'readline'.
  821. util := require value: 'util'
  822. ! !
  823. !Repl class methodsFor: 'not yet classified'!
  824. main
  825. self new createInterface
  826. ! !