@canton-network/core-rpc-transport 1.17.0 → 1.19.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.
- package/dist/index.cjs +15 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -11
- package/dist/index.js.map +1 -1
- package/package.json +6 -6
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
let _canton_network_core_types = require("@canton-network/core-types");
|
|
3
2
|
let _canton_network_core_rpc_errors = require("@canton-network/core-rpc-errors");
|
|
4
|
-
|
|
3
|
+
let _canton_network_core_types = require("@canton-network/core-types");
|
|
4
|
+
// \0@oxc-project+runtime@0.158.0/helpers/esm/typeof.js
|
|
5
5
|
function _typeof(o) {
|
|
6
6
|
"@babel/helpers - typeof";
|
|
7
7
|
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
@@ -10,23 +10,23 @@ function _typeof(o) {
|
|
|
10
10
|
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
|
|
11
11
|
}, _typeof(o);
|
|
12
12
|
}
|
|
13
|
-
// \0@oxc-project+runtime@0.
|
|
14
|
-
function toPrimitive(t,
|
|
13
|
+
// \0@oxc-project+runtime@0.158.0/helpers/esm/toPrimitive.js
|
|
14
|
+
function toPrimitive(t, e) {
|
|
15
15
|
if ("object" != _typeof(t) || !t) return t;
|
|
16
|
-
var
|
|
17
|
-
if (void 0 !==
|
|
18
|
-
var i =
|
|
16
|
+
var r;
|
|
17
|
+
if ("undefined" != typeof Symbol && void 0 !== (r = t[Symbol.toPrimitive])) {
|
|
18
|
+
var i = r.call(t, e || "default");
|
|
19
19
|
if ("object" != _typeof(i)) return i;
|
|
20
20
|
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
21
21
|
}
|
|
22
|
-
return ("string" ===
|
|
22
|
+
return ("string" === e ? String : Number)(t);
|
|
23
23
|
}
|
|
24
|
-
// \0@oxc-project+runtime@0.
|
|
24
|
+
// \0@oxc-project+runtime@0.158.0/helpers/esm/toPropertyKey.js
|
|
25
25
|
function toPropertyKey(t) {
|
|
26
26
|
var i = toPrimitive(t, "string");
|
|
27
27
|
return "symbol" == _typeof(i) ? i : i + "";
|
|
28
28
|
}
|
|
29
|
-
// \0@oxc-project+runtime@0.
|
|
29
|
+
// \0@oxc-project+runtime@0.158.0/helpers/esm/defineProperty.js
|
|
30
30
|
function _defineProperty(e, r, t) {
|
|
31
31
|
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
32
32
|
value: t,
|
|
@@ -160,7 +160,11 @@ var HttpTransport = class {
|
|
|
160
160
|
},
|
|
161
161
|
body: JSON.stringify(request)
|
|
162
162
|
}).catch((error) => {
|
|
163
|
-
|
|
163
|
+
const { message, data } = (0, _canton_network_core_rpc_errors.toJsonRpcError)(error);
|
|
164
|
+
throw _canton_network_core_rpc_errors.rpcErrors.resourceUnavailable({
|
|
165
|
+
message,
|
|
166
|
+
data
|
|
167
|
+
});
|
|
164
168
|
});
|
|
165
169
|
if (!response.ok) return this.handleErrorResponse(response);
|
|
166
170
|
const json = await response.json();
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["isSpliceMessageEvent","WalletEvent","toJsonRpcError","ErrorResponse","rpcErrors","providerErrors","SuccessResponse"],"sources":["../src/index.ts"],"sourcesContent":["// Copyright (c) 2025-2026 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.\n// SPDX-License-Identifier: Apache-2.0\n\nimport {\n type RequestPayload,\n type ResponsePayload,\n type JsonRpcRequest,\n type SpliceMessage,\n WalletEvent,\n isSpliceMessageEvent,\n SuccessResponse,\n ErrorResponse,\n type JsonRpcResponse,\n} from '@canton-network/core-types'\nimport {\n providerErrors,\n rpcErrors,\n toJsonRpcError,\n} from '@canton-network/core-rpc-errors'\n\nexport const jsonRpcRequest = (\n id: string | number | null,\n payload: RequestPayload\n): JsonRpcRequest => {\n return {\n jsonrpc: '2.0',\n id, // id should be set based on the request context\n ...payload,\n }\n}\n\nexport const jsonRpcResponse = (\n id: string | number | null,\n payload: ResponsePayload\n): JsonRpcResponse => {\n return {\n jsonrpc: '2.0',\n id, // id should be set based on the request context\n ...payload,\n }\n}\n\nexport interface RpcTransport {\n /** Rejects with a `JsonRpcError` when the wallet answers with an error. */\n submit: (payload: RequestPayload) => Promise<SuccessResponse>\n}\n\n/**\n * Handler for wallet-pushed JSON-RPC notifications (CIP-103 events such as\n * txChanged / accountsChanged / statusChanged / connected).\n */\nexport type NotificationHandler = (method: string, params?: unknown) => void\n\nexport type WindowTransportOptions = {\n /**\n * Optional routing key for browser-extension messaging. When set, extensions\n * should ignore messages that do not match their own identifier.\n */\n target?: string | undefined\n}\n\nexport class WindowTransport implements RpcTransport {\n private notificationHandlers = new Set<NotificationHandler>()\n private notificationListener: ((event: MessageEvent) => void) | undefined\n\n constructor(\n private win: Window,\n private options: WindowTransportOptions = {}\n ) {}\n\n /**\n * Subscribe to wallet-pushed JSON-RPC notifications: SPLICE_WALLET_REQUEST\n * frames with no `id` (JSON-RPC 2.0 §4.1). The window listener must be\n * always-on for the transport's lifetime — CIP-103 events arrive between\n * requests, where a per-request listener can never observe them.\n */\n onNotification = (handler: NotificationHandler): (() => void) => {\n this.notificationHandlers.add(handler)\n if (!this.notificationListener) {\n this.notificationListener = (event: MessageEvent) => {\n if (\n !isSpliceMessageEvent(event) ||\n event.data.type !== WalletEvent.SPLICE_WALLET_REQUEST\n ) {\n return\n }\n // Requests submitted by this transport always carry a uuid id,\n // so an id-less request can only be a wallet-originated\n // notification — this also keeps the dApp's own outgoing\n // frames (posted to the same window) out of the event path.\n if (event.data.request.id != null) {\n return\n }\n // Same gating as extension detection: a transport constructed\n // from an announced provider's routing key only accepts frames\n // stamped with that key, so multi-wallet pages do not\n // cross-deliver events between providers.\n if (\n this.options.target &&\n event.data.target !== this.options.target\n ) {\n return\n }\n const { method, params } = event.data.request\n this.notificationHandlers.forEach((h) => h(method, params))\n }\n this.win.addEventListener('message', this.notificationListener)\n }\n return () => {\n this.notificationHandlers.delete(handler)\n // Drop the DOM listener with the last subscriber so an abandoned\n // transport leaves nothing attached to the window; a later\n // subscribe re-installs it.\n if (\n this.notificationHandlers.size === 0 &&\n this.notificationListener\n ) {\n this.win.removeEventListener(\n 'message',\n this.notificationListener\n )\n this.notificationListener = undefined\n }\n }\n }\n\n submit = async (payload: RequestPayload) => {\n const message: SpliceMessage = {\n request: jsonRpcRequest(crypto.randomUUID(), payload),\n type: WalletEvent.SPLICE_WALLET_REQUEST,\n target: this.options.target,\n }\n\n this.win.postMessage(message, '*')\n\n return new Promise<SuccessResponse>((resolve, reject) => {\n const listener = (event: MessageEvent) => {\n if (\n !isSpliceMessageEvent(event) ||\n event.data.type !== WalletEvent.SPLICE_WALLET_RESPONSE ||\n event.data.response.id !== message.request.id\n ) {\n return\n }\n\n window.removeEventListener('message', listener)\n if ('error' in event.data.response) {\n reject(toJsonRpcError(event.data.response.error))\n } else {\n resolve(event.data.response)\n }\n }\n\n window.addEventListener('message', listener)\n })\n }\n\n submitResponse = (id: string | number | null, payload: ResponsePayload) => {\n const message: SpliceMessage = {\n response: jsonRpcResponse(id, payload),\n type: WalletEvent.SPLICE_WALLET_RESPONSE,\n }\n this.win.postMessage(message, '*')\n }\n}\n\nexport class HttpTransport implements RpcTransport {\n constructor(\n private url: URL,\n private accessToken?: string\n ) {}\n\n protected async handleErrorResponse(response: Response): Promise<never> {\n const body = await response.text()\n\n let parsedBody: unknown\n try {\n parsedBody = JSON.parse(body)\n } catch {\n // not JSON, fall back to the HTTP status below\n }\n const rpcError = ErrorResponse.safeParse(parsedBody)\n if (rpcError.success) {\n throw toJsonRpcError(rpcError.data.error)\n }\n\n const details = {\n message: response.statusText || `HTTP ${response.status}`,\n data: body,\n }\n switch (response.status) {\n case 400:\n throw rpcErrors.invalidRequest(details)\n case 401:\n case 403:\n throw providerErrors.unauthorized(details)\n case 404:\n throw rpcErrors.resourceNotFound(details)\n case 413:\n case 429:\n throw rpcErrors.limitExceeded(details)\n case 408:\n case 502:\n case 503:\n case 504:\n throw rpcErrors.resourceUnavailable(details)\n default:\n throw rpcErrors.internal(details)\n }\n }\n\n async submit(payload: RequestPayload): Promise<SuccessResponse> {\n const request: JsonRpcRequest = {\n jsonrpc: '2.0',\n method: payload.method,\n params: payload.params,\n id: crypto.randomUUID(),\n }\n\n const header = this.accessToken\n ? { Authorization: `Bearer ${this.accessToken}` }\n : undefined\n\n const response = await fetch(this.url.href, {\n method: 'POST',\n headers: {\n ...header,\n Accept: 'application/json',\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify(request),\n }).catch((error: unknown) => {\n throw toJsonRpcError(error)\n })\n\n if (!response.ok) {\n return this.handleErrorResponse(response)\n }\n\n const json = await response.json()\n const rpcError = ErrorResponse.safeParse(json)\n if (rpcError.success) {\n throw toJsonRpcError(rpcError.data.error)\n }\n\n return SuccessResponse.parse(json)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoBA,MAAa,kBACT,IACA,YACiB;CACjB,OAAO;EACH,SAAS;EACT;EACA,GAAG;CACP;AACJ;AAEA,MAAa,mBACT,IACA,YACkB;CAClB,OAAO;EACH,SAAS;EACT;EACA,GAAG;CACP;AACJ;AAqBA,IAAa,kBAAb,MAAqD;CAIjD,YACI,KACA,UAA0C,CAAC,GAC7C;EAFU,KAAA,MAAA;EACA,KAAA,UAAA;EALJ,gBAAA,MAAA,wCAAuB,IAAI,IAAyB,CAAA;EACpD,gBAAA,MAAA,wBAAA,KAAA,CAAA;;;;;;;;;GAaR;IAAkB,YAA+C;IAC7D,KAAK,qBAAqB,IAAI,OAAO;IACrC,IAAI,CAAC,KAAK,sBAAsB;KAC5B,KAAK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M;GACxC,QAAQ;GACR,SAAS;IACL,GAAG;IACH,QAAQ;IACR,gBAAgB;GACpB;GACA,MAAM,KAAK,UAAU,OAAO;EAChC,CAAC,CAAC,CAAC,OAAO,UAAmB;GACzB,OAAA,GAAMF,gCAAAA,eAAAA,CAAe,KAAK;EAC9B,CAAC;EAED,IAAI,CAAC,SAAS,IACV,OAAO,KAAK,oBAAoB,QAAQ;EAG5C,MAAM,OAAO,MAAM,SAAS,KAAK;EACjC,MAAM,WAAWC,2BAAAA,cAAc,UAAU,IAAI;EAC7C,IAAI,SAAS,SACT,OAAA,GAAMD,gCAAAA,eAAAA,CAAe,SAAS,KAAK,KAAK;EAG5C,OAAOI,2BAAAA,gBAAgB,MAAM,IAAI;CACrC;AACJ"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["isSpliceMessageEvent","WalletEvent","toJsonRpcError","ErrorResponse","rpcErrors","providerErrors","SuccessResponse"],"sources":["../src/index.ts"],"sourcesContent":["// Copyright (c) 2025-2026 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.\n// SPDX-License-Identifier: Apache-2.0\n\nimport {\n providerErrors,\n rpcErrors,\n toJsonRpcError,\n} from '@canton-network/core-rpc-errors'\nimport {\n type RequestPayload,\n type ResponsePayload,\n type JsonRpcRequest,\n type SpliceMessage,\n WalletEvent,\n isSpliceMessageEvent,\n SuccessResponse,\n ErrorResponse,\n type JsonRpcResponse,\n} from '@canton-network/core-types'\n\nexport const jsonRpcRequest = (\n id: string | number | null,\n payload: RequestPayload\n): JsonRpcRequest => {\n return {\n jsonrpc: '2.0',\n id, // id should be set based on the request context\n ...payload,\n }\n}\n\nexport const jsonRpcResponse = (\n id: string | number | null,\n payload: ResponsePayload\n): JsonRpcResponse => {\n return {\n jsonrpc: '2.0',\n id, // id should be set based on the request context\n ...payload,\n }\n}\n\nexport interface RpcTransport {\n /** Rejects with a `JsonRpcError` when the wallet answers with an error. */\n submit: (payload: RequestPayload) => Promise<SuccessResponse>\n}\n\n/**\n * Handler for wallet-pushed JSON-RPC notifications (CIP-103 events such as\n * txChanged / accountsChanged / statusChanged / connected).\n */\nexport type NotificationHandler = (method: string, params?: unknown) => void\n\nexport type WindowTransportOptions = {\n /**\n * Optional routing key for browser-extension messaging. When set, extensions\n * should ignore messages that do not match their own identifier.\n */\n target?: string | undefined\n}\n\nexport class WindowTransport implements RpcTransport {\n private notificationHandlers = new Set<NotificationHandler>()\n private notificationListener: ((event: MessageEvent) => void) | undefined\n\n constructor(\n private win: Window,\n private options: WindowTransportOptions = {}\n ) {}\n\n /**\n * Subscribe to wallet-pushed JSON-RPC notifications: SPLICE_WALLET_REQUEST\n * frames with no `id` (JSON-RPC 2.0 §4.1). The window listener must be\n * always-on for the transport's lifetime — CIP-103 events arrive between\n * requests, where a per-request listener can never observe them.\n */\n onNotification = (handler: NotificationHandler): (() => void) => {\n this.notificationHandlers.add(handler)\n if (!this.notificationListener) {\n this.notificationListener = (event: MessageEvent) => {\n if (\n !isSpliceMessageEvent(event) ||\n event.data.type !== WalletEvent.SPLICE_WALLET_REQUEST\n ) {\n return\n }\n // Requests submitted by this transport always carry a uuid id,\n // so an id-less request can only be a wallet-originated\n // notification — this also keeps the dApp's own outgoing\n // frames (posted to the same window) out of the event path.\n if (event.data.request.id != null) {\n return\n }\n // Same gating as extension detection: a transport constructed\n // from an announced provider's routing key only accepts frames\n // stamped with that key, so multi-wallet pages do not\n // cross-deliver events between providers.\n if (\n this.options.target &&\n event.data.target !== this.options.target\n ) {\n return\n }\n const { method, params } = event.data.request\n this.notificationHandlers.forEach((h) => h(method, params))\n }\n this.win.addEventListener('message', this.notificationListener)\n }\n return () => {\n this.notificationHandlers.delete(handler)\n // Drop the DOM listener with the last subscriber so an abandoned\n // transport leaves nothing attached to the window; a later\n // subscribe re-installs it.\n if (\n this.notificationHandlers.size === 0 &&\n this.notificationListener\n ) {\n this.win.removeEventListener(\n 'message',\n this.notificationListener\n )\n this.notificationListener = undefined\n }\n }\n }\n\n submit = async (payload: RequestPayload) => {\n const message: SpliceMessage = {\n request: jsonRpcRequest(crypto.randomUUID(), payload),\n type: WalletEvent.SPLICE_WALLET_REQUEST,\n target: this.options.target,\n }\n\n this.win.postMessage(message, '*')\n\n return new Promise<SuccessResponse>((resolve, reject) => {\n const listener = (event: MessageEvent) => {\n if (\n !isSpliceMessageEvent(event) ||\n event.data.type !== WalletEvent.SPLICE_WALLET_RESPONSE ||\n event.data.response.id !== message.request.id\n ) {\n return\n }\n\n window.removeEventListener('message', listener)\n if ('error' in event.data.response) {\n reject(toJsonRpcError(event.data.response.error))\n } else {\n resolve(event.data.response)\n }\n }\n\n window.addEventListener('message', listener)\n })\n }\n\n submitResponse = (id: string | number | null, payload: ResponsePayload) => {\n const message: SpliceMessage = {\n response: jsonRpcResponse(id, payload),\n type: WalletEvent.SPLICE_WALLET_RESPONSE,\n }\n this.win.postMessage(message, '*')\n }\n}\n\nexport class HttpTransport implements RpcTransport {\n constructor(\n private url: URL,\n private accessToken?: string\n ) {}\n\n protected async handleErrorResponse(response: Response): Promise<never> {\n const body = await response.text()\n\n let parsedBody: unknown\n try {\n parsedBody = JSON.parse(body)\n } catch {\n // not JSON, fall back to the HTTP status below\n }\n const rpcError = ErrorResponse.safeParse(parsedBody)\n if (rpcError.success) {\n throw toJsonRpcError(rpcError.data.error)\n }\n\n const details = {\n message: response.statusText || `HTTP ${response.status}`,\n data: body,\n }\n switch (response.status) {\n case 400:\n throw rpcErrors.invalidRequest(details)\n case 401:\n case 403:\n throw providerErrors.unauthorized(details)\n case 404:\n throw rpcErrors.resourceNotFound(details)\n case 413:\n case 429:\n throw rpcErrors.limitExceeded(details)\n case 408:\n case 502:\n case 503:\n case 504:\n throw rpcErrors.resourceUnavailable(details)\n default:\n throw rpcErrors.internal(details)\n }\n }\n\n async submit(payload: RequestPayload): Promise<SuccessResponse> {\n const request: JsonRpcRequest = {\n jsonrpc: '2.0',\n method: payload.method,\n params: payload.params,\n id: crypto.randomUUID(),\n }\n\n const header = this.accessToken\n ? { Authorization: `Bearer ${this.accessToken}` }\n : undefined\n\n const response = await fetch(this.url.href, {\n method: 'POST',\n headers: {\n ...header,\n Accept: 'application/json',\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify(request),\n }).catch((error: unknown) => {\n // No response (offline, connection refused, CORS), like a 502 or 504\n const { message, data } = toJsonRpcError(error)\n throw rpcErrors.resourceUnavailable({ message, data })\n })\n\n if (!response.ok) {\n return this.handleErrorResponse(response)\n }\n\n const json = await response.json()\n const rpcError = ErrorResponse.safeParse(json)\n if (rpcError.success) {\n throw toJsonRpcError(rpcError.data.error)\n }\n\n return SuccessResponse.parse(json)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoBA,MAAa,kBACT,IACA,YACiB;CACjB,OAAO;EACH,SAAS;EACT;EACA,GAAG;CACP;AACJ;AAEA,MAAa,mBACT,IACA,YACkB;CAClB,OAAO;EACH,SAAS;EACT;EACA,GAAG;CACP;AACJ;AAqBA,IAAa,kBAAb,MAAqD;CAIjD,YACY,KACA,UAAkC,CAAC,GAC7C;EAFU,KAAA,MAAA;EACA,KAAA,UAAA;EALJ,gBAAA,MAAA,wCAAuB,IAAI,IAAyB,CAAA;EACpD,gBAAA,MAAA,wBAAA,KAAA,CAAA;;;;;;;;;GAaR;IAAkB,YAA+C;IAC7D,KAAK,qBAAqB,IAAI,OAAO;IACrC,IAAI,CAAC,KAAK,sBAAsB;KAC5B,KAAK,wBAAwB,UAAwB;MACjD,IACI,EAAA,GAACA,2BAAAA,qBAAAA,CAAqB,KAAK,KAC3B,MAAM,KAAK,SAASC,2BAAAA,YAAY,uBAEhC;MAMJ,IAAI,MAAM,KAAK,QAAQ,MAAM,MACzB;MAMJ,IACI,KAAK,QAAQ,UACb,MAAM,KAAK,WAAW,KAAK,QAAQ,QAEnC;MAEJ,MAAM,EAAE,QAAQ,WAAW,MAAM,KAAK;MACtC,KAAK,qBAAqB,SAAS,MAAM,EAAE,QAAQ,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AAQ;EAG5C,MAAM,OAAO,MAAM,SAAS,KAAK;EACjC,MAAM,WAAWD,2BAAAA,cAAc,UAAU,IAAI;EAC7C,IAAI,SAAS,SACT,OAAA,GAAMD,gCAAAA,eAAAA,CAAe,SAAS,KAAK,KAAK;EAG5C,OAAOI,2BAAAA,gBAAgB,MAAM,IAAI;CACrC;AACJ"}
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,EACH,KAAK,cAAc,EACnB,KAAK,eAAe,EACpB,KAAK,cAAc,EAInB,eAAe,EAEf,KAAK,eAAe,EACvB,MAAM,4BAA4B,CAAA;AAEnC,eAAO,MAAM,cAAc,OACnB,MAAM,GAAG,MAAM,GAAG,IAAI,WACjB,cAAc,KACxB,cAMF,CAAA;AAED,eAAO,MAAM,eAAe,OACpB,MAAM,GAAG,MAAM,GAAG,IAAI,WACjB,eAAe,KACzB,eAMF,CAAA;AAED,MAAM,WAAW,YAAY;IACzB,2EAA2E;IAC3E,MAAM,EAAE,CAAC,OAAO,EAAE,cAAc,KAAK,OAAO,CAAC,eAAe,CAAC,CAAA;CAChE;AAED;;;GAGG;AACH,MAAM,MAAM,mBAAmB,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,KAAK,IAAI,CAAA;AAE5E,MAAM,MAAM,sBAAsB,GAAG;IACjC;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAC9B,CAAA;AAED,qBAAa,eAAgB,YAAW,YAAY;IAK5C,OAAO,CAAC,GAAG;IACX,OAAO,CAAC,OAAO;IALnB,OAAO,CAAC,oBAAoB,CAAiC;IAC7D,OAAO,CAAC,oBAAoB,CAA6C;IAEzE,YACY,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,sBAA2B,EAC5C;IAEJ;;;;;OAKG;IACH,cAAc,YAAa,mBAAmB,KAAG,CAAC,MAAM,IAAI,CAAC,CAgD5D;IAED,MAAM,YAAmB,cAAc;;OA6BtC;IAED,cAAc,OAAQ,MAAM,GAAG,MAAM,GAAG,IAAI,WAAW,eAAe,UAMrE;CACJ;AAED,qBAAa,aAAc,YAAW,YAAY;IAE1C,OAAO,CAAC,GAAG;IACX,OAAO,CAAC,WAAW,CAAC;IAFxB,YACY,GAAG,EAAE,GAAG,EACR,WAAW,CAAC,EAAE,MAAM,YAAA,EAC5B;IAEJ,UAAgB,mBAAmB,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAAC,KAAK,CAAC,CAqCtE;IAEK,MAAM,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,eAAe,CAAC,CAqC9D;CACJ"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { ErrorResponse, SuccessResponse, WalletEvent, isSpliceMessageEvent } from "@canton-network/core-types";
|
|
2
1
|
import { providerErrors, rpcErrors, toJsonRpcError } from "@canton-network/core-rpc-errors";
|
|
3
|
-
|
|
2
|
+
import { ErrorResponse, SuccessResponse, WalletEvent, isSpliceMessageEvent } from "@canton-network/core-types";
|
|
3
|
+
// \0@oxc-project+runtime@0.158.0/helpers/esm/typeof.js
|
|
4
4
|
function _typeof(o) {
|
|
5
5
|
"@babel/helpers - typeof";
|
|
6
6
|
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
@@ -9,23 +9,23 @@ function _typeof(o) {
|
|
|
9
9
|
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
|
|
10
10
|
}, _typeof(o);
|
|
11
11
|
}
|
|
12
|
-
// \0@oxc-project+runtime@0.
|
|
13
|
-
function toPrimitive(t,
|
|
12
|
+
// \0@oxc-project+runtime@0.158.0/helpers/esm/toPrimitive.js
|
|
13
|
+
function toPrimitive(t, e) {
|
|
14
14
|
if ("object" != _typeof(t) || !t) return t;
|
|
15
|
-
var
|
|
16
|
-
if (void 0 !==
|
|
17
|
-
var i =
|
|
15
|
+
var r;
|
|
16
|
+
if ("undefined" != typeof Symbol && void 0 !== (r = t[Symbol.toPrimitive])) {
|
|
17
|
+
var i = r.call(t, e || "default");
|
|
18
18
|
if ("object" != _typeof(i)) return i;
|
|
19
19
|
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
20
20
|
}
|
|
21
|
-
return ("string" ===
|
|
21
|
+
return ("string" === e ? String : Number)(t);
|
|
22
22
|
}
|
|
23
|
-
// \0@oxc-project+runtime@0.
|
|
23
|
+
// \0@oxc-project+runtime@0.158.0/helpers/esm/toPropertyKey.js
|
|
24
24
|
function toPropertyKey(t) {
|
|
25
25
|
var i = toPrimitive(t, "string");
|
|
26
26
|
return "symbol" == _typeof(i) ? i : i + "";
|
|
27
27
|
}
|
|
28
|
-
// \0@oxc-project+runtime@0.
|
|
28
|
+
// \0@oxc-project+runtime@0.158.0/helpers/esm/defineProperty.js
|
|
29
29
|
function _defineProperty(e, r, t) {
|
|
30
30
|
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
31
31
|
value: t,
|
|
@@ -159,7 +159,11 @@ var HttpTransport = class {
|
|
|
159
159
|
},
|
|
160
160
|
body: JSON.stringify(request)
|
|
161
161
|
}).catch((error) => {
|
|
162
|
-
|
|
162
|
+
const { message, data } = toJsonRpcError(error);
|
|
163
|
+
throw rpcErrors.resourceUnavailable({
|
|
164
|
+
message,
|
|
165
|
+
data
|
|
166
|
+
});
|
|
163
167
|
});
|
|
164
168
|
if (!response.ok) return this.handleErrorResponse(response);
|
|
165
169
|
const json = await response.json();
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["// Copyright (c) 2025-2026 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.\n// SPDX-License-Identifier: Apache-2.0\n\nimport {\n type RequestPayload,\n type ResponsePayload,\n type JsonRpcRequest,\n type SpliceMessage,\n WalletEvent,\n isSpliceMessageEvent,\n SuccessResponse,\n ErrorResponse,\n type JsonRpcResponse,\n} from '@canton-network/core-types'\nimport {\n providerErrors,\n rpcErrors,\n toJsonRpcError,\n} from '@canton-network/core-rpc-errors'\n\nexport const jsonRpcRequest = (\n id: string | number | null,\n payload: RequestPayload\n): JsonRpcRequest => {\n return {\n jsonrpc: '2.0',\n id, // id should be set based on the request context\n ...payload,\n }\n}\n\nexport const jsonRpcResponse = (\n id: string | number | null,\n payload: ResponsePayload\n): JsonRpcResponse => {\n return {\n jsonrpc: '2.0',\n id, // id should be set based on the request context\n ...payload,\n }\n}\n\nexport interface RpcTransport {\n /** Rejects with a `JsonRpcError` when the wallet answers with an error. */\n submit: (payload: RequestPayload) => Promise<SuccessResponse>\n}\n\n/**\n * Handler for wallet-pushed JSON-RPC notifications (CIP-103 events such as\n * txChanged / accountsChanged / statusChanged / connected).\n */\nexport type NotificationHandler = (method: string, params?: unknown) => void\n\nexport type WindowTransportOptions = {\n /**\n * Optional routing key for browser-extension messaging. When set, extensions\n * should ignore messages that do not match their own identifier.\n */\n target?: string | undefined\n}\n\nexport class WindowTransport implements RpcTransport {\n private notificationHandlers = new Set<NotificationHandler>()\n private notificationListener: ((event: MessageEvent) => void) | undefined\n\n constructor(\n private win: Window,\n private options: WindowTransportOptions = {}\n ) {}\n\n /**\n * Subscribe to wallet-pushed JSON-RPC notifications: SPLICE_WALLET_REQUEST\n * frames with no `id` (JSON-RPC 2.0 §4.1). The window listener must be\n * always-on for the transport's lifetime — CIP-103 events arrive between\n * requests, where a per-request listener can never observe them.\n */\n onNotification = (handler: NotificationHandler): (() => void) => {\n this.notificationHandlers.add(handler)\n if (!this.notificationListener) {\n this.notificationListener = (event: MessageEvent) => {\n if (\n !isSpliceMessageEvent(event) ||\n event.data.type !== WalletEvent.SPLICE_WALLET_REQUEST\n ) {\n return\n }\n // Requests submitted by this transport always carry a uuid id,\n // so an id-less request can only be a wallet-originated\n // notification — this also keeps the dApp's own outgoing\n // frames (posted to the same window) out of the event path.\n if (event.data.request.id != null) {\n return\n }\n // Same gating as extension detection: a transport constructed\n // from an announced provider's routing key only accepts frames\n // stamped with that key, so multi-wallet pages do not\n // cross-deliver events between providers.\n if (\n this.options.target &&\n event.data.target !== this.options.target\n ) {\n return\n }\n const { method, params } = event.data.request\n this.notificationHandlers.forEach((h) => h(method, params))\n }\n this.win.addEventListener('message', this.notificationListener)\n }\n return () => {\n this.notificationHandlers.delete(handler)\n // Drop the DOM listener with the last subscriber so an abandoned\n // transport leaves nothing attached to the window; a later\n // subscribe re-installs it.\n if (\n this.notificationHandlers.size === 0 &&\n this.notificationListener\n ) {\n this.win.removeEventListener(\n 'message',\n this.notificationListener\n )\n this.notificationListener = undefined\n }\n }\n }\n\n submit = async (payload: RequestPayload) => {\n const message: SpliceMessage = {\n request: jsonRpcRequest(crypto.randomUUID(), payload),\n type: WalletEvent.SPLICE_WALLET_REQUEST,\n target: this.options.target,\n }\n\n this.win.postMessage(message, '*')\n\n return new Promise<SuccessResponse>((resolve, reject) => {\n const listener = (event: MessageEvent) => {\n if (\n !isSpliceMessageEvent(event) ||\n event.data.type !== WalletEvent.SPLICE_WALLET_RESPONSE ||\n event.data.response.id !== message.request.id\n ) {\n return\n }\n\n window.removeEventListener('message', listener)\n if ('error' in event.data.response) {\n reject(toJsonRpcError(event.data.response.error))\n } else {\n resolve(event.data.response)\n }\n }\n\n window.addEventListener('message', listener)\n })\n }\n\n submitResponse = (id: string | number | null, payload: ResponsePayload) => {\n const message: SpliceMessage = {\n response: jsonRpcResponse(id, payload),\n type: WalletEvent.SPLICE_WALLET_RESPONSE,\n }\n this.win.postMessage(message, '*')\n }\n}\n\nexport class HttpTransport implements RpcTransport {\n constructor(\n private url: URL,\n private accessToken?: string\n ) {}\n\n protected async handleErrorResponse(response: Response): Promise<never> {\n const body = await response.text()\n\n let parsedBody: unknown\n try {\n parsedBody = JSON.parse(body)\n } catch {\n // not JSON, fall back to the HTTP status below\n }\n const rpcError = ErrorResponse.safeParse(parsedBody)\n if (rpcError.success) {\n throw toJsonRpcError(rpcError.data.error)\n }\n\n const details = {\n message: response.statusText || `HTTP ${response.status}`,\n data: body,\n }\n switch (response.status) {\n case 400:\n throw rpcErrors.invalidRequest(details)\n case 401:\n case 403:\n throw providerErrors.unauthorized(details)\n case 404:\n throw rpcErrors.resourceNotFound(details)\n case 413:\n case 429:\n throw rpcErrors.limitExceeded(details)\n case 408:\n case 502:\n case 503:\n case 504:\n throw rpcErrors.resourceUnavailable(details)\n default:\n throw rpcErrors.internal(details)\n }\n }\n\n async submit(payload: RequestPayload): Promise<SuccessResponse> {\n const request: JsonRpcRequest = {\n jsonrpc: '2.0',\n method: payload.method,\n params: payload.params,\n id: crypto.randomUUID(),\n }\n\n const header = this.accessToken\n ? { Authorization: `Bearer ${this.accessToken}` }\n : undefined\n\n const response = await fetch(this.url.href, {\n method: 'POST',\n headers: {\n ...header,\n Accept: 'application/json',\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify(request),\n }).catch((error: unknown) => {\n throw toJsonRpcError(error)\n })\n\n if (!response.ok) {\n return this.handleErrorResponse(response)\n }\n\n const json = await response.json()\n const rpcError = ErrorResponse.safeParse(json)\n if (rpcError.success) {\n throw toJsonRpcError(rpcError.data.error)\n }\n\n return SuccessResponse.parse(json)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoBA,MAAa,kBACT,IACA,YACiB;CACjB,OAAO;EACH,SAAS;EACT;EACA,GAAG;CACP;AACJ;AAEA,MAAa,mBACT,IACA,YACkB;CAClB,OAAO;EACH,SAAS;EACT;EACA,GAAG;CACP;AACJ;AAqBA,IAAa,kBAAb,MAAqD;CAIjD,YACI,KACA,UAA0C,CAAC,GAC7C;E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M,WAAW,MAAM,MAAM,KAAK,IAAI,MAAM;GACxC,QAAQ;GACR,SAAS;IACL,GAAG;IACH,QAAQ;IACR,gBAAgB;GACpB;GACA,MAAM,KAAK,UAAU,OAAO;EAChC,CAAC,CAAC,CAAC,OAAO,UAAmB;GACzB,MAAM,eAAe,KAAK;EAC9B,CAAC;EAED,IAAI,CAAC,SAAS,IACV,OAAO,KAAK,oBAAoB,QAAQ;EAG5C,MAAM,OAAO,MAAM,SAAS,KAAK;EACjC,MAAM,WAAW,cAAc,UAAU,IAAI;EAC7C,IAAI,SAAS,SACT,MAAM,eAAe,SAAS,KAAK,KAAK;EAG5C,OAAO,gBAAgB,MAAM,IAAI;CACrC;AACJ"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["// Copyright (c) 2025-2026 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.\n// SPDX-License-Identifier: Apache-2.0\n\nimport {\n providerErrors,\n rpcErrors,\n toJsonRpcError,\n} from '@canton-network/core-rpc-errors'\nimport {\n type RequestPayload,\n type ResponsePayload,\n type JsonRpcRequest,\n type SpliceMessage,\n WalletEvent,\n isSpliceMessageEvent,\n SuccessResponse,\n ErrorResponse,\n type JsonRpcResponse,\n} from '@canton-network/core-types'\n\nexport const jsonRpcRequest = (\n id: string | number | null,\n payload: RequestPayload\n): JsonRpcRequest => {\n return {\n jsonrpc: '2.0',\n id, // id should be set based on the request context\n ...payload,\n }\n}\n\nexport const jsonRpcResponse = (\n id: string | number | null,\n payload: ResponsePayload\n): JsonRpcResponse => {\n return {\n jsonrpc: '2.0',\n id, // id should be set based on the request context\n ...payload,\n }\n}\n\nexport interface RpcTransport {\n /** Rejects with a `JsonRpcError` when the wallet answers with an error. */\n submit: (payload: RequestPayload) => Promise<SuccessResponse>\n}\n\n/**\n * Handler for wallet-pushed JSON-RPC notifications (CIP-103 events such as\n * txChanged / accountsChanged / statusChanged / connected).\n */\nexport type NotificationHandler = (method: string, params?: unknown) => void\n\nexport type WindowTransportOptions = {\n /**\n * Optional routing key for browser-extension messaging. When set, extensions\n * should ignore messages that do not match their own identifier.\n */\n target?: string | undefined\n}\n\nexport class WindowTransport implements RpcTransport {\n private notificationHandlers = new Set<NotificationHandler>()\n private notificationListener: ((event: MessageEvent) => void) | undefined\n\n constructor(\n private win: Window,\n private options: WindowTransportOptions = {}\n ) {}\n\n /**\n * Subscribe to wallet-pushed JSON-RPC notifications: SPLICE_WALLET_REQUEST\n * frames with no `id` (JSON-RPC 2.0 §4.1). The window listener must be\n * always-on for the transport's lifetime — CIP-103 events arrive between\n * requests, where a per-request listener can never observe them.\n */\n onNotification = (handler: NotificationHandler): (() => void) => {\n this.notificationHandlers.add(handler)\n if (!this.notificationListener) {\n this.notificationListener = (event: MessageEvent) => {\n if (\n !isSpliceMessageEvent(event) ||\n event.data.type !== WalletEvent.SPLICE_WALLET_REQUEST\n ) {\n return\n }\n // Requests submitted by this transport always carry a uuid id,\n // so an id-less request can only be a wallet-originated\n // notification — this also keeps the dApp's own outgoing\n // frames (posted to the same window) out of the event path.\n if (event.data.request.id != null) {\n return\n }\n // Same gating as extension detection: a transport constructed\n // from an announced provider's routing key only accepts frames\n // stamped with that key, so multi-wallet pages do not\n // cross-deliver events between providers.\n if (\n this.options.target &&\n event.data.target !== this.options.target\n ) {\n return\n }\n const { method, params } = event.data.request\n this.notificationHandlers.forEach((h) => h(method, params))\n }\n this.win.addEventListener('message', this.notificationListener)\n }\n return () => {\n this.notificationHandlers.delete(handler)\n // Drop the DOM listener with the last subscriber so an abandoned\n // transport leaves nothing attached to the window; a later\n // subscribe re-installs it.\n if (\n this.notificationHandlers.size === 0 &&\n this.notificationListener\n ) {\n this.win.removeEventListener(\n 'message',\n this.notificationListener\n )\n this.notificationListener = undefined\n }\n }\n }\n\n submit = async (payload: RequestPayload) => {\n const message: SpliceMessage = {\n request: jsonRpcRequest(crypto.randomUUID(), payload),\n type: WalletEvent.SPLICE_WALLET_REQUEST,\n target: this.options.target,\n }\n\n this.win.postMessage(message, '*')\n\n return new Promise<SuccessResponse>((resolve, reject) => {\n const listener = (event: MessageEvent) => {\n if (\n !isSpliceMessageEvent(event) ||\n event.data.type !== WalletEvent.SPLICE_WALLET_RESPONSE ||\n event.data.response.id !== message.request.id\n ) {\n return\n }\n\n window.removeEventListener('message', listener)\n if ('error' in event.data.response) {\n reject(toJsonRpcError(event.data.response.error))\n } else {\n resolve(event.data.response)\n }\n }\n\n window.addEventListener('message', listener)\n })\n }\n\n submitResponse = (id: string | number | null, payload: ResponsePayload) => {\n const message: SpliceMessage = {\n response: jsonRpcResponse(id, payload),\n type: WalletEvent.SPLICE_WALLET_RESPONSE,\n }\n this.win.postMessage(message, '*')\n }\n}\n\nexport class HttpTransport implements RpcTransport {\n constructor(\n private url: URL,\n private accessToken?: string\n ) {}\n\n protected async handleErrorResponse(response: Response): Promise<never> {\n const body = await response.text()\n\n let parsedBody: unknown\n try {\n parsedBody = JSON.parse(body)\n } catch {\n // not JSON, fall back to the HTTP status below\n }\n const rpcError = ErrorResponse.safeParse(parsedBody)\n if (rpcError.success) {\n throw toJsonRpcError(rpcError.data.error)\n }\n\n const details = {\n message: response.statusText || `HTTP ${response.status}`,\n data: body,\n }\n switch (response.status) {\n case 400:\n throw rpcErrors.invalidRequest(details)\n case 401:\n case 403:\n throw providerErrors.unauthorized(details)\n case 404:\n throw rpcErrors.resourceNotFound(details)\n case 413:\n case 429:\n throw rpcErrors.limitExceeded(details)\n case 408:\n case 502:\n case 503:\n case 504:\n throw rpcErrors.resourceUnavailable(details)\n default:\n throw rpcErrors.internal(details)\n }\n }\n\n async submit(payload: RequestPayload): Promise<SuccessResponse> {\n const request: JsonRpcRequest = {\n jsonrpc: '2.0',\n method: payload.method,\n params: payload.params,\n id: crypto.randomUUID(),\n }\n\n const header = this.accessToken\n ? { Authorization: `Bearer ${this.accessToken}` }\n : undefined\n\n const response = await fetch(this.url.href, {\n method: 'POST',\n headers: {\n ...header,\n Accept: 'application/json',\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify(request),\n }).catch((error: unknown) => {\n // No response (offline, connection refused, CORS), like a 502 or 504\n const { message, data } = toJsonRpcError(error)\n throw rpcErrors.resourceUnavailable({ message, data })\n })\n\n if (!response.ok) {\n return this.handleErrorResponse(response)\n }\n\n const json = await response.json()\n const rpcError = ErrorResponse.safeParse(json)\n if (rpcError.success) {\n throw toJsonRpcError(rpcError.data.error)\n }\n\n return SuccessResponse.parse(json)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoBA,MAAa,kBACT,IACA,YACiB;CACjB,OAAO;EACH,SAAS;EACT;EACA,GAAG;CACP;AACJ;AAEA,MAAa,mBACT,IACA,YACkB;CAClB,OAAO;EACH,SAAS;EACT;EACA,GAAG;CACP;AACJ;AAqBA,IAAa,kBAAb,MAAqD;CAIjD,YACY,KACA,UAAkC,CAAC,GAC7C;EAFU,KAAA,MAAA;EACA,KAAA,UAAA;EALJ,gBAAA,MAAA,wCAAuB,IAAI,IAAyB,CAAA;EACpD,gBAAA,MAAA,wBAAA,KAAA,CAAA;;;;;;;;;GAaR;IAAkB,YAA+C;IAC7D,KAAK,qBAAqB,IAAI,OAAO;IACrC,IAAI,CAAC,KAAK,sBAAsB;KAC5B,KAAK,wBAAwB,UAAwB;MACjD,IACI,CAAC,qBAAqB,KAAK,KAC3B,MAAM,KAAK,SAAS,YAAY,uBAEhC;MAMJ,IAAI,MAAM,KAAK,QAAQ,MAAM,MACzB;MAMJ,IACI,KAAK,QAAQ,UACb,MAAM,KAAK,WAAW,KAAK,QAAQ,QAEnC;MAEJ,MAAM,EAAE,QAAQ,WAAW,MAAM,KAAK;MACtC,KAAK,qBAAqB,SAAS,MAAM,EAAE,QAAQ,MAAM,CAAC;KAC9D;KACA,KAAK,IAAI,iBAAiB,WAAW,KAAK,oBAAoB;IAClE;IACA,aAAa;KACT,KAAK,qBAAqB,OAAO,OAAO;KAIxC,IACI,KAAK,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
4
4
|
"type": "module",
|
|
5
5
|
"description": "RPC transport implementations",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"types": "./dist/index.d.ts",
|
|
12
12
|
"exports": {
|
|
13
13
|
".": {
|
|
14
|
+
"@canton-network/source": "./src/index.ts",
|
|
14
15
|
"types": "./dist/index.d.ts",
|
|
15
16
|
"import": "./dist/index.js",
|
|
16
17
|
"require": "./dist/index.cjs",
|
|
@@ -18,14 +19,14 @@
|
|
|
18
19
|
}
|
|
19
20
|
},
|
|
20
21
|
"dependencies": {
|
|
21
|
-
"@canton-network/core-rpc-errors": "1.
|
|
22
|
-
"@canton-network/core-types": "2.
|
|
22
|
+
"@canton-network/core-rpc-errors": "1.18.0",
|
|
23
|
+
"@canton-network/core-types": "2.5.0"
|
|
23
24
|
},
|
|
24
25
|
"devDependencies": {
|
|
25
26
|
"@vitest/browser-playwright": "^5.0.5",
|
|
26
27
|
"@vitest/coverage-v8": "^5.0.5",
|
|
27
|
-
"playwright": "^1.
|
|
28
|
-
"tsdown": "^0.23.
|
|
28
|
+
"playwright": "^1.65.1",
|
|
29
|
+
"tsdown": "^0.23.7",
|
|
29
30
|
"typescript": "npm:@typescript/typescript6@^6.0.2",
|
|
30
31
|
"vitest": "^5.0.5"
|
|
31
32
|
},
|
|
@@ -46,7 +47,6 @@
|
|
|
46
47
|
},
|
|
47
48
|
"scripts": {
|
|
48
49
|
"build": "tsdown --onSuccess \"tsc -p tsconfig.lib.json\"",
|
|
49
|
-
"dev": "tsdown --watch --onSuccess \"tsc -p tsconfig.lib.json\"",
|
|
50
50
|
"flatpack": "pnpm pack --pack-destination \"$FLATPACK_OUTDIR\"",
|
|
51
51
|
"clean": "rm -rf dist"
|
|
52
52
|
}
|