@walletpass/pass-js
Version:
Apple Wallet Pass generating and pushing updates from Node.js
161 lines • 6.59 kB
JavaScript
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2017-2026 Konstantin Vyatkin <tino@vtkn.io>
/**
* Class to handle Apple pass localizations
*
* @see {@link @see https://apple.co/2M9LWVu} - String Resources
* @see {@link https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/PassKit_PG/Creating.html#//apple_ref/doc/uid/TP40012195-CH4-SW54}
*/
import { createReadStream, promises as fs } from 'fs';
import { createInterface } from 'readline';
import * as path from 'path';
import { normalizeLocale } from './normalize-locale.js';
/**
* Just as in C, some characters must be prefixed with a backslash before you
* can include them in the string. These characters include double quotation
* marks, the backslash character itself, and special control characters such
* as linefeed (\n) and carriage returns (\r).
*
* Apple's spec uses literal LF (
) for `\n` regardless of host OS — the
* previous implementation substituted `os.EOL` (CRLF on Windows) and silently
* produced different output across platforms, breaking cross-platform
* reproducibility of pass bundles.
*
* @see {@link https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/LoadingResources/Strings/Strings.html#//apple_ref/doc/uid/10000051i-CH6-SW13}
*/
export function escapeString(str) {
return str
.replace(/["\\]/g, '\\$&') // quote and backslash
.replace(/\r\n|\r|\n/g, '\\n'); // all line-ending styles → escaped LF
}
export function unescapeString(str) {
// Use a sentinel to protect escaped backslashes from the \n rewrite.
// Without this, a literal "\\n" (escaped backslash followed by 'n', e.g.
// a Windows path segment) would incorrectly collapse to a newline.
const SENTINEL = '