@serveryyswys/node.operit 0.0.0-stage → 0.1.2
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.
- package/BUILTINS.json +233 -0
- package/DESIGN.md +513 -0
- package/LICENSE +674 -0
- package/README.md +301 -2
- package/dist/assert/strict.d.ts +2 -0
- package/dist/assert/strict.js +7 -0
- package/dist/assert.d.ts +3 -0
- package/dist/assert.js +483 -0
- package/dist/buffer.d.ts +60 -0
- package/dist/buffer.js +524 -0
- package/dist/child_process.d.ts +56 -0
- package/dist/child_process.js +371 -0
- package/dist/constants.d.ts +2 -0
- package/dist/constants.js +19 -0
- package/dist/crypto.d.ts +76 -0
- package/dist/crypto.js +312 -0
- package/dist/dns/promises.d.ts +34 -0
- package/dist/dns/promises.js +190 -0
- package/dist/dns.d.ts +88 -0
- package/dist/dns.js +259 -0
- package/dist/events.d.ts +40 -0
- package/dist/events.js +237 -0
- package/dist/fs/promises.d.ts +159 -0
- package/dist/fs/promises.js +129 -0
- package/dist/fs.d.ts +221 -0
- package/dist/fs.js +944 -0
- package/dist/http.d.ts +92 -0
- package/dist/http.js +319 -0
- package/dist/https.d.ts +296 -0
- package/dist/https.js +50 -0
- package/dist/index.d.ts +2422 -0
- package/dist/index.js +57 -0
- package/dist/lib/bridge.d.ts +60 -0
- package/dist/lib/bridge.js +205 -0
- package/dist/lib/bytes.d.ts +20 -0
- package/dist/lib/bytes.js +239 -0
- package/dist/lib/child-process-types.d.ts +45 -0
- package/dist/lib/child-process-types.js +2 -0
- package/dist/lib/cwd.d.ts +2 -0
- package/dist/lib/cwd.js +32 -0
- package/dist/lib/deep-equal.d.ts +3 -0
- package/dist/lib/deep-equal.js +223 -0
- package/dist/lib/deflate.d.ts +2 -0
- package/dist/lib/deflate.js +234 -0
- package/dist/lib/dns-driver.d.ts +14 -0
- package/dist/lib/dns-driver.js +85 -0
- package/dist/lib/dns-types.d.ts +22 -0
- package/dist/lib/dns-types.js +2 -0
- package/dist/lib/errors.d.ts +48 -0
- package/dist/lib/errors.js +424 -0
- package/dist/lib/fs-driver.d.ts +70 -0
- package/dist/lib/fs-driver.js +364 -0
- package/dist/lib/fs-env.d.ts +30 -0
- package/dist/lib/fs-env.js +157 -0
- package/dist/lib/fs-types.d.ts +19 -0
- package/dist/lib/fs-types.js +2 -0
- package/dist/lib/hashes.d.ts +27 -0
- package/dist/lib/hashes.js +432 -0
- package/dist/lib/http-driver.d.ts +30 -0
- package/dist/lib/http-driver.js +72 -0
- package/dist/lib/http-types.d.ts +18 -0
- package/dist/lib/http-types.js +2 -0
- package/dist/lib/inflate.d.ts +6 -0
- package/dist/lib/inflate.js +284 -0
- package/dist/lib/path-types.d.ts +37 -0
- package/dist/lib/path-types.js +2 -0
- package/dist/lib/paths.d.ts +8 -0
- package/dist/lib/paths.js +106 -0
- package/dist/lib/perf-hooks-types.d.ts +18 -0
- package/dist/lib/perf-hooks-types.js +2 -0
- package/dist/lib/process-driver.d.ts +31 -0
- package/dist/lib/process-driver.js +102 -0
- package/dist/lib/punycode.d.ts +8 -0
- package/dist/lib/punycode.js +179 -0
- package/dist/lib/stream-types.d.ts +29 -0
- package/dist/lib/stream-types.js +2 -0
- package/dist/lib/timers-types.d.ts +18 -0
- package/dist/lib/timers-types.js +2 -0
- package/dist/lib/url-search-params.d.ts +26 -0
- package/dist/lib/url-search-params.js +281 -0
- package/dist/lib/url-types.d.ts +44 -0
- package/dist/lib/url-types.js +2 -0
- package/dist/lib/url-whatwg.d.ts +78 -0
- package/dist/lib/url-whatwg.js +857 -0
- package/dist/lib/zlib-types.d.ts +12 -0
- package/dist/lib/zlib-types.js +2 -0
- package/dist/module.d.ts +8 -0
- package/dist/module.js +72 -0
- package/dist/os.d.ts +80 -0
- package/dist/os.js +247 -0
- package/dist/path/posix.d.ts +2 -0
- package/dist/path/posix.js +7 -0
- package/dist/path/win32.d.ts +2 -0
- package/dist/path/win32.js +10 -0
- package/dist/path.d.ts +3 -0
- package/dist/path.js +1222 -0
- package/dist/perf_hooks.d.ts +36 -0
- package/dist/perf_hooks.js +144 -0
- package/dist/process.d.ts +69 -0
- package/dist/process.js +230 -0
- package/dist/querystring.d.ts +15 -0
- package/dist/querystring.js +209 -0
- package/dist/stream/promises.d.ts +5 -0
- package/dist/stream/promises.js +10 -0
- package/dist/stream.d.ts +84 -0
- package/dist/stream.js +875 -0
- package/dist/string_decoder.d.ts +15 -0
- package/dist/string_decoder.js +237 -0
- package/dist/timers/promises.d.ts +19 -0
- package/dist/timers/promises.js +74 -0
- package/dist/timers.d.ts +21 -0
- package/dist/timers.js +41 -0
- package/dist/tty.d.ts +9 -0
- package/dist/tty.js +25 -0
- package/dist/unsupported.d.ts +12 -0
- package/dist/unsupported.js +17 -0
- package/dist/url.d.ts +28 -0
- package/dist/url.js +486 -0
- package/dist/util.d.ts +74 -0
- package/dist/util.js +515 -0
- package/dist/zlib.d.ts +88 -0
- package/dist/zlib.js +371 -0
- package/package.json +29 -4
package/dist/http.d.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* http 模块(**只有客户端**)。
|
|
3
|
+
*
|
|
4
|
+
* 建在宿主的 `toolCall('http_request')` 之上,而不是裸 socket ——
|
|
5
|
+
* 理由见 lib/http-driver.ts:那是一个走用户审批的工具,另起一套会绕过权限模型。
|
|
6
|
+
*
|
|
7
|
+
* 覆盖:request / get、ClientRequest(Writable,可 setHeader 后 end)、
|
|
8
|
+
* IncomingMessage(Readable,带 statusCode / headers / rawHeaders)、
|
|
9
|
+
* STATUS_CODES、METHODS、Agent(无连接池)。
|
|
10
|
+
*
|
|
11
|
+
* 不覆盖:
|
|
12
|
+
* - createServer / Server —— 沙箱里没有可监听的 socket,构造时显式抛 ONJ_UNSUPPORTED;
|
|
13
|
+
* - 请求体只能给文本(宿主工具的参数是字符串),二进制上行不做;
|
|
14
|
+
* - abort() 只发出 'abort' 事件,无法真正取消已经在宿主里跑着的请求(工具调用没有取消通道);
|
|
15
|
+
* - 没有连接池 / keep-alive / HTTP/2。
|
|
16
|
+
*/
|
|
17
|
+
import streamModule = require('./stream');
|
|
18
|
+
import * as httpDriver from './lib/http-driver';
|
|
19
|
+
import type { RequestOptions } from './lib/http-types';
|
|
20
|
+
declare class IncomingMessage extends streamModule.Readable {
|
|
21
|
+
statusCode: number;
|
|
22
|
+
statusMessage: string;
|
|
23
|
+
headers: Record<string, string>;
|
|
24
|
+
rawHeaders: string[];
|
|
25
|
+
httpVersion: string;
|
|
26
|
+
httpVersionMajor: number;
|
|
27
|
+
httpVersionMinor: number;
|
|
28
|
+
complete: boolean;
|
|
29
|
+
url: string;
|
|
30
|
+
method: string | null;
|
|
31
|
+
aborted: boolean;
|
|
32
|
+
constructor(response: httpDriver.HttpHostResponse);
|
|
33
|
+
_read(size: number): void;
|
|
34
|
+
}
|
|
35
|
+
type ResponseCallback = (response: IncomingMessage) => void;
|
|
36
|
+
declare class ClientRequest extends streamModule.Writable {
|
|
37
|
+
method: string;
|
|
38
|
+
path: string;
|
|
39
|
+
protocol: string;
|
|
40
|
+
host: string;
|
|
41
|
+
readonly maxHeadersCount: number | null;
|
|
42
|
+
readonly reusedSocket = false;
|
|
43
|
+
aborted: boolean;
|
|
44
|
+
finished: boolean;
|
|
45
|
+
readonly targetUrl: string;
|
|
46
|
+
readonly headerMap: Record<string, string>;
|
|
47
|
+
bodyChunks: Uint8Array[];
|
|
48
|
+
timer: unknown;
|
|
49
|
+
constructor(input: string | RequestOptions, options?: RequestOptions | ResponseCallback, callback?: ResponseCallback);
|
|
50
|
+
setHeader(name: unknown, value: unknown): this;
|
|
51
|
+
getHeader(name: unknown): string | undefined;
|
|
52
|
+
getHeaders(): Record<string, string>;
|
|
53
|
+
getHeaderNames(): string[];
|
|
54
|
+
hasHeader(name: unknown): boolean;
|
|
55
|
+
removeHeader(name: unknown): void;
|
|
56
|
+
_write(chunk: unknown, encoding: string, callback: (error?: Error | null) => void): void;
|
|
57
|
+
_final(callback: (error?: Error | null) => void): void;
|
|
58
|
+
send(): void;
|
|
59
|
+
clearTimer(): void;
|
|
60
|
+
setTimeout(milliseconds: number, callback?: () => void): this;
|
|
61
|
+
/** 只能发出 'abort':宿主的工具调用没有取消通道,已在跑的请求停不下来。 */
|
|
62
|
+
abort(): void;
|
|
63
|
+
destroy(error?: Error): this;
|
|
64
|
+
}
|
|
65
|
+
declare class Agent {
|
|
66
|
+
maxSockets: number;
|
|
67
|
+
maxFreeSockets: number;
|
|
68
|
+
readonly sockets: Record<string, unknown[]>;
|
|
69
|
+
readonly freeSockets: Record<string, unknown[]>;
|
|
70
|
+
readonly requests: Record<string, unknown[]>;
|
|
71
|
+
readonly options: Record<string, unknown>;
|
|
72
|
+
readonly maxTotalSockets: number;
|
|
73
|
+
totalSocketCount: number;
|
|
74
|
+
/** 没有真实连接可销毁 —— 请求由宿主工具代发。 */
|
|
75
|
+
destroy(): void;
|
|
76
|
+
}
|
|
77
|
+
declare function request(input: string | RequestOptions, options?: RequestOptions | ResponseCallback, callback?: ResponseCallback): ClientRequest;
|
|
78
|
+
declare function get(input: string | RequestOptions, options?: RequestOptions | ResponseCallback, callback?: ResponseCallback): ClientRequest;
|
|
79
|
+
declare function createServer(): never;
|
|
80
|
+
declare const api: {
|
|
81
|
+
request: typeof request;
|
|
82
|
+
get: typeof get;
|
|
83
|
+
createServer: typeof createServer;
|
|
84
|
+
ClientRequest: typeof ClientRequest;
|
|
85
|
+
IncomingMessage: typeof IncomingMessage;
|
|
86
|
+
Agent: typeof Agent;
|
|
87
|
+
globalAgent: Agent;
|
|
88
|
+
METHODS: string[];
|
|
89
|
+
STATUS_CODES: Record<number, string>;
|
|
90
|
+
maxHeaderSize: number;
|
|
91
|
+
};
|
|
92
|
+
export = api;
|
package/dist/http.js
ADDED
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
/**
|
|
36
|
+
* http 模块(**只有客户端**)。
|
|
37
|
+
*
|
|
38
|
+
* 建在宿主的 `toolCall('http_request')` 之上,而不是裸 socket ——
|
|
39
|
+
* 理由见 lib/http-driver.ts:那是一个走用户审批的工具,另起一套会绕过权限模型。
|
|
40
|
+
*
|
|
41
|
+
* 覆盖:request / get、ClientRequest(Writable,可 setHeader 后 end)、
|
|
42
|
+
* IncomingMessage(Readable,带 statusCode / headers / rawHeaders)、
|
|
43
|
+
* STATUS_CODES、METHODS、Agent(无连接池)。
|
|
44
|
+
*
|
|
45
|
+
* 不覆盖:
|
|
46
|
+
* - createServer / Server —— 沙箱里没有可监听的 socket,构造时显式抛 ONJ_UNSUPPORTED;
|
|
47
|
+
* - 请求体只能给文本(宿主工具的参数是字符串),二进制上行不做;
|
|
48
|
+
* - abort() 只发出 'abort' 事件,无法真正取消已经在宿主里跑着的请求(工具调用没有取消通道);
|
|
49
|
+
* - 没有连接池 / keep-alive / HTTP/2。
|
|
50
|
+
*/
|
|
51
|
+
const streamModule = require("./stream");
|
|
52
|
+
const buffer_1 = require("./buffer");
|
|
53
|
+
const errors_1 = require("./lib/errors");
|
|
54
|
+
const httpDriver = __importStar(require("./lib/http-driver"));
|
|
55
|
+
function toAbsoluteUrl(input, fallbackProtocol) {
|
|
56
|
+
if (typeof input === 'string') {
|
|
57
|
+
return input;
|
|
58
|
+
}
|
|
59
|
+
if (typeof input.href === 'string' && input.href.length > 0) {
|
|
60
|
+
return input.href;
|
|
61
|
+
}
|
|
62
|
+
const protocol = typeof input.protocol === 'string' && input.protocol.length > 0
|
|
63
|
+
? input.protocol
|
|
64
|
+
: fallbackProtocol;
|
|
65
|
+
const withColon = protocol.charAt(protocol.length - 1) === ':' ? protocol : protocol + ':';
|
|
66
|
+
let host = '';
|
|
67
|
+
if (typeof input.host === 'string' && input.host.length > 0) {
|
|
68
|
+
host = input.host;
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
host = typeof input.hostname === 'string' ? input.hostname : '';
|
|
72
|
+
if (input.port !== undefined && input.port !== null && String(input.port) !== '') {
|
|
73
|
+
host += ':' + String(input.port);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const auth = typeof input.auth === 'string' && input.auth.length > 0 ? input.auth + '@' : '';
|
|
77
|
+
const path = typeof input.path === 'string' && input.path.length > 0 ? input.path : '/';
|
|
78
|
+
return withColon + '//' + auth + host + path;
|
|
79
|
+
}
|
|
80
|
+
class IncomingMessage extends streamModule.Readable {
|
|
81
|
+
constructor(response) {
|
|
82
|
+
super();
|
|
83
|
+
this.httpVersion = '1.1';
|
|
84
|
+
this.httpVersionMajor = 1;
|
|
85
|
+
this.httpVersionMinor = 1;
|
|
86
|
+
this.complete = true;
|
|
87
|
+
this.method = null;
|
|
88
|
+
this.aborted = false;
|
|
89
|
+
this.statusCode = response.statusCode;
|
|
90
|
+
this.statusMessage = response.statusMessage;
|
|
91
|
+
this.url = response.url;
|
|
92
|
+
// Node 的 headers 用全小写键名
|
|
93
|
+
const normalized = {};
|
|
94
|
+
const raw = [];
|
|
95
|
+
const names = Object.keys(response.headers);
|
|
96
|
+
for (let i = 0; i < names.length; i += 1) {
|
|
97
|
+
const name = names[i];
|
|
98
|
+
normalized[name.toLowerCase()] = response.headers[name];
|
|
99
|
+
raw.push(name, response.headers[name]);
|
|
100
|
+
}
|
|
101
|
+
this.headers = normalized;
|
|
102
|
+
this.rawHeaders = raw;
|
|
103
|
+
const body = response.contentBase64 !== null && response.contentBase64 !== ''
|
|
104
|
+
? buffer_1.Buffer.from(response.contentBase64, 'base64')
|
|
105
|
+
: buffer_1.Buffer.from(response.content, 'utf8');
|
|
106
|
+
if (body.length > 0) {
|
|
107
|
+
this.push(body);
|
|
108
|
+
}
|
|
109
|
+
this.push(null);
|
|
110
|
+
}
|
|
111
|
+
// 数据在构造时就推完了,不需要主动拉
|
|
112
|
+
_read(size) {
|
|
113
|
+
void size;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
class ClientRequest extends streamModule.Writable {
|
|
117
|
+
constructor(input, options, callback) {
|
|
118
|
+
super();
|
|
119
|
+
this.maxHeadersCount = null;
|
|
120
|
+
this.reusedSocket = false;
|
|
121
|
+
this.aborted = false;
|
|
122
|
+
this.finished = false;
|
|
123
|
+
this.headerMap = {};
|
|
124
|
+
this.bodyChunks = [];
|
|
125
|
+
this.timer = null;
|
|
126
|
+
let opts = {};
|
|
127
|
+
let done = callback;
|
|
128
|
+
if (typeof options === 'function') {
|
|
129
|
+
done = options;
|
|
130
|
+
}
|
|
131
|
+
else if (options !== undefined && options !== null) {
|
|
132
|
+
opts = options;
|
|
133
|
+
}
|
|
134
|
+
const source = typeof input === 'string' ? { href: input } : input;
|
|
135
|
+
this.targetUrl = toAbsoluteUrl(input, 'http:');
|
|
136
|
+
this.method = (opts.method !== undefined ? opts.method : (source.method !== undefined ? source.method : 'GET')).toUpperCase();
|
|
137
|
+
const path = typeof source.path === 'string' && source.path.length > 0 ? source.path : '/';
|
|
138
|
+
this.path = path;
|
|
139
|
+
const schemeMatch = /^([a-zA-Z][a-zA-Z0-9+\-.]*):/.exec(this.targetUrl);
|
|
140
|
+
this.protocol = schemeMatch === null ? 'http:' : schemeMatch[1] + ':';
|
|
141
|
+
this.host = typeof source.hostname === 'string' ? source.hostname : '';
|
|
142
|
+
const headers = opts.headers !== undefined ? opts.headers : source.headers;
|
|
143
|
+
if (headers !== undefined && headers !== null) {
|
|
144
|
+
const names = Object.keys(headers);
|
|
145
|
+
for (let i = 0; i < names.length; i += 1) {
|
|
146
|
+
this.setHeader(names[i], headers[names[i]]);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if (typeof done === 'function') {
|
|
150
|
+
this.once('response', done);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
setHeader(name, value) {
|
|
154
|
+
this.headerMap[String(name).toLowerCase()] = String(value);
|
|
155
|
+
return this;
|
|
156
|
+
}
|
|
157
|
+
getHeader(name) {
|
|
158
|
+
return this.headerMap[String(name).toLowerCase()];
|
|
159
|
+
}
|
|
160
|
+
getHeaders() {
|
|
161
|
+
return Object.assign({}, this.headerMap);
|
|
162
|
+
}
|
|
163
|
+
getHeaderNames() {
|
|
164
|
+
return Object.keys(this.headerMap);
|
|
165
|
+
}
|
|
166
|
+
hasHeader(name) {
|
|
167
|
+
return Object.prototype.hasOwnProperty.call(this.headerMap, String(name).toLowerCase());
|
|
168
|
+
}
|
|
169
|
+
removeHeader(name) {
|
|
170
|
+
delete this.headerMap[String(name).toLowerCase()];
|
|
171
|
+
}
|
|
172
|
+
_write(chunk, encoding, callback) {
|
|
173
|
+
void encoding;
|
|
174
|
+
this.bodyChunks.push(chunk);
|
|
175
|
+
callback(null);
|
|
176
|
+
}
|
|
177
|
+
_final(callback) {
|
|
178
|
+
this.send();
|
|
179
|
+
callback(null);
|
|
180
|
+
}
|
|
181
|
+
send() {
|
|
182
|
+
const self = this;
|
|
183
|
+
if (this.aborted) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
let body;
|
|
187
|
+
if (this.bodyChunks.length > 0) {
|
|
188
|
+
body = buffer_1.Buffer.concat(this.bodyChunks).toString('utf8');
|
|
189
|
+
}
|
|
190
|
+
const spec = {
|
|
191
|
+
url: this.targetUrl,
|
|
192
|
+
method: this.method,
|
|
193
|
+
headers: this.getHeaders()
|
|
194
|
+
};
|
|
195
|
+
if (body !== undefined) {
|
|
196
|
+
spec.body = body;
|
|
197
|
+
}
|
|
198
|
+
this.finished = true;
|
|
199
|
+
let pending;
|
|
200
|
+
try {
|
|
201
|
+
pending = httpDriver.getDriver().request(spec);
|
|
202
|
+
}
|
|
203
|
+
catch (failure) {
|
|
204
|
+
// 驱动同步抛(例如宿主没有 toolCall)也要走 'error' 事件,不能漏出去
|
|
205
|
+
this.emit('error', failure);
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
pending.then(httpDriver.normalizeResponse).then(function (response) {
|
|
209
|
+
self.clearTimer();
|
|
210
|
+
self.emit('response', new IncomingMessage(response));
|
|
211
|
+
self.emit('close');
|
|
212
|
+
}, function (failure) {
|
|
213
|
+
self.clearTimer();
|
|
214
|
+
self.emit('error', failure);
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
clearTimer() {
|
|
218
|
+
if (this.timer !== null) {
|
|
219
|
+
clearTimeout(this.timer);
|
|
220
|
+
this.timer = null;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
setTimeout(milliseconds, callback) {
|
|
224
|
+
this.clearTimer();
|
|
225
|
+
const self = this;
|
|
226
|
+
this.timer = setTimeout(function () {
|
|
227
|
+
self.emit('timeout');
|
|
228
|
+
if (typeof callback === 'function') {
|
|
229
|
+
callback();
|
|
230
|
+
}
|
|
231
|
+
}, milliseconds);
|
|
232
|
+
return this;
|
|
233
|
+
}
|
|
234
|
+
/** 只能发出 'abort':宿主的工具调用没有取消通道,已在跑的请求停不下来。 */
|
|
235
|
+
abort() {
|
|
236
|
+
if (this.aborted) {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
this.aborted = true;
|
|
240
|
+
this.clearTimer();
|
|
241
|
+
this.emit('abort');
|
|
242
|
+
}
|
|
243
|
+
destroy(error) {
|
|
244
|
+
this.abort();
|
|
245
|
+
if (error !== undefined) {
|
|
246
|
+
this.emit('error', error);
|
|
247
|
+
}
|
|
248
|
+
return this;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
class Agent {
|
|
252
|
+
constructor() {
|
|
253
|
+
this.maxSockets = Infinity;
|
|
254
|
+
this.maxFreeSockets = 256;
|
|
255
|
+
this.sockets = {};
|
|
256
|
+
this.freeSockets = {};
|
|
257
|
+
this.requests = {};
|
|
258
|
+
this.options = {};
|
|
259
|
+
this.maxTotalSockets = Infinity;
|
|
260
|
+
this.totalSocketCount = 0;
|
|
261
|
+
}
|
|
262
|
+
/** 没有真实连接可销毁 —— 请求由宿主工具代发。 */
|
|
263
|
+
destroy() {
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
function request(input, options, callback) {
|
|
268
|
+
return new ClientRequest(input, options, callback);
|
|
269
|
+
}
|
|
270
|
+
function get(input, options, callback) {
|
|
271
|
+
const clientRequest = request(input, options, callback);
|
|
272
|
+
// end() 是 applyWritable 挂到 Writable.prototype 上的,类型里没有,得转一下
|
|
273
|
+
clientRequest.end();
|
|
274
|
+
return clientRequest;
|
|
275
|
+
}
|
|
276
|
+
function createServer() {
|
|
277
|
+
throw (0, errors_1.onjError)('ONJ_UNSUPPORTED', 'http.createServer() 需要监听 socket,Operit 的沙箱脚本没有这个能力;' +
|
|
278
|
+
'网络访问请用工具调用(toolCall("http_request") 或内建的 axios)。');
|
|
279
|
+
}
|
|
280
|
+
const METHODS = [
|
|
281
|
+
'ACL', 'BIND', 'CHECKOUT', 'CONNECT', 'COPY', 'DELETE', 'GET', 'HEAD', 'LINK', 'LOCK',
|
|
282
|
+
'M-SEARCH', 'MERGE', 'MKACTIVITY', 'MKCALENDAR', 'MKCOL', 'MOVE', 'NOTIFY', 'OPTIONS',
|
|
283
|
+
'PATCH', 'POST', 'PROPFIND', 'PROPPATCH', 'PURGE', 'PUT', 'REBIND', 'REPORT', 'SEARCH',
|
|
284
|
+
'SOURCE', 'SUBSCRIBE', 'TRACE', 'UNBIND', 'UNLINK', 'UNLOCK', 'UNSUBSCRIBE'
|
|
285
|
+
];
|
|
286
|
+
const STATUS_CODES = {
|
|
287
|
+
100: 'Continue', 101: 'Switching Protocols', 102: 'Processing', 103: 'Early Hints',
|
|
288
|
+
200: 'OK', 201: 'Created', 202: 'Accepted', 203: 'Non-Authoritative Information',
|
|
289
|
+
204: 'No Content', 205: 'Reset Content', 206: 'Partial Content', 207: 'Multi-Status',
|
|
290
|
+
208: 'Already Reported', 226: 'IM Used',
|
|
291
|
+
300: 'Multiple Choices', 301: 'Moved Permanently', 302: 'Found', 303: 'See Other',
|
|
292
|
+
304: 'Not Modified', 305: 'Use Proxy', 307: 'Temporary Redirect', 308: 'Permanent Redirect',
|
|
293
|
+
400: 'Bad Request', 401: 'Unauthorized', 402: 'Payment Required', 403: 'Forbidden',
|
|
294
|
+
404: 'Not Found', 405: 'Method Not Allowed', 406: 'Not Acceptable',
|
|
295
|
+
407: 'Proxy Authentication Required', 408: 'Request Timeout', 409: 'Conflict',
|
|
296
|
+
410: 'Gone', 411: 'Length Required', 412: 'Precondition Failed', 413: 'Payload Too Large',
|
|
297
|
+
414: 'URI Too Long', 415: 'Unsupported Media Type', 416: 'Range Not Satisfiable',
|
|
298
|
+
417: 'Expectation Failed', 418: "I'm a Teapot", 421: 'Misdirected Request',
|
|
299
|
+
422: 'Unprocessable Entity', 423: 'Locked', 424: 'Failed Dependency', 425: 'Too Early',
|
|
300
|
+
426: 'Upgrade Required', 428: 'Precondition Required', 429: 'Too Many Requests',
|
|
301
|
+
431: 'Request Header Fields Too Large', 451: 'Unavailable For Legal Reasons',
|
|
302
|
+
500: 'Internal Server Error', 501: 'Not Implemented', 502: 'Bad Gateway',
|
|
303
|
+
503: 'Service Unavailable', 504: 'Gateway Timeout', 505: 'HTTP Version Not Supported',
|
|
304
|
+
506: 'Variant Also Negotiates', 507: 'Insufficient Storage', 508: 'Loop Detected',
|
|
305
|
+
509: 'Bandwidth Limit Exceeded', 510: 'Not Extended', 511: 'Network Authentication Required'
|
|
306
|
+
};
|
|
307
|
+
const api = {
|
|
308
|
+
request: request,
|
|
309
|
+
get: get,
|
|
310
|
+
createServer: createServer,
|
|
311
|
+
ClientRequest: ClientRequest,
|
|
312
|
+
IncomingMessage: IncomingMessage,
|
|
313
|
+
Agent: Agent,
|
|
314
|
+
globalAgent: new Agent(),
|
|
315
|
+
METHODS: METHODS,
|
|
316
|
+
STATUS_CODES: STATUS_CODES,
|
|
317
|
+
maxHeaderSize: 16384
|
|
318
|
+
};
|
|
319
|
+
module.exports = api;
|