daiso 1.2.6 → 1.2.7

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.
@@ -5,7 +5,7 @@ import { fetchCgvMovies, fetchCgvTheaters, fetchCgvTimetable, toYyyymmdd, } from
5
5
  import { isCgvUpstreamUnavailableError } from '../services/cgv/errors.js';
6
6
  import { fetchCgvNearbyTheaters, resolveCgvNearestTheater } from '../services/cgv/location.js';
7
7
  import { filterAndSortTimetable } from '../services/cgv/timetable.js';
8
- import { errorResponse, successResponse } from './response.js';
8
+ import { errorResponse, serviceErrorResponse, successResponse } from './response.js';
9
9
  function parseOptionalNumber(value) {
10
10
  if (!value) {
11
11
  return undefined;
@@ -62,7 +62,7 @@ export async function handleCgvFindTheaters(c) {
62
62
  }
63
63
  catch (error) {
64
64
  if (isCgvUpstreamUnavailableError(error)) {
65
- return errorResponse(c, 'CGV_UPSTREAM_UNAVAILABLE', error.message, 503);
65
+ return serviceErrorResponse(c, error, 'theaters');
66
66
  }
67
67
  const message = error instanceof Error ? error.message : '알 수 없는 오류가 발생했습니다.';
68
68
  return errorResponse(c, 'CGV_THEATER_SEARCH_FAILED', message, 500);
@@ -120,7 +120,7 @@ export async function handleCgvSearchMovies(c) {
120
120
  }
121
121
  catch (error) {
122
122
  if (isCgvUpstreamUnavailableError(error)) {
123
- return errorResponse(c, 'CGV_UPSTREAM_UNAVAILABLE', error.message, 503);
123
+ return serviceErrorResponse(c, error, 'movies');
124
124
  }
125
125
  const message = error instanceof Error ? error.message : '알 수 없는 오류가 발생했습니다.';
126
126
  return errorResponse(c, 'CGV_MOVIE_SEARCH_FAILED', message, 500);
@@ -188,7 +188,7 @@ export async function handleCgvGetTimetable(c) {
188
188
  }
189
189
  catch (error) {
190
190
  if (isCgvUpstreamUnavailableError(error)) {
191
- return errorResponse(c, 'CGV_UPSTREAM_UNAVAILABLE', error.message, 503);
191
+ return serviceErrorResponse(c, error, 'timetable');
192
192
  }
193
193
  const message = error instanceof Error ? error.message : '알 수 없는 오류가 발생했습니다.';
194
194
  return errorResponse(c, 'CGV_TIMETABLE_FETCH_FAILED', message, 500);
@@ -1,3 +1,4 @@
1
+ import { isKnownCgvBlock } from './healthFailurePolicy.js';
1
2
  /**
2
3
  * 개별 서비스 헬스 체크 실행기
3
4
  */
@@ -70,10 +71,14 @@ function aggregateStatus(checks) {
70
71
  }
71
72
  return 'ok';
72
73
  }
73
- function shouldDegradeFailedResponse(check, message) {
74
+ function shouldDegradeFailedResponse(check, message, status, body) {
75
+ if (isKnownCgvBlock(check.path, status, body))
76
+ return true;
74
77
  return check.degradedFailurePatterns?.some((pattern) => message.includes(pattern)) ?? false;
75
78
  }
76
- function shouldDegradeCliContractPath(path, message) {
79
+ function shouldDegradeCliContractPath(path, message, status, body) {
80
+ if (isKnownCgvBlock(path, status, body))
81
+ return true;
77
82
  if (path.startsWith('/api/gs25/')) {
78
83
  return GS25_CLOUDFRONT_403_PATTERNS.some((pattern) => message.includes(pattern));
79
84
  }
@@ -131,7 +136,7 @@ async function runCliContractCheck(check, params) {
131
136
  const body = (await response.json().catch(() => ({})));
132
137
  if (!response.ok || !isCliCompatibleEnvelope(path, body)) {
133
138
  const message = body.error?.message || `${path} CLI 계약 응답이 올바르지 않습니다.`;
134
- if (shouldDegradeCliContractPath(path, message)) {
139
+ if (shouldDegradeCliContractPath(path, message, response.status, body)) {
135
140
  degradedMessages.push(`${path}: ${message}`);
136
141
  continue;
137
142
  }
@@ -179,7 +184,11 @@ async function runSingleCheck(check, params) {
179
184
  const response = await params.fetchImpl(buildCheckUrl(params.baseUrl, check, timeoutMs, cacheBustValue), {
180
185
  signal: AbortSignal.timeout(timeoutMs),
181
186
  });
182
- const body = (await response.json().catch(() => ({})));
187
+ const body = (await response.json().catch(() => {
188
+ if (response.ok)
189
+ throw new Error('invalid JSON response');
190
+ return {};
191
+ }));
183
192
  const durationMs = params.now() - startedAt;
184
193
  if (!response.ok || body.success === false) {
185
194
  const message = body.error?.message || `HTTP ${response.status}`;
@@ -187,7 +196,7 @@ async function runSingleCheck(check, params) {
187
196
  id: check.id,
188
197
  service: check.service,
189
198
  target: check.target,
190
- status: shouldDegradeFailedResponse(check, message) ? 'degraded' : 'fail',
199
+ status: shouldDegradeFailedResponse(check, message, response.status, body) ? 'degraded' : 'fail',
191
200
  durationMs,
192
201
  httpStatus: response.status,
193
202
  message,
@@ -0,0 +1,6 @@
1
+ export function isKnownCgvBlock(path, status, body) {
2
+ return (path.startsWith('/api/cgv/') &&
3
+ status === 503 &&
4
+ body.error?.code === 'CGV_UPSTREAM_UNAVAILABLE' &&
5
+ body.diagnostics?.upstreamStatus === 403);
6
+ }
@@ -24,6 +24,8 @@ export function errorResponse(c, code, message, status = 400) {
24
24
  }
25
25
  /** 원문이 제거된 외부 서비스 오류를 실제 상태와 함께 반환합니다. */
26
26
  export function serviceErrorResponse(c, error, operation) {
27
+ if (error.retryAfter !== undefined)
28
+ c.header('Retry-After', String(error.retryAfter));
27
29
  return c.json({
28
30
  success: false,
29
31
  error: { code: error.code, message: error.message },
@@ -8,13 +8,17 @@ export class ServiceError extends Error {
8
8
  status;
9
9
  retryable;
10
10
  upstreamStatus;
11
- constructor(code, message, status, retryable, upstreamStatus) {
11
+ quotaReason;
12
+ retryAfter;
13
+ constructor(code, message, status, retryable, upstreamStatus, quota) {
12
14
  super(message);
13
15
  this.code = code;
14
16
  this.status = status;
15
17
  this.retryable = retryable;
16
18
  this.upstreamStatus = upstreamStatus;
17
19
  this.name = 'ServiceError';
20
+ this.quotaReason = quota?.quotaReason;
21
+ this.retryAfter = quota?.retryAfter;
18
22
  }
19
23
  }
20
24
  const SERVICE_PREFIXES = [
@@ -116,6 +120,8 @@ export function toServiceErrorDiagnostics(error, operation) {
116
120
  operation,
117
121
  }),
118
122
  retryable: error.retryable,
123
+ quotaReason: error.quotaReason,
124
+ retryAfter: error.retryAfter,
119
125
  hint: buildHint(error.retryable),
120
126
  };
121
127
  }
@@ -35,7 +35,7 @@ export async function hashRateLimitIdentity(ip) {
35
35
  const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(ip));
36
36
  return Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, '0')).join('');
37
37
  }
38
- function resolveRateLimitIdentity(request) {
38
+ export function resolveRateLimitIdentity(request) {
39
39
  const ip = request.headers.get('CF-Connecting-IP')?.trim();
40
40
  if (!ip || ip !== CROSS_ZONE_WORKER_CLIENT_IP) {
41
41
  return ip || null;
@@ -1,3 +1,4 @@
1
+ import { resolveRateLimitIdentity } from './dailyRateLimit.js';
1
2
  import { withDiagnostics, diagnosticEvent, diagnosticHeaders, diagnosticOperation, } from '../utils/diagnostics.js';
2
3
  export const diagnosticsMiddleware = async (c, next) => withDiagnostics(async () => {
3
4
  const start = performance.now();
@@ -14,4 +15,4 @@ export const diagnosticsMiddleware = async (c, next) => withDiagnostics(async ()
14
15
  durationMs: performance.now() - start,
15
16
  });
16
17
  }
17
- });
18
+ }, resolveRateLimitIdentity(c.req.raw) || undefined);
@@ -1,13 +1,11 @@
1
1
  /**
2
2
  * CGV 원본 및 대체 전송 경로를 사용할 수 없는 상태
3
3
  */
4
- import { ZYTE_COST_POLICY_MESSAGE } from '../../core/errors.js';
5
- export class CgvUpstreamUnavailableError extends Error {
6
- upstreamStatus;
4
+ import { ServiceError, ZYTE_COST_POLICY_MESSAGE } from '../../core/errors.js';
5
+ export class CgvUpstreamUnavailableError extends ServiceError {
7
6
  constructor(upstreamStatus) {
8
7
  const statusSuffix = upstreamStatus === undefined ? '' : ` (HTTP ${upstreamStatus})`;
9
- super(`CGV 원본 서비스에 연결할 수 없습니다. ${ZYTE_COST_POLICY_MESSAGE}${statusSuffix}`);
10
- this.upstreamStatus = upstreamStatus;
8
+ super('CGV_UPSTREAM_UNAVAILABLE', `CGV 원본 서비스에 연결할 수 없습니다. ${ZYTE_COST_POLICY_MESSAGE}${statusSuffix}`, 503, false, upstreamStatus);
11
9
  this.name = 'CgvUpstreamUnavailableError';
12
10
  }
13
11
  }
@@ -3,7 +3,7 @@ import { ServiceError } from '../../core/errors.js';
3
3
  import { HttpError, UnexpectedHtmlResponseError } from '../../utils/http.js';
4
4
  export function toOliveyoungRelayError(error) {
5
5
  if (error instanceof HttpError) {
6
- return new ServiceError('OLIVEYOUNG_RELAY_HTTP_ERROR', `올리브영 브라우저 릴레이 요청 실패 (HTTP ${error.status})`, [429, 502, 503].includes(error.status) ? error.status : 502, error.status >= 500 || error.status === 408 || error.status === 429, error.status);
6
+ return new ServiceError('OLIVEYOUNG_RELAY_HTTP_ERROR', `올리브영 브라우저 릴레이 요청 실패 (HTTP ${error.status})`, [429, 502, 503].includes(error.status) ? error.status : 502, error.status >= 500 || error.status === 408 || error.status === 429, error.status, error.quota);
7
7
  }
8
8
  if (error instanceof Error && ['AbortError', 'TimeoutError'].includes(error.name)) {
9
9
  return new ServiceError('OLIVEYOUNG_RELAY_TIMEOUT', '올리브영 브라우저 릴레이 요청 시간 초과', 504, true);
@@ -1,8 +1,10 @@
1
- import { diagnosticHeaders } from '../../utils/diagnostics.js';
1
+ import { diagnosticHeaders, relayConsumer } from '../../utils/diagnostics.js';
2
2
  /** 올리브영 무료 직접 요청 및 운영자가 설정한 브라우저 릴레이 전송. */
3
- import { fetchJson } from '../../utils/http.js';
3
+ import { createRelayCooldown, relayCredentialScope } from '../../utils/relayQuota.js';
4
+ import { HttpError, fetchJson } from '../../utils/http.js';
4
5
  import { toOliveyoungRelayError } from './errors.js';
5
6
  import { OLIVEYOUNG_API } from './api.js';
7
+ const cooldown = createRelayCooldown();
6
8
  /** 설정 진단과 실제 요청에 같은 릴레이 URL 규칙을 적용합니다. */
7
9
  export function isValidOliveyoungRelayUrl(value) {
8
10
  if (!value?.trim())
@@ -28,6 +30,8 @@ export async function requestOliveyoung(path, body, options = {}) {
28
30
  Accept: 'application/json',
29
31
  'X-Requested-With': 'XMLHttpRequest',
30
32
  };
33
+ let scope = '';
34
+ let consumer = 'legacy';
31
35
  let url = `${OLIVEYOUNG_API.BASE_URL}${path}`;
32
36
  if (relayUrl) {
33
37
  if (!isValidOliveyoungRelayUrl(relayUrl)) {
@@ -38,6 +42,9 @@ export async function requestOliveyoung(path, body, options = {}) {
38
42
  url = `${relayUrl.replace(/\/$/, '')}/v1/oliveyoung/${path.split('/').pop()}`;
39
43
  Object.assign(headers, diagnosticHeaders());
40
44
  headers.Authorization = `Bearer ${relayToken}`;
45
+ consumer = (await relayConsumer(relayToken)) || 'legacy';
46
+ if (consumer !== 'legacy')
47
+ headers['x-relay-consumer'] = consumer;
41
48
  const { accessClientId, accessClientSecret } = options;
42
49
  if (accessClientId !== undefined || accessClientSecret !== undefined) {
43
50
  if (!accessClientId?.trim() || !accessClientSecret?.trim()) {
@@ -55,6 +62,14 @@ export async function requestOliveyoung(path, body, options = {}) {
55
62
  headers.Referer = `${OLIVEYOUNG_API.BASE_URL}/`;
56
63
  headers['Accept-Language'] = 'ko-KR,ko;q=0.9';
57
64
  }
65
+ if (relayUrl) {
66
+ scope = await relayCredentialScope([relayUrl.replace(/\/$/, ''), relayToken, options.accessClientId, options.accessClientSecret]);
67
+ const quota = cooldown.get(scope, consumer);
68
+ if (quota)
69
+ throw toOliveyoungRelayError(new HttpError(429, '', '', new Headers({
70
+ 'x-relay-quota-reason': quota.quotaReason, 'retry-after': String(quota.retryAfter),
71
+ })));
72
+ }
58
73
  let result;
59
74
  try {
60
75
  result = await fetchJson(url, {
@@ -69,8 +84,11 @@ export async function requestOliveyoung(path, body, options = {}) {
69
84
  });
70
85
  }
71
86
  catch (error) {
72
- if (relayUrl)
87
+ if (relayUrl) {
88
+ if (error instanceof HttpError && error.quota)
89
+ cooldown.set(scope, consumer, error.quota);
73
90
  throw toOliveyoungRelayError(error);
91
+ }
74
92
  if (error instanceof Error && error.name === 'AbortError') {
75
93
  throw new Error('올리브영 API 요청 시간 초과');
76
94
  }
@@ -1,10 +1,10 @@
1
1
  /** 비밀값과 원문 없이 요청 경계를 연결하는 진단 컨텍스트입니다. */
2
2
  import { AsyncLocalStorage } from 'node:async_hooks';
3
3
  const context = new AsyncLocalStorage();
4
- export function withDiagnostics(work) {
4
+ export function withDiagnostics(work, consumerIdentity) {
5
5
  if (context.getStore())
6
6
  return work();
7
- return context.run({ requestId: crypto.randomUUID(), sampled: Math.random() < 0.01, events: 0 }, work);
7
+ return context.run({ requestId: crypto.randomUUID(), sampled: Math.random() < 0.01, events: 0, consumerIdentity }, work);
8
8
  }
9
9
  export function diagnosticHeaders() {
10
10
  const current = context.getStore();
@@ -74,3 +74,13 @@ export function diagnosticOperation(url) {
74
74
  return `${parts[2]}.${parts[3]}`;
75
75
  return 'other';
76
76
  }
77
+ /** 릴레이 전용 일별 익명 식별자이며 로그나 공개 응답에는 포함하지 않습니다. */
78
+ export async function relayConsumer(token) {
79
+ const identity = context.getStore()?.consumerIdentity;
80
+ if (!identity)
81
+ return undefined;
82
+ const encoder = new TextEncoder();
83
+ const key = await crypto.subtle.importKey('raw', encoder.encode(token), { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']);
84
+ const signature = await crypto.subtle.sign('HMAC', key, encoder.encode(`${Math.floor(Date.now() / 86400000)}:${identity}`));
85
+ return Array.from(new Uint8Array(signature), byte => byte.toString(16).padStart(2, '0')).join('');
86
+ }
@@ -1,9 +1,11 @@
1
+ import { parseRelayQuota } from './relayQuota.js';
1
2
  import { diagnosticEvent } from './diagnostics.js';
2
3
  export class HttpError extends Error {
4
+ quota;
3
5
  status;
4
6
  statusText;
5
7
  bodyText;
6
- constructor(status, statusText, bodyText) {
8
+ constructor(status, statusText, bodyText, headers) {
7
9
  const normalizedBody = bodyText.trim().replace(/\s+/g, ' ').slice(0, 300);
8
10
  const detail = normalizedBody.length > 0 ? ` - ${normalizedBody}` : '';
9
11
  super(`API 요청 실패: ${status} ${statusText}${detail}`);
@@ -11,6 +13,7 @@ export class HttpError extends Error {
11
13
  this.status = status;
12
14
  this.statusText = statusText;
13
15
  this.bodyText = bodyText;
16
+ this.quota = parseRelayQuota(status, headers);
14
17
  }
15
18
  }
16
19
  /** HTML 원문을 노출하지 않고 예상하지 못한 응답 형식을 구분합니다. */
@@ -136,7 +139,7 @@ async function readTextResponse(url, options) {
136
139
  export async function fetchJson(url, options = {}) {
137
140
  const { response, body } = await readTextResponse(url, options);
138
141
  if (!response.ok || (options.expectedStatus !== undefined && response.status !== options.expectedStatus)) {
139
- throw new HttpError(response.status, response.statusText, body);
142
+ throw new HttpError(response.status, response.statusText, body, response.headers);
140
143
  }
141
144
  if (/^\s*(?:<!doctype\s+html\b|<html\b)/i.test(body)) {
142
145
  throw new UnexpectedHtmlResponseError(response.status, response.headers.get('content-type') || '');
@@ -146,7 +149,7 @@ export async function fetchJson(url, options = {}) {
146
149
  export async function fetchText(url, options = {}) {
147
150
  const { response, body } = await readTextResponse(url, options);
148
151
  if (!response.ok) {
149
- throw new HttpError(response.status, response.statusText, body);
152
+ throw new HttpError(response.status, response.statusText, body, response.headers);
150
153
  }
151
154
  return body;
152
155
  }
@@ -0,0 +1,44 @@
1
+ export function parseRelayQuota(status, headers) {
2
+ if (status !== 429 || !headers)
3
+ return undefined;
4
+ const reason = headers.get('x-relay-quota-reason');
5
+ const retry = headers.get('retry-after') || '';
6
+ if (reason !== 'minute' && reason !== 'daily' && reason !== 'consumer')
7
+ return undefined;
8
+ if (!/^[1-9][0-9]{0,4}$/.test(retry) || Number(retry) > 86400)
9
+ return undefined;
10
+ return { quotaReason: reason, retryAfter: Number(retry) };
11
+ }
12
+ /** 만료 후 정리하고 고정 크기에서 새 휴지 기록을 생략합니다. */
13
+ export function createRelayCooldown(now = () => Date.now(), maxEntries = 1024) {
14
+ const entries = new Map();
15
+ const prune = () => {
16
+ for (const [key, entry] of entries)
17
+ if (entry.until <= now())
18
+ entries.delete(key);
19
+ };
20
+ return {
21
+ get(scope, consumer) {
22
+ prune();
23
+ const entry = entries.get(scope) || entries.get(`${scope}:${consumer}`);
24
+ if (!entry)
25
+ return undefined;
26
+ return { quotaReason: entry.quotaReason, retryAfter: Math.ceil((entry.until - now()) / 1000) };
27
+ },
28
+ set(scope, consumer, quota) {
29
+ prune();
30
+ const key = quota.quotaReason === 'consumer' ? `${scope}:${consumer}` : scope;
31
+ if (!entries.has(key) && entries.size >= maxEntries)
32
+ return;
33
+ const until = now() + quota.retryAfter * 1000;
34
+ if ((entries.get(key)?.until || 0) > until)
35
+ return;
36
+ entries.set(key, { quotaReason: quota.quotaReason, until });
37
+ },
38
+ };
39
+ }
40
+ /** 자격증명 원문을 저장하지 않는 릴레이/Access 구성별 키. */
41
+ export async function relayCredentialScope(values) {
42
+ const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(JSON.stringify(values)));
43
+ return Array.from(new Uint8Array(digest), byte => byte.toString(16).padStart(2, '0')).join('');
44
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "daiso",
3
- "version": "1.2.6",
3
+ "version": "1.2.7",
4
4
  "description": "Korean retail and cinema MCP server, CLI, and Codex Skill for Daiso, convenience stores, marts, Olive Young, and theaters",
5
5
  "main": "dist/index.js",
6
6
  "bin": {