sfml.js 1.0.0-rc.2 → 1.1.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 (314) hide show
  1. package/README.ja.md +16 -11
  2. package/README.ko.md +16 -11
  3. package/README.md +16 -11
  4. package/README.zh-CN.md +16 -11
  5. package/assets/breakout.png +0 -0
  6. package/binding.gyp +23 -16
  7. package/dist/audio/effect-processor.js +9 -3
  8. package/dist/audio/input-sound-file.js +41 -15
  9. package/dist/audio/listener.js +33 -11
  10. package/dist/audio/music.js +41 -18
  11. package/dist/audio/output-sound-file.js +4 -2
  12. package/dist/audio/playback-device.js +11 -3
  13. package/dist/audio/sound-buffer-recorder.js +8 -5
  14. package/dist/audio/sound-buffer.js +40 -17
  15. package/dist/audio/sound-file-factory.js +21 -9
  16. package/dist/audio/sound-file-reader.js +21 -7
  17. package/dist/audio/sound-recorder.js +36 -15
  18. package/dist/audio/sound-source.js +117 -40
  19. package/dist/audio/sound-stream.js +14 -5
  20. package/dist/audio/sound.js +9 -4
  21. package/dist/audio/value.js +42 -16
  22. package/dist/graphics/blend-mode.js +2 -1
  23. package/dist/graphics/boolean-vector.d.ts +1 -2
  24. package/dist/graphics/boolean-vector.js +19 -9
  25. package/dist/graphics/color.js +2 -1
  26. package/dist/graphics/convex-shape.js +7 -3
  27. package/dist/graphics/font.js +52 -23
  28. package/dist/graphics/glsl-matrix.js +8 -3
  29. package/dist/graphics/glsl.js +47 -16
  30. package/dist/graphics/glyph.js +27 -10
  31. package/dist/graphics/image.js +30 -14
  32. package/dist/graphics/rect.js +15 -8
  33. package/dist/graphics/render-states.js +18 -9
  34. package/dist/graphics/render-target.js +49 -23
  35. package/dist/graphics/render-texture.js +24 -12
  36. package/dist/graphics/render-window.js +6 -3
  37. package/dist/graphics/shader.js +67 -32
  38. package/dist/graphics/shape.js +25 -13
  39. package/dist/graphics/sprite.js +19 -9
  40. package/dist/graphics/stencil-mode.js +10 -6
  41. package/dist/graphics/swap.js +2 -1
  42. package/dist/graphics/text.js +123 -48
  43. package/dist/graphics/texture.js +48 -24
  44. package/dist/graphics/transform.js +29 -13
  45. package/dist/graphics/transformable.js +2 -1
  46. package/dist/graphics/vertex-array.js +40 -18
  47. package/dist/graphics/vertex-buffer.js +39 -19
  48. package/dist/graphics/vertex.js +6 -3
  49. package/dist/graphics/view.js +30 -14
  50. package/dist/index.d.ts +4 -4
  51. package/dist/internal/audio-codec-registry.js +12 -6
  52. package/dist/internal/audio-input-stream.js +2 -1
  53. package/dist/internal/audio-music-pump.js +58 -24
  54. package/dist/internal/audio-recorder-session.js +17 -7
  55. package/dist/internal/audio-worker-session.js +28 -10
  56. package/dist/internal/audio-worker.js +40 -21
  57. package/dist/internal/glsl-value.d.ts +3 -1
  58. package/dist/internal/glsl-value.js +18 -8
  59. package/dist/internal/graphics-state.js +29 -15
  60. package/dist/internal/input-stream.d.ts +3 -1
  61. package/dist/internal/input-stream.js +16 -7
  62. package/dist/internal/numeric-properties.js +2 -1
  63. package/dist/internal/render-states.js +2 -1
  64. package/dist/internal/scalar.js +22 -13
  65. package/dist/internal/shaped-glyph.d.ts +3 -1
  66. package/dist/internal/shaped-glyph.js +12 -4
  67. package/dist/internal/struct-view.d.ts +3 -1
  68. package/dist/internal/struct-view.js +3 -1
  69. package/dist/internal/transformable.js +2 -1
  70. package/dist/network/common.js +23 -11
  71. package/dist/network/dns.js +25 -17
  72. package/dist/network/ftp.js +43 -18
  73. package/dist/network/http.js +126 -56
  74. package/dist/network/ip-address.js +50 -25
  75. package/dist/network/packet.js +63 -25
  76. package/dist/network/sftp.js +175 -74
  77. package/dist/network/socket-selector.js +28 -14
  78. package/dist/network/socket.js +71 -30
  79. package/dist/network/tcp-listener.js +42 -19
  80. package/dist/network/tcp-socket.js +111 -53
  81. package/dist/network/tls-material.js +2 -1
  82. package/dist/network/udp-socket.js +68 -33
  83. package/dist/system/angle.js +2 -1
  84. package/dist/system/clock.js +4 -2
  85. package/dist/system/err.js +5 -2
  86. package/dist/system/file-input-stream.js +8 -4
  87. package/dist/system/input-stream.js +6 -3
  88. package/dist/system/operators.d.ts +1 -1
  89. package/dist/system/operators.js +90 -50
  90. package/dist/system/sleep.js +4 -2
  91. package/dist/system/time.js +12 -6
  92. package/dist/system/vector2.js +6 -7
  93. package/dist/system/vector3.js +11 -6
  94. package/dist/window/clipboard.js +2 -1
  95. package/dist/window/context.js +10 -5
  96. package/dist/window/cursor.js +2 -1
  97. package/dist/window/event.js +9 -5
  98. package/dist/window/mouse.js +4 -2
  99. package/dist/window/video-mode.js +2 -1
  100. package/dist/window/window.js +19 -12
  101. package/native/addon.cc +25 -12
  102. package/native/audio-binding.cc +266 -126
  103. package/native/audio-codec-binding.cc +147 -77
  104. package/native/audio-device-binding.cc +76 -49
  105. package/native/audio-file-binding.cc +116 -67
  106. package/native/audio-recorder-binding.cc +147 -98
  107. package/native/audio-recorder.h +1 -1
  108. package/native/audio-state.h +53 -30
  109. package/native/audio-stream-binding.cc +148 -94
  110. package/native/audio-stream.h +5 -4
  111. package/native/audio-worker-binding.cc +163 -81
  112. package/native/audio-worker.h +35 -19
  113. package/native/batch-drawable.cc +29 -16
  114. package/native/batch-drawable.h +11 -8
  115. package/native/context-binding.cc +40 -27
  116. package/native/context-settings.h +15 -8
  117. package/native/deferred-delete.cc +9 -6
  118. package/native/deferred-delete.h +7 -8
  119. package/native/error-stream-binding.cc +42 -28
  120. package/native/font-binding.cc +89 -48
  121. package/native/graphics-binding.cc +122 -72
  122. package/native/graphics-state.h +50 -36
  123. package/native/image-binding.cc +38 -24
  124. package/native/image-state.h +26 -18
  125. package/native/input-binding.cc +42 -27
  126. package/native/render-window-binding.cc +38 -23
  127. package/native/shader-binding.cc +115 -50
  128. package/native/shape-binding.cc +94 -49
  129. package/native/shape-state.h +18 -9
  130. package/native/shaped-glyph-storage.h +9 -5
  131. package/native/stream-binding.cc +143 -117
  132. package/native/system-binding.cc +17 -12
  133. package/native/text-binding.cc +68 -35
  134. package/native/text-state.h +25 -18
  135. package/native/texture-binding.cc +42 -33
  136. package/native/texture-resource.h +20 -12
  137. package/native/time-value.h +3 -1
  138. package/native/transformable-binding.cc +12 -6
  139. package/native/transformable-state.h +6 -6
  140. package/native/value-binding.h +46 -28
  141. package/native/value-layout.h +10 -10
  142. package/native/values-binding.cc +29 -23
  143. package/native/vertex-binding.cc +18 -12
  144. package/native/vertex-buffer-binding.cc +38 -24
  145. package/native/vertex-storage.h +2 -2
  146. package/native/view-data.h +4 -3
  147. package/native/window-binding.cc +75 -45
  148. package/native/window-event.h +20 -11
  149. package/native/window-owner.h +15 -12
  150. package/package.json +43 -50
  151. package/src/audio/effect-processor.ts +9 -3
  152. package/src/audio/input-sound-file.ts +108 -36
  153. package/src/audio/listener.ts +39 -13
  154. package/src/audio/music.ts +112 -47
  155. package/src/audio/output-sound-file.ts +40 -11
  156. package/src/audio/playback-device.ts +23 -8
  157. package/src/audio/sound-buffer-recorder.ts +35 -13
  158. package/src/audio/sound-buffer.ts +166 -58
  159. package/src/audio/sound-file-factory.ts +127 -49
  160. package/src/audio/sound-file-reader.ts +49 -15
  161. package/src/audio/sound-recorder.ts +100 -34
  162. package/src/audio/sound-source.ts +142 -45
  163. package/src/audio/sound-stream.ts +15 -5
  164. package/src/audio/sound.ts +12 -4
  165. package/src/audio/value.ts +89 -26
  166. package/src/graphics/blend-mode.ts +3 -1
  167. package/src/graphics/boolean-vector.ts +56 -26
  168. package/src/graphics/color.ts +3 -1
  169. package/src/graphics/convex-shape.ts +10 -3
  170. package/src/graphics/font.ts +171 -50
  171. package/src/graphics/glsl-matrix.ts +9 -3
  172. package/src/graphics/glsl.ts +81 -35
  173. package/src/graphics/glyph.ts +67 -26
  174. package/src/graphics/image.ts +119 -45
  175. package/src/graphics/rect.ts +56 -24
  176. package/src/graphics/render-states.ts +31 -14
  177. package/src/graphics/render-target.ts +197 -81
  178. package/src/graphics/render-texture.ts +36 -12
  179. package/src/graphics/render-window.ts +9 -3
  180. package/src/graphics/shader.ts +186 -61
  181. package/src/graphics/shape.ts +93 -35
  182. package/src/graphics/sprite.ts +64 -25
  183. package/src/graphics/stencil-mode.ts +12 -6
  184. package/src/graphics/swap.ts +3 -2
  185. package/src/graphics/text.ts +229 -74
  186. package/src/graphics/texture.ts +180 -61
  187. package/src/graphics/transform.ts +132 -47
  188. package/src/graphics/transformable.ts +3 -1
  189. package/src/graphics/vertex-array.ts +87 -35
  190. package/src/graphics/vertex-buffer.ts +97 -33
  191. package/src/graphics/vertex.ts +9 -3
  192. package/src/graphics/view.ts +59 -21
  193. package/src/index.ts +101 -23
  194. package/src/internal/audio-codec-registry.ts +18 -6
  195. package/src/internal/audio-input-stream.ts +6 -2
  196. package/src/internal/audio-music-pump.ts +116 -41
  197. package/src/internal/audio-native.ts +249 -96
  198. package/src/internal/audio-recorder-session.ts +68 -24
  199. package/src/internal/audio-worker-session.ts +65 -21
  200. package/src/internal/audio-worker.ts +85 -41
  201. package/src/internal/glsl-value.ts +26 -11
  202. package/src/internal/graphics-state.ts +57 -20
  203. package/src/internal/input-stream.ts +23 -10
  204. package/src/internal/native.ts +252 -99
  205. package/src/internal/numeric-properties.ts +2 -1
  206. package/src/internal/render-states.ts +3 -1
  207. package/src/internal/scalar.ts +33 -14
  208. package/src/internal/shaped-glyph.ts +25 -12
  209. package/src/internal/struct-view.ts +40 -13
  210. package/src/internal/transformable.ts +3 -1
  211. package/src/network/common.ts +35 -11
  212. package/src/network/dns.ts +95 -38
  213. package/src/network/ftp.ts +129 -47
  214. package/src/network/http.ts +232 -77
  215. package/src/network/ip-address.ts +118 -48
  216. package/src/network/packet.ts +161 -54
  217. package/src/network/sftp.ts +498 -168
  218. package/src/network/socket-selector.ts +66 -26
  219. package/src/network/socket.ts +133 -46
  220. package/src/network/tcp-listener.ts +78 -29
  221. package/src/network/tcp-socket.ts +240 -95
  222. package/src/network/tls-material.ts +6 -2
  223. package/src/network/udp-socket.ts +147 -53
  224. package/src/system/angle.ts +3 -1
  225. package/src/system/clock.ts +12 -4
  226. package/src/system/err.ts +5 -2
  227. package/src/system/file-input-stream.ts +12 -4
  228. package/src/system/input-stream.ts +9 -3
  229. package/src/system/operators.ts +290 -115
  230. package/src/system/sleep.ts +6 -2
  231. package/src/system/time.ts +18 -6
  232. package/src/system/timeout-with-predicate.ts +19 -8
  233. package/src/system/vector2.ts +101 -49
  234. package/src/system/vector3.ts +85 -36
  235. package/src/window/clipboard.ts +3 -1
  236. package/src/window/context.ts +15 -5
  237. package/src/window/cursor.ts +3 -1
  238. package/src/window/event.ts +26 -12
  239. package/src/window/mouse.ts +6 -2
  240. package/src/window/video-mode.ts +3 -1
  241. package/src/window/window.ts +119 -37
  242. package/test/native/audio-probes.cc +74 -36
  243. package/test/native/deferred-delete-probes.cc +17 -11
  244. package/test/native/drawable-probes.cc +70 -36
  245. package/test/native/exception-probes.cc +14 -9
  246. package/test/native/image-probes.cc +20 -10
  247. package/test/native/matrix-probes.cc +8 -4
  248. package/test/native/network-oracle.cc +17 -11
  249. package/test/native/render-texture-probes.cc +33 -24
  250. package/test/native/shader-probes.cc +9 -9
  251. package/test/native/shape-probes.cc +37 -23
  252. package/test/native/sprite-probes.cc +41 -25
  253. package/test/native/system-probes.cc +58 -35
  254. package/test/native/text-probes.cc +77 -44
  255. package/test/native/texture-probes.cc +43 -26
  256. package/test/native/time-probes.cc +18 -12
  257. package/test/native/transform-probes.cc +15 -10
  258. package/test/native/transformable-probes.cc +23 -17
  259. package/test/native/value-oracle.h +9 -4
  260. package/test/native/value-probes.cc +31 -23
  261. package/test/native/vertex-array-probes.cc +22 -14
  262. package/test/native/vertex-buffer-probes.cc +28 -17
  263. package/test/native/view-probes.cc +29 -17
  264. package/test/native/window-probes.cc +66 -33
  265. package/tools/ensure-sfml.cjs +9 -0
  266. package/tools/generate-network-constants.cjs +11 -6
  267. package/tools/generate-window-constants.cjs +31 -12
  268. package/docs/README.ja.md +0 -30
  269. package/docs/README.ko.md +0 -30
  270. package/docs/README.md +0 -30
  271. package/docs/README.zh-CN.md +0 -30
  272. package/docs/api.ja.md +0 -84
  273. package/docs/api.ko.md +0 -84
  274. package/docs/api.md +0 -81
  275. package/docs/api.zh-CN.md +0 -81
  276. package/docs/audio.ja.md +0 -249
  277. package/docs/audio.ko.md +0 -251
  278. package/docs/audio.md +0 -252
  279. package/docs/audio.zh-CN.md +0 -252
  280. package/docs/concepts.ja.md +0 -130
  281. package/docs/concepts.ko.md +0 -130
  282. package/docs/concepts.md +0 -131
  283. package/docs/concepts.zh-CN.md +0 -131
  284. package/docs/development.ja.md +0 -148
  285. package/docs/development.ko.md +0 -148
  286. package/docs/development.md +0 -150
  287. package/docs/development.zh-CN.md +0 -150
  288. package/docs/getting-started.ja.md +0 -154
  289. package/docs/getting-started.ko.md +0 -154
  290. package/docs/getting-started.md +0 -156
  291. package/docs/getting-started.zh-CN.md +0 -156
  292. package/docs/graphics.ja.md +0 -256
  293. package/docs/graphics.ko.md +0 -254
  294. package/docs/graphics.md +0 -255
  295. package/docs/graphics.zh-CN.md +0 -257
  296. package/docs/network.ja.md +0 -214
  297. package/docs/network.ko.md +0 -216
  298. package/docs/network.md +0 -218
  299. package/docs/network.zh-CN.md +0 -218
  300. package/docs/system.ja.md +0 -169
  301. package/docs/system.ko.md +0 -171
  302. package/docs/system.md +0 -173
  303. package/docs/system.zh-CN.md +0 -173
  304. package/docs/window.ja.md +0 -138
  305. package/docs/window.ko.md +0 -140
  306. package/docs/window.md +0 -141
  307. package/docs/window.zh-CN.md +0 -141
  308. package/native/exception-boundary.h +0 -97
  309. package/native/numeric-fields.h +0 -96
  310. package/native/shared-handle.h +0 -69
  311. package/test/tsconfig.json +0 -8
  312. package/tools/configure-lsp.cjs +0 -23
  313. package/tools/lib_downloader.cjs +0 -92
  314. package/tools/lib_downloader_urls.cjs +0 -23
@@ -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 { bytes: Buffer; head: boolean }
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(uri = '/', method = HttpMethod.Get, body: string | Uint8Array = '') {
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 { this.method = uint(method, 4); }
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); this.minor = uint(minor);
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')) fields.set('host', host);
326
- if (!fields.has('from')) fields.set('from', 'user@sfml-dev.org');
327
- if (!fields.has('user-agent')) fields.set('user-agent', 'sfml.js');
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')) fields.set('connection', 'close');
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('Content-Length does not match the request body');
354
+ throw new RangeError(
355
+ 'Content-Length does not match the request body',
356
+ );
337
357
  }
338
- const header = `${methods[this.method]} ${this.uri} ` +
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 { bytes: Buffer.concat([ Buffer.from(header), this.body ]),
343
- head: this.method === HttpMethod.Head };
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(private readonly status: HttpStatus = HttpStatus.ConnectionFailed,
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, private readonly minor = 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 { return this.fields.get(field.toLowerCase()) ?? ''; }
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 { return this.status; }
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 { return this.major; }
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 { return this.minor; }
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 { return this.body.toString('utf8'); }
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 { return Uint8Array.from(this.body); }
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) this.pending = Buffer.concat([ this.pending, data ]);
438
- else this.pending = data;
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) throw new Error('Invalid header');
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(lines.shift()!);
447
- if (!match) throw new Error('Invalid HTTP status line');
448
- this.major = Number(match[1]); this.minor = Number(match[2]);
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) this.field(line);
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; this.fields.clear();
503
+ this.status = 0;
504
+ this.fields.clear();
454
505
  return this.feed(Buffer.alloc(0), ended);
455
506
  }
456
- this.chunked = this.fields.get('transfer-encoding')?.toLowerCase()
457
- .split(',')
458
- .map(value => value.trim())
459
- .includes('chunked') ?? false;
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)) throw new Error('Invalid Content-Length');
516
+ if (!/^\d+$/.test(length)) {
517
+ throw new Error('Invalid Content-Length');
518
+ }
463
519
  this.remaining = Number(length);
464
- if (!Number.isSafeInteger(this.remaining) || this.remaining > 67108864) {
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 (this.head || this.status === 204 || this.status === 304 ||
469
- this.status === 101) this.done = true;
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) break;
476
- if (end === 0) { this.done = true; break; }
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) break;
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) break;
492
- const text = this.pending.toString('latin1', 0, end).split(';')[0];
493
- if (!/^[0-9a-f]+$/i.test(text)) throw new Error('Invalid chunk length');
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) { this.trailers = true; continue; }
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(this.remaining ?? Infinity, this.pending.length);
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) this.done = true;
597
+ if (!this.remaining) {
598
+ this.done = true;
599
+ }
514
600
  }
515
601
  }
516
602
  }
517
- if (this.remaining === 0 && !this.chunked) this.done = true;
518
- if (this.pending.length > 65536) throw new Error('HTTP framing exceeds limit');
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 ? new HttpResponse(this.status, this.fields,
526
- Buffer.concat(this.chunks, this.count), this.major, this.minor) : undefined;
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) throw new Error('HTTP body exceeds 64 MiB');
530
- if (count) this.chunks.push(this.pending.subarray(0, count));
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) throw new Error('Invalid HTTP header field');
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 (name === 'content-length' && this.fields.has(name) &&
540
- this.fields.get(name) !== value) throw new Error('Conflicting lengths');
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) this.setHost(host, port, type);
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) uint(type, 1);
597
- if (typeof host !== 'string') throw new TypeError('Expected a hostname');
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(host.includes('://') ? host : `http://${host}`);
600
- if (![ 'http:', 'https:' ].includes(parsed.protocol)) return false;
601
- if (port) parsed.port = String(port);
602
- this.host = parsed; this.family = type;
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 { return false; }
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(request: HttpRequest, timeout: NetworkTime = Time.Zero,
635
- verifyServer = true, signal?: AbortSignal): Promise<HttpResponse> {
636
- if (!(request instanceof HttpRequest)) throw new TypeError('Expected Request');
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) return new HttpResponse();
768
+ if (!host || signal?.aborted) {
769
+ return new HttpResponse();
770
+ }
640
771
  const prepared = request[prepareRequest](host.host);
641
- const options = { host: host.hostname.replace(/^\[|\]$/g, ''),
772
+ const options = {
773
+ host: host.hostname.replace(/^\[|\]$/g, ''),
642
774
  port: Number(host.port) || (host.protocol === 'https:' ? 443 : 80),
643
- family: this.family === undefined ? 0 : this.family === IpAddressType.IpV4 ? 4 : 6 };
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) return;
788
+ if (finished) {
789
+ return;
790
+ }
651
791
  finished = true;
652
- if (timer) clearTimeout(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() { finish(); }
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) finish(response);
662
- } catch { finish(new HttpResponse(HttpStatus.InvalidResponse)); }
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, () => socket.write(prepared.bytes));
671
- } else socket = connectTcp(options, () => socket.write(prepared.bytes));
672
- socket.on('data', receive)
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) timer = setTimeout(abort, milliseconds);
830
+ if (milliseconds) {
831
+ timer = setTimeout(abort, milliseconds);
832
+ }
678
833
  });
679
834
  }
680
835
  }