AmberCli.st 28 KB

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