-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodejs.ml
More file actions
377 lines (295 loc) · 11.8 KB
/
Copy pathnodejs.ml
File metadata and controls
377 lines (295 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
(** Raw call for doing require("some_module") *)
module Bindings_utils = struct
let require_module s =
Js.Unsafe.fun_call
(Js.Unsafe.js_expr "require")
[|Js.Unsafe.inject (Js.string s)|]
let ( !@ ) f = Js.wrap_callback f
let ( !! ) o = Js.Unsafe.inject o
(** Get the field of a JavaScript Object *)
let ( <!> ) obj field = Js.Unsafe.get obj field
(** Same as console.log *)
let log obj = Firebug.console##log obj
(** Call method of a JavaScript object *)
let m = Js.Unsafe.meth_call
(** Inject something as a JS object, be sure its Js.t already,
functions seemingly exempt *)
let i = Js.Unsafe.inject
(** Turn a JavaScript Object into a string *)
let stringify o = Js._JSON##stringify o |> Js.to_string
(** Turn an OCaml string into a JavaScript string *)
let to_js_str s = Js.string s |> Js.Unsafe.inject
(** Turn a string into a JSON object *)
let json_of_string s = Js._JSON##parse (s |> Js.string)
(** Create a JavaScript Object out of an alist *)
let obj_of_alist a_l =
List.map (fun (key, value) -> (key, Js.Unsafe.inject value)) a_l
|> Array.of_list |> Js.Unsafe.obj
(** Turn JavaScript string array into OCaml list of string *)
let to_string_list g =
g |> Js.str_array |> Js.to_array |> Array.map Js.to_string |> Array.to_list
(** Turn OCaml list of strings into JavaScript string array *)
let of_string_list g =
g |> Array.of_list |> Array.map Js.string |> Js.array
(** Get all keys of an Object *)
let keys obj =
m (Js.Unsafe.variable "Object") "keys" [|obj|]
|> Js.to_array |> Array.map Js.to_string |> Array.to_list
(** Call a function for each value of each key in an Object, throw
away result *)
let for_each_iter ~f obj =
keys obj |> List.iter (fun k -> f (Js.Unsafe.get obj k))
(** Call a function for each value of each key in an Object, keep
result *)
let for_each_map ~f obj =
keys obj |> List.map (fun k -> f k (Js.Unsafe.get obj k))
let debug thing field =
Firebug.console##log (m (thing <!> field) "toString" [||]);
end
type error_arg = Js.error Js.opt
let __filename () = (Js.Unsafe.eval_string "__filename" : Js.js_string Js.t)
let __dirname () = (Js.Unsafe.eval_string "__dirname" : Js.js_string Js.t)
module Events = struct
class type event_emitter = object('self)
(* method on : Js.js_string Js.t -> 'a Js.callback -> unit Js.meth *)
method on_withNoArgs :
Js.js_string Js.t ->
(unit -> unit) Js.callback ->
unit Js.meth
method on_WithOneArg :
Js.js_string Js.t ->
(Js.Unsafe.any Js.t -> unit) Js.callback ->
unit Js.meth
method on_WithTwoArg :
Js.js_string Js.t ->
(Js.Unsafe.any Js.t -> Js.Unsafe.any Js.t -> unit) Js.callback ->
unit Js.meth
method emit : Js.js_string Js.t -> unit Js.meth
method emit_withArg : Js.js_string Js.t -> 'a Js.t -> unit Js.meth
method emit_withTwoArgs :
Js.js_string Js.t -> 'a Js.t -> 'b Js.t -> unit Js.meth
method getMaxListeners : int Js.meth
method once : Js.js_string Js.t -> 'a Js.callback -> unit Js.meth
method addListener : Js.js_string Js.t -> 'a Js.callback -> unit Js.meth
method eventNames : Js.string_array Js.t Js.meth
end
let event_emitter : event_emitter Js.t Js.constr =
Bindings_utils.require_module "events"
end
module Buffer = struct
class type buffer = object
method length : int Js.readonly_prop
method toString : Js.js_string Js.t Js.meth
method toJSON : 'a. 'a Js.t Js.meth
method swap16 : buffer Js.t Js.meth
method swap32 : buffer Js.t Js.meth
end
class type buffer_static = object
method from_array : Typed_array.uint16Array -> buffer Js.t Js.meth
method from : Js.js_string Js.t -> buffer Js.t Js.meth
method from_withEncoding :
Js.js_string Js.t -> Js.js_string Js.t -> buffer Js.t Js.meth
method alloc : int -> buffer Js.t Js.meth
method compare : buffer Js.t -> buffer Js.t -> bool Js.t Js.meth
method isBuffer : 'a Js.t -> bool Js.t Js.meth
method isEncoding : Js.js_string Js.t -> bool Js.t Js.meth
end
let buffer_static : buffer_static Js.t = Js.Unsafe.pure_js_expr "Buffer"
end
module Stream = struct
class type stream = object('self)
inherit Events.event_emitter
end
class type writable_stream = object('self)
inherit stream
(* method cork *)
(* method end_ *)
(* method setDefaultEncoding *)
(* method uncork *)
(* method write *)
end
class type readable_stream = object('self)
inherit stream
method isPaused : bool Js.t Js.meth
method pause : readable_stream Js.t Js.meth
method pipe : 'self Js.t -> writable_stream Js.t Js.meth
(* method pipe_with_options : *)
(* writable_stream Js.t -> *)
(* #stream Js.t Js.meth *)
method read : 'a Js.t Js.opt Js.meth
method read_withAmount : int -> 'a Js.t Js.opt Js.meth
method resume : readable_stream Js.t Js.meth
method setEncoding : Js.js_string Js.t -> readable_stream Js.t Js.meth
method unpipe : unit Js.meth
method unpipe_withDest : writable_stream Js.t -> unit Js.meth
method unshift : Buffer.buffer Js.t -> unit Js.meth
method unshift_withString : Js.js_string Js.t -> unit Js.meth
end
let stream : stream Js.t = Bindings_utils.require_module "stream"
end
module Net = struct
class type socket = object
(* Actually they are both read and writeable *)
inherit Stream.readable_stream
method address : 'a Js.t Js.meth
method bufferSize : int Js.readonly_prop
method bytesRead : int Js.readonly_prop
method bytesWritten : int Js.readonly_prop
method connect :
<port : int Js.prop;
host: Js.js_string Js.t Js.prop;
localAddress : Js.js_string Js.t Js.prop;
localPort: int Js.prop;
family : int Js.prop;
hints : int Js.prop;
lookup: 'a Js.callback Js.opt Js.t Js.prop> Js.t
(* Many more connect methods needed *)
(* method connect_with_listener : *)
method connecting : bool Js.t Js.readonly_prop
method destroy : unit Js.meth
method destroy_with_exception : 'a Js.t -> unit Js.meth
method destroyed : bool Js.t Js.readonly_prop
method end_ : unit Js.meth
method end_with_data : Buffer.buffer Js.t -> unit Js.meth
method end_with_data_and_encoding :
Buffer.buffer Js.t -> Js.js_string Js.t -> unit Js.meth
method localAddress : Js.js_string Js.t Js.readonly_prop
method localPort : int Js.readonly_prop
(* method! pause : unit Js.meth *)
method ref : unit Js.meth
method remoteAddress : Js.js_string Js.t Js.readonly_prop
method remoteFamily : Js.js_string Js.t Js.readonly_prop
method remotePort : int Js.readonly_prop
(* method resume : unit Js.meth *)
(* method setEncoding : unit Js.meth *)
method setEncoding_with_encoding : Js.js_string Js.t -> unit Js.meth
method setKeepAlive : socket Js.t Js.meth
method setKeepAlive_withDelay : bool Js.t-> int -> socket Js.t Js.meth
method setNoDelay : socket Js.t Js.meth
method setNoDeplay_withBool : bool Js.t -> socket Js.t Js.meth
method write : Buffer.buffer Js.t -> unit Js.meth
method write_withEncoding :
Buffer.buffer Js.t -> Js.js_string Js.t -> unit Js.meth
method write_withEncodingAndCallback :
Buffer.buffer Js.t -> Js.js_string Js.t -> 'a Js.callback -> unit Js.meth
end
and server = object
inherit Events.event_emitter
method address : <port: int Js.readonly_prop;
family : Js.js_string Js.t Js.readonly_prop;
address : Js.js_string Js.t Js.readonly_prop > Js.t Js.meth
method close : unit Js.meth
method close_withCallback : (error_arg -> unit) Js.callback Js.meth
method getConnections : (error_arg -> int -> unit) Js.callback Js.meth
(* Later type it, a server or socket *)
method listen : int -> (unit -> unit) Js.callback -> unit Js.meth
method maxConnections : int Js.prop
method listening : bool Js.t Js.readonly_prop
end
and net = object
(* method connect : <port : int Js.prop> Js.t *)
method createServer : server Js.t Js.meth
method createServer_withConnListener :
(socket Js.t -> unit) Js.callback -> server Js.t Js.meth
method createServer_withOptionsAndConnListener :
<allowHalfOpen: bool Js.t Js.prop;
pauseOnConnect : bool Js.t Js.prop> Js.t ->
server Js.t Js.meth
end
(* let socket_with_options : 'a. *)
(* (<fd : 'a Js.opt Js.t Js.prop; *)
(* allowHalfOpen : bool Js.t Js.prop; *)
(* readable: bool Js.t Js.prop; *)
(* writable : bool Js.t Js.prop> Js.t -> socket Js.t) Js.constr = fun item -> *)
(* (Bindings_utils.require_module "net")##.Socket *)
let socket : socket Js.t Js.constr =
(Bindings_utils.require_module "net")##.Socket
let net : net Js.t = Bindings_utils.require_module "net"
end
(* module Dns = struct *)
(* class type dns = object *)
(* method lookup : *)
(* Js.js_string Js.t -> *)
(* (Js.error -> Js.js_string Js.t -> int -> unit) Js.callback -> *)
(* unit Js.meth *)
(* method resolve4 : *)
(* Js.js_string Js.t -> *)
(* (Js.string_array Js.t -> unit) Js.callback -> *)
(* unit Js.meth *)
(* end *)
(* let dns : dns Js.t = Bindings_utils.require_module "dns" *)
(* end *)
module Fs = struct
class type fs = object
method readFileSync :
Js.js_string Js.t ->
<encoding: Js.js_string Js.t Js.Opt.t Js.readonly_prop;
flag : Js.js_string Js.t Js.readonly_prop> Js.t ->
Buffer.buffer Js.t Js.meth
method readFile :
Js.js_string Js.t ->
(Js.error Js.t -> Buffer.buffer Js.t -> unit) Js.callback ->
unit Js.meth
end
let fs : fs Js.t = Bindings_utils.require_module "fs"
end
module Os = struct
class type os = object
method _EOL : Js.js_string Js.t Js.readonly_prop
method arch : Js.js_string Js.t Js.meth
(* method constants *)
method cpus :
<model : Js.js_string Js.t Js.readonly_prop;
speed : int Js.readonly_prop;
times : <user: int Js.readonly_prop;
nice : int Js.readonly_prop;
sys: int Js.readonly_prop;
idle : int Js.readonly_prop;
irq : int Js.readonly_prop> Js.t Js.readonly_prop>
Js.t Js.js_array Js.t Js.meth
method endianness : Js.js_string Js.t Js.meth
method freemem : int Js.meth
method homedir : Js.js_string Js.t Js.meth
method hostname : Js.js_string Js.t Js.meth
method loadavg : int Js.js_array Js.t Js.meth
end
let os : os Js.t = Bindings_utils.require_module "os"
end
module Http = struct
class type agent = object
(* method createConnection *)
(* method destroy *)
(* method freeSockets *)
(* method getName *)
(* method maxFreeSockets *)
(* method maxSockets *)
(* method requests *)
(* method sockets *)
end
class type client_request = object
inherit Stream.writable_stream
end
let agent : agent Js.t = (Bindings_utils.require_module "http")##.Agent
let agent_with_options :
(<keepAlive: bool Js.t Js.readonly_prop;
keepAliveMsecs : int Js.readonly_prop;
maxSockets : int Js.readonly_prop;
maxFreeSockets : int Js.readonly_prop> Js.t -> agent Js.t) Js.constr =
(Bindings_utils.require_module "http")##.Agent
end
class type process = object
inherit Events.event_emitter
method argv : Js.js_string Js.t Js.js_array Js.t Js.readonly_prop
method abort : unit Js.meth
method arch : Js.js_string Js.t Js.readonly_prop
method chdir : Js.js_string Js.t -> unit Js.meth
method config : 'a Js.t Js.readonly_prop
method connected : bool Js.t Js.readonly_prop
method cwd : Js.js_string Js.t Js.meth
method disconnect : unit Js.meth
method env : 'a Js.t Js.readonly_prop
(* Many more missing *)
method pid : int Js.readonly_prop
method exit : int -> unit Js.meth
end
let process : process Js.t = Js.Unsafe.variable "process"