@naanlang/naan 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +46 -0
  3. package/dist/env_web.js +431 -0
  4. package/dist/naan.min.js +73 -0
  5. package/frameworks/browser/browser.nlg +30 -0
  6. package/frameworks/browser/pouchdb/pouchdb.min.js +7 -0
  7. package/frameworks/browser/spark-md5/spark-md5.min.js +1 -0
  8. package/frameworks/browser/sworker.js +192 -0
  9. package/frameworks/browser/terminals.nlg +787 -0
  10. package/frameworks/browser/workers.nlg +386 -0
  11. package/frameworks/client/apiclient.nlg +183 -0
  12. package/frameworks/client/client.nlg +29 -0
  13. package/frameworks/client/psm_client.nlg +572 -0
  14. package/frameworks/common/common.nlg +464 -0
  15. package/frameworks/common/preferences.nlg +131 -0
  16. package/frameworks/common/repltools.nlg +100 -0
  17. package/frameworks/common/watching.nlg +97 -0
  18. package/frameworks/node/apiserver.nlg +399 -0
  19. package/frameworks/node/filesystem.nlg +1012 -0
  20. package/frameworks/node/gitter.nlg +234 -0
  21. package/frameworks/node/node.nlg +81 -0
  22. package/frameworks/node/psm_server.nlg +465 -0
  23. package/frameworks/node/worker.nlg +560 -0
  24. package/frameworks/project/build.nlg +1229 -0
  25. package/frameworks/project/jszip.js +30 -0
  26. package/frameworks/project/jszip.nlg +87 -0
  27. package/frameworks/project/proj_cliser.nlg +139 -0
  28. package/frameworks/project/proj_console.nlg +137 -0
  29. package/frameworks/project/proj_folder.nlg +143 -0
  30. package/frameworks/project/proj_lambda.nlg +137 -0
  31. package/frameworks/project/proj_static.nlg +138 -0
  32. package/frameworks/project/project.nlg +29 -0
  33. package/frameworks/project/projects.nlg +587 -0
  34. package/frameworks/project/uglifyjs.js +1 -0
  35. package/frameworks/project/uglifyjs.nlg +79 -0
  36. package/frameworks/running/debugcom.nlg +163 -0
  37. package/frameworks/running/debugnub.nlg +565 -0
  38. package/frameworks/running/debugutil.nlg +510 -0
  39. package/frameworks/running/executors.nlg +767 -0
  40. package/frameworks/running/running.nlg +151 -0
  41. package/frameworks/running/sourcecode.nlg +346 -0
  42. package/frameworks/running/taskexec.nlg +196 -0
  43. package/frameworks/storage/dbt_pouch.nlg +773 -0
  44. package/frameworks/storage/file_manager.nlg +772 -0
  45. package/frameworks/storage/psm.nlg +326 -0
  46. package/frameworks/storage/psm_dbtables.nlg +814 -0
  47. package/frameworks/storage/resources.nlg +445 -0
  48. package/frameworks/storage/storage.nlg +52 -0
  49. package/lib/browser/env_web.js +431 -0
  50. package/lib/browser/env_webworker.js +146 -0
  51. package/lib/browser/require.js +1653 -0
  52. package/lib/core/naanlib.js +73 -0
  53. package/lib/env_node.js +282 -0
  54. package/lib/env_nodeworker.js +165 -0
  55. package/lib/node_repl.js +25 -0
  56. package/lib/node_repl_init.nlg +52 -0
  57. package/package.json +13 -0
  58. package/plugins/gitClient/clientops.nlg +317 -0
  59. package/plugins/gitClient/gitClient.nlg +63 -0
  60. package/plugins/gitClient/gitutils.nlg +1331 -0
  61. package/plugins/gitClient/minimatch.js +945 -0
  62. package/plugins/gitClient/minimatch_license.txt +15 -0
  63. package/plugins/gitClient/minimatch_readme.md +208 -0
  64. package/plugins/nodeLib/nodeLib.nlg +26 -0
  65. package/plugins/nodeLib/node_cp.nlg +65 -0
  66. package/plugins/nodeLib/node_fs.nlg +137 -0
  67. package/plugins/nodeLib/node_https.nlg +172 -0
  68. package/plugins/nodeLib/node_util.nlg +180 -0
  69. package/plugins/serviceAws/aws/aws-sdk.min.js +88 -0
  70. package/plugins/serviceAws/aws/lambda_rest_index.js +122 -0
  71. package/plugins/serviceAws/aws/lambda_rest_init.nlg +58 -0
  72. package/plugins/serviceAws/aws/lambda_ws_index.js +498 -0
  73. package/plugins/serviceAws/aws/lambda_ws_init.nlg +49 -0
  74. package/plugins/serviceAws/aws_cloudwatchlogs.nlg +122 -0
  75. package/plugins/serviceAws/aws_cognito.nlg +76 -0
  76. package/plugins/serviceAws/aws_dynamo.nlg +509 -0
  77. package/plugins/serviceAws/aws_lambda.nlg +95 -0
  78. package/plugins/serviceAws/aws_s3.nlg +134 -0
  79. package/plugins/serviceAws/aws_utils.nlg +86 -0
  80. package/plugins/serviceAws/dbt_aws.nlg +769 -0
  81. package/plugins/serviceAws/lambda_naan_control.nlg +175 -0
  82. package/plugins/serviceAws/psm_aws.nlg +258 -0
  83. package/plugins/serviceAws/serviceAws.nlg +38 -0
  84. package/plugins/serviceGitHub/gitops.nlg +522 -0
  85. package/plugins/serviceGitHub/psm_github.nlg +205 -0
  86. package/plugins/serviceGitHub/serviceGitHub.nlg +65 -0
  87. package/plugins/serviceGitLab/gitops.nlg +528 -0
  88. package/plugins/serviceGitLab/psm_gitlab.nlg +205 -0
  89. package/plugins/serviceGitLab/serviceGitLab.nlg +65 -0
  90. package/test/harness.nlg +312 -0
  91. package/test/node_test.js +33 -0
  92. package/test/test_01_core.nlg +768 -0
  93. package/test/test_02_context.nlg +269 -0
  94. package/test/test_03_jsinterop.nlg +137 -0
  95. package/test/test_04_numerics.nlg +450 -0
  96. package/test/test_05_strings.nlg +63 -0
  97. package/test/test_06_lingoparse.nlg +500 -0
  98. package/test/test_07_lingo.nlg +623 -0
@@ -0,0 +1,15 @@
1
+ The ISC License
2
+
3
+ Copyright (c) Isaac Z. Schlueter and Contributors
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any
6
+ purpose with or without fee is hereby granted, provided that the above
7
+ copyright notice and this permission notice appear in all copies.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
15
+ IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
@@ -0,0 +1,208 @@
1
+ # minimatch
2
+
3
+ A minimal matching utility.
4
+
5
+
6
+
7
+ This is the matching library used internally by npm.
8
+
9
+ It works by converting glob expressions into JavaScript `RegExp`
10
+ objects.
11
+
12
+ ## Usage
13
+
14
+ ```javascript
15
+ var minimatch = require("minimatch")
16
+
17
+ minimatch("bar.foo", "*.foo") // true!
18
+ minimatch("bar.foo", "*.bar") // false!
19
+ minimatch("bar.foo", "*.+(bar|foo)", { debug: true }) // true, and noisy!
20
+ ```
21
+
22
+ ## Features
23
+
24
+ Supports these glob features:
25
+
26
+ * Brace Expansion
27
+ * Extended glob matching
28
+ * "Globstar" `**` matching
29
+
30
+ See:
31
+
32
+ * `man sh`
33
+ * `man bash`
34
+ * `man 3 fnmatch`
35
+ * `man 5 gitignore`
36
+
37
+ ## Minimatch Class
38
+
39
+ Create a minimatch object by instantiating the `minimatch.Minimatch` class.
40
+
41
+ ```javascript
42
+ var Minimatch = require("minimatch").Minimatch
43
+ var mm = new Minimatch(pattern, options)
44
+ ```
45
+
46
+ ### Properties
47
+
48
+ * `pattern` The original pattern the minimatch object represents.
49
+ * `options` The options supplied to the constructor.
50
+ * `set` A 2-dimensional array of regexp or string expressions.
51
+ Each row in the
52
+ array corresponds to a brace-expanded pattern. Each item in the row
53
+ corresponds to a single path-part. For example, the pattern
54
+ `{a,b/c}/d` would expand to a set of patterns like:
55
+
56
+ [ [ a, d ]
57
+ , [ b, c, d ] ]
58
+
59
+ If a portion of the pattern doesn't have any "magic" in it
60
+ (that is, it's something like `"foo"` rather than `fo*o?`), then it
61
+ will be left as a string rather than converted to a regular
62
+ expression.
63
+
64
+ * `regexp` Created by the `makeRe` method. A single regular expression
65
+ expressing the entire pattern. This is useful in cases where you wish
66
+ to use the pattern somewhat like `fnmatch(3)` with `FNM_PATH` enabled.
67
+ * `negate` True if the pattern is negated.
68
+ * `comment` True if the pattern is a comment.
69
+ * `empty` True if the pattern is `""`.
70
+
71
+ ### Methods
72
+
73
+ * `makeRe` Generate the `regexp` member if necessary, and return it.
74
+ Will return `false` if the pattern is invalid.
75
+ * `match(fname)` Return true if the filename matches the pattern, or
76
+ false otherwise.
77
+ * `matchOne(fileArray, patternArray, partial)` Take a `/`-split
78
+ filename, and match it against a single row in the `regExpSet`. This
79
+ method is mainly for internal use, but is exposed so that it can be
80
+ used by a glob-walker that needs to avoid excessive filesystem calls.
81
+
82
+ All other methods are internal, and will be called as necessary.
83
+
84
+ ### minimatch(path, pattern, options)
85
+
86
+ Main export. Tests a path against the pattern using the options.
87
+
88
+ ```javascript
89
+ var isJS = minimatch(file, "*.js", { matchBase: true })
90
+ ```
91
+
92
+ ### minimatch.filter(pattern, options)
93
+
94
+ Returns a function that tests its
95
+ supplied argument, suitable for use with `Array.filter`. Example:
96
+
97
+ ```javascript
98
+ var javascripts = fileList.filter(minimatch.filter("*.js", {matchBase: true}))
99
+ ```
100
+
101
+ ### minimatch.match(list, pattern, options)
102
+
103
+ Match against the list of
104
+ files, in the style of fnmatch or glob. If nothing is matched, and
105
+ options.nonull is set, then return a list containing the pattern itself.
106
+
107
+ ```javascript
108
+ var javascripts = minimatch.match(fileList, "*.js", {matchBase: true}))
109
+ ```
110
+
111
+ ### minimatch.makeRe(pattern, options)
112
+
113
+ Make a regular expression object from the pattern.
114
+
115
+ ## Options
116
+
117
+ All options are `false` by default.
118
+
119
+ ### debug
120
+
121
+ Dump a ton of stuff to stderr.
122
+
123
+ ### nobrace
124
+
125
+ Do not expand `{a,b}` and `{1..3}` brace sets.
126
+
127
+ ### noglobstar
128
+
129
+ Disable `**` matching against multiple folder names.
130
+
131
+ ### dot
132
+
133
+ Allow patterns to match filenames starting with a period, even if
134
+ the pattern does not explicitly have a period in that spot.
135
+
136
+ Note that by default, `a/**/b` will **not** match `a/.d/b`, unless `dot`
137
+ is set.
138
+
139
+ ### noext
140
+
141
+ Disable "extglob" style patterns like `+(a|b)`.
142
+
143
+ ### nocase
144
+
145
+ Perform a case-insensitive match.
146
+
147
+ ### nonull
148
+
149
+ When a match is not found by `minimatch.match`, return a list containing
150
+ the pattern itself if this option is set. When not set, an empty list
151
+ is returned if there are no matches.
152
+
153
+ ### matchBase
154
+
155
+ If set, then patterns without slashes will be matched
156
+ against the basename of the path if it contains slashes. For example,
157
+ `a?b` would match the path `/xyz/123/acb`, but not `/xyz/acb/123`.
158
+
159
+ ### nocomment
160
+
161
+ Suppress the behavior of treating `#` at the start of a pattern as a
162
+ comment.
163
+
164
+ ### nonegate
165
+
166
+ Suppress the behavior of treating a leading `!` character as negation.
167
+
168
+ ### flipNegate
169
+
170
+ Returns from negate expressions the same as if they were not negated.
171
+ (Ie, true on a hit, false on a miss.)
172
+
173
+
174
+ ## Comparisons to other fnmatch/glob implementations
175
+
176
+ While strict compliance with the existing standards is a worthwhile
177
+ goal, some discrepancies exist between minimatch and other
178
+ implementations, and are intentional.
179
+
180
+ If the pattern starts with a `!` character, then it is negated. Set the
181
+ `nonegate` flag to suppress this behavior, and treat leading `!`
182
+ characters normally. This is perhaps relevant if you wish to start the
183
+ pattern with a negative extglob pattern like `!(a|B)`. Multiple `!`
184
+ characters at the start of a pattern will negate the pattern multiple
185
+ times.
186
+
187
+ If a pattern starts with `#`, then it is treated as a comment, and
188
+ will not match anything. Use `\#` to match a literal `#` at the
189
+ start of a line, or set the `nocomment` flag to suppress this behavior.
190
+
191
+ The double-star character `**` is supported by default, unless the
192
+ `noglobstar` flag is set. This is supported in the manner of bsdglob
193
+ and bash 4.1, where `**` only has special significance if it is the only
194
+ thing in a path part. That is, `a/**/b` will match `a/x/y/b`, but
195
+ `a/**b` will not.
196
+
197
+ If an escaped pattern has no matches, and the `nonull` flag is set,
198
+ then minimatch.match returns the pattern as-provided, rather than
199
+ interpreting the character escapes. For example,
200
+ `minimatch.match([], "\\*a\\?")` will return `"\\*a\\?"` rather than
201
+ `"*a?"`. This is akin to setting the `nullglob` option in bash, except
202
+ that it does not resolve escaped pattern characters.
203
+
204
+ If brace expansion is not disabled, then it is performed before any
205
+ other interpretation of the glob pattern. Thus, a pattern like
206
+ `+(a|{b),c)}`, which would not be valid in bash or zsh, is expanded
207
+ **first** into the set of `+(a|b)` and `+(a|c)`, and those patterns are
208
+ checked for validity. Since those two are valid, matching proceeds.
@@ -0,0 +1,26 @@
1
+ /*
2
+ * nodeLib.nlg
3
+ *
4
+ * Support library for running with NodeJS.
5
+ *
6
+ * column positioning: // // !
7
+ *
8
+ * Copyright (c) 2021-2022 by Richard C. Zulch
9
+ *
10
+ */
11
+
12
+
13
+ /*
14
+ * nodeLibInit
15
+ *
16
+ * Initialize NodeJS utilities.
17
+ *
18
+ */
19
+
20
+ function nodeLibInit(local manifest) {
21
+ manifest = `(nodeLibInit)
22
+
23
+ Naan.module.build(module.id, "nodeLib", function(modobj, compobj) {
24
+ compobj.manifest = manifest
25
+ })
26
+ }();
@@ -0,0 +1,65 @@
1
+ /*
2
+ * node_cp.nlg
3
+ *
4
+ * Child Process operations for NodeJS.
5
+ *
6
+ * column positioning: // // !
7
+ *
8
+ * Copyright (c) 2021-2022 by Richard C. Zulch
9
+ *
10
+ */
11
+
12
+
13
+ /*
14
+ * CatVideo
15
+ *
16
+ * Concatenate a list of videos.
17
+ *
18
+ */
19
+
20
+ closure CatVideo(filepaths, outfile, local fileslist, listfile, error, pending, child) {
21
+ fileslist = []
22
+ for file in filepaths
23
+ fileslist.push("file ".concat(nodePath.resolve("/tmp", file)))
24
+ fileslist = fileslist.join("\n")
25
+ listfile = nodePath.resolve("/tmp", "catVideoFiles")
26
+ `(error, data) = WriteFile(listfile, fileslist, "text")
27
+ if error
28
+ return (list(Error("CatVideo: can't write filelist:", error)))
29
+ outfile = nodePath.resolve("/tmp", outfile)
30
+ pending = new(nonce)
31
+ child = nodeCp.execFile("ffmpeg",
32
+ ["-f", "concat", "-safe", "0", "-i", listfile, "-c", "copy", outfile],
33
+ function(err, stdout, stderr) {
34
+ debuglog("CatVideo completed ffmpeg", err, "||", stderr, "||", stdout)
35
+ if err
36
+ pending.signal(list(Error("ffmpeg failed", err, stderr)))
37
+ else
38
+ pending.signal(list(false, stdout))
39
+ }
40
+ )
41
+ child.stdin.on("error", function(err) {
42
+ // make sure error isn't unhandled, so that a failed uglifyjs doesn't
43
+ // kill our process when the stdin write occurs after the pipe closes.
44
+ })
45
+ child.stdin.end(input)
46
+ pending.wait()
47
+ };
48
+
49
+
50
+ /*
51
+ * nodeCpInit
52
+ *
53
+ * Initialize NodeJS FS operations.
54
+ *
55
+ */
56
+
57
+ function nodeCpInit(local manifest) {
58
+ manifest = `(CatVideo, nodeCpInit)
59
+
60
+ Naan.module.build(module.id, "node_cp", function(modobj, compobj) {
61
+ require("node_util.nlg")
62
+ compobj.manifest = manifest
63
+ modobj.exports.CatVideo = CatVideo
64
+ })
65
+ }();
@@ -0,0 +1,137 @@
1
+ /*
2
+ * node_fs.nlg
3
+ *
4
+ * File system operations for NodeJS.
5
+ *
6
+ * column positioning: // // !
7
+ *
8
+ * Copyright (c) 2021-2022 by Richard C. Zulch
9
+ *
10
+ */
11
+
12
+
13
+ /*
14
+ * ReadFile
15
+ *
16
+ * Read an entire file from the specified path and return the contents.
17
+ *
18
+ * Encodings are:
19
+ * "text" - default
20
+ * "json" - perform JSON decoding on result
21
+ * "binary" - return file as buffer
22
+ *
23
+ */
24
+
25
+ function ReadFile(filepath, encoding, local options, error, data) {
26
+ filepath = nodePath.resolve("/tmp", filepath)
27
+ options = { }
28
+ if !encoding || encoding == "text" || encoding == "json"
29
+ options.encoding = "utf8"
30
+ else if encoding != "binary"
31
+ error = Error("ReadFile: invalid encoding", encoding)
32
+ if !error {
33
+ `(error, data) = await(nodeFs.promises.readFile(filepath, options))
34
+ if !error && encoding == "json"
35
+ `(error, data) = JsonParse(data)
36
+ if error
37
+ error = Error("ReadFile failed:", filepath, error)
38
+ }
39
+ if error
40
+ ErrorDebuglog(error)
41
+ list(error, data)
42
+ };
43
+
44
+
45
+ /*
46
+ * WriteFile
47
+ *
48
+ * Write the specified contents to a file at the path.
49
+ *
50
+ * Encodings are:
51
+ * "text" - default
52
+ * "json" - perform JSON decoding on result
53
+ * "binary" - return file as buffer
54
+ *
55
+ */
56
+
57
+ function WriteFile(filepath, data, encoding, local options, error) {
58
+ filepath = nodePath.resolve("/tmp", filepath)
59
+ options = { }
60
+ if !encoding || encoding == "text"
61
+ options.encoding = "utf8"
62
+ else if encoding == "json" {
63
+ `(error, data) = JsonStringify(data)
64
+ options.encoding = "utf8"
65
+ }
66
+ else if encoding != "binary"
67
+ error = Error("WriteFile: invalid encoding", encoding)
68
+ if !error {
69
+ `(error, data) = await(nodeFs.promises.writeFile(filepath, data, options))
70
+ if error
71
+ error = Error("WriteFile failed:", filepath, error)
72
+ }
73
+ if error
74
+ ErrorDebuglog(error)
75
+ list(error, data)
76
+ };
77
+
78
+
79
+ /*
80
+ * ReadStream
81
+ *
82
+ * Make a read stream.
83
+ *
84
+ * Encodings are:
85
+ * "text" - default
86
+ * "json" - perform JSON decoding on result
87
+ * "binary" - return file as buffer
88
+ *
89
+ */
90
+
91
+ function ReadStream(filepath, encoding, local options, error, data) {
92
+ filepath = nodePath.resolve("/tmp", filepath)
93
+ options = { }
94
+ if !encoding || encoding == "text" || encoding == "json"
95
+ options.encoding = "utf8"
96
+ else if encoding != "binary"
97
+ error = Error("ReadStream: invalid encoding", encoding)
98
+ if !error
99
+ data = nodeFs.createReadStream(filepath, options)
100
+ if error
101
+ ErrorDebuglog(error)
102
+ list(error, data)
103
+ };
104
+
105
+
106
+ /*
107
+ * DeleteDir
108
+ *
109
+ * Delete the directory at the specified path.
110
+ *
111
+ */
112
+
113
+ function DeleteDir(dirpath, local error, data) {
114
+ dirpath = nodePath.resolve("/tmp", dirpath)
115
+ `(error, data) = await(nodeFs.promises.rmdir(dirpath, { recursive: true }))
116
+ }
117
+
118
+
119
+ /*
120
+ * nodeFsInit
121
+ *
122
+ * Initialize NodeJS FS operations.
123
+ *
124
+ */
125
+
126
+ function nodeFsInit(local manifest) {
127
+ manifest = `(ReadFile, WriteFile, DeleteDir, nodeFsInit)
128
+
129
+ Naan.module.build(module.id, "node_fs", function(modobj, compobj) {
130
+ require("node_util.nlg")
131
+ compobj.manifest = manifest
132
+ modobj.exports.ReadFile = ReadFile
133
+ modobj.exports.WriteFile = WriteFile
134
+ modobj.exports.ReadStream = ReadStream
135
+ modobj.exports.DeleteDir = DeleteDir
136
+ })
137
+ }();
@@ -0,0 +1,172 @@
1
+ /*
2
+ * node_https.nlg
3
+ *
4
+ * Https operations for NodeJS.
5
+ *
6
+ * column positioning: // // !
7
+ *
8
+ * Copyright (c) 2021-2022 by Richard C. Zulch
9
+ *
10
+ */
11
+
12
+
13
+ /*
14
+ * HttpsGetJson
15
+ *
16
+ * Perform an HTTPS get on a host with JSON decoding.
17
+ *
18
+ */
19
+
20
+ closure HttpsGetJson(hostname, pathname, query, local https, options, pending, req) {
21
+ if query
22
+ pathname = EncodeQuery(pathname.concat("?"), query)
23
+ debuglog("HttpsGetJson", hostname, pathname)
24
+ options = {
25
+ hostname: hostname
26
+ port: 443
27
+ path: pathname
28
+ method: "GET"
29
+ }
30
+ pending = new(nonce)
31
+ req = nodeHttps.request(options, function (res) {
32
+ res.on("data", function (data, local error) {
33
+ `(error, data) = JsonParse(data)
34
+ if error
35
+ error = Error("HttpsGetJson:", hostname, pathname, error)
36
+ pending.signal(list(error, data))
37
+ })
38
+ })
39
+ req.on("error", function(error) {
40
+ error = Error("HttpsGetJson failed:", hostname, pathname, error)
41
+ pending.signal(list(error))
42
+ })
43
+ req.end()
44
+ pending.wait()
45
+ };
46
+
47
+
48
+ /*
49
+ * HttpsDownloadFile
50
+ *
51
+ * column positioning: // // !
52
+ * Download a file from an HTTPS endpoint using the GET method and store it on the local
53
+ * filesystem. This returns a standard result tuple.
54
+ *
55
+ */
56
+
57
+ closure HttpsDownloadFile(hostname, pathname, query, filepath,
58
+ local dirpath, error, pending, writeStream, options, req)
59
+ {
60
+ filepath = nodePath.resolve("/tmp", filepath)
61
+ if query
62
+ pathname = EncodeQuery(pathname.concat("?"), query)
63
+ debuglog("HttpsDownloadFile", hostname, pathname, filepath)
64
+ options = {
65
+ hostname: hostname
66
+ port: 443
67
+ path: pathname
68
+ method: "GET"
69
+ }
70
+ pending = new(nonce)
71
+ req = nodeHttps.request(options, function(resp) {
72
+ if resp.statusCode != 200
73
+ return (pending.signal(list(Error("HttpsDownloadFile cannot read:", resp.statusCode))))
74
+ `(error) = await(nodeFs.promises.mkdir(nodePath.dirname(filepath), { recursive: true }))
75
+ if error
76
+ return (pending.signal(list(Error("HttpsDownloadFile cannot create directory:", nodePath.dirname(filepath), error))))
77
+ writeStream = nodeFs.createWriteStream(filepath)
78
+ writeStream.on("error", function(error) {
79
+ error = Error("HttpsDownloadFile cannot write file:", filepath, error)
80
+ pending.signal(list(error))
81
+ })
82
+ resp.on("close", function() {
83
+ pending.signal(list(false, { ok: true }))
84
+ })
85
+ resp.pipe(writeStream)
86
+ })
87
+ req.on("error", function(error) {
88
+ error = Error("HttpsDownloadFile request failed:", hostname, pathname, error)
89
+ pending.signal(list(error))
90
+ })
91
+ req.end()
92
+ pending.wait()
93
+ };
94
+
95
+
96
+ /*
97
+ * HttpsUploadFile
98
+ *
99
+ * Upload a local filesystem file to an HTTPS endpoint using POST, returning a standard result tuple.
100
+ *
101
+ */
102
+
103
+ closure HttpsUploadFile(hostname, pathname, query, fieldname, filepath,
104
+ local error, buffer, form, fields, multipart, readStream, pending, options, req)
105
+ {
106
+ if query
107
+ pathname = EncodeQuery(pathname.concat("?"), query)
108
+ debuglog("HttpsUploadFile", hostname, pathname, filepath)
109
+ `(error, buffer) = ReadFile(filepath, "binary")
110
+ if error
111
+ return (list(Error("HttpsUploadFile cannot read file:", filepath, error)))
112
+ form = {}
113
+ form[fieldname] = buffer
114
+ fields = {}
115
+ fields[fieldname] = nodePath.basename(filepath)
116
+ multipart = MultipartBody(form, fields)
117
+ readStream = nodeStream.Readable.from(multipart.body)
118
+ options = {
119
+ hostname: hostname
120
+ port: 443
121
+ path: pathname
122
+ method: "POST"
123
+ headers: multipart.headers
124
+ }
125
+ pending = new(nonce)
126
+ req = nodeHttps.request(options, function(resp) {
127
+ resp.on("data", function (data, local error) {
128
+ `(error, data) = JsonParse(data)
129
+ if error || data.error_code {
130
+ if error
131
+ error = Error("HttpsUploadFile unexpected response:", hostname, pathname, error)
132
+ else
133
+ error = Error("HttpsUploadFile rejected:", hostname, pathname, data)
134
+ data = false
135
+ }
136
+ pending.signal(list(error, data))
137
+ })
138
+ resp.on("close", function(local error) {
139
+ error = Error("HttpsUploadFile no response:", hostname, pathname, resp.statusCode)
140
+ pending.signal(list(error))
141
+ })
142
+ })
143
+ req.on("error", function(error) {
144
+ error = Error("HttpsUploadFile request failed:", hostname, pathname, error)
145
+ pending.signal(list(error))
146
+ })
147
+ readStream.pipe(req)
148
+ readStream.on("close", function() {
149
+ req.end()
150
+ })
151
+ pending.wait()
152
+ };
153
+
154
+
155
+ /*
156
+ * nodeHttpsInit
157
+ *
158
+ * Initialize NodeJS HTTPS operations.
159
+ *
160
+ */
161
+
162
+ function nodeHttpsInit(local manifest) {
163
+ manifest = `(HttpsGetJson, HttpsDownloadFile, HttpsUploadFile, nodeHttpsInit)
164
+
165
+ Naan.module.build(module.id, "node_https", function(modobj, compobj) {
166
+ require("node_util.nlg")
167
+ compobj.manifest = manifest
168
+ modobj.exports.HttpsGetJson = HttpsGetJson
169
+ modobj.exports.HttpsDownloadFile = HttpsDownloadFile
170
+ modobj.exports.HttpsUploadFile = HttpsUploadFile
171
+ })
172
+ }();