sfml.js 1.0.0 → 1.2.1
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/README.ja.md +10 -9
- package/README.ko.md +10 -9
- package/README.md +10 -9
- package/README.zh-CN.md +10 -9
- package/binding.gyp +23 -16
- package/dist/audio/effect-processor.js +9 -3
- package/dist/audio/input-sound-file.js +41 -15
- package/dist/audio/listener.js +33 -11
- package/dist/audio/music.js +41 -18
- package/dist/audio/output-sound-file.js +4 -2
- package/dist/audio/playback-device.js +11 -3
- package/dist/audio/sound-buffer-recorder.js +8 -5
- package/dist/audio/sound-buffer.js +40 -17
- package/dist/audio/sound-file-factory.js +21 -9
- package/dist/audio/sound-file-reader.js +21 -7
- package/dist/audio/sound-recorder.js +36 -15
- package/dist/audio/sound-source.js +117 -40
- package/dist/audio/sound-stream.js +14 -5
- package/dist/audio/sound.js +9 -4
- package/dist/audio/value.js +42 -16
- package/dist/graphics/blend-mode.js +2 -1
- package/dist/graphics/boolean-vector.d.ts +1 -2
- package/dist/graphics/boolean-vector.js +19 -9
- package/dist/graphics/color.js +2 -1
- package/dist/graphics/convex-shape.js +7 -3
- package/dist/graphics/font.js +52 -23
- package/dist/graphics/glsl-matrix.js +8 -3
- package/dist/graphics/glsl.js +47 -16
- package/dist/graphics/glyph.js +27 -10
- package/dist/graphics/image.js +30 -14
- package/dist/graphics/rect.js +15 -8
- package/dist/graphics/render-states.js +18 -9
- package/dist/graphics/render-target.js +49 -23
- package/dist/graphics/render-texture.js +24 -12
- package/dist/graphics/render-window.js +6 -3
- package/dist/graphics/shader.js +67 -32
- package/dist/graphics/shape.js +25 -13
- package/dist/graphics/sprite.js +19 -9
- package/dist/graphics/stencil-mode.js +10 -6
- package/dist/graphics/swap.js +2 -1
- package/dist/graphics/text.js +123 -48
- package/dist/graphics/texture.js +48 -24
- package/dist/graphics/transform.js +29 -13
- package/dist/graphics/transformable.js +2 -1
- package/dist/graphics/vertex-array.js +40 -18
- package/dist/graphics/vertex-buffer.js +39 -19
- package/dist/graphics/vertex.js +6 -3
- package/dist/graphics/view.js +30 -14
- package/dist/index.d.ts +4 -4
- package/dist/internal/audio-codec-registry.js +12 -6
- package/dist/internal/audio-input-stream.js +2 -1
- package/dist/internal/audio-music-pump.js +58 -24
- package/dist/internal/audio-recorder-session.js +17 -7
- package/dist/internal/audio-worker-session.js +28 -10
- package/dist/internal/audio-worker.js +40 -21
- package/dist/internal/glsl-value.d.ts +3 -1
- package/dist/internal/glsl-value.js +18 -8
- package/dist/internal/graphics-state.js +29 -15
- package/dist/internal/input-stream.d.ts +3 -1
- package/dist/internal/input-stream.js +16 -7
- package/dist/internal/numeric-properties.js +2 -1
- package/dist/internal/render-states.js +2 -1
- package/dist/internal/scalar.js +22 -13
- package/dist/internal/shaped-glyph.d.ts +3 -1
- package/dist/internal/shaped-glyph.js +12 -4
- package/dist/internal/struct-view.d.ts +3 -1
- package/dist/internal/struct-view.js +3 -1
- package/dist/internal/transformable.js +2 -1
- package/dist/network/common.js +23 -11
- package/dist/network/dns.js +25 -17
- package/dist/network/ftp.js +43 -18
- package/dist/network/http.js +126 -56
- package/dist/network/ip-address.js +50 -25
- package/dist/network/packet.js +63 -25
- package/dist/network/sftp.js +175 -74
- package/dist/network/socket-selector.js +28 -14
- package/dist/network/socket.js +71 -30
- package/dist/network/tcp-listener.js +42 -19
- package/dist/network/tcp-socket.js +111 -53
- package/dist/network/tls-material.js +2 -1
- package/dist/network/udp-socket.js +68 -33
- package/dist/system/angle.js +2 -1
- package/dist/system/clock.js +4 -2
- package/dist/system/err.js +5 -2
- package/dist/system/file-input-stream.js +8 -4
- package/dist/system/input-stream.js +6 -3
- package/dist/system/operators.d.ts +1 -1
- package/dist/system/operators.js +90 -50
- package/dist/system/sleep.js +4 -2
- package/dist/system/time.js +12 -6
- package/dist/system/vector2.js +6 -7
- package/dist/system/vector3.js +11 -6
- package/dist/window/clipboard.js +2 -1
- package/dist/window/context.js +10 -5
- package/dist/window/cursor.js +2 -1
- package/dist/window/event.js +9 -5
- package/dist/window/mouse.js +4 -2
- package/dist/window/video-mode.js +2 -1
- package/dist/window/window.js +19 -12
- package/native/addon.cc +25 -12
- package/native/audio-binding.cc +266 -126
- package/native/audio-codec-binding.cc +147 -77
- package/native/audio-device-binding.cc +76 -49
- package/native/audio-file-binding.cc +116 -67
- package/native/audio-recorder-binding.cc +147 -98
- package/native/audio-recorder.h +1 -1
- package/native/audio-state.h +53 -30
- package/native/audio-stream-binding.cc +148 -94
- package/native/audio-stream.h +5 -4
- package/native/audio-worker-binding.cc +163 -81
- package/native/audio-worker.h +35 -19
- package/native/batch-drawable.cc +29 -16
- package/native/batch-drawable.h +11 -8
- package/native/context-binding.cc +40 -27
- package/native/context-settings.h +15 -8
- package/native/deferred-delete.cc +9 -6
- package/native/deferred-delete.h +7 -8
- package/native/error-stream-binding.cc +42 -28
- package/native/font-binding.cc +89 -48
- package/native/graphics-binding.cc +122 -72
- package/native/graphics-state.h +50 -36
- package/native/image-binding.cc +38 -24
- package/native/image-state.h +26 -18
- package/native/input-binding.cc +42 -27
- package/native/render-window-binding.cc +38 -23
- package/native/shader-binding.cc +115 -50
- package/native/shape-binding.cc +94 -49
- package/native/shape-state.h +18 -9
- package/native/shaped-glyph-storage.h +9 -5
- package/native/stream-binding.cc +143 -117
- package/native/system-binding.cc +17 -12
- package/native/text-binding.cc +68 -35
- package/native/text-state.h +25 -18
- package/native/texture-binding.cc +42 -33
- package/native/texture-resource.h +20 -12
- package/native/time-value.h +3 -1
- package/native/transformable-binding.cc +12 -6
- package/native/transformable-state.h +6 -6
- package/native/value-binding.h +46 -28
- package/native/value-layout.h +10 -10
- package/native/values-binding.cc +29 -23
- package/native/vertex-binding.cc +18 -12
- package/native/vertex-buffer-binding.cc +38 -24
- package/native/vertex-storage.h +2 -2
- package/native/view-data.h +4 -3
- package/native/window-binding.cc +75 -45
- package/native/window-event.h +20 -11
- package/native/window-owner.h +15 -12
- package/package.json +44 -51
- package/src/audio/effect-processor.ts +9 -3
- package/src/audio/input-sound-file.ts +108 -36
- package/src/audio/listener.ts +39 -13
- package/src/audio/music.ts +112 -47
- package/src/audio/output-sound-file.ts +40 -11
- package/src/audio/playback-device.ts +23 -8
- package/src/audio/sound-buffer-recorder.ts +35 -13
- package/src/audio/sound-buffer.ts +166 -58
- package/src/audio/sound-file-factory.ts +127 -49
- package/src/audio/sound-file-reader.ts +49 -15
- package/src/audio/sound-recorder.ts +100 -34
- package/src/audio/sound-source.ts +142 -45
- package/src/audio/sound-stream.ts +15 -5
- package/src/audio/sound.ts +12 -4
- package/src/audio/value.ts +89 -26
- package/src/graphics/blend-mode.ts +3 -1
- package/src/graphics/boolean-vector.ts +56 -26
- package/src/graphics/color.ts +3 -1
- package/src/graphics/convex-shape.ts +10 -3
- package/src/graphics/font.ts +171 -50
- package/src/graphics/glsl-matrix.ts +9 -3
- package/src/graphics/glsl.ts +81 -35
- package/src/graphics/glyph.ts +67 -26
- package/src/graphics/image.ts +119 -45
- package/src/graphics/rect.ts +56 -24
- package/src/graphics/render-states.ts +31 -14
- package/src/graphics/render-target.ts +197 -81
- package/src/graphics/render-texture.ts +36 -12
- package/src/graphics/render-window.ts +9 -3
- package/src/graphics/shader.ts +186 -61
- package/src/graphics/shape.ts +93 -35
- package/src/graphics/sprite.ts +64 -25
- package/src/graphics/stencil-mode.ts +12 -6
- package/src/graphics/swap.ts +3 -2
- package/src/graphics/text.ts +229 -74
- package/src/graphics/texture.ts +180 -61
- package/src/graphics/transform.ts +132 -47
- package/src/graphics/transformable.ts +3 -1
- package/src/graphics/vertex-array.ts +87 -35
- package/src/graphics/vertex-buffer.ts +97 -33
- package/src/graphics/vertex.ts +9 -3
- package/src/graphics/view.ts +59 -21
- package/src/index.ts +101 -23
- package/src/internal/audio-codec-registry.ts +18 -6
- package/src/internal/audio-input-stream.ts +6 -2
- package/src/internal/audio-music-pump.ts +116 -41
- package/src/internal/audio-native.ts +249 -96
- package/src/internal/audio-recorder-session.ts +68 -24
- package/src/internal/audio-worker-session.ts +65 -21
- package/src/internal/audio-worker.ts +85 -41
- package/src/internal/glsl-value.ts +26 -11
- package/src/internal/graphics-state.ts +57 -20
- package/src/internal/input-stream.ts +23 -10
- package/src/internal/native.ts +252 -99
- package/src/internal/numeric-properties.ts +2 -1
- package/src/internal/render-states.ts +3 -1
- package/src/internal/scalar.ts +33 -14
- package/src/internal/shaped-glyph.ts +25 -12
- package/src/internal/struct-view.ts +40 -13
- package/src/internal/transformable.ts +3 -1
- package/src/network/common.ts +35 -11
- package/src/network/dns.ts +95 -38
- package/src/network/ftp.ts +129 -47
- package/src/network/http.ts +232 -77
- package/src/network/ip-address.ts +118 -48
- package/src/network/packet.ts +161 -54
- package/src/network/sftp.ts +498 -168
- package/src/network/socket-selector.ts +66 -26
- package/src/network/socket.ts +133 -46
- package/src/network/tcp-listener.ts +78 -29
- package/src/network/tcp-socket.ts +240 -95
- package/src/network/tls-material.ts +6 -2
- package/src/network/udp-socket.ts +147 -53
- package/src/system/angle.ts +3 -1
- package/src/system/clock.ts +12 -4
- package/src/system/err.ts +5 -2
- package/src/system/file-input-stream.ts +12 -4
- package/src/system/input-stream.ts +9 -3
- package/src/system/operators.ts +290 -115
- package/src/system/sleep.ts +6 -2
- package/src/system/time.ts +18 -6
- package/src/system/timeout-with-predicate.ts +19 -8
- package/src/system/vector2.ts +101 -49
- package/src/system/vector3.ts +85 -36
- package/src/window/clipboard.ts +3 -1
- package/src/window/context.ts +15 -5
- package/src/window/cursor.ts +3 -1
- package/src/window/event.ts +26 -12
- package/src/window/mouse.ts +6 -2
- package/src/window/video-mode.ts +3 -1
- package/src/window/window.ts +119 -37
- package/test/native/audio-probes.cc +74 -36
- package/test/native/deferred-delete-probes.cc +17 -11
- package/test/native/drawable-probes.cc +70 -36
- package/test/native/exception-probes.cc +14 -9
- package/test/native/image-probes.cc +20 -10
- package/test/native/matrix-probes.cc +8 -4
- package/test/native/network-oracle.cc +17 -11
- package/test/native/render-texture-probes.cc +33 -24
- package/test/native/shader-probes.cc +9 -9
- package/test/native/shape-probes.cc +37 -23
- package/test/native/sprite-probes.cc +41 -25
- package/test/native/system-probes.cc +58 -35
- package/test/native/text-probes.cc +77 -44
- package/test/native/texture-probes.cc +43 -26
- package/test/native/time-probes.cc +18 -12
- package/test/native/transform-probes.cc +15 -10
- package/test/native/transformable-probes.cc +23 -17
- package/test/native/value-oracle.h +9 -4
- package/test/native/value-probes.cc +31 -23
- package/test/native/vertex-array-probes.cc +22 -14
- package/test/native/vertex-buffer-probes.cc +28 -17
- package/test/native/view-probes.cc +29 -17
- package/test/native/window-probes.cc +66 -33
- package/tools/ensure-sfml.cjs +9 -0
- package/tools/generate-network-constants.cjs +11 -6
- package/tools/generate-window-constants.cjs +31 -12
- package/docs/README.ja.md +0 -30
- package/docs/README.ko.md +0 -30
- package/docs/README.md +0 -30
- package/docs/README.zh-CN.md +0 -30
- package/docs/api.ja.md +0 -84
- package/docs/api.ko.md +0 -84
- package/docs/api.md +0 -81
- package/docs/api.zh-CN.md +0 -81
- package/docs/audio.ja.md +0 -249
- package/docs/audio.ko.md +0 -251
- package/docs/audio.md +0 -252
- package/docs/audio.zh-CN.md +0 -252
- package/docs/concepts.ja.md +0 -130
- package/docs/concepts.ko.md +0 -130
- package/docs/concepts.md +0 -131
- package/docs/concepts.zh-CN.md +0 -131
- package/docs/development.ja.md +0 -144
- package/docs/development.ko.md +0 -144
- package/docs/development.md +0 -146
- package/docs/development.zh-CN.md +0 -146
- package/docs/getting-started.ja.md +0 -154
- package/docs/getting-started.ko.md +0 -154
- package/docs/getting-started.md +0 -156
- package/docs/getting-started.zh-CN.md +0 -156
- package/docs/graphics.ja.md +0 -256
- package/docs/graphics.ko.md +0 -254
- package/docs/graphics.md +0 -255
- package/docs/graphics.zh-CN.md +0 -257
- package/docs/network.ja.md +0 -214
- package/docs/network.ko.md +0 -216
- package/docs/network.md +0 -218
- package/docs/network.zh-CN.md +0 -218
- package/docs/system.ja.md +0 -169
- package/docs/system.ko.md +0 -171
- package/docs/system.md +0 -173
- package/docs/system.zh-CN.md +0 -173
- package/docs/window.ja.md +0 -138
- package/docs/window.ko.md +0 -140
- package/docs/window.md +0 -141
- package/docs/window.zh-CN.md +0 -141
- package/native/exception-boundary.h +0 -97
- package/native/numeric-fields.h +0 -96
- package/native/shared-handle.h +0 -107
- package/test/tsconfig.json +0 -8
- package/tools/configure-lsp.cjs +0 -23
- package/tools/lib_downloader.cjs +0 -92
- package/tools/lib_downloader_urls.cjs +0 -23
package/src/network/http.ts
CHANGED
|
@@ -222,7 +222,10 @@ export enum HttpStatus {
|
|
|
222
222
|
}
|
|
223
223
|
const methods = [ 'GET', 'POST', 'HEAD', 'PUT', 'DELETE' ];
|
|
224
224
|
const prepareRequest: unique symbol = Symbol('Http.prepare');
|
|
225
|
-
interface PreparedRequest {
|
|
225
|
+
interface PreparedRequest {
|
|
226
|
+
bytes: Buffer;
|
|
227
|
+
head: boolean;
|
|
228
|
+
}
|
|
226
229
|
|
|
227
230
|
/**
|
|
228
231
|
* Mutable HTTP/1 request, defaulting to GET /, HTTP/1.0, and an empty body.
|
|
@@ -259,7 +262,11 @@ export class HttpRequest {
|
|
|
259
262
|
* @ko 인코딩된 경로, 메서드 및 본문으로 요청을 생성합니다.
|
|
260
263
|
* 문자열 본문은 UTF-8로 인코딩합니다.
|
|
261
264
|
*/
|
|
262
|
-
constructor(
|
|
265
|
+
constructor(
|
|
266
|
+
uri = '/',
|
|
267
|
+
method = HttpMethod.Get,
|
|
268
|
+
body: string | Uint8Array = '',
|
|
269
|
+
) {
|
|
263
270
|
this.setUri(uri);
|
|
264
271
|
this.setMethod(method);
|
|
265
272
|
this.setBody(body);
|
|
@@ -284,7 +291,9 @@ export class HttpRequest {
|
|
|
284
291
|
* @ja リクエストメソッドを設定します。
|
|
285
292
|
* @ko 요청 메서드를 설정합니다.
|
|
286
293
|
*/
|
|
287
|
-
setMethod(method: HttpMethod): void {
|
|
294
|
+
setMethod(method: HttpMethod): void {
|
|
295
|
+
this.method = uint(method, 4);
|
|
296
|
+
}
|
|
288
297
|
/**
|
|
289
298
|
* Set the URL-encoded path and query string, adding a leading slash
|
|
290
299
|
* if missing.
|
|
@@ -309,7 +318,8 @@ export class HttpRequest {
|
|
|
309
318
|
* @ko 요청 행에 쓸 HTTP 주 버전과 부 버전을 설정합니다.
|
|
310
319
|
*/
|
|
311
320
|
setHttpVersion(major: number, minor: number): void {
|
|
312
|
-
this.major = uint(major);
|
|
321
|
+
this.major = uint(major);
|
|
322
|
+
this.minor = uint(minor);
|
|
313
323
|
}
|
|
314
324
|
/**
|
|
315
325
|
* Copy request body bytes; encode strings as UTF-8.
|
|
@@ -322,25 +332,38 @@ export class HttpRequest {
|
|
|
322
332
|
}
|
|
323
333
|
[prepareRequest](host: string): PreparedRequest {
|
|
324
334
|
const fields = new Map(this.fields);
|
|
325
|
-
if (!fields.has('host'))
|
|
326
|
-
|
|
327
|
-
|
|
335
|
+
if (!fields.has('host')) {
|
|
336
|
+
fields.set('host', host);
|
|
337
|
+
}
|
|
338
|
+
if (!fields.has('from')) {
|
|
339
|
+
fields.set('from', 'user@sfml-dev.org');
|
|
340
|
+
}
|
|
341
|
+
if (!fields.has('user-agent')) {
|
|
342
|
+
fields.set('user-agent', 'sfml.js');
|
|
343
|
+
}
|
|
328
344
|
if (!fields.has('content-length')) {
|
|
329
345
|
fields.set('content-length', String(this.body.length));
|
|
330
346
|
}
|
|
331
|
-
if (!fields.has('connection'))
|
|
347
|
+
if (!fields.has('connection')) {
|
|
348
|
+
fields.set('connection', 'close');
|
|
349
|
+
}
|
|
332
350
|
if (fields.has('transfer-encoding')) {
|
|
333
351
|
throw new TypeError('Request bodies use Content-Length framing');
|
|
334
352
|
}
|
|
335
353
|
if (Number(fields.get('content-length')) !== this.body.length) {
|
|
336
|
-
throw new RangeError(
|
|
354
|
+
throw new RangeError(
|
|
355
|
+
'Content-Length does not match the request body',
|
|
356
|
+
);
|
|
337
357
|
}
|
|
338
|
-
const header =
|
|
358
|
+
const header =
|
|
359
|
+
`${methods[this.method]} ${this.uri} ` +
|
|
339
360
|
`HTTP/${this.major}.${this.minor}\r\n` +
|
|
340
361
|
[ ...fields ].map(([ key, value ]) => `${key}: ${value}\r\n`).join('') +
|
|
341
362
|
'\r\n';
|
|
342
|
-
return {
|
|
343
|
-
|
|
363
|
+
return {
|
|
364
|
+
bytes: Buffer.concat([ Buffer.from(header), this.body ]),
|
|
365
|
+
head: this.method === HttpMethod.Head,
|
|
366
|
+
};
|
|
344
367
|
}
|
|
345
368
|
}
|
|
346
369
|
/**
|
|
@@ -366,10 +389,13 @@ export class HttpResponse {
|
|
|
366
389
|
* @ko 응답을 생성하며 기본적으로 연결 실패를 나타냅니다.
|
|
367
390
|
* fields의 키는 소문자여야 합니다.
|
|
368
391
|
*/
|
|
369
|
-
constructor(
|
|
392
|
+
constructor(
|
|
393
|
+
private readonly status: HttpStatus = HttpStatus.ConnectionFailed,
|
|
370
394
|
private readonly fields: ReadonlyMap<string, string> = new Map(),
|
|
371
395
|
private readonly body = Buffer.alloc(0),
|
|
372
|
-
private readonly major = 0,
|
|
396
|
+
private readonly major = 0,
|
|
397
|
+
private readonly minor = 0,
|
|
398
|
+
) {}
|
|
373
399
|
/**
|
|
374
400
|
* Read a response header by case-insensitive name;
|
|
375
401
|
* return an empty string if absent.
|
|
@@ -379,42 +405,54 @@ export class HttpResponse {
|
|
|
379
405
|
* @ko 대소문자를 구분하지 않는 이름으로 응답 헤더를 읽습니다.
|
|
380
406
|
* 없으면 빈 문자열을 반환합니다.
|
|
381
407
|
*/
|
|
382
|
-
getField(field: string): string {
|
|
408
|
+
getField(field: string): string {
|
|
409
|
+
return this.fields.get(field.toLowerCase()) ?? '';
|
|
410
|
+
}
|
|
383
411
|
/**
|
|
384
412
|
* Return the server status code or client error code.
|
|
385
413
|
* @zh 返回服务端状态码或客户端错误码。
|
|
386
414
|
* @ja サーバーの状態コードまたはクライアントのエラーコードを返します。
|
|
387
415
|
* @ko 서버 상태 코드 또는 클라이언트 오류 코드를 반환합니다.
|
|
388
416
|
*/
|
|
389
|
-
getStatus(): HttpStatus {
|
|
417
|
+
getStatus(): HttpStatus {
|
|
418
|
+
return this.status;
|
|
419
|
+
}
|
|
390
420
|
/**
|
|
391
421
|
* Return the response's HTTP major version.
|
|
392
422
|
* @zh 返回响应的 HTTP 主版本。
|
|
393
423
|
* @ja レスポンスの HTTP メジャーバージョンを返します。
|
|
394
424
|
* @ko 응답의 HTTP 주 버전을 반환합니다.
|
|
395
425
|
*/
|
|
396
|
-
getMajorHttpVersion(): number {
|
|
426
|
+
getMajorHttpVersion(): number {
|
|
427
|
+
return this.major;
|
|
428
|
+
}
|
|
397
429
|
/**
|
|
398
430
|
* Return the response's HTTP minor version.
|
|
399
431
|
* @zh 返回响应的 HTTP 次版本。
|
|
400
432
|
* @ja レスポンスの HTTP マイナーバージョンを返します。
|
|
401
433
|
* @ko 응답의 HTTP 부 버전을 반환합니다.
|
|
402
434
|
*/
|
|
403
|
-
getMinorHttpVersion(): number {
|
|
435
|
+
getMinorHttpVersion(): number {
|
|
436
|
+
return this.minor;
|
|
437
|
+
}
|
|
404
438
|
/**
|
|
405
439
|
* Decode the response body as a UTF-8 string.
|
|
406
440
|
* @zh 将响应体按 UTF-8 解码为字符串。
|
|
407
441
|
* @ja レスポンス本文を UTF-8 文字列として復号します。
|
|
408
442
|
* @ko 응답 본문을 UTF-8 문자열로 디코딩합니다.
|
|
409
443
|
*/
|
|
410
|
-
getBody(): string {
|
|
444
|
+
getBody(): string {
|
|
445
|
+
return this.body.toString('utf8');
|
|
446
|
+
}
|
|
411
447
|
/**
|
|
412
448
|
* Return an independent copy of the raw response body bytes.
|
|
413
449
|
* @zh 返回响应体原始字节的独立副本。
|
|
414
450
|
* @ja レスポンス本文の生バイトの独立したコピーを返します。
|
|
415
451
|
* @ko 응답 본문 원시 바이트의 독립적인 복사본을 반환합니다.
|
|
416
452
|
*/
|
|
417
|
-
getBodyBytes(): Uint8Array {
|
|
453
|
+
getBodyBytes(): Uint8Array {
|
|
454
|
+
return Uint8Array.from(this.body);
|
|
455
|
+
}
|
|
418
456
|
}
|
|
419
457
|
|
|
420
458
|
/** Streaming HTTP/1 response parser for the explicit-version request path. */
|
|
@@ -434,52 +472,86 @@ class ResponseParser {
|
|
|
434
472
|
private done = false;
|
|
435
473
|
constructor(private readonly head: boolean) {}
|
|
436
474
|
feed(data: Buffer, ended = false): HttpResponse | undefined {
|
|
437
|
-
if (this.pending.length)
|
|
438
|
-
|
|
475
|
+
if (this.pending.length) {
|
|
476
|
+
this.pending = Buffer.concat([ this.pending, data ]);
|
|
477
|
+
} else {
|
|
478
|
+
this.pending = data;
|
|
479
|
+
}
|
|
439
480
|
if (!this.status) {
|
|
440
481
|
const end = this.pending.indexOf('\r\n\r\n');
|
|
441
482
|
if (end < 0) {
|
|
442
|
-
if (this.pending.length > 65536 || ended)
|
|
483
|
+
if (this.pending.length > 65536 || ended) {
|
|
484
|
+
throw new Error('Invalid header');
|
|
485
|
+
}
|
|
443
486
|
return undefined;
|
|
444
487
|
}
|
|
445
488
|
const lines = this.pending.toString('latin1', 0, end).split('\r\n');
|
|
446
|
-
const match = /^HTTP\/(\d+)\.(\d+) (\d{3})(?: |$)/.exec(
|
|
447
|
-
|
|
448
|
-
|
|
489
|
+
const match = /^HTTP\/(\d+)\.(\d+) (\d{3})(?: |$)/.exec(
|
|
490
|
+
lines.shift()!,
|
|
491
|
+
);
|
|
492
|
+
if (!match) {
|
|
493
|
+
throw new Error('Invalid HTTP status line');
|
|
494
|
+
}
|
|
495
|
+
this.major = Number(match[1]);
|
|
496
|
+
this.minor = Number(match[2]);
|
|
449
497
|
this.status = Number(match[3]);
|
|
450
|
-
for (const line of lines)
|
|
498
|
+
for (const line of lines) {
|
|
499
|
+
this.field(line);
|
|
500
|
+
}
|
|
451
501
|
this.pending = this.pending.subarray(end + 4);
|
|
452
502
|
if (this.status >= 100 && this.status < 200 && this.status !== 101) {
|
|
453
|
-
this.status = 0;
|
|
503
|
+
this.status = 0;
|
|
504
|
+
this.fields.clear();
|
|
454
505
|
return this.feed(Buffer.alloc(0), ended);
|
|
455
506
|
}
|
|
456
|
-
this.chunked =
|
|
457
|
-
.
|
|
458
|
-
|
|
459
|
-
|
|
507
|
+
this.chunked =
|
|
508
|
+
this.fields
|
|
509
|
+
.get('transfer-encoding')
|
|
510
|
+
?.toLowerCase()
|
|
511
|
+
.split(',')
|
|
512
|
+
.map(value => value.trim())
|
|
513
|
+
.includes('chunked') ?? false;
|
|
460
514
|
const length = this.fields.get('content-length');
|
|
461
515
|
if (!this.chunked && length !== undefined) {
|
|
462
|
-
if (!/^\d+$/.test(length))
|
|
516
|
+
if (!/^\d+$/.test(length)) {
|
|
517
|
+
throw new Error('Invalid Content-Length');
|
|
518
|
+
}
|
|
463
519
|
this.remaining = Number(length);
|
|
464
|
-
if (
|
|
520
|
+
if (
|
|
521
|
+
!Number.isSafeInteger(this.remaining) ||
|
|
522
|
+
this.remaining > 67108864
|
|
523
|
+
) {
|
|
465
524
|
throw new Error('HTTP body exceeds 64 MiB');
|
|
466
525
|
}
|
|
467
526
|
}
|
|
468
|
-
if (
|
|
469
|
-
|
|
527
|
+
if (
|
|
528
|
+
this.head ||
|
|
529
|
+
this.status === 204 ||
|
|
530
|
+
this.status === 304 ||
|
|
531
|
+
this.status === 101
|
|
532
|
+
) {
|
|
533
|
+
this.done = true;
|
|
534
|
+
}
|
|
470
535
|
}
|
|
471
536
|
while (!this.done && this.pending.length) {
|
|
472
537
|
if (this.chunked) {
|
|
473
538
|
if (this.trailers) {
|
|
474
539
|
const end = this.pending.indexOf('\r\n');
|
|
475
|
-
if (end < 0)
|
|
476
|
-
|
|
540
|
+
if (end < 0) {
|
|
541
|
+
break;
|
|
542
|
+
}
|
|
543
|
+
if (end === 0) {
|
|
544
|
+
this.done = true;
|
|
545
|
+
break;
|
|
546
|
+
}
|
|
477
547
|
this.field(this.pending.toString('latin1', 0, end));
|
|
478
548
|
this.pending = this.pending.subarray(end + 2);
|
|
479
549
|
continue;
|
|
480
550
|
}
|
|
481
551
|
if (this.chunkCrlf) {
|
|
482
|
-
if (this.pending.length < 2)
|
|
552
|
+
if (this.pending.length < 2) {
|
|
553
|
+
break;
|
|
554
|
+
}
|
|
483
555
|
if (this.pending.toString('latin1', 0, 2) !== '\r\n') {
|
|
484
556
|
throw new Error('Invalid chunk terminator');
|
|
485
557
|
}
|
|
@@ -488,15 +560,24 @@ class ResponseParser {
|
|
|
488
560
|
}
|
|
489
561
|
if (this.chunkRemaining === undefined) {
|
|
490
562
|
const end = this.pending.indexOf('\r\n');
|
|
491
|
-
if (end < 0)
|
|
492
|
-
|
|
493
|
-
|
|
563
|
+
if (end < 0) {
|
|
564
|
+
break;
|
|
565
|
+
}
|
|
566
|
+
const text = this.pending
|
|
567
|
+
.toString('latin1', 0, end)
|
|
568
|
+
.split(';')[0];
|
|
569
|
+
if (!/^[0-9a-f]+$/i.test(text)) {
|
|
570
|
+
throw new Error('Invalid chunk length');
|
|
571
|
+
}
|
|
494
572
|
this.chunkRemaining = parseInt(text, 16);
|
|
495
573
|
if (this.chunkRemaining > 67108864 - this.count) {
|
|
496
574
|
throw new Error('HTTP body exceeds 64 MiB');
|
|
497
575
|
}
|
|
498
576
|
this.pending = this.pending.subarray(end + 2);
|
|
499
|
-
if (!this.chunkRemaining) {
|
|
577
|
+
if (!this.chunkRemaining) {
|
|
578
|
+
this.trailers = true;
|
|
579
|
+
continue;
|
|
580
|
+
}
|
|
500
581
|
}
|
|
501
582
|
const count = Math.min(this.chunkRemaining, this.pending.length);
|
|
502
583
|
this.consume(count);
|
|
@@ -506,38 +587,65 @@ class ResponseParser {
|
|
|
506
587
|
this.chunkCrlf = true;
|
|
507
588
|
}
|
|
508
589
|
} else {
|
|
509
|
-
const count = Math.min(
|
|
590
|
+
const count = Math.min(
|
|
591
|
+
this.remaining ?? Infinity,
|
|
592
|
+
this.pending.length,
|
|
593
|
+
);
|
|
510
594
|
this.consume(count);
|
|
511
595
|
if (this.remaining !== undefined) {
|
|
512
596
|
this.remaining -= count;
|
|
513
|
-
if (!this.remaining)
|
|
597
|
+
if (!this.remaining) {
|
|
598
|
+
this.done = true;
|
|
599
|
+
}
|
|
514
600
|
}
|
|
515
601
|
}
|
|
516
602
|
}
|
|
517
|
-
if (this.remaining === 0 && !this.chunked)
|
|
518
|
-
|
|
603
|
+
if (this.remaining === 0 && !this.chunked) {
|
|
604
|
+
this.done = true;
|
|
605
|
+
}
|
|
606
|
+
if (this.pending.length > 65536) {
|
|
607
|
+
throw new Error('HTTP framing exceeds limit');
|
|
608
|
+
}
|
|
519
609
|
if (ended && !this.done) {
|
|
520
610
|
if (this.chunked || this.remaining !== undefined) {
|
|
521
611
|
throw new Error('Truncated HTTP response');
|
|
522
612
|
}
|
|
523
613
|
this.done = true;
|
|
524
614
|
}
|
|
525
|
-
return this.done
|
|
526
|
-
|
|
615
|
+
return this.done
|
|
616
|
+
? new HttpResponse(
|
|
617
|
+
this.status,
|
|
618
|
+
this.fields,
|
|
619
|
+
Buffer.concat(this.chunks, this.count),
|
|
620
|
+
this.major,
|
|
621
|
+
this.minor,
|
|
622
|
+
)
|
|
623
|
+
: undefined;
|
|
527
624
|
}
|
|
528
625
|
private consume(count: number): void {
|
|
529
|
-
if (this.count + count > 67108864)
|
|
530
|
-
|
|
626
|
+
if (this.count + count > 67108864) {
|
|
627
|
+
throw new Error('HTTP body exceeds 64 MiB');
|
|
628
|
+
}
|
|
629
|
+
if (count) {
|
|
630
|
+
this.chunks.push(this.pending.subarray(0, count));
|
|
631
|
+
}
|
|
531
632
|
this.count += count;
|
|
532
633
|
this.pending = this.pending.subarray(count);
|
|
533
634
|
}
|
|
534
635
|
private field(line: string): void {
|
|
535
636
|
const split = line.indexOf(':');
|
|
536
|
-
if (split <= 0)
|
|
637
|
+
if (split <= 0) {
|
|
638
|
+
throw new Error('Invalid HTTP header field');
|
|
639
|
+
}
|
|
537
640
|
const name = line.slice(0, split).toLowerCase();
|
|
538
641
|
const value = line.slice(split + 1).trim();
|
|
539
|
-
if (
|
|
540
|
-
|
|
642
|
+
if (
|
|
643
|
+
name === 'content-length' &&
|
|
644
|
+
this.fields.has(name) &&
|
|
645
|
+
this.fields.get(name) !== value
|
|
646
|
+
) {
|
|
647
|
+
throw new Error('Conflicting lengths');
|
|
648
|
+
}
|
|
541
649
|
this.fields.set(name, value);
|
|
542
650
|
}
|
|
543
651
|
}
|
|
@@ -580,7 +688,9 @@ export class Http {
|
|
|
580
688
|
* port가 0이면 URL 포트 또는 프로토콜 기본 포트를 유지합니다.
|
|
581
689
|
*/
|
|
582
690
|
constructor(host?: string, port = 0, type?: IpAddressType) {
|
|
583
|
-
if (host !== undefined)
|
|
691
|
+
if (host !== undefined) {
|
|
692
|
+
this.setHost(host, port, type);
|
|
693
|
+
}
|
|
584
694
|
}
|
|
585
695
|
/**
|
|
586
696
|
* Set the server and optional IP address family, returning whether the
|
|
@@ -593,15 +703,28 @@ export class Http {
|
|
|
593
703
|
*/
|
|
594
704
|
setHost(host: string, port = 0, type?: IpAddressType): boolean {
|
|
595
705
|
uint(port, 65535);
|
|
596
|
-
if (type !== undefined)
|
|
597
|
-
|
|
706
|
+
if (type !== undefined) {
|
|
707
|
+
uint(type, 1);
|
|
708
|
+
}
|
|
709
|
+
if (typeof host !== 'string') {
|
|
710
|
+
throw new TypeError('Expected a hostname');
|
|
711
|
+
}
|
|
598
712
|
try {
|
|
599
|
-
const parsed = new URL(
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
713
|
+
const parsed = new URL(
|
|
714
|
+
host.includes('://') ? host : `http://${host}`,
|
|
715
|
+
);
|
|
716
|
+
if (![ 'http:', 'https:' ].includes(parsed.protocol)) {
|
|
717
|
+
return false;
|
|
718
|
+
}
|
|
719
|
+
if (port) {
|
|
720
|
+
parsed.port = String(port);
|
|
721
|
+
}
|
|
722
|
+
this.host = parsed;
|
|
723
|
+
this.family = type;
|
|
603
724
|
return true;
|
|
604
|
-
} catch {
|
|
725
|
+
} catch {
|
|
726
|
+
return false;
|
|
727
|
+
}
|
|
605
728
|
}
|
|
606
729
|
/**
|
|
607
730
|
* Send a request snapshot asynchronously and return the complete response,
|
|
@@ -631,35 +754,60 @@ export class Http {
|
|
|
631
754
|
* キャンセル時は ConnectionFailed を返します。
|
|
632
755
|
* @ko signal - 선택적 취소 신호. 취소 시 ConnectionFailed를 반환합니다.
|
|
633
756
|
*/
|
|
634
|
-
async sendRequest(
|
|
635
|
-
|
|
636
|
-
|
|
757
|
+
async sendRequest(
|
|
758
|
+
request: HttpRequest,
|
|
759
|
+
timeout: NetworkTime = Time.Zero,
|
|
760
|
+
verifyServer = true,
|
|
761
|
+
signal?: AbortSignal,
|
|
762
|
+
): Promise<HttpResponse> {
|
|
763
|
+
if (!(request instanceof HttpRequest)) {
|
|
764
|
+
throw new TypeError('Expected Request');
|
|
765
|
+
}
|
|
637
766
|
const milliseconds = millisecondsOf(timeout);
|
|
638
767
|
const host = this.host;
|
|
639
|
-
if (!host || signal?.aborted)
|
|
768
|
+
if (!host || signal?.aborted) {
|
|
769
|
+
return new HttpResponse();
|
|
770
|
+
}
|
|
640
771
|
const prepared = request[prepareRequest](host.host);
|
|
641
|
-
const options = {
|
|
772
|
+
const options = {
|
|
773
|
+
host: host.hostname.replace(/^\[|\]$/g, ''),
|
|
642
774
|
port: Number(host.port) || (host.protocol === 'https:' ? 443 : 80),
|
|
643
|
-
family:
|
|
775
|
+
family:
|
|
776
|
+
this.family === undefined
|
|
777
|
+
? 0
|
|
778
|
+
: this.family === IpAddressType.IpV4
|
|
779
|
+
? 4
|
|
780
|
+
: 6,
|
|
781
|
+
};
|
|
644
782
|
return new Promise(resolve => {
|
|
645
783
|
let finished = false;
|
|
646
784
|
let timer: NodeJS.Timeout | undefined;
|
|
647
785
|
const parser = new ResponseParser(prepared.head);
|
|
648
786
|
let socket: Socket;
|
|
649
787
|
const finish = (response = new HttpResponse()) => {
|
|
650
|
-
if (finished)
|
|
788
|
+
if (finished) {
|
|
789
|
+
return;
|
|
790
|
+
}
|
|
651
791
|
finished = true;
|
|
652
|
-
if (timer)
|
|
792
|
+
if (timer) {
|
|
793
|
+
clearTimeout(timer);
|
|
794
|
+
}
|
|
653
795
|
signal?.removeEventListener('abort', abort);
|
|
654
796
|
socket?.destroy();
|
|
655
797
|
resolve(response);
|
|
656
798
|
};
|
|
657
|
-
function abort() {
|
|
799
|
+
function abort() {
|
|
800
|
+
finish();
|
|
801
|
+
}
|
|
658
802
|
const receive = (data: Buffer, ended = false) => {
|
|
659
803
|
try {
|
|
660
804
|
const response = parser.feed(data, ended);
|
|
661
|
-
if (response)
|
|
662
|
-
|
|
805
|
+
if (response) {
|
|
806
|
+
finish(response);
|
|
807
|
+
}
|
|
808
|
+
} catch {
|
|
809
|
+
finish(new HttpResponse(HttpStatus.InvalidResponse));
|
|
810
|
+
}
|
|
663
811
|
};
|
|
664
812
|
if (host.protocol === 'https:') {
|
|
665
813
|
const secureOptions = {
|
|
@@ -667,14 +815,21 @@ export class Http {
|
|
|
667
815
|
servername: isIP(options.host) ? undefined : options.host,
|
|
668
816
|
rejectUnauthorized: verifyServer,
|
|
669
817
|
};
|
|
670
|
-
socket = connectTls(secureOptions, () =>
|
|
671
|
-
|
|
672
|
-
|
|
818
|
+
socket = connectTls(secureOptions, () =>
|
|
819
|
+
socket.write(prepared.bytes),
|
|
820
|
+
);
|
|
821
|
+
} else {
|
|
822
|
+
socket = connectTcp(options, () => socket.write(prepared.bytes));
|
|
823
|
+
}
|
|
824
|
+
socket
|
|
825
|
+
.on('data', receive)
|
|
673
826
|
.on('end', () => receive(Buffer.alloc(0), true))
|
|
674
827
|
.on('error', () => finish())
|
|
675
828
|
.on('close', () => finish());
|
|
676
829
|
signal?.addEventListener('abort', abort, { once: true });
|
|
677
|
-
if (milliseconds)
|
|
830
|
+
if (milliseconds) {
|
|
831
|
+
timer = setTimeout(abort, milliseconds);
|
|
832
|
+
}
|
|
678
833
|
});
|
|
679
834
|
}
|
|
680
835
|
}
|